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 | |
Florin Malita | 5c3d927 | 2014-05-08 10:35:36 -0400 | [diff] [blame] | 32 | #include "android/graphics/GraphicsJNI.h" |
| 33 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 34 | #include "core_jni_helpers.h" |
| 35 | |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 36 | namespace android { |
| 37 | |
| 38 | // ---------------------------------------------------------------------------- |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 39 | // JNI Glue |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | |
| 42 | static struct { |
| 43 | jmethodID set; |
| 44 | jfieldID left; |
| 45 | jfieldID top; |
| 46 | jfieldID right; |
| 47 | jfieldID bottom; |
| 48 | } gRectClassInfo; |
| 49 | |
| 50 | static struct { |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 51 | jfieldID nativeWindow; |
| 52 | } gTextureViewClassInfo; |
| 53 | |
| 54 | #define GET_INT(object, field) \ |
| 55 | env->GetIntField(object, field) |
| 56 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 57 | #define GET_LONG(object, field) \ |
| 58 | env->GetLongField(object, field) |
| 59 | |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 60 | #define SET_INT(object, field, value) \ |
| 61 | env->SetIntField(object, field, value) |
| 62 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 63 | #define SET_LONG(object, field, value) \ |
| 64 | env->SetLongField(object, field, value) |
| 65 | |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 66 | #define INVOKEV(object, method, ...) \ |
| 67 | env->CallVoidMethod(object, method, __VA_ARGS__) |
| 68 | |
| 69 | // ---------------------------------------------------------------------------- |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 70 | // Native layer |
| 71 | // ---------------------------------------------------------------------------- |
| 72 | |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 73 | // FIXME: consider exporting this to share (e.g. android_view_Surface.cpp) |
| 74 | static inline SkImageInfo convertPixelFormat(const ANativeWindow_Buffer& buffer) { |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 75 | SkColorType colorType = kUnknown_SkColorType; |
| 76 | SkAlphaType alphaType = kOpaque_SkAlphaType; |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 77 | switch (buffer.format) { |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 78 | case WINDOW_FORMAT_RGBA_8888: |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 79 | colorType = kN32_SkColorType; |
| 80 | alphaType = kPremul_SkAlphaType; |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 81 | break; |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 82 | case WINDOW_FORMAT_RGBX_8888: |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 83 | colorType = kN32_SkColorType; |
| 84 | alphaType = kOpaque_SkAlphaType; |
Mike Reed | 73f9c7d | 2015-02-27 09:59:37 -0500 | [diff] [blame] | 85 | break; |
Jesse Hall | 7e7d0d5 | 2017-04-19 15:49:33 -0700 | [diff] [blame] | 86 | case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: |
Romain Guy | 9505a65 | 2016-12-14 09:43:50 -0800 | [diff] [blame] | 87 | colorType = kRGBA_F16_SkColorType; |
| 88 | alphaType = kPremul_SkAlphaType; |
| 89 | break; |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 90 | case WINDOW_FORMAT_RGB_565: |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 91 | colorType = kRGB_565_SkColorType; |
| 92 | alphaType = kOpaque_SkAlphaType; |
Mike Reed | 73f9c7d | 2015-02-27 09:59:37 -0500 | [diff] [blame] | 93 | break; |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 94 | default: |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 95 | break; |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 96 | } |
Derek Sollenberger | b100231 | 2018-08-02 10:15:09 -0400 | [diff] [blame] | 97 | return SkImageInfo::Make(buffer.width, buffer.height, colorType, alphaType); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * This is a private API, and this implementation is also provided in the NDK. |
| 102 | * However, the NDK links against android_runtime, which means that using the |
| 103 | * NDK implementation would create a circular dependency between the libraries. |
| 104 | */ |
| 105 | static int32_t native_window_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer, |
| 106 | Rect* inOutDirtyBounds) { |
| 107 | return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds); |
| 108 | } |
| 109 | |
| 110 | static int32_t native_window_unlockAndPost(ANativeWindow* window) { |
| 111 | return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST); |
| 112 | } |
| 113 | |
| 114 | static void android_view_TextureView_createNativeWindow(JNIEnv* env, jobject textureView, |
| 115 | jobject surface) { |
| 116 | |
Mathias Agopian | 52a9a10 | 2013-08-02 01:38:38 -0700 | [diff] [blame] | 117 | sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surface)); |
Mathias Agopian | 24f43c9 | 2013-08-06 20:16:12 -0700 | [diff] [blame] | 118 | sp<ANativeWindow> window = new Surface(producer, true); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 119 | |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 120 | window->incStrong((void*)android_view_TextureView_createNativeWindow); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 121 | SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, jlong(window.get())); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void android_view_TextureView_destroyNativeWindow(JNIEnv* env, jobject textureView) { |
| 125 | |
| 126 | ANativeWindow* nativeWindow = (ANativeWindow*) |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 127 | GET_LONG(textureView, gTextureViewClassInfo.nativeWindow); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 128 | |
| 129 | if (nativeWindow) { |
| 130 | sp<ANativeWindow> window(nativeWindow); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 131 | window->decStrong((void*)android_view_TextureView_createNativeWindow); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 132 | SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, 0); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Romain Guy | 53bacf5 | 2013-04-30 11:30:10 -0700 | [diff] [blame] | 136 | static jboolean android_view_TextureView_lockCanvas(JNIEnv* env, jobject, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 137 | jlong nativeWindow, jobject canvas, jobject dirtyRect) { |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 138 | |
| 139 | if (!nativeWindow) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 140 | return JNI_FALSE; |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | ANativeWindow_Buffer buffer; |
| 144 | |
Pablo Ceballos | 8270292 | 2015-08-07 17:28:03 -0700 | [diff] [blame] | 145 | Rect rect(Rect::EMPTY_RECT); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 146 | if (dirtyRect) { |
| 147 | rect.left = GET_INT(dirtyRect, gRectClassInfo.left); |
| 148 | rect.top = GET_INT(dirtyRect, gRectClassInfo.top); |
| 149 | rect.right = GET_INT(dirtyRect, gRectClassInfo.right); |
| 150 | rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom); |
| 151 | } else { |
| 152 | rect.set(Rect(0x3FFF, 0x3FFF)); |
| 153 | } |
| 154 | |
| 155 | sp<ANativeWindow> window((ANativeWindow*) nativeWindow); |
Romain Guy | 53bacf5 | 2013-04-30 11:30:10 -0700 | [diff] [blame] | 156 | int32_t status = native_window_lock(window.get(), &buffer, &rect); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 157 | if (status) return JNI_FALSE; |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 158 | |
| 159 | ssize_t bytesCount = buffer.stride * bytesPerPixel(buffer.format); |
| 160 | |
| 161 | SkBitmap bitmap; |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 162 | bitmap.setInfo(convertPixelFormat(buffer), bytesCount); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 163 | |
| 164 | if (buffer.width > 0 && buffer.height > 0) { |
| 165 | bitmap.setPixels(buffer.bits); |
| 166 | } else { |
| 167 | bitmap.setPixels(NULL); |
| 168 | } |
| 169 | |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 170 | Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvas); |
| 171 | nativeCanvas->setBitmap(bitmap); |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 172 | nativeCanvas->clipRect(rect.left, rect.top, rect.right, rect.bottom, |
Mike Reed | 6c67f1d | 2016-12-14 10:29:54 -0500 | [diff] [blame] | 173 | SkClipOp::kIntersect); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 174 | |
| 175 | if (dirtyRect) { |
| 176 | INVOKEV(dirtyRect, gRectClassInfo.set, |
| 177 | int(rect.left), int(rect.top), int(rect.right), int(rect.bottom)); |
| 178 | } |
Romain Guy | 53bacf5 | 2013-04-30 11:30:10 -0700 | [diff] [blame] | 179 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 180 | return JNI_TRUE; |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void android_view_TextureView_unlockCanvasAndPost(JNIEnv* env, jobject, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 184 | jlong nativeWindow, jobject canvas) { |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 185 | |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 186 | Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvas); |
| 187 | nativeCanvas->setBitmap(SkBitmap()); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 188 | |
| 189 | if (nativeWindow) { |
| 190 | sp<ANativeWindow> window((ANativeWindow*) nativeWindow); |
| 191 | native_window_unlockAndPost(window.get()); |
| 192 | } |
| 193 | } |
| 194 | |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 195 | // ---------------------------------------------------------------------------- |
| 196 | // JNI Glue |
| 197 | // ---------------------------------------------------------------------------- |
| 198 | |
| 199 | const char* const kClassPathName = "android/view/TextureView"; |
| 200 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 201 | static const JNINativeMethod gMethods[] = { |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 202 | { "nCreateNativeWindow", "(Landroid/graphics/SurfaceTexture;)V", |
| 203 | (void*) android_view_TextureView_createNativeWindow }, |
| 204 | { "nDestroyNativeWindow", "()V", |
| 205 | (void*) android_view_TextureView_destroyNativeWindow }, |
| 206 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 207 | { "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z", |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 208 | (void*) android_view_TextureView_lockCanvas }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 209 | { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V", |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 210 | (void*) android_view_TextureView_unlockCanvasAndPost }, |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | int register_android_view_TextureView(JNIEnv* env) { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 214 | jclass clazz = FindClassOrDie(env, "android/graphics/Rect"); |
| 215 | gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V"); |
| 216 | gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I"); |
| 217 | gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I"); |
| 218 | gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I"); |
| 219 | gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I"); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 220 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 221 | clazz = FindClassOrDie(env, "android/view/TextureView"); |
| 222 | gTextureViewClassInfo.nativeWindow = GetFieldIDOrDie(env, clazz, "mNativeWindow", "J"); |
Romain Guy | 6be3d55 | 2011-07-14 18:08:37 -0700 | [diff] [blame] | 223 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 224 | return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | }; |