ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2013 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #ifndef ANDROID_HWUI_SHADOW_TESSELLATOR_H |
| 19 | #define ANDROID_HWUI_SHADOW_TESSELLATOR_H |
| 20 | |
| 21 | #include "Debug.h" |
| 22 | #include "Matrix.h" |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 23 | #include "OpenGLRenderer.h" |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 24 | #include "VertexBuffer.h" |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 29 | // All SHADOW_* are used to define all the geometry property of shadows. |
| 30 | // Use a simplified example to illustrate the geometry setup here. |
| 31 | // Assuming we use 6 rays and only 1 layer, Then we will have 2 hexagons, which |
| 32 | // are 0 to 5 and 6 to 11. The area between them will be the penumbra area, and |
| 33 | // the area inside the 2nd hexagon is the umbra. |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 34 | // Ambient shadow is using only 1 layer for opaque caster, otherwise, spot |
| 35 | // shadow and ambient shadow are using 2 layers. |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 36 | // Triange strip indices for penumbra area: (0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5, 11, 0, 6) |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 37 | // 0 |
| 38 | // |
| 39 | // 5 6 1 |
| 40 | // 11 7 |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 41 | // |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 42 | // 10 8 |
| 43 | // 4 9 2 |
| 44 | // |
| 45 | // 3 |
| 46 | |
| 47 | // The total number of rays starting from the centroid of shadow area, in order |
| 48 | // to generate the shadow geometry. |
Chris Craik | 726118b | 2014-03-07 18:27:49 -0800 | [diff] [blame] | 49 | #define SHADOW_RAY_COUNT 128 |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 50 | |
| 51 | // The total number of all the vertices representing the shadow. |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 52 | // For the case we only have 1 layer, then we will just fill only 2/3 of it. |
| 53 | #define SHADOW_VERTEX_COUNT (3 * SHADOW_RAY_COUNT) |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 54 | |
| 55 | // The total number of indices used for drawing the shadow geometry as triangle strips. |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 56 | // Depending on the mode we are drawing, we can have 1 layer or 2 layers. |
| 57 | // Therefore, we only build the longer index buffer. |
| 58 | #define TWO_POLY_RING_SHADOW_INDEX_COUNT (4 * (SHADOW_RAY_COUNT + 1)) |
| 59 | #define ONE_POLY_RING_SHADOW_INDEX_COUNT (2 * (SHADOW_RAY_COUNT + 1)) |
| 60 | |
| 61 | #define MAX_SHADOW_INDEX_COUNT TWO_POLY_RING_SHADOW_INDEX_COUNT |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 62 | |
Chris Craik | b98f211 | 2014-03-11 17:42:29 -0700 | [diff] [blame] | 63 | #define SHADOW_MIN_CASTER_Z 0.001f |
| 64 | |
ztenghui | 7940dc5 | 2014-04-22 11:21:49 -0700 | [diff] [blame] | 65 | #define MINIMAL_DELTA_THETA (M_PI / 180 / 1000) |
| 66 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 67 | class ShadowTessellator { |
| 68 | public: |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 69 | static void tessellateAmbientShadow(bool isCasterOpaque, |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 70 | const Vector3* casterPolygon, int casterVertexCount, |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 71 | const Vector3& centroid3d, const Rect& casterBounds, |
| 72 | const Rect& localClip, float maxZ, VertexBuffer& shadowVertexBuffer); |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 73 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 74 | static void tessellateSpotShadow(bool isCasterOpaque, |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 75 | const Vector3* casterPolygon, int casterVertexCount, |
Chris Craik | 797b95b | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 76 | const mat4& receiverTransform, const Vector3& lightCenter, int lightRadius, |
| 77 | const Rect& casterBounds, const Rect& localClip, VertexBuffer& shadowVertexBuffer); |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 78 | |
| 79 | static void generateShadowIndices(uint16_t* shadowIndices); |
| 80 | |
| 81 | static Vector2 centroid2d(const Vector2* poly, int polyLength); |
ztenghui | 2e023f3 | 2014-04-28 16:43:13 -0700 | [diff] [blame] | 82 | |
| 83 | static bool isClockwise(const Vector2* polygon, int len); |
| 84 | |
| 85 | /** |
| 86 | * Determine whether the path is clockwise, using the control points. |
| 87 | * |
| 88 | * TODO: Given the skia is using inverted Y coordinate, shadow system needs |
| 89 | * to convert to the same coordinate to avoid the extra reverse. |
| 90 | * |
| 91 | * @param path The path to be examined. |
| 92 | */ |
| 93 | static bool isClockwisePath(const SkPath &path); |
| 94 | |
| 95 | /** |
| 96 | * Reverse the vertex array. |
| 97 | * |
| 98 | * @param polygon The vertex array to be reversed. |
| 99 | * @param len The length of the vertex array. |
| 100 | */ |
| 101 | static void reverseVertexArray(Vertex* polygon, int len); |
| 102 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 103 | }; // ShadowTessellator |
| 104 | |
| 105 | }; // namespace uirenderer |
| 106 | }; // namespace android |
| 107 | |
| 108 | #endif // ANDROID_HWUI_SHADOW_TESSELLATOR_H |