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

Commit 7c5646f1 authored by John Reck's avatar John Reck
Browse files

Adjust crop to account for child scaling

The computed crop is in view-relative domain, but we need
it to be in surface-relative domain since that's what
surfaceflinger is speaking. So adjust the crop by the scale
from surfaceWidth/Height to the RenderNode's width/height

Bug: 298621623
Flag: com.android.graphics.hwui.flags.clip_surfaceviews
Test: HwAccelerationTest demo & atest CtsUiRenderingTestCases
Change-Id: I9d5908c2ca2c6b6331c8f35f658783ab3b2fe6f1
parent a8ef53cc
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -3455,15 +3455,15 @@ public final class SurfaceControl implements Parcelable {
         * @return this This transaction for chaining
         * @hide
         */
        public @NonNull Transaction setCrop(@NonNull SurfaceControl sc, float top, float left,
                float bottom, float right) {
        public @NonNull Transaction setCrop(@NonNull SurfaceControl sc, float left, float top,
                float right, float bottom) {
            checkPreconditions(sc);
            if (SurfaceControlRegistry.sCallStackDebuggingEnabled) {
                SurfaceControlRegistry.getProcessInstance().checkCallStackDebugging(
                        "setCrop", this, sc, "crop={" + top + ", " + left + ", " +
                        bottom + ", " + right + "}");
            }
            nativeSetCrop(mNativeObject, sc.mNativeObject, top, left, bottom, right);
            nativeSetCrop(mNativeObject, sc.mNativeObject, left, top, right, bottom);
            return this;
        }

+26 −8
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.RenderNode;
import android.hardware.input.InputManager;
@@ -1666,7 +1667,7 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall
    }

    private final Rect mRTLastReportedPosition = new Rect();
    private final Rect mRTLastSetCrop = new Rect();
    private final RectF mRTLastSetCrop = new RectF();

    private class SurfaceViewPositionUpdateListener implements RenderNode.PositionUpdateListener {
        private final int mRtSurfaceWidth;
@@ -1711,16 +1712,18 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall

        @Override
        public void positionChanged(long frameNumber, int left, int top, int right, int bottom,
                int clipLeft, int clipTop, int clipRight, int clipBottom) {
                int clipLeft, int clipTop, int clipRight, int clipBottom,
                int nodeWidth, int nodeHeight) {
            try {
                if (DEBUG_POSITION) {
                    Log.d(TAG, String.format(
                            "%d updateSurfacePosition RenderWorker, frameNr = %d, "
                                    + "position = [%d, %d, %d, %d] clip = [%d, %d, %d, %d] "
                                    + "surfaceSize = %dx%d",
                                    + "surfaceSize = %dx%d renderNodeSize = %d%d",
                            System.identityHashCode(SurfaceView.this), frameNumber,
                            left, top, right, bottom, clipLeft, clipTop, clipRight, clipBottom,
                            mRtSurfaceWidth, mRtSurfaceHeight));
                            mRtSurfaceWidth, mRtSurfaceHeight,
                            nodeWidth, nodeHeight));
                }
                synchronized (mSurfaceControlLock) {
                    if (mSurfaceControl == null) return;
@@ -1735,14 +1738,29 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall
                            mRTLastReportedPosition.top /*positionTop*/,
                            postScaleX, postScaleY);

                    mRTLastSetCrop.set(clipLeft, clipTop, clipRight, clipBottom);
                    // The computed crop is in view-relative dimensions, however we need it to be
                    // in buffer-relative dimensions. So scale the crop by the ratio between
                    // the view's unscaled width/height (nodeWidth/Height), and the surface's
                    // width/height
                    // That is, if the Surface has a fixed size of 50x50, the SurfaceView is laid
                    // out to a size of 100x100, and the SurfaceView is ultimately scaled to
                    // 1000x1000, then we need to scale the crop by just the 2x from surface
                    // domain to SV domain.
                    final float surfaceToNodeScaleX = (float) mRtSurfaceWidth / (float) nodeWidth;
                    final float surfaceToNodeScaleY = (float) mRtSurfaceHeight / (float) nodeHeight;
                    mRTLastSetCrop.set(clipLeft * surfaceToNodeScaleX,
                            clipTop * surfaceToNodeScaleY,
                            clipRight * surfaceToNodeScaleX,
                            clipBottom * surfaceToNodeScaleY);

                    if (DEBUG_POSITION) {
                        Log.d(TAG, String.format("Setting layer crop = [%d, %d, %d, %d] "
                        Log.d(TAG, String.format("Setting layer crop = [%f, %f, %f, %f] "
                                        + "from scale %f, %f", mRTLastSetCrop.left,
                                mRTLastSetCrop.top, mRTLastSetCrop.right, mRTLastSetCrop.bottom,
                                postScaleX, postScaleY));
                                surfaceToNodeScaleX, surfaceToNodeScaleY));
                    }
                    mPositionChangedTransaction.setCrop(mSurfaceControl, mRTLastSetCrop);
                    mPositionChangedTransaction.setCrop(mSurfaceControl, mRTLastSetCrop.left,
                            mRTLastSetCrop.top, mRTLastSetCrop.right, mRTLastSetCrop.bottom);
                    if (mRTLastSetCrop.isEmpty()) {
                        mPositionChangedTransaction.hide(mSurfaceControl);
                    } else {
+8 −5
Original line number Diff line number Diff line
@@ -279,7 +279,8 @@ public final class RenderNode {
         * @hide
         */
        default void positionChanged(long frameNumber, int left, int top, int right, int bottom,
                int clipLeft, int clipTop, int clipRight, int clipBottom) {
                int clipLeft, int clipTop, int clipRight, int clipBottom,
                int nodeWidth, int nodeHeight) {
            positionChanged(frameNumber, left, top, right, bottom);
        }

@@ -304,11 +305,12 @@ public final class RenderNode {
         * @hide */
        static boolean callPositionChanged2(WeakReference<PositionUpdateListener> weakListener,
                long frameNumber, int left, int top, int right, int bottom,
                int clipLeft, int clipTop, int clipRight, int clipBottom) {
                int clipLeft, int clipTop, int clipRight, int clipBottom,
                int nodeWidth, int nodeHeight) {
            final PositionUpdateListener listener = weakListener.get();
            if (listener != null) {
                listener.positionChanged(frameNumber, left, top, right, bottom, clipLeft,
                        clipTop, clipRight, clipBottom);
                        clipTop, clipRight, clipBottom, nodeWidth, nodeHeight);
                return true;
            } else {
                return false;
@@ -401,10 +403,11 @@ public final class RenderNode {

        @Override
        public void positionChanged(long frameNumber, int left, int top, int right, int bottom,
                int clipLeft, int clipTop, int clipRight, int clipBottom) {
                int clipLeft, int clipTop, int clipRight, int clipBottom,
                int nodeWidth, int nodeHeight) {
            for (PositionUpdateListener pul : mListeners) {
                pul.positionChanged(frameNumber, left, top, right, bottom, clipLeft, clipTop,
                        clipRight, clipBottom);
                        clipRight, clipBottom, nodeWidth, nodeHeight);
            }
        }

+5 −5
Original line number Diff line number Diff line
@@ -593,9 +593,9 @@ static void android_view_RenderNode_requestPositionUpdates(JNIEnv* env, jobject,

            Matrix4 transform;
            SkIRect clipBounds;
            if (enableClip) {
            uirenderer::Rect initialClipBounds;
            const auto clipFlags = props.getClippingFlags();
            if (enableClip) {
                if (clipFlags) {
                    props.getClippingRectForFlags(clipFlags, &initialClipBounds);
                } else {
@@ -659,8 +659,8 @@ static void android_view_RenderNode_requestPositionUpdates(JNIEnv* env, jobject,
                        static_cast<jint>(bounds.left), static_cast<jint>(bounds.top),
                        static_cast<jint>(bounds.right), static_cast<jint>(bounds.bottom),
                        static_cast<jint>(clipBounds.fLeft), static_cast<jint>(clipBounds.fTop),
                        static_cast<jint>(clipBounds.fRight),
                        static_cast<jint>(clipBounds.fBottom));
                        static_cast<jint>(clipBounds.fRight), static_cast<jint>(clipBounds.fBottom),
                        static_cast<jint>(props.getWidth()), static_cast<jint>(props.getHeight()));
            }
            if (!keepListening) {
                env->DeleteGlobalRef(mListener);
@@ -891,7 +891,7 @@ int register_android_view_RenderNode(JNIEnv* env) {
    gPositionListener.callPositionChanged = GetStaticMethodIDOrDie(
            env, clazz, "callPositionChanged", "(Ljava/lang/ref/WeakReference;JIIII)Z");
    gPositionListener.callPositionChanged2 = GetStaticMethodIDOrDie(
            env, clazz, "callPositionChanged2", "(Ljava/lang/ref/WeakReference;JIIIIIIII)Z");
            env, clazz, "callPositionChanged2", "(Ljava/lang/ref/WeakReference;JIIIIIIIIII)Z");
    gPositionListener.callApplyStretch = GetStaticMethodIDOrDie(
            env, clazz, "callApplyStretch", "(Ljava/lang/ref/WeakReference;JFFFFFFFFFF)Z");
    gPositionListener.callPositionLost = GetStaticMethodIDOrDie(
+19 −1
Original line number Diff line number Diff line
@@ -409,6 +409,24 @@
            </intent-filter>
        </activity>

        <activity android:name="ScrollingZAboveSurfaceView"
            android:label="SurfaceView/Z-Above scrolling"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="com.android.test.hwui.TEST"/>
            </intent-filter>
        </activity>

        <activity android:name="ScrollingZAboveScaledSurfaceView"
            android:label="SurfaceView/Z-Above scrolling, scaled surface"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="com.android.test.hwui.TEST"/>
            </intent-filter>
        </activity>

        <activity android:name="StretchySurfaceViewActivity"
                  android:label="SurfaceView/Stretchy Movement"
                  android:exported="true">
Loading