1 /*
2  * Copyright 2015 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 "SkPaintFilterCanvas.h"
9 
10 #include "SkPaint.h"
11 #include "SkTLazy.h"
12 
13 class SkPaintFilterCanvas::AutoPaintFilter {
14 public:
AutoPaintFilter(const SkPaintFilterCanvas * canvas,Type type,const SkPaint * paint)15     AutoPaintFilter(const SkPaintFilterCanvas* canvas, Type type, const SkPaint* paint) {
16         if (paint) {
17             canvas->onFilterPaint(fLazyPaint.set(*paint), type);
18         }
19     }
20 
AutoPaintFilter(const SkPaintFilterCanvas * canvas,Type type,const SkPaint & paint)21     AutoPaintFilter(const SkPaintFilterCanvas* canvas, Type type, const SkPaint& paint) {
22         canvas->onFilterPaint(fLazyPaint.set(paint), type);
23     }
24 
paint() const25     const SkPaint* paint() const { return fLazyPaint.getMaybeNull(); }
26 
27 private:
28     SkTLazy<SkPaint> fLazyPaint;
29 };
30 
SkPaintFilterCanvas(int width,int height)31 SkPaintFilterCanvas::SkPaintFilterCanvas(int width, int height) : INHERITED(width, height) { }
32 
onDrawPaint(const SkPaint & paint)33 void SkPaintFilterCanvas::onDrawPaint(const SkPaint& paint) {
34     AutoPaintFilter apf(this, kPaint_Type, paint);
35     this->INHERITED::onDrawPaint(*apf.paint());
36 }
37 
onDrawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)38 void SkPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
39                                        const SkPaint& paint) {
40     AutoPaintFilter apf(this, kPoint_Type, paint);
41     this->INHERITED::onDrawPoints(mode, count, pts, *apf.paint());
42 }
43 
onDrawRect(const SkRect & rect,const SkPaint & paint)44 void SkPaintFilterCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
45     AutoPaintFilter apf(this, kRect_Type, paint);
46     this->INHERITED::onDrawRect(rect, *apf.paint());
47 }
48 
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)49 void SkPaintFilterCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
50     AutoPaintFilter apf(this, kRRect_Type, paint);
51     this->INHERITED::onDrawRRect(rrect, *apf.paint());
52 }
53 
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)54 void SkPaintFilterCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
55                                        const SkPaint& paint) {
56     AutoPaintFilter apf(this, kDRRect_Type, paint);
57     this->INHERITED::onDrawDRRect(outer, inner, *apf.paint());
58 }
59 
onDrawOval(const SkRect & rect,const SkPaint & paint)60 void SkPaintFilterCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
61     AutoPaintFilter apf(this, kOval_Type, paint);
62     this->INHERITED::onDrawOval(rect, *apf.paint());
63 }
64 
onDrawPath(const SkPath & path,const SkPaint & paint)65 void SkPaintFilterCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
66     AutoPaintFilter apf(this, kPath_Type, paint);
67     this->INHERITED::onDrawPath(path, *apf.paint());
68 }
69 
onDrawBitmap(const SkBitmap & bm,SkScalar left,SkScalar top,const SkPaint * paint)70 void SkPaintFilterCanvas::onDrawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
71                                        const SkPaint* paint) {
72     AutoPaintFilter apf(this, kBitmap_Type, paint);
73     this->INHERITED::onDrawBitmap(bm, left, top, apf.paint());
74 }
75 
onDrawBitmapRect(const SkBitmap & bm,const SkRect * src,const SkRect & dst,const SkPaint * paint,DrawBitmapRectFlags flags)76 void SkPaintFilterCanvas::onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& dst,
77                                            const SkPaint* paint, DrawBitmapRectFlags flags) {
78     AutoPaintFilter apf(this, kBitmap_Type, paint);
79     this->INHERITED::onDrawBitmapRect(bm, src, dst, apf.paint(), flags);
80 }
81 
onDrawImage(const SkImage * image,SkScalar left,SkScalar top,const SkPaint * paint)82 void SkPaintFilterCanvas::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
83                                       const SkPaint* paint) {
84     AutoPaintFilter apf(this, kBitmap_Type, paint);
85     this->INHERITED::onDrawImage(image, left, top, apf.paint());
86 }
87 
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint * paint)88 void SkPaintFilterCanvas::onDrawImageRect(const SkImage* image, const SkRect* src,
89                                           const SkRect& dst, const SkPaint* paint) {
90     AutoPaintFilter apf(this, kBitmap_Type, paint);
91     this->INHERITED::onDrawImageRect(image, src, dst, apf.paint());
92 }
93 
onDrawBitmapNine(const SkBitmap & bm,const SkIRect & center,const SkRect & dst,const SkPaint * paint)94 void SkPaintFilterCanvas::onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center,
95                                            const SkRect& dst, const SkPaint* paint) {
96     AutoPaintFilter apf(this, kBitmap_Type, paint);
97     this->INHERITED::onDrawBitmapNine(bm, center, dst, apf.paint());
98 }
99 
onDrawSprite(const SkBitmap & bm,int left,int top,const SkPaint * paint)100 void SkPaintFilterCanvas::onDrawSprite(const SkBitmap& bm, int left, int top,
101                                        const SkPaint* paint) {
102     AutoPaintFilter apf(this, kBitmap_Type, paint);
103     this->INHERITED::onDrawSprite(bm, left, top, apf.paint());
104 }
105 
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)106 void SkPaintFilterCanvas::onDrawVertices(VertexMode vmode, int vertexCount,
107                                          const SkPoint vertices[], const SkPoint texs[],
108                                          const SkColor colors[], SkXfermode* xmode,
109                                          const uint16_t indices[], int indexCount,
110                                          const SkPaint& paint) {
111     AutoPaintFilter apf(this, kVertices_Type, paint);
112     this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colors, xmode, indices,
113                                     indexCount, *apf.paint());
114 }
115 
onDrawPatch(const SkPoint cubics[],const SkColor colors[],const SkPoint texCoords[],SkXfermode * xmode,const SkPaint & paint)116 void SkPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColor colors[],
117                                       const SkPoint texCoords[], SkXfermode* xmode,
118                                       const SkPaint& paint) {
119     AutoPaintFilter apf(this, kPatch_Type, paint);
120     this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, *apf.paint());
121 }
122 
onDrawPicture(const SkPicture * picture,const SkMatrix * m,const SkPaint * paint)123 void SkPaintFilterCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* m,
124                                         const SkPaint* paint) {
125     AutoPaintFilter apf(this, kPicture_Type, paint);
126     this->INHERITED::onDrawPicture(picture, m, apf.paint());
127 }
128 
onDrawText(const void * text,size_t byteLength,SkScalar x,SkScalar y,const SkPaint & paint)129 void SkPaintFilterCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
130                                      const SkPaint& paint) {
131     AutoPaintFilter apf(this, kText_Type, paint);
132     this->INHERITED::onDrawText(text, byteLength, x, y, *apf.paint());
133 }
134 
onDrawPosText(const void * text,size_t byteLength,const SkPoint pos[],const SkPaint & paint)135 void SkPaintFilterCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
136                                         const SkPaint& paint) {
137     AutoPaintFilter apf(this, kText_Type, paint);
138     this->INHERITED::onDrawPosText(text, byteLength, pos, *apf.paint());
139 }
140 
onDrawPosTextH(const void * text,size_t byteLength,const SkScalar xpos[],SkScalar constY,const SkPaint & paint)141 void SkPaintFilterCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
142                                          SkScalar constY, const SkPaint& paint) {
143     AutoPaintFilter apf(this, kText_Type, paint);
144     this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, *apf.paint());
145 }
146 
onDrawTextOnPath(const void * text,size_t byteLength,const SkPath & path,const SkMatrix * matrix,const SkPaint & paint)147 void SkPaintFilterCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
148                                            const SkMatrix* matrix, const SkPaint& paint) {
149     AutoPaintFilter apf(this, kText_Type, paint);
150     this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, *apf.paint());
151 }
152 
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)153 void SkPaintFilterCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
154                                          const SkPaint& paint) {
155     AutoPaintFilter apf(this, kTextBlob_Type, paint);
156     this->INHERITED::onDrawTextBlob(blob, x, y, *apf.paint());
157 }
158