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