Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 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" |
Florin Malita | 5c3d927 | 2014-05-08 10:35:36 -0400 | [diff] [blame] | 21 | #include "android/graphics/GraphicsJNI.h" |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 22 | |
| 23 | #include <android_view_GraphicBuffer.h> |
Ruben Brunk | 87eac99 | 2013-09-09 17:44:59 -0700 | [diff] [blame] | 24 | #include <cutils/log.h> |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 25 | |
| 26 | #include <GLES2/gl2.h> |
| 27 | #include <GLES2/gl2ext.h> |
| 28 | |
| 29 | #include <EGL/egl.h> |
| 30 | #include <EGL/eglext.h> |
| 31 | |
Andreas Gampe | 184e3ed | 2014-09-29 15:04:06 -0700 | [diff] [blame] | 32 | // Disable warnings for Skia. |
| 33 | #pragma GCC diagnostic push |
| 34 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 35 | #include <SkCanvas.h> |
| 36 | #include <SkBitmap.h> |
Andreas Gampe | 184e3ed | 2014-09-29 15:04:06 -0700 | [diff] [blame] | 37 | #pragma GCC diagnostic pop |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 38 | |
| 39 | namespace 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 Reck | 9d4efdf | 2015-04-17 20:45:40 +0000 | [diff] [blame] | 50 | // JNI Helpers |
| 51 | // ---------------------------------------------------------------------------- |
| 52 | |
| 53 | static struct { |
| 54 | jmethodID setNativeBitmap; |
| 55 | } gCanvasClassInfo; |
| 56 | |
| 57 | #define INVOKEV(object, method, ...) \ |
| 58 | env->CallVoidMethod(object, method, __VA_ARGS__) |
| 59 | |
| 60 | // ---------------------------------------------------------------------------- |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 61 | // Canvas management |
| 62 | // ---------------------------------------------------------------------------- |
| 63 | |
John Reck | 9d4efdf | 2015-04-17 20:45:40 +0000 | [diff] [blame] | 64 | static 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 | |
| 75 | static 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 Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 84 | #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 | |
| 95 | static jboolean com_android_server_AssetAtlasService_upload(JNIEnv* env, jobject, |
John Reck | 9d4efdf | 2015-04-17 20:45:40 +0000 | [diff] [blame] | 96 | jobject graphicBuffer, jlong bitmapHandle) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 97 | |
John Reck | 9d4efdf | 2015-04-17 20:45:40 +0000 | [diff] [blame] | 98 | SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapHandle); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 99 | // 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 Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 105 | if (display == EGL_NO_DISPLAY) return JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 106 | |
| 107 | EGLint major; |
| 108 | EGLint minor; |
| 109 | if (!eglInitialize(display, &major, &minor)) { |
| 110 | ALOGW("Could not initialize EGL"); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 111 | return JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 112 | } |
| 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 Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 132 | return JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 133 | } |
| 134 | if (configCount <= 0) { |
| 135 | ALOGW("Could not find EGL configuration"); |
| 136 | eglReleaseThread(); |
| 137 | eglTerminate(display); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 138 | return JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 139 | } |
| 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 Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 153 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 154 | } |
| 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 Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 161 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | if (!eglMakeCurrent(display, surface, surface, context)) { |
| 165 | ALOGW("Could not change current EGL context"); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 166 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 167 | } |
| 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 Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 177 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 178 | } |
| 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 Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 185 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // Upload the content of the bitmap in the GraphicBuffer |
John Reck | 9d4efdf | 2015-04-17 20:45:40 +0000 | [diff] [blame] | 189 | 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 Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 192 | if (glGetError() != GL_NO_ERROR) { |
| 193 | ALOGW("Could not upload to texture"); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 194 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 195 | } |
| 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 Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 203 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 204 | } |
| 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 Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 212 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 215 | CLEANUP_GL_AND_RETURN(JNI_TRUE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 218 | return JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 219 | } |
| 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 Malita | c677675 | 2014-05-06 21:07:19 -0400 | [diff] [blame] | 229 | #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 Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 232 | |
| 233 | const char* const kClassPathName = "com/android/server/AssetAtlasService"; |
| 234 | |
| 235 | static JNINativeMethod gMethods[] = { |
John Reck | 9d4efdf | 2015-04-17 20:45:40 +0000 | [diff] [blame] | 236 | { "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 Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 241 | (void*) com_android_server_AssetAtlasService_upload }, |
| 242 | }; |
| 243 | |
| 244 | int register_android_server_AssetAtlasService(JNIEnv* env) { |
John Reck | 9d4efdf | 2015-04-17 20:45:40 +0000 | [diff] [blame] | 245 | jclass clazz; |
| 246 | |
| 247 | FIND_CLASS(clazz, "android/graphics/Canvas"); |
| 248 | GET_METHOD_ID(gCanvasClassInfo.setNativeBitmap, clazz, "setNativeBitmap", "(J)V"); |
| 249 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 250 | return jniRegisterNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 251 | } |
| 252 | |
| 253 | }; |