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

Commit 3d43d701 authored by Riddle Hsu's avatar Riddle Hsu Committed by Android (Google) Code Review
Browse files

Merge "Clean up some rotation utility methods"

parents 35feccef 3da3dc28
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@

package com.android.server.display;

import static com.android.server.wm.utils.RotationAnimationUtils.hasProtectedContent;
import static com.android.internal.policy.TransitionAnimation.hasProtectedContent;

import android.content.Context;
import android.graphics.BLASTBufferQueue;
+2 −3
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import static com.android.server.wm.ScreenRotationAnimationProto.STARTED;
import static com.android.server.wm.SurfaceAnimator.ANIMATION_TYPE_SCREEN_ROTATION;
import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
import static com.android.server.wm.utils.CoordinateTransforms.computeRotationMatrix;

import android.animation.ArgbEvaluator;
import android.content.Context;
@@ -60,7 +61,6 @@ import com.android.internal.protolog.common.ProtoLog;
import com.android.server.display.DisplayControl;
import com.android.server.wm.SurfaceAnimator.AnimationType;
import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback;
import com.android.server.wm.utils.RotationAnimationUtils;

import java.io.PrintWriter;

@@ -378,8 +378,7 @@ class ScreenRotationAnimation {
        // to the snapshot to make it stay in the same original position
        // with the current screen rotation.
        int delta = deltaRotation(rotation, mOriginalRotation);
        RotationAnimationUtils.createRotationMatrix(delta, mOriginalWidth, mOriginalHeight,
                mSnapshotInitialMatrix);
        computeRotationMatrix(delta, mOriginalWidth, mOriginalHeight, mSnapshotInitialMatrix);
        setRotationTransform(t, mSnapshotInitialMatrix);
    }

+19 −16
Original line number Diff line number Diff line
@@ -22,11 +22,9 @@ import static android.view.Surface.ROTATION_270;
import static android.view.Surface.ROTATION_90;

import android.annotation.Dimension;
import android.annotation.Nullable;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.DisplayInfo;
import android.view.Surface;
import android.view.Surface.Rotation;

public class CoordinateTransforms {
@@ -137,19 +135,24 @@ public class CoordinateTransforms {
        out.postConcat(tmp);
    }

    /**
     * Transforms a rect using a transformation matrix
     *
     * @param transform the transformation to apply to the rect
     * @param inOutRect the rect to transform
     * @param tmp a temporary value, if null the function will allocate its own.
     */
    public static void transformRect(Matrix transform, Rect inOutRect, @Nullable RectF tmp) {
        if (tmp == null) {
            tmp = new RectF();
    /** Computes the matrix that rotates the original w x h by the rotation delta. */
    public static void computeRotationMatrix(int rotationDelta, int w, int h, Matrix outMatrix) {
        switch (rotationDelta) {
            case Surface.ROTATION_0:
                outMatrix.reset();
                break;
            case Surface.ROTATION_90:
                outMatrix.setRotate(90);
                outMatrix.postTranslate(h, 0);
                break;
            case Surface.ROTATION_180:
                outMatrix.setRotate(180);
                outMatrix.postTranslate(w, h);
                break;
            case Surface.ROTATION_270:
                outMatrix.setRotate(270);
                outMatrix.postTranslate(0, w);
                break;
        }
        tmp.set(inOutRect);
        transform.mapRect(tmp);
        inOutRect.set((int) tmp.left, (int) tmp.top, (int) tmp.right, (int) tmp.bottom);
    }
}
+0 −55
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wm.utils;

import static android.hardware.HardwareBuffer.USAGE_PROTECTED_CONTENT;

import android.graphics.Matrix;
import android.hardware.HardwareBuffer;
import android.view.Surface;


/** Helper functions for the {@link com.android.server.wm.ScreenRotationAnimation} class*/
public class RotationAnimationUtils {

    /**
     * @return whether the hardwareBuffer passed in is marked as protected.
     */
    public static boolean hasProtectedContent(HardwareBuffer hardwareBuffer) {
        return (hardwareBuffer.getUsage() & USAGE_PROTECTED_CONTENT) == USAGE_PROTECTED_CONTENT;
    }

    public static void createRotationMatrix(int rotation, int width, int height, Matrix outMatrix) {
        switch (rotation) {
            case Surface.ROTATION_0:
                outMatrix.reset();
                break;
            case Surface.ROTATION_90:
                outMatrix.setRotate(90, 0, 0);
                outMatrix.postTranslate(height, 0);
                break;
            case Surface.ROTATION_180:
                outMatrix.setRotate(180, 0, 0);
                outMatrix.postTranslate(width, height);
                break;
            case Surface.ROTATION_270:
                outMatrix.setRotate(270, 0, 0);
                outMatrix.postTranslate(0, width);
                break;
        }
    }
}
+39 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static android.view.Surface.ROTATION_180;
import static android.view.Surface.ROTATION_270;
import static android.view.Surface.ROTATION_90;

import static com.android.server.wm.utils.CoordinateTransforms.computeRotationMatrix;
import static com.android.server.wm.utils.CoordinateTransforms.transformLogicalToPhysicalCoordinates;
import static com.android.server.wm.utils.CoordinateTransforms.transformPhysicalToLogicalCoordinates;
import static com.android.server.wm.utils.CoordinateTransforms.transformToRotation;
@@ -185,6 +186,44 @@ public class CoordinateTransformsTest {
        assertEquals(mMatrix2, mMatrix);
    }

    @Test
    public void rotate_0_bottomRight() {
        computeRotationMatrix(ROTATION_0, W, H, mMatrix);
        PointF newPoints = checkMappedPoints(W, H);
        assertEquals(W, newPoints.x, 0);
        assertEquals(H, newPoints.y, 0);
    }

    @Test
    public void rotate_90_bottomRight() {
        computeRotationMatrix(ROTATION_90, W, H, mMatrix);
        PointF newPoints = checkMappedPoints(W, H);
        assertEquals(0, newPoints.x, 0);
        assertEquals(W, newPoints.y, 0);
    }

    @Test
    public void rotate_180_bottomRight() {
        computeRotationMatrix(ROTATION_180, W, H, mMatrix);
        PointF newPoints = checkMappedPoints(W, H);
        assertEquals(0, newPoints.x, 0);
        assertEquals(0, newPoints.y, 0);
    }

    @Test
    public void rotate_270_bottomRight() {
        computeRotationMatrix(ROTATION_270, W, H, mMatrix);
        PointF newPoints = checkMappedPoints(W, H);
        assertEquals(H, newPoints.x, 0);
        assertEquals(0, newPoints.y, 0);
    }

    private PointF checkMappedPoints(int x, int y) {
        final float[] fs = new float[] {x, y};
        mMatrix.mapPoints(fs);
        return new PointF(fs[0], fs[1]);
    }

    private void assertMatricesAreInverses(Matrix matrix, Matrix matrix2) {
        final Matrix concat = new Matrix();
        concat.setConcat(matrix, matrix2);
Loading