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