1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #include "SkNWayCanvas.h"
9 
SkNWayCanvas(int width,int height)10 SkNWayCanvas::SkNWayCanvas(int width, int height)
11         : INHERITED(width, height) {}
12 
~SkNWayCanvas()13 SkNWayCanvas::~SkNWayCanvas() {
14     this->removeAll();
15 }
16 
addCanvas(SkCanvas * canvas)17 void SkNWayCanvas::addCanvas(SkCanvas* canvas) {
18     if (canvas) {
19         canvas->ref();
20         *fList.append() = canvas;
21     }
22 }
23 
removeCanvas(SkCanvas * canvas)24 void SkNWayCanvas::removeCanvas(SkCanvas* canvas) {
25     int index = fList.find(canvas);
26     if (index >= 0) {
27         canvas->unref();
28         fList.removeShuffle(index);
29     }
30 }
31 
removeAll()32 void SkNWayCanvas::removeAll() {
33     fList.unrefAll();
34     fList.reset();
35 }
36 
37 ///////////////////////////////////////////////////////////////////////////
38 // These are forwarded to the N canvases we're referencing
39 
40 class SkNWayCanvas::Iter {
41 public:
Iter(const SkTDArray<SkCanvas * > & list)42     Iter(const SkTDArray<SkCanvas*>& list) : fList(list) {
43         fIndex = 0;
44     }
next()45     bool next() {
46         if (fIndex < fList.count()) {
47             fCanvas = fList[fIndex++];
48             return true;
49         }
50         return false;
51     }
operator ->()52     SkCanvas* operator->() { return fCanvas; }
53 
54 private:
55     const SkTDArray<SkCanvas*>& fList;
56     int fIndex;
57     SkCanvas* fCanvas;
58 };
59 
willSave()60 void SkNWayCanvas::willSave() {
61     Iter iter(fList);
62     while (iter.next()) {
63         iter->save();
64     }
65 
66     this->INHERITED::willSave();
67 }
68 
getSaveLayerStrategy(const SaveLayerRec & rec)69 SkCanvas::SaveLayerStrategy SkNWayCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
70     Iter iter(fList);
71     while (iter.next()) {
72         iter->saveLayer(rec);
73     }
74 
75     this->INHERITED::getSaveLayerStrategy(rec);
76     // No need for a layer.
77     return kNoLayer_SaveLayerStrategy;
78 }
79 
willRestore()80 void SkNWayCanvas::willRestore() {
81     Iter iter(fList);
82     while (iter.next()) {
83         iter->restore();
84     }
85     this->INHERITED::willRestore();
86 }
87 
didConcat(const SkMatrix & matrix)88 void SkNWayCanvas::didConcat(const SkMatrix& matrix) {
89     Iter iter(fList);
90     while (iter.next()) {
91         iter->concat(matrix);
92     }
93     this->INHERITED::didConcat(matrix);
94 }
95 
didSetMatrix(const SkMatrix & matrix)96 void SkNWayCanvas::didSetMatrix(const SkMatrix& matrix) {
97     Iter iter(fList);
98     while (iter.next()) {
99         iter->setMatrix(matrix);
100     }
101     this->INHERITED::didSetMatrix(matrix);
102 }
103 
onClipRect(const SkRect & rect,SkRegion::Op op,ClipEdgeStyle edgeStyle)104 void SkNWayCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
105     Iter iter(fList);
106     while (iter.next()) {
107         iter->clipRect(rect, op, kSoft_ClipEdgeStyle == edgeStyle);
108     }
109     this->INHERITED::onClipRect(rect, op, edgeStyle);
110 }
111 
onClipRRect(const SkRRect & rrect,SkRegion::Op op,ClipEdgeStyle edgeStyle)112 void SkNWayCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
113     Iter iter(fList);
114     while (iter.next()) {
115         iter->clipRRect(rrect, op, kSoft_ClipEdgeStyle == edgeStyle);
116     }
117     this->INHERITED::onClipRRect(rrect, op, edgeStyle);
118 }
119 
onClipPath(const SkPath & path,SkRegion::Op op,ClipEdgeStyle edgeStyle)120 void SkNWayCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
121     Iter iter(fList);
122     while (iter.next()) {
123         iter->clipPath(path, op, kSoft_ClipEdgeStyle == edgeStyle);
124     }
125     this->INHERITED::onClipPath(path, op, edgeStyle);
126 }
127 
onClipRegion(const SkRegion & deviceRgn,SkRegion::Op op)128 void SkNWayCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
129     Iter iter(fList);
130     while (iter.next()) {
131         iter->clipRegion(deviceRgn, op);
132     }
133     this->INHERITED::onClipRegion(deviceRgn, op);
134 }
135 
onDrawPaint(const SkPaint & paint)136 void SkNWayCanvas::onDrawPaint(const SkPaint& paint) {
137     Iter iter(fList);
138     while (iter.next()) {
139         iter->drawPaint(paint);
140     }
141 }
142 
onDrawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)143 void SkNWayCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
144                                 const SkPaint& paint) {
145     Iter iter(fList);
146     while (iter.next()) {
147         iter->drawPoints(mode, count, pts, paint);
148     }
149 }
150 
onDrawRect(const SkRect & rect,const SkPaint & paint)151 void SkNWayCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
152     Iter iter(fList);
153     while (iter.next()) {
154         iter->drawRect(rect, paint);
155     }
156 }
157 
onDrawOval(const SkRect & rect,const SkPaint & paint)158 void SkNWayCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
159     Iter iter(fList);
160     while (iter.next()) {
161         iter->drawOval(rect, paint);
162     }
163 }
164 
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)165 void SkNWayCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
166     Iter iter(fList);
167     while (iter.next()) {
168         iter->drawRRect(rrect, paint);
169     }
170 }
171 
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)172 void SkNWayCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
173     Iter iter(fList);
174     while (iter.next()) {
175         iter->drawDRRect(outer, inner, paint);
176     }
177 }
178 
onDrawPath(const SkPath & path,const SkPaint & paint)179 void SkNWayCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
180     Iter iter(fList);
181     while (iter.next()) {
182         iter->drawPath(path, paint);
183     }
184 }
185 
onDrawBitmap(const SkBitmap & bitmap,SkScalar x,SkScalar y,const SkPaint * paint)186 void SkNWayCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
187                                 const SkPaint* paint) {
188     Iter iter(fList);
189     while (iter.next()) {
190         iter->drawBitmap(bitmap, x, y, paint);
191     }
192 }
193 
onDrawBitmapRect(const SkBitmap & bitmap,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)194 void SkNWayCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
195                                     const SkPaint* paint, SrcRectConstraint constraint) {
196     Iter iter(fList);
197     while (iter.next()) {
198         iter->legacy_drawBitmapRect(bitmap, src, dst, paint, (SrcRectConstraint)constraint);
199     }
200 }
201 
onDrawBitmapNine(const SkBitmap & bitmap,const SkIRect & center,const SkRect & dst,const SkPaint * paint)202 void SkNWayCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
203                                     const SkRect& dst, const SkPaint* paint) {
204     Iter iter(fList);
205     while (iter.next()) {
206         iter->drawBitmapNine(bitmap, center, dst, paint);
207     }
208 }
209 
onDrawImage(const SkImage * image,SkScalar left,SkScalar top,const SkPaint * paint)210 void SkNWayCanvas::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
211                                const SkPaint* paint) {
212     Iter iter(fList);
213     while (iter.next()) {
214         iter->drawImage(image, left, top, paint);
215     }
216 }
217 
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)218 void SkNWayCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
219                                    const SkPaint* paint, SrcRectConstraint constraint) {
220     Iter iter(fList);
221     while (iter.next()) {
222         iter->legacy_drawImageRect(image, src, dst, paint, constraint);
223     }
224 }
225 
onDrawText(const void * text,size_t byteLength,SkScalar x,SkScalar y,const SkPaint & paint)226 void SkNWayCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
227                               const SkPaint& paint) {
228     Iter iter(fList);
229     while (iter.next()) {
230         iter->drawText(text, byteLength, x, y, paint);
231     }
232 }
233 
onDrawPosText(const void * text,size_t byteLength,const SkPoint pos[],const SkPaint & paint)234 void SkNWayCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
235                                  const SkPaint& paint) {
236     Iter iter(fList);
237     while (iter.next()) {
238         iter->drawPosText(text, byteLength, pos, paint);
239     }
240 }
241 
onDrawPosTextH(const void * text,size_t byteLength,const SkScalar xpos[],SkScalar constY,const SkPaint & paint)242 void SkNWayCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
243                                   SkScalar constY, const SkPaint& paint) {
244     Iter iter(fList);
245     while (iter.next()) {
246         iter->drawPosTextH(text, byteLength, xpos, constY, paint);
247     }
248 }
249 
onDrawTextOnPath(const void * text,size_t byteLength,const SkPath & path,const SkMatrix * matrix,const SkPaint & paint)250 void SkNWayCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
251                                     const SkMatrix* matrix, const SkPaint& paint) {
252     Iter iter(fList);
253     while (iter.next()) {
254         iter->drawTextOnPath(text, byteLength, path, matrix, paint);
255     }
256 }
257 
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)258 void SkNWayCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
259                                   const SkPaint &paint) {
260     Iter iter(fList);
261     while (iter.next()) {
262         iter->drawTextBlob(blob, x, y, paint);
263     }
264 }
265 
onDrawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)266 void SkNWayCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
267                                  const SkPaint* paint) {
268     Iter iter(fList);
269     while (iter.next()) {
270         iter->drawPicture(picture, matrix, paint);
271     }
272 }
273 
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)274 void SkNWayCanvas::onDrawVertices(VertexMode vmode, int vertexCount,
275                                   const SkPoint vertices[], const SkPoint texs[],
276                                   const SkColor colors[], SkXfermode* xmode,
277                                   const uint16_t indices[], int indexCount,
278                                   const SkPaint& paint) {
279     Iter iter(fList);
280     while (iter.next()) {
281         iter->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
282                            indices, indexCount, paint);
283     }
284 }
285 
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkXfermode * xmode,const SkPaint & paint)286 void SkNWayCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
287                                const SkPoint texCoords[4], SkXfermode* xmode,
288                                const SkPaint& paint) {
289     Iter iter(fList);
290     while (iter.next()) {
291         iter->drawPatch(cubics, colors, texCoords, xmode, paint);
292     }
293 }
294 
295 #ifdef SK_SUPPORT_LEGACY_DRAWFILTER
setDrawFilter(SkDrawFilter * filter)296 SkDrawFilter* SkNWayCanvas::setDrawFilter(SkDrawFilter* filter) {
297     Iter iter(fList);
298     while (iter.next()) {
299         iter->setDrawFilter(filter);
300     }
301     return this->INHERITED::setDrawFilter(filter);
302 }
303 #endif
304