1 /*
2  * Copyright (C) 2022 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 package com.android.quickstep.util;
17 
18 import android.graphics.Matrix;
19 import android.graphics.Rect;
20 import android.view.SurfaceControl;
21 import android.view.SurfaceControl.Transaction;
22 
23 /**
24  * Helper class for building a {@link Transaction}.
25  */
26 public class SurfaceTransaction {
27 
28     private final Transaction mTransaction = new Transaction();
29     private final float[] mTmpValues = new float[9];
30 
31     /**
32      * Creates a new builder for the provided surface
33      */
forSurface(SurfaceControl surface)34     public SurfaceProperties forSurface(SurfaceControl surface) {
35         return surface.isValid() ? new SurfaceProperties(surface) : new MockProperties();
36     }
37 
38     /**
39      * Returns the final transaction
40      */
getTransaction()41     public Transaction getTransaction() {
42         return mTransaction;
43     }
44 
45     /**
46      * Utility class to update surface params in a transaction
47      */
48     public class SurfaceProperties {
49 
50         private final SurfaceControl mSurface;
51 
SurfaceProperties(SurfaceControl surface)52         SurfaceProperties(SurfaceControl surface) {
53             mSurface = surface;
54         }
55 
56         /**
57          * @param alpha The alpha value to apply to the surface.
58          * @return this Builder
59          */
setAlpha(float alpha)60         public SurfaceProperties setAlpha(float alpha) {
61             mTransaction.setAlpha(mSurface, alpha);
62             return this;
63         }
64 
65         /**
66          * @param matrix The matrix to apply to the surface.
67          * @return this Builder
68          */
setMatrix(Matrix matrix)69         public SurfaceProperties setMatrix(Matrix matrix) {
70             mTransaction.setMatrix(mSurface, matrix, mTmpValues);
71             return this;
72         }
73 
74         /**
75          * @param windowCrop The window crop to apply to the surface.
76          * @return this Builder
77          */
setWindowCrop(Rect windowCrop)78         public SurfaceProperties setWindowCrop(Rect windowCrop) {
79             mTransaction.setWindowCrop(mSurface, windowCrop);
80             return this;
81         }
82 
83         /**
84          * @param relativeLayer The relative layer.
85          * @return this Builder
86          */
setLayer(int relativeLayer)87         public SurfaceProperties setLayer(int relativeLayer) {
88             mTransaction.setLayer(mSurface, relativeLayer);
89             return this;
90         }
91 
92         /**
93          * @param radius the Radius for rounded corners to apply to the surface.
94          * @return this Builder
95          */
setCornerRadius(float radius)96         public SurfaceProperties setCornerRadius(float radius) {
97             mTransaction.setCornerRadius(mSurface, radius);
98             return this;
99         }
100 
101         /**
102          * @param radius the Radius for the shadows to apply to the surface.
103          * @return this Builder
104          */
setShadowRadius(float radius)105         public SurfaceProperties setShadowRadius(float radius) {
106             mTransaction.setShadowRadius(mSurface, radius);
107             return this;
108         }
109 
110         /**
111          * Requests to show the given surface.
112          * @return this Builder
113          */
setShow()114         public SurfaceProperties setShow() {
115             mTransaction.show(mSurface);
116             return this;
117         }
118     }
119 
120     /**
121      * Extension of {@link SurfaceProperties} which just stores all the values locally
122      */
123     public class MockProperties extends SurfaceProperties {
124 
125         public float alpha = -1;
126         public Matrix matrix = null;
127         public Rect windowCrop = null;
128         public float cornerRadius = 0;
129         public float shadowRadius = 0;
130 
MockProperties()131         protected MockProperties() {
132             super(null);
133         }
134 
135         @Override
setAlpha(float alpha)136         public SurfaceProperties setAlpha(float alpha) {
137             this.alpha = alpha;
138             return this;
139         }
140 
141         @Override
setMatrix(Matrix matrix)142         public SurfaceProperties setMatrix(Matrix matrix) {
143             this.matrix = matrix;
144             return this;
145         }
146 
147         @Override
setWindowCrop(Rect windowCrop)148         public SurfaceProperties setWindowCrop(Rect windowCrop) {
149             this.windowCrop = windowCrop;
150             return this;
151         }
152 
153         @Override
setLayer(int relativeLayer)154         public SurfaceProperties setLayer(int relativeLayer) {
155             return this;
156         }
157 
158         @Override
setCornerRadius(float radius)159         public SurfaceProperties setCornerRadius(float radius) {
160             this.cornerRadius = radius;
161             return this;
162         }
163 
164         @Override
setShadowRadius(float radius)165         public SurfaceProperties setShadowRadius(float radius) {
166             this.shadowRadius = radius;
167             return this;
168         }
169 
170         @Override
setShow()171         public SurfaceProperties setShow() {
172             return this;
173         }
174     }
175 }
176