android_view_Surface.cpp revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/*
2 * Copyright (C) 2007 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#include <stdio.h>
18
19#include "android_util_Binder.h"
20
21#include <ui/SurfaceComposerClient.h>
22#include <ui/Region.h>
23#include <ui/Rect.h>
24
25#include <SkCanvas.h>
26#include <SkBitmap.h>
27
28#include "jni.h"
29#include <android_runtime/AndroidRuntime.h>
30#include <utils/misc.h>
31
32
33// ----------------------------------------------------------------------------
34
35namespace android {
36
37// ----------------------------------------------------------------------------
38
39static const char* const OutOfResourcesException =
40    "android/view/Surface$OutOfResourcesException";
41
42struct sso_t {
43    jfieldID client;
44};
45static sso_t sso;
46
47struct so_t {
48    jfieldID surface;
49    jfieldID saveCount;
50    jfieldID canvas;
51};
52static so_t so;
53
54struct ro_t {
55    jfieldID l;
56    jfieldID t;
57    jfieldID r;
58    jfieldID b;
59};
60static ro_t ro;
61
62struct po_t {
63    jfieldID x;
64    jfieldID y;
65};
66static po_t po;
67
68struct co_t {
69    jfieldID surfaceFormat;
70};
71static co_t co;
72
73struct no_t {
74    jfieldID native_canvas;
75    jfieldID native_region;
76    jfieldID native_parcel;
77};
78static no_t no;
79
80
81static __attribute__((noinline))
82void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
83{
84    if (!env->ExceptionOccurred()) {
85        jclass npeClazz = env->FindClass(exc);
86        env->ThrowNew(npeClazz, msg);
87    }
88}
89
90// ----------------------------------------------------------------------------
91// ----------------------------------------------------------------------------
92// ----------------------------------------------------------------------------
93
94static void SurfaceSession_init(JNIEnv* env, jobject clazz)
95{
96    sp<SurfaceComposerClient> client = new SurfaceComposerClient;
97    client->incStrong(clazz);
98    env->SetIntField(clazz, sso.client, (int)client.get());
99}
100
101static void SurfaceSession_destroy(JNIEnv* env, jobject clazz)
102{
103    SurfaceComposerClient* client =
104            (SurfaceComposerClient*)env->GetIntField(clazz, sso.client);
105    if (client != 0) {
106        client->decStrong(clazz);
107        env->SetIntField(clazz, sso.client, 0);
108    }
109}
110
111static void SurfaceSession_kill(JNIEnv* env, jobject clazz)
112{
113    SurfaceComposerClient* client =
114            (SurfaceComposerClient*)env->GetIntField(clazz, sso.client);
115    if (client != 0) {
116        client->dispose();
117        client->decStrong(clazz);
118        env->SetIntField(clazz, sso.client, 0);
119    }
120}
121
122// ----------------------------------------------------------------------------
123
124static sp<Surface> getSurface(JNIEnv* env, jobject clazz)
125{
126    Surface* const p = (Surface*)env->GetIntField(clazz, so.surface);
127    return sp<Surface>(p);
128}
129
130static void setSurface(JNIEnv* env, jobject clazz, const sp<Surface>& surface)
131{
132    Surface* const p = (Surface*)env->GetIntField(clazz, so.surface);
133    if (surface.get()) {
134        surface->incStrong(clazz);
135    }
136    if (p) {
137        p->decStrong(clazz);
138    }
139    env->SetIntField(clazz, so.surface, (int)surface.get());
140}
141
142// ----------------------------------------------------------------------------
143
144static void Surface_init(
145        JNIEnv* env, jobject clazz,
146        jobject session, jint pid, jint dpy, jint w, jint h, jint format, jint flags)
147{
148    if (session == NULL) {
149        doThrow(env, "java/lang/NullPointerException");
150        return;
151    }
152
153    SurfaceComposerClient* client =
154            (SurfaceComposerClient*)env->GetIntField(session, sso.client);
155
156    sp<Surface> surface(client->createSurface(pid, dpy, w, h, format, flags));
157    if (surface == 0) {
158        doThrow(env, OutOfResourcesException);
159        return;
160    }
161    setSurface(env, clazz, surface);
162}
163
164static void Surface_initParcel(JNIEnv* env, jobject clazz, jobject argParcel)
165{
166    Parcel* parcel = (Parcel*)env->GetIntField(argParcel, no.native_parcel);
167    if (parcel == NULL) {
168        doThrow(env, "java/lang/NullPointerException", NULL);
169        return;
170    }
171    const sp<Surface>& rhs = Surface::readFromParcel(parcel);
172    setSurface(env, clazz, rhs);
173}
174
175static void Surface_clear(JNIEnv* env, jobject clazz, uintptr_t *ostack)
176{
177    setSurface(env, clazz, 0);
178}
179
180static jboolean Surface_isValid(JNIEnv* env, jobject clazz)
181{
182    const sp<Surface>& surface = getSurface(env, clazz);
183    return surface->isValid() ? JNI_TRUE : JNI_FALSE;
184}
185
186static inline SkBitmap::Config convertPixelFormat(PixelFormat format)
187{
188    /* note: if PIXEL_FORMAT_XRGB_8888 means that all alpha bytes are 0xFF, then
189        we can map to SkBitmap::kARGB_8888_Config, and optionally call
190        bitmap.setIsOpaque(true) on the resulting SkBitmap (as an accelerator)
191    */
192	switch (format) {
193    case PIXEL_FORMAT_RGBA_8888:    return SkBitmap::kARGB_8888_Config;
194    case PIXEL_FORMAT_RGBA_4444:    return SkBitmap::kARGB_4444_Config;
195	case PIXEL_FORMAT_RGB_565:		return SkBitmap::kRGB_565_Config;
196	case PIXEL_FORMAT_A_8:          return SkBitmap::kA8_Config;
197	default:                        return SkBitmap::kNo_Config;
198	}
199}
200
201static jobject Surface_lockCanvas(JNIEnv* env, jobject clazz, jobject dirtyRect)
202{
203    const sp<Surface>& surface = getSurface(env, clazz);
204    if (!surface->isValid())
205        return 0;
206
207    // get dirty region
208    Region dirtyRegion;
209    if (dirtyRect) {
210        Rect dirty;
211        dirty.left  = env->GetIntField(dirtyRect, ro.l);
212        dirty.top   = env->GetIntField(dirtyRect, ro.t);
213        dirty.right = env->GetIntField(dirtyRect, ro.r);
214        dirty.bottom= env->GetIntField(dirtyRect, ro.b);
215        dirtyRegion.set(dirty);
216    } else {
217        dirtyRegion.set(Rect(0x3FFF,0x3FFF));
218    }
219
220    Surface::SurfaceInfo info;
221    status_t err = surface->lock(&info, &dirtyRegion);
222    if (err < 0) {
223        const char* const exception = (err == NO_MEMORY) ?
224            OutOfResourcesException :
225            "java/lang/IllegalArgumentException";
226        doThrow(env, exception, NULL);
227        return 0;
228    }
229
230    // Associate a SkCanvas object to this surface
231    jobject canvas = env->GetObjectField(clazz, so.canvas);
232    env->SetIntField(canvas, co.surfaceFormat, info.format);
233
234    SkCanvas* nativeCanvas = (SkCanvas*)env->GetIntField(canvas, no.native_canvas);
235    SkBitmap bitmap;
236    bitmap.setConfig(convertPixelFormat(info.format), info.w, info.h, info.bpr);
237    if (info.w > 0 && info.h > 0) {
238        bitmap.setPixels(info.bits);
239    } else {
240        // be safe with an empty bitmap.
241        bitmap.setPixels(NULL);
242    }
243    nativeCanvas->setBitmapDevice(bitmap);
244    nativeCanvas->clipRegion(dirtyRegion.toSkRegion());
245
246    int saveCount = nativeCanvas->save();
247    env->SetIntField(clazz, so.saveCount, saveCount);
248
249	return canvas;
250}
251
252static void Surface_unlockCanvasAndPost(
253        JNIEnv* env, jobject clazz, jobject argCanvas)
254{
255    jobject canvas = env->GetObjectField(clazz, so.canvas);
256    if (canvas != argCanvas) {
257        doThrow(env, "java/lang/IllegalArgumentException", NULL);
258        return;
259    }
260
261    const sp<Surface>& surface = getSurface(env, clazz);
262    if (!surface->isValid())
263        return;
264
265    // detach the canvas from the surface
266    SkCanvas* nativeCanvas = (SkCanvas*)env->GetIntField(canvas, no.native_canvas);
267    int saveCount = env->GetIntField(clazz, so.saveCount);
268    nativeCanvas->restoreToCount(saveCount);
269    nativeCanvas->setBitmapDevice(SkBitmap());
270    env->SetIntField(clazz, so.saveCount, 0);
271
272    // unlock surface
273    status_t err = surface->unlockAndPost();
274    if (err < 0) {
275        doThrow(env, "java/lang/IllegalArgumentException", NULL);
276    }
277}
278
279static void Surface_unlockCanvas(
280        JNIEnv* env, jobject clazz, jobject argCanvas)
281{
282    jobject canvas = env->GetObjectField(clazz, so.canvas);
283    if (canvas != argCanvas) {
284        doThrow(env, "java/lang/IllegalArgumentException", NULL);
285        return;
286    }
287
288    const sp<Surface>& surface = getSurface(env, clazz);
289    if (!surface->isValid())
290        return;
291
292    status_t err = surface->unlock();
293    if (err < 0) {
294        doThrow(env, "java/lang/IllegalArgumentException", NULL);
295        return;
296    }
297    SkCanvas* nativeCanvas = (SkCanvas*)env->GetIntField(canvas, no.native_canvas);
298    int saveCount = env->GetIntField(clazz, so.saveCount);
299    nativeCanvas->restoreToCount(saveCount);
300    nativeCanvas->setBitmapDevice(SkBitmap());
301    env->SetIntField(clazz, so.saveCount, 0);
302}
303
304static void Surface_openTransaction(
305        JNIEnv* env, jobject clazz)
306{
307    SurfaceComposerClient::openGlobalTransaction();
308}
309
310static void Surface_closeTransaction(
311        JNIEnv* env, jobject clazz)
312{
313    SurfaceComposerClient::closeGlobalTransaction();
314}
315
316static void Surface_setOrientation(
317        JNIEnv* env, jobject clazz, jint display, jint orientation)
318{
319    int err = SurfaceComposerClient::setOrientation(display, orientation);
320    if (err < 0) {
321        doThrow(env, "java/lang/IllegalArgumentException", NULL);
322    }
323}
324
325static void Surface_freezeDisplay(
326        JNIEnv* env, jobject clazz, jint display)
327{
328    int err = SurfaceComposerClient::freezeDisplay(display, 0);
329    if (err < 0) {
330        doThrow(env, "java/lang/IllegalArgumentException", NULL);
331    }
332}
333
334static void Surface_unfreezeDisplay(
335        JNIEnv* env, jobject clazz, jint display)
336{
337    int err = SurfaceComposerClient::unfreezeDisplay(display, 0);
338    if (err < 0) {
339        doThrow(env, "java/lang/IllegalArgumentException", NULL);
340    }
341}
342
343static void Surface_setLayer(
344        JNIEnv* env, jobject clazz, jint zorder)
345{
346    const sp<Surface>& surface = getSurface(env, clazz);
347    if (surface->isValid()) {
348        if (surface->setLayer(zorder) < 0) {
349            doThrow(env, "java/lang/IllegalArgumentException", NULL);
350        }
351    }
352}
353
354static void Surface_setPosition(
355        JNIEnv* env, jobject clazz, jint x, jint y)
356{
357    const sp<Surface>& surface = getSurface(env, clazz);
358    if (surface->isValid()) {
359        if (surface->setPosition(x, y) < 0) {
360            doThrow(env, "java/lang/IllegalArgumentException", NULL);
361        }
362    }
363}
364
365static void Surface_setSize(
366        JNIEnv* env, jobject clazz, jint w, jint h)
367{
368    const sp<Surface>& surface = getSurface(env, clazz);
369    if (surface->isValid()) {
370        if (surface->setSize(w, h) < 0) {
371            doThrow(env, "java/lang/IllegalArgumentException", NULL);
372        }
373    }
374}
375
376static void Surface_hide(
377        JNIEnv* env, jobject clazz)
378{
379    const sp<Surface>& surface = getSurface(env, clazz);
380    if (surface->isValid()) {
381        if (surface->hide() < 0) {
382            doThrow(env, "java/lang/IllegalArgumentException", NULL);
383        }
384    }
385}
386
387static void Surface_show(
388        JNIEnv* env, jobject clazz)
389{
390    const sp<Surface>& surface = getSurface(env, clazz);
391    if (surface->isValid()) {
392        if (surface->show() < 0) {
393            doThrow(env, "java/lang/IllegalArgumentException", NULL);
394        }
395    }
396}
397
398static void Surface_freeze(
399        JNIEnv* env, jobject clazz)
400{
401    const sp<Surface>& surface = getSurface(env, clazz);
402    if (surface->isValid()) {
403        if (surface->freeze() < 0) {
404            doThrow(env, "java/lang/IllegalArgumentException", NULL);
405        }
406    }
407}
408
409static void Surface_unfreeze(
410        JNIEnv* env, jobject clazz)
411{
412    const sp<Surface>& surface = getSurface(env, clazz);
413    if (surface->isValid()) {
414        if (surface->unfreeze() < 0) {
415            doThrow(env, "java/lang/IllegalArgumentException", NULL);
416        }
417    }
418}
419
420static void Surface_setFlags(
421        JNIEnv* env, jobject clazz, jint flags, jint mask)
422{
423    const sp<Surface>& surface = getSurface(env, clazz);
424    if (surface->isValid()) {
425        if (surface->setFlags(flags, mask) < 0) {
426            doThrow(env, "java/lang/IllegalArgumentException", NULL);
427        }
428    }
429}
430
431static void Surface_setTransparentRegion(
432        JNIEnv* env, jobject clazz, jobject argRegion)
433{
434    const sp<Surface>& surface = getSurface(env, clazz);
435    if (surface->isValid()) {
436        SkRegion* nativeRegion = (SkRegion*)env->GetIntField(argRegion, no.native_region);
437        if (surface->setTransparentRegionHint(Region(*nativeRegion)) < 0) {
438            doThrow(env, "java/lang/IllegalArgumentException", NULL);
439        }
440    }
441}
442
443static void Surface_setAlpha(
444        JNIEnv* env, jobject clazz, jfloat alpha)
445{
446    const sp<Surface>& surface = getSurface(env, clazz);
447    if (surface->isValid()) {
448        if (surface->setAlpha(alpha) < 0) {
449            doThrow(env, "java/lang/IllegalArgumentException", NULL);
450        }
451    }
452}
453
454static void Surface_setMatrix(
455        JNIEnv* env, jobject clazz,
456        jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy)
457{
458    const sp<Surface>& surface = getSurface(env, clazz);
459    if (surface->isValid()) {
460        if (surface->setMatrix(dsdx, dtdx, dsdy, dtdy) < 0) {
461            doThrow(env, "java/lang/IllegalArgumentException", NULL);
462        }
463    }
464}
465
466static void Surface_setFreezeTint(
467        JNIEnv* env, jobject clazz,
468        jint tint)
469{
470    const sp<Surface>& surface = getSurface(env, clazz);
471    if (surface->isValid()) {
472        if (surface->setFreezeTint(tint) < 0) {
473            doThrow(env, "java/lang/IllegalArgumentException", NULL);
474        }
475    }
476}
477
478static void Surface_copyFrom(
479        JNIEnv* env, jobject clazz, jobject other)
480{
481    if (clazz == other)
482        return;
483
484    if (other == NULL) {
485        doThrow(env, "java/lang/NullPointerException", NULL);
486        return;
487    }
488
489    const sp<Surface>& surface = getSurface(env, clazz);
490    const sp<Surface>& rhs = getSurface(env, other);
491    if (!Surface::isSameSurface(surface, rhs)) {
492        // we reassign the surface only if it's a different one
493        // otherwise we would loose our client-side state.
494        setSurface(env, clazz, rhs->dup());
495    }
496}
497
498
499static void Surface_readFromParcel(
500        JNIEnv* env, jobject clazz, jobject argParcel)
501{
502    Parcel* parcel = (Parcel*)env->GetIntField( argParcel, no.native_parcel);
503    if (parcel == NULL) {
504        doThrow(env, "java/lang/NullPointerException", NULL);
505        return;
506    }
507
508    const sp<Surface>& surface = getSurface(env, clazz);
509    const sp<Surface>& rhs = Surface::readFromParcel(parcel);
510    if (!Surface::isSameSurface(surface, rhs)) {
511        // we reassign the surface only if it's a different one
512        // otherwise we would loose our client-side state.
513        setSurface(env, clazz, rhs);
514    }
515}
516
517static void Surface_writeToParcel(
518        JNIEnv* env, jobject clazz, jobject argParcel, jint flags)
519{
520    Parcel* parcel = (Parcel*)env->GetIntField(
521            argParcel, no.native_parcel);
522
523    if (parcel == NULL) {
524        doThrow(env, "java/lang/NullPointerException", NULL);
525        return;
526    }
527
528    const sp<Surface>& surface = getSurface(env, clazz);
529    Surface::writeToParcel(surface, parcel);
530}
531
532// ----------------------------------------------------------------------------
533// ----------------------------------------------------------------------------
534// ----------------------------------------------------------------------------
535
536const char* const kSurfaceSessionClassPathName = "android/view/SurfaceSession";
537const char* const kSurfaceClassPathName = "android/view/Surface";
538static void nativeClassInit(JNIEnv* env, jclass clazz);
539
540static JNINativeMethod gSurfaceSessionMethods[] = {
541	{"init",     "()V",  (void*)SurfaceSession_init },
542	{"destroy",  "()V",  (void*)SurfaceSession_destroy },
543    {"kill",     "()V",  (void*)SurfaceSession_kill },
544};
545
546static JNINativeMethod gSurfaceMethods[] = {
547    {"nativeClassInit",     "()V",  (void*)nativeClassInit },
548    {"init",                "(Landroid/view/SurfaceSession;IIIIII)V",  (void*)Surface_init },
549    {"init",                "(Landroid/os/Parcel;)V",  (void*)Surface_initParcel },
550	{"clear",               "()V",  (void*)Surface_clear },
551	{"copyFrom",            "(Landroid/view/Surface;)V",  (void*)Surface_copyFrom },
552	{"isValid",             "()Z",  (void*)Surface_isValid },
553	{"lockCanvasNative",    "(Landroid/graphics/Rect;)Landroid/graphics/Canvas;",  (void*)Surface_lockCanvas },
554	{"unlockCanvasAndPost", "(Landroid/graphics/Canvas;)V", (void*)Surface_unlockCanvasAndPost },
555	{"unlockCanvas",        "(Landroid/graphics/Canvas;)V", (void*)Surface_unlockCanvas },
556	{"openTransaction",     "()V",  (void*)Surface_openTransaction },
557    {"closeTransaction",    "()V",  (void*)Surface_closeTransaction },
558    {"setOrientation",      "(II)V", (void*)Surface_setOrientation },
559    {"freezeDisplay",       "(I)V", (void*)Surface_freezeDisplay },
560    {"unfreezeDisplay",     "(I)V", (void*)Surface_unfreezeDisplay },
561    {"setLayer",            "(I)V", (void*)Surface_setLayer },
562	{"setPosition",         "(II)V",(void*)Surface_setPosition },
563	{"setSize",             "(II)V",(void*)Surface_setSize },
564	{"hide",                "()V",  (void*)Surface_hide },
565	{"show",                "()V",  (void*)Surface_show },
566	{"freeze",              "()V",  (void*)Surface_freeze },
567	{"unfreeze",            "()V",  (void*)Surface_unfreeze },
568	{"setFlags",            "(II)V",(void*)Surface_setFlags },
569	{"setTransparentRegionHint","(Landroid/graphics/Region;)V", (void*)Surface_setTransparentRegion },
570	{"setAlpha",            "(F)V", (void*)Surface_setAlpha },
571	{"setMatrix",           "(FFFF)V",  (void*)Surface_setMatrix },
572	{"setFreezeTint",       "(I)V",  (void*)Surface_setFreezeTint },
573	{"readFromParcel",      "(Landroid/os/Parcel;)V", (void*)Surface_readFromParcel },
574	{"writeToParcel",       "(Landroid/os/Parcel;I)V", (void*)Surface_writeToParcel },
575};
576
577void nativeClassInit(JNIEnv* env, jclass clazz)
578{
579	so.surface   = env->GetFieldID(clazz, "mSurface", "I");
580	so.saveCount = env->GetFieldID(clazz, "mSaveCount", "I");
581	so.canvas    = env->GetFieldID(clazz, "mCanvas", "Landroid/graphics/Canvas;");
582
583    jclass surfaceSession = env->FindClass("android/view/SurfaceSession");
584 	sso.client = env->GetFieldID(surfaceSession, "mClient", "I");
585
586    jclass canvas = env->FindClass("android/graphics/Canvas");
587    no.native_canvas = env->GetFieldID(canvas, "mNativeCanvas", "I");
588    co.surfaceFormat = env->GetFieldID(canvas, "mSurfaceFormat", "I");
589
590    jclass region = env->FindClass("android/graphics/Region");
591    no.native_region = env->GetFieldID(region, "mNativeRegion", "I");
592
593    jclass parcel = env->FindClass("android/os/Parcel");
594    no.native_parcel = env->GetFieldID(parcel, "mObject", "I");
595
596    jclass rect = env->FindClass("android/graphics/Rect");
597    ro.l = env->GetFieldID(rect, "left", "I");
598    ro.t = env->GetFieldID(rect, "top", "I");
599    ro.r = env->GetFieldID(rect, "right", "I");
600    ro.b = env->GetFieldID(rect, "bottom", "I");
601
602    jclass point = env->FindClass("android/graphics/Point");
603    po.x = env->GetFieldID(point, "x", "I");
604    po.y = env->GetFieldID(point, "y", "I");
605}
606
607int register_android_view_Surface(JNIEnv* env)
608{
609    int err;
610    err = AndroidRuntime::registerNativeMethods(env, kSurfaceSessionClassPathName,
611            gSurfaceSessionMethods, NELEM(gSurfaceSessionMethods));
612
613    err |= AndroidRuntime::registerNativeMethods(env, kSurfaceClassPathName,
614            gSurfaceMethods, NELEM(gSurfaceMethods));
615    return err;
616}
617
618};
619
620