title: loading textures in native code and returing texture id to java
body: <p>My previous questions led me to this one, where I need to move some code from Java to native. I load lot of textures (data in RGBA888 is up to 2GiB, no compression is allowed (e.g. 565, ETC...)) on the fly and discard old ones, so during loading I get a lot of garbage collection. I was able to partially move the GC from OpenGL thread, so it does not slow down FPS but at the cost of longer texture load times, because they are paused by the GC.</p>
  
  <p>So I want to remove GC completely by decoding and uploading the images in C. Right now I use a producer-consumer pattern, with one producer and two consumers. For this I use Java's LinkedBlockingDeque and consumers extend the Thread class and take items from the queue in an infinite loop. Items carry information about image's filepath, which I decode in the worker thread and send to GL thread with GLSurfaceView's queueEvent() method.</p>
  
  <p>All the images (PNGs) are in on zip file with no compression and I directly stream them out of it (Android's APK expansion file mechanism). I have lot of C/C++ experiencie but no JNI experience. I am reading Android's JNI examples at the moment.</p>
  
  <p>The question is how to send an InputStream to native code, decode the png there (I will use libpng), and now the tricky part: then send it to the GL thread and upload it as a texture and return the texture id and set ready flag back to Java? Or could I use a java.nio ByteBuffer, which will be filled in the native code and then sent back to Java where I will upload it with a glTexImage2D method which takes a Buffer instead of a Bitmap? Will this java.nio buffer prevent garbage collection or I have to do everything in C?</p>
  
  <p>I am targeting Android 3.2 and newer.</p>