The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Mathias Agopian | fae5cb2 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 17 | #define LOG_TAG "Surface" |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 21 | #include "jni.h" |
| 22 | #include "JNIHelp.h" |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 23 | #include "android_os_Parcel.h" |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 24 | #include "android/graphics/GraphicsJNI.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 26 | #include <android_runtime/AndroidRuntime.h> |
| 27 | #include <android_runtime/android_view_Surface.h> |
| 28 | #include <android_runtime/android_graphics_SurfaceTexture.h> |
Ruben Brunk | 87eac99 | 2013-09-09 17:44:59 -0700 | [diff] [blame] | 29 | #include <android_runtime/Log.h> |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 30 | |
Mathias Agopian | 4a05f43 | 2013-03-12 18:43:34 -0700 | [diff] [blame] | 31 | #include <binder/Parcel.h> |
| 32 | |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 33 | #include <gui/Surface.h> |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 34 | #include <gui/SurfaceControl.h> |
Andy McFadden | d47f7d8 | 2012-12-18 09:48:38 -0800 | [diff] [blame] | 35 | #include <gui/GLConsumer.h> |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 36 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | #include <ui/Rect.h> |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 38 | #include <ui/Region.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| 40 | #include <SkCanvas.h> |
| 41 | #include <SkBitmap.h> |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 42 | #include <SkImage.h> |
Mathias Agopian | 6158b1b | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 43 | #include <SkRegion.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | #include <utils/misc.h> |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 46 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 48 | #include <ScopedUtfChars.h> |
| 49 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | // ---------------------------------------------------------------------------- |
| 51 | |
| 52 | namespace android { |
| 53 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | static const char* const OutOfResourcesException = |
| 55 | "android/view/Surface$OutOfResourcesException"; |
| 56 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 57 | static struct { |
| 58 | jclass clazz; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 59 | jfieldID mNativeObject; |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 60 | jfieldID mLock; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 61 | jmethodID ctor; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 62 | } gSurfaceClassInfo; |
tedbo | 0503161 | 2011-06-06 16:02:47 -0700 | [diff] [blame] | 63 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 64 | static struct { |
| 65 | jfieldID left; |
| 66 | jfieldID top; |
| 67 | jfieldID right; |
| 68 | jfieldID bottom; |
| 69 | } gRectClassInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 71 | static struct { |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 72 | jfieldID mFinalizer; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 73 | jfieldID mNativeCanvas; |
| 74 | jfieldID mSurfaceFormat; |
| 75 | } gCanvasClassInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 77 | static struct { |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 78 | jfieldID mNativeCanvas; |
| 79 | } gCanvasFinalizerClassInfo; |
| 80 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 81 | // ---------------------------------------------------------------------------- |
| 82 | |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 83 | // this is just a pointer we use to pass to inc/decStrong |
| 84 | static const void *sRefBaseOwner; |
| 85 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 86 | bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) { |
| 87 | return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz); |
| 88 | } |
| 89 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 90 | sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) { |
| 91 | return android_view_Surface_getSurface(env, surfaceObj); |
| 92 | } |
| 93 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 94 | sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 95 | sp<Surface> sur; |
| 96 | jobject lock = env->GetObjectField(surfaceObj, |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 97 | gSurfaceClassInfo.mLock); |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 98 | if (env->MonitorEnter(lock) == JNI_OK) { |
| 99 | sur = reinterpret_cast<Surface *>( |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 100 | env->GetLongField(surfaceObj, gSurfaceClassInfo.mNativeObject)); |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 101 | env->MonitorExit(lock); |
| 102 | } |
| 103 | return sur; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 106 | jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env, |
Andy McFadden | d47f7d8 | 2012-12-18 09:48:38 -0800 | [diff] [blame] | 107 | const sp<IGraphicBufferProducer>& bufferProducer) { |
| 108 | if (bufferProducer == NULL) { |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 109 | return NULL; |
| 110 | } |
| 111 | |
Mathias Agopian | e326326 | 2013-07-16 22:54:56 -0700 | [diff] [blame] | 112 | sp<Surface> surface(new Surface(bufferProducer, true)); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 113 | if (surface == NULL) { |
| 114 | return NULL; |
| 115 | } |
| 116 | |
Chong Zhang | af608e8 | 2014-01-29 12:52:15 -0800 | [diff] [blame] | 117 | jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz, |
| 118 | gSurfaceClassInfo.ctor, (jlong)surface.get()); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 119 | if (surfaceObj == NULL) { |
| 120 | if (env->ExceptionCheck()) { |
Andy McFadden | d47f7d8 | 2012-12-18 09:48:38 -0800 | [diff] [blame] | 121 | ALOGE("Could not create instance of Surface from IGraphicBufferProducer."); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 122 | LOGE_EX(env); |
| 123 | env->ExceptionClear(); |
| 124 | } |
| 125 | return NULL; |
| 126 | } |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 127 | surface->incStrong(&sRefBaseOwner); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 128 | return surfaceObj; |
| 129 | } |
| 130 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 131 | // ---------------------------------------------------------------------------- |
| 132 | |
Mathias Agopian | f3d3b4d | 2013-02-19 18:26:15 -0800 | [diff] [blame] | 133 | static inline bool isSurfaceValid(const sp<Surface>& sur) { |
| 134 | return Surface::isValid(sur); |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // ---------------------------------------------------------------------------- |
| 138 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 139 | static jlong nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz, |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 140 | jobject surfaceTextureObj) { |
Mathias Agopian | 52a9a10 | 2013-08-02 01:38:38 -0700 | [diff] [blame] | 141 | sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surfaceTextureObj)); |
| 142 | if (producer == NULL) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 143 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 144 | "SurfaceTexture has already been released"); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 145 | return 0; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Mathias Agopian | 52a9a10 | 2013-08-02 01:38:38 -0700 | [diff] [blame] | 148 | sp<Surface> surface(new Surface(producer, true)); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 149 | if (surface == NULL) { |
| 150 | jniThrowException(env, OutOfResourcesException, NULL); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 151 | return 0; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 154 | surface->incStrong(&sRefBaseOwner); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 155 | return jlong(surface.get()); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 158 | static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 159 | sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 160 | sur->decStrong(&sRefBaseOwner); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 163 | static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jlong nativeObject) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 164 | sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 165 | return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 168 | static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jlong nativeObject) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 169 | sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 170 | if (!isSurfaceValid(sur)) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 171 | doThrowIAE(env); |
| 172 | return JNI_FALSE; |
| 173 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 174 | int value = 0; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 175 | ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get()); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 176 | anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value); |
| 177 | return value; |
| 178 | } |
| 179 | |
| 180 | static inline SkBitmap::Config convertPixelFormat(PixelFormat format) { |
| 181 | /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then |
| 182 | we can map to SkBitmap::kARGB_8888_Config, and optionally call |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 183 | bitmap.setAlphaType(kOpaque_SkAlphaType) on the resulting SkBitmap |
| 184 | (as an accelerator) |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 185 | */ |
| 186 | switch (format) { |
| 187 | case PIXEL_FORMAT_RGBX_8888: return SkBitmap::kARGB_8888_Config; |
| 188 | case PIXEL_FORMAT_RGBA_8888: return SkBitmap::kARGB_8888_Config; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 189 | case PIXEL_FORMAT_RGB_565: return SkBitmap::kRGB_565_Config; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 190 | default: return SkBitmap::kNo_Config; |
| 191 | } |
| 192 | } |
| 193 | |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 194 | static inline void swapCanvasPtr(JNIEnv* env, jobject canvasObj, SkCanvas* newCanvas) { |
| 195 | jobject canvasFinalizerObj = env->GetObjectField(canvasObj, gCanvasClassInfo.mFinalizer); |
| 196 | SkCanvas* previousCanvas = reinterpret_cast<SkCanvas*>( |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 197 | env->GetLongField(canvasObj, gCanvasClassInfo.mNativeCanvas)); |
| 198 | env->SetLongField(canvasObj, gCanvasClassInfo.mNativeCanvas, (jlong)newCanvas); |
| 199 | env->SetLongField(canvasFinalizerObj, gCanvasFinalizerClassInfo.mNativeCanvas, (jlong)newCanvas); |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 200 | SkSafeUnref(previousCanvas); |
| 201 | } |
| 202 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 203 | static jlong nativeLockCanvas(JNIEnv* env, jclass clazz, |
| 204 | jlong nativeObject, jobject canvasObj, jobject dirtyRectObj) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 205 | sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); |
| 206 | |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 207 | if (!isSurfaceValid(surface)) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 208 | doThrowIAE(env); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 209 | return 0; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Mathias Agopian | 667809e | 2013-04-17 14:57:41 -0700 | [diff] [blame] | 212 | Rect dirtyRect; |
| 213 | Rect* dirtyRectPtr = NULL; |
| 214 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 215 | if (dirtyRectObj) { |
Mathias Agopian | 667809e | 2013-04-17 14:57:41 -0700 | [diff] [blame] | 216 | dirtyRect.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left); |
| 217 | dirtyRect.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top); |
| 218 | dirtyRect.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right); |
| 219 | dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom); |
| 220 | dirtyRectPtr = &dirtyRect; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 223 | ANativeWindow_Buffer outBuffer; |
Mathias Agopian | 667809e | 2013-04-17 14:57:41 -0700 | [diff] [blame] | 224 | status_t err = surface->lock(&outBuffer, dirtyRectPtr); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 225 | if (err < 0) { |
| 226 | const char* const exception = (err == NO_MEMORY) ? |
| 227 | OutOfResourcesException : |
| 228 | "java/lang/IllegalArgumentException"; |
| 229 | jniThrowException(env, exception, NULL); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 230 | return 0; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // Associate a SkCanvas object to this surface |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 234 | env->SetIntField(canvasObj, gCanvasClassInfo.mSurfaceFormat, outBuffer.format); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 235 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 236 | SkBitmap bitmap; |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 237 | ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format); |
| 238 | bitmap.setConfig(convertPixelFormat(outBuffer.format), outBuffer.width, outBuffer.height, bpr); |
| 239 | if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) { |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 240 | bitmap.setAlphaType(kOpaque_SkAlphaType); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 241 | } |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 242 | if (outBuffer.width > 0 && outBuffer.height > 0) { |
| 243 | bitmap.setPixels(outBuffer.bits); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 244 | } else { |
| 245 | // be safe with an empty bitmap. |
| 246 | bitmap.setPixels(NULL); |
| 247 | } |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 248 | |
| 249 | SkCanvas* nativeCanvas = SkNEW_ARGS(SkCanvas, (bitmap)); |
| 250 | swapCanvasPtr(env, canvasObj, nativeCanvas); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 251 | |
Mathias Agopian | 667809e | 2013-04-17 14:57:41 -0700 | [diff] [blame] | 252 | if (dirtyRectPtr) { |
| 253 | nativeCanvas->clipRect( SkRect::Make(reinterpret_cast<const SkIRect&>(dirtyRect)) ); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 256 | if (dirtyRectObj) { |
Mathias Agopian | 667809e | 2013-04-17 14:57:41 -0700 | [diff] [blame] | 257 | env->SetIntField(dirtyRectObj, gRectClassInfo.left, dirtyRect.left); |
| 258 | env->SetIntField(dirtyRectObj, gRectClassInfo.top, dirtyRect.top); |
| 259 | env->SetIntField(dirtyRectObj, gRectClassInfo.right, dirtyRect.right); |
| 260 | env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 261 | } |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 262 | |
| 263 | // Create another reference to the surface and return it. This reference |
| 264 | // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject, |
| 265 | // because the latter could be replaced while the surface is locked. |
| 266 | sp<Surface> lockedSurface(surface); |
| 267 | lockedSurface->incStrong(&sRefBaseOwner); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 268 | return (jlong) lockedSurface.get(); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 271 | static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 272 | jlong nativeObject, jobject canvasObj) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 273 | sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 274 | if (!isSurfaceValid(surface)) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 275 | return; |
| 276 | } |
| 277 | |
| 278 | // detach the canvas from the surface |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 279 | SkCanvas* nativeCanvas = SkNEW(SkCanvas); |
| 280 | swapCanvasPtr(env, canvasObj, nativeCanvas); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 281 | |
| 282 | // unlock surface |
| 283 | status_t err = surface->unlockAndPost(); |
| 284 | if (err < 0) { |
| 285 | doThrowIAE(env); |
| 286 | } |
| 287 | } |
| 288 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 289 | // ---------------------------------------------------------------------------- |
| 290 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 291 | static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz, |
| 292 | jlong surfaceControlNativeObj) { |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 293 | /* |
| 294 | * This is used by the WindowManagerService just after constructing |
| 295 | * a Surface and is necessary for returning the Surface reference to |
| 296 | * the caller. At this point, we should only have a SurfaceControl. |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 297 | */ |
Mathias Agopian | fae5cb2 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 298 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 299 | sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj)); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 300 | sp<Surface> surface(ctrl->getSurface()); |
| 301 | if (surface != NULL) { |
| 302 | surface->incStrong(&sRefBaseOwner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 304 | return reinterpret_cast<jlong>(surface.get()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 307 | static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz, |
| 308 | jlong nativeObject, jobject parcelObj) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 309 | Parcel* parcel = parcelForJavaObject(env, parcelObj); |
| 310 | if (parcel == NULL) { |
| 311 | doThrowNPE(env); |
| 312 | return 0; |
| 313 | } |
Mathias Agopian | c3b9cd6 | 2013-03-13 16:07:57 -0700 | [diff] [blame] | 314 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 315 | sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); |
Mathias Agopian | c3b9cd6 | 2013-03-13 16:07:57 -0700 | [diff] [blame] | 316 | sp<IBinder> binder(parcel->readStrongBinder()); |
| 317 | |
| 318 | // update the Surface only if the underlying IGraphicBufferProducer |
| 319 | // has changed. |
| 320 | if (self != NULL |
| 321 | && (self->getIGraphicBufferProducer()->asBinder() == binder)) { |
| 322 | // same IGraphicBufferProducer, return ourselves |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 323 | return jlong(self.get()); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 324 | } |
Mathias Agopian | 4a05f43 | 2013-03-12 18:43:34 -0700 | [diff] [blame] | 325 | |
| 326 | sp<Surface> sur; |
Mathias Agopian | c3b9cd6 | 2013-03-13 16:07:57 -0700 | [diff] [blame] | 327 | sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder)); |
Mathias Agopian | 4a05f43 | 2013-03-12 18:43:34 -0700 | [diff] [blame] | 328 | if (gbp != NULL) { |
Mathias Agopian | c3b9cd6 | 2013-03-13 16:07:57 -0700 | [diff] [blame] | 329 | // we have a new IGraphicBufferProducer, create a new Surface for it |
Mathias Agopian | e326326 | 2013-07-16 22:54:56 -0700 | [diff] [blame] | 330 | sur = new Surface(gbp, true); |
Mathias Agopian | c3b9cd6 | 2013-03-13 16:07:57 -0700 | [diff] [blame] | 331 | // and keep a reference before passing to java |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 332 | sur->incStrong(&sRefBaseOwner); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 333 | } |
Mathias Agopian | 4a05f43 | 2013-03-12 18:43:34 -0700 | [diff] [blame] | 334 | |
Mathias Agopian | c3b9cd6 | 2013-03-13 16:07:57 -0700 | [diff] [blame] | 335 | if (self != NULL) { |
| 336 | // and loose the java reference to ourselves |
| 337 | self->decStrong(&sRefBaseOwner); |
| 338 | } |
| 339 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 340 | return jlong(sur.get()); |
Dianne Hackborn | 61566cc | 2011-12-02 23:31:52 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 343 | static void nativeWriteToParcel(JNIEnv* env, jclass clazz, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 344 | jlong nativeObject, jobject parcelObj) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 345 | Parcel* parcel = parcelForJavaObject(env, parcelObj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | if (parcel == NULL) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 347 | doThrowNPE(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | return; |
| 349 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 350 | sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); |
Mathias Agopian | 4a05f43 | 2013-03-12 18:43:34 -0700 | [diff] [blame] | 351 | parcel->writeStrongBinder( self != 0 ? self->getIGraphicBufferProducer()->asBinder() : NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | |
| 356 | static JNINativeMethod gSurfaceMethods[] = { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 357 | {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)J", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 358 | (void*)nativeCreateFromSurfaceTexture }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 359 | {"nativeRelease", "(J)V", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 360 | (void*)nativeRelease }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 361 | {"nativeIsValid", "(J)Z", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 362 | (void*)nativeIsValid }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 363 | {"nativeIsConsumerRunningBehind", "(J)Z", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 364 | (void*)nativeIsConsumerRunningBehind }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 365 | {"nativeLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)J", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 366 | (void*)nativeLockCanvas }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 367 | {"nativeUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 368 | (void*)nativeUnlockCanvasAndPost }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 369 | {"nativeCreateFromSurfaceControl", "(J)J", |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 370 | (void*)nativeCreateFromSurfaceControl }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 371 | {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 372 | (void*)nativeReadFromParcel }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 373 | {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 374 | (void*)nativeWriteToParcel }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | }; |
| 376 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | int register_android_view_Surface(JNIEnv* env) |
| 378 | { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 379 | int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | gSurfaceMethods, NELEM(gSurfaceMethods)); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 381 | |
| 382 | jclass clazz = env->FindClass("android/view/Surface"); |
| 383 | gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz)); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 384 | gSurfaceClassInfo.mNativeObject = |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 385 | env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "J"); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 386 | gSurfaceClassInfo.mLock = |
| 387 | env->GetFieldID(gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;"); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 388 | gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(J)V"); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 389 | |
| 390 | clazz = env->FindClass("android/graphics/Canvas"); |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 391 | gCanvasClassInfo.mFinalizer = env->GetFieldID(clazz, "mFinalizer", "Landroid/graphics/Canvas$CanvasFinalizer;"); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 392 | gCanvasClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "J"); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 393 | gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I"); |
| 394 | |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 395 | clazz = env->FindClass("android/graphics/Canvas$CanvasFinalizer"); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 396 | gCanvasFinalizerClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "J"); |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 397 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 398 | clazz = env->FindClass("android/graphics/Rect"); |
| 399 | gRectClassInfo.left = env->GetFieldID(clazz, "left", "I"); |
| 400 | gRectClassInfo.top = env->GetFieldID(clazz, "top", "I"); |
| 401 | gRectClassInfo.right = env->GetFieldID(clazz, "right", "I"); |
| 402 | gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I"); |
| 403 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | return err; |
| 405 | } |
| 406 | |
| 407 | }; |