blob: 1dfe40a324b3a2eb5ed6bbbda50c8f6ac8da98d3 [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
Dan Stoza9890e3412014-05-22 16:12:54 -0700113static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz,
114 jobject displayTokenObj, jobject sourceCropObj, jint width, jint height,
Riley Andrews1d134062014-08-21 15:47:07 -0700115 jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform,
116 int rotation) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800117 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
118 if (displayToken == NULL) {
119 return NULL;
120 }
121
Dan Stoza9890e3412014-05-22 16:12:54 -0700122 int left = env->GetIntField(sourceCropObj, gRectClassInfo.left);
123 int top = env->GetIntField(sourceCropObj, gRectClassInfo.top);
124 int right = env->GetIntField(sourceCropObj, gRectClassInfo.right);
125 int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom);
126 Rect sourceCrop(left, top, right, bottom);
127
Ben Wagner60126ef2015-08-07 12:13:48 -0400128 std::unique_ptr<ScreenshotClient> screenshot(new ScreenshotClient());
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500129 status_t res;
Riley Andrews1d134062014-08-21 15:47:07 -0700130 if (allLayers) {
131 minLayer = 0;
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800132 maxLayer = -1;
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500133 }
Riley Andrews1d134062014-08-21 15:47:07 -0700134
135 res = screenshot->update(displayToken, sourceCrop, width, height,
136 minLayer, maxLayer, useIdentityTransform, static_cast<uint32_t>(rotation));
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500137 if (res != NO_ERROR) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800138 return NULL;
139 }
140
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400141 SkColorType colorType;
142 SkAlphaType alphaType;
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500143 switch (screenshot->getFormat()) {
144 case PIXEL_FORMAT_RGBX_8888: {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400145 colorType = kRGBA_8888_SkColorType;
146 alphaType = kOpaque_SkAlphaType;
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500147 break;
148 }
149 case PIXEL_FORMAT_RGBA_8888: {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400150 colorType = kRGBA_8888_SkColorType;
151 alphaType = kPremul_SkAlphaType;
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500152 break;
153 }
154 case PIXEL_FORMAT_RGB_565: {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400155 colorType = kRGB_565_SkColorType;
156 alphaType = kOpaque_SkAlphaType;
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500157 break;
158 }
159 default: {
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500160 return NULL;
161 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800162 }
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400163 SkImageInfo screenshotInfo = SkImageInfo::Make(screenshot->getWidth(),
164 screenshot->getHeight(),
165 colorType, alphaType);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800166
John Reckf29ed282015-04-07 07:32:03 -0700167 const size_t rowBytes =
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500168 screenshot->getStride() * android::bytesPerPixel(screenshot->getFormat());
169
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400170 if (!screenshotInfo.width() || !screenshotInfo.height()) {
John Reckf29ed282015-04-07 07:32:03 -0700171 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800172 }
173
John Reckf29ed282015-04-07 07:32:03 -0700174 Bitmap* bitmap = new Bitmap(
175 (void*) screenshot->getPixels(), (void*) screenshot.get(), DeleteScreenshot,
176 screenshotInfo, rowBytes, nullptr);
Ben Wagner60126ef2015-08-07 12:13:48 -0400177 screenshot.release();
John Reckae2e8b42015-05-06 14:55:05 -0700178 bitmap->peekAtPixelRef()->setImmutable();
John Reckf29ed282015-04-07 07:32:03 -0700179
Chris Craik1abf5d62013-08-16 12:47:03 -0700180 return GraphicsJNI::createBitmap(env, bitmap,
181 GraphicsJNI::kBitmapCreateFlag_Premultiplied, NULL);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800182}
183
Dan Stoza9890e3412014-05-22 16:12:54 -0700184static void nativeScreenshot(JNIEnv* env, jclass clazz, jobject displayTokenObj,
185 jobject surfaceObj, jobject sourceCropObj, jint width, jint height,
186 jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform) {
Mathias Agopian0449a402013-03-01 23:01:51 -0800187 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
188 if (displayToken != NULL) {
189 sp<Surface> consumer = android_view_Surface_getSurface(env, surfaceObj);
190 if (consumer != NULL) {
Dan Stoza9890e3412014-05-22 16:12:54 -0700191 int left = env->GetIntField(sourceCropObj, gRectClassInfo.left);
192 int top = env->GetIntField(sourceCropObj, gRectClassInfo.top);
193 int right = env->GetIntField(sourceCropObj, gRectClassInfo.right);
194 int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom);
195 Rect sourceCrop(left, top, right, bottom);
196
Mathias Agopian0449a402013-03-01 23:01:51 -0800197 if (allLayers) {
198 minLayer = 0;
199 maxLayer = -1;
200 }
Dan Stoza9890e3412014-05-22 16:12:54 -0700201 ScreenshotClient::capture(displayToken,
202 consumer->getIGraphicBufferProducer(), sourceCrop,
Dan Stoza16ec12a2014-02-14 15:06:55 -0800203 width, height, uint32_t(minLayer), uint32_t(maxLayer),
204 useIdentityTransform);
Mathias Agopian0449a402013-03-01 23:01:51 -0800205 }
206 }
207}
208
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800209static void nativeOpenTransaction(JNIEnv* env, jclass clazz) {
210 SurfaceComposerClient::openGlobalTransaction();
211}
212
213static void nativeCloseTransaction(JNIEnv* env, jclass clazz) {
214 SurfaceComposerClient::closeGlobalTransaction();
215}
216
217static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz) {
218 SurfaceComposerClient::setAnimationTransaction();
219}
220
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000221static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong nativeObject, jint zorder) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800222 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
223 status_t err = ctrl->setLayer(zorder);
224 if (err < 0 && err != NO_INIT) {
225 doThrowIAE(env);
226 }
227}
228
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000229static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat x, jfloat y) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800230 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
231 status_t err = ctrl->setPosition(x, y);
232 if (err < 0 && err != NO_INIT) {
233 doThrowIAE(env);
234 }
235}
236
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000237static void nativeSetSize(JNIEnv* env, jclass clazz, jlong nativeObject, jint w, jint h) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800238 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
239 status_t err = ctrl->setSize(w, h);
240 if (err < 0 && err != NO_INIT) {
241 doThrowIAE(env);
242 }
243}
244
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000245static void nativeSetFlags(JNIEnv* env, jclass clazz, jlong nativeObject, jint flags, jint mask) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800246 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
247 status_t err = ctrl->setFlags(flags, mask);
248 if (err < 0 && err != NO_INIT) {
249 doThrowIAE(env);
250 }
251}
252
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000253static void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jlong nativeObject, jobject regionObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800254 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
255 SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj);
256 if (!region) {
257 doThrowIAE(env);
258 return;
259 }
260
261 const SkIRect& b(region->getBounds());
262 Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
263 if (region->isComplex()) {
264 SkRegion::Iterator it(*region);
265 while (!it.done()) {
266 const SkIRect& r(it.rect());
267 reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
268 it.next();
269 }
270 }
271
272 status_t err = ctrl->setTransparentRegionHint(reg);
273 if (err < 0 && err != NO_INIT) {
274 doThrowIAE(env);
275 }
276}
277
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000278static void nativeSetAlpha(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat alpha) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800279 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
280 status_t err = ctrl->setAlpha(alpha);
281 if (err < 0 && err != NO_INIT) {
282 doThrowIAE(env);
283 }
284}
285
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000286static void nativeSetMatrix(JNIEnv* env, jclass clazz, jlong nativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800287 jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy) {
288 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
289 status_t err = ctrl->setMatrix(dsdx, dtdx, dsdy, dtdy);
290 if (err < 0 && err != NO_INIT) {
291 doThrowIAE(env);
292 }
293}
294
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000295static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong nativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800296 jint l, jint t, jint r, jint b) {
297 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
298 Rect crop(l, t, r, b);
299 status_t err = ctrl->setCrop(crop);
300 if (err < 0 && err != NO_INIT) {
301 doThrowIAE(env);
302 }
303}
304
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000305static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong nativeObject, jint layerStack) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800306 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
307 status_t err = ctrl->setLayerStack(layerStack);
308 if (err < 0 && err != NO_INIT) {
309 doThrowIAE(env);
310 }
311}
312
313static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) {
314 sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id));
315 return javaObjectForIBinder(env, token);
316}
317
318static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj,
319 jboolean secure) {
320 ScopedUtfChars name(env, nameObj);
321 sp<IBinder> token(SurfaceComposerClient::createDisplay(
322 String8(name.c_str()), bool(secure)));
323 return javaObjectForIBinder(env, token);
324}
325
Jesse Hall6a6bc212013-08-08 12:15:03 -0700326static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
327 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
328 if (token == NULL) return;
329 SurfaceComposerClient::destroyDisplay(token);
330}
331
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800332static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000333 jobject tokenObj, jlong nativeSurfaceObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800334 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
335 if (token == NULL) return;
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800336 sp<IGraphicBufferProducer> bufferProducer;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800337 sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800338 if (sur != NULL) {
339 bufferProducer = sur->getIGraphicBufferProducer();
340 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800341 SurfaceComposerClient::setDisplaySurface(token, bufferProducer);
342}
343
344static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
345 jobject tokenObj, jint layerStack) {
346 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
347 if (token == NULL) return;
348
349 SurfaceComposerClient::setDisplayLayerStack(token, layerStack);
350}
351
352static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz,
353 jobject tokenObj, jint orientation,
354 jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom,
355 jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) {
356 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
357 if (token == NULL) return;
358 Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom);
359 Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom);
360 SurfaceComposerClient::setDisplayProjection(token, orientation, layerStackRect, displayRect);
361}
362
Michael Wright01e840f2014-06-26 16:03:25 -0700363static void nativeSetDisplaySize(JNIEnv* env, jclass clazz,
364 jobject tokenObj, jint width, jint height) {
365 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
366 if (token == NULL) return;
367 SurfaceComposerClient::setDisplaySize(token, width, height);
368}
369
Dan Stoza00101052014-05-02 15:23:40 -0700370static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
371 jobject tokenObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800372 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
Dan Stoza00101052014-05-02 15:23:40 -0700373 if (token == NULL) return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800374
Dan Stoza00101052014-05-02 15:23:40 -0700375 Vector<DisplayInfo> configs;
376 if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
377 configs.size() == 0) {
378 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800379 }
380
Dan Stoza00101052014-05-02 15:23:40 -0700381 jobjectArray configArray = env->NewObjectArray(configs.size(),
382 gPhysicalDisplayInfoClassInfo.clazz, NULL);
383
384 for (size_t c = 0; c < configs.size(); ++c) {
385 const DisplayInfo& info = configs[c];
386 jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
387 gPhysicalDisplayInfoClassInfo.ctor);
388 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
389 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
390 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
391 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
392 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
393 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
394 env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700395 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos,
396 info.appVsyncOffset);
397 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos,
398 info.presentationDeadline);
Dan Stoza904f4852015-08-31 12:01:48 -0700399 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.colorTransform,
400 info.colorTransform);
Dan Stoza00101052014-05-02 15:23:40 -0700401 env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
402 env->DeleteLocalRef(infoObj);
403 }
404
405 return configArray;
406}
407
408static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
409 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
410 if (token == NULL) return -1;
411 return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
412}
413
414static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
415 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
416 if (token == NULL) return JNI_FALSE;
417 status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
418 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800419}
420
Prashant Malanic55929a2014-05-25 01:59:21 -0700421static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800422 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
423 if (token == NULL) return;
424
Prashant Malanic55929a2014-05-25 01:59:21 -0700425 ALOGD_IF_SLOW(100, "Excessive delay in setPowerMode()");
426 SurfaceComposerClient::setDisplayPowerMode(token, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800427}
428
Svetoslav1376d602014-03-13 11:17:26 -0700429static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
430 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
431 status_t err = ctrl->clearLayerFrameStats();
432
433 if (err < 0 && err != NO_INIT) {
434 doThrowIAE(env);
435 }
436
437 // The other end is not ready, just report we failed.
438 if (err == NO_INIT) {
439 return JNI_FALSE;
440 }
441
442 return JNI_TRUE;
443}
444
445static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
446 jobject outStats) {
447 FrameStats stats;
448
449 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
450 status_t err = ctrl->getLayerFrameStats(&stats);
451 if (err < 0 && err != NO_INIT) {
452 doThrowIAE(env);
453 }
454
455 // The other end is not ready, fine just return empty stats.
456 if (err == NO_INIT) {
457 return JNI_FALSE;
458 }
459
460 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
461 size_t frameCount = stats.desiredPresentTimesNano.size();
462
463 jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
464 if (postedTimesNanoDst == NULL) {
465 return JNI_FALSE;
466 }
467
468 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
469 if (presentedTimesNanoDst == NULL) {
470 return JNI_FALSE;
471 }
472
473 jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
474 if (readyTimesNanoDst == NULL) {
475 return JNI_FALSE;
476 }
477
478 nsecs_t postedTimesNanoSrc[frameCount];
479 nsecs_t presentedTimesNanoSrc[frameCount];
480 nsecs_t readyTimesNanoSrc[frameCount];
481
482 for (size_t i = 0; i < frameCount; i++) {
483 nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
484 if (postedTimeNano == INT64_MAX) {
485 postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
486 }
487 postedTimesNanoSrc[i] = postedTimeNano;
488
489 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
490 if (presentedTimeNano == INT64_MAX) {
491 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
492 }
493 presentedTimesNanoSrc[i] = presentedTimeNano;
494
495 nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
496 if (readyTimeNano == INT64_MAX) {
497 readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
498 }
499 readyTimesNanoSrc[i] = readyTimeNano;
500 }
501
502 env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
503 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
504 env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
505
506 env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
507 postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
508
509 if (env->ExceptionCheck()) {
510 return JNI_FALSE;
511 }
512
513 return JNI_TRUE;
514}
515
516static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
517 status_t err = SurfaceComposerClient::clearAnimationFrameStats();
518
519 if (err < 0 && err != NO_INIT) {
520 doThrowIAE(env);
521 }
522
523 // The other end is not ready, just report we failed.
524 if (err == NO_INIT) {
525 return JNI_FALSE;
526 }
527
528 return JNI_TRUE;
529}
530
531static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
532 FrameStats stats;
533
534 status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
535 if (err < 0 && err != NO_INIT) {
536 doThrowIAE(env);
537 }
538
539 // The other end is not ready, fine just return empty stats.
540 if (err == NO_INIT) {
541 return JNI_FALSE;
542 }
543
544 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
545 size_t frameCount = stats.desiredPresentTimesNano.size();
546
547 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
548 if (presentedTimesNanoDst == NULL) {
549 return JNI_FALSE;
550 }
551
552 nsecs_t presentedTimesNanoSrc[frameCount];
553
554 for (size_t i = 0; i < frameCount; i++) {
Allen Hairac5eda32014-04-24 11:50:37 -0700555 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
Svetoslav1376d602014-03-13 11:17:26 -0700556 if (presentedTimeNano == INT64_MAX) {
557 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
558 }
559 presentedTimesNanoSrc[i] = presentedTimeNano;
560 }
561
562 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
563
564 env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
565 presentedTimesNanoDst);
566
567 if (env->ExceptionCheck()) {
568 return JNI_FALSE;
569 }
570
571 return JNI_TRUE;
572}
573
Rob Carr64e516f2015-10-29 00:20:45 +0000574
575static void nativeDeferTransactionUntil(JNIEnv* env, jclass clazz, jlong nativeObject,
576 jobject handleObject, jlong frameNumber) {
577 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
578 sp<IBinder> handle = ibinderForJavaObject(env, handleObject);
579
580 ctrl->deferTransactionUntil(handle, frameNumber);
581}
582
583static jobject nativeGetHandle(JNIEnv* env, jclass clazz, jlong nativeObject) {
584 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
585
586 return javaObjectForIBinder(env, ctrl->getHandle());
587}
588
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800589// ----------------------------------------------------------------------------
590
Daniel Micay76f6a862015-09-19 17:31:01 -0400591static const JNINativeMethod sSurfaceControlMethods[] = {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000592 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800593 (void*)nativeCreate },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000594 {"nativeRelease", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800595 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000596 {"nativeDestroy", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800597 (void*)nativeDestroy },
Riley Andrews1d134062014-08-21 15:47:07 -0700598 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZI)Landroid/graphics/Bitmap;",
Mathias Agopian0449a402013-03-01 23:01:51 -0800599 (void*)nativeScreenshotBitmap },
Dan Stoza9890e3412014-05-22 16:12:54 -0700600 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;Landroid/graphics/Rect;IIIIZZ)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800601 (void*)nativeScreenshot },
602 {"nativeOpenTransaction", "()V",
603 (void*)nativeOpenTransaction },
604 {"nativeCloseTransaction", "()V",
605 (void*)nativeCloseTransaction },
606 {"nativeSetAnimationTransaction", "()V",
607 (void*)nativeSetAnimationTransaction },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000608 {"nativeSetLayer", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800609 (void*)nativeSetLayer },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000610 {"nativeSetPosition", "(JFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800611 (void*)nativeSetPosition },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000612 {"nativeSetSize", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800613 (void*)nativeSetSize },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000614 {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800615 (void*)nativeSetTransparentRegionHint },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000616 {"nativeSetAlpha", "(JF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800617 (void*)nativeSetAlpha },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000618 {"nativeSetMatrix", "(JFFFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800619 (void*)nativeSetMatrix },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000620 {"nativeSetFlags", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800621 (void*)nativeSetFlags },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000622 {"nativeSetWindowCrop", "(JIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800623 (void*)nativeSetWindowCrop },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000624 {"nativeSetLayerStack", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800625 (void*)nativeSetLayerStack },
626 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
627 (void*)nativeGetBuiltInDisplay },
628 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
629 (void*)nativeCreateDisplay },
Jesse Hall6a6bc212013-08-08 12:15:03 -0700630 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
631 (void*)nativeDestroyDisplay },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000632 {"nativeSetDisplaySurface", "(Landroid/os/IBinder;J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800633 (void*)nativeSetDisplaySurface },
634 {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V",
635 (void*)nativeSetDisplayLayerStack },
636 {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V",
637 (void*)nativeSetDisplayProjection },
Michael Wright01e840f2014-06-26 16:03:25 -0700638 {"nativeSetDisplaySize", "(Landroid/os/IBinder;II)V",
639 (void*)nativeSetDisplaySize },
Dan Stoza00101052014-05-02 15:23:40 -0700640 {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
641 (void*)nativeGetDisplayConfigs },
642 {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
643 (void*)nativeGetActiveConfig },
644 {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
645 (void*)nativeSetActiveConfig },
Svetoslav1376d602014-03-13 11:17:26 -0700646 {"nativeClearContentFrameStats", "(J)Z",
647 (void*)nativeClearContentFrameStats },
648 {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
649 (void*)nativeGetContentFrameStats },
650 {"nativeClearAnimationFrameStats", "()Z",
651 (void*)nativeClearAnimationFrameStats },
652 {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
653 (void*)nativeGetAnimationFrameStats },
Prashant Malanic55929a2014-05-25 01:59:21 -0700654 {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
655 (void*)nativeSetDisplayPowerMode },
Rob Carr64e516f2015-10-29 00:20:45 +0000656 {"nativeDeferTransactionUntil", "(JLandroid/os/IBinder;J)V",
657 (void*)nativeDeferTransactionUntil },
658 {"nativeGetHandle", "(J)Landroid/os/IBinder;",
659 (void*)nativeGetHandle }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800660};
661
662int register_android_view_SurfaceControl(JNIEnv* env)
663{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800664 int err = RegisterMethodsOrDie(env, "android/view/SurfaceControl",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800665 sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
666
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800667 jclass clazz = FindClassOrDie(env, "android/view/SurfaceControl$PhysicalDisplayInfo");
668 gPhysicalDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
669 gPhysicalDisplayInfoClassInfo.ctor = GetMethodIDOrDie(env,
670 gPhysicalDisplayInfoClassInfo.clazz, "<init>", "()V");
671 gPhysicalDisplayInfoClassInfo.width = GetFieldIDOrDie(env, clazz, "width", "I");
672 gPhysicalDisplayInfoClassInfo.height = GetFieldIDOrDie(env, clazz, "height", "I");
673 gPhysicalDisplayInfoClassInfo.refreshRate = GetFieldIDOrDie(env, clazz, "refreshRate", "F");
674 gPhysicalDisplayInfoClassInfo.density = GetFieldIDOrDie(env, clazz, "density", "F");
675 gPhysicalDisplayInfoClassInfo.xDpi = GetFieldIDOrDie(env, clazz, "xDpi", "F");
676 gPhysicalDisplayInfoClassInfo.yDpi = GetFieldIDOrDie(env, clazz, "yDpi", "F");
677 gPhysicalDisplayInfoClassInfo.secure = GetFieldIDOrDie(env, clazz, "secure", "Z");
678 gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = GetFieldIDOrDie(env,
679 clazz, "appVsyncOffsetNanos", "J");
680 gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env,
681 clazz, "presentationDeadlineNanos", "J");
Dan Stoza904f4852015-08-31 12:01:48 -0700682 gPhysicalDisplayInfoClassInfo.colorTransform = GetFieldIDOrDie(env, clazz,
683 "colorTransform", "I");
Svetoslav1376d602014-03-13 11:17:26 -0700684
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800685 jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect");
686 gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I");
687 gRectClassInfo.left = GetFieldIDOrDie(env, rectClazz, "left", "I");
688 gRectClassInfo.right = GetFieldIDOrDie(env, rectClazz, "right", "I");
689 gRectClassInfo.top = GetFieldIDOrDie(env, rectClazz, "top", "I");
Dan Stoza9890e3412014-05-22 16:12:54 -0700690
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800691 jclass frameStatsClazz = FindClassOrDie(env, "android/view/FrameStats");
692 jfieldID undefined_time_nano_field = GetStaticFieldIDOrDie(env,
693 frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700694 nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
695
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800696 jclass contFrameStatsClazz = FindClassOrDie(env, "android/view/WindowContentFrameStats");
697 gWindowContentFrameStatsClassInfo.init = GetMethodIDOrDie(env,
698 contFrameStatsClazz, "init", "(J[J[J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700699 gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
700
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800701 jclass animFrameStatsClazz = FindClassOrDie(env, "android/view/WindowAnimationFrameStats");
702 gWindowAnimationFrameStatsClassInfo.init = GetMethodIDOrDie(env,
703 animFrameStatsClazz, "init", "(J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700704 gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
705
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800706 return err;
707}
708
709};