Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 52a9a10b authored by Mathias Agopian's avatar Mathias Agopian
Browse files

Clearly separate consumer and producer interfaces

Bug: 9265647
Change-Id: Ic68e91788d0a05251e1d2fb9f9d4de403c7099bf
parent 0dff7064
Loading
Loading
Loading
Loading
+3 −6
Original line number Diff line number Diff line
@@ -322,7 +322,7 @@ public class TextureView extends View {
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mSurface != null) {
            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
            mSurface.setDefaultBufferSize(getWidth(), getHeight());
            updateLayer();
            if (mListener != null) {
                mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight());
@@ -362,7 +362,7 @@ public class TextureView extends View {
                // Create a new SurfaceTexture for the layer.
                mSurface = mAttachInfo.mHardwareRenderer.createSurfaceTexture(mLayer);
            }
            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
            mSurface.setDefaultBufferSize(getWidth(), getHeight());
            nCreateNativeWindow(mSurface);

            mUpdateListener = new SurfaceTexture.OnFrameAvailableListener() {
@@ -399,7 +399,7 @@ public class TextureView extends View {
            mMatrixChanged = true;

            mAttachInfo.mHardwareRenderer.setSurfaceTexture(mLayer, mSurface);
            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
            mSurface.setDefaultBufferSize(getWidth(), getHeight());
        }

        applyUpdate();
@@ -816,9 +816,6 @@ public class TextureView extends View {
    private native void nCreateNativeWindow(SurfaceTexture surface);
    private native void nDestroyNativeWindow();

    private static native void nSetDefaultBufferSize(SurfaceTexture surfaceTexture,
            int width, int height);

    private static native boolean nLockCanvas(int nativeWindow, Canvas canvas, Rect dirty);
    private static native void nUnlockCanvasAndPost(int nativeWindow, Canvas canvas);
}
+39 −12
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ const char* const kSurfaceTextureClassPathName = "android/graphics/SurfaceTextur

struct fields_t {
    jfieldID  surfaceTexture;
    jfieldID  bufferQueue;
    jfieldID  frameAvailableListener;
    jmethodID postEvent;
};
@@ -61,6 +62,20 @@ static void SurfaceTexture_setSurfaceTexture(JNIEnv* env, jobject thiz,
    env->SetIntField(thiz, fields.surfaceTexture, (int)surfaceTexture.get());
}

static void SurfaceTexture_setBufferQueue(JNIEnv* env, jobject thiz,
        const sp<BufferQueue>& bq)
{
    BufferQueue* const p =
        (BufferQueue*)env->GetIntField(thiz, fields.bufferQueue);
    if (bq.get()) {
        bq->incStrong((void*)SurfaceTexture_setBufferQueue);
    }
    if (p) {
        p->decStrong((void*)SurfaceTexture_setBufferQueue);
    }
    env->SetIntField(thiz, fields.bufferQueue, (int)bq.get());
}

static void SurfaceTexture_setFrameAvailableListener(JNIEnv* env,
        jobject thiz, sp<GLConsumer::FrameAvailableListener> listener)
{
@@ -76,23 +91,22 @@ static void SurfaceTexture_setFrameAvailableListener(JNIEnv* env,
    env->SetIntField(thiz, fields.frameAvailableListener, (int)listener.get());
}

sp<GLConsumer> SurfaceTexture_getSurfaceTexture(JNIEnv* env,
        jobject thiz)
{
sp<GLConsumer> SurfaceTexture_getSurfaceTexture(JNIEnv* env, jobject thiz) {
    return (GLConsumer*)env->GetIntField(thiz, fields.surfaceTexture);
}

sp<ANativeWindow> android_SurfaceTexture_getNativeWindow(
        JNIEnv* env, jobject thiz)
{
sp<IGraphicBufferProducer> SurfaceTexture_getProducer(JNIEnv* env, jobject thiz) {
    return (BufferQueue*)env->GetIntField(thiz, fields.bufferQueue);
}

sp<ANativeWindow> android_SurfaceTexture_getNativeWindow(JNIEnv* env, jobject thiz) {
    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
    sp<Surface> surfaceTextureClient(surfaceTexture != NULL ?
            new Surface(surfaceTexture->getBufferQueue()) : NULL);
    sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, thiz));
    sp<Surface> surfaceTextureClient(surfaceTexture != NULL ? new Surface(producer) : NULL);
    return surfaceTextureClient;
}

bool android_SurfaceTexture_isInstanceOf(JNIEnv* env, jobject thiz)
{
bool android_SurfaceTexture_isInstanceOf(JNIEnv* env, jobject thiz) {
    jclass surfaceTextureClass = env->FindClass(kSurfaceTextureClassPathName);
    return env->IsInstanceOf(thiz, surfaceTextureClass);
}
@@ -175,6 +189,12 @@ void JNISurfaceTextureContext::onFrameAvailable()

// ----------------------------------------------------------------------------


#define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture"
#define ANDROID_GRAPHICS_BUFFERQUEUE_JNI_ID "mBufferQueue"
#define ANDROID_GRAPHICS_FRAMEAVAILABLELISTENER_JNI_ID \
                                         "mFrameAvailableListener"

static void SurfaceTexture_classInit(JNIEnv* env, jclass clazz)
{
    fields.surfaceTexture = env->GetFieldID(clazz,
@@ -183,6 +203,12 @@ static void SurfaceTexture_classInit(JNIEnv* env, jclass clazz)
        ALOGE("can't find android/graphics/SurfaceTexture.%s",
                ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID);
    }
    fields.bufferQueue = env->GetFieldID(clazz,
            ANDROID_GRAPHICS_BUFFERQUEUE_JNI_ID, "I");
    if (fields.bufferQueue == NULL) {
        ALOGE("can't find android/graphics/SurfaceTexture.%s",
                ANDROID_GRAPHICS_BUFFERQUEUE_JNI_ID);
    }
    fields.frameAvailableListener = env->GetFieldID(clazz,
            ANDROID_GRAPHICS_FRAMEAVAILABLELISTENER_JNI_ID, "I");
    if (fields.frameAvailableListener == NULL) {
@@ -214,6 +240,7 @@ static void SurfaceTexture_init(JNIEnv* env, jobject thiz,
        return;
    }
    SurfaceTexture_setSurfaceTexture(env, thiz, surfaceTexture);
    SurfaceTexture_setBufferQueue(env, thiz, bq);

    jclass clazz = env->GetObjectClass(thiz);
    if (clazz == NULL) {
@@ -234,11 +261,11 @@ static void SurfaceTexture_finalize(JNIEnv* env, jobject thiz)
    surfaceTexture->setFrameAvailableListener(0);
    SurfaceTexture_setFrameAvailableListener(env, thiz, 0);
    SurfaceTexture_setSurfaceTexture(env, thiz, 0);
    SurfaceTexture_setBufferQueue(env, thiz, 0);
}

static void SurfaceTexture_setDefaultBufferSize(
        JNIEnv* env, jobject thiz, jint width, jint height)
{
        JNIEnv* env, jobject thiz, jint width, jint height) {
    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
    surfaceTexture->setDefaultBufferSize(width, height);
}
+4 −8
Original line number Diff line number Diff line
@@ -565,14 +565,10 @@ static void android_hardware_Camera_setPreviewTexture(JNIEnv *env,
    sp<Camera> camera = get_native_camera(env, thiz, NULL);
    if (camera == 0) return;

    sp<BufferQueue> bufferQueue = NULL;
    sp<IGraphicBufferProducer> producer = NULL;
    if (jSurfaceTexture != NULL) {
        sp<GLConsumer> surfaceTexture =
            SurfaceTexture_getSurfaceTexture(env, jSurfaceTexture);
        if (surfaceTexture != NULL) {
            bufferQueue = surfaceTexture->getBufferQueue();
        }
        else {
        producer = SurfaceTexture_getProducer(env, jSurfaceTexture);
        if (producer == NULL) {
            jniThrowException(env, "java/lang/IllegalArgumentException",
                    "SurfaceTexture already released in setPreviewTexture");
            return;
@@ -580,7 +576,7 @@ static void android_hardware_Camera_setPreviewTexture(JNIEnv *env,

    }

    if (camera->setPreviewTexture(bufferQueue) != NO_ERROR) {
    if (camera->setPreviewTexture(producer) != NO_ERROR) {
        jniThrowException(env, "java/io/IOException",
                "setPreviewTexture failed");
    }
+4 −4
Original line number Diff line number Diff line
@@ -604,7 +604,7 @@ android_eglCreateWindowSurfaceTexture
    jint _remaining;
    EGLint *attrib_list = (EGLint *) 0;
    android::sp<ANativeWindow> window;
    android::sp<android::GLConsumer> glConsumer;
    android::sp<android::IGraphicBufferProducer> producer;

    if (!attrib_list_ref) {
        _exception = 1;
@@ -625,12 +625,12 @@ not_valid_surface:
        _exceptionMessage = "Make sure the SurfaceView or associated SurfaceHolder has a valid Surface";
        goto exit;
    }
    glConsumer = android::SurfaceTexture_getSurfaceTexture(_env, win);
    producer = android::SurfaceTexture_getProducer(_env, win);

    if (glConsumer == NULL)
    if (producer == NULL)
        goto not_valid_surface;

    window = new android::Surface(glConsumer->getBufferQueue());
    window = new android::Surface(producer);

    if (window == NULL)
        goto not_valid_surface;
+3 −4
Original line number Diff line number Diff line
@@ -135,15 +135,14 @@ static inline bool isSurfaceValid(const sp<Surface>& sur) {

static jint nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz,
        jobject surfaceTextureObj) {
    sp<GLConsumer> st(SurfaceTexture_getSurfaceTexture(env, surfaceTextureObj));
    if (st == NULL) {
    sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surfaceTextureObj));
    if (producer == NULL) {
        jniThrowException(env, "java/lang/IllegalArgumentException",
                "SurfaceTexture has already been released");
        return 0;
    }

    sp<IGraphicBufferProducer> bq = st->getBufferQueue();
    sp<Surface> surface(new Surface(bq, true));
    sp<Surface> surface(new Surface(producer, true));
    if (surface == NULL) {
        jniThrowException(env, OutOfResourcesException, NULL);
        return 0;
Loading