title: android opengl textures loaded in background thread get overwritten
body: <p>i am loading textures in a background thread and then send the Bitmap to the GL thread (via GLSurfaceView.queueEvent(...)) to upload it to the graphics card. to prevent garbage collection after each image load i reuse one Bitmap with power of two dimensions for sending a texture to open gl.</p>
  
  <p>and that is the problem, this bitmap gets overwritten sometimes, as the worker thread and GL thread are fighting each other. as i understand it, it is impossible to acquire a lock in one thread (worker) and unlock it in another thread (GL). so i came with a solution like this (my thread is a HandlerThread):</p>
  
  <pre><code>public boolean handleMessage(Message msg) {
      switch(msg.what) {
          case MESSAGE_FROM_MAIN_THREAD:
              m_Lock.lock();
              loadTextureFromAssets(msg.obj.toString(), msg.arg1);
              break;
  
          case MESSAGE_FROM_GL_THREAD:
              m_Lock.unlock();
              break;
      }
  
      return true;
  }
  </code></pre>
  
  <p>were m_Lock is a ReentrantLock. but this still does not work and sometimes a texture is repeated (or partially overwritten by the next one). it looks like the unlocks are in LIFO order...</p>
  
  <p>any ideas except creating a new Bitmap for each loaded texture and causing GC? ...it could be solved for almost all cases with having two Bitmaps for loading textures and switching between them, but this is not 100% correct. i'd like to solve this with proper threads synchronization.</p>