blob: 4594cc31adac4083499e48fb2fb8d9f270d3c135 [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
Dan Stoza9890e3412014-05-22 16:12:54 -070064static struct {
65 jfieldID bottom;
66 jfieldID left;
67 jfieldID right;
68 jfieldID top;
69} gRectClassInfo;
70
Leon Scroggins46cb9bd2014-03-06 15:36:39 -050071// 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}
Mathias Agopian3866f0d2013-02-11 22:08:48 -080076
Svetoslav1376d602014-03-13 11:17:26 -070077static 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
Mathias Agopian3866f0d2013-02-11 22:08:48 -080087// ----------------------------------------------------------------------------
88
Ashok Bhat36bef0b2014-01-20 20:08:01 +000089static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -080090 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 }
Mathias Agopianb1d90c82013-03-06 17:45:42 -080099 surface->incStrong((void *)nativeCreate);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000100 return reinterpret_cast<jlong>(surface.get());
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800101}
102
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000103static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800104 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800105 ctrl->decStrong((void *)nativeCreate);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800106}
107
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000108static void nativeDestroy(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800109 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
110 ctrl->clear();
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800111 ctrl->decStrong((void *)nativeCreate);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800112}
113
Dan Stoza9890e3412014-05-22 16:12:54 -0700114static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz,
115 jobject displayTokenObj, jobject sourceCropObj, jint width, jint height,
116 jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800117 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
118 if (displayToken == NULL) {
119 return NULL;
120 }
121
Dan Stoza9890e3412014-05-22 16:12:54 -0700122 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
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500128 ScreenshotClient* screenshot = new ScreenshotClient();
129 status_t res;
130 if (width > 0 && height > 0) {
131 if (allLayers) {
Dan Stoza9890e3412014-05-22 16:12:54 -0700132 res = screenshot->update(displayToken, sourceCrop, width, height,
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500133 useIdentityTransform);
Dan Stoza9890e3412014-05-22 16:12:54 -0700134 } else {
135 res = screenshot->update(displayToken, sourceCrop, width, height,
136 minLayer, maxLayer, useIdentityTransform);
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500137 }
138 } else {
Dan Stoza9890e3412014-05-22 16:12:54 -0700139 res = screenshot->update(displayToken, sourceCrop, useIdentityTransform);
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500140 }
141 if (res != NO_ERROR) {
142 delete screenshot;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800143 return NULL;
144 }
145
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500146 SkImageInfo screenshotInfo;
147 screenshotInfo.fWidth = screenshot->getWidth();
148 screenshotInfo.fHeight = screenshot->getHeight();
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800149
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500150 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 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800170 }
171
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500172 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) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500178 // 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();
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800183 bitmap->setPixelRef(pixels)->unref();
184 bitmap->lockPixels();
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800185 }
186
Chris Craik1abf5d62013-08-16 12:47:03 -0700187 return GraphicsJNI::createBitmap(env, bitmap,
188 GraphicsJNI::kBitmapCreateFlag_Premultiplied, NULL);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800189}
190
Dan Stoza9890e3412014-05-22 16:12:54 -0700191static 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) {
Mathias Agopian0449a402013-03-01 23:01:51 -0800194 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
195 if (displayToken != NULL) {
196 sp<Surface> consumer = android_view_Surface_getSurface(env, surfaceObj);
197 if (consumer != NULL) {
Dan Stoza9890e3412014-05-22 16:12:54 -0700198 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
Mathias Agopian0449a402013-03-01 23:01:51 -0800204 if (allLayers) {
205 minLayer = 0;
206 maxLayer = -1;
207 }
Dan Stoza9890e3412014-05-22 16:12:54 -0700208 ScreenshotClient::capture(displayToken,
209 consumer->getIGraphicBufferProducer(), sourceCrop,
Dan Stoza16ec12a2014-02-14 15:06:55 -0800210 width, height, uint32_t(minLayer), uint32_t(maxLayer),
211 useIdentityTransform);
Mathias Agopian0449a402013-03-01 23:01:51 -0800212 }
213 }
214}
215
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800216static 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
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000228static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong nativeObject, jint zorder) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800229 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
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000236static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat x, jfloat y) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800237 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
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000244static void nativeSetSize(JNIEnv* env, jclass clazz, jlong nativeObject, jint w, jint h) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800245 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
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000252static void nativeSetFlags(JNIEnv* env, jclass clazz, jlong nativeObject, jint flags, jint mask) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800253 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
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000260static void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jlong nativeObject, jobject regionObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800261 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
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000285static void nativeSetAlpha(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat alpha) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800286 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
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000293static void nativeSetMatrix(JNIEnv* env, jclass clazz, jlong nativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800294 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
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000302static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong nativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800303 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
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000312static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong nativeObject, jint layerStack) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800313 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
Jesse Hall6a6bc212013-08-08 12:15:03 -0700333static 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
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800339static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000340 jobject tokenObj, jlong nativeSurfaceObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800341 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
342 if (token == NULL) return;
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800343 sp<IGraphicBufferProducer> bufferProducer;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800344 sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800345 if (sur != NULL) {
346 bufferProducer = sur->getIGraphicBufferProducer();
347 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800348 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
Dan Stoza00101052014-05-02 15:23:40 -0700370static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
371 jobject tokenObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800372 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
Dan Stoza00101052014-05-02 15:23:40 -0700373 if (token == NULL) return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800374
Dan Stoza00101052014-05-02 15:23:40 -0700375 Vector<DisplayInfo> configs;
376 if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
377 configs.size() == 0) {
378 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800379 }
380
Dan Stoza00101052014-05-02 15:23:40 -0700381 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;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800413}
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
Svetoslav1376d602014-03-13 11:17:26 -0700431static 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++) {
Allen Hairac5eda32014-04-24 11:50:37 -0700557 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
Svetoslav1376d602014-03-13 11:17:26 -0700558 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
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800576// ----------------------------------------------------------------------------
577
578static JNINativeMethod sSurfaceControlMethods[] = {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000579 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800580 (void*)nativeCreate },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000581 {"nativeRelease", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800582 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000583 {"nativeDestroy", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800584 (void*)nativeDestroy },
Dan Stoza9890e3412014-05-22 16:12:54 -0700585 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZ)Landroid/graphics/Bitmap;",
Mathias Agopian0449a402013-03-01 23:01:51 -0800586 (void*)nativeScreenshotBitmap },
Dan Stoza9890e3412014-05-22 16:12:54 -0700587 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;Landroid/graphics/Rect;IIIIZZ)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800588 (void*)nativeScreenshot },
589 {"nativeOpenTransaction", "()V",
590 (void*)nativeOpenTransaction },
591 {"nativeCloseTransaction", "()V",
592 (void*)nativeCloseTransaction },
593 {"nativeSetAnimationTransaction", "()V",
594 (void*)nativeSetAnimationTransaction },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000595 {"nativeSetLayer", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800596 (void*)nativeSetLayer },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000597 {"nativeSetPosition", "(JFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800598 (void*)nativeSetPosition },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000599 {"nativeSetSize", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800600 (void*)nativeSetSize },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000601 {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800602 (void*)nativeSetTransparentRegionHint },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000603 {"nativeSetAlpha", "(JF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800604 (void*)nativeSetAlpha },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000605 {"nativeSetMatrix", "(JFFFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800606 (void*)nativeSetMatrix },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000607 {"nativeSetFlags", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800608 (void*)nativeSetFlags },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000609 {"nativeSetWindowCrop", "(JIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800610 (void*)nativeSetWindowCrop },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000611 {"nativeSetLayerStack", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800612 (void*)nativeSetLayerStack },
613 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
614 (void*)nativeGetBuiltInDisplay },
615 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
616 (void*)nativeCreateDisplay },
Jesse Hall6a6bc212013-08-08 12:15:03 -0700617 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
618 (void*)nativeDestroyDisplay },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000619 {"nativeSetDisplaySurface", "(Landroid/os/IBinder;J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800620 (void*)nativeSetDisplaySurface },
621 {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V",
622 (void*)nativeSetDisplayLayerStack },
623 {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V",
624 (void*)nativeSetDisplayProjection },
Dan Stoza00101052014-05-02 15:23:40 -0700625 {"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 },
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800631 {"nativeBlankDisplay", "(Landroid/os/IBinder;)V",
632 (void*)nativeBlankDisplay },
633 {"nativeUnblankDisplay", "(Landroid/os/IBinder;)V",
634 (void*)nativeUnblankDisplay },
Svetoslav1376d602014-03-13 11:17:26 -0700635 {"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 },
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800643};
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");
Dan Stoza00101052014-05-02 15:23:40 -0700651 gPhysicalDisplayInfoClassInfo.clazz = static_cast<jclass>(env->NewGlobalRef(clazz));
652 gPhysicalDisplayInfoClassInfo.ctor = env->GetMethodID(gPhysicalDisplayInfoClassInfo.clazz,
653 "<init>", "()V");
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800654 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");
Svetoslav1376d602014-03-13 11:17:26 -0700661
Dan Stoza9890e3412014-05-22 16:12:54 -0700662 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
Svetoslav1376d602014-03-13 11:17:26 -0700668 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
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800680 return err;
681}
682
683};