blob: 158783035867951e0b3a057e4bce105801026183 [file] [log] [blame]
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceControl"
18
19#include <stdio.h>
20
21#include "jni.h"
22#include "JNIHelp.h"
23
24#include "android_os_Parcel.h"
25#include "android_util_Binder.h"
26#include "android/graphics/GraphicsJNI.h"
27#include "android/graphics/Region.h"
28
29#include <android_runtime/AndroidRuntime.h>
30#include <android_runtime/android_view_SurfaceSession.h>
31
32#include <gui/Surface.h>
33#include <gui/SurfaceComposerClient.h>
34
35#include <ui/DisplayInfo.h>
36#include <ui/Rect.h>
37#include <ui/Region.h>
38
39#include <utils/Log.h>
40
41#include <ScopedUtfChars.h>
42
43// ----------------------------------------------------------------------------
44
45namespace android {
46
47static const char* const OutOfResourcesException =
48 "android/view/Surface$OutOfResourcesException";
49
50static struct {
51 jfieldID width;
52 jfieldID height;
53 jfieldID refreshRate;
54 jfieldID density;
55 jfieldID xDpi;
56 jfieldID yDpi;
57 jfieldID secure;
58} gPhysicalDisplayInfoClassInfo;
59
60
61class ScreenshotPixelRef : public SkPixelRef {
62public:
63 ScreenshotPixelRef(SkColorTable* ctable) {
64 fCTable = ctable;
65 SkSafeRef(ctable);
66 setImmutable();
67 }
68
69 virtual ~ScreenshotPixelRef() {
70 SkSafeUnref(fCTable);
71 }
72
73 status_t update(const sp<IBinder>& display, int width, int height,
74 int minLayer, int maxLayer, bool allLayers) {
75 status_t res = (width > 0 && height > 0)
76 ? (allLayers
77 ? mScreenshot.update(display, width, height)
78 : mScreenshot.update(display, width, height, minLayer, maxLayer))
79 : mScreenshot.update(display);
80 if (res != NO_ERROR) {
81 return res;
82 }
83
84 return NO_ERROR;
85 }
86
87 uint32_t getWidth() const {
88 return mScreenshot.getWidth();
89 }
90
91 uint32_t getHeight() const {
92 return mScreenshot.getHeight();
93 }
94
95 uint32_t getStride() const {
96 return mScreenshot.getStride();
97 }
98
99 uint32_t getFormat() const {
100 return mScreenshot.getFormat();
101 }
102
103protected:
104 // overrides from SkPixelRef
105 virtual void* onLockPixels(SkColorTable** ct) {
106 *ct = fCTable;
107 return (void*)mScreenshot.getPixels();
108 }
109
110 virtual void onUnlockPixels() {
111 }
112
Kristian Monsene32e2b32013-02-14 21:35:35 -0800113 SK_DECLARE_UNFLATTENABLE_OBJECT()
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800114private:
115 ScreenshotClient mScreenshot;
116 SkColorTable* fCTable;
117
118 typedef SkPixelRef INHERITED;
119};
120
121
122// ----------------------------------------------------------------------------
123
124static jint nativeCreate(JNIEnv* env, jobject surfaceObj, jobject sessionObj,
125 jstring nameStr, jint w, jint h, jint format, jint flags) {
126 ScopedUtfChars name(env, nameStr);
127 sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));
128 sp<SurfaceControl> surface = client->createSurface(
129 String8(name.c_str()), w, h, format, flags);
130 if (surface == NULL) {
131 jniThrowException(env, OutOfResourcesException, NULL);
132 return 0;
133 }
134 surface->incStrong(surfaceObj);
135 return int(surface.get());
136}
137
138static void nativeRelease(JNIEnv* env, jobject surfaceObj, jint nativeObject) {
139 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
140 ctrl->decStrong(surfaceObj);
141}
142
143static void nativeDestroy(JNIEnv* env, jobject surfaceObj, jint nativeObject) {
144 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject));
145 ctrl->clear();
146 ctrl->decStrong(surfaceObj);
147}
148
149static inline SkBitmap::Config convertPixelFormat(PixelFormat format) {
150 /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then
151 we can map to SkBitmap::kARGB_8888_Config, and optionally call
152 bitmap.setIsOpaque(true) on the resulting SkBitmap (as an accelerator)
153 */
154 switch (format) {
155 case PIXEL_FORMAT_RGBX_8888: return SkBitmap::kARGB_8888_Config;
156 case PIXEL_FORMAT_RGBA_8888: return SkBitmap::kARGB_8888_Config;
157 case PIXEL_FORMAT_RGBA_4444: return SkBitmap::kARGB_4444_Config;
158 case PIXEL_FORMAT_RGB_565: return SkBitmap::kRGB_565_Config;
159 case PIXEL_FORMAT_A_8: return SkBitmap::kA8_Config;
160 default: return SkBitmap::kNo_Config;
161 }
162}
163
164static jobject nativeScreenshot(JNIEnv* env, jclass clazz, jobject displayTokenObj,
165 jint width, jint height, jint minLayer, jint maxLayer, bool allLayers) {
166 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj);
167 if (displayToken == NULL) {
168 return NULL;
169 }
170
171 ScreenshotPixelRef* pixels = new ScreenshotPixelRef(NULL);
172 if (pixels->update(displayToken, width, height,
173 minLayer, maxLayer, allLayers) != NO_ERROR) {
174 delete pixels;
175 return NULL;
176 }
177
178 uint32_t w = pixels->getWidth();
179 uint32_t h = pixels->getHeight();
180 uint32_t s = pixels->getStride();
181 uint32_t f = pixels->getFormat();
182 ssize_t bpr = s * android::bytesPerPixel(f);
183
184 SkBitmap* bitmap = new SkBitmap();
185 bitmap->setConfig(convertPixelFormat(f), w, h, bpr);
186 if (f == PIXEL_FORMAT_RGBX_8888) {
187 bitmap->setIsOpaque(true);
188 }
189
190 if (w > 0 && h > 0) {
191 bitmap->setPixelRef(pixels)->unref();
192 bitmap->lockPixels();
193 } else {
194 // be safe with an empty bitmap.
195 delete pixels;
196 bitmap->setPixels(NULL);
197 }
198
199 return GraphicsJNI::createBitmap(env, bitmap, false, NULL);
200}
201
202static void nativeOpenTransaction(JNIEnv* env, jclass clazz) {
203 SurfaceComposerClient::openGlobalTransaction();
204}
205
206static void nativeCloseTransaction(JNIEnv* env, jclass clazz) {
207 SurfaceComposerClient::closeGlobalTransaction();
208}
209
210static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz) {
211 SurfaceComposerClient::setAnimationTransaction();
212}
213
214static void nativeSetLayer(JNIEnv* env, jobject surfaceObj, jint nativeObject, jint zorder) {
215 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
216 status_t err = ctrl->setLayer(zorder);
217 if (err < 0 && err != NO_INIT) {
218 doThrowIAE(env);
219 }
220}
221
222static void nativeSetPosition(JNIEnv* env, jobject surfaceObj, jint nativeObject, jfloat x, jfloat y) {
223 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
224 status_t err = ctrl->setPosition(x, y);
225 if (err < 0 && err != NO_INIT) {
226 doThrowIAE(env);
227 }
228}
229
230static void nativeSetSize(JNIEnv* env, jobject surfaceObj, jint nativeObject, jint w, jint h) {
231 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
232 status_t err = ctrl->setSize(w, h);
233 if (err < 0 && err != NO_INIT) {
234 doThrowIAE(env);
235 }
236}
237
238static void nativeSetFlags(JNIEnv* env, jobject surfaceObj, jint nativeObject, jint flags, jint mask) {
239 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
240 status_t err = ctrl->setFlags(flags, mask);
241 if (err < 0 && err != NO_INIT) {
242 doThrowIAE(env);
243 }
244}
245
246static void nativeSetTransparentRegionHint(JNIEnv* env, jobject surfaceObj, jint nativeObject, jobject regionObj) {
247 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
248 SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj);
249 if (!region) {
250 doThrowIAE(env);
251 return;
252 }
253
254 const SkIRect& b(region->getBounds());
255 Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
256 if (region->isComplex()) {
257 SkRegion::Iterator it(*region);
258 while (!it.done()) {
259 const SkIRect& r(it.rect());
260 reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
261 it.next();
262 }
263 }
264
265 status_t err = ctrl->setTransparentRegionHint(reg);
266 if (err < 0 && err != NO_INIT) {
267 doThrowIAE(env);
268 }
269}
270
271static void nativeSetAlpha(JNIEnv* env, jobject surfaceObj, jint nativeObject, jfloat alpha) {
272 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
273 status_t err = ctrl->setAlpha(alpha);
274 if (err < 0 && err != NO_INIT) {
275 doThrowIAE(env);
276 }
277}
278
279static void nativeSetMatrix(JNIEnv* env, jobject surfaceObj, jint nativeObject,
280 jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy) {
281 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
282 status_t err = ctrl->setMatrix(dsdx, dtdx, dsdy, dtdy);
283 if (err < 0 && err != NO_INIT) {
284 doThrowIAE(env);
285 }
286}
287
288static void nativeSetWindowCrop(JNIEnv* env, jobject surfaceObj, jint nativeObject,
289 jint l, jint t, jint r, jint b) {
290 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
291 Rect crop(l, t, r, b);
292 status_t err = ctrl->setCrop(crop);
293 if (err < 0 && err != NO_INIT) {
294 doThrowIAE(env);
295 }
296}
297
298static void nativeSetLayerStack(JNIEnv* env, jobject surfaceObj, jint nativeObject, jint layerStack) {
299 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
300 status_t err = ctrl->setLayerStack(layerStack);
301 if (err < 0 && err != NO_INIT) {
302 doThrowIAE(env);
303 }
304}
305
306static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) {
307 sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id));
308 return javaObjectForIBinder(env, token);
309}
310
311static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj,
312 jboolean secure) {
313 ScopedUtfChars name(env, nameObj);
314 sp<IBinder> token(SurfaceComposerClient::createDisplay(
315 String8(name.c_str()), bool(secure)));
316 return javaObjectForIBinder(env, token);
317}
318
319static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz,
320 jobject tokenObj, jint nativeSurfaceObject) {
321 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
322 if (token == NULL) return;
323 sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject));
324 sp<IGraphicBufferProducer> bufferProducer(sur->getSurfaceTexture());
325 SurfaceComposerClient::setDisplaySurface(token, bufferProducer);
326}
327
328static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
329 jobject tokenObj, jint layerStack) {
330 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
331 if (token == NULL) return;
332
333 SurfaceComposerClient::setDisplayLayerStack(token, layerStack);
334}
335
336static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz,
337 jobject tokenObj, jint orientation,
338 jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom,
339 jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) {
340 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
341 if (token == NULL) return;
342 Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom);
343 Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom);
344 SurfaceComposerClient::setDisplayProjection(token, orientation, layerStackRect, displayRect);
345}
346
347static jboolean nativeGetDisplayInfo(JNIEnv* env, jclass clazz,
348 jobject tokenObj, jobject infoObj) {
349 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
350 if (token == NULL) return JNI_FALSE;
351
352 DisplayInfo info;
353 if (SurfaceComposerClient::getDisplayInfo(token, &info)) {
354 return JNI_FALSE;
355 }
356
357 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w);
358 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h);
359 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps);
360 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density);
361 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi);
362 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi);
363 env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure);
364 return JNI_TRUE;
365}
366
367static void nativeBlankDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
368 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
369 if (token == NULL) return;
370
371 ALOGD_IF_SLOW(100, "Excessive delay in blankDisplay() while turning screen off");
372 SurfaceComposerClient::blankDisplay(token);
373}
374
375static void nativeUnblankDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) {
376 sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
377 if (token == NULL) return;
378
379 ALOGD_IF_SLOW(100, "Excessive delay in unblankDisplay() while turning screen on");
380 SurfaceComposerClient::unblankDisplay(token);
381}
382
383// ----------------------------------------------------------------------------
384
385static JNINativeMethod sSurfaceControlMethods[] = {
386 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)I",
387 (void*)nativeCreate },
388 {"nativeRelease", "(I)V",
389 (void*)nativeRelease },
390 {"nativeDestroy", "(I)V",
391 (void*)nativeDestroy },
392 {"nativeScreenshot", "(Landroid/os/IBinder;IIIIZ)Landroid/graphics/Bitmap;",
393 (void*)nativeScreenshot },
394 {"nativeOpenTransaction", "()V",
395 (void*)nativeOpenTransaction },
396 {"nativeCloseTransaction", "()V",
397 (void*)nativeCloseTransaction },
398 {"nativeSetAnimationTransaction", "()V",
399 (void*)nativeSetAnimationTransaction },
400 {"nativeSetLayer", "(II)V",
401 (void*)nativeSetLayer },
402 {"nativeSetPosition", "(IFF)V",
403 (void*)nativeSetPosition },
404 {"nativeSetSize", "(III)V",
405 (void*)nativeSetSize },
406 {"nativeSetTransparentRegionHint", "(ILandroid/graphics/Region;)V",
407 (void*)nativeSetTransparentRegionHint },
408 {"nativeSetAlpha", "(IF)V",
409 (void*)nativeSetAlpha },
410 {"nativeSetMatrix", "(IFFFF)V",
411 (void*)nativeSetMatrix },
412 {"nativeSetFlags", "(III)V",
413 (void*)nativeSetFlags },
414 {"nativeSetWindowCrop", "(IIIII)V",
415 (void*)nativeSetWindowCrop },
416 {"nativeSetLayerStack", "(II)V",
417 (void*)nativeSetLayerStack },
418 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
419 (void*)nativeGetBuiltInDisplay },
420 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;",
421 (void*)nativeCreateDisplay },
422 {"nativeSetDisplaySurface", "(Landroid/os/IBinder;I)V",
423 (void*)nativeSetDisplaySurface },
424 {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V",
425 (void*)nativeSetDisplayLayerStack },
426 {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V",
427 (void*)nativeSetDisplayProjection },
428 {"nativeGetDisplayInfo", "(Landroid/os/IBinder;Landroid/view/SurfaceControl$PhysicalDisplayInfo;)Z",
429 (void*)nativeGetDisplayInfo },
430 {"nativeBlankDisplay", "(Landroid/os/IBinder;)V",
431 (void*)nativeBlankDisplay },
432 {"nativeUnblankDisplay", "(Landroid/os/IBinder;)V",
433 (void*)nativeUnblankDisplay },
434};
435
436int register_android_view_SurfaceControl(JNIEnv* env)
437{
438 int err = AndroidRuntime::registerNativeMethods(env, "android/view/SurfaceControl",
439 sSurfaceControlMethods, NELEM(sSurfaceControlMethods));
440
441 jclass clazz = env->FindClass("android/view/SurfaceControl$PhysicalDisplayInfo");
442 gPhysicalDisplayInfoClassInfo.width = env->GetFieldID(clazz, "width", "I");
443 gPhysicalDisplayInfoClassInfo.height = env->GetFieldID(clazz, "height", "I");
444 gPhysicalDisplayInfoClassInfo.refreshRate = env->GetFieldID(clazz, "refreshRate", "F");
445 gPhysicalDisplayInfoClassInfo.density = env->GetFieldID(clazz, "density", "F");
446 gPhysicalDisplayInfoClassInfo.xDpi = env->GetFieldID(clazz, "xDpi", "F");
447 gPhysicalDisplayInfoClassInfo.yDpi = env->GetFieldID(clazz, "yDpi", "F");
448 gPhysicalDisplayInfoClassInfo.secure = env->GetFieldID(clazz, "secure", "Z");
449 return err;
450}
451
452};