android_view_Surface.cpp revision a8a0aa8b922c45fb4633f51610f264a19f9bd825
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <stdio.h>
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "android_util_Binder.h"
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
21000479f9e325b4e426a67033abd92d47da412725Mathias Agopian#include <surfaceflinger/SurfaceComposerClient.h>
228b138323d54e36fd66b22a634494648e20c53373Mathias Agopian#include <surfaceflinger/Surface.h>
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <ui/Region.h>
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <ui/Rect.h>
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <SkCanvas.h>
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <SkBitmap.h>
286158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian#include <SkRegion.h>
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "jni.h"
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <android_runtime/AndroidRuntime.h>
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/misc.h>
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnamespace android {
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
39af1e11b849ea527f4b1b1695924ce42cc9d7f670Mathias Agopianenum {
40af1e11b849ea527f4b1b1695924ce42cc9d7f670Mathias Agopian    // should match Parcelable.java
41af1e11b849ea527f4b1b1695924ce42cc9d7f670Mathias Agopian    PARCELABLE_WRITE_RETURN_VALUE = 0x0001
42af1e11b849ea527f4b1b1695924ce42cc9d7f670Mathias Agopian};
43af1e11b849ea527f4b1b1695924ce42cc9d7f670Mathias Agopian
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic const char* const OutOfResourcesException =
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    "android/view/Surface$OutOfResourcesException";
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct sso_t {
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID client;
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic sso_t sso;
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct so_t {
5517f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    jfieldID surfaceControl;
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID surface;
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID saveCount;
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID canvas;
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic so_t so;
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct ro_t {
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID l;
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID t;
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID r;
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID b;
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic ro_t ro;
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct po_t {
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID x;
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID y;
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic po_t po;
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct co_t {
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID surfaceFormat;
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic co_t co;
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstruct no_t {
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID native_canvas;
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID native_region;
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jfieldID native_parcel;
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic no_t no;
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic __attribute__((noinline))
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (!env->ExceptionOccurred()) {
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jclass npeClazz = env->FindClass(exc);
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->ThrowNew(npeClazz, msg);
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void SurfaceSession_init(JNIEnv* env, jobject clazz)
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    sp<SurfaceComposerClient> client = new SurfaceComposerClient;
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    client->incStrong(clazz);
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(clazz, sso.client, (int)client.get());
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void SurfaceSession_destroy(JNIEnv* env, jobject clazz)
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SurfaceComposerClient* client =
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            (SurfaceComposerClient*)env->GetIntField(clazz, sso.client);
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (client != 0) {
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        client->decStrong(clazz);
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->SetIntField(clazz, sso.client, 0);
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void SurfaceSession_kill(JNIEnv* env, jobject clazz)
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SurfaceComposerClient* client =
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            (SurfaceComposerClient*)env->GetIntField(clazz, sso.client);
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (client != 0) {
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        client->dispose();
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        client->decStrong(clazz);
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->SetIntField(clazz, sso.client, 0);
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
13217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopianstatic sp<SurfaceControl> getSurfaceControl(JNIEnv* env, jobject clazz)
13317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian{
13417f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    SurfaceControl* const p =
13517f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        (SurfaceControl*)env->GetIntField(clazz, so.surfaceControl);
13617f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    return sp<SurfaceControl>(p);
13717f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian}
13817f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian
13917f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopianstatic void setSurfaceControl(JNIEnv* env, jobject clazz,
14017f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        const sp<SurfaceControl>& surface)
14117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian{
14217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    SurfaceControl* const p =
14317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        (SurfaceControl*)env->GetIntField(clazz, so.surfaceControl);
14417f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (surface.get()) {
14517f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        surface->incStrong(clazz);
14617f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    }
14717f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (p) {
14817f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        p->decStrong(clazz);
14917f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    }
15017f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    env->SetIntField(clazz, so.surfaceControl, (int)surface.get());
15117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian}
15217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic sp<Surface> getSurface(JNIEnv* env, jobject clazz)
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
15569d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian    sp<Surface> result((Surface*)env->GetIntField(clazz, so.surface));
15669d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian    if (result == 0) {
15769d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian        /*
15869d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian         * if this method is called from the WindowManager's process, it means
15969d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian         * the client is is not remote, and therefore is allowed to have
16069d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian         * a Surface (data), so we create it here.
16169d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian         * If we don't have a SurfaceControl, it means we're in a different
16269d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian         * process.
16369d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian         */
16469d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian
16569d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian        SurfaceControl* const control =
16669d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian            (SurfaceControl*)env->GetIntField(clazz, so.surfaceControl);
16769d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian        if (control) {
16869d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian            result = control->getSurface();
16969d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian            if (result != 0) {
17069d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian                result->incStrong(clazz);
17169d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian                env->SetIntField(clazz, so.surface, (int)result.get());
17269d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian            }
17369d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian        }
17469d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian    }
17569d62097e8195c947de7e4cdc4a491181aa56e61Mathias Agopian    return result;
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void setSurface(JNIEnv* env, jobject clazz, const sp<Surface>& surface)
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Surface* const p = (Surface*)env->GetIntField(clazz, so.surface);
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (surface.get()) {
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        surface->incStrong(clazz);
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (p) {
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        p->decStrong(clazz);
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(clazz, so.surface, (int)surface.get());
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_init(
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz,
1945d26c1e38dabb3ad8b4b6e1000375f3b1a6b7693Mathias Agopian        jobject session,
1955d26c1e38dabb3ad8b4b6e1000375f3b1a6b7693Mathias Agopian        jint pid, jstring jname, jint dpy, jint w, jint h, jint format, jint flags)
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (session == NULL) {
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doThrow(env, "java/lang/NullPointerException");
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SurfaceComposerClient* client =
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            (SurfaceComposerClient*)env->GetIntField(session, sso.client);
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2055d26c1e38dabb3ad8b4b6e1000375f3b1a6b7693Mathias Agopian    sp<SurfaceControl> surface;
2065d26c1e38dabb3ad8b4b6e1000375f3b1a6b7693Mathias Agopian    if (jname == NULL) {
2075d26c1e38dabb3ad8b4b6e1000375f3b1a6b7693Mathias Agopian        surface = client->createSurface(pid, dpy, w, h, format, flags);
2085d26c1e38dabb3ad8b4b6e1000375f3b1a6b7693Mathias Agopian    } else {
2095d26c1e38dabb3ad8b4b6e1000375f3b1a6b7693Mathias Agopian        const jchar* str = env->GetStringCritical(jname, 0);
2105d26c1e38dabb3ad8b4b6e1000375f3b1a6b7693Mathias Agopian        const String8 name(str, env->GetStringLength(jname));
2115d26c1e38dabb3ad8b4b6e1000375f3b1a6b7693Mathias Agopian        env->ReleaseStringCritical(jname, str);
2125d26c1e38dabb3ad8b4b6e1000375f3b1a6b7693Mathias Agopian        surface = client->createSurface(pid, name, dpy, w, h, format, flags);
2135d26c1e38dabb3ad8b4b6e1000375f3b1a6b7693Mathias Agopian    }
2145d26c1e38dabb3ad8b4b6e1000375f3b1a6b7693Mathias Agopian
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (surface == 0) {
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doThrow(env, OutOfResourcesException);
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
21917f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    setSurfaceControl(env, clazz, surface);
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_initParcel(JNIEnv* env, jobject clazz, jobject argParcel)
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Parcel* parcel = (Parcel*)env->GetIntField(argParcel, no.native_parcel);
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (parcel == NULL) {
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doThrow(env, "java/lang/NullPointerException", NULL);
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
22917f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    sp<Surface> rhs = new Surface(*parcel);
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    setSurface(env, clazz, rhs);
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2330586a1b77a788a119166a37fccd909bf9ed65f23Dianne Hackbornstatic void Surface_destroy(JNIEnv* env, jobject clazz, uintptr_t *ostack)
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
23517f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
23617f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (SurfaceControl::isValid(surface)) {
237402c34649f514669517c2208e35caa58ff8bb2b9Mathias Agopian        surface->clear();
238402c34649f514669517c2208e35caa58ff8bb2b9Mathias Agopian    }
23917f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    setSurfaceControl(env, clazz, 0);
240402c34649f514669517c2208e35caa58ff8bb2b9Mathias Agopian    setSurface(env, clazz, 0);
241402c34649f514669517c2208e35caa58ff8bb2b9Mathias Agopian}
242402c34649f514669517c2208e35caa58ff8bb2b9Mathias Agopian
243402c34649f514669517c2208e35caa58ff8bb2b9Mathias Agopianstatic void Surface_release(JNIEnv* env, jobject clazz, uintptr_t *ostack)
244402c34649f514669517c2208e35caa58ff8bb2b9Mathias Agopian{
24517f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    setSurfaceControl(env, clazz, 0);
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    setSurface(env, clazz, 0);
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jboolean Surface_isValid(JNIEnv* env, jobject clazz)
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
25117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surfaceControl(getSurfaceControl(env, clazz));
25217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (surfaceControl != 0) {
25317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        return SurfaceControl::isValid(surfaceControl) ? JNI_TRUE : JNI_FALSE;
25417f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    }
25517f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<Surface>& surface(getSurface(env, clazz));
256402c34649f514669517c2208e35caa58ff8bb2b9Mathias Agopian    return Surface::isValid(surface) ? JNI_TRUE : JNI_FALSE;
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic inline SkBitmap::Config convertPixelFormat(PixelFormat format)
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
261f1e5b0d4e5d0d2a78c234cd0cbd3005a74a79429Mathias Agopian    /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then
2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        we can map to SkBitmap::kARGB_8888_Config, and optionally call
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        bitmap.setIsOpaque(true) on the resulting SkBitmap (as an accelerator)
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    */
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	switch (format) {
266f1e5b0d4e5d0d2a78c234cd0cbd3005a74a79429Mathias Agopian	case PIXEL_FORMAT_RGBX_8888:    return SkBitmap::kARGB_8888_Config;
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    case PIXEL_FORMAT_RGBA_8888:    return SkBitmap::kARGB_8888_Config;
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    case PIXEL_FORMAT_RGBA_4444:    return SkBitmap::kARGB_4444_Config;
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	case PIXEL_FORMAT_RGB_565:		return SkBitmap::kRGB_565_Config;
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	case PIXEL_FORMAT_A_8:          return SkBitmap::kA8_Config;
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	default:                        return SkBitmap::kNo_Config;
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	}
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic jobject Surface_lockCanvas(JNIEnv* env, jobject clazz, jobject dirtyRect)
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
27717f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<Surface>& surface(getSurface(env, clazz));
278402c34649f514669517c2208e35caa58ff8bb2b9Mathias Agopian    if (!Surface::isValid(surface))
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // get dirty region
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Region dirtyRegion;
2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (dirtyRect) {
2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Rect dirty;
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        dirty.left  = env->GetIntField(dirtyRect, ro.l);
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        dirty.top   = env->GetIntField(dirtyRect, ro.t);
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        dirty.right = env->GetIntField(dirtyRect, ro.r);
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        dirty.bottom= env->GetIntField(dirtyRect, ro.b);
2896158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian        if (!dirty.isEmpty()) {
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            dirtyRegion.set(dirty);
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else {
2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        dirtyRegion.set(Rect(0x3FFF,0x3FFF));
2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Surface::SurfaceInfo info;
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    status_t err = surface->lock(&info, &dirtyRegion);
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (err < 0) {
2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const char* const exception = (err == NO_MEMORY) ?
3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            OutOfResourcesException :
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            "java/lang/IllegalArgumentException";
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doThrow(env, exception, NULL);
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Associate a SkCanvas object to this surface
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jobject canvas = env->GetObjectField(clazz, so.canvas);
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(canvas, co.surfaceFormat, info.format);
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkCanvas* nativeCanvas = (SkCanvas*)env->GetIntField(canvas, no.native_canvas);
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkBitmap bitmap;
3121473f46cbc82aa6f0ba744cc896a36923823d55bMathias Agopian    ssize_t bpr = info.s * bytesPerPixel(info.format);
3131473f46cbc82aa6f0ba744cc896a36923823d55bMathias Agopian    bitmap.setConfig(convertPixelFormat(info.format), info.w, info.h, bpr);
314f1e5b0d4e5d0d2a78c234cd0cbd3005a74a79429Mathias Agopian    if (info.format == PIXEL_FORMAT_RGBX_8888) {
315f1e5b0d4e5d0d2a78c234cd0cbd3005a74a79429Mathias Agopian        bitmap.setIsOpaque(true);
316f1e5b0d4e5d0d2a78c234cd0cbd3005a74a79429Mathias Agopian    }
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (info.w > 0 && info.h > 0) {
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        bitmap.setPixels(info.bits);
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else {
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // be safe with an empty bitmap.
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        bitmap.setPixels(NULL);
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    nativeCanvas->setBitmapDevice(bitmap);
3246158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian
3256158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian    SkRegion clipReg;
3266158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian    if (dirtyRegion.isRect()) { // very common case
327a8a0aa8b922c45fb4633f51610f264a19f9bd825Mathias Agopian        const Rect b(dirtyRegion.getBounds());
3286158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian        clipReg.setRect(b.left, b.top, b.right, b.bottom);
3296158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian    } else {
3306158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian        size_t count;
3316158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian        Rect const* r = dirtyRegion.getArray(&count);
3326158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian        while (count) {
3336158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian            clipReg.op(r->left, r->top, r->right, r->bottom, SkRegion::kUnion_Op);
3346158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian            r++, count--;
3356158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian        }
3366158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian    }
3376158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian
3386158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian    nativeCanvas->clipRegion(clipReg);
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int saveCount = nativeCanvas->save();
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(clazz, so.saveCount, saveCount);
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (dirtyRect) {
3446158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian        const Rect& bounds(dirtyRegion.getBounds());
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->SetIntField(dirtyRect, ro.l, bounds.left);
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->SetIntField(dirtyRect, ro.t, bounds.top);
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->SetIntField(dirtyRect, ro.r, bounds.right);
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        env->SetIntField(dirtyRect, ro.b, bounds.bottom);
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	return canvas;
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_unlockCanvasAndPost(
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz, jobject argCanvas)
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jobject canvas = env->GetObjectField(clazz, so.canvas);
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (canvas != argCanvas) {
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doThrow(env, "java/lang/IllegalArgumentException", NULL);
3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
36317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<Surface>& surface(getSurface(env, clazz));
364402c34649f514669517c2208e35caa58ff8bb2b9Mathias Agopian    if (!Surface::isValid(surface))
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // detach the canvas from the surface
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkCanvas* nativeCanvas = (SkCanvas*)env->GetIntField(canvas, no.native_canvas);
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int saveCount = env->GetIntField(clazz, so.saveCount);
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    nativeCanvas->restoreToCount(saveCount);
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    nativeCanvas->setBitmapDevice(SkBitmap());
3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    env->SetIntField(clazz, so.saveCount, 0);
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // unlock surface
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    status_t err = surface->unlockAndPost();
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (err < 0) {
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doThrow(env, "java/lang/IllegalArgumentException", NULL);
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_unlockCanvas(
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz, jobject argCanvas)
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3841473f46cbc82aa6f0ba744cc896a36923823d55bMathias Agopian    // XXX: this API has been removed
3851473f46cbc82aa6f0ba744cc896a36923823d55bMathias Agopian    doThrow(env, "java/lang/IllegalArgumentException", NULL);
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_openTransaction(
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz)
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SurfaceComposerClient::openGlobalTransaction();
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_closeTransaction(
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz)
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SurfaceComposerClient::closeGlobalTransaction();
3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_setOrientation(
401eb0c86e18b7d620b679ff2a45a0233867a53a334Mathias Agopian        JNIEnv* env, jobject clazz, jint display, jint orientation, jint flags)
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
403eb0c86e18b7d620b679ff2a45a0233867a53a334Mathias Agopian    int err = SurfaceComposerClient::setOrientation(display, orientation, flags);
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (err < 0) {
4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doThrow(env, "java/lang/IllegalArgumentException", NULL);
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_freezeDisplay(
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz, jint display)
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int err = SurfaceComposerClient::freezeDisplay(display, 0);
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (err < 0) {
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doThrow(env, "java/lang/IllegalArgumentException", NULL);
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_unfreezeDisplay(
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz, jint display)
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int err = SurfaceComposerClient::unfreezeDisplay(display, 0);
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (err < 0) {
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doThrow(env, "java/lang/IllegalArgumentException", NULL);
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_setLayer(
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz, jint zorder)
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
43017f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
43117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (surface == 0) return;
43217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    status_t err = surface->setLayer(zorder);
43317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (err<0 && err!=NO_INIT)
43417f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        doThrow(env, "java/lang/IllegalArgumentException", NULL);
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_setPosition(
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz, jint x, jint y)
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
44017f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
44117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (surface == 0) return;
44217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    status_t err = surface->setPosition(x, y);
44317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (err<0 && err!=NO_INIT)
44417f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        doThrow(env, "java/lang/IllegalArgumentException", NULL);
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_setSize(
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz, jint w, jint h)
4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
45017f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
45117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (surface == 0) return;
45217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    status_t err = surface->setSize(w, h);
45317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (err<0 && err!=NO_INIT)
45417f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        doThrow(env, "java/lang/IllegalArgumentException", NULL);
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_hide(
4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz)
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
46017f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
46117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (surface == 0) return;
46217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    status_t err = surface->hide();
46317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (err<0 && err!=NO_INIT)
46417f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        doThrow(env, "java/lang/IllegalArgumentException", NULL);
4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_show(
4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz)
4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
47017f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
47117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (surface == 0) return;
47217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    status_t err = surface->show();
47317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (err<0 && err!=NO_INIT)
47417f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        doThrow(env, "java/lang/IllegalArgumentException", NULL);
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_freeze(
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz)
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
48017f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
48117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (surface == 0) return;
48217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    status_t err = surface->freeze();
48317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (err<0 && err!=NO_INIT)
48417f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        doThrow(env, "java/lang/IllegalArgumentException", NULL);
4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_unfreeze(
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz)
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
49017f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
49117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (surface == 0) return;
49217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    status_t err = surface->unfreeze();
49317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (err<0 && err!=NO_INIT)
49417f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        doThrow(env, "java/lang/IllegalArgumentException", NULL);
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_setFlags(
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz, jint flags, jint mask)
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
50017f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
50117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (surface == 0) return;
50217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    status_t err = surface->setFlags(flags, mask);
50317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (err<0 && err!=NO_INIT)
50417f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        doThrow(env, "java/lang/IllegalArgumentException", NULL);
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_setTransparentRegion(
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz, jobject argRegion)
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
51017f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
51117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (surface == 0) return;
51217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    SkRegion* nativeRegion = (SkRegion*)env->GetIntField(argRegion, no.native_region);
5136158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian
5146158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian    const SkIRect& b(nativeRegion->getBounds());
5156158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian    Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom));
5166158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian    if (nativeRegion->isComplex()) {
5176158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian        SkRegion::Iterator it(*nativeRegion);
5186158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian        while (!it.done()) {
5196158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian            const SkIRect& r(it.rect());
5206158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian            reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom);
5216158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian            it.next();
5226158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian        }
5236158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian    }
5246158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian
5256158b1bf0364da1582468a98ec09d004ba99deecMathias Agopian    status_t err = surface->setTransparentRegionHint(reg);
52617f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (err<0 && err!=NO_INIT)
52717f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        doThrow(env, "java/lang/IllegalArgumentException", NULL);
5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_setAlpha(
5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz, jfloat alpha)
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
53317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
53417f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (surface == 0) return;
53517f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    status_t err = surface->setAlpha(alpha);
53617f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (err<0 && err!=NO_INIT)
53717f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        doThrow(env, "java/lang/IllegalArgumentException", NULL);
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_setMatrix(
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz,
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy)
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
54417f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
54517f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (surface == 0) return;
54617f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    status_t err = surface->setMatrix(dsdx, dtdx, dsdy, dtdy);
54717f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (err<0 && err!=NO_INIT)
54817f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        doThrow(env, "java/lang/IllegalArgumentException", NULL);
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_setFreezeTint(
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz,
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jint tint)
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
55517f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz));
55617f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (surface == 0) return;
55717f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    status_t err = surface->setFreezeTint(tint);
55817f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (err<0 && err!=NO_INIT)
55917f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        doThrow(env, "java/lang/IllegalArgumentException", NULL);
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
56217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian// ----------------------------------------------------------------------------
56317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_copyFrom(
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz, jobject other)
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (clazz == other)
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (other == NULL) {
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doThrow(env, "java/lang/NullPointerException", NULL);
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
57517f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    /*
57617f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian     * This is used by the WindowManagerService just after constructing
57717f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian     * a Surface and is necessary for returning the Surface reference to
57817f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian     * the caller. At this point, we should only have a SurfaceControl.
57917f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian     */
58017f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian
58117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& surface = getSurfaceControl(env, clazz);
58217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& rhs = getSurfaceControl(env, other);
58317f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (!SurfaceControl::isSameSurface(surface, rhs)) {
5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // we reassign the surface only if it's a different one
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // otherwise we would loose our client-side state.
58617f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian        setSurfaceControl(env, clazz, rhs);
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_readFromParcel(
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz, jobject argParcel)
5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Parcel* parcel = (Parcel*)env->GetIntField( argParcel, no.native_parcel);
5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (parcel == NULL) {
5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doThrow(env, "java/lang/NullPointerException", NULL);
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
59917f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<Surface>& control(getSurface(env, clazz));
60017f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    sp<Surface> rhs = new Surface(*parcel);
60117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    if (!Surface::isSameSurface(control, rhs)) {
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // we reassign the surface only if it's a different one
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // otherwise we would loose our client-side state.
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        setSurface(env, clazz, rhs);
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void Surface_writeToParcel(
6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        JNIEnv* env, jobject clazz, jobject argParcel, jint flags)
6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Parcel* parcel = (Parcel*)env->GetIntField(
6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            argParcel, no.native_parcel);
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (parcel == NULL) {
6159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doThrow(env, "java/lang/NullPointerException", NULL);
6169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return;
6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
61917f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    const sp<SurfaceControl>& control(getSurfaceControl(env, clazz));
62017f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    SurfaceControl::writeSurfaceToParcel(control, parcel);
621af1e11b849ea527f4b1b1695924ce42cc9d7f670Mathias Agopian    if (flags & PARCELABLE_WRITE_RETURN_VALUE) {
622af1e11b849ea527f4b1b1695924ce42cc9d7f670Mathias Agopian        setSurfaceControl(env, clazz, 0);
623af1e11b849ea527f4b1b1695924ce42cc9d7f670Mathias Agopian    }
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
6259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
6279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
6289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ----------------------------------------------------------------------------
6299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst char* const kSurfaceSessionClassPathName = "android/view/SurfaceSession";
6319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst char* const kSurfaceClassPathName = "android/view/Surface";
6329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void nativeClassInit(JNIEnv* env, jclass clazz);
6339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNINativeMethod gSurfaceSessionMethods[] = {
6359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"init",     "()V",  (void*)SurfaceSession_init },
6369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"destroy",  "()V",  (void*)SurfaceSession_destroy },
6379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"kill",     "()V",  (void*)SurfaceSession_kill },
6389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
6399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic JNINativeMethod gSurfaceMethods[] = {
6419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"nativeClassInit",     "()V",  (void*)nativeClassInit },
6425d26c1e38dabb3ad8b4b6e1000375f3b1a6b7693Mathias Agopian    {"init",                "(Landroid/view/SurfaceSession;ILjava/lang/String;IIIII)V",  (void*)Surface_init },
6439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"init",                "(Landroid/os/Parcel;)V",  (void*)Surface_initParcel },
6440586a1b77a788a119166a37fccd909bf9ed65f23Dianne Hackborn    {"destroy",             "()V",  (void*)Surface_destroy },
645402c34649f514669517c2208e35caa58ff8bb2b9Mathias Agopian    {"release",             "()V",  (void*)Surface_release },
6469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"copyFrom",            "(Landroid/view/Surface;)V",  (void*)Surface_copyFrom },
6479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"isValid",             "()Z",  (void*)Surface_isValid },
6489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"lockCanvasNative",    "(Landroid/graphics/Rect;)Landroid/graphics/Canvas;",  (void*)Surface_lockCanvas },
6499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"unlockCanvasAndPost", "(Landroid/graphics/Canvas;)V", (void*)Surface_unlockCanvasAndPost },
6509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"unlockCanvas",        "(Landroid/graphics/Canvas;)V", (void*)Surface_unlockCanvas },
6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"openTransaction",     "()V",  (void*)Surface_openTransaction },
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"closeTransaction",    "()V",  (void*)Surface_closeTransaction },
653eb0c86e18b7d620b679ff2a45a0233867a53a334Mathias Agopian    {"setOrientation",      "(III)V", (void*)Surface_setOrientation },
6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"freezeDisplay",       "(I)V", (void*)Surface_freezeDisplay },
6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"unfreezeDisplay",     "(I)V", (void*)Surface_unfreezeDisplay },
6569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {"setLayer",            "(I)V", (void*)Surface_setLayer },
6579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"setPosition",         "(II)V",(void*)Surface_setPosition },
6589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"setSize",             "(II)V",(void*)Surface_setSize },
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"hide",                "()V",  (void*)Surface_hide },
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"show",                "()V",  (void*)Surface_show },
6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"freeze",              "()V",  (void*)Surface_freeze },
6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"unfreeze",            "()V",  (void*)Surface_unfreeze },
6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"setFlags",            "(II)V",(void*)Surface_setFlags },
6649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"setTransparentRegionHint","(Landroid/graphics/Region;)V", (void*)Surface_setTransparentRegion },
6659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"setAlpha",            "(F)V", (void*)Surface_setAlpha },
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"setMatrix",           "(FFFF)V",  (void*)Surface_setMatrix },
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"setFreezeTint",       "(I)V",  (void*)Surface_setFreezeTint },
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"readFromParcel",      "(Landroid/os/Parcel;)V", (void*)Surface_readFromParcel },
6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	{"writeToParcel",       "(Landroid/os/Parcel;I)V", (void*)Surface_writeToParcel },
6709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid nativeClassInit(JNIEnv* env, jclass clazz)
6739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
6748b138323d54e36fd66b22a634494648e20c53373Mathias Agopian    so.surface = env->GetFieldID(clazz, ANDROID_VIEW_SURFACE_JNI_ID, "I");
67517f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    so.surfaceControl = env->GetFieldID(clazz, "mSurfaceControl", "I");
6769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	so.saveCount = env->GetFieldID(clazz, "mSaveCount", "I");
6779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project	so.canvas    = env->GetFieldID(clazz, "mCanvas", "Landroid/graphics/Canvas;");
6789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass surfaceSession = env->FindClass("android/view/SurfaceSession");
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 	sso.client = env->GetFieldID(surfaceSession, "mClient", "I");
6819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass canvas = env->FindClass("android/graphics/Canvas");
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    no.native_canvas = env->GetFieldID(canvas, "mNativeCanvas", "I");
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    co.surfaceFormat = env->GetFieldID(canvas, "mSurfaceFormat", "I");
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass region = env->FindClass("android/graphics/Region");
6879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    no.native_region = env->GetFieldID(region, "mNativeRegion", "I");
6889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass parcel = env->FindClass("android/os/Parcel");
6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    no.native_parcel = env->GetFieldID(parcel, "mObject", "I");
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass rect = env->FindClass("android/graphics/Rect");
6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ro.l = env->GetFieldID(rect, "left", "I");
6949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ro.t = env->GetFieldID(rect, "top", "I");
6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ro.r = env->GetFieldID(rect, "right", "I");
6969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ro.b = env->GetFieldID(rect, "bottom", "I");
6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    jclass point = env->FindClass("android/graphics/Point");
6999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    po.x = env->GetFieldID(point, "x", "I");
7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    po.y = env->GetFieldID(point, "y", "I");
7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint register_android_view_Surface(JNIEnv* env)
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int err;
7069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    err = AndroidRuntime::registerNativeMethods(env, kSurfaceSessionClassPathName,
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            gSurfaceSessionMethods, NELEM(gSurfaceSessionMethods));
7089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    err |= AndroidRuntime::registerNativeMethods(env, kSurfaceClassPathName,
7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            gSurfaceMethods, NELEM(gSurfaceMethods));
7119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return err;
7129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
7139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
7159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
716