blob: 55b82e490ae2a6efefcfd338926fc3c52cacadab [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 Craikf5be3ca2014-04-30 18:20:03 -070071 const mat4& receiverTransform,
ztenghuiaf6f7ed2014-03-18 17:25:49 -070072 int screenWidth, int screenHeight, const Rect& casterBounds,
73 const Rect& localClip, VertexBuffer& shadowVertexBuffer) {
Chris Craik87f9df82014-03-07 14:34:42 -080074 ATRACE_CALL();
75
Chris Craikf5be3ca2014-04-30 18:20:03 -070076 Caches& caches = Caches::getInstance();
77
ztenghui7b4516e2014-01-07 10:42:55 -080078 // A bunch of parameters to tweak the shadow.
79 // TODO: Allow some of these changable by debug settings or APIs.
ztenghui7b4516e2014-01-07 10:42:55 -080080 int maximal = max(screenWidth, screenHeight);
Chris Craikf5be3ca2014-04-30 18:20:03 -070081 Vector3 lightCenter(screenWidth * 0.5f, 0, maximal);
82
83 if (CC_UNLIKELY(caches.propertyLightPosY > 0)) {
84 lightCenter.y = - caches.propertyLightPosY; // negated since this shifts up
85 }
86 if (CC_UNLIKELY(caches.propertyLightPosZ > 0)) {
87 lightCenter.z = caches.propertyLightPosZ;
88 }
89
90
ztenghui7b4516e2014-01-07 10:42:55 -080091#if DEBUG_SHADOW
92 ALOGD("light center %f %f %f", lightCenter.x, lightCenter.y, lightCenter.z);
93#endif
Chris Craik3197cde2014-01-16 14:03:39 -080094
95 // light position (because it's in local space) needs to compensate for receiver transform
96 // TODO: should apply to light orientation, not just position
97 Matrix4 reverseReceiverTransform;
98 reverseReceiverTransform.loadInverse(receiverTransform);
99 reverseReceiverTransform.mapPoint3d(lightCenter);
100
Chris Craikf5be3ca2014-04-30 18:20:03 -0700101 float lightSize = maximal / 4;
Chris Craik726118b2014-03-07 18:27:49 -0800102 const int lightVertexCount = 8;
ztenghui7b4516e2014-01-07 10:42:55 -0800103
Chris Craikf5be3ca2014-04-30 18:20:03 -0700104 if (CC_UNLIKELY(caches.propertyLightDiameter > 0)) {
105 lightSize = caches.propertyLightDiameter;
106 }
107
ztenghuiaf6f7ed2014-03-18 17:25:49 -0700108 // Now light and caster are both in local space, we will check whether
109 // the shadow is within the clip area.
110 Rect lightRect = Rect(lightCenter.x - lightSize, lightCenter.y - lightSize,
111 lightCenter.x + lightSize, lightCenter.y + lightSize);
112 lightRect.unionWith(localClip);
113 if (!lightRect.intersects(casterBounds)) {
114#if DEBUG_SHADOW
115 ALOGD("Spot shadow is out of clip rect!");
116#endif
117 return kVertexBufferMode_OnePolyRingShadow;
118 }
119
ztenghui50ecf842014-03-11 16:52:30 -0700120 VertexBufferMode mode = SpotShadow::createSpotShadow(isCasterOpaque,
121 casterPolygon, casterVertexCount, lightCenter, lightSize,
122 lightVertexCount, shadowVertexBuffer);
ztenghui7b4516e2014-01-07 10:42:55 -0800123
ztenghui50ecf842014-03-11 16:52:30 -0700124#if DEBUG_SHADOW
125 if(shadowVertexBuffer.getVertexCount() <= 0) {
126 ALOGD("Spot shadow generation failed %d", shadowVertexBuffer.getVertexCount());
127 }
128#endif
129 return mode;
ztenghui7b4516e2014-01-07 10:42:55 -0800130}
ztenghui63d41ab2014-02-14 13:13:41 -0800131
132void ShadowTessellator::generateShadowIndices(uint16_t* shadowIndices) {
133 int currentIndex = 0;
ztenghui63d41ab2014-02-14 13:13:41 -0800134 const int rays = SHADOW_RAY_COUNT;
135 // For the penumbra area.
ztenghui50ecf842014-03-11 16:52:30 -0700136 for (int layer = 0; layer < 2; layer ++) {
137 int baseIndex = layer * rays;
138 for (int i = 0; i < rays; i++) {
139 shadowIndices[currentIndex++] = i + baseIndex;
140 shadowIndices[currentIndex++] = rays + i + baseIndex;
141 }
142 // To close the loop, back to the ray 0.
143 shadowIndices[currentIndex++] = 0 + baseIndex;
144 // Note this is the same as the first index of next layer loop.
145 shadowIndices[currentIndex++] = rays + baseIndex;
ztenghui63d41ab2014-02-14 13:13:41 -0800146 }
ztenghui63d41ab2014-02-14 13:13:41 -0800147
148#if DEBUG_SHADOW
ztenghui50ecf842014-03-11 16:52:30 -0700149 if (currentIndex != MAX_SHADOW_INDEX_COUNT) {
150 ALOGW("vertex index count is wrong. current %d, expected %d",
151 currentIndex, MAX_SHADOW_INDEX_COUNT);
ztenghui63d41ab2014-02-14 13:13:41 -0800152 }
ztenghui50ecf842014-03-11 16:52:30 -0700153 for (int i = 0; i < MAX_SHADOW_INDEX_COUNT; i++) {
ztenghui63d41ab2014-02-14 13:13:41 -0800154 ALOGD("vertex index is (%d, %d)", i, shadowIndices[i]);
155 }
156#endif
157}
158
159/**
160 * Calculate the centroid of a 2d polygon.
161 *
162 * @param poly The polygon, which is represented in a Vector2 array.
163 * @param polyLength The length of the polygon in terms of number of vertices.
164 * @return the centroid of the polygon.
165 */
166Vector2 ShadowTessellator::centroid2d(const Vector2* poly, int polyLength) {
167 double sumx = 0;
168 double sumy = 0;
169 int p1 = polyLength - 1;
170 double area = 0;
171 for (int p2 = 0; p2 < polyLength; p2++) {
172 double x1 = poly[p1].x;
173 double y1 = poly[p1].y;
174 double x2 = poly[p2].x;
175 double y2 = poly[p2].y;
176 double a = (x1 * y2 - x2 * y1);
177 sumx += (x1 + x2) * a;
178 sumy += (y1 + y2) * a;
179 area += a;
180 p1 = p2;
181 }
182
183 Vector2 centroid = poly[0];
184 if (area != 0) {
185 centroid = Vector2(sumx / (3 * area), sumy / (3 * area));
186 } else {
ztenghui50ecf842014-03-11 16:52:30 -0700187 ALOGW("Area is 0 while computing centroid!");
ztenghui63d41ab2014-02-14 13:13:41 -0800188 }
189 return centroid;
190}
191
ztenghui2e023f32014-04-28 16:43:13 -0700192/**
193 * Test whether the polygon is order in clockwise.
194 *
195 * @param polygon the polygon as a Vector2 array
196 * @param len the number of points of the polygon
197 */
198bool ShadowTessellator::isClockwise(const Vector2* polygon, int len) {
ztenghuif11310f2014-05-14 13:48:27 -0700199 if (len < 2 || polygon == NULL) {
200 ALOGW("Invalid polygon %p, length is %d @ isClockwise()", polygon, len);
201 return true;
202 }
ztenghui2e023f32014-04-28 16:43:13 -0700203 double sum = 0;
204 double p1x = polygon[len - 1].x;
205 double p1y = polygon[len - 1].y;
206 for (int i = 0; i < len; i++) {
207
208 double p2x = polygon[i].x;
209 double p2y = polygon[i].y;
210 sum += p1x * p2y - p2x * p1y;
211 p1x = p2x;
212 p1y = p2y;
213 }
214 return sum < 0;
215}
216
217bool ShadowTessellator::isClockwisePath(const SkPath& path) {
218 SkPath::Iter iter(path, false);
219 SkPoint pts[4];
220 SkPath::Verb v;
221
222 Vector<Vector2> arrayForDirection;
223 while (SkPath::kDone_Verb != (v = iter.next(pts))) {
224 switch (v) {
225 case SkPath::kMove_Verb:
226 arrayForDirection.add(Vector2(pts[0].x(), pts[0].y()));
227 break;
228 case SkPath::kLine_Verb:
229 arrayForDirection.add(Vector2(pts[1].x(), pts[1].y()));
230 break;
231 case SkPath::kQuad_Verb:
232 arrayForDirection.add(Vector2(pts[1].x(), pts[1].y()));
233 arrayForDirection.add(Vector2(pts[2].x(), pts[2].y()));
234 break;
235 case SkPath::kCubic_Verb:
236 arrayForDirection.add(Vector2(pts[1].x(), pts[1].y()));
237 arrayForDirection.add(Vector2(pts[2].x(), pts[2].y()));
238 arrayForDirection.add(Vector2(pts[3].x(), pts[3].y()));
239 break;
240 default:
241 break;
242 }
243 }
244
245 return isClockwise(arrayForDirection.array(), arrayForDirection.size());
246}
247
248void ShadowTessellator::reverseVertexArray(Vertex* polygon, int len) {
249 int n = len / 2;
250 for (int i = 0; i < n; i++) {
251 Vertex tmp = polygon[i];
252 int k = len - 1 - i;
253 polygon[i] = polygon[k];
254 polygon[k] = tmp;
255 }
256}
257
ztenghui55bfb4e2013-12-03 10:38:55 -0800258}; // namespace uirenderer
259}; // namespace android