android_view_Surface.cpp revision f35b989d26bb98900f6c5fa2e586326b30b6e161
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 "core_jni_helpers.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 <FrameInfo.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
76// ----------------------------------------------------------------------------
77
78// this is just a pointer we use to pass to inc/decStrong
79static const void *sRefBaseOwner;
80
81bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) {
82    return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz);
83}
84
85sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) {
86    return android_view_Surface_getSurface(env, surfaceObj);
87}
88
89sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) {
90    sp<Surface> sur;
91    jobject lock = env->GetObjectField(surfaceObj,
92            gSurfaceClassInfo.mLock);
93    if (env->MonitorEnter(lock) == JNI_OK) {
94        sur = reinterpret_cast<Surface *>(
95                env->GetLongField(surfaceObj, gSurfaceClassInfo.mNativeObject));
96        env->MonitorExit(lock);
97    }
98    env->DeleteLocalRef(lock);
99    return sur;
100}
101
102jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env,
103        const sp<IGraphicBufferProducer>& bufferProducer) {
104    if (bufferProducer == NULL) {
105        return NULL;
106    }
107
108    sp<Surface> surface(new Surface(bufferProducer, true));
109    if (surface == NULL) {
110        return NULL;
111    }
112
113    jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz,
114            gSurfaceClassInfo.ctor, (jlong)surface.get());
115    if (surfaceObj == NULL) {
116        if (env->ExceptionCheck()) {
117            ALOGE("Could not create instance of Surface from IGraphicBufferProducer.");
118            LOGE_EX(env);
119            env->ExceptionClear();
120        }
121        return NULL;
122    }
123    surface->incStrong(&sRefBaseOwner);
124    return surfaceObj;
125}
126
127int android_view_Surface_mapPublicFormatToHalFormat(PublicFormat f) {
128
129    switch(f) {
130        case PublicFormat::JPEG:
131        case PublicFormat::DEPTH_POINT_CLOUD:
132            return HAL_PIXEL_FORMAT_BLOB;
133        case PublicFormat::DEPTH16:
134            return HAL_PIXEL_FORMAT_Y16;
135        case PublicFormat::RAW_SENSOR:
136            return HAL_PIXEL_FORMAT_RAW16;
137        default:
138            // Most formats map 1:1
139            return static_cast<int>(f);
140    }
141}
142
143android_dataspace android_view_Surface_mapPublicFormatToHalDataspace(
144        PublicFormat f) {
145    switch(f) {
146        case PublicFormat::JPEG:
147            return HAL_DATASPACE_JFIF;
148        case PublicFormat::DEPTH_POINT_CLOUD:
149        case PublicFormat::DEPTH16:
150            return HAL_DATASPACE_DEPTH;
151        case PublicFormat::RAW_SENSOR:
152        case PublicFormat::RAW10:
153            return HAL_DATASPACE_ARBITRARY;
154        case PublicFormat::YUV_420_888:
155        case PublicFormat::NV21:
156        case PublicFormat::YV12:
157            return HAL_DATASPACE_JFIF;
158        default:
159            // Most formats map to UNKNOWN
160            return HAL_DATASPACE_UNKNOWN;
161    }
162}
163
164PublicFormat android_view_Surface_mapHalFormatDataspaceToPublicFormat(
165        int format, android_dataspace dataSpace) {
166    switch(format) {
167        case HAL_PIXEL_FORMAT_RGBA_8888:
168        case HAL_PIXEL_FORMAT_RGBX_8888:
169        case HAL_PIXEL_FORMAT_RGB_888:
170        case HAL_PIXEL_FORMAT_RGB_565:
171        case HAL_PIXEL_FORMAT_Y8:
172        case HAL_PIXEL_FORMAT_RAW10:
173        case HAL_PIXEL_FORMAT_YCbCr_420_888:
174        case HAL_PIXEL_FORMAT_YV12:
175            // Enums overlap in both name and value
176            return static_cast<PublicFormat>(format);
177        case HAL_PIXEL_FORMAT_RAW16:
178            // Name differs, though value is the same
179            return PublicFormat::RAW_SENSOR;
180        case HAL_PIXEL_FORMAT_YCbCr_422_SP:
181            // Name differs, though the value is the same
182            return PublicFormat::NV16;
183        case HAL_PIXEL_FORMAT_YCrCb_420_SP:
184            // Name differs, though the value is the same
185            return PublicFormat::NV21;
186        case HAL_PIXEL_FORMAT_YCbCr_422_I:
187            // Name differs, though the value is the same
188            return PublicFormat::YUY2;
189        case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
190            // Name differs, though the value is the same
191            return PublicFormat::PRIVATE;
192        case HAL_PIXEL_FORMAT_Y16:
193            // Dataspace-dependent
194            switch (dataSpace) {
195                case HAL_DATASPACE_DEPTH:
196                    return PublicFormat::DEPTH16;
197                default:
198                    // Assume non-depth Y16 is just Y16.
199                    return PublicFormat::Y16;
200            }
201            break;
202        case HAL_PIXEL_FORMAT_BLOB:
203            // Dataspace-dependent
204            switch (dataSpace) {
205                case HAL_DATASPACE_DEPTH:
206                    return PublicFormat::DEPTH_POINT_CLOUD;
207                case HAL_DATASPACE_JFIF:
208                    return PublicFormat::JPEG;
209                default:
210                    // Assume otherwise-marked blobs are also JPEG
211                    return PublicFormat::JPEG;
212            }
213            break;
214        case HAL_PIXEL_FORMAT_BGRA_8888:
215        case HAL_PIXEL_FORMAT_RAW_OPAQUE:
216            // Not defined in public API
217            return PublicFormat::UNKNOWN;
218
219        default:
220            return PublicFormat::UNKNOWN;
221    }
222}
223// ----------------------------------------------------------------------------
224
225static inline bool isSurfaceValid(const sp<Surface>& sur) {
226    return Surface::isValid(sur);
227}
228
229// ----------------------------------------------------------------------------
230
231static jlong nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz,
232        jobject surfaceTextureObj) {
233    sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surfaceTextureObj));
234    if (producer == NULL) {
235        jniThrowException(env, "java/lang/IllegalArgumentException",
236                "SurfaceTexture has already been released");
237        return 0;
238    }
239
240    sp<Surface> surface(new Surface(producer, true));
241    if (surface == NULL) {
242        jniThrowException(env, OutOfResourcesException, NULL);
243        return 0;
244    }
245
246    surface->incStrong(&sRefBaseOwner);
247    return jlong(surface.get());
248}
249
250static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
251    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
252    sur->decStrong(&sRefBaseOwner);
253}
254
255static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jlong nativeObject) {
256    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
257    return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE;
258}
259
260static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jlong nativeObject) {
261    sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
262    if (!isSurfaceValid(sur)) {
263        doThrowIAE(env);
264        return JNI_FALSE;
265    }
266    int value = 0;
267    ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get());
268    anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value);
269    return value;
270}
271
272static inline SkColorType convertPixelFormat(PixelFormat format) {
273    /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then
274        we can map to kN32_SkColorType, and optionally call
275        bitmap.setAlphaType(kOpaque_SkAlphaType) on the resulting SkBitmap
276        (as an accelerator)
277    */
278    switch (format) {
279    case PIXEL_FORMAT_RGBX_8888:    return kN32_SkColorType;
280    case PIXEL_FORMAT_RGBA_8888:    return kN32_SkColorType;
281    case PIXEL_FORMAT_RGB_565:      return kRGB_565_SkColorType;
282    default:                        return kUnknown_SkColorType;
283    }
284}
285
286static jlong nativeLockCanvas(JNIEnv* env, jclass clazz,
287        jlong nativeObject, jobject canvasObj, jobject dirtyRectObj) {
288    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
289
290    if (!isSurfaceValid(surface)) {
291        doThrowIAE(env);
292        return 0;
293    }
294
295    Rect dirtyRect(Rect::EMPTY_RECT);
296    Rect* dirtyRectPtr = NULL;
297
298    if (dirtyRectObj) {
299        dirtyRect.left   = env->GetIntField(dirtyRectObj, gRectClassInfo.left);
300        dirtyRect.top    = env->GetIntField(dirtyRectObj, gRectClassInfo.top);
301        dirtyRect.right  = env->GetIntField(dirtyRectObj, gRectClassInfo.right);
302        dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom);
303        dirtyRectPtr = &dirtyRect;
304    }
305
306    ANativeWindow_Buffer outBuffer;
307    status_t err = surface->lock(&outBuffer, dirtyRectPtr);
308    if (err < 0) {
309        const char* const exception = (err == NO_MEMORY) ?
310                OutOfResourcesException :
311                "java/lang/IllegalArgumentException";
312        jniThrowException(env, exception, NULL);
313        return 0;
314    }
315
316
317    SkImageInfo info = SkImageInfo::Make(outBuffer.width, outBuffer.height,
318                                         convertPixelFormat(outBuffer.format),
319                                         outBuffer.format == PIXEL_FORMAT_RGBX_8888 ?
320                                         kOpaque_SkAlphaType : kPremul_SkAlphaType);
321
322    SkBitmap bitmap;
323    ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
324    bitmap.setInfo(info, bpr);
325    if (outBuffer.width > 0 && outBuffer.height > 0) {
326        bitmap.setPixels(outBuffer.bits);
327    } else {
328        // be safe with an empty bitmap.
329        bitmap.setPixels(NULL);
330    }
331
332    Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj);
333    nativeCanvas->setBitmap(bitmap);
334
335    if (dirtyRectPtr) {
336        nativeCanvas->clipRect(dirtyRect.left, dirtyRect.top,
337                dirtyRect.right, dirtyRect.bottom);
338    }
339
340    if (dirtyRectObj) {
341        env->SetIntField(dirtyRectObj, gRectClassInfo.left,   dirtyRect.left);
342        env->SetIntField(dirtyRectObj, gRectClassInfo.top,    dirtyRect.top);
343        env->SetIntField(dirtyRectObj, gRectClassInfo.right,  dirtyRect.right);
344        env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom);
345    }
346
347    // Create another reference to the surface and return it.  This reference
348    // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject,
349    // because the latter could be replaced while the surface is locked.
350    sp<Surface> lockedSurface(surface);
351    lockedSurface->incStrong(&sRefBaseOwner);
352    return (jlong) lockedSurface.get();
353}
354
355static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz,
356        jlong nativeObject, jobject canvasObj) {
357    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
358    if (!isSurfaceValid(surface)) {
359        return;
360    }
361
362    // detach the canvas from the surface
363    Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj);
364    nativeCanvas->setBitmap(SkBitmap());
365
366    // unlock surface
367    status_t err = surface->unlockAndPost();
368    if (err < 0) {
369        doThrowIAE(env);
370    }
371}
372
373static void nativeAllocateBuffers(JNIEnv* /* env */ , jclass /* clazz */,
374        jlong nativeObject) {
375    sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
376    if (!isSurfaceValid(surface)) {
377        return;
378    }
379
380    surface->allocateBuffers();
381}
382
383// ----------------------------------------------------------------------------
384
385static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz,
386        jlong surfaceControlNativeObj) {
387    /*
388     * This is used by the WindowManagerService just after constructing
389     * a Surface and is necessary for returning the Surface reference to
390     * the caller. At this point, we should only have a SurfaceControl.
391     */
392
393    sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
394    sp<Surface> surface(ctrl->getSurface());
395    if (surface != NULL) {
396        surface->incStrong(&sRefBaseOwner);
397    }
398    return reinterpret_cast<jlong>(surface.get());
399}
400
401static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz,
402        jlong nativeObject, jobject parcelObj) {
403    Parcel* parcel = parcelForJavaObject(env, parcelObj);
404    if (parcel == NULL) {
405        doThrowNPE(env);
406        return 0;
407    }
408
409    sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
410    sp<IBinder> binder(parcel->readStrongBinder());
411
412    // update the Surface only if the underlying IGraphicBufferProducer
413    // has changed.
414    if (self != NULL
415            && (IInterface::asBinder(self->getIGraphicBufferProducer()) == binder)) {
416        // same IGraphicBufferProducer, return ourselves
417        return jlong(self.get());
418    }
419
420    sp<Surface> sur;
421    sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder));
422    if (gbp != NULL) {
423        // we have a new IGraphicBufferProducer, create a new Surface for it
424        sur = new Surface(gbp, true);
425        // and keep a reference before passing to java
426        sur->incStrong(&sRefBaseOwner);
427    }
428
429    if (self != NULL) {
430        // and loose the java reference to ourselves
431        self->decStrong(&sRefBaseOwner);
432    }
433
434    return jlong(sur.get());
435}
436
437static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
438        jlong nativeObject, jobject parcelObj) {
439    Parcel* parcel = parcelForJavaObject(env, parcelObj);
440    if (parcel == NULL) {
441        doThrowNPE(env);
442        return;
443    }
444    sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
445    parcel->writeStrongBinder( self != 0 ? IInterface::asBinder(self->getIGraphicBufferProducer()) : NULL);
446}
447
448static jint nativeGetWidth(JNIEnv* env, jclass clazz, jlong nativeObject) {
449    Surface* surface = reinterpret_cast<Surface*>(nativeObject);
450    ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
451    int value = 0;
452    anw->query(anw, NATIVE_WINDOW_WIDTH, &value);
453    return value;
454}
455
456static jint nativeGetHeight(JNIEnv* env, jclass clazz, jlong nativeObject) {
457    Surface* surface = reinterpret_cast<Surface*>(nativeObject);
458    ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
459    int value = 0;
460    anw->query(anw, NATIVE_WINDOW_HEIGHT, &value);
461    return value;
462}
463
464namespace uirenderer {
465
466using namespace android::uirenderer::renderthread;
467
468class ContextFactory : public IContextFactory {
469public:
470    virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) {
471        return new AnimationContext(clock);
472    }
473};
474
475static jlong create(JNIEnv* env, jclass clazz, jlong rootNodePtr, jlong surfacePtr) {
476    RenderNode* rootNode = reinterpret_cast<RenderNode*>(rootNodePtr);
477    sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr));
478    ContextFactory factory;
479    RenderProxy* proxy = new RenderProxy(false, rootNode, &factory);
480    proxy->loadSystemProperties();
481    proxy->setSwapBehavior(kSwap_discardBuffer);
482    proxy->initialize(surface);
483    // Shadows can't be used via this interface, so just set the light source
484    // to all 0s. (and width & height are unused, TODO remove them)
485    proxy->setup(0, 0, 0, 0, 0);
486    proxy->setLightCenter((Vector3){0, 0, 0});
487    return (jlong) proxy;
488}
489
490static void setSurface(JNIEnv* env, jclass clazz, jlong rendererPtr, jlong surfacePtr) {
491    RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
492    sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr));
493    proxy->updateSurface(surface);
494}
495
496static void draw(JNIEnv* env, jclass clazz, jlong rendererPtr) {
497    RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
498    nsecs_t vsync = systemTime(CLOCK_MONOTONIC);
499    UiFrameInfoBuilder(proxy->frameInfo())
500            .setVsync(vsync, vsync)
501            .addFlag(FrameInfoFlags::SurfaceCanvas);
502    proxy->syncAndDrawFrame();
503}
504
505static void destroy(JNIEnv* env, jclass clazz, jlong rendererPtr) {
506    RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
507    delete proxy;
508}
509
510} // uirenderer
511
512// ----------------------------------------------------------------------------
513
514namespace hwui = android::uirenderer;
515
516static const JNINativeMethod gSurfaceMethods[] = {
517    {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)J",
518            (void*)nativeCreateFromSurfaceTexture },
519    {"nativeRelease", "(J)V",
520            (void*)nativeRelease },
521    {"nativeIsValid", "(J)Z",
522            (void*)nativeIsValid },
523    {"nativeIsConsumerRunningBehind", "(J)Z",
524            (void*)nativeIsConsumerRunningBehind },
525    {"nativeLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)J",
526            (void*)nativeLockCanvas },
527    {"nativeUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
528            (void*)nativeUnlockCanvasAndPost },
529    {"nativeAllocateBuffers", "(J)V",
530            (void*)nativeAllocateBuffers },
531    {"nativeCreateFromSurfaceControl", "(J)J",
532            (void*)nativeCreateFromSurfaceControl },
533    {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J",
534            (void*)nativeReadFromParcel },
535    {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
536            (void*)nativeWriteToParcel },
537    {"nativeGetWidth", "(J)I", (void*)nativeGetWidth },
538    {"nativeGetHeight", "(J)I", (void*)nativeGetHeight },
539
540    // HWUI context
541    {"nHwuiCreate", "(JJ)J", (void*) hwui::create },
542    {"nHwuiSetSurface", "(JJ)V", (void*) hwui::setSurface },
543    {"nHwuiDraw", "(J)V", (void*) hwui::draw },
544    {"nHwuiDestroy", "(J)V", (void*) hwui::destroy },
545};
546
547int register_android_view_Surface(JNIEnv* env)
548{
549    int err = RegisterMethodsOrDie(env, "android/view/Surface",
550            gSurfaceMethods, NELEM(gSurfaceMethods));
551
552    jclass clazz = FindClassOrDie(env, "android/view/Surface");
553    gSurfaceClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
554    gSurfaceClassInfo.mNativeObject = GetFieldIDOrDie(env,
555            gSurfaceClassInfo.clazz, "mNativeObject", "J");
556    gSurfaceClassInfo.mLock = GetFieldIDOrDie(env,
557            gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;");
558    gSurfaceClassInfo.ctor = GetMethodIDOrDie(env, gSurfaceClassInfo.clazz, "<init>", "(J)V");
559
560    clazz = FindClassOrDie(env, "android/graphics/Rect");
561    gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
562    gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
563    gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
564    gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
565
566    return err;
567}
568
569};
570