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