Debian Codegraph @

src/utils/SkNWayCanvas.cpp

raw
1 /*
2 * Copyright 2011 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 "include/utils/SkNWayCanvas.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRSXform.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkShader.h"
19 #include "include/core/SkTypes.h"
20 #include "include/private/base/SkTDArray.h"
21 #include "include/utils/SkNoDrawCanvas.h"
22 #include "src/core/SkCanvasPriv.h"
23
24 #include <algorithm>
25 #include <iterator>
26 #include <utility>
27
28 namespace sktext {
29 class GlyphRunList;
30 }
31
32 class SkData;
33 class SkDrawable;
34 class SkImage;
35 class SkPaint;
36 class SkPath;
37 class SkPicture;
38 class SkRRect;
39 class SkRegion;
40 class SkTextBlob;
41 class SkVertices;
42 enum class SkBlendMode;
43 enum class SkClipOp;
44 struct SkDrawShadowRec;
45
46 SkNWayCanvas::SkNWayCanvas(int width, int height) : INHERITED(width, height) {}
47
48 SkNWayCanvas::~SkNWayCanvas() {
49 this->removeAll();
50 }
51
52 void SkNWayCanvas::addCanvas(SkCanvas* canvas) {
53 if (!fList.empty()) {
54 // We are using the nway canvas as a wrapper for the originally added canvas, and the device
55 // on the nway may contradict calls for the device on this canvas. So, to add a second
56 // canvas, the devices on the first canvas, and the nway base device must be different.
57 SkASSERT(fList[0]->rootDevice() != this->rootDevice());
58 }
59 if (canvas) {
60 *fList.append() = canvas;
61 }
62 }
63
64 void SkNWayCanvas::removeCanvas(SkCanvas* canvas) {
65 auto found = std::find(fList.begin(), fList.end(), canvas);
66 if (found != fList.end()) {
67 fList.removeShuffle(std::distance(fList.begin(), found));
68 }
69 }
70
71 void SkNWayCanvas::removeAll() {
72 fList.reset();
73 }
74
75 ///////////////////////////////////////////////////////////////////////////
76 // These are forwarded to the N canvases we're referencing
77
78 class SkNWayCanvas::Iter {
79 public:
80 Iter(const SkTDArray<SkCanvas*>& list) : fList(list) {
81 fIndex = 0;
82 }
83 bool next() {
84 if (fIndex < fList.size()) {
85 fCanvas = fList[fIndex++];
86 return true;
87 }
88 return false;
89 }
90 SkCanvas* operator->() { return fCanvas; }
91 SkCanvas* get() const { return fCanvas; }
92
93 private:
94 const SkTDArray<SkCanvas*>& fList;
95 int fIndex;
96 SkCanvas* fCanvas;
97 };
98
99 void SkNWayCanvas::willSave() {
100 Iter iter(fList);
101 while (iter.next()) {
102 iter->save();
103 }
104
105 this->INHERITED::willSave();
106 }
107
108 SkCanvas::SaveLayerStrategy SkNWayCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
109 Iter iter(fList);
110 while (iter.next()) {
111 iter->saveLayer(rec);
112 }
113
114 this->INHERITED::getSaveLayerStrategy(rec);
115 // No need for a layer.
116 return kNoLayer_SaveLayerStrategy;
117 }
118
119 bool SkNWayCanvas::onDoSaveBehind(const SkRect* bounds) {
120 Iter iter(fList);
121 while (iter.next()) {
122 SkCanvasPriv::SaveBehind(iter.get(), bounds);
123 }
124 this->INHERITED::onDoSaveBehind(bounds);
125 return false;
126 }
127
128 void SkNWayCanvas::willRestore() {
129 Iter iter(fList);
130 while (iter.next()) {
131 iter->restore();
132 }
133 this->INHERITED::willRestore();
134 }
135
136 void SkNWayCanvas::didConcat44(const SkM44& m) {
137 Iter iter(fList);
138 while (iter.next()) {
139 iter->concat(m);
140 }
141 }
142
143 void SkNWayCanvas::didSetM44(const SkM44& matrix) {
144 Iter iter(fList);
145 while (iter.next()) {
146 iter->setMatrix(matrix);
147 }
148 }
149
150 void SkNWayCanvas::didTranslate(SkScalar x, SkScalar y) {
151 Iter iter(fList);
152 while (iter.next()) {
153 iter->translate(x, y);
154 }
155 }
156
157 void SkNWayCanvas::didScale(SkScalar x, SkScalar y) {
158 Iter iter(fList);
159 while (iter.next()) {
160 iter->scale(x, y);
161 }
162 }
163
164 void SkNWayCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
165 Iter iter(fList);
166 while (iter.next()) {
167 iter->clipRect(rect, op, kSoft_ClipEdgeStyle == edgeStyle);
168 }
169 this->INHERITED::onClipRect(rect, op, edgeStyle);
170 }
171
172 void SkNWayCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
173 Iter iter(fList);
174 while (iter.next()) {
175 iter->clipRRect(rrect, op, kSoft_ClipEdgeStyle == edgeStyle);
176 }
177 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
178 }
179
180 void SkNWayCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
181 Iter iter(fList);
182 while (iter.next()) {
183 iter->clipPath(path, op, kSoft_ClipEdgeStyle == edgeStyle);
184 }
185 this->INHERITED::onClipPath(path, op, edgeStyle);
186 }
187
188 void SkNWayCanvas::onClipShader(sk_sp<SkShader> sh, SkClipOp op) {
189 Iter iter(fList);
190 while (iter.next()) {
191 iter->clipShader(sh, op);
192 }
193 this->INHERITED::onClipShader(std::move(sh), op);
194 }
195
196 void SkNWayCanvas::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
197 Iter iter(fList);
198 while (iter.next()) {
199 iter->clipRegion(deviceRgn, op);
200 }
201 this->INHERITED::onClipRegion(deviceRgn, op);
202 }
203
204 void SkNWayCanvas::onResetClip() {
205 Iter iter(fList);
206 while (iter.next()) {
207 SkCanvasPriv::ResetClip(iter.get());
208 }
209 this->INHERITED::onResetClip();
210 }
211
212 void SkNWayCanvas::onDrawPaint(const SkPaint& paint) {
213 Iter iter(fList);
214 while (iter.next()) {
215 iter->drawPaint(paint);
216 }
217 }
218
219 void SkNWayCanvas::onDrawBehind(const SkPaint& paint) {
220 Iter iter(fList);
221 while (iter.next()) {
222 SkCanvasPriv::DrawBehind(iter.get(), paint);
223 }
224 }
225
226 void SkNWayCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
227 const SkPaint& paint) {
228 Iter iter(fList);
229 while (iter.next()) {
230 iter->drawPoints(mode, {pts, count}, paint);
231 }
232 }
233
234 void SkNWayCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
235 Iter iter(fList);
236 while (iter.next()) {
237 iter->drawRect(rect, paint);
238 }
239 }
240
241 void SkNWayCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
242 Iter iter(fList);
243 while (iter.next()) {
244 iter->drawRegion(region, paint);
245 }
246 }
247
248 void SkNWayCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
249 Iter iter(fList);
250 while (iter.next()) {
251 iter->drawOval(rect, paint);
252 }
253 }
254
255 void SkNWayCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
256 bool useCenter, const SkPaint& paint) {
257 Iter iter(fList);
258 while (iter.next()) {
259 iter->drawArc(rect, startAngle, sweepAngle, useCenter, paint);
260 }
261 }
262
263 void SkNWayCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
264 Iter iter(fList);
265 while (iter.next()) {
266 iter->drawRRect(rrect, paint);
267 }
268 }
269
270 void SkNWayCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
271 Iter iter(fList);
272 while (iter.next()) {
273 iter->drawDRRect(outer, inner, paint);
274 }
275 }
276
277 void SkNWayCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
278 Iter iter(fList);
279 while (iter.next()) {
280 iter->drawPath(path, paint);
281 }
282 }
283
284 void SkNWayCanvas::onDrawImage2(const SkImage* image, SkScalar left, SkScalar top,
285 const SkSamplingOptions& sampling, const SkPaint* paint) {
286 Iter iter(fList);
287 while (iter.next()) {
288 iter->drawImage(image, left, top, sampling, paint);
289 }
290 }
291
292 void SkNWayCanvas::onDrawImageRect2(const SkImage* image, const SkRect& src, const SkRect& dst,
293 const SkSamplingOptions& sampling, const SkPaint* paint,
294 SrcRectConstraint constraint) {
295 Iter iter(fList);
296 while (iter.next()) {
297 iter->drawImageRect(image, src, dst, sampling, paint, constraint);
298 }
299 }
300
301 void SkNWayCanvas::onDrawImageLattice2(const SkImage* image, const Lattice& lattice,
302 const SkRect& dst, SkFilterMode filter,
303 const SkPaint* paint) {
304 Iter iter(fList);
305 while (iter.next()) {
306 iter->drawImageLattice(image, lattice, dst, filter, paint);
307 }
308 }
309
310 void SkNWayCanvas::onDrawAtlas2(const SkImage* image, const SkRSXform xform[], const SkRect tex[],
311 const SkColor colors[], int count, SkBlendMode bmode,
312 const SkSamplingOptions& sampling, const SkRect* cull,
313 const SkPaint* paint) {
314 Iter iter(fList);
315 while (iter.next()) {
316 iter->drawAtlas(image,
317 {xform, (size_t)count},
318 {tex, (size_t)count},
319 {colors, colors ? (size_t)count : 0},
320 bmode, sampling, cull, paint);
321 }
322 }
323
324 void SkNWayCanvas::onDrawGlyphRunList(const sktext::GlyphRunList& list,
325 const SkPaint &paint) {
326 Iter iter(fList);
327 while (iter.next()) {
328 iter->onDrawGlyphRunList(list, paint);
329 }
330 }
331
332 void SkNWayCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
333 const SkPaint &paint) {
334 Iter iter(fList);
335 while (iter.next()) {
336 iter->drawTextBlob(blob, x, y, paint);
337 }
338 }
339
340 void SkNWayCanvas::onDrawSlug(const sktext::gpu::Slug* slug, const SkPaint& paint) {
341 Iter iter(fList);
342 while (iter.next()) {
343 iter->drawSlug(slug, paint);
344 }
345 }
346
347 void SkNWayCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
348 const SkPaint* paint) {
349 Iter iter(fList);
350 while (iter.next()) {
351 iter->drawPicture(picture, matrix, paint);
352 }
353 }
354
355 void SkNWayCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
356 Iter iter(fList);
357 while (iter.next()) {
358 iter->drawDrawable(drawable, matrix);
359 }
360 }
361
362 void SkNWayCanvas::onDrawVerticesObject(const SkVertices* vertices,
363 SkBlendMode bmode, const SkPaint& paint) {
364 Iter iter(fList);
365 while (iter.next()) {
366 iter->drawVertices(vertices, bmode, paint);
367 }
368 }
369
370 void SkNWayCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
371 const SkPoint texCoords[4], SkBlendMode bmode,
372 const SkPaint& paint) {
373 Iter iter(fList);
374 while (iter.next()) {
375 iter->drawPatch(cubics, colors, texCoords, bmode, paint);
376 }
377 }
378
379 void SkNWayCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
380 Iter iter(fList);
381 while (iter.next()) {
382 iter->private_draw_shadow_rec(path, rec);
383 }
384 }
385
386 void SkNWayCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* data) {
387 Iter iter(fList);
388 while (iter.next()) {
389 iter->drawAnnotation(rect, key, data);
390 }
391 }
392
393 void SkNWayCanvas::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
394 QuadAAFlags aa, const SkColor4f& color, SkBlendMode mode) {
395 Iter iter(fList);
396 while (iter.next()) {
397 iter->experimental_DrawEdgeAAQuad(rect, clip, aa, color, mode);
398 }
399 }
400
401 void SkNWayCanvas::onDrawEdgeAAImageSet2(const ImageSetEntry set[], int count,
402 const SkPoint dstClips[], const SkMatrix preViewMatrices[],
403 const SkSamplingOptions& sampling, const SkPaint* paint,
404 SrcRectConstraint constraint) {
405 Iter iter(fList);
406 while (iter.next()) {
407 iter->experimental_DrawEdgeAAImageSet(
408 set, count, dstClips, preViewMatrices, sampling, paint, constraint);
409 }
410 }