1 /*
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"
18 #define LOG_NDEBUG 0
19 
20 #include "android_os_Parcel.h"
21 #include "android_util_Binder.h"
22 #include "android_hardware_input_InputWindowHandle.h"
23 #include "android/graphics/Bitmap.h"
24 #include "android/graphics/GraphicsJNI.h"
25 #include "android/graphics/Region.h"
26 #include "core_jni_helpers.h"
27 
28 #include <android-base/chrono_utils.h>
29 #include <nativehelper/JNIHelp.h>
30 #include <nativehelper/ScopedUtfChars.h>
31 #include <android_runtime/android_view_Surface.h>
32 #include <android_runtime/android_view_SurfaceSession.h>
33 #include <gui/Surface.h>
34 #include <gui/SurfaceComposerClient.h>
35 #include <jni.h>
36 #include <memory>
37 #include <stdio.h>
38 #include <system/graphics.h>
39 #include <ui/ConfigStoreTypes.h>
40 #include <ui/DisplayInfo.h>
41 #include <ui/DisplayedFrameStats.h>
42 #include <ui/FrameStats.h>
43 #include <ui/GraphicTypes.h>
44 #include <ui/HdrCapabilities.h>
45 #include <ui/Rect.h>
46 #include <ui/Region.h>
47 #include <utils/Log.h>
48 
49 // ----------------------------------------------------------------------------
50 
51 namespace android {
52 
53 static const char* const OutOfResourcesException =
54     "android/view/Surface$OutOfResourcesException";
55 
56 static struct {
57     jclass clazz;
58     jmethodID ctor;
59     jfieldID width;
60     jfieldID height;
61     jfieldID refreshRate;
62     jfieldID density;
63     jfieldID xDpi;
64     jfieldID yDpi;
65     jfieldID secure;
66     jfieldID appVsyncOffsetNanos;
67     jfieldID presentationDeadlineNanos;
68 } gPhysicalDisplayInfoClassInfo;
69 
70 static struct {
71     jfieldID bottom;
72     jfieldID left;
73     jfieldID right;
74     jfieldID top;
75 } gRectClassInfo;
76 
77 // Implements SkMallocPixelRef::ReleaseProc, to delete the screenshot on unref.
DeleteScreenshot(void * addr,void * context)78 void DeleteScreenshot(void* addr, void* context) {
79     delete ((ScreenshotClient*) context);
80 }
81 
82 static struct {
83     nsecs_t UNDEFINED_TIME_NANO;
84     jmethodID init;
85 } gWindowContentFrameStatsClassInfo;
86 
87 static struct {
88     nsecs_t UNDEFINED_TIME_NANO;
89     jmethodID init;
90 } gWindowAnimationFrameStatsClassInfo;
91 
92 static struct {
93     jclass clazz;
94     jmethodID ctor;
95 } gHdrCapabilitiesClassInfo;
96 
97 static struct {
98     jclass clazz;
99     jmethodID builder;
100 } gGraphicBufferClassInfo;
101 
102 static struct {
103     jclass clazz;
104     jmethodID ctor;
105 } gDisplayedContentSampleClassInfo;
106 
107 static struct {
108     jclass clazz;
109     jmethodID ctor;
110 } gDisplayedContentSamplingAttributesClassInfo;
111 
112 static struct {
113     jclass clazz;
114     jmethodID ctor;
115     jfieldID X;
116     jfieldID Y;
117     jfieldID Z;
118 } gCieXyzClassInfo;
119 
120 static struct {
121     jclass clazz;
122     jmethodID ctor;
123     jfieldID red;
124     jfieldID green;
125     jfieldID blue;
126     jfieldID white;
127 } gDisplayPrimariesClassInfo;
128 
129 static struct {
130     jclass clazz;
131     jmethodID builder;
132 } gScreenshotGraphicBufferClassInfo;
133 
134 class JNamedColorSpace {
135 public:
136     // ColorSpace.Named.SRGB.ordinal() = 0;
137     static constexpr jint SRGB = 0;
138 
139     // ColorSpace.Named.DISPLAY_P3.ordinal() = 7;
140     static constexpr jint DISPLAY_P3 = 7;
141 };
142 
fromDataspaceToNamedColorSpaceValue(const ui::Dataspace dataspace)143 constexpr jint fromDataspaceToNamedColorSpaceValue(const ui::Dataspace dataspace) {
144     switch (dataspace) {
145         case ui::Dataspace::DISPLAY_P3:
146             return JNamedColorSpace::DISPLAY_P3;
147         default:
148             return JNamedColorSpace::SRGB;
149     }
150 }
151 
pickDataspaceFromColorMode(const ui::ColorMode colorMode)152 constexpr ui::Dataspace pickDataspaceFromColorMode(const ui::ColorMode colorMode) {
153     switch (colorMode) {
154         case ui::ColorMode::DISPLAY_P3:
155         case ui::ColorMode::BT2100_PQ:
156         case ui::ColorMode::BT2100_HLG:
157         case ui::ColorMode::DISPLAY_BT2020:
158             return ui::Dataspace::DISPLAY_P3;
159         default:
160             return ui::Dataspace::V0_SRGB;
161     }
162 }
163 
164 // ----------------------------------------------------------------------------
165 
nativeCreateTransaction(JNIEnv * env,jclass clazz)166 static jlong nativeCreateTransaction(JNIEnv* env, jclass clazz) {
167     return reinterpret_cast<jlong>(new SurfaceComposerClient::Transaction);
168 }
169 
releaseTransaction(SurfaceComposerClient::Transaction * t)170 static void releaseTransaction(SurfaceComposerClient::Transaction* t) {
171     delete t;
172 }
173 
nativeGetNativeTransactionFinalizer(JNIEnv * env,jclass clazz)174 static jlong nativeGetNativeTransactionFinalizer(JNIEnv* env, jclass clazz) {
175     return static_cast<jlong>(reinterpret_cast<uintptr_t>(&releaseTransaction));
176 }
177 
nativeCreate(JNIEnv * env,jclass clazz,jobject sessionObj,jstring nameStr,jint w,jint h,jint format,jint flags,jlong parentObject,jobject metadataParcel)178 static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
179         jstring nameStr, jint w, jint h, jint format, jint flags, jlong parentObject,
180         jobject metadataParcel) {
181     ScopedUtfChars name(env, nameStr);
182     sp<SurfaceComposerClient> client;
183     if (sessionObj != NULL) {
184         client = android_view_SurfaceSession_getClient(env, sessionObj);
185     } else {
186         client = SurfaceComposerClient::getDefault();
187     }
188     SurfaceControl *parent = reinterpret_cast<SurfaceControl*>(parentObject);
189     sp<SurfaceControl> surface;
190     LayerMetadata metadata;
191     Parcel* parcel = parcelForJavaObject(env, metadataParcel);
192     if (parcel && !parcel->objectsCount()) {
193         status_t err = metadata.readFromParcel(parcel);
194         if (err != NO_ERROR) {
195           jniThrowException(env, "java/lang/IllegalArgumentException",
196                             "Metadata parcel has wrong format");
197         }
198     }
199 
200     status_t err = client->createSurfaceChecked(
201             String8(name.c_str()), w, h, format, &surface, flags, parent, std::move(metadata));
202     if (err == NAME_NOT_FOUND) {
203         jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
204         return 0;
205     } else if (err != NO_ERROR) {
206         jniThrowException(env, OutOfResourcesException, NULL);
207         return 0;
208     }
209 
210     surface->incStrong((void *)nativeCreate);
211     return reinterpret_cast<jlong>(surface.get());
212 }
213 
nativeRelease(JNIEnv * env,jclass clazz,jlong nativeObject)214 static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
215     sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
216     ctrl->release();
217     ctrl->decStrong((void *)nativeCreate);
218 }
219 
nativeDisconnect(JNIEnv * env,jclass clazz,jlong nativeObject)220 static void nativeDisconnect(JNIEnv* env, jclass clazz, jlong nativeObject) {
221     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
222     if (ctrl != NULL) {
223         ctrl->disconnect();
224     }
225 }
226 
rectFromObj(JNIEnv * env,jobject rectObj)227 static Rect rectFromObj(JNIEnv* env, jobject rectObj) {
228     int left = env->GetIntField(rectObj, gRectClassInfo.left);
229     int top = env->GetIntField(rectObj, gRectClassInfo.top);
230     int right = env->GetIntField(rectObj, gRectClassInfo.right);
231     int bottom = env->GetIntField(rectObj, gRectClassInfo.bottom);
232     return Rect(left, top, right, bottom);
233 }
234 
nativeScreenshot(JNIEnv * env,jclass clazz,jobject displayTokenObj,jobject sourceCropObj,jint width,jint height,bool useIdentityTransform,int rotation,bool captureSecureLayers)235 static jobject nativeScreenshot(JNIEnv* env, jclass clazz,
236         jobject displayTokenObj, jobject sourceCropObj, jint width, jint height,
237         bool useIdentityTransform, int rotation, bool captureSecureLayers) {
238     sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
239     if (displayToken == NULL) {
240         return NULL;
241     }
242     const ui::ColorMode colorMode = SurfaceComposerClient::getActiveColorMode(displayToken);
243     const ui::Dataspace dataspace = pickDataspaceFromColorMode(colorMode);
244 
245     Rect sourceCrop = rectFromObj(env, sourceCropObj);
246     sp<GraphicBuffer> buffer;
247     bool capturedSecureLayers = false;
248     status_t res = ScreenshotClient::capture(displayToken, dataspace,
249             ui::PixelFormat::RGBA_8888,
250             sourceCrop, width, height,
251             useIdentityTransform, rotation, captureSecureLayers, &buffer, capturedSecureLayers);
252     if (res != NO_ERROR) {
253         return NULL;
254     }
255 
256     const jint namedColorSpace = fromDataspaceToNamedColorSpaceValue(dataspace);
257     return env->CallStaticObjectMethod(gScreenshotGraphicBufferClassInfo.clazz,
258             gScreenshotGraphicBufferClassInfo.builder,
259             buffer->getWidth(),
260             buffer->getHeight(),
261             buffer->getPixelFormat(),
262             (jint)buffer->getUsage(),
263             (jlong)buffer.get(),
264             namedColorSpace,
265             capturedSecureLayers);
266 }
267 
nativeCaptureLayers(JNIEnv * env,jclass clazz,jobject displayTokenObj,jobject layerHandleToken,jobject sourceCropObj,jfloat frameScale,jobjectArray excludeArray)268 static jobject nativeCaptureLayers(JNIEnv* env, jclass clazz, jobject displayTokenObj,
269         jobject layerHandleToken, jobject sourceCropObj, jfloat frameScale,
270         jobjectArray excludeArray) {
271 
272     sp<IBinder> layerHandle = ibinderForJavaObject(env, layerHandleToken);
273     if (layerHandle == NULL) {
274         return NULL;
275     }
276 
277     Rect sourceCrop;
278     if (sourceCropObj != NULL) {
279         sourceCrop = rectFromObj(env, sourceCropObj);
280     }
281 
282     std::unordered_set<sp<IBinder>,ISurfaceComposer::SpHash<IBinder>> excludeHandles;
283     if (excludeArray != NULL) {
284         const jsize len = env->GetArrayLength(excludeArray);
285         excludeHandles.reserve(len);
286 
287         for (jsize i = 0; i < len; i++) {
288             jobject obj = env->GetObjectArrayElement(excludeArray, i);
289             if (obj == nullptr) {
290                 jniThrowNullPointerException(env, "Exclude layer is null");
291                 return NULL;
292             }
293             sp<IBinder> excludeHandle = ibinderForJavaObject(env, obj);
294             excludeHandles.emplace(excludeHandle);
295         }
296     }
297 
298     sp<GraphicBuffer> buffer;
299     ui::Dataspace dataspace = ui::Dataspace::V0_SRGB;
300     sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
301     if (displayToken != nullptr) {
302         const ui::ColorMode colorMode = SurfaceComposerClient::getActiveColorMode(displayToken);
303         dataspace = pickDataspaceFromColorMode(colorMode);
304     }
305     status_t res = ScreenshotClient::captureChildLayers(layerHandle, dataspace,
306                                                         ui::PixelFormat::RGBA_8888, sourceCrop,
307                                                         excludeHandles, frameScale, &buffer);
308     if (res != NO_ERROR) {
309         return NULL;
310     }
311 
312     const jint namedColorSpace = fromDataspaceToNamedColorSpaceValue(dataspace);
313     return env->CallStaticObjectMethod(gScreenshotGraphicBufferClassInfo.clazz,
314                                        gScreenshotGraphicBufferClassInfo.builder,
315                                        buffer->getWidth(),
316                                        buffer->getHeight(),
317                                        buffer->getPixelFormat(),
318                                        (jint)buffer->getUsage(),
319                                        (jlong)buffer.get(),
320                                        namedColorSpace,
321                                        false /* capturedSecureLayers */);
322 }
323 
nativeApplyTransaction(JNIEnv * env,jclass clazz,jlong transactionObj,jboolean sync)324 static void nativeApplyTransaction(JNIEnv* env, jclass clazz, jlong transactionObj, jboolean sync) {
325     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
326     transaction->apply(sync);
327 }
328 
nativeMergeTransaction(JNIEnv * env,jclass clazz,jlong transactionObj,jlong otherTransactionObj)329 static void nativeMergeTransaction(JNIEnv* env, jclass clazz,
330         jlong transactionObj, jlong otherTransactionObj) {
331     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
332     auto otherTransaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(
333             otherTransactionObj);
334     transaction->merge(std::move(*otherTransaction));
335 }
336 
nativeSetAnimationTransaction(JNIEnv * env,jclass clazz,jlong transactionObj)337 static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz, jlong transactionObj) {
338     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
339     transaction->setAnimationTransaction();
340 }
341 
nativeSetEarlyWakeup(JNIEnv * env,jclass clazz,jlong transactionObj)342 static void nativeSetEarlyWakeup(JNIEnv* env, jclass clazz, jlong transactionObj) {
343     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
344     transaction->setEarlyWakeup();
345 }
346 
nativeSetLayer(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jint zorder)347 static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong transactionObj,
348         jlong nativeObject, jint zorder) {
349     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
350 
351     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
352     transaction->setLayer(ctrl, zorder);
353 }
354 
nativeSetRelativeLayer(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jobject relativeTo,jint zorder)355 static void nativeSetRelativeLayer(JNIEnv* env, jclass clazz, jlong transactionObj,
356         jlong nativeObject,
357         jobject relativeTo, jint zorder) {
358 
359     auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
360     sp<IBinder> handle = ibinderForJavaObject(env, relativeTo);
361 
362     {
363         auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
364         transaction->setRelativeLayer(ctrl, handle, zorder);
365     }
366 }
367 
nativeSetPosition(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jfloat x,jfloat y)368 static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong transactionObj,
369         jlong nativeObject, jfloat x, jfloat y) {
370     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
371 
372     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
373     transaction->setPosition(ctrl, x, y);
374 }
375 
nativeSetGeometry(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jobject sourceObj,jobject dstObj,jlong orientation)376 static void nativeSetGeometry(JNIEnv* env, jclass clazz, jlong transactionObj, jlong nativeObject,
377         jobject sourceObj, jobject dstObj, jlong orientation) {
378     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
379     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
380 
381     Rect source, dst;
382     if (sourceObj != NULL) {
383         source = rectFromObj(env, sourceObj);
384     } else {
385         source.makeInvalid();
386     }
387     if (dstObj != NULL) {
388         dst = rectFromObj(env, dstObj);
389     } else {
390         dst.makeInvalid();
391     }
392     transaction->setGeometry(ctrl, source, dst, orientation);
393 }
394 
nativeSetGeometryAppliesWithResize(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject)395 static void nativeSetGeometryAppliesWithResize(JNIEnv* env, jclass clazz,
396 jlong transactionObj,
397         jlong nativeObject) {
398     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
399 
400     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
401     transaction->setGeometryAppliesWithResize(ctrl);
402 }
403 
nativeSetSize(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jint w,jint h)404 static void nativeSetSize(JNIEnv* env, jclass clazz, jlong transactionObj,
405         jlong nativeObject, jint w, jint h) {
406     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
407 
408     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
409     transaction->setSize(ctrl, w, h);
410 }
411 
nativeSetFlags(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jint flags,jint mask)412 static void nativeSetFlags(JNIEnv* env, jclass clazz, jlong transactionObj,
413         jlong nativeObject, jint flags, jint mask) {
414     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
415 
416     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
417     transaction->setFlags(ctrl, flags, mask);
418 }
419 
nativeSetTransparentRegionHint(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jobject regionObj)420 static void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jlong transactionObj,
421         jlong nativeObject, jobject regionObj) {
422     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
423     SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj);
424     if (!region) {
425         doThrowIAE(env);
426         return;
427     }
428 
429     const SkIRect& b(region->getBounds());
430     Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
431     if (region->isComplex()) {
432         SkRegion::Iterator it(*region);
433         while (!it.done()) {
434             const SkIRect& r(it.rect());
435             reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
436             it.next();
437         }
438     }
439 
440     {
441         auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
442         transaction->setTransparentRegionHint(ctrl, reg);
443     }
444 }
445 
nativeSetAlpha(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jfloat alpha)446 static void nativeSetAlpha(JNIEnv* env, jclass clazz, jlong transactionObj,
447         jlong nativeObject, jfloat alpha) {
448     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
449 
450     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
451     transaction->setAlpha(ctrl, alpha);
452 }
453 
nativeSetInputWindowInfo(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jobject inputWindow)454 static void nativeSetInputWindowInfo(JNIEnv* env, jclass clazz, jlong transactionObj,
455         jlong nativeObject, jobject inputWindow) {
456     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
457 
458     sp<NativeInputWindowHandle> handle = android_view_InputWindowHandle_getHandle(
459             env, inputWindow);
460     handle->updateInfo();
461 
462     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
463     transaction->setInputWindowInfo(ctrl, *handle->getInfo());
464 }
465 
nativeSyncInputWindows(JNIEnv * env,jclass clazz,jlong transactionObj)466 static void nativeSyncInputWindows(JNIEnv* env, jclass clazz, jlong transactionObj) {
467     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
468     transaction->syncInputWindows();
469 }
470 
nativeSetMetadata(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jint id,jobject parcelObj)471 static void nativeSetMetadata(JNIEnv* env, jclass clazz, jlong transactionObj,
472         jlong nativeObject, jint id, jobject parcelObj) {
473     Parcel* parcel = parcelForJavaObject(env, parcelObj);
474     if (!parcel) {
475         jniThrowNullPointerException(env, "attribute data");
476         return;
477     }
478     if (parcel->objectsCount()) {
479         jniThrowException(env, "java/lang/RuntimeException",
480                 "Tried to marshall a Parcel that contained Binder objects.");
481         return;
482     }
483 
484     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
485 
486     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl*>(nativeObject);
487     transaction->setMetadata(ctrl, id, *parcel);
488 }
489 
nativeSetColor(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jfloatArray fColor)490 static void nativeSetColor(JNIEnv* env, jclass clazz, jlong transactionObj,
491         jlong nativeObject, jfloatArray fColor) {
492     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
493     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
494 
495     float* floatColors = env->GetFloatArrayElements(fColor, 0);
496     half3 color(floatColors[0], floatColors[1], floatColors[2]);
497     transaction->setColor(ctrl, color);
498 }
499 
nativeSetMatrix(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jfloat dsdx,jfloat dtdx,jfloat dtdy,jfloat dsdy)500 static void nativeSetMatrix(JNIEnv* env, jclass clazz, jlong transactionObj,
501         jlong nativeObject,
502         jfloat dsdx, jfloat dtdx, jfloat dtdy, jfloat dsdy) {
503     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
504 
505     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
506     transaction->setMatrix(ctrl, dsdx, dtdx, dtdy, dsdy);
507 }
508 
nativeSetColorTransform(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jfloatArray fMatrix,jfloatArray fTranslation)509 static void nativeSetColorTransform(JNIEnv* env, jclass clazz, jlong transactionObj,
510         jlong nativeObject, jfloatArray fMatrix, jfloatArray fTranslation) {
511     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
512     SurfaceControl* const surfaceControl = reinterpret_cast<SurfaceControl*>(nativeObject);
513     float* floatMatrix = env->GetFloatArrayElements(fMatrix, 0);
514     mat3 matrix(static_cast<float const*>(floatMatrix));
515     float* floatTranslation = env->GetFloatArrayElements(fTranslation, 0);
516     vec3 translation(floatTranslation[0], floatTranslation[1], floatTranslation[2]);
517     transaction->setColorTransform(surfaceControl, matrix, translation);
518 }
519 
nativeSetColorSpaceAgnostic(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jboolean agnostic)520 static void nativeSetColorSpaceAgnostic(JNIEnv* env, jclass clazz, jlong transactionObj,
521         jlong nativeObject, jboolean agnostic) {
522     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
523     SurfaceControl* const surfaceControl = reinterpret_cast<SurfaceControl*>(nativeObject);
524     transaction->setColorSpaceAgnostic(surfaceControl, agnostic);
525 }
526 
nativeSetWindowCrop(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jint l,jint t,jint r,jint b)527 static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong transactionObj,
528         jlong nativeObject,
529         jint l, jint t, jint r, jint b) {
530     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
531 
532     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
533     Rect crop(l, t, r, b);
534     transaction->setCrop_legacy(ctrl, crop);
535 }
536 
nativeSetCornerRadius(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jfloat cornerRadius)537 static void nativeSetCornerRadius(JNIEnv* env, jclass clazz, jlong transactionObj,
538          jlong nativeObject, jfloat cornerRadius) {
539     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
540 
541     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
542     transaction->setCornerRadius(ctrl, cornerRadius);
543 }
544 
nativeSetLayerStack(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jint layerStack)545 static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong transactionObj,
546         jlong nativeObject, jint layerStack) {
547     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
548 
549     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
550     transaction->setLayerStack(ctrl, layerStack);
551 }
552 
nativeGetPhysicalDisplayIds(JNIEnv * env,jclass clazz)553 static jlongArray nativeGetPhysicalDisplayIds(JNIEnv* env, jclass clazz) {
554     const auto displayIds = SurfaceComposerClient::getPhysicalDisplayIds();
555     jlongArray array = env->NewLongArray(displayIds.size());
556     if (array == nullptr) {
557         jniThrowException(env, "java/lang/OutOfMemoryError", nullptr);
558         return nullptr;
559     }
560 
561     if (displayIds.empty()) {
562         return array;
563     }
564 
565     jlong* values = env->GetLongArrayElements(array, 0);
566     for (size_t i = 0; i < displayIds.size(); ++i) {
567         values[i] = static_cast<jlong>(displayIds[i]);
568     }
569 
570     env->ReleaseLongArrayElements(array, values, 0);
571     return array;
572 }
573 
nativeGetPhysicalDisplayToken(JNIEnv * env,jclass clazz,jlong physicalDisplayId)574 static jobject nativeGetPhysicalDisplayToken(JNIEnv* env, jclass clazz, jlong physicalDisplayId) {
575     sp<IBinder> token = SurfaceComposerClient::getPhysicalDisplayToken(physicalDisplayId);
576     return javaObjectForIBinder(env, token);
577 }
578 
nativeGetDisplayedContentSamplingAttributes(JNIEnv * env,jclass clazz,jobject tokenObj)579 static jobject nativeGetDisplayedContentSamplingAttributes(JNIEnv* env, jclass clazz,
580         jobject tokenObj) {
581     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
582 
583     ui::PixelFormat format;
584     ui::Dataspace dataspace;
585     uint8_t componentMask;
586     status_t err = SurfaceComposerClient::getDisplayedContentSamplingAttributes(
587             token, &format, &dataspace, &componentMask);
588     if (err != OK) {
589         return nullptr;
590     }
591     return env->NewObject(gDisplayedContentSamplingAttributesClassInfo.clazz,
592                           gDisplayedContentSamplingAttributesClassInfo.ctor,
593                           format, dataspace, componentMask);
594 }
595 
nativeSetDisplayedContentSamplingEnabled(JNIEnv * env,jclass clazz,jobject tokenObj,jboolean enable,jint componentMask,jint maxFrames)596 static jboolean nativeSetDisplayedContentSamplingEnabled(JNIEnv* env, jclass clazz,
597         jobject tokenObj, jboolean enable, jint componentMask, jint maxFrames) {
598     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
599     status_t rc = SurfaceComposerClient::setDisplayContentSamplingEnabled(
600             token, enable, componentMask, maxFrames);
601     return rc == OK;
602 }
603 
nativeGetDisplayedContentSample(JNIEnv * env,jclass clazz,jobject tokenObj,jlong maxFrames,jlong timestamp)604 static jobject nativeGetDisplayedContentSample(JNIEnv* env, jclass clazz, jobject tokenObj,
605     jlong maxFrames, jlong timestamp) {
606     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
607 
608     DisplayedFrameStats stats;
609     status_t err = SurfaceComposerClient::getDisplayedContentSample(
610             token, maxFrames, timestamp, &stats);
611     if (err != OK) {
612         return nullptr;
613     }
614 
615     jlongArray histogramComponent0 = env->NewLongArray(stats.component_0_sample.size());
616     jlongArray histogramComponent1 = env->NewLongArray(stats.component_1_sample.size());
617     jlongArray histogramComponent2 = env->NewLongArray(stats.component_2_sample.size());
618     jlongArray histogramComponent3 = env->NewLongArray(stats.component_3_sample.size());
619     if ((histogramComponent0 == nullptr) ||
620         (histogramComponent1 == nullptr) ||
621         (histogramComponent2 == nullptr) ||
622         (histogramComponent3 == nullptr)) {
623         return JNI_FALSE;
624     }
625 
626     env->SetLongArrayRegion(histogramComponent0, 0,
627             stats.component_0_sample.size(),
628             reinterpret_cast<jlong*>(stats.component_0_sample.data()));
629     env->SetLongArrayRegion(histogramComponent1, 0,
630             stats.component_1_sample.size(),
631             reinterpret_cast<jlong*>(stats.component_1_sample.data()));
632     env->SetLongArrayRegion(histogramComponent2, 0,
633             stats.component_2_sample.size(),
634             reinterpret_cast<jlong*>(stats.component_2_sample.data()));
635     env->SetLongArrayRegion(histogramComponent3, 0,
636             stats.component_3_sample.size(),
637             reinterpret_cast<jlong*>(stats.component_3_sample.data()));
638     return env->NewObject(gDisplayedContentSampleClassInfo.clazz,
639                           gDisplayedContentSampleClassInfo.ctor,
640                           stats.numFrames,
641                           histogramComponent0,
642                           histogramComponent1,
643                           histogramComponent2,
644                           histogramComponent3);
645 }
646 
nativeCreateDisplay(JNIEnv * env,jclass clazz,jstring nameObj,jboolean secure)647 static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj,
648         jboolean secure) {
649     ScopedUtfChars name(env, nameObj);
650     sp<IBinder> token(SurfaceComposerClient::createDisplay(
651             String8(name.c_str()), bool(secure)));
652     return javaObjectForIBinder(env, token);
653 }
654 
nativeDestroyDisplay(JNIEnv * env,jclass clazz,jobject tokenObj)655 static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
656     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
657     if (token == NULL) return;
658     SurfaceComposerClient::destroyDisplay(token);
659 }
660 
nativeSetDisplaySurface(JNIEnv * env,jclass clazz,jlong transactionObj,jobject tokenObj,jlong nativeSurfaceObject)661 static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
662         jlong transactionObj,
663         jobject tokenObj, jlong nativeSurfaceObject) {
664     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
665     if (token == NULL) return;
666     sp<IGraphicBufferProducer> bufferProducer;
667     sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
668     if (sur != NULL) {
669         bufferProducer = sur->getIGraphicBufferProducer();
670     }
671 
672 
673     status_t err = NO_ERROR;
674     {
675         auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
676         err = transaction->setDisplaySurface(token,
677                 bufferProducer);
678     }
679     if (err != NO_ERROR) {
680         doThrowIAE(env, "Illegal Surface, could not enable async mode. Was this"
681                 " Surface created with singleBufferMode?");
682     }
683 }
684 
nativeSetDisplayLayerStack(JNIEnv * env,jclass clazz,jlong transactionObj,jobject tokenObj,jint layerStack)685 static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
686         jlong transactionObj,
687         jobject tokenObj, jint layerStack) {
688 
689     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
690     if (token == NULL) return;
691 
692     {
693         auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
694         transaction->setDisplayLayerStack(token, layerStack);
695     }
696 }
697 
nativeSetDisplayProjection(JNIEnv * env,jclass clazz,jlong transactionObj,jobject tokenObj,jint orientation,jint layerStackRect_left,jint layerStackRect_top,jint layerStackRect_right,jint layerStackRect_bottom,jint displayRect_left,jint displayRect_top,jint displayRect_right,jint displayRect_bottom)698 static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz,
699         jlong transactionObj,
700         jobject tokenObj, jint orientation,
701         jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom,
702         jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) {
703     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
704     if (token == NULL) return;
705     Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom);
706     Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom);
707 
708     {
709         auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
710         transaction->setDisplayProjection(token, orientation, layerStackRect, displayRect);
711     }
712 }
713 
nativeSetDisplaySize(JNIEnv * env,jclass clazz,jlong transactionObj,jobject tokenObj,jint width,jint height)714 static void nativeSetDisplaySize(JNIEnv* env, jclass clazz,
715         jlong transactionObj,
716         jobject tokenObj, jint width, jint height) {
717     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
718     if (token == NULL) return;
719 
720     {
721         auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
722         transaction->setDisplaySize(token, width, height);
723     }
724 }
725 
nativeGetDisplayConfigs(JNIEnv * env,jclass clazz,jobject tokenObj)726 static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
727         jobject tokenObj) {
728     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
729     if (token == NULL) return NULL;
730 
731     Vector<DisplayInfo> configs;
732     if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
733             configs.size() == 0) {
734         return NULL;
735     }
736 
737     jobjectArray configArray = env->NewObjectArray(configs.size(),
738             gPhysicalDisplayInfoClassInfo.clazz, NULL);
739 
740     for (size_t c = 0; c < configs.size(); ++c) {
741         const DisplayInfo& info = configs[c];
742         jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
743                 gPhysicalDisplayInfoClassInfo.ctor);
744         env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
745         env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
746         env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
747         env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
748         env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
749         env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
750         env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
751         env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos,
752                 info.appVsyncOffset);
753         env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos,
754                 info.presentationDeadline);
755         env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
756         env->DeleteLocalRef(infoObj);
757     }
758 
759     return configArray;
760 }
761 
nativeSetAllowedDisplayConfigs(JNIEnv * env,jclass clazz,jobject tokenObj,jintArray configArray)762 static jboolean nativeSetAllowedDisplayConfigs(JNIEnv* env, jclass clazz,
763         jobject tokenObj, jintArray configArray) {
764     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
765     if (token == nullptr) return JNI_FALSE;
766 
767     std::vector<int32_t> allowedConfigs;
768     jsize configArraySize = env->GetArrayLength(configArray);
769     allowedConfigs.reserve(configArraySize);
770 
771     jint* configArrayElements = env->GetIntArrayElements(configArray, 0);
772     for (int i = 0; i < configArraySize; i++) {
773         allowedConfigs.push_back(configArrayElements[i]);
774     }
775     env->ReleaseIntArrayElements(configArray, configArrayElements, 0);
776 
777     size_t result = SurfaceComposerClient::setAllowedDisplayConfigs(token, allowedConfigs);
778     return result == NO_ERROR ? JNI_TRUE : JNI_FALSE;
779 }
780 
nativeGetAllowedDisplayConfigs(JNIEnv * env,jclass clazz,jobject tokenObj)781 static jintArray nativeGetAllowedDisplayConfigs(JNIEnv* env, jclass clazz, jobject tokenObj) {
782     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
783     if (token == nullptr) return JNI_FALSE;
784 
785     std::vector<int32_t> allowedConfigs;
786     size_t result = SurfaceComposerClient::getAllowedDisplayConfigs(token, &allowedConfigs);
787     if (result != NO_ERROR) {
788         return nullptr;
789     }
790 
791     jintArray allowedConfigsArray = env->NewIntArray(allowedConfigs.size());
792     if (allowedConfigsArray == nullptr) {
793         jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
794         return nullptr;
795     }
796     jint* allowedConfigsArrayValues = env->GetIntArrayElements(allowedConfigsArray, 0);
797     for (size_t i = 0; i < allowedConfigs.size(); i++) {
798         allowedConfigsArrayValues[i] = static_cast<jint>(allowedConfigs[i]);
799     }
800     env->ReleaseIntArrayElements(allowedConfigsArray, allowedConfigsArrayValues, 0);
801     return allowedConfigsArray;
802 }
803 
nativeGetActiveConfig(JNIEnv * env,jclass clazz,jobject tokenObj)804 static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
805     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
806     if (token == NULL) return -1;
807     return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
808 }
809 
nativeSetActiveConfig(JNIEnv * env,jclass clazz,jobject tokenObj,jint id)810 static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
811     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
812     if (token == NULL) return JNI_FALSE;
813     status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
814     return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
815 }
816 
nativeGetDisplayColorModes(JNIEnv * env,jclass,jobject tokenObj)817 static jintArray nativeGetDisplayColorModes(JNIEnv* env, jclass, jobject tokenObj) {
818     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
819     if (token == NULL) return NULL;
820     Vector<ui::ColorMode> colorModes;
821     if (SurfaceComposerClient::getDisplayColorModes(token, &colorModes) != NO_ERROR ||
822             colorModes.isEmpty()) {
823         return NULL;
824     }
825 
826     jintArray colorModesArray = env->NewIntArray(colorModes.size());
827     if (colorModesArray == NULL) {
828         jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
829         return NULL;
830     }
831     jint* colorModesArrayValues = env->GetIntArrayElements(colorModesArray, 0);
832     for (size_t i = 0; i < colorModes.size(); i++) {
833         colorModesArrayValues[i] = static_cast<jint>(colorModes[i]);
834     }
835     env->ReleaseIntArrayElements(colorModesArray, colorModesArrayValues, 0);
836     return colorModesArray;
837 }
838 
nativeGetDisplayNativePrimaries(JNIEnv * env,jclass,jobject tokenObj)839 static jobject nativeGetDisplayNativePrimaries(JNIEnv* env, jclass, jobject tokenObj) {
840     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
841     if (token == NULL) return NULL;
842 
843     ui::DisplayPrimaries primaries;
844     if (SurfaceComposerClient::getDisplayNativePrimaries(token, primaries) != NO_ERROR) {
845         return NULL;
846     }
847 
848     jobject jred = env->NewObject(gCieXyzClassInfo.clazz, gCieXyzClassInfo.ctor);
849     if (jred == NULL) {
850         jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
851         return NULL;
852     }
853 
854     jobject jgreen = env->NewObject(gCieXyzClassInfo.clazz, gCieXyzClassInfo.ctor);
855     if (jgreen == NULL) {
856         jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
857         return NULL;
858     }
859 
860     jobject jblue = env->NewObject(gCieXyzClassInfo.clazz, gCieXyzClassInfo.ctor);
861     if (jblue == NULL) {
862         jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
863         return NULL;
864     }
865 
866     jobject jwhite = env->NewObject(gCieXyzClassInfo.clazz, gCieXyzClassInfo.ctor);
867     if (jwhite == NULL) {
868         jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
869         return NULL;
870     }
871 
872     jobject jprimaries = env->NewObject(gDisplayPrimariesClassInfo.clazz,
873             gDisplayPrimariesClassInfo.ctor);
874     if (jprimaries == NULL) {
875         jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
876         return NULL;
877     }
878 
879     env->SetFloatField(jred, gCieXyzClassInfo.X, primaries.red.X);
880     env->SetFloatField(jred, gCieXyzClassInfo.Y, primaries.red.Y);
881     env->SetFloatField(jred, gCieXyzClassInfo.Z, primaries.red.Z);
882     env->SetFloatField(jgreen, gCieXyzClassInfo.X, primaries.green.X);
883     env->SetFloatField(jgreen, gCieXyzClassInfo.Y, primaries.green.Y);
884     env->SetFloatField(jgreen, gCieXyzClassInfo.Z, primaries.green.Z);
885     env->SetFloatField(jblue, gCieXyzClassInfo.X, primaries.blue.X);
886     env->SetFloatField(jblue, gCieXyzClassInfo.Y, primaries.blue.Y);
887     env->SetFloatField(jblue, gCieXyzClassInfo.Z, primaries.blue.Z);
888     env->SetFloatField(jwhite, gCieXyzClassInfo.X, primaries.white.X);
889     env->SetFloatField(jwhite, gCieXyzClassInfo.Y, primaries.white.Y);
890     env->SetFloatField(jwhite, gCieXyzClassInfo.Z, primaries.white.Z);
891     env->SetObjectField(jprimaries, gDisplayPrimariesClassInfo.red, jred);
892     env->SetObjectField(jprimaries, gDisplayPrimariesClassInfo.green, jgreen);
893     env->SetObjectField(jprimaries, gDisplayPrimariesClassInfo.blue, jblue);
894     env->SetObjectField(jprimaries, gDisplayPrimariesClassInfo.white, jwhite);
895 
896     return jprimaries;
897 }
898 
nativeGetActiveColorMode(JNIEnv * env,jclass,jobject tokenObj)899 static jint nativeGetActiveColorMode(JNIEnv* env, jclass, jobject tokenObj) {
900     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
901     if (token == NULL) return -1;
902     return static_cast<jint>(SurfaceComposerClient::getActiveColorMode(token));
903 }
904 
nativeGetCompositionDataspaces(JNIEnv * env,jclass)905 static jintArray nativeGetCompositionDataspaces(JNIEnv* env, jclass) {
906     ui::Dataspace defaultDataspace, wcgDataspace;
907     ui::PixelFormat defaultPixelFormat, wcgPixelFormat;
908     if (SurfaceComposerClient::getCompositionPreference(&defaultDataspace,
909                                                         &defaultPixelFormat,
910                                                         &wcgDataspace,
911                                                         &wcgPixelFormat) != NO_ERROR) {
912         return nullptr;
913     }
914     jintArray array = env->NewIntArray(2);
915     if (array == nullptr) {
916         jniThrowException(env, "java/lang/OutOfMemoryError", nullptr);
917         return nullptr;
918     }
919     jint* arrayValues = env->GetIntArrayElements(array, 0);
920     arrayValues[0] = static_cast<jint>(defaultDataspace);
921     arrayValues[1] = static_cast<jint>(wcgDataspace);
922     env->ReleaseIntArrayElements(array, arrayValues, 0);
923     return array;
924 }
925 
nativeSetActiveColorMode(JNIEnv * env,jclass,jobject tokenObj,jint colorMode)926 static jboolean nativeSetActiveColorMode(JNIEnv* env, jclass,
927         jobject tokenObj, jint colorMode) {
928     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
929     if (token == NULL) return JNI_FALSE;
930     status_t err = SurfaceComposerClient::setActiveColorMode(token,
931             static_cast<ui::ColorMode>(colorMode));
932     return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
933 }
934 
nativeSetDisplayPowerMode(JNIEnv * env,jclass clazz,jobject tokenObj,jint mode)935 static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
936     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
937     if (token == NULL) return;
938 
939     android::base::Timer t;
940     SurfaceComposerClient::setDisplayPowerMode(token, mode);
941     if (t.duration() > 100ms) ALOGD("Excessive delay in setPowerMode()");
942 }
943 
nativeGetProtectedContentSupport(JNIEnv * env,jclass)944 static jboolean nativeGetProtectedContentSupport(JNIEnv* env, jclass) {
945     return static_cast<jboolean>(SurfaceComposerClient::getProtectedContentSupport());
946 }
947 
nativeClearContentFrameStats(JNIEnv * env,jclass clazz,jlong nativeObject)948 static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
949     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
950     status_t err = ctrl->clearLayerFrameStats();
951 
952     if (err < 0 && err != NO_INIT) {
953         doThrowIAE(env);
954     }
955 
956     // The other end is not ready, just report we failed.
957     if (err == NO_INIT) {
958         return JNI_FALSE;
959     }
960 
961     return JNI_TRUE;
962 }
963 
nativeGetContentFrameStats(JNIEnv * env,jclass clazz,jlong nativeObject,jobject outStats)964 static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
965     jobject outStats) {
966     FrameStats stats;
967 
968     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
969     status_t err = ctrl->getLayerFrameStats(&stats);
970     if (err < 0 && err != NO_INIT) {
971         doThrowIAE(env);
972     }
973 
974     // The other end is not ready, fine just return empty stats.
975     if (err == NO_INIT) {
976         return JNI_FALSE;
977     }
978 
979     jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
980     size_t frameCount = stats.desiredPresentTimesNano.size();
981 
982     jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
983     if (postedTimesNanoDst == NULL) {
984         return JNI_FALSE;
985     }
986 
987     jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
988     if (presentedTimesNanoDst == NULL) {
989         return JNI_FALSE;
990     }
991 
992     jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
993     if (readyTimesNanoDst == NULL) {
994         return JNI_FALSE;
995     }
996 
997     nsecs_t postedTimesNanoSrc[frameCount];
998     nsecs_t presentedTimesNanoSrc[frameCount];
999     nsecs_t readyTimesNanoSrc[frameCount];
1000 
1001     for (size_t i = 0; i < frameCount; i++) {
1002         nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
1003         if (postedTimeNano == INT64_MAX) {
1004             postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
1005         }
1006         postedTimesNanoSrc[i] = postedTimeNano;
1007 
1008         nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
1009         if (presentedTimeNano == INT64_MAX) {
1010             presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
1011         }
1012         presentedTimesNanoSrc[i] = presentedTimeNano;
1013 
1014         nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
1015         if (readyTimeNano == INT64_MAX) {
1016             readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
1017         }
1018         readyTimesNanoSrc[i] = readyTimeNano;
1019     }
1020 
1021     env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
1022     env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
1023     env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
1024 
1025     env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
1026             postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
1027 
1028     if (env->ExceptionCheck()) {
1029         return JNI_FALSE;
1030     }
1031 
1032     return JNI_TRUE;
1033 }
1034 
nativeClearAnimationFrameStats(JNIEnv * env,jclass clazz)1035 static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
1036     status_t err = SurfaceComposerClient::clearAnimationFrameStats();
1037 
1038     if (err < 0 && err != NO_INIT) {
1039         doThrowIAE(env);
1040     }
1041 
1042     // The other end is not ready, just report we failed.
1043     if (err == NO_INIT) {
1044         return JNI_FALSE;
1045     }
1046 
1047     return JNI_TRUE;
1048 }
1049 
nativeGetAnimationFrameStats(JNIEnv * env,jclass clazz,jobject outStats)1050 static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
1051     FrameStats stats;
1052 
1053     status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
1054     if (err < 0 && err != NO_INIT) {
1055         doThrowIAE(env);
1056     }
1057 
1058     // The other end is not ready, fine just return empty stats.
1059     if (err == NO_INIT) {
1060         return JNI_FALSE;
1061     }
1062 
1063     jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
1064     size_t frameCount = stats.desiredPresentTimesNano.size();
1065 
1066     jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
1067     if (presentedTimesNanoDst == NULL) {
1068         return JNI_FALSE;
1069     }
1070 
1071     nsecs_t presentedTimesNanoSrc[frameCount];
1072 
1073     for (size_t i = 0; i < frameCount; i++) {
1074         nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
1075         if (presentedTimeNano == INT64_MAX) {
1076             presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
1077         }
1078         presentedTimesNanoSrc[i] = presentedTimeNano;
1079     }
1080 
1081     env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
1082 
1083     env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
1084             presentedTimesNanoDst);
1085 
1086     if (env->ExceptionCheck()) {
1087         return JNI_FALSE;
1088     }
1089 
1090     return JNI_TRUE;
1091 }
1092 
nativeDeferTransactionUntil(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jobject handleObject,jlong frameNumber)1093 static void nativeDeferTransactionUntil(JNIEnv* env, jclass clazz, jlong transactionObj,
1094         jlong nativeObject,
1095         jobject handleObject, jlong frameNumber) {
1096     auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
1097     sp<IBinder> handle = ibinderForJavaObject(env, handleObject);
1098 
1099     {
1100         auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
1101         transaction->deferTransactionUntil_legacy(ctrl, handle, frameNumber);
1102     }
1103 }
1104 
nativeDeferTransactionUntilSurface(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jlong surfaceObject,jlong frameNumber)1105 static void nativeDeferTransactionUntilSurface(JNIEnv* env, jclass clazz, jlong transactionObj,
1106         jlong nativeObject,
1107         jlong surfaceObject, jlong frameNumber) {
1108     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
1109 
1110     auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
1111     sp<Surface> barrier = reinterpret_cast<Surface *>(surfaceObject);
1112 
1113     transaction->deferTransactionUntil_legacy(ctrl, barrier, frameNumber);
1114 }
1115 
nativeReparentChildren(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jobject newParentObject)1116 static void nativeReparentChildren(JNIEnv* env, jclass clazz, jlong transactionObj,
1117         jlong nativeObject,
1118         jobject newParentObject) {
1119 
1120     auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
1121     sp<IBinder> handle = ibinderForJavaObject(env, newParentObject);
1122 
1123     {
1124         auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
1125         transaction->reparentChildren(ctrl, handle);
1126     }
1127 }
1128 
nativeReparent(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jlong newParentObject)1129 static void nativeReparent(JNIEnv* env, jclass clazz, jlong transactionObj,
1130         jlong nativeObject,
1131         jlong newParentObject) {
1132     auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
1133     auto newParent = reinterpret_cast<SurfaceControl *>(newParentObject);
1134 
1135     {
1136         auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
1137         transaction->reparent(ctrl, newParent != NULL ? newParent->getHandle() : NULL);
1138     }
1139 }
1140 
nativeSeverChildren(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject)1141 static void nativeSeverChildren(JNIEnv* env, jclass clazz, jlong transactionObj,
1142         jlong nativeObject) {
1143     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
1144 
1145     auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
1146     transaction->detachChildren(ctrl);
1147 }
1148 
nativeSetOverrideScalingMode(JNIEnv * env,jclass clazz,jlong transactionObj,jlong nativeObject,jint scalingMode)1149 static void nativeSetOverrideScalingMode(JNIEnv* env, jclass clazz, jlong transactionObj,
1150         jlong nativeObject,
1151         jint scalingMode) {
1152     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
1153 
1154     auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
1155     transaction->setOverrideScalingMode(ctrl, scalingMode);
1156 }
1157 
nativeGetHandle(JNIEnv * env,jclass clazz,jlong nativeObject)1158 static jobject nativeGetHandle(JNIEnv* env, jclass clazz, jlong nativeObject) {
1159     auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
1160     return javaObjectForIBinder(env, ctrl->getHandle());
1161 }
1162 
nativeGetHdrCapabilities(JNIEnv * env,jclass clazz,jobject tokenObject)1163 static jobject nativeGetHdrCapabilities(JNIEnv* env, jclass clazz, jobject tokenObject) {
1164     sp<IBinder> token(ibinderForJavaObject(env, tokenObject));
1165     if (token == NULL) return NULL;
1166 
1167     HdrCapabilities capabilities;
1168     SurfaceComposerClient::getHdrCapabilities(token, &capabilities);
1169 
1170     const auto& types = capabilities.getSupportedHdrTypes();
1171     std::vector<int32_t> intTypes;
1172     for (auto type : types) {
1173         intTypes.push_back(static_cast<int32_t>(type));
1174     }
1175     auto typesArray = env->NewIntArray(types.size());
1176     env->SetIntArrayRegion(typesArray, 0, intTypes.size(), intTypes.data());
1177 
1178     return env->NewObject(gHdrCapabilitiesClassInfo.clazz, gHdrCapabilitiesClassInfo.ctor,
1179             typesArray, capabilities.getDesiredMaxLuminance(),
1180             capabilities.getDesiredMaxAverageLuminance(), capabilities.getDesiredMinLuminance());
1181 }
1182 
nativeReadFromParcel(JNIEnv * env,jclass clazz,jobject parcelObj)1183 static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz, jobject parcelObj) {
1184     Parcel* parcel = parcelForJavaObject(env, parcelObj);
1185     if (parcel == NULL) {
1186         doThrowNPE(env);
1187         return 0;
1188     }
1189     sp<SurfaceControl> surface = SurfaceControl::readFromParcel(parcel);
1190     if (surface == nullptr) {
1191         return 0;
1192     }
1193     surface->incStrong((void *)nativeCreate);
1194     return reinterpret_cast<jlong>(surface.get());
1195 }
1196 
nativeCopyFromSurfaceControl(JNIEnv * env,jclass clazz,jlong surfaceControlNativeObj)1197 static jlong nativeCopyFromSurfaceControl(JNIEnv* env, jclass clazz, jlong surfaceControlNativeObj) {
1198     sp<SurfaceControl> surface(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
1199     if (surface == nullptr) {
1200         return 0;
1201     }
1202 
1203     sp<SurfaceControl> newSurface = new SurfaceControl(surface);
1204     newSurface->incStrong((void *)nativeCreate);
1205     return reinterpret_cast<jlong>(newSurface.get());
1206 }
1207 
nativeWriteToParcel(JNIEnv * env,jclass clazz,jlong nativeObject,jobject parcelObj)1208 static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
1209         jlong nativeObject, jobject parcelObj) {
1210     Parcel* parcel = parcelForJavaObject(env, parcelObj);
1211     if (parcel == NULL) {
1212         doThrowNPE(env);
1213         return;
1214     }
1215     SurfaceControl* const self = reinterpret_cast<SurfaceControl *>(nativeObject);
1216     if (self != nullptr) {
1217         self->writeToParcel(parcel);
1218     }
1219 }
1220 
nativeGetDisplayBrightnessSupport(JNIEnv * env,jclass clazz,jobject displayTokenObject)1221 static jboolean nativeGetDisplayBrightnessSupport(JNIEnv* env, jclass clazz,
1222         jobject displayTokenObject) {
1223     sp<IBinder> displayToken(ibinderForJavaObject(env, displayTokenObject));
1224     if (displayToken == nullptr) {
1225         return JNI_FALSE;
1226     }
1227     return static_cast<jboolean>(SurfaceComposerClient::getDisplayBrightnessSupport(displayToken));
1228 }
1229 
nativeSetDisplayBrightness(JNIEnv * env,jclass clazz,jobject displayTokenObject,jfloat brightness)1230 static jboolean nativeSetDisplayBrightness(JNIEnv* env, jclass clazz, jobject displayTokenObject,
1231         jfloat brightness) {
1232     sp<IBinder> displayToken(ibinderForJavaObject(env, displayTokenObject));
1233     if (displayToken == nullptr) {
1234         return JNI_FALSE;
1235     }
1236     status_t error = SurfaceComposerClient::setDisplayBrightness(displayToken, brightness);
1237     return error == OK ? JNI_TRUE : JNI_FALSE;
1238 }
1239 
1240 // ----------------------------------------------------------------------------
1241 
1242 static const JNINativeMethod sSurfaceControlMethods[] = {
1243     {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIIIJLandroid/os/Parcel;)J",
1244             (void*)nativeCreate },
1245     {"nativeReadFromParcel", "(Landroid/os/Parcel;)J",
1246             (void*)nativeReadFromParcel },
1247     {"nativeCopyFromSurfaceControl", "(J)J" ,
1248             (void*)nativeCopyFromSurfaceControl },
1249     {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
1250             (void*)nativeWriteToParcel },
1251     {"nativeRelease", "(J)V",
1252             (void*)nativeRelease },
1253     {"nativeDisconnect", "(J)V",
1254             (void*)nativeDisconnect },
1255     {"nativeCreateTransaction", "()J",
1256             (void*)nativeCreateTransaction },
1257     {"nativeApplyTransaction", "(JZ)V",
1258             (void*)nativeApplyTransaction },
1259     {"nativeGetNativeTransactionFinalizer", "()J",
1260             (void*)nativeGetNativeTransactionFinalizer },
1261     {"nativeMergeTransaction", "(JJ)V",
1262             (void*)nativeMergeTransaction },
1263     {"nativeSetAnimationTransaction", "(J)V",
1264             (void*)nativeSetAnimationTransaction },
1265     {"nativeSetEarlyWakeup", "(J)V",
1266             (void*)nativeSetEarlyWakeup },
1267     {"nativeSetLayer", "(JJI)V",
1268             (void*)nativeSetLayer },
1269     {"nativeSetRelativeLayer", "(JJLandroid/os/IBinder;I)V",
1270             (void*)nativeSetRelativeLayer },
1271     {"nativeSetPosition", "(JJFF)V",
1272             (void*)nativeSetPosition },
1273     {"nativeSetGeometryAppliesWithResize", "(JJ)V",
1274             (void*)nativeSetGeometryAppliesWithResize },
1275     {"nativeSetSize", "(JJII)V",
1276             (void*)nativeSetSize },
1277     {"nativeSetTransparentRegionHint", "(JJLandroid/graphics/Region;)V",
1278             (void*)nativeSetTransparentRegionHint },
1279     {"nativeSetAlpha", "(JJF)V",
1280             (void*)nativeSetAlpha },
1281     {"nativeSetColor", "(JJ[F)V",
1282             (void*)nativeSetColor },
1283     {"nativeSetMatrix", "(JJFFFF)V",
1284             (void*)nativeSetMatrix },
1285     {"nativeSetColorTransform", "(JJ[F[F)V",
1286             (void*)nativeSetColorTransform },
1287     {"nativeSetColorSpaceAgnostic", "(JJZ)V",
1288             (void*)nativeSetColorSpaceAgnostic },
1289     {"nativeSetFlags", "(JJII)V",
1290             (void*)nativeSetFlags },
1291     {"nativeSetWindowCrop", "(JJIIII)V",
1292             (void*)nativeSetWindowCrop },
1293     {"nativeSetCornerRadius", "(JJF)V",
1294             (void*)nativeSetCornerRadius },
1295     {"nativeSetLayerStack", "(JJI)V",
1296             (void*)nativeSetLayerStack },
1297     {"nativeGetPhysicalDisplayIds", "()[J",
1298             (void*)nativeGetPhysicalDisplayIds },
1299     {"nativeGetPhysicalDisplayToken", "(J)Landroid/os/IBinder;",
1300             (void*)nativeGetPhysicalDisplayToken },
1301     {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
1302             (void*)nativeCreateDisplay },
1303     {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
1304             (void*)nativeDestroyDisplay },
1305     {"nativeSetDisplaySurface", "(JLandroid/os/IBinder;J)V",
1306             (void*)nativeSetDisplaySurface },
1307     {"nativeSetDisplayLayerStack", "(JLandroid/os/IBinder;I)V",
1308             (void*)nativeSetDisplayLayerStack },
1309     {"nativeSetDisplayProjection", "(JLandroid/os/IBinder;IIIIIIIII)V",
1310             (void*)nativeSetDisplayProjection },
1311     {"nativeSetDisplaySize", "(JLandroid/os/IBinder;II)V",
1312             (void*)nativeSetDisplaySize },
1313     {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
1314             (void*)nativeGetDisplayConfigs },
1315     {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
1316             (void*)nativeGetActiveConfig },
1317     {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
1318             (void*)nativeSetActiveConfig },
1319     {"nativeSetAllowedDisplayConfigs", "(Landroid/os/IBinder;[I)Z",
1320             (void*)nativeSetAllowedDisplayConfigs },
1321     {"nativeGetAllowedDisplayConfigs", "(Landroid/os/IBinder;)[I",
1322             (void*)nativeGetAllowedDisplayConfigs },
1323     {"nativeGetDisplayColorModes", "(Landroid/os/IBinder;)[I",
1324             (void*)nativeGetDisplayColorModes},
1325     {"nativeGetDisplayNativePrimaries", "(Landroid/os/IBinder;)Landroid/view/SurfaceControl$DisplayPrimaries;",
1326             (void*)nativeGetDisplayNativePrimaries },
1327     {"nativeGetActiveColorMode", "(Landroid/os/IBinder;)I",
1328             (void*)nativeGetActiveColorMode},
1329     {"nativeSetActiveColorMode", "(Landroid/os/IBinder;I)Z",
1330             (void*)nativeSetActiveColorMode},
1331     {"nativeGetCompositionDataspaces", "()[I",
1332             (void*)nativeGetCompositionDataspaces},
1333     {"nativeGetHdrCapabilities", "(Landroid/os/IBinder;)Landroid/view/Display$HdrCapabilities;",
1334             (void*)nativeGetHdrCapabilities },
1335     {"nativeClearContentFrameStats", "(J)Z",
1336             (void*)nativeClearContentFrameStats },
1337     {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
1338             (void*)nativeGetContentFrameStats },
1339     {"nativeClearAnimationFrameStats", "()Z",
1340             (void*)nativeClearAnimationFrameStats },
1341     {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
1342             (void*)nativeGetAnimationFrameStats },
1343     {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
1344             (void*)nativeSetDisplayPowerMode },
1345     {"nativeGetProtectedContentSupport", "()Z",
1346             (void*)nativeGetProtectedContentSupport },
1347     {"nativeDeferTransactionUntil", "(JJLandroid/os/IBinder;J)V",
1348             (void*)nativeDeferTransactionUntil },
1349     {"nativeDeferTransactionUntilSurface", "(JJJJ)V",
1350             (void*)nativeDeferTransactionUntilSurface },
1351     {"nativeReparentChildren", "(JJLandroid/os/IBinder;)V",
1352             (void*)nativeReparentChildren } ,
1353     {"nativeReparent", "(JJJ)V",
1354             (void*)nativeReparent },
1355     {"nativeSeverChildren", "(JJ)V",
1356             (void*)nativeSeverChildren } ,
1357     {"nativeSetOverrideScalingMode", "(JJI)V",
1358             (void*)nativeSetOverrideScalingMode },
1359     {"nativeGetHandle", "(J)Landroid/os/IBinder;",
1360             (void*)nativeGetHandle },
1361     {"nativeScreenshot",
1362             "(Landroid/os/IBinder;Landroid/graphics/Rect;IIZIZ)"
1363             "Landroid/view/SurfaceControl$ScreenshotGraphicBuffer;",
1364             (void*)nativeScreenshot },
1365     {"nativeCaptureLayers",
1366             "(Landroid/os/IBinder;Landroid/os/IBinder;Landroid/graphics/Rect;"
1367             "F[Landroid/os/IBinder;)"
1368             "Landroid/view/SurfaceControl$ScreenshotGraphicBuffer;",
1369             (void*)nativeCaptureLayers },
1370     {"nativeSetInputWindowInfo", "(JJLandroid/view/InputWindowHandle;)V",
1371             (void*)nativeSetInputWindowInfo },
1372     {"nativeSetMetadata", "(JJILandroid/os/Parcel;)V",
1373             (void*)nativeSetMetadata },
1374     {"nativeGetDisplayedContentSamplingAttributes",
1375             "(Landroid/os/IBinder;)Landroid/hardware/display/DisplayedContentSamplingAttributes;",
1376             (void*)nativeGetDisplayedContentSamplingAttributes },
1377     {"nativeSetDisplayedContentSamplingEnabled", "(Landroid/os/IBinder;ZII)Z",
1378             (void*)nativeSetDisplayedContentSamplingEnabled },
1379     {"nativeGetDisplayedContentSample",
1380             "(Landroid/os/IBinder;JJ)Landroid/hardware/display/DisplayedContentSample;",
1381             (void*)nativeGetDisplayedContentSample },
1382     {"nativeSetGeometry", "(JJLandroid/graphics/Rect;Landroid/graphics/Rect;J)V",
1383             (void*)nativeSetGeometry },
1384     {"nativeSyncInputWindows", "(J)V",
1385             (void*)nativeSyncInputWindows },
1386     {"nativeGetDisplayBrightnessSupport", "(Landroid/os/IBinder;)Z",
1387             (void*)nativeGetDisplayBrightnessSupport },
1388     {"nativeSetDisplayBrightness", "(Landroid/os/IBinder;F)Z",
1389             (void*)nativeSetDisplayBrightness },
1390 };
1391 
register_android_view_SurfaceControl(JNIEnv * env)1392 int register_android_view_SurfaceControl(JNIEnv* env)
1393 {
1394     int err = RegisterMethodsOrDie(env, "android/view/SurfaceControl",
1395             sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
1396 
1397     jclass clazz = FindClassOrDie(env, "android/view/SurfaceControl$PhysicalDisplayInfo");
1398     gPhysicalDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
1399     gPhysicalDisplayInfoClassInfo.ctor = GetMethodIDOrDie(env,
1400             gPhysicalDisplayInfoClassInfo.clazz, "<init>", "()V");
1401     gPhysicalDisplayInfoClassInfo.width =       GetFieldIDOrDie(env, clazz, "width", "I");
1402     gPhysicalDisplayInfoClassInfo.height =      GetFieldIDOrDie(env, clazz, "height", "I");
1403     gPhysicalDisplayInfoClassInfo.refreshRate = GetFieldIDOrDie(env, clazz, "refreshRate", "F");
1404     gPhysicalDisplayInfoClassInfo.density =     GetFieldIDOrDie(env, clazz, "density", "F");
1405     gPhysicalDisplayInfoClassInfo.xDpi =        GetFieldIDOrDie(env, clazz, "xDpi", "F");
1406     gPhysicalDisplayInfoClassInfo.yDpi =        GetFieldIDOrDie(env, clazz, "yDpi", "F");
1407     gPhysicalDisplayInfoClassInfo.secure =      GetFieldIDOrDie(env, clazz, "secure", "Z");
1408     gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = GetFieldIDOrDie(env,
1409             clazz, "appVsyncOffsetNanos", "J");
1410     gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env,
1411             clazz, "presentationDeadlineNanos", "J");
1412 
1413     jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect");
1414     gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I");
1415     gRectClassInfo.left =   GetFieldIDOrDie(env, rectClazz, "left", "I");
1416     gRectClassInfo.right =  GetFieldIDOrDie(env, rectClazz, "right", "I");
1417     gRectClassInfo.top =    GetFieldIDOrDie(env, rectClazz, "top", "I");
1418 
1419     jclass frameStatsClazz = FindClassOrDie(env, "android/view/FrameStats");
1420     jfieldID undefined_time_nano_field = GetStaticFieldIDOrDie(env,
1421             frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
1422     nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
1423 
1424     jclass contFrameStatsClazz = FindClassOrDie(env, "android/view/WindowContentFrameStats");
1425     gWindowContentFrameStatsClassInfo.init = GetMethodIDOrDie(env,
1426             contFrameStatsClazz, "init", "(J[J[J[J)V");
1427     gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
1428 
1429     jclass animFrameStatsClazz = FindClassOrDie(env, "android/view/WindowAnimationFrameStats");
1430     gWindowAnimationFrameStatsClassInfo.init =  GetMethodIDOrDie(env,
1431             animFrameStatsClazz, "init", "(J[J)V");
1432     gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
1433 
1434     jclass hdrCapabilitiesClazz = FindClassOrDie(env, "android/view/Display$HdrCapabilities");
1435     gHdrCapabilitiesClassInfo.clazz = MakeGlobalRefOrDie(env, hdrCapabilitiesClazz);
1436     gHdrCapabilitiesClassInfo.ctor = GetMethodIDOrDie(env, hdrCapabilitiesClazz, "<init>",
1437             "([IFFF)V");
1438 
1439     jclass graphicsBufferClazz = FindClassOrDie(env, "android/graphics/GraphicBuffer");
1440     gGraphicBufferClassInfo.clazz = MakeGlobalRefOrDie(env, graphicsBufferClazz);
1441     gGraphicBufferClassInfo.builder = GetStaticMethodIDOrDie(env, graphicsBufferClazz,
1442             "createFromExisting", "(IIIIJ)Landroid/graphics/GraphicBuffer;");
1443 
1444     jclass screenshotGraphicsBufferClazz = FindClassOrDie(env,
1445             "android/view/SurfaceControl$ScreenshotGraphicBuffer");
1446     gScreenshotGraphicBufferClassInfo.clazz =
1447             MakeGlobalRefOrDie(env, screenshotGraphicsBufferClazz);
1448     gScreenshotGraphicBufferClassInfo.builder = GetStaticMethodIDOrDie(env,
1449             screenshotGraphicsBufferClazz,
1450             "createFromNative", "(IIIIJIZ)Landroid/view/SurfaceControl$ScreenshotGraphicBuffer;");
1451 
1452     jclass displayedContentSampleClazz = FindClassOrDie(env,
1453             "android/hardware/display/DisplayedContentSample");
1454     gDisplayedContentSampleClassInfo.clazz = MakeGlobalRefOrDie(env, displayedContentSampleClazz);
1455     gDisplayedContentSampleClassInfo.ctor = GetMethodIDOrDie(env,
1456             displayedContentSampleClazz, "<init>", "(J[J[J[J[J)V");
1457 
1458     jclass displayedContentSamplingAttributesClazz = FindClassOrDie(env,
1459             "android/hardware/display/DisplayedContentSamplingAttributes");
1460     gDisplayedContentSamplingAttributesClassInfo.clazz = MakeGlobalRefOrDie(env,
1461             displayedContentSamplingAttributesClazz);
1462     gDisplayedContentSamplingAttributesClassInfo.ctor = GetMethodIDOrDie(env,
1463             displayedContentSamplingAttributesClazz, "<init>", "(III)V");
1464 
1465     jclass cieXyzClazz = FindClassOrDie(env, "android/view/SurfaceControl$CieXyz");
1466     gCieXyzClassInfo.clazz = MakeGlobalRefOrDie(env, cieXyzClazz);
1467     gCieXyzClassInfo.ctor = GetMethodIDOrDie(env, gCieXyzClassInfo.clazz, "<init>", "()V");
1468     gCieXyzClassInfo.X = GetFieldIDOrDie(env, cieXyzClazz, "X", "F");
1469     gCieXyzClassInfo.Y = GetFieldIDOrDie(env, cieXyzClazz, "Y", "F");
1470     gCieXyzClassInfo.Z = GetFieldIDOrDie(env, cieXyzClazz, "Z", "F");
1471 
1472     jclass displayPrimariesClazz = FindClassOrDie(env,
1473             "android/view/SurfaceControl$DisplayPrimaries");
1474     gDisplayPrimariesClassInfo.clazz = MakeGlobalRefOrDie(env, displayPrimariesClazz);
1475     gDisplayPrimariesClassInfo.ctor = GetMethodIDOrDie(env, gDisplayPrimariesClassInfo.clazz,
1476             "<init>", "()V");
1477     gDisplayPrimariesClassInfo.red = GetFieldIDOrDie(env, displayPrimariesClazz, "red",
1478             "Landroid/view/SurfaceControl$CieXyz;");
1479     gDisplayPrimariesClassInfo.green = GetFieldIDOrDie(env, displayPrimariesClazz, "green",
1480             "Landroid/view/SurfaceControl$CieXyz;");
1481     gDisplayPrimariesClassInfo.blue = GetFieldIDOrDie(env, displayPrimariesClazz, "blue",
1482             "Landroid/view/SurfaceControl$CieXyz;");
1483     gDisplayPrimariesClassInfo.white = GetFieldIDOrDie(env, displayPrimariesClazz, "white",
1484             "Landroid/view/SurfaceControl$CieXyz;");
1485 
1486     return err;
1487 }
1488 
1489 } // namespace android
1490