blob: ee60a63de309916f5123f20e53caaf23242d77b2 [file] [log] [blame]
ztenghui55bfb4e2013-12-03 10:38:55 -08001/*
2 * Copyright (C) 2013 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#define LOG_TAG "OpenGLRenderer"
Chris Craik87f9df82014-03-07 14:34:42 -080018#define ATRACE_TAG ATRACE_TAG_VIEW
ztenghui55bfb4e2013-12-03 10:38:55 -080019
20#include <math.h>
21#include <utils/Log.h>
Chris Craik87f9df82014-03-07 14:34:42 -080022#include <utils/Trace.h>
ztenghui55bfb4e2013-12-03 10:38:55 -080023
24#include "AmbientShadow.h"
25#include "ShadowTessellator.h"
ztenghui7b4516e2014-01-07 10:42:55 -080026#include "SpotShadow.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080027
28namespace android {
29namespace uirenderer {
30
ztenghui7b4516e2014-01-07 10:42:55 -080031template<typename T>
32static inline T max(T a, T b) {
33 return a > b ? a : b;
34}
35
ztenghui50ecf842014-03-11 16:52:30 -070036VertexBufferMode ShadowTessellator::tessellateAmbientShadow(bool isCasterOpaque,
37 const Vector3* casterPolygon, int casterVertexCount,
ztenghuiaf6f7ed2014-03-18 17:25:49 -070038 const Vector3& centroid3d, const Rect& casterBounds,
39 const Rect& localClip, float maxZ, VertexBuffer& shadowVertexBuffer) {
Chris Craik87f9df82014-03-07 14:34:42 -080040 ATRACE_CALL();
41
ztenghui55bfb4e2013-12-03 10:38:55 -080042 // A bunch of parameters to tweak the shadow.
43 // TODO: Allow some of these changable by debug settings or APIs.
Chris Craik726118b2014-03-07 18:27:49 -080044 const float heightFactor = 1.0f / 128;
ztenghui7b4516e2014-01-07 10:42:55 -080045 const float geomFactor = 64;
ztenghui55bfb4e2013-12-03 10:38:55 -080046
ztenghuiaf6f7ed2014-03-18 17:25:49 -070047 Rect ambientShadowBounds(casterBounds);
48 ambientShadowBounds.outset(maxZ * geomFactor * heightFactor);
49
50 if (!localClip.intersects(ambientShadowBounds)) {
51#if DEBUG_SHADOW
52 ALOGD("Ambient shadow is out of clip rect!");
53#endif
54 return kVertexBufferMode_OnePolyRingShadow;
55 }
56
ztenghui50ecf842014-03-11 16:52:30 -070057 return AmbientShadow::createAmbientShadow(isCasterOpaque, casterPolygon,
58 casterVertexCount, centroid3d, heightFactor, geomFactor,
59 shadowVertexBuffer);
ztenghui55bfb4e2013-12-03 10:38:55 -080060
61}
62
ztenghui50ecf842014-03-11 16:52:30 -070063VertexBufferMode ShadowTessellator::tessellateSpotShadow(bool isCasterOpaque,
64 const Vector3* casterPolygon, int casterVertexCount,
ztenghuicc3c2562014-01-17 10:34:10 -080065 const Vector3& lightPosScale, const mat4& receiverTransform,
ztenghuiaf6f7ed2014-03-18 17:25:49 -070066 int screenWidth, int screenHeight, const Rect& casterBounds,
67 const Rect& localClip, VertexBuffer& shadowVertexBuffer) {
Chris Craik87f9df82014-03-07 14:34:42 -080068 ATRACE_CALL();
69
ztenghui7b4516e2014-01-07 10:42:55 -080070 // A bunch of parameters to tweak the shadow.
71 // TODO: Allow some of these changable by debug settings or APIs.
ztenghui7b4516e2014-01-07 10:42:55 -080072 int maximal = max(screenWidth, screenHeight);
ztenghuicc3c2562014-01-17 10:34:10 -080073 Vector3 lightCenter(screenWidth * lightPosScale.x, screenHeight * lightPosScale.y,
74 maximal * lightPosScale.z);
ztenghui7b4516e2014-01-07 10:42:55 -080075#if DEBUG_SHADOW
76 ALOGD("light center %f %f %f", lightCenter.x, lightCenter.y, lightCenter.z);
77#endif
Chris Craik3197cde2014-01-16 14:03:39 -080078
79 // light position (because it's in local space) needs to compensate for receiver transform
80 // TODO: should apply to light orientation, not just position
81 Matrix4 reverseReceiverTransform;
82 reverseReceiverTransform.loadInverse(receiverTransform);
83 reverseReceiverTransform.mapPoint3d(lightCenter);
84
ztenghuicc3c2562014-01-17 10:34:10 -080085 const float lightSize = maximal / 4;
Chris Craik726118b2014-03-07 18:27:49 -080086 const int lightVertexCount = 8;
ztenghui7b4516e2014-01-07 10:42:55 -080087
ztenghuiaf6f7ed2014-03-18 17:25:49 -070088 // Now light and caster are both in local space, we will check whether
89 // the shadow is within the clip area.
90 Rect lightRect = Rect(lightCenter.x - lightSize, lightCenter.y - lightSize,
91 lightCenter.x + lightSize, lightCenter.y + lightSize);
92 lightRect.unionWith(localClip);
93 if (!lightRect.intersects(casterBounds)) {
94#if DEBUG_SHADOW
95 ALOGD("Spot shadow is out of clip rect!");
96#endif
97 return kVertexBufferMode_OnePolyRingShadow;
98 }
99
ztenghui50ecf842014-03-11 16:52:30 -0700100 VertexBufferMode mode = SpotShadow::createSpotShadow(isCasterOpaque,
101 casterPolygon, casterVertexCount, lightCenter, lightSize,
102 lightVertexCount, shadowVertexBuffer);
ztenghui7b4516e2014-01-07 10:42:55 -0800103
ztenghui50ecf842014-03-11 16:52:30 -0700104#if DEBUG_SHADOW
105 if(shadowVertexBuffer.getVertexCount() <= 0) {
106 ALOGD("Spot shadow generation failed %d", shadowVertexBuffer.getVertexCount());
107 }
108#endif
109 return mode;
ztenghui7b4516e2014-01-07 10:42:55 -0800110}
ztenghui63d41ab2014-02-14 13:13:41 -0800111
112void ShadowTessellator::generateShadowIndices(uint16_t* shadowIndices) {
113 int currentIndex = 0;
ztenghui63d41ab2014-02-14 13:13:41 -0800114 const int rays = SHADOW_RAY_COUNT;
115 // For the penumbra area.
ztenghui50ecf842014-03-11 16:52:30 -0700116 for (int layer = 0; layer < 2; layer ++) {
117 int baseIndex = layer * rays;
118 for (int i = 0; i < rays; i++) {
119 shadowIndices[currentIndex++] = i + baseIndex;
120 shadowIndices[currentIndex++] = rays + i + baseIndex;
121 }
122 // To close the loop, back to the ray 0.
123 shadowIndices[currentIndex++] = 0 + baseIndex;
124 // Note this is the same as the first index of next layer loop.
125 shadowIndices[currentIndex++] = rays + baseIndex;
ztenghui63d41ab2014-02-14 13:13:41 -0800126 }
ztenghui63d41ab2014-02-14 13:13:41 -0800127
128#if DEBUG_SHADOW
ztenghui50ecf842014-03-11 16:52:30 -0700129 if (currentIndex != MAX_SHADOW_INDEX_COUNT) {
130 ALOGW("vertex index count is wrong. current %d, expected %d",
131 currentIndex, MAX_SHADOW_INDEX_COUNT);
ztenghui63d41ab2014-02-14 13:13:41 -0800132 }
ztenghui50ecf842014-03-11 16:52:30 -0700133 for (int i = 0; i < MAX_SHADOW_INDEX_COUNT; i++) {
ztenghui63d41ab2014-02-14 13:13:41 -0800134 ALOGD("vertex index is (%d, %d)", i, shadowIndices[i]);
135 }
136#endif
137}
138
139/**
140 * Calculate the centroid of a 2d polygon.
141 *
142 * @param poly The polygon, which is represented in a Vector2 array.
143 * @param polyLength The length of the polygon in terms of number of vertices.
144 * @return the centroid of the polygon.
145 */
146Vector2 ShadowTessellator::centroid2d(const Vector2* poly, int polyLength) {
147 double sumx = 0;
148 double sumy = 0;
149 int p1 = polyLength - 1;
150 double area = 0;
151 for (int p2 = 0; p2 < polyLength; p2++) {
152 double x1 = poly[p1].x;
153 double y1 = poly[p1].y;
154 double x2 = poly[p2].x;
155 double y2 = poly[p2].y;
156 double a = (x1 * y2 - x2 * y1);
157 sumx += (x1 + x2) * a;
158 sumy += (y1 + y2) * a;
159 area += a;
160 p1 = p2;
161 }
162
163 Vector2 centroid = poly[0];
164 if (area != 0) {
165 centroid = Vector2(sumx / (3 * area), sumy / (3 * area));
166 } else {
ztenghui50ecf842014-03-11 16:52:30 -0700167 ALOGW("Area is 0 while computing centroid!");
ztenghui63d41ab2014-02-14 13:13:41 -0800168 }
169 return centroid;
170}
171
ztenghui2e023f32014-04-28 16:43:13 -0700172/**
173 * Test whether the polygon is order in clockwise.
174 *
175 * @param polygon the polygon as a Vector2 array
176 * @param len the number of points of the polygon
177 */
178bool ShadowTessellator::isClockwise(const Vector2* polygon, int len) {
179 double sum = 0;
180 double p1x = polygon[len - 1].x;
181 double p1y = polygon[len - 1].y;
182 for (int i = 0; i < len; i++) {
183
184 double p2x = polygon[i].x;
185 double p2y = polygon[i].y;
186 sum += p1x * p2y - p2x * p1y;
187 p1x = p2x;
188 p1y = p2y;
189 }
190 return sum < 0;
191}
192
193bool ShadowTessellator::isClockwisePath(const SkPath& path) {
194 SkPath::Iter iter(path, false);
195 SkPoint pts[4];
196 SkPath::Verb v;
197
198 Vector<Vector2> arrayForDirection;
199 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
200 switch (v) {
201 case SkPath::kMove_Verb:
202 arrayForDirection.add(Vector2(pts[0].x(), pts[0].y()));
203 break;
204 case SkPath::kLine_Verb:
205 arrayForDirection.add(Vector2(pts[1].x(), pts[1].y()));
206 break;
207 case SkPath::kQuad_Verb:
208 arrayForDirection.add(Vector2(pts[1].x(), pts[1].y()));
209 arrayForDirection.add(Vector2(pts[2].x(), pts[2].y()));
210 break;
211 case SkPath::kCubic_Verb:
212 arrayForDirection.add(Vector2(pts[1].x(), pts[1].y()));
213 arrayForDirection.add(Vector2(pts[2].x(), pts[2].y()));
214 arrayForDirection.add(Vector2(pts[3].x(), pts[3].y()));
215 break;
216 default:
217 break;
218 }
219 }
220
221 return isClockwise(arrayForDirection.array(), arrayForDirection.size());
222}
223
224void ShadowTessellator::reverseVertexArray(Vertex* polygon, int len) {
225 int n = len / 2;
226 for (int i = 0; i < n; i++) {
227 Vertex tmp = polygon[i];
228 int k = len - 1 - i;
229 polygon[i] = polygon[k];
230 polygon[k] = tmp;
231 }
232}
233
ztenghui55bfb4e2013-12-03 10:38:55 -0800234}; // namespace uirenderer
235}; // namespace android