blob: 8f4fb51093d939d966883777a68a32ff5336c266 [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// ----------------------------------------------------------------------------
Romain Guy3b748a42013-04-17 18:54:38 -070050// Canvas management
51// ----------------------------------------------------------------------------
52
Romain Guy3b748a42013-04-17 18:54:38 -070053#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
64static jboolean com_android_server_AssetAtlasService_upload(JNIEnv* env, jobject,
John Reckc294d122015-04-13 15:20:29 -070065 jobject graphicBuffer, jobject bitmapHandle) {
Romain Guy3b748a42013-04-17 18:54:38 -070066
John Reckc294d122015-04-13 15:20:29 -070067 SkBitmap bitmap;
68 GraphicsJNI::getSkBitmap(env, bitmapHandle, &bitmap);
69 SkAutoLockPixels alp(bitmap);
70
Romain Guy3b748a42013-04-17 18:54:38 -070071 // 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 Bhat17ab38f2014-01-27 16:00:23 +000077 if (display == EGL_NO_DISPLAY) return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -070078
79 EGLint major;
80 EGLint minor;
81 if (!eglInitialize(display, &major, &minor)) {
82 ALOGW("Could not initialize EGL");
Ashok Bhat17ab38f2014-01-27 16:00:23 +000083 return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -070084 }
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 Bhat17ab38f2014-01-27 16:00:23 +0000104 return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700105 }
106 if (configCount <= 0) {
107 ALOGW("Could not find EGL configuration");
108 eglReleaseThread();
109 eglTerminate(display);
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000110 return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700111 }
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 Bhat17ab38f2014-01-27 16:00:23 +0000125 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700126 }
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 Bhat17ab38f2014-01-27 16:00:23 +0000133 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700134 }
135
136 if (!eglMakeCurrent(display, surface, surface, context)) {
137 ALOGW("Could not change current EGL context");
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000138 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700139 }
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 Bhat17ab38f2014-01-27 16:00:23 +0000149 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700150 }
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 Bhat17ab38f2014-01-27 16:00:23 +0000157 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700158 }
159
160 // Upload the content of the bitmap in the GraphicBuffer
John Reckc294d122015-04-13 15:20:29 -0700161 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 Guy3b748a42013-04-17 18:54:38 -0700164 if (glGetError() != GL_NO_ERROR) {
165 ALOGW("Could not upload to texture");
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000166 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700167 }
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 Bhat17ab38f2014-01-27 16:00:23 +0000175 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700176 }
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 Bhat17ab38f2014-01-27 16:00:23 +0000184 CLEANUP_GL_AND_RETURN(JNI_FALSE);
Romain Guy3b748a42013-04-17 18:54:38 -0700185 }
186
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000187 CLEANUP_GL_AND_RETURN(JNI_TRUE);
Romain Guy3b748a42013-04-17 18:54:38 -0700188 }
189
Ashok Bhat17ab38f2014-01-27 16:00:23 +0000190 return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700191}
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 Malitac6776752014-05-06 21:07:19 -0400201#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 Guy3b748a42013-04-17 18:54:38 -0700204
205const char* const kClassPathName = "com/android/server/AssetAtlasService";
206
207static JNINativeMethod gMethods[] = {
John Reckc294d122015-04-13 15:20:29 -0700208 { "nUploadAtlas", "(Landroid/view/GraphicBuffer;Landroid/graphics/Bitmap;)Z",
Romain Guy3b748a42013-04-17 18:54:38 -0700209 (void*) com_android_server_AssetAtlasService_upload },
210};
211
212int register_android_server_AssetAtlasService(JNIEnv* env) {
Romain Guy3b748a42013-04-17 18:54:38 -0700213 return jniRegisterNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
214}
215
216};