android_view_SurfaceControl.cpp revision c55929a2a5686fe456b19cd54a73b8bde2a4332b
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// ----------------------------------------------------------------------------
46
47namespace android {
48
49static const char* const OutOfResourcesException =
50    "android/view/Surface$OutOfResourcesException";
51
52static struct {
53    jclass clazz;
54    jmethodID ctor;
55    jfieldID width;
56    jfieldID height;
57    jfieldID refreshRate;
58    jfieldID density;
59    jfieldID xDpi;
60    jfieldID yDpi;
61    jfieldID secure;
62} gPhysicalDisplayInfoClassInfo;
63
64static struct {
65    jfieldID bottom;
66    jfieldID left;
67    jfieldID right;
68    jfieldID top;
69} gRectClassInfo;
70
71// Implements SkMallocPixelRef::ReleaseProc, to delete the screenshot on unref.
72void DeleteScreenshot(void* addr, void* context) {
73    SkASSERT(addr == ((ScreenshotClient*) context)->getPixels());
74    delete ((ScreenshotClient*) context);
75}
76
77static struct {
78    nsecs_t UNDEFINED_TIME_NANO;
79    jmethodID init;
80} gWindowContentFrameStatsClassInfo;
81
82static struct {
83    nsecs_t UNDEFINED_TIME_NANO;
84    jmethodID init;
85} gWindowAnimationFrameStatsClassInfo;
86
87// ----------------------------------------------------------------------------
88
89static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
90        jstring nameStr, jint w, jint h, jint format, jint flags) {
91    ScopedUtfChars name(env, nameStr);
92    sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));
93    sp<SurfaceControl> surface = client->createSurface(
94            String8(name.c_str()), w, h, format, flags);
95    if (surface == NULL) {
96        jniThrowException(env, OutOfResourcesException, NULL);
97        return 0;
98    }
99    surface->incStrong((void *)nativeCreate);
100    return reinterpret_cast<jlong>(surface.get());
101}
102
103static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
104    sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
105    ctrl->decStrong((void *)nativeCreate);
106}
107
108static void nativeDestroy(JNIEnv* env, jclass clazz, jlong nativeObject) {
109    sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
110    ctrl->clear();
111    ctrl->decStrong((void *)nativeCreate);
112}
113
114static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz,
115        jobject displayTokenObj, jobject sourceCropObj, jint width, jint height,
116        jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform) {
117    sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
118    if (displayToken == NULL) {
119        return NULL;
120    }
121
122    int left = env->GetIntField(sourceCropObj, gRectClassInfo.left);
123    int top = env->GetIntField(sourceCropObj, gRectClassInfo.top);
124    int right = env->GetIntField(sourceCropObj, gRectClassInfo.right);
125    int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom);
126    Rect sourceCrop(left, top, right, bottom);
127
128    ScreenshotClient* screenshot = new ScreenshotClient();
129    status_t res;
130    if (width > 0 && height > 0) {
131        if (allLayers) {
132            res = screenshot->update(displayToken, sourceCrop, width, height,
133                    useIdentityTransform);
134        } else {
135            res = screenshot->update(displayToken, sourceCrop, width, height,
136                    minLayer, maxLayer, useIdentityTransform);
137        }
138    } else {
139        res = screenshot->update(displayToken, sourceCrop, useIdentityTransform);
140    }
141    if (res != NO_ERROR) {
142        delete screenshot;
143        return NULL;
144    }
145
146    SkImageInfo screenshotInfo;
147    screenshotInfo.fWidth = screenshot->getWidth();
148    screenshotInfo.fHeight = screenshot->getHeight();
149
150    switch (screenshot->getFormat()) {
151        case PIXEL_FORMAT_RGBX_8888: {
152            screenshotInfo.fColorType = kRGBA_8888_SkColorType;
153            screenshotInfo.fAlphaType = kIgnore_SkAlphaType;
154            break;
155        }
156        case PIXEL_FORMAT_RGBA_8888: {
157            screenshotInfo.fColorType = kRGBA_8888_SkColorType;
158            screenshotInfo.fAlphaType = kPremul_SkAlphaType;
159            break;
160        }
161        case PIXEL_FORMAT_RGB_565: {
162            screenshotInfo.fColorType = kRGB_565_SkColorType;
163            screenshotInfo.fAlphaType = kIgnore_SkAlphaType;
164            break;
165        }
166        default: {
167            delete screenshot;
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);
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 nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
416    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
417    if (token == NULL) return;
418
419    ALOGD_IF_SLOW(100, "Excessive delay in setPowerMode()");
420    SurfaceComposerClient::setDisplayPowerMode(token, mode);
421}
422
423static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
424    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
425    status_t err = ctrl->clearLayerFrameStats();
426
427    if (err < 0 && err != NO_INIT) {
428        doThrowIAE(env);
429    }
430
431    // The other end is not ready, just report we failed.
432    if (err == NO_INIT) {
433        return JNI_FALSE;
434    }
435
436    return JNI_TRUE;
437}
438
439static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
440    jobject outStats) {
441    FrameStats stats;
442
443    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
444    status_t err = ctrl->getLayerFrameStats(&stats);
445    if (err < 0 && err != NO_INIT) {
446        doThrowIAE(env);
447    }
448
449    // The other end is not ready, fine just return empty stats.
450    if (err == NO_INIT) {
451        return JNI_FALSE;
452    }
453
454    jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
455    size_t frameCount = stats.desiredPresentTimesNano.size();
456
457    jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
458    if (postedTimesNanoDst == NULL) {
459        return JNI_FALSE;
460    }
461
462    jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
463    if (presentedTimesNanoDst == NULL) {
464        return JNI_FALSE;
465    }
466
467    jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
468    if (readyTimesNanoDst == NULL) {
469        return JNI_FALSE;
470    }
471
472    nsecs_t postedTimesNanoSrc[frameCount];
473    nsecs_t presentedTimesNanoSrc[frameCount];
474    nsecs_t readyTimesNanoSrc[frameCount];
475
476    for (size_t i = 0; i < frameCount; i++) {
477        nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
478        if (postedTimeNano == INT64_MAX) {
479            postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
480        }
481        postedTimesNanoSrc[i] = postedTimeNano;
482
483        nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
484        if (presentedTimeNano == INT64_MAX) {
485            presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
486        }
487        presentedTimesNanoSrc[i] = presentedTimeNano;
488
489        nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
490        if (readyTimeNano == INT64_MAX) {
491            readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
492        }
493        readyTimesNanoSrc[i] = readyTimeNano;
494    }
495
496    env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
497    env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
498    env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
499
500    env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
501            postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
502
503    if (env->ExceptionCheck()) {
504        return JNI_FALSE;
505    }
506
507    return JNI_TRUE;
508}
509
510static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
511    status_t err = SurfaceComposerClient::clearAnimationFrameStats();
512
513    if (err < 0 && err != NO_INIT) {
514        doThrowIAE(env);
515    }
516
517    // The other end is not ready, just report we failed.
518    if (err == NO_INIT) {
519        return JNI_FALSE;
520    }
521
522    return JNI_TRUE;
523}
524
525static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
526    FrameStats stats;
527
528    status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
529    if (err < 0 && err != NO_INIT) {
530        doThrowIAE(env);
531    }
532
533    // The other end is not ready, fine just return empty stats.
534    if (err == NO_INIT) {
535        return JNI_FALSE;
536    }
537
538    jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
539    size_t frameCount = stats.desiredPresentTimesNano.size();
540
541    jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
542    if (presentedTimesNanoDst == NULL) {
543        return JNI_FALSE;
544    }
545
546    nsecs_t presentedTimesNanoSrc[frameCount];
547
548    for (size_t i = 0; i < frameCount; i++) {
549        nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
550        if (presentedTimeNano == INT64_MAX) {
551            presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
552        }
553        presentedTimesNanoSrc[i] = presentedTimeNano;
554    }
555
556    env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
557
558    env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
559            presentedTimesNanoDst);
560
561    if (env->ExceptionCheck()) {
562        return JNI_FALSE;
563    }
564
565    return JNI_TRUE;
566}
567
568// ----------------------------------------------------------------------------
569
570static JNINativeMethod sSurfaceControlMethods[] = {
571    {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J",
572            (void*)nativeCreate },
573    {"nativeRelease", "(J)V",
574            (void*)nativeRelease },
575    {"nativeDestroy", "(J)V",
576            (void*)nativeDestroy },
577    {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZ)Landroid/graphics/Bitmap;",
578            (void*)nativeScreenshotBitmap },
579    {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;Landroid/graphics/Rect;IIIIZZ)V",
580            (void*)nativeScreenshot },
581    {"nativeOpenTransaction", "()V",
582            (void*)nativeOpenTransaction },
583    {"nativeCloseTransaction", "()V",
584            (void*)nativeCloseTransaction },
585    {"nativeSetAnimationTransaction", "()V",
586            (void*)nativeSetAnimationTransaction },
587    {"nativeSetLayer", "(JI)V",
588            (void*)nativeSetLayer },
589    {"nativeSetPosition", "(JFF)V",
590            (void*)nativeSetPosition },
591    {"nativeSetSize", "(JII)V",
592            (void*)nativeSetSize },
593    {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V",
594            (void*)nativeSetTransparentRegionHint },
595    {"nativeSetAlpha", "(JF)V",
596            (void*)nativeSetAlpha },
597    {"nativeSetMatrix", "(JFFFF)V",
598            (void*)nativeSetMatrix },
599    {"nativeSetFlags", "(JII)V",
600            (void*)nativeSetFlags },
601    {"nativeSetWindowCrop", "(JIIII)V",
602            (void*)nativeSetWindowCrop },
603    {"nativeSetLayerStack", "(JI)V",
604            (void*)nativeSetLayerStack },
605    {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
606            (void*)nativeGetBuiltInDisplay },
607    {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
608            (void*)nativeCreateDisplay },
609    {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
610            (void*)nativeDestroyDisplay },
611    {"nativeSetDisplaySurface", "(Landroid/os/IBinder;J)V",
612            (void*)nativeSetDisplaySurface },
613    {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V",
614            (void*)nativeSetDisplayLayerStack },
615    {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V",
616            (void*)nativeSetDisplayProjection },
617    {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
618            (void*)nativeGetDisplayConfigs },
619    {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
620            (void*)nativeGetActiveConfig },
621    {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
622            (void*)nativeSetActiveConfig },
623    {"nativeClearContentFrameStats", "(J)Z",
624            (void*)nativeClearContentFrameStats },
625    {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
626            (void*)nativeGetContentFrameStats },
627    {"nativeClearAnimationFrameStats", "()Z",
628            (void*)nativeClearAnimationFrameStats },
629    {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
630            (void*)nativeGetAnimationFrameStats },
631    {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
632            (void*)nativeSetDisplayPowerMode },
633};
634
635int register_android_view_SurfaceControl(JNIEnv* env)
636{
637    int err = AndroidRuntime::registerNativeMethods(env, "android/view/SurfaceControl",
638            sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
639
640    jclass clazz = env->FindClass("android/view/SurfaceControl$PhysicalDisplayInfo");
641    gPhysicalDisplayInfoClassInfo.clazz = static_cast<jclass>(env->NewGlobalRef(clazz));
642    gPhysicalDisplayInfoClassInfo.ctor = env->GetMethodID(gPhysicalDisplayInfoClassInfo.clazz,
643            "<init>", "()V");
644    gPhysicalDisplayInfoClassInfo.width = env->GetFieldID(clazz, "width", "I");
645    gPhysicalDisplayInfoClassInfo.height = env->GetFieldID(clazz, "height", "I");
646    gPhysicalDisplayInfoClassInfo.refreshRate = env->GetFieldID(clazz, "refreshRate", "F");
647    gPhysicalDisplayInfoClassInfo.density = env->GetFieldID(clazz, "density", "F");
648    gPhysicalDisplayInfoClassInfo.xDpi = env->GetFieldID(clazz, "xDpi", "F");
649    gPhysicalDisplayInfoClassInfo.yDpi = env->GetFieldID(clazz, "yDpi", "F");
650    gPhysicalDisplayInfoClassInfo.secure = env->GetFieldID(clazz, "secure", "Z");
651
652    jclass rectClazz = env->FindClass("android/graphics/Rect");
653    gRectClassInfo.bottom = env->GetFieldID(rectClazz, "bottom", "I");
654    gRectClassInfo.left = env->GetFieldID(rectClazz, "left", "I");
655    gRectClassInfo.right = env->GetFieldID(rectClazz, "right", "I");
656    gRectClassInfo.top = env->GetFieldID(rectClazz, "top", "I");
657
658    jclass frameStatsClazz = env->FindClass("android/view/FrameStats");
659    jfieldID undefined_time_nano_field =  env->GetStaticFieldID(frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
660    nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
661
662    jclass contFrameStatsClazz = env->FindClass("android/view/WindowContentFrameStats");
663    gWindowContentFrameStatsClassInfo.init =  env->GetMethodID(contFrameStatsClazz, "init", "(J[J[J[J)V");
664    gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
665
666    jclass animFrameStatsClazz = env->FindClass("android/view/WindowAnimationFrameStats");
667    gWindowAnimationFrameStatsClassInfo.init =  env->GetMethodID(animFrameStatsClazz, "init", "(J[J)V");
668    gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
669
670    return err;
671}
672
673};
674