Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
| 17 | #define LOG_TAG "SurfaceControl" |
Robert Carr | 6486d31 | 2017-01-09 19:48:29 -0800 | [diff] [blame^] | 18 | #define LOG_NDEBUG 0 |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 19 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 20 | #include "android_os_Parcel.h" |
| 21 | #include "android_util_Binder.h" |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 22 | #include "android/graphics/Bitmap.h" |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 23 | #include "android/graphics/GraphicsJNI.h" |
| 24 | #include "android/graphics/Region.h" |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 25 | #include "core_jni_helpers.h" |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 26 | |
| 27 | #include <JNIHelp.h> |
| 28 | #include <ScopedUtfChars.h> |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 29 | #include <android_runtime/android_view_Surface.h> |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 30 | #include <android_runtime/android_view_SurfaceSession.h> |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 31 | #include <gui/Surface.h> |
| 32 | #include <gui/SurfaceComposerClient.h> |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 33 | #include <jni.h> |
| 34 | #include <memory> |
| 35 | #include <stdio.h> |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 36 | #include <system/graphics.h> |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 37 | #include <ui/DisplayInfo.h> |
Hangyu Kuang | 54ac219 | 2016-04-25 13:22:02 -0700 | [diff] [blame] | 38 | #include <ui/HdrCapabilities.h> |
Svetoslav | 1376d60 | 2014-03-13 11:17:26 -0700 | [diff] [blame] | 39 | #include <ui/FrameStats.h> |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 40 | #include <ui/Rect.h> |
| 41 | #include <ui/Region.h> |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 42 | #include <utils/Log.h> |
| 43 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 44 | // ---------------------------------------------------------------------------- |
| 45 | |
| 46 | namespace android { |
| 47 | |
| 48 | static const char* const OutOfResourcesException = |
| 49 | "android/view/Surface$OutOfResourcesException"; |
| 50 | |
| 51 | static struct { |
Dan Stoza | 0010105 | 2014-05-02 15:23:40 -0700 | [diff] [blame] | 52 | jclass clazz; |
| 53 | jmethodID ctor; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 54 | jfieldID width; |
| 55 | jfieldID height; |
| 56 | jfieldID refreshRate; |
| 57 | jfieldID density; |
| 58 | jfieldID xDpi; |
| 59 | jfieldID yDpi; |
| 60 | jfieldID secure; |
Andy McFadden | e8b1aeb | 2014-06-13 14:05:40 -0700 | [diff] [blame] | 61 | jfieldID appVsyncOffsetNanos; |
| 62 | jfieldID presentationDeadlineNanos; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 63 | } gPhysicalDisplayInfoClassInfo; |
| 64 | |
Dan Stoza | 9890e341 | 2014-05-22 16:12:54 -0700 | [diff] [blame] | 65 | static struct { |
| 66 | jfieldID bottom; |
| 67 | jfieldID left; |
| 68 | jfieldID right; |
| 69 | jfieldID top; |
| 70 | } gRectClassInfo; |
| 71 | |
Leon Scroggins | 46cb9bd | 2014-03-06 15:36:39 -0500 | [diff] [blame] | 72 | // Implements SkMallocPixelRef::ReleaseProc, to delete the screenshot on unref. |
| 73 | void DeleteScreenshot(void* addr, void* context) { |
| 74 | SkASSERT(addr == ((ScreenshotClient*) context)->getPixels()); |
| 75 | delete ((ScreenshotClient*) context); |
| 76 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 77 | |
Svetoslav | 1376d60 | 2014-03-13 11:17:26 -0700 | [diff] [blame] | 78 | static struct { |
| 79 | nsecs_t UNDEFINED_TIME_NANO; |
| 80 | jmethodID init; |
| 81 | } gWindowContentFrameStatsClassInfo; |
| 82 | |
| 83 | static struct { |
| 84 | nsecs_t UNDEFINED_TIME_NANO; |
| 85 | jmethodID init; |
| 86 | } gWindowAnimationFrameStatsClassInfo; |
| 87 | |
Hangyu Kuang | 54ac219 | 2016-04-25 13:22:02 -0700 | [diff] [blame] | 88 | static struct { |
| 89 | jclass clazz; |
| 90 | jmethodID ctor; |
| 91 | } gHdrCapabilitiesClassInfo; |
| 92 | |
Robert Carr | 6486d31 | 2017-01-09 19:48:29 -0800 | [diff] [blame^] | 93 | static struct { |
| 94 | jclass clazz; |
| 95 | jmethodID builder; |
| 96 | } gGraphicBufferClassInfo; |
| 97 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 98 | // ---------------------------------------------------------------------------- |
| 99 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 100 | static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 101 | jstring nameStr, jint w, jint h, jint format, jint flags) { |
| 102 | ScopedUtfChars name(env, nameStr); |
| 103 | sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj)); |
| 104 | sp<SurfaceControl> surface = client->createSurface( |
| 105 | String8(name.c_str()), w, h, format, flags); |
| 106 | if (surface == NULL) { |
| 107 | jniThrowException(env, OutOfResourcesException, NULL); |
| 108 | return 0; |
| 109 | } |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 110 | surface->incStrong((void *)nativeCreate); |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 111 | return reinterpret_cast<jlong>(surface.get()); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 114 | static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 115 | sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject)); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 116 | ctrl->decStrong((void *)nativeCreate); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 119 | static void nativeDestroy(JNIEnv* env, jclass clazz, jlong nativeObject) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 120 | sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject)); |
| 121 | ctrl->clear(); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 122 | ctrl->decStrong((void *)nativeCreate); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Chong Zhang | 47e36a3 | 2016-02-29 16:44:33 -0800 | [diff] [blame] | 125 | static void nativeDisconnect(JNIEnv* env, jclass clazz, jlong nativeObject) { |
| 126 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 127 | if (ctrl != NULL) { |
| 128 | ctrl->disconnect(); |
| 129 | } |
| 130 | } |
| 131 | |
Robert Carr | 6486d31 | 2017-01-09 19:48:29 -0800 | [diff] [blame^] | 132 | static Rect rectFromObj(JNIEnv* env, jobject rectObj) { |
| 133 | int left = env->GetIntField(rectObj, gRectClassInfo.left); |
| 134 | int top = env->GetIntField(rectObj, gRectClassInfo.top); |
| 135 | int right = env->GetIntField(rectObj, gRectClassInfo.right); |
| 136 | int bottom = env->GetIntField(rectObj, gRectClassInfo.bottom); |
| 137 | return Rect(left, top, right, bottom); |
| 138 | } |
| 139 | |
| 140 | static jobject nativeScreenshotToBuffer(JNIEnv* env, jclass clazz, |
| 141 | jobject displayTokenObj, jobject sourceCropObj, jint width, jint height, |
| 142 | jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform, |
| 143 | int rotation) { |
| 144 | sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj); |
| 145 | if (displayToken == NULL) { |
| 146 | return NULL; |
| 147 | } |
| 148 | Rect sourceCrop = rectFromObj(env, sourceCropObj); |
| 149 | if (allLayers) { |
| 150 | minLayer = 0; |
| 151 | maxLayer = -1; |
| 152 | } |
| 153 | sp<GraphicBuffer> buffer; |
| 154 | status_t res = ScreenshotClient::captureToBuffer(displayToken, |
| 155 | sourceCrop, width, height, minLayer, maxLayer, useIdentityTransform, |
| 156 | rotation, &buffer); |
| 157 | if (res != NO_ERROR) { |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | return env->CallStaticObjectMethod(gGraphicBufferClassInfo.clazz, |
| 162 | gGraphicBufferClassInfo.builder, |
| 163 | buffer->getWidth(), |
| 164 | buffer->getHeight(), |
| 165 | buffer->getPixelFormat(), |
| 166 | buffer->getUsage(), |
| 167 | (void*)buffer.get()); |
| 168 | } |
| 169 | |
Dan Stoza | 9890e341 | 2014-05-22 16:12:54 -0700 | [diff] [blame] | 170 | static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz, |
| 171 | jobject displayTokenObj, jobject sourceCropObj, jint width, jint height, |
Riley Andrews | 1d13406 | 2014-08-21 15:47:07 -0700 | [diff] [blame] | 172 | jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform, |
| 173 | int rotation) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 174 | sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj); |
| 175 | if (displayToken == NULL) { |
| 176 | return NULL; |
| 177 | } |
| 178 | |
Robert Carr | 6486d31 | 2017-01-09 19:48:29 -0800 | [diff] [blame^] | 179 | Rect sourceCrop = rectFromObj(env, sourceCropObj); |
Dan Stoza | 9890e341 | 2014-05-22 16:12:54 -0700 | [diff] [blame] | 180 | |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 181 | std::unique_ptr<ScreenshotClient> screenshot(new ScreenshotClient()); |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 182 | status_t res; |
Riley Andrews | 1d13406 | 2014-08-21 15:47:07 -0700 | [diff] [blame] | 183 | if (allLayers) { |
| 184 | minLayer = 0; |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 185 | maxLayer = -1; |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 186 | } |
Riley Andrews | 1d13406 | 2014-08-21 15:47:07 -0700 | [diff] [blame] | 187 | |
| 188 | res = screenshot->update(displayToken, sourceCrop, width, height, |
| 189 | minLayer, maxLayer, useIdentityTransform, static_cast<uint32_t>(rotation)); |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 190 | if (res != NO_ERROR) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 191 | return NULL; |
| 192 | } |
| 193 | |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 194 | SkColorType colorType; |
| 195 | SkAlphaType alphaType; |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 196 | switch (screenshot->getFormat()) { |
| 197 | case PIXEL_FORMAT_RGBX_8888: { |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 198 | colorType = kRGBA_8888_SkColorType; |
| 199 | alphaType = kOpaque_SkAlphaType; |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 200 | break; |
| 201 | } |
| 202 | case PIXEL_FORMAT_RGBA_8888: { |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 203 | colorType = kRGBA_8888_SkColorType; |
| 204 | alphaType = kPremul_SkAlphaType; |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 205 | break; |
| 206 | } |
Romain Guy | 9505a65 | 2016-12-14 09:43:50 -0800 | [diff] [blame] | 207 | case PIXEL_FORMAT_RGBA_FP16: { |
| 208 | colorType = kRGBA_F16_SkColorType; |
| 209 | alphaType = kPremul_SkAlphaType; |
| 210 | break; |
| 211 | } |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 212 | case PIXEL_FORMAT_RGB_565: { |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 213 | colorType = kRGB_565_SkColorType; |
| 214 | alphaType = kOpaque_SkAlphaType; |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 215 | break; |
| 216 | } |
| 217 | default: { |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 218 | return NULL; |
| 219 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 220 | } |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 221 | SkImageInfo screenshotInfo = SkImageInfo::Make(screenshot->getWidth(), |
| 222 | screenshot->getHeight(), |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 223 | colorType, |
| 224 | alphaType, |
| 225 | GraphicsJNI::defaultColorSpace()); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 226 | |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 227 | const size_t rowBytes = |
Derek Sollenberger | b644a3b | 2014-01-17 15:45:10 -0500 | [diff] [blame] | 228 | screenshot->getStride() * android::bytesPerPixel(screenshot->getFormat()); |
| 229 | |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 230 | if (!screenshotInfo.width() || !screenshotInfo.height()) { |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 231 | return NULL; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 232 | } |
| 233 | |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 234 | auto bitmap = new Bitmap( |
John Reck | f29ed28 | 2015-04-07 07:32:03 -0700 | [diff] [blame] | 235 | (void*) screenshot->getPixels(), (void*) screenshot.get(), DeleteScreenshot, |
| 236 | screenshotInfo, rowBytes, nullptr); |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 237 | screenshot.release(); |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 238 | bitmap->setImmutable(); |
| 239 | return bitmap::createBitmap(env, bitmap, |
| 240 | android::bitmap::kBitmapCreateFlag_Premultiplied, NULL); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Dan Stoza | 9890e341 | 2014-05-22 16:12:54 -0700 | [diff] [blame] | 243 | static void nativeScreenshot(JNIEnv* env, jclass clazz, jobject displayTokenObj, |
| 244 | jobject surfaceObj, jobject sourceCropObj, jint width, jint height, |
| 245 | jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform) { |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 246 | sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj); |
| 247 | if (displayToken != NULL) { |
| 248 | sp<Surface> consumer = android_view_Surface_getSurface(env, surfaceObj); |
| 249 | if (consumer != NULL) { |
Dan Stoza | 9890e341 | 2014-05-22 16:12:54 -0700 | [diff] [blame] | 250 | int left = env->GetIntField(sourceCropObj, gRectClassInfo.left); |
| 251 | int top = env->GetIntField(sourceCropObj, gRectClassInfo.top); |
| 252 | int right = env->GetIntField(sourceCropObj, gRectClassInfo.right); |
| 253 | int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom); |
| 254 | Rect sourceCrop(left, top, right, bottom); |
| 255 | |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 256 | if (allLayers) { |
| 257 | minLayer = 0; |
| 258 | maxLayer = -1; |
| 259 | } |
Dan Stoza | 9890e341 | 2014-05-22 16:12:54 -0700 | [diff] [blame] | 260 | ScreenshotClient::capture(displayToken, |
| 261 | consumer->getIGraphicBufferProducer(), sourceCrop, |
Dan Stoza | 16ec12a | 2014-02-14 15:06:55 -0800 | [diff] [blame] | 262 | width, height, uint32_t(minLayer), uint32_t(maxLayer), |
| 263 | useIdentityTransform); |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 268 | static void nativeOpenTransaction(JNIEnv* env, jclass clazz) { |
| 269 | SurfaceComposerClient::openGlobalTransaction(); |
| 270 | } |
| 271 | |
Robert Carr | e9953b1 | 2016-05-23 20:52:04 -0700 | [diff] [blame] | 272 | |
| 273 | static void nativeCloseTransaction(JNIEnv* env, jclass clazz, jboolean sync) { |
| 274 | SurfaceComposerClient::closeGlobalTransaction(sync); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz) { |
| 278 | SurfaceComposerClient::setAnimationTransaction(); |
| 279 | } |
| 280 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 281 | static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong nativeObject, jint zorder) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 282 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 283 | status_t err = ctrl->setLayer(zorder); |
| 284 | if (err < 0 && err != NO_INIT) { |
| 285 | doThrowIAE(env); |
| 286 | } |
| 287 | } |
| 288 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 289 | static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat x, jfloat y) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 290 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 291 | status_t err = ctrl->setPosition(x, y); |
| 292 | if (err < 0 && err != NO_INIT) { |
| 293 | doThrowIAE(env); |
| 294 | } |
| 295 | } |
| 296 | |
Robert Carr | 6da3cc0 | 2016-06-16 15:17:07 -0700 | [diff] [blame] | 297 | static void nativeSetGeometryAppliesWithResize(JNIEnv* env, jclass clazz, |
Robert Carr | a9408d4 | 2016-06-03 13:28:48 -0700 | [diff] [blame] | 298 | jlong nativeObject) { |
| 299 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
Robert Carr | 6da3cc0 | 2016-06-16 15:17:07 -0700 | [diff] [blame] | 300 | status_t err = ctrl->setGeometryAppliesWithResize(); |
Robert Carr | a9408d4 | 2016-06-03 13:28:48 -0700 | [diff] [blame] | 301 | if (err < 0 && err != NO_INIT) { |
| 302 | doThrowIAE(env); |
| 303 | } |
| 304 | } |
| 305 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 306 | static void nativeSetSize(JNIEnv* env, jclass clazz, jlong nativeObject, jint w, jint h) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 307 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 308 | status_t err = ctrl->setSize(w, h); |
| 309 | if (err < 0 && err != NO_INIT) { |
| 310 | doThrowIAE(env); |
| 311 | } |
| 312 | } |
| 313 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 314 | static void nativeSetFlags(JNIEnv* env, jclass clazz, jlong nativeObject, jint flags, jint mask) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 315 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 316 | status_t err = ctrl->setFlags(flags, mask); |
| 317 | if (err < 0 && err != NO_INIT) { |
| 318 | doThrowIAE(env); |
| 319 | } |
| 320 | } |
| 321 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 322 | static void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jlong nativeObject, jobject regionObj) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 323 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 324 | SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj); |
| 325 | if (!region) { |
| 326 | doThrowIAE(env); |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | const SkIRect& b(region->getBounds()); |
| 331 | Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom)); |
| 332 | if (region->isComplex()) { |
| 333 | SkRegion::Iterator it(*region); |
| 334 | while (!it.done()) { |
| 335 | const SkIRect& r(it.rect()); |
| 336 | reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom); |
| 337 | it.next(); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | status_t err = ctrl->setTransparentRegionHint(reg); |
| 342 | if (err < 0 && err != NO_INIT) { |
| 343 | doThrowIAE(env); |
| 344 | } |
| 345 | } |
| 346 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 347 | static void nativeSetAlpha(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat alpha) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 348 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 349 | status_t err = ctrl->setAlpha(alpha); |
| 350 | if (err < 0 && err != NO_INIT) { |
| 351 | doThrowIAE(env); |
| 352 | } |
| 353 | } |
| 354 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 355 | static void nativeSetMatrix(JNIEnv* env, jclass clazz, jlong nativeObject, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 356 | jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy) { |
| 357 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 358 | status_t err = ctrl->setMatrix(dsdx, dtdx, dsdy, dtdy); |
| 359 | if (err < 0 && err != NO_INIT) { |
| 360 | doThrowIAE(env); |
| 361 | } |
| 362 | } |
| 363 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 364 | static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong nativeObject, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 365 | jint l, jint t, jint r, jint b) { |
| 366 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 367 | Rect crop(l, t, r, b); |
| 368 | status_t err = ctrl->setCrop(crop); |
| 369 | if (err < 0 && err != NO_INIT) { |
| 370 | doThrowIAE(env); |
| 371 | } |
| 372 | } |
| 373 | |
Pablo Ceballos | 27982e6 | 2016-03-09 10:50:45 -0800 | [diff] [blame] | 374 | static void nativeSetFinalCrop(JNIEnv* env, jclass clazz, jlong nativeObject, |
| 375 | jint l, jint t, jint r, jint b) { |
| 376 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 377 | Rect crop(l, t, r, b); |
| 378 | status_t err = ctrl->setFinalCrop(crop); |
| 379 | if (err < 0 && err != NO_INIT) { |
| 380 | doThrowIAE(env); |
| 381 | } |
| 382 | } |
| 383 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 384 | static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong nativeObject, jint layerStack) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 385 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 386 | status_t err = ctrl->setLayerStack(layerStack); |
| 387 | if (err < 0 && err != NO_INIT) { |
| 388 | doThrowIAE(env); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) { |
| 393 | sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id)); |
| 394 | return javaObjectForIBinder(env, token); |
| 395 | } |
| 396 | |
| 397 | static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj, |
| 398 | jboolean secure) { |
| 399 | ScopedUtfChars name(env, nameObj); |
| 400 | sp<IBinder> token(SurfaceComposerClient::createDisplay( |
| 401 | String8(name.c_str()), bool(secure))); |
| 402 | return javaObjectForIBinder(env, token); |
| 403 | } |
| 404 | |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 405 | static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) { |
| 406 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 407 | if (token == NULL) return; |
| 408 | SurfaceComposerClient::destroyDisplay(token); |
| 409 | } |
| 410 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 411 | static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 412 | jobject tokenObj, jlong nativeSurfaceObject) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 413 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 414 | if (token == NULL) return; |
Mathias Agopian | ffddc9b | 2013-02-25 15:56:31 -0800 | [diff] [blame] | 415 | sp<IGraphicBufferProducer> bufferProducer; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 416 | sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject)); |
Mathias Agopian | ffddc9b | 2013-02-25 15:56:31 -0800 | [diff] [blame] | 417 | if (sur != NULL) { |
| 418 | bufferProducer = sur->getIGraphicBufferProducer(); |
| 419 | } |
Pablo Ceballos | aff2f94 | 2016-07-29 14:49:55 -0700 | [diff] [blame] | 420 | status_t err = SurfaceComposerClient::setDisplaySurface(token, |
| 421 | bufferProducer); |
| 422 | if (err != NO_ERROR) { |
| 423 | doThrowIAE(env, "Illegal Surface, could not enable async mode. Was this" |
| 424 | " Surface created with singleBufferMode?"); |
| 425 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz, |
| 429 | jobject tokenObj, jint layerStack) { |
| 430 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 431 | if (token == NULL) return; |
| 432 | |
| 433 | SurfaceComposerClient::setDisplayLayerStack(token, layerStack); |
| 434 | } |
| 435 | |
| 436 | static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz, |
| 437 | jobject tokenObj, jint orientation, |
| 438 | jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom, |
| 439 | jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) { |
| 440 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 441 | if (token == NULL) return; |
| 442 | Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom); |
| 443 | Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom); |
| 444 | SurfaceComposerClient::setDisplayProjection(token, orientation, layerStackRect, displayRect); |
| 445 | } |
| 446 | |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 447 | static void nativeSetDisplaySize(JNIEnv* env, jclass clazz, |
| 448 | jobject tokenObj, jint width, jint height) { |
| 449 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 450 | if (token == NULL) return; |
| 451 | SurfaceComposerClient::setDisplaySize(token, width, height); |
| 452 | } |
| 453 | |
Dan Stoza | 0010105 | 2014-05-02 15:23:40 -0700 | [diff] [blame] | 454 | static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz, |
| 455 | jobject tokenObj) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 456 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
Dan Stoza | 0010105 | 2014-05-02 15:23:40 -0700 | [diff] [blame] | 457 | if (token == NULL) return NULL; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 458 | |
Dan Stoza | 0010105 | 2014-05-02 15:23:40 -0700 | [diff] [blame] | 459 | Vector<DisplayInfo> configs; |
| 460 | if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR || |
| 461 | configs.size() == 0) { |
| 462 | return NULL; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Dan Stoza | 0010105 | 2014-05-02 15:23:40 -0700 | [diff] [blame] | 465 | jobjectArray configArray = env->NewObjectArray(configs.size(), |
| 466 | gPhysicalDisplayInfoClassInfo.clazz, NULL); |
| 467 | |
| 468 | for (size_t c = 0; c < configs.size(); ++c) { |
| 469 | const DisplayInfo& info = configs[c]; |
| 470 | jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz, |
| 471 | gPhysicalDisplayInfoClassInfo.ctor); |
| 472 | env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w); |
| 473 | env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h); |
| 474 | env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps); |
| 475 | env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density); |
| 476 | env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi); |
| 477 | env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi); |
| 478 | env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure); |
Andy McFadden | e8b1aeb | 2014-06-13 14:05:40 -0700 | [diff] [blame] | 479 | env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos, |
| 480 | info.appVsyncOffset); |
| 481 | env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos, |
| 482 | info.presentationDeadline); |
Dan Stoza | 0010105 | 2014-05-02 15:23:40 -0700 | [diff] [blame] | 483 | env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj); |
| 484 | env->DeleteLocalRef(infoObj); |
| 485 | } |
| 486 | |
| 487 | return configArray; |
| 488 | } |
| 489 | |
| 490 | static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) { |
| 491 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 492 | if (token == NULL) return -1; |
| 493 | return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token)); |
| 494 | } |
| 495 | |
| 496 | static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) { |
| 497 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 498 | if (token == NULL) return JNI_FALSE; |
| 499 | status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id)); |
| 500 | return err == NO_ERROR ? JNI_TRUE : JNI_FALSE; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 501 | } |
| 502 | |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 503 | static jintArray nativeGetDisplayColorModes(JNIEnv* env, jclass, jobject tokenObj) { |
| 504 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 505 | if (token == NULL) return NULL; |
| 506 | Vector<android_color_mode_t> colorModes; |
| 507 | if (SurfaceComposerClient::getDisplayColorModes(token, &colorModes) != NO_ERROR || |
| 508 | colorModes.isEmpty()) { |
| 509 | return NULL; |
| 510 | } |
| 511 | |
| 512 | jintArray colorModesArray = env->NewIntArray(colorModes.size()); |
| 513 | if (colorModesArray == NULL) { |
| 514 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 515 | return NULL; |
| 516 | } |
| 517 | jint* colorModesArrayValues = env->GetIntArrayElements(colorModesArray, 0); |
| 518 | for (size_t i = 0; i < colorModes.size(); i++) { |
| 519 | colorModesArrayValues[i] = static_cast<jint>(colorModes[i]); |
| 520 | } |
| 521 | env->ReleaseIntArrayElements(colorModesArray, colorModesArrayValues, 0); |
| 522 | return colorModesArray; |
| 523 | } |
| 524 | |
| 525 | static jint nativeGetActiveColorMode(JNIEnv* env, jclass, jobject tokenObj) { |
| 526 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 527 | if (token == NULL) return -1; |
| 528 | return static_cast<jint>(SurfaceComposerClient::getActiveColorMode(token)); |
| 529 | } |
| 530 | |
| 531 | static jboolean nativeSetActiveColorMode(JNIEnv* env, jclass, |
| 532 | jobject tokenObj, jint colorMode) { |
| 533 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 534 | if (token == NULL) return JNI_FALSE; |
| 535 | status_t err = SurfaceComposerClient::setActiveColorMode(token, |
| 536 | static_cast<android_color_mode_t>(colorMode)); |
| 537 | return err == NO_ERROR ? JNI_TRUE : JNI_FALSE; |
| 538 | } |
| 539 | |
Prashant Malani | c55929a | 2014-05-25 01:59:21 -0700 | [diff] [blame] | 540 | static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 541 | sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); |
| 542 | if (token == NULL) return; |
| 543 | |
Prashant Malani | c55929a | 2014-05-25 01:59:21 -0700 | [diff] [blame] | 544 | ALOGD_IF_SLOW(100, "Excessive delay in setPowerMode()"); |
| 545 | SurfaceComposerClient::setDisplayPowerMode(token, mode); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Svetoslav | 1376d60 | 2014-03-13 11:17:26 -0700 | [diff] [blame] | 548 | static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) { |
| 549 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 550 | status_t err = ctrl->clearLayerFrameStats(); |
| 551 | |
| 552 | if (err < 0 && err != NO_INIT) { |
| 553 | doThrowIAE(env); |
| 554 | } |
| 555 | |
| 556 | // The other end is not ready, just report we failed. |
| 557 | if (err == NO_INIT) { |
| 558 | return JNI_FALSE; |
| 559 | } |
| 560 | |
| 561 | return JNI_TRUE; |
| 562 | } |
| 563 | |
| 564 | static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject, |
| 565 | jobject outStats) { |
| 566 | FrameStats stats; |
| 567 | |
| 568 | SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 569 | status_t err = ctrl->getLayerFrameStats(&stats); |
| 570 | if (err < 0 && err != NO_INIT) { |
| 571 | doThrowIAE(env); |
| 572 | } |
| 573 | |
| 574 | // The other end is not ready, fine just return empty stats. |
| 575 | if (err == NO_INIT) { |
| 576 | return JNI_FALSE; |
| 577 | } |
| 578 | |
| 579 | jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano); |
| 580 | size_t frameCount = stats.desiredPresentTimesNano.size(); |
| 581 | |
| 582 | jlongArray postedTimesNanoDst = env->NewLongArray(frameCount); |
| 583 | if (postedTimesNanoDst == NULL) { |
| 584 | return JNI_FALSE; |
| 585 | } |
| 586 | |
| 587 | jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount); |
| 588 | if (presentedTimesNanoDst == NULL) { |
| 589 | return JNI_FALSE; |
| 590 | } |
| 591 | |
| 592 | jlongArray readyTimesNanoDst = env->NewLongArray(frameCount); |
| 593 | if (readyTimesNanoDst == NULL) { |
| 594 | return JNI_FALSE; |
| 595 | } |
| 596 | |
| 597 | nsecs_t postedTimesNanoSrc[frameCount]; |
| 598 | nsecs_t presentedTimesNanoSrc[frameCount]; |
| 599 | nsecs_t readyTimesNanoSrc[frameCount]; |
| 600 | |
| 601 | for (size_t i = 0; i < frameCount; i++) { |
| 602 | nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i]; |
| 603 | if (postedTimeNano == INT64_MAX) { |
| 604 | postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO; |
| 605 | } |
| 606 | postedTimesNanoSrc[i] = postedTimeNano; |
| 607 | |
| 608 | nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i]; |
| 609 | if (presentedTimeNano == INT64_MAX) { |
| 610 | presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO; |
| 611 | } |
| 612 | presentedTimesNanoSrc[i] = presentedTimeNano; |
| 613 | |
| 614 | nsecs_t readyTimeNano = stats.frameReadyTimesNano[i]; |
| 615 | if (readyTimeNano == INT64_MAX) { |
| 616 | readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO; |
| 617 | } |
| 618 | readyTimesNanoSrc[i] = readyTimeNano; |
| 619 | } |
| 620 | |
| 621 | env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc); |
| 622 | env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc); |
| 623 | env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc); |
| 624 | |
| 625 | env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano, |
| 626 | postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst); |
| 627 | |
| 628 | if (env->ExceptionCheck()) { |
| 629 | return JNI_FALSE; |
| 630 | } |
| 631 | |
| 632 | return JNI_TRUE; |
| 633 | } |
| 634 | |
| 635 | static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) { |
| 636 | status_t err = SurfaceComposerClient::clearAnimationFrameStats(); |
| 637 | |
| 638 | if (err < 0 && err != NO_INIT) { |
| 639 | doThrowIAE(env); |
| 640 | } |
| 641 | |
| 642 | // The other end is not ready, just report we failed. |
| 643 | if (err == NO_INIT) { |
| 644 | return JNI_FALSE; |
| 645 | } |
| 646 | |
| 647 | return JNI_TRUE; |
| 648 | } |
| 649 | |
| 650 | static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) { |
| 651 | FrameStats stats; |
| 652 | |
| 653 | status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats); |
| 654 | if (err < 0 && err != NO_INIT) { |
| 655 | doThrowIAE(env); |
| 656 | } |
| 657 | |
| 658 | // The other end is not ready, fine just return empty stats. |
| 659 | if (err == NO_INIT) { |
| 660 | return JNI_FALSE; |
| 661 | } |
| 662 | |
| 663 | jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano); |
| 664 | size_t frameCount = stats.desiredPresentTimesNano.size(); |
| 665 | |
| 666 | jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount); |
| 667 | if (presentedTimesNanoDst == NULL) { |
| 668 | return JNI_FALSE; |
| 669 | } |
| 670 | |
| 671 | nsecs_t presentedTimesNanoSrc[frameCount]; |
| 672 | |
| 673 | for (size_t i = 0; i < frameCount; i++) { |
Allen Hair | ac5eda3 | 2014-04-24 11:50:37 -0700 | [diff] [blame] | 674 | nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i]; |
Svetoslav | 1376d60 | 2014-03-13 11:17:26 -0700 | [diff] [blame] | 675 | if (presentedTimeNano == INT64_MAX) { |
| 676 | presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO; |
| 677 | } |
| 678 | presentedTimesNanoSrc[i] = presentedTimeNano; |
| 679 | } |
| 680 | |
| 681 | env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc); |
| 682 | |
| 683 | env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano, |
| 684 | presentedTimesNanoDst); |
| 685 | |
| 686 | if (env->ExceptionCheck()) { |
| 687 | return JNI_FALSE; |
| 688 | } |
| 689 | |
| 690 | return JNI_TRUE; |
| 691 | } |
| 692 | |
Rob Carr | 64e516f | 2015-10-29 00:20:45 +0000 | [diff] [blame] | 693 | |
| 694 | static void nativeDeferTransactionUntil(JNIEnv* env, jclass clazz, jlong nativeObject, |
| 695 | jobject handleObject, jlong frameNumber) { |
| 696 | auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 697 | sp<IBinder> handle = ibinderForJavaObject(env, handleObject); |
| 698 | |
| 699 | ctrl->deferTransactionUntil(handle, frameNumber); |
| 700 | } |
| 701 | |
Robert Carr | 1ca6a33 | 2016-04-11 18:00:43 -0700 | [diff] [blame] | 702 | static void nativeSetOverrideScalingMode(JNIEnv* env, jclass clazz, jlong nativeObject, |
| 703 | jint scalingMode) { |
| 704 | auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 705 | |
| 706 | ctrl->setOverrideScalingMode(scalingMode); |
| 707 | } |
| 708 | |
Rob Carr | 64e516f | 2015-10-29 00:20:45 +0000 | [diff] [blame] | 709 | static jobject nativeGetHandle(JNIEnv* env, jclass clazz, jlong nativeObject) { |
| 710 | auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 711 | |
| 712 | return javaObjectForIBinder(env, ctrl->getHandle()); |
| 713 | } |
| 714 | |
Robert Carr | 6da3cc0 | 2016-06-16 15:17:07 -0700 | [diff] [blame] | 715 | static jboolean nativeGetTransformToDisplayInverse(JNIEnv* env, jclass clazz, jlong nativeObject) { |
| 716 | bool out = false; |
| 717 | auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); |
| 718 | status_t status = ctrl->getTransformToDisplayInverse(&out); |
| 719 | if (status != NO_ERROR) { |
| 720 | return false; |
| 721 | } |
| 722 | return out; |
| 723 | } |
| 724 | |
Hangyu Kuang | 54ac219 | 2016-04-25 13:22:02 -0700 | [diff] [blame] | 725 | static jobject nativeGetHdrCapabilities(JNIEnv* env, jclass clazz, jobject tokenObject) { |
| 726 | sp<IBinder> token(ibinderForJavaObject(env, tokenObject)); |
| 727 | if (token == NULL) return NULL; |
| 728 | |
| 729 | HdrCapabilities capabilities; |
| 730 | SurfaceComposerClient::getHdrCapabilities(token, &capabilities); |
| 731 | |
| 732 | const auto& types = capabilities.getSupportedHdrTypes(); |
| 733 | auto typesArray = env->NewIntArray(types.size()); |
| 734 | env->SetIntArrayRegion(typesArray, 0, types.size(), types.data()); |
| 735 | |
Michael Wright | 9ff94c0 | 2016-03-30 18:05:40 -0700 | [diff] [blame] | 736 | return env->NewObject(gHdrCapabilitiesClassInfo.clazz, gHdrCapabilitiesClassInfo.ctor, |
Hangyu Kuang | 54ac219 | 2016-04-25 13:22:02 -0700 | [diff] [blame] | 737 | typesArray, capabilities.getDesiredMaxLuminance(), |
| 738 | capabilities.getDesiredMaxAverageLuminance(), capabilities.getDesiredMinLuminance()); |
| 739 | } |
| 740 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 741 | // ---------------------------------------------------------------------------- |
| 742 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 743 | static const JNINativeMethod sSurfaceControlMethods[] = { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 744 | {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 745 | (void*)nativeCreate }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 746 | {"nativeRelease", "(J)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 747 | (void*)nativeRelease }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 748 | {"nativeDestroy", "(J)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 749 | (void*)nativeDestroy }, |
Chong Zhang | 47e36a3 | 2016-02-29 16:44:33 -0800 | [diff] [blame] | 750 | {"nativeDisconnect", "(J)V", |
| 751 | (void*)nativeDisconnect }, |
Riley Andrews | 1d13406 | 2014-08-21 15:47:07 -0700 | [diff] [blame] | 752 | {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZI)Landroid/graphics/Bitmap;", |
Mathias Agopian | 0449a40 | 2013-03-01 23:01:51 -0800 | [diff] [blame] | 753 | (void*)nativeScreenshotBitmap }, |
Dan Stoza | 9890e341 | 2014-05-22 16:12:54 -0700 | [diff] [blame] | 754 | {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;Landroid/graphics/Rect;IIIIZZ)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 755 | (void*)nativeScreenshot }, |
| 756 | {"nativeOpenTransaction", "()V", |
| 757 | (void*)nativeOpenTransaction }, |
Robert Carr | e9953b1 | 2016-05-23 20:52:04 -0700 | [diff] [blame] | 758 | {"nativeCloseTransaction", "(Z)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 759 | (void*)nativeCloseTransaction }, |
| 760 | {"nativeSetAnimationTransaction", "()V", |
| 761 | (void*)nativeSetAnimationTransaction }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 762 | {"nativeSetLayer", "(JI)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 763 | (void*)nativeSetLayer }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 764 | {"nativeSetPosition", "(JFF)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 765 | (void*)nativeSetPosition }, |
Robert Carr | 6da3cc0 | 2016-06-16 15:17:07 -0700 | [diff] [blame] | 766 | {"nativeSetGeometryAppliesWithResize", "(J)V", |
| 767 | (void*)nativeSetGeometryAppliesWithResize }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 768 | {"nativeSetSize", "(JII)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 769 | (void*)nativeSetSize }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 770 | {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 771 | (void*)nativeSetTransparentRegionHint }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 772 | {"nativeSetAlpha", "(JF)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 773 | (void*)nativeSetAlpha }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 774 | {"nativeSetMatrix", "(JFFFF)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 775 | (void*)nativeSetMatrix }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 776 | {"nativeSetFlags", "(JII)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 777 | (void*)nativeSetFlags }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 778 | {"nativeSetWindowCrop", "(JIIII)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 779 | (void*)nativeSetWindowCrop }, |
Pablo Ceballos | 27982e6 | 2016-03-09 10:50:45 -0800 | [diff] [blame] | 780 | {"nativeSetFinalCrop", "(JIIII)V", |
| 781 | (void*)nativeSetFinalCrop }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 782 | {"nativeSetLayerStack", "(JI)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 783 | (void*)nativeSetLayerStack }, |
| 784 | {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;", |
| 785 | (void*)nativeGetBuiltInDisplay }, |
| 786 | {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;", |
| 787 | (void*)nativeCreateDisplay }, |
Jesse Hall | 6a6bc21 | 2013-08-08 12:15:03 -0700 | [diff] [blame] | 788 | {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V", |
| 789 | (void*)nativeDestroyDisplay }, |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 790 | {"nativeSetDisplaySurface", "(Landroid/os/IBinder;J)V", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 791 | (void*)nativeSetDisplaySurface }, |
| 792 | {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V", |
| 793 | (void*)nativeSetDisplayLayerStack }, |
| 794 | {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V", |
| 795 | (void*)nativeSetDisplayProjection }, |
Michael Wright | 01e840f | 2014-06-26 16:03:25 -0700 | [diff] [blame] | 796 | {"nativeSetDisplaySize", "(Landroid/os/IBinder;II)V", |
| 797 | (void*)nativeSetDisplaySize }, |
Dan Stoza | 0010105 | 2014-05-02 15:23:40 -0700 | [diff] [blame] | 798 | {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;", |
| 799 | (void*)nativeGetDisplayConfigs }, |
| 800 | {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I", |
| 801 | (void*)nativeGetActiveConfig }, |
| 802 | {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z", |
| 803 | (void*)nativeSetActiveConfig }, |
Michael Wright | 1c9977b | 2016-07-12 13:30:10 -0700 | [diff] [blame] | 804 | {"nativeGetDisplayColorModes", "(Landroid/os/IBinder;)[I", |
| 805 | (void*)nativeGetDisplayColorModes}, |
| 806 | {"nativeGetActiveColorMode", "(Landroid/os/IBinder;)I", |
| 807 | (void*)nativeGetActiveColorMode}, |
| 808 | {"nativeSetActiveColorMode", "(Landroid/os/IBinder;I)Z", |
| 809 | (void*)nativeSetActiveColorMode}, |
Michael Wright | 9ff94c0 | 2016-03-30 18:05:40 -0700 | [diff] [blame] | 810 | {"nativeGetHdrCapabilities", "(Landroid/os/IBinder;)Landroid/view/Display$HdrCapabilities;", |
| 811 | (void*)nativeGetHdrCapabilities }, |
Svetoslav | 1376d60 | 2014-03-13 11:17:26 -0700 | [diff] [blame] | 812 | {"nativeClearContentFrameStats", "(J)Z", |
| 813 | (void*)nativeClearContentFrameStats }, |
| 814 | {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z", |
| 815 | (void*)nativeGetContentFrameStats }, |
| 816 | {"nativeClearAnimationFrameStats", "()Z", |
| 817 | (void*)nativeClearAnimationFrameStats }, |
| 818 | {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z", |
| 819 | (void*)nativeGetAnimationFrameStats }, |
Prashant Malani | c55929a | 2014-05-25 01:59:21 -0700 | [diff] [blame] | 820 | {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V", |
| 821 | (void*)nativeSetDisplayPowerMode }, |
Rob Carr | 64e516f | 2015-10-29 00:20:45 +0000 | [diff] [blame] | 822 | {"nativeDeferTransactionUntil", "(JLandroid/os/IBinder;J)V", |
| 823 | (void*)nativeDeferTransactionUntil }, |
Robert Carr | 1ca6a33 | 2016-04-11 18:00:43 -0700 | [diff] [blame] | 824 | {"nativeSetOverrideScalingMode", "(JI)V", |
| 825 | (void*)nativeSetOverrideScalingMode }, |
Rob Carr | 64e516f | 2015-10-29 00:20:45 +0000 | [diff] [blame] | 826 | {"nativeGetHandle", "(J)Landroid/os/IBinder;", |
Robert Carr | 6da3cc0 | 2016-06-16 15:17:07 -0700 | [diff] [blame] | 827 | (void*)nativeGetHandle }, |
| 828 | {"nativeGetTransformToDisplayInverse", "(J)Z", |
Robert Carr | 6486d31 | 2017-01-09 19:48:29 -0800 | [diff] [blame^] | 829 | (void*)nativeGetTransformToDisplayInverse }, |
| 830 | {"nativeScreenshotToBuffer", |
| 831 | "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZI)Landroid/graphics/GraphicBuffer;", |
| 832 | (void*)nativeScreenshotToBuffer }, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 833 | }; |
| 834 | |
| 835 | int register_android_view_SurfaceControl(JNIEnv* env) |
| 836 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 837 | int err = RegisterMethodsOrDie(env, "android/view/SurfaceControl", |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 838 | sSurfaceControlMethods, NELEM(sSurfaceControlMethods)); |
| 839 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 840 | jclass clazz = FindClassOrDie(env, "android/view/SurfaceControl$PhysicalDisplayInfo"); |
| 841 | gPhysicalDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz); |
| 842 | gPhysicalDisplayInfoClassInfo.ctor = GetMethodIDOrDie(env, |
| 843 | gPhysicalDisplayInfoClassInfo.clazz, "<init>", "()V"); |
| 844 | gPhysicalDisplayInfoClassInfo.width = GetFieldIDOrDie(env, clazz, "width", "I"); |
| 845 | gPhysicalDisplayInfoClassInfo.height = GetFieldIDOrDie(env, clazz, "height", "I"); |
| 846 | gPhysicalDisplayInfoClassInfo.refreshRate = GetFieldIDOrDie(env, clazz, "refreshRate", "F"); |
| 847 | gPhysicalDisplayInfoClassInfo.density = GetFieldIDOrDie(env, clazz, "density", "F"); |
| 848 | gPhysicalDisplayInfoClassInfo.xDpi = GetFieldIDOrDie(env, clazz, "xDpi", "F"); |
| 849 | gPhysicalDisplayInfoClassInfo.yDpi = GetFieldIDOrDie(env, clazz, "yDpi", "F"); |
| 850 | gPhysicalDisplayInfoClassInfo.secure = GetFieldIDOrDie(env, clazz, "secure", "Z"); |
| 851 | gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = GetFieldIDOrDie(env, |
| 852 | clazz, "appVsyncOffsetNanos", "J"); |
| 853 | gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env, |
| 854 | clazz, "presentationDeadlineNanos", "J"); |
Svetoslav | 1376d60 | 2014-03-13 11:17:26 -0700 | [diff] [blame] | 855 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 856 | jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect"); |
| 857 | gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I"); |
| 858 | gRectClassInfo.left = GetFieldIDOrDie(env, rectClazz, "left", "I"); |
| 859 | gRectClassInfo.right = GetFieldIDOrDie(env, rectClazz, "right", "I"); |
| 860 | gRectClassInfo.top = GetFieldIDOrDie(env, rectClazz, "top", "I"); |
Dan Stoza | 9890e341 | 2014-05-22 16:12:54 -0700 | [diff] [blame] | 861 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 862 | jclass frameStatsClazz = FindClassOrDie(env, "android/view/FrameStats"); |
| 863 | jfieldID undefined_time_nano_field = GetStaticFieldIDOrDie(env, |
| 864 | frameStatsClazz, "UNDEFINED_TIME_NANO", "J"); |
Svetoslav | 1376d60 | 2014-03-13 11:17:26 -0700 | [diff] [blame] | 865 | nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field); |
| 866 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 867 | jclass contFrameStatsClazz = FindClassOrDie(env, "android/view/WindowContentFrameStats"); |
| 868 | gWindowContentFrameStatsClassInfo.init = GetMethodIDOrDie(env, |
| 869 | contFrameStatsClazz, "init", "(J[J[J[J)V"); |
Svetoslav | 1376d60 | 2014-03-13 11:17:26 -0700 | [diff] [blame] | 870 | gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano; |
| 871 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 872 | jclass animFrameStatsClazz = FindClassOrDie(env, "android/view/WindowAnimationFrameStats"); |
| 873 | gWindowAnimationFrameStatsClassInfo.init = GetMethodIDOrDie(env, |
| 874 | animFrameStatsClazz, "init", "(J[J)V"); |
Svetoslav | 1376d60 | 2014-03-13 11:17:26 -0700 | [diff] [blame] | 875 | gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano; |
| 876 | |
Hangyu Kuang | 54ac219 | 2016-04-25 13:22:02 -0700 | [diff] [blame] | 877 | jclass hdrCapabilitiesClazz = FindClassOrDie(env, "android/view/Display$HdrCapabilities"); |
| 878 | gHdrCapabilitiesClassInfo.clazz = MakeGlobalRefOrDie(env, hdrCapabilitiesClazz); |
| 879 | gHdrCapabilitiesClassInfo.ctor = GetMethodIDOrDie(env, hdrCapabilitiesClazz, "<init>", |
| 880 | "([IFFF)V"); |
| 881 | |
Robert Carr | 6486d31 | 2017-01-09 19:48:29 -0800 | [diff] [blame^] | 882 | jclass graphicsBufferClazz = FindClassOrDie(env, "android/graphics/GraphicBuffer"); |
| 883 | gGraphicBufferClassInfo.clazz = MakeGlobalRefOrDie(env, graphicsBufferClazz); |
| 884 | gGraphicBufferClassInfo.builder = GetStaticMethodIDOrDie(env, graphicsBufferClazz, |
| 885 | "createFromExisting", "(IIIIJ)Landroid/graphics/GraphicBuffer;"); |
| 886 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 887 | return err; |
| 888 | } |
| 889 | |
| 890 | }; |