1/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm.utils;
18
19import static android.view.Surface.ROTATION_0;
20import static android.view.Surface.ROTATION_180;
21import static android.view.Surface.ROTATION_270;
22import static android.view.Surface.ROTATION_90;
23
24import android.annotation.Dimension;
25import android.graphics.Matrix;
26import android.view.Surface.Rotation;
27
28public class CoordinateTransforms {
29
30    private CoordinateTransforms() {
31    }
32
33    /**
34     * Sets a matrix such that given a rotation, it transforms physical display
35     * coordinates to that rotation's logical coordinates.
36     *
37     * @param rotation the rotation to which the matrix should transform
38     * @param out      the matrix to be set
39     */
40    public static void transformPhysicalToLogicalCoordinates(@Rotation int rotation,
41            @Dimension int physicalWidth, @Dimension int physicalHeight, Matrix out) {
42        switch (rotation) {
43            case ROTATION_0:
44                out.reset();
45                break;
46            case ROTATION_90:
47                out.setRotate(270);
48                out.postTranslate(0, physicalWidth);
49                break;
50            case ROTATION_180:
51                out.setRotate(180);
52                out.postTranslate(physicalWidth, physicalHeight);
53                break;
54            case ROTATION_270:
55                out.setRotate(90);
56                out.postTranslate(physicalHeight, 0);
57                break;
58            default:
59                throw new IllegalArgumentException("Unknown rotation: " + rotation);
60        }
61    }
62}
63