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" |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 22 | #include <nativehelper/JNIHelp.h> |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 23 | #include "android_os_Parcel.h" |
Robert Carr | a35e5de | 2017-03-03 17:04:54 -0800 | [diff] [blame] | 24 | #include "android/graphics/GraphicBuffer.h" |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 25 | #include "android/graphics/GraphicsJNI.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 27 | #include "core_jni_helpers.h" |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 28 | #include <android_runtime/android_view_Surface.h> |
| 29 | #include <android_runtime/android_graphics_SurfaceTexture.h> |
Ruben Brunk | 87eac99 | 2013-09-09 17:44:59 -0700 | [diff] [blame] | 30 | #include <android_runtime/Log.h> |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 31 | |
Mathias Agopian | 4a05f43 | 2013-03-12 18:43:34 -0700 | [diff] [blame] | 32 | #include <binder/Parcel.h> |
| 33 | |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 34 | #include <gui/Surface.h> |
Mathias Agopian | 7a1e28d | 2017-02-08 17:07:13 -0800 | [diff] [blame] | 35 | #include <gui/view/Surface.h> |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 36 | #include <gui/SurfaceControl.h> |
Andy McFadden | d47f7d8 | 2012-12-18 09:48:38 -0800 | [diff] [blame] | 37 | #include <gui/GLConsumer.h> |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 38 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | #include <ui/Rect.h> |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 40 | #include <ui/Region.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| 42 | #include <SkCanvas.h> |
| 43 | #include <SkBitmap.h> |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 44 | #include <SkImage.h> |
Mathias Agopian | 6158b1b | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 45 | #include <SkRegion.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | #include <utils/misc.h> |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 48 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 50 | #include <nativehelper/ScopedUtfChars.h> |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 51 | |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 52 | #include <AnimationContext.h> |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 53 | #include <FrameInfo.h> |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 54 | #include <RenderNode.h> |
| 55 | #include <renderthread/RenderProxy.h> |
| 56 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | // ---------------------------------------------------------------------------- |
| 58 | |
| 59 | namespace android { |
| 60 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | static const char* const OutOfResourcesException = |
| 62 | "android/view/Surface$OutOfResourcesException"; |
| 63 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 64 | static struct { |
| 65 | jclass clazz; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 66 | jfieldID mNativeObject; |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 67 | jfieldID mLock; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 68 | jmethodID ctor; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 69 | } gSurfaceClassInfo; |
tedbo | 0503161 | 2011-06-06 16:02:47 -0700 | [diff] [blame] | 70 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 71 | static struct { |
| 72 | jfieldID left; |
| 73 | jfieldID top; |
| 74 | jfieldID right; |
| 75 | jfieldID bottom; |
| 76 | } gRectClassInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 78 | // ---------------------------------------------------------------------------- |
| 79 | |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 80 | // this is just a pointer we use to pass to inc/decStrong |
| 81 | static const void *sRefBaseOwner; |
| 82 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 83 | bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) { |
| 84 | return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz); |
| 85 | } |
| 86 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 87 | sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) { |
| 88 | return android_view_Surface_getSurface(env, surfaceObj); |
| 89 | } |
| 90 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 91 | sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) { |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 92 | sp<Surface> sur; |
| 93 | jobject lock = env->GetObjectField(surfaceObj, |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 94 | gSurfaceClassInfo.mLock); |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 95 | if (env->MonitorEnter(lock) == JNI_OK) { |
| 96 | sur = reinterpret_cast<Surface *>( |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 97 | env->GetLongField(surfaceObj, gSurfaceClassInfo.mNativeObject)); |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 98 | env->MonitorExit(lock); |
| 99 | } |
Andy McFadden | 5ad3ab8 | 2014-06-11 15:22:32 -0700 | [diff] [blame] | 100 | env->DeleteLocalRef(lock); |
Mathias Agopian | 7c116b5 | 2013-03-18 20:27:02 -0700 | [diff] [blame] | 101 | return sur; |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Jiwen 'Steve' Cai | d28e828 | 2017-03-31 11:19:50 -0700 | [diff] [blame] | 104 | jobject android_view_Surface_createFromSurface(JNIEnv* env, const sp<Surface>& surface) { |
Chong Zhang | af608e8 | 2014-01-29 12:52:15 -0800 | [diff] [blame] | 105 | jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz, |
| 106 | gSurfaceClassInfo.ctor, (jlong)surface.get()); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 107 | if (surfaceObj == NULL) { |
| 108 | if (env->ExceptionCheck()) { |
Andy McFadden | d47f7d8 | 2012-12-18 09:48:38 -0800 | [diff] [blame] | 109 | ALOGE("Could not create instance of Surface from IGraphicBufferProducer."); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 110 | LOGE_EX(env); |
| 111 | env->ExceptionClear(); |
| 112 | } |
| 113 | return NULL; |
| 114 | } |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 115 | surface->incStrong(&sRefBaseOwner); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 116 | return surfaceObj; |
| 117 | } |
| 118 | |
Jiwen 'Steve' Cai | d28e828 | 2017-03-31 11:19:50 -0700 | [diff] [blame] | 119 | jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env, |
| 120 | const sp<IGraphicBufferProducer>& bufferProducer) { |
| 121 | if (bufferProducer == NULL) { |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | sp<Surface> surface(new Surface(bufferProducer, true)); |
| 126 | return android_view_Surface_createFromSurface(env, surface); |
| 127 | } |
| 128 | |
Eino-Ville Talvala | 805f3c9 | 2015-02-26 10:57:55 -0800 | [diff] [blame] | 129 | int android_view_Surface_mapPublicFormatToHalFormat(PublicFormat f) { |
| 130 | |
| 131 | switch(f) { |
| 132 | case PublicFormat::JPEG: |
| 133 | case PublicFormat::DEPTH_POINT_CLOUD: |
| 134 | return HAL_PIXEL_FORMAT_BLOB; |
| 135 | case PublicFormat::DEPTH16: |
| 136 | return HAL_PIXEL_FORMAT_Y16; |
| 137 | case PublicFormat::RAW_SENSOR: |
Emilian Peev | f7fec73 | 2017-03-17 19:54:30 +0000 | [diff] [blame] | 138 | case PublicFormat::RAW_DEPTH: |
Eino-Ville Talvala | 805f3c9 | 2015-02-26 10:57:55 -0800 | [diff] [blame] | 139 | return HAL_PIXEL_FORMAT_RAW16; |
| 140 | default: |
| 141 | // Most formats map 1:1 |
| 142 | return static_cast<int>(f); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | android_dataspace android_view_Surface_mapPublicFormatToHalDataspace( |
| 147 | PublicFormat f) { |
| 148 | switch(f) { |
| 149 | case PublicFormat::JPEG: |
Eino-Ville Talvala | 7966d44 | 2016-03-14 13:21:03 -0700 | [diff] [blame] | 150 | return HAL_DATASPACE_V0_JFIF; |
Eino-Ville Talvala | 805f3c9 | 2015-02-26 10:57:55 -0800 | [diff] [blame] | 151 | case PublicFormat::DEPTH_POINT_CLOUD: |
| 152 | case PublicFormat::DEPTH16: |
Emilian Peev | f7fec73 | 2017-03-17 19:54:30 +0000 | [diff] [blame] | 153 | case PublicFormat::RAW_DEPTH: |
Eino-Ville Talvala | 805f3c9 | 2015-02-26 10:57:55 -0800 | [diff] [blame] | 154 | return HAL_DATASPACE_DEPTH; |
| 155 | case PublicFormat::RAW_SENSOR: |
Yin-Chia Yeh | 44581ff | 2015-12-07 17:15:24 -0800 | [diff] [blame] | 156 | case PublicFormat::RAW_PRIVATE: |
Eino-Ville Talvala | 805f3c9 | 2015-02-26 10:57:55 -0800 | [diff] [blame] | 157 | case PublicFormat::RAW10: |
Yin-Chia Yeh | 44581ff | 2015-12-07 17:15:24 -0800 | [diff] [blame] | 158 | case PublicFormat::RAW12: |
Eino-Ville Talvala | 805f3c9 | 2015-02-26 10:57:55 -0800 | [diff] [blame] | 159 | return HAL_DATASPACE_ARBITRARY; |
| 160 | case PublicFormat::YUV_420_888: |
| 161 | case PublicFormat::NV21: |
| 162 | case PublicFormat::YV12: |
Eino-Ville Talvala | 7966d44 | 2016-03-14 13:21:03 -0700 | [diff] [blame] | 163 | return HAL_DATASPACE_V0_JFIF; |
Eino-Ville Talvala | 805f3c9 | 2015-02-26 10:57:55 -0800 | [diff] [blame] | 164 | default: |
| 165 | // Most formats map to UNKNOWN |
| 166 | return HAL_DATASPACE_UNKNOWN; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | PublicFormat android_view_Surface_mapHalFormatDataspaceToPublicFormat( |
| 171 | int format, android_dataspace dataSpace) { |
| 172 | switch(format) { |
| 173 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 174 | case HAL_PIXEL_FORMAT_RGBX_8888: |
Romain Guy | 9505a65 | 2016-12-14 09:43:50 -0800 | [diff] [blame] | 175 | case HAL_PIXEL_FORMAT_RGBA_FP16: |
Romain Guy | 8ec2106 | 2017-02-10 18:49:33 -0800 | [diff] [blame] | 176 | case HAL_PIXEL_FORMAT_RGBA_1010102: |
Eino-Ville Talvala | 805f3c9 | 2015-02-26 10:57:55 -0800 | [diff] [blame] | 177 | case HAL_PIXEL_FORMAT_RGB_888: |
| 178 | case HAL_PIXEL_FORMAT_RGB_565: |
| 179 | case HAL_PIXEL_FORMAT_Y8: |
| 180 | case HAL_PIXEL_FORMAT_RAW10: |
Yin-Chia Yeh | 44581ff | 2015-12-07 17:15:24 -0800 | [diff] [blame] | 181 | case HAL_PIXEL_FORMAT_RAW12: |
Eino-Ville Talvala | 805f3c9 | 2015-02-26 10:57:55 -0800 | [diff] [blame] | 182 | case HAL_PIXEL_FORMAT_YCbCr_420_888: |
| 183 | case HAL_PIXEL_FORMAT_YV12: |
| 184 | // Enums overlap in both name and value |
| 185 | return static_cast<PublicFormat>(format); |
| 186 | case HAL_PIXEL_FORMAT_RAW16: |
Emilian Peev | f7fec73 | 2017-03-17 19:54:30 +0000 | [diff] [blame] | 187 | switch (dataSpace) { |
| 188 | case HAL_DATASPACE_DEPTH: |
| 189 | return PublicFormat::RAW_DEPTH; |
| 190 | default: |
| 191 | return PublicFormat::RAW_SENSOR; |
| 192 | } |
Yin-Chia Yeh | 44581ff | 2015-12-07 17:15:24 -0800 | [diff] [blame] | 193 | case HAL_PIXEL_FORMAT_RAW_OPAQUE: |
| 194 | // Name differs, though value is the same |
| 195 | return PublicFormat::RAW_PRIVATE; |
Eino-Ville Talvala | 805f3c9 | 2015-02-26 10:57:55 -0800 | [diff] [blame] | 196 | case HAL_PIXEL_FORMAT_YCbCr_422_SP: |
| 197 | // Name differs, though the value is the same |
| 198 | return PublicFormat::NV16; |
| 199 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 200 | // Name differs, though the value is the same |
| 201 | return PublicFormat::NV21; |
| 202 | case HAL_PIXEL_FORMAT_YCbCr_422_I: |
| 203 | // Name differs, though the value is the same |
| 204 | return PublicFormat::YUY2; |
Zhijun He | 2f17431 | 2015-03-20 11:39:55 -0700 | [diff] [blame] | 205 | case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: |
| 206 | // Name differs, though the value is the same |
| 207 | return PublicFormat::PRIVATE; |
Eino-Ville Talvala | 805f3c9 | 2015-02-26 10:57:55 -0800 | [diff] [blame] | 208 | case HAL_PIXEL_FORMAT_Y16: |
| 209 | // Dataspace-dependent |
| 210 | switch (dataSpace) { |
| 211 | case HAL_DATASPACE_DEPTH: |
| 212 | return PublicFormat::DEPTH16; |
| 213 | default: |
| 214 | // Assume non-depth Y16 is just Y16. |
| 215 | return PublicFormat::Y16; |
| 216 | } |
| 217 | break; |
| 218 | case HAL_PIXEL_FORMAT_BLOB: |
| 219 | // Dataspace-dependent |
| 220 | switch (dataSpace) { |
| 221 | case HAL_DATASPACE_DEPTH: |
| 222 | return PublicFormat::DEPTH_POINT_CLOUD; |
Eino-Ville Talvala | 7966d44 | 2016-03-14 13:21:03 -0700 | [diff] [blame] | 223 | case HAL_DATASPACE_V0_JFIF: |
Eino-Ville Talvala | 805f3c9 | 2015-02-26 10:57:55 -0800 | [diff] [blame] | 224 | return PublicFormat::JPEG; |
| 225 | default: |
| 226 | // Assume otherwise-marked blobs are also JPEG |
| 227 | return PublicFormat::JPEG; |
| 228 | } |
| 229 | break; |
| 230 | case HAL_PIXEL_FORMAT_BGRA_8888: |
Eino-Ville Talvala | 805f3c9 | 2015-02-26 10:57:55 -0800 | [diff] [blame] | 231 | // Not defined in public API |
| 232 | return PublicFormat::UNKNOWN; |
| 233 | |
| 234 | default: |
| 235 | return PublicFormat::UNKNOWN; |
| 236 | } |
| 237 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 238 | // ---------------------------------------------------------------------------- |
| 239 | |
Mathias Agopian | f3d3b4d | 2013-02-19 18:26:15 -0800 | [diff] [blame] | 240 | static inline bool isSurfaceValid(const sp<Surface>& sur) { |
| 241 | return Surface::isValid(sur); |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // ---------------------------------------------------------------------------- |
| 245 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 246 | static jlong nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz, |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 247 | jobject surfaceTextureObj) { |
Mathias Agopian | 52a9a10 | 2013-08-02 01:38:38 -0700 | [diff] [blame] | 248 | sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surfaceTextureObj)); |
| 249 | if (producer == NULL) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 250 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 251 | "SurfaceTexture has already been released"); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 252 | return 0; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Mathias Agopian | 52a9a10 | 2013-08-02 01:38:38 -0700 | [diff] [blame] | 255 | sp<Surface> surface(new Surface(producer, true)); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 256 | if (surface == NULL) { |
| 257 | jniThrowException(env, OutOfResourcesException, NULL); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 258 | return 0; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 261 | surface->incStrong(&sRefBaseOwner); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 262 | return jlong(surface.get()); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 265 | static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 266 | sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 267 | sur->decStrong(&sRefBaseOwner); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 270 | static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jlong nativeObject) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 271 | sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 272 | return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 275 | static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jlong nativeObject) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 276 | sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 277 | if (!isSurfaceValid(sur)) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 278 | doThrowIAE(env); |
| 279 | return JNI_FALSE; |
| 280 | } |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 281 | int value = 0; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 282 | ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get()); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 283 | anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value); |
| 284 | return value; |
| 285 | } |
| 286 | |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 287 | static inline SkColorType convertPixelFormat(PixelFormat format) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 288 | /* 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] | 289 | we can map to kN32_SkColorType, and optionally call |
Leon Scroggins III | 8790be6 | 2013-12-03 16:26:51 -0500 | [diff] [blame] | 290 | bitmap.setAlphaType(kOpaque_SkAlphaType) on the resulting SkBitmap |
| 291 | (as an accelerator) |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 292 | */ |
| 293 | switch (format) { |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 294 | case PIXEL_FORMAT_RGBX_8888: return kN32_SkColorType; |
| 295 | case PIXEL_FORMAT_RGBA_8888: return kN32_SkColorType; |
Romain Guy | 9505a65 | 2016-12-14 09:43:50 -0800 | [diff] [blame] | 296 | case PIXEL_FORMAT_RGBA_FP16: return kRGBA_F16_SkColorType; |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 297 | case PIXEL_FORMAT_RGB_565: return kRGB_565_SkColorType; |
| 298 | default: return kUnknown_SkColorType; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 302 | static jlong nativeLockCanvas(JNIEnv* env, jclass clazz, |
| 303 | jlong nativeObject, jobject canvasObj, jobject dirtyRectObj) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 304 | sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); |
| 305 | |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 306 | if (!isSurfaceValid(surface)) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 307 | doThrowIAE(env); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 308 | return 0; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Pablo Ceballos | 8270292 | 2015-08-07 17:28:03 -0700 | [diff] [blame] | 311 | Rect dirtyRect(Rect::EMPTY_RECT); |
Mathias Agopian | 667809e | 2013-04-17 14:57:41 -0700 | [diff] [blame] | 312 | Rect* dirtyRectPtr = NULL; |
| 313 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 314 | if (dirtyRectObj) { |
Mathias Agopian | 667809e | 2013-04-17 14:57:41 -0700 | [diff] [blame] | 315 | dirtyRect.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left); |
| 316 | dirtyRect.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top); |
| 317 | dirtyRect.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right); |
| 318 | dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom); |
| 319 | dirtyRectPtr = &dirtyRect; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 322 | ANativeWindow_Buffer outBuffer; |
Mathias Agopian | 667809e | 2013-04-17 14:57:41 -0700 | [diff] [blame] | 323 | status_t err = surface->lock(&outBuffer, dirtyRectPtr); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 324 | if (err < 0) { |
| 325 | const char* const exception = (err == NO_MEMORY) ? |
| 326 | OutOfResourcesException : |
| 327 | "java/lang/IllegalArgumentException"; |
| 328 | jniThrowException(env, exception, NULL); |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 329 | return 0; |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 332 | SkImageInfo info = SkImageInfo::Make(outBuffer.width, outBuffer.height, |
| 333 | convertPixelFormat(outBuffer.format), |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 334 | outBuffer.format == PIXEL_FORMAT_RGBX_8888 |
| 335 | ? kOpaque_SkAlphaType : kPremul_SkAlphaType, |
| 336 | GraphicsJNI::defaultColorSpace()); |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 337 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 338 | SkBitmap bitmap; |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 339 | ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format); |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 340 | bitmap.setInfo(info, bpr); |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 341 | if (outBuffer.width > 0 && outBuffer.height > 0) { |
| 342 | bitmap.setPixels(outBuffer.bits); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 343 | } else { |
| 344 | // be safe with an empty bitmap. |
| 345 | bitmap.setPixels(NULL); |
| 346 | } |
Derek Sollenberger | fc615a0 | 2012-12-20 14:39:57 -0500 | [diff] [blame] | 347 | |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 348 | Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj); |
| 349 | nativeCanvas->setBitmap(bitmap); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 350 | |
Mathias Agopian | 667809e | 2013-04-17 14:57:41 -0700 | [diff] [blame] | 351 | if (dirtyRectPtr) { |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 352 | nativeCanvas->clipRect(dirtyRect.left, dirtyRect.top, |
Mike Reed | 6c67f1d | 2016-12-14 10:29:54 -0500 | [diff] [blame] | 353 | dirtyRect.right, dirtyRect.bottom, SkClipOp::kIntersect); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 356 | if (dirtyRectObj) { |
Mathias Agopian | 667809e | 2013-04-17 14:57:41 -0700 | [diff] [blame] | 357 | env->SetIntField(dirtyRectObj, gRectClassInfo.left, dirtyRect.left); |
| 358 | env->SetIntField(dirtyRectObj, gRectClassInfo.top, dirtyRect.top); |
| 359 | env->SetIntField(dirtyRectObj, gRectClassInfo.right, dirtyRect.right); |
| 360 | env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 361 | } |
Andy McFadden | ed55c8d | 2013-08-20 10:05:51 -0700 | [diff] [blame] | 362 | |
| 363 | // Create another reference to the surface and return it. This reference |
| 364 | // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject, |
| 365 | // because the latter could be replaced while the surface is locked. |
| 366 | sp<Surface> lockedSurface(surface); |
| 367 | lockedSurface->incStrong(&sRefBaseOwner); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 368 | return (jlong) lockedSurface.get(); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 371 | static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 372 | jlong nativeObject, jobject canvasObj) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 373 | sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); |
Mathias Agopian | 5280061 | 2013-02-14 17:11:20 -0800 | [diff] [blame] | 374 | if (!isSurfaceValid(surface)) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 375 | return; |
| 376 | } |
| 377 | |
| 378 | // detach the canvas from the surface |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 379 | Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj); |
| 380 | nativeCanvas->setBitmap(SkBitmap()); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 381 | |
| 382 | // unlock surface |
| 383 | status_t err = surface->unlockAndPost(); |
| 384 | if (err < 0) { |
| 385 | doThrowIAE(env); |
| 386 | } |
| 387 | } |
| 388 | |
Dan Stoza | 5795d64 | 2014-06-20 13:01:36 -0700 | [diff] [blame] | 389 | static void nativeAllocateBuffers(JNIEnv* /* env */ , jclass /* clazz */, |
| 390 | jlong nativeObject) { |
| 391 | sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); |
| 392 | if (!isSurfaceValid(surface)) { |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | surface->allocateBuffers(); |
| 397 | } |
| 398 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 399 | // ---------------------------------------------------------------------------- |
| 400 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 401 | static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz, |
| 402 | jlong surfaceControlNativeObj) { |
Bryce Lee | 02949f1 | 2017-06-16 07:20:34 -0700 | [diff] [blame] | 403 | sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj)); |
| 404 | sp<Surface> surface(ctrl->createSurface()); |
| 405 | if (surface != NULL) { |
| 406 | surface->incStrong(&sRefBaseOwner); |
| 407 | } |
| 408 | return reinterpret_cast<jlong>(surface.get()); |
| 409 | } |
| 410 | |
| 411 | static jlong nativeGetFromSurfaceControl(JNIEnv* env, jclass clazz, |
| 412 | jlong surfaceControlNativeObj) { |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 413 | /* |
| 414 | * This is used by the WindowManagerService just after constructing |
| 415 | * a Surface and is necessary for returning the Surface reference to |
| 416 | * the caller. At this point, we should only have a SurfaceControl. |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 417 | */ |
Mathias Agopian | fae5cb2 | 2010-06-04 18:26:32 -0700 | [diff] [blame] | 418 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 419 | sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj)); |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 420 | sp<Surface> surface(ctrl->getSurface()); |
| 421 | if (surface != NULL) { |
| 422 | surface->incStrong(&sRefBaseOwner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 424 | return reinterpret_cast<jlong>(surface.get()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 427 | static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz, |
| 428 | jlong nativeObject, jobject parcelObj) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 429 | Parcel* parcel = parcelForJavaObject(env, parcelObj); |
| 430 | if (parcel == NULL) { |
| 431 | doThrowNPE(env); |
| 432 | return 0; |
| 433 | } |
Mathias Agopian | c3b9cd6 | 2013-03-13 16:07:57 -0700 | [diff] [blame] | 434 | |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 435 | android::view::Surface surfaceShim; |
| 436 | |
| 437 | // Calling code in Surface.java has already read the name of the Surface |
| 438 | // from the Parcel |
| 439 | surfaceShim.readFromParcel(parcel, /*nameAlreadyRead*/true); |
| 440 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 441 | sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); |
Mathias Agopian | c3b9cd6 | 2013-03-13 16:07:57 -0700 | [diff] [blame] | 442 | |
| 443 | // update the Surface only if the underlying IGraphicBufferProducer |
| 444 | // has changed. |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 445 | if (self != nullptr |
| 446 | && (IInterface::asBinder(self->getIGraphicBufferProducer()) == |
| 447 | IInterface::asBinder(surfaceShim.graphicBufferProducer))) { |
Mathias Agopian | c3b9cd6 | 2013-03-13 16:07:57 -0700 | [diff] [blame] | 448 | // same IGraphicBufferProducer, return ourselves |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 449 | return jlong(self.get()); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 450 | } |
Mathias Agopian | 4a05f43 | 2013-03-12 18:43:34 -0700 | [diff] [blame] | 451 | |
| 452 | sp<Surface> sur; |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 453 | if (surfaceShim.graphicBufferProducer != nullptr) { |
Mathias Agopian | c3b9cd6 | 2013-03-13 16:07:57 -0700 | [diff] [blame] | 454 | // we have a new IGraphicBufferProducer, create a new Surface for it |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 455 | sur = new Surface(surfaceShim.graphicBufferProducer, true); |
Mathias Agopian | c3b9cd6 | 2013-03-13 16:07:57 -0700 | [diff] [blame] | 456 | // and keep a reference before passing to java |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 457 | sur->incStrong(&sRefBaseOwner); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 458 | } |
Mathias Agopian | 4a05f43 | 2013-03-12 18:43:34 -0700 | [diff] [blame] | 459 | |
Mathias Agopian | c3b9cd6 | 2013-03-13 16:07:57 -0700 | [diff] [blame] | 460 | if (self != NULL) { |
| 461 | // and loose the java reference to ourselves |
| 462 | self->decStrong(&sRefBaseOwner); |
| 463 | } |
| 464 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 465 | return jlong(sur.get()); |
Dianne Hackborn | 61566cc | 2011-12-02 23:31:52 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Mathias Agopian | 29479eb | 2013-02-14 14:36:04 -0800 | [diff] [blame] | 468 | static void nativeWriteToParcel(JNIEnv* env, jclass clazz, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 469 | jlong nativeObject, jobject parcelObj) { |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 470 | Parcel* parcel = parcelForJavaObject(env, parcelObj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | if (parcel == NULL) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 472 | doThrowNPE(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | return; |
| 474 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 475 | sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); |
Eino-Ville Talvala | 5d2d778 | 2015-12-17 16:50:50 -0800 | [diff] [blame] | 476 | android::view::Surface surfaceShim; |
| 477 | if (self != nullptr) { |
| 478 | surfaceShim.graphicBufferProducer = self->getIGraphicBufferProducer(); |
| 479 | } |
| 480 | // Calling code in Surface.java has already written the name of the Surface |
| 481 | // to the Parcel |
| 482 | surfaceShim.writeToParcel(parcel, /*nameAlreadyWritten*/true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | } |
| 484 | |
John Reck | b35c960 | 2014-11-13 16:15:08 -0800 | [diff] [blame] | 485 | static jint nativeGetWidth(JNIEnv* env, jclass clazz, jlong nativeObject) { |
| 486 | Surface* surface = reinterpret_cast<Surface*>(nativeObject); |
| 487 | ANativeWindow* anw = static_cast<ANativeWindow*>(surface); |
| 488 | int value = 0; |
| 489 | anw->query(anw, NATIVE_WINDOW_WIDTH, &value); |
| 490 | return value; |
| 491 | } |
| 492 | |
| 493 | static jint nativeGetHeight(JNIEnv* env, jclass clazz, jlong nativeObject) { |
| 494 | Surface* surface = reinterpret_cast<Surface*>(nativeObject); |
| 495 | ANativeWindow* anw = static_cast<ANativeWindow*>(surface); |
| 496 | int value = 0; |
| 497 | anw->query(anw, NATIVE_WINDOW_HEIGHT, &value); |
| 498 | return value; |
| 499 | } |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 500 | |
Rob Carr | 64e516f | 2015-10-29 00:20:45 +0000 | [diff] [blame] | 501 | static jlong nativeGetNextFrameNumber(JNIEnv *env, jclass clazz, jlong nativeObject) { |
| 502 | Surface* surface = reinterpret_cast<Surface*>(nativeObject); |
| 503 | return surface->getNextFrameNumber(); |
| 504 | } |
John Reck | b35c960 | 2014-11-13 16:15:08 -0800 | [diff] [blame] | 505 | |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 506 | static jint nativeSetScalingMode(JNIEnv *env, jclass clazz, jlong nativeObject, jint scalingMode) { |
| 507 | Surface* surface = reinterpret_cast<Surface*>(nativeObject); |
| 508 | return surface->setScalingMode(scalingMode); |
| 509 | } |
| 510 | |
Robert Carr | 387838b | 2016-09-07 14:12:44 -0700 | [diff] [blame] | 511 | static jint nativeForceScopedDisconnect(JNIEnv *env, jclass clazz, jlong nativeObject) { |
| 512 | Surface* surface = reinterpret_cast<Surface*>(nativeObject); |
| 513 | return surface->disconnect(-1, IGraphicBufferProducer::DisconnectMode::AllLocal); |
| 514 | } |
| 515 | |
Robert Carr | a35e5de | 2017-03-03 17:04:54 -0800 | [diff] [blame] | 516 | static jint nativeAttachAndQueueBuffer(JNIEnv *env, jclass clazz, jlong nativeObject, |
| 517 | jobject graphicBuffer) { |
| 518 | Surface* surface = reinterpret_cast<Surface*>(nativeObject); |
| 519 | sp<GraphicBuffer> bp = graphicBufferForJavaObject(env, graphicBuffer); |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 520 | int err = Surface::attachAndQueueBuffer(surface, bp); |
Robert Carr | a35e5de | 2017-03-03 17:04:54 -0800 | [diff] [blame] | 521 | return err; |
| 522 | } |
| 523 | |
Romain Guy | 0bbf0b4 | 2017-07-20 08:57:07 -0700 | [diff] [blame] | 524 | static jint nativeSetSharedBufferModeEnabled(JNIEnv* env, jclass clazz, jlong nativeObject, |
| 525 | jboolean enabled) { |
| 526 | Surface* surface = reinterpret_cast<Surface*>(nativeObject); |
Adrian Roos | c43dfdf | 2018-01-29 17:55:44 +0100 | [diff] [blame] | 527 | ANativeWindow* anw = static_cast<ANativeWindow*>(surface); |
| 528 | return anw->perform(surface, NATIVE_WINDOW_SET_SHARED_BUFFER_MODE, int(enabled)); |
Romain Guy | 0bbf0b4 | 2017-07-20 08:57:07 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static jint nativeSetAutoRefreshEnabled(JNIEnv* env, jclass clazz, jlong nativeObject, |
| 532 | jboolean enabled) { |
| 533 | Surface* surface = reinterpret_cast<Surface*>(nativeObject); |
Adrian Roos | c43dfdf | 2018-01-29 17:55:44 +0100 | [diff] [blame] | 534 | ANativeWindow* anw = static_cast<ANativeWindow*>(surface); |
| 535 | return anw->perform(surface, NATIVE_WINDOW_SET_AUTO_REFRESH, int(enabled)); |
Romain Guy | 0bbf0b4 | 2017-07-20 08:57:07 -0700 | [diff] [blame] | 536 | } |
| 537 | |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 538 | namespace uirenderer { |
| 539 | |
| 540 | using namespace android::uirenderer::renderthread; |
| 541 | |
| 542 | class ContextFactory : public IContextFactory { |
| 543 | public: |
| 544 | virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) { |
| 545 | return new AnimationContext(clock); |
| 546 | } |
| 547 | }; |
| 548 | |
Peiyong Lin | d8b6871 | 2018-03-06 18:34:21 -0800 | [diff] [blame] | 549 | static jlong create(JNIEnv* env, jclass clazz, jlong rootNodePtr, jlong surfacePtr, |
| 550 | jboolean isWideColorGamut) { |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 551 | RenderNode* rootNode = reinterpret_cast<RenderNode*>(rootNodePtr); |
| 552 | sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr)); |
| 553 | ContextFactory factory; |
| 554 | RenderProxy* proxy = new RenderProxy(false, rootNode, &factory); |
| 555 | proxy->loadSystemProperties(); |
Peiyong Lin | d8b6871 | 2018-03-06 18:34:21 -0800 | [diff] [blame] | 556 | if (isWideColorGamut) { |
| 557 | proxy->setWideGamut(true); |
| 558 | } |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 559 | proxy->setSwapBehavior(SwapBehavior::kSwap_discardBuffer); |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 560 | proxy->initialize(surface); |
| 561 | // Shadows can't be used via this interface, so just set the light source |
John Reck | ab1080c | 2016-06-21 16:24:20 -0700 | [diff] [blame] | 562 | // to all 0s. |
| 563 | proxy->setup(0, 0, 0); |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame] | 564 | proxy->setLightCenter((Vector3){0, 0, 0}); |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 565 | return (jlong) proxy; |
| 566 | } |
| 567 | |
| 568 | static void setSurface(JNIEnv* env, jclass clazz, jlong rendererPtr, jlong surfacePtr) { |
| 569 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr); |
| 570 | sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr)); |
| 571 | proxy->updateSurface(surface); |
| 572 | } |
| 573 | |
| 574 | static void draw(JNIEnv* env, jclass clazz, jlong rendererPtr) { |
| 575 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 576 | nsecs_t vsync = systemTime(CLOCK_MONOTONIC); |
| 577 | UiFrameInfoBuilder(proxy->frameInfo()) |
| 578 | .setVsync(vsync, vsync) |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 579 | .addFlag(FrameInfoFlags::SurfaceCanvas); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 580 | proxy->syncAndDrawFrame(); |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | static void destroy(JNIEnv* env, jclass clazz, jlong rendererPtr) { |
| 584 | RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr); |
| 585 | delete proxy; |
| 586 | } |
| 587 | |
| 588 | } // uirenderer |
| 589 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 592 | namespace hwui = android::uirenderer; |
| 593 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 594 | static const JNINativeMethod gSurfaceMethods[] = { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 595 | {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)J", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 596 | (void*)nativeCreateFromSurfaceTexture }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 597 | {"nativeRelease", "(J)V", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 598 | (void*)nativeRelease }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 599 | {"nativeIsValid", "(J)Z", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 600 | (void*)nativeIsValid }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 601 | {"nativeIsConsumerRunningBehind", "(J)Z", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 602 | (void*)nativeIsConsumerRunningBehind }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 603 | {"nativeLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)J", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 604 | (void*)nativeLockCanvas }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 605 | {"nativeUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 606 | (void*)nativeUnlockCanvasAndPost }, |
Dan Stoza | 5795d64 | 2014-06-20 13:01:36 -0700 | [diff] [blame] | 607 | {"nativeAllocateBuffers", "(J)V", |
| 608 | (void*)nativeAllocateBuffers }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 609 | {"nativeCreateFromSurfaceControl", "(J)J", |
Jeff Brown | fc0ebd7 | 2013-04-30 16:33:00 -0700 | [diff] [blame] | 610 | (void*)nativeCreateFromSurfaceControl }, |
Bryce Lee | 02949f1 | 2017-06-16 07:20:34 -0700 | [diff] [blame] | 611 | {"nativeGetFromSurfaceControl", "(J)J", |
| 612 | (void*)nativeGetFromSurfaceControl }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 613 | {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 614 | (void*)nativeReadFromParcel }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 615 | {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V", |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 616 | (void*)nativeWriteToParcel }, |
John Reck | b35c960 | 2014-11-13 16:15:08 -0800 | [diff] [blame] | 617 | {"nativeGetWidth", "(J)I", (void*)nativeGetWidth }, |
| 618 | {"nativeGetHeight", "(J)I", (void*)nativeGetHeight }, |
Rob Carr | 64e516f | 2015-10-29 00:20:45 +0000 | [diff] [blame] | 619 | {"nativeGetNextFrameNumber", "(J)J", (void*)nativeGetNextFrameNumber }, |
Robert Carr | cd9a18c | 2015-12-16 18:16:21 -0800 | [diff] [blame] | 620 | {"nativeSetScalingMode", "(JI)I", (void*)nativeSetScalingMode }, |
Robert Carr | 387838b | 2016-09-07 14:12:44 -0700 | [diff] [blame] | 621 | {"nativeForceScopedDisconnect", "(J)I", (void*)nativeForceScopedDisconnect}, |
Robert Carr | a35e5de | 2017-03-03 17:04:54 -0800 | [diff] [blame] | 622 | {"nativeAttachAndQueueBuffer", "(JLandroid/graphics/GraphicBuffer;)I", (void*)nativeAttachAndQueueBuffer}, |
Romain Guy | 0bbf0b4 | 2017-07-20 08:57:07 -0700 | [diff] [blame] | 623 | {"nativeSetSharedBufferModeEnabled", "(JZ)I", (void*)nativeSetSharedBufferModeEnabled}, |
| 624 | {"nativeSetAutoRefreshEnabled", "(JZ)I", (void*)nativeSetAutoRefreshEnabled}, |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 625 | |
| 626 | // HWUI context |
Peiyong Lin | d8b6871 | 2018-03-06 18:34:21 -0800 | [diff] [blame] | 627 | {"nHwuiCreate", "(JJZ)J", (void*) hwui::create }, |
John Reck | bb2d0cc | 2014-10-21 10:09:36 -0700 | [diff] [blame] | 628 | {"nHwuiSetSurface", "(JJ)V", (void*) hwui::setSurface }, |
| 629 | {"nHwuiDraw", "(J)V", (void*) hwui::draw }, |
| 630 | {"nHwuiDestroy", "(J)V", (void*) hwui::destroy }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | }; |
| 632 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | int register_android_view_Surface(JNIEnv* env) |
| 634 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 635 | int err = RegisterMethodsOrDie(env, "android/view/Surface", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | gSurfaceMethods, NELEM(gSurfaceMethods)); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 637 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 638 | jclass clazz = FindClassOrDie(env, "android/view/Surface"); |
| 639 | gSurfaceClassInfo.clazz = MakeGlobalRefOrDie(env, clazz); |
| 640 | gSurfaceClassInfo.mNativeObject = GetFieldIDOrDie(env, |
| 641 | gSurfaceClassInfo.clazz, "mNativeObject", "J"); |
| 642 | gSurfaceClassInfo.mLock = GetFieldIDOrDie(env, |
| 643 | gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;"); |
| 644 | gSurfaceClassInfo.ctor = GetMethodIDOrDie(env, gSurfaceClassInfo.clazz, "<init>", "(J)V"); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 645 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 646 | clazz = FindClassOrDie(env, "android/graphics/Rect"); |
| 647 | gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I"); |
| 648 | gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I"); |
| 649 | gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I"); |
| 650 | gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I"); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 651 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 652 | return err; |
| 653 | } |
| 654 | |
| 655 | }; |