blob: ea6e0178bd9c116441d2c18862c485e169ce2fc6 [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
Lucas Dupinff9d6ab2018-10-16 18:05:50 -0700380static void nativeSetCornerRadius(JNIEnv* env, jclass clazz, jlong transactionObj,
381 jlong nativeObject, jfloat cornerRadius) {
382 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
383
384 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
385 transaction->setCornerRadius(ctrl, cornerRadius);
386}
387
Robert Carre13b58e2017-08-31 14:50:44 -0700388static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong transactionObj,
389 jlong nativeObject, jint layerStack) {
390 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
391
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800392 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700393 transaction->setLayerStack(ctrl, layerStack);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800394}
395
396static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) {
397 sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id));
398 return javaObjectForIBinder(env, token);
399}
400
401static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj,
402 jboolean secure) {
403 ScopedUtfChars name(env, nameObj);
404 sp<IBinder> token(SurfaceComposerClient::createDisplay(
405 String8(name.c_str()), bool(secure)));
406 return javaObjectForIBinder(env, token);
407}
408
Jesse Hall6a6bc212013-08-08 12:15:03 -0700409static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
410 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
411 if (token == NULL) return;
412 SurfaceComposerClient::destroyDisplay(token);
413}
414
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800415static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700416 jlong transactionObj,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000417 jobject tokenObj, jlong nativeSurfaceObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800418 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
419 if (token == NULL) return;
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800420 sp<IGraphicBufferProducer> bufferProducer;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800421 sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800422 if (sur != NULL) {
423 bufferProducer = sur->getIGraphicBufferProducer();
424 }
Robert Carre13b58e2017-08-31 14:50:44 -0700425
426
427 status_t err = NO_ERROR;
428 {
429 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
430 err = transaction->setDisplaySurface(token,
431 bufferProducer);
432 }
Pablo Ceballosaff2f942016-07-29 14:49:55 -0700433 if (err != NO_ERROR) {
434 doThrowIAE(env, "Illegal Surface, could not enable async mode. Was this"
435 " Surface created with singleBufferMode?");
436 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800437}
438
439static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700440 jlong transactionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800441 jobject tokenObj, jint layerStack) {
Robert Carre13b58e2017-08-31 14:50:44 -0700442
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800443 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
444 if (token == NULL) return;
445
Robert Carre13b58e2017-08-31 14:50:44 -0700446 {
447 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
448 transaction->setDisplayLayerStack(token, layerStack);
449 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800450}
451
452static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700453 jlong transactionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800454 jobject tokenObj, jint orientation,
455 jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom,
456 jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) {
457 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
458 if (token == NULL) return;
459 Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom);
460 Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom);
Robert Carre13b58e2017-08-31 14:50:44 -0700461
462 {
463 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
464 transaction->setDisplayProjection(token, orientation, layerStackRect, displayRect);
465 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800466}
467
Michael Wright01e840f2014-06-26 16:03:25 -0700468static void nativeSetDisplaySize(JNIEnv* env, jclass clazz,
Robert Carre13b58e2017-08-31 14:50:44 -0700469 jlong transactionObj,
Michael Wright01e840f2014-06-26 16:03:25 -0700470 jobject tokenObj, jint width, jint height) {
471 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
472 if (token == NULL) return;
Robert Carre13b58e2017-08-31 14:50:44 -0700473
474 {
475 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
476 transaction->setDisplaySize(token, width, height);
477 }
Michael Wright01e840f2014-06-26 16:03:25 -0700478}
479
Dan Stoza00101052014-05-02 15:23:40 -0700480static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
481 jobject tokenObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800482 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
Dan Stoza00101052014-05-02 15:23:40 -0700483 if (token == NULL) return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800484
Dan Stoza00101052014-05-02 15:23:40 -0700485 Vector<DisplayInfo> configs;
486 if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
487 configs.size() == 0) {
488 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800489 }
490
Dan Stoza00101052014-05-02 15:23:40 -0700491 jobjectArray configArray = env->NewObjectArray(configs.size(),
492 gPhysicalDisplayInfoClassInfo.clazz, NULL);
493
494 for (size_t c = 0; c < configs.size(); ++c) {
495 const DisplayInfo& info = configs[c];
496 jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
497 gPhysicalDisplayInfoClassInfo.ctor);
498 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
499 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
500 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
501 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
502 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
503 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
504 env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700505 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos,
506 info.appVsyncOffset);
507 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos,
508 info.presentationDeadline);
Dan Stoza00101052014-05-02 15:23:40 -0700509 env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
510 env->DeleteLocalRef(infoObj);
511 }
512
513 return configArray;
514}
515
516static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
517 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
518 if (token == NULL) return -1;
519 return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
520}
521
522static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
523 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
524 if (token == NULL) return JNI_FALSE;
525 status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
526 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800527}
528
Michael Wright1c9977b2016-07-12 13:30:10 -0700529static jintArray nativeGetDisplayColorModes(JNIEnv* env, jclass, jobject tokenObj) {
530 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
531 if (token == NULL) return NULL;
Peiyong Linb88549e2018-03-28 12:03:45 -0700532 Vector<ui::ColorMode> colorModes;
Michael Wright1c9977b2016-07-12 13:30:10 -0700533 if (SurfaceComposerClient::getDisplayColorModes(token, &colorModes) != NO_ERROR ||
534 colorModes.isEmpty()) {
535 return NULL;
536 }
537
538 jintArray colorModesArray = env->NewIntArray(colorModes.size());
539 if (colorModesArray == NULL) {
540 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
541 return NULL;
542 }
543 jint* colorModesArrayValues = env->GetIntArrayElements(colorModesArray, 0);
544 for (size_t i = 0; i < colorModes.size(); i++) {
545 colorModesArrayValues[i] = static_cast<jint>(colorModes[i]);
546 }
547 env->ReleaseIntArrayElements(colorModesArray, colorModesArrayValues, 0);
548 return colorModesArray;
549}
550
551static jint nativeGetActiveColorMode(JNIEnv* env, jclass, jobject tokenObj) {
552 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
553 if (token == NULL) return -1;
554 return static_cast<jint>(SurfaceComposerClient::getActiveColorMode(token));
555}
556
557static jboolean nativeSetActiveColorMode(JNIEnv* env, jclass,
558 jobject tokenObj, jint colorMode) {
559 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
560 if (token == NULL) return JNI_FALSE;
561 status_t err = SurfaceComposerClient::setActiveColorMode(token,
Peiyong Linb88549e2018-03-28 12:03:45 -0700562 static_cast<ui::ColorMode>(colorMode));
Michael Wright1c9977b2016-07-12 13:30:10 -0700563 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
564}
565
Prashant Malanic55929a2014-05-25 01:59:21 -0700566static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800567 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
568 if (token == NULL) return;
569
Tom Cherry8ed74bb2017-07-10 14:31:18 -0700570 android::base::Timer t;
Prashant Malanic55929a2014-05-25 01:59:21 -0700571 SurfaceComposerClient::setDisplayPowerMode(token, mode);
Tom Cherry8ed74bb2017-07-10 14:31:18 -0700572 if (t.duration() > 100ms) ALOGD("Excessive delay in setPowerMode()");
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800573}
574
Svetoslav1376d602014-03-13 11:17:26 -0700575static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
576 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
577 status_t err = ctrl->clearLayerFrameStats();
578
579 if (err < 0 && err != NO_INIT) {
580 doThrowIAE(env);
581 }
582
583 // The other end is not ready, just report we failed.
584 if (err == NO_INIT) {
585 return JNI_FALSE;
586 }
587
588 return JNI_TRUE;
589}
590
591static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
592 jobject outStats) {
593 FrameStats stats;
594
595 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
596 status_t err = ctrl->getLayerFrameStats(&stats);
597 if (err < 0 && err != NO_INIT) {
598 doThrowIAE(env);
599 }
600
601 // The other end is not ready, fine just return empty stats.
602 if (err == NO_INIT) {
603 return JNI_FALSE;
604 }
605
606 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
607 size_t frameCount = stats.desiredPresentTimesNano.size();
608
609 jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
610 if (postedTimesNanoDst == NULL) {
611 return JNI_FALSE;
612 }
613
614 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
615 if (presentedTimesNanoDst == NULL) {
616 return JNI_FALSE;
617 }
618
619 jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
620 if (readyTimesNanoDst == NULL) {
621 return JNI_FALSE;
622 }
623
624 nsecs_t postedTimesNanoSrc[frameCount];
625 nsecs_t presentedTimesNanoSrc[frameCount];
626 nsecs_t readyTimesNanoSrc[frameCount];
627
628 for (size_t i = 0; i < frameCount; i++) {
629 nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
630 if (postedTimeNano == INT64_MAX) {
631 postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
632 }
633 postedTimesNanoSrc[i] = postedTimeNano;
634
635 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
636 if (presentedTimeNano == INT64_MAX) {
637 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
638 }
639 presentedTimesNanoSrc[i] = presentedTimeNano;
640
641 nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
642 if (readyTimeNano == INT64_MAX) {
643 readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
644 }
645 readyTimesNanoSrc[i] = readyTimeNano;
646 }
647
648 env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
649 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
650 env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
651
652 env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
653 postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
654
655 if (env->ExceptionCheck()) {
656 return JNI_FALSE;
657 }
658
659 return JNI_TRUE;
660}
661
662static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
663 status_t err = SurfaceComposerClient::clearAnimationFrameStats();
664
665 if (err < 0 && err != NO_INIT) {
666 doThrowIAE(env);
667 }
668
669 // The other end is not ready, just report we failed.
670 if (err == NO_INIT) {
671 return JNI_FALSE;
672 }
673
674 return JNI_TRUE;
675}
676
677static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
678 FrameStats stats;
679
680 status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
681 if (err < 0 && err != NO_INIT) {
682 doThrowIAE(env);
683 }
684
685 // The other end is not ready, fine just return empty stats.
686 if (err == NO_INIT) {
687 return JNI_FALSE;
688 }
689
690 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
691 size_t frameCount = stats.desiredPresentTimesNano.size();
692
693 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
694 if (presentedTimesNanoDst == NULL) {
695 return JNI_FALSE;
696 }
697
698 nsecs_t presentedTimesNanoSrc[frameCount];
699
700 for (size_t i = 0; i < frameCount; i++) {
Allen Hairac5eda32014-04-24 11:50:37 -0700701 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
Svetoslav1376d602014-03-13 11:17:26 -0700702 if (presentedTimeNano == INT64_MAX) {
703 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
704 }
705 presentedTimesNanoSrc[i] = presentedTimeNano;
706 }
707
708 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
709
710 env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
711 presentedTimesNanoDst);
712
713 if (env->ExceptionCheck()) {
714 return JNI_FALSE;
715 }
716
717 return JNI_TRUE;
718}
719
Robert Carre13b58e2017-08-31 14:50:44 -0700720static void nativeDeferTransactionUntil(JNIEnv* env, jclass clazz, jlong transactionObj,
721 jlong nativeObject,
Rob Carr64e516f2015-10-29 00:20:45 +0000722 jobject handleObject, jlong frameNumber) {
723 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
724 sp<IBinder> handle = ibinderForJavaObject(env, handleObject);
725
Robert Carre13b58e2017-08-31 14:50:44 -0700726 {
727 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
Marissa Wallcb32fdd2018-07-24 09:53:30 -0700728 transaction->deferTransactionUntil_legacy(ctrl, handle, frameNumber);
Robert Carre13b58e2017-08-31 14:50:44 -0700729 }
Rob Carr64e516f2015-10-29 00:20:45 +0000730}
731
Robert Carre13b58e2017-08-31 14:50:44 -0700732static void nativeDeferTransactionUntilSurface(JNIEnv* env, jclass clazz, jlong transactionObj,
733 jlong nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800734 jlong surfaceObject, jlong frameNumber) {
Robert Carre13b58e2017-08-31 14:50:44 -0700735 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
736
Robert Carrd5c7dd62017-03-08 10:39:30 -0800737 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
738 sp<Surface> barrier = reinterpret_cast<Surface *>(surfaceObject);
739
Marissa Wallcb32fdd2018-07-24 09:53:30 -0700740 transaction->deferTransactionUntil_legacy(ctrl, barrier, frameNumber);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800741}
742
Robert Carre13b58e2017-08-31 14:50:44 -0700743static void nativeReparentChildren(JNIEnv* env, jclass clazz, jlong transactionObj,
744 jlong nativeObject,
Robert Carrd5c7dd62017-03-08 10:39:30 -0800745 jobject newParentObject) {
Robert Carre13b58e2017-08-31 14:50:44 -0700746
Robert Carrd5c7dd62017-03-08 10:39:30 -0800747 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
748 sp<IBinder> handle = ibinderForJavaObject(env, newParentObject);
749
Robert Carre13b58e2017-08-31 14:50:44 -0700750 {
751 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
752 transaction->reparentChildren(ctrl, handle);
753 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800754}
755
Robert Carre13b58e2017-08-31 14:50:44 -0700756static void nativeReparent(JNIEnv* env, jclass clazz, jlong transactionObj,
757 jlong nativeObject,
chaviw76431402017-09-18 16:50:05 -0700758 jobject newParentObject) {
chaviw63542382017-08-17 17:39:29 -0700759 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
760 sp<IBinder> parentHandle = ibinderForJavaObject(env, newParentObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700761
762 {
763 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
764 transaction->reparent(ctrl, parentHandle);
765 }
chaviw63542382017-08-17 17:39:29 -0700766}
767
Robert Carre13b58e2017-08-31 14:50:44 -0700768static void nativeSeverChildren(JNIEnv* env, jclass clazz, jlong transactionObj,
769 jlong nativeObject) {
770 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
771
Robert Carrd5c7dd62017-03-08 10:39:30 -0800772 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Robert Carre13b58e2017-08-31 14:50:44 -0700773 transaction->detachChildren(ctrl);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800774}
775
Robert Carre13b58e2017-08-31 14:50:44 -0700776static void nativeSetOverrideScalingMode(JNIEnv* env, jclass clazz, jlong transactionObj,
777 jlong nativeObject,
Robert Carr1ca6a332016-04-11 18:00:43 -0700778 jint scalingMode) {
Robert Carre13b58e2017-08-31 14:50:44 -0700779 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
Robert Carr1ca6a332016-04-11 18:00:43 -0700780
Robert Carre13b58e2017-08-31 14:50:44 -0700781 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
782 transaction->setOverrideScalingMode(ctrl, scalingMode);
Robert Carr1ca6a332016-04-11 18:00:43 -0700783}
784
Chavi Weingartenb736e322018-02-23 00:27:54 +0000785static void nativeDestroyInTransaction(JNIEnv* env, jclass clazz,
786 jlong transactionObj,
787 jlong nativeObject) {
788 auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
789 auto ctrl = reinterpret_cast<SurfaceControl*>(nativeObject);
790 transaction->destroySurface(ctrl);
791}
792
Rob Carr64e516f2015-10-29 00:20:45 +0000793static jobject nativeGetHandle(JNIEnv* env, jclass clazz, jlong nativeObject) {
794 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
Rob Carr64e516f2015-10-29 00:20:45 +0000795 return javaObjectForIBinder(env, ctrl->getHandle());
796}
797
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700798static jobject nativeGetHdrCapabilities(JNIEnv* env, jclass clazz, jobject tokenObject) {
799 sp<IBinder> token(ibinderForJavaObject(env, tokenObject));
800 if (token == NULL) return NULL;
801
802 HdrCapabilities capabilities;
803 SurfaceComposerClient::getHdrCapabilities(token, &capabilities);
804
805 const auto& types = capabilities.getSupportedHdrTypes();
Peiyong Lin3a0c6e12018-04-16 11:05:29 -0700806 std::vector<int32_t> intTypes;
807 for (auto type : types) {
808 intTypes.push_back(static_cast<int32_t>(type));
809 }
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700810 auto typesArray = env->NewIntArray(types.size());
Peiyong Lin3a0c6e12018-04-16 11:05:29 -0700811 env->SetIntArrayRegion(typesArray, 0, intTypes.size(), intTypes.data());
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700812
Michael Wright9ff94c02016-03-30 18:05:40 -0700813 return env->NewObject(gHdrCapabilitiesClassInfo.clazz, gHdrCapabilitiesClassInfo.ctor,
Hangyu Kuang54ac2192016-04-25 13:22:02 -0700814 typesArray, capabilities.getDesiredMaxLuminance(),
815 capabilities.getDesiredMaxAverageLuminance(), capabilities.getDesiredMinLuminance());
816}
817
Jorim Jaggi06975df2017-12-01 14:52:13 +0100818static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz, jobject parcelObj) {
819 Parcel* parcel = parcelForJavaObject(env, parcelObj);
820 if (parcel == NULL) {
821 doThrowNPE(env);
822 return 0;
823 }
824 sp<SurfaceControl> surface = SurfaceControl::readFromParcel(parcel);
825 if (surface == nullptr) {
826 return 0;
827 }
828 surface->incStrong((void *)nativeCreate);
829 return reinterpret_cast<jlong>(surface.get());
830}
831
832static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
833 jlong nativeObject, jobject parcelObj) {
834 Parcel* parcel = parcelForJavaObject(env, parcelObj);
835 if (parcel == NULL) {
836 doThrowNPE(env);
837 return;
838 }
839 SurfaceControl* const self = reinterpret_cast<SurfaceControl *>(nativeObject);
840 self->writeToParcel(parcel);
841}
842
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800843// ----------------------------------------------------------------------------
844
Daniel Micay76f6a862015-09-19 17:31:01 -0400845static const JNINativeMethod sSurfaceControlMethods[] = {
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500846 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIIIJII)J",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800847 (void*)nativeCreate },
Jorim Jaggi06975df2017-12-01 14:52:13 +0100848 {"nativeReadFromParcel", "(Landroid/os/Parcel;)J",
849 (void*)nativeReadFromParcel },
850 {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
851 (void*)nativeWriteToParcel },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000852 {"nativeRelease", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800853 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000854 {"nativeDestroy", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800855 (void*)nativeDestroy },
Chong Zhang47e36a32016-02-29 16:44:33 -0800856 {"nativeDisconnect", "(J)V",
857 (void*)nativeDisconnect },
Robert Carre13b58e2017-08-31 14:50:44 -0700858 {"nativeCreateTransaction", "()J",
859 (void*)nativeCreateTransaction },
860 {"nativeApplyTransaction", "(JZ)V",
861 (void*)nativeApplyTransaction },
862 {"nativeGetNativeTransactionFinalizer", "()J",
863 (void*)nativeGetNativeTransactionFinalizer },
Robert Carrb1579c82017-09-05 14:54:47 -0700864 {"nativeMergeTransaction", "(JJ)V",
865 (void*)nativeMergeTransaction },
Robert Carre13b58e2017-08-31 14:50:44 -0700866 {"nativeSetAnimationTransaction", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800867 (void*)nativeSetAnimationTransaction },
Jorim Jaggiaa763cd2018-03-22 23:20:36 +0100868 {"nativeSetEarlyWakeup", "(J)V",
869 (void*)nativeSetEarlyWakeup },
Robert Carre13b58e2017-08-31 14:50:44 -0700870 {"nativeSetLayer", "(JJI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800871 (void*)nativeSetLayer },
Robert Carre13b58e2017-08-31 14:50:44 -0700872 {"nativeSetRelativeLayer", "(JJLandroid/os/IBinder;I)V",
Robert Carraf422a82017-04-10 18:34:33 -0700873 (void*)nativeSetRelativeLayer },
Robert Carre13b58e2017-08-31 14:50:44 -0700874 {"nativeSetPosition", "(JJFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800875 (void*)nativeSetPosition },
Robert Carre13b58e2017-08-31 14:50:44 -0700876 {"nativeSetGeometryAppliesWithResize", "(JJ)V",
Robert Carr6da3cc02016-06-16 15:17:07 -0700877 (void*)nativeSetGeometryAppliesWithResize },
Robert Carre13b58e2017-08-31 14:50:44 -0700878 {"nativeSetSize", "(JJII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800879 (void*)nativeSetSize },
Robert Carre13b58e2017-08-31 14:50:44 -0700880 {"nativeSetTransparentRegionHint", "(JJLandroid/graphics/Region;)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800881 (void*)nativeSetTransparentRegionHint },
Robert Carre13b58e2017-08-31 14:50:44 -0700882 {"nativeSetAlpha", "(JJF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800883 (void*)nativeSetAlpha },
Robert Carre13b58e2017-08-31 14:50:44 -0700884 {"nativeSetColor", "(JJ[F)V",
chaviw0dd03f52017-08-25 12:15:26 -0700885 (void*)nativeSetColor },
Robert Carre13b58e2017-08-31 14:50:44 -0700886 {"nativeSetMatrix", "(JJFFFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800887 (void*)nativeSetMatrix },
Peiyong Lin52bb6b42018-10-01 11:40:50 -0700888 {"nativeSetColorTransform", "(JJ[F[F)V",
889 (void*)nativeSetColorTransform },
Robert Carre13b58e2017-08-31 14:50:44 -0700890 {"nativeSetFlags", "(JJII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800891 (void*)nativeSetFlags },
Robert Carre13b58e2017-08-31 14:50:44 -0700892 {"nativeSetWindowCrop", "(JJIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800893 (void*)nativeSetWindowCrop },
Lucas Dupinff9d6ab2018-10-16 18:05:50 -0700894 {"nativeSetCornerRadius", "(JJF)V",
895 (void*)nativeSetCornerRadius },
Robert Carre13b58e2017-08-31 14:50:44 -0700896 {"nativeSetLayerStack", "(JJI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800897 (void*)nativeSetLayerStack },
898 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
899 (void*)nativeGetBuiltInDisplay },
900 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
901 (void*)nativeCreateDisplay },
Jesse Hall6a6bc212013-08-08 12:15:03 -0700902 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
903 (void*)nativeDestroyDisplay },
Robert Carre13b58e2017-08-31 14:50:44 -0700904 {"nativeSetDisplaySurface", "(JLandroid/os/IBinder;J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800905 (void*)nativeSetDisplaySurface },
Robert Carre13b58e2017-08-31 14:50:44 -0700906 {"nativeSetDisplayLayerStack", "(JLandroid/os/IBinder;I)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800907 (void*)nativeSetDisplayLayerStack },
Robert Carre13b58e2017-08-31 14:50:44 -0700908 {"nativeSetDisplayProjection", "(JLandroid/os/IBinder;IIIIIIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800909 (void*)nativeSetDisplayProjection },
Robert Carre13b58e2017-08-31 14:50:44 -0700910 {"nativeSetDisplaySize", "(JLandroid/os/IBinder;II)V",
Michael Wright01e840f2014-06-26 16:03:25 -0700911 (void*)nativeSetDisplaySize },
Dan Stoza00101052014-05-02 15:23:40 -0700912 {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
913 (void*)nativeGetDisplayConfigs },
914 {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
915 (void*)nativeGetActiveConfig },
916 {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
917 (void*)nativeSetActiveConfig },
Michael Wright1c9977b2016-07-12 13:30:10 -0700918 {"nativeGetDisplayColorModes", "(Landroid/os/IBinder;)[I",
919 (void*)nativeGetDisplayColorModes},
920 {"nativeGetActiveColorMode", "(Landroid/os/IBinder;)I",
921 (void*)nativeGetActiveColorMode},
922 {"nativeSetActiveColorMode", "(Landroid/os/IBinder;I)Z",
923 (void*)nativeSetActiveColorMode},
Michael Wright9ff94c02016-03-30 18:05:40 -0700924 {"nativeGetHdrCapabilities", "(Landroid/os/IBinder;)Landroid/view/Display$HdrCapabilities;",
925 (void*)nativeGetHdrCapabilities },
Svetoslav1376d602014-03-13 11:17:26 -0700926 {"nativeClearContentFrameStats", "(J)Z",
927 (void*)nativeClearContentFrameStats },
928 {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
929 (void*)nativeGetContentFrameStats },
930 {"nativeClearAnimationFrameStats", "()Z",
931 (void*)nativeClearAnimationFrameStats },
932 {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
933 (void*)nativeGetAnimationFrameStats },
Prashant Malanic55929a2014-05-25 01:59:21 -0700934 {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
935 (void*)nativeSetDisplayPowerMode },
Robert Carre13b58e2017-08-31 14:50:44 -0700936 {"nativeDeferTransactionUntil", "(JJLandroid/os/IBinder;J)V",
Rob Carr64e516f2015-10-29 00:20:45 +0000937 (void*)nativeDeferTransactionUntil },
Robert Carre13b58e2017-08-31 14:50:44 -0700938 {"nativeDeferTransactionUntilSurface", "(JJJJ)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800939 (void*)nativeDeferTransactionUntilSurface },
Robert Carre13b58e2017-08-31 14:50:44 -0700940 {"nativeReparentChildren", "(JJLandroid/os/IBinder;)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800941 (void*)nativeReparentChildren } ,
Robert Carre13b58e2017-08-31 14:50:44 -0700942 {"nativeReparent", "(JJLandroid/os/IBinder;)V",
chaviw76431402017-09-18 16:50:05 -0700943 (void*)nativeReparent },
Robert Carre13b58e2017-08-31 14:50:44 -0700944 {"nativeSeverChildren", "(JJ)V",
Robert Carrd5c7dd62017-03-08 10:39:30 -0800945 (void*)nativeSeverChildren } ,
Robert Carre13b58e2017-08-31 14:50:44 -0700946 {"nativeSetOverrideScalingMode", "(JJI)V",
Robert Carr1ca6a332016-04-11 18:00:43 -0700947 (void*)nativeSetOverrideScalingMode },
Chavi Weingartenb736e322018-02-23 00:27:54 +0000948 {"nativeDestroy", "(JJ)V",
949 (void*)nativeDestroyInTransaction },
Rob Carr64e516f2015-10-29 00:20:45 +0000950 {"nativeGetHandle", "(J)Landroid/os/IBinder;",
Robert Carr6da3cc02016-06-16 15:17:07 -0700951 (void*)nativeGetHandle },
chaviw08520a02018-09-10 16:44:56 -0700952 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIZI)Landroid/graphics/GraphicBuffer;",
953 (void*)nativeScreenshot },
Chavi Weingartenea2eb5a2017-11-29 21:26:24 +0000954 {"nativeCaptureLayers", "(Landroid/os/IBinder;Landroid/graphics/Rect;F)Landroid/graphics/GraphicBuffer;",
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000955 (void*)nativeCaptureLayers },
Robert Carr788f5742018-07-30 17:46:45 -0700956 {"nativeSetInputWindowInfo", "(JJLandroid/view/InputWindowHandle;)V",
957 (void*)nativeSetInputWindowInfo },
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800958};
959
960int register_android_view_SurfaceControl(JNIEnv* env)
961{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800962 int err = RegisterMethodsOrDie(env, "android/view/SurfaceControl",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800963 sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
964
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800965 jclass clazz = FindClassOrDie(env, "android/view/SurfaceControl$PhysicalDisplayInfo");
966 gPhysicalDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
967 gPhysicalDisplayInfoClassInfo.ctor = GetMethodIDOrDie(env,
968 gPhysicalDisplayInfoClassInfo.clazz, "<init>", "()V");
969 gPhysicalDisplayInfoClassInfo.width = GetFieldIDOrDie(env, clazz, "width", "I");
970 gPhysicalDisplayInfoClassInfo.height = GetFieldIDOrDie(env, clazz, "height", "I");
971 gPhysicalDisplayInfoClassInfo.refreshRate = GetFieldIDOrDie(env, clazz, "refreshRate", "F");
972 gPhysicalDisplayInfoClassInfo.density = GetFieldIDOrDie(env, clazz, "density", "F");
973 gPhysicalDisplayInfoClassInfo.xDpi = GetFieldIDOrDie(env, clazz, "xDpi", "F");
974 gPhysicalDisplayInfoClassInfo.yDpi = GetFieldIDOrDie(env, clazz, "yDpi", "F");
975 gPhysicalDisplayInfoClassInfo.secure = GetFieldIDOrDie(env, clazz, "secure", "Z");
976 gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = GetFieldIDOrDie(env,
977 clazz, "appVsyncOffsetNanos", "J");
978 gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env,
979 clazz, "presentationDeadlineNanos", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700980
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800981 jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect");
982 gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I");
983 gRectClassInfo.left = GetFieldIDOrDie(env, rectClazz, "left", "I");
984 gRectClassInfo.right = GetFieldIDOrDie(env, rectClazz, "right", "I");
985 gRectClassInfo.top = GetFieldIDOrDie(env, rectClazz, "top", "I");
Dan Stoza9890e3412014-05-22 16:12:54 -0700986
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800987 jclass frameStatsClazz = FindClassOrDie(env, "android/view/FrameStats");
988 jfieldID undefined_time_nano_field = GetStaticFieldIDOrDie(env,
989 frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700990 nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
991
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800992 jclass contFrameStatsClazz = FindClassOrDie(env, "android/view/WindowContentFrameStats");
993 gWindowContentFrameStatsClassInfo.init = GetMethodIDOrDie(env,
994 contFrameStatsClazz, "init", "(J[J[J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700995 gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
996
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800997 jclass animFrameStatsClazz = FindClassOrDie(env, "android/view/WindowAnimationFrameStats");
998 gWindowAnimationFrameStatsClassInfo.init = GetMethodIDOrDie(env,
999 animFrameStatsClazz, "init", "(J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -07001000 gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
1001
Hangyu Kuang54ac2192016-04-25 13:22:02 -07001002 jclass hdrCapabilitiesClazz = FindClassOrDie(env, "android/view/Display$HdrCapabilities");
1003 gHdrCapabilitiesClassInfo.clazz = MakeGlobalRefOrDie(env, hdrCapabilitiesClazz);
1004 gHdrCapabilitiesClassInfo.ctor = GetMethodIDOrDie(env, hdrCapabilitiesClazz, "<init>",
1005 "([IFFF)V");
1006
Robert Carr6486d312017-01-09 19:48:29 -08001007 jclass graphicsBufferClazz = FindClassOrDie(env, "android/graphics/GraphicBuffer");
1008 gGraphicBufferClassInfo.clazz = MakeGlobalRefOrDie(env, graphicsBufferClazz);
1009 gGraphicBufferClassInfo.builder = GetStaticMethodIDOrDie(env, graphicsBufferClazz,
1010 "createFromExisting", "(IIIIJ)Landroid/graphics/GraphicBuffer;");
1011
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001012 return err;
1013}
1014
1015};