Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
John Reck | 3083579 | 2013-11-07 14:21:24 -0800 | [diff] [blame] | 17 | #define LOG_TAG "GLRenderer" |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 18 | |
| 19 | #include "jni.h" |
| 20 | #include <nativehelper/JNIHelp.h> |
| 21 | #include <android_runtime/AndroidRuntime.h> |
| 22 | |
Romain Guy | e9bc11f | 2013-05-23 12:47:26 -0700 | [diff] [blame] | 23 | #include <EGL/egl.h> |
| 24 | #include <EGL/eglext.h> |
Romain Guy | 6db53c3 | 2012-04-02 10:52:52 -0700 | [diff] [blame] | 25 | #include <EGL/egl_cache.h> |
| 26 | |
Romain Guy | e9bc11f | 2013-05-23 12:47:26 -0700 | [diff] [blame] | 27 | #include <utils/Timers.h> |
| 28 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 29 | #include <Caches.h> |
Romain Guy | e9bc11f | 2013-05-23 12:47:26 -0700 | [diff] [blame] | 30 | #include <Extensions.h> |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 31 | |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 32 | #ifdef USE_OPENGL_RENDERER |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 33 | EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface); |
| 34 | #endif |
Jamie Gennis | b335fad | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 35 | |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 36 | namespace android { |
| 37 | |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Note: OpenGLRenderer JNI layer is generated and compiled only on supported |
| 40 | * devices. This means all the logic must be compiled only when the |
| 41 | * preprocessor variable USE_OPENGL_RENDERER is defined. |
| 42 | */ |
| 43 | #ifdef USE_OPENGL_RENDERER |
| 44 | |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 45 | // ---------------------------------------------------------------------------- |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 46 | // Defines |
| 47 | // ---------------------------------------------------------------------------- |
| 48 | |
| 49 | // Debug |
| 50 | #define DEBUG_RENDERER 0 |
| 51 | |
| 52 | // Debug |
| 53 | #if DEBUG_RENDERER |
| 54 | #define RENDERER_LOGD(...) ALOGD(__VA_ARGS__) |
| 55 | #else |
| 56 | #define RENDERER_LOGD(...) |
| 57 | #endif |
| 58 | |
| 59 | // ---------------------------------------------------------------------------- |
| 60 | // Surface and display management |
| 61 | // ---------------------------------------------------------------------------- |
| 62 | |
John Reck | 3083579 | 2013-11-07 14:21:24 -0800 | [diff] [blame] | 63 | static jboolean android_view_GLRenderer_preserveBackBuffer(JNIEnv* env, jobject clazz) { |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 64 | EGLDisplay display = eglGetCurrentDisplay(); |
| 65 | EGLSurface surface = eglGetCurrentSurface(EGL_DRAW); |
| 66 | |
| 67 | eglGetError(); |
| 68 | eglSurfaceAttrib(display, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED); |
| 69 | |
| 70 | EGLint error = eglGetError(); |
| 71 | if (error != EGL_SUCCESS) { |
| 72 | RENDERER_LOGD("Could not enable buffer preserved swap behavior (%x)", error); |
| 73 | } |
| 74 | |
| 75 | return error == EGL_SUCCESS; |
| 76 | } |
| 77 | |
John Reck | 3083579 | 2013-11-07 14:21:24 -0800 | [diff] [blame] | 78 | static jboolean android_view_GLRenderer_isBackBufferPreserved(JNIEnv* env, jobject clazz) { |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 79 | EGLDisplay display = eglGetCurrentDisplay(); |
| 80 | EGLSurface surface = eglGetCurrentSurface(EGL_DRAW); |
| 81 | EGLint value; |
| 82 | |
| 83 | eglGetError(); |
| 84 | eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &value); |
| 85 | |
| 86 | EGLint error = eglGetError(); |
| 87 | if (error != EGL_SUCCESS) { |
| 88 | RENDERER_LOGD("Could not query buffer preserved swap behavior (%x)", error); |
| 89 | } |
| 90 | |
| 91 | return error == EGL_SUCCESS && value == EGL_BUFFER_PRESERVED; |
| 92 | } |
| 93 | |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 94 | // ---------------------------------------------------------------------------- |
| 95 | // Tracing and debugging |
| 96 | // ---------------------------------------------------------------------------- |
| 97 | |
John Reck | 3083579 | 2013-11-07 14:21:24 -0800 | [diff] [blame] | 98 | static bool android_view_GLRenderer_loadProperties(JNIEnv* env, jobject clazz) { |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 99 | if (uirenderer::Caches::hasInstance()) { |
| 100 | return uirenderer::Caches::getInstance().initProperties(); |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | |
John Reck | 3083579 | 2013-11-07 14:21:24 -0800 | [diff] [blame] | 105 | static void android_view_GLRenderer_beginFrame(JNIEnv* env, jobject clazz, |
Romain Guy | 7687882 | 2012-03-30 14:54:22 -0700 | [diff] [blame] | 106 | jintArray size) { |
| 107 | |
| 108 | EGLDisplay display = eglGetCurrentDisplay(); |
| 109 | EGLSurface surface = eglGetCurrentSurface(EGL_DRAW); |
| 110 | |
| 111 | if (size) { |
| 112 | EGLint value; |
| 113 | jint* storage = env->GetIntArrayElements(size, NULL); |
| 114 | |
| 115 | eglQuerySurface(display, surface, EGL_WIDTH, &value); |
| 116 | storage[0] = value; |
| 117 | |
| 118 | eglQuerySurface(display, surface, EGL_HEIGHT, &value); |
| 119 | storage[1] = value; |
| 120 | |
| 121 | env->ReleaseIntArrayElements(size, storage, 0); |
| 122 | } |
| 123 | |
| 124 | eglBeginFrame(display, surface); |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 125 | } |
| 126 | |
John Reck | 3083579 | 2013-11-07 14:21:24 -0800 | [diff] [blame] | 127 | static jlong android_view_GLRenderer_getSystemTime(JNIEnv* env, jobject clazz) { |
Romain Guy | e9bc11f | 2013-05-23 12:47:26 -0700 | [diff] [blame] | 128 | if (uirenderer::Extensions::getInstance().hasNvSystemTime()) { |
| 129 | return eglGetSystemTimeNV(); |
| 130 | } |
| 131 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 132 | } |
| 133 | |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 134 | #endif // USE_OPENGL_RENDERER |
| 135 | |
| 136 | // ---------------------------------------------------------------------------- |
| 137 | // Shaders |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 138 | // ---------------------------------------------------------------------------- |
| 139 | |
John Reck | 3083579 | 2013-11-07 14:21:24 -0800 | [diff] [blame] | 140 | static void android_view_GLRenderer_setupShadersDiskCache(JNIEnv* env, jobject clazz, |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 141 | jstring diskCachePath) { |
| 142 | |
| 143 | const char* cacheArray = env->GetStringUTFChars(diskCachePath, NULL); |
| 144 | egl_cache_t::get()->setCacheFilename(cacheArray); |
| 145 | env->ReleaseStringUTFChars(diskCachePath, cacheArray); |
| 146 | } |
| 147 | |
| 148 | // ---------------------------------------------------------------------------- |
| 149 | // JNI Glue |
| 150 | // ---------------------------------------------------------------------------- |
| 151 | |
John Reck | 3083579 | 2013-11-07 14:21:24 -0800 | [diff] [blame] | 152 | const char* const kClassPathName = "android/view/GLRenderer"; |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 153 | |
| 154 | static JNINativeMethod gMethods[] = { |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 155 | #ifdef USE_OPENGL_RENDERER |
John Reck | 3083579 | 2013-11-07 14:21:24 -0800 | [diff] [blame] | 156 | { "isBackBufferPreserved", "()Z", (void*) android_view_GLRenderer_isBackBufferPreserved }, |
| 157 | { "preserveBackBuffer", "()Z", (void*) android_view_GLRenderer_preserveBackBuffer }, |
| 158 | { "loadProperties", "()Z", (void*) android_view_GLRenderer_loadProperties }, |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 159 | |
John Reck | 3083579 | 2013-11-07 14:21:24 -0800 | [diff] [blame] | 160 | { "beginFrame", "([I)V", (void*) android_view_GLRenderer_beginFrame }, |
Romain Guy | e9bc11f | 2013-05-23 12:47:26 -0700 | [diff] [blame] | 161 | |
John Reck | 3083579 | 2013-11-07 14:21:24 -0800 | [diff] [blame] | 162 | { "getSystemTime", "()J", (void*) android_view_GLRenderer_getSystemTime }, |
Romain Guy | 244ada1 | 2012-03-28 16:41:26 -0700 | [diff] [blame] | 163 | #endif |
| 164 | |
John Reck | 3083579 | 2013-11-07 14:21:24 -0800 | [diff] [blame] | 165 | { "setupShadersDiskCache", "(Ljava/lang/String;)V", |
| 166 | (void*) android_view_GLRenderer_setupShadersDiskCache }, |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 167 | }; |
| 168 | |
John Reck | 3083579 | 2013-11-07 14:21:24 -0800 | [diff] [blame] | 169 | int register_android_view_GLRenderer(JNIEnv* env) { |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 170 | return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 171 | } |
| 172 | |
| 173 | }; |