android_view_Surface.cpp revision f3d3b4d80618d498fb669c8bfdc492ce9ea25691
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 <gui/Surface.h>
31#include <gui/SurfaceControl.h>
32#include <gui/GLConsumer.h>
33
34#include <ui/Rect.h>
35#include <ui/Region.h>
36
37#include <SkCanvas.h>
38#include <SkBitmap.h>
39#include <SkRegion.h>
40
41#include <utils/misc.h>
42#include <utils/Log.h>
43
44#include <ScopedUtfChars.h>
45
46// ----------------------------------------------------------------------------
47
48namespace android {
49
50static const char* const OutOfResourcesException =
51    "android/view/Surface$OutOfResourcesException";
52
53static struct {
54    jclass clazz;
55    jfieldID mNativeObject;
56    jfieldID mGenerationId;
57    jfieldID mCanvas;
58    jfieldID mCanvasSaveCount;
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 mNativeCanvas;
71    jfieldID mSurfaceFormat;
72} gCanvasClassInfo;
73
74// ----------------------------------------------------------------------------
75
76bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) {
77    return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz);
78}
79
80sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) {
81    return android_view_Surface_getSurface(env, surfaceObj);
82}
83
84sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) {
85    return reinterpret_cast<Surface *>(
86            env->GetIntField(surfaceObj, gSurfaceClassInfo.mNativeObject));
87}
88
89jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env,
90        const sp<IGraphicBufferProducer>& bufferProducer) {
91    if (bufferProducer == NULL) {
92        return NULL;
93    }
94
95    sp<Surface> surface(new Surface(bufferProducer));
96    if (surface == NULL) {
97        return NULL;
98    }
99
100    jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz, gSurfaceClassInfo.ctor, surface.get());
101    if (surfaceObj == NULL) {
102        if (env->ExceptionCheck()) {
103            ALOGE("Could not create instance of Surface from IGraphicBufferProducer.");
104            LOGE_EX(env);
105            env->ExceptionClear();
106        }
107        return NULL;
108    }
109    surface->incStrong(surfaceObj);
110    return surfaceObj;
111}
112
113// ----------------------------------------------------------------------------
114
115static inline bool isSurfaceValid(const sp<Surface>& sur) {
116    return Surface::isValid(sur);
117}
118
119// ----------------------------------------------------------------------------
120
121static jint nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz,
122        jobject surfaceTextureObj) {
123    sp<GLConsumer> st(SurfaceTexture_getSurfaceTexture(env, surfaceTextureObj));
124    if (st == NULL) {
125        jniThrowException(env, "java/lang/IllegalArgumentException",
126                "SurfaceTexture has already been released");
127        return 0;
128    }
129
130    sp<IGraphicBufferProducer> bq = st->getBufferQueue();
131    sp<Surface> surface(new Surface(bq));
132    if (surface == NULL) {
133        jniThrowException(env, OutOfResourcesException, NULL);
134        return 0;
135    }
136
137    surface->incStrong(clazz);
138    return int(surface.get());
139}
140
141static void nativeRelease(JNIEnv* env, jclass clazz, jint nativeObject) {
142    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
143    sur->decStrong(clazz);
144}
145
146static void nativeDestroy(JNIEnv* env, jclass clazz, jint nativeObject) {
147    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
148    sur->decStrong(clazz);
149}
150
151static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jint nativeObject) {
152    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
153    return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE;
154}
155
156static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jint nativeObject) {
157    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
158    if (!isSurfaceValid(sur)) {
159        doThrowIAE(env);
160        return JNI_FALSE;
161    }
162    int value = 0;
163    ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get());
164    anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value);
165    return value;
166}
167
168static inline SkBitmap::Config convertPixelFormat(PixelFormat format) {
169    /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then
170        we can map to SkBitmap::kARGB_8888_Config, and optionally call
171        bitmap.setIsOpaque(true) on the resulting SkBitmap (as an accelerator)
172    */
173    switch (format) {
174    case PIXEL_FORMAT_RGBX_8888:    return SkBitmap::kARGB_8888_Config;
175    case PIXEL_FORMAT_RGBA_8888:    return SkBitmap::kARGB_8888_Config;
176    case PIXEL_FORMAT_RGBA_4444:    return SkBitmap::kARGB_4444_Config;
177    case PIXEL_FORMAT_RGB_565:      return SkBitmap::kRGB_565_Config;
178    case PIXEL_FORMAT_A_8:          return SkBitmap::kA8_Config;
179    default:                        return SkBitmap::kNo_Config;
180    }
181}
182
183static jobject nativeLockCanvas(JNIEnv* env, jobject surfaceObj, jint nativeObject, jobject dirtyRectObj) {
184    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
185
186    if (!isSurfaceValid(surface)) {
187        doThrowIAE(env);
188        return NULL;
189    }
190
191    // get dirty region
192    Region dirtyRegion;
193    if (dirtyRectObj) {
194        Rect dirty;
195        dirty.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left);
196        dirty.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top);
197        dirty.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right);
198        dirty.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom);
199        if (!dirty.isEmpty()) {
200            dirtyRegion.set(dirty);
201        }
202    } else {
203        dirtyRegion.set(Rect(0x3FFF, 0x3FFF));
204    }
205
206    ANativeWindow_Buffer outBuffer;
207    Rect dirtyBounds(dirtyRegion.getBounds());
208    status_t err = surface->lock(&outBuffer, &dirtyBounds);
209    dirtyRegion.set(dirtyBounds);
210    if (err < 0) {
211        const char* const exception = (err == NO_MEMORY) ?
212                OutOfResourcesException :
213                "java/lang/IllegalArgumentException";
214        jniThrowException(env, exception, NULL);
215        return NULL;
216    }
217
218    // Associate a SkCanvas object to this surface
219    jobject canvasObj = env->GetObjectField(surfaceObj, gSurfaceClassInfo.mCanvas);
220    env->SetIntField(canvasObj, gCanvasClassInfo.mSurfaceFormat, outBuffer.format);
221
222    SkCanvas* nativeCanvas = reinterpret_cast<SkCanvas*>(
223            env->GetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas));
224    SkBitmap bitmap;
225    ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
226    bitmap.setConfig(convertPixelFormat(outBuffer.format), outBuffer.width, outBuffer.height, bpr);
227    if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) {
228        bitmap.setIsOpaque(true);
229    }
230    if (outBuffer.width > 0 && outBuffer.height > 0) {
231        bitmap.setPixels(outBuffer.bits);
232    } else {
233        // be safe with an empty bitmap.
234        bitmap.setPixels(NULL);
235    }
236    nativeCanvas->setBitmapDevice(bitmap);
237
238    SkRegion clipReg;
239    if (dirtyRegion.isRect()) { // very common case
240        const Rect b(dirtyRegion.getBounds());
241        clipReg.setRect(b.left, b.top, b.right, b.bottom);
242    } else {
243        size_t count;
244        Rect const* r = dirtyRegion.getArray(&count);
245        while (count) {
246            clipReg.op(r->left, r->top, r->right, r->bottom, SkRegion::kUnion_Op);
247            r++, count--;
248        }
249    }
250
251    nativeCanvas->clipRegion(clipReg);
252
253    int saveCount = nativeCanvas->save();
254    env->SetIntField(surfaceObj, gSurfaceClassInfo.mCanvasSaveCount, saveCount);
255
256    if (dirtyRectObj) {
257        const Rect& bounds(dirtyRegion.getBounds());
258        env->SetIntField(dirtyRectObj, gRectClassInfo.left, bounds.left);
259        env->SetIntField(dirtyRectObj, gRectClassInfo.top, bounds.top);
260        env->SetIntField(dirtyRectObj, gRectClassInfo.right, bounds.right);
261        env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, bounds.bottom);
262    }
263
264    return canvasObj;
265}
266
267static void nativeUnlockCanvasAndPost(JNIEnv* env, jobject surfaceObj, jint nativeObject, jobject canvasObj) {
268    jobject ownCanvasObj = env->GetObjectField(surfaceObj, gSurfaceClassInfo.mCanvas);
269    if (!env->IsSameObject(ownCanvasObj, canvasObj)) {
270        doThrowIAE(env);
271        return;
272    }
273
274    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
275    if (!isSurfaceValid(surface)) {
276        return;
277    }
278
279    // detach the canvas from the surface
280    SkCanvas* nativeCanvas = reinterpret_cast<SkCanvas*>(
281            env->GetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas));
282    int saveCount = env->GetIntField(surfaceObj, gSurfaceClassInfo.mCanvasSaveCount);
283    nativeCanvas->restoreToCount(saveCount);
284    nativeCanvas->setBitmapDevice(SkBitmap());
285    env->SetIntField(surfaceObj, gSurfaceClassInfo.mCanvasSaveCount, 0);
286
287    // unlock surface
288    status_t err = surface->unlockAndPost();
289    if (err < 0) {
290        doThrowIAE(env);
291    }
292}
293
294// ----------------------------------------------------------------------------
295
296static jint nativeCopyFrom(JNIEnv* env, jclass clazz,
297        jint nativeObject, jint surfaceControlNativeObj) {
298    /*
299     * This is used by the WindowManagerService just after constructing
300     * a Surface and is necessary for returning the Surface reference to
301     * the caller. At this point, we should only have a SurfaceControl.
302     */
303
304    sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
305    sp<Surface> other(ctrl->getSurface());
306    if (other != NULL) {
307        other->incStrong(clazz);
308    }
309
310    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
311    if (sur != NULL) {
312        sur->decStrong(clazz);
313    }
314
315    return int(other.get());
316}
317
318static jint nativeReadFromParcel(JNIEnv* env, jclass clazz,
319        jint nativeObject, jobject parcelObj) {
320    Parcel* parcel = parcelForJavaObject(env, parcelObj);
321    if (parcel == NULL) {
322        doThrowNPE(env);
323        return 0;
324    }
325    sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
326    if (self != NULL) {
327        self->decStrong(clazz);
328    }
329    sp<Surface> sur(Surface::readFromParcel(*parcel));
330    if (sur != NULL) {
331        sur->incStrong(clazz);
332    }
333    return int(sur.get());
334}
335
336static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
337        jint nativeObject, jobject parcelObj) {
338    Parcel* parcel = parcelForJavaObject(env, parcelObj);
339    if (parcel == NULL) {
340        doThrowNPE(env);
341        return;
342    }
343    sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
344    Surface::writeToParcel(self, parcel);
345}
346
347// ----------------------------------------------------------------------------
348
349static JNINativeMethod gSurfaceMethods[] = {
350    {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)I",
351            (void*)nativeCreateFromSurfaceTexture },
352    {"nativeRelease", "(I)V",
353            (void*)nativeRelease },
354    {"nativeDestroy", "(I)V",
355            (void*)nativeDestroy },
356    {"nativeIsValid", "(I)Z",
357            (void*)nativeIsValid },
358    {"nativeIsConsumerRunningBehind", "(I)Z",
359            (void*)nativeIsConsumerRunningBehind },
360    {"nativeLockCanvas", "(ILandroid/graphics/Rect;)Landroid/graphics/Canvas;",
361            (void*)nativeLockCanvas },
362    {"nativeUnlockCanvasAndPost", "(ILandroid/graphics/Canvas;)V",
363            (void*)nativeUnlockCanvasAndPost },
364    {"nativeCopyFrom", "(II)I",
365            (void*)nativeCopyFrom },
366    {"nativeReadFromParcel", "(ILandroid/os/Parcel;)I",
367            (void*)nativeReadFromParcel },
368    {"nativeWriteToParcel", "(ILandroid/os/Parcel;)V",
369            (void*)nativeWriteToParcel },
370};
371
372int register_android_view_Surface(JNIEnv* env)
373{
374    int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface",
375            gSurfaceMethods, NELEM(gSurfaceMethods));
376
377    jclass clazz = env->FindClass("android/view/Surface");
378    gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz));
379    gSurfaceClassInfo.mNativeObject =
380            env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "I");
381    gSurfaceClassInfo.mGenerationId =
382            env->GetFieldID(gSurfaceClassInfo.clazz, "mGenerationId", "I");
383    gSurfaceClassInfo.mCanvas =
384            env->GetFieldID(gSurfaceClassInfo.clazz, "mCanvas", "Landroid/graphics/Canvas;");
385    gSurfaceClassInfo.mCanvasSaveCount =
386            env->GetFieldID(gSurfaceClassInfo.clazz, "mCanvasSaveCount", "I");
387    gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(I)V");
388
389    clazz = env->FindClass("android/graphics/Canvas");
390    gCanvasClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I");
391    gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I");
392
393    clazz = env->FindClass("android/graphics/Rect");
394    gRectClassInfo.left = env->GetFieldID(clazz, "left", "I");
395    gRectClassInfo.top = env->GetFieldID(clazz, "top", "I");
396    gRectClassInfo.right = env->GetFieldID(clazz, "right", "I");
397    gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I");
398
399    return err;
400}
401
402};
403