Debian Codegraph @

src/core/SkDraw.cpp

raw
1 /*
2 * Copyright 2006 The Android Open Source Project
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 "src/core/SkDraw.h"
9
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPath.h"
16 #include "include/core/SkPathBuilder.h"
17 #include "include/core/SkPathEffect.h"
18 #include "include/core/SkPathUtils.h"
19 #include "include/core/SkPixmap.h"
20 #include "include/core/SkPoint.h"
21 #include "include/core/SkRRect.h"
22 #include "include/core/SkRect.h"
23 #include "include/core/SkRegion.h"
24 #include "include/core/SkScalar.h"
25 #include "include/core/SkSpan.h"
26 #include "include/core/SkStrokeRec.h"
27 #include "include/core/SkTileMode.h"
28 #include "include/private/base/SkAlign.h"
29 #include "include/private/base/SkAssert.h"
30 #include "include/private/base/SkCPUTypes.h"
31 #include "include/private/base/SkDebug.h"
32 #include "include/private/base/SkFixed.h"
33 #include "include/private/base/SkFloatingPoint.h"
34 #include "include/private/base/SkTemplates.h"
35 #include "include/private/base/SkTo.h"
36 #include "src/base/SkArenaAlloc.h"
37 #include "src/base/SkTLazy.h"
38 #include "src/base/SkZip.h"
39 #include "src/core/SkAutoBlitterChoose.h"
40 #include "src/core/SkBlendModePriv.h"
41 #include "src/core/SkBlitter.h"
42 #include "src/core/SkBlitter_A8.h"
43 #include "src/core/SkDevice.h"
44 #include "src/core/SkDrawProcs.h"
45 #include "src/core/SkDrawTypes.h"
46 #include "src/core/SkImageInfoPriv.h"
47 #include "src/core/SkMask.h"
48 #include "src/core/SkMaskFilterBase.h"
49 #include "src/core/SkMatrixUtils.h"
50 #include "src/core/SkMipmap.h"
51 #include "src/core/SkPathData.h"
52 #include "src/core/SkPathEffectBase.h"
53 #include "src/core/SkPathPriv.h"
54 #include "src/core/SkRasterClip.h"
55 #include "src/core/SkRectPriv.h"
56 #include "src/core/SkScan.h"
57 #include "src/image/SkImage_Raster.h"
58 #include "src/shaders/SkImageShader.h"
59
60 #include <algorithm>
61 #include <cstddef>
62 #include <cstdint>
63 #include <optional>
64 #include <string>
65
66 class SkResourceCache;
67
68 using namespace skia_private;
69
70 namespace skcpu {
71
72 static SkPaint make_paint_with_image_and_mips(const SkPaint& origPaint,
73 const SkBitmap& bitmap,
74 const SkSamplingOptions& sampling,
75 SkMatrix* matrix,
76 sk_sp<SkMipmap> mips) {
77 SkPaint paint(origPaint);
78 auto img = SkImage_Raster::MakeFromBitmap(bitmap, SkCopyPixelsMode::kNever, std::move(mips));
79 paint.setShader(img->makeShaderForPaint(
80 origPaint, SkTileMode::kClamp, SkTileMode::kClamp, sampling, matrix));
81 return paint;
82 }
83
84 static SkPaint make_paint_with_image(const SkPaint& origPaint,
85 const SkBitmap& bitmap,
86 const SkSamplingOptions& sampling,
87 SkMatrix* matrix) {
88 return make_paint_with_image_and_mips(origPaint, bitmap, sampling, matrix, nullptr);
89 }
90
91 Draw::Draw() { fBlitterChooser = SkBlitter::Choose; }
92
93 struct PtProcRec {
94 SkCanvas::PointMode fMode;
95 const SkPaint* fPaint;
96 const SkRegion* fClip;
97 const SkRasterClip* fRC;
98
99 // computed values
100 SkRect fClipBounds;
101 SkScalar fRadius;
102
103 typedef void (*Proc)(const PtProcRec&, SkSpan<const SkPoint> devPts, SkBlitter*);
104
105 bool init(SkCanvas::PointMode, const SkPaint&, const SkMatrix* matrix,
106 const SkRasterClip*);
107 Proc chooseProc(SkBlitter** blitter);
108
109 private:
110 SkAAClipBlitterWrapper fWrapper;
111 };
112
113 #define DIRECT_BLIT_LOOP(writable_method, value) \
114 do { \
115 for (auto p : devPts) { \
116 int x = SkScalarFloorToInt(p.fX); \
117 int y = SkScalarFloorToInt(p.fY); \
118 if (cr.contains(x, y)) { \
119 *pm.writable_method(x, y) = value; \
120 } \
121 } \
122 } while (0)
123
124
125 static void bw_pt_hair_proc(const PtProcRec& rec, SkSpan<const SkPoint> devPts,
126 SkBlitter* blitter) {
127 const auto direct = blitter->canDirectBlit();
128 if (direct && rec.fClip->isRect()) {
129 const SkIRect cr = rec.fClip->getBounds();
130 auto pm = direct->pm;
131 const auto v = direct->value;
132 switch (pm.info().bytesPerPixel()) {
133 case 1: DIRECT_BLIT_LOOP(writable_addr8, v); break;
134 case 2: DIRECT_BLIT_LOOP(writable_addr16, v); break;
135 case 4: DIRECT_BLIT_LOOP(writable_addr32, v); break;
136 case 8: DIRECT_BLIT_LOOP(writable_addr64, v); break;
137 default: SkASSERT(false);
138 }
139 } else {
140 for (auto p : devPts) {
141 int x = SkScalarFloorToInt(p.fX);
142 int y = SkScalarFloorToInt(p.fY);
143 if (rec.fClip->contains(x, y)) {
144 blitter->blitH(x, y, 1);
145 }
146 }
147 }
148 }
149
150 static void bw_line_hair_proc(const PtProcRec& rec, SkSpan<const SkPoint> devPts,
151 SkBlitter* blitter) {
152 for (size_t i = 0; i+1 < devPts.size(); i += 2) {
153 SkScan::HairLine({&devPts[i], 2}, *rec.fRC, blitter);
154 }
155 }
156
157 static void bw_poly_hair_proc(const PtProcRec& rec, SkSpan<const SkPoint> devPts,
158 SkBlitter* blitter) {
159 SkScan::HairLine(devPts, *rec.fRC, blitter);
160 }
161
162 // aa versions
163
164 static void aa_line_hair_proc(const PtProcRec& rec, SkSpan<const SkPoint> devPts,
165 SkBlitter* blitter) {
166 for (size_t i = 0; i+1 < devPts.size(); i += 2) {
167 SkScan::AntiHairLine({&devPts[i], 2}, *rec.fRC, blitter);
168 }
169 }
170
171 static void aa_poly_hair_proc(const PtProcRec& rec, SkSpan<const SkPoint> devPts,
172 SkBlitter* blitter) {
173 SkScan::AntiHairLine(devPts, *rec.fRC, blitter);
174 }
175
176 // square procs (strokeWidth > 0 but matrix is square-scale (sx == sy)
177
178 static SkRect make_square_rad(SkPoint center, SkScalar radius) {
179 return {
180 center.fX - radius, center.fY - radius,
181 center.fX + radius, center.fY + radius
182 };
183 }
184
185 static SkXRect make_xrect(const SkRect& r) {
186 SkASSERT(SkRectPriv::FitsInFixed(r));
187 return {
188 SkScalarToFixed(r.fLeft), SkScalarToFixed(r.fTop),
189 SkScalarToFixed(r.fRight), SkScalarToFixed(r.fBottom)
190 };
191 }
192
193 static void bw_square_proc(const PtProcRec& rec, SkSpan<const SkPoint> devPts,
194 SkBlitter* blitter) {
195 for (auto p : devPts) {
196 SkRect r = make_square_rad(p, rec.fRadius);
197 if (r.intersect(rec.fClipBounds)) {
198 SkScan::FillXRect(make_xrect(r), *rec.fRC, blitter);
199 }
200 }
201 }
202
203 static void aa_square_proc(const PtProcRec& rec, SkSpan<const SkPoint> devPts,
204 SkBlitter* blitter) {
205 for (auto p : devPts) {
206 SkRect r = make_square_rad(p, rec.fRadius);
207 if (r.intersect(rec.fClipBounds)) {
208 SkScan::AntiFillXRect(make_xrect(r), *rec.fRC, blitter);
209 }
210 }
211 }
212
213 // If this returns true, then chooseProc() must return a valid proc
214 bool PtProcRec::init(SkCanvas::PointMode mode, const SkPaint& paint,
215 const SkMatrix* matrix, const SkRasterClip* rc) {
216 if ((unsigned)mode > (unsigned)SkCanvas::kPolygon_PointMode) {
217 return false;
218 }
219 if (paint.getPathEffect() || paint.getMaskFilter()) {
220 return false;
221 }
222 SkScalar width = paint.getStrokeWidth();
223 SkScalar radius = -1; // sentinel value, a "valid" value must be > 0
224
225 if (0 == width) {
226 radius = 0.5f;
227 } else if (paint.getStrokeCap() != SkPaint::kRound_Cap &&
228 matrix->isScaleTranslate() && SkCanvas::kPoints_PointMode == mode) {
229 SkScalar sx = matrix->get(SkMatrix::kMScaleX);
230 SkScalar sy = matrix->get(SkMatrix::kMScaleY);
231 if (SkScalarNearlyZero(sx - sy)) {
232 radius = SkScalarHalf(width * SkScalarAbs(sx));
233 }
234 }
235 if (radius > 0) {
236 SkRect clipBounds = SkRect::Make(rc->getBounds());
237 // if we return true, the caller may assume that the constructed shapes can be represented
238 // using SkFixed (after clipping), so we preflight that here.
239 if (!SkRectPriv::FitsInFixed(clipBounds)) {
240 return false;
241 }
242 fMode = mode;
243 fPaint = &paint;
244 fClip = nullptr;
245 fRC = rc;
246 fClipBounds = clipBounds;
247 fRadius = radius;
248 return true;
249 }
250 return false;
251 }
252
253 PtProcRec::Proc PtProcRec::chooseProc(SkBlitter** blitterPtr) {
254 Proc proc = nullptr;
255
256 SkBlitter* blitter = *blitterPtr;
257 if (fRC->isBW()) {
258 fClip = &fRC->bwRgn();
259 } else {
260 fWrapper.init(*fRC, blitter);
261 fClip = &fWrapper.getRgn();
262 blitter = fWrapper.getBlitter();
263 *blitterPtr = blitter;
264 }
265
266 // for our arrays
267 SkASSERT(0 == SkCanvas::kPoints_PointMode);
268 SkASSERT(1 == SkCanvas::kLines_PointMode);
269 SkASSERT(2 == SkCanvas::kPolygon_PointMode);
270 SkASSERT((unsigned)fMode <= (unsigned)SkCanvas::kPolygon_PointMode);
271
272 if (fPaint->isAntiAlias()) {
273 if (0 == fPaint->getStrokeWidth()) {
274 static const Proc gAAProcs[] = {
275 aa_square_proc, aa_line_hair_proc, aa_poly_hair_proc
276 };
277 proc = gAAProcs[fMode];
278 } else if (fPaint->getStrokeCap() != SkPaint::kRound_Cap) {
279 SkASSERT(SkCanvas::kPoints_PointMode == fMode);
280 proc = aa_square_proc;
281 }
282 } else { // BW
283 if (fRadius <= 0.5f) { // small radii and hairline
284 static const Proc gBWProcs[] = {
285 bw_pt_hair_proc, bw_line_hair_proc, bw_poly_hair_proc
286 };
287 proc = gBWProcs[fMode];
288 } else {
289 proc = bw_square_proc;
290 }
291 }
292 return proc;
293 }
294
295 // each of these costs 8-bytes of stack space, so don't make it too large
296 // must be even for lines/polygon to work
297 #define MAX_DEV_PTS 32
298
299 void Draw::drawPoints(SkCanvas::PointMode mode,
300 SkSpan<const SkPoint> points,
301 const SkPaint& paint,
302 SkDevice* device) const {
303 // if we're in lines mode, force count to be even
304 if (SkCanvas::kLines_PointMode == mode) {
305 points = points.first(points.size() & ~1); // force it to be even
306 }
307
308 SkDEBUGCODE(this->validate();)
309
310 // nothing to draw
311 if (points.empty() || fRC->isEmpty()) {
312 return;
313 }
314
315 PtProcRec rec;
316 if (!device && rec.init(mode, paint, fCTM, fRC)) {
317 // Can't easily get bounds of points so don't try.
318 SkAutoBlitterChoose blitter(*this, nullptr, paint, SkRect::MakeEmpty());
319
320 SkPoint devPts[MAX_DEV_PTS];
321 SkBlitter* bltr = blitter.get();
322 PtProcRec::Proc proc = rec.chooseProc(&bltr);
323 // we have to back up subsequent passes if we're in polygon mode
324 const size_t backup = (SkCanvas::kPolygon_PointMode == mode);
325
326 auto count = points.size();
327 auto pts = points.data();
328 do {
329 size_t n = count;
330 if (n > MAX_DEV_PTS) {
331 n = MAX_DEV_PTS;
332 }
333 fCTM->mapPoints({devPts, n}, {pts, n});
334 if (!SkIsFinite(&devPts[0].fX, n * 2)) {
335 return;
336 }
337 proc(rec, {devPts, n}, bltr);
338 pts += n - backup;
339 SkASSERT(count >= n);
340 count -= n;
341 if (count > 0) {
342 count += backup;
343 }
344 } while (count != 0);
345 } else {
346 this->drawDevicePoints(mode, points, paint, device);
347 }
348 }
349
350 static bool clipped_out(const SkMatrix& m, const SkRasterClip& c,
351 const SkRect& srcR) {
352 SkRect dstR;
353 m.mapRect(&dstR, srcR);
354 return c.quickReject(dstR.roundOut());
355 }
356
357 static bool clipped_out(const SkMatrix& matrix, const SkRasterClip& clip,
358 int width, int height) {
359 SkRect r;
360 r.setIWH(width, height);
361 return clipped_out(matrix, clip, r);
362 }
363
364 static bool clipHandlesSprite(const SkRasterClip& clip, int x, int y, const SkPixmap& pmap) {
365 return clip.isBW() || clip.quickContains(SkIRect::MakeXYWH(x, y, pmap.width(), pmap.height()));
366 }
367
368 void Draw::drawBitmap(const SkBitmap& bitmap,
369 const SkMatrix& prematrix,
370 const SkRect* dstBounds,
371 const SkSamplingOptions& sampling,
372 const SkPaint& origPaint,
373 sk_sp<SkMipmap> mips) const {
374 SkDEBUGCODE(this->validate();)
375
376 // nothing to draw
377 if (fRC->isEmpty() ||
378 bitmap.width() == 0 || bitmap.height() == 0 ||
379 bitmap.colorType() == kUnknown_SkColorType) {
380 return;
381 }
382
383 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
384 if (origPaint.getStyle() != SkPaint::kFill_Style) {
385 paint.writable()->setStyle(SkPaint::kFill_Style);
386 }
387
388 SkMatrix matrix = *fCTM * prematrix;
389
390 if (clipped_out(matrix, *fRC, bitmap.width(), bitmap.height())) {
391 return;
392 }
393
394 if (!SkColorTypeIsAlphaOnly(bitmap.colorType()) &&
395 SkTreatAsSprite(matrix, bitmap.dimensions(), sampling, paint->isAntiAlias())) {
396 //
397 // It is safe to call lock pixels now, since we know the matrix is
398 // (more or less) identity.
399 //
400 SkPixmap pmap;
401 if (!bitmap.peekPixels(&pmap)) {
402 return;
403 }
404 int ix = SkScalarRoundToInt(matrix.getTranslateX());
405 int iy = SkScalarRoundToInt(matrix.getTranslateY());
406 if (clipHandlesSprite(*fRC, ix, iy, pmap)) {
407 SkSTArenaAlloc<kSkBlitterContextSize> allocator;
408 // blitter will be owned by the allocator.
409 SkBlitter* blitter = SkBlitter::ChooseSprite(fDst, *paint, pmap, ix, iy, &allocator,
410 fRC->clipShader());
411 if (blitter) {
412 SkScan::FillIRect(SkIRect::MakeXYWH(ix, iy, pmap.width(), pmap.height()),
413 *fRC, blitter);
414 return;
415 }
416 // if !blitter, then we fall-through to the slower case
417 }
418 }
419
420 // now make a temp draw on the stack, and use it
421 Draw draw(*this);
422 draw.fCTM = &matrix;
423
424 // For a long time, the CPU backend treated A8 bitmaps as coverage, rather than alpha. This was
425 // inconsistent with the GPU backend (skbug.com/40041022). When this was fixed, it altered behavior
426 // for some Android apps (b/231400686). Thus: keep the old behavior in the framework.
427 #if defined(SK_SUPPORT_LEGACY_ALPHA_BITMAP_AS_COVERAGE)
428 if (bitmap.colorType() == kAlpha_8_SkColorType && !paint->getColorFilter()) {
429 draw.drawBitmapAsMask(bitmap, sampling, *paint, nullptr);
430 return;
431 }
432 #endif
433
434 SkPaint paintWithShader =
435 make_paint_with_image_and_mips(*paint, bitmap, sampling, nullptr, mips);
436 const SkRect srcBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height());
437 if (dstBounds) {
438 this->drawRect(srcBounds, paintWithShader, &prematrix, dstBounds);
439 } else {
440 draw.drawRect(srcBounds, paintWithShader);
441 }
442 }
443
444 void Draw::drawSprite(const SkBitmap& bitmap, int x, int y, const SkPaint& origPaint) const {
445 SkDEBUGCODE(this->validate();)
446
447 // nothing to draw
448 if (fRC->isEmpty() ||
449 bitmap.width() == 0 || bitmap.height() == 0 ||
450 bitmap.colorType() == kUnknown_SkColorType) {
451 return;
452 }
453
454 const SkIRect bounds = SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height());
455
456 if (fRC->quickReject(bounds)) {
457 return; // nothing to draw
458 }
459
460 SkPaint paint(origPaint);
461 paint.setStyle(SkPaint::kFill_Style);
462
463 SkPixmap pmap;
464 if (!bitmap.peekPixels(&pmap)) {
465 return;
466 }
467
468 if (nullptr == paint.getColorFilter() && clipHandlesSprite(*fRC, x, y, pmap)) {
469 // blitter will be owned by the allocator.
470 SkSTArenaAlloc<kSkBlitterContextSize> allocator;
471 SkBlitter* blitter = SkBlitter::ChooseSprite(fDst, paint, pmap, x, y, &allocator,
472 fRC->clipShader());
473 if (blitter) {
474 SkScan::FillIRect(bounds, *fRC, blitter);
475 return;
476 }
477 }
478
479 SkMatrix matrix;
480 SkRect r;
481
482 // get a scalar version of our rect
483 r.set(bounds);
484
485 // create shader with offset
486 matrix.setTranslate(r.fLeft, r.fTop);
487 SkPaint paintWithShader = make_paint_with_image(paint, bitmap, SkSamplingOptions(), &matrix);
488 Draw draw(*this);
489 draw.fCTM = &SkMatrix::I();
490 // call ourself with a rect
491 draw.drawRect(r, paintWithShader);
492 }
493
494 void Draw::drawDevMask(const SkMask& srcM,
495 const SkPaint& paint,
496 const SkMatrix* paintMatrix) const {
497 if (srcM.fBounds.isEmpty()) {
498 return;
499 }
500
501 const SkMask* mask = &srcM;
502
503 SkMaskBuilder dstM;
504 if (paint.getMaskFilter() &&
505 as_MFB(paint.getMaskFilter())->filterMask(&dstM, srcM, *fCTM, nullptr)) {
506 mask = &dstM;
507 }
508 SkAutoMaskFreeImage ami(dstM.image());
509
510 SkAutoBlitterChoose blitterChooser(*this, paintMatrix, paint, SkRect::Make(dstM.bounds()));
511 SkBlitter* blitter = blitterChooser.get();
512
513 SkAAClipBlitterWrapper wrapper;
514 const SkRegion* clipRgn;
515
516 if (fRC->isBW()) {
517 clipRgn = &fRC->bwRgn();
518 } else {
519 wrapper.init(*fRC, blitter);
520 clipRgn = &wrapper.getRgn();
521 blitter = wrapper.getBlitter();
522 }
523 blitter->blitMaskRegion(*mask, *clipRgn);
524 }
525
526 void Draw::drawBitmapAsMask(const SkBitmap& bitmap,
527 const SkSamplingOptions& sampling,
528 const SkPaint& paint,
529 const SkMatrix* paintMatrix) const {
530 SkASSERT(bitmap.colorType() == kAlpha_8_SkColorType);
531
532 // nothing to draw
533 if (fRC->isEmpty()) {
534 return;
535 }
536
537 if (SkTreatAsSprite(*fCTM, bitmap.dimensions(), sampling, paint.isAntiAlias()))
538 {
539 int ix = SkScalarRoundToInt(fCTM->getTranslateX());
540 int iy = SkScalarRoundToInt(fCTM->getTranslateY());
541
542 SkPixmap pmap;
543 if (!bitmap.peekPixels(&pmap)) {
544 return;
545 }
546 SkMask mask(pmap.addr8(0, 0),
547 SkIRect::MakeXYWH(ix, iy, pmap.width(), pmap.height()),
548 SkToU32(pmap.rowBytes()),
549 SkMask::kA8_Format);
550
551 this->drawDevMask(mask, paint, paintMatrix);
552 } else { // need to xform the bitmap first
553 SkRect r;
554 SkMaskBuilder mask;
555
556 r.setIWH(bitmap.width(), bitmap.height());
557 fCTM->mapRect(&r);
558 r.round(&mask.bounds());
559
560 // set the mask's bounds to the transformed bitmap-bounds,
561 // clipped to the actual device and further limited by the clip bounds
562 {
563 SkASSERT(fDst.bounds().contains(fRC->getBounds()));
564 SkIRect devBounds = fDst.bounds();
565 devBounds.intersect(fRC->getBounds().makeOutset(1, 1));
566 // need intersect(l, t, r, b) on irect
567 if (!mask.bounds().intersect(devBounds)) {
568 return;
569 }
570 }
571
572 mask.format() = SkMask::kA8_Format;
573 mask.rowBytes() = SkAlign4(mask.fBounds.width());
574 size_t size = mask.computeImageSize();
575 if (0 == size) {
576 // the mask is too big to allocated, draw nothing
577 return;
578 }
579
580 // allocate (and clear) our temp buffer to hold the transformed bitmap
581 AutoTMalloc<uint8_t> storage(size);
582 mask.image() = storage.get();
583 memset(mask.image(), 0, size);
584
585 // now draw our bitmap(src) into mask(dst), transformed by the matrix
586 {
587 SkBitmap device;
588 device.installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()),
589 mask.image(), mask.fRowBytes);
590
591 SkCanvas c(device);
592 // need the unclipped top/left for the translate
593 c.translate(-SkIntToScalar(mask.fBounds.fLeft),
594 -SkIntToScalar(mask.fBounds.fTop));
595 c.concat(*fCTM);
596
597 // We can't call drawBitmap, or we'll infinitely recurse. Instead
598 // we manually build a shader and draw that into our new mask
599 SkPaint tmpPaint;
600 tmpPaint.setAntiAlias(paint.isAntiAlias());
601 tmpPaint.setDither(paint.isDither());
602 SkPaint paintWithShader = make_paint_with_image(tmpPaint, bitmap, sampling, nullptr);
603 SkRect rr;
604 rr.setIWH(bitmap.width(), bitmap.height());
605 c.drawRect(rr, paintWithShader);
606 }
607 this->drawDevMask(mask, paint, paintMatrix);
608 }
609 }
610
611 ///////////////////////////////////////////////////////////////////////////////
612
613 bool Draw::computeConservativeLocalClipBounds(SkRect* localBounds) const {
614 if (fRC->isEmpty()) {
615 return false;
616 }
617
618 if (auto inverse = fCTM->invert()) {
619 SkIRect devBounds = fRC->getBounds();
620 // outset to have slop for antialasing and hairlines
621 devBounds.outset(1, 1);
622 inverse->mapRect(localBounds, SkRect::Make(devBounds));
623 return true;
624 }
625 return false;
626 }
627
628 ///////////////////////////////////////////////////////////////////////////////
629
630 void Draw::drawPaint(const SkPaint& paint) const {
631 SkDEBUGCODE(this->validate();)
632
633 if (fRC->isEmpty()) {
634 return;
635 }
636
637 SkIRect devRect;
638 devRect.setWH(fDst.width(), fDst.height());
639
640 SkAutoBlitterChoose blitter(*this, nullptr, paint, SkRect::Make(devRect));
641 SkScan::FillIRect(devRect, *fRC, blitter.get());
642 }
643
644 ///////////////////////////////////////////////////////////////////////////////
645
646 static inline SkPoint compute_stroke_size(const SkPaint& paint, const SkMatrix& matrix) {
647 SkASSERT(matrix.rectStaysRect());
648 SkASSERT(SkPaint::kFill_Style != paint.getStyle());
649
650 SkVector size = matrix.mapVector({paint.getStrokeWidth(), paint.getStrokeWidth()});
651 return SkPoint::Make(SkScalarAbs(size.fX), SkScalarAbs(size.fY));
652 }
653
654 static bool easy_rect_join(const SkRect& rect,
655 const SkPaint& paint,
656 const SkMatrix& matrix,
657 SkPoint* strokeSize) {
658 if (rect.isEmpty() || SkPaint::kMiter_Join != paint.getStrokeJoin() ||
659 paint.getStrokeMiter() < SK_ScalarSqrt2) {
660 return false;
661 }
662
663 *strokeSize = compute_stroke_size(paint, matrix);
664 return true;
665 }
666
667 Draw::RectType Draw::ComputeRectType(const SkRect& rect,
668 const SkPaint& paint,
669 const SkMatrix& matrix,
670 SkPoint* strokeSize) {
671 const SkScalar width = paint.getStrokeWidth();
672 const bool zeroWidth = (0 == width);
673 SkPaint::Style style = paint.getStyle();
674
675 if ((SkPaint::kStrokeAndFill_Style == style) && zeroWidth) {
676 style = SkPaint::kFill_Style;
677 }
678
679 if (paint.getPathEffect() || paint.getMaskFilter() || !matrix.rectStaysRect() ||
680 SkPaint::kStrokeAndFill_Style == style) {
681 return RectType::kPath;
682 }
683 if (SkPaint::kFill_Style == style) {
684 return RectType::kFill;
685 }
686 if (zeroWidth) {
687 return RectType::kHair;
688 }
689 if (easy_rect_join(rect, paint, matrix, strokeSize)) {
690 return RectType::kStroke;
691 }
692 return RectType::kPath;
693 }
694
695 static SkSpan<const SkPoint> rect_points(const SkRect& r) {
696 return {reinterpret_cast<const SkPoint*>(&r), 2};
697 }
698
699
700 static SkSpan<SkPoint> rect_points(SkRect& r) { return {reinterpret_cast<SkPoint*>(&r), 2}; }
701
702 static void draw_rect_as_path(const Draw& orig,
703 const SkRect& prePaintRect,
704 const SkPaint& paint,
705 const SkMatrix& ctm) {
706 Draw draw(orig);
707 draw.fCTM = &ctm;
708 draw.drawPath(SkPath::Rect(prePaintRect), paint, nullptr);
709 }
710
711 void Draw::drawRect(const SkRect& prePaintRect,
712 const SkPaint& paint,
713 const SkMatrix* paintMatrix,
714 const SkRect* postPaintRect) const {
715 SkDEBUGCODE(this->validate();)
716
717 // nothing to draw
718 if (fRC->isEmpty()) {
719 return;
720 }
721
722 SkTCopyOnFirstWrite<SkMatrix> matrix(fCTM);
723 if (paintMatrix) {
724 SkASSERT(postPaintRect);
725 matrix.writable()->preConcat(*paintMatrix);
726 } else {
727 SkASSERT(!postPaintRect);
728 }
729
730 SkPoint strokeSize;
731 RectType rtype = ComputeRectType(prePaintRect, paint, *fCTM, &strokeSize);
732
733 if (RectType::kPath == rtype) {
734 draw_rect_as_path(*this, prePaintRect, paint, *matrix);
735 return;
736 }
737
738 SkRect devRect;
739 const SkRect& paintRect = paintMatrix ? *postPaintRect : prePaintRect;
740 // skip the paintMatrix when transforming the rect by the CTM
741 fCTM->mapPoints(rect_points(devRect), rect_points(paintRect));
742 devRect.sort();
743
744 // look for the quick exit, before we build a blitter
745 SkRect bbox = devRect;
746 if (paint.getStyle() != SkPaint::kFill_Style) {
747 // extra space for hairlines
748 if (paint.getStrokeWidth() == 0) {
749 bbox.outset(1, 1);
750 } else {
751 // For RectType::kStroke, strokeSize is already computed.
752 const SkPoint& ssize = (RectType::kStroke == rtype)
753 ? strokeSize
754 : compute_stroke_size(paint, *fCTM);
755 bbox.outset(SkScalarHalf(ssize.x()), SkScalarHalf(ssize.y()));
756 }
757 }
758 if (SkPathPriv::TooBigForMath(bbox)) {
759 return;
760 }
761
762 if (!SkRectPriv::FitsInFixed(bbox) && rtype != RectType::kHair) {
763 draw_rect_as_path(*this, prePaintRect, paint, *matrix);
764 return;
765 }
766
767 SkIRect ir = bbox.roundOut();
768 if (fRC->quickReject(ir)) {
769 return;
770 }
771
772 SkAutoBlitterChoose blitterStorage(*this, matrix, paint, devRect);
773 const SkRasterClip& clip = *fRC;
774 SkBlitter* blitter = blitterStorage.get();
775
776 // we want to "fill" if we are kFill or kStrokeAndFill, since in the latter
777 // case we are also hairline (if we've gotten to here), which devolves to
778 // effectively just kFill
779 switch (rtype) {
780 case RectType::kFill:
781 if (paint.isAntiAlias()) {
782 SkScan::AntiFillRect(devRect, clip, blitter);
783 } else {
784 SkScan::FillRect(devRect, clip, blitter);
785 }
786 break;
787 case RectType::kStroke:
788 if (paint.isAntiAlias()) {
789 SkScan::AntiFrameRect(devRect, strokeSize, clip, blitter);
790 } else {
791 SkScan::FrameRect(devRect, strokeSize, clip, blitter);
792 }
793 break;
794 case RectType::kHair:
795 if (paint.isAntiAlias()) {
796 SkScan::AntiHairRect(devRect, clip, blitter);
797 } else {
798 SkScan::HairRect(devRect, clip, blitter);
799 }
800 break;
801 default:
802 SkDEBUGFAIL("bad rtype");
803 }
804 }
805
806 static SkScalar fast_len(const SkVector& vec) {
807 SkScalar x = SkScalarAbs(vec.fX);
808 SkScalar y = SkScalarAbs(vec.fY);
809 if (x < y) {
810 using std::swap;
811 swap(x, y);
812 }
813 return x + SkScalarHalf(y);
814 }
815
816 bool DrawTreatAAStrokeAsHairline(SkScalar strokeWidth, const SkMatrix& matrix, SkScalar* coverage) {
817 SkASSERT(strokeWidth > 0);
818 // We need to try to fake a thick-stroke with a modulated hairline.
819
820 if (matrix.hasPerspective()) {
821 return false;
822 }
823
824 SkVector src[2], dst[2];
825 src[0].set(strokeWidth, 0);
826 src[1].set(0, strokeWidth);
827 matrix.mapVectors(dst, src);
828 SkScalar len0 = fast_len(dst[0]);
829 SkScalar len1 = fast_len(dst[1]);
830 if (len0 <= SK_Scalar1 && len1 <= SK_Scalar1) {
831 if (coverage) {
832 *coverage = sk_float_midpoint(len0, len1);
833 }
834 return true;
835 }
836 return false;
837 }
838
839 void Draw::drawOval(const SkRect& oval, const SkPaint& paint) const {
840 SkDEBUGCODE(this->validate();)
841
842 if (fRC->isEmpty()) {
843 return;
844 }
845
846 this->drawPath(SkPath::Oval(oval), paint, nullptr);
847 }
848
849 void Draw::drawRRect(const SkRRect& rrect, const SkPaint& paint) const {
850 SkDEBUGCODE(this->validate();)
851
852 if (fRC->isEmpty()) {
853 return;
854 }
855
856 {
857 // TODO: Investigate optimizing these options. They are in the same
858 // order as skcpu::Draw::drawPath, which handles each case. It may be
859 // that there is no way to optimize for these using the SkRRect path.
860 SkScalar coverage;
861 if (skcpu::DrawTreatAsHairline(paint, *fCTM, &coverage)) {
862 goto DRAW_PATH;
863 }
864
865 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
866 goto DRAW_PATH;
867 }
868 }
869
870 if (paint.getMaskFilter()) {
871 if (this->drawRRectNinePatch(rrect, paint)) {
872 return;
873 }
874 }
875
876 DRAW_PATH:
877 // Now fall back to the default case of using a path.
878 this->drawPath(SkPath::RRect(rrect), paint, nullptr);
879 }
880
881 bool Draw::drawRRectNinePatch(const SkRRect& rrect, const SkPaint& paint) const {
882 SkASSERT(paint.getMaskFilter());
883
884 if (auto rr = rrect.transform(*fCTM)) {
885 SkAutoBlitterChoose blitter(*this, nullptr, paint, rrect.getBounds());
886 SkResourceCache* cache = nullptr; // TODO(kjlubick) get this from fCtx
887 const SkMaskFilterBase* maskFilter = as_MFB(paint.getMaskFilter());
888 if (rrect.getType() == SkRRect::kRect_Type) {
889 SkRect devRect = rr->rect();
890 if (maskFilter->filterRects(SkSpan(&devRect, 1), *fCTM, *fRC, blitter.get(), cache) ==
891 SkMaskFilterBase::FilterReturn::kTrue) {
892 return true;
893 }
894 } else {
895 if (maskFilter->filterRRect(*rr, *fCTM, *fRC, blitter.get(), cache)) {
896 return true; // filterRRect() called the blitter, so we're done
897 }
898 }
899 }
900 return false;
901 }
902
903 void Draw::drawDevPath(const SkPathRaw& raw,
904 const SkPaint& paint,
905 SkDrawCoverage drawCoverage,
906 SkBlitter* customBlitter,
907 bool doFill) const {
908 if (SkPathPriv::TooBigForMath(raw.bounds())) {
909 return;
910 }
911
912 SkBlitter* blitter = nullptr;
913 SkAutoBlitterChoose blitterStorage;
914 if (nullptr == customBlitter) {
915 blitter = blitterStorage.choose(*this, nullptr, paint, raw.bounds(), drawCoverage);
916 } else {
917 blitter = customBlitter;
918 }
919
920 if (paint.getMaskFilter()) {
921 SkStrokeRec::InitStyle style = doFill ? SkStrokeRec::kFill_InitStyle
922 : SkStrokeRec::kHairline_InitStyle;
923 SkResourceCache* cache = nullptr; // TODO(kjlubick) get this from fCtx
924 if (as_MFB(paint.getMaskFilter())->filterPath(raw, *fCTM, *fRC, blitter, style, cache)) {
925 return; // filterPath() called the blitter, so we're done
926 }
927 }
928
929 void (*proc)(const SkPathRaw&, const SkRasterClip&, SkBlitter*);
930 if (doFill) {
931 if (paint.isAntiAlias()) {
932 proc = SkScan::AntiFillPath;
933 } else {
934 proc = SkScan::FillPath;
935 }
936 } else { // hairline
937 if (paint.isAntiAlias()) {
938 switch (paint.getStrokeCap()) {
939 case SkPaint::kButt_Cap:
940 proc = SkScan::AntiHairPath;
941 break;
942 case SkPaint::kSquare_Cap:
943 proc = SkScan::AntiHairSquarePath;
944 break;
945 case SkPaint::kRound_Cap:
946 proc = SkScan::AntiHairRoundPath;
947 break;
948 }
949 } else {
950 switch (paint.getStrokeCap()) {
951 case SkPaint::kButt_Cap:
952 proc = SkScan::HairPath;
953 break;
954 case SkPaint::kSquare_Cap:
955 proc = SkScan::HairSquarePath;
956 break;
957 case SkPaint::kRound_Cap:
958 proc = SkScan::HairRoundPath;
959 break;
960 }
961 }
962 }
963 proc(raw, *fRC, blitter);
964 }
965
966 /*
967 * Tricky idea: can we treat thin strokes as hairlines? If so, depending on how
968 * thin, we may decide to modulate the paint's alpha to 'simulate' very think
969 * strokes, even though hairline is always 1-pixel wide.
970 *
971 * The motivation at the time was performance: hairlines draw faster than constructing
972 * the inner/outer contours and filling that (as we do for normal stroking).
973 *
974 * Questionable decision, since our hairline algorithm draws each segment of the path
975 * separately, meaning a path that crosses itself can have blending artifacts.
976 * Note: this doesn't happen with normal stroking, as the built inner/outer path
977 * never double-hits a pixel.
978 */
979 static std::optional<SkPaint> modifyPaintForHairlines(const SkPaint& origPaint,
980 const SkMatrix& matrix) {
981 float coverage;
982 if (DrawTreatAsHairline(origPaint, matrix, &coverage)) {
983 const auto bm = origPaint.asBlendMode();
984 if (coverage == 1) {
985 SkPaint paint(origPaint);
986 paint.setStrokeWidth(0);
987 return paint;
988 } else if (bm && SkBlendMode_SupportsCoverageAsAlpha(bm.value())) {
989 U8CPU newAlpha;
990 #if 0
991 newAlpha = SkToU8(SkScalarRoundToInt(coverage * origPaint.getAlpha()));
992 #else
993 // this is the old technique, which we preserve for now so
994 // we don't change previous results (testing)
995 // the new way seems fine, its just (a tiny bit) different
996 int scale = (int)(coverage * 256);
997 newAlpha = origPaint.getAlpha() * scale >> 8;
998 #endif
999 SkPaint paint(origPaint);
1000 paint.setStrokeWidth(0);
1001 paint.setAlpha(newAlpha);
1002 return paint;
1003 }
1004 }
1005 return {};
1006 }
1007
1008 void Draw::drawPath(const SkPath& origSrcPath,
1009 const SkPaint& origPaint,
1010 const SkMatrix* prePathMatrix,
1011 SkDrawCoverage drawCoverage,
1012 SkBlitter* customBlitter) const {
1013 SkDEBUGCODE(this->validate();)
1014
1015 // nothing to draw
1016 if (fRC->isEmpty()) {
1017 return;
1018 }
1019
1020 std::optional<SkPaint> newPaint = modifyPaintForHairlines(origPaint, *fCTM);
1021 const SkPaint* paint = newPaint.has_value() ? &newPaint.value()
1022 : &origPaint;
1023
1024 const bool needsFillPath = paint->getPathEffect() || paint->getStyle() != SkPaint::kFill_Style;
1025
1026 SkPathBuilder builder;
1027 std::optional<SkPathRaw> raw; // will point to either origSrcPath or builder
1028 bool doFill = true;
1029
1030 sk_sp<SkPathData> pdata;
1031
1032 if (needsFillPath) {
1033 SkRect cullRect;
1034 const SkRect* cullRectPtr = nullptr;
1035 if (this->computeConservativeLocalClipBounds(&cullRect)) {
1036 cullRectPtr = &cullRect;
1037 }
1038
1039 std::optional<SkPath> prePathStorage;
1040 const SkPath* pathPtr = &origSrcPath;
1041 if (prePathMatrix) {
1042 prePathStorage = pathPtr->tryMakeTransform(*prePathMatrix);
1043 if (!prePathStorage.has_value()) {
1044 return;
1045 }
1046 pathPtr = &prePathStorage.value();
1047 }
1048 doFill = skpathutils::FillPathWithPaint(*pathPtr, *paint, &builder, cullRectPtr, *fCTM);
1049 builder.transform(*fCTM);
1050 raw = SkPathPriv::Raw(builder, SkResolveConvexity::kYes);
1051 } else {
1052 SkMatrix matrix = *fCTM;
1053 if (prePathMatrix) {
1054 matrix.preConcat(*prePathMatrix);
1055 }
1056
1057 if (matrix.isIdentity()) {
1058 raw = SkPathPriv::Raw(origSrcPath, SkResolveConvexity::kYes);
1059 } else {
1060 raw = SkPathPriv::Raw(origSrcPath, SkResolveConvexity::kNo);
1061 if (raw && (pdata = SkPathData::MakeTransform(*raw, matrix))) {
1062 raw = pdata->raw(origSrcPath.getFillType(), SkResolveConvexity::kYes);
1063 } else {
1064 return; // failed to create pdata
1065 }
1066 }
1067 }
1068
1069 if (!raw) {
1070 return;
1071 }
1072
1073 #if defined(SK_BUILD_FOR_FUZZER)
1074 if (raw->points().size() > 1000) {
1075 return;
1076 }
1077 #endif
1078
1079 this->drawDevPath(*raw, *paint, drawCoverage, customBlitter, doFill);
1080 }
1081
1082 ////////////////////////////////////////////////////////////////////////////////////////////////
1083
1084 #ifdef SK_DEBUG
1085
1086 void Draw::validate() const {
1087 SkASSERT(fCTM != nullptr);
1088 SkASSERT(fRC != nullptr);
1089
1090 const SkIRect& cr = fRC->getBounds();
1091 SkIRect br;
1092
1093 br.setWH(fDst.width(), fDst.height());
1094 SkASSERT(cr.isEmpty() || br.contains(cr));
1095 }
1096
1097 #endif
1098
1099 ////////////////////////////////////////////////////////////////////////////////////////////////
1100
1101 static bool compute_mask_bounds(const SkRect& devPathBounds,
1102 const SkIRect& clipBounds,
1103 const SkMaskFilter* filter,
1104 const SkMatrix* filterMatrix,
1105 SkIRect* bounds) {
1106 SkASSERT(filter);
1107 SkASSERT(filterMatrix);
1108 // init our bounds from the path
1109 *bounds = devPathBounds.makeOutset(SK_ScalarHalf, SK_ScalarHalf).roundOut();
1110
1111 SkIVector margin = SkIPoint::Make(0, 0);
1112 SkMask srcM(nullptr, *bounds, 0, SkMask::kA8_Format);
1113 SkMaskBuilder dstM;
1114 if (!as_MFB(filter)->filterMask(&dstM, srcM, *filterMatrix, &margin)) {
1115 return false;
1116 }
1117
1118 // trim the bounds to reflect the clip (plus whatever slop the filter needs)
1119 // Ugh. Guard against gigantic margins from wacky filters. Without this
1120 // check we can request arbitrary amounts of slop beyond our visible
1121 // clip, and bring down the renderer (at least on finite RAM machines
1122 // like handsets, etc.). Need to balance this invented value between
1123 // quality of large filters like blurs, and the corresponding memory
1124 // requests.
1125 static constexpr int kMaxMargin = 128;
1126 if (!bounds->intersect(clipBounds.makeOutset(std::min(margin.fX, kMaxMargin),
1127 std::min(margin.fY, kMaxMargin)))) {
1128 return false;
1129 }
1130
1131 return true;
1132 }
1133
1134 static void draw_into_mask(const SkMask& mask,
1135 SkPathRaw raw,
1136 SkStrokeRec::InitStyle style) {
1137 SkPixmap dst;
1138 if (!dst.reset(mask)) {
1139 return;
1140 }
1141
1142 const float dx = -mask.fBounds.fLeft,
1143 dy = -mask.fBounds.fTop;
1144 const SkMatrix translate = SkMatrix::Translate(dx, dy);
1145
1146 SkPaint paint;
1147 paint.setAntiAlias(true);
1148 SkBlitterSizedArena alloc;
1149 SkBlitter* blitter = SkChooseA8Blitter(dst, translate, paint, &alloc,
1150 SkDrawCoverage::kNo, nullptr);
1151
1152
1153 // transform a copy of the points, so we can apply the ctm/translate
1154 skia_private::AutoSTArray<32, SkPoint> devPoints(raw.fPoints.size());
1155 translate.mapPoints(devPoints, raw.fPoints);
1156 raw.fPoints = devPoints;
1157 raw.fBounds = raw.fBounds.makeOffset(dx, dy);
1158 if (!raw.fBounds.isFinite()) {
1159 return;
1160 }
1161
1162 const SkRasterClip clip(SkIRect::MakeWH(mask.fBounds.width(), mask.fBounds.height()));
1163
1164 switch (style) {
1165 case SkStrokeRec::kHairline_InitStyle:
1166 SkScan::AntiHairPath(raw, clip, blitter);
1167 break;
1168 case SkStrokeRec::kFill_InitStyle:
1169 SkScan::AntiFillPath(raw, clip, blitter);
1170 break;
1171 }
1172 }
1173
1174 bool DrawToMask(const SkPathRaw& devRaw,
1175 const SkIRect& clipBounds,
1176 const SkMaskFilter* filter,
1177 const SkMatrix* filterMatrix,
1178 SkMaskBuilder* dst,
1179 SkMaskBuilder::CreateMode mode,
1180 SkStrokeRec::InitStyle style) {
1181 SkASSERT(filter);
1182 if (devRaw.empty()) {
1183 return false;
1184 }
1185
1186 if (SkMaskBuilder::kJustRenderImage_CreateMode != mode) {
1187 // By using infinite bounds for inverse fills, compute_mask_bounds is able to clip it to
1188 // 'clipBounds' outset by whatever extra margin the mask filter requires.
1189 static const SkRect kInverseBounds = {SK_ScalarNegativeInfinity,
1190 SK_ScalarNegativeInfinity,
1191 SK_ScalarInfinity,
1192 SK_ScalarInfinity};
1193 SkRect pathBounds = devRaw.isInverseFillType() ? kInverseBounds : devRaw.bounds();
1194 if (!compute_mask_bounds(pathBounds, clipBounds, filter, filterMatrix, &dst->bounds())) {
1195 return false;
1196 }
1197 }
1198
1199 if (SkMaskBuilder::kComputeBoundsAndRenderImage_CreateMode == mode) {
1200 dst->format() = SkMask::kA8_Format;
1201 dst->rowBytes() = dst->fBounds.width();
1202 size_t size = dst->computeImageSize();
1203 if (0 == size) {
1204 // we're too big to allocate the mask, abort
1205 return false;
1206 }
1207 dst->image() = SkMaskBuilder::AllocImage(size, SkMaskBuilder::kZeroInit_Alloc);
1208 }
1209
1210 if (SkMaskBuilder::kJustComputeBounds_CreateMode != mode) {
1211 draw_into_mask(*dst, devRaw, style);
1212 }
1213 return true;
1214 }
1215
1216 void Draw::drawDevicePoints(SkCanvas::PointMode mode,
1217 SkSpan<const SkPoint> points,
1218 const SkPaint& paint,
1219 SkDevice* device) const {
1220 // if we're in lines mode, force count to be even
1221 if (SkCanvas::kLines_PointMode == mode) {
1222 points = points.first(points.size() & ~1); // force it to be even
1223 }
1224
1225 SkDEBUGCODE(this->validate();)
1226
1227 // nothing to draw
1228 if (points.empty() || fRC->isEmpty()) {
1229 return;
1230 }
1231
1232 // needed?
1233 if (!SkIsFinite(&points[0].fX, points.size() * 2)) {
1234 return;
1235 }
1236
1237 switch (mode) {
1238 case SkCanvas::kPoints_PointMode: {
1239 // temporarily mark the paint as filling.
1240 SkPaint newPaint(paint);
1241 newPaint.setStyle(SkPaint::kFill_Style);
1242
1243 SkScalar width = newPaint.getStrokeWidth();
1244 SkScalar radius = SkScalarHalf(width);
1245
1246 if (newPaint.getStrokeCap() == SkPaint::kRound_Cap) {
1247 if (device) {
1248 for (const auto& pt : points) {
1249 SkRect r = SkRect::MakeLTRB(pt.fX - radius, pt.fY - radius,
1250 pt.fX + radius, pt.fY + radius);
1251 device->drawOval(r, newPaint);
1252 }
1253 } else {
1254 SkPath path = SkPath::Circle(0, 0, radius);
1255 SkMatrix preMatrix;
1256
1257 for (const auto& pt : points) {
1258 preMatrix.setTranslate(pt.fX, pt.fY);
1259 this->drawPath(path, newPaint, &preMatrix);
1260 }
1261 }
1262 } else {
1263 SkRect r;
1264
1265 for (const auto& pt : points) {
1266 r.fLeft = pt.fX - radius;
1267 r.fTop = pt.fY - radius;
1268 r.fRight = r.fLeft + width;
1269 r.fBottom = r.fTop + width;
1270 if (device) {
1271 device->drawRect(r, newPaint);
1272 } else {
1273 this->drawRect(r, newPaint);
1274 }
1275 }
1276 }
1277 break;
1278 }
1279 case SkCanvas::kLines_PointMode:
1280 if (2 == points.size() && paint.getPathEffect()) {
1281 // most likely a dashed line - see if it is one of the ones
1282 // we can accelerate
1283 SkStrokeRec stroke(paint);
1284 SkPathEffectBase::PointData pointData;
1285
1286 SkPath path = SkPath::Line(points[0], points[1]);
1287
1288 SkRect cullRect = SkRect::Make(fRC->getBounds());
1289
1290 if (as_PEB(paint.getPathEffect())
1291 ->asPoints(&pointData, path, stroke, *fCTM, &cullRect)) {
1292 // 'asPoints' managed to find some fast path
1293
1294 SkPaint newP(paint);
1295 newP.setPathEffect(nullptr);
1296 newP.setStyle(SkPaint::kFill_Style);
1297
1298 if (!pointData.fFirst.isEmpty()) {
1299 if (device) {
1300 device->drawPath(pointData.fFirst, newP);
1301 } else {
1302 this->drawPath(pointData.fFirst, newP, nullptr);
1303 }
1304 }
1305
1306 if (!pointData.fLast.isEmpty()) {
1307 if (device) {
1308 device->drawPath(pointData.fLast, newP);
1309 } else {
1310 this->drawPath(pointData.fLast, newP, nullptr);
1311 }
1312 }
1313
1314 if (pointData.fSize.fX == pointData.fSize.fY) {
1315 // The rest of the dashed line can just be drawn as points
1316 SkASSERT(pointData.fSize.fX == SkScalarHalf(newP.getStrokeWidth()));
1317
1318 if (SkPathEffectBase::PointData::kCircles_PointFlag & pointData.fFlags) {
1319 newP.setStrokeCap(SkPaint::kRound_Cap);
1320 } else {
1321 newP.setStrokeCap(SkPaint::kButt_Cap);
1322 }
1323
1324 if (device) {
1325 device->drawPoints(
1326 SkCanvas::kPoints_PointMode, pointData.points(), newP);
1327 } else {
1328 this->drawDevicePoints(
1329 SkCanvas::kPoints_PointMode, pointData.points(), newP, device);
1330 }
1331 break;
1332 } else {
1333 // The rest of the dashed line must be drawn as rects
1334 SkASSERT(!(SkPathEffectBase::PointData::kCircles_PointFlag &
1335 pointData.fFlags));
1336
1337 SkRect r;
1338
1339 for (const auto& pt : pointData.points()) {
1340 r.setLTRB(pt.fX - pointData.fSize.fX,
1341 pt.fY - pointData.fSize.fY,
1342 pt.fX + pointData.fSize.fX,
1343 pt.fY + pointData.fSize.fY);
1344 if (device) {
1345 device->drawRect(r, newP);
1346 } else {
1347 this->drawRect(r, newP);
1348 }
1349 }
1350 }
1351
1352 break;
1353 }
1354 }
1355 [[fallthrough]]; // couldn't take fast path
1356 case SkCanvas::kPolygon_PointMode: {
1357 auto count = points.size() - 1;
1358 SkPaint p(paint);
1359 p.setStyle(SkPaint::kStroke_Style);
1360 size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1;
1361
1362 for (size_t i = 0; i < count; i += inc) {
1363 auto path = SkPath::Line(points[i], points[i + 1]);
1364 if (device) {
1365 device->drawPath(path, p);
1366 } else {
1367 this->drawPath(path, p, nullptr);
1368 }
1369 }
1370 break;
1371 }
1372 }
1373 }
1374
1375 } // namespace skcpu