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 | // ---------------------------------------------------------------------------- |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 50 | // Canvas management |
| 51 | // ---------------------------------------------------------------------------- |
| 52 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 53 | #define CLEANUP_GL_AND_RETURN(result) \ |
| 54 | if (fence != EGL_NO_SYNC_KHR) eglDestroySyncKHR(display, fence); \ |
| 55 | if (image) eglDestroyImageKHR(display, image); \ |
| 56 | if (texture) glDeleteTextures(1, &texture); \ |
| 57 | if (surface != EGL_NO_SURFACE) eglDestroySurface(display, surface); \ |
| 58 | if (context != EGL_NO_CONTEXT) eglDestroyContext(display, context); \ |
| 59 | eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); \ |
| 60 | eglReleaseThread(); \ |
| 61 | eglTerminate(display); \ |
| 62 | return result; |
| 63 | |
| 64 | static jboolean com_android_server_AssetAtlasService_upload(JNIEnv* env, jobject, |
John Reck | c294d12 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 65 | jobject graphicBuffer, jobject bitmapHandle) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 66 | |
John Reck | c294d12 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 67 | SkBitmap bitmap; |
| 68 | GraphicsJNI::getSkBitmap(env, bitmapHandle, &bitmap); |
| 69 | SkAutoLockPixels alp(bitmap); |
| 70 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 71 | // The goal of this method is to copy the bitmap into the GraphicBuffer |
| 72 | // using the GPU to swizzle the texture content |
| 73 | sp<GraphicBuffer> buffer(graphicBufferForJavaObject(env, graphicBuffer)); |
| 74 | |
| 75 | if (buffer != NULL) { |
| 76 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 77 | if (display == EGL_NO_DISPLAY) return JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 78 | |
| 79 | EGLint major; |
| 80 | EGLint minor; |
| 81 | if (!eglInitialize(display, &major, &minor)) { |
| 82 | ALOGW("Could not initialize EGL"); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 83 | return JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // We're going to use a 1x1 pbuffer surface later on |
| 87 | // The configuration doesn't really matter for what we're trying to do |
| 88 | EGLint configAttrs[] = { |
| 89 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 90 | EGL_RED_SIZE, 8, |
| 91 | EGL_GREEN_SIZE, 8, |
| 92 | EGL_BLUE_SIZE, 8, |
| 93 | EGL_ALPHA_SIZE, 0, |
| 94 | EGL_DEPTH_SIZE, 0, |
| 95 | EGL_STENCIL_SIZE, 0, |
| 96 | EGL_NONE |
| 97 | }; |
| 98 | EGLConfig configs[1]; |
| 99 | EGLint configCount; |
| 100 | if (!eglChooseConfig(display, configAttrs, configs, 1, &configCount)) { |
| 101 | ALOGW("Could not select EGL configuration"); |
| 102 | eglReleaseThread(); |
| 103 | eglTerminate(display); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 104 | return JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 105 | } |
| 106 | if (configCount <= 0) { |
| 107 | ALOGW("Could not find EGL configuration"); |
| 108 | eglReleaseThread(); |
| 109 | eglTerminate(display); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 110 | return JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // These objects are initialized below but the default "null" |
| 114 | // values are used to cleanup properly at any point in the |
| 115 | // initialization sequence |
| 116 | GLuint texture = 0; |
| 117 | EGLImageKHR image = EGL_NO_IMAGE_KHR; |
| 118 | EGLSurface surface = EGL_NO_SURFACE; |
| 119 | EGLSyncKHR fence = EGL_NO_SYNC_KHR; |
| 120 | |
| 121 | EGLint attrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; |
| 122 | EGLContext context = eglCreateContext(display, configs[0], EGL_NO_CONTEXT, attrs); |
| 123 | if (context == EGL_NO_CONTEXT) { |
| 124 | ALOGW("Could not create EGL context"); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 125 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Create the 1x1 pbuffer |
| 129 | EGLint surfaceAttrs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE }; |
| 130 | surface = eglCreatePbufferSurface(display, configs[0], surfaceAttrs); |
| 131 | if (surface == EGL_NO_SURFACE) { |
| 132 | ALOGW("Could not create EGL surface"); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 133 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | if (!eglMakeCurrent(display, surface, surface, context)) { |
| 137 | ALOGW("Could not change current EGL context"); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 138 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // We use an EGLImage to access the content of the GraphicBuffer |
| 142 | // The EGL image is later bound to a 2D texture |
| 143 | EGLClientBuffer clientBuffer = (EGLClientBuffer) buffer->getNativeBuffer(); |
| 144 | EGLint imageAttrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE }; |
| 145 | image = eglCreateImageKHR(display, EGL_NO_CONTEXT, |
| 146 | EGL_NATIVE_BUFFER_ANDROID, clientBuffer, imageAttrs); |
| 147 | if (image == EGL_NO_IMAGE_KHR) { |
| 148 | ALOGW("Could not create EGL image"); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 149 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | glGenTextures(1, &texture); |
| 153 | glBindTexture(GL_TEXTURE_2D, texture); |
| 154 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image); |
| 155 | if (glGetError() != GL_NO_ERROR) { |
| 156 | ALOGW("Could not create/bind texture"); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 157 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Upload the content of the bitmap in the GraphicBuffer |
John Reck | c294d12 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 161 | glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap.bytesPerPixel()); |
| 162 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bitmap.width(), bitmap.height(), |
| 163 | GL_RGBA, GL_UNSIGNED_BYTE, bitmap.getPixels()); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 164 | if (glGetError() != GL_NO_ERROR) { |
| 165 | ALOGW("Could not upload to texture"); |
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 | // The fence is used to wait for the texture upload to finish |
| 170 | // properly. We cannot rely on glFlush() and glFinish() as |
| 171 | // some drivers completely ignore these API calls |
| 172 | fence = eglCreateSyncKHR(display, EGL_SYNC_FENCE_KHR, NULL); |
| 173 | if (fence == EGL_NO_SYNC_KHR) { |
| 174 | ALOGW("Could not create sync fence %#x", eglGetError()); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 175 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // The flag EGL_SYNC_FLUSH_COMMANDS_BIT_KHR will trigger a |
| 179 | // pipeline flush (similar to what a glFlush() would do.) |
| 180 | EGLint waitStatus = eglClientWaitSyncKHR(display, fence, |
| 181 | EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, FENCE_TIMEOUT); |
| 182 | if (waitStatus != EGL_CONDITION_SATISFIED_KHR) { |
| 183 | ALOGW("Failed to wait for the fence %#x", eglGetError()); |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 184 | CLEANUP_GL_AND_RETURN(JNI_FALSE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 187 | CLEANUP_GL_AND_RETURN(JNI_TRUE); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Ashok Bhat | 17ab38f | 2014-01-27 16:00:23 +0000 | [diff] [blame] | 190 | return JNI_FALSE; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // ---------------------------------------------------------------------------- |
| 194 | // JNI Glue |
| 195 | // ---------------------------------------------------------------------------- |
| 196 | |
| 197 | #define FIND_CLASS(var, className) \ |
| 198 | var = env->FindClass(className); \ |
| 199 | LOG_FATAL_IF(! var, "Unable to find class " className); |
| 200 | |
Florin Malita | c677675 | 2014-05-06 21:07:19 -0400 | [diff] [blame] | 201 | #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \ |
| 202 | var = env->GetMethodID(clazz, methodName, methodDescriptor); \ |
| 203 | LOG_FATAL_IF(!var, "Unable to find method " methodName); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 204 | |
| 205 | const char* const kClassPathName = "com/android/server/AssetAtlasService"; |
| 206 | |
| 207 | static JNINativeMethod gMethods[] = { |
John Reck | c294d12 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 208 | { "nUploadAtlas", "(Landroid/view/GraphicBuffer;Landroid/graphics/Bitmap;)Z", |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 209 | (void*) com_android_server_AssetAtlasService_upload }, |
| 210 | }; |
| 211 | |
| 212 | int register_android_server_AssetAtlasService(JNIEnv* env) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 213 | return jniRegisterNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 214 | } |
| 215 | |
| 216 | }; |