title: Android Bitmap Object as a draw buffer
body: <p><strong>Setup:</strong>
  I have implemented a native (read JNI) mechanism to copy pixels from a <code>Bitmap</code> object, to native memory. This is done by <code>malloc()</code> <code>uint23_t array</code> in native memory and later using <code>memcpy()</code> to copy pixels to/from Bitmap's native pointer. This works well and have been tested. Pixels are successfully saved in native memory from a <code>Bitmap</code> object, and copied back to a <code>Bitmap</code> object, and visible on screen. Its pretty fast in copying, up to order of several milliseconds for fairly large bitmaps. But extremely slow in rendering it.</p>
  
  <p><strong>Intention:</strong>
  The above was done to break free of heap limit on default android <code>Bitmaps</code> (refer to <a href="https://stackoverflow.com/a/1949205/1531054">https://stackoverflow.com/a/1949205/1531054</a>). There would be only 1 Java <code>Bitmap</code> object acting as buffer between native memory and target canvas.</p>
  
  <p>Save a shape:</p>
  
  <ol>
  <li>clear Buffer Bitmap.</li>
  <li>Draw shape on Bitmap.</li>
  <li>Copy pixels to native memory, and save the memory pointer.</li>
  <li>Clear Buffer Bitmap.</li>
  </ol>
  
  <p>So, any number of shapes can be saved to native memory, without running into heap size limits. This works.</p>
  
  <p>Later when need to draw a shape (say in <code>onDraw()</code>):</p>
  
  <ol>
  <li>clear Buffer Bitmap.</li>
  <li>Copy pixels from native memory, to Buffer Bitmap, using the saved memory pointer.</li>
  <li>Draw Buffer Bitmap on <code>canvas</code>.</li>
  <li>Clear Buffer Bitmap.</li>
  <li>Repeat again for next shape.</li>
  </ol>
  
  <p><strong>Problem</strong> When quickly drawing many shapes from memory, The Buffer <code>Bitmap</code> sorts of lags. Basically we're doing </p>
  
  <pre><code>clear bitmap -&gt; load pixels from memory onto it -&gt; draw it on view canvas
  </code></pre>
  
  <p>in Quick succession inside <code>onDraw()</code>, only the latest shape's pixels are drawn onto canvas. It appears as if:</p>
  
  <ol>
  <li>The internal <code>canvas.drawBitmap()</code> is asynchronous and copies pixels off the bitmap later sometimes.</li>
  <li>Android's <code>Bitmaps</code> have some hidden caching mechanism. </li>
  </ol>
  
  <p>Has anyone run into such trouble before ? Or has some insight regarding this ?</p>
  
  <p>I know one can get native skia lib's canvas instance in JNI and draw on it, but this is a non standard way.</p>