blob: 8f30f5d0430aa6d37ffbf5db770c43b40c1862db [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
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,
120 jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800121 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
122 if (displayToken == NULL) {
123 return NULL;
124 }
125
Dan Stoza9890e3412014-05-22 16:12:54 -0700126 int left = env->GetIntField(sourceCropObj, gRectClassInfo.left);
127 int top = env->GetIntField(sourceCropObj, gRectClassInfo.top);
128 int right = env->GetIntField(sourceCropObj, gRectClassInfo.right);
129 int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom);
130 Rect sourceCrop(left, top, right, bottom);
131
Leon Scroggins IIIe3f98002014-06-10 13:19:35 -0400132 SkAutoTDelete<ScreenshotClient> screenshot(new ScreenshotClient());
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500133 status_t res;
134 if (width > 0 && height > 0) {
135 if (allLayers) {
Dan Stoza9890e3412014-05-22 16:12:54 -0700136 res = screenshot->update(displayToken, sourceCrop, width, height,
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500137 useIdentityTransform);
Dan Stoza9890e3412014-05-22 16:12:54 -0700138 } else {
139 res = screenshot->update(displayToken, sourceCrop, width, height,
140 minLayer, maxLayer, useIdentityTransform);
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500141 }
142 } else {
Dan Stoza9890e3412014-05-22 16:12:54 -0700143 res = screenshot->update(displayToken, sourceCrop, useIdentityTransform);
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500144 }
145 if (res != NO_ERROR) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800146 return NULL;
147 }
148
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500149 SkImageInfo screenshotInfo;
150 screenshotInfo.fWidth = screenshot->getWidth();
151 screenshotInfo.fHeight = screenshot->getHeight();
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800152
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500153 switch (screenshot->getFormat()) {
154 case PIXEL_FORMAT_RGBX_8888: {
155 screenshotInfo.fColorType = kRGBA_8888_SkColorType;
156 screenshotInfo.fAlphaType = kIgnore_SkAlphaType;
157 break;
158 }
159 case PIXEL_FORMAT_RGBA_8888: {
160 screenshotInfo.fColorType = kRGBA_8888_SkColorType;
161 screenshotInfo.fAlphaType = kPremul_SkAlphaType;
162 break;
163 }
164 case PIXEL_FORMAT_RGB_565: {
165 screenshotInfo.fColorType = kRGB_565_SkColorType;
166 screenshotInfo.fAlphaType = kIgnore_SkAlphaType;
167 break;
168 }
169 default: {
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500170 return NULL;
171 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800172 }
173
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500174 const ssize_t rowBytes =
175 screenshot->getStride() * android::bytesPerPixel(screenshot->getFormat());
176
177 SkBitmap* bitmap = new SkBitmap();
Mike Reedb9330552014-06-16 17:31:48 -0400178 bitmap->setInfo(screenshotInfo, (size_t)rowBytes);
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500179 if (screenshotInfo.fWidth > 0 && screenshotInfo.fHeight > 0) {
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500180 // takes ownership of ScreenshotClient
181 SkMallocPixelRef* pixels = SkMallocPixelRef::NewWithProc(screenshotInfo,
182 (size_t) rowBytes, NULL, (void*) screenshot->getPixels(), &DeleteScreenshot,
Thierry Strudel5474f332014-08-22 11:19:09 -0700183 (void*) (screenshot.get()));
184 screenshot.detach();
Leon Scroggins46cb9bd2014-03-06 15:36:39 -0500185 pixels->setImmutable();
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800186 bitmap->setPixelRef(pixels)->unref();
187 bitmap->lockPixels();
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800188 }
189
Chris Craik1abf5d62013-08-16 12:47:03 -0700190 return GraphicsJNI::createBitmap(env, bitmap,
191 GraphicsJNI::kBitmapCreateFlag_Premultiplied, NULL);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800192}
193
Dan Stoza9890e3412014-05-22 16:12:54 -0700194static void nativeScreenshot(JNIEnv* env, jclass clazz, jobject displayTokenObj,
195 jobject surfaceObj, jobject sourceCropObj, jint width, jint height,
196 jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform) {
Mathias Agopian0449a402013-03-01 23:01:51 -0800197 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
198 if (displayToken != NULL) {
199 sp<Surface> consumer = android_view_Surface_getSurface(env, surfaceObj);
200 if (consumer != NULL) {
Dan Stoza9890e3412014-05-22 16:12:54 -0700201 int left = env->GetIntField(sourceCropObj, gRectClassInfo.left);
202 int top = env->GetIntField(sourceCropObj, gRectClassInfo.top);
203 int right = env->GetIntField(sourceCropObj, gRectClassInfo.right);
204 int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom);
205 Rect sourceCrop(left, top, right, bottom);
206
Mathias Agopian0449a402013-03-01 23:01:51 -0800207 if (allLayers) {
208 minLayer = 0;
209 maxLayer = -1;
210 }
Dan Stoza9890e3412014-05-22 16:12:54 -0700211 ScreenshotClient::capture(displayToken,
212 consumer->getIGraphicBufferProducer(), sourceCrop,
Dan Stoza16ec12a2014-02-14 15:06:55 -0800213 width, height, uint32_t(minLayer), uint32_t(maxLayer),
214 useIdentityTransform);
Mathias Agopian0449a402013-03-01 23:01:51 -0800215 }
216 }
217}
218
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800219static void nativeOpenTransaction(JNIEnv* env, jclass clazz) {
220 SurfaceComposerClient::openGlobalTransaction();
221}
222
223static void nativeCloseTransaction(JNIEnv* env, jclass clazz) {
224 SurfaceComposerClient::closeGlobalTransaction();
225}
226
227static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz) {
228 SurfaceComposerClient::setAnimationTransaction();
229}
230
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000231static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong nativeObject, jint zorder) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800232 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
233 status_t err = ctrl->setLayer(zorder);
234 if (err < 0 && err != NO_INIT) {
235 doThrowIAE(env);
236 }
237}
238
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000239static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat x, jfloat y) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800240 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
241 status_t err = ctrl->setPosition(x, y);
242 if (err < 0 && err != NO_INIT) {
243 doThrowIAE(env);
244 }
245}
246
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000247static void nativeSetSize(JNIEnv* env, jclass clazz, jlong nativeObject, jint w, jint h) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800248 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
249 status_t err = ctrl->setSize(w, h);
250 if (err < 0 && err != NO_INIT) {
251 doThrowIAE(env);
252 }
253}
254
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000255static void nativeSetFlags(JNIEnv* env, jclass clazz, jlong nativeObject, jint flags, jint mask) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800256 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
257 status_t err = ctrl->setFlags(flags, mask);
258 if (err < 0 && err != NO_INIT) {
259 doThrowIAE(env);
260 }
261}
262
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000263static void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jlong nativeObject, jobject regionObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800264 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
265 SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj);
266 if (!region) {
267 doThrowIAE(env);
268 return;
269 }
270
271 const SkIRect& b(region->getBounds());
272 Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
273 if (region->isComplex()) {
274 SkRegion::Iterator it(*region);
275 while (!it.done()) {
276 const SkIRect& r(it.rect());
277 reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
278 it.next();
279 }
280 }
281
282 status_t err = ctrl->setTransparentRegionHint(reg);
283 if (err < 0 && err != NO_INIT) {
284 doThrowIAE(env);
285 }
286}
287
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000288static void nativeSetAlpha(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat alpha) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800289 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
290 status_t err = ctrl->setAlpha(alpha);
291 if (err < 0 && err != NO_INIT) {
292 doThrowIAE(env);
293 }
294}
295
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000296static void nativeSetMatrix(JNIEnv* env, jclass clazz, jlong nativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800297 jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy) {
298 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
299 status_t err = ctrl->setMatrix(dsdx, dtdx, dsdy, dtdy);
300 if (err < 0 && err != NO_INIT) {
301 doThrowIAE(env);
302 }
303}
304
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000305static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong nativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800306 jint l, jint t, jint r, jint b) {
307 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
308 Rect crop(l, t, r, b);
309 status_t err = ctrl->setCrop(crop);
310 if (err < 0 && err != NO_INIT) {
311 doThrowIAE(env);
312 }
313}
314
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000315static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong nativeObject, jint layerStack) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800316 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
317 status_t err = ctrl->setLayerStack(layerStack);
318 if (err < 0 && err != NO_INIT) {
319 doThrowIAE(env);
320 }
321}
322
323static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) {
324 sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id));
325 return javaObjectForIBinder(env, token);
326}
327
328static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj,
329 jboolean secure) {
330 ScopedUtfChars name(env, nameObj);
331 sp<IBinder> token(SurfaceComposerClient::createDisplay(
332 String8(name.c_str()), bool(secure)));
333 return javaObjectForIBinder(env, token);
334}
335
Jesse Hall6a6bc212013-08-08 12:15:03 -0700336static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
337 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
338 if (token == NULL) return;
339 SurfaceComposerClient::destroyDisplay(token);
340}
341
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800342static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000343 jobject tokenObj, jlong nativeSurfaceObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800344 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
345 if (token == NULL) return;
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800346 sp<IGraphicBufferProducer> bufferProducer;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800347 sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800348 if (sur != NULL) {
349 bufferProducer = sur->getIGraphicBufferProducer();
350 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800351 SurfaceComposerClient::setDisplaySurface(token, bufferProducer);
352}
353
354static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
355 jobject tokenObj, jint layerStack) {
356 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
357 if (token == NULL) return;
358
359 SurfaceComposerClient::setDisplayLayerStack(token, layerStack);
360}
361
362static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz,
363 jobject tokenObj, jint orientation,
364 jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom,
365 jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) {
366 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
367 if (token == NULL) return;
368 Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom);
369 Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom);
370 SurfaceComposerClient::setDisplayProjection(token, orientation, layerStackRect, displayRect);
371}
372
Michael Wright01e840f2014-06-26 16:03:25 -0700373static void nativeSetDisplaySize(JNIEnv* env, jclass clazz,
374 jobject tokenObj, jint width, jint height) {
375 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
376 if (token == NULL) return;
377 SurfaceComposerClient::setDisplaySize(token, width, height);
378}
379
Dan Stoza00101052014-05-02 15:23:40 -0700380static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
381 jobject tokenObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800382 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
Dan Stoza00101052014-05-02 15:23:40 -0700383 if (token == NULL) return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800384
Dan Stoza00101052014-05-02 15:23:40 -0700385 Vector<DisplayInfo> configs;
386 if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
387 configs.size() == 0) {
388 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800389 }
390
Dan Stoza00101052014-05-02 15:23:40 -0700391 jobjectArray configArray = env->NewObjectArray(configs.size(),
392 gPhysicalDisplayInfoClassInfo.clazz, NULL);
393
394 for (size_t c = 0; c < configs.size(); ++c) {
395 const DisplayInfo& info = configs[c];
396 jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
397 gPhysicalDisplayInfoClassInfo.ctor);
398 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
399 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
400 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
401 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
402 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
403 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
404 env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700405 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos,
406 info.appVsyncOffset);
407 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos,
408 info.presentationDeadline);
Dan Stoza00101052014-05-02 15:23:40 -0700409 env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
410 env->DeleteLocalRef(infoObj);
411 }
412
413 return configArray;
414}
415
416static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
417 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
418 if (token == NULL) return -1;
419 return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
420}
421
422static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
423 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
424 if (token == NULL) return JNI_FALSE;
425 status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
426 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800427}
428
Prashant Malanic55929a2014-05-25 01:59:21 -0700429static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800430 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
431 if (token == NULL) return;
432
Prashant Malanic55929a2014-05-25 01:59:21 -0700433 ALOGD_IF_SLOW(100, "Excessive delay in setPowerMode()");
434 SurfaceComposerClient::setDisplayPowerMode(token, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800435}
436
Svetoslav1376d602014-03-13 11:17:26 -0700437static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
438 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
439 status_t err = ctrl->clearLayerFrameStats();
440
441 if (err < 0 && err != NO_INIT) {
442 doThrowIAE(env);
443 }
444
445 // The other end is not ready, just report we failed.
446 if (err == NO_INIT) {
447 return JNI_FALSE;
448 }
449
450 return JNI_TRUE;
451}
452
453static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
454 jobject outStats) {
455 FrameStats stats;
456
457 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
458 status_t err = ctrl->getLayerFrameStats(&stats);
459 if (err < 0 && err != NO_INIT) {
460 doThrowIAE(env);
461 }
462
463 // The other end is not ready, fine just return empty stats.
464 if (err == NO_INIT) {
465 return JNI_FALSE;
466 }
467
468 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
469 size_t frameCount = stats.desiredPresentTimesNano.size();
470
471 jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
472 if (postedTimesNanoDst == NULL) {
473 return JNI_FALSE;
474 }
475
476 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
477 if (presentedTimesNanoDst == NULL) {
478 return JNI_FALSE;
479 }
480
481 jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
482 if (readyTimesNanoDst == NULL) {
483 return JNI_FALSE;
484 }
485
486 nsecs_t postedTimesNanoSrc[frameCount];
487 nsecs_t presentedTimesNanoSrc[frameCount];
488 nsecs_t readyTimesNanoSrc[frameCount];
489
490 for (size_t i = 0; i < frameCount; i++) {
491 nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
492 if (postedTimeNano == INT64_MAX) {
493 postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
494 }
495 postedTimesNanoSrc[i] = postedTimeNano;
496
497 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
498 if (presentedTimeNano == INT64_MAX) {
499 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
500 }
501 presentedTimesNanoSrc[i] = presentedTimeNano;
502
503 nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
504 if (readyTimeNano == INT64_MAX) {
505 readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
506 }
507 readyTimesNanoSrc[i] = readyTimeNano;
508 }
509
510 env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
511 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
512 env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
513
514 env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
515 postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
516
517 if (env->ExceptionCheck()) {
518 return JNI_FALSE;
519 }
520
521 return JNI_TRUE;
522}
523
524static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
525 status_t err = SurfaceComposerClient::clearAnimationFrameStats();
526
527 if (err < 0 && err != NO_INIT) {
528 doThrowIAE(env);
529 }
530
531 // The other end is not ready, just report we failed.
532 if (err == NO_INIT) {
533 return JNI_FALSE;
534 }
535
536 return JNI_TRUE;
537}
538
539static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
540 FrameStats stats;
541
542 status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
543 if (err < 0 && err != NO_INIT) {
544 doThrowIAE(env);
545 }
546
547 // The other end is not ready, fine just return empty stats.
548 if (err == NO_INIT) {
549 return JNI_FALSE;
550 }
551
552 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
553 size_t frameCount = stats.desiredPresentTimesNano.size();
554
555 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
556 if (presentedTimesNanoDst == NULL) {
557 return JNI_FALSE;
558 }
559
560 nsecs_t presentedTimesNanoSrc[frameCount];
561
562 for (size_t i = 0; i < frameCount; i++) {
Allen Hairac5eda32014-04-24 11:50:37 -0700563 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
Svetoslav1376d602014-03-13 11:17:26 -0700564 if (presentedTimeNano == INT64_MAX) {
565 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
566 }
567 presentedTimesNanoSrc[i] = presentedTimeNano;
568 }
569
570 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
571
572 env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
573 presentedTimesNanoDst);
574
575 if (env->ExceptionCheck()) {
576 return JNI_FALSE;
577 }
578
579 return JNI_TRUE;
580}
581
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800582// ----------------------------------------------------------------------------
583
584static JNINativeMethod sSurfaceControlMethods[] = {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000585 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800586 (void*)nativeCreate },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000587 {"nativeRelease", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800588 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000589 {"nativeDestroy", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800590 (void*)nativeDestroy },
Dan Stoza9890e3412014-05-22 16:12:54 -0700591 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZ)Landroid/graphics/Bitmap;",
Mathias Agopian0449a402013-03-01 23:01:51 -0800592 (void*)nativeScreenshotBitmap },
Dan Stoza9890e3412014-05-22 16:12:54 -0700593 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;Landroid/graphics/Rect;IIIIZZ)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800594 (void*)nativeScreenshot },
595 {"nativeOpenTransaction", "()V",
596 (void*)nativeOpenTransaction },
597 {"nativeCloseTransaction", "()V",
598 (void*)nativeCloseTransaction },
599 {"nativeSetAnimationTransaction", "()V",
600 (void*)nativeSetAnimationTransaction },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000601 {"nativeSetLayer", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800602 (void*)nativeSetLayer },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000603 {"nativeSetPosition", "(JFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800604 (void*)nativeSetPosition },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000605 {"nativeSetSize", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800606 (void*)nativeSetSize },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000607 {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800608 (void*)nativeSetTransparentRegionHint },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000609 {"nativeSetAlpha", "(JF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800610 (void*)nativeSetAlpha },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000611 {"nativeSetMatrix", "(JFFFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800612 (void*)nativeSetMatrix },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000613 {"nativeSetFlags", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800614 (void*)nativeSetFlags },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000615 {"nativeSetWindowCrop", "(JIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800616 (void*)nativeSetWindowCrop },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000617 {"nativeSetLayerStack", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800618 (void*)nativeSetLayerStack },
619 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
620 (void*)nativeGetBuiltInDisplay },
621 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
622 (void*)nativeCreateDisplay },
Jesse Hall6a6bc212013-08-08 12:15:03 -0700623 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
624 (void*)nativeDestroyDisplay },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000625 {"nativeSetDisplaySurface", "(Landroid/os/IBinder;J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800626 (void*)nativeSetDisplaySurface },
627 {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V",
628 (void*)nativeSetDisplayLayerStack },
629 {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V",
630 (void*)nativeSetDisplayProjection },
Michael Wright01e840f2014-06-26 16:03:25 -0700631 {"nativeSetDisplaySize", "(Landroid/os/IBinder;II)V",
632 (void*)nativeSetDisplaySize },
Dan Stoza00101052014-05-02 15:23:40 -0700633 {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
634 (void*)nativeGetDisplayConfigs },
635 {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
636 (void*)nativeGetActiveConfig },
637 {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
638 (void*)nativeSetActiveConfig },
Svetoslav1376d602014-03-13 11:17:26 -0700639 {"nativeClearContentFrameStats", "(J)Z",
640 (void*)nativeClearContentFrameStats },
641 {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
642 (void*)nativeGetContentFrameStats },
643 {"nativeClearAnimationFrameStats", "()Z",
644 (void*)nativeClearAnimationFrameStats },
645 {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
646 (void*)nativeGetAnimationFrameStats },
Prashant Malanic55929a2014-05-25 01:59:21 -0700647 {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
648 (void*)nativeSetDisplayPowerMode },
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800649};
650
651int register_android_view_SurfaceControl(JNIEnv* env)
652{
653 int err = AndroidRuntime::registerNativeMethods(env, "android/view/SurfaceControl",
654 sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
655
656 jclass clazz = env->FindClass("android/view/SurfaceControl$PhysicalDisplayInfo");
Dan Stoza00101052014-05-02 15:23:40 -0700657 gPhysicalDisplayInfoClassInfo.clazz = static_cast<jclass>(env->NewGlobalRef(clazz));
658 gPhysicalDisplayInfoClassInfo.ctor = env->GetMethodID(gPhysicalDisplayInfoClassInfo.clazz,
659 "<init>", "()V");
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800660 gPhysicalDisplayInfoClassInfo.width = env->GetFieldID(clazz, "width", "I");
661 gPhysicalDisplayInfoClassInfo.height = env->GetFieldID(clazz, "height", "I");
662 gPhysicalDisplayInfoClassInfo.refreshRate = env->GetFieldID(clazz, "refreshRate", "F");
663 gPhysicalDisplayInfoClassInfo.density = env->GetFieldID(clazz, "density", "F");
664 gPhysicalDisplayInfoClassInfo.xDpi = env->GetFieldID(clazz, "xDpi", "F");
665 gPhysicalDisplayInfoClassInfo.yDpi = env->GetFieldID(clazz, "yDpi", "F");
666 gPhysicalDisplayInfoClassInfo.secure = env->GetFieldID(clazz, "secure", "Z");
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700667 gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = env->GetFieldID(clazz,
668 "appVsyncOffsetNanos", "J");
669 gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = env->GetFieldID(clazz,
670 "presentationDeadlineNanos", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700671
Dan Stoza9890e3412014-05-22 16:12:54 -0700672 jclass rectClazz = env->FindClass("android/graphics/Rect");
673 gRectClassInfo.bottom = env->GetFieldID(rectClazz, "bottom", "I");
674 gRectClassInfo.left = env->GetFieldID(rectClazz, "left", "I");
675 gRectClassInfo.right = env->GetFieldID(rectClazz, "right", "I");
676 gRectClassInfo.top = env->GetFieldID(rectClazz, "top", "I");
677
Svetoslav1376d602014-03-13 11:17:26 -0700678 jclass frameStatsClazz = env->FindClass("android/view/FrameStats");
679 jfieldID undefined_time_nano_field = env->GetStaticFieldID(frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
680 nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
681
682 jclass contFrameStatsClazz = env->FindClass("android/view/WindowContentFrameStats");
683 gWindowContentFrameStatsClassInfo.init = env->GetMethodID(contFrameStatsClazz, "init", "(J[J[J[J)V");
684 gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
685
686 jclass animFrameStatsClazz = env->FindClass("android/view/WindowAnimationFrameStats");
687 gWindowAnimationFrameStatsClassInfo.init = env->GetMethodID(animFrameStatsClazz, "init", "(J[J)V");
688 gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
689
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800690 return err;
691}
692
693};