android_view_SurfaceControl.cpp revision 9ff94c0251722c44eece7c3561b4ed36b286d4a8
19cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien/*
29cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * Copyright (C) 2013 The Android Open Source Project
39cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien *
49cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * Licensed under the Apache License, Version 2.0 (the "License");
59cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * you may not use this file except in compliance with the License.
69cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * You may obtain a copy of the License at
79cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien *
89cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien *      http://www.apache.org/licenses/LICENSE-2.0
99cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien *
109cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * Unless required by applicable law or agreed to in writing, software
119cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * distributed under the License is distributed on an "AS IS" BASIS,
129cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * See the License for the specific language governing permissions and
149cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien * limitations under the License.
159cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien */
169cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
17ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien#define LOG_TAG "SurfaceControl"
18ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
19ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien#include "android_os_Parcel.h"
20ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien#include "android_util_Binder.h"
219cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include "android/graphics/Bitmap.h"
22c31e3883456e018d742e9f29815ba5ff8b315ea1Raph Levien#include "android/graphics/GraphicsJNI.h"
239cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include "android/graphics/Region.h"
249cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include "core_jni_helpers.h"
259cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
269cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <JNIHelp.h>
279cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <ScopedUtfChars.h>
289cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <android_runtime/android_view_Surface.h>
299cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <android_runtime/android_view_SurfaceSession.h>
309cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <gui/Surface.h>
319cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <gui/SurfaceComposerClient.h>
329cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <jni.h>
339cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <memory>
349cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <stdio.h>
359cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <ui/DisplayInfo.h>
369cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <ui/HdrCapabilities.h>
37c31e3883456e018d742e9f29815ba5ff8b315ea1Raph Levien#include <ui/FrameStats.h>
389cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <ui/Rect.h>
399cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <ui/Region.h>
409cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien#include <utils/Log.h>
41ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
429cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien// ----------------------------------------------------------------------------
439cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
449cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Leviennamespace android {
459cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
46b80c1f19c58b927820a8a24bf2218e5645724608Raph Levienstatic const char* const OutOfResourcesException =
479cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    "android/view/Surface$OutOfResourcesException";
489cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
499cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienstatic struct {
509cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    jclass clazz;
519cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    jmethodID ctor;
52bcc3dc5a2591a95a57e379e27cbad69c18e91e67Raph Levien    jfieldID width;
539cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    jfieldID height;
54ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien    jfieldID refreshRate;
559cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    jfieldID density;
56bcc3dc5a2591a95a57e379e27cbad69c18e91e67Raph Levien    jfieldID xDpi;
57bcc3dc5a2591a95a57e379e27cbad69c18e91e67Raph Levien    jfieldID yDpi;
58bcc3dc5a2591a95a57e379e27cbad69c18e91e67Raph Levien    jfieldID secure;
599cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    jfieldID appVsyncOffsetNanos;
60bcc3dc5a2591a95a57e379e27cbad69c18e91e67Raph Levien    jfieldID presentationDeadlineNanos;
619cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    jfieldID colorTransform;
629cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien} gPhysicalDisplayInfoClassInfo;
63ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
649cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienstatic struct {
659cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    jfieldID bottom;
669cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    jfieldID left;
679cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    jfieldID right;
689cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    jfieldID top;
6972fe9422c869b7878240a23e4650d9d90edb1c2aRaph Levien} gRectClassInfo;
709cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
719cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien// Implements SkMallocPixelRef::ReleaseProc, to delete the screenshot on unref.
729cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienvoid DeleteScreenshot(void* addr, void* context) {
739cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    SkASSERT(addr == ((ScreenshotClient*) context)->getPixels());
749cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    delete ((ScreenshotClient*) context);
759cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}
76ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
779cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienstatic struct {
789cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    nsecs_t UNDEFINED_TIME_NANO;
799cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    jmethodID init;
809cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien} gWindowContentFrameStatsClassInfo;
819cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
829cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienstatic struct {
839cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    nsecs_t UNDEFINED_TIME_NANO;
849cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    jmethodID init;
859cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien} gWindowAnimationFrameStatsClassInfo;
86ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levien
879cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienstatic struct {
889cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    jclass clazz;
899cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    jmethodID ctor;
909cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien} gHdrCapabilitiesClassInfo;
919cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
929cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien// ----------------------------------------------------------------------------
939cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
949cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienstatic jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
959cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        jstring nameStr, jint w, jint h, jint format, jint flags) {
969cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    ScopedUtfChars name(env, nameStr);
979cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));
98b80c1f19c58b927820a8a24bf2218e5645724608Raph Levien    sp<SurfaceControl> surface = client->createSurface(
999cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien            String8(name.c_str()), w, h, format, flags);
1009cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    if (surface == NULL) {
1019cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        jniThrowException(env, OutOfResourcesException, NULL);
1029cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        return 0;
1039cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    }
1049cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    surface->incStrong((void *)nativeCreate);
1059cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    return reinterpret_cast<jlong>(surface.get());
1069cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}
1079cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
108ecc2d34ac23a497988f21e5f415b53c007b9d8c5Raph Levienstatic void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
1099cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
1109cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    ctrl->decStrong((void *)nativeCreate);
1119cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}
1129cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
1139cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienstatic void nativeDestroy(JNIEnv* env, jclass clazz, jlong nativeObject) {
1149cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
1159cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    ctrl->clear();
1169cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    ctrl->decStrong((void *)nativeCreate);
1179cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}
1189cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
1199cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienstatic void nativeDisconnect(JNIEnv* env, jclass clazz, jlong nativeObject) {
1209cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
1219cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    if (ctrl != NULL) {
1229cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        ctrl->disconnect();
1239cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    }
1249cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien}
1259cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
1269cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levienstatic jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz,
1279cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        jobject displayTokenObj, jobject sourceCropObj, jint width, jint height,
1289cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform,
1299cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        int rotation) {
1309cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
1319cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    if (displayToken == NULL) {
1329cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        return NULL;
1339cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    }
1349cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
1359cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    int left = env->GetIntField(sourceCropObj, gRectClassInfo.left);
1369cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    int top = env->GetIntField(sourceCropObj, gRectClassInfo.top);
1379cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    int right = env->GetIntField(sourceCropObj, gRectClassInfo.right);
1389cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom);
1399cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    Rect sourceCrop(left, top, right, bottom);
1409cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
1419cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    std::unique_ptr<ScreenshotClient> screenshot(new ScreenshotClient());
1429cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    status_t res;
143b80c1f19c58b927820a8a24bf2218e5645724608Raph Levien    if (allLayers) {
1449cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        minLayer = 0;
1459cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        maxLayer = -1;
1469cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    }
1479cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien
1489cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    res = screenshot->update(displayToken, sourceCrop, width, height,
1499cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        minLayer, maxLayer, useIdentityTransform, static_cast<uint32_t>(rotation));
1509cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    if (res != NO_ERROR) {
1519cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien        return NULL;
1529cc9bbe1461f359f0b27c5e7645c17dda001ab1dRaph Levien    }
153
154    SkColorType colorType;
155    SkAlphaType alphaType;
156    switch (screenshot->getFormat()) {
157        case PIXEL_FORMAT_RGBX_8888: {
158            colorType = kRGBA_8888_SkColorType;
159            alphaType = kOpaque_SkAlphaType;
160            break;
161        }
162        case PIXEL_FORMAT_RGBA_8888: {
163            colorType = kRGBA_8888_SkColorType;
164            alphaType = kPremul_SkAlphaType;
165            break;
166        }
167        case PIXEL_FORMAT_RGB_565: {
168            colorType = kRGB_565_SkColorType;
169            alphaType = kOpaque_SkAlphaType;
170            break;
171        }
172        default: {
173            return NULL;
174        }
175    }
176    SkImageInfo screenshotInfo = SkImageInfo::Make(screenshot->getWidth(),
177                                                   screenshot->getHeight(),
178                                                   colorType, alphaType);
179
180    const size_t rowBytes =
181            screenshot->getStride() * android::bytesPerPixel(screenshot->getFormat());
182
183    if (!screenshotInfo.width() || !screenshotInfo.height()) {
184        return NULL;
185    }
186
187    Bitmap* bitmap = new Bitmap(
188            (void*) screenshot->getPixels(), (void*) screenshot.get(), DeleteScreenshot,
189            screenshotInfo, rowBytes, nullptr);
190    screenshot.release();
191    bitmap->peekAtPixelRef()->setImmutable();
192
193    return GraphicsJNI::createBitmap(env, bitmap,
194            GraphicsJNI::kBitmapCreateFlag_Premultiplied, NULL);
195}
196
197static void nativeScreenshot(JNIEnv* env, jclass clazz, jobject displayTokenObj,
198        jobject surfaceObj, jobject sourceCropObj, jint width, jint height,
199        jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform) {
200    sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
201    if (displayToken != NULL) {
202        sp<Surface> consumer = android_view_Surface_getSurface(env, surfaceObj);
203        if (consumer != NULL) {
204            int left = env->GetIntField(sourceCropObj, gRectClassInfo.left);
205            int top = env->GetIntField(sourceCropObj, gRectClassInfo.top);
206            int right = env->GetIntField(sourceCropObj, gRectClassInfo.right);
207            int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom);
208            Rect sourceCrop(left, top, right, bottom);
209
210            if (allLayers) {
211                minLayer = 0;
212                maxLayer = -1;
213            }
214            ScreenshotClient::capture(displayToken,
215                    consumer->getIGraphicBufferProducer(), sourceCrop,
216                    width, height, uint32_t(minLayer), uint32_t(maxLayer),
217                    useIdentityTransform);
218        }
219    }
220}
221
222static void nativeOpenTransaction(JNIEnv* env, jclass clazz) {
223    SurfaceComposerClient::openGlobalTransaction();
224}
225
226static void nativeCloseTransaction(JNIEnv* env, jclass clazz) {
227    SurfaceComposerClient::closeGlobalTransaction();
228}
229
230static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz) {
231    SurfaceComposerClient::setAnimationTransaction();
232}
233
234static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong nativeObject, jint zorder) {
235    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
236    status_t err = ctrl->setLayer(zorder);
237    if (err < 0 && err != NO_INIT) {
238        doThrowIAE(env);
239    }
240}
241
242static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat x, jfloat y) {
243    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
244    status_t err = ctrl->setPosition(x, y);
245    if (err < 0 && err != NO_INIT) {
246        doThrowIAE(env);
247    }
248}
249
250static void nativeSetSize(JNIEnv* env, jclass clazz, jlong nativeObject, jint w, jint h) {
251    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
252    status_t err = ctrl->setSize(w, h);
253    if (err < 0 && err != NO_INIT) {
254        doThrowIAE(env);
255    }
256}
257
258static void nativeSetFlags(JNIEnv* env, jclass clazz, jlong nativeObject, jint flags, jint mask) {
259    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
260    status_t err = ctrl->setFlags(flags, mask);
261    if (err < 0 && err != NO_INIT) {
262        doThrowIAE(env);
263    }
264}
265
266static void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jlong nativeObject, jobject regionObj) {
267    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
268    SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj);
269    if (!region) {
270        doThrowIAE(env);
271        return;
272    }
273
274    const SkIRect& b(region->getBounds());
275    Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
276    if (region->isComplex()) {
277        SkRegion::Iterator it(*region);
278        while (!it.done()) {
279            const SkIRect& r(it.rect());
280            reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
281            it.next();
282        }
283    }
284
285    status_t err = ctrl->setTransparentRegionHint(reg);
286    if (err < 0 && err != NO_INIT) {
287        doThrowIAE(env);
288    }
289}
290
291static void nativeSetAlpha(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat alpha) {
292    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
293    status_t err = ctrl->setAlpha(alpha);
294    if (err < 0 && err != NO_INIT) {
295        doThrowIAE(env);
296    }
297}
298
299static void nativeSetMatrix(JNIEnv* env, jclass clazz, jlong nativeObject,
300        jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy) {
301    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
302    status_t err = ctrl->setMatrix(dsdx, dtdx, dsdy, dtdy);
303    if (err < 0 && err != NO_INIT) {
304        doThrowIAE(env);
305    }
306}
307
308static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong nativeObject,
309        jint l, jint t, jint r, jint b) {
310    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
311    Rect crop(l, t, r, b);
312    status_t err = ctrl->setCrop(crop);
313    if (err < 0 && err != NO_INIT) {
314        doThrowIAE(env);
315    }
316}
317
318static void nativeSetFinalCrop(JNIEnv* env, jclass clazz, jlong nativeObject,
319        jint l, jint t, jint r, jint b) {
320    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
321    Rect crop(l, t, r, b);
322    status_t err = ctrl->setFinalCrop(crop);
323    if (err < 0 && err != NO_INIT) {
324        doThrowIAE(env);
325    }
326}
327
328static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong nativeObject, jint layerStack) {
329    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
330    status_t err = ctrl->setLayerStack(layerStack);
331    if (err < 0 && err != NO_INIT) {
332        doThrowIAE(env);
333    }
334}
335
336static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) {
337    sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id));
338    return javaObjectForIBinder(env, token);
339}
340
341static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj,
342        jboolean secure) {
343    ScopedUtfChars name(env, nameObj);
344    sp<IBinder> token(SurfaceComposerClient::createDisplay(
345            String8(name.c_str()), bool(secure)));
346    return javaObjectForIBinder(env, token);
347}
348
349static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
350    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
351    if (token == NULL) return;
352    SurfaceComposerClient::destroyDisplay(token);
353}
354
355static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
356        jobject tokenObj, jlong nativeSurfaceObject) {
357    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
358    if (token == NULL) return;
359    sp<IGraphicBufferProducer> bufferProducer;
360    sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
361    if (sur != NULL) {
362        bufferProducer = sur->getIGraphicBufferProducer();
363    }
364    SurfaceComposerClient::setDisplaySurface(token, bufferProducer);
365}
366
367static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
368        jobject tokenObj, jint layerStack) {
369    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
370    if (token == NULL) return;
371
372    SurfaceComposerClient::setDisplayLayerStack(token, layerStack);
373}
374
375static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz,
376        jobject tokenObj, jint orientation,
377        jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom,
378        jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) {
379    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
380    if (token == NULL) return;
381    Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom);
382    Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom);
383    SurfaceComposerClient::setDisplayProjection(token, orientation, layerStackRect, displayRect);
384}
385
386static void nativeSetDisplaySize(JNIEnv* env, jclass clazz,
387        jobject tokenObj, jint width, jint height) {
388    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
389    if (token == NULL) return;
390    SurfaceComposerClient::setDisplaySize(token, width, height);
391}
392
393static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
394        jobject tokenObj) {
395    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
396    if (token == NULL) return NULL;
397
398    Vector<DisplayInfo> configs;
399    if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
400            configs.size() == 0) {
401        return NULL;
402    }
403
404    jobjectArray configArray = env->NewObjectArray(configs.size(),
405            gPhysicalDisplayInfoClassInfo.clazz, NULL);
406
407    for (size_t c = 0; c < configs.size(); ++c) {
408        const DisplayInfo& info = configs[c];
409        jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
410                gPhysicalDisplayInfoClassInfo.ctor);
411        env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
412        env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
413        env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
414        env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
415        env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
416        env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
417        env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
418        env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos,
419                info.appVsyncOffset);
420        env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos,
421                info.presentationDeadline);
422        env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.colorTransform,
423                info.colorTransform);
424        env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
425        env->DeleteLocalRef(infoObj);
426    }
427
428    return configArray;
429}
430
431static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
432    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
433    if (token == NULL) return -1;
434    return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
435}
436
437static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
438    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
439    if (token == NULL) return JNI_FALSE;
440    status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
441    return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
442}
443
444static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
445    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
446    if (token == NULL) return;
447
448    ALOGD_IF_SLOW(100, "Excessive delay in setPowerMode()");
449    SurfaceComposerClient::setDisplayPowerMode(token, mode);
450}
451
452static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
453    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
454    status_t err = ctrl->clearLayerFrameStats();
455
456    if (err < 0 && err != NO_INIT) {
457        doThrowIAE(env);
458    }
459
460    // The other end is not ready, just report we failed.
461    if (err == NO_INIT) {
462        return JNI_FALSE;
463    }
464
465    return JNI_TRUE;
466}
467
468static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
469    jobject outStats) {
470    FrameStats stats;
471
472    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
473    status_t err = ctrl->getLayerFrameStats(&stats);
474    if (err < 0 && err != NO_INIT) {
475        doThrowIAE(env);
476    }
477
478    // The other end is not ready, fine just return empty stats.
479    if (err == NO_INIT) {
480        return JNI_FALSE;
481    }
482
483    jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
484    size_t frameCount = stats.desiredPresentTimesNano.size();
485
486    jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
487    if (postedTimesNanoDst == NULL) {
488        return JNI_FALSE;
489    }
490
491    jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
492    if (presentedTimesNanoDst == NULL) {
493        return JNI_FALSE;
494    }
495
496    jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
497    if (readyTimesNanoDst == NULL) {
498        return JNI_FALSE;
499    }
500
501    nsecs_t postedTimesNanoSrc[frameCount];
502    nsecs_t presentedTimesNanoSrc[frameCount];
503    nsecs_t readyTimesNanoSrc[frameCount];
504
505    for (size_t i = 0; i < frameCount; i++) {
506        nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
507        if (postedTimeNano == INT64_MAX) {
508            postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
509        }
510        postedTimesNanoSrc[i] = postedTimeNano;
511
512        nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
513        if (presentedTimeNano == INT64_MAX) {
514            presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
515        }
516        presentedTimesNanoSrc[i] = presentedTimeNano;
517
518        nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
519        if (readyTimeNano == INT64_MAX) {
520            readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
521        }
522        readyTimesNanoSrc[i] = readyTimeNano;
523    }
524
525    env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
526    env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
527    env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
528
529    env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
530            postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
531
532    if (env->ExceptionCheck()) {
533        return JNI_FALSE;
534    }
535
536    return JNI_TRUE;
537}
538
539static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
540    status_t err = SurfaceComposerClient::clearAnimationFrameStats();
541
542    if (err < 0 && err != NO_INIT) {
543        doThrowIAE(env);
544    }
545
546    // The other end is not ready, just report we failed.
547    if (err == NO_INIT) {
548        return JNI_FALSE;
549    }
550
551    return JNI_TRUE;
552}
553
554static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
555    FrameStats stats;
556
557    status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
558    if (err < 0 && err != NO_INIT) {
559        doThrowIAE(env);
560    }
561
562    // The other end is not ready, fine just return empty stats.
563    if (err == NO_INIT) {
564        return JNI_FALSE;
565    }
566
567    jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
568    size_t frameCount = stats.desiredPresentTimesNano.size();
569
570    jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
571    if (presentedTimesNanoDst == NULL) {
572        return JNI_FALSE;
573    }
574
575    nsecs_t presentedTimesNanoSrc[frameCount];
576
577    for (size_t i = 0; i < frameCount; i++) {
578        nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
579        if (presentedTimeNano == INT64_MAX) {
580            presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
581        }
582        presentedTimesNanoSrc[i] = presentedTimeNano;
583    }
584
585    env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
586
587    env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
588            presentedTimesNanoDst);
589
590    if (env->ExceptionCheck()) {
591        return JNI_FALSE;
592    }
593
594    return JNI_TRUE;
595}
596
597
598static void nativeDeferTransactionUntil(JNIEnv* env, jclass clazz, jlong nativeObject,
599        jobject handleObject, jlong frameNumber) {
600    auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
601    sp<IBinder> handle = ibinderForJavaObject(env, handleObject);
602
603    ctrl->deferTransactionUntil(handle, frameNumber);
604}
605
606static void nativeSetOverrideScalingMode(JNIEnv* env, jclass clazz, jlong nativeObject,
607        jint scalingMode) {
608    auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
609
610    ctrl->setOverrideScalingMode(scalingMode);
611}
612
613static jobject nativeGetHandle(JNIEnv* env, jclass clazz, jlong nativeObject) {
614    auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
615
616    return javaObjectForIBinder(env, ctrl->getHandle());
617}
618
619static jobject nativeGetHdrCapabilities(JNIEnv* env, jclass clazz, jobject tokenObject) {
620    sp<IBinder> token(ibinderForJavaObject(env, tokenObject));
621    if (token == NULL) return NULL;
622
623    HdrCapabilities capabilities;
624    SurfaceComposerClient::getHdrCapabilities(token, &capabilities);
625
626    const auto& types = capabilities.getSupportedHdrTypes();
627    auto typesArray = env->NewIntArray(types.size());
628    env->SetIntArrayRegion(typesArray, 0, types.size(), types.data());
629
630    return env->NewObject(gHdrCapabilitiesClassInfo.clazz, gHdrCapabilitiesClassInfo.ctor,
631            typesArray, capabilities.getDesiredMaxLuminance(),
632            capabilities.getDesiredMaxAverageLuminance(), capabilities.getDesiredMinLuminance());
633}
634
635// ----------------------------------------------------------------------------
636
637static const JNINativeMethod sSurfaceControlMethods[] = {
638    {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J",
639            (void*)nativeCreate },
640    {"nativeRelease", "(J)V",
641            (void*)nativeRelease },
642    {"nativeDestroy", "(J)V",
643            (void*)nativeDestroy },
644    {"nativeDisconnect", "(J)V",
645            (void*)nativeDisconnect },
646    {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZI)Landroid/graphics/Bitmap;",
647            (void*)nativeScreenshotBitmap },
648    {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;Landroid/graphics/Rect;IIIIZZ)V",
649            (void*)nativeScreenshot },
650    {"nativeOpenTransaction", "()V",
651            (void*)nativeOpenTransaction },
652    {"nativeCloseTransaction", "()V",
653            (void*)nativeCloseTransaction },
654    {"nativeSetAnimationTransaction", "()V",
655            (void*)nativeSetAnimationTransaction },
656    {"nativeSetLayer", "(JI)V",
657            (void*)nativeSetLayer },
658    {"nativeSetPosition", "(JFF)V",
659            (void*)nativeSetPosition },
660    {"nativeSetSize", "(JII)V",
661            (void*)nativeSetSize },
662    {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V",
663            (void*)nativeSetTransparentRegionHint },
664    {"nativeSetAlpha", "(JF)V",
665            (void*)nativeSetAlpha },
666    {"nativeSetMatrix", "(JFFFF)V",
667            (void*)nativeSetMatrix },
668    {"nativeSetFlags", "(JII)V",
669            (void*)nativeSetFlags },
670    {"nativeSetWindowCrop", "(JIIII)V",
671            (void*)nativeSetWindowCrop },
672    {"nativeSetFinalCrop", "(JIIII)V",
673            (void*)nativeSetFinalCrop },
674    {"nativeSetLayerStack", "(JI)V",
675            (void*)nativeSetLayerStack },
676    {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
677            (void*)nativeGetBuiltInDisplay },
678    {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
679            (void*)nativeCreateDisplay },
680    {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
681            (void*)nativeDestroyDisplay },
682    {"nativeSetDisplaySurface", "(Landroid/os/IBinder;J)V",
683            (void*)nativeSetDisplaySurface },
684    {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V",
685            (void*)nativeSetDisplayLayerStack },
686    {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V",
687            (void*)nativeSetDisplayProjection },
688    {"nativeSetDisplaySize", "(Landroid/os/IBinder;II)V",
689            (void*)nativeSetDisplaySize },
690    {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
691            (void*)nativeGetDisplayConfigs },
692    {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
693            (void*)nativeGetActiveConfig },
694    {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
695            (void*)nativeSetActiveConfig },
696    {"nativeGetHdrCapabilities", "(Landroid/os/IBinder;)Landroid/view/Display$HdrCapabilities;",
697            (void*)nativeGetHdrCapabilities },
698    {"nativeClearContentFrameStats", "(J)Z",
699            (void*)nativeClearContentFrameStats },
700    {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
701            (void*)nativeGetContentFrameStats },
702    {"nativeClearAnimationFrameStats", "()Z",
703            (void*)nativeClearAnimationFrameStats },
704    {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
705            (void*)nativeGetAnimationFrameStats },
706    {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
707            (void*)nativeSetDisplayPowerMode },
708    {"nativeDeferTransactionUntil", "(JLandroid/os/IBinder;J)V",
709            (void*)nativeDeferTransactionUntil },
710    {"nativeSetOverrideScalingMode", "(JI)V",
711            (void*)nativeSetOverrideScalingMode },
712    {"nativeGetHandle", "(J)Landroid/os/IBinder;",
713            (void*)nativeGetHandle }
714};
715
716int register_android_view_SurfaceControl(JNIEnv* env)
717{
718    int err = RegisterMethodsOrDie(env, "android/view/SurfaceControl",
719            sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
720
721    jclass clazz = FindClassOrDie(env, "android/view/SurfaceControl$PhysicalDisplayInfo");
722    gPhysicalDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
723    gPhysicalDisplayInfoClassInfo.ctor = GetMethodIDOrDie(env,
724            gPhysicalDisplayInfoClassInfo.clazz, "<init>", "()V");
725    gPhysicalDisplayInfoClassInfo.width =       GetFieldIDOrDie(env, clazz, "width", "I");
726    gPhysicalDisplayInfoClassInfo.height =      GetFieldIDOrDie(env, clazz, "height", "I");
727    gPhysicalDisplayInfoClassInfo.refreshRate = GetFieldIDOrDie(env, clazz, "refreshRate", "F");
728    gPhysicalDisplayInfoClassInfo.density =     GetFieldIDOrDie(env, clazz, "density", "F");
729    gPhysicalDisplayInfoClassInfo.xDpi =        GetFieldIDOrDie(env, clazz, "xDpi", "F");
730    gPhysicalDisplayInfoClassInfo.yDpi =        GetFieldIDOrDie(env, clazz, "yDpi", "F");
731    gPhysicalDisplayInfoClassInfo.secure =      GetFieldIDOrDie(env, clazz, "secure", "Z");
732    gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = GetFieldIDOrDie(env,
733            clazz, "appVsyncOffsetNanos", "J");
734    gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env,
735            clazz, "presentationDeadlineNanos", "J");
736    gPhysicalDisplayInfoClassInfo.colorTransform = GetFieldIDOrDie(env, clazz,
737            "colorTransform", "I");
738
739    jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect");
740    gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I");
741    gRectClassInfo.left =   GetFieldIDOrDie(env, rectClazz, "left", "I");
742    gRectClassInfo.right =  GetFieldIDOrDie(env, rectClazz, "right", "I");
743    gRectClassInfo.top =    GetFieldIDOrDie(env, rectClazz, "top", "I");
744
745    jclass frameStatsClazz = FindClassOrDie(env, "android/view/FrameStats");
746    jfieldID undefined_time_nano_field = GetStaticFieldIDOrDie(env,
747            frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
748    nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
749
750    jclass contFrameStatsClazz = FindClassOrDie(env, "android/view/WindowContentFrameStats");
751    gWindowContentFrameStatsClassInfo.init = GetMethodIDOrDie(env,
752            contFrameStatsClazz, "init", "(J[J[J[J)V");
753    gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
754
755    jclass animFrameStatsClazz = FindClassOrDie(env, "android/view/WindowAnimationFrameStats");
756    gWindowAnimationFrameStatsClassInfo.init =  GetMethodIDOrDie(env,
757            animFrameStatsClazz, "init", "(J[J)V");
758    gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
759
760    jclass hdrCapabilitiesClazz = FindClassOrDie(env, "android/view/Display$HdrCapabilities");
761    gHdrCapabilitiesClassInfo.clazz = MakeGlobalRefOrDie(env, hdrCapabilitiesClazz);
762    gHdrCapabilitiesClassInfo.ctor = GetMethodIDOrDie(env, hdrCapabilitiesClazz, "<init>",
763            "([IFFF)V");
764
765    return err;
766}
767
768};
769