blob: c1ffa0ab4807fd591a3f928116a707b16dfd43a3 [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"
Chris Craikf5be3ca2014-04-30 18:20:03 -070025#include "Caches.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080026#include "ShadowTessellator.h"
ztenghui7b4516e2014-01-07 10:42:55 -080027#include "SpotShadow.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080028
29namespace android {
30namespace uirenderer {
31
Chris Craik05f3d6e2014-06-02 16:27:04 -070032void ShadowTessellator::tessellateAmbientShadow(bool isCasterOpaque,
ztenghui50ecf842014-03-11 16:52:30 -070033 const Vector3* casterPolygon, int casterVertexCount,
ztenghuiaf6f7ed2014-03-18 17:25:49 -070034 const Vector3& centroid3d, const Rect& casterBounds,
35 const Rect& localClip, float maxZ, VertexBuffer& shadowVertexBuffer) {
Chris Craik87f9df82014-03-07 14:34:42 -080036 ATRACE_CALL();
37
ztenghui55bfb4e2013-12-03 10:38:55 -080038 // A bunch of parameters to tweak the shadow.
39 // TODO: Allow some of these changable by debug settings or APIs.
ztenghui8def74d2014-10-01 16:10:16 -070040 float heightFactor = 1.0f / 128;
ztenghui7b4516e2014-01-07 10:42:55 -080041 const float geomFactor = 64;
ztenghui55bfb4e2013-12-03 10:38:55 -080042
Chris Craikf5be3ca2014-04-30 18:20:03 -070043 Caches& caches = Caches::getInstance();
44 if (CC_UNLIKELY(caches.propertyAmbientRatio > 0.0f)) {
45 heightFactor *= caches.propertyAmbientRatio;
46 }
47
ztenghuiaf6f7ed2014-03-18 17:25:49 -070048 Rect ambientShadowBounds(casterBounds);
49 ambientShadowBounds.outset(maxZ * geomFactor * heightFactor);
50
51 if (!localClip.intersects(ambientShadowBounds)) {
52#if DEBUG_SHADOW
53 ALOGD("Ambient shadow is out of clip rect!");
54#endif
Chris Craik05f3d6e2014-06-02 16:27:04 -070055 return;
ztenghuiaf6f7ed2014-03-18 17:25:49 -070056 }
57
Chris Craik05f3d6e2014-06-02 16:27:04 -070058 AmbientShadow::createAmbientShadow(isCasterOpaque, casterPolygon,
ztenghui50ecf842014-03-11 16:52:30 -070059 casterVertexCount, centroid3d, heightFactor, geomFactor,
60 shadowVertexBuffer);
ztenghui55bfb4e2013-12-03 10:38:55 -080061}
62
Chris Craik05f3d6e2014-06-02 16:27:04 -070063void ShadowTessellator::tessellateSpotShadow(bool isCasterOpaque,
ztenghuic50a03d2014-08-21 13:47:54 -070064 const Vector3* casterPolygon, int casterVertexCount, const Vector3& casterCentroid,
Chris Craik797b95b2014-05-20 18:10:25 -070065 const mat4& receiverTransform, const Vector3& lightCenter, int lightRadius,
66 const Rect& casterBounds, const Rect& localClip, VertexBuffer& shadowVertexBuffer) {
Chris Craik87f9df82014-03-07 14:34:42 -080067 ATRACE_CALL();
68
Chris Craikf5be3ca2014-04-30 18:20:03 -070069 Caches& caches = Caches::getInstance();
70
Chris Craik797b95b2014-05-20 18:10:25 -070071 Vector3 adjustedLightCenter(lightCenter);
Chris Craikf5be3ca2014-04-30 18:20:03 -070072 if (CC_UNLIKELY(caches.propertyLightPosY > 0)) {
Chris Craik797b95b2014-05-20 18:10:25 -070073 adjustedLightCenter.y = - caches.propertyLightPosY; // negated since this shifts up
Chris Craikf5be3ca2014-04-30 18:20:03 -070074 }
75 if (CC_UNLIKELY(caches.propertyLightPosZ > 0)) {
Chris Craik797b95b2014-05-20 18:10:25 -070076 adjustedLightCenter.z = caches.propertyLightPosZ;
Chris Craikf5be3ca2014-04-30 18:20:03 -070077 }
78
ztenghui7b4516e2014-01-07 10:42:55 -080079#if DEBUG_SHADOW
Chris Craik797b95b2014-05-20 18:10:25 -070080 ALOGD("light center %f %f %f",
81 adjustedLightCenter.x, adjustedLightCenter.y, adjustedLightCenter.z);
ztenghui7b4516e2014-01-07 10:42:55 -080082#endif
Chris Craik3197cde2014-01-16 14:03:39 -080083
84 // light position (because it's in local space) needs to compensate for receiver transform
85 // TODO: should apply to light orientation, not just position
86 Matrix4 reverseReceiverTransform;
87 reverseReceiverTransform.loadInverse(receiverTransform);
Chris Craik797b95b2014-05-20 18:10:25 -070088 reverseReceiverTransform.mapPoint3d(adjustedLightCenter);
Chris Craik3197cde2014-01-16 14:03:39 -080089
Chris Craik726118b2014-03-07 18:27:49 -080090 const int lightVertexCount = 8;
Chris Craikf5be3ca2014-04-30 18:20:03 -070091 if (CC_UNLIKELY(caches.propertyLightDiameter > 0)) {
Chris Craik797b95b2014-05-20 18:10:25 -070092 lightRadius = caches.propertyLightDiameter;
Chris Craikf5be3ca2014-04-30 18:20:03 -070093 }
94
ztenghuiaf6f7ed2014-03-18 17:25:49 -070095 // Now light and caster are both in local space, we will check whether
96 // the shadow is within the clip area.
Chris Craik797b95b2014-05-20 18:10:25 -070097 Rect lightRect = Rect(adjustedLightCenter.x - lightRadius, adjustedLightCenter.y - lightRadius,
98 adjustedLightCenter.x + lightRadius, adjustedLightCenter.y + lightRadius);
ztenghuiaf6f7ed2014-03-18 17:25:49 -070099 lightRect.unionWith(localClip);
100 if (!lightRect.intersects(casterBounds)) {
101#if DEBUG_SHADOW
102 ALOGD("Spot shadow is out of clip rect!");
103#endif
Chris Craik05f3d6e2014-06-02 16:27:04 -0700104 return;
ztenghuiaf6f7ed2014-03-18 17:25:49 -0700105 }
106
ztenghuic50a03d2014-08-21 13:47:54 -0700107 SpotShadow::createSpotShadow(isCasterOpaque, adjustedLightCenter, lightRadius,
108 casterPolygon, casterVertexCount, casterCentroid, shadowVertexBuffer);
109
ztenghui50ecf842014-03-11 16:52:30 -0700110#if DEBUG_SHADOW
111 if(shadowVertexBuffer.getVertexCount() <= 0) {
112 ALOGD("Spot shadow generation failed %d", shadowVertexBuffer.getVertexCount());
113 }
114#endif
ztenghui7b4516e2014-01-07 10:42:55 -0800115}
ztenghui63d41ab2014-02-14 13:13:41 -0800116
117void ShadowTessellator::generateShadowIndices(uint16_t* shadowIndices) {
118 int currentIndex = 0;
ztenghui63d41ab2014-02-14 13:13:41 -0800119 const int rays = SHADOW_RAY_COUNT;
120 // For the penumbra area.
ztenghui50ecf842014-03-11 16:52:30 -0700121 for (int layer = 0; layer < 2; layer ++) {
122 int baseIndex = layer * rays;
123 for (int i = 0; i < rays; i++) {
124 shadowIndices[currentIndex++] = i + baseIndex;
125 shadowIndices[currentIndex++] = rays + i + baseIndex;
126 }
127 // To close the loop, back to the ray 0.
128 shadowIndices[currentIndex++] = 0 + baseIndex;
129 // Note this is the same as the first index of next layer loop.
130 shadowIndices[currentIndex++] = rays + baseIndex;
ztenghui63d41ab2014-02-14 13:13:41 -0800131 }
ztenghui63d41ab2014-02-14 13:13:41 -0800132
133#if DEBUG_SHADOW
ztenghui50ecf842014-03-11 16:52:30 -0700134 if (currentIndex != MAX_SHADOW_INDEX_COUNT) {
135 ALOGW("vertex index count is wrong. current %d, expected %d",
136 currentIndex, MAX_SHADOW_INDEX_COUNT);
ztenghui63d41ab2014-02-14 13:13:41 -0800137 }
ztenghui50ecf842014-03-11 16:52:30 -0700138 for (int i = 0; i < MAX_SHADOW_INDEX_COUNT; i++) {
ztenghui63d41ab2014-02-14 13:13:41 -0800139 ALOGD("vertex index is (%d, %d)", i, shadowIndices[i]);
140 }
141#endif
142}
143
144/**
145 * Calculate the centroid of a 2d polygon.
146 *
147 * @param poly The polygon, which is represented in a Vector2 array.
148 * @param polyLength The length of the polygon in terms of number of vertices.
149 * @return the centroid of the polygon.
150 */
151Vector2 ShadowTessellator::centroid2d(const Vector2* poly, int polyLength) {
152 double sumx = 0;
153 double sumy = 0;
154 int p1 = polyLength - 1;
155 double area = 0;
156 for (int p2 = 0; p2 < polyLength; p2++) {
157 double x1 = poly[p1].x;
158 double y1 = poly[p1].y;
159 double x2 = poly[p2].x;
160 double y2 = poly[p2].y;
161 double a = (x1 * y2 - x2 * y1);
162 sumx += (x1 + x2) * a;
163 sumy += (y1 + y2) * a;
164 area += a;
165 p1 = p2;
166 }
167
168 Vector2 centroid = poly[0];
169 if (area != 0) {
John Reck1aa5d2d2014-07-24 13:38:28 -0700170 centroid = (Vector2){static_cast<float>(sumx / (3 * area)),
171 static_cast<float>(sumy / (3 * area))};
ztenghui63d41ab2014-02-14 13:13:41 -0800172 } else {
ztenghui50ecf842014-03-11 16:52:30 -0700173 ALOGW("Area is 0 while computing centroid!");
ztenghui63d41ab2014-02-14 13:13:41 -0800174 }
175 return centroid;
176}
177
ztenghuic50a03d2014-08-21 13:47:54 -0700178// Make sure p1 -> p2 is going CW around the poly.
179Vector2 ShadowTessellator::calculateNormal(const Vector2& p1, const Vector2& p2) {
180 Vector2 result = p2 - p1;
181 if (result.x != 0 || result.y != 0) {
182 result.normalize();
183 // Calculate the normal , which is CCW 90 rotate to the delta.
184 float tempy = result.y;
185 result.y = result.x;
186 result.x = -tempy;
187 }
188 return result;
189}
ztenghui2e023f32014-04-28 16:43:13 -0700190/**
191 * Test whether the polygon is order in clockwise.
192 *
193 * @param polygon the polygon as a Vector2 array
194 * @param len the number of points of the polygon
195 */
196bool ShadowTessellator::isClockwise(const Vector2* polygon, int len) {
ztenghuif11310f2014-05-14 13:48:27 -0700197 if (len < 2 || polygon == NULL) {
ztenghuif11310f2014-05-14 13:48:27 -0700198 return true;
199 }
ztenghui2e023f32014-04-28 16:43:13 -0700200 double sum = 0;
201 double p1x = polygon[len - 1].x;
202 double p1y = polygon[len - 1].y;
203 for (int i = 0; i < len; i++) {
204
205 double p2x = polygon[i].x;
206 double p2y = polygon[i].y;
207 sum += p1x * p2y - p2x * p1y;
208 p1x = p2x;
209 p1y = p2y;
210 }
211 return sum < 0;
212}
213
214bool ShadowTessellator::isClockwisePath(const SkPath& path) {
215 SkPath::Iter iter(path, false);
216 SkPoint pts[4];
217 SkPath::Verb v;
218
219 Vector<Vector2> arrayForDirection;
220 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
221 switch (v) {
222 case SkPath::kMove_Verb:
John Reck1aa5d2d2014-07-24 13:38:28 -0700223 arrayForDirection.add((Vector2){pts[0].x(), pts[0].y()});
ztenghui2e023f32014-04-28 16:43:13 -0700224 break;
225 case SkPath::kLine_Verb:
John Reck1aa5d2d2014-07-24 13:38:28 -0700226 arrayForDirection.add((Vector2){pts[1].x(), pts[1].y()});
ztenghui2e023f32014-04-28 16:43:13 -0700227 break;
228 case SkPath::kQuad_Verb:
John Reck1aa5d2d2014-07-24 13:38:28 -0700229 arrayForDirection.add((Vector2){pts[1].x(), pts[1].y()});
230 arrayForDirection.add((Vector2){pts[2].x(), pts[2].y()});
ztenghui2e023f32014-04-28 16:43:13 -0700231 break;
232 case SkPath::kCubic_Verb:
John Reck1aa5d2d2014-07-24 13:38:28 -0700233 arrayForDirection.add((Vector2){pts[1].x(), pts[1].y()});
234 arrayForDirection.add((Vector2){pts[2].x(), pts[2].y()});
235 arrayForDirection.add((Vector2){pts[3].x(), pts[3].y()});
ztenghui2e023f32014-04-28 16:43:13 -0700236 break;
237 default:
238 break;
239 }
240 }
241
242 return isClockwise(arrayForDirection.array(), arrayForDirection.size());
243}
244
245void ShadowTessellator::reverseVertexArray(Vertex* polygon, int len) {
246 int n = len / 2;
247 for (int i = 0; i < n; i++) {
248 Vertex tmp = polygon[i];
249 int k = len - 1 - i;
250 polygon[i] = polygon[k];
251 polygon[k] = tmp;
252 }
253}
254
ztenghui512e6432014-09-10 13:08:20 -0700255int ShadowTessellator::getExtraVertexNumber(const Vector2& vector1,
256 const Vector2& vector2, float divisor) {
257 // When there is no distance difference, there is no need for extra vertices.
258 if (vector1.lengthSquared() == 0 || vector2.lengthSquared() == 0) {
259 return 0;
260 }
261 // The formula is :
262 // extraNumber = floor(acos(dot(n1, n2)) / (M_PI / EXTRA_VERTEX_PER_PI))
263 // The value ranges for each step are:
264 // dot( ) --- [-1, 1]
265 // acos( ) --- [0, M_PI]
266 // floor(...) --- [0, EXTRA_VERTEX_PER_PI]
267 float dotProduct = vector1.dot(vector2);
268 // TODO: Use look up table for the dotProduct to extraVerticesNumber
269 // computation, if needed.
270 float angle = acosf(dotProduct);
271 return (int) floor(angle / divisor);
272}
273
274void ShadowTessellator::checkOverflow(int used, int total, const char* bufferName) {
275 LOG_ALWAYS_FATAL_IF(used > total, "Error: %s overflow!!! used %d, total %d",
276 bufferName, used, total);
277}
278
ztenghui55bfb4e2013-12-03 10:38:55 -0800279}; // namespace uirenderer
280}; // namespace android