blob: b70177ffab9abfb38ae6ea59e1768c5a76400b0a [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
Robert Carre13b58e2017-08-31 14:50:44 -0700342static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong transactionObj,
343 jlong nativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800344 jint l, jint t, jint r, jint b) {
Robert Carre13b58e2017-08-31 14:50:44 -0700345 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
346
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800347 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
348 Rect crop(l, t, r, b);
Marissa Wallcb32fdd2018-07-24 09:53:30 -0700349 transaction->setCrop_legacy(ctrl, crop);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800350}
351
Robert Carre13b58e2017-08-31 14:50:44 -0700352static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong transactionObj,
353 jlong nativeObject, jint layerStack) {
354 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
355
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800356 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700357 transaction->setLayerStack(ctrl, layerStack);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800358}
359
360static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) {
361 sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id));
362 return javaObjectForIBinder(env, token);
363}
364
365static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj,
366 jboolean secure) {
367 ScopedUtfChars name(env, nameObj);
368 sp<IBinder> token(SurfaceComposerClient::createDisplay(
369 String8(name.c_str()), bool(secure)));
370 return javaObjectForIBinder(env, token);
371}
372
Jesse Hall6a6bc212013-08-08 12:15:03 -0700373static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
374 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
375 if (token == NULL) return;
376 SurfaceComposerClient::destroyDisplay(token);
377}
378
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800379static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700380 jlong transactionObj,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000381 jobject tokenObj, jlong nativeSurfaceObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800382 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
383 if (token == NULL) return;
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800384 sp<IGraphicBufferProducer> bufferProducer;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800385 sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800386 if (sur != NULL) {
387 bufferProducer = sur->getIGraphicBufferProducer();
388 }
Robert Carre13b58e2017-08-31 14:50:44 -0700389
390
391 status_t err = NO_ERROR;
392 {
393 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
394 err = transaction->setDisplaySurface(token,
395 bufferProducer);
396 }
Pablo Ceballosaff2f942016-07-29 14:49:55 -0700397 if (err != NO_ERROR) {
398 doThrowIAE(env, "Illegal Surface, could not enable async mode. Was this"
399 " Surface created with singleBufferMode?");
400 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800401}
402
403static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700404 jlong transactionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800405 jobject tokenObj, jint layerStack) {
Robert Carre13b58e2017-08-31 14:50:44 -0700406
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800407 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
408 if (token == NULL) return;
409
Robert Carre13b58e2017-08-31 14:50:44 -0700410 {
411 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
412 transaction->setDisplayLayerStack(token, layerStack);
413 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800414}
415
416static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700417 jlong transactionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800418 jobject tokenObj, jint orientation,
419 jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom,
420 jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) {
421 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
422 if (token == NULL) return;
423 Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom);
424 Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom);
Robert Carre13b58e2017-08-31 14:50:44 -0700425
426 {
427 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
428 transaction->setDisplayProjection(token, orientation, layerStackRect, displayRect);
429 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800430}
431
Michael Wright01e840f2014-06-26 16:03:25 -0700432static void nativeSetDisplaySize(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700433 jlong transactionObj,
Michael Wright01e840f2014-06-26 16:03:25 -0700434 jobject tokenObj, jint width, jint height) {
435 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
436 if (token == NULL) return;
Robert Carre13b58e2017-08-31 14:50:44 -0700437
438 {
439 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
440 transaction->setDisplaySize(token, width, height);
441 }
Michael Wright01e840f2014-06-26 16:03:25 -0700442}
443
Dan Stoza00101052014-05-02 15:23:40 -0700444static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
445 jobject tokenObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800446 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
Dan Stoza00101052014-05-02 15:23:40 -0700447 if (token == NULL) return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800448
Dan Stoza00101052014-05-02 15:23:40 -0700449 Vector<DisplayInfo> configs;
450 if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
451 configs.size() == 0) {
452 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800453 }
454
Dan Stoza00101052014-05-02 15:23:40 -0700455 jobjectArray configArray = env->NewObjectArray(configs.size(),
456 gPhysicalDisplayInfoClassInfo.clazz, NULL);
457
458 for (size_t c = 0; c < configs.size(); ++c) {
459 const DisplayInfo& info = configs[c];
460 jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
461 gPhysicalDisplayInfoClassInfo.ctor);
462 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
463 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
464 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
465 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
466 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
467 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
468 env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700469 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos,
470 info.appVsyncOffset);
471 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos,
472 info.presentationDeadline);
Dan Stoza00101052014-05-02 15:23:40 -0700473 env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
474 env->DeleteLocalRef(infoObj);
475 }
476
477 return configArray;
478}
479
480static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
481 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
482 if (token == NULL) return -1;
483 return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
484}
485
486static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
487 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
488 if (token == NULL) return JNI_FALSE;
489 status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
490 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800491}
492
Michael Wright1c9977b2016-07-12 13:30:10 -0700493static jintArray nativeGetDisplayColorModes(JNIEnv* env, jclass, jobject tokenObj) {
494 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
495 if (token == NULL) return NULL;
Peiyong Linb88549e2018-03-28 12:03:45 -0700496 Vector<ui::ColorMode> colorModes;
Michael Wright1c9977b2016-07-12 13:30:10 -0700497 if (SurfaceComposerClient::getDisplayColorModes(token, &colorModes) != NO_ERROR ||
498 colorModes.isEmpty()) {
499 return NULL;
500 }
501
502 jintArray colorModesArray = env->NewIntArray(colorModes.size());
503 if (colorModesArray == NULL) {
504 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
505 return NULL;
506 }
507 jint* colorModesArrayValues = env->GetIntArrayElements(colorModesArray, 0);
508 for (size_t i = 0; i < colorModes.size(); i++) {
509 colorModesArrayValues[i] = static_cast<jint>(colorModes[i]);
510 }
511 env->ReleaseIntArrayElements(colorModesArray, colorModesArrayValues, 0);
512 return colorModesArray;
513}
514
515static jint nativeGetActiveColorMode(JNIEnv* env, jclass, jobject tokenObj) {
516 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
517 if (token == NULL) return -1;
518 return static_cast<jint>(SurfaceComposerClient::getActiveColorMode(token));
519}
520
521static jboolean nativeSetActiveColorMode(JNIEnv* env, jclass,
522 jobject tokenObj, jint colorMode) {
523 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
524 if (token == NULL) return JNI_FALSE;
525 status_t err = SurfaceComposerClient::setActiveColorMode(token,
Peiyong Linb88549e2018-03-28 12:03:45 -0700526 static_cast<ui::ColorMode>(colorMode));
Michael Wright1c9977b2016-07-12 13:30:10 -0700527 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
528}
529
Prashant Malanic55929a2014-05-25 01:59:21 -0700530static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800531 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
532 if (token == NULL) return;
533
Tom Cherry8ed74bb2017-07-10 14:31:18 -0700534 android::base::Timer t;
Prashant Malanic55929a2014-05-25 01:59:21 -0700535 SurfaceComposerClient::setDisplayPowerMode(token, mode);
Tom Cherry8ed74bb2017-07-10 14:31:18 -0700536 if (t.duration() > 100ms) ALOGD("Excessive delay in setPowerMode()");
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800537}
538
Svetoslav1376d602014-03-13 11:17:26 -0700539static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
540 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
541 status_t err = ctrl->clearLayerFrameStats();
542
543 if (err < 0 && err != NO_INIT) {
544 doThrowIAE(env);
545 }
546
547 // The other end is not ready, just report we failed.
548 if (err == NO_INIT) {
549 return JNI_FALSE;
550 }
551
552 return JNI_TRUE;
553}
554
555static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
556 jobject outStats) {
557 FrameStats stats;
558
559 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
560 status_t err = ctrl->getLayerFrameStats(&stats);
561 if (err < 0 && err != NO_INIT) {
562 doThrowIAE(env);
563 }
564
565 // The other end is not ready, fine just return empty stats.
566 if (err == NO_INIT) {
567 return JNI_FALSE;
568 }
569
570 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
571 size_t frameCount = stats.desiredPresentTimesNano.size();
572
573 jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
574 if (postedTimesNanoDst == NULL) {
575 return JNI_FALSE;
576 }
577
578 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
579 if (presentedTimesNanoDst == NULL) {
580 return JNI_FALSE;
581 }
582
583 jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
584 if (readyTimesNanoDst == NULL) {
585 return JNI_FALSE;
586 }
587
588 nsecs_t postedTimesNanoSrc[frameCount];
589 nsecs_t presentedTimesNanoSrc[frameCount];
590 nsecs_t readyTimesNanoSrc[frameCount];
591
592 for (size_t i = 0; i < frameCount; i++) {
593 nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
594 if (postedTimeNano == INT64_MAX) {
595 postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
596 }
597 postedTimesNanoSrc[i] = postedTimeNano;
598
599 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
600 if (presentedTimeNano == INT64_MAX) {
601 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
602 }
603 presentedTimesNanoSrc[i] = presentedTimeNano;
604
605 nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
606 if (readyTimeNano == INT64_MAX) {
607 readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
608 }
609 readyTimesNanoSrc[i] = readyTimeNano;
610 }
611
612 env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
613 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
614 env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
615
616 env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
617 postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
618
619 if (env->ExceptionCheck()) {
620 return JNI_FALSE;
621 }
622
623 return JNI_TRUE;
624}
625
626static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
627 status_t err = SurfaceComposerClient::clearAnimationFrameStats();
628
629 if (err < 0 && err != NO_INIT) {
630 doThrowIAE(env);
631 }
632
633 // The other end is not ready, just report we failed.
634 if (err == NO_INIT) {
635 return JNI_FALSE;
636 }
637
638 return JNI_TRUE;
639}
640
641static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
642 FrameStats stats;
643
644 status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
645 if (err < 0 && err != NO_INIT) {
646 doThrowIAE(env);
647 }
648
649 // The other end is not ready, fine just return empty stats.
650 if (err == NO_INIT) {
651 return JNI_FALSE;
652 }
653
654 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
655 size_t frameCount = stats.desiredPresentTimesNano.size();
656
657 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
658 if (presentedTimesNanoDst == NULL) {
659 return JNI_FALSE;
660 }
661
662 nsecs_t presentedTimesNanoSrc[frameCount];
663
664 for (size_t i = 0; i < frameCount; i++) {
Allen Hairac5eda32014-04-24 11:50:37 -0700665 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
Svetoslav1376d602014-03-13 11:17:26 -0700666 if (presentedTimeNano == INT64_MAX) {
667 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
668 }
669 presentedTimesNanoSrc[i] = presentedTimeNano;
670 }
671
672 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
673
674 env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
675 presentedTimesNanoDst);
676
677 if (env->ExceptionCheck()) {
678 return JNI_FALSE;
679 }
680
681 return JNI_TRUE;
682}
683
Robert Carre13b58e2017-08-31 14:50:44 -0700684static void nativeDeferTransactionUntil(JNIEnv* env, jclass clazz, jlong transactionObj,
685 jlong nativeObject,
Rob Carr64e516f2015-10-29 00:20:45 +0000686 jobject handleObject, jlong frameNumber) {
687 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
688 sp<IBinder> handle = ibinderForJavaObject(env, handleObject);
689
Robert Carre13b58e2017-08-31 14:50:44 -0700690 {
691 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
Marissa Wallcb32fdd2018-07-24 09:53:30 -0700692 transaction->deferTransactionUntil_legacy(ctrl, handle, frameNumber);
Robert Carre13b58e2017-08-31 14:50:44 -0700693 }
Rob Carr64e516f2015-10-29 00:20:45 +0000694}
695
Robert Carre13b58e2017-08-31 14:50:44 -0700696static void nativeDeferTransactionUntilSurface(JNIEnv* env, jclass clazz, jlong transactionObj,
697 jlong nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800698 jlong surfaceObject, jlong frameNumber) {
Robert Carre13b58e2017-08-31 14:50:44 -0700699 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
700
Robert Carrd5c7dd62017-03-08 10:39:30 -0800701 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
702 sp<Surface> barrier = reinterpret_cast<Surface *>(surfaceObject);
703
Marissa Wallcb32fdd2018-07-24 09:53:30 -0700704 transaction->deferTransactionUntil_legacy(ctrl, barrier, frameNumber);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800705}
706
Robert Carre13b58e2017-08-31 14:50:44 -0700707static void nativeReparentChildren(JNIEnv* env, jclass clazz, jlong transactionObj,
708 jlong nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800709 jobject newParentObject) {
Robert Carre13b58e2017-08-31 14:50:44 -0700710
Robert Carrd5c7dd62017-03-08 10:39:30 -0800711 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
712 sp<IBinder> handle = ibinderForJavaObject(env, newParentObject);
713
Robert Carre13b58e2017-08-31 14:50:44 -0700714 {
715 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
716 transaction->reparentChildren(ctrl, handle);
717 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800718}
719
Robert Carre13b58e2017-08-31 14:50:44 -0700720static void nativeReparent(JNIEnv* env, jclass clazz, jlong transactionObj,
721 jlong nativeObject,
chaviw76431402017-09-18 16:50:05 -0700722 jobject newParentObject) {
chaviw63542382017-08-17 17:39:29 -0700723 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
724 sp<IBinder> parentHandle = ibinderForJavaObject(env, newParentObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700725
726 {
727 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
728 transaction->reparent(ctrl, parentHandle);
729 }
chaviw63542382017-08-17 17:39:29 -0700730}
731
Robert Carre13b58e2017-08-31 14:50:44 -0700732static void nativeSeverChildren(JNIEnv* env, jclass clazz, jlong transactionObj,
733 jlong nativeObject) {
734 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
735
Robert Carrd5c7dd62017-03-08 10:39:30 -0800736 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700737 transaction->detachChildren(ctrl);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800738}
739
Robert Carre13b58e2017-08-31 14:50:44 -0700740static void nativeSetOverrideScalingMode(JNIEnv* env, jclass clazz, jlong transactionObj,
741 jlong nativeObject,
Robert Carr1ca6a332016-04-11 18:00:43 -0700742 jint scalingMode) {
Robert Carre13b58e2017-08-31 14:50:44 -0700743 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
Robert Carr1ca6a332016-04-11 18:00:43 -0700744
Robert Carre13b58e2017-08-31 14:50:44 -0700745 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
746 transaction->setOverrideScalingMode(ctrl, scalingMode);
Robert Carr1ca6a332016-04-11 18:00:43 -0700747}
748
Chavi Weingartenb736e322018-02-23 00:27:54 +0000749static void nativeDestroyInTransaction(JNIEnv* env, jclass clazz,
750 jlong transactionObj,
751 jlong nativeObject) {
752 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
753 auto ctrl = reinterpret_cast<SurfaceControl*>(nativeObject);
754 transaction->destroySurface(ctrl);
755}
756
Rob Carr64e516f2015-10-29 00:20:45 +0000757static jobject nativeGetHandle(JNIEnv* env, jclass clazz, jlong nativeObject) {
758 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Rob Carr64e516f2015-10-29 00:20:45 +0000759 return javaObjectForIBinder(env, ctrl->getHandle());
760}
761
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700762static jobject nativeGetHdrCapabilities(JNIEnv* env, jclass clazz, jobject tokenObject) {
763 sp<IBinder> token(ibinderForJavaObject(env, tokenObject));
764 if (token == NULL) return NULL;
765
766 HdrCapabilities capabilities;
767 SurfaceComposerClient::getHdrCapabilities(token, &capabilities);
768
769 const auto& types = capabilities.getSupportedHdrTypes();
Peiyong Lin3a0c6e12018-04-16 11:05:29 -0700770 std::vector<int32_t> intTypes;
771 for (auto type : types) {
772 intTypes.push_back(static_cast<int32_t>(type));
773 }
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700774 auto typesArray = env->NewIntArray(types.size());
Peiyong Lin3a0c6e12018-04-16 11:05:29 -0700775 env->SetIntArrayRegion(typesArray, 0, intTypes.size(), intTypes.data());
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700776
Michael Wright9ff94c02016-03-30 18:05:40 -0700777 return env->NewObject(gHdrCapabilitiesClassInfo.clazz, gHdrCapabilitiesClassInfo.ctor,
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700778 typesArray, capabilities.getDesiredMaxLuminance(),
779 capabilities.getDesiredMaxAverageLuminance(), capabilities.getDesiredMinLuminance());
780}
781
Jorim Jaggi06975df2017-12-01 14:52:13 +0100782static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz, jobject parcelObj) {
783 Parcel* parcel = parcelForJavaObject(env, parcelObj);
784 if (parcel == NULL) {
785 doThrowNPE(env);
786 return 0;
787 }
788 sp<SurfaceControl> surface = SurfaceControl::readFromParcel(parcel);
789 if (surface == nullptr) {
790 return 0;
791 }
792 surface->incStrong((void *)nativeCreate);
793 return reinterpret_cast<jlong>(surface.get());
794}
795
796static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
797 jlong nativeObject, jobject parcelObj) {
798 Parcel* parcel = parcelForJavaObject(env, parcelObj);
799 if (parcel == NULL) {
800 doThrowNPE(env);
801 return;
802 }
803 SurfaceControl* const self = reinterpret_cast<SurfaceControl *>(nativeObject);
804 self->writeToParcel(parcel);
805}
806
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800807// ----------------------------------------------------------------------------
808
Daniel Micay76f6a862015-09-19 17:31:01 -0400809static const JNINativeMethod sSurfaceControlMethods[] = {
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500810 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIIIJII)J",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800811 (void*)nativeCreate },
Jorim Jaggi06975df2017-12-01 14:52:13 +0100812 {"nativeReadFromParcel", "(Landroid/os/Parcel;)J",
813 (void*)nativeReadFromParcel },
814 {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
815 (void*)nativeWriteToParcel },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000816 {"nativeRelease", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800817 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000818 {"nativeDestroy", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800819 (void*)nativeDestroy },
Chong Zhang47e36a32016-02-29 16:44:33 -0800820 {"nativeDisconnect", "(J)V",
821 (void*)nativeDisconnect },
Robert Carre13b58e2017-08-31 14:50:44 -0700822 {"nativeCreateTransaction", "()J",
823 (void*)nativeCreateTransaction },
824 {"nativeApplyTransaction", "(JZ)V",
825 (void*)nativeApplyTransaction },
826 {"nativeGetNativeTransactionFinalizer", "()J",
827 (void*)nativeGetNativeTransactionFinalizer },
Robert Carrb1579c82017-09-05 14:54:47 -0700828 {"nativeMergeTransaction", "(JJ)V",
829 (void*)nativeMergeTransaction },
Robert Carre13b58e2017-08-31 14:50:44 -0700830 {"nativeSetAnimationTransaction", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800831 (void*)nativeSetAnimationTransaction },
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100832 {"nativeSetEarlyWakeup", "(J)V",
833 (void*)nativeSetEarlyWakeup },
Robert Carre13b58e2017-08-31 14:50:44 -0700834 {"nativeSetLayer", "(JJI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800835 (void*)nativeSetLayer },
Robert Carre13b58e2017-08-31 14:50:44 -0700836 {"nativeSetRelativeLayer", "(JJLandroid/os/IBinder;I)V",
Robert Carraf422a82017-04-10 18:34:33 -0700837 (void*)nativeSetRelativeLayer },
Robert Carre13b58e2017-08-31 14:50:44 -0700838 {"nativeSetPosition", "(JJFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800839 (void*)nativeSetPosition },
Robert Carre13b58e2017-08-31 14:50:44 -0700840 {"nativeSetGeometryAppliesWithResize", "(JJ)V",
Robert Carr6da3cc02016-06-16 15:17:07 -0700841 (void*)nativeSetGeometryAppliesWithResize },
Robert Carre13b58e2017-08-31 14:50:44 -0700842 {"nativeSetSize", "(JJII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800843 (void*)nativeSetSize },
Robert Carre13b58e2017-08-31 14:50:44 -0700844 {"nativeSetTransparentRegionHint", "(JJLandroid/graphics/Region;)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800845 (void*)nativeSetTransparentRegionHint },
Robert Carre13b58e2017-08-31 14:50:44 -0700846 {"nativeSetAlpha", "(JJF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800847 (void*)nativeSetAlpha },
Robert Carre13b58e2017-08-31 14:50:44 -0700848 {"nativeSetColor", "(JJ[F)V",
chaviw0dd03f52017-08-25 12:15:26 -0700849 (void*)nativeSetColor },
Robert Carre13b58e2017-08-31 14:50:44 -0700850 {"nativeSetMatrix", "(JJFFFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800851 (void*)nativeSetMatrix },
Robert Carre13b58e2017-08-31 14:50:44 -0700852 {"nativeSetFlags", "(JJII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800853 (void*)nativeSetFlags },
Robert Carre13b58e2017-08-31 14:50:44 -0700854 {"nativeSetWindowCrop", "(JJIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800855 (void*)nativeSetWindowCrop },
Robert Carre13b58e2017-08-31 14:50:44 -0700856 {"nativeSetLayerStack", "(JJI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800857 (void*)nativeSetLayerStack },
858 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
859 (void*)nativeGetBuiltInDisplay },
860 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
861 (void*)nativeCreateDisplay },
Jesse Hall6a6bc212013-08-08 12:15:03 -0700862 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
863 (void*)nativeDestroyDisplay },
Robert Carre13b58e2017-08-31 14:50:44 -0700864 {"nativeSetDisplaySurface", "(JLandroid/os/IBinder;J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800865 (void*)nativeSetDisplaySurface },
Robert Carre13b58e2017-08-31 14:50:44 -0700866 {"nativeSetDisplayLayerStack", "(JLandroid/os/IBinder;I)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800867 (void*)nativeSetDisplayLayerStack },
Robert Carre13b58e2017-08-31 14:50:44 -0700868 {"nativeSetDisplayProjection", "(JLandroid/os/IBinder;IIIIIIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800869 (void*)nativeSetDisplayProjection },
Robert Carre13b58e2017-08-31 14:50:44 -0700870 {"nativeSetDisplaySize", "(JLandroid/os/IBinder;II)V",
Michael Wright01e840f2014-06-26 16:03:25 -0700871 (void*)nativeSetDisplaySize },
Dan Stoza00101052014-05-02 15:23:40 -0700872 {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
873 (void*)nativeGetDisplayConfigs },
874 {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
875 (void*)nativeGetActiveConfig },
876 {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
877 (void*)nativeSetActiveConfig },
Michael Wright1c9977b2016-07-12 13:30:10 -0700878 {"nativeGetDisplayColorModes", "(Landroid/os/IBinder;)[I",
879 (void*)nativeGetDisplayColorModes},
880 {"nativeGetActiveColorMode", "(Landroid/os/IBinder;)I",
881 (void*)nativeGetActiveColorMode},
882 {"nativeSetActiveColorMode", "(Landroid/os/IBinder;I)Z",
883 (void*)nativeSetActiveColorMode},
Michael Wright9ff94c02016-03-30 18:05:40 -0700884 {"nativeGetHdrCapabilities", "(Landroid/os/IBinder;)Landroid/view/Display$HdrCapabilities;",
885 (void*)nativeGetHdrCapabilities },
Svetoslav1376d602014-03-13 11:17:26 -0700886 {"nativeClearContentFrameStats", "(J)Z",
887 (void*)nativeClearContentFrameStats },
888 {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
889 (void*)nativeGetContentFrameStats },
890 {"nativeClearAnimationFrameStats", "()Z",
891 (void*)nativeClearAnimationFrameStats },
892 {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
893 (void*)nativeGetAnimationFrameStats },
Prashant Malanic55929a2014-05-25 01:59:21 -0700894 {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
895 (void*)nativeSetDisplayPowerMode },
Robert Carre13b58e2017-08-31 14:50:44 -0700896 {"nativeDeferTransactionUntil", "(JJLandroid/os/IBinder;J)V",
Rob Carr64e516f2015-10-29 00:20:45 +0000897 (void*)nativeDeferTransactionUntil },
Robert Carre13b58e2017-08-31 14:50:44 -0700898 {"nativeDeferTransactionUntilSurface", "(JJJJ)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800899 (void*)nativeDeferTransactionUntilSurface },
Robert Carre13b58e2017-08-31 14:50:44 -0700900 {"nativeReparentChildren", "(JJLandroid/os/IBinder;)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800901 (void*)nativeReparentChildren } ,
Robert Carre13b58e2017-08-31 14:50:44 -0700902 {"nativeReparent", "(JJLandroid/os/IBinder;)V",
chaviw76431402017-09-18 16:50:05 -0700903 (void*)nativeReparent },
Robert Carre13b58e2017-08-31 14:50:44 -0700904 {"nativeSeverChildren", "(JJ)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800905 (void*)nativeSeverChildren } ,
Robert Carre13b58e2017-08-31 14:50:44 -0700906 {"nativeSetOverrideScalingMode", "(JJI)V",
Robert Carr1ca6a332016-04-11 18:00:43 -0700907 (void*)nativeSetOverrideScalingMode },
Chavi Weingartenb736e322018-02-23 00:27:54 +0000908 {"nativeDestroy", "(JJ)V",
909 (void*)nativeDestroyInTransaction },
Rob Carr64e516f2015-10-29 00:20:45 +0000910 {"nativeGetHandle", "(J)Landroid/os/IBinder;",
Robert Carr6da3cc02016-06-16 15:17:07 -0700911 (void*)nativeGetHandle },
chaviw08520a02018-09-10 16:44:56 -0700912 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIZI)Landroid/graphics/GraphicBuffer;",
913 (void*)nativeScreenshot },
Chavi Weingartenea2eb5a2017-11-29 21:26:24 +0000914 {"nativeCaptureLayers", "(Landroid/os/IBinder;Landroid/graphics/Rect;F)Landroid/graphics/GraphicBuffer;",
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000915 (void*)nativeCaptureLayers },
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800916};
917
918int register_android_view_SurfaceControl(JNIEnv* env)
919{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800920 int err = RegisterMethodsOrDie(env, "android/view/SurfaceControl",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800921 sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
922
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800923 jclass clazz = FindClassOrDie(env, "android/view/SurfaceControl$PhysicalDisplayInfo");
924 gPhysicalDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
925 gPhysicalDisplayInfoClassInfo.ctor = GetMethodIDOrDie(env,
926 gPhysicalDisplayInfoClassInfo.clazz, "<init>", "()V");
927 gPhysicalDisplayInfoClassInfo.width = GetFieldIDOrDie(env, clazz, "width", "I");
928 gPhysicalDisplayInfoClassInfo.height = GetFieldIDOrDie(env, clazz, "height", "I");
929 gPhysicalDisplayInfoClassInfo.refreshRate = GetFieldIDOrDie(env, clazz, "refreshRate", "F");
930 gPhysicalDisplayInfoClassInfo.density = GetFieldIDOrDie(env, clazz, "density", "F");
931 gPhysicalDisplayInfoClassInfo.xDpi = GetFieldIDOrDie(env, clazz, "xDpi", "F");
932 gPhysicalDisplayInfoClassInfo.yDpi = GetFieldIDOrDie(env, clazz, "yDpi", "F");
933 gPhysicalDisplayInfoClassInfo.secure = GetFieldIDOrDie(env, clazz, "secure", "Z");
934 gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = GetFieldIDOrDie(env,
935 clazz, "appVsyncOffsetNanos", "J");
936 gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env,
937 clazz, "presentationDeadlineNanos", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700938
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800939 jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect");
940 gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I");
941 gRectClassInfo.left = GetFieldIDOrDie(env, rectClazz, "left", "I");
942 gRectClassInfo.right = GetFieldIDOrDie(env, rectClazz, "right", "I");
943 gRectClassInfo.top = GetFieldIDOrDie(env, rectClazz, "top", "I");
Dan Stoza9890e3412014-05-22 16:12:54 -0700944
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800945 jclass frameStatsClazz = FindClassOrDie(env, "android/view/FrameStats");
946 jfieldID undefined_time_nano_field = GetStaticFieldIDOrDie(env,
947 frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700948 nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
949
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800950 jclass contFrameStatsClazz = FindClassOrDie(env, "android/view/WindowContentFrameStats");
951 gWindowContentFrameStatsClassInfo.init = GetMethodIDOrDie(env,
952 contFrameStatsClazz, "init", "(J[J[J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700953 gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
954
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800955 jclass animFrameStatsClazz = FindClassOrDie(env, "android/view/WindowAnimationFrameStats");
956 gWindowAnimationFrameStatsClassInfo.init = GetMethodIDOrDie(env,
957 animFrameStatsClazz, "init", "(J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700958 gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
959
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700960 jclass hdrCapabilitiesClazz = FindClassOrDie(env, "android/view/Display$HdrCapabilities");
961 gHdrCapabilitiesClassInfo.clazz = MakeGlobalRefOrDie(env, hdrCapabilitiesClazz);
962 gHdrCapabilitiesClassInfo.ctor = GetMethodIDOrDie(env, hdrCapabilitiesClazz, "<init>",
963 "([IFFF)V");
964
Robert Carr6486d312017-01-09 19:48:29 -0800965 jclass graphicsBufferClazz = FindClassOrDie(env, "android/graphics/GraphicBuffer");
966 gGraphicBufferClassInfo.clazz = MakeGlobalRefOrDie(env, graphicsBufferClazz);
967 gGraphicBufferClassInfo.builder = GetStaticMethodIDOrDie(env, graphicsBufferClazz,
968 "createFromExisting", "(IIIIJ)Landroid/graphics/GraphicBuffer;");
969
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800970 return err;
971}
972
973};