android_view_Surface.cpp revision 55e395ab33f24b009d87a4d45a5566394260fff7
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
484class ScreenshotPixelRef : public SkPixelRef {
485public:
486    ScreenshotPixelRef(SkColorTable* ctable) {
487        fCTable = ctable;
488        SkSafeRef(ctable);
489        setImmutable();
490    }
491    virtual ~ScreenshotPixelRef() {
492        SkSafeUnref(fCTable);
493    }
494
495    status_t update(int width, int height, int minLayer, int maxLayer, bool allLayers) {
496        status_t res = (width > 0 && height > 0)
497                ? (allLayers
498                        ? mScreenshot.update(width, height)
499                        : mScreenshot.update(width, height, minLayer, maxLayer))
500                : mScreenshot.update();
501        if (res != NO_ERROR) {
502            return res;
503        }
504
505        return NO_ERROR;
506    }
507
508    uint32_t getWidth() const {
509        return mScreenshot.getWidth();
510    }
511
512    uint32_t getHeight() const {
513        return mScreenshot.getHeight();
514    }
515
516    uint32_t getStride() const {
517        return mScreenshot.getStride();
518    }
519
520    uint32_t getFormat() const {
521        return mScreenshot.getFormat();
522    }
523
524protected:
525    // overrides from SkPixelRef
526    virtual void* onLockPixels(SkColorTable** ct) {
527        *ct = fCTable;
528        return (void*)mScreenshot.getPixels();
529    }
530
531    virtual void onUnlockPixels() {
532    }
533
534private:
535    ScreenshotClient mScreenshot;
536    SkColorTable*    fCTable;
537
538    typedef SkPixelRef INHERITED;
539};
540
541static jobject doScreenshot(JNIEnv* env, jobject clazz, jint width, jint height,
542        jint minLayer, jint maxLayer, bool allLayers)
543{
544    ScreenshotPixelRef* pixels = new ScreenshotPixelRef(NULL);
545    if (pixels->update(width, height, minLayer, maxLayer, allLayers) != NO_ERROR) {
546        delete pixels;
547        return 0;
548    }
549
550    uint32_t w = pixels->getWidth();
551    uint32_t h = pixels->getHeight();
552    uint32_t s = pixels->getStride();
553    uint32_t f = pixels->getFormat();
554    ssize_t bpr = s * android::bytesPerPixel(f);
555
556    SkBitmap* bitmap = new SkBitmap();
557    bitmap->setConfig(convertPixelFormat(f), w, h, bpr);
558    if (f == PIXEL_FORMAT_RGBX_8888) {
559        bitmap->setIsOpaque(true);
560    }
561
562    if (w > 0 && h > 0) {
563        bitmap->setPixelRef(pixels)->unref();
564        bitmap->lockPixels();
565    } else {
566        // be safe with an empty bitmap.
567        delete pixels;
568        bitmap->setPixels(NULL);
569    }
570
571    return GraphicsJNI::createBitmap(env, bitmap, false, NULL);
572}
573
574static jobject Surface_screenshotAll(JNIEnv* env, jobject clazz, jint width, jint height)
575{
576    return doScreenshot(env, clazz, width, height, 0, 0, true);
577}
578
579static jobject Surface_screenshot(JNIEnv* env, jobject clazz, jint width, jint height,
580        jint minLayer, jint maxLayer)
581{
582    return doScreenshot(env, clazz, width, height, minLayer, maxLayer, false);
583}
584
585static void Surface_setLayer(
586        JNIEnv* env, jobject clazz, jint zorder)
587{
588    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
589    if (surface == 0) return;
590    status_t err = surface->setLayer(zorder);
591    if (err<0 && err!=NO_INIT) {
592        doThrowIAE(env);
593    }
594}
595
596static void Surface_setPosition(
597        JNIEnv* env, jobject clazz, jfloat x, jfloat y)
598{
599    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
600    if (surface == 0) return;
601    status_t err = surface->setPosition(x, y);
602    if (err<0 && err!=NO_INIT) {
603        doThrowIAE(env);
604    }
605}
606
607static void Surface_setSize(
608        JNIEnv* env, jobject clazz, jint w, jint h)
609{
610    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
611    if (surface == 0) return;
612    status_t err = surface->setSize(w, h);
613    if (err<0 && err!=NO_INIT) {
614        doThrowIAE(env);
615    }
616}
617
618static void Surface_hide(
619        JNIEnv* env, jobject clazz)
620{
621    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
622    if (surface == 0) return;
623    status_t err = surface->hide();
624    if (err<0 && err!=NO_INIT) {
625        doThrowIAE(env);
626    }
627}
628
629static void Surface_show(
630        JNIEnv* env, jobject clazz)
631{
632    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
633    if (surface == 0) return;
634    status_t err = surface->show();
635    if (err<0 && err!=NO_INIT) {
636        doThrowIAE(env);
637    }
638}
639
640static void Surface_freeze(
641        JNIEnv* env, jobject clazz)
642{
643    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
644    if (surface == 0) return;
645    status_t err = surface->freeze();
646    if (err<0 && err!=NO_INIT) {
647        doThrowIAE(env);
648    }
649}
650
651static void Surface_unfreeze(
652        JNIEnv* env, jobject clazz)
653{
654    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
655    if (surface == 0) return;
656    status_t err = surface->unfreeze();
657    if (err<0 && err!=NO_INIT) {
658        doThrowIAE(env);
659    }
660}
661
662static void Surface_setFlags(
663        JNIEnv* env, jobject clazz, jint flags, jint mask)
664{
665    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
666    if (surface == 0) return;
667    status_t err = surface->setFlags(flags, mask);
668    if (err<0 && err!=NO_INIT) {
669        doThrowIAE(env);
670    }
671}
672
673static void Surface_setTransparentRegion(
674        JNIEnv* env, jobject clazz, jobject argRegion)
675{
676    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
677    if (surface == 0) return;
678    SkRegion* nativeRegion = (SkRegion*)env->GetIntField(argRegion, no.native_region);
679
680    const SkIRect& b(nativeRegion->getBounds());
681    Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
682    if (nativeRegion->isComplex()) {
683        SkRegion::Iterator it(*nativeRegion);
684        while (!it.done()) {
685            const SkIRect& r(it.rect());
686            reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
687            it.next();
688        }
689    }
690
691    status_t err = surface->setTransparentRegionHint(reg);
692    if (err<0 && err!=NO_INIT) {
693        doThrowIAE(env);
694    }
695}
696
697static void Surface_setAlpha(
698        JNIEnv* env, jobject clazz, jfloat alpha)
699{
700    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
701    if (surface == 0) return;
702    status_t err = surface->setAlpha(alpha);
703    if (err<0 && err!=NO_INIT) {
704        doThrowIAE(env);
705    }
706}
707
708static void Surface_setMatrix(
709        JNIEnv* env, jobject clazz,
710        jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy)
711{
712    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
713    if (surface == 0) return;
714    status_t err = surface->setMatrix(dsdx, dtdx, dsdy, dtdy);
715    if (err<0 && err!=NO_INIT) {
716        doThrowIAE(env);
717    }
718}
719
720static void Surface_setFreezeTint(
721        JNIEnv* env, jobject clazz,
722        jint tint)
723{
724    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
725    if (surface == 0) return;
726    status_t err = surface->setFreezeTint(tint);
727    if (err<0 && err!=NO_INIT) {
728        doThrowIAE(env);
729    }
730}
731
732static void Surface_setWindowCrop(JNIEnv* env, jobject thiz, jobject crop)
733{
734    const sp<SurfaceControl>& surface(getSurfaceControl(env, thiz));
735    if (surface == 0) return;
736
737    Rect nativeCrop;
738    if (crop) {
739        nativeCrop.left  = env->GetIntField(crop, ro.l);
740        nativeCrop.top   = env->GetIntField(crop, ro.t);
741        nativeCrop.right = env->GetIntField(crop, ro.r);
742        nativeCrop.bottom= env->GetIntField(crop, ro.b);
743    } else {
744        nativeCrop.left = nativeCrop.top = nativeCrop.right =
745                nativeCrop.bottom = 0;
746    }
747
748    status_t err = surface->setCrop(nativeCrop);
749    if (err<0 && err!=NO_INIT) {
750        doThrowIAE(env);
751    }
752}
753
754// ----------------------------------------------------------------------------
755
756static void Surface_copyFrom(
757        JNIEnv* env, jobject clazz, jobject other)
758{
759    if (clazz == other)
760        return;
761
762    if (other == NULL) {
763        doThrowNPE(env);
764        return;
765    }
766
767    /*
768     * This is used by the WindowManagerService just after constructing
769     * a Surface and is necessary for returning the Surface reference to
770     * the caller. At this point, we should only have a SurfaceControl.
771     */
772
773    const sp<SurfaceControl>& surface = getSurfaceControl(env, clazz);
774    const sp<SurfaceControl>& rhs = getSurfaceControl(env, other);
775    if (!SurfaceControl::isSameSurface(surface, rhs)) {
776        // we reassign the surface only if it's a different one
777        // otherwise we would loose our client-side state.
778        setSurfaceControl(env, clazz, rhs);
779    }
780}
781
782static void Surface_transferFrom(
783        JNIEnv* env, jobject clazz, jobject other)
784{
785    if (clazz == other)
786        return;
787
788    if (other == NULL) {
789        doThrowNPE(env);
790        return;
791    }
792
793    sp<SurfaceControl> control(getSurfaceControl(env, other));
794    sp<Surface> surface(Surface_getSurface(env, other));
795    setSurfaceControl(env, clazz, control);
796    setSurface(env, clazz, surface);
797    setSurfaceControl(env, other, 0);
798    setSurface(env, other, 0);
799}
800
801static void Surface_readFromParcel(
802        JNIEnv* env, jobject clazz, jobject argParcel)
803{
804    Parcel* parcel = (Parcel*)env->GetIntField( argParcel, no.native_parcel);
805    if (parcel == NULL) {
806        doThrowNPE(env);
807        return;
808    }
809
810    sp<Surface> sur(Surface::readFromParcel(*parcel));
811    setSurface(env, clazz, sur);
812}
813
814static void Surface_writeToParcel(
815        JNIEnv* env, jobject clazz, jobject argParcel, jint flags)
816{
817    Parcel* parcel = (Parcel*)env->GetIntField(
818            argParcel, no.native_parcel);
819
820    if (parcel == NULL) {
821        doThrowNPE(env);
822        return;
823    }
824
825    // The Java instance may have a SurfaceControl (in the case of the
826    // WindowManager or a system app). In that case, we defer to the
827    // SurfaceControl to send its ISurface. Otherwise, if the Surface is
828    // available we let it parcel itself. Finally, if the Surface is also
829    // NULL we fall back to using the SurfaceControl path which sends an
830    // empty surface; this matches legacy behavior.
831    const sp<SurfaceControl>& control(getSurfaceControl(env, clazz));
832    if (control != NULL) {
833        SurfaceControl::writeSurfaceToParcel(control, parcel);
834    } else {
835        sp<Surface> surface(Surface_getSurface(env, clazz));
836        if (surface != NULL) {
837            Surface::writeToParcel(surface, parcel);
838        } else {
839            SurfaceControl::writeSurfaceToParcel(NULL, parcel);
840        }
841    }
842    if (flags & PARCELABLE_WRITE_RETURN_VALUE) {
843        setSurfaceControl(env, clazz, NULL);
844        setSurface(env, clazz, NULL);
845    }
846}
847
848// ----------------------------------------------------------------------------
849// ----------------------------------------------------------------------------
850// ----------------------------------------------------------------------------
851
852static void nativeClassInit(JNIEnv* env, jclass clazz);
853
854static JNINativeMethod gSurfaceSessionMethods[] = {
855    {"init",     "()V",  (void*)SurfaceSession_init },
856    {"destroy",  "()V",  (void*)SurfaceSession_destroy },
857    {"kill",     "()V",  (void*)SurfaceSession_kill },
858};
859
860static JNINativeMethod gSurfaceMethods[] = {
861    {"nativeClassInit",     "()V",  (void*)nativeClassInit },
862    {"init",                "(Landroid/view/SurfaceSession;ILjava/lang/String;IIIII)V",  (void*)Surface_init },
863    {"init",                "(Landroid/os/Parcel;)V",  (void*)Surface_initParcel },
864    {"initFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)V", (void*)Surface_initFromSurfaceTexture },
865    {"getIdentity",         "()I",  (void*)Surface_getIdentity },
866    {"destroy",             "()V",  (void*)Surface_destroy },
867    {"release",             "()V",  (void*)Surface_release },
868    {"copyFrom",            "(Landroid/view/Surface;)V",  (void*)Surface_copyFrom },
869    {"transferFrom",        "(Landroid/view/Surface;)V",  (void*)Surface_transferFrom },
870    {"isValid",             "()Z",  (void*)Surface_isValid },
871    {"lockCanvasNative",    "(Landroid/graphics/Rect;)Landroid/graphics/Canvas;",  (void*)Surface_lockCanvas },
872    {"unlockCanvasAndPost", "(Landroid/graphics/Canvas;)V", (void*)Surface_unlockCanvasAndPost },
873    {"unlockCanvas",        "(Landroid/graphics/Canvas;)V", (void*)Surface_unlockCanvas },
874    {"openTransaction",     "()V",  (void*)Surface_openTransaction },
875    {"closeTransaction",    "()V",  (void*)Surface_closeTransaction },
876    {"setOrientation",      "(III)V", (void*)Surface_setOrientation },
877    {"screenshot",          "(II)Landroid/graphics/Bitmap;", (void*)Surface_screenshotAll },
878    {"screenshot",          "(IIII)Landroid/graphics/Bitmap;", (void*)Surface_screenshot },
879    {"setLayer",            "(I)V", (void*)Surface_setLayer },
880    {"setPosition",         "(FF)V",(void*)Surface_setPosition },
881    {"setSize",             "(II)V",(void*)Surface_setSize },
882    {"hide",                "()V",  (void*)Surface_hide },
883    {"show",                "()V",  (void*)Surface_show },
884    {"freeze",              "()V",  (void*)Surface_freeze },
885    {"unfreeze",            "()V",  (void*)Surface_unfreeze },
886    {"setFlags",            "(II)V",(void*)Surface_setFlags },
887    {"setTransparentRegionHint","(Landroid/graphics/Region;)V", (void*)Surface_setTransparentRegion },
888    {"setAlpha",            "(F)V", (void*)Surface_setAlpha },
889    {"setMatrix",           "(FFFF)V",  (void*)Surface_setMatrix },
890    {"setFreezeTint",       "(I)V",  (void*)Surface_setFreezeTint },
891    {"readFromParcel",      "(Landroid/os/Parcel;)V", (void*)Surface_readFromParcel },
892    {"writeToParcel",       "(Landroid/os/Parcel;I)V", (void*)Surface_writeToParcel },
893    {"isConsumerRunningBehind", "()Z", (void*)Surface_isConsumerRunningBehind },
894    {"setWindowCrop",       "(Landroid/graphics/Rect;)V", (void*)Surface_setWindowCrop },
895};
896
897void nativeClassInit(JNIEnv* env, jclass clazz)
898{
899    so.surface = env->GetFieldID(clazz, ANDROID_VIEW_SURFACE_JNI_ID, "I");
900    so.surfaceGenerationId = env->GetFieldID(clazz, "mSurfaceGenerationId", "I");
901    so.surfaceControl = env->GetFieldID(clazz, "mSurfaceControl", "I");
902    so.saveCount = env->GetFieldID(clazz, "mSaveCount", "I");
903    so.canvas    = env->GetFieldID(clazz, "mCanvas", "Landroid/graphics/Canvas;");
904
905    jclass surfaceSession = env->FindClass("android/view/SurfaceSession");
906    sso.client = env->GetFieldID(surfaceSession, "mClient", "I");
907
908    jclass canvas = env->FindClass("android/graphics/Canvas");
909    no.native_canvas = env->GetFieldID(canvas, "mNativeCanvas", "I");
910    co.surfaceFormat = env->GetFieldID(canvas, "mSurfaceFormat", "I");
911
912    jclass region = env->FindClass("android/graphics/Region");
913    no.native_region = env->GetFieldID(region, "mNativeRegion", "I");
914
915    jclass parcel = env->FindClass("android/os/Parcel");
916    no.native_parcel = env->GetFieldID(parcel, "mNativePtr", "I");
917
918    jclass rect = env->FindClass("android/graphics/Rect");
919    ro.l = env->GetFieldID(rect, "left", "I");
920    ro.t = env->GetFieldID(rect, "top", "I");
921    ro.r = env->GetFieldID(rect, "right", "I");
922    ro.b = env->GetFieldID(rect, "bottom", "I");
923
924    jclass point = env->FindClass("android/graphics/Point");
925    po.x = env->GetFieldID(point, "x", "I");
926    po.y = env->GetFieldID(point, "y", "I");
927}
928
929int register_android_view_Surface(JNIEnv* env)
930{
931    int err;
932    err = AndroidRuntime::registerNativeMethods(env, kSurfaceSessionClassPathName,
933            gSurfaceSessionMethods, NELEM(gSurfaceSessionMethods));
934
935    err |= AndroidRuntime::registerNativeMethods(env, kSurfaceClassPathName,
936            gSurfaceMethods, NELEM(gSurfaceMethods));
937    return err;
938}
939
940};
941