[android] SurfaceView compatible snapshot api

[android] #5587 - snapshot API on surface view, create Bitmap with a ByteBuffer

[android] #5587 - write bitmap to external storage

PreMultipliedImage approach

callback implemented

clean up code

introduce bitmap reuse

cleanup

renaming surfaceContent to bitmap
This commit is contained in:
Tobrun Van Nuland
2016-07-27 18:39:32 +02:00
committed by Tobrun
parent 9b871a9a0f
commit 6016b2981e
6 changed files with 105 additions and 16 deletions

View File

@@ -194,6 +194,35 @@ void NativeMapView::render() {
map->render();
if(snapshot){
snapshot = false;
// take snapshot
const unsigned int w = fbWidth;
const unsigned int h = fbHeight;
mbgl::PremultipliedImage image { w, h };
MBGL_CHECK_ERROR(glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, image.data.get()));
const size_t stride = image.stride();
auto tmp = std::make_unique<uint8_t[]>(stride);
uint8_t *rgba = image.data.get();
for (int i = 0, j = h - 1; i < j; i++, j--) {
std::memcpy(tmp.get(), rgba + i * stride, stride);
std::memcpy(rgba + i * stride, rgba + j * stride, stride);
std::memcpy(rgba + j * stride, tmp.get(), stride);
}
// encode and convert to jbytes
std::string string = encodePNG(image);
jbyteArray arr = env->NewByteArray(string.length());
env->SetByteArrayRegion(arr,0,string.length(),(jbyte*)string.c_str());
// invoke Mapview#OnSnapshotReady
env->CallVoidMethod(obj, onSnapshotReadyId, arr);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
}
}
if ((display != EGL_NO_DISPLAY) && (surface != EGL_NO_SURFACE)) {
if (!eglSwapBuffers(display, surface)) {
mbgl::Log::Error(mbgl::Event::OpenGL, "eglSwapBuffers() returned error %d",
@@ -436,6 +465,10 @@ void NativeMapView::destroySurface() {
}
}
void NativeMapView::scheduleTakeSnapshot() {
snapshot = true;
}
// Speed
/*
typedef enum {