blob: 54be41086bdf87e3e2846867a8bce2806bf78695 [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;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080060} gPhysicalDisplayInfoClassInfo;
61
Dan Stoza9890e3412014-05-22 16:12:54 -070062static struct {
63 jfieldID bottom;
64 jfieldID left;
65 jfieldID right;
66 jfieldID top;
67} gRectClassInfo;
68
Leon Scroggins46cb9bd2014-03-06 15:36:39 -050069// Implements SkMallocPixelRef::ReleaseProc, to delete the screenshot on unref.
70void DeleteScreenshot(void* addr, void* context) {
71 SkASSERT(addr == ((ScreenshotClient*) context)->getPixels());
72 delete ((ScreenshotClient*) context);
73}
Mathias Agopian3866f0d2013-02-11 22:08:48 -080074
Svetoslav1376d602014-03-13 11:17:26 -070075static struct {
76 nsecs_t UNDEFINED_TIME_NANO;
77 jmethodID init;
78} gWindowContentFrameStatsClassInfo;
79
80static struct {
81 nsecs_t UNDEFINED_TIME_NANO;
82 jmethodID init;
83} gWindowAnimationFrameStatsClassInfo;
84
Mathias Agopian3866f0d2013-02-11 22:08:48 -080085// ----------------------------------------------------------------------------
86
Ashok Bhat36bef0b2014-01-20 20:08:01 +000087static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,
Mathias Agopian3866f0d2013-02-11 22:08:48 -080088 jstring nameStr, jint w, jint h, jint format, jint flags) {
89 ScopedUtfChars name(env, nameStr);
90 sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));
91 sp<SurfaceControl> surface = client->createSurface(
92 String8(name.c_str()), w, h, format, flags);
93 if (surface == NULL) {
94 jniThrowException(env, OutOfResourcesException, NULL);
95 return 0;
96 }
Mathias Agopianb1d90c82013-03-06 17:45:42 -080097 surface->incStrong((void *)nativeCreate);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000098 return reinterpret_cast<jlong>(surface.get());
Mathias Agopian3866f0d2013-02-11 22:08:48 -080099}
100
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000101static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800102 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800103 ctrl->decStrong((void *)nativeCreate);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800104}
105
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000106static void nativeDestroy(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800107 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
108 ctrl->clear();
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800109 ctrl->decStrong((void *)nativeCreate);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800110}
111
Dan Stoza9890e3412014-05-22 16:12:54 -0700112static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz,
113 jobject displayTokenObj, jobject sourceCropObj, jint width, jint height,
Riley Andrews1d134062014-08-21 15:47:07 -0700114 jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform,
115 int rotation) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800116 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
117 if (displayToken == NULL) {
118 return NULL;
119 }
120
Dan Stoza9890e3412014-05-22 16:12:54 -0700121 int left = env->GetIntField(sourceCropObj, gRectClassInfo.left);
122 int top = env->GetIntField(sourceCropObj, gRectClassInfo.top);
123 int right = env->GetIntField(sourceCropObj, gRectClassInfo.right);
124 int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom);
125 Rect sourceCrop(left, top, right, bottom);
126
Ben Wagner60126ef2015-08-07 12:13:48 -0400127 std::unique_ptr<ScreenshotClient> screenshot(new ScreenshotClient());
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500128 status_t res;
Riley Andrews1d134062014-08-21 15:47:07 -0700129 if (allLayers) {
130 minLayer = 0;
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800131 maxLayer = -1;
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500132 }
Riley Andrews1d134062014-08-21 15:47:07 -0700133
134 res = screenshot->update(displayToken, sourceCrop, width, height,
135 minLayer, maxLayer, useIdentityTransform, static_cast<uint32_t>(rotation));
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500136 if (res != NO_ERROR) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800137 return NULL;
138 }
139
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500140 SkImageInfo screenshotInfo;
141 screenshotInfo.fWidth = screenshot->getWidth();
142 screenshotInfo.fHeight = screenshot->getHeight();
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800143
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500144 switch (screenshot->getFormat()) {
145 case PIXEL_FORMAT_RGBX_8888: {
146 screenshotInfo.fColorType = kRGBA_8888_SkColorType;
Mike Reed73f9c7d2015-02-27 09:59:37 -0500147 screenshotInfo.fAlphaType = kOpaque_SkAlphaType;
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500148 break;
149 }
150 case PIXEL_FORMAT_RGBA_8888: {
151 screenshotInfo.fColorType = kRGBA_8888_SkColorType;
152 screenshotInfo.fAlphaType = kPremul_SkAlphaType;
153 break;
154 }
155 case PIXEL_FORMAT_RGB_565: {
156 screenshotInfo.fColorType = kRGB_565_SkColorType;
Mike Reed73f9c7d2015-02-27 09:59:37 -0500157 screenshotInfo.fAlphaType = kOpaque_SkAlphaType;
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500158 break;
159 }
160 default: {
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500161 return NULL;
162 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800163 }
164
John Reckf29ed282015-04-07 07:32:03 -0700165 const size_t rowBytes =
Derek Sollenbergerb644a3b2014-01-17 15:45:10 -0500166 screenshot->getStride() * android::bytesPerPixel(screenshot->getFormat());
167
John Reckf29ed282015-04-07 07:32:03 -0700168 if (!screenshotInfo.fWidth || !screenshotInfo.fHeight) {
169 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800170 }
171
John Reckf29ed282015-04-07 07:32:03 -0700172 Bitmap* bitmap = new Bitmap(
173 (void*) screenshot->getPixels(), (void*) screenshot.get(), DeleteScreenshot,
174 screenshotInfo, rowBytes, nullptr);
Ben Wagner60126ef2015-08-07 12:13:48 -0400175 screenshot.release();
John Reckae2e8b42015-05-06 14:55:05 -0700176 bitmap->peekAtPixelRef()->setImmutable();
John Reckf29ed282015-04-07 07:32:03 -0700177
Chris Craik1abf5d62013-08-16 12:47:03 -0700178 return GraphicsJNI::createBitmap(env, bitmap,
179 GraphicsJNI::kBitmapCreateFlag_Premultiplied, NULL);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800180}
181
Dan Stoza9890e3412014-05-22 16:12:54 -0700182static void nativeScreenshot(JNIEnv* env, jclass clazz, jobject displayTokenObj,
183 jobject surfaceObj, jobject sourceCropObj, jint width, jint height,
184 jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform) {
Mathias Agopian0449a402013-03-01 23:01:51 -0800185 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
186 if (displayToken != NULL) {
187 sp<Surface> consumer = android_view_Surface_getSurface(env, surfaceObj);
188 if (consumer != NULL) {
Dan Stoza9890e3412014-05-22 16:12:54 -0700189 int left = env->GetIntField(sourceCropObj, gRectClassInfo.left);
190 int top = env->GetIntField(sourceCropObj, gRectClassInfo.top);
191 int right = env->GetIntField(sourceCropObj, gRectClassInfo.right);
192 int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom);
193 Rect sourceCrop(left, top, right, bottom);
194
Mathias Agopian0449a402013-03-01 23:01:51 -0800195 if (allLayers) {
196 minLayer = 0;
197 maxLayer = -1;
198 }
Dan Stoza9890e3412014-05-22 16:12:54 -0700199 ScreenshotClient::capture(displayToken,
200 consumer->getIGraphicBufferProducer(), sourceCrop,
Dan Stoza16ec12a2014-02-14 15:06:55 -0800201 width, height, uint32_t(minLayer), uint32_t(maxLayer),
202 useIdentityTransform);
Mathias Agopian0449a402013-03-01 23:01:51 -0800203 }
204 }
205}
206
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800207static void nativeOpenTransaction(JNIEnv* env, jclass clazz) {
208 SurfaceComposerClient::openGlobalTransaction();
209}
210
211static void nativeCloseTransaction(JNIEnv* env, jclass clazz) {
212 SurfaceComposerClient::closeGlobalTransaction();
213}
214
215static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz) {
216 SurfaceComposerClient::setAnimationTransaction();
217}
218
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000219static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong nativeObject, jint zorder) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800220 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
221 status_t err = ctrl->setLayer(zorder);
222 if (err < 0 && err != NO_INIT) {
223 doThrowIAE(env);
224 }
225}
226
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000227static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat x, jfloat y) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800228 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
229 status_t err = ctrl->setPosition(x, y);
230 if (err < 0 && err != NO_INIT) {
231 doThrowIAE(env);
232 }
233}
234
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000235static void nativeSetSize(JNIEnv* env, jclass clazz, jlong nativeObject, jint w, jint h) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800236 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
237 status_t err = ctrl->setSize(w, h);
238 if (err < 0 && err != NO_INIT) {
239 doThrowIAE(env);
240 }
241}
242
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000243static void nativeSetFlags(JNIEnv* env, jclass clazz, jlong nativeObject, jint flags, jint mask) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800244 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
245 status_t err = ctrl->setFlags(flags, mask);
246 if (err < 0 && err != NO_INIT) {
247 doThrowIAE(env);
248 }
249}
250
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000251static void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jlong nativeObject, jobject regionObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800252 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
253 SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj);
254 if (!region) {
255 doThrowIAE(env);
256 return;
257 }
258
259 const SkIRect& b(region->getBounds());
260 Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
261 if (region->isComplex()) {
262 SkRegion::Iterator it(*region);
263 while (!it.done()) {
264 const SkIRect& r(it.rect());
265 reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
266 it.next();
267 }
268 }
269
270 status_t err = ctrl->setTransparentRegionHint(reg);
271 if (err < 0 && err != NO_INIT) {
272 doThrowIAE(env);
273 }
274}
275
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000276static void nativeSetAlpha(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat alpha) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800277 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
278 status_t err = ctrl->setAlpha(alpha);
279 if (err < 0 && err != NO_INIT) {
280 doThrowIAE(env);
281 }
282}
283
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000284static void nativeSetMatrix(JNIEnv* env, jclass clazz, jlong nativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800285 jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy) {
286 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
287 status_t err = ctrl->setMatrix(dsdx, dtdx, dsdy, dtdy);
288 if (err < 0 && err != NO_INIT) {
289 doThrowIAE(env);
290 }
291}
292
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000293static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong nativeObject,
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800294 jint l, jint t, jint r, jint b) {
295 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
296 Rect crop(l, t, r, b);
297 status_t err = ctrl->setCrop(crop);
298 if (err < 0 && err != NO_INIT) {
299 doThrowIAE(env);
300 }
301}
302
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000303static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong nativeObject, jint layerStack) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800304 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
305 status_t err = ctrl->setLayerStack(layerStack);
306 if (err < 0 && err != NO_INIT) {
307 doThrowIAE(env);
308 }
309}
310
311static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) {
312 sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id));
313 return javaObjectForIBinder(env, token);
314}
315
316static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj,
317 jboolean secure) {
318 ScopedUtfChars name(env, nameObj);
319 sp<IBinder> token(SurfaceComposerClient::createDisplay(
320 String8(name.c_str()), bool(secure)));
321 return javaObjectForIBinder(env, token);
322}
323
Jesse Hall6a6bc212013-08-08 12:15:03 -0700324static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
325 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
326 if (token == NULL) return;
327 SurfaceComposerClient::destroyDisplay(token);
328}
329
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800330static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000331 jobject tokenObj, jlong nativeSurfaceObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800332 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
333 if (token == NULL) return;
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800334 sp<IGraphicBufferProducer> bufferProducer;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800335 sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
Mathias Agopianffddc9b2013-02-25 15:56:31 -0800336 if (sur != NULL) {
337 bufferProducer = sur->getIGraphicBufferProducer();
338 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800339 SurfaceComposerClient::setDisplaySurface(token, bufferProducer);
340}
341
342static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
343 jobject tokenObj, jint layerStack) {
344 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
345 if (token == NULL) return;
346
347 SurfaceComposerClient::setDisplayLayerStack(token, layerStack);
348}
349
350static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz,
351 jobject tokenObj, jint orientation,
352 jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom,
353 jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) {
354 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
355 if (token == NULL) return;
356 Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom);
357 Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom);
358 SurfaceComposerClient::setDisplayProjection(token, orientation, layerStackRect, displayRect);
359}
360
Michael Wright01e840f2014-06-26 16:03:25 -0700361static void nativeSetDisplaySize(JNIEnv* env, jclass clazz,
362 jobject tokenObj, jint width, jint height) {
363 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
364 if (token == NULL) return;
365 SurfaceComposerClient::setDisplaySize(token, width, height);
366}
367
Dan Stoza00101052014-05-02 15:23:40 -0700368static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz,
369 jobject tokenObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800370 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
Dan Stoza00101052014-05-02 15:23:40 -0700371 if (token == NULL) return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800372
Dan Stoza00101052014-05-02 15:23:40 -0700373 Vector<DisplayInfo> configs;
374 if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR ||
375 configs.size() == 0) {
376 return NULL;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800377 }
378
Dan Stoza00101052014-05-02 15:23:40 -0700379 jobjectArray configArray = env->NewObjectArray(configs.size(),
380 gPhysicalDisplayInfoClassInfo.clazz, NULL);
381
382 for (size_t c = 0; c < configs.size(); ++c) {
383 const DisplayInfo& info = configs[c];
384 jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz,
385 gPhysicalDisplayInfoClassInfo.ctor);
386 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
387 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
388 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
389 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
390 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
391 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
392 env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
Andy McFaddene8b1aeb2014-06-13 14:05:40 -0700393 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos,
394 info.appVsyncOffset);
395 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos,
396 info.presentationDeadline);
Dan Stoza00101052014-05-02 15:23:40 -0700397 env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
398 env->DeleteLocalRef(infoObj);
399 }
400
401 return configArray;
402}
403
404static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) {
405 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
406 if (token == NULL) return -1;
407 return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token));
408}
409
410static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) {
411 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
412 if (token == NULL) return JNI_FALSE;
413 status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id));
414 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800415}
416
Prashant Malanic55929a2014-05-25 01:59:21 -0700417static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800418 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
419 if (token == NULL) return;
420
Prashant Malanic55929a2014-05-25 01:59:21 -0700421 ALOGD_IF_SLOW(100, "Excessive delay in setPowerMode()");
422 SurfaceComposerClient::setDisplayPowerMode(token, mode);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800423}
424
Svetoslav1376d602014-03-13 11:17:26 -0700425static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) {
426 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
427 status_t err = ctrl->clearLayerFrameStats();
428
429 if (err < 0 && err != NO_INIT) {
430 doThrowIAE(env);
431 }
432
433 // The other end is not ready, just report we failed.
434 if (err == NO_INIT) {
435 return JNI_FALSE;
436 }
437
438 return JNI_TRUE;
439}
440
441static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject,
442 jobject outStats) {
443 FrameStats stats;
444
445 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
446 status_t err = ctrl->getLayerFrameStats(&stats);
447 if (err < 0 && err != NO_INIT) {
448 doThrowIAE(env);
449 }
450
451 // The other end is not ready, fine just return empty stats.
452 if (err == NO_INIT) {
453 return JNI_FALSE;
454 }
455
456 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
457 size_t frameCount = stats.desiredPresentTimesNano.size();
458
459 jlongArray postedTimesNanoDst = env->NewLongArray(frameCount);
460 if (postedTimesNanoDst == NULL) {
461 return JNI_FALSE;
462 }
463
464 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
465 if (presentedTimesNanoDst == NULL) {
466 return JNI_FALSE;
467 }
468
469 jlongArray readyTimesNanoDst = env->NewLongArray(frameCount);
470 if (readyTimesNanoDst == NULL) {
471 return JNI_FALSE;
472 }
473
474 nsecs_t postedTimesNanoSrc[frameCount];
475 nsecs_t presentedTimesNanoSrc[frameCount];
476 nsecs_t readyTimesNanoSrc[frameCount];
477
478 for (size_t i = 0; i < frameCount; i++) {
479 nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i];
480 if (postedTimeNano == INT64_MAX) {
481 postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
482 }
483 postedTimesNanoSrc[i] = postedTimeNano;
484
485 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
486 if (presentedTimeNano == INT64_MAX) {
487 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
488 }
489 presentedTimesNanoSrc[i] = presentedTimeNano;
490
491 nsecs_t readyTimeNano = stats.frameReadyTimesNano[i];
492 if (readyTimeNano == INT64_MAX) {
493 readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
494 }
495 readyTimesNanoSrc[i] = readyTimeNano;
496 }
497
498 env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc);
499 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
500 env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc);
501
502 env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano,
503 postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst);
504
505 if (env->ExceptionCheck()) {
506 return JNI_FALSE;
507 }
508
509 return JNI_TRUE;
510}
511
512static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) {
513 status_t err = SurfaceComposerClient::clearAnimationFrameStats();
514
515 if (err < 0 && err != NO_INIT) {
516 doThrowIAE(env);
517 }
518
519 // The other end is not ready, just report we failed.
520 if (err == NO_INIT) {
521 return JNI_FALSE;
522 }
523
524 return JNI_TRUE;
525}
526
527static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) {
528 FrameStats stats;
529
530 status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats);
531 if (err < 0 && err != NO_INIT) {
532 doThrowIAE(env);
533 }
534
535 // The other end is not ready, fine just return empty stats.
536 if (err == NO_INIT) {
537 return JNI_FALSE;
538 }
539
540 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano);
541 size_t frameCount = stats.desiredPresentTimesNano.size();
542
543 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount);
544 if (presentedTimesNanoDst == NULL) {
545 return JNI_FALSE;
546 }
547
548 nsecs_t presentedTimesNanoSrc[frameCount];
549
550 for (size_t i = 0; i < frameCount; i++) {
Allen Hairac5eda32014-04-24 11:50:37 -0700551 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i];
Svetoslav1376d602014-03-13 11:17:26 -0700552 if (presentedTimeNano == INT64_MAX) {
553 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO;
554 }
555 presentedTimesNanoSrc[i] = presentedTimeNano;
556 }
557
558 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc);
559
560 env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano,
561 presentedTimesNanoDst);
562
563 if (env->ExceptionCheck()) {
564 return JNI_FALSE;
565 }
566
567 return JNI_TRUE;
568}
569
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800570// ----------------------------------------------------------------------------
571
572static JNINativeMethod sSurfaceControlMethods[] = {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000573 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800574 (void*)nativeCreate },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000575 {"nativeRelease", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800576 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000577 {"nativeDestroy", "(J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800578 (void*)nativeDestroy },
Riley Andrews1d134062014-08-21 15:47:07 -0700579 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZI)Landroid/graphics/Bitmap;",
Mathias Agopian0449a402013-03-01 23:01:51 -0800580 (void*)nativeScreenshotBitmap },
Dan Stoza9890e3412014-05-22 16:12:54 -0700581 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;Landroid/graphics/Rect;IIIIZZ)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800582 (void*)nativeScreenshot },
583 {"nativeOpenTransaction", "()V",
584 (void*)nativeOpenTransaction },
585 {"nativeCloseTransaction", "()V",
586 (void*)nativeCloseTransaction },
587 {"nativeSetAnimationTransaction", "()V",
588 (void*)nativeSetAnimationTransaction },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000589 {"nativeSetLayer", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800590 (void*)nativeSetLayer },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000591 {"nativeSetPosition", "(JFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800592 (void*)nativeSetPosition },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000593 {"nativeSetSize", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800594 (void*)nativeSetSize },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000595 {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800596 (void*)nativeSetTransparentRegionHint },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000597 {"nativeSetAlpha", "(JF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800598 (void*)nativeSetAlpha },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000599 {"nativeSetMatrix", "(JFFFF)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800600 (void*)nativeSetMatrix },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000601 {"nativeSetFlags", "(JII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800602 (void*)nativeSetFlags },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000603 {"nativeSetWindowCrop", "(JIIII)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800604 (void*)nativeSetWindowCrop },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000605 {"nativeSetLayerStack", "(JI)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800606 (void*)nativeSetLayerStack },
607 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
608 (void*)nativeGetBuiltInDisplay },
609 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
610 (void*)nativeCreateDisplay },
Jesse Hall6a6bc212013-08-08 12:15:03 -0700611 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V",
612 (void*)nativeDestroyDisplay },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000613 {"nativeSetDisplaySurface", "(Landroid/os/IBinder;J)V",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800614 (void*)nativeSetDisplaySurface },
615 {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V",
616 (void*)nativeSetDisplayLayerStack },
617 {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V",
618 (void*)nativeSetDisplayProjection },
Michael Wright01e840f2014-06-26 16:03:25 -0700619 {"nativeSetDisplaySize", "(Landroid/os/IBinder;II)V",
620 (void*)nativeSetDisplaySize },
Dan Stoza00101052014-05-02 15:23:40 -0700621 {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;",
622 (void*)nativeGetDisplayConfigs },
623 {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I",
624 (void*)nativeGetActiveConfig },
625 {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
626 (void*)nativeSetActiveConfig },
Svetoslav1376d602014-03-13 11:17:26 -0700627 {"nativeClearContentFrameStats", "(J)Z",
628 (void*)nativeClearContentFrameStats },
629 {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z",
630 (void*)nativeGetContentFrameStats },
631 {"nativeClearAnimationFrameStats", "()Z",
632 (void*)nativeClearAnimationFrameStats },
633 {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z",
634 (void*)nativeGetAnimationFrameStats },
Prashant Malanic55929a2014-05-25 01:59:21 -0700635 {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V",
636 (void*)nativeSetDisplayPowerMode },
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800637};
638
639int register_android_view_SurfaceControl(JNIEnv* env)
640{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800641 int err = RegisterMethodsOrDie(env, "android/view/SurfaceControl",
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800642 sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
643
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800644 jclass clazz = FindClassOrDie(env, "android/view/SurfaceControl$PhysicalDisplayInfo");
645 gPhysicalDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
646 gPhysicalDisplayInfoClassInfo.ctor = GetMethodIDOrDie(env,
647 gPhysicalDisplayInfoClassInfo.clazz, "<init>", "()V");
648 gPhysicalDisplayInfoClassInfo.width = GetFieldIDOrDie(env, clazz, "width", "I");
649 gPhysicalDisplayInfoClassInfo.height = GetFieldIDOrDie(env, clazz, "height", "I");
650 gPhysicalDisplayInfoClassInfo.refreshRate = GetFieldIDOrDie(env, clazz, "refreshRate", "F");
651 gPhysicalDisplayInfoClassInfo.density = GetFieldIDOrDie(env, clazz, "density", "F");
652 gPhysicalDisplayInfoClassInfo.xDpi = GetFieldIDOrDie(env, clazz, "xDpi", "F");
653 gPhysicalDisplayInfoClassInfo.yDpi = GetFieldIDOrDie(env, clazz, "yDpi", "F");
654 gPhysicalDisplayInfoClassInfo.secure = GetFieldIDOrDie(env, clazz, "secure", "Z");
655 gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = GetFieldIDOrDie(env,
656 clazz, "appVsyncOffsetNanos", "J");
657 gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env,
658 clazz, "presentationDeadlineNanos", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700659
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800660 jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect");
661 gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I");
662 gRectClassInfo.left = GetFieldIDOrDie(env, rectClazz, "left", "I");
663 gRectClassInfo.right = GetFieldIDOrDie(env, rectClazz, "right", "I");
664 gRectClassInfo.top = GetFieldIDOrDie(env, rectClazz, "top", "I");
Dan Stoza9890e3412014-05-22 16:12:54 -0700665
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800666 jclass frameStatsClazz = FindClassOrDie(env, "android/view/FrameStats");
667 jfieldID undefined_time_nano_field = GetStaticFieldIDOrDie(env,
668 frameStatsClazz, "UNDEFINED_TIME_NANO", "J");
Svetoslav1376d602014-03-13 11:17:26 -0700669 nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field);
670
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800671 jclass contFrameStatsClazz = FindClassOrDie(env, "android/view/WindowContentFrameStats");
672 gWindowContentFrameStatsClassInfo.init = GetMethodIDOrDie(env,
673 contFrameStatsClazz, "init", "(J[J[J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700674 gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
675
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800676 jclass animFrameStatsClazz = FindClassOrDie(env, "android/view/WindowAnimationFrameStats");
677 gWindowAnimationFrameStatsClassInfo.init = GetMethodIDOrDie(env,
678 animFrameStatsClazz, "init", "(J[J)V");
Svetoslav1376d602014-03-13 11:17:26 -0700679 gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano;
680
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800681 return err;
682}
683
684};