blob: ec9c8606e8df08547cbf50b1a68ebb7683303826 [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"
Robert Carr788f5742018-07-30 17:46:45 -070022#include "android_hardware_input_InputWindowHandle.h"
John Reckf29ed282015-04-07 07:32:03 -070023#include "android/graphics/Bitmap.h"
Mathias Agopian3866f0d2013-02-11 22:08:48 -080024#include "android/graphics/GraphicsJNI.h"
25#include "android/graphics/Region.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080026#include "core_jni_helpers.h"
Ben Wagner60126ef2015-08-07 12:13:48 -040027
Tom Cherry8ed74bb2017-07-10 14:31:18 -070028#include <android-base/chrono_utils.h>
Steven Moreland2279b252017-07-19 09:50:45 -070029#include <nativehelper/JNIHelp.h>
30#include <nativehelper/ScopedUtfChars.h>
Mathias Agopian0449a402013-03-01 23:01:51 -080031#include <android_runtime/android_view_Surface.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080032#include <android_runtime/android_view_SurfaceSession.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080033#include <gui/Surface.h>
34#include <gui/SurfaceComposerClient.h>
Ben Wagner60126ef2015-08-07 12:13:48 -040035#include <jni.h>
36#include <memory>
37#include <stdio.h>
Michael Wright1c9977b2016-07-12 13:30:10 -070038#include <system/graphics.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080039#include <ui/DisplayInfo.h>
Svetoslav1376d602014-03-13 11:17:26 -070040#include <ui/FrameStats.h>
Peiyong Linb88549e2018-03-28 12:03:45 -070041#include <ui/GraphicTypes.h>
Peiyong Lin371b98f2018-03-14 17:29:10 -070042#include <ui/HdrCapabilities.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080043#include <ui/Rect.h>
44#include <ui/Region.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080045#include <utils/Log.h>
46
Mathias Agopian3866f0d2013-02-11 22:08:48 -080047// ----------------------------------------------------------------------------
48
49namespace android {
50
51static const char* const OutOfResourcesException =
52 "android/view/Surface$OutOfResourcesException";
53
54static struct {
Dan Stoza00101052014-05-02 15:23:40 -070055 jclass clazz;
56 jmethodID ctor;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080057 jfieldID width;
58 jfieldID height;
59 jfieldID refreshRate;
60 jfieldID density;
61 jfieldID xDpi;
62 jfieldID yDpi;
63 jfieldID secure;
Andy McFaddene8b1aeb2014-06-13 14:05:40 -070064 jfieldID appVsyncOffsetNanos;
65 jfieldID presentationDeadlineNanos;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080066} gPhysicalDisplayInfoClassInfo;
67
Dan Stoza9890e3412014-05-22 16:12:54 -070068static struct {
69 jfieldID bottom;
70 jfieldID left;
71 jfieldID right;
72 jfieldID top;
73} gRectClassInfo;
74
Leon Scroggins46cb9bd2014-03-06 15:36:39 -050075// Implements SkMallocPixelRef::ReleaseProc, to delete the screenshot on unref.
76void DeleteScreenshot(void* addr, void* context) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -050077 delete ((ScreenshotClient*) context);
78}
Mathias Agopian3866f0d2013-02-11 22:08:48 -080079
Svetoslav1376d602014-03-13 11:17:26 -070080static struct {
81 nsecs_t UNDEFINED_TIME_NANO;
82 jmethodID init;
83} gWindowContentFrameStatsClassInfo;
84
85static struct {
86 nsecs_t UNDEFINED_TIME_NANO;
87 jmethodID init;
88} gWindowAnimationFrameStatsClassInfo;
89
Hangyu Kuang54ac2192016-04-25 13:22:02 -070090static struct {
91 jclass clazz;
92 jmethodID ctor;
93} gHdrCapabilitiesClassInfo;
94
Robert Carr6486d312017-01-09 19:48:29 -080095static struct {
96 jclass clazz;
97 jmethodID builder;
98} gGraphicBufferClassInfo;
99
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800100// ----------------------------------------------------------------------------
101
Robert Carre13b58e2017-08-31 14:50:44 -0700102static jlong nativeCreateTransaction(JNIEnv* env, jclass clazz) {
103 return reinterpret_cast<jlong>(new SurfaceComposerClient::Transaction);
104}
105
106static void releaseTransaction(SurfaceComposerClient::Transaction* t) {
107 delete t;
108}
109
110static jlong nativeGetNativeTransactionFinalizer(JNIEnv* env, jclass clazz) {
111 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&releaseTransaction));
112}
113
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000114static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500115 jstring nameStr, jint w, jint h, jint format, jint flags, jlong parentObject,
116 jint windowType, jint ownerUid) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800117 ScopedUtfChars name(env, nameStr);
118 sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));
Robert Carr838120c2016-11-01 18:31:12 -0700119 SurfaceControl *parent = reinterpret_cast<SurfaceControl*>(parentObject);
Robert Carrb0f39362018-03-14 13:52:25 -0700120 sp<SurfaceControl> surface;
121 status_t err = client->createSurfaceChecked(
122 String8(name.c_str()), w, h, format, &surface, flags, parent, windowType, ownerUid);
123 if (err == NAME_NOT_FOUND) {
124 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
125 return 0;
126 } else if (err != NO_ERROR) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800127 jniThrowException(env, OutOfResourcesException, NULL);
128 return 0;
129 }
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500130
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800131 surface->incStrong((void *)nativeCreate);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000132 return reinterpret_cast<jlong>(surface.get());
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800133}
134
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000135static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800136 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800137 ctrl->decStrong((void *)nativeCreate);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800138}
139
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000140static void nativeDestroy(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800141 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
142 ctrl->clear();
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800143 ctrl->decStrong((void *)nativeCreate);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800144}
145
Chong Zhang47e36a32016-02-29 16:44:33 -0800146static void nativeDisconnect(JNIEnv* env, jclass clazz, jlong nativeObject) {
147 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
148 if (ctrl != NULL) {
149 ctrl->disconnect();
150 }
151}
152
Robert Carr6486d312017-01-09 19:48:29 -0800153static Rect rectFromObj(JNIEnv* env, jobject rectObj) {
154 int left = env->GetIntField(rectObj, gRectClassInfo.left);
155 int top = env->GetIntField(rectObj, gRectClassInfo.top);
156 int right = env->GetIntField(rectObj, gRectClassInfo.right);
157 int bottom = env->GetIntField(rectObj, gRectClassInfo.bottom);
158 return Rect(left, top, right, bottom);
159}
160
chaviw08520a02018-09-10 16:44:56 -0700161static jobject nativeScreenshot(JNIEnv* env, jclass clazz,
Robert Carr6486d312017-01-09 19:48:29 -0800162 jobject displayTokenObj, jobject sourceCropObj, jint width, jint height,
chaviw08520a02018-09-10 16:44:56 -0700163 bool useIdentityTransform, int rotation) {
Robert Carr6486d312017-01-09 19:48:29 -0800164 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
165 if (displayToken == NULL) {
166 return NULL;
167 }
168 Rect sourceCrop = rectFromObj(env, sourceCropObj);
Robert Carr6486d312017-01-09 19:48:29 -0800169 sp<GraphicBuffer> buffer;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700170 status_t res = ScreenshotClient::capture(displayToken, ui::Dataspace::V0_SRGB,
171 ui::PixelFormat::RGBA_8888,
172 sourceCrop, width, height,
173 useIdentityTransform, rotation, &buffer);
Robert Carr6486d312017-01-09 19:48:29 -0800174 if (res != NO_ERROR) {
175 return NULL;
176 }
177
178 return env->CallStaticObjectMethod(gGraphicBufferClassInfo.clazz,
179 gGraphicBufferClassInfo.builder,
180 buffer->getWidth(),
181 buffer->getHeight(),
182 buffer->getPixelFormat(),
Mathias Agopian113fd302017-05-25 18:31:04 -0700183 (jint)buffer->getUsage(),
Patrik Torstensson511a8082017-03-27 15:04:11 +0100184 (jlong)buffer.get());
Robert Carr6486d312017-01-09 19:48:29 -0800185}
186
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000187static jobject nativeCaptureLayers(JNIEnv* env, jclass clazz, jobject layerHandleToken,
chaviwfbe47df2017-11-10 16:14:49 -0800188 jobject sourceCropObj, jfloat frameScale) {
189
190 sp<IBinder> layerHandle = ibinderForJavaObject(env, layerHandleToken);
191 if (layerHandle == NULL) {
192 return NULL;
193 }
194
195 Rect sourceCrop;
196 if (sourceCropObj != NULL) {
197 sourceCrop = rectFromObj(env, sourceCropObj);
198 }
199
200 sp<GraphicBuffer> buffer;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700201 status_t res = ScreenshotClient::captureChildLayers(layerHandle, ui::Dataspace::V0_SRGB,
202 ui::PixelFormat::RGBA_8888, sourceCrop,
203 frameScale, &buffer);
chaviwfbe47df2017-11-10 16:14:49 -0800204 if (res != NO_ERROR) {
205 return NULL;
206 }
207
208 return env->CallStaticObjectMethod(gGraphicBufferClassInfo.clazz,
209 gGraphicBufferClassInfo.builder,
210 buffer->getWidth(),
211 buffer->getHeight(),
212 buffer->getPixelFormat(),
213 (jint)buffer->getUsage(),
214 (jlong)buffer.get());
chaviw1cda84c2017-10-23 16:47:10 -0700215}
216
Robert Carre13b58e2017-08-31 14:50:44 -0700217static void nativeApplyTransaction(JNIEnv* env, jclass clazz, jlong transactionObj, jboolean sync) {
218 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
219 transaction->apply(sync);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800220}
221
Robert Carrb1579c82017-09-05 14:54:47 -0700222static void nativeMergeTransaction(JNIEnv* env, jclass clazz,
223 jlong transactionObj, jlong otherTransactionObj) {
224 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
225 auto otherTransaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(
226 otherTransactionObj);
227 transaction->merge(std::move(*otherTransaction));
228}
229
Robert Carre13b58e2017-08-31 14:50:44 -0700230static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz, jlong transactionObj) {
231 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
232 transaction->setAnimationTransaction();
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800233}
234
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100235static void nativeSetEarlyWakeup(JNIEnv* env, jclass clazz, jlong transactionObj) {
236 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
237 transaction->setEarlyWakeup();
238}
239
Robert Carre13b58e2017-08-31 14:50:44 -0700240static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong transactionObj,
241 jlong nativeObject, jint zorder) {
242 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800243
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800244 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700245 transaction->setLayer(ctrl, zorder);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800246}
247
Robert Carre13b58e2017-08-31 14:50:44 -0700248static void nativeSetRelativeLayer(JNIEnv* env, jclass clazz, jlong transactionObj,
249 jlong nativeObject,
Robert Carraf422a82017-04-10 18:34:33 -0700250 jobject relativeTo, jint zorder) {
Robert Carre13b58e2017-08-31 14:50:44 -0700251
Robert Carraf422a82017-04-10 18:34:33 -0700252 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
253 sp<IBinder> handle = ibinderForJavaObject(env, relativeTo);
254
Robert Carre13b58e2017-08-31 14:50:44 -0700255 {
256 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
257 transaction->setRelativeLayer(ctrl, handle, zorder);
258 }
Robert Carraf422a82017-04-10 18:34:33 -0700259}
260
Robert Carre13b58e2017-08-31 14:50:44 -0700261static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong transactionObj,
262 jlong nativeObject, jfloat x, jfloat y) {
263 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
264
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800265 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700266 transaction->setPosition(ctrl, x, y);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800267}
268
Robert Carr6da3cc02016-06-16 15:17:07 -0700269static void nativeSetGeometryAppliesWithResize(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700270jlong transactionObj,
Robert Carra9408d42016-06-03 13:28:48 -0700271 jlong nativeObject) {
Robert Carre13b58e2017-08-31 14:50:44 -0700272 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
273
Robert Carra9408d42016-06-03 13:28:48 -0700274 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700275 transaction->setGeometryAppliesWithResize(ctrl);
Robert Carra9408d42016-06-03 13:28:48 -0700276}
277
Robert Carre13b58e2017-08-31 14:50:44 -0700278static void nativeSetSize(JNIEnv* env, jclass clazz, jlong transactionObj,
279 jlong nativeObject, jint w, jint h) {
280 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
281
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800282 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700283 transaction->setSize(ctrl, w, h);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800284}
285
Robert Carre13b58e2017-08-31 14:50:44 -0700286static void nativeSetFlags(JNIEnv* env, jclass clazz, jlong transactionObj,
287 jlong nativeObject, jint flags, jint mask) {
288 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
289
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800290 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700291 transaction->setFlags(ctrl, flags, mask);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800292}
293
Robert Carre13b58e2017-08-31 14:50:44 -0700294static void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jlong transactionObj,
295 jlong nativeObject, jobject regionObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800296 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
297 SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj);
298 if (!region) {
299 doThrowIAE(env);
300 return;
301 }
302
303 const SkIRect& b(region->getBounds());
304 Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
305 if (region->isComplex()) {
306 SkRegion::Iterator it(*region);
307 while (!it.done()) {
308 const SkIRect& r(it.rect());
309 reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
310 it.next();
311 }
312 }
313
Robert Carre13b58e2017-08-31 14:50:44 -0700314 {
315 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
316 transaction->setTransparentRegionHint(ctrl, reg);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800317 }
318}
319
Robert Carre13b58e2017-08-31 14:50:44 -0700320static void nativeSetAlpha(JNIEnv* env, jclass clazz, jlong transactionObj,
321 jlong nativeObject, jfloat alpha) {
322 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
323
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800324 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700325 transaction->setAlpha(ctrl, alpha);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800326}
327
Robert Carr788f5742018-07-30 17:46:45 -0700328static void nativeSetInputWindowInfo(JNIEnv* env, jclass clazz, jlong transactionObj,
329 jlong nativeObject, jobject inputWindow) {
330 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
331
332 sp<NativeInputWindowHandle> handle = android_server_InputWindowHandle_getHandle(
333 env, inputWindow);
334 handle->updateInfo();
335
336 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
337 transaction->setInputWindowInfo(ctrl, *handle->getInfo());
338}
339
Robert Carre13b58e2017-08-31 14:50:44 -0700340static void nativeSetColor(JNIEnv* env, jclass clazz, jlong transactionObj,
341 jlong nativeObject, jfloatArray fColor) {
342 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
chaviw0dd03f52017-08-25 12:15:26 -0700343 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700344
chaviw0dd03f52017-08-25 12:15:26 -0700345 float* floatColors = env->GetFloatArrayElements(fColor, 0);
346 half3 color(floatColors[0], floatColors[1], floatColors[2]);
Robert Carre13b58e2017-08-31 14:50:44 -0700347 transaction->setColor(ctrl, color);
chaviw0dd03f52017-08-25 12:15:26 -0700348}
349
Robert Carre13b58e2017-08-31 14:50:44 -0700350static void nativeSetMatrix(JNIEnv* env, jclass clazz, jlong transactionObj,
351 jlong nativeObject,
Robert Carr0edf18f2017-02-21 20:01:47 -0800352 jfloat dsdx, jfloat dtdx, jfloat dtdy, jfloat dsdy) {
Robert Carre13b58e2017-08-31 14:50:44 -0700353 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
354
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800355 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700356 transaction->setMatrix(ctrl, dsdx, dtdx, dtdy, dsdy);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800357}
358
Peiyong Lin52bb6b42018-10-01 11:40:50 -0700359static void nativeSetColorTransform(JNIEnv* env, jclass clazz, jlong transactionObj,
360 jlong nativeObject, jfloatArray fMatrix, jfloatArray fTranslation) {
361 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
362 SurfaceControl* const surfaceControl = reinterpret_cast<SurfaceControl*>(nativeObject);
363 float* floatMatrix = env->GetFloatArrayElements(fMatrix, 0);
364 mat3 matrix(static_cast<float const*>(floatMatrix));
365 float* floatTranslation = env->GetFloatArrayElements(fTranslation, 0);
366 vec3 translation(floatTranslation[0], floatTranslation[1], floatTranslation[2]);
367 transaction->setColorTransform(surfaceControl, matrix, translation);
368}
369
Robert Carre13b58e2017-08-31 14:50:44 -0700370static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong transactionObj,
371 jlong nativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800372 jint l, jint t, jint r, jint b) {
Robert Carre13b58e2017-08-31 14:50:44 -0700373 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
374
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800375 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
376 Rect crop(l, t, r, b);
Marissa Wallcb32fdd2018-07-24 09:53:30 -0700377 transaction->setCrop_legacy(ctrl, crop);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800378}
379
Robert Carre13b58e2017-08-31 14:50:44 -0700380static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong transactionObj,
381 jlong nativeObject, jint layerStack) {
382 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
383
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800384 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700385 transaction->setLayerStack(ctrl, layerStack);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800386}
387
388static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) {
389 sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id));
390 return javaObjectForIBinder(env, token);
391}
392
393static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj,
394 jboolean secure) {
395 ScopedUtfChars name(env, nameObj);
396 sp<IBinder> token(SurfaceComposerClient::createDisplay(
397 String8(name.c_str()), bool(secure)));
398 return javaObjectForIBinder(env, token);
399}
400
Jesse Hall6a6bc212013-08-08 12:15:03 -0700401static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
402 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
403 if (token == NULL) return;
404 SurfaceComposerClient::destroyDisplay(token);
405}
406
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800407static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700408 jlong transactionObj,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000409 jobject tokenObj, jlong nativeSurfaceObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800410 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
411 if (token == NULL) return;
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800412 sp<IGraphicBufferProducer> bufferProducer;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800413 sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800414 if (sur != NULL) {
415 bufferProducer = sur->getIGraphicBufferProducer();
416 }
Robert Carre13b58e2017-08-31 14:50:44 -0700417
418
419 status_t err = NO_ERROR;
420 {
421 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
422 err = transaction->setDisplaySurface(token,
423 bufferProducer);
424 }
Pablo Ceballosaff2f942016-07-29 14:49:55 -0700425 if (err != NO_ERROR) {
426 doThrowIAE(env, "Illegal Surface, could not enable async mode. Was this"
427 " Surface created with singleBufferMode?");
428 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800429}
430
431static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700432 jlong transactionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800433 jobject tokenObj, jint layerStack) {
Robert Carre13b58e2017-08-31 14:50:44 -0700434
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800435 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
436 if (token == NULL) return;
437
Robert Carre13b58e2017-08-31 14:50:44 -0700438 {
439 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
440 transaction->setDisplayLayerStack(token, layerStack);
441 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800442}
443
444static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700445 jlong transactionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800446 jobject tokenObj, jint orientation,
447 jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom,
448 jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) {
449 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
450 if (token == NULL) return;
451 Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom);
452 Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom);
Robert Carre13b58e2017-08-31 14:50:44 -0700453
454 {
455 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
456 transaction->setDisplayProjection(token, orientation, layerStackRect, displayRect);
457 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800458}
459
Michael Wright01e840f2014-06-26 16:03:25 -0700460static void nativeSetDisplaySize(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700461 jlong transactionObj,
Michael Wright01e840f2014-06-26 16:03:25 -0700462 jobject tokenObj, jint width, jint height) {
463 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
464 if (token == NULL) return;
Robert Carre13b58e2017-08-31 14:50:44 -0700465
466 {
467 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
468 transaction->setDisplaySize(token, width, height);
469 }
Michael Wright01e840f2014-06-26 16:03:25 -0700470}
471
Dan Stoza00101052014-05-02 15:23:40 -0700472static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
473 jobject tokenObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800474 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
Dan Stoza00101052014-05-02 15:23:40 -0700475 if (token == NULL) return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800476
Dan Stoza00101052014-05-02 15:23:40 -0700477 Vector<DisplayInfo> configs;
478 if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
479 configs.size() == 0) {
480 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800481 }
482
Dan Stoza00101052014-05-02 15:23:40 -0700483 jobjectArray configArray = env->NewObjectArray(configs.size(),
484 gPhysicalDisplayInfoClassInfo.clazz, NULL);
485
486 for (size_t c = 0; c < configs.size(); ++c) {
487 const DisplayInfo& info = configs[c];
488 jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
489 gPhysicalDisplayInfoClassInfo.ctor);
490 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
491 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
492 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
493 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
494 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
495 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
496 env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700497 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos,
498 info.appVsyncOffset);
499 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos,
500 info.presentationDeadline);
Dan Stoza00101052014-05-02 15:23:40 -0700501 env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
502 env->DeleteLocalRef(infoObj);
503 }
504
505 return configArray;
506}
507
508static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
509 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
510 if (token == NULL) return -1;
511 return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
512}
513
514static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
515 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
516 if (token == NULL) return JNI_FALSE;
517 status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
518 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800519}
520
Michael Wright1c9977b2016-07-12 13:30:10 -0700521static jintArray nativeGetDisplayColorModes(JNIEnv* env, jclass, jobject tokenObj) {
522 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
523 if (token == NULL) return NULL;
Peiyong Linb88549e2018-03-28 12:03:45 -0700524 Vector<ui::ColorMode> colorModes;
Michael Wright1c9977b2016-07-12 13:30:10 -0700525 if (SurfaceComposerClient::getDisplayColorModes(token, &colorModes) != NO_ERROR ||
526 colorModes.isEmpty()) {
527 return NULL;
528 }
529
530 jintArray colorModesArray = env->NewIntArray(colorModes.size());
531 if (colorModesArray == NULL) {
532 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
533 return NULL;
534 }
535 jint* colorModesArrayValues = env->GetIntArrayElements(colorModesArray, 0);
536 for (size_t i = 0; i < colorModes.size(); i++) {
537 colorModesArrayValues[i] = static_cast<jint>(colorModes[i]);
538 }
539 env->ReleaseIntArrayElements(colorModesArray, colorModesArrayValues, 0);
540 return colorModesArray;
541}
542
543static jint nativeGetActiveColorMode(JNIEnv* env, jclass, jobject tokenObj) {
544 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
545 if (token == NULL) return -1;
546 return static_cast<jint>(SurfaceComposerClient::getActiveColorMode(token));
547}
548
549static jboolean nativeSetActiveColorMode(JNIEnv* env, jclass,
550 jobject tokenObj, jint colorMode) {
551 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
552 if (token == NULL) return JNI_FALSE;
553 status_t err = SurfaceComposerClient::setActiveColorMode(token,
Peiyong Linb88549e2018-03-28 12:03:45 -0700554 static_cast<ui::ColorMode>(colorMode));
Michael Wright1c9977b2016-07-12 13:30:10 -0700555 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
556}
557
Prashant Malanic55929a2014-05-25 01:59:21 -0700558static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800559 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
560 if (token == NULL) return;
561
Tom Cherry8ed74bb2017-07-10 14:31:18 -0700562 android::base::Timer t;
Prashant Malanic55929a2014-05-25 01:59:21 -0700563 SurfaceComposerClient::setDisplayPowerMode(token, mode);
Tom Cherry8ed74bb2017-07-10 14:31:18 -0700564 if (t.duration() > 100ms) ALOGD("Excessive delay in setPowerMode()");
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800565}
566
Svetoslav1376d602014-03-13 11:17:26 -0700567static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
568 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
569 status_t err = ctrl->clearLayerFrameStats();
570
571 if (err < 0 && err != NO_INIT) {
572 doThrowIAE(env);
573 }
574
575 // The other end is not ready, just report we failed.
576 if (err == NO_INIT) {
577 return JNI_FALSE;
578 }
579
580 return JNI_TRUE;
581}
582
583static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
584 jobject outStats) {
585 FrameStats stats;
586
587 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
588 status_t err = ctrl->getLayerFrameStats(&stats);
589 if (err < 0 && err != NO_INIT) {
590 doThrowIAE(env);
591 }
592
593 // The other end is not ready, fine just return empty stats.
594 if (err == NO_INIT) {
595 return JNI_FALSE;
596 }
597
598 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
599 size_t frameCount = stats.desiredPresentTimesNano.size();
600
601 jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
602 if (postedTimesNanoDst == NULL) {
603 return JNI_FALSE;
604 }
605
606 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
607 if (presentedTimesNanoDst == NULL) {
608 return JNI_FALSE;
609 }
610
611 jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
612 if (readyTimesNanoDst == NULL) {
613 return JNI_FALSE;
614 }
615
616 nsecs_t postedTimesNanoSrc[frameCount];
617 nsecs_t presentedTimesNanoSrc[frameCount];
618 nsecs_t readyTimesNanoSrc[frameCount];
619
620 for (size_t i = 0; i < frameCount; i++) {
621 nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
622 if (postedTimeNano == INT64_MAX) {
623 postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
624 }
625 postedTimesNanoSrc[i] = postedTimeNano;
626
627 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
628 if (presentedTimeNano == INT64_MAX) {
629 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
630 }
631 presentedTimesNanoSrc[i] = presentedTimeNano;
632
633 nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
634 if (readyTimeNano == INT64_MAX) {
635 readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
636 }
637 readyTimesNanoSrc[i] = readyTimeNano;
638 }
639
640 env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
641 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
642 env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
643
644 env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
645 postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
646
647 if (env->ExceptionCheck()) {
648 return JNI_FALSE;
649 }
650
651 return JNI_TRUE;
652}
653
654static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
655 status_t err = SurfaceComposerClient::clearAnimationFrameStats();
656
657 if (err < 0 && err != NO_INIT) {
658 doThrowIAE(env);
659 }
660
661 // The other end is not ready, just report we failed.
662 if (err == NO_INIT) {
663 return JNI_FALSE;
664 }
665
666 return JNI_TRUE;
667}
668
669static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
670 FrameStats stats;
671
672 status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
673 if (err < 0 && err != NO_INIT) {
674 doThrowIAE(env);
675 }
676
677 // The other end is not ready, fine just return empty stats.
678 if (err == NO_INIT) {
679 return JNI_FALSE;
680 }
681
682 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
683 size_t frameCount = stats.desiredPresentTimesNano.size();
684
685 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
686 if (presentedTimesNanoDst == NULL) {
687 return JNI_FALSE;
688 }
689
690 nsecs_t presentedTimesNanoSrc[frameCount];
691
692 for (size_t i = 0; i < frameCount; i++) {
Allen Hairac5eda32014-04-24 11:50:37 -0700693 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
Svetoslav1376d602014-03-13 11:17:26 -0700694 if (presentedTimeNano == INT64_MAX) {
695 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
696 }
697 presentedTimesNanoSrc[i] = presentedTimeNano;
698 }
699
700 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
701
702 env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
703 presentedTimesNanoDst);
704
705 if (env->ExceptionCheck()) {
706 return JNI_FALSE;
707 }
708
709 return JNI_TRUE;
710}
711
Robert Carre13b58e2017-08-31 14:50:44 -0700712static void nativeDeferTransactionUntil(JNIEnv* env, jclass clazz, jlong transactionObj,
713 jlong nativeObject,
Rob Carr64e516f2015-10-29 00:20:45 +0000714 jobject handleObject, jlong frameNumber) {
715 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
716 sp<IBinder> handle = ibinderForJavaObject(env, handleObject);
717
Robert Carre13b58e2017-08-31 14:50:44 -0700718 {
719 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
Marissa Wallcb32fdd2018-07-24 09:53:30 -0700720 transaction->deferTransactionUntil_legacy(ctrl, handle, frameNumber);
Robert Carre13b58e2017-08-31 14:50:44 -0700721 }
Rob Carr64e516f2015-10-29 00:20:45 +0000722}
723
Robert Carre13b58e2017-08-31 14:50:44 -0700724static void nativeDeferTransactionUntilSurface(JNIEnv* env, jclass clazz, jlong transactionObj,
725 jlong nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800726 jlong surfaceObject, jlong frameNumber) {
Robert Carre13b58e2017-08-31 14:50:44 -0700727 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
728
Robert Carrd5c7dd62017-03-08 10:39:30 -0800729 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
730 sp<Surface> barrier = reinterpret_cast<Surface *>(surfaceObject);
731
Marissa Wallcb32fdd2018-07-24 09:53:30 -0700732 transaction->deferTransactionUntil_legacy(ctrl, barrier, frameNumber);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800733}
734
Robert Carre13b58e2017-08-31 14:50:44 -0700735static void nativeReparentChildren(JNIEnv* env, jclass clazz, jlong transactionObj,
736 jlong nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800737 jobject newParentObject) {
Robert Carre13b58e2017-08-31 14:50:44 -0700738
Robert Carrd5c7dd62017-03-08 10:39:30 -0800739 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
740 sp<IBinder> handle = ibinderForJavaObject(env, newParentObject);
741
Robert Carre13b58e2017-08-31 14:50:44 -0700742 {
743 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
744 transaction->reparentChildren(ctrl, handle);
745 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800746}
747
Robert Carre13b58e2017-08-31 14:50:44 -0700748static void nativeReparent(JNIEnv* env, jclass clazz, jlong transactionObj,
749 jlong nativeObject,
chaviw76431402017-09-18 16:50:05 -0700750 jobject newParentObject) {
chaviw63542382017-08-17 17:39:29 -0700751 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
752 sp<IBinder> parentHandle = ibinderForJavaObject(env, newParentObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700753
754 {
755 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
756 transaction->reparent(ctrl, parentHandle);
757 }
chaviw63542382017-08-17 17:39:29 -0700758}
759
Robert Carre13b58e2017-08-31 14:50:44 -0700760static void nativeSeverChildren(JNIEnv* env, jclass clazz, jlong transactionObj,
761 jlong nativeObject) {
762 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
763
Robert Carrd5c7dd62017-03-08 10:39:30 -0800764 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700765 transaction->detachChildren(ctrl);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800766}
767
Robert Carre13b58e2017-08-31 14:50:44 -0700768static void nativeSetOverrideScalingMode(JNIEnv* env, jclass clazz, jlong transactionObj,
769 jlong nativeObject,
Robert Carr1ca6a332016-04-11 18:00:43 -0700770 jint scalingMode) {
Robert Carre13b58e2017-08-31 14:50:44 -0700771 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
Robert Carr1ca6a332016-04-11 18:00:43 -0700772
Robert Carre13b58e2017-08-31 14:50:44 -0700773 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
774 transaction->setOverrideScalingMode(ctrl, scalingMode);
Robert Carr1ca6a332016-04-11 18:00:43 -0700775}
776
Chavi Weingartenb736e322018-02-23 00:27:54 +0000777static void nativeDestroyInTransaction(JNIEnv* env, jclass clazz,
778 jlong transactionObj,
779 jlong nativeObject) {
780 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
781 auto ctrl = reinterpret_cast<SurfaceControl*>(nativeObject);
782 transaction->destroySurface(ctrl);
783}
784
Rob Carr64e516f2015-10-29 00:20:45 +0000785static jobject nativeGetHandle(JNIEnv* env, jclass clazz, jlong nativeObject) {
786 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Rob Carr64e516f2015-10-29 00:20:45 +0000787 return javaObjectForIBinder(env, ctrl->getHandle());
788}
789
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700790static jobject nativeGetHdrCapabilities(JNIEnv* env, jclass clazz, jobject tokenObject) {
791 sp<IBinder> token(ibinderForJavaObject(env, tokenObject));
792 if (token == NULL) return NULL;
793
794 HdrCapabilities capabilities;
795 SurfaceComposerClient::getHdrCapabilities(token, &capabilities);
796
797 const auto& types = capabilities.getSupportedHdrTypes();
Peiyong Lin3a0c6e12018-04-16 11:05:29 -0700798 std::vector<int32_t> intTypes;
799 for (auto type : types) {
800 intTypes.push_back(static_cast<int32_t>(type));
801 }
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700802 auto typesArray = env->NewIntArray(types.size());
Peiyong Lin3a0c6e12018-04-16 11:05:29 -0700803 env->SetIntArrayRegion(typesArray, 0, intTypes.size(), intTypes.data());
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700804
Michael Wright9ff94c02016-03-30 18:05:40 -0700805 return env->NewObject(gHdrCapabilitiesClassInfo.clazz, gHdrCapabilitiesClassInfo.ctor,
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700806 typesArray, capabilities.getDesiredMaxLuminance(),
807 capabilities.getDesiredMaxAverageLuminance(), capabilities.getDesiredMinLuminance());
808}
809
Jorim Jaggi06975df2017-12-01 14:52:13 +0100810static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz, jobject parcelObj) {
811 Parcel* parcel = parcelForJavaObject(env, parcelObj);
812 if (parcel == NULL) {
813 doThrowNPE(env);
814 return 0;
815 }
816 sp<SurfaceControl> surface = SurfaceControl::readFromParcel(parcel);
817 if (surface == nullptr) {
818 return 0;
819 }
820 surface->incStrong((void *)nativeCreate);
821 return reinterpret_cast<jlong>(surface.get());
822}
823
824static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
825 jlong nativeObject, jobject parcelObj) {
826 Parcel* parcel = parcelForJavaObject(env, parcelObj);
827 if (parcel == NULL) {
828 doThrowNPE(env);
829 return;
830 }
831 SurfaceControl* const self = reinterpret_cast<SurfaceControl *>(nativeObject);
832 self->writeToParcel(parcel);
833}
834
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800835// ----------------------------------------------------------------------------
836
Daniel Micay76f6a862015-09-19 17:31:01 -0400837static const JNINativeMethod sSurfaceControlMethods[] = {
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500838 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIIIJII)J",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800839 (void*)nativeCreate },
Jorim Jaggi06975df2017-12-01 14:52:13 +0100840 {"nativeReadFromParcel", "(Landroid/os/Parcel;)J",
841 (void*)nativeReadFromParcel },
842 {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
843 (void*)nativeWriteToParcel },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000844 {"nativeRelease", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800845 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000846 {"nativeDestroy", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800847 (void*)nativeDestroy },
Chong Zhang47e36a32016-02-29 16:44:33 -0800848 {"nativeDisconnect", "(J)V",
849 (void*)nativeDisconnect },
Robert Carre13b58e2017-08-31 14:50:44 -0700850 {"nativeCreateTransaction", "()J",
851 (void*)nativeCreateTransaction },
852 {"nativeApplyTransaction", "(JZ)V",
853 (void*)nativeApplyTransaction },
854 {"nativeGetNativeTransactionFinalizer", "()J",
855 (void*)nativeGetNativeTransactionFinalizer },
Robert Carrb1579c82017-09-05 14:54:47 -0700856 {"nativeMergeTransaction", "(JJ)V",
857 (void*)nativeMergeTransaction },
Robert Carre13b58e2017-08-31 14:50:44 -0700858 {"nativeSetAnimationTransaction", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800859 (void*)nativeSetAnimationTransaction },
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100860 {"nativeSetEarlyWakeup", "(J)V",
861 (void*)nativeSetEarlyWakeup },
Robert Carre13b58e2017-08-31 14:50:44 -0700862 {"nativeSetLayer", "(JJI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800863 (void*)nativeSetLayer },
Robert Carre13b58e2017-08-31 14:50:44 -0700864 {"nativeSetRelativeLayer", "(JJLandroid/os/IBinder;I)V",
Robert Carraf422a82017-04-10 18:34:33 -0700865 (void*)nativeSetRelativeLayer },
Robert Carre13b58e2017-08-31 14:50:44 -0700866 {"nativeSetPosition", "(JJFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800867 (void*)nativeSetPosition },
Robert Carre13b58e2017-08-31 14:50:44 -0700868 {"nativeSetGeometryAppliesWithResize", "(JJ)V",
Robert Carr6da3cc02016-06-16 15:17:07 -0700869 (void*)nativeSetGeometryAppliesWithResize },
Robert Carre13b58e2017-08-31 14:50:44 -0700870 {"nativeSetSize", "(JJII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800871 (void*)nativeSetSize },
Robert Carre13b58e2017-08-31 14:50:44 -0700872 {"nativeSetTransparentRegionHint", "(JJLandroid/graphics/Region;)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800873 (void*)nativeSetTransparentRegionHint },
Robert Carre13b58e2017-08-31 14:50:44 -0700874 {"nativeSetAlpha", "(JJF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800875 (void*)nativeSetAlpha },
Robert Carre13b58e2017-08-31 14:50:44 -0700876 {"nativeSetColor", "(JJ[F)V",
chaviw0dd03f52017-08-25 12:15:26 -0700877 (void*)nativeSetColor },
Robert Carre13b58e2017-08-31 14:50:44 -0700878 {"nativeSetMatrix", "(JJFFFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800879 (void*)nativeSetMatrix },
Peiyong Lin52bb6b42018-10-01 11:40:50 -0700880 {"nativeSetColorTransform", "(JJ[F[F)V",
881 (void*)nativeSetColorTransform },
Robert Carre13b58e2017-08-31 14:50:44 -0700882 {"nativeSetFlags", "(JJII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800883 (void*)nativeSetFlags },
Robert Carre13b58e2017-08-31 14:50:44 -0700884 {"nativeSetWindowCrop", "(JJIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800885 (void*)nativeSetWindowCrop },
Robert Carre13b58e2017-08-31 14:50:44 -0700886 {"nativeSetLayerStack", "(JJI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800887 (void*)nativeSetLayerStack },
888 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
889 (void*)nativeGetBuiltInDisplay },
890 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
891 (void*)nativeCreateDisplay },
Jesse Hall6a6bc212013-08-08 12:15:03 -0700892 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
893 (void*)nativeDestroyDisplay },
Robert Carre13b58e2017-08-31 14:50:44 -0700894 {"nativeSetDisplaySurface", "(JLandroid/os/IBinder;J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800895 (void*)nativeSetDisplaySurface },
Robert Carre13b58e2017-08-31 14:50:44 -0700896 {"nativeSetDisplayLayerStack", "(JLandroid/os/IBinder;I)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800897 (void*)nativeSetDisplayLayerStack },
Robert Carre13b58e2017-08-31 14:50:44 -0700898 {"nativeSetDisplayProjection", "(JLandroid/os/IBinder;IIIIIIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800899 (void*)nativeSetDisplayProjection },
Robert Carre13b58e2017-08-31 14:50:44 -0700900 {"nativeSetDisplaySize", "(JLandroid/os/IBinder;II)V",
Michael Wright01e840f2014-06-26 16:03:25 -0700901 (void*)nativeSetDisplaySize },
Dan Stoza00101052014-05-02 15:23:40 -0700902 {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
903 (void*)nativeGetDisplayConfigs },
904 {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
905 (void*)nativeGetActiveConfig },
906 {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
907 (void*)nativeSetActiveConfig },
Michael Wright1c9977b2016-07-12 13:30:10 -0700908 {"nativeGetDisplayColorModes", "(Landroid/os/IBinder;)[I",
909 (void*)nativeGetDisplayColorModes},
910 {"nativeGetActiveColorMode", "(Landroid/os/IBinder;)I",
911 (void*)nativeGetActiveColorMode},
912 {"nativeSetActiveColorMode", "(Landroid/os/IBinder;I)Z",
913 (void*)nativeSetActiveColorMode},
Michael Wright9ff94c02016-03-30 18:05:40 -0700914 {"nativeGetHdrCapabilities", "(Landroid/os/IBinder;)Landroid/view/Display$HdrCapabilities;",
915 (void*)nativeGetHdrCapabilities },
Svetoslav1376d602014-03-13 11:17:26 -0700916 {"nativeClearContentFrameStats", "(J)Z",
917 (void*)nativeClearContentFrameStats },
918 {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
919 (void*)nativeGetContentFrameStats },
920 {"nativeClearAnimationFrameStats", "()Z",
921 (void*)nativeClearAnimationFrameStats },
922 {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
923 (void*)nativeGetAnimationFrameStats },
Prashant Malanic55929a2014-05-25 01:59:21 -0700924 {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
925 (void*)nativeSetDisplayPowerMode },
Robert Carre13b58e2017-08-31 14:50:44 -0700926 {"nativeDeferTransactionUntil", "(JJLandroid/os/IBinder;J)V",
Rob Carr64e516f2015-10-29 00:20:45 +0000927 (void*)nativeDeferTransactionUntil },
Robert Carre13b58e2017-08-31 14:50:44 -0700928 {"nativeDeferTransactionUntilSurface", "(JJJJ)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800929 (void*)nativeDeferTransactionUntilSurface },
Robert Carre13b58e2017-08-31 14:50:44 -0700930 {"nativeReparentChildren", "(JJLandroid/os/IBinder;)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800931 (void*)nativeReparentChildren } ,
Robert Carre13b58e2017-08-31 14:50:44 -0700932 {"nativeReparent", "(JJLandroid/os/IBinder;)V",
chaviw76431402017-09-18 16:50:05 -0700933 (void*)nativeReparent },
Robert Carre13b58e2017-08-31 14:50:44 -0700934 {"nativeSeverChildren", "(JJ)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800935 (void*)nativeSeverChildren } ,
Robert Carre13b58e2017-08-31 14:50:44 -0700936 {"nativeSetOverrideScalingMode", "(JJI)V",
Robert Carr1ca6a332016-04-11 18:00:43 -0700937 (void*)nativeSetOverrideScalingMode },
Chavi Weingartenb736e322018-02-23 00:27:54 +0000938 {"nativeDestroy", "(JJ)V",
939 (void*)nativeDestroyInTransaction },
Rob Carr64e516f2015-10-29 00:20:45 +0000940 {"nativeGetHandle", "(J)Landroid/os/IBinder;",
Robert Carr6da3cc02016-06-16 15:17:07 -0700941 (void*)nativeGetHandle },
chaviw08520a02018-09-10 16:44:56 -0700942 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIZI)Landroid/graphics/GraphicBuffer;",
943 (void*)nativeScreenshot },
Chavi Weingartenea2eb5a2017-11-29 21:26:24 +0000944 {"nativeCaptureLayers", "(Landroid/os/IBinder;Landroid/graphics/Rect;F)Landroid/graphics/GraphicBuffer;",
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000945 (void*)nativeCaptureLayers },
Robert Carr788f5742018-07-30 17:46:45 -0700946 {"nativeSetInputWindowInfo", "(JJLandroid/view/InputWindowHandle;)V",
947 (void*)nativeSetInputWindowInfo },
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800948};
949
950int register_android_view_SurfaceControl(JNIEnv* env)
951{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800952 int err = RegisterMethodsOrDie(env, "android/view/SurfaceControl",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800953 sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
954
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800955 jclass clazz = FindClassOrDie(env, "android/view/SurfaceControl$PhysicalDisplayInfo");
956 gPhysicalDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
957 gPhysicalDisplayInfoClassInfo.ctor = GetMethodIDOrDie(env,
958 gPhysicalDisplayInfoClassInfo.clazz, "<init>", "()V");
959 gPhysicalDisplayInfoClassInfo.width = GetFieldIDOrDie(env, clazz, "width", "I");
960 gPhysicalDisplayInfoClassInfo.height = GetFieldIDOrDie(env, clazz, "height", "I");
961 gPhysicalDisplayInfoClassInfo.refreshRate = GetFieldIDOrDie(env, clazz, "refreshRate", "F");
962 gPhysicalDisplayInfoClassInfo.density = GetFieldIDOrDie(env, clazz, "density", "F");
963 gPhysicalDisplayInfoClassInfo.xDpi = GetFieldIDOrDie(env, clazz, "xDpi", "F");
964 gPhysicalDisplayInfoClassInfo.yDpi = GetFieldIDOrDie(env, clazz, "yDpi", "F");
965 gPhysicalDisplayInfoClassInfo.secure = GetFieldIDOrDie(env, clazz, "secure", "Z");
966 gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = GetFieldIDOrDie(env,
967 clazz, "appVsyncOffsetNanos", "J");
968 gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env,
969 clazz, "presentationDeadlineNanos", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700970
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800971 jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect");
972 gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I");
973 gRectClassInfo.left = GetFieldIDOrDie(env, rectClazz, "left", "I");
974 gRectClassInfo.right = GetFieldIDOrDie(env, rectClazz, "right", "I");
975 gRectClassInfo.top = GetFieldIDOrDie(env, rectClazz, "top", "I");
Dan Stoza9890e3412014-05-22 16:12:54 -0700976
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800977 jclass frameStatsClazz = FindClassOrDie(env, "android/view/FrameStats");
978 jfieldID undefined_time_nano_field = GetStaticFieldIDOrDie(env,
979 frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700980 nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
981
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800982 jclass contFrameStatsClazz = FindClassOrDie(env, "android/view/WindowContentFrameStats");
983 gWindowContentFrameStatsClassInfo.init = GetMethodIDOrDie(env,
984 contFrameStatsClazz, "init", "(J[J[J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700985 gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
986
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800987 jclass animFrameStatsClazz = FindClassOrDie(env, "android/view/WindowAnimationFrameStats");
988 gWindowAnimationFrameStatsClassInfo.init = GetMethodIDOrDie(env,
989 animFrameStatsClazz, "init", "(J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700990 gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
991
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700992 jclass hdrCapabilitiesClazz = FindClassOrDie(env, "android/view/Display$HdrCapabilities");
993 gHdrCapabilitiesClassInfo.clazz = MakeGlobalRefOrDie(env, hdrCapabilitiesClazz);
994 gHdrCapabilitiesClassInfo.ctor = GetMethodIDOrDie(env, hdrCapabilitiesClazz, "<init>",
995 "([IFFF)V");
996
Robert Carr6486d312017-01-09 19:48:29 -0800997 jclass graphicsBufferClazz = FindClassOrDie(env, "android/graphics/GraphicBuffer");
998 gGraphicBufferClassInfo.clazz = MakeGlobalRefOrDie(env, graphicsBufferClazz);
999 gGraphicBufferClassInfo.builder = GetStaticMethodIDOrDie(env, graphicsBufferClazz,
1000 "createFromExisting", "(IIIIJ)Landroid/graphics/GraphicBuffer;");
1001
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001002 return err;
1003}
1004
1005};