1 /* 2 * Copyright 2023 Google LLC 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 #ifndef SkDrawBase_DEFINED 9 #define SkDrawBase_DEFINED 10 11 #include "include/core/SkCanvas.h" 12 #include "include/core/SkPaint.h" 13 #include "include/core/SkPixmap.h" 14 #include "include/core/SkRefCnt.h" 15 #include "include/core/SkSamplingOptions.h" 16 #include "include/core/SkStrokeRec.h" 17 #include "include/private/base/SkDebug.h" 18 #include "src/base/SkZip.h" 19 #include "src/core/SkGlyphRunPainter.h" 20 #include "src/core/SkMask.h" 21 22 #include <cstddef> 23 24 class SkArenaAlloc; 25 class SkBitmap; 26 class SkBlitter; 27 class SkDevice; 28 class SkGlyph; 29 class SkMaskFilter; 30 class SkMatrix; 31 class SkPath; 32 class SkRRect; 33 class SkRasterClip; 34 class SkShader; 35 class SkSurfaceProps; 36 struct SkIRect; 37 struct SkPoint; 38 struct SkRect; 39 40 class SkDrawBase : public SkGlyphRunListPainterCPU::BitmapDevicePainter { 41 public: 42 SkDrawBase(); 43 44 void drawPaint(const SkPaint&) const; 45 void drawRect(const SkRect& prePaintRect, const SkPaint&, const SkMatrix* paintMatrix, 46 const SkRect* postPaintRect) const; drawRect(const SkRect & rect,const SkPaint & paint)47 void drawRect(const SkRect& rect, const SkPaint& paint) const { 48 this->drawRect(rect, paint, nullptr, nullptr); 49 } 50 void drawRRect(const SkRRect&, const SkPaint&) const; 51 /** 52 * To save on mallocs, we allow a flag that tells us that srcPath is 53 * mutable, so that we don't have to make copies of it as we transform it. 54 * 55 * If prePathMatrix is not null, it should logically be applied before any 56 * stroking or other effects. If there are no effects on the paint that 57 * affect the geometry/rasterization, then the pre matrix can just be 58 * pre-concated with the current matrix. 59 */ 60 void drawPath(const SkPath& path, const SkPaint& paint, 61 const SkMatrix* prePathMatrix = nullptr, bool pathIsMutable = false) const { 62 this->drawPath(path, paint, prePathMatrix, pathIsMutable, false); 63 } 64 65 /** 66 * Overwrite the target with the path's coverage (i.e. its mask). 67 * Will overwrite the entire device, so it need not be zero'd first. 68 * 69 * Only device A8 is supported right now. 70 */ 71 void drawPathCoverage(const SkPath& src, const SkPaint& paint, 72 SkBlitter* customBlitter = nullptr) const { 73 bool isHairline = paint.getStyle() == SkPaint::kStroke_Style && 74 paint.getStrokeWidth() == 0; 75 this->drawPath(src, paint, nullptr, false, !isHairline, customBlitter); 76 } 77 78 void drawDevicePoints(SkCanvas::PointMode, size_t count, const SkPoint[], const SkPaint&, 79 SkDevice*) const; 80 81 static bool ComputeMaskBounds(const SkRect& devPathBounds, const SkIRect& clipBounds, 82 const SkMaskFilter* filter, const SkMatrix* filterMatrix, 83 SkIRect* bounds); 84 85 /** Helper function that creates a mask from a path and an optional maskfilter. 86 Note however, that the resulting mask will not have been actually filtered, 87 that must be done afterwards (by calling filterMask). The maskfilter is provided 88 solely to assist in computing the mask's bounds (if the mode requests that). 89 */ 90 static bool DrawToMask(const SkPath& devPath, const SkIRect& clipBounds, 91 const SkMaskFilter*, const SkMatrix* filterMatrix, 92 SkMaskBuilder* dst, SkMaskBuilder::CreateMode mode, 93 SkStrokeRec::InitStyle style); 94 95 enum RectType { 96 kHair_RectType, 97 kFill_RectType, 98 kStroke_RectType, 99 kPath_RectType 100 }; 101 102 /** 103 * Based on the paint's style, strokeWidth, and the matrix, classify how 104 * to draw the rect. If no special-case is available, returns 105 * kPath_RectType. 106 * 107 * Iff RectType == kStroke_RectType, then strokeSize is set to the device 108 * width and height of the stroke. 109 */ 110 static RectType ComputeRectType(const SkRect&, const SkPaint&, const SkMatrix&, 111 SkPoint* strokeSize); 112 113 using BlitterChooser = SkBlitter* (const SkPixmap& dst, 114 const SkMatrix& ctm, 115 const SkPaint&, 116 SkArenaAlloc*, 117 bool drawCoverage, 118 sk_sp<SkShader> clipShader, 119 const SkSurfaceProps&); 120 121 122 private: 123 // not supported 124 void paintMasks(SkZip<const SkGlyph*, SkPoint> accepted, const SkPaint& paint) const override; 125 void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull, 126 const SkSamplingOptions&, const SkPaint&) const override; 127 128 void drawPath(const SkPath&, 129 const SkPaint&, 130 const SkMatrix* preMatrix, 131 bool pathIsMutable, 132 bool drawCoverage, 133 SkBlitter* customBlitter = nullptr) const; 134 135 void drawLine(const SkPoint[2], const SkPaint&) const; 136 137 void drawDevPath(const SkPath& devPath, 138 const SkPaint& paint, 139 bool drawCoverage, 140 SkBlitter* customBlitter, 141 bool doFill) const; 142 /** 143 * Return the current clip bounds, in local coordinates, with slop to account 144 * for antialiasing or hairlines (i.e. device-bounds outset by 1, and then 145 * run through the inverse of the matrix). 146 * 147 * If the matrix cannot be inverted, or the current clip is empty, return 148 * false and ignore bounds parameter. 149 */ 150 [[nodiscard]] bool computeConservativeLocalClipBounds(SkRect* bounds) const; 151 152 public: 153 SkPixmap fDst; 154 BlitterChooser* fBlitterChooser{nullptr}; // required 155 const SkMatrix* fCTM{nullptr}; // required 156 const SkRasterClip* fRC{nullptr}; // required 157 const SkSurfaceProps* fProps{nullptr}; // optional 158 159 #ifdef SK_DEBUG 160 void validate() const; 161 #else validate()162 void validate() const {} 163 #endif 164 }; 165 166 #endif // SkDrawBase_DEFINED 167