13866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian/*
23866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian * Copyright (C) 2013 The Android Open Source Project
33866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian *
43866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
53866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian * you may not use this file except in compliance with the License.
63866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian * You may obtain a copy of the License at
73866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian *
83866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
93866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian *
103866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian * Unless required by applicable law or agreed to in writing, software
113866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
123866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian * See the License for the specific language governing permissions and
143866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian * limitations under the License.
153866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian */
163866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
173866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian#define LOG_TAG "SurfaceControl"
183866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
193866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian#include "android_os_Parcel.h"
203866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian#include "android_util_Binder.h"
21f29ed28c7b878ef28058bc730715d0d32445bc57John Reck#include "android/graphics/Bitmap.h"
223866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian#include "android/graphics/GraphicsJNI.h"
233866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian#include "android/graphics/Region.h"
24ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe#include "core_jni_helpers.h"
2560126efd7d905ca24822765c6dafac17fef278abBen Wagner
2660126efd7d905ca24822765c6dafac17fef278abBen Wagner#include <JNIHelp.h>
2760126efd7d905ca24822765c6dafac17fef278abBen Wagner#include <ScopedUtfChars.h>
280449a40586b45ee05f760802020dc2f8a6e0506bMathias Agopian#include <android_runtime/android_view_Surface.h>
293866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian#include <android_runtime/android_view_SurfaceSession.h>
303866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian#include <gui/Surface.h>
313866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian#include <gui/SurfaceComposerClient.h>
3260126efd7d905ca24822765c6dafac17fef278abBen Wagner#include <jni.h>
3360126efd7d905ca24822765c6dafac17fef278abBen Wagner#include <memory>
3460126efd7d905ca24822765c6dafac17fef278abBen Wagner#include <stdio.h>
353866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian#include <ui/DisplayInfo.h>
3654ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang#include <ui/HdrCapabilities.h>
371376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav#include <ui/FrameStats.h>
383866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian#include <ui/Rect.h>
393866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian#include <ui/Region.h>
403866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian#include <utils/Log.h>
413866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
423866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian// ----------------------------------------------------------------------------
433866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
443866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopiannamespace android {
453866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
463866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopianstatic const char* const OutOfResourcesException =
473866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    "android/view/Surface$OutOfResourcesException";
483866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
493866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopianstatic struct {
5000101052b5bf99a29acea3525864ed64b709c16cDan Stoza    jclass clazz;
5100101052b5bf99a29acea3525864ed64b709c16cDan Stoza    jmethodID ctor;
523866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    jfieldID width;
533866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    jfieldID height;
543866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    jfieldID refreshRate;
553866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    jfieldID density;
563866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    jfieldID xDpi;
573866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    jfieldID yDpi;
583866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    jfieldID secure;
59e8b1aeb51e1e5da64f1d4fd40f2ee1e815886fe5Andy McFadden    jfieldID appVsyncOffsetNanos;
60e8b1aeb51e1e5da64f1d4fd40f2ee1e815886fe5Andy McFadden    jfieldID presentationDeadlineNanos;
61904f485bc6013060e81b627be61314b3d7fe5ef8Dan Stoza    jfieldID colorTransform;
623866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian} gPhysicalDisplayInfoClassInfo;
633866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
649890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stozastatic struct {
659890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza    jfieldID bottom;
669890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza    jfieldID left;
679890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza    jfieldID right;
689890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza    jfieldID top;
699890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza} gRectClassInfo;
709890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza
7146cb9bdbf56b27cbf2ab878b41d21e30896a4feaLeon Scroggins// Implements SkMallocPixelRef::ReleaseProc, to delete the screenshot on unref.
7246cb9bdbf56b27cbf2ab878b41d21e30896a4feaLeon Scrogginsvoid DeleteScreenshot(void* addr, void* context) {
7346cb9bdbf56b27cbf2ab878b41d21e30896a4feaLeon Scroggins    SkASSERT(addr == ((ScreenshotClient*) context)->getPixels());
7446cb9bdbf56b27cbf2ab878b41d21e30896a4feaLeon Scroggins    delete ((ScreenshotClient*) context);
7546cb9bdbf56b27cbf2ab878b41d21e30896a4feaLeon Scroggins}
763866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
771376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslavstatic struct {
781376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    nsecs_t UNDEFINED_TIME_NANO;
791376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    jmethodID init;
801376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav} gWindowContentFrameStatsClassInfo;
811376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
821376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslavstatic struct {
831376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    nsecs_t UNDEFINED_TIME_NANO;
841376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    jmethodID init;
851376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav} gWindowAnimationFrameStatsClassInfo;
861376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
8754ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuangstatic struct {
8854ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang    jclass clazz;
8954ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang    jmethodID ctor;
9054ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang} gHdrCapabilitiesClassInfo;
9154ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang
923866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian// ----------------------------------------------------------------------------
933866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
9436bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhatstatic jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
953866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        jstring nameStr, jint w, jint h, jint format, jint flags) {
963866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    ScopedUtfChars name(env, nameStr);
973866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));
983866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    sp<SurfaceControl> surface = client->createSurface(
993866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            String8(name.c_str()), w, h, format, flags);
1003866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (surface == NULL) {
1013866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        jniThrowException(env, OutOfResourcesException, NULL);
1023866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        return 0;
1033866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
104b1d90c8f60f71422196c337f1d078b68867f5710Mathias Agopian    surface->incStrong((void *)nativeCreate);
10536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    return reinterpret_cast<jlong>(surface.get());
1063866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
1073866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
10836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhatstatic void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
1093866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
110b1d90c8f60f71422196c337f1d078b68867f5710Mathias Agopian    ctrl->decStrong((void *)nativeCreate);
1113866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
1123866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
11336bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhatstatic void nativeDestroy(JNIEnv* env, jclass clazz, jlong nativeObject) {
1143866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
1153866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    ctrl->clear();
116b1d90c8f60f71422196c337f1d078b68867f5710Mathias Agopian    ctrl->decStrong((void *)nativeCreate);
1173866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
1183866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
11947e36a3e270ff3e94750d730ac2a9f0bdfe96c04Chong Zhangstatic void nativeDisconnect(JNIEnv* env, jclass clazz, jlong nativeObject) {
12047e36a3e270ff3e94750d730ac2a9f0bdfe96c04Chong Zhang    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
12147e36a3e270ff3e94750d730ac2a9f0bdfe96c04Chong Zhang    if (ctrl != NULL) {
12247e36a3e270ff3e94750d730ac2a9f0bdfe96c04Chong Zhang        ctrl->disconnect();
12347e36a3e270ff3e94750d730ac2a9f0bdfe96c04Chong Zhang    }
12447e36a3e270ff3e94750d730ac2a9f0bdfe96c04Chong Zhang}
12547e36a3e270ff3e94750d730ac2a9f0bdfe96c04Chong Zhang
1269890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stozastatic jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz,
1279890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza        jobject displayTokenObj, jobject sourceCropObj, jint width, jint height,
1281d13406538b79785d2d0928689b8d5037e851ecfRiley Andrews        jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform,
1291d13406538b79785d2d0928689b8d5037e851ecfRiley Andrews        int rotation) {
1303866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
1313866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (displayToken == NULL) {
1323866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        return NULL;
1333866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
1343866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
1359890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza    int left = env->GetIntField(sourceCropObj, gRectClassInfo.left);
1369890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza    int top = env->GetIntField(sourceCropObj, gRectClassInfo.top);
1379890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza    int right = env->GetIntField(sourceCropObj, gRectClassInfo.right);
1389890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza    int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom);
1399890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza    Rect sourceCrop(left, top, right, bottom);
1409890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza
14160126efd7d905ca24822765c6dafac17fef278abBen Wagner    std::unique_ptr<ScreenshotClient> screenshot(new ScreenshotClient());
142b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger    status_t res;
1431d13406538b79785d2d0928689b8d5037e851ecfRiley Andrews    if (allLayers) {
1441d13406538b79785d2d0928689b8d5037e851ecfRiley Andrews        minLayer = 0;
1450f0b4919667f418b249c497f5ad3e83fdf4437e5Andreas Gampe        maxLayer = -1;
146b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger    }
1471d13406538b79785d2d0928689b8d5037e851ecfRiley Andrews
1481d13406538b79785d2d0928689b8d5037e851ecfRiley Andrews    res = screenshot->update(displayToken, sourceCrop, width, height,
1491d13406538b79785d2d0928689b8d5037e851ecfRiley Andrews        minLayer, maxLayer, useIdentityTransform, static_cast<uint32_t>(rotation));
150b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger    if (res != NO_ERROR) {
1513866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        return NULL;
1523866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
1533866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
154f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III    SkColorType colorType;
155f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III    SkAlphaType alphaType;
156b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger    switch (screenshot->getFormat()) {
157b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        case PIXEL_FORMAT_RGBX_8888: {
158f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III            colorType = kRGBA_8888_SkColorType;
159f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III            alphaType = kOpaque_SkAlphaType;
160b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger            break;
161b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        }
162b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        case PIXEL_FORMAT_RGBA_8888: {
163f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III            colorType = kRGBA_8888_SkColorType;
164f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III            alphaType = kPremul_SkAlphaType;
165b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger            break;
166b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        }
167b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        case PIXEL_FORMAT_RGB_565: {
168f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III            colorType = kRGB_565_SkColorType;
169f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III            alphaType = kOpaque_SkAlphaType;
170b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger            break;
171b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        }
172b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        default: {
173b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger            return NULL;
174b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger        }
1753866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
176f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III    SkImageInfo screenshotInfo = SkImageInfo::Make(screenshot->getWidth(),
177f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III                                                   screenshot->getHeight(),
178f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III                                                   colorType, alphaType);
1793866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
180f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    const size_t rowBytes =
181b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger            screenshot->getStride() * android::bytesPerPixel(screenshot->getFormat());
182b644a3b84521e2155a5af985a4d4ed305474e567Derek Sollenberger
183f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III    if (!screenshotInfo.width() || !screenshotInfo.height()) {
184f29ed28c7b878ef28058bc730715d0d32445bc57John Reck        return NULL;
1853866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
1863866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
187f29ed28c7b878ef28058bc730715d0d32445bc57John Reck    Bitmap* bitmap = new Bitmap(
188f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            (void*) screenshot->getPixels(), (void*) screenshot.get(), DeleteScreenshot,
189f29ed28c7b878ef28058bc730715d0d32445bc57John Reck            screenshotInfo, rowBytes, nullptr);
19060126efd7d905ca24822765c6dafac17fef278abBen Wagner    screenshot.release();
191ae2e8b4891491e8e89bed5f2c9626415adee09cbJohn Reck    bitmap->peekAtPixelRef()->setImmutable();
192f29ed28c7b878ef28058bc730715d0d32445bc57John Reck
1931abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik    return GraphicsJNI::createBitmap(env, bitmap,
1941abf5d62429e5a9329520b2f7c2b5a5e7a8e72ecChris Craik            GraphicsJNI::kBitmapCreateFlag_Premultiplied, NULL);
1953866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
1963866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
1979890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stozastatic void nativeScreenshot(JNIEnv* env, jclass clazz, jobject displayTokenObj,
1989890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza        jobject surfaceObj, jobject sourceCropObj, jint width, jint height,
1999890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza        jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform) {
2000449a40586b45ee05f760802020dc2f8a6e0506bMathias Agopian    sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
2010449a40586b45ee05f760802020dc2f8a6e0506bMathias Agopian    if (displayToken != NULL) {
2020449a40586b45ee05f760802020dc2f8a6e0506bMathias Agopian        sp<Surface> consumer = android_view_Surface_getSurface(env, surfaceObj);
2030449a40586b45ee05f760802020dc2f8a6e0506bMathias Agopian        if (consumer != NULL) {
2049890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza            int left = env->GetIntField(sourceCropObj, gRectClassInfo.left);
2059890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza            int top = env->GetIntField(sourceCropObj, gRectClassInfo.top);
2069890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza            int right = env->GetIntField(sourceCropObj, gRectClassInfo.right);
2079890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza            int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom);
2089890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza            Rect sourceCrop(left, top, right, bottom);
2099890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza
2100449a40586b45ee05f760802020dc2f8a6e0506bMathias Agopian            if (allLayers) {
2110449a40586b45ee05f760802020dc2f8a6e0506bMathias Agopian                minLayer = 0;
2120449a40586b45ee05f760802020dc2f8a6e0506bMathias Agopian                maxLayer = -1;
2130449a40586b45ee05f760802020dc2f8a6e0506bMathias Agopian            }
2149890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza            ScreenshotClient::capture(displayToken,
2159890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza                    consumer->getIGraphicBufferProducer(), sourceCrop,
21616ec12ae77fdd2b09ea0ea0885ac52f11bd32c59Dan Stoza                    width, height, uint32_t(minLayer), uint32_t(maxLayer),
21716ec12ae77fdd2b09ea0ea0885ac52f11bd32c59Dan Stoza                    useIdentityTransform);
2180449a40586b45ee05f760802020dc2f8a6e0506bMathias Agopian        }
2190449a40586b45ee05f760802020dc2f8a6e0506bMathias Agopian    }
2200449a40586b45ee05f760802020dc2f8a6e0506bMathias Agopian}
2210449a40586b45ee05f760802020dc2f8a6e0506bMathias Agopian
2223866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopianstatic void nativeOpenTransaction(JNIEnv* env, jclass clazz) {
2233866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SurfaceComposerClient::openGlobalTransaction();
2243866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
2253866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
226e9953b1752a583058e639ef3de456988ab6ef7bfRobert Carr
227e9953b1752a583058e639ef3de456988ab6ef7bfRobert Carrstatic void nativeCloseTransaction(JNIEnv* env, jclass clazz, jboolean sync) {
228e9953b1752a583058e639ef3de456988ab6ef7bfRobert Carr    SurfaceComposerClient::closeGlobalTransaction(sync);
2293866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
2303866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
2313866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopianstatic void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz) {
2323866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SurfaceComposerClient::setAnimationTransaction();
2333866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
2343866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
23536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhatstatic void nativeSetLayer(JNIEnv* env, jclass clazz, jlong nativeObject, jint zorder) {
2363866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
2373866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    status_t err = ctrl->setLayer(zorder);
2383866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (err < 0 && err != NO_INIT) {
2393866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        doThrowIAE(env);
2403866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
2413866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
2423866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
24336bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhatstatic void nativeSetPosition(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat x, jfloat y) {
2443866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
2453866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    status_t err = ctrl->setPosition(x, y);
2463866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (err < 0 && err != NO_INIT) {
2473866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        doThrowIAE(env);
2483866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
2493866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
2503866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
251a9408d4a4809dd229fb7fb8f9594cb6db4b1da64Robert Carrstatic void nativeSetPositionAppliesWithResize(JNIEnv* env, jclass clazz,
252a9408d4a4809dd229fb7fb8f9594cb6db4b1da64Robert Carr        jlong nativeObject) {
253a9408d4a4809dd229fb7fb8f9594cb6db4b1da64Robert Carr    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
254a9408d4a4809dd229fb7fb8f9594cb6db4b1da64Robert Carr    status_t err = ctrl->setPositionAppliesWithResize();
255a9408d4a4809dd229fb7fb8f9594cb6db4b1da64Robert Carr    if (err < 0 && err != NO_INIT) {
256a9408d4a4809dd229fb7fb8f9594cb6db4b1da64Robert Carr        doThrowIAE(env);
257a9408d4a4809dd229fb7fb8f9594cb6db4b1da64Robert Carr    }
258a9408d4a4809dd229fb7fb8f9594cb6db4b1da64Robert Carr}
259a9408d4a4809dd229fb7fb8f9594cb6db4b1da64Robert Carr
26036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhatstatic void nativeSetSize(JNIEnv* env, jclass clazz, jlong nativeObject, jint w, jint h) {
2613866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
2623866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    status_t err = ctrl->setSize(w, h);
2633866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (err < 0 && err != NO_INIT) {
2643866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        doThrowIAE(env);
2653866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
2663866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
2673866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
26836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhatstatic void nativeSetFlags(JNIEnv* env, jclass clazz, jlong nativeObject, jint flags, jint mask) {
2693866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
2703866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    status_t err = ctrl->setFlags(flags, mask);
2713866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (err < 0 && err != NO_INIT) {
2723866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        doThrowIAE(env);
2733866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
2743866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
2753866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
27636bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhatstatic void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jlong nativeObject, jobject regionObj) {
2773866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
2783866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj);
2793866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (!region) {
2803866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        doThrowIAE(env);
2813866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        return;
2823866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
2833866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
2843866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    const SkIRect& b(region->getBounds());
2853866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
2863866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (region->isComplex()) {
2873866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        SkRegion::Iterator it(*region);
2883866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        while (!it.done()) {
2893866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            const SkIRect& r(it.rect());
2903866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
2913866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            it.next();
2923866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        }
2933866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
2943866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
2953866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    status_t err = ctrl->setTransparentRegionHint(reg);
2963866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (err < 0 && err != NO_INIT) {
2973866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        doThrowIAE(env);
2983866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
2993866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
3003866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
30136bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhatstatic void nativeSetAlpha(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat alpha) {
3023866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
3033866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    status_t err = ctrl->setAlpha(alpha);
3043866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (err < 0 && err != NO_INIT) {
3053866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        doThrowIAE(env);
3063866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
3073866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
3083866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
30936bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhatstatic void nativeSetMatrix(JNIEnv* env, jclass clazz, jlong nativeObject,
3103866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy) {
3113866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
3123866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    status_t err = ctrl->setMatrix(dsdx, dtdx, dsdy, dtdy);
3133866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (err < 0 && err != NO_INIT) {
3143866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        doThrowIAE(env);
3153866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
3163866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
3173866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
31836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhatstatic void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong nativeObject,
3193866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        jint l, jint t, jint r, jint b) {
3203866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
3213866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    Rect crop(l, t, r, b);
3223866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    status_t err = ctrl->setCrop(crop);
3233866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (err < 0 && err != NO_INIT) {
3243866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        doThrowIAE(env);
3253866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
3263866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
3273866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
32827982e65eebdd8c23619e29c4d85a8b208e30200Pablo Ceballosstatic void nativeSetFinalCrop(JNIEnv* env, jclass clazz, jlong nativeObject,
32927982e65eebdd8c23619e29c4d85a8b208e30200Pablo Ceballos        jint l, jint t, jint r, jint b) {
33027982e65eebdd8c23619e29c4d85a8b208e30200Pablo Ceballos    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
33127982e65eebdd8c23619e29c4d85a8b208e30200Pablo Ceballos    Rect crop(l, t, r, b);
33227982e65eebdd8c23619e29c4d85a8b208e30200Pablo Ceballos    status_t err = ctrl->setFinalCrop(crop);
33327982e65eebdd8c23619e29c4d85a8b208e30200Pablo Ceballos    if (err < 0 && err != NO_INIT) {
33427982e65eebdd8c23619e29c4d85a8b208e30200Pablo Ceballos        doThrowIAE(env);
33527982e65eebdd8c23619e29c4d85a8b208e30200Pablo Ceballos    }
33627982e65eebdd8c23619e29c4d85a8b208e30200Pablo Ceballos}
33727982e65eebdd8c23619e29c4d85a8b208e30200Pablo Ceballos
33836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhatstatic void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong nativeObject, jint layerStack) {
3393866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
3403866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    status_t err = ctrl->setLayerStack(layerStack);
3413866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (err < 0 && err != NO_INIT) {
3423866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        doThrowIAE(env);
3433866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
3443866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
3453866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
3463866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopianstatic jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) {
3473866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id));
3483866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    return javaObjectForIBinder(env, token);
3493866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
3503866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
3513866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopianstatic jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj,
3523866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        jboolean secure) {
3533866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    ScopedUtfChars name(env, nameObj);
3543866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    sp<IBinder> token(SurfaceComposerClient::createDisplay(
3553866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            String8(name.c_str()), bool(secure)));
3563866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    return javaObjectForIBinder(env, token);
3573866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
3583866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
3596a6bc216d15293886ad088bb0ca99aadb7df85dcJesse Hallstatic void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
3606a6bc216d15293886ad088bb0ca99aadb7df85dcJesse Hall    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
3616a6bc216d15293886ad088bb0ca99aadb7df85dcJesse Hall    if (token == NULL) return;
3626a6bc216d15293886ad088bb0ca99aadb7df85dcJesse Hall    SurfaceComposerClient::destroyDisplay(token);
3636a6bc216d15293886ad088bb0ca99aadb7df85dcJesse Hall}
3646a6bc216d15293886ad088bb0ca99aadb7df85dcJesse Hall
3653866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopianstatic void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
36636bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat        jobject tokenObj, jlong nativeSurfaceObject) {
3673866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
3683866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (token == NULL) return;
369ffddc9b8045235a493ec506965ae4892601eb23dMathias Agopian    sp<IGraphicBufferProducer> bufferProducer;
3703866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
371ffddc9b8045235a493ec506965ae4892601eb23dMathias Agopian    if (sur != NULL) {
372ffddc9b8045235a493ec506965ae4892601eb23dMathias Agopian        bufferProducer = sur->getIGraphicBufferProducer();
373ffddc9b8045235a493ec506965ae4892601eb23dMathias Agopian    }
3743866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SurfaceComposerClient::setDisplaySurface(token, bufferProducer);
3753866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
3763866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
3773866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopianstatic void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
3783866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        jobject tokenObj, jint layerStack) {
3793866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
3803866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (token == NULL) return;
3813866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
3823866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SurfaceComposerClient::setDisplayLayerStack(token, layerStack);
3833866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
3843866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
3853866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopianstatic void nativeSetDisplayProjection(JNIEnv* env, jclass clazz,
3863866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        jobject tokenObj, jint orientation,
3873866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom,
3883866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian        jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) {
3893866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
3903866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (token == NULL) return;
3913866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom);
3923866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom);
3933866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    SurfaceComposerClient::setDisplayProjection(token, orientation, layerStackRect, displayRect);
3943866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
3953866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
39601e840ff9441e005153d799e71d65b38bcb21902Michael Wrightstatic void nativeSetDisplaySize(JNIEnv* env, jclass clazz,
39701e840ff9441e005153d799e71d65b38bcb21902Michael Wright        jobject tokenObj, jint width, jint height) {
39801e840ff9441e005153d799e71d65b38bcb21902Michael Wright    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
39901e840ff9441e005153d799e71d65b38bcb21902Michael Wright    if (token == NULL) return;
40001e840ff9441e005153d799e71d65b38bcb21902Michael Wright    SurfaceComposerClient::setDisplaySize(token, width, height);
40101e840ff9441e005153d799e71d65b38bcb21902Michael Wright}
40201e840ff9441e005153d799e71d65b38bcb21902Michael Wright
40300101052b5bf99a29acea3525864ed64b709c16cDan Stozastatic jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
40400101052b5bf99a29acea3525864ed64b709c16cDan Stoza        jobject tokenObj) {
4053866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
40600101052b5bf99a29acea3525864ed64b709c16cDan Stoza    if (token == NULL) return NULL;
4073866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
40800101052b5bf99a29acea3525864ed64b709c16cDan Stoza    Vector<DisplayInfo> configs;
40900101052b5bf99a29acea3525864ed64b709c16cDan Stoza    if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
41000101052b5bf99a29acea3525864ed64b709c16cDan Stoza            configs.size() == 0) {
41100101052b5bf99a29acea3525864ed64b709c16cDan Stoza        return NULL;
4123866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    }
4133866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
41400101052b5bf99a29acea3525864ed64b709c16cDan Stoza    jobjectArray configArray = env->NewObjectArray(configs.size(),
41500101052b5bf99a29acea3525864ed64b709c16cDan Stoza            gPhysicalDisplayInfoClassInfo.clazz, NULL);
41600101052b5bf99a29acea3525864ed64b709c16cDan Stoza
41700101052b5bf99a29acea3525864ed64b709c16cDan Stoza    for (size_t c = 0; c < configs.size(); ++c) {
41800101052b5bf99a29acea3525864ed64b709c16cDan Stoza        const DisplayInfo& info = configs[c];
41900101052b5bf99a29acea3525864ed64b709c16cDan Stoza        jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
42000101052b5bf99a29acea3525864ed64b709c16cDan Stoza                gPhysicalDisplayInfoClassInfo.ctor);
42100101052b5bf99a29acea3525864ed64b709c16cDan Stoza        env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
42200101052b5bf99a29acea3525864ed64b709c16cDan Stoza        env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
42300101052b5bf99a29acea3525864ed64b709c16cDan Stoza        env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
42400101052b5bf99a29acea3525864ed64b709c16cDan Stoza        env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
42500101052b5bf99a29acea3525864ed64b709c16cDan Stoza        env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
42600101052b5bf99a29acea3525864ed64b709c16cDan Stoza        env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
42700101052b5bf99a29acea3525864ed64b709c16cDan Stoza        env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
428e8b1aeb51e1e5da64f1d4fd40f2ee1e815886fe5Andy McFadden        env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos,
429e8b1aeb51e1e5da64f1d4fd40f2ee1e815886fe5Andy McFadden                info.appVsyncOffset);
430e8b1aeb51e1e5da64f1d4fd40f2ee1e815886fe5Andy McFadden        env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos,
431e8b1aeb51e1e5da64f1d4fd40f2ee1e815886fe5Andy McFadden                info.presentationDeadline);
432904f485bc6013060e81b627be61314b3d7fe5ef8Dan Stoza        env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.colorTransform,
433904f485bc6013060e81b627be61314b3d7fe5ef8Dan Stoza                info.colorTransform);
43400101052b5bf99a29acea3525864ed64b709c16cDan Stoza        env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
43500101052b5bf99a29acea3525864ed64b709c16cDan Stoza        env->DeleteLocalRef(infoObj);
43600101052b5bf99a29acea3525864ed64b709c16cDan Stoza    }
43700101052b5bf99a29acea3525864ed64b709c16cDan Stoza
43800101052b5bf99a29acea3525864ed64b709c16cDan Stoza    return configArray;
43900101052b5bf99a29acea3525864ed64b709c16cDan Stoza}
44000101052b5bf99a29acea3525864ed64b709c16cDan Stoza
44100101052b5bf99a29acea3525864ed64b709c16cDan Stozastatic jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
44200101052b5bf99a29acea3525864ed64b709c16cDan Stoza    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
44300101052b5bf99a29acea3525864ed64b709c16cDan Stoza    if (token == NULL) return -1;
44400101052b5bf99a29acea3525864ed64b709c16cDan Stoza    return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
44500101052b5bf99a29acea3525864ed64b709c16cDan Stoza}
44600101052b5bf99a29acea3525864ed64b709c16cDan Stoza
44700101052b5bf99a29acea3525864ed64b709c16cDan Stozastatic jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
44800101052b5bf99a29acea3525864ed64b709c16cDan Stoza    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
44900101052b5bf99a29acea3525864ed64b709c16cDan Stoza    if (token == NULL) return JNI_FALSE;
45000101052b5bf99a29acea3525864ed64b709c16cDan Stoza    status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
45100101052b5bf99a29acea3525864ed64b709c16cDan Stoza    return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
4523866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
4533866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
454c55929a2a5686fe456b19cd54a73b8bde2a4332bPrashant Malanistatic void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
4553866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
4563866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    if (token == NULL) return;
4573866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
458c55929a2a5686fe456b19cd54a73b8bde2a4332bPrashant Malani    ALOGD_IF_SLOW(100, "Excessive delay in setPowerMode()");
459c55929a2a5686fe456b19cd54a73b8bde2a4332bPrashant Malani    SurfaceComposerClient::setDisplayPowerMode(token, mode);
4603866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian}
4613866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
4621376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslavstatic jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
4631376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
4641376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    status_t err = ctrl->clearLayerFrameStats();
4651376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
4661376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    if (err < 0 && err != NO_INIT) {
4671376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        doThrowIAE(env);
4681376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
4691376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
4701376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    // The other end is not ready, just report we failed.
4711376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    if (err == NO_INIT) {
4721376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        return JNI_FALSE;
4731376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
4741376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
4751376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    return JNI_TRUE;
4761376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav}
4771376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
4781376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslavstatic jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
4791376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    jobject outStats) {
4801376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    FrameStats stats;
4811376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
4821376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
4831376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    status_t err = ctrl->getLayerFrameStats(&stats);
4841376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    if (err < 0 && err != NO_INIT) {
4851376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        doThrowIAE(env);
4861376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
4871376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
4881376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    // The other end is not ready, fine just return empty stats.
4891376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    if (err == NO_INIT) {
4901376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        return JNI_FALSE;
4911376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
4921376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
4931376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
4941376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    size_t frameCount = stats.desiredPresentTimesNano.size();
4951376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
4961376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
4971376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    if (postedTimesNanoDst == NULL) {
4981376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        return JNI_FALSE;
4991376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
5001376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5011376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
5021376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    if (presentedTimesNanoDst == NULL) {
5031376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        return JNI_FALSE;
5041376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
5051376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5061376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
5071376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    if (readyTimesNanoDst == NULL) {
5081376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        return JNI_FALSE;
5091376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
5101376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5111376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    nsecs_t postedTimesNanoSrc[frameCount];
5121376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    nsecs_t presentedTimesNanoSrc[frameCount];
5131376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    nsecs_t readyTimesNanoSrc[frameCount];
5141376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5151376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    for (size_t i = 0; i < frameCount; i++) {
5161376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
5171376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        if (postedTimeNano == INT64_MAX) {
5181376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav            postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
5191376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        }
5201376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        postedTimesNanoSrc[i] = postedTimeNano;
5211376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5221376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
5231376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        if (presentedTimeNano == INT64_MAX) {
5241376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav            presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
5251376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        }
5261376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        presentedTimesNanoSrc[i] = presentedTimeNano;
5271376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5281376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
5291376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        if (readyTimeNano == INT64_MAX) {
5301376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav            readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
5311376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        }
5321376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        readyTimesNanoSrc[i] = readyTimeNano;
5331376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
5341376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5351376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
5361376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
5371376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
5381376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5391376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
5401376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav            postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
5411376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5421376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    if (env->ExceptionCheck()) {
5431376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        return JNI_FALSE;
5441376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
5451376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5461376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    return JNI_TRUE;
5471376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav}
5481376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5491376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslavstatic jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
5501376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    status_t err = SurfaceComposerClient::clearAnimationFrameStats();
5511376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5521376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    if (err < 0 && err != NO_INIT) {
5531376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        doThrowIAE(env);
5541376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
5551376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5561376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    // The other end is not ready, just report we failed.
5571376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    if (err == NO_INIT) {
5581376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        return JNI_FALSE;
5591376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
5601376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5611376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    return JNI_TRUE;
5621376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav}
5631376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5641376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslavstatic jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
5651376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    FrameStats stats;
5661376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5671376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
5681376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    if (err < 0 && err != NO_INIT) {
5691376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        doThrowIAE(env);
5701376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
5711376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5721376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    // The other end is not ready, fine just return empty stats.
5731376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    if (err == NO_INIT) {
5741376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        return JNI_FALSE;
5751376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
5761376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5771376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
5781376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    size_t frameCount = stats.desiredPresentTimesNano.size();
5791376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5801376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
5811376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    if (presentedTimesNanoDst == NULL) {
5821376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        return JNI_FALSE;
5831376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
5841376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5851376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    nsecs_t presentedTimesNanoSrc[frameCount];
5861376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5871376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    for (size_t i = 0; i < frameCount; i++) {
588ac5eda366fafce6ec9fd89b14e31f4aaaf08f762Allen Hair        nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
5891376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        if (presentedTimeNano == INT64_MAX) {
5901376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav            presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
5911376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        }
5921376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        presentedTimesNanoSrc[i] = presentedTimeNano;
5931376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
5941376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5951376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
5961376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
5971376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
5981376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav            presentedTimesNanoDst);
5991376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
6001376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    if (env->ExceptionCheck()) {
6011376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav        return JNI_FALSE;
6021376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    }
6031376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
6041376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav    return JNI_TRUE;
6051376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav}
6061376d600d8e0eefdbc0aa11d398cf7517fc77129Svetoslav
60764e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carr
60864e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carrstatic void nativeDeferTransactionUntil(JNIEnv* env, jclass clazz, jlong nativeObject,
60964e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carr        jobject handleObject, jlong frameNumber) {
61064e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carr    auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
61164e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carr    sp<IBinder> handle = ibinderForJavaObject(env, handleObject);
61264e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carr
61364e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carr    ctrl->deferTransactionUntil(handle, frameNumber);
61464e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carr}
61564e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carr
6161ca6a33f36357281b3445e85d9e67cacd1a12edeRobert Carrstatic void nativeSetOverrideScalingMode(JNIEnv* env, jclass clazz, jlong nativeObject,
6171ca6a33f36357281b3445e85d9e67cacd1a12edeRobert Carr        jint scalingMode) {
6181ca6a33f36357281b3445e85d9e67cacd1a12edeRobert Carr    auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
6191ca6a33f36357281b3445e85d9e67cacd1a12edeRobert Carr
6201ca6a33f36357281b3445e85d9e67cacd1a12edeRobert Carr    ctrl->setOverrideScalingMode(scalingMode);
6211ca6a33f36357281b3445e85d9e67cacd1a12edeRobert Carr}
6221ca6a33f36357281b3445e85d9e67cacd1a12edeRobert Carr
62364e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carrstatic jobject nativeGetHandle(JNIEnv* env, jclass clazz, jlong nativeObject) {
62464e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carr    auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
62564e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carr
62664e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carr    return javaObjectForIBinder(env, ctrl->getHandle());
62764e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carr}
62864e516f5fa56d71bf6b5076e5620c30dc5810ba1Rob Carr
62954ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuangstatic jobject nativeGetHdrCapabilities(JNIEnv* env, jclass clazz, jobject tokenObject) {
63054ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang    sp<IBinder> token(ibinderForJavaObject(env, tokenObject));
63154ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang    if (token == NULL) return NULL;
63254ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang
63354ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang    HdrCapabilities capabilities;
63454ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang    SurfaceComposerClient::getHdrCapabilities(token, &capabilities);
63554ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang
63654ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang    const auto& types = capabilities.getSupportedHdrTypes();
63754ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang    auto typesArray = env->NewIntArray(types.size());
63854ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang    env->SetIntArrayRegion(typesArray, 0, types.size(), types.data());
63954ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang
6409ff94c0251722c44eece7c3561b4ed36b286d4a8Michael Wright    return env->NewObject(gHdrCapabilitiesClassInfo.clazz, gHdrCapabilitiesClassInfo.ctor,
64154ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang            typesArray, capabilities.getDesiredMaxLuminance(),
64254ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang            capabilities.getDesiredMaxAverageLuminance(), capabilities.getDesiredMinLuminance());
64354ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang}
64454ac21918481fe6f7aac1c0effde51f9e9860ae3Hangyu Kuang
6453866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian// ----------------------------------------------------------------------------
6463866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian
64776f6a86de25e1bf74717e047e55fd44b089673f3Daniel Micaystatic const JNINativeMethod sSurfaceControlMethods[] = {
64836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J",
6493866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            (void*)nativeCreate },
65036bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    {"nativeRelease", "(J)V",
6513866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            (void*)nativeRelease },
65236bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    {"nativeDestroy", "(J)V",
6533866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            (void*)nativeDestroy },
65447e36a3e270ff3e94750d730ac2a9f0bdfe96c04Chong Zhang    {"nativeDisconnect", "(J)V",
65547e36a3e270ff3e94750d730ac2a9f0bdfe96c04Chong Zhang            (void*)nativeDisconnect },
6561d13406538b79785d2d0928689b8d5037e851ecfRiley Andrews    {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZI)Landroid/graphics/Bitmap;",
6570449a40586b45ee05f760802020dc2f8a6e0506bMathias Agopian            (void*)nativeScreenshotBitmap },
6589890e341bf9d565309cd7db5e6c4194c66c0a1d8Dan Stoza    {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;Landroid/graphics/Rect;IIIIZZ)V",
6593866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            (void*)nativeScreenshot },
6603866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    {"nativeOpenTransaction", "()V",
6613866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            (void*)nativeOpenTransaction },
662e9953b1752a583058e639ef3de456988ab6ef7bfRobert Carr    {"nativeCloseTransaction", "(Z)V",
6633866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            (void*)nativeCloseTransaction },
6643866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian    {"nativeSetAnimationTransaction", "()V",
6653866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            (void*)nativeSetAnimationTransaction },
66636bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    {"nativeSetLayer", "(JI)V",
6673866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            (void*)nativeSetLayer },
66836bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    {"nativeSetPosition", "(JFF)V",
6693866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            (void*)nativeSetPosition },
670a9408d4a4809dd229fb7fb8f9594cb6db4b1da64Robert Carr    {"nativeSetPositionAppliesWithResize", "(J)V",
671a9408d4a4809dd229fb7fb8f9594cb6db4b1da64Robert Carr            (void*)nativeSetPositionAppliesWithResize },
67236bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat    {"nativeSetSize", "(JII)V",
6733866f0d581ceaa165710feeee9f37fe1b0d7067dMathias Agopian            (void*)nativeSetSize },
674