android_view_Surface.cpp revision c14bacf1fb511472138eeb5dc84a9423fc003214
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 jboolean Surface_isConsumerRunningBehind(JNIEnv* env, jobject clazz)
320{
321    int value = 0;
322    const sp<Surface>& surface(getSurface(env, clazz));
323    if (!Surface::isValid(surface)) {
324        doThrowIAE(env);
325        return 0;
326    }
327    ANativeWindow* anw = static_cast<ANativeWindow *>(surface.get());
328    anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value);
329    return (jboolean)value;
330}
331
332static inline SkBitmap::Config convertPixelFormat(PixelFormat format)
333{
334    /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then
335        we can map to SkBitmap::kARGB_8888_Config, and optionally call
336        bitmap.setIsOpaque(true) on the resulting SkBitmap (as an accelerator)
337    */
338    switch (format) {
339    case PIXEL_FORMAT_RGBX_8888:    return SkBitmap::kARGB_8888_Config;
340    case PIXEL_FORMAT_RGBA_8888:    return SkBitmap::kARGB_8888_Config;
341    case PIXEL_FORMAT_RGBA_4444:    return SkBitmap::kARGB_4444_Config;
342    case PIXEL_FORMAT_RGB_565:      return SkBitmap::kRGB_565_Config;
343    case PIXEL_FORMAT_A_8:          return SkBitmap::kA8_Config;
344    default:                        return SkBitmap::kNo_Config;
345    }
346}
347
348static jobject Surface_lockCanvas(JNIEnv* env, jobject clazz, jobject dirtyRect)
349{
350    const sp<Surface>& surface(getSurface(env, clazz));
351    if (!Surface::isValid(surface)) {
352        doThrowIAE(env);
353        return 0;
354    }
355
356    // get dirty region
357    Region dirtyRegion;
358    if (dirtyRect) {
359        Rect dirty;
360        dirty.left  = env->GetIntField(dirtyRect, ro.l);
361        dirty.top   = env->GetIntField(dirtyRect, ro.t);
362        dirty.right = env->GetIntField(dirtyRect, ro.r);
363        dirty.bottom= env->GetIntField(dirtyRect, ro.b);
364        if (!dirty.isEmpty()) {
365            dirtyRegion.set(dirty);
366        }
367    } else {
368        dirtyRegion.set(Rect(0x3FFF,0x3FFF));
369    }
370
371    Surface::SurfaceInfo info;
372    status_t err = surface->lock(&info, &dirtyRegion);
373    if (err < 0) {
374        const char* const exception = (err == NO_MEMORY) ?
375            OutOfResourcesException :
376            "java/lang/IllegalArgumentException";
377        jniThrowException(env, exception, NULL);
378        return 0;
379    }
380
381    // Associate a SkCanvas object to this surface
382    jobject canvas = env->GetObjectField(clazz, so.canvas);
383    env->SetIntField(canvas, co.surfaceFormat, info.format);
384
385    SkCanvas* nativeCanvas = (SkCanvas*)env->GetIntField(canvas, no.native_canvas);
386    SkBitmap bitmap;
387    ssize_t bpr = info.s * bytesPerPixel(info.format);
388    bitmap.setConfig(convertPixelFormat(info.format), info.w, info.h, bpr);
389    if (info.format == PIXEL_FORMAT_RGBX_8888) {
390        bitmap.setIsOpaque(true);
391    }
392    if (info.w > 0 && info.h > 0) {
393        bitmap.setPixels(info.bits);
394    } else {
395        // be safe with an empty bitmap.
396        bitmap.setPixels(NULL);
397    }
398    nativeCanvas->setBitmapDevice(bitmap);
399
400    SkRegion clipReg;
401    if (dirtyRegion.isRect()) { // very common case
402        const Rect b(dirtyRegion.getBounds());
403        clipReg.setRect(b.left, b.top, b.right, b.bottom);
404    } else {
405        size_t count;
406        Rect const* r = dirtyRegion.getArray(&count);
407        while (count) {
408            clipReg.op(r->left, r->top, r->right, r->bottom, SkRegion::kUnion_Op);
409            r++, count--;
410        }
411    }
412
413    nativeCanvas->clipRegion(clipReg);
414
415    int saveCount = nativeCanvas->save();
416    env->SetIntField(clazz, so.saveCount, saveCount);
417
418    if (dirtyRect) {
419        const Rect& bounds(dirtyRegion.getBounds());
420        env->SetIntField(dirtyRect, ro.l, bounds.left);
421        env->SetIntField(dirtyRect, ro.t, bounds.top);
422        env->SetIntField(dirtyRect, ro.r, bounds.right);
423        env->SetIntField(dirtyRect, ro.b, bounds.bottom);
424    }
425
426    return canvas;
427}
428
429static void Surface_unlockCanvasAndPost(
430        JNIEnv* env, jobject clazz, jobject argCanvas)
431{
432    jobject canvas = env->GetObjectField(clazz, so.canvas);
433    if (env->IsSameObject(canvas, argCanvas) == JNI_FALSE) {
434        doThrowIAE(env);
435        return;
436    }
437
438    const sp<Surface>& surface(getSurface(env, clazz));
439    if (!Surface::isValid(surface))
440        return;
441
442    // detach the canvas from the surface
443    SkCanvas* nativeCanvas = (SkCanvas*)env->GetIntField(canvas, no.native_canvas);
444    int saveCount = env->GetIntField(clazz, so.saveCount);
445    nativeCanvas->restoreToCount(saveCount);
446    nativeCanvas->setBitmapDevice(SkBitmap());
447    env->SetIntField(clazz, so.saveCount, 0);
448
449    // unlock surface
450    status_t err = surface->unlockAndPost();
451    if (err < 0) {
452        doThrowIAE(env);
453    }
454}
455
456static void Surface_unlockCanvas(
457        JNIEnv* env, jobject clazz, jobject argCanvas)
458{
459    // XXX: this API has been removed
460    doThrowIAE(env);
461}
462
463static void Surface_openTransaction(
464        JNIEnv* env, jobject clazz)
465{
466    SurfaceComposerClient::openGlobalTransaction();
467}
468
469static void Surface_closeTransaction(
470        JNIEnv* env, jobject clazz)
471{
472    SurfaceComposerClient::closeGlobalTransaction();
473}
474
475static void Surface_setOrientation(
476        JNIEnv* env, jobject clazz, jint display, jint orientation, jint flags)
477{
478    int err = SurfaceComposerClient::setOrientation(display, orientation, flags);
479    if (err < 0) {
480        doThrowIAE(env);
481    }
482}
483
484static void Surface_freezeDisplay(
485        JNIEnv* env, jobject clazz, jint display)
486{
487    int err = SurfaceComposerClient::freezeDisplay(display, 0);
488    if (err < 0) {
489        doThrowIAE(env);
490    }
491}
492
493static void Surface_unfreezeDisplay(
494        JNIEnv* env, jobject clazz, jint display)
495{
496    int err = SurfaceComposerClient::unfreezeDisplay(display, 0);
497    if (err < 0) {
498        doThrowIAE(env);
499    }
500}
501
502class ScreenshotPixelRef : public SkPixelRef {
503public:
504    ScreenshotPixelRef(SkColorTable* ctable) {
505        fCTable = ctable;
506        SkSafeRef(ctable);
507        setImmutable();
508    }
509    virtual ~ScreenshotPixelRef() {
510        SkSafeUnref(fCTable);
511    }
512
513    status_t update(int width, int height, int minLayer, int maxLayer, bool allLayers) {
514        status_t res = (width > 0 && height > 0)
515                ? (allLayers
516                        ? mScreenshot.update(width, height)
517                        : mScreenshot.update(width, height, minLayer, maxLayer))
518                : mScreenshot.update();
519        if (res != NO_ERROR) {
520            return res;
521        }
522
523        return NO_ERROR;
524    }
525
526    uint32_t getWidth() const {
527        return mScreenshot.getWidth();
528    }
529
530    uint32_t getHeight() const {
531        return mScreenshot.getHeight();
532    }
533
534    uint32_t getStride() const {
535        return mScreenshot.getStride();
536    }
537
538    uint32_t getFormat() const {
539        return mScreenshot.getFormat();
540    }
541
542protected:
543    // overrides from SkPixelRef
544    virtual void* onLockPixels(SkColorTable** ct) {
545        *ct = fCTable;
546        return (void*)mScreenshot.getPixels();
547    }
548
549    virtual void onUnlockPixels() {
550    }
551
552private:
553    ScreenshotClient mScreenshot;
554    SkColorTable*    fCTable;
555
556    typedef SkPixelRef INHERITED;
557};
558
559static jobject doScreenshot(JNIEnv* env, jobject clazz, jint width, jint height,
560        jint minLayer, jint maxLayer, bool allLayers)
561{
562    ScreenshotPixelRef* pixels = new ScreenshotPixelRef(NULL);
563    if (pixels->update(width, height, minLayer, maxLayer, allLayers) != NO_ERROR) {
564        delete pixels;
565        return 0;
566    }
567
568    uint32_t w = pixels->getWidth();
569    uint32_t h = pixels->getHeight();
570    uint32_t s = pixels->getStride();
571    uint32_t f = pixels->getFormat();
572    ssize_t bpr = s * android::bytesPerPixel(f);
573
574    SkBitmap* bitmap = new SkBitmap();
575    bitmap->setConfig(convertPixelFormat(f), w, h, bpr);
576    if (f == PIXEL_FORMAT_RGBX_8888) {
577        bitmap->setIsOpaque(true);
578    }
579
580    if (w > 0 && h > 0) {
581        bitmap->setPixelRef(pixels)->unref();
582        bitmap->lockPixels();
583    } else {
584        // be safe with an empty bitmap.
585        delete pixels;
586        bitmap->setPixels(NULL);
587    }
588
589    return GraphicsJNI::createBitmap(env, bitmap, false, NULL);
590}
591
592static jobject Surface_screenshotAll(JNIEnv* env, jobject clazz, jint width, jint height)
593{
594    return doScreenshot(env, clazz, width, height, 0, 0, true);
595}
596
597static jobject Surface_screenshot(JNIEnv* env, jobject clazz, jint width, jint height,
598        jint minLayer, jint maxLayer)
599{
600    return doScreenshot(env, clazz, width, height, minLayer, maxLayer, false);
601}
602
603static void Surface_setLayer(
604        JNIEnv* env, jobject clazz, jint zorder)
605{
606    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
607    if (surface == 0) return;
608    status_t err = surface->setLayer(zorder);
609    if (err<0 && err!=NO_INIT) {
610        doThrowIAE(env);
611    }
612}
613
614static void Surface_setPosition(
615        JNIEnv* env, jobject clazz, jfloat x, jfloat y)
616{
617    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
618    if (surface == 0) return;
619    status_t err = surface->setPosition(x, y);
620    if (err<0 && err!=NO_INIT) {
621        doThrowIAE(env);
622    }
623}
624
625static void Surface_setSize(
626        JNIEnv* env, jobject clazz, jint w, jint h)
627{
628    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
629    if (surface == 0) return;
630    status_t err = surface->setSize(w, h);
631    if (err<0 && err!=NO_INIT) {
632        doThrowIAE(env);
633    }
634}
635
636static void Surface_hide(
637        JNIEnv* env, jobject clazz)
638{
639    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
640    if (surface == 0) return;
641    status_t err = surface->hide();
642    if (err<0 && err!=NO_INIT) {
643        doThrowIAE(env);
644    }
645}
646
647static void Surface_show(
648        JNIEnv* env, jobject clazz)
649{
650    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
651    if (surface == 0) return;
652    status_t err = surface->show();
653    if (err<0 && err!=NO_INIT) {
654        doThrowIAE(env);
655    }
656}
657
658static void Surface_freeze(
659        JNIEnv* env, jobject clazz)
660{
661    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
662    if (surface == 0) return;
663    status_t err = surface->freeze();
664    if (err<0 && err!=NO_INIT) {
665        doThrowIAE(env);
666    }
667}
668
669static void Surface_unfreeze(
670        JNIEnv* env, jobject clazz)
671{
672    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
673    if (surface == 0) return;
674    status_t err = surface->unfreeze();
675    if (err<0 && err!=NO_INIT) {
676        doThrowIAE(env);
677    }
678}
679
680static void Surface_setFlags(
681        JNIEnv* env, jobject clazz, jint flags, jint mask)
682{
683    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
684    if (surface == 0) return;
685    status_t err = surface->setFlags(flags, mask);
686    if (err<0 && err!=NO_INIT) {
687        doThrowIAE(env);
688    }
689}
690
691static void Surface_setTransparentRegion(
692        JNIEnv* env, jobject clazz, jobject argRegion)
693{
694    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
695    if (surface == 0) return;
696    SkRegion* nativeRegion = (SkRegion*)env->GetIntField(argRegion, no.native_region);
697
698    const SkIRect& b(nativeRegion->getBounds());
699    Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
700    if (nativeRegion->isComplex()) {
701        SkRegion::Iterator it(*nativeRegion);
702        while (!it.done()) {
703            const SkIRect& r(it.rect());
704            reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
705            it.next();
706        }
707    }
708
709    status_t err = surface->setTransparentRegionHint(reg);
710    if (err<0 && err!=NO_INIT) {
711        doThrowIAE(env);
712    }
713}
714
715static void Surface_setAlpha(
716        JNIEnv* env, jobject clazz, jfloat alpha)
717{
718    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
719    if (surface == 0) return;
720    status_t err = surface->setAlpha(alpha);
721    if (err<0 && err!=NO_INIT) {
722        doThrowIAE(env);
723    }
724}
725
726static void Surface_setMatrix(
727        JNIEnv* env, jobject clazz,
728        jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy)
729{
730    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
731    if (surface == 0) return;
732    status_t err = surface->setMatrix(dsdx, dtdx, dsdy, dtdy);
733    if (err<0 && err!=NO_INIT) {
734        doThrowIAE(env);
735    }
736}
737
738static void Surface_setFreezeTint(
739        JNIEnv* env, jobject clazz,
740        jint tint)
741{
742    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
743    if (surface == 0) return;
744    status_t err = surface->setFreezeTint(tint);
745    if (err<0 && err!=NO_INIT) {
746        doThrowIAE(env);
747    }
748}
749
750// ----------------------------------------------------------------------------
751
752static void Surface_copyFrom(
753        JNIEnv* env, jobject clazz, jobject other)
754{
755    if (clazz == other)
756        return;
757
758    if (other == NULL) {
759        doThrowNPE(env);
760        return;
761    }
762
763    /*
764     * This is used by the WindowManagerService just after constructing
765     * a Surface and is necessary for returning the Surface reference to
766     * the caller. At this point, we should only have a SurfaceControl.
767     */
768
769    const sp<SurfaceControl>& surface = getSurfaceControl(env, clazz);
770    const sp<SurfaceControl>& rhs = getSurfaceControl(env, other);
771    if (!SurfaceControl::isSameSurface(surface, rhs)) {
772        // we reassign the surface only if it's a different one
773        // otherwise we would loose our client-side state.
774        setSurfaceControl(env, clazz, rhs);
775    }
776}
777
778static void Surface_transferFrom(
779        JNIEnv* env, jobject clazz, jobject other)
780{
781    if (clazz == other)
782        return;
783
784    if (other == NULL) {
785        doThrowNPE(env);
786        return;
787    }
788
789    sp<SurfaceControl> control(getSurfaceControl(env, other));
790    sp<Surface> surface(Surface_getSurface(env, other));
791    setSurfaceControl(env, clazz, control);
792    setSurface(env, clazz, surface);
793    setSurfaceControl(env, other, 0);
794    setSurface(env, other, 0);
795}
796
797static void Surface_readFromParcel(
798        JNIEnv* env, jobject clazz, jobject argParcel)
799{
800    Parcel* parcel = (Parcel*)env->GetIntField( argParcel, no.native_parcel);
801    if (parcel == NULL) {
802        doThrowNPE(env);
803        return;
804    }
805
806    sp<Surface> sur(Surface::readFromParcel(*parcel));
807    setSurface(env, clazz, sur);
808}
809
810static void Surface_writeToParcel(
811        JNIEnv* env, jobject clazz, jobject argParcel, jint flags)
812{
813    Parcel* parcel = (Parcel*)env->GetIntField(
814            argParcel, no.native_parcel);
815
816    if (parcel == NULL) {
817        doThrowNPE(env);
818        return;
819    }
820
821    // The Java instance may have a SurfaceControl (in the case of the
822    // WindowManager or a system app). In that case, we defer to the
823    // SurfaceControl to send its ISurface. Otherwise, if the Surface is
824    // available we let it parcel itself. Finally, if the Surface is also
825    // NULL we fall back to using the SurfaceControl path which sends an
826    // empty surface; this matches legacy behavior.
827    const sp<SurfaceControl>& control(getSurfaceControl(env, clazz));
828    if (control != NULL) {
829        SurfaceControl::writeSurfaceToParcel(control, parcel);
830    } else {
831        sp<Surface> surface(Surface_getSurface(env, clazz));
832        if (surface != NULL) {
833            Surface::writeToParcel(surface, parcel);
834        } else {
835            SurfaceControl::writeSurfaceToParcel(NULL, parcel);
836        }
837    }
838    if (flags & PARCELABLE_WRITE_RETURN_VALUE) {
839        setSurfaceControl(env, clazz, NULL);
840        setSurface(env, clazz, NULL);
841    }
842}
843
844// ----------------------------------------------------------------------------
845// ----------------------------------------------------------------------------
846// ----------------------------------------------------------------------------
847
848static void nativeClassInit(JNIEnv* env, jclass clazz);
849
850static JNINativeMethod gSurfaceSessionMethods[] = {
851    {"init",     "()V",  (void*)SurfaceSession_init },
852    {"destroy",  "()V",  (void*)SurfaceSession_destroy },
853    {"kill",     "()V",  (void*)SurfaceSession_kill },
854};
855
856static JNINativeMethod gSurfaceMethods[] = {
857    {"nativeClassInit",     "()V",  (void*)nativeClassInit },
858    {"init",                "(Landroid/view/SurfaceSession;ILjava/lang/String;IIIII)V",  (void*)Surface_init },
859    {"init",                "(Landroid/os/Parcel;)V",  (void*)Surface_initParcel },
860    {"initFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)V", (void*)Surface_initFromSurfaceTexture },
861    {"getIdentity",         "()I",  (void*)Surface_getIdentity },
862    {"destroy",             "()V",  (void*)Surface_destroy },
863    {"release",             "()V",  (void*)Surface_release },
864    {"copyFrom",            "(Landroid/view/Surface;)V",  (void*)Surface_copyFrom },
865    {"transferFrom",        "(Landroid/view/Surface;)V",  (void*)Surface_transferFrom },
866    {"isValid",             "()Z",  (void*)Surface_isValid },
867    {"lockCanvasNative",    "(Landroid/graphics/Rect;)Landroid/graphics/Canvas;",  (void*)Surface_lockCanvas },
868    {"unlockCanvasAndPost", "(Landroid/graphics/Canvas;)V", (void*)Surface_unlockCanvasAndPost },
869    {"unlockCanvas",        "(Landroid/graphics/Canvas;)V", (void*)Surface_unlockCanvas },
870    {"openTransaction",     "()V",  (void*)Surface_openTransaction },
871    {"closeTransaction",    "()V",  (void*)Surface_closeTransaction },
872    {"setOrientation",      "(III)V", (void*)Surface_setOrientation },
873    {"freezeDisplay",       "(I)V", (void*)Surface_freezeDisplay },
874    {"unfreezeDisplay",     "(I)V", (void*)Surface_unfreezeDisplay },
875    {"screenshot",          "(II)Landroid/graphics/Bitmap;", (void*)Surface_screenshotAll },
876    {"screenshot",          "(IIII)Landroid/graphics/Bitmap;", (void*)Surface_screenshot },
877    {"setLayer",            "(I)V", (void*)Surface_setLayer },
878    {"setPosition",         "(FF)V",(void*)Surface_setPosition },
879    {"setSize",             "(II)V",(void*)Surface_setSize },
880    {"hide",                "()V",  (void*)Surface_hide },
881    {"show",                "()V",  (void*)Surface_show },
882    {"freeze",              "()V",  (void*)Surface_freeze },
883    {"unfreeze",            "()V",  (void*)Surface_unfreeze },
884    {"setFlags",            "(II)V",(void*)Surface_setFlags },
885    {"setTransparentRegionHint","(Landroid/graphics/Region;)V", (void*)Surface_setTransparentRegion },
886    {"setAlpha",            "(F)V", (void*)Surface_setAlpha },
887    {"setMatrix",           "(FFFF)V",  (void*)Surface_setMatrix },
888    {"setFreezeTint",       "(I)V",  (void*)Surface_setFreezeTint },
889    {"readFromParcel",      "(Landroid/os/Parcel;)V", (void*)Surface_readFromParcel },
890    {"writeToParcel",       "(Landroid/os/Parcel;I)V", (void*)Surface_writeToParcel },
891    {"isConsumerRunningBehind", "()Z", (void*)Surface_isConsumerRunningBehind },
892};
893
894void nativeClassInit(JNIEnv* env, jclass clazz)
895{
896    so.surface = env->GetFieldID(clazz, ANDROID_VIEW_SURFACE_JNI_ID, "I");
897    so.surfaceGenerationId = env->GetFieldID(clazz, "mSurfaceGenerationId", "I");
898    so.surfaceControl = env->GetFieldID(clazz, "mSurfaceControl", "I");
899    so.saveCount = env->GetFieldID(clazz, "mSaveCount", "I");
900    so.canvas    = env->GetFieldID(clazz, "mCanvas", "Landroid/graphics/Canvas;");
901
902    jclass surfaceSession = env->FindClass("android/view/SurfaceSession");
903    sso.client = env->GetFieldID(surfaceSession, "mClient", "I");
904
905    jclass canvas = env->FindClass("android/graphics/Canvas");
906    no.native_canvas = env->GetFieldID(canvas, "mNativeCanvas", "I");
907    co.surfaceFormat = env->GetFieldID(canvas, "mSurfaceFormat", "I");
908
909    jclass region = env->FindClass("android/graphics/Region");
910    no.native_region = env->GetFieldID(region, "mNativeRegion", "I");
911
912    jclass parcel = env->FindClass("android/os/Parcel");
913    no.native_parcel = env->GetFieldID(parcel, "mNativePtr", "I");
914
915    jclass rect = env->FindClass("android/graphics/Rect");
916    ro.l = env->GetFieldID(rect, "left", "I");
917    ro.t = env->GetFieldID(rect, "top", "I");
918    ro.r = env->GetFieldID(rect, "right", "I");
919    ro.b = env->GetFieldID(rect, "bottom", "I");
920
921    jclass point = env->FindClass("android/graphics/Point");
922    po.x = env->GetFieldID(point, "x", "I");
923    po.y = env->GetFieldID(point, "y", "I");
924}
925
926int register_android_view_Surface(JNIEnv* env)
927{
928    int err;
929    err = AndroidRuntime::registerNativeMethods(env, kSurfaceSessionClassPathName,
930            gSurfaceSessionMethods, NELEM(gSurfaceSessionMethods));
931
932    err |= AndroidRuntime::registerNativeMethods(env, kSurfaceClassPathName,
933            gSurfaceMethods, NELEM(gSurfaceMethods));
934    return err;
935}
936
937};
938