1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkPathEffect.h"
9 #include "SkShadowPaintFilterCanvas.h"
10 
11 #ifdef SK_EXPERIMENTAL_SHADOWING
12 
SkShadowPaintFilterCanvas(SkCanvas * canvas)13 SkShadowPaintFilterCanvas::SkShadowPaintFilterCanvas(SkCanvas *canvas)
14         : SkPaintFilterCanvas(canvas) {
15     fShadowParams.fShadowRadius = 0.0f;
16     fShadowParams.fType = SkShadowParams::kNoBlur_ShadowType;
17     fShadowParams.fBiasingConstant = 0.0f;
18     fShadowParams.fMinVariance = 0.0f;
19 }
20 
21 // TODO use a shader instead
onFilter(SkTCopyOnFirstWrite<SkPaint> * paint,Type type) const22 bool SkShadowPaintFilterCanvas::onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const {
23     if (*paint) {
24         int z = this->getZ();
25         SkASSERT(z <= 0xFF && z >= 0x00);
26 
27         SkPaint newPaint;
28         newPaint.setPathEffect(sk_ref_sp<SkPathEffect>((*paint)->getPathEffect()));
29 
30         SkColor color = 0xFF000000; // init color to opaque black
31         color |= z; // Put the index into the blue component
32 
33         if (fShadowParams.fType == SkShadowParams::kVariance_ShadowType) {
34             int z2 = z * z;
35             if (z2 > 255 * 256) {
36                 color |= 0xff00;
37             } else {
38                 // Let's only store the more significant bits of z2 to save space.
39                 // In practice, this should barely impact shadow blur quality.
40                 color |= z2 & 0x0000ff00;
41             }
42         }
43         newPaint.setColor(color);
44 
45         *paint->writable() = newPaint;
46     }
47 
48     return true;
49 }
50 
ComputeDepthMapSize(const SkLights::Light & light,int maxDepth,int width,int height)51 SkISize SkShadowPaintFilterCanvas::ComputeDepthMapSize(const SkLights::Light& light, int maxDepth,
52                                                        int width, int height) {
53     if (light.type() != SkLights::Light::kDirectional_LightType) {
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 scenes.
59         return SkISize::Make(width * 4, height * 4);
60     }
61 
62     int dMapWidth = SkMin32(maxDepth * fabs(light.dir().fX) + width,
63                             width * 2);
64     int dMapHeight = SkMin32(maxDepth * fabs(light.dir().fY) + height,
65                              height * 2);
66     return SkISize::Make(dMapWidth, dMapHeight);
67 }
68 
setShadowParams(const SkShadowParams & params)69 void SkShadowPaintFilterCanvas::setShadowParams(const SkShadowParams &params) {
70     fShadowParams = params;
71 }
72 
onDrawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)73 void SkShadowPaintFilterCanvas::onDrawPicture(const SkPicture *picture, const SkMatrix *matrix,
74                                               const SkPaint *paint) {
75     SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint);
76     if (this->onFilter(&filteredPaint, kPicture_Type)) {
77         SkCanvas::onDrawPicture(picture, matrix, filteredPaint);
78     }
79 }
80 
updateMatrix()81 void SkShadowPaintFilterCanvas::updateMatrix() {
82     //  It is up to the user to set the 0th light in fLights to
83     //  the light the want to render the depth map with.
84     if (this->fLights->light(0).type() == SkLights::Light::kDirectional_LightType) {
85         const SkVector3& lightDir = this->fLights->light(0).dir();
86         SkScalar x = lightDir.fX * this->getZ();
87         SkScalar y = lightDir.fY * this->getZ();
88 
89         this->translate(x, y);
90     } else if (this->fLights->light(0).type() == SkLights::Light::kPoint_LightType) {
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 experimentation
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 multiplier
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));
121     }
122 }
123 
onDrawPaint(const SkPaint & paint)124 void SkShadowPaintFilterCanvas::onDrawPaint(const SkPaint &paint) {
125     this->save();
126     this->updateMatrix();
127     this->INHERITED::onDrawPaint(paint);
128     this->restore();
129 }
130 
onDrawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)131 void SkShadowPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
132                                              const SkPaint &paint) {
133     this->save();
134     this->updateMatrix();
135     this->INHERITED::onDrawPoints(mode, count, pts, paint);
136     this->restore();
137 }
138 
onDrawRect(const SkRect & rect,const SkPaint & paint)139 void SkShadowPaintFilterCanvas::onDrawRect(const SkRect &rect, const SkPaint &paint) {
140     this->save();
141     this->updateMatrix();
142     this->INHERITED::onDrawRect(rect, paint);
143     this->restore();
144 }
145 
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)146 void SkShadowPaintFilterCanvas::onDrawRRect(const SkRRect &rrect, const SkPaint &paint) {
147     this->save();
148     this->updateMatrix();
149     this->INHERITED::onDrawRRect(rrect, paint);
150     this->restore();
151 }
152 
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)153 void SkShadowPaintFilterCanvas::onDrawDRRect(const SkRRect &outer, const SkRRect &inner,
154                   const SkPaint &paint) {
155     this->save();
156     this->updateMatrix();
157     this->INHERITED::onDrawDRRect(outer, inner, paint);
158     this->restore();
159 }
160 
onDrawOval(const SkRect & rect,const SkPaint & paint)161 void SkShadowPaintFilterCanvas::onDrawOval(const SkRect &rect, const SkPaint &paint) {
162     this->save();
163     this->updateMatrix();
164     this->INHERITED::onDrawOval(rect, paint);
165     this->restore();
166 }
167 
onDrawArc(const SkRect & rect,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)168 void SkShadowPaintFilterCanvas::onDrawArc(const SkRect &rect, SkScalar startAngle,
169                                           SkScalar sweepAngle, bool useCenter,
170                                           const SkPaint &paint) {
171     this->save();
172     this->updateMatrix();
173     this->INHERITED::onDrawArc(rect, startAngle, sweepAngle, useCenter, paint);
174     this->restore();
175 }
176 
onDrawPath(const SkPath & path,const SkPaint & paint)177 void SkShadowPaintFilterCanvas::onDrawPath(const SkPath &path, const SkPaint &paint) {
178     this->save();
179     this->updateMatrix();
180     this->INHERITED::onDrawPath(path, paint);
181     this->restore();
182 }
183 
onDrawBitmap(const SkBitmap & bm,SkScalar left,SkScalar top,const SkPaint * paint)184 void SkShadowPaintFilterCanvas::onDrawBitmap(const SkBitmap &bm, SkScalar left, SkScalar top,
185                                              const SkPaint *paint) {
186     this->save();
187     this->updateMatrix();
188     this->INHERITED::onDrawBitmap(bm, left, top, paint);
189     this->restore();
190 }
191 
onDrawBitmapRect(const SkBitmap & bm,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)192 void SkShadowPaintFilterCanvas::onDrawBitmapRect(const SkBitmap &bm, const SkRect *src,
193                                                  const SkRect &dst, const SkPaint *paint,
194                                                  SrcRectConstraint constraint) {
195     this->save();
196     this->updateMatrix();
197     this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint);
198     this->restore();
199 }
200 
onDrawBitmapNine(const SkBitmap & bm,const SkIRect & center,const SkRect & dst,const SkPaint * paint)201 void SkShadowPaintFilterCanvas::onDrawBitmapNine(const SkBitmap &bm, const SkIRect &center,
202                                                  const SkRect &dst, const SkPaint *paint) {
203     this->save();
204     this->updateMatrix();
205     this->INHERITED::onDrawBitmapNine(bm, center, dst, paint);
206     this->restore();
207 }
208 
onDrawImage(const SkImage * image,SkScalar left,SkScalar top,const SkPaint * paint)209 void SkShadowPaintFilterCanvas::onDrawImage(const SkImage *image, SkScalar left,
210                                             SkScalar top, const SkPaint *paint) {
211     this->save();
212     this->updateMatrix();
213     this->INHERITED::onDrawImage(image, left, top, paint);
214     this->restore();
215 }
216 
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)217 void SkShadowPaintFilterCanvas::onDrawImageRect(const SkImage *image, const SkRect *src,
218                                                 const SkRect &dst, const SkPaint *paint,
219                                                 SrcRectConstraint constraint) {
220     this->save();
221     this->updateMatrix();
222     this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint);
223     this->restore();
224 }
225 
onDrawImageNine(const SkImage * image,const SkIRect & center,const SkRect & dst,const SkPaint * paint)226 void SkShadowPaintFilterCanvas::onDrawImageNine(const SkImage *image, const SkIRect &center,
227                                                 const SkRect &dst, const SkPaint *paint) {
228     this->save();
229     this->updateMatrix();
230     this->INHERITED::onDrawImageNine(image, center, dst, paint);
231     this->restore();
232 }
233 
234 
onDrawVertices(VertexMode vmode,int vertexCount,const SkPoint vertices[],const SkPoint texs[],const SkColor colors[],SkXfermode * xmode,const uint16_t indices[],int indexCount,const SkPaint & paint)235 void SkShadowPaintFilterCanvas::onDrawVertices(VertexMode vmode, int vertexCount,
236                                                const SkPoint vertices[], const SkPoint texs[],
237                                                const SkColor colors[], SkXfermode *xmode,
238                                                const uint16_t indices[], int indexCount,
239                                                const SkPaint &paint) {
240     this->save();
241     this->updateMatrix();
242     this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colors,
243                                     xmode, indices, indexCount, paint);
244     this->restore();
245 }
246 
onDrawPatch(const SkPoint cubics[],const SkColor colors[],const SkPoint texCoords[],SkXfermode * xmode,const SkPaint & paint)247 void SkShadowPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColor colors[],
248                                             const SkPoint texCoords[], SkXfermode *xmode,
249                                             const SkPaint &paint) {
250     this->save();
251     this->updateMatrix();
252     this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint);
253     this->restore();
254 }
255 
onDrawText(const void * text,size_t byteLength,SkScalar x,SkScalar y,const SkPaint & paint)256 void SkShadowPaintFilterCanvas::onDrawText(const void *text, size_t byteLength, SkScalar x,
257                                            SkScalar y, const SkPaint &paint) {
258     this->save();
259     this->updateMatrix();
260     this->INHERITED::onDrawText(text, byteLength, x, y, paint);
261     this->restore();
262 }
263 
onDrawPosText(const void * text,size_t byteLength,const SkPoint pos[],const SkPaint & paint)264 void SkShadowPaintFilterCanvas::onDrawPosText(const void *text, size_t byteLength,
265                                               const SkPoint pos[], const SkPaint &paint) {
266     this->save();
267     this->updateMatrix();
268     this->INHERITED::onDrawPosText(text, byteLength, pos, paint);
269     this->restore();
270 }
271 
onDrawPosTextH(const void * text,size_t byteLength,const SkScalar xpos[],SkScalar constY,const SkPaint & paint)272 void SkShadowPaintFilterCanvas::onDrawPosTextH(const void *text, size_t byteLength,
273                                                const SkScalar xpos[],
274                                                SkScalar constY, const SkPaint &paint) {
275     this->save();
276     this->updateMatrix();
277     this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint);
278     this->restore();
279 }
280 
onDrawTextOnPath(const void * text,size_t byteLength,const SkPath & path,const SkMatrix * matrix,const SkPaint & paint)281 void SkShadowPaintFilterCanvas::onDrawTextOnPath(const void *text, size_t byteLength,
282                                                  const SkPath &path, const SkMatrix *matrix,
283                                                  const SkPaint &paint) {
284     this->save();
285     this->updateMatrix();
286     this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint);
287     this->restore();
288 }
289 
onDrawTextRSXform(const void * text,size_t byteLength,const SkRSXform xform[],const SkRect * cull,const SkPaint & paint)290 void SkShadowPaintFilterCanvas::onDrawTextRSXform(const void *text, size_t byteLength,
291                                                   const SkRSXform xform[], const SkRect *cull,
292                                                   const SkPaint &paint) {
293     this->save();
294     this->updateMatrix();
295     this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint);
296     this->restore();
297 }
298 
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)299 void SkShadowPaintFilterCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y,
300                                                const SkPaint &paint) {
301     this->save();
302     this->updateMatrix();
303     this->INHERITED::onDrawTextBlob(blob, x, y, paint);
304     this->restore();
305 }
306 
307 #endif
308