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> |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 30 | #include <SkImage.h> |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | // ---------------------------------------------------------------------------- |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 35 | // JNI Glue |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
| 38 | static struct { |
| 39 | jmethodID set; |
| 40 | jfieldID left; |
| 41 | jfieldID top; |
| 42 | jfieldID right; |
| 43 | jfieldID bottom; |
| 44 | } gRectClassInfo; |
| 45 | |
| 46 | static struct { |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 47 | jfieldID mFinalizer; |
| 48 | jfieldID mNativeCanvas; |
| 49 | jfieldID mSurfaceFormat; |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 50 | } gCanvasClassInfo; |
| 51 | |
| 52 | static struct { |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 53 | jfieldID mNativeCanvas; |
| 54 | } gCanvasFinalizerClassInfo; |
| 55 | |
| 56 | static struct { |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 57 | jfieldID nativeWindow; |
| 58 | } gTextureViewClassInfo; |
| 59 | |
| 60 | #define GET_INT(object, field) \ |
| 61 | env->GetIntField(object, field) |
| 62 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 63 | #define GET_LONG(object, field) \ |
| 64 | env->GetLongField(object, field) |
| 65 | |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 66 | #define SET_INT(object, field, value) \ |
| 67 | env->SetIntField(object, field, value) |
| 68 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 69 | #define SET_LONG(object, field, value) \ |
| 70 | env->SetLongField(object, field, value) |
| 71 | |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 72 | #define INVOKEV(object, method, ...) \ |
| 73 | env->CallVoidMethod(object, method, __VA_ARGS__) |
| 74 | |
| 75 | // ---------------------------------------------------------------------------- |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 76 | // Native layer |
| 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 | |
Mathias Agopian | 52a9a10 | 2013-08-02 01:38:38 -0700 | [diff] [blame] | 109 | sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surface)); |
Mathias Agopian | 24f43c9 | 2013-08-06 20:16:12 -0700 | [diff] [blame] | 110 | sp<ANativeWindow> window = new Surface(producer, true); |
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); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 113 | SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, jlong(window.get())); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static void android_view_TextureView_destroyNativeWindow(JNIEnv* env, jobject textureView) { |
| 117 | |
| 118 | ANativeWindow* nativeWindow = (ANativeWindow*) |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 119 | GET_LONG(textureView, gTextureViewClassInfo.nativeWindow); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 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); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 124 | SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, 0); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 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) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 129 | jobject canvasFinalizerObj = env->GetObjectField(canvasObj, gCanvasClassInfo.mFinalizer); |
| 130 | SkCanvas* previousCanvas = reinterpret_cast<SkCanvas*>( |
Narayan Kamath | e3a6663 | 2014-01-29 12:21:32 +0000 | [diff] [blame] | 131 | env->GetLongField(canvasObj, gCanvasClassInfo.mNativeCanvas)); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 132 | env->SetLongField(canvasObj, gCanvasClassInfo.mNativeCanvas, (jlong)newCanvas); |
| 133 | env->SetLongField(canvasFinalizerObj, gCanvasFinalizerClassInfo.mNativeCanvas, (jlong)newCanvas); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 134 | SkSafeUnref(previousCanvas); |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Romain Guy | 53bacf5 | 2013-04-30 11:30:10 -0700 | [diff] [blame] | 137 | static jboolean android_view_TextureView_lockCanvas(JNIEnv* env, jobject, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 138 | jlong nativeWindow, jobject canvas, jobject dirtyRect) { |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 139 | |
| 140 | if (!nativeWindow) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 141 | return JNI_FALSE; |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 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); |
Romain Guy | 53bacf5 | 2013-04-30 11:30:10 -0700 | [diff] [blame] | 157 | int32_t status = native_window_lock(window.get(), &buffer, &rect); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 158 | if (status) return JNI_FALSE; |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 159 | |
| 160 | ssize_t bytesCount = buffer.stride * bytesPerPixel(buffer.format); |
| 161 | |
| 162 | SkBitmap bitmap; |
| 163 | bitmap.setConfig(convertPixelFormat(buffer.format), buffer.width, buffer.height, bytesCount); |
| 164 | |
| 165 | if (buffer.format == WINDOW_FORMAT_RGBX_8888) { |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 166 | bitmap.setAlphaType(kOpaque_SkAlphaType); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | if (buffer.width > 0 && buffer.height > 0) { |
| 170 | bitmap.setPixels(buffer.bits); |
| 171 | } else { |
| 172 | bitmap.setPixels(NULL); |
| 173 | } |
| 174 | |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 175 | SET_INT(canvas, gCanvasClassInfo.mSurfaceFormat, buffer.format); |
| 176 | |
| 177 | SkCanvas* nativeCanvas = SkNEW_ARGS(SkCanvas, (bitmap)); |
| 178 | swapCanvasPtr(env, canvas, nativeCanvas); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 179 | |
| 180 | SkRect clipRect; |
| 181 | clipRect.set(rect.left, rect.top, rect.right, rect.bottom); |
| 182 | nativeCanvas->clipRect(clipRect); |
| 183 | |
| 184 | if (dirtyRect) { |
| 185 | INVOKEV(dirtyRect, gRectClassInfo.set, |
| 186 | int(rect.left), int(rect.top), int(rect.right), int(rect.bottom)); |
| 187 | } |
Romain Guy | 53bacf5 | 2013-04-30 11:30:10 -0700 | [diff] [blame] | 188 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 189 | return JNI_TRUE; |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void android_view_TextureView_unlockCanvasAndPost(JNIEnv* env, jobject, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 193 | jlong nativeWindow, jobject canvas) { |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 194 | |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 195 | SkCanvas* nativeCanvas = SkNEW(SkCanvas); |
| 196 | swapCanvasPtr(env, canvas, nativeCanvas); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 197 | |
| 198 | if (nativeWindow) { |
| 199 | sp<ANativeWindow> window((ANativeWindow*) nativeWindow); |
| 200 | native_window_unlockAndPost(window.get()); |
| 201 | } |
| 202 | } |
| 203 | |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 204 | // ---------------------------------------------------------------------------- |
| 205 | // JNI Glue |
| 206 | // ---------------------------------------------------------------------------- |
| 207 | |
| 208 | const char* const kClassPathName = "android/view/TextureView"; |
| 209 | |
| 210 | static JNINativeMethod gMethods[] = { |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 211 | { "nCreateNativeWindow", "(Landroid/graphics/SurfaceTexture;)V", |
| 212 | (void*) android_view_TextureView_createNativeWindow }, |
| 213 | { "nDestroyNativeWindow", "()V", |
| 214 | (void*) android_view_TextureView_destroyNativeWindow }, |
| 215 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 216 | { "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z", |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 217 | (void*) android_view_TextureView_lockCanvas }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 218 | { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V", |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 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"); |
Romain Guy | 53bacf5 | 2013-04-30 11:30:10 -0700 | [diff] [blame] | 244 | GET_FIELD_ID(gCanvasClassInfo.mFinalizer, clazz, "mFinalizer", |
| 245 | "Landroid/graphics/Canvas$CanvasFinalizer;"); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 246 | GET_FIELD_ID(gCanvasClassInfo.mNativeCanvas, clazz, "mNativeCanvas", "J"); |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 247 | GET_FIELD_ID(gCanvasClassInfo.mSurfaceFormat, clazz, "mSurfaceFormat", "I"); |
| 248 | |
| 249 | FIND_CLASS(clazz, "android/graphics/Canvas$CanvasFinalizer"); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 250 | GET_FIELD_ID(gCanvasFinalizerClassInfo.mNativeCanvas, clazz, "mNativeCanvas", "J"); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 251 | |
| 252 | FIND_CLASS(clazz, "android/view/TextureView"); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 253 | GET_FIELD_ID(gTextureViewClassInfo.nativeWindow, clazz, "mNativeWindow", "J"); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 254 | |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 255 | return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 256 | } |
| 257 | |
| 258 | }; |