blob: 030fcf8f3f5fb5756e9164992d3751bddcd4c065 [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
Prashant Malanic55929a2014-05-25 01:59:21 -0700415static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800416 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
417 if (token == NULL) return;
418
Prashant Malanic55929a2014-05-25 01:59:21 -0700419 ALOGD_IF_SLOW(100, "Excessive delay in setPowerMode()");
420 SurfaceComposerClient::setDisplayPowerMode(token, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800421}
422
Svetoslav1376d602014-03-13 11:17:26 -0700423static 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++) {
Allen Hairac5eda32014-04-24 11:50:37 -0700549 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
Svetoslav1376d602014-03-13 11:17:26 -0700550 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
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800568// ----------------------------------------------------------------------------
569
570static JNINativeMethod sSurfaceControlMethods[] = {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000571 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800572 (void*)nativeCreate },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000573 {"nativeRelease", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800574 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000575 {"nativeDestroy", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800576 (void*)nativeDestroy },
Dan Stoza9890e3412014-05-22 16:12:54 -0700577 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZ)Landroid/graphics/Bitmap;",
Mathias Agopian0449a402013-03-01 23:01:51 -0800578 (void*)nativeScreenshotBitmap },
Dan Stoza9890e3412014-05-22 16:12:54 -0700579 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;Landroid/graphics/Rect;IIIIZZ)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800580 (void*)nativeScreenshot },
581 {"nativeOpenTransaction", "()V",
582 (void*)nativeOpenTransaction },
583 {"nativeCloseTransaction", "()V",
584 (void*)nativeCloseTransaction },
585 {"nativeSetAnimationTransaction", "()V",
586 (void*)nativeSetAnimationTransaction },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000587 {"nativeSetLayer", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800588 (void*)nativeSetLayer },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000589 {"nativeSetPosition", "(JFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800590 (void*)nativeSetPosition },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000591 {"nativeSetSize", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800592 (void*)nativeSetSize },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000593 {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800594 (void*)nativeSetTransparentRegionHint },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000595 {"nativeSetAlpha", "(JF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800596 (void*)nativeSetAlpha },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000597 {"nativeSetMatrix", "(JFFFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800598 (void*)nativeSetMatrix },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000599 {"nativeSetFlags", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800600 (void*)nativeSetFlags },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000601 {"nativeSetWindowCrop", "(JIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800602 (void*)nativeSetWindowCrop },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000603 {"nativeSetLayerStack", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800604 (void*)nativeSetLayerStack },
605 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
606 (void*)nativeGetBuiltInDisplay },
607 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
608 (void*)nativeCreateDisplay },
Jesse Hall6a6bc212013-08-08 12:15:03 -0700609 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
610 (void*)nativeDestroyDisplay },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000611 {"nativeSetDisplaySurface", "(Landroid/os/IBinder;J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800612 (void*)nativeSetDisplaySurface },
613 {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V",
614 (void*)nativeSetDisplayLayerStack },
615 {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V",
616 (void*)nativeSetDisplayProjection },
Dan Stoza00101052014-05-02 15:23:40 -0700617 {"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 },
Svetoslav1376d602014-03-13 11:17:26 -0700623 {"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 },
Prashant Malanic55929a2014-05-25 01:59:21 -0700631 {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
632 (void*)nativeSetDisplayPowerMode },
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800633};
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");
Dan Stoza00101052014-05-02 15:23:40 -0700641 gPhysicalDisplayInfoClassInfo.clazz = static_cast<jclass>(env->NewGlobalRef(clazz));
642 gPhysicalDisplayInfoClassInfo.ctor = env->GetMethodID(gPhysicalDisplayInfoClassInfo.clazz,
643 "<init>", "()V");
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800644 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");
Svetoslav1376d602014-03-13 11:17:26 -0700651
Dan Stoza9890e3412014-05-22 16:12:54 -0700652 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
Svetoslav1376d602014-03-13 11:17:26 -0700658 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
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800670 return err;
671}
672
673};