Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | #include "jni.h" |
| 18 | #include <nativehelper/JNIHelp.h> |
| 19 | #include <android_runtime/AndroidRuntime.h> |
Romain Guy | 035f1010 | 2011-06-15 17:57:28 -0700 | [diff] [blame] | 20 | #include <android_runtime/android_graphics_SurfaceTexture.h> |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 21 | |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 22 | #include <ui/Region.h> |
| 23 | #include <ui/Rect.h> |
| 24 | |
Andy McFadden | d47f7d8 | 2012-12-18 09:48:38 -0800 | [diff] [blame] | 25 | #include <gui/GLConsumer.h> |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 26 | #include <gui/Surface.h> |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 27 | |
| 28 | #include <SkBitmap.h> |
| 29 | #include <SkCanvas.h> |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | // ---------------------------------------------------------------------------- |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 34 | // JNI Glue |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | |
| 37 | static struct { |
| 38 | jmethodID set; |
| 39 | jfieldID left; |
| 40 | jfieldID top; |
| 41 | jfieldID right; |
| 42 | jfieldID bottom; |
| 43 | } gRectClassInfo; |
| 44 | |
| 45 | static struct { |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 46 | jfieldID mFinalizer; |
| 47 | jfieldID mNativeCanvas; |
| 48 | jfieldID mSurfaceFormat; |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 49 | } gCanvasClassInfo; |
| 50 | |
| 51 | static struct { |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 52 | jfieldID mNativeCanvas; |
| 53 | } gCanvasFinalizerClassInfo; |
| 54 | |
| 55 | static struct { |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 56 | jfieldID nativeWindow; |
| 57 | } gTextureViewClassInfo; |
| 58 | |
| 59 | #define GET_INT(object, field) \ |
| 60 | env->GetIntField(object, field) |
| 61 | |
| 62 | #define SET_INT(object, field, value) \ |
| 63 | env->SetIntField(object, field, value) |
| 64 | |
| 65 | #define INVOKEV(object, method, ...) \ |
| 66 | env->CallVoidMethod(object, method, __VA_ARGS__) |
| 67 | |
| 68 | // ---------------------------------------------------------------------------- |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 69 | // Native layer |
| 70 | // ---------------------------------------------------------------------------- |
| 71 | |
| 72 | static void android_view_TextureView_setDefaultBufferSize(JNIEnv* env, jobject, |
Romain Guy | 035f1010 | 2011-06-15 17:57:28 -0700 | [diff] [blame] | 73 | jobject surface, jint width, jint height) { |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 74 | |
Andy McFadden | d47f7d8 | 2012-12-18 09:48:38 -0800 | [diff] [blame] | 75 | sp<GLConsumer> glConsumer(SurfaceTexture_getSurfaceTexture(env, surface)); |
| 76 | glConsumer->setDefaultBufferSize(width, height); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 79 | static inline SkBitmap::Config convertPixelFormat(int32_t format) { |
| 80 | switch (format) { |
| 81 | case WINDOW_FORMAT_RGBA_8888: |
| 82 | return SkBitmap::kARGB_8888_Config; |
| 83 | case WINDOW_FORMAT_RGBX_8888: |
| 84 | return SkBitmap::kARGB_8888_Config; |
| 85 | case WINDOW_FORMAT_RGB_565: |
| 86 | return SkBitmap::kRGB_565_Config; |
| 87 | default: |
| 88 | return SkBitmap::kNo_Config; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * This is a private API, and this implementation is also provided in the NDK. |
| 94 | * However, the NDK links against android_runtime, which means that using the |
| 95 | * NDK implementation would create a circular dependency between the libraries. |
| 96 | */ |
| 97 | static int32_t native_window_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer, |
| 98 | Rect* inOutDirtyBounds) { |
| 99 | return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds); |
| 100 | } |
| 101 | |
| 102 | static int32_t native_window_unlockAndPost(ANativeWindow* window) { |
| 103 | return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST); |
| 104 | } |
| 105 | |
| 106 | static void android_view_TextureView_createNativeWindow(JNIEnv* env, jobject textureView, |
| 107 | jobject surface) { |
| 108 | |
Andy McFadden | d47f7d8 | 2012-12-18 09:48:38 -0800 | [diff] [blame] | 109 | sp<GLConsumer> glConsumer(SurfaceTexture_getSurfaceTexture(env, surface)); |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 110 | sp<ANativeWindow> window = new Surface(glConsumer->getBufferQueue()); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 111 | |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 112 | window->incStrong((void*)android_view_TextureView_createNativeWindow); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 113 | SET_INT(textureView, gTextureViewClassInfo.nativeWindow, jint(window.get())); |
| 114 | } |
| 115 | |
| 116 | static void android_view_TextureView_destroyNativeWindow(JNIEnv* env, jobject textureView) { |
| 117 | |
| 118 | ANativeWindow* nativeWindow = (ANativeWindow*) |
| 119 | GET_INT(textureView, gTextureViewClassInfo.nativeWindow); |
| 120 | |
| 121 | if (nativeWindow) { |
| 122 | sp<ANativeWindow> window(nativeWindow); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 123 | window->decStrong((void*)android_view_TextureView_createNativeWindow); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 124 | SET_INT(textureView, gTextureViewClassInfo.nativeWindow, 0); |
| 125 | } |
| 126 | } |
| 127 | |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 128 | static inline void swapCanvasPtr(JNIEnv* env, jobject canvasObj, SkCanvas* newCanvas) { |
| 129 | jobject canvasFinalizerObj = env->GetObjectField(canvasObj, gCanvasClassInfo.mFinalizer); |
| 130 | SkCanvas* previousCanvas = reinterpret_cast<SkCanvas*>( |
| 131 | env->GetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas)); |
| 132 | env->SetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas, (int)newCanvas); |
| 133 | env->SetIntField(canvasFinalizerObj, gCanvasFinalizerClassInfo.mNativeCanvas, (int)newCanvas); |
| 134 | SkSafeUnref(previousCanvas); |
| 135 | } |
| 136 | |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 137 | static void android_view_TextureView_lockCanvas(JNIEnv* env, jobject, |
| 138 | jint nativeWindow, jobject canvas, jobject dirtyRect) { |
| 139 | |
| 140 | if (!nativeWindow) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | ANativeWindow_Buffer buffer; |
| 145 | |
| 146 | Rect rect; |
| 147 | if (dirtyRect) { |
| 148 | rect.left = GET_INT(dirtyRect, gRectClassInfo.left); |
| 149 | rect.top = GET_INT(dirtyRect, gRectClassInfo.top); |
| 150 | rect.right = GET_INT(dirtyRect, gRectClassInfo.right); |
| 151 | rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom); |
| 152 | } else { |
| 153 | rect.set(Rect(0x3FFF, 0x3FFF)); |
| 154 | } |
| 155 | |
| 156 | sp<ANativeWindow> window((ANativeWindow*) nativeWindow); |
| 157 | native_window_lock(window.get(), &buffer, &rect); |
| 158 | |
| 159 | ssize_t bytesCount = buffer.stride * bytesPerPixel(buffer.format); |
| 160 | |
| 161 | SkBitmap bitmap; |
| 162 | bitmap.setConfig(convertPixelFormat(buffer.format), buffer.width, buffer.height, bytesCount); |
| 163 | |
| 164 | if (buffer.format == WINDOW_FORMAT_RGBX_8888) { |
| 165 | bitmap.setIsOpaque(true); |
| 166 | } |
| 167 | |
| 168 | if (buffer.width > 0 && buffer.height > 0) { |
| 169 | bitmap.setPixels(buffer.bits); |
| 170 | } else { |
| 171 | bitmap.setPixels(NULL); |
| 172 | } |
| 173 | |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 174 | SET_INT(canvas, gCanvasClassInfo.mSurfaceFormat, buffer.format); |
| 175 | |
| 176 | SkCanvas* nativeCanvas = SkNEW_ARGS(SkCanvas, (bitmap)); |
| 177 | swapCanvasPtr(env, canvas, nativeCanvas); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 178 | |
| 179 | SkRect clipRect; |
| 180 | clipRect.set(rect.left, rect.top, rect.right, rect.bottom); |
| 181 | nativeCanvas->clipRect(clipRect); |
| 182 | |
| 183 | if (dirtyRect) { |
| 184 | INVOKEV(dirtyRect, gRectClassInfo.set, |
| 185 | int(rect.left), int(rect.top), int(rect.right), int(rect.bottom)); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static void android_view_TextureView_unlockCanvasAndPost(JNIEnv* env, jobject, |
| 190 | jint nativeWindow, jobject canvas) { |
| 191 | |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 192 | SkCanvas* nativeCanvas = SkNEW(SkCanvas); |
| 193 | swapCanvasPtr(env, canvas, nativeCanvas); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 194 | |
| 195 | if (nativeWindow) { |
| 196 | sp<ANativeWindow> window((ANativeWindow*) nativeWindow); |
| 197 | native_window_unlockAndPost(window.get()); |
| 198 | } |
| 199 | } |
| 200 | |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 201 | // ---------------------------------------------------------------------------- |
| 202 | // JNI Glue |
| 203 | // ---------------------------------------------------------------------------- |
| 204 | |
| 205 | const char* const kClassPathName = "android/view/TextureView"; |
| 206 | |
| 207 | static JNINativeMethod gMethods[] = { |
Romain Guy | 035f1010 | 2011-06-15 17:57:28 -0700 | [diff] [blame] | 208 | { "nSetDefaultBufferSize", "(Landroid/graphics/SurfaceTexture;II)V", |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 209 | (void*) android_view_TextureView_setDefaultBufferSize }, |
| 210 | |
| 211 | { "nCreateNativeWindow", "(Landroid/graphics/SurfaceTexture;)V", |
| 212 | (void*) android_view_TextureView_createNativeWindow }, |
| 213 | { "nDestroyNativeWindow", "()V", |
| 214 | (void*) android_view_TextureView_destroyNativeWindow }, |
| 215 | |
| 216 | { "nLockCanvas", "(ILandroid/graphics/Canvas;Landroid/graphics/Rect;)V", |
| 217 | (void*) android_view_TextureView_lockCanvas }, |
| 218 | { "nUnlockCanvasAndPost", "(ILandroid/graphics/Canvas;)V", |
| 219 | (void*) android_view_TextureView_unlockCanvasAndPost }, |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 222 | #define FIND_CLASS(var, className) \ |
| 223 | var = env->FindClass(className); \ |
| 224 | LOG_FATAL_IF(!var, "Unable to find class " className); |
| 225 | |
| 226 | #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \ |
| 227 | var = env->GetMethodID(clazz, methodName, methodDescriptor); \ |
| 228 | LOG_FATAL_IF(!var, "Unable to find method " methodName); |
| 229 | |
| 230 | #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ |
| 231 | var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ |
| 232 | LOG_FATAL_IF(!var, "Unable to find field" fieldName); |
| 233 | |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 234 | int register_android_view_TextureView(JNIEnv* env) { |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 235 | jclass clazz; |
| 236 | FIND_CLASS(clazz, "android/graphics/Rect"); |
| 237 | GET_METHOD_ID(gRectClassInfo.set, clazz, "set", "(IIII)V"); |
| 238 | GET_FIELD_ID(gRectClassInfo.left, clazz, "left", "I"); |
| 239 | GET_FIELD_ID(gRectClassInfo.top, clazz, "top", "I"); |
| 240 | GET_FIELD_ID(gRectClassInfo.right, clazz, "right", "I"); |
| 241 | GET_FIELD_ID(gRectClassInfo.bottom, clazz, "bottom", "I"); |
| 242 | |
| 243 | FIND_CLASS(clazz, "android/graphics/Canvas"); |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 244 | GET_FIELD_ID(gCanvasClassInfo.mFinalizer, clazz, "mFinalizer", "Landroid/graphics/Canvas$CanvasFinalizer;"); |
| 245 | GET_FIELD_ID(gCanvasClassInfo.mNativeCanvas, clazz, "mNativeCanvas", "I"); |
| 246 | GET_FIELD_ID(gCanvasClassInfo.mSurfaceFormat, clazz, "mSurfaceFormat", "I"); |
| 247 | |
| 248 | FIND_CLASS(clazz, "android/graphics/Canvas$CanvasFinalizer"); |
| 249 | GET_FIELD_ID(gCanvasFinalizerClassInfo.mNativeCanvas, clazz, "mNativeCanvas", "I"); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 250 | |
| 251 | FIND_CLASS(clazz, "android/view/TextureView"); |
| 252 | GET_FIELD_ID(gTextureViewClassInfo.nativeWindow, clazz, "mNativeWindow", "I"); |
| 253 | |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 254 | return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 255 | } |
| 256 | |
| 257 | }; |