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 * Generate spot shadow bitmap. 24 */ 25 class SpotShadowTriangulator { 26 27 private final SpotShadowConfig mShadowConfig; 28 private float[][] mStrips; 29 SpotShadowTriangulator(SpotShadowConfig config)30 public SpotShadowTriangulator(SpotShadowConfig config) { 31 mShadowConfig = config; 32 } 33 34 /** 35 * Populate the shadow bitmap. 36 */ triangulate()37 public void triangulate() { 38 try { 39 float[] lightSources = 40 SpotShadowVertexCalculator.calculateLight(mShadowConfig.getLightRadius(), 41 mShadowConfig.getLightCoord()[0], 42 mShadowConfig.getLightCoord()[1], mShadowConfig.getLightCoord()[2]); 43 44 45 mStrips = new float[2][]; 46 int[] sizes = SpotShadowVertexCalculator.getStripSizes(mShadowConfig.getPolyLength()); 47 for (int i = 0; i < sizes.length; ++i) { 48 mStrips[i] = new float[3 * sizes[i]]; 49 } 50 51 SpotShadowVertexCalculator.calculateShadow(lightSources, 52 mShadowConfig.getPoly(), 53 mShadowConfig.getPolyLength(), 54 mShadowConfig.getShadowStrength(), 55 mStrips); 56 } catch (IndexOutOfBoundsException|ArithmeticException mathError) { 57 Bridge.getLog().warning(ILayoutLog.TAG_INFO, "Arithmetic error while drawing " + 58 "spot shadow", 59 null, mathError); 60 } catch (Exception ex) { 61 Bridge.getLog().warning(ILayoutLog.TAG_INFO, "Error while drawing shadow", 62 null, ex); 63 } 64 } 65 /** 66 * @return true if generated shadow poly is valid. False otherwise. 67 */ validate()68 public boolean validate() { 69 return mStrips != null && mStrips[0].length >= 9; 70 } 71 getStrips()72 public float[][] getStrips() { 73 return mStrips; 74 } 75 } 76