blob: c838d038c4c29b6d67b8c2ad5eaabc8a8961de93 [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
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 Stoza904f4852015-08-31 12:01:48 -0700406 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.colorTransform,
407 info.colorTransform);
Dan Stoza00101052014-05-02 15:23:40 -0700408 env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
409 env->DeleteLocalRef(infoObj);
410 }
411
412 return configArray;
413}
414
415static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
416 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
417 if (token == NULL) return -1;
418 return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
419}
420
421static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
422 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
423 if (token == NULL) return JNI_FALSE;
424 status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
425 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800426}
427
Prashant Malanic55929a2014-05-25 01:59:21 -0700428static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800429 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
430 if (token == NULL) return;
431
Prashant Malanic55929a2014-05-25 01:59:21 -0700432 ALOGD_IF_SLOW(100, "Excessive delay in setPowerMode()");
433 SurfaceComposerClient::setDisplayPowerMode(token, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800434}
435
Svetoslav1376d602014-03-13 11:17:26 -0700436static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
437 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
438 status_t err = ctrl->clearLayerFrameStats();
439
440 if (err < 0 && err != NO_INIT) {
441 doThrowIAE(env);
442 }
443
444 // The other end is not ready, just report we failed.
445 if (err == NO_INIT) {
446 return JNI_FALSE;
447 }
448
449 return JNI_TRUE;
450}
451
452static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
453 jobject outStats) {
454 FrameStats stats;
455
456 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
457 status_t err = ctrl->getLayerFrameStats(&stats);
458 if (err < 0 && err != NO_INIT) {
459 doThrowIAE(env);
460 }
461
462 // The other end is not ready, fine just return empty stats.
463 if (err == NO_INIT) {
464 return JNI_FALSE;
465 }
466
467 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
468 size_t frameCount = stats.desiredPresentTimesNano.size();
469
470 jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
471 if (postedTimesNanoDst == NULL) {
472 return JNI_FALSE;
473 }
474
475 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
476 if (presentedTimesNanoDst == NULL) {
477 return JNI_FALSE;
478 }
479
480 jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
481 if (readyTimesNanoDst == NULL) {
482 return JNI_FALSE;
483 }
484
485 nsecs_t postedTimesNanoSrc[frameCount];
486 nsecs_t presentedTimesNanoSrc[frameCount];
487 nsecs_t readyTimesNanoSrc[frameCount];
488
489 for (size_t i = 0; i < frameCount; i++) {
490 nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
491 if (postedTimeNano == INT64_MAX) {
492 postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
493 }
494 postedTimesNanoSrc[i] = postedTimeNano;
495
496 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
497 if (presentedTimeNano == INT64_MAX) {
498 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
499 }
500 presentedTimesNanoSrc[i] = presentedTimeNano;
501
502 nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
503 if (readyTimeNano == INT64_MAX) {
504 readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
505 }
506 readyTimesNanoSrc[i] = readyTimeNano;
507 }
508
509 env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
510 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
511 env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
512
513 env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
514 postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
515
516 if (env->ExceptionCheck()) {
517 return JNI_FALSE;
518 }
519
520 return JNI_TRUE;
521}
522
523static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
524 status_t err = SurfaceComposerClient::clearAnimationFrameStats();
525
526 if (err < 0 && err != NO_INIT) {
527 doThrowIAE(env);
528 }
529
530 // The other end is not ready, just report we failed.
531 if (err == NO_INIT) {
532 return JNI_FALSE;
533 }
534
535 return JNI_TRUE;
536}
537
538static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
539 FrameStats stats;
540
541 status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
542 if (err < 0 && err != NO_INIT) {
543 doThrowIAE(env);
544 }
545
546 // The other end is not ready, fine just return empty stats.
547 if (err == NO_INIT) {
548 return JNI_FALSE;
549 }
550
551 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
552 size_t frameCount = stats.desiredPresentTimesNano.size();
553
554 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
555 if (presentedTimesNanoDst == NULL) {
556 return JNI_FALSE;
557 }
558
559 nsecs_t presentedTimesNanoSrc[frameCount];
560
561 for (size_t i = 0; i < frameCount; i++) {
Allen Hairac5eda32014-04-24 11:50:37 -0700562 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
Svetoslav1376d602014-03-13 11:17:26 -0700563 if (presentedTimeNano == INT64_MAX) {
564 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
565 }
566 presentedTimesNanoSrc[i] = presentedTimeNano;
567 }
568
569 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
570
571 env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
572 presentedTimesNanoDst);
573
574 if (env->ExceptionCheck()) {
575 return JNI_FALSE;
576 }
577
578 return JNI_TRUE;
579}
580
Rob Carr64e516f2015-10-29 00:20:45 +0000581
582static void nativeDeferTransactionUntil(JNIEnv* env, jclass clazz, jlong nativeObject,
583 jobject handleObject, jlong frameNumber) {
584 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
585 sp<IBinder> handle = ibinderForJavaObject(env, handleObject);
586
587 ctrl->deferTransactionUntil(handle, frameNumber);
588}
589
590static jobject nativeGetHandle(JNIEnv* env, jclass clazz, jlong nativeObject) {
591 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
592
593 return javaObjectForIBinder(env, ctrl->getHandle());
594}
595
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800596// ----------------------------------------------------------------------------
597
Daniel Micay76f6a862015-09-19 17:31:01 -0400598static const JNINativeMethod sSurfaceControlMethods[] = {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000599 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800600 (void*)nativeCreate },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000601 {"nativeRelease", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800602 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000603 {"nativeDestroy", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800604 (void*)nativeDestroy },
Chong Zhang47e36a32016-02-29 16:44:33 -0800605 {"nativeDisconnect", "(J)V",
606 (void*)nativeDisconnect },
Riley Andrews1d134062014-08-21 15:47:07 -0700607 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZI)Landroid/graphics/Bitmap;",
Mathias Agopian0449a402013-03-01 23:01:51 -0800608 (void*)nativeScreenshotBitmap },
Dan Stoza9890e3412014-05-22 16:12:54 -0700609 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;Landroid/graphics/Rect;IIIIZZ)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800610 (void*)nativeScreenshot },
611 {"nativeOpenTransaction", "()V",
612 (void*)nativeOpenTransaction },
613 {"nativeCloseTransaction", "()V",
614 (void*)nativeCloseTransaction },
615 {"nativeSetAnimationTransaction", "()V",
616 (void*)nativeSetAnimationTransaction },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000617 {"nativeSetLayer", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800618 (void*)nativeSetLayer },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000619 {"nativeSetPosition", "(JFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800620 (void*)nativeSetPosition },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000621 {"nativeSetSize", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800622 (void*)nativeSetSize },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000623 {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800624 (void*)nativeSetTransparentRegionHint },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000625 {"nativeSetAlpha", "(JF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800626 (void*)nativeSetAlpha },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000627 {"nativeSetMatrix", "(JFFFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800628 (void*)nativeSetMatrix },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000629 {"nativeSetFlags", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800630 (void*)nativeSetFlags },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000631 {"nativeSetWindowCrop", "(JIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800632 (void*)nativeSetWindowCrop },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000633 {"nativeSetLayerStack", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800634 (void*)nativeSetLayerStack },
635 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
636 (void*)nativeGetBuiltInDisplay },
637 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
638 (void*)nativeCreateDisplay },
Jesse Hall6a6bc212013-08-08 12:15:03 -0700639 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
640 (void*)nativeDestroyDisplay },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000641 {"nativeSetDisplaySurface", "(Landroid/os/IBinder;J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800642 (void*)nativeSetDisplaySurface },
643 {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V",
644 (void*)nativeSetDisplayLayerStack },
645 {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V",
646 (void*)nativeSetDisplayProjection },
Michael Wright01e840f2014-06-26 16:03:25 -0700647 {"nativeSetDisplaySize", "(Landroid/os/IBinder;II)V",
648 (void*)nativeSetDisplaySize },
Dan Stoza00101052014-05-02 15:23:40 -0700649 {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
650 (void*)nativeGetDisplayConfigs },
651 {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
652 (void*)nativeGetActiveConfig },
653 {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
654 (void*)nativeSetActiveConfig },
Svetoslav1376d602014-03-13 11:17:26 -0700655 {"nativeClearContentFrameStats", "(J)Z",
656 (void*)nativeClearContentFrameStats },
657 {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
658 (void*)nativeGetContentFrameStats },
659 {"nativeClearAnimationFrameStats", "()Z",
660 (void*)nativeClearAnimationFrameStats },
661 {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
662 (void*)nativeGetAnimationFrameStats },
Prashant Malanic55929a2014-05-25 01:59:21 -0700663 {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
664 (void*)nativeSetDisplayPowerMode },
Rob Carr64e516f2015-10-29 00:20:45 +0000665 {"nativeDeferTransactionUntil", "(JLandroid/os/IBinder;J)V",
666 (void*)nativeDeferTransactionUntil },
667 {"nativeGetHandle", "(J)Landroid/os/IBinder;",
668 (void*)nativeGetHandle }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800669};
670
671int register_android_view_SurfaceControl(JNIEnv* env)
672{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800673 int err = RegisterMethodsOrDie(env, "android/view/SurfaceControl",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800674 sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
675
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800676 jclass clazz = FindClassOrDie(env, "android/view/SurfaceControl$PhysicalDisplayInfo");
677 gPhysicalDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
678 gPhysicalDisplayInfoClassInfo.ctor = GetMethodIDOrDie(env,
679 gPhysicalDisplayInfoClassInfo.clazz, "<init>", "()V");
680 gPhysicalDisplayInfoClassInfo.width = GetFieldIDOrDie(env, clazz, "width", "I");
681 gPhysicalDisplayInfoClassInfo.height = GetFieldIDOrDie(env, clazz, "height", "I");
682 gPhysicalDisplayInfoClassInfo.refreshRate = GetFieldIDOrDie(env, clazz, "refreshRate", "F");
683 gPhysicalDisplayInfoClassInfo.density = GetFieldIDOrDie(env, clazz, "density", "F");
684 gPhysicalDisplayInfoClassInfo.xDpi = GetFieldIDOrDie(env, clazz, "xDpi", "F");
685 gPhysicalDisplayInfoClassInfo.yDpi = GetFieldIDOrDie(env, clazz, "yDpi", "F");
686 gPhysicalDisplayInfoClassInfo.secure = GetFieldIDOrDie(env, clazz, "secure", "Z");
687 gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = GetFieldIDOrDie(env,
688 clazz, "appVsyncOffsetNanos", "J");
689 gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env,
690 clazz, "presentationDeadlineNanos", "J");
Dan Stoza904f4852015-08-31 12:01:48 -0700691 gPhysicalDisplayInfoClassInfo.colorTransform = GetFieldIDOrDie(env, clazz,
692 "colorTransform", "I");
Svetoslav1376d602014-03-13 11:17:26 -0700693
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800694 jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect");
695 gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I");
696 gRectClassInfo.left = GetFieldIDOrDie(env, rectClazz, "left", "I");
697 gRectClassInfo.right = GetFieldIDOrDie(env, rectClazz, "right", "I");
698 gRectClassInfo.top = GetFieldIDOrDie(env, rectClazz, "top", "I");
Dan Stoza9890e3412014-05-22 16:12:54 -0700699
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800700 jclass frameStatsClazz = FindClassOrDie(env, "android/view/FrameStats");
701 jfieldID undefined_time_nano_field = GetStaticFieldIDOrDie(env,
702 frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700703 nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
704
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800705 jclass contFrameStatsClazz = FindClassOrDie(env, "android/view/WindowContentFrameStats");
706 gWindowContentFrameStatsClassInfo.init = GetMethodIDOrDie(env,
707 contFrameStatsClazz, "init", "(J[J[J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700708 gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
709
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800710 jclass animFrameStatsClazz = FindClassOrDie(env, "android/view/WindowAnimationFrameStats");
711 gWindowAnimationFrameStatsClassInfo.init = GetMethodIDOrDie(env,
712 animFrameStatsClazz, "init", "(J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700713 gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
714
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800715 return err;
716}
717
718};