blob: 059013423f19befb3a52eb7cc19dd63d806d7eff [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
Mathias Agopian3866f0d2013-02-11 22:08:48 -080019#include "android_os_Parcel.h"
20#include "android_util_Binder.h"
John Reckf29ed282015-04-07 07:32:03 -070021#include "android/graphics/Bitmap.h"
Mathias Agopian3866f0d2013-02-11 22:08:48 -080022#include "android/graphics/GraphicsJNI.h"
23#include "android/graphics/Region.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080024#include "core_jni_helpers.h"
Ben Wagner60126ef2015-08-07 12:13:48 -040025
26#include <JNIHelp.h>
27#include <ScopedUtfChars.h>
Mathias Agopian0449a402013-03-01 23:01:51 -080028#include <android_runtime/android_view_Surface.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080029#include <android_runtime/android_view_SurfaceSession.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080030#include <gui/Surface.h>
31#include <gui/SurfaceComposerClient.h>
Ben Wagner60126ef2015-08-07 12:13:48 -040032#include <jni.h>
33#include <memory>
34#include <stdio.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080035#include <ui/DisplayInfo.h>
Svetoslav1376d602014-03-13 11:17:26 -070036#include <ui/FrameStats.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080037#include <ui/Rect.h>
38#include <ui/Region.h>
Mathias Agopian3866f0d2013-02-11 22:08:48 -080039#include <utils/Log.h>
40
Mathias Agopian3866f0d2013-02-11 22:08:48 -080041// ----------------------------------------------------------------------------
42
43namespace android {
44
45static const char* const OutOfResourcesException =
46 "android/view/Surface$OutOfResourcesException";
47
48static struct {
Dan Stoza00101052014-05-02 15:23:40 -070049 jclass clazz;
50 jmethodID ctor;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080051 jfieldID width;
52 jfieldID height;
53 jfieldID refreshRate;
54 jfieldID density;
55 jfieldID xDpi;
56 jfieldID yDpi;
57 jfieldID secure;
Andy McFaddene8b1aeb2014-06-13 14:05:40 -070058 jfieldID appVsyncOffsetNanos;
59 jfieldID presentationDeadlineNanos;
Dan Stoza904f4852015-08-31 12:01:48 -070060 jfieldID colorTransform;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080061} gPhysicalDisplayInfoClassInfo;
62
Dan Stoza9890e3412014-05-22 16:12:54 -070063static struct {
64 jfieldID bottom;
65 jfieldID left;
66 jfieldID right;
67 jfieldID top;
68} gRectClassInfo;
69
Leon Scroggins46cb9bd2014-03-06 15:36:39 -050070// Implements SkMallocPixelRef::ReleaseProc, to delete the screenshot on unref.
71void DeleteScreenshot(void* addr, void* context) {
72 SkASSERT(addr == ((ScreenshotClient*) context)->getPixels());
73 delete ((ScreenshotClient*) context);
74}
Mathias Agopian3866f0d2013-02-11 22:08:48 -080075
Svetoslav1376d602014-03-13 11:17:26 -070076static struct {
77 nsecs_t UNDEFINED_TIME_NANO;
78 jmethodID init;
79} gWindowContentFrameStatsClassInfo;
80
81static struct {
82 nsecs_t UNDEFINED_TIME_NANO;
83 jmethodID init;
84} gWindowAnimationFrameStatsClassInfo;
85
Mathias Agopian3866f0d2013-02-11 22:08:48 -080086// ----------------------------------------------------------------------------
87
Ashok Bhat36bef0b2014-01-20 20:08:01 +000088static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -080089 jstring nameStr, jint w, jint h, jint format, jint flags) {
90 ScopedUtfChars name(env, nameStr);
91 sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));
92 sp<SurfaceControl> surface = client->createSurface(
93 String8(name.c_str()), w, h, format, flags);
94 if (surface == NULL) {
95 jniThrowException(env, OutOfResourcesException, NULL);
96 return 0;
97 }
Mathias Agopianb1d90c82013-03-06 17:45:42 -080098 surface->incStrong((void *)nativeCreate);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000099 return reinterpret_cast<jlong>(surface.get());
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800100}
101
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000102static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800103 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800104 ctrl->decStrong((void *)nativeCreate);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800105}
106
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000107static void nativeDestroy(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800108 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
109 ctrl->clear();
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800110 ctrl->decStrong((void *)nativeCreate);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800111}
112
Chong Zhang47e36a32016-02-29 16:44:33 -0800113static void nativeDisconnect(JNIEnv* env, jclass clazz, jlong nativeObject) {
114 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
115 if (ctrl != NULL) {
116 ctrl->disconnect();
117 }
118}
119
Dan Stoza9890e3412014-05-22 16:12:54 -0700120static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz,
121 jobject displayTokenObj, jobject sourceCropObj, jint width, jint height,
Riley Andrews1d134062014-08-21 15:47:07 -0700122 jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform,
123 int rotation) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800124 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
125 if (displayToken == NULL) {
126 return NULL;
127 }
128
Dan Stoza9890e3412014-05-22 16:12:54 -0700129 int left = env->GetIntField(sourceCropObj, gRectClassInfo.left);
130 int top = env->GetIntField(sourceCropObj, gRectClassInfo.top);
131 int right = env->GetIntField(sourceCropObj, gRectClassInfo.right);
132 int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom);
133 Rect sourceCrop(left, top, right, bottom);
134
Ben Wagner60126ef2015-08-07 12:13:48 -0400135 std::unique_ptr<ScreenshotClient> screenshot(new ScreenshotClient());
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500136 status_t res;
Riley Andrews1d134062014-08-21 15:47:07 -0700137 if (allLayers) {
138 minLayer = 0;
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800139 maxLayer = -1;
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500140 }
Riley Andrews1d134062014-08-21 15:47:07 -0700141
142 res = screenshot->update(displayToken, sourceCrop, width, height,
143 minLayer, maxLayer, useIdentityTransform, static_cast<uint32_t>(rotation));
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500144 if (res != NO_ERROR) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800145 return NULL;
146 }
147
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400148 SkColorType colorType;
149 SkAlphaType alphaType;
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500150 switch (screenshot->getFormat()) {
151 case PIXEL_FORMAT_RGBX_8888: {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400152 colorType = kRGBA_8888_SkColorType;
153 alphaType = kOpaque_SkAlphaType;
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500154 break;
155 }
156 case PIXEL_FORMAT_RGBA_8888: {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400157 colorType = kRGBA_8888_SkColorType;
158 alphaType = kPremul_SkAlphaType;
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500159 break;
160 }
161 case PIXEL_FORMAT_RGB_565: {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400162 colorType = kRGB_565_SkColorType;
163 alphaType = kOpaque_SkAlphaType;
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500164 break;
165 }
166 default: {
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500167 return NULL;
168 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800169 }
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400170 SkImageInfo screenshotInfo = SkImageInfo::Make(screenshot->getWidth(),
171 screenshot->getHeight(),
172 colorType, alphaType);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800173
John Reckf29ed282015-04-07 07:32:03 -0700174 const size_t rowBytes =
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500175 screenshot->getStride() * android::bytesPerPixel(screenshot->getFormat());
176
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400177 if (!screenshotInfo.width() || !screenshotInfo.height()) {
John Reckf29ed282015-04-07 07:32:03 -0700178 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800179 }
180
John Reckf29ed282015-04-07 07:32:03 -0700181 Bitmap* bitmap = new Bitmap(
182 (void*) screenshot->getPixels(), (void*) screenshot.get(), DeleteScreenshot,
183 screenshotInfo, rowBytes, nullptr);
Ben Wagner60126ef2015-08-07 12:13:48 -0400184 screenshot.release();
John Reckae2e8b42015-05-06 14:55:05 -0700185 bitmap->peekAtPixelRef()->setImmutable();
John Reckf29ed282015-04-07 07:32:03 -0700186
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
Pablo Ceballos27982e62016-03-09 10:50:45 -0800312static void nativeSetFinalCrop(JNIEnv* env, jclass clazz, jlong nativeObject,
313 jint l, jint t, jint r, jint b) {
314 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
315 Rect crop(l, t, r, b);
316 status_t err = ctrl->setFinalCrop(crop);
317 if (err < 0 && err != NO_INIT) {
318 doThrowIAE(env);
319 }
320}
321
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000322static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong nativeObject, jint layerStack) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800323 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
324 status_t err = ctrl->setLayerStack(layerStack);
325 if (err < 0 && err != NO_INIT) {
326 doThrowIAE(env);
327 }
328}
329
330static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) {
331 sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id));
332 return javaObjectForIBinder(env, token);
333}
334
335static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj,
336 jboolean secure) {
337 ScopedUtfChars name(env, nameObj);
338 sp<IBinder> token(SurfaceComposerClient::createDisplay(
339 String8(name.c_str()), bool(secure)));
340 return javaObjectForIBinder(env, token);
341}
342
Jesse Hall6a6bc212013-08-08 12:15:03 -0700343static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
344 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
345 if (token == NULL) return;
346 SurfaceComposerClient::destroyDisplay(token);
347}
348
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800349static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000350 jobject tokenObj, jlong nativeSurfaceObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800351 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
352 if (token == NULL) return;
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800353 sp<IGraphicBufferProducer> bufferProducer;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800354 sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800355 if (sur != NULL) {
356 bufferProducer = sur->getIGraphicBufferProducer();
357 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800358 SurfaceComposerClient::setDisplaySurface(token, bufferProducer);
359}
360
361static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
362 jobject tokenObj, jint layerStack) {
363 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
364 if (token == NULL) return;
365
366 SurfaceComposerClient::setDisplayLayerStack(token, layerStack);
367}
368
369static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz,
370 jobject tokenObj, jint orientation,
371 jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom,
372 jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) {
373 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
374 if (token == NULL) return;
375 Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom);
376 Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom);
377 SurfaceComposerClient::setDisplayProjection(token, orientation, layerStackRect, displayRect);
378}
379
Michael Wright01e840f2014-06-26 16:03:25 -0700380static void nativeSetDisplaySize(JNIEnv* env, jclass clazz,
381 jobject tokenObj, jint width, jint height) {
382 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
383 if (token == NULL) return;
384 SurfaceComposerClient::setDisplaySize(token, width, height);
385}
386
Dan Stoza00101052014-05-02 15:23:40 -0700387static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
388 jobject tokenObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800389 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
Dan Stoza00101052014-05-02 15:23:40 -0700390 if (token == NULL) return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800391
Dan Stoza00101052014-05-02 15:23:40 -0700392 Vector<DisplayInfo> configs;
393 if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
394 configs.size() == 0) {
395 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800396 }
397
Dan Stoza00101052014-05-02 15:23:40 -0700398 jobjectArray configArray = env->NewObjectArray(configs.size(),
399 gPhysicalDisplayInfoClassInfo.clazz, NULL);
400
401 for (size_t c = 0; c < configs.size(); ++c) {
402 const DisplayInfo& info = configs[c];
403 jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
404 gPhysicalDisplayInfoClassInfo.ctor);
405 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
406 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
407 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
408 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
409 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
410 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
411 env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700412 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos,
413 info.appVsyncOffset);
414 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos,
415 info.presentationDeadline);
Dan Stoza904f4852015-08-31 12:01:48 -0700416 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.colorTransform,
417 info.colorTransform);
Dan Stoza00101052014-05-02 15:23:40 -0700418 env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
419 env->DeleteLocalRef(infoObj);
420 }
421
422 return configArray;
423}
424
425static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
426 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
427 if (token == NULL) return -1;
428 return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
429}
430
431static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
432 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
433 if (token == NULL) return JNI_FALSE;
434 status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
435 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800436}
437
Prashant Malanic55929a2014-05-25 01:59:21 -0700438static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800439 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
440 if (token == NULL) return;
441
Prashant Malanic55929a2014-05-25 01:59:21 -0700442 ALOGD_IF_SLOW(100, "Excessive delay in setPowerMode()");
443 SurfaceComposerClient::setDisplayPowerMode(token, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800444}
445
Svetoslav1376d602014-03-13 11:17:26 -0700446static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
447 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
448 status_t err = ctrl->clearLayerFrameStats();
449
450 if (err < 0 && err != NO_INIT) {
451 doThrowIAE(env);
452 }
453
454 // The other end is not ready, just report we failed.
455 if (err == NO_INIT) {
456 return JNI_FALSE;
457 }
458
459 return JNI_TRUE;
460}
461
462static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
463 jobject outStats) {
464 FrameStats stats;
465
466 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
467 status_t err = ctrl->getLayerFrameStats(&stats);
468 if (err < 0 && err != NO_INIT) {
469 doThrowIAE(env);
470 }
471
472 // The other end is not ready, fine just return empty stats.
473 if (err == NO_INIT) {
474 return JNI_FALSE;
475 }
476
477 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
478 size_t frameCount = stats.desiredPresentTimesNano.size();
479
480 jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
481 if (postedTimesNanoDst == NULL) {
482 return JNI_FALSE;
483 }
484
485 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
486 if (presentedTimesNanoDst == NULL) {
487 return JNI_FALSE;
488 }
489
490 jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
491 if (readyTimesNanoDst == NULL) {
492 return JNI_FALSE;
493 }
494
495 nsecs_t postedTimesNanoSrc[frameCount];
496 nsecs_t presentedTimesNanoSrc[frameCount];
497 nsecs_t readyTimesNanoSrc[frameCount];
498
499 for (size_t i = 0; i < frameCount; i++) {
500 nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
501 if (postedTimeNano == INT64_MAX) {
502 postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
503 }
504 postedTimesNanoSrc[i] = postedTimeNano;
505
506 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
507 if (presentedTimeNano == INT64_MAX) {
508 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
509 }
510 presentedTimesNanoSrc[i] = presentedTimeNano;
511
512 nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
513 if (readyTimeNano == INT64_MAX) {
514 readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
515 }
516 readyTimesNanoSrc[i] = readyTimeNano;
517 }
518
519 env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
520 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
521 env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
522
523 env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
524 postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
525
526 if (env->ExceptionCheck()) {
527 return JNI_FALSE;
528 }
529
530 return JNI_TRUE;
531}
532
533static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
534 status_t err = SurfaceComposerClient::clearAnimationFrameStats();
535
536 if (err < 0 && err != NO_INIT) {
537 doThrowIAE(env);
538 }
539
540 // The other end is not ready, just report we failed.
541 if (err == NO_INIT) {
542 return JNI_FALSE;
543 }
544
545 return JNI_TRUE;
546}
547
548static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
549 FrameStats stats;
550
551 status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
552 if (err < 0 && err != NO_INIT) {
553 doThrowIAE(env);
554 }
555
556 // The other end is not ready, fine just return empty stats.
557 if (err == NO_INIT) {
558 return JNI_FALSE;
559 }
560
561 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
562 size_t frameCount = stats.desiredPresentTimesNano.size();
563
564 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
565 if (presentedTimesNanoDst == NULL) {
566 return JNI_FALSE;
567 }
568
569 nsecs_t presentedTimesNanoSrc[frameCount];
570
571 for (size_t i = 0; i < frameCount; i++) {
Allen Hairac5eda32014-04-24 11:50:37 -0700572 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
Svetoslav1376d602014-03-13 11:17:26 -0700573 if (presentedTimeNano == INT64_MAX) {
574 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
575 }
576 presentedTimesNanoSrc[i] = presentedTimeNano;
577 }
578
579 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
580
581 env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
582 presentedTimesNanoDst);
583
584 if (env->ExceptionCheck()) {
585 return JNI_FALSE;
586 }
587
588 return JNI_TRUE;
589}
590
Rob Carr64e516f2015-10-29 00:20:45 +0000591
592static void nativeDeferTransactionUntil(JNIEnv* env, jclass clazz, jlong nativeObject,
593 jobject handleObject, jlong frameNumber) {
594 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
595 sp<IBinder> handle = ibinderForJavaObject(env, handleObject);
596
597 ctrl->deferTransactionUntil(handle, frameNumber);
598}
599
Robert Carr1ca6a332016-04-11 18:00:43 -0700600static void nativeSetOverrideScalingMode(JNIEnv* env, jclass clazz, jlong nativeObject,
601 jint scalingMode) {
602 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
603
604 ctrl->setOverrideScalingMode(scalingMode);
605}
606
Rob Carr64e516f2015-10-29 00:20:45 +0000607static jobject nativeGetHandle(JNIEnv* env, jclass clazz, jlong nativeObject) {
608 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
609
610 return javaObjectForIBinder(env, ctrl->getHandle());
611}
612
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800613// ----------------------------------------------------------------------------
614
Daniel Micay76f6a862015-09-19 17:31:01 -0400615static const JNINativeMethod sSurfaceControlMethods[] = {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000616 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800617 (void*)nativeCreate },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000618 {"nativeRelease", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800619 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000620 {"nativeDestroy", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800621 (void*)nativeDestroy },
Chong Zhang47e36a32016-02-29 16:44:33 -0800622 {"nativeDisconnect", "(J)V",
623 (void*)nativeDisconnect },
Riley Andrews1d134062014-08-21 15:47:07 -0700624 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZI)Landroid/graphics/Bitmap;",
Mathias Agopian0449a402013-03-01 23:01:51 -0800625 (void*)nativeScreenshotBitmap },
Dan Stoza9890e3412014-05-22 16:12:54 -0700626 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;Landroid/graphics/Rect;IIIIZZ)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800627 (void*)nativeScreenshot },
628 {"nativeOpenTransaction", "()V",
629 (void*)nativeOpenTransaction },
630 {"nativeCloseTransaction", "()V",
631 (void*)nativeCloseTransaction },
632 {"nativeSetAnimationTransaction", "()V",
633 (void*)nativeSetAnimationTransaction },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000634 {"nativeSetLayer", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800635 (void*)nativeSetLayer },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000636 {"nativeSetPosition", "(JFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800637 (void*)nativeSetPosition },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000638 {"nativeSetSize", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800639 (void*)nativeSetSize },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000640 {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800641 (void*)nativeSetTransparentRegionHint },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000642 {"nativeSetAlpha", "(JF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800643 (void*)nativeSetAlpha },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000644 {"nativeSetMatrix", "(JFFFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800645 (void*)nativeSetMatrix },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000646 {"nativeSetFlags", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800647 (void*)nativeSetFlags },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000648 {"nativeSetWindowCrop", "(JIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800649 (void*)nativeSetWindowCrop },
Pablo Ceballos27982e62016-03-09 10:50:45 -0800650 {"nativeSetFinalCrop", "(JIIII)V",
651 (void*)nativeSetFinalCrop },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000652 {"nativeSetLayerStack", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800653 (void*)nativeSetLayerStack },
654 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
655 (void*)nativeGetBuiltInDisplay },
656 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
657 (void*)nativeCreateDisplay },
Jesse Hall6a6bc212013-08-08 12:15:03 -0700658 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
659 (void*)nativeDestroyDisplay },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000660 {"nativeSetDisplaySurface", "(Landroid/os/IBinder;J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800661 (void*)nativeSetDisplaySurface },
662 {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V",
663 (void*)nativeSetDisplayLayerStack },
664 {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V",
665 (void*)nativeSetDisplayProjection },
Michael Wright01e840f2014-06-26 16:03:25 -0700666 {"nativeSetDisplaySize", "(Landroid/os/IBinder;II)V",
667 (void*)nativeSetDisplaySize },
Dan Stoza00101052014-05-02 15:23:40 -0700668 {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
669 (void*)nativeGetDisplayConfigs },
670 {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
671 (void*)nativeGetActiveConfig },
672 {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
673 (void*)nativeSetActiveConfig },
Svetoslav1376d602014-03-13 11:17:26 -0700674 {"nativeClearContentFrameStats", "(J)Z",
675 (void*)nativeClearContentFrameStats },
676 {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
677 (void*)nativeGetContentFrameStats },
678 {"nativeClearAnimationFrameStats", "()Z",
679 (void*)nativeClearAnimationFrameStats },
680 {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
681 (void*)nativeGetAnimationFrameStats },
Prashant Malanic55929a2014-05-25 01:59:21 -0700682 {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
683 (void*)nativeSetDisplayPowerMode },
Rob Carr64e516f2015-10-29 00:20:45 +0000684 {"nativeDeferTransactionUntil", "(JLandroid/os/IBinder;J)V",
685 (void*)nativeDeferTransactionUntil },
Robert Carr1ca6a332016-04-11 18:00:43 -0700686 {"nativeSetOverrideScalingMode", "(JI)V",
687 (void*)nativeSetOverrideScalingMode },
Rob Carr64e516f2015-10-29 00:20:45 +0000688 {"nativeGetHandle", "(J)Landroid/os/IBinder;",
689 (void*)nativeGetHandle }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800690};
691
692int register_android_view_SurfaceControl(JNIEnv* env)
693{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800694 int err = RegisterMethodsOrDie(env, "android/view/SurfaceControl",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800695 sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
696
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800697 jclass clazz = FindClassOrDie(env, "android/view/SurfaceControl$PhysicalDisplayInfo");
698 gPhysicalDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
699 gPhysicalDisplayInfoClassInfo.ctor = GetMethodIDOrDie(env,
700 gPhysicalDisplayInfoClassInfo.clazz, "<init>", "()V");
701 gPhysicalDisplayInfoClassInfo.width = GetFieldIDOrDie(env, clazz, "width", "I");
702 gPhysicalDisplayInfoClassInfo.height = GetFieldIDOrDie(env, clazz, "height", "I");
703 gPhysicalDisplayInfoClassInfo.refreshRate = GetFieldIDOrDie(env, clazz, "refreshRate", "F");
704 gPhysicalDisplayInfoClassInfo.density = GetFieldIDOrDie(env, clazz, "density", "F");
705 gPhysicalDisplayInfoClassInfo.xDpi = GetFieldIDOrDie(env, clazz, "xDpi", "F");
706 gPhysicalDisplayInfoClassInfo.yDpi = GetFieldIDOrDie(env, clazz, "yDpi", "F");
707 gPhysicalDisplayInfoClassInfo.secure = GetFieldIDOrDie(env, clazz, "secure", "Z");
708 gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = GetFieldIDOrDie(env,
709 clazz, "appVsyncOffsetNanos", "J");
710 gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env,
711 clazz, "presentationDeadlineNanos", "J");
Dan Stoza904f4852015-08-31 12:01:48 -0700712 gPhysicalDisplayInfoClassInfo.colorTransform = GetFieldIDOrDie(env, clazz,
713 "colorTransform", "I");
Svetoslav1376d602014-03-13 11:17:26 -0700714
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800715 jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect");
716 gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I");
717 gRectClassInfo.left = GetFieldIDOrDie(env, rectClazz, "left", "I");
718 gRectClassInfo.right = GetFieldIDOrDie(env, rectClazz, "right", "I");
719 gRectClassInfo.top = GetFieldIDOrDie(env, rectClazz, "top", "I");
Dan Stoza9890e3412014-05-22 16:12:54 -0700720
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800721 jclass frameStatsClazz = FindClassOrDie(env, "android/view/FrameStats");
722 jfieldID undefined_time_nano_field = GetStaticFieldIDOrDie(env,
723 frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700724 nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
725
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800726 jclass contFrameStatsClazz = FindClassOrDie(env, "android/view/WindowContentFrameStats");
727 gWindowContentFrameStatsClassInfo.init = GetMethodIDOrDie(env,
728 contFrameStatsClazz, "init", "(J[J[J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700729 gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
730
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800731 jclass animFrameStatsClazz = FindClassOrDie(env, "android/view/WindowAnimationFrameStats");
732 gWindowAnimationFrameStatsClassInfo.init = GetMethodIDOrDie(env,
733 animFrameStatsClazz, "init", "(J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700734 gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
735
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800736 return err;
737}
738
739};