155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui/*
355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * Copyright (C) 2013 The Android Open Source Project
455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * Licensed under the Apache License, Version 2.0 (the "License");
655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * you may not use this file except in compliance with the License.
755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * You may obtain a copy of the License at
855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *      http://www.apache.org/licenses/LICENSE-2.0
1055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui *
1155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * Unless required by applicable law or agreed to in writing, software
1255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * distributed under the License is distributed on an "AS IS" BASIS,
1355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * See the License for the specific language governing permissions and
1555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui * limitations under the License.
1655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui */
1755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
1855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#ifndef ANDROID_HWUI_SHADOW_TESSELLATOR_H
1955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#define ANDROID_HWUI_SHADOW_TESSELLATOR_H
2055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
212dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudson#include <SkPath.h>
222dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudson
2355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#include "Debug.h"
2455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#include "Matrix.h"
2555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
2655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghuinamespace android {
2755bfb4e728fe1db619af5d2c287f4abe711b3343ztenghuinamespace uirenderer {
2855bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
292dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudsonclass VertexBuffer;
302dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudson
3163d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui// All SHADOW_* are used to define all the geometry property of shadows.
3263d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui// Use a simplified example to illustrate the geometry setup here.
3363d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui// Assuming we use 6 rays and only 1 layer, Then we will have 2 hexagons, which
3463d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui// are 0 to 5 and 6 to 11. The area between them will be the penumbra area, and
3563d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui// the area inside the 2nd hexagon is the umbra.
3650ecf849cb7ccc3482517b74d2214b347927791eztenghui// Ambient shadow is using only 1 layer for opaque caster, otherwise, spot
3750ecf849cb7ccc3482517b74d2214b347927791eztenghui// shadow and ambient shadow are using 2 layers.
3863d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui// Triange strip indices for penumbra area: (0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5, 11, 0, 6)
3963d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui//                 0
4063d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui//
4163d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui//      5          6         1
4263d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui//           11         7
4350ecf849cb7ccc3482517b74d2214b347927791eztenghui//
4463d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui//           10         8
4563d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui//      4          9         2
4663d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui//
4763d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui//                 3
4863d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui
4963d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui// The total number of rays starting from the centroid of shadow area, in order
5063d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui// to generate the shadow geometry.
51726118b35240957710d4d85fb5747e2ba8b934f7Chris Craik#define SHADOW_RAY_COUNT 128
5263d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui
5363d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui// The total number of all the vertices representing the shadow.
5450ecf849cb7ccc3482517b74d2214b347927791eztenghui// For the case we only have 1 layer, then we will just fill only 2/3 of it.
5550ecf849cb7ccc3482517b74d2214b347927791eztenghui#define SHADOW_VERTEX_COUNT (3 * SHADOW_RAY_COUNT)
5663d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui
5763d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui// The total number of indices used for drawing the shadow geometry as triangle strips.
5850ecf849cb7ccc3482517b74d2214b347927791eztenghui// Depending on the mode we are drawing, we can have 1 layer or 2 layers.
5950ecf849cb7ccc3482517b74d2214b347927791eztenghui// Therefore, we only build the longer index buffer.
6050ecf849cb7ccc3482517b74d2214b347927791eztenghui#define TWO_POLY_RING_SHADOW_INDEX_COUNT (4 * (SHADOW_RAY_COUNT + 1))
6150ecf849cb7ccc3482517b74d2214b347927791eztenghui#define ONE_POLY_RING_SHADOW_INDEX_COUNT (2 * (SHADOW_RAY_COUNT + 1))
6250ecf849cb7ccc3482517b74d2214b347927791eztenghui
6350ecf849cb7ccc3482517b74d2214b347927791eztenghui#define MAX_SHADOW_INDEX_COUNT TWO_POLY_RING_SHADOW_INDEX_COUNT
6463d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui
65b98f2116f5896acb4b221015b22624a3c5d5a7feChris Craik#define SHADOW_MIN_CASTER_Z 0.001f
66b98f2116f5896acb4b221015b22624a3c5d5a7feChris Craik
677940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui#define MINIMAL_DELTA_THETA (M_PI / 180 / 1000)
687940dc57e3ffcf9c4a33375215f3a42250fa896cztenghui
6955bfb4e728fe1db619af5d2c287f4abe711b3343ztenghuiclass ShadowTessellator {
7055bfb4e728fe1db619af5d2c287f4abe711b3343ztenghuipublic:
7105f3d6e5111fd08df5cd9aae2c3d28399dc0e7f5Chris Craik    static void tessellateAmbientShadow(bool isCasterOpaque,
7250ecf849cb7ccc3482517b74d2214b347927791eztenghui            const Vector3* casterPolygon, int casterVertexCount,
73af6f7ed8dd4288a41d0a07a1f0f0be7d6d035b33ztenghui            const Vector3& centroid3d,  const Rect& casterBounds,
74af6f7ed8dd4288a41d0a07a1f0f0be7d6d035b33ztenghui            const Rect& localClip, float maxZ, VertexBuffer& shadowVertexBuffer);
757b4516e7ea552ad08d6e7277d311ef11bd8f12e8ztenghui
7605f3d6e5111fd08df5cd9aae2c3d28399dc0e7f5Chris Craik    static void tessellateSpotShadow(bool isCasterOpaque,
77c50a03d78aaedd0003377e98710e7038bda330e9ztenghui            const Vector3* casterPolygon, int casterVertexCount, const Vector3& casterCentroid,
78797b95b26bbb7557678af78b9a2a61830158920fChris Craik            const mat4& receiverTransform, const Vector3& lightCenter, int lightRadius,
79797b95b26bbb7557678af78b9a2a61830158920fChris Craik            const Rect& casterBounds, const Rect& localClip, VertexBuffer& shadowVertexBuffer);
8063d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui
8163d41abb40b3ce40d8b9bccb1cf186e8158a3687ztenghui    static Vector2 centroid2d(const Vector2* poly, int polyLength);
822e023f3827dfc0dfc1ed7c3dd54d02b4a993f0b4ztenghui
832e023f3827dfc0dfc1ed7c3dd54d02b4a993f0b4ztenghui    static bool isClockwise(const Vector2* polygon, int len);
842e023f3827dfc0dfc1ed7c3dd54d02b4a993f0b4ztenghui
85c50a03d78aaedd0003377e98710e7038bda330e9ztenghui    static Vector2 calculateNormal(const Vector2& p1, const Vector2& p2);
862e023f3827dfc0dfc1ed7c3dd54d02b4a993f0b4ztenghui
87512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    static int getExtraVertexNumber(const Vector2& vector1, const Vector2& vector2,
88512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui            float divisor);
89512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui
90512e643ce83b1d48ad9630a3622276f795cf4fb2ztenghui    static void checkOverflow(int used, int total, const char* bufferName);
9155bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui}; // ShadowTessellator
9255bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
9355bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui}; // namespace uirenderer
9455bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui}; // namespace android
9555bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui
9655bfb4e728fe1db619af5d2c287f4abe711b3343ztenghui#endif // ANDROID_HWUI_SHADOW_TESSELLATOR_H
97