android_view_Surface.cpp revision fc0ebd7d379ff63c00ebf78ca252fab5070213da
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));
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));
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    case PIXEL_FORMAT_A_8:          return SkBitmap::kA8_Config;
189    default:                        return SkBitmap::kNo_Config;
190    }
191}
192
193static inline void swapCanvasPtr(JNIEnv* env, jobject canvasObj, SkCanvas* newCanvas) {
194  jobject canvasFinalizerObj = env->GetObjectField(canvasObj, gCanvasClassInfo.mFinalizer);
195  SkCanvas* previousCanvas = reinterpret_cast<SkCanvas*>(
196          env->GetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas));
197  env->SetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas, (int)newCanvas);
198  env->SetIntField(canvasFinalizerObj, gCanvasFinalizerClassInfo.mNativeCanvas, (int)newCanvas);
199  SkSafeUnref(previousCanvas);
200}
201
202static void nativeLockCanvas(JNIEnv* env, jclass clazz,
203        jint nativeObject, jobject canvasObj, jobject dirtyRectObj) {
204    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
205
206    if (!isSurfaceValid(surface)) {
207        doThrowIAE(env);
208        return;
209    }
210
211    // get dirty region
212    Region dirtyRegion;
213    if (dirtyRectObj) {
214        Rect dirty;
215        dirty.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left);
216        dirty.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top);
217        dirty.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right);
218        dirty.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom);
219        if (!dirty.isEmpty()) {
220            dirtyRegion.set(dirty);
221        }
222    } else {
223        dirtyRegion.set(Rect(0x3FFF, 0x3FFF));
224    }
225
226    ANativeWindow_Buffer outBuffer;
227    Rect dirtyBounds(dirtyRegion.getBounds());
228    status_t err = surface->lock(&outBuffer, &dirtyBounds);
229    dirtyRegion.set(dirtyBounds);
230    if (err < 0) {
231        const char* const exception = (err == NO_MEMORY) ?
232                OutOfResourcesException :
233                "java/lang/IllegalArgumentException";
234        jniThrowException(env, exception, NULL);
235        return;
236    }
237
238    // Associate a SkCanvas object to this surface
239    env->SetIntField(canvasObj, gCanvasClassInfo.mSurfaceFormat, outBuffer.format);
240
241    SkBitmap bitmap;
242    ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
243    bitmap.setConfig(convertPixelFormat(outBuffer.format), outBuffer.width, outBuffer.height, bpr);
244    if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) {
245        bitmap.setIsOpaque(true);
246    }
247    if (outBuffer.width > 0 && outBuffer.height > 0) {
248        bitmap.setPixels(outBuffer.bits);
249    } else {
250        // be safe with an empty bitmap.
251        bitmap.setPixels(NULL);
252    }
253
254    SkCanvas* nativeCanvas = SkNEW_ARGS(SkCanvas, (bitmap));
255    swapCanvasPtr(env, canvasObj, nativeCanvas);
256
257    SkRegion clipReg;
258    if (dirtyRegion.isRect()) { // very common case
259        const Rect b(dirtyRegion.getBounds());
260        clipReg.setRect(b.left, b.top, b.right, b.bottom);
261    } else {
262        size_t count;
263        Rect const* r = dirtyRegion.getArray(&count);
264        while (count) {
265            clipReg.op(r->left, r->top, r->right, r->bottom, SkRegion::kUnion_Op);
266            r++, count--;
267        }
268    }
269
270    nativeCanvas->clipRegion(clipReg);
271
272    if (dirtyRectObj) {
273        const Rect& bounds(dirtyRegion.getBounds());
274        env->SetIntField(dirtyRectObj, gRectClassInfo.left, bounds.left);
275        env->SetIntField(dirtyRectObj, gRectClassInfo.top, bounds.top);
276        env->SetIntField(dirtyRectObj, gRectClassInfo.right, bounds.right);
277        env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, bounds.bottom);
278    }
279}
280
281static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz,
282        jint nativeObject, jobject canvasObj) {
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 nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz,
302        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> surface(ctrl->getSurface());
311    if (surface != NULL) {
312        surface->incStrong(&sRefBaseOwner);
313    }
314    return reinterpret_cast<jint>(surface.get());
315}
316
317static jint nativeReadFromParcel(JNIEnv* env, jclass clazz,
318        jint nativeObject, jobject parcelObj) {
319    Parcel* parcel = parcelForJavaObject(env, parcelObj);
320    if (parcel == NULL) {
321        doThrowNPE(env);
322        return 0;
323    }
324
325    sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
326    sp<IBinder> binder(parcel->readStrongBinder());
327
328    // update the Surface only if the underlying IGraphicBufferProducer
329    // has changed.
330    if (self != NULL
331            && (self->getIGraphicBufferProducer()->asBinder() == binder)) {
332        // same IGraphicBufferProducer, return ourselves
333        return int(self.get());
334    }
335
336    sp<Surface> sur;
337    sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder));
338    if (gbp != NULL) {
339        // we have a new IGraphicBufferProducer, create a new Surface for it
340        sur = new Surface(gbp);
341        // and keep a reference before passing to java
342        sur->incStrong(&sRefBaseOwner);
343    }
344
345    if (self != NULL) {
346        // and loose the java reference to ourselves
347        self->decStrong(&sRefBaseOwner);
348    }
349
350    return int(sur.get());
351}
352
353static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
354        jint nativeObject, jobject parcelObj) {
355    Parcel* parcel = parcelForJavaObject(env, parcelObj);
356    if (parcel == NULL) {
357        doThrowNPE(env);
358        return;
359    }
360    sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
361    parcel->writeStrongBinder( self != 0 ? self->getIGraphicBufferProducer()->asBinder() : NULL);
362}
363
364// ----------------------------------------------------------------------------
365
366static JNINativeMethod gSurfaceMethods[] = {
367    {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)I",
368            (void*)nativeCreateFromSurfaceTexture },
369    {"nativeRelease", "(I)V",
370            (void*)nativeRelease },
371    {"nativeIsValid", "(I)Z",
372            (void*)nativeIsValid },
373    {"nativeIsConsumerRunningBehind", "(I)Z",
374            (void*)nativeIsConsumerRunningBehind },
375    {"nativeLockCanvas", "(ILandroid/graphics/Canvas;Landroid/graphics/Rect;)V",
376            (void*)nativeLockCanvas },
377    {"nativeUnlockCanvasAndPost", "(ILandroid/graphics/Canvas;)V",
378            (void*)nativeUnlockCanvasAndPost },
379    {"nativeCreateFromSurfaceControl", "(I)I",
380            (void*)nativeCreateFromSurfaceControl },
381    {"nativeReadFromParcel", "(ILandroid/os/Parcel;)I",
382            (void*)nativeReadFromParcel },
383    {"nativeWriteToParcel", "(ILandroid/os/Parcel;)V",
384            (void*)nativeWriteToParcel },
385};
386
387int register_android_view_Surface(JNIEnv* env)
388{
389    int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface",
390            gSurfaceMethods, NELEM(gSurfaceMethods));
391
392    jclass clazz = env->FindClass("android/view/Surface");
393    gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz));
394    gSurfaceClassInfo.mNativeObject =
395            env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "I");
396    gSurfaceClassInfo.mLock =
397            env->GetFieldID(gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;");
398    gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(I)V");
399
400    clazz = env->FindClass("android/graphics/Canvas");
401    gCanvasClassInfo.mFinalizer = env->GetFieldID(clazz, "mFinalizer", "Landroid/graphics/Canvas$CanvasFinalizer;");
402    gCanvasClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I");
403    gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I");
404
405    clazz = env->FindClass("android/graphics/Canvas$CanvasFinalizer");
406    gCanvasFinalizerClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I");
407
408    clazz = env->FindClass("android/graphics/Rect");
409    gRectClassInfo.left = env->GetFieldID(clazz, "left", "I");
410    gRectClassInfo.top = env->GetFieldID(clazz, "top", "I");
411    gRectClassInfo.right = env->GetFieldID(clazz, "right", "I");
412    gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I");
413
414    return err;
415}
416
417};
418