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