android_view_Surface.cpp revision b933055cf3f7f8ea89bfd3bc9c37a3891ff7310a
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 "jni.h"
22#include "JNIHelp.h"
23#include "android_os_Parcel.h"
24#include "android/graphics/GraphicsJNI.h"
25
26#include <android_runtime/AndroidRuntime.h>
27#include <android_runtime/android_view_Surface.h>
28#include <android_runtime/android_graphics_SurfaceTexture.h>
29#include <android_runtime/Log.h>
30
31#include <binder/Parcel.h>
32
33#include <gui/Surface.h>
34#include <gui/SurfaceControl.h>
35#include <gui/GLConsumer.h>
36
37#include <ui/Rect.h>
38#include <ui/Region.h>
39
40#include <SkCanvas.h>
41#include <SkBitmap.h>
42#include <SkImage.h>
43#include <SkRegion.h>
44
45#include <utils/misc.h>
46#include <utils/Log.h>
47
48#include <ScopedUtfChars.h>
49
50// ----------------------------------------------------------------------------
51
52namespace android {
53
54static const char* const OutOfResourcesException =
55    "android/view/Surface$OutOfResourcesException";
56
57static struct {
58    jclass clazz;
59    jfieldID mNativeObject;
60    jfieldID mLock;
61    jmethodID ctor;
62} gSurfaceClassInfo;
63
64static struct {
65    jfieldID left;
66    jfieldID top;
67    jfieldID right;
68    jfieldID bottom;
69} gRectClassInfo;
70
71static struct {
72    jfieldID mSurfaceFormat;
73    jmethodID setNativeBitmap;
74} gCanvasClassInfo;
75
76// ----------------------------------------------------------------------------
77
78// this is just a pointer we use to pass to inc/decStrong
79static const void *sRefBaseOwner;
80
81bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) {
82    return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz);
83}
84
85sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) {
86    return android_view_Surface_getSurface(env, surfaceObj);
87}
88
89sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) {
90    sp<Surface> sur;
91    jobject lock = env->GetObjectField(surfaceObj,
92            gSurfaceClassInfo.mLock);
93    if (env->MonitorEnter(lock) == JNI_OK) {
94        sur = reinterpret_cast<Surface *>(
95                env->GetLongField(surfaceObj, gSurfaceClassInfo.mNativeObject));
96        env->MonitorExit(lock);
97    }
98    env->DeleteLocalRef(lock);
99    return sur;
100}
101
102jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env,
103        const sp<IGraphicBufferProducer>& bufferProducer) {
104    if (bufferProducer == NULL) {
105        return NULL;
106    }
107
108    sp<Surface> surface(new Surface(bufferProducer, true));
109    if (surface == NULL) {
110        return NULL;
111    }
112
113    jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz,
114            gSurfaceClassInfo.ctor, (jlong)surface.get());
115    if (surfaceObj == NULL) {
116        if (env->ExceptionCheck()) {
117            ALOGE("Could not create instance of Surface from IGraphicBufferProducer.");
118            LOGE_EX(env);
119            env->ExceptionClear();
120        }
121        return NULL;
122    }
123    surface->incStrong(&sRefBaseOwner);
124    return surfaceObj;
125}
126
127// ----------------------------------------------------------------------------
128
129static inline bool isSurfaceValid(const sp<Surface>& sur) {
130    return Surface::isValid(sur);
131}
132
133// ----------------------------------------------------------------------------
134
135static jlong nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz,
136        jobject surfaceTextureObj) {
137    sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surfaceTextureObj));
138    if (producer == NULL) {
139        jniThrowException(env, "java/lang/IllegalArgumentException",
140                "SurfaceTexture has already been released");
141        return 0;
142    }
143
144    sp<Surface> surface(new Surface(producer, true));
145    if (surface == NULL) {
146        jniThrowException(env, OutOfResourcesException, NULL);
147        return 0;
148    }
149
150    surface->incStrong(&sRefBaseOwner);
151    return jlong(surface.get());
152}
153
154static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
155    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
156    sur->decStrong(&sRefBaseOwner);
157}
158
159static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jlong nativeObject) {
160    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
161    return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE;
162}
163
164static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jlong nativeObject) {
165    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
166    if (!isSurfaceValid(sur)) {
167        doThrowIAE(env);
168        return JNI_FALSE;
169    }
170    int value = 0;
171    ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get());
172    anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value);
173    return value;
174}
175
176static inline SkColorType convertPixelFormat(PixelFormat format) {
177    /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then
178        we can map to kN32_SkColorType, and optionally call
179        bitmap.setAlphaType(kOpaque_SkAlphaType) on the resulting SkBitmap
180        (as an accelerator)
181    */
182    switch (format) {
183    case PIXEL_FORMAT_RGBX_8888:    return kN32_SkColorType;
184    case PIXEL_FORMAT_RGBA_8888:    return kN32_SkColorType;
185    case PIXEL_FORMAT_RGB_565:      return kRGB_565_SkColorType;
186    default:                        return kUnknown_SkColorType;
187    }
188}
189
190static jlong nativeLockCanvas(JNIEnv* env, jclass clazz,
191        jlong nativeObject, jobject canvasObj, jobject dirtyRectObj) {
192    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
193
194    if (!isSurfaceValid(surface)) {
195        doThrowIAE(env);
196        return 0;
197    }
198
199    Rect dirtyRect;
200    Rect* dirtyRectPtr = NULL;
201
202    if (dirtyRectObj) {
203        dirtyRect.left   = env->GetIntField(dirtyRectObj, gRectClassInfo.left);
204        dirtyRect.top    = env->GetIntField(dirtyRectObj, gRectClassInfo.top);
205        dirtyRect.right  = env->GetIntField(dirtyRectObj, gRectClassInfo.right);
206        dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom);
207        dirtyRectPtr = &dirtyRect;
208    }
209
210    ANativeWindow_Buffer outBuffer;
211    status_t err = surface->lock(&outBuffer, dirtyRectPtr);
212    if (err < 0) {
213        const char* const exception = (err == NO_MEMORY) ?
214                OutOfResourcesException :
215                "java/lang/IllegalArgumentException";
216        jniThrowException(env, exception, NULL);
217        return 0;
218    }
219
220    // Associate a SkCanvas object to this surface
221    env->SetIntField(canvasObj, gCanvasClassInfo.mSurfaceFormat, outBuffer.format);
222
223    SkImageInfo info = SkImageInfo::Make(outBuffer.width, outBuffer.height,
224                                         convertPixelFormat(outBuffer.format),
225                                         kPremul_SkAlphaType);
226    if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) {
227        info.fAlphaType = kOpaque_SkAlphaType;
228    }
229
230    SkBitmap bitmap;
231    ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
232    bitmap.setInfo(info, bpr);
233    if (outBuffer.width > 0 && outBuffer.height > 0) {
234        bitmap.setPixels(outBuffer.bits);
235    } else {
236        // be safe with an empty bitmap.
237        bitmap.setPixels(NULL);
238    }
239
240    env->CallVoidMethod(canvasObj, gCanvasClassInfo.setNativeBitmap,
241                        reinterpret_cast<jlong>(&bitmap));
242
243    if (dirtyRectPtr) {
244        SkCanvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj);
245        nativeCanvas->clipRect( SkRect::Make(reinterpret_cast<const SkIRect&>(dirtyRect)) );
246    }
247
248    if (dirtyRectObj) {
249        env->SetIntField(dirtyRectObj, gRectClassInfo.left,   dirtyRect.left);
250        env->SetIntField(dirtyRectObj, gRectClassInfo.top,    dirtyRect.top);
251        env->SetIntField(dirtyRectObj, gRectClassInfo.right,  dirtyRect.right);
252        env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom);
253    }
254
255    // Create another reference to the surface and return it.  This reference
256    // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject,
257    // because the latter could be replaced while the surface is locked.
258    sp<Surface> lockedSurface(surface);
259    lockedSurface->incStrong(&sRefBaseOwner);
260    return (jlong) lockedSurface.get();
261}
262
263static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz,
264        jlong nativeObject, jobject canvasObj) {
265    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
266    if (!isSurfaceValid(surface)) {
267        return;
268    }
269
270    // detach the canvas from the surface
271    env->CallVoidMethod(canvasObj, gCanvasClassInfo.setNativeBitmap, (jlong)0);
272
273    // unlock surface
274    status_t err = surface->unlockAndPost();
275    if (err < 0) {
276        doThrowIAE(env);
277    }
278}
279
280// ----------------------------------------------------------------------------
281
282static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz,
283        jlong surfaceControlNativeObj) {
284    /*
285     * This is used by the WindowManagerService just after constructing
286     * a Surface and is necessary for returning the Surface reference to
287     * the caller. At this point, we should only have a SurfaceControl.
288     */
289
290    sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
291    sp<Surface> surface(ctrl->getSurface());
292    if (surface != NULL) {
293        surface->incStrong(&sRefBaseOwner);
294    }
295    return reinterpret_cast<jlong>(surface.get());
296}
297
298static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz,
299        jlong nativeObject, jobject parcelObj) {
300    Parcel* parcel = parcelForJavaObject(env, parcelObj);
301    if (parcel == NULL) {
302        doThrowNPE(env);
303        return 0;
304    }
305
306    sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
307    sp<IBinder> binder(parcel->readStrongBinder());
308
309    // update the Surface only if the underlying IGraphicBufferProducer
310    // has changed.
311    if (self != NULL
312            && (self->getIGraphicBufferProducer()->asBinder() == binder)) {
313        // same IGraphicBufferProducer, return ourselves
314        return jlong(self.get());
315    }
316
317    sp<Surface> sur;
318    sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder));
319    if (gbp != NULL) {
320        // we have a new IGraphicBufferProducer, create a new Surface for it
321        sur = new Surface(gbp, true);
322        // and keep a reference before passing to java
323        sur->incStrong(&sRefBaseOwner);
324    }
325
326    if (self != NULL) {
327        // and loose the java reference to ourselves
328        self->decStrong(&sRefBaseOwner);
329    }
330
331    return jlong(sur.get());
332}
333
334static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
335        jlong nativeObject, jobject parcelObj) {
336    Parcel* parcel = parcelForJavaObject(env, parcelObj);
337    if (parcel == NULL) {
338        doThrowNPE(env);
339        return;
340    }
341    sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
342    parcel->writeStrongBinder( self != 0 ? self->getIGraphicBufferProducer()->asBinder() : NULL);
343}
344
345// ----------------------------------------------------------------------------
346
347static JNINativeMethod gSurfaceMethods[] = {
348    {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)J",
349            (void*)nativeCreateFromSurfaceTexture },
350    {"nativeRelease", "(J)V",
351            (void*)nativeRelease },
352    {"nativeIsValid", "(J)Z",
353            (void*)nativeIsValid },
354    {"nativeIsConsumerRunningBehind", "(J)Z",
355            (void*)nativeIsConsumerRunningBehind },
356    {"nativeLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)J",
357            (void*)nativeLockCanvas },
358    {"nativeUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
359            (void*)nativeUnlockCanvasAndPost },
360    {"nativeCreateFromSurfaceControl", "(J)J",
361            (void*)nativeCreateFromSurfaceControl },
362    {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J",
363            (void*)nativeReadFromParcel },
364    {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
365            (void*)nativeWriteToParcel },
366};
367
368int register_android_view_Surface(JNIEnv* env)
369{
370    int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface",
371            gSurfaceMethods, NELEM(gSurfaceMethods));
372
373    jclass clazz = env->FindClass("android/view/Surface");
374    gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz));
375    gSurfaceClassInfo.mNativeObject =
376            env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "J");
377    gSurfaceClassInfo.mLock =
378            env->GetFieldID(gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;");
379    gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(J)V");
380
381    clazz = env->FindClass("android/graphics/Canvas");
382    gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I");
383    gCanvasClassInfo.setNativeBitmap = env->GetMethodID(clazz, "setNativeBitmap", "(J)V");
384
385    clazz = env->FindClass("android/graphics/Rect");
386    gRectClassInfo.left = env->GetFieldID(clazz, "left", "I");
387    gRectClassInfo.top = env->GetFieldID(clazz, "top", "I");
388    gRectClassInfo.right = env->GetFieldID(clazz, "right", "I");
389    gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I");
390
391    return err;
392}
393
394};
395