Search code examples
androidperformanceandroid-ndknativewindow

Any other method than copying bits to an ANativeWindow buffer?


I'm using native window in android to show a video in a surface view. Here's the part of my code.

ANativeWindow_lock(window, &windowBuffer, NULL);
memcpy(windowBuffer.bits, buffer,  size);
ANativeWindow_unlockAndPost(window);

But the memcpy is taking too much time. Is there any other method without memcpy so that I can directly use the buffer allocated in native code? I want to fill the surface view completely with data in buffer.


Solution

  • Surfaces aren't buffers, they're queues of buffers. Anything headed to the display will be double- or triple-buffered.

    You can generate your output directly into the buffer returned by ANativeWindow_lock(). Because it's (at least) double-buffered, there is no penalty for locking the next buffer right after you unlock the previous one -- you won't prevent the compositor from seeing the just-finished frame by immediately calling lock.

    See the Android graphics architecture doc for more details, particularly this section.