title: android: how to Update a SurfaceView from the ndk by updating the Surface buffer?
body: <p>I am working on an image processing project. I have a <code>SurfaceView</code> where I want to show "images" form the jni side.</p>
  
  <p>I followed this <a href="https://vec.io/posts/how-to-render-image-buffer-in-android-ndk-native-code" rel="nofollow">blog</a>  , the <strong>NDK ANativeWindow API</strong> instructions to get the pointer of the buffer and updated from the C side.</p>
  
  <p>I got the code to run but my SurfaceView is not updating (not showing any image...). Also the callback <code>surfaceChanged</code> is not called when the buffer is updated.</p>
  
  <p>Here is what I am doing:</p>
  
  <p><strong><em>JNI SIDE :</em></strong></p>
  
  <pre><code>/*
    * Class:     com_example_myLib
    * Method:    renderImage
    * Signature: (JI)V
    */
  
  JNIEXPORT void JNICALL com_example_myLib_renderImage
    (JNIEnv *mJNIEnv, jobject mjobject, jobject javaSurface) {
  
  #ifdef DEBUG_I
      LOGI("renderImage attempt !");
  #endif
  
  // load an ipl image. code tested and works well with ImageView.
  IplImage *iplimage = loadMyIplImage();
  
  int iplImageWidth = iplimage-&gt;width;
  int iplImageHeitgh = iplimage-&gt;height;
  
  char * javalBmpPointer = malloc(iplimage-&gt;width * iplimage-&gt;height * 4);
  int _javaBmpRowBytes = iplimage-&gt;width * 4;
  
  
  
   // code tested and works well with ImageView.
      copyIplImageToARGBPointer(iplimage, javalBmpPointer, _javaBmpRowBytes,
              iplimage-&gt;width, iplimage-&gt;height);
  #ifdef DEBUG_I
      LOGI("ANativeWindow_fromSurface");
   #endif
  ANativeWindow* window = ANativeWindow_fromSurface(env, javaSurface);
  
  #ifdef DEBUG_I
      LOGI("Got window %p", window);
   #endif
  
   if(window != 0 ){
      ANativeWindow_Buffer buffer;
      if (ANativeWindow_lock(window, &amp;buffer, NULL) == 0) {
          #ifdef DEBUG_I
              LOGI("GANativeWindow_lock %p", buffer);
           #endif
          memcpy(buffer.bits, javalBmpPointer,  iplimage-&gt;width* iplimage-&gt;height* 4); // ARGB_8888
          ANativeWindow_unlockAndPost(window);
      }
  
      ANativeWindow_release(window);
  
   }
  </code></pre>
  
  <p>}</p>
  
  <p><strong><em>java SIDE :</em></strong></p>
  
  <pre><code>// every time that I want to reload the image:
     renderImage(mySurfaceView.getHolder().getSurface());
  </code></pre>
  
  <p>Thanks for your time and help! </p>