blob: 4eda3abdd0d62eb2a3d2a4e544e71888eef29f44 [file] [log] [blame]
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001/*
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 Carr6486d312017-01-09 19:48:29 -080018#define LOG_NDEBUG 0
Mathias Agopian3866f0d2013-02-11 22:08:48 -080019
Mathias Agopian3866f0d2013-02-11 22:08:48 -080020#include "android_os_Parcel.h"
21#include "android_util_Binder.h"
John Reckf29ed282015-04-07 07:32:03 -070022#include "android/graphics/Bitmap.h"
Mathias Agopian3866f0d2013-02-11 22:08:48 -080023#include "android/graphics/GraphicsJNI.h"
24#include "android/graphics/Region.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080025#include "core_jni_helpers.h"
Ben Wagner60126ef2015-08-07 12:13:48 -040026
Tom Cherry8ed74bb2017-07-10 14:31:18 -070027#include <android-base/chrono_utils.h>
Steven Moreland2279b252017-07-19 09:50:45 -070028#include <nativehelper/JNIHelp.h>
29#include <nativehelper/ScopedUtfChars.h>
Mathias Agopian0449a402013-03-01 23:01:51 -080030#include <android_runtime/android_view_Surface.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080031#include <android_runtime/android_view_SurfaceSession.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080032#include <gui/Surface.h>
33#include <gui/SurfaceComposerClient.h>
Ben Wagner60126ef2015-08-07 12:13:48 -040034#include <jni.h>
35#include <memory>
36#include <stdio.h>
Michael Wright1c9977b2016-07-12 13:30:10 -070037#include <system/graphics.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080038#include <ui/DisplayInfo.h>
Svetoslav1376d602014-03-13 11:17:26 -070039#include <ui/FrameStats.h>
Peiyong Linb88549e2018-03-28 12:03:45 -070040#include <ui/GraphicTypes.h>
Peiyong Lin371b98f2018-03-14 17:29:10 -070041#include <ui/HdrCapabilities.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080042#include <ui/Rect.h>
43#include <ui/Region.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080044#include <utils/Log.h>
45
Mathias Agopian3866f0d2013-02-11 22:08:48 -080046// ----------------------------------------------------------------------------
47
48namespace android {
49
50static const char* const OutOfResourcesException =
51 "android/view/Surface$OutOfResourcesException";
52
53static struct {
Dan Stoza00101052014-05-02 15:23:40 -070054 jclass clazz;
55 jmethodID ctor;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080056 jfieldID width;
57 jfieldID height;
58 jfieldID refreshRate;
59 jfieldID density;
60 jfieldID xDpi;
61 jfieldID yDpi;
62 jfieldID secure;
Andy McFaddene8b1aeb2014-06-13 14:05:40 -070063 jfieldID appVsyncOffsetNanos;
64 jfieldID presentationDeadlineNanos;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080065} gPhysicalDisplayInfoClassInfo;
66
Dan Stoza9890e3412014-05-22 16:12:54 -070067static struct {
68 jfieldID bottom;
69 jfieldID left;
70 jfieldID right;
71 jfieldID top;
72} gRectClassInfo;
73
Leon Scroggins46cb9bd2014-03-06 15:36:39 -050074// Implements SkMallocPixelRef::ReleaseProc, to delete the screenshot on unref.
75void DeleteScreenshot(void* addr, void* context) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -050076 delete ((ScreenshotClient*) context);
77}
Mathias Agopian3866f0d2013-02-11 22:08:48 -080078
Svetoslav1376d602014-03-13 11:17:26 -070079static struct {
80 nsecs_t UNDEFINED_TIME_NANO;
81 jmethodID init;
82} gWindowContentFrameStatsClassInfo;
83
84static struct {
85 nsecs_t UNDEFINED_TIME_NANO;
86 jmethodID init;
87} gWindowAnimationFrameStatsClassInfo;
88
Hangyu Kuang54ac2192016-04-25 13:22:02 -070089static struct {
90 jclass clazz;
91 jmethodID ctor;
92} gHdrCapabilitiesClassInfo;
93
Robert Carr6486d312017-01-09 19:48:29 -080094static struct {
95 jclass clazz;
96 jmethodID builder;
97} gGraphicBufferClassInfo;
98
Mathias Agopian3866f0d2013-02-11 22:08:48 -080099// ----------------------------------------------------------------------------
100
Robert Carre13b58e2017-08-31 14:50:44 -0700101static jlong nativeCreateTransaction(JNIEnv* env, jclass clazz) {
102 return reinterpret_cast<jlong>(new SurfaceComposerClient::Transaction);
103}
104
105static void releaseTransaction(SurfaceComposerClient::Transaction* t) {
106 delete t;
107}
108
109static jlong nativeGetNativeTransactionFinalizer(JNIEnv* env, jclass clazz) {
110 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&releaseTransaction));
111}
112
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000113static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500114 jstring nameStr, jint w, jint h, jint format, jint flags, jlong parentObject,
115 jint windowType, jint ownerUid) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800116 ScopedUtfChars name(env, nameStr);
117 sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));
Robert Carr838120c2016-11-01 18:31:12 -0700118 SurfaceControl *parent = reinterpret_cast<SurfaceControl*>(parentObject);
Robert Carrb0f39362018-03-14 13:52:25 -0700119 sp<SurfaceControl> surface;
120 status_t err = client->createSurfaceChecked(
121 String8(name.c_str()), w, h, format, &surface, flags, parent, windowType, ownerUid);
122 if (err == NAME_NOT_FOUND) {
123 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
124 return 0;
125 } else if (err != NO_ERROR) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800126 jniThrowException(env, OutOfResourcesException, NULL);
127 return 0;
128 }
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500129
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800130 surface->incStrong((void *)nativeCreate);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000131 return reinterpret_cast<jlong>(surface.get());
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800132}
133
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000134static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800135 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800136 ctrl->decStrong((void *)nativeCreate);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800137}
138
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000139static void nativeDestroy(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800140 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
141 ctrl->clear();
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800142 ctrl->decStrong((void *)nativeCreate);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800143}
144
Chong Zhang47e36a32016-02-29 16:44:33 -0800145static void nativeDisconnect(JNIEnv* env, jclass clazz, jlong nativeObject) {
146 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
147 if (ctrl != NULL) {
148 ctrl->disconnect();
149 }
150}
151
Robert Carr6486d312017-01-09 19:48:29 -0800152static Rect rectFromObj(JNIEnv* env, jobject rectObj) {
153 int left = env->GetIntField(rectObj, gRectClassInfo.left);
154 int top = env->GetIntField(rectObj, gRectClassInfo.top);
155 int right = env->GetIntField(rectObj, gRectClassInfo.right);
156 int bottom = env->GetIntField(rectObj, gRectClassInfo.bottom);
157 return Rect(left, top, right, bottom);
158}
159
chaviw08520a02018-09-10 16:44:56 -0700160static jobject nativeScreenshot(JNIEnv* env, jclass clazz,
Robert Carr6486d312017-01-09 19:48:29 -0800161 jobject displayTokenObj, jobject sourceCropObj, jint width, jint height,
chaviw08520a02018-09-10 16:44:56 -0700162 bool useIdentityTransform, int rotation) {
Robert Carr6486d312017-01-09 19:48:29 -0800163 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
164 if (displayToken == NULL) {
165 return NULL;
166 }
167 Rect sourceCrop = rectFromObj(env, sourceCropObj);
Robert Carr6486d312017-01-09 19:48:29 -0800168 sp<GraphicBuffer> buffer;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700169 status_t res = ScreenshotClient::capture(displayToken, ui::Dataspace::V0_SRGB,
170 ui::PixelFormat::RGBA_8888,
171 sourceCrop, width, height,
172 useIdentityTransform, rotation, &buffer);
Robert Carr6486d312017-01-09 19:48:29 -0800173 if (res != NO_ERROR) {
174 return NULL;
175 }
176
177 return env->CallStaticObjectMethod(gGraphicBufferClassInfo.clazz,
178 gGraphicBufferClassInfo.builder,
179 buffer->getWidth(),
180 buffer->getHeight(),
181 buffer->getPixelFormat(),
Mathias Agopian113fd302017-05-25 18:31:04 -0700182 (jint)buffer->getUsage(),
Patrik Torstensson511a8082017-03-27 15:04:11 +0100183 (jlong)buffer.get());
Robert Carr6486d312017-01-09 19:48:29 -0800184}
185
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000186static jobject nativeCaptureLayers(JNIEnv* env, jclass clazz, jobject layerHandleToken,
chaviwfbe47df2017-11-10 16:14:49 -0800187 jobject sourceCropObj, jfloat frameScale) {
188
189 sp<IBinder> layerHandle = ibinderForJavaObject(env, layerHandleToken);
190 if (layerHandle == NULL) {
191 return NULL;
192 }
193
194 Rect sourceCrop;
195 if (sourceCropObj != NULL) {
196 sourceCrop = rectFromObj(env, sourceCropObj);
197 }
198
199 sp<GraphicBuffer> buffer;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700200 status_t res = ScreenshotClient::captureChildLayers(layerHandle, ui::Dataspace::V0_SRGB,
201 ui::PixelFormat::RGBA_8888, sourceCrop,
202 frameScale, &buffer);
chaviwfbe47df2017-11-10 16:14:49 -0800203 if (res != NO_ERROR) {
204 return NULL;
205 }
206
207 return env->CallStaticObjectMethod(gGraphicBufferClassInfo.clazz,
208 gGraphicBufferClassInfo.builder,
209 buffer->getWidth(),
210 buffer->getHeight(),
211 buffer->getPixelFormat(),
212 (jint)buffer->getUsage(),
213 (jlong)buffer.get());
chaviw1cda84c2017-10-23 16:47:10 -0700214}
215
Robert Carre13b58e2017-08-31 14:50:44 -0700216static void nativeApplyTransaction(JNIEnv* env, jclass clazz, jlong transactionObj, jboolean sync) {
217 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
218 transaction->apply(sync);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800219}
220
Robert Carrb1579c82017-09-05 14:54:47 -0700221static void nativeMergeTransaction(JNIEnv* env, jclass clazz,
222 jlong transactionObj, jlong otherTransactionObj) {
223 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
224 auto otherTransaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(
225 otherTransactionObj);
226 transaction->merge(std::move(*otherTransaction));
227}
228
Robert Carre13b58e2017-08-31 14:50:44 -0700229static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz, jlong transactionObj) {
230 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
231 transaction->setAnimationTransaction();
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800232}
233
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100234static void nativeSetEarlyWakeup(JNIEnv* env, jclass clazz, jlong transactionObj) {
235 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
236 transaction->setEarlyWakeup();
237}
238
Robert Carre13b58e2017-08-31 14:50:44 -0700239static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong transactionObj,
240 jlong nativeObject, jint zorder) {
241 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800242
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800243 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700244 transaction->setLayer(ctrl, zorder);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800245}
246
Robert Carre13b58e2017-08-31 14:50:44 -0700247static void nativeSetRelativeLayer(JNIEnv* env, jclass clazz, jlong transactionObj,
248 jlong nativeObject,
Robert Carraf422a82017-04-10 18:34:33 -0700249 jobject relativeTo, jint zorder) {
Robert Carre13b58e2017-08-31 14:50:44 -0700250
Robert Carraf422a82017-04-10 18:34:33 -0700251 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
252 sp<IBinder> handle = ibinderForJavaObject(env, relativeTo);
253
Robert Carre13b58e2017-08-31 14:50:44 -0700254 {
255 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
256 transaction->setRelativeLayer(ctrl, handle, zorder);
257 }
Robert Carraf422a82017-04-10 18:34:33 -0700258}
259
Robert Carre13b58e2017-08-31 14:50:44 -0700260static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong transactionObj,
261 jlong nativeObject, jfloat x, jfloat y) {
262 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
263
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800264 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700265 transaction->setPosition(ctrl, x, y);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800266}
267
Robert Carr6da3cc02016-06-16 15:17:07 -0700268static void nativeSetGeometryAppliesWithResize(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700269jlong transactionObj,
Robert Carra9408d42016-06-03 13:28:48 -0700270 jlong nativeObject) {
Robert Carre13b58e2017-08-31 14:50:44 -0700271 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
272
Robert Carra9408d42016-06-03 13:28:48 -0700273 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700274 transaction->setGeometryAppliesWithResize(ctrl);
Robert Carra9408d42016-06-03 13:28:48 -0700275}
276
Robert Carre13b58e2017-08-31 14:50:44 -0700277static void nativeSetSize(JNIEnv* env, jclass clazz, jlong transactionObj,
278 jlong nativeObject, jint w, jint h) {
279 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
280
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800281 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700282 transaction->setSize(ctrl, w, h);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800283}
284
Robert Carre13b58e2017-08-31 14:50:44 -0700285static void nativeSetFlags(JNIEnv* env, jclass clazz, jlong transactionObj,
286 jlong nativeObject, jint flags, jint mask) {
287 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
288
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800289 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700290 transaction->setFlags(ctrl, flags, mask);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800291}
292
Robert Carre13b58e2017-08-31 14:50:44 -0700293static void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jlong transactionObj,
294 jlong nativeObject, jobject regionObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800295 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
296 SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj);
297 if (!region) {
298 doThrowIAE(env);
299 return;
300 }
301
302 const SkIRect& b(region->getBounds());
303 Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
304 if (region->isComplex()) {
305 SkRegion::Iterator it(*region);
306 while (!it.done()) {
307 const SkIRect& r(it.rect());
308 reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
309 it.next();
310 }
311 }
312
Robert Carre13b58e2017-08-31 14:50:44 -0700313 {
314 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
315 transaction->setTransparentRegionHint(ctrl, reg);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800316 }
317}
318
Robert Carre13b58e2017-08-31 14:50:44 -0700319static void nativeSetAlpha(JNIEnv* env, jclass clazz, jlong transactionObj,
320 jlong nativeObject, jfloat alpha) {
321 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
322
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800323 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700324 transaction->setAlpha(ctrl, alpha);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800325}
326
Robert Carre13b58e2017-08-31 14:50:44 -0700327static void nativeSetColor(JNIEnv* env, jclass clazz, jlong transactionObj,
328 jlong nativeObject, jfloatArray fColor) {
329 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
chaviw0dd03f52017-08-25 12:15:26 -0700330 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700331
chaviw0dd03f52017-08-25 12:15:26 -0700332 float* floatColors = env->GetFloatArrayElements(fColor, 0);
333 half3 color(floatColors[0], floatColors[1], floatColors[2]);
Robert Carre13b58e2017-08-31 14:50:44 -0700334 transaction->setColor(ctrl, color);
chaviw0dd03f52017-08-25 12:15:26 -0700335}
336
Robert Carre13b58e2017-08-31 14:50:44 -0700337static void nativeSetMatrix(JNIEnv* env, jclass clazz, jlong transactionObj,
338 jlong nativeObject,
Robert Carr0edf18f2017-02-21 20:01:47 -0800339 jfloat dsdx, jfloat dtdx, jfloat dtdy, jfloat dsdy) {
Robert Carre13b58e2017-08-31 14:50:44 -0700340 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
341
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800342 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700343 transaction->setMatrix(ctrl, dsdx, dtdx, dtdy, dsdy);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800344}
345
Peiyong Lin52bb6b42018-10-01 11:40:50 -0700346static void nativeSetColorTransform(JNIEnv* env, jclass clazz, jlong transactionObj,
347 jlong nativeObject, jfloatArray fMatrix, jfloatArray fTranslation) {
348 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
349 SurfaceControl* const surfaceControl = reinterpret_cast<SurfaceControl*>(nativeObject);
350 float* floatMatrix = env->GetFloatArrayElements(fMatrix, 0);
351 mat3 matrix(static_cast<float const*>(floatMatrix));
352 float* floatTranslation = env->GetFloatArrayElements(fTranslation, 0);
353 vec3 translation(floatTranslation[0], floatTranslation[1], floatTranslation[2]);
354 transaction->setColorTransform(surfaceControl, matrix, translation);
355}
356
Robert Carre13b58e2017-08-31 14:50:44 -0700357static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong transactionObj,
358 jlong nativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800359 jint l, jint t, jint r, jint b) {
Robert Carre13b58e2017-08-31 14:50:44 -0700360 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
361
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800362 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
363 Rect crop(l, t, r, b);
Marissa Wallcb32fdd2018-07-24 09:53:30 -0700364 transaction->setCrop_legacy(ctrl, crop);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800365}
366
Robert Carre13b58e2017-08-31 14:50:44 -0700367static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong transactionObj,
368 jlong nativeObject, jint layerStack) {
369 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
370
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800371 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700372 transaction->setLayerStack(ctrl, layerStack);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800373}
374
375static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) {
376 sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id));
377 return javaObjectForIBinder(env, token);
378}
379
380static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj,
381 jboolean secure) {
382 ScopedUtfChars name(env, nameObj);
383 sp<IBinder> token(SurfaceComposerClient::createDisplay(
384 String8(name.c_str()), bool(secure)));
385 return javaObjectForIBinder(env, token);
386}
387
Jesse Hall6a6bc212013-08-08 12:15:03 -0700388static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
389 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
390 if (token == NULL) return;
391 SurfaceComposerClient::destroyDisplay(token);
392}
393
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800394static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700395 jlong transactionObj,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000396 jobject tokenObj, jlong nativeSurfaceObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800397 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
398 if (token == NULL) return;
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800399 sp<IGraphicBufferProducer> bufferProducer;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800400 sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800401 if (sur != NULL) {
402 bufferProducer = sur->getIGraphicBufferProducer();
403 }
Robert Carre13b58e2017-08-31 14:50:44 -0700404
405
406 status_t err = NO_ERROR;
407 {
408 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
409 err = transaction->setDisplaySurface(token,
410 bufferProducer);
411 }
Pablo Ceballosaff2f942016-07-29 14:49:55 -0700412 if (err != NO_ERROR) {
413 doThrowIAE(env, "Illegal Surface, could not enable async mode. Was this"
414 " Surface created with singleBufferMode?");
415 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800416}
417
418static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700419 jlong transactionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800420 jobject tokenObj, jint layerStack) {
Robert Carre13b58e2017-08-31 14:50:44 -0700421
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800422 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
423 if (token == NULL) return;
424
Robert Carre13b58e2017-08-31 14:50:44 -0700425 {
426 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
427 transaction->setDisplayLayerStack(token, layerStack);
428 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800429}
430
431static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700432 jlong transactionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800433 jobject tokenObj, jint orientation,
434 jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom,
435 jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) {
436 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
437 if (token == NULL) return;
438 Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom);
439 Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom);
Robert Carre13b58e2017-08-31 14:50:44 -0700440
441 {
442 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
443 transaction->setDisplayProjection(token, orientation, layerStackRect, displayRect);
444 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800445}
446
Michael Wright01e840f2014-06-26 16:03:25 -0700447static void nativeSetDisplaySize(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700448 jlong transactionObj,
Michael Wright01e840f2014-06-26 16:03:25 -0700449 jobject tokenObj, jint width, jint height) {
450 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
451 if (token == NULL) return;
Robert Carre13b58e2017-08-31 14:50:44 -0700452
453 {
454 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
455 transaction->setDisplaySize(token, width, height);
456 }
Michael Wright01e840f2014-06-26 16:03:25 -0700457}
458
Dan Stoza00101052014-05-02 15:23:40 -0700459static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
460 jobject tokenObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800461 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
Dan Stoza00101052014-05-02 15:23:40 -0700462 if (token == NULL) return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800463
Dan Stoza00101052014-05-02 15:23:40 -0700464 Vector<DisplayInfo> configs;
465 if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
466 configs.size() == 0) {
467 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800468 }
469
Dan Stoza00101052014-05-02 15:23:40 -0700470 jobjectArray configArray = env->NewObjectArray(configs.size(),
471 gPhysicalDisplayInfoClassInfo.clazz, NULL);
472
473 for (size_t c = 0; c < configs.size(); ++c) {
474 const DisplayInfo& info = configs[c];
475 jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
476 gPhysicalDisplayInfoClassInfo.ctor);
477 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
478 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
479 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
480 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
481 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
482 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
483 env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700484 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos,
485 info.appVsyncOffset);
486 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos,
487 info.presentationDeadline);
Dan Stoza00101052014-05-02 15:23:40 -0700488 env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
489 env->DeleteLocalRef(infoObj);
490 }
491
492 return configArray;
493}
494
495static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
496 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
497 if (token == NULL) return -1;
498 return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
499}
500
501static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
502 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
503 if (token == NULL) return JNI_FALSE;
504 status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
505 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800506}
507
Michael Wright1c9977b2016-07-12 13:30:10 -0700508static jintArray nativeGetDisplayColorModes(JNIEnv* env, jclass, jobject tokenObj) {
509 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
510 if (token == NULL) return NULL;
Peiyong Linb88549e2018-03-28 12:03:45 -0700511 Vector<ui::ColorMode> colorModes;
Michael Wright1c9977b2016-07-12 13:30:10 -0700512 if (SurfaceComposerClient::getDisplayColorModes(token, &colorModes) != NO_ERROR ||
513 colorModes.isEmpty()) {
514 return NULL;
515 }
516
517 jintArray colorModesArray = env->NewIntArray(colorModes.size());
518 if (colorModesArray == NULL) {
519 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
520 return NULL;
521 }
522 jint* colorModesArrayValues = env->GetIntArrayElements(colorModesArray, 0);
523 for (size_t i = 0; i < colorModes.size(); i++) {
524 colorModesArrayValues[i] = static_cast<jint>(colorModes[i]);
525 }
526 env->ReleaseIntArrayElements(colorModesArray, colorModesArrayValues, 0);
527 return colorModesArray;
528}
529
530static jint nativeGetActiveColorMode(JNIEnv* env, jclass, jobject tokenObj) {
531 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
532 if (token == NULL) return -1;
533 return static_cast<jint>(SurfaceComposerClient::getActiveColorMode(token));
534}
535
536static jboolean nativeSetActiveColorMode(JNIEnv* env, jclass,
537 jobject tokenObj, jint colorMode) {
538 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
539 if (token == NULL) return JNI_FALSE;
540 status_t err = SurfaceComposerClient::setActiveColorMode(token,
Peiyong Linb88549e2018-03-28 12:03:45 -0700541 static_cast<ui::ColorMode>(colorMode));
Michael Wright1c9977b2016-07-12 13:30:10 -0700542 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
543}
544
Prashant Malanic55929a2014-05-25 01:59:21 -0700545static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800546 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
547 if (token == NULL) return;
548
Tom Cherry8ed74bb2017-07-10 14:31:18 -0700549 android::base::Timer t;
Prashant Malanic55929a2014-05-25 01:59:21 -0700550 SurfaceComposerClient::setDisplayPowerMode(token, mode);
Tom Cherry8ed74bb2017-07-10 14:31:18 -0700551 if (t.duration() > 100ms) ALOGD("Excessive delay in setPowerMode()");
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800552}
553
Svetoslav1376d602014-03-13 11:17:26 -0700554static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
555 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
556 status_t err = ctrl->clearLayerFrameStats();
557
558 if (err < 0 && err != NO_INIT) {
559 doThrowIAE(env);
560 }
561
562 // The other end is not ready, just report we failed.
563 if (err == NO_INIT) {
564 return JNI_FALSE;
565 }
566
567 return JNI_TRUE;
568}
569
570static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
571 jobject outStats) {
572 FrameStats stats;
573
574 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
575 status_t err = ctrl->getLayerFrameStats(&stats);
576 if (err < 0 && err != NO_INIT) {
577 doThrowIAE(env);
578 }
579
580 // The other end is not ready, fine just return empty stats.
581 if (err == NO_INIT) {
582 return JNI_FALSE;
583 }
584
585 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
586 size_t frameCount = stats.desiredPresentTimesNano.size();
587
588 jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
589 if (postedTimesNanoDst == NULL) {
590 return JNI_FALSE;
591 }
592
593 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
594 if (presentedTimesNanoDst == NULL) {
595 return JNI_FALSE;
596 }
597
598 jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
599 if (readyTimesNanoDst == NULL) {
600 return JNI_FALSE;
601 }
602
603 nsecs_t postedTimesNanoSrc[frameCount];
604 nsecs_t presentedTimesNanoSrc[frameCount];
605 nsecs_t readyTimesNanoSrc[frameCount];
606
607 for (size_t i = 0; i < frameCount; i++) {
608 nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
609 if (postedTimeNano == INT64_MAX) {
610 postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
611 }
612 postedTimesNanoSrc[i] = postedTimeNano;
613
614 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
615 if (presentedTimeNano == INT64_MAX) {
616 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
617 }
618 presentedTimesNanoSrc[i] = presentedTimeNano;
619
620 nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
621 if (readyTimeNano == INT64_MAX) {
622 readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
623 }
624 readyTimesNanoSrc[i] = readyTimeNano;
625 }
626
627 env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
628 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
629 env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
630
631 env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
632 postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
633
634 if (env->ExceptionCheck()) {
635 return JNI_FALSE;
636 }
637
638 return JNI_TRUE;
639}
640
641static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
642 status_t err = SurfaceComposerClient::clearAnimationFrameStats();
643
644 if (err < 0 && err != NO_INIT) {
645 doThrowIAE(env);
646 }
647
648 // The other end is not ready, just report we failed.
649 if (err == NO_INIT) {
650 return JNI_FALSE;
651 }
652
653 return JNI_TRUE;
654}
655
656static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
657 FrameStats stats;
658
659 status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
660 if (err < 0 && err != NO_INIT) {
661 doThrowIAE(env);
662 }
663
664 // The other end is not ready, fine just return empty stats.
665 if (err == NO_INIT) {
666 return JNI_FALSE;
667 }
668
669 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
670 size_t frameCount = stats.desiredPresentTimesNano.size();
671
672 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
673 if (presentedTimesNanoDst == NULL) {
674 return JNI_FALSE;
675 }
676
677 nsecs_t presentedTimesNanoSrc[frameCount];
678
679 for (size_t i = 0; i < frameCount; i++) {
Allen Hairac5eda32014-04-24 11:50:37 -0700680 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
Svetoslav1376d602014-03-13 11:17:26 -0700681 if (presentedTimeNano == INT64_MAX) {
682 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
683 }
684 presentedTimesNanoSrc[i] = presentedTimeNano;
685 }
686
687 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
688
689 env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
690 presentedTimesNanoDst);
691
692 if (env->ExceptionCheck()) {
693 return JNI_FALSE;
694 }
695
696 return JNI_TRUE;
697}
698
Robert Carre13b58e2017-08-31 14:50:44 -0700699static void nativeDeferTransactionUntil(JNIEnv* env, jclass clazz, jlong transactionObj,
700 jlong nativeObject,
Rob Carr64e516f2015-10-29 00:20:45 +0000701 jobject handleObject, jlong frameNumber) {
702 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
703 sp<IBinder> handle = ibinderForJavaObject(env, handleObject);
704
Robert Carre13b58e2017-08-31 14:50:44 -0700705 {
706 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
Marissa Wallcb32fdd2018-07-24 09:53:30 -0700707 transaction->deferTransactionUntil_legacy(ctrl, handle, frameNumber);
Robert Carre13b58e2017-08-31 14:50:44 -0700708 }
Rob Carr64e516f2015-10-29 00:20:45 +0000709}
710
Robert Carre13b58e2017-08-31 14:50:44 -0700711static void nativeDeferTransactionUntilSurface(JNIEnv* env, jclass clazz, jlong transactionObj,
712 jlong nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800713 jlong surfaceObject, jlong frameNumber) {
Robert Carre13b58e2017-08-31 14:50:44 -0700714 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
715
Robert Carrd5c7dd62017-03-08 10:39:30 -0800716 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
717 sp<Surface> barrier = reinterpret_cast<Surface *>(surfaceObject);
718
Marissa Wallcb32fdd2018-07-24 09:53:30 -0700719 transaction->deferTransactionUntil_legacy(ctrl, barrier, frameNumber);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800720}
721
Robert Carre13b58e2017-08-31 14:50:44 -0700722static void nativeReparentChildren(JNIEnv* env, jclass clazz, jlong transactionObj,
723 jlong nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800724 jobject newParentObject) {
Robert Carre13b58e2017-08-31 14:50:44 -0700725
Robert Carrd5c7dd62017-03-08 10:39:30 -0800726 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
727 sp<IBinder> handle = ibinderForJavaObject(env, newParentObject);
728
Robert Carre13b58e2017-08-31 14:50:44 -0700729 {
730 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
731 transaction->reparentChildren(ctrl, handle);
732 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800733}
734
Robert Carre13b58e2017-08-31 14:50:44 -0700735static void nativeReparent(JNIEnv* env, jclass clazz, jlong transactionObj,
736 jlong nativeObject,
chaviw76431402017-09-18 16:50:05 -0700737 jobject newParentObject) {
chaviw63542382017-08-17 17:39:29 -0700738 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
739 sp<IBinder> parentHandle = ibinderForJavaObject(env, newParentObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700740
741 {
742 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
743 transaction->reparent(ctrl, parentHandle);
744 }
chaviw63542382017-08-17 17:39:29 -0700745}
746
Robert Carre13b58e2017-08-31 14:50:44 -0700747static void nativeSeverChildren(JNIEnv* env, jclass clazz, jlong transactionObj,
748 jlong nativeObject) {
749 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
750
Robert Carrd5c7dd62017-03-08 10:39:30 -0800751 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700752 transaction->detachChildren(ctrl);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800753}
754
Robert Carre13b58e2017-08-31 14:50:44 -0700755static void nativeSetOverrideScalingMode(JNIEnv* env, jclass clazz, jlong transactionObj,
756 jlong nativeObject,
Robert Carr1ca6a332016-04-11 18:00:43 -0700757 jint scalingMode) {
Robert Carre13b58e2017-08-31 14:50:44 -0700758 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
Robert Carr1ca6a332016-04-11 18:00:43 -0700759
Robert Carre13b58e2017-08-31 14:50:44 -0700760 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
761 transaction->setOverrideScalingMode(ctrl, scalingMode);
Robert Carr1ca6a332016-04-11 18:00:43 -0700762}
763
Chavi Weingartenb736e322018-02-23 00:27:54 +0000764static void nativeDestroyInTransaction(JNIEnv* env, jclass clazz,
765 jlong transactionObj,
766 jlong nativeObject) {
767 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
768 auto ctrl = reinterpret_cast<SurfaceControl*>(nativeObject);
769 transaction->destroySurface(ctrl);
770}
771
Rob Carr64e516f2015-10-29 00:20:45 +0000772static jobject nativeGetHandle(JNIEnv* env, jclass clazz, jlong nativeObject) {
773 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Rob Carr64e516f2015-10-29 00:20:45 +0000774 return javaObjectForIBinder(env, ctrl->getHandle());
775}
776
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700777static jobject nativeGetHdrCapabilities(JNIEnv* env, jclass clazz, jobject tokenObject) {
778 sp<IBinder> token(ibinderForJavaObject(env, tokenObject));
779 if (token == NULL) return NULL;
780
781 HdrCapabilities capabilities;
782 SurfaceComposerClient::getHdrCapabilities(token, &capabilities);
783
784 const auto& types = capabilities.getSupportedHdrTypes();
Peiyong Lin3a0c6e12018-04-16 11:05:29 -0700785 std::vector<int32_t> intTypes;
786 for (auto type : types) {
787 intTypes.push_back(static_cast<int32_t>(type));
788 }
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700789 auto typesArray = env->NewIntArray(types.size());
Peiyong Lin3a0c6e12018-04-16 11:05:29 -0700790 env->SetIntArrayRegion(typesArray, 0, intTypes.size(), intTypes.data());
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700791
Michael Wright9ff94c02016-03-30 18:05:40 -0700792 return env->NewObject(gHdrCapabilitiesClassInfo.clazz, gHdrCapabilitiesClassInfo.ctor,
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700793 typesArray, capabilities.getDesiredMaxLuminance(),
794 capabilities.getDesiredMaxAverageLuminance(), capabilities.getDesiredMinLuminance());
795}
796
Jorim Jaggi06975df2017-12-01 14:52:13 +0100797static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz, jobject parcelObj) {
798 Parcel* parcel = parcelForJavaObject(env, parcelObj);
799 if (parcel == NULL) {
800 doThrowNPE(env);
801 return 0;
802 }
803 sp<SurfaceControl> surface = SurfaceControl::readFromParcel(parcel);
804 if (surface == nullptr) {
805 return 0;
806 }
807 surface->incStrong((void *)nativeCreate);
808 return reinterpret_cast<jlong>(surface.get());
809}
810
811static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
812 jlong nativeObject, jobject parcelObj) {
813 Parcel* parcel = parcelForJavaObject(env, parcelObj);
814 if (parcel == NULL) {
815 doThrowNPE(env);
816 return;
817 }
818 SurfaceControl* const self = reinterpret_cast<SurfaceControl *>(nativeObject);
819 self->writeToParcel(parcel);
820}
821
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800822// ----------------------------------------------------------------------------
823
Daniel Micay76f6a862015-09-19 17:31:01 -0400824static const JNINativeMethod sSurfaceControlMethods[] = {
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500825 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIIIJII)J",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800826 (void*)nativeCreate },
Jorim Jaggi06975df2017-12-01 14:52:13 +0100827 {"nativeReadFromParcel", "(Landroid/os/Parcel;)J",
828 (void*)nativeReadFromParcel },
829 {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
830 (void*)nativeWriteToParcel },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000831 {"nativeRelease", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800832 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000833 {"nativeDestroy", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800834 (void*)nativeDestroy },
Chong Zhang47e36a32016-02-29 16:44:33 -0800835 {"nativeDisconnect", "(J)V",
836 (void*)nativeDisconnect },
Robert Carre13b58e2017-08-31 14:50:44 -0700837 {"nativeCreateTransaction", "()J",
838 (void*)nativeCreateTransaction },
839 {"nativeApplyTransaction", "(JZ)V",
840 (void*)nativeApplyTransaction },
841 {"nativeGetNativeTransactionFinalizer", "()J",
842 (void*)nativeGetNativeTransactionFinalizer },
Robert Carrb1579c82017-09-05 14:54:47 -0700843 {"nativeMergeTransaction", "(JJ)V",
844 (void*)nativeMergeTransaction },
Robert Carre13b58e2017-08-31 14:50:44 -0700845 {"nativeSetAnimationTransaction", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800846 (void*)nativeSetAnimationTransaction },
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100847 {"nativeSetEarlyWakeup", "(J)V",
848 (void*)nativeSetEarlyWakeup },
Robert Carre13b58e2017-08-31 14:50:44 -0700849 {"nativeSetLayer", "(JJI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800850 (void*)nativeSetLayer },
Robert Carre13b58e2017-08-31 14:50:44 -0700851 {"nativeSetRelativeLayer", "(JJLandroid/os/IBinder;I)V",
Robert Carraf422a82017-04-10 18:34:33 -0700852 (void*)nativeSetRelativeLayer },
Robert Carre13b58e2017-08-31 14:50:44 -0700853 {"nativeSetPosition", "(JJFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800854 (void*)nativeSetPosition },
Robert Carre13b58e2017-08-31 14:50:44 -0700855 {"nativeSetGeometryAppliesWithResize", "(JJ)V",
Robert Carr6da3cc02016-06-16 15:17:07 -0700856 (void*)nativeSetGeometryAppliesWithResize },
Robert Carre13b58e2017-08-31 14:50:44 -0700857 {"nativeSetSize", "(JJII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800858 (void*)nativeSetSize },
Robert Carre13b58e2017-08-31 14:50:44 -0700859 {"nativeSetTransparentRegionHint", "(JJLandroid/graphics/Region;)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800860 (void*)nativeSetTransparentRegionHint },
Robert Carre13b58e2017-08-31 14:50:44 -0700861 {"nativeSetAlpha", "(JJF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800862 (void*)nativeSetAlpha },
Robert Carre13b58e2017-08-31 14:50:44 -0700863 {"nativeSetColor", "(JJ[F)V",
chaviw0dd03f52017-08-25 12:15:26 -0700864 (void*)nativeSetColor },
Robert Carre13b58e2017-08-31 14:50:44 -0700865 {"nativeSetMatrix", "(JJFFFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800866 (void*)nativeSetMatrix },
Peiyong Lin52bb6b42018-10-01 11:40:50 -0700867 {"nativeSetColorTransform", "(JJ[F[F)V",
868 (void*)nativeSetColorTransform },
Robert Carre13b58e2017-08-31 14:50:44 -0700869 {"nativeSetFlags", "(JJII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800870 (void*)nativeSetFlags },
Robert Carre13b58e2017-08-31 14:50:44 -0700871 {"nativeSetWindowCrop", "(JJIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800872 (void*)nativeSetWindowCrop },
Robert Carre13b58e2017-08-31 14:50:44 -0700873 {"nativeSetLayerStack", "(JJI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800874 (void*)nativeSetLayerStack },
875 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
876 (void*)nativeGetBuiltInDisplay },
877 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
878 (void*)nativeCreateDisplay },
Jesse Hall6a6bc212013-08-08 12:15:03 -0700879 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
880 (void*)nativeDestroyDisplay },
Robert Carre13b58e2017-08-31 14:50:44 -0700881 {"nativeSetDisplaySurface", "(JLandroid/os/IBinder;J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800882 (void*)nativeSetDisplaySurface },
Robert Carre13b58e2017-08-31 14:50:44 -0700883 {"nativeSetDisplayLayerStack", "(JLandroid/os/IBinder;I)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800884 (void*)nativeSetDisplayLayerStack },
Robert Carre13b58e2017-08-31 14:50:44 -0700885 {"nativeSetDisplayProjection", "(JLandroid/os/IBinder;IIIIIIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800886 (void*)nativeSetDisplayProjection },
Robert Carre13b58e2017-08-31 14:50:44 -0700887 {"nativeSetDisplaySize", "(JLandroid/os/IBinder;II)V",
Michael Wright01e840f2014-06-26 16:03:25 -0700888 (void*)nativeSetDisplaySize },
Dan Stoza00101052014-05-02 15:23:40 -0700889 {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
890 (void*)nativeGetDisplayConfigs },
891 {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
892 (void*)nativeGetActiveConfig },
893 {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
894 (void*)nativeSetActiveConfig },
Michael Wright1c9977b2016-07-12 13:30:10 -0700895 {"nativeGetDisplayColorModes", "(Landroid/os/IBinder;)[I",
896 (void*)nativeGetDisplayColorModes},
897 {"nativeGetActiveColorMode", "(Landroid/os/IBinder;)I",
898 (void*)nativeGetActiveColorMode},
899 {"nativeSetActiveColorMode", "(Landroid/os/IBinder;I)Z",
900 (void*)nativeSetActiveColorMode},
Michael Wright9ff94c02016-03-30 18:05:40 -0700901 {"nativeGetHdrCapabilities", "(Landroid/os/IBinder;)Landroid/view/Display$HdrCapabilities;",
902 (void*)nativeGetHdrCapabilities },
Svetoslav1376d602014-03-13 11:17:26 -0700903 {"nativeClearContentFrameStats", "(J)Z",
904 (void*)nativeClearContentFrameStats },
905 {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
906 (void*)nativeGetContentFrameStats },
907 {"nativeClearAnimationFrameStats", "()Z",
908 (void*)nativeClearAnimationFrameStats },
909 {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
910 (void*)nativeGetAnimationFrameStats },
Prashant Malanic55929a2014-05-25 01:59:21 -0700911 {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
912 (void*)nativeSetDisplayPowerMode },
Robert Carre13b58e2017-08-31 14:50:44 -0700913 {"nativeDeferTransactionUntil", "(JJLandroid/os/IBinder;J)V",
Rob Carr64e516f2015-10-29 00:20:45 +0000914 (void*)nativeDeferTransactionUntil },
Robert Carre13b58e2017-08-31 14:50:44 -0700915 {"nativeDeferTransactionUntilSurface", "(JJJJ)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800916 (void*)nativeDeferTransactionUntilSurface },
Robert Carre13b58e2017-08-31 14:50:44 -0700917 {"nativeReparentChildren", "(JJLandroid/os/IBinder;)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800918 (void*)nativeReparentChildren } ,
Robert Carre13b58e2017-08-31 14:50:44 -0700919 {"nativeReparent", "(JJLandroid/os/IBinder;)V",
chaviw76431402017-09-18 16:50:05 -0700920 (void*)nativeReparent },
Robert Carre13b58e2017-08-31 14:50:44 -0700921 {"nativeSeverChildren", "(JJ)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800922 (void*)nativeSeverChildren } ,
Robert Carre13b58e2017-08-31 14:50:44 -0700923 {"nativeSetOverrideScalingMode", "(JJI)V",
Robert Carr1ca6a332016-04-11 18:00:43 -0700924 (void*)nativeSetOverrideScalingMode },
Chavi Weingartenb736e322018-02-23 00:27:54 +0000925 {"nativeDestroy", "(JJ)V",
926 (void*)nativeDestroyInTransaction },
Rob Carr64e516f2015-10-29 00:20:45 +0000927 {"nativeGetHandle", "(J)Landroid/os/IBinder;",
Robert Carr6da3cc02016-06-16 15:17:07 -0700928 (void*)nativeGetHandle },
chaviw08520a02018-09-10 16:44:56 -0700929 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIZI)Landroid/graphics/GraphicBuffer;",
930 (void*)nativeScreenshot },
Chavi Weingartenea2eb5a2017-11-29 21:26:24 +0000931 {"nativeCaptureLayers", "(Landroid/os/IBinder;Landroid/graphics/Rect;F)Landroid/graphics/GraphicBuffer;",
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000932 (void*)nativeCaptureLayers },
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800933};
934
935int register_android_view_SurfaceControl(JNIEnv* env)
936{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800937 int err = RegisterMethodsOrDie(env, "android/view/SurfaceControl",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800938 sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
939
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800940 jclass clazz = FindClassOrDie(env, "android/view/SurfaceControl$PhysicalDisplayInfo");
941 gPhysicalDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
942 gPhysicalDisplayInfoClassInfo.ctor = GetMethodIDOrDie(env,
943 gPhysicalDisplayInfoClassInfo.clazz, "<init>", "()V");
944 gPhysicalDisplayInfoClassInfo.width = GetFieldIDOrDie(env, clazz, "width", "I");
945 gPhysicalDisplayInfoClassInfo.height = GetFieldIDOrDie(env, clazz, "height", "I");
946 gPhysicalDisplayInfoClassInfo.refreshRate = GetFieldIDOrDie(env, clazz, "refreshRate", "F");
947 gPhysicalDisplayInfoClassInfo.density = GetFieldIDOrDie(env, clazz, "density", "F");
948 gPhysicalDisplayInfoClassInfo.xDpi = GetFieldIDOrDie(env, clazz, "xDpi", "F");
949 gPhysicalDisplayInfoClassInfo.yDpi = GetFieldIDOrDie(env, clazz, "yDpi", "F");
950 gPhysicalDisplayInfoClassInfo.secure = GetFieldIDOrDie(env, clazz, "secure", "Z");
951 gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = GetFieldIDOrDie(env,
952 clazz, "appVsyncOffsetNanos", "J");
953 gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env,
954 clazz, "presentationDeadlineNanos", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700955
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800956 jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect");
957 gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I");
958 gRectClassInfo.left = GetFieldIDOrDie(env, rectClazz, "left", "I");
959 gRectClassInfo.right = GetFieldIDOrDie(env, rectClazz, "right", "I");
960 gRectClassInfo.top = GetFieldIDOrDie(env, rectClazz, "top", "I");
Dan Stoza9890e3412014-05-22 16:12:54 -0700961
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800962 jclass frameStatsClazz = FindClassOrDie(env, "android/view/FrameStats");
963 jfieldID undefined_time_nano_field = GetStaticFieldIDOrDie(env,
964 frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700965 nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
966
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800967 jclass contFrameStatsClazz = FindClassOrDie(env, "android/view/WindowContentFrameStats");
968 gWindowContentFrameStatsClassInfo.init = GetMethodIDOrDie(env,
969 contFrameStatsClazz, "init", "(J[J[J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700970 gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
971
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800972 jclass animFrameStatsClazz = FindClassOrDie(env, "android/view/WindowAnimationFrameStats");
973 gWindowAnimationFrameStatsClassInfo.init = GetMethodIDOrDie(env,
974 animFrameStatsClazz, "init", "(J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700975 gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
976
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700977 jclass hdrCapabilitiesClazz = FindClassOrDie(env, "android/view/Display$HdrCapabilities");
978 gHdrCapabilitiesClassInfo.clazz = MakeGlobalRefOrDie(env, hdrCapabilitiesClazz);
979 gHdrCapabilitiesClassInfo.ctor = GetMethodIDOrDie(env, hdrCapabilitiesClazz, "<init>",
980 "([IFFF)V");
981
Robert Carr6486d312017-01-09 19:48:29 -0800982 jclass graphicsBufferClazz = FindClassOrDie(env, "android/graphics/GraphicBuffer");
983 gGraphicBufferClassInfo.clazz = MakeGlobalRefOrDie(env, graphicsBufferClazz);
984 gGraphicBufferClassInfo.builder = GetStaticMethodIDOrDie(env, graphicsBufferClazz,
985 "createFromExisting", "(IIIIJ)Landroid/graphics/GraphicBuffer;");
986
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800987 return err;
988}
989
990};