blob: e4f242e32fba85c7bfd98d10a82913dbe6fbc8d9 [file] [log] [blame]
Romain Guy3b748a42013-04-17 18:54:38 -07001/*
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"
Florin Malita5c3d9272014-05-08 10:35:36 -040021#include "android/graphics/GraphicsJNI.h"
Romain Guy3b748a42013-04-17 18:54:38 -070022
23#include <android_view_GraphicBuffer.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070024#include <cutils/log.h>
Romain Guy3b748a42013-04-17 18:54:38 -070025
26#include <GLES2/gl2.h>
27#include <GLES2/gl2ext.h>
28
29#include <EGL/egl.h>
30#include <EGL/eglext.h>
31
Andreas Gampe184e3ed2014-09-29 15:04:06 -070032// Disable warnings for Skia.
33#pragma GCC diagnostic push
34#pragma GCC diagnostic ignored "-Wunused-parameter"
Romain Guy3b748a42013-04-17 18:54:38 -070035#include <SkCanvas.h>
36#include <SkBitmap.h>
Andreas Gampe184e3ed2014-09-29 15:04:06 -070037#pragma GCC diagnostic pop
Romain Guy3b748a42013-04-17 18:54:38 -070038
39namespace android {
40
41// ----------------------------------------------------------------------------
42// Defines
43// ----------------------------------------------------------------------------
44
45// Defines how long to wait for the GPU when uploading the atlas
46// This timeout is defined in nanoseconds (see EGL_KHR_fence_sync extension)
47#define FENCE_TIMEOUT 2000000000
48
49// ----------------------------------------------------------------------------
John Reck9d4efdf2015-04-17 20:45:40 +000050// JNI Helpers
51// ----------------------------------------------------------------------------
52
53static struct {
54 jmethodID setNativeBitmap;
55} gCanvasClassInfo;
56
57#define INVOKEV(object, method, ...) \
58 env->CallVoidMethod(object, method, __VA_ARGS__)
59
60// ----------------------------------------------------------------------------
Romain Guy3b748a42013-04-17 18:54:38 -070061// Canvas management
62// ----------------------------------------------------------------------------
63
John Reck9d4efdf2015-04-17 20:45:40 +000064static jlong com_android_server_AssetAtlasService_acquireCanvas(JNIEnv* env, jobject,
65 jobject canvas, jint width, jint height) {
66
67 SkBitmap* bitmap = new SkBitmap;
68 bitmap->allocN32Pixels(width, height);
69 bitmap->eraseColor(0);
70 INVOKEV(canvas, gCanvasClassInfo.setNativeBitmap, reinterpret_cast<jlong>(bitmap));
71
72 return reinterpret_cast<jlong>(bitmap);
73}
74
75static void com_android_server_AssetAtlasService_releaseCanvas(JNIEnv* env, jobject,
76 jobject canvas, jlong bitmapHandle) {
77
78 SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle);
79 INVOKEV(canvas, gCanvasClassInfo.setNativeBitmap, (jlong)0);
80
81 delete bitmap;
82}
83
Romain Guy3b748a42013-04-17 18:54:38 -070084#define CLEANUP_GL_AND_RETURN(result) \
85 if (fence != EGL_NO_SYNC_KHR) eglDestroySyncKHR(display, fence); \
86 if (image) eglDestroyImageKHR(display, image); \
87 if (texture) glDeleteTextures(1, &texture); \
88 if (surface != EGL_NO_SURFACE) eglDestroySurface(display, surface); \
89 if (context != EGL_NO_CONTEXT) eglDestroyContext(display, context); \
90 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); \
91 eglReleaseThread(); \
92 eglTerminate(display); \
93 return result;
94
95static jboolean com_android_server_AssetAtlasService_upload(JNIEnv* env, jobject,
John Reck9d4efdf2015-04-17 20:45:40 +000096 jobject graphicBuffer, jlong bitmapHandle) {
Romain Guy3b748a42013-04-17 18:54:38 -070097
John Reck9d4efdf2015-04-17 20:45:40 +000098 SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle);
Romain Guy3b748a42013-04-17 18:54:38 -070099 // The goal of this method is to copy the bitmap into the GraphicBuffer
100 // using the GPU to swizzle the texture content
101 sp<GraphicBuffer> buffer(graphicBufferForJavaObject(env, graphicBuffer));
102
103 if (buffer != NULL) {
104 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000105 if (display == EGL_NO_DISPLAY) return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700106
107 EGLint major;
108 EGLint minor;
109 if (!eglInitialize(display, &major, &minor)) {
110 ALOGW("Could not initialize EGL");
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000111 return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700112 }
113
114 // We're going to use a 1x1 pbuffer surface later on
115 // The configuration doesn't really matter for what we're trying to do
116 EGLint configAttrs[] = {
117 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
118 EGL_RED_SIZE, 8,
119 EGL_GREEN_SIZE, 8,
120 EGL_BLUE_SIZE, 8,
121 EGL_ALPHA_SIZE, 0,
122 EGL_DEPTH_SIZE, 0,
123 EGL_STENCIL_SIZE, 0,
124 EGL_NONE
125 };
126 EGLConfig configs[1];
127 EGLint configCount;
128 if (!eglChooseConfig(display, configAttrs, configs, 1, &configCount)) {
129 ALOGW("Could not select EGL configuration");
130 eglReleaseThread();
131 eglTerminate(display);
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000132 return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700133 }
134 if (configCount <= 0) {
135 ALOGW("Could not find EGL configuration");
136 eglReleaseThread();
137 eglTerminate(display);
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000138 return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700139 }
140
141 // These objects are initialized below but the default "null"
142 // values are used to cleanup properly at any point in the
143 // initialization sequence
144 GLuint texture = 0;
145 EGLImageKHR image = EGL_NO_IMAGE_KHR;
146 EGLSurface surface = EGL_NO_SURFACE;
147 EGLSyncKHR fence = EGL_NO_SYNC_KHR;
148
149 EGLint attrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
150 EGLContext context = eglCreateContext(display, configs[0], EGL_NO_CONTEXT, attrs);
151 if (context == EGL_NO_CONTEXT) {
152 ALOGW("Could not create EGL context");
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000153 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700154 }
155
156 // Create the 1x1 pbuffer
157 EGLint surfaceAttrs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
158 surface = eglCreatePbufferSurface(display, configs[0], surfaceAttrs);
159 if (surface == EGL_NO_SURFACE) {
160 ALOGW("Could not create EGL surface");
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000161 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700162 }
163
164 if (!eglMakeCurrent(display, surface, surface, context)) {
165 ALOGW("Could not change current EGL context");
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000166 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700167 }
168
169 // We use an EGLImage to access the content of the GraphicBuffer
170 // The EGL image is later bound to a 2D texture
171 EGLClientBuffer clientBuffer = (EGLClientBuffer) buffer->getNativeBuffer();
172 EGLint imageAttrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
173 image = eglCreateImageKHR(display, EGL_NO_CONTEXT,
174 EGL_NATIVE_BUFFER_ANDROID, clientBuffer, imageAttrs);
175 if (image == EGL_NO_IMAGE_KHR) {
176 ALOGW("Could not create EGL image");
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000177 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700178 }
179
180 glGenTextures(1, &texture);
181 glBindTexture(GL_TEXTURE_2D, texture);
182 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
183 if (glGetError() != GL_NO_ERROR) {
184 ALOGW("Could not create/bind texture");
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000185 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700186 }
187
188 // Upload the content of the bitmap in the GraphicBuffer
John Reck9d4efdf2015-04-17 20:45:40 +0000189 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
190 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap->width(), bitmap->height(),
191 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
Romain Guy3b748a42013-04-17 18:54:38 -0700192 if (glGetError() != GL_NO_ERROR) {
193 ALOGW("Could not upload to texture");
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000194 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700195 }
196
197 // The fence is used to wait for the texture upload to finish
198 // properly. We cannot rely on glFlush() and glFinish() as
199 // some drivers completely ignore these API calls
200 fence = eglCreateSyncKHR(display, EGL_SYNC_FENCE_KHR, NULL);
201 if (fence == EGL_NO_SYNC_KHR) {
202 ALOGW("Could not create sync fence %#x", eglGetError());
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000203 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700204 }
205
206 // The flag EGL_SYNC_FLUSH_COMMANDS_BIT_KHR will trigger a
207 // pipeline flush (similar to what a glFlush() would do.)
208 EGLint waitStatus = eglClientWaitSyncKHR(display, fence,
209 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, FENCE_TIMEOUT);
210 if (waitStatus != EGL_CONDITION_SATISFIED_KHR) {
211 ALOGW("Failed to wait for the fence %#x", eglGetError());
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000212 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700213 }
214
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000215 CLEANUP_GL_AND_RETURN(JNI_TRUE);
Romain Guy3b748a42013-04-17 18:54:38 -0700216 }
217
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000218 return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700219}
220
221// ----------------------------------------------------------------------------
222// JNI Glue
223// ----------------------------------------------------------------------------
224
225#define FIND_CLASS(var, className) \
226 var = env->FindClass(className); \
227 LOG_FATAL_IF(! var, "Unable to find class " className);
228
Florin Malitac6776752014-05-06 21:07:19 -0400229#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
230 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
231 LOG_FATAL_IF(!var, "Unable to find method " methodName);
Romain Guy3b748a42013-04-17 18:54:38 -0700232
233const char* const kClassPathName = "com/android/server/AssetAtlasService";
234
235static JNINativeMethod gMethods[] = {
John Reck9d4efdf2015-04-17 20:45:40 +0000236 { "nAcquireAtlasCanvas", "(Landroid/graphics/Canvas;II)J",
237 (void*) com_android_server_AssetAtlasService_acquireCanvas },
238 { "nReleaseAtlasCanvas", "(Landroid/graphics/Canvas;J)V",
239 (void*) com_android_server_AssetAtlasService_releaseCanvas },
240 { "nUploadAtlas", "(Landroid/view/GraphicBuffer;J)Z",
Romain Guy3b748a42013-04-17 18:54:38 -0700241 (void*) com_android_server_AssetAtlasService_upload },
242};
243
244int register_android_server_AssetAtlasService(JNIEnv* env) {
John Reck9d4efdf2015-04-17 20:45:40 +0000245 jclass clazz;
246
247 FIND_CLASS(clazz, "android/graphics/Canvas");
248 GET_METHOD_ID(gCanvasClassInfo.setNativeBitmap, clazz, "setNativeBitmap", "(J)V");
249
Romain Guy3b748a42013-04-17 18:54:38 -0700250 return jniRegisterNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
251}
252
253};