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 : fPaint(paint) {
17 fShouldDraw = canvas->onFilter(&fPaint, type);
18 }
19
AutoPaintFilter(const SkPaintFilterCanvas * canvas,Type type,const SkPaint & paint)20 AutoPaintFilter(const SkPaintFilterCanvas* canvas, Type type, const SkPaint& paint)
21 : AutoPaintFilter(canvas, type, &paint) { }
22
paint() const23 const SkPaint* paint() const { return fPaint; }
24
shouldDraw() const25 bool shouldDraw() const { return fShouldDraw; }
26
27 private:
28 SkTCopyOnFirstWrite<SkPaint> fPaint;
29 bool fShouldDraw;
30 };
31
SkPaintFilterCanvas(int width,int height)32 SkPaintFilterCanvas::SkPaintFilterCanvas(int width, int height) : INHERITED(width, height) { }
33
SkPaintFilterCanvas(SkCanvas * canvas)34 SkPaintFilterCanvas::SkPaintFilterCanvas(SkCanvas *canvas)
35 : INHERITED(canvas->imageInfo().width(), canvas->imageInfo().height()) {
36
37 // Transfer matrix & clip state before adding the target canvas.
38 SkIRect devClip;
39 canvas->getClipDeviceBounds(&devClip);
40 this->clipRect(SkRect::Make(devClip));
41 this->setMatrix(canvas->getTotalMatrix());
42
43 this->addCanvas(canvas);
44 }
45
onDrawPaint(const SkPaint & paint)46 void SkPaintFilterCanvas::onDrawPaint(const SkPaint& paint) {
47 AutoPaintFilter apf(this, kPaint_Type, paint);
48 if (apf.shouldDraw()) {
49 this->INHERITED::onDrawPaint(*apf.paint());
50 }
51 }
52
onDrawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)53 void SkPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
54 const SkPaint& paint) {
55 AutoPaintFilter apf(this, kPoint_Type, paint);
56 if (apf.shouldDraw()) {
57 this->INHERITED::onDrawPoints(mode, count, pts, *apf.paint());
58 }
59 }
60
onDrawRect(const SkRect & rect,const SkPaint & paint)61 void SkPaintFilterCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
62 AutoPaintFilter apf(this, kRect_Type, paint);
63 if (apf.shouldDraw()) {
64 this->INHERITED::onDrawRect(rect, *apf.paint());
65 }
66 }
67
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)68 void SkPaintFilterCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
69 AutoPaintFilter apf(this, kRRect_Type, paint);
70 if (apf.shouldDraw()) {
71 this->INHERITED::onDrawRRect(rrect, *apf.paint());
72 }
73 }
74
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)75 void SkPaintFilterCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
76 const SkPaint& paint) {
77 AutoPaintFilter apf(this, kDRRect_Type, paint);
78 if (apf.shouldDraw()) {
79 this->INHERITED::onDrawDRRect(outer, inner, *apf.paint());
80 }
81 }
82
onDrawOval(const SkRect & rect,const SkPaint & paint)83 void SkPaintFilterCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
84 AutoPaintFilter apf(this, kOval_Type, paint);
85 if (apf.shouldDraw()) {
86 this->INHERITED::onDrawOval(rect, *apf.paint());
87 }
88 }
89
onDrawPath(const SkPath & path,const SkPaint & paint)90 void SkPaintFilterCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
91 AutoPaintFilter apf(this, kPath_Type, paint);
92 if (apf.shouldDraw()) {
93 this->INHERITED::onDrawPath(path, *apf.paint());
94 }
95 }
96
onDrawBitmap(const SkBitmap & bm,SkScalar left,SkScalar top,const SkPaint * paint)97 void SkPaintFilterCanvas::onDrawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
98 const SkPaint* paint) {
99 AutoPaintFilter apf(this, kBitmap_Type, paint);
100 if (apf.shouldDraw()) {
101 this->INHERITED::onDrawBitmap(bm, left, top, apf.paint());
102 }
103 }
104
onDrawBitmapRect(const SkBitmap & bm,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)105 void SkPaintFilterCanvas::onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& dst,
106 const SkPaint* paint, SrcRectConstraint constraint) {
107 AutoPaintFilter apf(this, kBitmap_Type, paint);
108 if (apf.shouldDraw()) {
109 this->INHERITED::onDrawBitmapRect(bm, src, dst, apf.paint(), constraint);
110 }
111 }
112
onDrawBitmapNine(const SkBitmap & bm,const SkIRect & center,const SkRect & dst,const SkPaint * paint)113 void SkPaintFilterCanvas::onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center,
114 const SkRect& dst, const SkPaint* paint) {
115 AutoPaintFilter apf(this, kBitmap_Type, paint);
116 if (apf.shouldDraw()) {
117 this->INHERITED::onDrawBitmapNine(bm, center, dst, apf.paint());
118 }
119 }
120
onDrawImage(const SkImage * image,SkScalar left,SkScalar top,const SkPaint * paint)121 void SkPaintFilterCanvas::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
122 const SkPaint* paint) {
123 AutoPaintFilter apf(this, kBitmap_Type, paint);
124 if (apf.shouldDraw()) {
125 this->INHERITED::onDrawImage(image, left, top, apf.paint());
126 }
127 }
128
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)129 void SkPaintFilterCanvas::onDrawImageRect(const SkImage* image, const SkRect* src,
130 const SkRect& dst, const SkPaint* paint,
131 SrcRectConstraint constraint) {
132 AutoPaintFilter apf(this, kBitmap_Type, paint);
133 if (apf.shouldDraw()) {
134 this->INHERITED::onDrawImageRect(image, src, dst, apf.paint(), constraint);
135 }
136 }
137
onDrawImageNine(const SkImage * image,const SkIRect & center,const SkRect & dst,const SkPaint * paint)138 void SkPaintFilterCanvas::onDrawImageNine(const SkImage* image, const SkIRect& center,
139 const SkRect& dst, const SkPaint* paint) {
140 AutoPaintFilter apf(this, kBitmap_Type, paint);
141 if (apf.shouldDraw()) {
142 this->INHERITED::onDrawImageNine(image, center, dst, apf.paint());
143 }
144 }
145
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)146 void SkPaintFilterCanvas::onDrawVertices(VertexMode vmode, int vertexCount,
147 const SkPoint vertices[], const SkPoint texs[],
148 const SkColor colors[], SkXfermode* xmode,
149 const uint16_t indices[], int indexCount,
150 const SkPaint& paint) {
151 AutoPaintFilter apf(this, kVertices_Type, paint);
152 if (apf.shouldDraw()) {
153 this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colors, xmode, indices,
154 indexCount, *apf.paint());
155 }
156 }
157
onDrawPatch(const SkPoint cubics[],const SkColor colors[],const SkPoint texCoords[],SkXfermode * xmode,const SkPaint & paint)158 void SkPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColor colors[],
159 const SkPoint texCoords[], SkXfermode* xmode,
160 const SkPaint& paint) {
161 AutoPaintFilter apf(this, kPatch_Type, paint);
162 if (apf.shouldDraw()) {
163 this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, *apf.paint());
164 }
165 }
166
onDrawPicture(const SkPicture * picture,const SkMatrix * m,const SkPaint * paint)167 void SkPaintFilterCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* m,
168 const SkPaint* paint) {
169 AutoPaintFilter apf(this, kPicture_Type, paint);
170 if (apf.shouldDraw()) {
171 this->INHERITED::onDrawPicture(picture, m, apf.paint());
172 }
173 }
174
onDrawText(const void * text,size_t byteLength,SkScalar x,SkScalar y,const SkPaint & paint)175 void SkPaintFilterCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
176 const SkPaint& paint) {
177 AutoPaintFilter apf(this, kText_Type, paint);
178 if (apf.shouldDraw()) {
179 this->INHERITED::onDrawText(text, byteLength, x, y, *apf.paint());
180 }
181 }
182
onDrawPosText(const void * text,size_t byteLength,const SkPoint pos[],const SkPaint & paint)183 void SkPaintFilterCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
184 const SkPaint& paint) {
185 AutoPaintFilter apf(this, kText_Type, paint);
186 if (apf.shouldDraw()) {
187 this->INHERITED::onDrawPosText(text, byteLength, pos, *apf.paint());
188 }
189 }
190
onDrawPosTextH(const void * text,size_t byteLength,const SkScalar xpos[],SkScalar constY,const SkPaint & paint)191 void SkPaintFilterCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
192 SkScalar constY, const SkPaint& paint) {
193 AutoPaintFilter apf(this, kText_Type, paint);
194 if (apf.shouldDraw()) {
195 this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, *apf.paint());
196 }
197 }
198
onDrawTextOnPath(const void * text,size_t byteLength,const SkPath & path,const SkMatrix * matrix,const SkPaint & paint)199 void SkPaintFilterCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
200 const SkMatrix* matrix, const SkPaint& paint) {
201 AutoPaintFilter apf(this, kText_Type, paint);
202 if (apf.shouldDraw()) {
203 this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, *apf.paint());
204 }
205 }
206
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)207 void SkPaintFilterCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
208 const SkPaint& paint) {
209 AutoPaintFilter apf(this, kTextBlob_Type, paint);
210 if (apf.shouldDraw()) {
211 this->INHERITED::onDrawTextBlob(blob, x, y, *apf.paint());
212 }
213 }
214