Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can' get rendering to work(android,SurfaceView,ndk,ANativeWindow)

I searched the whole day and can still not figure out, what I am missing. All Examples I found are either incomplete (only not connected snippets) or overcomplete(cannt see what is really part if the principle)

I have an Activity that has a View that extends SurfaceView that should be filled using a native method. It is currently implemented by a memset(..,0,..) but my View is white although all calls seem fine.

MyView:

public class MyView extends SurfaceView implements SurfaceHolder.Callback
{
  public MyView(Context context)
  {
    super(context);
    SurfaceHolder sh = getHolder();
    sh.addCallback(this);
  }

  // protected void onDraw(Canvas canvas) // works to make the view red
  //   { canvas.drawColor(Color.RED);} 

  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
  {
    nativeRender(holder.getSurface(), width, height);
  }

  public void surfaceCreated(SurfaceHolder holder)
  {}

  public void surfaceDestroyed(SurfaceHolder holder)
  {}

  private native void nativeRender(Object surface, int width, int height);
}

Native method:

void ...nativeRender(JNIEnv* env, jobject myView, jobject surface, jint width, jint height)
{
   ANativeWindow* pWindow(ANativeWindow_fromSurface(env, surface));
   ANativeWindow_setBuffersGeometry(pWindow, width,height,WINDOW_FORMAT_RGBX_8888);

   ANativeWindow_Buffer buffer;

   if (ANativeWindow_lock(pWindow, &buffer, NULL) == 0) 
   {
       memset(buffer.bits, 0, buffer.stride*buffer.height*4);
       ANativeWindow_unlockAndPost(pWindow);
   }
   ANativeWindow_release(pWindow);
}

Things I checked:

  • return values of all ANativeWindow_* are 0.
  • buffer size/format is as expected

Things I tried:

  • calling nativeRender() from onDraw()
  • calling nativeRender() repeatetly (Handler(mainLooper(), postDelayed())
  • keeping ANativeWindow* as static variable (over multiple calls of the above)
  • same without the ANativeWindow_release(pWindow);
  • WINDOW_FORMAT_RGBX_8888 vs WINDOW_FORMAT_RGBA_8888
  • fill with patterns

So it looks to me that I fill a buffer of the correct dimensions that is never shown. Probably I' missing something obvious because nobody else seems to have problems in that way.

Thanks in Advance Moritz

like image 752
moritz Avatar asked Apr 09 '26 09:04

moritz


1 Answers

Thanks!

fadden's comment was the answer. My code actually called setBackgroundColor() which is obviously not what I wanted...

(Although I would expect that in this context this property would have no meaning or would be what the buffer would be prefilled with...)

like image 81
moritz Avatar answered Apr 11 '26 23:04

moritz