Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1171)

Side by Side Diff: src/utils/SkShadowPaintFilterCanvas.cpp

Issue 2294323003: made point light shadows (Closed)
Patch Set: made req changes (comments) Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/utils/SkShadowPaintFilterCanvas.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkPathEffect.h" 8 #include "SkPathEffect.h"
9 #include "SkShadowPaintFilterCanvas.h" 9 #include "SkShadowPaintFilterCanvas.h"
10 10
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 44
45 *paint->writable() = newPaint; 45 *paint->writable() = newPaint;
46 } 46 }
47 47
48 return true; 48 return true;
49 } 49 }
50 50
51 SkISize SkShadowPaintFilterCanvas::ComputeDepthMapSize(const SkLights::Light& li ght, int maxDepth, 51 SkISize SkShadowPaintFilterCanvas::ComputeDepthMapSize(const SkLights::Light& li ght, int maxDepth,
52 int width, int height) { 52 int width, int height) {
53 if (light.type() != SkLights::Light::kDirectional_LightType) { 53 if (light.type() != SkLights::Light::kDirectional_LightType) {
54 return SkISize::Make(width *2 , height * 2); 54 // Calculating the right depth map size for point lights is complex,
55 // as it depends on the max depth, the max depth delta, the location
56 // of the point light and the shapes, etc... If we take upper bounds
57 // on those metrics, the shadow map will be pretty big in any case.
58 // Thus, just using 4x the width and height seems to work for most scene s.
59 return SkISize::Make(width * 4, height * 4);
55 } 60 }
56 61
57 int dMapWidth = SkMin32(maxDepth * fabs(light.dir().fX) + width, 62 int dMapWidth = SkMin32(maxDepth * fabs(light.dir().fX) + width,
58 width * 2); 63 width * 2);
59 int dMapHeight = SkMin32(maxDepth * fabs(light.dir().fY) + height, 64 int dMapHeight = SkMin32(maxDepth * fabs(light.dir().fY) + height,
60 height * 2); 65 height * 2);
61 return SkISize::Make(dMapWidth, dMapHeight); 66 return SkISize::Make(dMapWidth, dMapHeight);
62 } 67 }
63 68
64 void SkShadowPaintFilterCanvas::setShadowParams(const SkShadowParams &params) { 69 void SkShadowPaintFilterCanvas::setShadowParams(const SkShadowParams &params) {
65 fShadowParams = params; 70 fShadowParams = params;
66 } 71 }
67 72
68 void SkShadowPaintFilterCanvas::onDrawPicture(const SkPicture *picture, const Sk Matrix *matrix, 73 void SkShadowPaintFilterCanvas::onDrawPicture(const SkPicture *picture, const Sk Matrix *matrix,
69 const SkPaint *paint) { 74 const SkPaint *paint) {
70 SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint); 75 SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint);
71 if (this->onFilter(&filteredPaint, kPicture_Type)) { 76 if (this->onFilter(&filteredPaint, kPicture_Type)) {
72 SkCanvas::onDrawPicture(picture, matrix, filteredPaint); 77 SkCanvas::onDrawPicture(picture, matrix, filteredPaint);
73 } 78 }
74 } 79 }
75 80
76 void SkShadowPaintFilterCanvas::updateMatrix() { 81 void SkShadowPaintFilterCanvas::updateMatrix() {
77 this->save();
78
79 // It is up to the user to set the 0th light in fLights to 82 // It is up to the user to set the 0th light in fLights to
80 // the light the want to render the depth map with. 83 // the light the want to render the depth map with.
81 if (this->fLights->light(0).type() == SkLights::Light::kDirectional_LightTyp e) { 84 if (this->fLights->light(0).type() == SkLights::Light::kDirectional_LightTyp e) {
82 const SkVector3& lightDir = this->fLights->light(0).dir(); 85 const SkVector3& lightDir = this->fLights->light(0).dir();
83 SkScalar x = lightDir.fX * this->getZ(); 86 SkScalar x = lightDir.fX * this->getZ();
84 SkScalar y = lightDir.fY * this->getZ(); 87 SkScalar y = lightDir.fY * this->getZ();
85 88
86 this->translate(x, y); 89 this->translate(x, y);
90 } else if (this->fLights->light(0).type() == SkLights::Light::kPoint_LightTy pe) {
91 SkISize size = this->getBaseLayerSize();
92
93 SkPoint3 lightPos = this->fLights->light(0).pos();
94
95 // shadow maps for point lights are 4x the size of the diffuse map, by e xperimentation
96 // (see SPFCanvas::ComputeDepthMapSize())
97 SkScalar diffuseHeight = size.fHeight / 4.0f;
98
99 // move point light with canvas's CTM
100 SkPoint lightPoint = SkPoint::Make(lightPos.fX, diffuseHeight - lightPos .fY);
101 SkMatrix mat = this->getTotalMatrix();
102 if (mat.invert(&mat)) {
103 mat.mapPoints(&lightPoint, 1);
104 }
105 lightPoint.set(lightPoint.fX, diffuseHeight - lightPoint.fY);
106
107 // center the shadow map
108 // note: the 3/8 constant is specific to the 4.0 depth map size multipli er
109 mat = this->getTotalMatrix();
110 mat.postTranslate(size.width() * 0.375f, size.height() * 0.375f);
111 this->setMatrix(mat);
112
113 // project shapes onto canvas as shadows
114 SkScalar scale = (lightPos.fZ) / (lightPos.fZ - this->getZ());
115 this->scale(scale, scale);
116
117 this->translate(-lightPoint.fX * this->getZ() /
118 ((lightPos.fZ - this->getZ()) * scale),
119 -(diffuseHeight - lightPoint.fY) * this->getZ() /
120 ((lightPos.fZ - this->getZ()) * scale));
87 } 121 }
88 } 122 }
89 123
90 void SkShadowPaintFilterCanvas::onDrawPaint(const SkPaint &paint) { 124 void SkShadowPaintFilterCanvas::onDrawPaint(const SkPaint &paint) {
125 this->save();
91 this->updateMatrix(); 126 this->updateMatrix();
92 this->INHERITED::onDrawPaint(paint); 127 this->INHERITED::onDrawPaint(paint);
93 this->restore(); 128 this->restore();
94 } 129 }
95 130
96 void SkShadowPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], 131 void SkShadowPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
97 const SkPaint &paint) { 132 const SkPaint &paint) {
133 this->save();
98 this->updateMatrix(); 134 this->updateMatrix();
99 this->INHERITED::onDrawPoints(mode, count, pts, paint); 135 this->INHERITED::onDrawPoints(mode, count, pts, paint);
100 this->restore(); 136 this->restore();
101 } 137 }
102 138
103 void SkShadowPaintFilterCanvas::onDrawRect(const SkRect &rect, const SkPaint &pa int) { 139 void SkShadowPaintFilterCanvas::onDrawRect(const SkRect &rect, const SkPaint &pa int) {
140 this->save();
104 this->updateMatrix(); 141 this->updateMatrix();
105 this->INHERITED::onDrawRect(rect, paint); 142 this->INHERITED::onDrawRect(rect, paint);
106 this->restore(); 143 this->restore();
107 } 144 }
108 145
109 void SkShadowPaintFilterCanvas::onDrawRRect(const SkRRect &rrect, const SkPaint &paint) { 146 void SkShadowPaintFilterCanvas::onDrawRRect(const SkRRect &rrect, const SkPaint &paint) {
147 this->save();
110 this->updateMatrix(); 148 this->updateMatrix();
111 this->INHERITED::onDrawRRect(rrect, paint); 149 this->INHERITED::onDrawRRect(rrect, paint);
112 this->restore(); 150 this->restore();
113 } 151 }
114 152
115 void SkShadowPaintFilterCanvas::onDrawDRRect(const SkRRect &outer, const SkRRect &inner, 153 void SkShadowPaintFilterCanvas::onDrawDRRect(const SkRRect &outer, const SkRRect &inner,
116 const SkPaint &paint) { 154 const SkPaint &paint) {
155 this->save();
117 this->updateMatrix(); 156 this->updateMatrix();
118 this->INHERITED::onDrawDRRect(outer, inner, paint); 157 this->INHERITED::onDrawDRRect(outer, inner, paint);
119 this->restore(); 158 this->restore();
120 } 159 }
121 160
122 void SkShadowPaintFilterCanvas::onDrawOval(const SkRect &rect, const SkPaint &pa int) { 161 void SkShadowPaintFilterCanvas::onDrawOval(const SkRect &rect, const SkPaint &pa int) {
162 this->save();
123 this->updateMatrix(); 163 this->updateMatrix();
124 this->INHERITED::onDrawOval(rect, paint); 164 this->INHERITED::onDrawOval(rect, paint);
125 this->restore(); 165 this->restore();
126 } 166 }
127 167
128 void SkShadowPaintFilterCanvas::onDrawArc(const SkRect &rect, SkScalar startAngl e, 168 void SkShadowPaintFilterCanvas::onDrawArc(const SkRect &rect, SkScalar startAngl e,
129 SkScalar sweepAngle, bool useCenter, 169 SkScalar sweepAngle, bool useCenter,
130 const SkPaint &paint) { 170 const SkPaint &paint) {
171 this->save();
131 this->updateMatrix(); 172 this->updateMatrix();
132 this->INHERITED::onDrawArc(rect, startAngle, sweepAngle, useCenter, paint); 173 this->INHERITED::onDrawArc(rect, startAngle, sweepAngle, useCenter, paint);
133 this->restore(); 174 this->restore();
134 } 175 }
135 176
136 void SkShadowPaintFilterCanvas::onDrawPath(const SkPath &path, const SkPaint &pa int) { 177 void SkShadowPaintFilterCanvas::onDrawPath(const SkPath &path, const SkPaint &pa int) {
178 this->save();
137 this->updateMatrix(); 179 this->updateMatrix();
138 this->INHERITED::onDrawPath(path, paint); 180 this->INHERITED::onDrawPath(path, paint);
139 this->restore(); 181 this->restore();
140 } 182 }
141 183
142 void SkShadowPaintFilterCanvas::onDrawBitmap(const SkBitmap &bm, SkScalar left, SkScalar top, 184 void SkShadowPaintFilterCanvas::onDrawBitmap(const SkBitmap &bm, SkScalar left, SkScalar top,
143 const SkPaint *paint) { 185 const SkPaint *paint) {
186 this->save();
144 this->updateMatrix(); 187 this->updateMatrix();
145 this->INHERITED::onDrawBitmap(bm, left, top, paint); 188 this->INHERITED::onDrawBitmap(bm, left, top, paint);
146 this->restore(); 189 this->restore();
147 } 190 }
148 191
149 void SkShadowPaintFilterCanvas::onDrawBitmapRect(const SkBitmap &bm, const SkRec t *src, 192 void SkShadowPaintFilterCanvas::onDrawBitmapRect(const SkBitmap &bm, const SkRec t *src,
150 const SkRect &dst, const SkPain t *paint, 193 const SkRect &dst, const SkPain t *paint,
151 SrcRectConstraint constraint) { 194 SrcRectConstraint constraint) {
195 this->save();
152 this->updateMatrix(); 196 this->updateMatrix();
153 this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint); 197 this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint);
154 this->restore(); 198 this->restore();
155 } 199 }
156 200
157 void SkShadowPaintFilterCanvas::onDrawBitmapNine(const SkBitmap &bm, const SkIRe ct &center, 201 void SkShadowPaintFilterCanvas::onDrawBitmapNine(const SkBitmap &bm, const SkIRe ct &center,
158 const SkRect &dst, const SkPain t *paint) { 202 const SkRect &dst, const SkPain t *paint) {
203 this->save();
159 this->updateMatrix(); 204 this->updateMatrix();
160 this->INHERITED::onDrawBitmapNine(bm, center, dst, paint); 205 this->INHERITED::onDrawBitmapNine(bm, center, dst, paint);
161 this->restore(); 206 this->restore();
162 } 207 }
163 208
164 void SkShadowPaintFilterCanvas::onDrawImage(const SkImage *image, SkScalar left, 209 void SkShadowPaintFilterCanvas::onDrawImage(const SkImage *image, SkScalar left,
165 SkScalar top, const SkPaint *paint) { 210 SkScalar top, const SkPaint *paint) {
211 this->save();
166 this->updateMatrix(); 212 this->updateMatrix();
167 this->INHERITED::onDrawImage(image, left, top, paint); 213 this->INHERITED::onDrawImage(image, left, top, paint);
168 this->restore(); 214 this->restore();
169 } 215 }
170 216
171 void SkShadowPaintFilterCanvas::onDrawImageRect(const SkImage *image, const SkRe ct *src, 217 void SkShadowPaintFilterCanvas::onDrawImageRect(const SkImage *image, const SkRe ct *src,
172 const SkRect &dst, const SkPaint *paint, 218 const SkRect &dst, const SkPaint *paint,
173 SrcRectConstraint constraint) { 219 SrcRectConstraint constraint) {
220 this->save();
174 this->updateMatrix(); 221 this->updateMatrix();
175 this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint); 222 this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint);
176 this->restore(); 223 this->restore();
177 } 224 }
178 225
179 void SkShadowPaintFilterCanvas::onDrawImageNine(const SkImage *image, const SkIR ect &center, 226 void SkShadowPaintFilterCanvas::onDrawImageNine(const SkImage *image, const SkIR ect &center,
180 const SkRect &dst, const SkPaint *paint) { 227 const SkRect &dst, const SkPaint *paint) {
228 this->save();
181 this->updateMatrix(); 229 this->updateMatrix();
182 this->INHERITED::onDrawImageNine(image, center, dst, paint); 230 this->INHERITED::onDrawImageNine(image, center, dst, paint);
183 this->restore(); 231 this->restore();
184 } 232 }
185 233
186 234
187 void SkShadowPaintFilterCanvas::onDrawVertices(VertexMode vmode, int vertexCount , 235 void SkShadowPaintFilterCanvas::onDrawVertices(VertexMode vmode, int vertexCount ,
188 const SkPoint vertices[], const S kPoint texs[], 236 const SkPoint vertices[], const S kPoint texs[],
189 const SkColor colors[], SkXfermod e *xmode, 237 const SkColor colors[], SkXfermod e *xmode,
190 const uint16_t indices[], int ind exCount, 238 const uint16_t indices[], int ind exCount,
191 const SkPaint &paint) { 239 const SkPaint &paint) {
240 this->save();
192 this->updateMatrix(); 241 this->updateMatrix();
193 this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colors, 242 this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colors,
194 xmode, indices, indexCount, paint); 243 xmode, indices, indexCount, paint);
195 this->restore(); 244 this->restore();
196 } 245 }
197 246
198 void SkShadowPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColo r colors[], 247 void SkShadowPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColo r colors[],
199 const SkPoint texCoords[], SkXfermod e *xmode, 248 const SkPoint texCoords[], SkXfermod e *xmode,
200 const SkPaint &paint) { 249 const SkPaint &paint) {
250 this->save();
201 this->updateMatrix(); 251 this->updateMatrix();
202 this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint); 252 this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint);
203 this->restore(); 253 this->restore();
204 } 254 }
205 255
206 void SkShadowPaintFilterCanvas::onDrawText(const void *text, size_t byteLength, SkScalar x, 256 void SkShadowPaintFilterCanvas::onDrawText(const void *text, size_t byteLength, SkScalar x,
207 SkScalar y, const SkPaint &paint) { 257 SkScalar y, const SkPaint &paint) {
258 this->save();
208 this->updateMatrix(); 259 this->updateMatrix();
209 this->INHERITED::onDrawText(text, byteLength, x, y, paint); 260 this->INHERITED::onDrawText(text, byteLength, x, y, paint);
210 this->restore(); 261 this->restore();
211 } 262 }
212 263
213 void SkShadowPaintFilterCanvas::onDrawPosText(const void *text, size_t byteLengt h, 264 void SkShadowPaintFilterCanvas::onDrawPosText(const void *text, size_t byteLengt h,
214 const SkPoint pos[], const SkPaint &paint) { 265 const SkPoint pos[], const SkPaint &paint) {
266 this->save();
215 this->updateMatrix(); 267 this->updateMatrix();
216 this->INHERITED::onDrawPosText(text, byteLength, pos, paint); 268 this->INHERITED::onDrawPosText(text, byteLength, pos, paint);
217 this->restore(); 269 this->restore();
218 } 270 }
219 271
220 void SkShadowPaintFilterCanvas::onDrawPosTextH(const void *text, size_t byteLeng th, 272 void SkShadowPaintFilterCanvas::onDrawPosTextH(const void *text, size_t byteLeng th,
221 const SkScalar xpos[], 273 const SkScalar xpos[],
222 SkScalar constY, const SkPaint &p aint) { 274 SkScalar constY, const SkPaint &p aint) {
275 this->save();
223 this->updateMatrix(); 276 this->updateMatrix();
224 this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint); 277 this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint);
225 this->restore(); 278 this->restore();
226 } 279 }
227 280
228 void SkShadowPaintFilterCanvas::onDrawTextOnPath(const void *text, size_t byteLe ngth, 281 void SkShadowPaintFilterCanvas::onDrawTextOnPath(const void *text, size_t byteLe ngth,
229 const SkPath &path, const SkMat rix *matrix, 282 const SkPath &path, const SkMat rix *matrix,
230 const SkPaint &paint) { 283 const SkPaint &paint) {
284 this->save();
231 this->updateMatrix(); 285 this->updateMatrix();
232 this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint); 286 this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint);
233 this->restore(); 287 this->restore();
234 } 288 }
235 289
236 void SkShadowPaintFilterCanvas::onDrawTextRSXform(const void *text, size_t byteL ength, 290 void SkShadowPaintFilterCanvas::onDrawTextRSXform(const void *text, size_t byteL ength,
237 const SkRSXform xform[], const SkRect *cull, 291 const SkRSXform xform[], const SkRect *cull,
238 const SkPaint &paint) { 292 const SkPaint &paint) {
293 this->save();
239 this->updateMatrix(); 294 this->updateMatrix();
240 this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint); 295 this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint);
241 this->restore(); 296 this->restore();
242 } 297 }
243 298
244 void SkShadowPaintFilterCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, 299 void SkShadowPaintFilterCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y,
245 const SkPaint &paint) { 300 const SkPaint &paint) {
301 this->save();
246 this->updateMatrix(); 302 this->updateMatrix();
247 this->INHERITED::onDrawTextBlob(blob, x, y, paint); 303 this->INHERITED::onDrawTextBlob(blob, x, y, paint);
248 this->restore(); 304 this->restore();
249 } 305 }
250 306
251 #endif 307 #endif
OLDNEW
« no previous file with comments | « src/utils/SkShadowPaintFilterCanvas.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698