Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "AssetAtlasService"
     18 
     19 #include "jni.h"
     20 #include "JNIHelp.h"
     21 
     22 #include <android_view_GraphicBuffer.h>
     23 #include <cutils/log.h>
     24 
     25 #include <GLES2/gl2.h>
     26 #include <GLES2/gl2ext.h>
     27 
     28 #include <EGL/egl.h>
     29 #include <EGL/eglext.h>
     30 
     31 #include <SkCanvas.h>
     32 #include <SkBitmap.h>
     33 
     34 namespace android {
     35 
     36 // ----------------------------------------------------------------------------
     37 // Defines
     38 // ----------------------------------------------------------------------------
     39 
     40 // Defines how long to wait for the GPU when uploading the atlas
     41 // This timeout is defined in nanoseconds (see EGL_KHR_fence_sync extension)
     42 #define FENCE_TIMEOUT 2000000000
     43 
     44 // ----------------------------------------------------------------------------
     45 // JNI Helpers
     46 // ----------------------------------------------------------------------------
     47 
     48 static struct {
     49     jfieldID mFinalizer;
     50     jfieldID mNativeCanvas;
     51 } gCanvasClassInfo;
     52 
     53 static struct {
     54     jfieldID mNativeCanvas;
     55 } gCanvasFinalizerClassInfo;
     56 
     57 #define GET_INT(object, field) \
     58     env->GetIntField(object, field)
     59 
     60 #define SET_INT(object, field, value) \
     61     env->SetIntField(object, field, value)
     62 
     63 // ----------------------------------------------------------------------------
     64 // Canvas management
     65 // ----------------------------------------------------------------------------
     66 
     67 static inline void swapCanvasPtr(JNIEnv* env, jobject canvasObj, SkCanvas* newCanvas) {
     68     jobject canvasFinalizerObj = env->GetObjectField(canvasObj, gCanvasClassInfo.mFinalizer);
     69     SkCanvas* previousCanvas = reinterpret_cast<SkCanvas*>(
     70             GET_INT(canvasObj, gCanvasClassInfo.mNativeCanvas));
     71     SET_INT(canvasObj, gCanvasClassInfo.mNativeCanvas, (int) newCanvas);
     72     SET_INT(canvasFinalizerObj, gCanvasFinalizerClassInfo.mNativeCanvas, (int) newCanvas);
     73     SkSafeUnref(previousCanvas);
     74 }
     75 
     76 static SkBitmap* com_android_server_AssetAtlasService_acquireCanvas(JNIEnv* env, jobject,
     77         jobject canvas, jint width, jint height) {
     78 
     79     SkBitmap* bitmap = new SkBitmap;
     80     bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
     81     bitmap->allocPixels();
     82     bitmap->eraseColor(0);
     83 
     84     SkCanvas* nativeCanvas = SkNEW_ARGS(SkCanvas, (*bitmap));
     85     swapCanvasPtr(env, canvas, nativeCanvas);
     86 
     87     return bitmap;
     88 }
     89 
     90 static void com_android_server_AssetAtlasService_releaseCanvas(JNIEnv* env, jobject,
     91         jobject canvas, SkBitmap* bitmap) {
     92 
     93     SkCanvas* nativeCanvas = SkNEW(SkCanvas);
     94     swapCanvasPtr(env, canvas, nativeCanvas);
     95 
     96     delete bitmap;
     97 }
     98 
     99 #define CLEANUP_GL_AND_RETURN(result) \
    100     if (fence != EGL_NO_SYNC_KHR) eglDestroySyncKHR(display, fence); \
    101     if (image) eglDestroyImageKHR(display, image); \
    102     if (texture) glDeleteTextures(1, &texture); \
    103     if (surface != EGL_NO_SURFACE) eglDestroySurface(display, surface); \
    104     if (context != EGL_NO_CONTEXT) eglDestroyContext(display, context); \
    105     eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); \
    106     eglReleaseThread(); \
    107     eglTerminate(display); \
    108     return result;
    109 
    110 static jboolean com_android_server_AssetAtlasService_upload(JNIEnv* env, jobject,
    111         jobject graphicBuffer, SkBitmap* bitmap) {
    112 
    113     // The goal of this method is to copy the bitmap into the GraphicBuffer
    114     // using the GPU to swizzle the texture content
    115     sp<GraphicBuffer> buffer(graphicBufferForJavaObject(env, graphicBuffer));
    116 
    117     if (buffer != NULL) {
    118         EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    119         if (display == EGL_NO_DISPLAY) return false;
    120 
    121         EGLint major;
    122         EGLint minor;
    123         if (!eglInitialize(display, &major, &minor)) {
    124             ALOGW("Could not initialize EGL");
    125             return false;
    126         }
    127 
    128         // We're going to use a 1x1 pbuffer surface later on
    129         // The configuration doesn't really matter for what we're trying to do
    130         EGLint configAttrs[] = {
    131                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
    132                 EGL_RED_SIZE, 8,
    133                 EGL_GREEN_SIZE, 8,
    134                 EGL_BLUE_SIZE, 8,
    135                 EGL_ALPHA_SIZE, 0,
    136                 EGL_DEPTH_SIZE, 0,
    137                 EGL_STENCIL_SIZE, 0,
    138                 EGL_NONE
    139         };
    140         EGLConfig configs[1];
    141         EGLint configCount;
    142         if (!eglChooseConfig(display, configAttrs, configs, 1, &configCount)) {
    143             ALOGW("Could not select EGL configuration");
    144             eglReleaseThread();
    145             eglTerminate(display);
    146             return false;
    147         }
    148         if (configCount <= 0) {
    149             ALOGW("Could not find EGL configuration");
    150             eglReleaseThread();
    151             eglTerminate(display);
    152             return false;
    153         }
    154 
    155         // These objects are initialized below but the default "null"
    156         // values are used to cleanup properly at any point in the
    157         // initialization sequence
    158         GLuint texture = 0;
    159         EGLImageKHR image = EGL_NO_IMAGE_KHR;
    160         EGLSurface surface = EGL_NO_SURFACE;
    161         EGLSyncKHR fence = EGL_NO_SYNC_KHR;
    162 
    163         EGLint attrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
    164         EGLContext context = eglCreateContext(display, configs[0], EGL_NO_CONTEXT, attrs);
    165         if (context == EGL_NO_CONTEXT) {
    166             ALOGW("Could not create EGL context");
    167             CLEANUP_GL_AND_RETURN(false);
    168         }
    169 
    170         // Create the 1x1 pbuffer
    171         EGLint surfaceAttrs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
    172         surface = eglCreatePbufferSurface(display, configs[0], surfaceAttrs);
    173         if (surface == EGL_NO_SURFACE) {
    174             ALOGW("Could not create EGL surface");
    175             CLEANUP_GL_AND_RETURN(false);
    176         }
    177 
    178         if (!eglMakeCurrent(display, surface, surface, context)) {
    179             ALOGW("Could not change current EGL context");
    180             CLEANUP_GL_AND_RETURN(false);
    181         }
    182 
    183         // We use an EGLImage to access the content of the GraphicBuffer
    184         // The EGL image is later bound to a 2D texture
    185         EGLClientBuffer clientBuffer = (EGLClientBuffer) buffer->getNativeBuffer();
    186         EGLint imageAttrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
    187         image = eglCreateImageKHR(display, EGL_NO_CONTEXT,
    188                 EGL_NATIVE_BUFFER_ANDROID, clientBuffer, imageAttrs);
    189         if (image == EGL_NO_IMAGE_KHR) {
    190             ALOGW("Could not create EGL image");
    191             CLEANUP_GL_AND_RETURN(false);
    192         }
    193 
    194         glGenTextures(1, &texture);
    195         glBindTexture(GL_TEXTURE_2D, texture);
    196         glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
    197         if (glGetError() != GL_NO_ERROR) {
    198             ALOGW("Could not create/bind texture");
    199             CLEANUP_GL_AND_RETURN(false);
    200         }
    201 
    202         // Upload the content of the bitmap in the GraphicBuffer
    203         glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
    204         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap->width(), bitmap->height(),
    205                 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
    206         if (glGetError() != GL_NO_ERROR) {
    207             ALOGW("Could not upload to texture");
    208             CLEANUP_GL_AND_RETURN(false);
    209         }
    210 
    211         // The fence is used to wait for the texture upload to finish
    212         // properly. We cannot rely on glFlush() and glFinish() as
    213         // some drivers completely ignore these API calls
    214         fence = eglCreateSyncKHR(display, EGL_SYNC_FENCE_KHR, NULL);
    215         if (fence == EGL_NO_SYNC_KHR) {
    216             ALOGW("Could not create sync fence %#x", eglGetError());
    217             CLEANUP_GL_AND_RETURN(false);
    218         }
    219 
    220         // The flag EGL_SYNC_FLUSH_COMMANDS_BIT_KHR will trigger a
    221         // pipeline flush (similar to what a glFlush() would do.)
    222         EGLint waitStatus = eglClientWaitSyncKHR(display, fence,
    223                 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, FENCE_TIMEOUT);
    224         if (waitStatus != EGL_CONDITION_SATISFIED_KHR) {
    225             ALOGW("Failed to wait for the fence %#x", eglGetError());
    226             CLEANUP_GL_AND_RETURN(false);
    227         }
    228 
    229         CLEANUP_GL_AND_RETURN(true);
    230     }
    231 
    232     return false;
    233 }
    234 
    235 // ----------------------------------------------------------------------------
    236 // JNI Glue
    237 // ----------------------------------------------------------------------------
    238 
    239 #define FIND_CLASS(var, className) \
    240         var = env->FindClass(className); \
    241         LOG_FATAL_IF(! var, "Unable to find class " className);
    242 
    243 #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
    244         var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
    245         LOG_FATAL_IF(! var, "Unable to find field " fieldName);
    246 
    247 const char* const kClassPathName = "com/android/server/AssetAtlasService";
    248 
    249 static JNINativeMethod gMethods[] = {
    250     { "nAcquireAtlasCanvas", "(Landroid/graphics/Canvas;II)I",
    251             (void*) com_android_server_AssetAtlasService_acquireCanvas },
    252     { "nReleaseAtlasCanvas", "(Landroid/graphics/Canvas;I)V",
    253             (void*) com_android_server_AssetAtlasService_releaseCanvas },
    254     { "nUploadAtlas", "(Landroid/view/GraphicBuffer;I)Z",
    255             (void*) com_android_server_AssetAtlasService_upload },
    256 };
    257 
    258 int register_android_server_AssetAtlasService(JNIEnv* env) {
    259     jclass clazz;
    260 
    261     FIND_CLASS(clazz, "android/graphics/Canvas");
    262     GET_FIELD_ID(gCanvasClassInfo.mFinalizer, clazz, "mFinalizer",
    263             "Landroid/graphics/Canvas$CanvasFinalizer;");
    264     GET_FIELD_ID(gCanvasClassInfo.mNativeCanvas, clazz, "mNativeCanvas", "I");
    265 
    266     FIND_CLASS(clazz, "android/graphics/Canvas$CanvasFinalizer");
    267     GET_FIELD_ID(gCanvasFinalizerClassInfo.mNativeCanvas, clazz, "mNativeCanvas", "I");
    268 
    269     return jniRegisterNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
    270 }
    271 
    272 };
    273