blob: 13c373f6d3ce7aa24bab4dc7a7b01d9537f4b06c [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
Andreas Gampeed6b9df2014-11-20 22:02:20 -080029#include "core_jni_helpers.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
Leon Scroggins IIIe3f98002014-06-10 13:19:35 -040045#include "SkTemplates.h"
46
Mathias Agopian3866f0d2013-02-11 22:08:48 -080047// ----------------------------------------------------------------------------
48
49namespace android {
50
51static const char* const OutOfResourcesException =
52 "android/view/Surface$OutOfResourcesException";
53
54static struct {
Dan Stoza00101052014-05-02 15:23:40 -070055 jclass clazz;
56 jmethodID ctor;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080057 jfieldID width;
58 jfieldID height;
59 jfieldID refreshRate;
60 jfieldID density;
61 jfieldID xDpi;
62 jfieldID yDpi;
63 jfieldID secure;
Andy McFaddene8b1aeb2014-06-13 14:05:40 -070064 jfieldID appVsyncOffsetNanos;
65 jfieldID presentationDeadlineNanos;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080066} gPhysicalDisplayInfoClassInfo;
67
Dan Stoza9890e3412014-05-22 16:12:54 -070068static struct {
69 jfieldID bottom;
70 jfieldID left;
71 jfieldID right;
72 jfieldID top;
73} gRectClassInfo;
74
Leon Scroggins46cb9bd2014-03-06 15:36:39 -050075// Implements SkMallocPixelRef::ReleaseProc, to delete the screenshot on unref.
76void DeleteScreenshot(void* addr, void* context) {
77 SkASSERT(addr == ((ScreenshotClient*) context)->getPixels());
78 delete ((ScreenshotClient*) context);
79}
Mathias Agopian3866f0d2013-02-11 22:08:48 -080080
Svetoslav1376d602014-03-13 11:17:26 -070081static struct {
82 nsecs_t UNDEFINED_TIME_NANO;
83 jmethodID init;
84} gWindowContentFrameStatsClassInfo;
85
86static struct {
87 nsecs_t UNDEFINED_TIME_NANO;
88 jmethodID init;
89} gWindowAnimationFrameStatsClassInfo;
90
Mathias Agopian3866f0d2013-02-11 22:08:48 -080091// ----------------------------------------------------------------------------
92
Ashok Bhat36bef0b2014-01-20 20:08:01 +000093static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -080094 jstring nameStr, jint w, jint h, jint format, jint flags) {
95 ScopedUtfChars name(env, nameStr);
96 sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));
97 sp<SurfaceControl> surface = client->createSurface(
98 String8(name.c_str()), w, h, format, flags);
99 if (surface == NULL) {
100 jniThrowException(env, OutOfResourcesException, NULL);
101 return 0;
102 }
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800103 surface->incStrong((void *)nativeCreate);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000104 return reinterpret_cast<jlong>(surface.get());
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800105}
106
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000107static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800108 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800109 ctrl->decStrong((void *)nativeCreate);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800110}
111
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000112static void nativeDestroy(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800113 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
114 ctrl->clear();
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800115 ctrl->decStrong((void *)nativeCreate);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800116}
117
Dan Stoza9890e3412014-05-22 16:12:54 -0700118static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz,
119 jobject displayTokenObj, jobject sourceCropObj, jint width, jint height,
Riley Andrews1d134062014-08-21 15:47:07 -0700120 jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform,
121 int rotation) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800122 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
123 if (displayToken == NULL) {
124 return NULL;
125 }
126
Dan Stoza9890e3412014-05-22 16:12:54 -0700127 int left = env->GetIntField(sourceCropObj, gRectClassInfo.left);
128 int top = env->GetIntField(sourceCropObj, gRectClassInfo.top);
129 int right = env->GetIntField(sourceCropObj, gRectClassInfo.right);
130 int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom);
131 Rect sourceCrop(left, top, right, bottom);
132
Leon Scroggins IIIe3f98002014-06-10 13:19:35 -0400133 SkAutoTDelete<ScreenshotClient> screenshot(new ScreenshotClient());
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500134 status_t res;
Riley Andrews1d134062014-08-21 15:47:07 -0700135 if (allLayers) {
136 minLayer = 0;
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800137 maxLayer = -1;
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500138 }
Riley Andrews1d134062014-08-21 15:47:07 -0700139
140 res = screenshot->update(displayToken, sourceCrop, width, height,
141 minLayer, maxLayer, useIdentityTransform, static_cast<uint32_t>(rotation));
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500142 if (res != NO_ERROR) {
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: {
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500167 return NULL;
168 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800169 }
170
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500171 const ssize_t rowBytes =
172 screenshot->getStride() * android::bytesPerPixel(screenshot->getFormat());
173
174 SkBitmap* bitmap = new SkBitmap();
Mike Reedb9330552014-06-16 17:31:48 -0400175 bitmap->setInfo(screenshotInfo, (size_t)rowBytes);
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500176 if (screenshotInfo.fWidth > 0 && screenshotInfo.fHeight > 0) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500177 // takes ownership of ScreenshotClient
178 SkMallocPixelRef* pixels = SkMallocPixelRef::NewWithProc(screenshotInfo,
179 (size_t) rowBytes, NULL, (void*) screenshot->getPixels(), &DeleteScreenshot,
Thierry Strudel5474f332014-08-22 11:19:09 -0700180 (void*) (screenshot.get()));
181 screenshot.detach();
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500182 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
Michael Wright01e840f2014-06-26 16:03:25 -0700370static void nativeSetDisplaySize(JNIEnv* env, jclass clazz,
371 jobject tokenObj, jint width, jint height) {
372 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
373 if (token == NULL) return;
374 SurfaceComposerClient::setDisplaySize(token, width, height);
375}
376
Dan Stoza00101052014-05-02 15:23:40 -0700377static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
378 jobject tokenObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800379 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
Dan Stoza00101052014-05-02 15:23:40 -0700380 if (token == NULL) return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800381
Dan Stoza00101052014-05-02 15:23:40 -0700382 Vector<DisplayInfo> configs;
383 if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
384 configs.size() == 0) {
385 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800386 }
387
Dan Stoza00101052014-05-02 15:23:40 -0700388 jobjectArray configArray = env->NewObjectArray(configs.size(),
389 gPhysicalDisplayInfoClassInfo.clazz, NULL);
390
391 for (size_t c = 0; c < configs.size(); ++c) {
392 const DisplayInfo& info = configs[c];
393 jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
394 gPhysicalDisplayInfoClassInfo.ctor);
395 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
396 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
397 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
398 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
399 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
400 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
401 env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700402 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos,
403 info.appVsyncOffset);
404 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos,
405 info.presentationDeadline);
Dan Stoza00101052014-05-02 15:23:40 -0700406 env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
407 env->DeleteLocalRef(infoObj);
408 }
409
410 return configArray;
411}
412
413static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
414 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
415 if (token == NULL) return -1;
416 return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
417}
418
419static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
420 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
421 if (token == NULL) return JNI_FALSE;
422 status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
423 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800424}
425
Prashant Malanic55929a2014-05-25 01:59:21 -0700426static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800427 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
428 if (token == NULL) return;
429
Prashant Malanic55929a2014-05-25 01:59:21 -0700430 ALOGD_IF_SLOW(100, "Excessive delay in setPowerMode()");
431 SurfaceComposerClient::setDisplayPowerMode(token, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800432}
433
Svetoslav1376d602014-03-13 11:17:26 -0700434static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
435 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
436 status_t err = ctrl->clearLayerFrameStats();
437
438 if (err < 0 && err != NO_INIT) {
439 doThrowIAE(env);
440 }
441
442 // The other end is not ready, just report we failed.
443 if (err == NO_INIT) {
444 return JNI_FALSE;
445 }
446
447 return JNI_TRUE;
448}
449
450static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
451 jobject outStats) {
452 FrameStats stats;
453
454 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
455 status_t err = ctrl->getLayerFrameStats(&stats);
456 if (err < 0 && err != NO_INIT) {
457 doThrowIAE(env);
458 }
459
460 // The other end is not ready, fine just return empty stats.
461 if (err == NO_INIT) {
462 return JNI_FALSE;
463 }
464
465 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
466 size_t frameCount = stats.desiredPresentTimesNano.size();
467
468 jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
469 if (postedTimesNanoDst == NULL) {
470 return JNI_FALSE;
471 }
472
473 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
474 if (presentedTimesNanoDst == NULL) {
475 return JNI_FALSE;
476 }
477
478 jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
479 if (readyTimesNanoDst == NULL) {
480 return JNI_FALSE;
481 }
482
483 nsecs_t postedTimesNanoSrc[frameCount];
484 nsecs_t presentedTimesNanoSrc[frameCount];
485 nsecs_t readyTimesNanoSrc[frameCount];
486
487 for (size_t i = 0; i < frameCount; i++) {
488 nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
489 if (postedTimeNano == INT64_MAX) {
490 postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
491 }
492 postedTimesNanoSrc[i] = postedTimeNano;
493
494 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
495 if (presentedTimeNano == INT64_MAX) {
496 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
497 }
498 presentedTimesNanoSrc[i] = presentedTimeNano;
499
500 nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
501 if (readyTimeNano == INT64_MAX) {
502 readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
503 }
504 readyTimesNanoSrc[i] = readyTimeNano;
505 }
506
507 env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
508 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
509 env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
510
511 env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
512 postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
513
514 if (env->ExceptionCheck()) {
515 return JNI_FALSE;
516 }
517
518 return JNI_TRUE;
519}
520
521static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
522 status_t err = SurfaceComposerClient::clearAnimationFrameStats();
523
524 if (err < 0 && err != NO_INIT) {
525 doThrowIAE(env);
526 }
527
528 // The other end is not ready, just report we failed.
529 if (err == NO_INIT) {
530 return JNI_FALSE;
531 }
532
533 return JNI_TRUE;
534}
535
536static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
537 FrameStats stats;
538
539 status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
540 if (err < 0 && err != NO_INIT) {
541 doThrowIAE(env);
542 }
543
544 // The other end is not ready, fine just return empty stats.
545 if (err == NO_INIT) {
546 return JNI_FALSE;
547 }
548
549 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
550 size_t frameCount = stats.desiredPresentTimesNano.size();
551
552 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
553 if (presentedTimesNanoDst == NULL) {
554 return JNI_FALSE;
555 }
556
557 nsecs_t presentedTimesNanoSrc[frameCount];
558
559 for (size_t i = 0; i < frameCount; i++) {
Allen Hairac5eda32014-04-24 11:50:37 -0700560 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
Svetoslav1376d602014-03-13 11:17:26 -0700561 if (presentedTimeNano == INT64_MAX) {
562 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
563 }
564 presentedTimesNanoSrc[i] = presentedTimeNano;
565 }
566
567 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
568
569 env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
570 presentedTimesNanoDst);
571
572 if (env->ExceptionCheck()) {
573 return JNI_FALSE;
574 }
575
576 return JNI_TRUE;
577}
578
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800579// ----------------------------------------------------------------------------
580
581static JNINativeMethod sSurfaceControlMethods[] = {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000582 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800583 (void*)nativeCreate },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000584 {"nativeRelease", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800585 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000586 {"nativeDestroy", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800587 (void*)nativeDestroy },
Riley Andrews1d134062014-08-21 15:47:07 -0700588 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZI)Landroid/graphics/Bitmap;",
Mathias Agopian0449a402013-03-01 23:01:51 -0800589 (void*)nativeScreenshotBitmap },
Dan Stoza9890e3412014-05-22 16:12:54 -0700590 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;Landroid/graphics/Rect;IIIIZZ)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800591 (void*)nativeScreenshot },
592 {"nativeOpenTransaction", "()V",
593 (void*)nativeOpenTransaction },
594 {"nativeCloseTransaction", "()V",
595 (void*)nativeCloseTransaction },
596 {"nativeSetAnimationTransaction", "()V",
597 (void*)nativeSetAnimationTransaction },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000598 {"nativeSetLayer", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800599 (void*)nativeSetLayer },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000600 {"nativeSetPosition", "(JFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800601 (void*)nativeSetPosition },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000602 {"nativeSetSize", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800603 (void*)nativeSetSize },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000604 {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800605 (void*)nativeSetTransparentRegionHint },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000606 {"nativeSetAlpha", "(JF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800607 (void*)nativeSetAlpha },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000608 {"nativeSetMatrix", "(JFFFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800609 (void*)nativeSetMatrix },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000610 {"nativeSetFlags", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800611 (void*)nativeSetFlags },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000612 {"nativeSetWindowCrop", "(JIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800613 (void*)nativeSetWindowCrop },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000614 {"nativeSetLayerStack", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800615 (void*)nativeSetLayerStack },
616 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
617 (void*)nativeGetBuiltInDisplay },
618 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
619 (void*)nativeCreateDisplay },
Jesse Hall6a6bc212013-08-08 12:15:03 -0700620 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
621 (void*)nativeDestroyDisplay },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000622 {"nativeSetDisplaySurface", "(Landroid/os/IBinder;J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800623 (void*)nativeSetDisplaySurface },
624 {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V",
625 (void*)nativeSetDisplayLayerStack },
626 {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V",
627 (void*)nativeSetDisplayProjection },
Michael Wright01e840f2014-06-26 16:03:25 -0700628 {"nativeSetDisplaySize", "(Landroid/os/IBinder;II)V",
629 (void*)nativeSetDisplaySize },
Dan Stoza00101052014-05-02 15:23:40 -0700630 {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
631 (void*)nativeGetDisplayConfigs },
632 {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
633 (void*)nativeGetActiveConfig },
634 {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
635 (void*)nativeSetActiveConfig },
Svetoslav1376d602014-03-13 11:17:26 -0700636 {"nativeClearContentFrameStats", "(J)Z",
637 (void*)nativeClearContentFrameStats },
638 {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
639 (void*)nativeGetContentFrameStats },
640 {"nativeClearAnimationFrameStats", "()Z",
641 (void*)nativeClearAnimationFrameStats },
642 {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
643 (void*)nativeGetAnimationFrameStats },
Prashant Malanic55929a2014-05-25 01:59:21 -0700644 {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
645 (void*)nativeSetDisplayPowerMode },
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800646};
647
648int register_android_view_SurfaceControl(JNIEnv* env)
649{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800650 int err = RegisterMethodsOrDie(env, "android/view/SurfaceControl",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800651 sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
652
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800653 jclass clazz = FindClassOrDie(env, "android/view/SurfaceControl$PhysicalDisplayInfo");
654 gPhysicalDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
655 gPhysicalDisplayInfoClassInfo.ctor = GetMethodIDOrDie(env,
656 gPhysicalDisplayInfoClassInfo.clazz, "<init>", "()V");
657 gPhysicalDisplayInfoClassInfo.width = GetFieldIDOrDie(env, clazz, "width", "I");
658 gPhysicalDisplayInfoClassInfo.height = GetFieldIDOrDie(env, clazz, "height", "I");
659 gPhysicalDisplayInfoClassInfo.refreshRate = GetFieldIDOrDie(env, clazz, "refreshRate", "F");
660 gPhysicalDisplayInfoClassInfo.density = GetFieldIDOrDie(env, clazz, "density", "F");
661 gPhysicalDisplayInfoClassInfo.xDpi = GetFieldIDOrDie(env, clazz, "xDpi", "F");
662 gPhysicalDisplayInfoClassInfo.yDpi = GetFieldIDOrDie(env, clazz, "yDpi", "F");
663 gPhysicalDisplayInfoClassInfo.secure = GetFieldIDOrDie(env, clazz, "secure", "Z");
664 gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = GetFieldIDOrDie(env,
665 clazz, "appVsyncOffsetNanos", "J");
666 gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env,
667 clazz, "presentationDeadlineNanos", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700668
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800669 jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect");
670 gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I");
671 gRectClassInfo.left = GetFieldIDOrDie(env, rectClazz, "left", "I");
672 gRectClassInfo.right = GetFieldIDOrDie(env, rectClazz, "right", "I");
673 gRectClassInfo.top = GetFieldIDOrDie(env, rectClazz, "top", "I");
Dan Stoza9890e3412014-05-22 16:12:54 -0700674
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800675 jclass frameStatsClazz = FindClassOrDie(env, "android/view/FrameStats");
676 jfieldID undefined_time_nano_field = GetStaticFieldIDOrDie(env,
677 frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700678 nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
679
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800680 jclass contFrameStatsClazz = FindClassOrDie(env, "android/view/WindowContentFrameStats");
681 gWindowContentFrameStatsClassInfo.init = GetMethodIDOrDie(env,
682 contFrameStatsClazz, "init", "(J[J[J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700683 gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
684
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800685 jclass animFrameStatsClazz = FindClassOrDie(env, "android/view/WindowAnimationFrameStats");
686 gWindowAnimationFrameStatsClassInfo.init = GetMethodIDOrDie(env,
687 animFrameStatsClazz, "init", "(J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700688 gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
689
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800690 return err;
691}
692
693};