android_view_Surface.cpp revision 2a83f001fdb189f945e82e81e717ba204824b112
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#define LOG_TAG "Surface"
18
19#include <stdio.h>
20
21#include "android_util_Binder.h"
22#include "android/graphics/GraphicsJNI.h"
23
24#include <binder/IMemory.h>
25#include <surfaceflinger/SurfaceComposerClient.h>
26#include <surfaceflinger/Surface.h>
27#include <ui/Region.h>
28#include <ui/Rect.h>
29
30#include <EGL/egl.h>
31
32#include <SkCanvas.h>
33#include <SkBitmap.h>
34#include <SkRegion.h>
35#include <SkPixelRef.h>
36
37#include "jni.h"
38#include <android_runtime/AndroidRuntime.h>
39#include <android_runtime/android_view_Surface.h>
40#include <utils/misc.h>
41
42
43// ----------------------------------------------------------------------------
44
45namespace android {
46
47enum {
48    // should match Parcelable.java
49    PARCELABLE_WRITE_RETURN_VALUE = 0x0001
50};
51
52// ----------------------------------------------------------------------------
53
54static const char* const OutOfResourcesException =
55    "android/view/Surface$OutOfResourcesException";
56
57struct sso_t {
58    jfieldID client;
59};
60static sso_t sso;
61
62struct so_t {
63    jfieldID surfaceControl;
64    jfieldID surfaceGenerationId;
65    jfieldID surface;
66    jfieldID saveCount;
67    jfieldID canvas;
68};
69static so_t so;
70
71struct ro_t {
72    jfieldID l;
73    jfieldID t;
74    jfieldID r;
75    jfieldID b;
76};
77static ro_t ro;
78
79struct po_t {
80    jfieldID x;
81    jfieldID y;
82};
83static po_t po;
84
85struct co_t {
86    jfieldID surfaceFormat;
87};
88static co_t co;
89
90struct no_t {
91    jfieldID native_canvas;
92    jfieldID native_region;
93    jfieldID native_parcel;
94};
95static no_t no;
96
97
98// ----------------------------------------------------------------------------
99// ----------------------------------------------------------------------------
100// ----------------------------------------------------------------------------
101
102static void SurfaceSession_init(JNIEnv* env, jobject clazz)
103{
104    sp<SurfaceComposerClient> client = new SurfaceComposerClient;
105    client->incStrong(clazz);
106    env->SetIntField(clazz, sso.client, (int)client.get());
107}
108
109static void SurfaceSession_destroy(JNIEnv* env, jobject clazz)
110{
111    SurfaceComposerClient* client =
112            (SurfaceComposerClient*)env->GetIntField(clazz, sso.client);
113    if (client != 0) {
114        client->decStrong(clazz);
115        env->SetIntField(clazz, sso.client, 0);
116    }
117}
118
119static void SurfaceSession_kill(JNIEnv* env, jobject clazz)
120{
121    SurfaceComposerClient* client =
122            (SurfaceComposerClient*)env->GetIntField(clazz, sso.client);
123    if (client != 0) {
124        client->dispose();
125        client->decStrong(clazz);
126        env->SetIntField(clazz, sso.client, 0);
127    }
128}
129
130// ----------------------------------------------------------------------------
131
132static sp<SurfaceControl> getSurfaceControl(JNIEnv* env, jobject clazz)
133{
134    SurfaceControl* const p =
135        (SurfaceControl*)env->GetIntField(clazz, so.surfaceControl);
136    return sp<SurfaceControl>(p);
137}
138
139static void setSurfaceControl(JNIEnv* env, jobject clazz,
140        const sp<SurfaceControl>& surface)
141{
142    SurfaceControl* const p =
143        (SurfaceControl*)env->GetIntField(clazz, so.surfaceControl);
144    if (surface.get()) {
145        surface->incStrong(clazz);
146    }
147    if (p) {
148        p->decStrong(clazz);
149    }
150    env->SetIntField(clazz, so.surfaceControl, (int)surface.get());
151}
152
153static sp<Surface> getSurface(JNIEnv* env, jobject clazz)
154{
155    sp<Surface> result((Surface*)env->GetIntField(clazz, so.surface));
156    if (result == 0) {
157        /*
158         * if this method is called from the WindowManager's process, it means
159         * the client is is not remote, and therefore is allowed to have
160         * a Surface (data), so we create it here.
161         * If we don't have a SurfaceControl, it means we're in a different
162         * process.
163         */
164
165        SurfaceControl* const control =
166            (SurfaceControl*)env->GetIntField(clazz, so.surfaceControl);
167        if (control) {
168            result = control->getSurface();
169            if (result != 0) {
170                result->incStrong(clazz);
171                env->SetIntField(clazz, so.surface, (int)result.get());
172            }
173        }
174    }
175    return result;
176}
177
178sp<ANativeWindow> android_Surface_getNativeWindow(
179        JNIEnv* env, jobject clazz) {
180    return getSurface(env, clazz).get();
181}
182
183static void setSurface(JNIEnv* env, jobject clazz, const sp<Surface>& surface)
184{
185    Surface* const p = (Surface*)env->GetIntField(clazz, so.surface);
186    if (surface.get()) {
187        surface->incStrong(clazz);
188    }
189    if (p) {
190        p->decStrong(clazz);
191    }
192    env->SetIntField(clazz, so.surface, (int)surface.get());
193    // This test is conservative and it would be better to compare the ISurfaces
194    if (p && p != surface.get()) {
195        jint generationId = env->GetIntField(clazz, so.surfaceGenerationId);
196        generationId++;
197        env->SetIntField(clazz, so.surfaceGenerationId, generationId);
198    }
199}
200
201// ----------------------------------------------------------------------------
202
203static void Surface_init(
204        JNIEnv* env, jobject clazz,
205        jobject session,
206        jint pid, jstring jname, jint dpy, jint w, jint h, jint format, jint flags)
207{
208    if (session == NULL) {
209        doThrow(env, "java/lang/NullPointerException");
210        return;
211    }
212
213    SurfaceComposerClient* client =
214            (SurfaceComposerClient*)env->GetIntField(session, sso.client);
215
216    sp<SurfaceControl> surface;
217    if (jname == NULL) {
218        surface = client->createSurface(pid, dpy, w, h, format, flags);
219    } else {
220        const jchar* str = env->GetStringCritical(jname, 0);
221        const String8 name(str, env->GetStringLength(jname));
222        env->ReleaseStringCritical(jname, str);
223        surface = client->createSurface(pid, name, dpy, w, h, format, flags);
224    }
225
226    if (surface == 0) {
227        doThrow(env, OutOfResourcesException);
228        return;
229    }
230    setSurfaceControl(env, clazz, surface);
231}
232
233static void Surface_initParcel(JNIEnv* env, jobject clazz, jobject argParcel)
234{
235    Parcel* parcel = (Parcel*)env->GetIntField(argParcel, no.native_parcel);
236    if (parcel == NULL) {
237        doThrow(env, "java/lang/NullPointerException", NULL);
238        return;
239    }
240
241    sp<Surface> sur(Surface::readFromParcel(*parcel));
242    setSurface(env, clazz, sur);
243}
244
245static jint Surface_getIdentity(JNIEnv* env, jobject clazz)
246{
247    const sp<SurfaceControl>& control(getSurfaceControl(env, clazz));
248    if (control != 0) return (jint) control->getIdentity();
249    const sp<Surface>& surface(getSurface(env, clazz));
250    if (surface != 0) return (jint) surface->getIdentity();
251    return -1;
252}
253
254static void Surface_destroy(JNIEnv* env, jobject clazz, uintptr_t *ostack)
255{
256    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
257    if (SurfaceControl::isValid(surface)) {
258        surface->clear();
259    }
260    setSurfaceControl(env, clazz, 0);
261    setSurface(env, clazz, 0);
262}
263
264static void Surface_release(JNIEnv* env, jobject clazz, uintptr_t *ostack)
265{
266    setSurfaceControl(env, clazz, 0);
267    setSurface(env, clazz, 0);
268}
269
270static jboolean Surface_isValid(JNIEnv* env, jobject clazz)
271{
272    const sp<SurfaceControl>& surfaceControl(getSurfaceControl(env, clazz));
273    if (surfaceControl != 0) {
274        return SurfaceControl::isValid(surfaceControl) ? JNI_TRUE : JNI_FALSE;
275    }
276    const sp<Surface>& surface(getSurface(env, clazz));
277    return Surface::isValid(surface) ? JNI_TRUE : JNI_FALSE;
278}
279
280static inline SkBitmap::Config convertPixelFormat(PixelFormat format)
281{
282    /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then
283        we can map to SkBitmap::kARGB_8888_Config, and optionally call
284        bitmap.setIsOpaque(true) on the resulting SkBitmap (as an accelerator)
285    */
286    switch (format) {
287    case PIXEL_FORMAT_RGBX_8888:    return SkBitmap::kARGB_8888_Config;
288    case PIXEL_FORMAT_RGBA_8888:    return SkBitmap::kARGB_8888_Config;
289    case PIXEL_FORMAT_RGBA_4444:    return SkBitmap::kARGB_4444_Config;
290    case PIXEL_FORMAT_RGB_565:      return SkBitmap::kRGB_565_Config;
291    case PIXEL_FORMAT_A_8:          return SkBitmap::kA8_Config;
292    default:                        return SkBitmap::kNo_Config;
293    }
294}
295
296static jobject Surface_lockCanvas(JNIEnv* env, jobject clazz, jobject dirtyRect)
297{
298    const sp<Surface>& surface(getSurface(env, clazz));
299    if (!Surface::isValid(surface))
300        return 0;
301
302    // get dirty region
303    Region dirtyRegion;
304    if (dirtyRect) {
305        Rect dirty;
306        dirty.left  = env->GetIntField(dirtyRect, ro.l);
307        dirty.top   = env->GetIntField(dirtyRect, ro.t);
308        dirty.right = env->GetIntField(dirtyRect, ro.r);
309        dirty.bottom= env->GetIntField(dirtyRect, ro.b);
310        if (!dirty.isEmpty()) {
311            dirtyRegion.set(dirty);
312        }
313    } else {
314        dirtyRegion.set(Rect(0x3FFF,0x3FFF));
315    }
316
317    Surface::SurfaceInfo info;
318    status_t err = surface->lock(&info, &dirtyRegion);
319    if (err < 0) {
320        const char* const exception = (err == NO_MEMORY) ?
321            OutOfResourcesException :
322            "java/lang/IllegalArgumentException";
323        doThrow(env, exception, NULL);
324        return 0;
325    }
326
327    // Associate a SkCanvas object to this surface
328    jobject canvas = env->GetObjectField(clazz, so.canvas);
329    env->SetIntField(canvas, co.surfaceFormat, info.format);
330
331    SkCanvas* nativeCanvas = (SkCanvas*)env->GetIntField(canvas, no.native_canvas);
332    SkBitmap bitmap;
333    ssize_t bpr = info.s * bytesPerPixel(info.format);
334    bitmap.setConfig(convertPixelFormat(info.format), info.w, info.h, bpr);
335    if (info.format == PIXEL_FORMAT_RGBX_8888) {
336        bitmap.setIsOpaque(true);
337    }
338    if (info.w > 0 && info.h > 0) {
339        bitmap.setPixels(info.bits);
340    } else {
341        // be safe with an empty bitmap.
342        bitmap.setPixels(NULL);
343    }
344    nativeCanvas->setBitmapDevice(bitmap);
345
346    SkRegion clipReg;
347    if (dirtyRegion.isRect()) { // very common case
348        const Rect b(dirtyRegion.getBounds());
349        clipReg.setRect(b.left, b.top, b.right, b.bottom);
350    } else {
351        size_t count;
352        Rect const* r = dirtyRegion.getArray(&count);
353        while (count) {
354            clipReg.op(r->left, r->top, r->right, r->bottom, SkRegion::kUnion_Op);
355            r++, count--;
356        }
357    }
358
359    nativeCanvas->clipRegion(clipReg);
360
361    int saveCount = nativeCanvas->save();
362    env->SetIntField(clazz, so.saveCount, saveCount);
363
364    if (dirtyRect) {
365        const Rect& bounds(dirtyRegion.getBounds());
366        env->SetIntField(dirtyRect, ro.l, bounds.left);
367        env->SetIntField(dirtyRect, ro.t, bounds.top);
368        env->SetIntField(dirtyRect, ro.r, bounds.right);
369        env->SetIntField(dirtyRect, ro.b, bounds.bottom);
370    }
371
372    return canvas;
373}
374
375static void Surface_unlockCanvasAndPost(
376        JNIEnv* env, jobject clazz, jobject argCanvas)
377{
378    jobject canvas = env->GetObjectField(clazz, so.canvas);
379    if (canvas != argCanvas) {
380        doThrow(env, "java/lang/IllegalArgumentException", NULL);
381        return;
382    }
383
384    const sp<Surface>& surface(getSurface(env, clazz));
385    if (!Surface::isValid(surface))
386        return;
387
388    // detach the canvas from the surface
389    SkCanvas* nativeCanvas = (SkCanvas*)env->GetIntField(canvas, no.native_canvas);
390    int saveCount = env->GetIntField(clazz, so.saveCount);
391    nativeCanvas->restoreToCount(saveCount);
392    nativeCanvas->setBitmapDevice(SkBitmap());
393    env->SetIntField(clazz, so.saveCount, 0);
394
395    // unlock surface
396    status_t err = surface->unlockAndPost();
397    if (err < 0) {
398        doThrow(env, "java/lang/IllegalArgumentException", NULL);
399    }
400}
401
402static void Surface_unlockCanvas(
403        JNIEnv* env, jobject clazz, jobject argCanvas)
404{
405    // XXX: this API has been removed
406    doThrow(env, "java/lang/IllegalArgumentException", NULL);
407}
408
409static void Surface_openTransaction(
410        JNIEnv* env, jobject clazz)
411{
412    SurfaceComposerClient::openGlobalTransaction();
413}
414
415static void Surface_closeTransaction(
416        JNIEnv* env, jobject clazz)
417{
418    SurfaceComposerClient::closeGlobalTransaction();
419}
420
421static void Surface_setOrientation(
422        JNIEnv* env, jobject clazz, jint display, jint orientation, jint flags)
423{
424    int err = SurfaceComposerClient::setOrientation(display, orientation, flags);
425    if (err < 0) {
426        doThrow(env, "java/lang/IllegalArgumentException", NULL);
427    }
428}
429
430static void Surface_freezeDisplay(
431        JNIEnv* env, jobject clazz, jint display)
432{
433    int err = SurfaceComposerClient::freezeDisplay(display, 0);
434    if (err < 0) {
435        doThrow(env, "java/lang/IllegalArgumentException", NULL);
436    }
437}
438
439static void Surface_unfreezeDisplay(
440        JNIEnv* env, jobject clazz, jint display)
441{
442    int err = SurfaceComposerClient::unfreezeDisplay(display, 0);
443    if (err < 0) {
444        doThrow(env, "java/lang/IllegalArgumentException", NULL);
445    }
446}
447
448class ScreenshotPixelRef : public SkPixelRef {
449public:
450    ScreenshotPixelRef(SkColorTable* ctable) {
451        fCTable = ctable;
452        ctable->safeRef();
453        setImmutable();
454    }
455    virtual ~ScreenshotPixelRef() {
456        SkSafeUnref(fCTable);
457    }
458
459    status_t update(int width, int height, int minLayer, int maxLayer, bool allLayers) {
460        status_t res = (width > 0 && height > 0)
461                ? (allLayers
462                        ? mScreenshot.update(width, height)
463                        : mScreenshot.update(width, height, minLayer, maxLayer))
464                : mScreenshot.update();
465        if (res != NO_ERROR) {
466            return res;
467        }
468
469        return NO_ERROR;
470    }
471
472    uint32_t getWidth() const {
473        return mScreenshot.getWidth();
474    }
475
476    uint32_t getHeight() const {
477        return mScreenshot.getHeight();
478    }
479
480    uint32_t getStride() const {
481        return mScreenshot.getStride();
482    }
483
484    uint32_t getFormat() const {
485        return mScreenshot.getFormat();
486    }
487
488protected:
489    // overrides from SkPixelRef
490    virtual void* onLockPixels(SkColorTable** ct) {
491        *ct = fCTable;
492        return (void*)mScreenshot.getPixels();
493    }
494
495    virtual void onUnlockPixels() {
496    }
497
498private:
499    ScreenshotClient mScreenshot;
500    SkColorTable*    fCTable;
501
502    typedef SkPixelRef INHERITED;
503};
504
505static jobject doScreenshot(JNIEnv* env, jobject clazz, jint width, jint height,
506        jint minLayer, jint maxLayer, bool allLayers)
507{
508    ScreenshotPixelRef* pixels = new ScreenshotPixelRef(NULL);
509    if (pixels->update(width, height, minLayer, maxLayer, allLayers) != NO_ERROR) {
510        delete pixels;
511        return 0;
512    }
513
514    uint32_t w = pixels->getWidth();
515    uint32_t h = pixels->getHeight();
516    uint32_t s = pixels->getStride();
517    uint32_t f = pixels->getFormat();
518    ssize_t bpr = s * android::bytesPerPixel(f);
519
520    SkBitmap* bitmap = new SkBitmap();
521    bitmap->setConfig(convertPixelFormat(f), w, h, bpr);
522    if (f == PIXEL_FORMAT_RGBX_8888) {
523        bitmap->setIsOpaque(true);
524    }
525
526    if (w > 0 && h > 0) {
527        bitmap->setPixelRef(pixels)->unref();
528        bitmap->lockPixels();
529    } else {
530        // be safe with an empty bitmap.
531        delete pixels;
532        bitmap->setPixels(NULL);
533    }
534
535    return GraphicsJNI::createBitmap(env, bitmap, false, NULL);
536}
537
538static jobject Surface_screenshotAll(JNIEnv* env, jobject clazz, jint width, jint height)
539{
540    return doScreenshot(env, clazz, width, height, 0, 0, true);
541}
542
543static jobject Surface_screenshot(JNIEnv* env, jobject clazz, jint width, jint height,
544        jint minLayer, jint maxLayer, bool allLayers)
545{
546    return doScreenshot(env, clazz, width, height, minLayer, maxLayer, false);
547}
548
549static void Surface_setLayer(
550        JNIEnv* env, jobject clazz, jint zorder)
551{
552    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
553    if (surface == 0) return;
554    status_t err = surface->setLayer(zorder);
555    if (err<0 && err!=NO_INIT)
556        doThrow(env, "java/lang/IllegalArgumentException", NULL);
557}
558
559static void Surface_setPosition(
560        JNIEnv* env, jobject clazz, jint x, jint y)
561{
562    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
563    if (surface == 0) return;
564    status_t err = surface->setPosition(x, y);
565    if (err<0 && err!=NO_INIT)
566        doThrow(env, "java/lang/IllegalArgumentException", NULL);
567}
568
569static void Surface_setSize(
570        JNIEnv* env, jobject clazz, jint w, jint h)
571{
572    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
573    if (surface == 0) return;
574    status_t err = surface->setSize(w, h);
575    if (err<0 && err!=NO_INIT)
576        doThrow(env, "java/lang/IllegalArgumentException", NULL);
577}
578
579static void Surface_hide(
580        JNIEnv* env, jobject clazz)
581{
582    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
583    if (surface == 0) return;
584    status_t err = surface->hide();
585    if (err<0 && err!=NO_INIT)
586        doThrow(env, "java/lang/IllegalArgumentException", NULL);
587}
588
589static void Surface_show(
590        JNIEnv* env, jobject clazz)
591{
592    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
593    if (surface == 0) return;
594    status_t err = surface->show();
595    if (err<0 && err!=NO_INIT)
596        doThrow(env, "java/lang/IllegalArgumentException", NULL);
597}
598
599static void Surface_freeze(
600        JNIEnv* env, jobject clazz)
601{
602    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
603    if (surface == 0) return;
604    status_t err = surface->freeze();
605    if (err<0 && err!=NO_INIT)
606        doThrow(env, "java/lang/IllegalArgumentException", NULL);
607}
608
609static void Surface_unfreeze(
610        JNIEnv* env, jobject clazz)
611{
612    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
613    if (surface == 0) return;
614    status_t err = surface->unfreeze();
615    if (err<0 && err!=NO_INIT)
616        doThrow(env, "java/lang/IllegalArgumentException", NULL);
617}
618
619static void Surface_setFlags(
620        JNIEnv* env, jobject clazz, jint flags, jint mask)
621{
622    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
623    if (surface == 0) return;
624    status_t err = surface->setFlags(flags, mask);
625    if (err<0 && err!=NO_INIT)
626        doThrow(env, "java/lang/IllegalArgumentException", NULL);
627}
628
629static void Surface_setTransparentRegion(
630        JNIEnv* env, jobject clazz, jobject argRegion)
631{
632    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
633    if (surface == 0) return;
634    SkRegion* nativeRegion = (SkRegion*)env->GetIntField(argRegion, no.native_region);
635
636    const SkIRect& b(nativeRegion->getBounds());
637    Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
638    if (nativeRegion->isComplex()) {
639        SkRegion::Iterator it(*nativeRegion);
640        while (!it.done()) {
641            const SkIRect& r(it.rect());
642            reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
643            it.next();
644        }
645    }
646
647    status_t err = surface->setTransparentRegionHint(reg);
648    if (err<0 && err!=NO_INIT)
649        doThrow(env, "java/lang/IllegalArgumentException", NULL);
650}
651
652static void Surface_setAlpha(
653        JNIEnv* env, jobject clazz, jfloat alpha)
654{
655    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
656    if (surface == 0) return;
657    status_t err = surface->setAlpha(alpha);
658    if (err<0 && err!=NO_INIT)
659        doThrow(env, "java/lang/IllegalArgumentException", NULL);
660}
661
662static void Surface_setMatrix(
663        JNIEnv* env, jobject clazz,
664        jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy)
665{
666    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
667    if (surface == 0) return;
668    status_t err = surface->setMatrix(dsdx, dtdx, dsdy, dtdy);
669    if (err<0 && err!=NO_INIT)
670        doThrow(env, "java/lang/IllegalArgumentException", NULL);
671}
672
673static void Surface_setFreezeTint(
674        JNIEnv* env, jobject clazz,
675        jint tint)
676{
677    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
678    if (surface == 0) return;
679    status_t err = surface->setFreezeTint(tint);
680    if (err<0 && err!=NO_INIT)
681        doThrow(env, "java/lang/IllegalArgumentException", NULL);
682}
683
684// ----------------------------------------------------------------------------
685
686static void Surface_copyFrom(
687        JNIEnv* env, jobject clazz, jobject other)
688{
689    if (clazz == other)
690        return;
691
692    if (other == NULL) {
693        doThrow(env, "java/lang/NullPointerException", NULL);
694        return;
695    }
696
697    /*
698     * This is used by the WindowManagerService just after constructing
699     * a Surface and is necessary for returning the Surface reference to
700     * the caller. At this point, we should only have a SurfaceControl.
701     */
702
703    const sp<SurfaceControl>& surface = getSurfaceControl(env, clazz);
704    const sp<SurfaceControl>& rhs = getSurfaceControl(env, other);
705    if (!SurfaceControl::isSameSurface(surface, rhs)) {
706        // we reassign the surface only if it's a different one
707        // otherwise we would loose our client-side state.
708        setSurfaceControl(env, clazz, rhs);
709    }
710}
711
712static void Surface_readFromParcel(
713        JNIEnv* env, jobject clazz, jobject argParcel)
714{
715    Parcel* parcel = (Parcel*)env->GetIntField( argParcel, no.native_parcel);
716    if (parcel == NULL) {
717        doThrow(env, "java/lang/NullPointerException", NULL);
718        return;
719    }
720
721    sp<Surface> sur(Surface::readFromParcel(*parcel));
722    setSurface(env, clazz, sur);
723}
724
725static void Surface_writeToParcel(
726        JNIEnv* env, jobject clazz, jobject argParcel, jint flags)
727{
728    Parcel* parcel = (Parcel*)env->GetIntField(
729            argParcel, no.native_parcel);
730
731    if (parcel == NULL) {
732        doThrow(env, "java/lang/NullPointerException", NULL);
733        return;
734    }
735
736    const sp<SurfaceControl>& control(getSurfaceControl(env, clazz));
737    SurfaceControl::writeSurfaceToParcel(control, parcel);
738    if (flags & PARCELABLE_WRITE_RETURN_VALUE) {
739        setSurfaceControl(env, clazz, 0);
740    }
741}
742
743// ----------------------------------------------------------------------------
744// ----------------------------------------------------------------------------
745// ----------------------------------------------------------------------------
746
747const char* const kSurfaceSessionClassPathName = "android/view/SurfaceSession";
748const char* const kSurfaceClassPathName = "android/view/Surface";
749static void nativeClassInit(JNIEnv* env, jclass clazz);
750
751static JNINativeMethod gSurfaceSessionMethods[] = {
752    {"init",     "()V",  (void*)SurfaceSession_init },
753    {"destroy",  "()V",  (void*)SurfaceSession_destroy },
754    {"kill",     "()V",  (void*)SurfaceSession_kill },
755};
756
757static JNINativeMethod gSurfaceMethods[] = {
758    {"nativeClassInit",     "()V",  (void*)nativeClassInit },
759    {"init",                "(Landroid/view/SurfaceSession;ILjava/lang/String;IIIII)V",  (void*)Surface_init },
760    {"init",                "(Landroid/os/Parcel;)V",  (void*)Surface_initParcel },
761    {"getIdentity",         "()I",  (void*)Surface_getIdentity },
762    {"destroy",             "()V",  (void*)Surface_destroy },
763    {"release",             "()V",  (void*)Surface_release },
764    {"copyFrom",            "(Landroid/view/Surface;)V",  (void*)Surface_copyFrom },
765    {"isValid",             "()Z",  (void*)Surface_isValid },
766    {"lockCanvasNative",    "(Landroid/graphics/Rect;)Landroid/graphics/Canvas;",  (void*)Surface_lockCanvas },
767    {"unlockCanvasAndPost", "(Landroid/graphics/Canvas;)V", (void*)Surface_unlockCanvasAndPost },
768    {"unlockCanvas",        "(Landroid/graphics/Canvas;)V", (void*)Surface_unlockCanvas },
769    {"openTransaction",     "()V",  (void*)Surface_openTransaction },
770    {"closeTransaction",    "()V",  (void*)Surface_closeTransaction },
771    {"setOrientation",      "(III)V", (void*)Surface_setOrientation },
772    {"freezeDisplay",       "(I)V", (void*)Surface_freezeDisplay },
773    {"unfreezeDisplay",     "(I)V", (void*)Surface_unfreezeDisplay },
774    {"screenshot",          "(II)Landroid/graphics/Bitmap;", (void*)Surface_screenshotAll },
775    {"screenshot",          "(IIII)Landroid/graphics/Bitmap;", (void*)Surface_screenshot },
776    {"setLayer",            "(I)V", (void*)Surface_setLayer },
777    {"setPosition",         "(II)V",(void*)Surface_setPosition },
778    {"setSize",             "(II)V",(void*)Surface_setSize },
779    {"hide",                "()V",  (void*)Surface_hide },
780    {"show",                "()V",  (void*)Surface_show },
781    {"freeze",              "()V",  (void*)Surface_freeze },
782    {"unfreeze",            "()V",  (void*)Surface_unfreeze },
783    {"setFlags",            "(II)V",(void*)Surface_setFlags },
784    {"setTransparentRegionHint","(Landroid/graphics/Region;)V", (void*)Surface_setTransparentRegion },
785    {"setAlpha",            "(F)V", (void*)Surface_setAlpha },
786    {"setMatrix",           "(FFFF)V",  (void*)Surface_setMatrix },
787    {"setFreezeTint",       "(I)V",  (void*)Surface_setFreezeTint },
788    {"readFromParcel",      "(Landroid/os/Parcel;)V", (void*)Surface_readFromParcel },
789    {"writeToParcel",       "(Landroid/os/Parcel;I)V", (void*)Surface_writeToParcel },
790};
791
792void nativeClassInit(JNIEnv* env, jclass clazz)
793{
794    so.surface = env->GetFieldID(clazz, ANDROID_VIEW_SURFACE_JNI_ID, "I");
795    so.surfaceGenerationId = env->GetFieldID(clazz, "mSurfaceGenerationId", "I");
796    so.surfaceControl = env->GetFieldID(clazz, "mSurfaceControl", "I");
797    so.saveCount = env->GetFieldID(clazz, "mSaveCount", "I");
798    so.canvas    = env->GetFieldID(clazz, "mCanvas", "Landroid/graphics/Canvas;");
799
800    jclass surfaceSession = env->FindClass("android/view/SurfaceSession");
801    sso.client = env->GetFieldID(surfaceSession, "mClient", "I");
802
803    jclass canvas = env->FindClass("android/graphics/Canvas");
804    no.native_canvas = env->GetFieldID(canvas, "mNativeCanvas", "I");
805    co.surfaceFormat = env->GetFieldID(canvas, "mSurfaceFormat", "I");
806
807    jclass region = env->FindClass("android/graphics/Region");
808    no.native_region = env->GetFieldID(region, "mNativeRegion", "I");
809
810    jclass parcel = env->FindClass("android/os/Parcel");
811    no.native_parcel = env->GetFieldID(parcel, "mObject", "I");
812
813    jclass rect = env->FindClass("android/graphics/Rect");
814    ro.l = env->GetFieldID(rect, "left", "I");
815    ro.t = env->GetFieldID(rect, "top", "I");
816    ro.r = env->GetFieldID(rect, "right", "I");
817    ro.b = env->GetFieldID(rect, "bottom", "I");
818
819    jclass point = env->FindClass("android/graphics/Point");
820    po.x = env->GetFieldID(point, "x", "I");
821    po.y = env->GetFieldID(point, "y", "I");
822}
823
824int register_android_view_Surface(JNIEnv* env)
825{
826    int err;
827    err = AndroidRuntime::registerNativeMethods(env, kSurfaceSessionClassPathName,
828            gSurfaceSessionMethods, NELEM(gSurfaceSessionMethods));
829
830    err |= AndroidRuntime::registerNativeMethods(env, kSurfaceClassPathName,
831            gSurfaceMethods, NELEM(gSurfaceMethods));
832    return err;
833}
834
835};
836