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 #ifndef SkShadowPaintFilterCanvas_DEFINED
9 #define SkShadowPaintFilterCanvas_DEFINED
10 
11 #include "SkPaintFilterCanvas.h"
12 
13 #ifdef SK_EXPERIMENTAL_SHADOWING
14 
15 /** \class SkShadowPaintFilterCanvas
16  *
17  *  A utility proxy class for implementing shadow maps.
18  *
19  *  We override the onFilter method to draw depths into the canvas
20  *  depending on the current draw depth of the canvas, throwing out
21  *  the actual draw color.
22  *
23  *  Note that we can only do this for one light at a time!
24  *  It is up to the user to set the 0th light in fLights to
25  *  the light the want to render the depth map with.
26  */
27 class SkShadowPaintFilterCanvas : public SkPaintFilterCanvas {
28 public:
29 
30     SkShadowPaintFilterCanvas(SkCanvas *canvas);
31 
32     // TODO use a shader instead
33     bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const override;
34 
35     static SkISize ComputeDepthMapSize(const SkLights::Light& light, int maxDepth,
36                                        int width, int height);
37 
38     void setShadowParams(const SkShadowParams &params);
39 protected:
40     void updateMatrix();
41 
42     void onDrawPicture(const SkPicture *picture, const SkMatrix *matrix,
43                        const SkPaint *paint) override;
44 
45     void onDrawPaint(const SkPaint &paint) override;
46 
47     void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
48                       const SkPaint &paint) override;
49 
50     void onDrawRect(const SkRect &rect, const SkPaint &paint) override;
51 
52     void onDrawRRect(const SkRRect &rrect, const SkPaint &paint) override;
53 
54     void onDrawDRRect(const SkRRect &outer, const SkRRect &inner,
55                       const SkPaint &paint) override;
56 
57     void onDrawOval(const SkRect &rect, const SkPaint &paint) override;
58 
59     void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
60 
61     void onDrawPath(const SkPath &path, const SkPaint &paint) override;
62 
63     void onDrawBitmap(const SkBitmap &bm, SkScalar left, SkScalar top,
64                       const SkPaint *paint) override;
65 
66     void onDrawBitmapRect(const SkBitmap &bm, const SkRect *src, const SkRect &dst,
67                           const SkPaint *paint, SrcRectConstraint constraint) override;
68 
69     void onDrawBitmapNine(const SkBitmap &bm, const SkIRect &center,
70                           const SkRect &dst, const SkPaint *paint) override;
71 
72     void onDrawImage(const SkImage *image, SkScalar left, SkScalar top,
73                      const SkPaint *paint) override;
74 
75     void onDrawImageRect(const SkImage *image, const SkRect *src,
76                          const SkRect &dst, const SkPaint *paint,
77                          SrcRectConstraint constraint) override;
78 
79     void onDrawImageNine(const SkImage *image, const SkIRect &center,
80                          const SkRect &dst, const SkPaint *paint) override;
81 
82     void onDrawVertices(VertexMode vmode, int vertexCount,
83                         const SkPoint vertices[], const SkPoint texs[],
84                         const SkColor colors[], SkXfermode *xmode,
85                         const uint16_t indices[], int indexCount,
86                         const SkPaint &paint) override;
87 
88     void onDrawPatch(const SkPoint cubics[], const SkColor colors[],
89                      const SkPoint texCoords[], SkXfermode *xmode,
90                      const SkPaint &paint) override;
91 
92     void onDrawText(const void *text, size_t byteLength, SkScalar x, SkScalar y,
93                     const SkPaint &paint) override;
94 
95     void onDrawPosText(const void *text, size_t byteLength, const SkPoint pos[],
96                        const SkPaint &paint) override;
97 
98     void onDrawPosTextH(const void *text, size_t byteLength, const SkScalar xpos[],
99                         SkScalar constY, const SkPaint &paint) override;
100 
101     void onDrawTextOnPath(const void *text, size_t byteLength, const SkPath &path,
102                           const SkMatrix *matrix, const SkPaint &paint) override;
103 
104     void onDrawTextRSXform(const void *text, size_t byteLength,
105                            const SkRSXform xform[], const SkRect *cull,
106                            const SkPaint &paint) override;
107 
108     void onDrawTextBlob(const SkTextBlob *blob, SkScalar x,
109                         SkScalar y, const SkPaint &paint) override;
110 private:
111     SkShadowParams fShadowParams;
112     typedef SkPaintFilterCanvas INHERITED;
113 };
114 
115 
116 #endif
117 #endif
118