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