blob: 2f714a1b6ad8b3651f8a1ff609e443ea4980a742 [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
ztenghui7b4516e2014-01-07 10:42:55 -080032template<typename T>
33static inline T max(T a, T b) {
34 return a > b ? a : b;
35}
36
ztenghui50ecf842014-03-11 16:52:30 -070037VertexBufferMode ShadowTessellator::tessellateAmbientShadow(bool isCasterOpaque,
38 const Vector3* casterPolygon, int casterVertexCount,
ztenghuiaf6f7ed2014-03-18 17:25:49 -070039 const Vector3& centroid3d, const Rect& casterBounds,
40 const Rect& localClip, float maxZ, VertexBuffer& shadowVertexBuffer) {
Chris Craik87f9df82014-03-07 14:34:42 -080041 ATRACE_CALL();
42
ztenghui55bfb4e2013-12-03 10:38:55 -080043 // A bunch of parameters to tweak the shadow.
44 // TODO: Allow some of these changable by debug settings or APIs.
Chris Craikf5be3ca2014-04-30 18:20:03 -070045 float heightFactor = 1.0f / 128;
ztenghui7b4516e2014-01-07 10:42:55 -080046 const float geomFactor = 64;
ztenghui55bfb4e2013-12-03 10:38:55 -080047
Chris Craikf5be3ca2014-04-30 18:20:03 -070048 Caches& caches = Caches::getInstance();
49 if (CC_UNLIKELY(caches.propertyAmbientRatio > 0.0f)) {
50 heightFactor *= caches.propertyAmbientRatio;
51 }
52
ztenghuiaf6f7ed2014-03-18 17:25:49 -070053 Rect ambientShadowBounds(casterBounds);
54 ambientShadowBounds.outset(maxZ * geomFactor * heightFactor);
55
56 if (!localClip.intersects(ambientShadowBounds)) {
57#if DEBUG_SHADOW
58 ALOGD("Ambient shadow is out of clip rect!");
59#endif
60 return kVertexBufferMode_OnePolyRingShadow;
61 }
62
ztenghui50ecf842014-03-11 16:52:30 -070063 return AmbientShadow::createAmbientShadow(isCasterOpaque, casterPolygon,
64 casterVertexCount, centroid3d, heightFactor, geomFactor,
65 shadowVertexBuffer);
ztenghui55bfb4e2013-12-03 10:38:55 -080066
67}
68
ztenghui50ecf842014-03-11 16:52:30 -070069VertexBufferMode ShadowTessellator::tessellateSpotShadow(bool isCasterOpaque,
70 const Vector3* casterPolygon, int casterVertexCount,
Chris Craik797b95b2014-05-20 18:10:25 -070071 const mat4& receiverTransform, const Vector3& lightCenter, int lightRadius,
72 const Rect& casterBounds, const Rect& localClip, VertexBuffer& shadowVertexBuffer) {
Chris Craik87f9df82014-03-07 14:34:42 -080073 ATRACE_CALL();
74
Chris Craikf5be3ca2014-04-30 18:20:03 -070075 Caches& caches = Caches::getInstance();
76
Chris Craik797b95b2014-05-20 18:10:25 -070077 Vector3 adjustedLightCenter(lightCenter);
Chris Craikf5be3ca2014-04-30 18:20:03 -070078 if (CC_UNLIKELY(caches.propertyLightPosY > 0)) {
Chris Craik797b95b2014-05-20 18:10:25 -070079 adjustedLightCenter.y = - caches.propertyLightPosY; // negated since this shifts up
Chris Craikf5be3ca2014-04-30 18:20:03 -070080 }
81 if (CC_UNLIKELY(caches.propertyLightPosZ > 0)) {
Chris Craik797b95b2014-05-20 18:10:25 -070082 adjustedLightCenter.z = caches.propertyLightPosZ;
Chris Craikf5be3ca2014-04-30 18:20:03 -070083 }
84
ztenghui7b4516e2014-01-07 10:42:55 -080085#if DEBUG_SHADOW
Chris Craik797b95b2014-05-20 18:10:25 -070086 ALOGD("light center %f %f %f",
87 adjustedLightCenter.x, adjustedLightCenter.y, adjustedLightCenter.z);
ztenghui7b4516e2014-01-07 10:42:55 -080088#endif
Chris Craik3197cde2014-01-16 14:03:39 -080089
90 // light position (because it's in local space) needs to compensate for receiver transform
91 // TODO: should apply to light orientation, not just position
92 Matrix4 reverseReceiverTransform;
93 reverseReceiverTransform.loadInverse(receiverTransform);
Chris Craik797b95b2014-05-20 18:10:25 -070094 reverseReceiverTransform.mapPoint3d(adjustedLightCenter);
Chris Craik3197cde2014-01-16 14:03:39 -080095
Chris Craik726118b2014-03-07 18:27:49 -080096 const int lightVertexCount = 8;
Chris Craikf5be3ca2014-04-30 18:20:03 -070097 if (CC_UNLIKELY(caches.propertyLightDiameter > 0)) {
Chris Craik797b95b2014-05-20 18:10:25 -070098 lightRadius = caches.propertyLightDiameter;
Chris Craikf5be3ca2014-04-30 18:20:03 -070099 }
100
ztenghuiaf6f7ed2014-03-18 17:25:49 -0700101 // Now light and caster are both in local space, we will check whether
102 // the shadow is within the clip area.
Chris Craik797b95b2014-05-20 18:10:25 -0700103 Rect lightRect = Rect(adjustedLightCenter.x - lightRadius, adjustedLightCenter.y - lightRadius,
104 adjustedLightCenter.x + lightRadius, adjustedLightCenter.y + lightRadius);
ztenghuiaf6f7ed2014-03-18 17:25:49 -0700105 lightRect.unionWith(localClip);
106 if (!lightRect.intersects(casterBounds)) {
107#if DEBUG_SHADOW
108 ALOGD("Spot shadow is out of clip rect!");
109#endif
110 return kVertexBufferMode_OnePolyRingShadow;
111 }
112
ztenghui50ecf842014-03-11 16:52:30 -0700113 VertexBufferMode mode = SpotShadow::createSpotShadow(isCasterOpaque,
Chris Craik797b95b2014-05-20 18:10:25 -0700114 casterPolygon, casterVertexCount, adjustedLightCenter, lightRadius,
ztenghui50ecf842014-03-11 16:52:30 -0700115 lightVertexCount, shadowVertexBuffer);
ztenghui7b4516e2014-01-07 10:42:55 -0800116
ztenghui50ecf842014-03-11 16:52:30 -0700117#if DEBUG_SHADOW
118 if(shadowVertexBuffer.getVertexCount() <= 0) {
119 ALOGD("Spot shadow generation failed %d", shadowVertexBuffer.getVertexCount());
120 }
121#endif
122 return mode;
ztenghui7b4516e2014-01-07 10:42:55 -0800123}
ztenghui63d41ab2014-02-14 13:13:41 -0800124
125void ShadowTessellator::generateShadowIndices(uint16_t* shadowIndices) {
126 int currentIndex = 0;
ztenghui63d41ab2014-02-14 13:13:41 -0800127 const int rays = SHADOW_RAY_COUNT;
128 // For the penumbra area.
ztenghui50ecf842014-03-11 16:52:30 -0700129 for (int layer = 0; layer < 2; layer ++) {
130 int baseIndex = layer * rays;
131 for (int i = 0; i < rays; i++) {
132 shadowIndices[currentIndex++] = i + baseIndex;
133 shadowIndices[currentIndex++] = rays + i + baseIndex;
134 }
135 // To close the loop, back to the ray 0.
136 shadowIndices[currentIndex++] = 0 + baseIndex;
137 // Note this is the same as the first index of next layer loop.
138 shadowIndices[currentIndex++] = rays + baseIndex;
ztenghui63d41ab2014-02-14 13:13:41 -0800139 }
ztenghui63d41ab2014-02-14 13:13:41 -0800140
141#if DEBUG_SHADOW
ztenghui50ecf842014-03-11 16:52:30 -0700142 if (currentIndex != MAX_SHADOW_INDEX_COUNT) {
143 ALOGW("vertex index count is wrong. current %d, expected %d",
144 currentIndex, MAX_SHADOW_INDEX_COUNT);
ztenghui63d41ab2014-02-14 13:13:41 -0800145 }
ztenghui50ecf842014-03-11 16:52:30 -0700146 for (int i = 0; i < MAX_SHADOW_INDEX_COUNT; i++) {
ztenghui63d41ab2014-02-14 13:13:41 -0800147 ALOGD("vertex index is (%d, %d)", i, shadowIndices[i]);
148 }
149#endif
150}
151
152/**
153 * Calculate the centroid of a 2d polygon.
154 *
155 * @param poly The polygon, which is represented in a Vector2 array.
156 * @param polyLength The length of the polygon in terms of number of vertices.
157 * @return the centroid of the polygon.
158 */
159Vector2 ShadowTessellator::centroid2d(const Vector2* poly, int polyLength) {
160 double sumx = 0;
161 double sumy = 0;
162 int p1 = polyLength - 1;
163 double area = 0;
164 for (int p2 = 0; p2 < polyLength; p2++) {
165 double x1 = poly[p1].x;
166 double y1 = poly[p1].y;
167 double x2 = poly[p2].x;
168 double y2 = poly[p2].y;
169 double a = (x1 * y2 - x2 * y1);
170 sumx += (x1 + x2) * a;
171 sumy += (y1 + y2) * a;
172 area += a;
173 p1 = p2;
174 }
175
176 Vector2 centroid = poly[0];
177 if (area != 0) {
178 centroid = Vector2(sumx / (3 * area), sumy / (3 * area));
179 } else {
ztenghui50ecf842014-03-11 16:52:30 -0700180 ALOGW("Area is 0 while computing centroid!");
ztenghui63d41ab2014-02-14 13:13:41 -0800181 }
182 return centroid;
183}
184
ztenghui2e023f32014-04-28 16:43:13 -0700185/**
186 * Test whether the polygon is order in clockwise.
187 *
188 * @param polygon the polygon as a Vector2 array
189 * @param len the number of points of the polygon
190 */
191bool ShadowTessellator::isClockwise(const Vector2* polygon, int len) {
ztenghuif11310f2014-05-14 13:48:27 -0700192 if (len < 2 || polygon == NULL) {
193 ALOGW("Invalid polygon %p, length is %d @ isClockwise()", polygon, len);
194 return true;
195 }
ztenghui2e023f32014-04-28 16:43:13 -0700196 double sum = 0;
197 double p1x = polygon[len - 1].x;
198 double p1y = polygon[len - 1].y;
199 for (int i = 0; i < len; i++) {
200
201 double p2x = polygon[i].x;
202 double p2y = polygon[i].y;
203 sum += p1x * p2y - p2x * p1y;
204 p1x = p2x;
205 p1y = p2y;
206 }
207 return sum < 0;
208}
209
210bool ShadowTessellator::isClockwisePath(const SkPath& path) {
211 SkPath::Iter iter(path, false);
212 SkPoint pts[4];
213 SkPath::Verb v;
214
215 Vector<Vector2> arrayForDirection;
216 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
217 switch (v) {
218 case SkPath::kMove_Verb:
219 arrayForDirection.add(Vector2(pts[0].x(), pts[0].y()));
220 break;
221 case SkPath::kLine_Verb:
222 arrayForDirection.add(Vector2(pts[1].x(), pts[1].y()));
223 break;
224 case SkPath::kQuad_Verb:
225 arrayForDirection.add(Vector2(pts[1].x(), pts[1].y()));
226 arrayForDirection.add(Vector2(pts[2].x(), pts[2].y()));
227 break;
228 case SkPath::kCubic_Verb:
229 arrayForDirection.add(Vector2(pts[1].x(), pts[1].y()));
230 arrayForDirection.add(Vector2(pts[2].x(), pts[2].y()));
231 arrayForDirection.add(Vector2(pts[3].x(), pts[3].y()));
232 break;
233 default:
234 break;
235 }
236 }
237
238 return isClockwise(arrayForDirection.array(), arrayForDirection.size());
239}
240
241void ShadowTessellator::reverseVertexArray(Vertex* polygon, int len) {
242 int n = len / 2;
243 for (int i = 0; i < n; i++) {
244 Vertex tmp = polygon[i];
245 int k = len - 1 - i;
246 polygon[i] = polygon[k];
247 polygon[k] = tmp;
248 }
249}
250
ztenghui55bfb4e2013-12-03 10:38:55 -0800251}; // namespace uirenderer
252}; // namespace android