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 #pragma once
18 
19 #include <math/vec4.h>
20 #include <renderengine/Mesh.h>
21 #include <ui/Rect.h>
22 
23 namespace android {
24 namespace renderengine {
25 namespace gl {
26 
27 /**
28  * The shadow geometry logic and vertex generation code has been ported from skia shadow
29  * fast path OpenGL implementation to draw shadows around rects and rounded rects including
30  * circles.
31  *
32  * path: skia/src/gpu/GrRenderTargetContext.cpp GrRenderTargetContext::drawFastShadow
33  *
34  * Modifications made:
35  * - Switched to using std lib math functions
36  * - Fall off function is implemented in vertex shader rather than a shadow texture
37  * - Removed transformations applied on the caster rect since the caster will be in local
38  *   coordinate space and will be transformed by the vertex shader.
39  */
40 
41 enum RRectType {
42     kFill_RRectType,
43     kStroke_RRectType,
44     kOverstroke_RRectType,
45 };
46 
47 struct Geometry {
48     vec4 fColor;
49     float fOuterRadius;
50     float fUmbraInset;
51     float fInnerRadius;
52     float fBlurRadius;
53     FloatRect fDevBounds;
54     RRectType fType;
55     bool fIsCircle;
56     bool fIsStroked;
57 };
58 
59 std::unique_ptr<Geometry> getSpotShadowGeometry(const FloatRect& casterRect,
60                                                 float casterCornerRadius, float casterZ,
61                                                 bool casterIsTranslucent, const vec4& spotColor,
62                                                 const vec3& lightPosition, float lightRadius);
63 
64 std::unique_ptr<Geometry> getAmbientShadowGeometry(const FloatRect& casterRect,
65                                                    float casterCornerRadius, float casterZ,
66                                                    bool casterIsTranslucent,
67                                                    const vec4& ambientColor);
68 
69 int getVertexCountForGeometry(const Geometry& shadowGeometry);
70 
71 int getIndexCountForGeometry(const Geometry& shadowGeometry);
72 
73 void fillVerticesForGeometry(const Geometry& shadowGeometry, int vertexCount,
74                              Mesh::VertexArray<vec2> position, Mesh::VertexArray<vec4> shadowColor,
75                              Mesh::VertexArray<vec3> shadowParams);
76 
77 void fillIndicesForGeometry(const Geometry& shadowGeometry, int indexCount,
78                             int startingVertexOffset, uint16_t* indices);
79 
80 /**
81  * Maps shadow geometry 'alpha' varying (1 for darkest, 0 for transparent) to
82  * darkness at that spot. Values are determined by an exponential falloff
83  * function provided by UX.
84  *
85  * The texture is used for quick lookup in theshadow shader.
86  *
87  * textureData - filled with shadow texture data that needs to be at least of
88  *               size textureWidth
89  *
90  * textureWidth - width of the texture, height is always 1
91  */
92 void fillShadowTextureData(uint8_t* textureData, size_t textureWidth);
93 
94 } // namespace gl
95 } // namespace renderengine
96 } // namespace android
97