2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "AssetAtlasService"
22 #include <android_view_GraphicBuffer.h>
23 #include <cutils/log.h>
25 #include <GLES2/gl2.h>
26 #include <GLES2/gl2ext.h>
29 #include <EGL/eglext.h>
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
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
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
50 jfieldID mNativeCanvas;
54 jfieldID mNativeCanvas;
55 } gCanvasFinalizerClassInfo;
57 #define GET_INT(object, field) \
58 env->GetIntField(object, field)
60 #define SET_INT(object, field, value) \
61 env->SetIntField(object, field, value)
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
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);
76 static SkBitmap* com_android_server_AssetAtlasService_acquireCanvas(JNIEnv* env, jobject,
77 jobject canvas, jint width, jint height) {
79 SkBitmap* bitmap = new SkBitmap;
80 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
81 bitmap->allocPixels();
82 bitmap->eraseColor(0);
84 SkCanvas* nativeCanvas = SkNEW_ARGS(SkCanvas, (*bitmap));
85 swapCanvasPtr(env, canvas, nativeCanvas);
90 static void com_android_server_AssetAtlasService_releaseCanvas(JNIEnv* env, jobject,
91 jobject canvas, SkBitmap* bitmap) {
93 SkCanvas* nativeCanvas = SkNEW(SkCanvas);
94 swapCanvasPtr(env, canvas, nativeCanvas);
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); \
110 static jboolean com_android_server_AssetAtlasService_upload(JNIEnv* env, jobject,
111 jobject graphicBuffer, SkBitmap* bitmap) {
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));
117 if (buffer != NULL) {
118 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
119 if (display == EGL_NO_DISPLAY) return false;
123 if (!eglInitialize(display, &major, &minor)) {
124 ALOGW("Could not initialize EGL");
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,
140 EGLConfig configs[1];
142 if (!eglChooseConfig(display, configAttrs, configs, 1, &configCount)) {
143 ALOGW("Could not select EGL configuration");
145 eglTerminate(display);
148 if (configCount <= 0) {
149 ALOGW("Could not find EGL configuration");
151 eglTerminate(display);
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
159 EGLImageKHR image = EGL_NO_IMAGE_KHR;
160 EGLSurface surface = EGL_NO_SURFACE;
161 EGLSyncKHR fence = EGL_NO_SYNC_KHR;
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);
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);
178 if (!eglMakeCurrent(display, surface, surface, context)) {
179 ALOGW("Could not change current EGL context");
180 CLEANUP_GL_AND_RETURN(false);
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);
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);
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);
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);
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);
229 CLEANUP_GL_AND_RETURN(true);
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 #define FIND_CLASS(var, className) \
240 var = env->FindClass(className); \
241 LOG_FATAL_IF(! var, "Unable to find class " className);
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);
247 const char* const kClassPathName = "com/android/server/AssetAtlasService";
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 },
258 int register_android_server_AssetAtlasService(JNIEnv* env) {
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");
266 FIND_CLASS(clazz, "android/graphics/Canvas$CanvasFinalizer");
267 GET_FIELD_ID(gCanvasFinalizerClassInfo.mNativeCanvas, clazz, "mNativeCanvas", "I");
269 return jniRegisterNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));