【问题标题】:SurfaceTexture/Surface mapping with ANativeWindow使用 ANativeWindow 的 SurfaceTexture/Surface 映射
【发布时间】:2025-12-19 19:30:07
【问题描述】:

给定一个 GraphicBufferProducer,我创建一个 Surface,然后检索 ANativeWindow。使用 ANativeWindow_lock 我得到一个指向缓冲区的指针。使用缓冲区,我在缓冲区中执行 memcpy。问题是我在这个缓冲区上绘制的任何内容都被限制在屏幕的 25% 以下。请记住,buffer.width 和 buffer.height 的尺寸非常接近屏幕本身的分辨率。

我的问题是,为什么缓冲区只覆盖屏幕的一小部分?以及如何确保它覆盖大部分(如果不是全部)屏幕?以下是代码供参考:

ANativeWindow_Buffer buffer;
// window is created from a "new Surface(sp<IGraphicBufferProducer>)"
if (ANativeWindow_lock(window, &buffer, NULL) == 0) {
     // For testing purposes just put grey in the buffer
     memcpy(buffer.bits, 0x99,  buffer.width * buffer.height);
     ANativeWindow_unlockAndPost(window);
}

【问题讨论】:

    标签: android c++ android-ndk textures surface


    【解决方案1】:

    我有一个猜测,虽然我从未使用过 ANativeWindow_Buffer。 memcpy 复制一定数量的字节。您的图像每像素多少位?如果该值大于 8,则您没有传输完整的缓冲区。由于每个像素可能有 4 个字节 (AARRGGBB),您可能需要将其乘以 4。

    【讨论】:

    • 你说得对。我应该乘以 4。