• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /**
112      * Extension of {@link SurfaceProperties} which just stores all the values locally
113      */
114     public class MockProperties extends SurfaceProperties {
115 
116         public float alpha = -1;
117         public Matrix matrix = null;
118         public Rect windowCrop = null;
119         public float cornerRadius = 0;
120         public float shadowRadius = 0;
121 
MockProperties()122         protected MockProperties() {
123             super(null);
124         }
125 
126         @Override
setAlpha(float alpha)127         public SurfaceProperties setAlpha(float alpha) {
128             this.alpha = alpha;
129             return this;
130         }
131 
132         @Override
setMatrix(Matrix matrix)133         public SurfaceProperties setMatrix(Matrix matrix) {
134             this.matrix = matrix;
135             return this;
136         }
137 
138         @Override
setWindowCrop(Rect windowCrop)139         public SurfaceProperties setWindowCrop(Rect windowCrop) {
140             this.windowCrop = windowCrop;
141             return this;
142         }
143 
144         @Override
setLayer(int relativeLayer)145         public SurfaceProperties setLayer(int relativeLayer) {
146             return this;
147         }
148 
149         @Override
setCornerRadius(float radius)150         public SurfaceProperties setCornerRadius(float radius) {
151             this.cornerRadius = radius;
152             return this;
153         }
154 
155         @Override
setShadowRadius(float radius)156         public SurfaceProperties setShadowRadius(float radius) {
157             this.shadowRadius = radius;
158             return this;
159         }
160     }
161 }
162