1 /*
2  * Copyright 2019 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 #include <renderengine/Mesh.h>
18 
19 #include <math/vec4.h>
20 
21 #include <ui/Rect.h>
22 #include <ui/Transform.h>
23 
24 #include "GLShadowVertexGenerator.h"
25 
26 namespace android {
27 namespace renderengine {
28 namespace gl {
29 
GLShadowVertexGenerator(const FloatRect & casterRect,float casterCornerRadius,float casterZ,bool casterIsTranslucent,const vec4 & ambientColor,const vec4 & spotColor,const vec3 & lightPosition,float lightRadius)30 GLShadowVertexGenerator::GLShadowVertexGenerator(const FloatRect& casterRect,
31                                                  float casterCornerRadius, float casterZ,
32                                                  bool casterIsTranslucent, const vec4& ambientColor,
33                                                  const vec4& spotColor, const vec3& lightPosition,
34                                                  float lightRadius) {
35     mDrawAmbientShadow = ambientColor.a > 0.f;
36     mDrawSpotShadow = spotColor.a > 0.f;
37 
38     // Generate geometries and find number of vertices to generate
39     if (mDrawAmbientShadow) {
40         mAmbientShadowGeometry = getAmbientShadowGeometry(casterRect, casterCornerRadius, casterZ,
41                                                           casterIsTranslucent, ambientColor);
42         mAmbientShadowVertexCount = getVertexCountForGeometry(*mAmbientShadowGeometry.get());
43         mAmbientShadowIndexCount = getIndexCountForGeometry(*mAmbientShadowGeometry.get());
44     } else {
45         mAmbientShadowVertexCount = 0;
46         mAmbientShadowIndexCount = 0;
47     }
48 
49     if (mDrawSpotShadow) {
50         mSpotShadowGeometry =
51                 getSpotShadowGeometry(casterRect, casterCornerRadius, casterZ, casterIsTranslucent,
52                                       spotColor, lightPosition, lightRadius);
53         mSpotShadowVertexCount = getVertexCountForGeometry(*mSpotShadowGeometry.get());
54         mSpotShadowIndexCount = getIndexCountForGeometry(*mSpotShadowGeometry.get());
55     } else {
56         mSpotShadowVertexCount = 0;
57         mSpotShadowIndexCount = 0;
58     }
59 }
60 
getVertexCount() const61 size_t GLShadowVertexGenerator::getVertexCount() const {
62     return mAmbientShadowVertexCount + mSpotShadowVertexCount;
63 }
64 
getIndexCount() const65 size_t GLShadowVertexGenerator::getIndexCount() const {
66     return mAmbientShadowIndexCount + mSpotShadowIndexCount;
67 }
68 
fillVertices(Mesh::VertexArray<vec2> & position,Mesh::VertexArray<vec4> & color,Mesh::VertexArray<vec3> & params) const69 void GLShadowVertexGenerator::fillVertices(Mesh::VertexArray<vec2>& position,
70                                            Mesh::VertexArray<vec4>& color,
71                                            Mesh::VertexArray<vec3>& params) const {
72     if (mDrawAmbientShadow) {
73         fillVerticesForGeometry(*mAmbientShadowGeometry.get(), mAmbientShadowVertexCount, position,
74                                 color, params);
75     }
76     if (mDrawSpotShadow) {
77         fillVerticesForGeometry(*mSpotShadowGeometry.get(), mSpotShadowVertexCount,
78                                 Mesh::VertexArray<vec2>(position, mAmbientShadowVertexCount),
79                                 Mesh::VertexArray<vec4>(color, mAmbientShadowVertexCount),
80                                 Mesh::VertexArray<vec3>(params, mAmbientShadowVertexCount));
81     }
82 }
83 
fillIndices(uint16_t * indices) const84 void GLShadowVertexGenerator::fillIndices(uint16_t* indices) const {
85     if (mDrawAmbientShadow) {
86         fillIndicesForGeometry(*mAmbientShadowGeometry.get(), mAmbientShadowIndexCount,
87                                0 /* starting vertex offset */, indices);
88     }
89     if (mDrawSpotShadow) {
90         fillIndicesForGeometry(*mSpotShadowGeometry.get(), mSpotShadowIndexCount,
91                                mAmbientShadowVertexCount /* starting vertex offset */,
92                                &(indices[mAmbientShadowIndexCount]));
93     }
94 }
95 
96 } // namespace gl
97 } // namespace renderengine
98 } // namespace android
99