blob: 5a935a96eaff8d3e56a463a9471e17ef51591ff7 [file] [log] [blame]
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001/*
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>
Mathias Agopian0449a402013-03-01 23:01:51 -080030#include <android_runtime/android_view_Surface.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080031#include <android_runtime/android_view_SurfaceSession.h>
32
33#include <gui/Surface.h>
34#include <gui/SurfaceComposerClient.h>
35
36#include <ui/DisplayInfo.h>
Svetoslav1376d602014-03-13 11:17:26 -070037#include <ui/FrameStats.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080038#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 {
Dan Stoza00101052014-05-02 15:23:40 -070053 jclass clazz;
54 jmethodID ctor;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080055 jfieldID width;
56 jfieldID height;
57 jfieldID refreshRate;
58 jfieldID density;
59 jfieldID xDpi;
60 jfieldID yDpi;
61 jfieldID secure;
62} gPhysicalDisplayInfoClassInfo;
63
Leon Scroggins46cb9bd2014-03-06 15:36:39 -050064// Implements SkMallocPixelRef::ReleaseProc, to delete the screenshot on unref.
65void DeleteScreenshot(void* addr, void* context) {
66 SkASSERT(addr == ((ScreenshotClient*) context)->getPixels());
67 delete ((ScreenshotClient*) context);
68}
Mathias Agopian3866f0d2013-02-11 22:08:48 -080069
Svetoslav1376d602014-03-13 11:17:26 -070070static struct {
71 nsecs_t UNDEFINED_TIME_NANO;
72 jmethodID init;
73} gWindowContentFrameStatsClassInfo;
74
75static struct {
76 nsecs_t UNDEFINED_TIME_NANO;
77 jmethodID init;
78} gWindowAnimationFrameStatsClassInfo;
79
Mathias Agopian3866f0d2013-02-11 22:08:48 -080080// ----------------------------------------------------------------------------
81
Ashok Bhat36bef0b2014-01-20 20:08:01 +000082static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -080083 jstring nameStr, jint w, jint h, jint format, jint flags) {
84 ScopedUtfChars name(env, nameStr);
85 sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));
86 sp<SurfaceControl> surface = client->createSurface(
87 String8(name.c_str()), w, h, format, flags);
88 if (surface == NULL) {
89 jniThrowException(env, OutOfResourcesException, NULL);
90 return 0;
91 }
Mathias Agopianb1d90c82013-03-06 17:45:42 -080092 surface->incStrong((void *)nativeCreate);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000093 return reinterpret_cast<jlong>(surface.get());
Mathias Agopian3866f0d2013-02-11 22:08:48 -080094}
95
Ashok Bhat36bef0b2014-01-20 20:08:01 +000096static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -080097 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
Mathias Agopianb1d90c82013-03-06 17:45:42 -080098 ctrl->decStrong((void *)nativeCreate);
Mathias Agopian3866f0d2013-02-11 22:08:48 -080099}
100
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000101static void nativeDestroy(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800102 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
103 ctrl->clear();
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800104 ctrl->decStrong((void *)nativeCreate);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800105}
106
Mathias Agopian0449a402013-03-01 23:01:51 -0800107static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz, jobject displayTokenObj,
Dan Stoza16ec12a2014-02-14 15:06:55 -0800108 jint width, jint height, jint minLayer, jint maxLayer, bool allLayers,
109 bool useIdentityTransform) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800110 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
111 if (displayToken == NULL) {
112 return NULL;
113 }
114
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500115 ScreenshotClient* screenshot = new ScreenshotClient();
116 status_t res;
117 if (width > 0 && height > 0) {
118 if (allLayers) {
119 res = screenshot->update(displayToken, width, height, useIdentityTransform);
120 } else {
121 res = screenshot->update(displayToken, width, height, minLayer, maxLayer,
122 useIdentityTransform);
123 }
124 } else {
125 res = screenshot->update(displayToken, useIdentityTransform);
126 }
127 if (res != NO_ERROR) {
128 delete screenshot;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800129 return NULL;
130 }
131
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500132 SkImageInfo screenshotInfo;
133 screenshotInfo.fWidth = screenshot->getWidth();
134 screenshotInfo.fHeight = screenshot->getHeight();
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800135
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500136 switch (screenshot->getFormat()) {
137 case PIXEL_FORMAT_RGBX_8888: {
138 screenshotInfo.fColorType = kRGBA_8888_SkColorType;
139 screenshotInfo.fAlphaType = kIgnore_SkAlphaType;
140 break;
141 }
142 case PIXEL_FORMAT_RGBA_8888: {
143 screenshotInfo.fColorType = kRGBA_8888_SkColorType;
144 screenshotInfo.fAlphaType = kPremul_SkAlphaType;
145 break;
146 }
147 case PIXEL_FORMAT_RGB_565: {
148 screenshotInfo.fColorType = kRGB_565_SkColorType;
149 screenshotInfo.fAlphaType = kIgnore_SkAlphaType;
150 break;
151 }
152 default: {
153 delete screenshot;
154 return NULL;
155 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800156 }
157
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500158 const ssize_t rowBytes =
159 screenshot->getStride() * android::bytesPerPixel(screenshot->getFormat());
160
161 SkBitmap* bitmap = new SkBitmap();
162 bitmap->setConfig(screenshotInfo, (size_t)rowBytes);
163 if (screenshotInfo.fWidth > 0 && screenshotInfo.fHeight > 0) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500164 // takes ownership of ScreenshotClient
165 SkMallocPixelRef* pixels = SkMallocPixelRef::NewWithProc(screenshotInfo,
166 (size_t) rowBytes, NULL, (void*) screenshot->getPixels(), &DeleteScreenshot,
167 (void*) screenshot);
168 pixels->setImmutable();
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800169 bitmap->setPixelRef(pixels)->unref();
170 bitmap->lockPixels();
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800171 }
172
Chris Craik1abf5d62013-08-16 12:47:03 -0700173 return GraphicsJNI::createBitmap(env, bitmap,
174 GraphicsJNI::kBitmapCreateFlag_Premultiplied, NULL);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800175}
176
Mathias Agopian0449a402013-03-01 23:01:51 -0800177static void nativeScreenshot(JNIEnv* env, jclass clazz,
178 jobject displayTokenObj, jobject surfaceObj,
Dan Stoza16ec12a2014-02-14 15:06:55 -0800179 jint width, jint height, jint minLayer, jint maxLayer, bool allLayers,
180 bool useIdentityTransform) {
Mathias Agopian0449a402013-03-01 23:01:51 -0800181 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
182 if (displayToken != NULL) {
183 sp<Surface> consumer = android_view_Surface_getSurface(env, surfaceObj);
184 if (consumer != NULL) {
185 if (allLayers) {
186 minLayer = 0;
187 maxLayer = -1;
188 }
189 ScreenshotClient::capture(
190 displayToken, consumer->getIGraphicBufferProducer(),
Dan Stoza16ec12a2014-02-14 15:06:55 -0800191 width, height, uint32_t(minLayer), uint32_t(maxLayer),
192 useIdentityTransform);
Mathias Agopian0449a402013-03-01 23:01:51 -0800193 }
194 }
195}
196
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800197static void nativeOpenTransaction(JNIEnv* env, jclass clazz) {
198 SurfaceComposerClient::openGlobalTransaction();
199}
200
201static void nativeCloseTransaction(JNIEnv* env, jclass clazz) {
202 SurfaceComposerClient::closeGlobalTransaction();
203}
204
205static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz) {
206 SurfaceComposerClient::setAnimationTransaction();
207}
208
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000209static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong nativeObject, jint zorder) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800210 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
211 status_t err = ctrl->setLayer(zorder);
212 if (err < 0 && err != NO_INIT) {
213 doThrowIAE(env);
214 }
215}
216
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000217static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat x, jfloat y) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800218 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
219 status_t err = ctrl->setPosition(x, y);
220 if (err < 0 && err != NO_INIT) {
221 doThrowIAE(env);
222 }
223}
224
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000225static void nativeSetSize(JNIEnv* env, jclass clazz, jlong nativeObject, jint w, jint h) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800226 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
227 status_t err = ctrl->setSize(w, h);
228 if (err < 0 && err != NO_INIT) {
229 doThrowIAE(env);
230 }
231}
232
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000233static void nativeSetFlags(JNIEnv* env, jclass clazz, jlong nativeObject, jint flags, jint mask) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800234 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
235 status_t err = ctrl->setFlags(flags, mask);
236 if (err < 0 && err != NO_INIT) {
237 doThrowIAE(env);
238 }
239}
240
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000241static void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jlong nativeObject, jobject regionObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800242 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
243 SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj);
244 if (!region) {
245 doThrowIAE(env);
246 return;
247 }
248
249 const SkIRect& b(region->getBounds());
250 Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
251 if (region->isComplex()) {
252 SkRegion::Iterator it(*region);
253 while (!it.done()) {
254 const SkIRect& r(it.rect());
255 reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
256 it.next();
257 }
258 }
259
260 status_t err = ctrl->setTransparentRegionHint(reg);
261 if (err < 0 && err != NO_INIT) {
262 doThrowIAE(env);
263 }
264}
265
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000266static void nativeSetAlpha(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat alpha) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800267 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
268 status_t err = ctrl->setAlpha(alpha);
269 if (err < 0 && err != NO_INIT) {
270 doThrowIAE(env);
271 }
272}
273
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000274static void nativeSetMatrix(JNIEnv* env, jclass clazz, jlong nativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800275 jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy) {
276 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
277 status_t err = ctrl->setMatrix(dsdx, dtdx, dsdy, dtdy);
278 if (err < 0 && err != NO_INIT) {
279 doThrowIAE(env);
280 }
281}
282
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000283static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong nativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800284 jint l, jint t, jint r, jint b) {
285 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
286 Rect crop(l, t, r, b);
287 status_t err = ctrl->setCrop(crop);
288 if (err < 0 && err != NO_INIT) {
289 doThrowIAE(env);
290 }
291}
292
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000293static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong nativeObject, jint layerStack) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800294 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
295 status_t err = ctrl->setLayerStack(layerStack);
296 if (err < 0 && err != NO_INIT) {
297 doThrowIAE(env);
298 }
299}
300
301static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) {
302 sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id));
303 return javaObjectForIBinder(env, token);
304}
305
306static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj,
307 jboolean secure) {
308 ScopedUtfChars name(env, nameObj);
309 sp<IBinder> token(SurfaceComposerClient::createDisplay(
310 String8(name.c_str()), bool(secure)));
311 return javaObjectForIBinder(env, token);
312}
313
Jesse Hall6a6bc212013-08-08 12:15:03 -0700314static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
315 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
316 if (token == NULL) return;
317 SurfaceComposerClient::destroyDisplay(token);
318}
319
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800320static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000321 jobject tokenObj, jlong nativeSurfaceObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800322 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
323 if (token == NULL) return;
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800324 sp<IGraphicBufferProducer> bufferProducer;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800325 sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800326 if (sur != NULL) {
327 bufferProducer = sur->getIGraphicBufferProducer();
328 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800329 SurfaceComposerClient::setDisplaySurface(token, bufferProducer);
330}
331
332static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
333 jobject tokenObj, jint layerStack) {
334 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
335 if (token == NULL) return;
336
337 SurfaceComposerClient::setDisplayLayerStack(token, layerStack);
338}
339
340static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz,
341 jobject tokenObj, jint orientation,
342 jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom,
343 jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) {
344 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
345 if (token == NULL) return;
346 Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom);
347 Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom);
348 SurfaceComposerClient::setDisplayProjection(token, orientation, layerStackRect, displayRect);
349}
350
Dan Stoza00101052014-05-02 15:23:40 -0700351static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
352 jobject tokenObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800353 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
Dan Stoza00101052014-05-02 15:23:40 -0700354 if (token == NULL) return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800355
Dan Stoza00101052014-05-02 15:23:40 -0700356 Vector<DisplayInfo> configs;
357 if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
358 configs.size() == 0) {
359 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800360 }
361
Dan Stoza00101052014-05-02 15:23:40 -0700362 jobjectArray configArray = env->NewObjectArray(configs.size(),
363 gPhysicalDisplayInfoClassInfo.clazz, NULL);
364
365 for (size_t c = 0; c < configs.size(); ++c) {
366 const DisplayInfo& info = configs[c];
367 jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
368 gPhysicalDisplayInfoClassInfo.ctor);
369 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
370 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
371 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
372 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
373 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
374 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
375 env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
376 env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
377 env->DeleteLocalRef(infoObj);
378 }
379
380 return configArray;
381}
382
383static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
384 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
385 if (token == NULL) return -1;
386 return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
387}
388
389static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
390 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
391 if (token == NULL) return JNI_FALSE;
392 status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
393 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800394}
395
396static void nativeBlankDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
397 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
398 if (token == NULL) return;
399
400 ALOGD_IF_SLOW(100, "Excessive delay in blankDisplay() while turning screen off");
401 SurfaceComposerClient::blankDisplay(token);
402}
403
404static void nativeUnblankDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
405 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
406 if (token == NULL) return;
407
408 ALOGD_IF_SLOW(100, "Excessive delay in unblankDisplay() while turning screen on");
409 SurfaceComposerClient::unblankDisplay(token);
410}
411
Svetoslav1376d602014-03-13 11:17:26 -0700412static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
413 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
414 status_t err = ctrl->clearLayerFrameStats();
415
416 if (err < 0 && err != NO_INIT) {
417 doThrowIAE(env);
418 }
419
420 // The other end is not ready, just report we failed.
421 if (err == NO_INIT) {
422 return JNI_FALSE;
423 }
424
425 return JNI_TRUE;
426}
427
428static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
429 jobject outStats) {
430 FrameStats stats;
431
432 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
433 status_t err = ctrl->getLayerFrameStats(&stats);
434 if (err < 0 && err != NO_INIT) {
435 doThrowIAE(env);
436 }
437
438 // The other end is not ready, fine just return empty stats.
439 if (err == NO_INIT) {
440 return JNI_FALSE;
441 }
442
443 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
444 size_t frameCount = stats.desiredPresentTimesNano.size();
445
446 jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
447 if (postedTimesNanoDst == NULL) {
448 return JNI_FALSE;
449 }
450
451 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
452 if (presentedTimesNanoDst == NULL) {
453 return JNI_FALSE;
454 }
455
456 jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
457 if (readyTimesNanoDst == NULL) {
458 return JNI_FALSE;
459 }
460
461 nsecs_t postedTimesNanoSrc[frameCount];
462 nsecs_t presentedTimesNanoSrc[frameCount];
463 nsecs_t readyTimesNanoSrc[frameCount];
464
465 for (size_t i = 0; i < frameCount; i++) {
466 nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
467 if (postedTimeNano == INT64_MAX) {
468 postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
469 }
470 postedTimesNanoSrc[i] = postedTimeNano;
471
472 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
473 if (presentedTimeNano == INT64_MAX) {
474 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
475 }
476 presentedTimesNanoSrc[i] = presentedTimeNano;
477
478 nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
479 if (readyTimeNano == INT64_MAX) {
480 readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
481 }
482 readyTimesNanoSrc[i] = readyTimeNano;
483 }
484
485 env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
486 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
487 env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
488
489 env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
490 postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
491
492 if (env->ExceptionCheck()) {
493 return JNI_FALSE;
494 }
495
496 return JNI_TRUE;
497}
498
499static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
500 status_t err = SurfaceComposerClient::clearAnimationFrameStats();
501
502 if (err < 0 && err != NO_INIT) {
503 doThrowIAE(env);
504 }
505
506 // The other end is not ready, just report we failed.
507 if (err == NO_INIT) {
508 return JNI_FALSE;
509 }
510
511 return JNI_TRUE;
512}
513
514static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
515 FrameStats stats;
516
517 status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
518 if (err < 0 && err != NO_INIT) {
519 doThrowIAE(env);
520 }
521
522 // The other end is not ready, fine just return empty stats.
523 if (err == NO_INIT) {
524 return JNI_FALSE;
525 }
526
527 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
528 size_t frameCount = stats.desiredPresentTimesNano.size();
529
530 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
531 if (presentedTimesNanoDst == NULL) {
532 return JNI_FALSE;
533 }
534
535 nsecs_t presentedTimesNanoSrc[frameCount];
536
537 for (size_t i = 0; i < frameCount; i++) {
Allen Hairac5eda32014-04-24 11:50:37 -0700538 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
Svetoslav1376d602014-03-13 11:17:26 -0700539 if (presentedTimeNano == INT64_MAX) {
540 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
541 }
542 presentedTimesNanoSrc[i] = presentedTimeNano;
543 }
544
545 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
546
547 env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
548 presentedTimesNanoDst);
549
550 if (env->ExceptionCheck()) {
551 return JNI_FALSE;
552 }
553
554 return JNI_TRUE;
555}
556
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800557// ----------------------------------------------------------------------------
558
559static JNINativeMethod sSurfaceControlMethods[] = {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000560 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800561 (void*)nativeCreate },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000562 {"nativeRelease", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800563 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000564 {"nativeDestroy", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800565 (void*)nativeDestroy },
Dan Stoza16ec12a2014-02-14 15:06:55 -0800566 {"nativeScreenshot", "(Landroid/os/IBinder;IIIIZZ)Landroid/graphics/Bitmap;",
Mathias Agopian0449a402013-03-01 23:01:51 -0800567 (void*)nativeScreenshotBitmap },
Dan Stoza16ec12a2014-02-14 15:06:55 -0800568 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;IIIIZZ)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800569 (void*)nativeScreenshot },
570 {"nativeOpenTransaction", "()V",
571 (void*)nativeOpenTransaction },
572 {"nativeCloseTransaction", "()V",
573 (void*)nativeCloseTransaction },
574 {"nativeSetAnimationTransaction", "()V",
575 (void*)nativeSetAnimationTransaction },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000576 {"nativeSetLayer", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800577 (void*)nativeSetLayer },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000578 {"nativeSetPosition", "(JFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800579 (void*)nativeSetPosition },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000580 {"nativeSetSize", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800581 (void*)nativeSetSize },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000582 {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800583 (void*)nativeSetTransparentRegionHint },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000584 {"nativeSetAlpha", "(JF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800585 (void*)nativeSetAlpha },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000586 {"nativeSetMatrix", "(JFFFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800587 (void*)nativeSetMatrix },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000588 {"nativeSetFlags", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800589 (void*)nativeSetFlags },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000590 {"nativeSetWindowCrop", "(JIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800591 (void*)nativeSetWindowCrop },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000592 {"nativeSetLayerStack", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800593 (void*)nativeSetLayerStack },
594 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
595 (void*)nativeGetBuiltInDisplay },
596 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
597 (void*)nativeCreateDisplay },
Jesse Hall6a6bc212013-08-08 12:15:03 -0700598 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
599 (void*)nativeDestroyDisplay },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000600 {"nativeSetDisplaySurface", "(Landroid/os/IBinder;J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800601 (void*)nativeSetDisplaySurface },
602 {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V",
603 (void*)nativeSetDisplayLayerStack },
604 {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V",
605 (void*)nativeSetDisplayProjection },
Dan Stoza00101052014-05-02 15:23:40 -0700606 {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
607 (void*)nativeGetDisplayConfigs },
608 {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
609 (void*)nativeGetActiveConfig },
610 {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
611 (void*)nativeSetActiveConfig },
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800612 {"nativeBlankDisplay", "(Landroid/os/IBinder;)V",
613 (void*)nativeBlankDisplay },
614 {"nativeUnblankDisplay", "(Landroid/os/IBinder;)V",
615 (void*)nativeUnblankDisplay },
Svetoslav1376d602014-03-13 11:17:26 -0700616 {"nativeClearContentFrameStats", "(J)Z",
617 (void*)nativeClearContentFrameStats },
618 {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
619 (void*)nativeGetContentFrameStats },
620 {"nativeClearAnimationFrameStats", "()Z",
621 (void*)nativeClearAnimationFrameStats },
622 {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
623 (void*)nativeGetAnimationFrameStats },
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800624};
625
626int register_android_view_SurfaceControl(JNIEnv* env)
627{
628 int err = AndroidRuntime::registerNativeMethods(env, "android/view/SurfaceControl",
629 sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
630
631 jclass clazz = env->FindClass("android/view/SurfaceControl$PhysicalDisplayInfo");
Dan Stoza00101052014-05-02 15:23:40 -0700632 gPhysicalDisplayInfoClassInfo.clazz = static_cast<jclass>(env->NewGlobalRef(clazz));
633 gPhysicalDisplayInfoClassInfo.ctor = env->GetMethodID(gPhysicalDisplayInfoClassInfo.clazz,
634 "<init>", "()V");
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800635 gPhysicalDisplayInfoClassInfo.width = env->GetFieldID(clazz, "width", "I");
636 gPhysicalDisplayInfoClassInfo.height = env->GetFieldID(clazz, "height", "I");
637 gPhysicalDisplayInfoClassInfo.refreshRate = env->GetFieldID(clazz, "refreshRate", "F");
638 gPhysicalDisplayInfoClassInfo.density = env->GetFieldID(clazz, "density", "F");
639 gPhysicalDisplayInfoClassInfo.xDpi = env->GetFieldID(clazz, "xDpi", "F");
640 gPhysicalDisplayInfoClassInfo.yDpi = env->GetFieldID(clazz, "yDpi", "F");
641 gPhysicalDisplayInfoClassInfo.secure = env->GetFieldID(clazz, "secure", "Z");
Svetoslav1376d602014-03-13 11:17:26 -0700642
643 jclass frameStatsClazz = env->FindClass("android/view/FrameStats");
644 jfieldID undefined_time_nano_field = env->GetStaticFieldID(frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
645 nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
646
647 jclass contFrameStatsClazz = env->FindClass("android/view/WindowContentFrameStats");
648 gWindowContentFrameStatsClassInfo.init = env->GetMethodID(contFrameStatsClazz, "init", "(J[J[J[J)V");
649 gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
650
651 jclass animFrameStatsClazz = env->FindClass("android/view/WindowAnimationFrameStats");
652 gWindowAnimationFrameStatsClassInfo.init = env->GetMethodID(animFrameStatsClazz, "init", "(J[J)V");
653 gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
654
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800655 return err;
656}
657
658};