android_view_Surface.cpp revision bb2d0cc7e1d487f7021b1f9ec0c6740e41b535f2
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#include <android_runtime/Log.h>
30
31#include <binder/Parcel.h>
32
33#include <gui/Surface.h>
34#include <gui/SurfaceControl.h>
35#include <gui/GLConsumer.h>
36
37#include <ui/Rect.h>
38#include <ui/Region.h>
39
40#include <SkCanvas.h>
41#include <SkBitmap.h>
42#include <SkImage.h>
43#include <SkRegion.h>
44
45#include <utils/misc.h>
46#include <utils/Log.h>
47
48#include <ScopedUtfChars.h>
49
50#include <AnimationContext.h>
51#include <DisplayListRenderer.h>
52#include <RenderNode.h>
53#include <renderthread/RenderProxy.h>
54
55// ----------------------------------------------------------------------------
56
57namespace android {
58
59static const char* const OutOfResourcesException =
60    "android/view/Surface$OutOfResourcesException";
61
62static struct {
63    jclass clazz;
64    jfieldID mNativeObject;
65    jfieldID mLock;
66    jmethodID ctor;
67} gSurfaceClassInfo;
68
69static struct {
70    jfieldID left;
71    jfieldID top;
72    jfieldID right;
73    jfieldID bottom;
74} gRectClassInfo;
75
76static struct {
77    jfieldID mSurfaceFormat;
78    jmethodID setNativeBitmap;
79} gCanvasClassInfo;
80
81// ----------------------------------------------------------------------------
82
83// this is just a pointer we use to pass to inc/decStrong
84static const void *sRefBaseOwner;
85
86bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) {
87    return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz);
88}
89
90sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) {
91    return android_view_Surface_getSurface(env, surfaceObj);
92}
93
94sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) {
95    sp<Surface> sur;
96    jobject lock = env->GetObjectField(surfaceObj,
97            gSurfaceClassInfo.mLock);
98    if (env->MonitorEnter(lock) == JNI_OK) {
99        sur = reinterpret_cast<Surface *>(
100                env->GetLongField(surfaceObj, gSurfaceClassInfo.mNativeObject));
101        env->MonitorExit(lock);
102    }
103    env->DeleteLocalRef(lock);
104    return sur;
105}
106
107jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env,
108        const sp<IGraphicBufferProducer>& bufferProducer) {
109    if (bufferProducer == NULL) {
110        return NULL;
111    }
112
113    sp<Surface> surface(new Surface(bufferProducer, true));
114    if (surface == NULL) {
115        return NULL;
116    }
117
118    jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz,
119            gSurfaceClassInfo.ctor, (jlong)surface.get());
120    if (surfaceObj == NULL) {
121        if (env->ExceptionCheck()) {
122            ALOGE("Could not create instance of Surface from IGraphicBufferProducer.");
123            LOGE_EX(env);
124            env->ExceptionClear();
125        }
126        return NULL;
127    }
128    surface->incStrong(&sRefBaseOwner);
129    return surfaceObj;
130}
131
132// ----------------------------------------------------------------------------
133
134static inline bool isSurfaceValid(const sp<Surface>& sur) {
135    return Surface::isValid(sur);
136}
137
138// ----------------------------------------------------------------------------
139
140static jlong nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz,
141        jobject surfaceTextureObj) {
142    sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surfaceTextureObj));
143    if (producer == NULL) {
144        jniThrowException(env, "java/lang/IllegalArgumentException",
145                "SurfaceTexture has already been released");
146        return 0;
147    }
148
149    sp<Surface> surface(new Surface(producer, true));
150    if (surface == NULL) {
151        jniThrowException(env, OutOfResourcesException, NULL);
152        return 0;
153    }
154
155    surface->incStrong(&sRefBaseOwner);
156    return jlong(surface.get());
157}
158
159static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
160    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
161    sur->decStrong(&sRefBaseOwner);
162}
163
164static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jlong nativeObject) {
165    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
166    return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE;
167}
168
169static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jlong nativeObject) {
170    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
171    if (!isSurfaceValid(sur)) {
172        doThrowIAE(env);
173        return JNI_FALSE;
174    }
175    int value = 0;
176    ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get());
177    anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value);
178    return value;
179}
180
181static inline SkColorType convertPixelFormat(PixelFormat format) {
182    /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then
183        we can map to kN32_SkColorType, and optionally call
184        bitmap.setAlphaType(kOpaque_SkAlphaType) on the resulting SkBitmap
185        (as an accelerator)
186    */
187    switch (format) {
188    case PIXEL_FORMAT_RGBX_8888:    return kN32_SkColorType;
189    case PIXEL_FORMAT_RGBA_8888:    return kN32_SkColorType;
190    case PIXEL_FORMAT_RGB_565:      return kRGB_565_SkColorType;
191    default:                        return kUnknown_SkColorType;
192    }
193}
194
195static jlong nativeLockCanvas(JNIEnv* env, jclass clazz,
196        jlong nativeObject, jobject canvasObj, jobject dirtyRectObj) {
197    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
198
199    if (!isSurfaceValid(surface)) {
200        doThrowIAE(env);
201        return 0;
202    }
203
204    Rect dirtyRect;
205    Rect* dirtyRectPtr = NULL;
206
207    if (dirtyRectObj) {
208        dirtyRect.left   = env->GetIntField(dirtyRectObj, gRectClassInfo.left);
209        dirtyRect.top    = env->GetIntField(dirtyRectObj, gRectClassInfo.top);
210        dirtyRect.right  = env->GetIntField(dirtyRectObj, gRectClassInfo.right);
211        dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom);
212        dirtyRectPtr = &dirtyRect;
213    }
214
215    ANativeWindow_Buffer outBuffer;
216    status_t err = surface->lock(&outBuffer, dirtyRectPtr);
217    if (err < 0) {
218        const char* const exception = (err == NO_MEMORY) ?
219                OutOfResourcesException :
220                "java/lang/IllegalArgumentException";
221        jniThrowException(env, exception, NULL);
222        return 0;
223    }
224
225    // Associate a SkCanvas object to this surface
226    env->SetIntField(canvasObj, gCanvasClassInfo.mSurfaceFormat, outBuffer.format);
227
228    SkImageInfo info = SkImageInfo::Make(outBuffer.width, outBuffer.height,
229                                         convertPixelFormat(outBuffer.format),
230                                         kPremul_SkAlphaType);
231    if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) {
232        info.fAlphaType = kOpaque_SkAlphaType;
233    }
234
235    SkBitmap bitmap;
236    ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
237    bitmap.setInfo(info, bpr);
238    if (outBuffer.width > 0 && outBuffer.height > 0) {
239        bitmap.setPixels(outBuffer.bits);
240    } else {
241        // be safe with an empty bitmap.
242        bitmap.setPixels(NULL);
243    }
244
245    env->CallVoidMethod(canvasObj, gCanvasClassInfo.setNativeBitmap,
246                        reinterpret_cast<jlong>(&bitmap));
247
248    if (dirtyRectPtr) {
249        SkCanvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj);
250        nativeCanvas->clipRect( SkRect::Make(reinterpret_cast<const SkIRect&>(dirtyRect)) );
251    }
252
253    if (dirtyRectObj) {
254        env->SetIntField(dirtyRectObj, gRectClassInfo.left,   dirtyRect.left);
255        env->SetIntField(dirtyRectObj, gRectClassInfo.top,    dirtyRect.top);
256        env->SetIntField(dirtyRectObj, gRectClassInfo.right,  dirtyRect.right);
257        env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom);
258    }
259
260    // Create another reference to the surface and return it.  This reference
261    // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject,
262    // because the latter could be replaced while the surface is locked.
263    sp<Surface> lockedSurface(surface);
264    lockedSurface->incStrong(&sRefBaseOwner);
265    return (jlong) lockedSurface.get();
266}
267
268static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz,
269        jlong nativeObject, jobject canvasObj) {
270    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
271    if (!isSurfaceValid(surface)) {
272        return;
273    }
274
275    // detach the canvas from the surface
276    env->CallVoidMethod(canvasObj, gCanvasClassInfo.setNativeBitmap, (jlong)0);
277
278    // unlock surface
279    status_t err = surface->unlockAndPost();
280    if (err < 0) {
281        doThrowIAE(env);
282    }
283}
284
285static void nativeAllocateBuffers(JNIEnv* /* env */ , jclass /* clazz */,
286        jlong nativeObject) {
287    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
288    if (!isSurfaceValid(surface)) {
289        return;
290    }
291
292    surface->allocateBuffers();
293}
294
295// ----------------------------------------------------------------------------
296
297static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz,
298        jlong surfaceControlNativeObj) {
299    /*
300     * This is used by the WindowManagerService just after constructing
301     * a Surface and is necessary for returning the Surface reference to
302     * the caller. At this point, we should only have a SurfaceControl.
303     */
304
305    sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
306    sp<Surface> surface(ctrl->getSurface());
307    if (surface != NULL) {
308        surface->incStrong(&sRefBaseOwner);
309    }
310    return reinterpret_cast<jlong>(surface.get());
311}
312
313static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz,
314        jlong nativeObject, jobject parcelObj) {
315    Parcel* parcel = parcelForJavaObject(env, parcelObj);
316    if (parcel == NULL) {
317        doThrowNPE(env);
318        return 0;
319    }
320
321    sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
322    sp<IBinder> binder(parcel->readStrongBinder());
323
324    // update the Surface only if the underlying IGraphicBufferProducer
325    // has changed.
326    if (self != NULL
327            && (self->getIGraphicBufferProducer()->asBinder() == binder)) {
328        // same IGraphicBufferProducer, return ourselves
329        return jlong(self.get());
330    }
331
332    sp<Surface> sur;
333    sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder));
334    if (gbp != NULL) {
335        // we have a new IGraphicBufferProducer, create a new Surface for it
336        sur = new Surface(gbp, true);
337        // and keep a reference before passing to java
338        sur->incStrong(&sRefBaseOwner);
339    }
340
341    if (self != NULL) {
342        // and loose the java reference to ourselves
343        self->decStrong(&sRefBaseOwner);
344    }
345
346    return jlong(sur.get());
347}
348
349static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
350        jlong nativeObject, jobject parcelObj) {
351    Parcel* parcel = parcelForJavaObject(env, parcelObj);
352    if (parcel == NULL) {
353        doThrowNPE(env);
354        return;
355    }
356    sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
357    parcel->writeStrongBinder( self != 0 ? self->getIGraphicBufferProducer()->asBinder() : NULL);
358}
359
360namespace uirenderer {
361
362using namespace android::uirenderer::renderthread;
363
364class ContextFactory : public IContextFactory {
365public:
366    virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) {
367        return new AnimationContext(clock);
368    }
369};
370
371static jlong create(JNIEnv* env, jclass clazz, jlong rootNodePtr, jlong surfacePtr) {
372    RenderNode* rootNode = reinterpret_cast<RenderNode*>(rootNodePtr);
373    sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr));
374    ContextFactory factory;
375    RenderProxy* proxy = new RenderProxy(false, rootNode, &factory);
376    proxy->loadSystemProperties();
377    proxy->initialize(surface);
378    // Shadows can't be used via this interface, so just set the light source
379    // to all 0s. (and width & height are unused, TODO remove them)
380    proxy->setup(0, 0, (Vector3){0, 0, 0}, 0, 0, 0);
381    return (jlong) proxy;
382}
383
384static void setSurface(JNIEnv* env, jclass clazz, jlong rendererPtr, jlong surfacePtr) {
385    RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
386    sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr));
387    proxy->updateSurface(surface);
388}
389
390static void draw(JNIEnv* env, jclass clazz, jlong rendererPtr) {
391    RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
392    nsecs_t frameTimeNs = systemTime(CLOCK_MONOTONIC);
393    proxy->syncAndDrawFrame(frameTimeNs, 0, 1.0f);
394}
395
396static void destroy(JNIEnv* env, jclass clazz, jlong rendererPtr) {
397    RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
398    delete proxy;
399}
400
401} // uirenderer
402
403// ----------------------------------------------------------------------------
404
405namespace hwui = android::uirenderer;
406
407static JNINativeMethod gSurfaceMethods[] = {
408    {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)J",
409            (void*)nativeCreateFromSurfaceTexture },
410    {"nativeRelease", "(J)V",
411            (void*)nativeRelease },
412    {"nativeIsValid", "(J)Z",
413            (void*)nativeIsValid },
414    {"nativeIsConsumerRunningBehind", "(J)Z",
415            (void*)nativeIsConsumerRunningBehind },
416    {"nativeLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)J",
417            (void*)nativeLockCanvas },
418    {"nativeUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
419            (void*)nativeUnlockCanvasAndPost },
420    {"nativeAllocateBuffers", "(J)V",
421            (void*)nativeAllocateBuffers },
422    {"nativeCreateFromSurfaceControl", "(J)J",
423            (void*)nativeCreateFromSurfaceControl },
424    {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J",
425            (void*)nativeReadFromParcel },
426    {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
427            (void*)nativeWriteToParcel },
428
429    // HWUI context
430    {"nHwuiCreate", "(JJ)J", (void*) hwui::create },
431    {"nHwuiSetSurface", "(JJ)V", (void*) hwui::setSurface },
432    {"nHwuiDraw", "(J)V", (void*) hwui::draw },
433    {"nHwuiDestroy", "(J)V", (void*) hwui::destroy },
434};
435
436int register_android_view_Surface(JNIEnv* env)
437{
438    int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface",
439            gSurfaceMethods, NELEM(gSurfaceMethods));
440
441    jclass clazz = env->FindClass("android/view/Surface");
442    gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz));
443    gSurfaceClassInfo.mNativeObject =
444            env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "J");
445    gSurfaceClassInfo.mLock =
446            env->GetFieldID(gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;");
447    gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(J)V");
448
449    clazz = env->FindClass("android/graphics/Canvas");
450    gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I");
451    gCanvasClassInfo.setNativeBitmap = env->GetMethodID(clazz, "setNativeBitmap", "(J)V");
452
453    clazz = env->FindClass("android/graphics/Rect");
454    gRectClassInfo.left = env->GetFieldID(clazz, "left", "I");
455    gRectClassInfo.top = env->GetFieldID(clazz, "top", "I");
456    gRectClassInfo.right = env->GetFieldID(clazz, "right", "I");
457    gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I");
458
459    return err;
460}
461
462};
463