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