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

Commit 19b6bcfd authored by John Reck's avatar John Reck
Browse files

Support HardwareLayers in RenderThread

 Also has a few HardwareLayer lifecycle fixes

Change-Id: I6308cb05f8f199eed72189ace768013a46815941
parent 587f43d8
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3949,6 +3949,7 @@ public final class ActivityThread {
        ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);

        // Cleanup hardware accelerated stuff
        // TODO: Do we actually want to do this in response to all config changes?
        WindowManagerGlobal.getInstance().trimLocalMemory();

        freeTextLayoutCachesIfNeeded(configDiff);
+9 −32
Original line number Diff line number Diff line
@@ -477,17 +477,21 @@ public class GLRenderer extends HardwareRenderer {

    @Override
    void flushLayerUpdates() {
        if (validate()) {
            mGlCanvas.flushLayerUpdates();
        }
    }

    @Override
    HardwareLayer createTextureLayer() {
        validate();
        return HardwareLayer.createTextureLayer(this);
    }

    @Override
    public HardwareLayer createDisplayListLayer(int width, int height) {
        return HardwareLayer.createRenderLayer(this, width, height);
        validate();
        return HardwareLayer.createDisplayListLayer(this, width, height);
    }

    @Override
@@ -510,6 +514,9 @@ public class GLRenderer extends HardwareRenderer {

    @Override
    boolean copyLayerInto(HardwareLayer layer, Bitmap bitmap) {
        if (!validate()) {
            throw new IllegalStateException("Could not acquire hardware rendering context");
        }
        layer.flushChanges();
        return GLES20Canvas.nCopyLayer(layer.getLayer(), bitmap.mNativeBitmap);
    }
@@ -537,35 +544,6 @@ public class GLRenderer extends HardwareRenderer {
        return true;
    }

    @Override
    void destroyLayers(final View view) {
        if (view != null) {
            safelyRun(new Runnable() {
                @Override
                public void run() {
                    if (mCanvas != null) {
                        mCanvas.clearLayerUpdates();
                    }
                    destroyHardwareLayer(view);
                    GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_LAYERS);
                }
            });
        }
    }

    private static void destroyHardwareLayer(View view) {
        view.destroyLayer(true);

        if (view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) view;

            int count = group.getChildCount();
            for (int i = 0; i < count; i++) {
                destroyHardwareLayer(group.getChildAt(i));
            }
        }
    }

    @Override
    void destroyHardwareResources(final View view) {
        if (view != null) {
@@ -1069,7 +1047,6 @@ public class GLRenderer extends HardwareRenderer {
        return true;
    }

    @Override
    boolean validate() {
        return checkRenderContext() != SURFACE_STATE_ERROR;
    }
+26 −6
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ import android.graphics.SurfaceTexture;
 */
final class HardwareLayer {
    private static final int LAYER_TYPE_TEXTURE = 1;
    private static final int LAYER_TYPE_RENDER = 2;
    private static final int LAYER_TYPE_DISPLAY_LIST = 2;

    private HardwareRenderer mRenderer;
    private Finalizer mFinalizer;
@@ -99,6 +99,10 @@ final class HardwareLayer {
        doDestroyLayerUpdater();
    }

    public long getDeferredLayerUpdater() {
        return mFinalizer.mDeferredUpdater;
    }

    /**
     * Destroys the deferred layer updater but not the backing layer. The
     * backing layer is instead returned and is the caller's responsibility
@@ -120,7 +124,7 @@ final class HardwareLayer {
    }

    public DisplayList startRecording() {
        assertType(LAYER_TYPE_RENDER);
        assertType(LAYER_TYPE_DISPLAY_LIST);

        if (mDisplayList == null) {
            mDisplayList = DisplayList.create("HardwareLayer");
@@ -172,10 +176,18 @@ final class HardwareLayer {
    /**
     * Indicates that this layer has lost its texture.
     */
    public void onTextureDestroyed() {
    public void detachSurfaceTexture(final SurfaceTexture surface) {
        assertType(LAYER_TYPE_TEXTURE);
        mRenderer.safelyRun(new Runnable() {
            @Override
            public void run() {
                surface.detachFromGLContext();
                // SurfaceTexture owns the texture name and detachFromGLContext
                // should have deleted it
                nOnTextureDestroyed(mFinalizer.mDeferredUpdater);
            }
        });
    }

    /**
     * This exists to minimize impact into the current HardwareLayer paths as
@@ -226,12 +238,20 @@ final class HardwareLayer {
        return new HardwareLayer(renderer, nCreateTextureLayer(), LAYER_TYPE_TEXTURE);
    }

    static HardwareLayer adoptTextureLayer(HardwareRenderer renderer, long layer) {
        return new HardwareLayer(renderer, layer, LAYER_TYPE_TEXTURE);
    }

    /**
     * This should only be used by HardwareRenderer! Do not call directly
     */
    static HardwareLayer createRenderLayer(HardwareRenderer renderer,
    static HardwareLayer createDisplayListLayer(HardwareRenderer renderer,
            int width, int height) {
        return new HardwareLayer(renderer, nCreateRenderLayer(width, height), LAYER_TYPE_RENDER);
        return new HardwareLayer(renderer, nCreateRenderLayer(width, height), LAYER_TYPE_DISPLAY_LIST);
    }

    static HardwareLayer adoptDisplayListLayer(HardwareRenderer renderer, long layer) {
        return new HardwareLayer(renderer, layer, LAYER_TYPE_DISPLAY_LIST);
    }

    /** This also creates the underlying layer */
+0 −16
Original line number Diff line number Diff line
@@ -233,13 +233,6 @@ public abstract class HardwareRenderer {
     */
    abstract void updateSurface(Surface surface) throws OutOfResourcesException;

    /**
     * Destroys the layers used by the specified view hierarchy.
     *
     * @param view The root of the view hierarchy
     */
    abstract void destroyLayers(View view);

    /**
     * Destroys all hardware rendering resources associated with the specified
     * view hierarchy.
@@ -256,15 +249,6 @@ public abstract class HardwareRenderer {
     */
    abstract void invalidate(Surface surface);

    /**
     * This method should be invoked to ensure the hardware renderer is in
     * valid state (for instance, to ensure the correct EGL context is bound
     * to the current thread.)
     *
     * @return true if the renderer is now valid, false otherwise
     */
    abstract boolean validate();

    /**
     * This method ensures the hardware renderer is in a valid state
     * before executing the specified action.
+2 −24
Original line number Diff line number Diff line
@@ -231,26 +231,12 @@ public class TextureView extends View {
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mLayer != null) {
            boolean success = executeHardwareAction(new Runnable() {
                @Override
                public void run() {
        destroySurface();
    }
            });

            if (!success) {
                Log.w(LOG_TAG, "TextureView was not able to destroy its surface: " + this);
            }
        }
    }

    private void destroySurface() {
        if (mLayer != null) {
            mSurface.detachFromGLContext();
            // SurfaceTexture owns the texture name and detachFromGLContext
            // should have deleted it
            mLayer.onTextureDestroyed();
            mLayer.detachSurfaceTexture(mSurface);

            boolean shouldRelease = true;
            if (mListener != null) {
@@ -608,14 +594,6 @@ public class TextureView extends View {
     */
    public Bitmap getBitmap(Bitmap bitmap) {
        if (bitmap != null && isAvailable()) {
            AttachInfo info = mAttachInfo;
            if (info != null && info.mHardwareRenderer != null &&
                    info.mHardwareRenderer.isEnabled()) {
                if (!info.mHardwareRenderer.validate()) {
                    throw new IllegalStateException("Could not acquire hardware rendering context");
                }
            }

            applyUpdate();
            applyTransformMatrix();

Loading