1 /*
2  * Copyright (C) 2020 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 
17 package com.android.server.wm.traces.parser.layers
18 
19 import android.surfaceflinger.nano.Layers
20 import android.surfaceflinger.nano.Common.TransformProto
21 import com.android.server.wm.traces.common.layers.Transform
22 import com.android.server.wm.traces.common.layers.Transform.Companion.FLIP_H_VAL
23 import com.android.server.wm.traces.common.layers.Transform.Companion.FLIP_V_VAL
24 import com.android.server.wm.traces.common.layers.Transform.Companion.ROTATE_VAL
25 import com.android.server.wm.traces.common.layers.Transform.Companion.ROT_90_VAL
26 import com.android.server.wm.traces.common.layers.Transform.Companion.SCALE_VAL
27 import com.android.server.wm.traces.common.layers.Transform.Companion.isFlagClear
28 import com.android.server.wm.traces.common.layers.Transform.Companion.isFlagSet
29 
30 class Transform(transform: TransformProto?, position: Layers.PositionProto?) :
31         Transform(
32             transform?.type,
33             getMatrix(transform, position)
34         )
35 
getMatrixnull36 private fun getMatrix(transform: TransformProto?, position: Layers.PositionProto?):
37         Transform.Matrix {
38     val x = position?.x ?: 0f
39     val y = position?.y ?: 0f
40 
41     return when {
42         transform == null || Transform.isSimpleTransform(transform.type) ->
43             transform?.type.getDefaultTransform(x, y)
44         else ->
45             Transform.Matrix(transform.dsdx, transform.dtdx, x, transform.dsdy, transform.dtdy, y)
46     }
47 }
48 
getDefaultTransformnull49 private fun Int?.getDefaultTransform(x: Float, y: Float): Transform.Matrix {
50     return when {
51         // IDENTITY
52         this == null ->
53             Transform.Matrix(1f, 0f, x, 0f, 1f, y)
54         // // ROT_270 = ROT_90|FLIP_H|FLIP_V
55         isFlagSet(ROT_90_VAL or FLIP_V_VAL or FLIP_H_VAL) ->
56             Transform.Matrix(0f, -1f, x, 1f, 0f, y)
57         // ROT_180 = FLIP_H|FLIP_V
58         isFlagSet(FLIP_V_VAL or FLIP_H_VAL) ->
59             Transform.Matrix(-1f, 0f, x, 0f, -1f, y)
60         // ROT_90
61         isFlagSet(ROT_90_VAL) ->
62             Transform.Matrix(0f, 1f, x, -1f, 0f, y)
63         // IDENTITY
64         isFlagClear(SCALE_VAL or ROTATE_VAL) ->
65             Transform.Matrix(1f, 0f, x, 0f, 1f, y)
66         else ->
67             throw IllegalStateException("Unknown transform type $this")
68     }
69 }
70