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