android_view_Surface.cpp revision 6f7b58917104916ee6afd6f246c251c1d7a2102a
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
30#include <binder/Parcel.h>
31
32#include <gui/Surface.h>
33#include <gui/SurfaceControl.h>
34#include <gui/GLConsumer.h>
35
36#include <ui/Rect.h>
37#include <ui/Region.h>
38
39#include <SkCanvas.h>
40#include <SkBitmap.h>
41#include <SkRegion.h>
42
43#include <utils/misc.h>
44#include <utils/Log.h>
45
46#include <ScopedUtfChars.h>
47
48// ----------------------------------------------------------------------------
49
50namespace android {
51
52static const char* const OutOfResourcesException =
53    "android/view/Surface$OutOfResourcesException";
54
55static struct {
56    jclass clazz;
57    jfieldID mNativeObject;
58    jfieldID mLock;
59    jmethodID ctor;
60} gSurfaceClassInfo;
61
62static struct {
63    jfieldID left;
64    jfieldID top;
65    jfieldID right;
66    jfieldID bottom;
67} gRectClassInfo;
68
69static struct {
70    jfieldID mFinalizer;
71    jfieldID mNativeCanvas;
72    jfieldID mSurfaceFormat;
73} gCanvasClassInfo;
74
75static struct {
76    jfieldID mNativeCanvas;
77} gCanvasFinalizerClassInfo;
78
79// ----------------------------------------------------------------------------
80
81// this is just a pointer we use to pass to inc/decStrong
82static const void *sRefBaseOwner;
83
84bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) {
85    return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz);
86}
87
88sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) {
89    return android_view_Surface_getSurface(env, surfaceObj);
90}
91
92sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) {
93    sp<Surface> sur;
94    jobject lock = env->GetObjectField(surfaceObj,
95            gSurfaceClassInfo.mLock);
96    if (env->MonitorEnter(lock) == JNI_OK) {
97        sur = reinterpret_cast<Surface *>(
98                env->GetIntField(surfaceObj, gSurfaceClassInfo.mNativeObject));
99        env->MonitorExit(lock);
100    }
101    return sur;
102}
103
104jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env,
105        const sp<IGraphicBufferProducer>& bufferProducer) {
106    if (bufferProducer == NULL) {
107        return NULL;
108    }
109
110    sp<Surface> surface(new Surface(bufferProducer, true));
111    if (surface == NULL) {
112        return NULL;
113    }
114
115    jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz, gSurfaceClassInfo.ctor, surface.get());
116    if (surfaceObj == NULL) {
117        if (env->ExceptionCheck()) {
118            ALOGE("Could not create instance of Surface from IGraphicBufferProducer.");
119            LOGE_EX(env);
120            env->ExceptionClear();
121        }
122        return NULL;
123    }
124    surface->incStrong(&sRefBaseOwner);
125    return surfaceObj;
126}
127
128// ----------------------------------------------------------------------------
129
130static inline bool isSurfaceValid(const sp<Surface>& sur) {
131    return Surface::isValid(sur);
132}
133
134// ----------------------------------------------------------------------------
135
136static jint nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz,
137        jobject surfaceTextureObj) {
138    sp<GLConsumer> st(SurfaceTexture_getSurfaceTexture(env, surfaceTextureObj));
139    if (st == NULL) {
140        jniThrowException(env, "java/lang/IllegalArgumentException",
141                "SurfaceTexture has already been released");
142        return 0;
143    }
144
145    sp<IGraphicBufferProducer> bq = st->getBufferQueue();
146    sp<Surface> surface(new Surface(bq, true));
147    if (surface == NULL) {
148        jniThrowException(env, OutOfResourcesException, NULL);
149        return 0;
150    }
151
152    surface->incStrong(&sRefBaseOwner);
153    return int(surface.get());
154}
155
156static void nativeRelease(JNIEnv* env, jclass clazz, jint nativeObject) {
157    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
158    sur->decStrong(&sRefBaseOwner);
159}
160
161static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jint nativeObject) {
162    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
163    return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE;
164}
165
166static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jint nativeObject) {
167    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
168    if (!isSurfaceValid(sur)) {
169        doThrowIAE(env);
170        return JNI_FALSE;
171    }
172    int value = 0;
173    ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get());
174    anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value);
175    return value;
176}
177
178static inline SkBitmap::Config convertPixelFormat(PixelFormat format) {
179    /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then
180        we can map to SkBitmap::kARGB_8888_Config, and optionally call
181        bitmap.setIsOpaque(true) on the resulting SkBitmap (as an accelerator)
182    */
183    switch (format) {
184    case PIXEL_FORMAT_RGBX_8888:    return SkBitmap::kARGB_8888_Config;
185    case PIXEL_FORMAT_RGBA_8888:    return SkBitmap::kARGB_8888_Config;
186    case PIXEL_FORMAT_RGBA_4444:    return SkBitmap::kARGB_4444_Config;
187    case PIXEL_FORMAT_RGB_565:      return SkBitmap::kRGB_565_Config;
188    default:                        return SkBitmap::kNo_Config;
189    }
190}
191
192static inline void swapCanvasPtr(JNIEnv* env, jobject canvasObj, SkCanvas* newCanvas) {
193  jobject canvasFinalizerObj = env->GetObjectField(canvasObj, gCanvasClassInfo.mFinalizer);
194  SkCanvas* previousCanvas = reinterpret_cast<SkCanvas*>(
195          env->GetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas));
196  env->SetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas, (int)newCanvas);
197  env->SetIntField(canvasFinalizerObj, gCanvasFinalizerClassInfo.mNativeCanvas, (int)newCanvas);
198  SkSafeUnref(previousCanvas);
199}
200
201static void nativeLockCanvas(JNIEnv* env, jclass clazz,
202        jint nativeObject, jobject canvasObj, jobject dirtyRectObj) {
203    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
204
205    if (!isSurfaceValid(surface)) {
206        doThrowIAE(env);
207        return;
208    }
209
210    Rect dirtyRect;
211    Rect* dirtyRectPtr = NULL;
212
213    if (dirtyRectObj) {
214        dirtyRect.left   = env->GetIntField(dirtyRectObj, gRectClassInfo.left);
215        dirtyRect.top    = env->GetIntField(dirtyRectObj, gRectClassInfo.top);
216        dirtyRect.right  = env->GetIntField(dirtyRectObj, gRectClassInfo.right);
217        dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom);
218        dirtyRectPtr = &dirtyRect;
219    }
220
221    ANativeWindow_Buffer outBuffer;
222    status_t err = surface->lock(&outBuffer, dirtyRectPtr);
223    if (err < 0) {
224        const char* const exception = (err == NO_MEMORY) ?
225                OutOfResourcesException :
226                "java/lang/IllegalArgumentException";
227        jniThrowException(env, exception, NULL);
228        return;
229    }
230
231    // Associate a SkCanvas object to this surface
232    env->SetIntField(canvasObj, gCanvasClassInfo.mSurfaceFormat, outBuffer.format);
233
234    SkBitmap bitmap;
235    ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
236    bitmap.setConfig(convertPixelFormat(outBuffer.format), outBuffer.width, outBuffer.height, bpr);
237    if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) {
238        bitmap.setIsOpaque(true);
239    }
240    if (outBuffer.width > 0 && outBuffer.height > 0) {
241        bitmap.setPixels(outBuffer.bits);
242    } else {
243        // be safe with an empty bitmap.
244        bitmap.setPixels(NULL);
245    }
246
247    SkCanvas* nativeCanvas = SkNEW_ARGS(SkCanvas, (bitmap));
248    swapCanvasPtr(env, canvasObj, nativeCanvas);
249
250    if (dirtyRectPtr) {
251        nativeCanvas->clipRect( SkRect::Make(reinterpret_cast<const SkIRect&>(dirtyRect)) );
252    }
253
254    if (dirtyRectObj) {
255        env->SetIntField(dirtyRectObj, gRectClassInfo.left,   dirtyRect.left);
256        env->SetIntField(dirtyRectObj, gRectClassInfo.top,    dirtyRect.top);
257        env->SetIntField(dirtyRectObj, gRectClassInfo.right,  dirtyRect.right);
258        env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom);
259    }
260}
261
262static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz,
263        jint nativeObject, jobject canvasObj) {
264    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
265    if (!isSurfaceValid(surface)) {
266        return;
267    }
268
269    // detach the canvas from the surface
270    SkCanvas* nativeCanvas = SkNEW(SkCanvas);
271    swapCanvasPtr(env, canvasObj, nativeCanvas);
272
273    // unlock surface
274    status_t err = surface->unlockAndPost();
275    if (err < 0) {
276        doThrowIAE(env);
277    }
278}
279
280// ----------------------------------------------------------------------------
281
282static jint nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz,
283        jint 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<jint>(surface.get());
296}
297
298static jint nativeReadFromParcel(JNIEnv* env, jclass clazz,
299        jint 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 int(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 int(sur.get());
332}
333
334static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
335        jint 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;)I",
349            (void*)nativeCreateFromSurfaceTexture },
350    {"nativeRelease", "(I)V",
351            (void*)nativeRelease },
352    {"nativeIsValid", "(I)Z",
353            (void*)nativeIsValid },
354    {"nativeIsConsumerRunningBehind", "(I)Z",
355            (void*)nativeIsConsumerRunningBehind },
356    {"nativeLockCanvas", "(ILandroid/graphics/Canvas;Landroid/graphics/Rect;)V",
357            (void*)nativeLockCanvas },
358    {"nativeUnlockCanvasAndPost", "(ILandroid/graphics/Canvas;)V",
359            (void*)nativeUnlockCanvasAndPost },
360    {"nativeCreateFromSurfaceControl", "(I)I",
361            (void*)nativeCreateFromSurfaceControl },
362    {"nativeReadFromParcel", "(ILandroid/os/Parcel;)I",
363            (void*)nativeReadFromParcel },
364    {"nativeWriteToParcel", "(ILandroid/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", "I");
377    gSurfaceClassInfo.mLock =
378            env->GetFieldID(gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;");
379    gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(I)V");
380
381    clazz = env->FindClass("android/graphics/Canvas");
382    gCanvasClassInfo.mFinalizer = env->GetFieldID(clazz, "mFinalizer", "Landroid/graphics/Canvas$CanvasFinalizer;");
383    gCanvasClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I");
384    gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I");
385
386    clazz = env->FindClass("android/graphics/Canvas$CanvasFinalizer");
387    gCanvasFinalizerClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I");
388
389    clazz = env->FindClass("android/graphics/Rect");
390    gRectClassInfo.left = env->GetFieldID(clazz, "left", "I");
391    gRectClassInfo.top = env->GetFieldID(clazz, "top", "I");
392    gRectClassInfo.right = env->GetFieldID(clazz, "right", "I");
393    gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I");
394
395    return err;
396}
397
398};
399