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 
17 package android.view.shadow;
18 
19 import com.android.ide.common.rendering.api.ILayoutLog;
20 import com.android.layoutlib.bridge.Bridge;
21 
22 /**
23  * Generates ambient shadow bitmap
24  */
25 class AmbientShadowTriangulator {
26 
27     private final AmbientShadowConfig mShadowConfig;
28     private final AmbientShadowVertexCalculator mCalculator;
29 
30     private boolean mValid;
31 
AmbientShadowTriangulator(AmbientShadowConfig shadowConfig)32     public AmbientShadowTriangulator(AmbientShadowConfig shadowConfig) {
33         mShadowConfig = shadowConfig;
34 
35         mCalculator = new AmbientShadowVertexCalculator(mShadowConfig);
36     }
37 
38     /**
39      * Populate vertices and fill the triangle buffers.
40      */
triangulate()41     public void triangulate() {
42         try {
43             mCalculator.generateVertex();
44             mValid = true;
45         } catch (IndexOutOfBoundsException|ArithmeticException mathError) {
46             Bridge.getLog().warning(ILayoutLog.TAG_INFO,  "Arithmetic error while drawing " +
47                             "ambient shadow",
48                     null, mathError);
49         } catch (Exception ex) {
50             Bridge.getLog().warning(ILayoutLog.TAG_INFO,  "Error while drawing shadow",
51                     null, ex);
52         }
53     }
54 
isValid()55     public boolean isValid() {
56         return mValid;
57     }
58 
getVertices()59     public float[] getVertices() { return mCalculator.getVertex(); }
60 
getIndices()61     public int[] getIndices() { return mCalculator.getIndex(); }
62 
getColors()63     public float[] getColors() { return mCalculator.getColor(); }
64 }
65