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

Commit 335a6625 authored by Jamie Gennis's avatar Jamie Gennis Committed by Android (Google) Code Review
Browse files

Merge changes Iac9cc917,I8eed4b0d

* changes:
  TextureView: add setSurfaceTexture method
  SurfaceTexture: add GL context attach & detach
parents aca9ef4d 2af3524b
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -42,6 +42,12 @@ class GLES20TextureLayer extends GLES20Layer {
        }        
    }

    GLES20TextureLayer(SurfaceTexture surface, boolean isOpaque) {
        this(isOpaque);
        mSurface = surface;
        mSurface.attachToGLContext(mTexture);
    }

    @Override
    boolean isValid() {
        return mLayer != 0 && mTexture != 0;
@@ -72,6 +78,14 @@ class GLES20TextureLayer extends GLES20Layer {
        return mSurface;
    }

    void setSurfaceTexture(SurfaceTexture surfaceTexture) {
        if (mSurface != null) {
            mSurface.release();
        }
        mSurface = surfaceTexture;
        mSurface.attachToGLContext(mTexture);
    }

    @Override
    void update(int width, int height, boolean isOpaque) {
        super.update(width, height, isOpaque);
+16 −2
Original line number Diff line number Diff line
@@ -416,6 +416,15 @@ public abstract class HardwareRenderer {
     */
    abstract SurfaceTexture createSurfaceTexture(HardwareLayer layer);

    /**
     * Sets the {@link android.graphics.SurfaceTexture} that will be used to
     * render into the specified hardware layer.
     *
     * @param layer The layer to render into using a {@link android.graphics.SurfaceTexture}
     * @param surfaceTexture The {@link android.graphics.SurfaceTexture} to use for the layer
     */
    abstract void setSurfaceTexture(HardwareLayer layer, SurfaceTexture surfaceTexture);

    /**
     * Initializes the hardware renderer for the specified surface and setup the
     * renderer for drawing, if needed. This is invoked when the ViewAncestor has
@@ -1344,6 +1353,11 @@ public abstract class HardwareRenderer {
            return ((GLES20TextureLayer) layer).getSurfaceTexture();
        }

        @Override
        void setSurfaceTexture(HardwareLayer layer, SurfaceTexture surfaceTexture) {
            ((GLES20TextureLayer) layer).setSurfaceTexture(surfaceTexture);
        }

        @Override
        void destroyLayers(View view) {
            if (view != null && isEnabled() && checkCurrent() != SURFACE_STATE_ERROR) {
+45 −3
Original line number Diff line number Diff line
@@ -115,6 +115,7 @@ public class TextureView extends View {

    private final Object[] mLock = new Object[0];
    private boolean mUpdateLayer;
    private boolean mUpdateSurface;

    private SurfaceTexture.OnFrameAvailableListener mUpdateListener;

@@ -208,6 +209,8 @@ public class TextureView extends View {

    private void destroySurface() {
        if (mLayer != null) {
            mSurface.detachFromGLContext();

            boolean shouldRelease = true;
            if (mListener != null) {
                shouldRelease = mListener.onSurfaceTextureDestroyed(mSurface);
@@ -322,7 +325,11 @@ public class TextureView extends View {
            }

            mLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(mOpaque);
            if (!mUpdateSurface) {
                // We already have a SurfaceTexture to use, and we will pass it
                // to mLayer below.
                mSurface = mAttachInfo.mHardwareRenderer.createSurfaceTexture(mLayer);
            }
            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
            nCreateNativeWindow(mSurface);

@@ -344,6 +351,15 @@ public class TextureView extends View {
            }
        }

        if (mUpdateSurface) {
            // Someone has requested that we use a specific SurfaceTexture, so
            // tell mLayer about it and set the SurfaceTexture to use the
            // current view size.
            mUpdateSurface = false;
            mAttachInfo.mHardwareRenderer.setSurfaceTexture(mLayer, mSurface);
            nSetDefaultBufferSize(mSurface, getWidth(), getHeight());
        }

        applyUpdate();
        applyTransformMatrix();

@@ -635,6 +651,32 @@ public class TextureView extends View {
        return mSurface;
    }

    /**
     * Set the {@link SurfaceTexture} for this view to use. If a {@link
     * SurfaceTexture} is already being used by this view, it is immediately
     * released and not be usable any more.  The {@link
     * SurfaceTextureListener#onSurfaceTextureDestroyed} callback is <b>not</b>
     * called.
     *
     * The {@link SurfaceTexture} object must be detached from all OpenGL ES
     * contexts prior to calling this method.
     *
     * @param surfaceTexture The {@link SurfaceTexture} that the view should use.
     * @see SurfaceTexture#detachFromGLContext()
     * @hide
     */
    public void setSurfaceTexture(SurfaceTexture surfaceTexture) {
        if (surfaceTexture == null) {
            throw new NullPointerException("surfaceTexture must not be null");
        }
        if (mSurface != null) {
            mSurface.release();
        }
        mSurface = surfaceTexture;
        mUpdateSurface = true;
        invalidateParentIfNeeded();
    }

    /**
     * Returns the {@link SurfaceTextureListener} currently associated with this
     * texture view.
+21 −7
Original line number Diff line number Diff line
@@ -218,6 +218,18 @@ static jint SurfaceTexture_updateTexImage(JNIEnv* env, jobject thiz)
    return surfaceTexture->updateTexImage();
}

static jint SurfaceTexture_detachFromGLContext(JNIEnv* env, jobject thiz)
{
    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
    return surfaceTexture->detachFromContext();
}

static jint SurfaceTexture_attachToGLContext(JNIEnv* env, jobject thiz, jint tex)
{
    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
    return surfaceTexture->attachToContext((GLuint)tex);
}

static void SurfaceTexture_getTransformMatrix(JNIEnv* env, jobject thiz,
        jfloatArray jmtx)
{
@@ -247,6 +259,8 @@ static JNINativeMethod gSurfaceTextureMethods[] = {
    {"nativeFinalize",             "()V",   (void*)SurfaceTexture_finalize },
    {"nativeSetDefaultBufferSize", "(II)V", (void*)SurfaceTexture_setDefaultBufferSize },
    {"nativeUpdateTexImage",       "()I",   (void*)SurfaceTexture_updateTexImage },
    {"nativeDetachFromGLContext",  "()I",   (void*)SurfaceTexture_detachFromGLContext },
    {"nativeAttachToGLContext",    "(I)I",   (void*)SurfaceTexture_attachToGLContext },
    {"nativeGetTransformMatrix",   "([F)V", (void*)SurfaceTexture_getTransformMatrix },
    {"nativeGetTimestamp",         "()J",   (void*)SurfaceTexture_getTimestamp },
    {"nativeRelease",              "()V",   (void*)SurfaceTexture_release },
+27 −1
Original line number Diff line number Diff line
@@ -161,7 +161,31 @@ public class SurfaceTexture {
    public void updateTexImage() {
        int err = nativeUpdateTexImage(); 
        if (err != 0) {
            throw new RuntimeException("Error during updateTexImage (see logs)");
            throw new RuntimeException("Error during updateTexImage (see logcat for details)");
        }
    }

    /**
     * Detach the SurfaceTexture from the OpenGL ES context with which it is currently associated.
     * This can be used to change from one OpenGL ES context to another.
     *
     * @hide
     */
    public void detachFromGLContext() {
        int err = nativeDetachFromGLContext();
        if (err != 0) {
            throw new RuntimeException("Error during detachFromGLContext (see logcat for details)");
        }
    }

    /**
     *
     * @hide
     */
    public void attachToGLContext(int texName) {
        int err = nativeAttachToGLContext(texName);
        if (err != 0) {
            throw new RuntimeException("Error during detachFromGLContext (see logcat for details)");
        }
    }

@@ -269,6 +293,8 @@ public class SurfaceTexture {
    private native long nativeGetTimestamp();
    private native void nativeSetDefaultBufferSize(int width, int height);
    private native int nativeUpdateTexImage();
    private native int nativeDetachFromGLContext();
    private native int nativeAttachToGLContext(int texName);
    private native int nativeGetQueuedCount();
    private native void nativeRelease();