blob: 2e1e130d837574f885fa199551af880c8dd0b622 [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;
chaviw08520a02018-09-10 16:44:56 -0700169 status_t res = ScreenshotClient::capture(displayToken, sourceCrop, width, height,
170 useIdentityTransform, rotation, &buffer);
Robert Carr6486d312017-01-09 19:48:29 -0800171 if (res != NO_ERROR) {
172 return NULL;
173 }
174
175 return env->CallStaticObjectMethod(gGraphicBufferClassInfo.clazz,
176 gGraphicBufferClassInfo.builder,
177 buffer->getWidth(),
178 buffer->getHeight(),
179 buffer->getPixelFormat(),
Mathias Agopian113fd302017-05-25 18:31:04 -0700180 (jint)buffer->getUsage(),
Patrik Torstensson511a8082017-03-27 15:04:11 +0100181 (jlong)buffer.get());
Robert Carr6486d312017-01-09 19:48:29 -0800182}
183
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000184static jobject nativeCaptureLayers(JNIEnv* env, jclass clazz, jobject layerHandleToken,
chaviwfbe47df2017-11-10 16:14:49 -0800185 jobject sourceCropObj, jfloat frameScale) {
186
187 sp<IBinder> layerHandle = ibinderForJavaObject(env, layerHandleToken);
188 if (layerHandle == NULL) {
189 return NULL;
190 }
191
192 Rect sourceCrop;
193 if (sourceCropObj != NULL) {
194 sourceCrop = rectFromObj(env, sourceCropObj);
195 }
196
197 sp<GraphicBuffer> buffer;
Robert Carrd87f3cf2018-03-07 14:08:09 -0800198 status_t res = ScreenshotClient::captureChildLayers(layerHandle, sourceCrop, frameScale, &buffer);
chaviwfbe47df2017-11-10 16:14:49 -0800199 if (res != NO_ERROR) {
200 return NULL;
201 }
202
203 return env->CallStaticObjectMethod(gGraphicBufferClassInfo.clazz,
204 gGraphicBufferClassInfo.builder,
205 buffer->getWidth(),
206 buffer->getHeight(),
207 buffer->getPixelFormat(),
208 (jint)buffer->getUsage(),
209 (jlong)buffer.get());
chaviw1cda84c2017-10-23 16:47:10 -0700210}
211
Robert Carre13b58e2017-08-31 14:50:44 -0700212static void nativeApplyTransaction(JNIEnv* env, jclass clazz, jlong transactionObj, jboolean sync) {
213 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
214 transaction->apply(sync);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800215}
216
Robert Carrb1579c82017-09-05 14:54:47 -0700217static void nativeMergeTransaction(JNIEnv* env, jclass clazz,
218 jlong transactionObj, jlong otherTransactionObj) {
219 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
220 auto otherTransaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(
221 otherTransactionObj);
222 transaction->merge(std::move(*otherTransaction));
223}
224
Robert Carre13b58e2017-08-31 14:50:44 -0700225static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz, jlong transactionObj) {
226 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
227 transaction->setAnimationTransaction();
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800228}
229
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100230static void nativeSetEarlyWakeup(JNIEnv* env, jclass clazz, jlong transactionObj) {
231 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
232 transaction->setEarlyWakeup();
233}
234
Robert Carre13b58e2017-08-31 14:50:44 -0700235static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong transactionObj,
236 jlong nativeObject, jint zorder) {
237 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800238
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800239 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700240 transaction->setLayer(ctrl, zorder);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800241}
242
Robert Carre13b58e2017-08-31 14:50:44 -0700243static void nativeSetRelativeLayer(JNIEnv* env, jclass clazz, jlong transactionObj,
244 jlong nativeObject,
Robert Carraf422a82017-04-10 18:34:33 -0700245 jobject relativeTo, jint zorder) {
Robert Carre13b58e2017-08-31 14:50:44 -0700246
Robert Carraf422a82017-04-10 18:34:33 -0700247 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
248 sp<IBinder> handle = ibinderForJavaObject(env, relativeTo);
249
Robert Carre13b58e2017-08-31 14:50:44 -0700250 {
251 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
252 transaction->setRelativeLayer(ctrl, handle, zorder);
253 }
Robert Carraf422a82017-04-10 18:34:33 -0700254}
255
Robert Carre13b58e2017-08-31 14:50:44 -0700256static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong transactionObj,
257 jlong nativeObject, jfloat x, jfloat y) {
258 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
259
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800260 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700261 transaction->setPosition(ctrl, x, y);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800262}
263
Robert Carr6da3cc02016-06-16 15:17:07 -0700264static void nativeSetGeometryAppliesWithResize(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700265jlong transactionObj,
Robert Carra9408d42016-06-03 13:28:48 -0700266 jlong nativeObject) {
Robert Carre13b58e2017-08-31 14:50:44 -0700267 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
268
Robert Carra9408d42016-06-03 13:28:48 -0700269 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700270 transaction->setGeometryAppliesWithResize(ctrl);
Robert Carra9408d42016-06-03 13:28:48 -0700271}
272
Robert Carre13b58e2017-08-31 14:50:44 -0700273static void nativeSetSize(JNIEnv* env, jclass clazz, jlong transactionObj,
274 jlong nativeObject, jint w, jint h) {
275 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
276
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800277 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700278 transaction->setSize(ctrl, w, h);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800279}
280
Robert Carre13b58e2017-08-31 14:50:44 -0700281static void nativeSetFlags(JNIEnv* env, jclass clazz, jlong transactionObj,
282 jlong nativeObject, jint flags, jint mask) {
283 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
284
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800285 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700286 transaction->setFlags(ctrl, flags, mask);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800287}
288
Robert Carre13b58e2017-08-31 14:50:44 -0700289static void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jlong transactionObj,
290 jlong nativeObject, jobject regionObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800291 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
292 SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj);
293 if (!region) {
294 doThrowIAE(env);
295 return;
296 }
297
298 const SkIRect& b(region->getBounds());
299 Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
300 if (region->isComplex()) {
301 SkRegion::Iterator it(*region);
302 while (!it.done()) {
303 const SkIRect& r(it.rect());
304 reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
305 it.next();
306 }
307 }
308
Robert Carre13b58e2017-08-31 14:50:44 -0700309 {
310 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
311 transaction->setTransparentRegionHint(ctrl, reg);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800312 }
313}
314
Robert Carre13b58e2017-08-31 14:50:44 -0700315static void nativeSetAlpha(JNIEnv* env, jclass clazz, jlong transactionObj,
316 jlong nativeObject, jfloat alpha) {
317 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
318
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800319 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700320 transaction->setAlpha(ctrl, alpha);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800321}
322
Robert Carre13b58e2017-08-31 14:50:44 -0700323static void nativeSetColor(JNIEnv* env, jclass clazz, jlong transactionObj,
324 jlong nativeObject, jfloatArray fColor) {
325 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
chaviw0dd03f52017-08-25 12:15:26 -0700326 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700327
chaviw0dd03f52017-08-25 12:15:26 -0700328 float* floatColors = env->GetFloatArrayElements(fColor, 0);
329 half3 color(floatColors[0], floatColors[1], floatColors[2]);
Robert Carre13b58e2017-08-31 14:50:44 -0700330 transaction->setColor(ctrl, color);
chaviw0dd03f52017-08-25 12:15:26 -0700331}
332
Robert Carre13b58e2017-08-31 14:50:44 -0700333static void nativeSetMatrix(JNIEnv* env, jclass clazz, jlong transactionObj,
334 jlong nativeObject,
Robert Carr0edf18f2017-02-21 20:01:47 -0800335 jfloat dsdx, jfloat dtdx, jfloat dtdy, jfloat dsdy) {
Robert Carre13b58e2017-08-31 14:50:44 -0700336 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
337
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800338 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700339 transaction->setMatrix(ctrl, dsdx, dtdx, dtdy, dsdy);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800340}
341
Peiyong Lin52bb6b42018-10-01 11:40:50 -0700342static void nativeSetColorTransform(JNIEnv* env, jclass clazz, jlong transactionObj,
343 jlong nativeObject, jfloatArray fMatrix, jfloatArray fTranslation) {
344 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
345 SurfaceControl* const surfaceControl = reinterpret_cast<SurfaceControl*>(nativeObject);
346 float* floatMatrix = env->GetFloatArrayElements(fMatrix, 0);
347 mat3 matrix(static_cast<float const*>(floatMatrix));
348 float* floatTranslation = env->GetFloatArrayElements(fTranslation, 0);
349 vec3 translation(floatTranslation[0], floatTranslation[1], floatTranslation[2]);
350 transaction->setColorTransform(surfaceControl, matrix, translation);
351}
352
Robert Carre13b58e2017-08-31 14:50:44 -0700353static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong transactionObj,
354 jlong nativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800355 jint l, jint t, jint r, jint b) {
Robert Carre13b58e2017-08-31 14:50:44 -0700356 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
357
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800358 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
359 Rect crop(l, t, r, b);
Marissa Wallcb32fdd2018-07-24 09:53:30 -0700360 transaction->setCrop_legacy(ctrl, crop);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800361}
362
Robert Carre13b58e2017-08-31 14:50:44 -0700363static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong transactionObj,
364 jlong nativeObject, jint layerStack) {
365 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
366
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800367 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700368 transaction->setLayerStack(ctrl, layerStack);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800369}
370
371static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) {
372 sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id));
373 return javaObjectForIBinder(env, token);
374}
375
376static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj,
377 jboolean secure) {
378 ScopedUtfChars name(env, nameObj);
379 sp<IBinder> token(SurfaceComposerClient::createDisplay(
380 String8(name.c_str()), bool(secure)));
381 return javaObjectForIBinder(env, token);
382}
383
Jesse Hall6a6bc212013-08-08 12:15:03 -0700384static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
385 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
386 if (token == NULL) return;
387 SurfaceComposerClient::destroyDisplay(token);
388}
389
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800390static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700391 jlong transactionObj,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000392 jobject tokenObj, jlong nativeSurfaceObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800393 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
394 if (token == NULL) return;
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800395 sp<IGraphicBufferProducer> bufferProducer;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800396 sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800397 if (sur != NULL) {
398 bufferProducer = sur->getIGraphicBufferProducer();
399 }
Robert Carre13b58e2017-08-31 14:50:44 -0700400
401
402 status_t err = NO_ERROR;
403 {
404 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
405 err = transaction->setDisplaySurface(token,
406 bufferProducer);
407 }
Pablo Ceballosaff2f942016-07-29 14:49:55 -0700408 if (err != NO_ERROR) {
409 doThrowIAE(env, "Illegal Surface, could not enable async mode. Was this"
410 " Surface created with singleBufferMode?");
411 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800412}
413
414static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700415 jlong transactionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800416 jobject tokenObj, jint layerStack) {
Robert Carre13b58e2017-08-31 14:50:44 -0700417
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800418 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
419 if (token == NULL) return;
420
Robert Carre13b58e2017-08-31 14:50:44 -0700421 {
422 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
423 transaction->setDisplayLayerStack(token, layerStack);
424 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800425}
426
427static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700428 jlong transactionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800429 jobject tokenObj, jint orientation,
430 jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom,
431 jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) {
432 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
433 if (token == NULL) return;
434 Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom);
435 Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom);
Robert Carre13b58e2017-08-31 14:50:44 -0700436
437 {
438 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
439 transaction->setDisplayProjection(token, orientation, layerStackRect, displayRect);
440 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800441}
442
Michael Wright01e840f2014-06-26 16:03:25 -0700443static void nativeSetDisplaySize(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700444 jlong transactionObj,
Michael Wright01e840f2014-06-26 16:03:25 -0700445 jobject tokenObj, jint width, jint height) {
446 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
447 if (token == NULL) return;
Robert Carre13b58e2017-08-31 14:50:44 -0700448
449 {
450 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
451 transaction->setDisplaySize(token, width, height);
452 }
Michael Wright01e840f2014-06-26 16:03:25 -0700453}
454
Dan Stoza00101052014-05-02 15:23:40 -0700455static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
456 jobject tokenObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800457 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
Dan Stoza00101052014-05-02 15:23:40 -0700458 if (token == NULL) return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800459
Dan Stoza00101052014-05-02 15:23:40 -0700460 Vector<DisplayInfo> configs;
461 if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
462 configs.size() == 0) {
463 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800464 }
465
Dan Stoza00101052014-05-02 15:23:40 -0700466 jobjectArray configArray = env->NewObjectArray(configs.size(),
467 gPhysicalDisplayInfoClassInfo.clazz, NULL);
468
469 for (size_t c = 0; c < configs.size(); ++c) {
470 const DisplayInfo& info = configs[c];
471 jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
472 gPhysicalDisplayInfoClassInfo.ctor);
473 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
474 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
475 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
476 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
477 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
478 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
479 env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700480 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos,
481 info.appVsyncOffset);
482 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos,
483 info.presentationDeadline);
Dan Stoza00101052014-05-02 15:23:40 -0700484 env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
485 env->DeleteLocalRef(infoObj);
486 }
487
488 return configArray;
489}
490
491static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
492 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
493 if (token == NULL) return -1;
494 return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
495}
496
497static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
498 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
499 if (token == NULL) return JNI_FALSE;
500 status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
501 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800502}
503
Michael Wright1c9977b2016-07-12 13:30:10 -0700504static jintArray nativeGetDisplayColorModes(JNIEnv* env, jclass, jobject tokenObj) {
505 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
506 if (token == NULL) return NULL;
Peiyong Linb88549e2018-03-28 12:03:45 -0700507 Vector<ui::ColorMode> colorModes;
Michael Wright1c9977b2016-07-12 13:30:10 -0700508 if (SurfaceComposerClient::getDisplayColorModes(token, &colorModes) != NO_ERROR ||
509 colorModes.isEmpty()) {
510 return NULL;
511 }
512
513 jintArray colorModesArray = env->NewIntArray(colorModes.size());
514 if (colorModesArray == NULL) {
515 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
516 return NULL;
517 }
518 jint* colorModesArrayValues = env->GetIntArrayElements(colorModesArray, 0);
519 for (size_t i = 0; i < colorModes.size(); i++) {
520 colorModesArrayValues[i] = static_cast<jint>(colorModes[i]);
521 }
522 env->ReleaseIntArrayElements(colorModesArray, colorModesArrayValues, 0);
523 return colorModesArray;
524}
525
526static jint nativeGetActiveColorMode(JNIEnv* env, jclass, jobject tokenObj) {
527 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
528 if (token == NULL) return -1;
529 return static_cast<jint>(SurfaceComposerClient::getActiveColorMode(token));
530}
531
532static jboolean nativeSetActiveColorMode(JNIEnv* env, jclass,
533 jobject tokenObj, jint colorMode) {
534 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
535 if (token == NULL) return JNI_FALSE;
536 status_t err = SurfaceComposerClient::setActiveColorMode(token,
Peiyong Linb88549e2018-03-28 12:03:45 -0700537 static_cast<ui::ColorMode>(colorMode));
Michael Wright1c9977b2016-07-12 13:30:10 -0700538 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
539}
540
Prashant Malanic55929a2014-05-25 01:59:21 -0700541static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800542 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
543 if (token == NULL) return;
544
Tom Cherry8ed74bb2017-07-10 14:31:18 -0700545 android::base::Timer t;
Prashant Malanic55929a2014-05-25 01:59:21 -0700546 SurfaceComposerClient::setDisplayPowerMode(token, mode);
Tom Cherry8ed74bb2017-07-10 14:31:18 -0700547 if (t.duration() > 100ms) ALOGD("Excessive delay in setPowerMode()");
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800548}
549
Svetoslav1376d602014-03-13 11:17:26 -0700550static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
551 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
552 status_t err = ctrl->clearLayerFrameStats();
553
554 if (err < 0 && err != NO_INIT) {
555 doThrowIAE(env);
556 }
557
558 // The other end is not ready, just report we failed.
559 if (err == NO_INIT) {
560 return JNI_FALSE;
561 }
562
563 return JNI_TRUE;
564}
565
566static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
567 jobject outStats) {
568 FrameStats stats;
569
570 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
571 status_t err = ctrl->getLayerFrameStats(&stats);
572 if (err < 0 && err != NO_INIT) {
573 doThrowIAE(env);
574 }
575
576 // The other end is not ready, fine just return empty stats.
577 if (err == NO_INIT) {
578 return JNI_FALSE;
579 }
580
581 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
582 size_t frameCount = stats.desiredPresentTimesNano.size();
583
584 jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
585 if (postedTimesNanoDst == NULL) {
586 return JNI_FALSE;
587 }
588
589 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
590 if (presentedTimesNanoDst == NULL) {
591 return JNI_FALSE;
592 }
593
594 jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
595 if (readyTimesNanoDst == NULL) {
596 return JNI_FALSE;
597 }
598
599 nsecs_t postedTimesNanoSrc[frameCount];
600 nsecs_t presentedTimesNanoSrc[frameCount];
601 nsecs_t readyTimesNanoSrc[frameCount];
602
603 for (size_t i = 0; i < frameCount; i++) {
604 nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
605 if (postedTimeNano == INT64_MAX) {
606 postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
607 }
608 postedTimesNanoSrc[i] = postedTimeNano;
609
610 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
611 if (presentedTimeNano == INT64_MAX) {
612 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
613 }
614 presentedTimesNanoSrc[i] = presentedTimeNano;
615
616 nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
617 if (readyTimeNano == INT64_MAX) {
618 readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
619 }
620 readyTimesNanoSrc[i] = readyTimeNano;
621 }
622
623 env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
624 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
625 env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
626
627 env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
628 postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
629
630 if (env->ExceptionCheck()) {
631 return JNI_FALSE;
632 }
633
634 return JNI_TRUE;
635}
636
637static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
638 status_t err = SurfaceComposerClient::clearAnimationFrameStats();
639
640 if (err < 0 && err != NO_INIT) {
641 doThrowIAE(env);
642 }
643
644 // The other end is not ready, just report we failed.
645 if (err == NO_INIT) {
646 return JNI_FALSE;
647 }
648
649 return JNI_TRUE;
650}
651
652static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
653 FrameStats stats;
654
655 status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
656 if (err < 0 && err != NO_INIT) {
657 doThrowIAE(env);
658 }
659
660 // The other end is not ready, fine just return empty stats.
661 if (err == NO_INIT) {
662 return JNI_FALSE;
663 }
664
665 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
666 size_t frameCount = stats.desiredPresentTimesNano.size();
667
668 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
669 if (presentedTimesNanoDst == NULL) {
670 return JNI_FALSE;
671 }
672
673 nsecs_t presentedTimesNanoSrc[frameCount];
674
675 for (size_t i = 0; i < frameCount; i++) {
Allen Hairac5eda32014-04-24 11:50:37 -0700676 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
Svetoslav1376d602014-03-13 11:17:26 -0700677 if (presentedTimeNano == INT64_MAX) {
678 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
679 }
680 presentedTimesNanoSrc[i] = presentedTimeNano;
681 }
682
683 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
684
685 env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
686 presentedTimesNanoDst);
687
688 if (env->ExceptionCheck()) {
689 return JNI_FALSE;
690 }
691
692 return JNI_TRUE;
693}
694
Robert Carre13b58e2017-08-31 14:50:44 -0700695static void nativeDeferTransactionUntil(JNIEnv* env, jclass clazz, jlong transactionObj,
696 jlong nativeObject,
Rob Carr64e516f2015-10-29 00:20:45 +0000697 jobject handleObject, jlong frameNumber) {
698 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
699 sp<IBinder> handle = ibinderForJavaObject(env, handleObject);
700
Robert Carre13b58e2017-08-31 14:50:44 -0700701 {
702 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
Marissa Wallcb32fdd2018-07-24 09:53:30 -0700703 transaction->deferTransactionUntil_legacy(ctrl, handle, frameNumber);
Robert Carre13b58e2017-08-31 14:50:44 -0700704 }
Rob Carr64e516f2015-10-29 00:20:45 +0000705}
706
Robert Carre13b58e2017-08-31 14:50:44 -0700707static void nativeDeferTransactionUntilSurface(JNIEnv* env, jclass clazz, jlong transactionObj,
708 jlong nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800709 jlong surfaceObject, jlong frameNumber) {
Robert Carre13b58e2017-08-31 14:50:44 -0700710 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
711
Robert Carrd5c7dd62017-03-08 10:39:30 -0800712 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
713 sp<Surface> barrier = reinterpret_cast<Surface *>(surfaceObject);
714
Marissa Wallcb32fdd2018-07-24 09:53:30 -0700715 transaction->deferTransactionUntil_legacy(ctrl, barrier, frameNumber);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800716}
717
Robert Carre13b58e2017-08-31 14:50:44 -0700718static void nativeReparentChildren(JNIEnv* env, jclass clazz, jlong transactionObj,
719 jlong nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800720 jobject newParentObject) {
Robert Carre13b58e2017-08-31 14:50:44 -0700721
Robert Carrd5c7dd62017-03-08 10:39:30 -0800722 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
723 sp<IBinder> handle = ibinderForJavaObject(env, newParentObject);
724
Robert Carre13b58e2017-08-31 14:50:44 -0700725 {
726 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
727 transaction->reparentChildren(ctrl, handle);
728 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800729}
730
Robert Carre13b58e2017-08-31 14:50:44 -0700731static void nativeReparent(JNIEnv* env, jclass clazz, jlong transactionObj,
732 jlong nativeObject,
chaviw76431402017-09-18 16:50:05 -0700733 jobject newParentObject) {
chaviw63542382017-08-17 17:39:29 -0700734 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
735 sp<IBinder> parentHandle = ibinderForJavaObject(env, newParentObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700736
737 {
738 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
739 transaction->reparent(ctrl, parentHandle);
740 }
chaviw63542382017-08-17 17:39:29 -0700741}
742
Robert Carre13b58e2017-08-31 14:50:44 -0700743static void nativeSeverChildren(JNIEnv* env, jclass clazz, jlong transactionObj,
744 jlong nativeObject) {
745 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
746
Robert Carrd5c7dd62017-03-08 10:39:30 -0800747 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700748 transaction->detachChildren(ctrl);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800749}
750
Robert Carre13b58e2017-08-31 14:50:44 -0700751static void nativeSetOverrideScalingMode(JNIEnv* env, jclass clazz, jlong transactionObj,
752 jlong nativeObject,
Robert Carr1ca6a332016-04-11 18:00:43 -0700753 jint scalingMode) {
Robert Carre13b58e2017-08-31 14:50:44 -0700754 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
Robert Carr1ca6a332016-04-11 18:00:43 -0700755
Robert Carre13b58e2017-08-31 14:50:44 -0700756 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
757 transaction->setOverrideScalingMode(ctrl, scalingMode);
Robert Carr1ca6a332016-04-11 18:00:43 -0700758}
759
Chavi Weingartenb736e322018-02-23 00:27:54 +0000760static void nativeDestroyInTransaction(JNIEnv* env, jclass clazz,
761 jlong transactionObj,
762 jlong nativeObject) {
763 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
764 auto ctrl = reinterpret_cast<SurfaceControl*>(nativeObject);
765 transaction->destroySurface(ctrl);
766}
767
Rob Carr64e516f2015-10-29 00:20:45 +0000768static jobject nativeGetHandle(JNIEnv* env, jclass clazz, jlong nativeObject) {
769 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Rob Carr64e516f2015-10-29 00:20:45 +0000770 return javaObjectForIBinder(env, ctrl->getHandle());
771}
772
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700773static jobject nativeGetHdrCapabilities(JNIEnv* env, jclass clazz, jobject tokenObject) {
774 sp<IBinder> token(ibinderForJavaObject(env, tokenObject));
775 if (token == NULL) return NULL;
776
777 HdrCapabilities capabilities;
778 SurfaceComposerClient::getHdrCapabilities(token, &capabilities);
779
780 const auto& types = capabilities.getSupportedHdrTypes();
Peiyong Lin3a0c6e12018-04-16 11:05:29 -0700781 std::vector<int32_t> intTypes;
782 for (auto type : types) {
783 intTypes.push_back(static_cast<int32_t>(type));
784 }
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700785 auto typesArray = env->NewIntArray(types.size());
Peiyong Lin3a0c6e12018-04-16 11:05:29 -0700786 env->SetIntArrayRegion(typesArray, 0, intTypes.size(), intTypes.data());
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700787
Michael Wright9ff94c02016-03-30 18:05:40 -0700788 return env->NewObject(gHdrCapabilitiesClassInfo.clazz, gHdrCapabilitiesClassInfo.ctor,
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700789 typesArray, capabilities.getDesiredMaxLuminance(),
790 capabilities.getDesiredMaxAverageLuminance(), capabilities.getDesiredMinLuminance());
791}
792
Jorim Jaggi06975df2017-12-01 14:52:13 +0100793static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz, jobject parcelObj) {
794 Parcel* parcel = parcelForJavaObject(env, parcelObj);
795 if (parcel == NULL) {
796 doThrowNPE(env);
797 return 0;
798 }
799 sp<SurfaceControl> surface = SurfaceControl::readFromParcel(parcel);
800 if (surface == nullptr) {
801 return 0;
802 }
803 surface->incStrong((void *)nativeCreate);
804 return reinterpret_cast<jlong>(surface.get());
805}
806
807static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
808 jlong nativeObject, jobject parcelObj) {
809 Parcel* parcel = parcelForJavaObject(env, parcelObj);
810 if (parcel == NULL) {
811 doThrowNPE(env);
812 return;
813 }
814 SurfaceControl* const self = reinterpret_cast<SurfaceControl *>(nativeObject);
815 self->writeToParcel(parcel);
816}
817
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800818// ----------------------------------------------------------------------------
819
Daniel Micay76f6a862015-09-19 17:31:01 -0400820static const JNINativeMethod sSurfaceControlMethods[] = {
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500821 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIIIJII)J",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800822 (void*)nativeCreate },
Jorim Jaggi06975df2017-12-01 14:52:13 +0100823 {"nativeReadFromParcel", "(Landroid/os/Parcel;)J",
824 (void*)nativeReadFromParcel },
825 {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
826 (void*)nativeWriteToParcel },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000827 {"nativeRelease", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800828 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000829 {"nativeDestroy", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800830 (void*)nativeDestroy },
Chong Zhang47e36a32016-02-29 16:44:33 -0800831 {"nativeDisconnect", "(J)V",
832 (void*)nativeDisconnect },
Robert Carre13b58e2017-08-31 14:50:44 -0700833 {"nativeCreateTransaction", "()J",
834 (void*)nativeCreateTransaction },
835 {"nativeApplyTransaction", "(JZ)V",
836 (void*)nativeApplyTransaction },
837 {"nativeGetNativeTransactionFinalizer", "()J",
838 (void*)nativeGetNativeTransactionFinalizer },
Robert Carrb1579c82017-09-05 14:54:47 -0700839 {"nativeMergeTransaction", "(JJ)V",
840 (void*)nativeMergeTransaction },
Robert Carre13b58e2017-08-31 14:50:44 -0700841 {"nativeSetAnimationTransaction", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800842 (void*)nativeSetAnimationTransaction },
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100843 {"nativeSetEarlyWakeup", "(J)V",
844 (void*)nativeSetEarlyWakeup },
Robert Carre13b58e2017-08-31 14:50:44 -0700845 {"nativeSetLayer", "(JJI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800846 (void*)nativeSetLayer },
Robert Carre13b58e2017-08-31 14:50:44 -0700847 {"nativeSetRelativeLayer", "(JJLandroid/os/IBinder;I)V",
Robert Carraf422a82017-04-10 18:34:33 -0700848 (void*)nativeSetRelativeLayer },
Robert Carre13b58e2017-08-31 14:50:44 -0700849 {"nativeSetPosition", "(JJFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800850 (void*)nativeSetPosition },
Robert Carre13b58e2017-08-31 14:50:44 -0700851 {"nativeSetGeometryAppliesWithResize", "(JJ)V",
Robert Carr6da3cc02016-06-16 15:17:07 -0700852 (void*)nativeSetGeometryAppliesWithResize },
Robert Carre13b58e2017-08-31 14:50:44 -0700853 {"nativeSetSize", "(JJII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800854 (void*)nativeSetSize },
Robert Carre13b58e2017-08-31 14:50:44 -0700855 {"nativeSetTransparentRegionHint", "(JJLandroid/graphics/Region;)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800856 (void*)nativeSetTransparentRegionHint },
Robert Carre13b58e2017-08-31 14:50:44 -0700857 {"nativeSetAlpha", "(JJF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800858 (void*)nativeSetAlpha },
Robert Carre13b58e2017-08-31 14:50:44 -0700859 {"nativeSetColor", "(JJ[F)V",
chaviw0dd03f52017-08-25 12:15:26 -0700860 (void*)nativeSetColor },
Robert Carre13b58e2017-08-31 14:50:44 -0700861 {"nativeSetMatrix", "(JJFFFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800862 (void*)nativeSetMatrix },
Peiyong Lin52bb6b42018-10-01 11:40:50 -0700863 {"nativeSetColorTransform", "(JJ[F[F)V",
864 (void*)nativeSetColorTransform },
Robert Carre13b58e2017-08-31 14:50:44 -0700865 {"nativeSetFlags", "(JJII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800866 (void*)nativeSetFlags },
Robert Carre13b58e2017-08-31 14:50:44 -0700867 {"nativeSetWindowCrop", "(JJIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800868 (void*)nativeSetWindowCrop },
Robert Carre13b58e2017-08-31 14:50:44 -0700869 {"nativeSetLayerStack", "(JJI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800870 (void*)nativeSetLayerStack },
871 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
872 (void*)nativeGetBuiltInDisplay },
873 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
874 (void*)nativeCreateDisplay },
Jesse Hall6a6bc212013-08-08 12:15:03 -0700875 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
876 (void*)nativeDestroyDisplay },
Robert Carre13b58e2017-08-31 14:50:44 -0700877 {"nativeSetDisplaySurface", "(JLandroid/os/IBinder;J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800878 (void*)nativeSetDisplaySurface },
Robert Carre13b58e2017-08-31 14:50:44 -0700879 {"nativeSetDisplayLayerStack", "(JLandroid/os/IBinder;I)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800880 (void*)nativeSetDisplayLayerStack },
Robert Carre13b58e2017-08-31 14:50:44 -0700881 {"nativeSetDisplayProjection", "(JLandroid/os/IBinder;IIIIIIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800882 (void*)nativeSetDisplayProjection },
Robert Carre13b58e2017-08-31 14:50:44 -0700883 {"nativeSetDisplaySize", "(JLandroid/os/IBinder;II)V",
Michael Wright01e840f2014-06-26 16:03:25 -0700884 (void*)nativeSetDisplaySize },
Dan Stoza00101052014-05-02 15:23:40 -0700885 {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
886 (void*)nativeGetDisplayConfigs },
887 {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
888 (void*)nativeGetActiveConfig },
889 {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
890 (void*)nativeSetActiveConfig },
Michael Wright1c9977b2016-07-12 13:30:10 -0700891 {"nativeGetDisplayColorModes", "(Landroid/os/IBinder;)[I",
892 (void*)nativeGetDisplayColorModes},
893 {"nativeGetActiveColorMode", "(Landroid/os/IBinder;)I",
894 (void*)nativeGetActiveColorMode},
895 {"nativeSetActiveColorMode", "(Landroid/os/IBinder;I)Z",
896 (void*)nativeSetActiveColorMode},
Michael Wright9ff94c02016-03-30 18:05:40 -0700897 {"nativeGetHdrCapabilities", "(Landroid/os/IBinder;)Landroid/view/Display$HdrCapabilities;",
898 (void*)nativeGetHdrCapabilities },
Svetoslav1376d602014-03-13 11:17:26 -0700899 {"nativeClearContentFrameStats", "(J)Z",
900 (void*)nativeClearContentFrameStats },
901 {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
902 (void*)nativeGetContentFrameStats },
903 {"nativeClearAnimationFrameStats", "()Z",
904 (void*)nativeClearAnimationFrameStats },
905 {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
906 (void*)nativeGetAnimationFrameStats },
Prashant Malanic55929a2014-05-25 01:59:21 -0700907 {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
908 (void*)nativeSetDisplayPowerMode },
Robert Carre13b58e2017-08-31 14:50:44 -0700909 {"nativeDeferTransactionUntil", "(JJLandroid/os/IBinder;J)V",
Rob Carr64e516f2015-10-29 00:20:45 +0000910 (void*)nativeDeferTransactionUntil },
Robert Carre13b58e2017-08-31 14:50:44 -0700911 {"nativeDeferTransactionUntilSurface", "(JJJJ)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800912 (void*)nativeDeferTransactionUntilSurface },
Robert Carre13b58e2017-08-31 14:50:44 -0700913 {"nativeReparentChildren", "(JJLandroid/os/IBinder;)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800914 (void*)nativeReparentChildren } ,
Robert Carre13b58e2017-08-31 14:50:44 -0700915 {"nativeReparent", "(JJLandroid/os/IBinder;)V",
chaviw76431402017-09-18 16:50:05 -0700916 (void*)nativeReparent },
Robert Carre13b58e2017-08-31 14:50:44 -0700917 {"nativeSeverChildren", "(JJ)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800918 (void*)nativeSeverChildren } ,
Robert Carre13b58e2017-08-31 14:50:44 -0700919 {"nativeSetOverrideScalingMode", "(JJI)V",
Robert Carr1ca6a332016-04-11 18:00:43 -0700920 (void*)nativeSetOverrideScalingMode },
Chavi Weingartenb736e322018-02-23 00:27:54 +0000921 {"nativeDestroy", "(JJ)V",
922 (void*)nativeDestroyInTransaction },
Rob Carr64e516f2015-10-29 00:20:45 +0000923 {"nativeGetHandle", "(J)Landroid/os/IBinder;",
Robert Carr6da3cc02016-06-16 15:17:07 -0700924 (void*)nativeGetHandle },
chaviw08520a02018-09-10 16:44:56 -0700925 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIZI)Landroid/graphics/GraphicBuffer;",
926 (void*)nativeScreenshot },
Chavi Weingartenea2eb5a2017-11-29 21:26:24 +0000927 {"nativeCaptureLayers", "(Landroid/os/IBinder;Landroid/graphics/Rect;F)Landroid/graphics/GraphicBuffer;",
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000928 (void*)nativeCaptureLayers },
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800929};
930
931int register_android_view_SurfaceControl(JNIEnv* env)
932{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800933 int err = RegisterMethodsOrDie(env, "android/view/SurfaceControl",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800934 sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
935
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800936 jclass clazz = FindClassOrDie(env, "android/view/SurfaceControl$PhysicalDisplayInfo");
937 gPhysicalDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
938 gPhysicalDisplayInfoClassInfo.ctor = GetMethodIDOrDie(env,
939 gPhysicalDisplayInfoClassInfo.clazz, "<init>", "()V");
940 gPhysicalDisplayInfoClassInfo.width = GetFieldIDOrDie(env, clazz, "width", "I");
941 gPhysicalDisplayInfoClassInfo.height = GetFieldIDOrDie(env, clazz, "height", "I");
942 gPhysicalDisplayInfoClassInfo.refreshRate = GetFieldIDOrDie(env, clazz, "refreshRate", "F");
943 gPhysicalDisplayInfoClassInfo.density = GetFieldIDOrDie(env, clazz, "density", "F");
944 gPhysicalDisplayInfoClassInfo.xDpi = GetFieldIDOrDie(env, clazz, "xDpi", "F");
945 gPhysicalDisplayInfoClassInfo.yDpi = GetFieldIDOrDie(env, clazz, "yDpi", "F");
946 gPhysicalDisplayInfoClassInfo.secure = GetFieldIDOrDie(env, clazz, "secure", "Z");
947 gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = GetFieldIDOrDie(env,
948 clazz, "appVsyncOffsetNanos", "J");
949 gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env,
950 clazz, "presentationDeadlineNanos", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700951
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800952 jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect");
953 gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I");
954 gRectClassInfo.left = GetFieldIDOrDie(env, rectClazz, "left", "I");
955 gRectClassInfo.right = GetFieldIDOrDie(env, rectClazz, "right", "I");
956 gRectClassInfo.top = GetFieldIDOrDie(env, rectClazz, "top", "I");
Dan Stoza9890e3412014-05-22 16:12:54 -0700957
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800958 jclass frameStatsClazz = FindClassOrDie(env, "android/view/FrameStats");
959 jfieldID undefined_time_nano_field = GetStaticFieldIDOrDie(env,
960 frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700961 nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
962
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800963 jclass contFrameStatsClazz = FindClassOrDie(env, "android/view/WindowContentFrameStats");
964 gWindowContentFrameStatsClassInfo.init = GetMethodIDOrDie(env,
965 contFrameStatsClazz, "init", "(J[J[J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700966 gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
967
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800968 jclass animFrameStatsClazz = FindClassOrDie(env, "android/view/WindowAnimationFrameStats");
969 gWindowAnimationFrameStatsClassInfo.init = GetMethodIDOrDie(env,
970 animFrameStatsClazz, "init", "(J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700971 gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
972
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700973 jclass hdrCapabilitiesClazz = FindClassOrDie(env, "android/view/Display$HdrCapabilities");
974 gHdrCapabilitiesClassInfo.clazz = MakeGlobalRefOrDie(env, hdrCapabilitiesClazz);
975 gHdrCapabilitiesClassInfo.ctor = GetMethodIDOrDie(env, hdrCapabilitiesClazz, "<init>",
976 "([IFFF)V");
977
Robert Carr6486d312017-01-09 19:48:29 -0800978 jclass graphicsBufferClazz = FindClassOrDie(env, "android/graphics/GraphicBuffer");
979 gGraphicBufferClassInfo.clazz = MakeGlobalRefOrDie(env, graphicsBufferClazz);
980 gGraphicBufferClassInfo.builder = GetStaticMethodIDOrDie(env, graphicsBufferClazz,
981 "createFromExisting", "(IIIIJ)Landroid/graphics/GraphicBuffer;");
982
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800983 return err;
984}
985
986};