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#include "android/graphics/GraphicsJNI.h"
22
23#include <android_view_GraphicBuffer.h>
24#include <cutils/log.h>
25
26#include <GLES2/gl2.h>
27#include <GLES2/gl2ext.h>
28
29#include <EGL/egl.h>
30#include <EGL/eglext.h>
31
32#include <SkCanvas.h>
33#include <SkBitmap.h>
34
35namespace android {
36
37// ----------------------------------------------------------------------------
38// Defines
39// ----------------------------------------------------------------------------
40
41// Defines how long to wait for the GPU when uploading the atlas
42// This timeout is defined in nanoseconds (see EGL_KHR_fence_sync extension)
43#define FENCE_TIMEOUT 2000000000
44
45// ----------------------------------------------------------------------------
46// JNI Helpers
47// ----------------------------------------------------------------------------
48
49static struct {
50    jmethodID setNativeBitmap;
51} gCanvasClassInfo;
52
53#define INVOKEV(object, method, ...) \
54    env->CallVoidMethod(object, method, __VA_ARGS__)
55
56// ----------------------------------------------------------------------------
57// Canvas management
58// ----------------------------------------------------------------------------
59
60static jlong com_android_server_AssetAtlasService_acquireCanvas(JNIEnv* env, jobject,
61        jobject canvas, jint width, jint height) {
62
63    SkBitmap* bitmap = new SkBitmap;
64    bitmap->allocN32Pixels(width, height);
65    bitmap->eraseColor(0);
66    INVOKEV(canvas, gCanvasClassInfo.setNativeBitmap, reinterpret_cast<jlong>(bitmap));
67
68    return reinterpret_cast<jlong>(bitmap);
69}
70
71static void com_android_server_AssetAtlasService_releaseCanvas(JNIEnv* env, jobject,
72        jobject canvas, jlong bitmapHandle) {
73
74    SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle);
75    INVOKEV(canvas, gCanvasClassInfo.setNativeBitmap, (jlong)0);
76
77    delete bitmap;
78}
79
80#define CLEANUP_GL_AND_RETURN(result) \
81    if (fence != EGL_NO_SYNC_KHR) eglDestroySyncKHR(display, fence); \
82    if (image) eglDestroyImageKHR(display, image); \
83    if (texture) glDeleteTextures(1, &texture); \
84    if (surface != EGL_NO_SURFACE) eglDestroySurface(display, surface); \
85    if (context != EGL_NO_CONTEXT) eglDestroyContext(display, context); \
86    eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); \
87    eglReleaseThread(); \
88    eglTerminate(display); \
89    return result;
90
91static jboolean com_android_server_AssetAtlasService_upload(JNIEnv* env, jobject,
92        jobject graphicBuffer, jlong bitmapHandle) {
93
94    SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle);
95    // The goal of this method is to copy the bitmap into the GraphicBuffer
96    // using the GPU to swizzle the texture content
97    sp<GraphicBuffer> buffer(graphicBufferForJavaObject(env, graphicBuffer));
98
99    if (buffer != NULL) {
100        EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
101        if (display == EGL_NO_DISPLAY) return JNI_FALSE;
102
103        EGLint major;
104        EGLint minor;
105        if (!eglInitialize(display, &major, &minor)) {
106            ALOGW("Could not initialize EGL");
107            return JNI_FALSE;
108        }
109
110        // We're going to use a 1x1 pbuffer surface later on
111        // The configuration doesn't really matter for what we're trying to do
112        EGLint configAttrs[] = {
113                EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
114                EGL_RED_SIZE, 8,
115                EGL_GREEN_SIZE, 8,
116                EGL_BLUE_SIZE, 8,
117                EGL_ALPHA_SIZE, 0,
118                EGL_DEPTH_SIZE, 0,
119                EGL_STENCIL_SIZE, 0,
120                EGL_NONE
121        };
122        EGLConfig configs[1];
123        EGLint configCount;
124        if (!eglChooseConfig(display, configAttrs, configs, 1, &configCount)) {
125            ALOGW("Could not select EGL configuration");
126            eglReleaseThread();
127            eglTerminate(display);
128            return JNI_FALSE;
129        }
130        if (configCount <= 0) {
131            ALOGW("Could not find EGL configuration");
132            eglReleaseThread();
133            eglTerminate(display);
134            return JNI_FALSE;
135        }
136
137        // These objects are initialized below but the default "null"
138        // values are used to cleanup properly at any point in the
139        // initialization sequence
140        GLuint texture = 0;
141        EGLImageKHR image = EGL_NO_IMAGE_KHR;
142        EGLSurface surface = EGL_NO_SURFACE;
143        EGLSyncKHR fence = EGL_NO_SYNC_KHR;
144
145        EGLint attrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
146        EGLContext context = eglCreateContext(display, configs[0], EGL_NO_CONTEXT, attrs);
147        if (context == EGL_NO_CONTEXT) {
148            ALOGW("Could not create EGL context");
149            CLEANUP_GL_AND_RETURN(JNI_FALSE);
150        }
151
152        // Create the 1x1 pbuffer
153        EGLint surfaceAttrs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
154        surface = eglCreatePbufferSurface(display, configs[0], surfaceAttrs);
155        if (surface == EGL_NO_SURFACE) {
156            ALOGW("Could not create EGL surface");
157            CLEANUP_GL_AND_RETURN(JNI_FALSE);
158        }
159
160        if (!eglMakeCurrent(display, surface, surface, context)) {
161            ALOGW("Could not change current EGL context");
162            CLEANUP_GL_AND_RETURN(JNI_FALSE);
163        }
164
165        // We use an EGLImage to access the content of the GraphicBuffer
166        // The EGL image is later bound to a 2D texture
167        EGLClientBuffer clientBuffer = (EGLClientBuffer) buffer->getNativeBuffer();
168        EGLint imageAttrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
169        image = eglCreateImageKHR(display, EGL_NO_CONTEXT,
170                EGL_NATIVE_BUFFER_ANDROID, clientBuffer, imageAttrs);
171        if (image == EGL_NO_IMAGE_KHR) {
172            ALOGW("Could not create EGL image");
173            CLEANUP_GL_AND_RETURN(JNI_FALSE);
174        }
175
176        glGenTextures(1, &texture);
177        glBindTexture(GL_TEXTURE_2D, texture);
178        glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
179        if (glGetError() != GL_NO_ERROR) {
180            ALOGW("Could not create/bind texture");
181            CLEANUP_GL_AND_RETURN(JNI_FALSE);
182        }
183
184        // Upload the content of the bitmap in the GraphicBuffer
185        glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
186        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap->width(), bitmap->height(),
187                GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
188        if (glGetError() != GL_NO_ERROR) {
189            ALOGW("Could not upload to texture");
190            CLEANUP_GL_AND_RETURN(JNI_FALSE);
191        }
192
193        // The fence is used to wait for the texture upload to finish
194        // properly. We cannot rely on glFlush() and glFinish() as
195        // some drivers completely ignore these API calls
196        fence = eglCreateSyncKHR(display, EGL_SYNC_FENCE_KHR, NULL);
197        if (fence == EGL_NO_SYNC_KHR) {
198            ALOGW("Could not create sync fence %#x", eglGetError());
199            CLEANUP_GL_AND_RETURN(JNI_FALSE);
200        }
201
202        // The flag EGL_SYNC_FLUSH_COMMANDS_BIT_KHR will trigger a
203        // pipeline flush (similar to what a glFlush() would do.)
204        EGLint waitStatus = eglClientWaitSyncKHR(display, fence,
205                EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, FENCE_TIMEOUT);
206        if (waitStatus != EGL_CONDITION_SATISFIED_KHR) {
207            ALOGW("Failed to wait for the fence %#x", eglGetError());
208            CLEANUP_GL_AND_RETURN(JNI_FALSE);
209        }
210
211        CLEANUP_GL_AND_RETURN(JNI_TRUE);
212    }
213
214    return JNI_FALSE;
215}
216
217// ----------------------------------------------------------------------------
218// JNI Glue
219// ----------------------------------------------------------------------------
220
221#define FIND_CLASS(var, className) \
222        var = env->FindClass(className); \
223        LOG_FATAL_IF(! var, "Unable to find class " className);
224
225#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
226        var = env->GetMethodID(clazz, methodName, methodDescriptor); \
227        LOG_FATAL_IF(!var, "Unable to find method " methodName);
228
229const char* const kClassPathName = "com/android/server/AssetAtlasService";
230
231static JNINativeMethod gMethods[] = {
232    { "nAcquireAtlasCanvas", "(Landroid/graphics/Canvas;II)J",
233            (void*) com_android_server_AssetAtlasService_acquireCanvas },
234    { "nReleaseAtlasCanvas", "(Landroid/graphics/Canvas;J)V",
235            (void*) com_android_server_AssetAtlasService_releaseCanvas },
236    { "nUploadAtlas", "(Landroid/view/GraphicBuffer;J)Z",
237            (void*) com_android_server_AssetAtlasService_upload },
238};
239
240int register_android_server_AssetAtlasService(JNIEnv* env) {
241    jclass clazz;
242
243    FIND_CLASS(clazz, "android/graphics/Canvas");
244    GET_METHOD_ID(gCanvasClassInfo.setNativeBitmap, clazz, "setNativeBitmap", "(J)V");
245
246    return jniRegisterNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
247}
248
249};
250