android_view_Surface.cpp revision 86e1bc730570765355dc8789b5c6de6962a053cc
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 mCanvas;
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    return reinterpret_cast<Surface *>(
94            env->GetIntField(surfaceObj, gSurfaceClassInfo.mNativeObject));
95}
96
97jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env,
98        const sp<IGraphicBufferProducer>& bufferProducer) {
99    if (bufferProducer == NULL) {
100        return NULL;
101    }
102
103    sp<Surface> surface(new Surface(bufferProducer));
104    if (surface == NULL) {
105        return NULL;
106    }
107
108    jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz, gSurfaceClassInfo.ctor, surface.get());
109    if (surfaceObj == NULL) {
110        if (env->ExceptionCheck()) {
111            ALOGE("Could not create instance of Surface from IGraphicBufferProducer.");
112            LOGE_EX(env);
113            env->ExceptionClear();
114        }
115        return NULL;
116    }
117    surface->incStrong(&sRefBaseOwner);
118    return surfaceObj;
119}
120
121// ----------------------------------------------------------------------------
122
123static inline bool isSurfaceValid(const sp<Surface>& sur) {
124    return Surface::isValid(sur);
125}
126
127// ----------------------------------------------------------------------------
128
129static jint nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz,
130        jobject surfaceTextureObj) {
131    sp<GLConsumer> st(SurfaceTexture_getSurfaceTexture(env, surfaceTextureObj));
132    if (st == NULL) {
133        jniThrowException(env, "java/lang/IllegalArgumentException",
134                "SurfaceTexture has already been released");
135        return 0;
136    }
137
138    sp<IGraphicBufferProducer> bq = st->getBufferQueue();
139    sp<Surface> surface(new Surface(bq));
140    if (surface == NULL) {
141        jniThrowException(env, OutOfResourcesException, NULL);
142        return 0;
143    }
144
145    surface->incStrong(&sRefBaseOwner);
146    return int(surface.get());
147}
148
149static void nativeRelease(JNIEnv* env, jclass clazz, jint nativeObject) {
150    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
151    sur->decStrong(&sRefBaseOwner);
152}
153
154static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jint nativeObject) {
155    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
156    return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE;
157}
158
159static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jint nativeObject) {
160    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
161    if (!isSurfaceValid(sur)) {
162        doThrowIAE(env);
163        return JNI_FALSE;
164    }
165    int value = 0;
166    ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get());
167    anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value);
168    return value;
169}
170
171static inline SkBitmap::Config convertPixelFormat(PixelFormat format) {
172    /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then
173        we can map to SkBitmap::kARGB_8888_Config, and optionally call
174        bitmap.setIsOpaque(true) on the resulting SkBitmap (as an accelerator)
175    */
176    switch (format) {
177    case PIXEL_FORMAT_RGBX_8888:    return SkBitmap::kARGB_8888_Config;
178    case PIXEL_FORMAT_RGBA_8888:    return SkBitmap::kARGB_8888_Config;
179    case PIXEL_FORMAT_RGBA_4444:    return SkBitmap::kARGB_4444_Config;
180    case PIXEL_FORMAT_RGB_565:      return SkBitmap::kRGB_565_Config;
181    case PIXEL_FORMAT_A_8:          return SkBitmap::kA8_Config;
182    default:                        return SkBitmap::kNo_Config;
183    }
184}
185
186static inline void swapCanvasPtr(JNIEnv* env, jobject canvasObj, SkCanvas* newCanvas) {
187  jobject canvasFinalizerObj = env->GetObjectField(canvasObj, gCanvasClassInfo.mFinalizer);
188  SkCanvas* previousCanvas = reinterpret_cast<SkCanvas*>(
189          env->GetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas));
190  env->SetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas, (int)newCanvas);
191  env->SetIntField(canvasFinalizerObj, gCanvasFinalizerClassInfo.mNativeCanvas, (int)newCanvas);
192  SkSafeUnref(previousCanvas);
193}
194
195static jobject nativeLockCanvas(JNIEnv* env, jobject surfaceObj, jint nativeObject, jobject dirtyRectObj) {
196    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
197
198    if (!isSurfaceValid(surface)) {
199        doThrowIAE(env);
200        return NULL;
201    }
202
203    // get dirty region
204    Region dirtyRegion;
205    if (dirtyRectObj) {
206        Rect dirty;
207        dirty.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left);
208        dirty.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top);
209        dirty.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right);
210        dirty.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom);
211        if (!dirty.isEmpty()) {
212            dirtyRegion.set(dirty);
213        }
214    } else {
215        dirtyRegion.set(Rect(0x3FFF, 0x3FFF));
216    }
217
218    ANativeWindow_Buffer outBuffer;
219    Rect dirtyBounds(dirtyRegion.getBounds());
220    status_t err = surface->lock(&outBuffer, &dirtyBounds);
221    dirtyRegion.set(dirtyBounds);
222    if (err < 0) {
223        const char* const exception = (err == NO_MEMORY) ?
224                OutOfResourcesException :
225                "java/lang/IllegalArgumentException";
226        jniThrowException(env, exception, NULL);
227        return NULL;
228    }
229
230    // Associate a SkCanvas object to this surface
231    jobject canvasObj = env->GetObjectField(surfaceObj, gSurfaceClassInfo.mCanvas);
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    SkRegion clipReg;
251    if (dirtyRegion.isRect()) { // very common case
252        const Rect b(dirtyRegion.getBounds());
253        clipReg.setRect(b.left, b.top, b.right, b.bottom);
254    } else {
255        size_t count;
256        Rect const* r = dirtyRegion.getArray(&count);
257        while (count) {
258            clipReg.op(r->left, r->top, r->right, r->bottom, SkRegion::kUnion_Op);
259            r++, count--;
260        }
261    }
262
263    nativeCanvas->clipRegion(clipReg);
264
265    if (dirtyRectObj) {
266        const Rect& bounds(dirtyRegion.getBounds());
267        env->SetIntField(dirtyRectObj, gRectClassInfo.left, bounds.left);
268        env->SetIntField(dirtyRectObj, gRectClassInfo.top, bounds.top);
269        env->SetIntField(dirtyRectObj, gRectClassInfo.right, bounds.right);
270        env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, bounds.bottom);
271    }
272
273    return canvasObj;
274}
275
276static void nativeUnlockCanvasAndPost(JNIEnv* env, jobject surfaceObj, jint nativeObject, jobject canvasObj) {
277    jobject ownCanvasObj = env->GetObjectField(surfaceObj, gSurfaceClassInfo.mCanvas);
278    if (!env->IsSameObject(ownCanvasObj, canvasObj)) {
279        doThrowIAE(env);
280        return;
281    }
282
283    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
284    if (!isSurfaceValid(surface)) {
285        return;
286    }
287
288    // detach the canvas from the surface
289    SkCanvas* nativeCanvas = SkNEW(SkCanvas);
290    swapCanvasPtr(env, canvasObj, nativeCanvas);
291
292    // unlock surface
293    status_t err = surface->unlockAndPost();
294    if (err < 0) {
295        doThrowIAE(env);
296    }
297}
298
299// ----------------------------------------------------------------------------
300
301static jint nativeCopyFrom(JNIEnv* env, jclass clazz,
302        jint nativeObject, jint surfaceControlNativeObj) {
303    /*
304     * This is used by the WindowManagerService just after constructing
305     * a Surface and is necessary for returning the Surface reference to
306     * the caller. At this point, we should only have a SurfaceControl.
307     */
308
309    sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
310    sp<Surface> other(ctrl->getSurface());
311    if (other != NULL) {
312        other->incStrong(&sRefBaseOwner);
313    }
314
315    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
316    if (sur != NULL) {
317        sur->decStrong(&sRefBaseOwner);
318    }
319
320    return int(other.get());
321}
322
323static jint nativeReadFromParcel(JNIEnv* env, jclass clazz,
324        jint nativeObject, jobject parcelObj) {
325    Parcel* parcel = parcelForJavaObject(env, parcelObj);
326    if (parcel == NULL) {
327        doThrowNPE(env);
328        return 0;
329    }
330
331    sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
332    sp<IBinder> binder(parcel->readStrongBinder());
333
334    // update the Surface only if the underlying IGraphicBufferProducer
335    // has changed.
336    if (self != NULL
337            && (self->getIGraphicBufferProducer()->asBinder() == binder)) {
338        // same IGraphicBufferProducer, return ourselves
339        return int(self.get());
340    }
341
342    sp<Surface> sur;
343    sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder));
344    if (gbp != NULL) {
345        // we have a new IGraphicBufferProducer, create a new Surface for it
346        sur = new Surface(gbp);
347        // and keep a reference before passing to java
348        sur->incStrong(&sRefBaseOwner);
349    }
350
351    if (self != NULL) {
352        // and loose the java reference to ourselves
353        self->decStrong(&sRefBaseOwner);
354    }
355
356    return int(sur.get());
357}
358
359static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
360        jint nativeObject, jobject parcelObj) {
361    Parcel* parcel = parcelForJavaObject(env, parcelObj);
362    if (parcel == NULL) {
363        doThrowNPE(env);
364        return;
365    }
366    sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
367    parcel->writeStrongBinder( self != 0 ? self->getIGraphicBufferProducer()->asBinder() : NULL);
368}
369
370// ----------------------------------------------------------------------------
371
372static JNINativeMethod gSurfaceMethods[] = {
373    {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)I",
374            (void*)nativeCreateFromSurfaceTexture },
375    {"nativeRelease", "(I)V",
376            (void*)nativeRelease },
377    {"nativeIsValid", "(I)Z",
378            (void*)nativeIsValid },
379    {"nativeIsConsumerRunningBehind", "(I)Z",
380            (void*)nativeIsConsumerRunningBehind },
381    {"nativeLockCanvas", "(ILandroid/graphics/Rect;)Landroid/graphics/Canvas;",
382            (void*)nativeLockCanvas },
383    {"nativeUnlockCanvasAndPost", "(ILandroid/graphics/Canvas;)V",
384            (void*)nativeUnlockCanvasAndPost },
385    {"nativeCopyFrom", "(II)I",
386            (void*)nativeCopyFrom },
387    {"nativeReadFromParcel", "(ILandroid/os/Parcel;)I",
388            (void*)nativeReadFromParcel },
389    {"nativeWriteToParcel", "(ILandroid/os/Parcel;)V",
390            (void*)nativeWriteToParcel },
391};
392
393int register_android_view_Surface(JNIEnv* env)
394{
395    int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface",
396            gSurfaceMethods, NELEM(gSurfaceMethods));
397
398    jclass clazz = env->FindClass("android/view/Surface");
399    gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz));
400    gSurfaceClassInfo.mNativeObject =
401            env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "I");
402    gSurfaceClassInfo.mCanvas =
403            env->GetFieldID(gSurfaceClassInfo.clazz, "mCanvas", "Landroid/graphics/Canvas;");
404    gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(I)V");
405
406    clazz = env->FindClass("android/graphics/Canvas");
407    gCanvasClassInfo.mFinalizer = env->GetFieldID(clazz, "mFinalizer", "Landroid/graphics/Canvas$CanvasFinalizer;");
408    gCanvasClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I");
409    gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I");
410
411    clazz = env->FindClass("android/graphics/Canvas$CanvasFinalizer");
412    gCanvasFinalizerClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I");
413
414    clazz = env->FindClass("android/graphics/Rect");
415    gRectClassInfo.left = env->GetFieldID(clazz, "left", "I");
416    gRectClassInfo.top = env->GetFieldID(clazz, "top", "I");
417    gRectClassInfo.right = env->GetFieldID(clazz, "right", "I");
418    gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I");
419
420    return err;
421}
422
423};
424