android_view_SurfaceControl.cpp revision e3f9800f38da3cd1d83889132952eb4ab2e8c278
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 <stdio.h>
20
21#include "jni.h"
22#include "JNIHelp.h"
23
24#include "android_os_Parcel.h"
25#include "android_util_Binder.h"
26#include "android/graphics/GraphicsJNI.h"
27#include "android/graphics/Region.h"
28
29#include <android_runtime/AndroidRuntime.h>
30#include <android_runtime/android_view_Surface.h>
31#include <android_runtime/android_view_SurfaceSession.h>
32
33#include <gui/Surface.h>
34#include <gui/SurfaceComposerClient.h>
35
36#include <ui/DisplayInfo.h>
37#include <ui/FrameStats.h>
38#include <ui/Rect.h>
39#include <ui/Region.h>
40
41#include <utils/Log.h>
42
43#include <ScopedUtfChars.h>
44
45#include "SkTemplates.h"
46
47// ----------------------------------------------------------------------------
48
49namespace android {
50
51static const char* const OutOfResourcesException =
52    "android/view/Surface$OutOfResourcesException";
53
54static struct {
55    jclass clazz;
56    jmethodID ctor;
57    jfieldID width;
58    jfieldID height;
59    jfieldID refreshRate;
60    jfieldID density;
61    jfieldID xDpi;
62    jfieldID yDpi;
63    jfieldID secure;
64} gPhysicalDisplayInfoClassInfo;
65
66static struct {
67    jfieldID bottom;
68    jfieldID left;
69    jfieldID right;
70    jfieldID top;
71} gRectClassInfo;
72
73// Implements SkMallocPixelRef::ReleaseProc, to delete the screenshot on unref.
74void DeleteScreenshot(void* addr, void* context) {
75    SkASSERT(addr == ((ScreenshotClient*) context)->getPixels());
76    delete ((ScreenshotClient*) context);
77}
78
79static struct {
80    nsecs_t UNDEFINED_TIME_NANO;
81    jmethodID init;
82} gWindowContentFrameStatsClassInfo;
83
84static struct {
85    nsecs_t UNDEFINED_TIME_NANO;
86    jmethodID init;
87} gWindowAnimationFrameStatsClassInfo;
88
89// ----------------------------------------------------------------------------
90
91static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
92        jstring nameStr, jint w, jint h, jint format, jint flags) {
93    ScopedUtfChars name(env, nameStr);
94    sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));
95    sp<SurfaceControl> surface = client->createSurface(
96            String8(name.c_str()), w, h, format, flags);
97    if (surface == NULL) {
98        jniThrowException(env, OutOfResourcesException, NULL);
99        return 0;
100    }
101    surface->incStrong((void *)nativeCreate);
102    return reinterpret_cast<jlong>(surface.get());
103}
104
105static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
106    sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
107    ctrl->decStrong((void *)nativeCreate);
108}
109
110static void nativeDestroy(JNIEnv* env, jclass clazz, jlong nativeObject) {
111    sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
112    ctrl->clear();
113    ctrl->decStrong((void *)nativeCreate);
114}
115
116static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz,
117        jobject displayTokenObj, jobject sourceCropObj, jint width, jint height,
118        jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform) {
119    sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
120    if (displayToken == NULL) {
121        return NULL;
122    }
123
124    int left = env->GetIntField(sourceCropObj, gRectClassInfo.left);
125    int top = env->GetIntField(sourceCropObj, gRectClassInfo.top);
126    int right = env->GetIntField(sourceCropObj, gRectClassInfo.right);
127    int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom);
128    Rect sourceCrop(left, top, right, bottom);
129
130    SkAutoTDelete<ScreenshotClient> screenshot(new ScreenshotClient());
131    status_t res;
132    if (width > 0 && height > 0) {
133        if (allLayers) {
134            res = screenshot->update(displayToken, sourceCrop, width, height,
135                    useIdentityTransform);
136        } else {
137            res = screenshot->update(displayToken, sourceCrop, width, height,
138                    minLayer, maxLayer, useIdentityTransform);
139        }
140    } else {
141        res = screenshot->update(displayToken, sourceCrop, useIdentityTransform);
142    }
143    if (res != NO_ERROR) {
144        return NULL;
145    }
146
147    SkImageInfo screenshotInfo;
148    screenshotInfo.fWidth = screenshot->getWidth();
149    screenshotInfo.fHeight = screenshot->getHeight();
150
151    switch (screenshot->getFormat()) {
152        case PIXEL_FORMAT_RGBX_8888: {
153            screenshotInfo.fColorType = kRGBA_8888_SkColorType;
154            screenshotInfo.fAlphaType = kIgnore_SkAlphaType;
155            break;
156        }
157        case PIXEL_FORMAT_RGBA_8888: {
158            screenshotInfo.fColorType = kRGBA_8888_SkColorType;
159            screenshotInfo.fAlphaType = kPremul_SkAlphaType;
160            break;
161        }
162        case PIXEL_FORMAT_RGB_565: {
163            screenshotInfo.fColorType = kRGB_565_SkColorType;
164            screenshotInfo.fAlphaType = kIgnore_SkAlphaType;
165            break;
166        }
167        default: {
168            return NULL;
169        }
170    }
171
172    const ssize_t rowBytes =
173            screenshot->getStride() * android::bytesPerPixel(screenshot->getFormat());
174
175    SkBitmap* bitmap = new SkBitmap();
176    bitmap->setConfig(screenshotInfo, (size_t)rowBytes);
177    if (screenshotInfo.fWidth > 0 && screenshotInfo.fHeight > 0) {
178        // takes ownership of ScreenshotClient
179        SkMallocPixelRef* pixels = SkMallocPixelRef::NewWithProc(screenshotInfo,
180                (size_t) rowBytes, NULL, (void*) screenshot->getPixels(), &DeleteScreenshot,
181                (void*) (screenshot.detach()));
182        pixels->setImmutable();
183        bitmap->setPixelRef(pixels)->unref();
184        bitmap->lockPixels();
185    }
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 jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
371        jobject tokenObj) {
372    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
373    if (token == NULL) return NULL;
374
375    Vector<DisplayInfo> configs;
376    if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
377            configs.size() == 0) {
378        return NULL;
379    }
380
381    jobjectArray configArray = env->NewObjectArray(configs.size(),
382            gPhysicalDisplayInfoClassInfo.clazz, NULL);
383
384    for (size_t c = 0; c < configs.size(); ++c) {
385        const DisplayInfo& info = configs[c];
386        jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
387                gPhysicalDisplayInfoClassInfo.ctor);
388        env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
389        env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
390        env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
391        env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
392        env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
393        env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
394        env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
395        env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
396        env->DeleteLocalRef(infoObj);
397    }
398
399    return configArray;
400}
401
402static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
403    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
404    if (token == NULL) return -1;
405    return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
406}
407
408static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
409    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
410    if (token == NULL) return JNI_FALSE;
411    status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
412    return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
413}
414
415static void nativeBlankDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
416    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
417    if (token == NULL) return;
418
419    ALOGD_IF_SLOW(100, "Excessive delay in blankDisplay() while turning screen off");
420    SurfaceComposerClient::blankDisplay(token);
421}
422
423static void nativeUnblankDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
424    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
425    if (token == NULL) return;
426
427    ALOGD_IF_SLOW(100, "Excessive delay in unblankDisplay() while turning screen on");
428    SurfaceComposerClient::unblankDisplay(token);
429}
430
431static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
432    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
433    status_t err = ctrl->clearLayerFrameStats();
434
435    if (err < 0 && err != NO_INIT) {
436        doThrowIAE(env);
437    }
438
439    // The other end is not ready, just report we failed.
440    if (err == NO_INIT) {
441        return JNI_FALSE;
442    }
443
444    return JNI_TRUE;
445}
446
447static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
448    jobject outStats) {
449    FrameStats stats;
450
451    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
452    status_t err = ctrl->getLayerFrameStats(&stats);
453    if (err < 0 && err != NO_INIT) {
454        doThrowIAE(env);
455    }
456
457    // The other end is not ready, fine just return empty stats.
458    if (err == NO_INIT) {
459        return JNI_FALSE;
460    }
461
462    jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
463    size_t frameCount = stats.desiredPresentTimesNano.size();
464
465    jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
466    if (postedTimesNanoDst == NULL) {
467        return JNI_FALSE;
468    }
469
470    jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
471    if (presentedTimesNanoDst == NULL) {
472        return JNI_FALSE;
473    }
474
475    jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
476    if (readyTimesNanoDst == NULL) {
477        return JNI_FALSE;
478    }
479
480    nsecs_t postedTimesNanoSrc[frameCount];
481    nsecs_t presentedTimesNanoSrc[frameCount];
482    nsecs_t readyTimesNanoSrc[frameCount];
483
484    for (size_t i = 0; i < frameCount; i++) {
485        nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
486        if (postedTimeNano == INT64_MAX) {
487            postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
488        }
489        postedTimesNanoSrc[i] = postedTimeNano;
490
491        nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
492        if (presentedTimeNano == INT64_MAX) {
493            presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
494        }
495        presentedTimesNanoSrc[i] = presentedTimeNano;
496
497        nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
498        if (readyTimeNano == INT64_MAX) {
499            readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
500        }
501        readyTimesNanoSrc[i] = readyTimeNano;
502    }
503
504    env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
505    env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
506    env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
507
508    env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
509            postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
510
511    if (env->ExceptionCheck()) {
512        return JNI_FALSE;
513    }
514
515    return JNI_TRUE;
516}
517
518static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
519    status_t err = SurfaceComposerClient::clearAnimationFrameStats();
520
521    if (err < 0 && err != NO_INIT) {
522        doThrowIAE(env);
523    }
524
525    // The other end is not ready, just report we failed.
526    if (err == NO_INIT) {
527        return JNI_FALSE;
528    }
529
530    return JNI_TRUE;
531}
532
533static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
534    FrameStats stats;
535
536    status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
537    if (err < 0 && err != NO_INIT) {
538        doThrowIAE(env);
539    }
540
541    // The other end is not ready, fine just return empty stats.
542    if (err == NO_INIT) {
543        return JNI_FALSE;
544    }
545
546    jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
547    size_t frameCount = stats.desiredPresentTimesNano.size();
548
549    jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
550    if (presentedTimesNanoDst == NULL) {
551        return JNI_FALSE;
552    }
553
554    nsecs_t presentedTimesNanoSrc[frameCount];
555
556    for (size_t i = 0; i < frameCount; i++) {
557        nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
558        if (presentedTimeNano == INT64_MAX) {
559            presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
560        }
561        presentedTimesNanoSrc[i] = presentedTimeNano;
562    }
563
564    env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
565
566    env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
567            presentedTimesNanoDst);
568
569    if (env->ExceptionCheck()) {
570        return JNI_FALSE;
571    }
572
573    return JNI_TRUE;
574}
575
576// ----------------------------------------------------------------------------
577
578static JNINativeMethod sSurfaceControlMethods[] = {
579    {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J",
580            (void*)nativeCreate },
581    {"nativeRelease", "(J)V",
582            (void*)nativeRelease },
583    {"nativeDestroy", "(J)V",
584            (void*)nativeDestroy },
585    {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZ)Landroid/graphics/Bitmap;",
586            (void*)nativeScreenshotBitmap },
587    {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;Landroid/graphics/Rect;IIIIZZ)V",
588            (void*)nativeScreenshot },
589    {"nativeOpenTransaction", "()V",
590            (void*)nativeOpenTransaction },
591    {"nativeCloseTransaction", "()V",
592            (void*)nativeCloseTransaction },
593    {"nativeSetAnimationTransaction", "()V",
594            (void*)nativeSetAnimationTransaction },
595    {"nativeSetLayer", "(JI)V",
596            (void*)nativeSetLayer },
597    {"nativeSetPosition", "(JFF)V",
598            (void*)nativeSetPosition },
599    {"nativeSetSize", "(JII)V",
600            (void*)nativeSetSize },
601    {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V",
602            (void*)nativeSetTransparentRegionHint },
603    {"nativeSetAlpha", "(JF)V",
604            (void*)nativeSetAlpha },
605    {"nativeSetMatrix", "(JFFFF)V",
606            (void*)nativeSetMatrix },
607    {"nativeSetFlags", "(JII)V",
608            (void*)nativeSetFlags },
609    {"nativeSetWindowCrop", "(JIIII)V",
610            (void*)nativeSetWindowCrop },
611    {"nativeSetLayerStack", "(JI)V",
612            (void*)nativeSetLayerStack },
613    {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
614            (void*)nativeGetBuiltInDisplay },
615    {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
616            (void*)nativeCreateDisplay },
617    {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
618            (void*)nativeDestroyDisplay },
619    {"nativeSetDisplaySurface", "(Landroid/os/IBinder;J)V",
620            (void*)nativeSetDisplaySurface },
621    {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V",
622            (void*)nativeSetDisplayLayerStack },
623    {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V",
624            (void*)nativeSetDisplayProjection },
625    {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
626            (void*)nativeGetDisplayConfigs },
627    {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
628            (void*)nativeGetActiveConfig },
629    {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
630            (void*)nativeSetActiveConfig },
631    {"nativeBlankDisplay", "(Landroid/os/IBinder;)V",
632            (void*)nativeBlankDisplay },
633    {"nativeUnblankDisplay", "(Landroid/os/IBinder;)V",
634            (void*)nativeUnblankDisplay },
635    {"nativeClearContentFrameStats", "(J)Z",
636            (void*)nativeClearContentFrameStats },
637    {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
638            (void*)nativeGetContentFrameStats },
639    {"nativeClearAnimationFrameStats", "()Z",
640            (void*)nativeClearAnimationFrameStats },
641    {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
642            (void*)nativeGetAnimationFrameStats },
643};
644
645int register_android_view_SurfaceControl(JNIEnv* env)
646{
647    int err = AndroidRuntime::registerNativeMethods(env, "android/view/SurfaceControl",
648            sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
649
650    jclass clazz = env->FindClass("android/view/SurfaceControl$PhysicalDisplayInfo");
651    gPhysicalDisplayInfoClassInfo.clazz = static_cast<jclass>(env->NewGlobalRef(clazz));
652    gPhysicalDisplayInfoClassInfo.ctor = env->GetMethodID(gPhysicalDisplayInfoClassInfo.clazz,
653            "<init>", "()V");
654    gPhysicalDisplayInfoClassInfo.width = env->GetFieldID(clazz, "width", "I");
655    gPhysicalDisplayInfoClassInfo.height = env->GetFieldID(clazz, "height", "I");
656    gPhysicalDisplayInfoClassInfo.refreshRate = env->GetFieldID(clazz, "refreshRate", "F");
657    gPhysicalDisplayInfoClassInfo.density = env->GetFieldID(clazz, "density", "F");
658    gPhysicalDisplayInfoClassInfo.xDpi = env->GetFieldID(clazz, "xDpi", "F");
659    gPhysicalDisplayInfoClassInfo.yDpi = env->GetFieldID(clazz, "yDpi", "F");
660    gPhysicalDisplayInfoClassInfo.secure = env->GetFieldID(clazz, "secure", "Z");
661
662    jclass rectClazz = env->FindClass("android/graphics/Rect");
663    gRectClassInfo.bottom = env->GetFieldID(rectClazz, "bottom", "I");
664    gRectClassInfo.left = env->GetFieldID(rectClazz, "left", "I");
665    gRectClassInfo.right = env->GetFieldID(rectClazz, "right", "I");
666    gRectClassInfo.top = env->GetFieldID(rectClazz, "top", "I");
667
668    jclass frameStatsClazz = env->FindClass("android/view/FrameStats");
669    jfieldID undefined_time_nano_field =  env->GetStaticFieldID(frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
670    nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
671
672    jclass contFrameStatsClazz = env->FindClass("android/view/WindowContentFrameStats");
673    gWindowContentFrameStatsClassInfo.init =  env->GetMethodID(contFrameStatsClazz, "init", "(J[J[J[J)V");
674    gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
675
676    jclass animFrameStatsClazz = env->FindClass("android/view/WindowAnimationFrameStats");
677    gWindowAnimationFrameStatsClassInfo.init =  env->GetMethodID(animFrameStatsClazz, "init", "(J[J)V");
678    gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
679
680    return err;
681}
682
683};
684