| 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 |
#ifndef skcpu_Draw_DEFINED |
| 9 |
#define skcpu_Draw_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/SkSpan.h" |
| 17 |
#include "include/core/SkStrokeRec.h" |
| 18 |
#include "include/core/SkSurfaceProps.h" |
| 19 |
#include "include/private/base/SkDebug.h" |
| 20 |
#include "src/base/SkZip.h" |
| 21 |
#include "src/core/SkDrawTypes.h" |
| 22 |
#include "src/core/SkMask.h" |
| 23 |
|
| 24 |
class SkArenaAlloc; |
| 25 |
class SkBitmap; |
| 26 |
class SkBlender; |
| 27 |
class SkBlitter; |
| 28 |
class SkDevice; |
| 29 |
class SkGlyph; |
| 30 |
class SkMaskFilter; |
| 31 |
class SkMatrix; |
| 32 |
class SkMipmap; |
| 33 |
class SkPath; |
| 34 |
struct SkPathRaw; |
| 35 |
class SkRRect; |
| 36 |
class SkRasterClip; |
| 37 |
class SkShader; |
| 38 |
class SkVertices; |
| 39 |
struct SkIRect; |
| 40 |
struct SkPoint; |
| 41 |
struct SkPoint3; |
| 42 |
struct SkRSXform; |
| 43 |
struct SkRect; |
| 44 |
|
| 45 |
namespace sktext { |
| 46 |
class GlyphRunList; |
| 47 |
} |
| 48 |
|
| 49 |
namespace skcpu { |
| 50 |
|
| 51 |
class GlyphRunListPainter; |
| 52 |
class ContextImpl; |
| 53 |
|
| 54 |
|
| 55 |
/** Helper function that creates a mask from a path and a required maskfilter. |
| 56 |
Note however, that the resulting mask will not have been actually filtered, |
| 57 |
that must be done afterwards (by calling filterMask). The maskfilter is provided |
| 58 |
solelely to assist in computing the mask's bounds (if the mode requests that). |
| 59 |
*/ |
| 60 |
bool DrawToMask(const SkPathRaw& devRaw, |
| 61 |
const SkIRect& clipBounds, |
| 62 |
const SkMaskFilter*, |
| 63 |
const SkMatrix* filterMatrix, |
| 64 |
SkMaskBuilder* dst, |
| 65 |
SkMaskBuilder::CreateMode mode, |
| 66 |
SkStrokeRec::InitStyle style); |
| 67 |
|
| 68 |
/** |
| 69 |
* BitmapDevicePainter provides a minimal interface for painting pre-rasterized objects like glyphs |
| 70 |
* and bitmaps. |
| 71 |
* |
| 72 |
* This interface creates a seam that allows components to intercept and handle low-level drawing |
| 73 |
* primitives. For example, SkOverdrawCanvas uses this to visualize overdraw by drawing bounding |
| 74 |
* boxes for glyphs |
| 75 |
*/ |
| 76 |
class BitmapDevicePainter { |
| 77 |
public: |
| 78 |
BitmapDevicePainter() = default; |
| 79 |
BitmapDevicePainter(const BitmapDevicePainter&) = default; |
| 80 |
virtual ~BitmapDevicePainter() = default; |
| 81 |
|
| 82 |
virtual void paintMasks(SkZip<const SkGlyph*, SkPoint> accepted, |
| 83 |
const SkPaint& paint) const = 0; |
| 84 |
virtual void drawBitmap(const SkBitmap&, |
| 85 |
const SkMatrix&, |
| 86 |
const SkRect* dstOrNull, |
| 87 |
const SkSamplingOptions&, |
| 88 |
const SkPaint&, |
| 89 |
sk_sp<SkMipmap>) const = 0; |
| 90 |
}; |
| 91 |
|
| 92 |
/** |
| 93 |
* The Draw class is the workhorse for the software rendering backend. It is an implementation |
| 94 |
* detail of SkCanvas that orchestrates the drawing of primitives into a CPU-backed SkPixmap. |
| 95 |
* |
| 96 |
* A Draw object is a lightweight context configured with everything needed for a single drawing |
| 97 |
* operation: |
| 98 |
* - The destination pixels (SkPixmap) |
| 99 |
* - The current transformation matrix |
| 100 |
* - The clipping region |
| 101 |
* |
| 102 |
* Its primary responsibility is to analyze the primitive (path, rect, etc.), the SkPaint, and the |
| 103 |
* device state to select and configure the most efficient SkBlitter. The SkBlitter then handles |
| 104 |
* the actual work of writing pixels into the destination pixmap. |
| 105 |
* The default Blitter is chosen via SkBlitter::Choose() |
| 106 |
*/ |
| 107 |
class Draw : public BitmapDevicePainter { |
| 108 |
public: |
| 109 |
Draw(); |
| 110 |
|
| 111 |
void drawPaint(const SkPaint&) const; |
| 112 |
void drawRect(const SkRect& prePaintRect, |
| 113 |
const SkPaint&, |
| 114 |
const SkMatrix* paintMatrix, |
| 115 |
const SkRect* postPaintRect) const; |
| 116 |
void drawRect(const SkRect& rect, const SkPaint& paint) const { |
| 117 |
this->drawRect(rect, paint, nullptr, nullptr); |
| 118 |
} |
| 119 |
void drawOval(const SkRect&, const SkPaint&) const; |
| 120 |
void drawRRect(const SkRRect&, const SkPaint&) const; |
| 121 |
// Specialized draw for RRect that only draws if it is nine-patchable. |
| 122 |
bool drawRRectNinePatch(const SkRRect&, const SkPaint&) const; |
| 123 |
/** |
| 124 |
* If prePathMatrix is not null, it should logically be applied before any |
| 125 |
* stroking or other effects. If there are no effects on the paint that |
| 126 |
* affect the geometry/rasterization, then the pre matrix can just be |
| 127 |
* pre-concated with the current matrix. |
| 128 |
*/ |
| 129 |
void drawPath(const SkPath& path, |
| 130 |
const SkPaint& paint, |
| 131 |
const SkMatrix* prePathMatrix) const { |
| 132 |
this->drawPath(path, paint, prePathMatrix, SkDrawCoverage::kNo); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Overwrite the target with the path's coverage (i.e. its mask). |
| 137 |
* Will overwrite the entire device, so it need not be zero'd first. |
| 138 |
* |
| 139 |
* Only device A8 is supported right now. |
| 140 |
*/ |
| 141 |
void drawPathCoverage(const SkPath& src, |
| 142 |
const SkPaint& paint, |
| 143 |
SkBlitter* customBlitter = nullptr) const { |
| 144 |
bool isHairline = paint.getStyle() == SkPaint::kStroke_Style && paint.getStrokeWidth() == 0; |
| 145 |
this->drawPath(src, |
| 146 |
paint, |
| 147 |
nullptr, |
| 148 |
isHairline ? SkDrawCoverage::kNo : SkDrawCoverage::kYes, |
| 149 |
customBlitter); |
| 150 |
} |
| 151 |
|
| 152 |
void drawDevicePoints(SkCanvas::PointMode, |
| 153 |
SkSpan<const SkPoint>, |
| 154 |
const SkPaint&, |
| 155 |
SkDevice*) const; |
| 156 |
|
| 157 |
enum class RectType { |
| 158 |
kHair, |
| 159 |
kFill, |
| 160 |
kStroke, |
| 161 |
kPath, |
| 162 |
}; |
| 163 |
|
| 164 |
/** |
| 165 |
* Based on the paint's style, strokeWidth, and the matrix, classify how |
| 166 |
* to draw the rect. If no special-case is available, returns |
| 167 |
* RectType::kPath. |
| 168 |
* |
| 169 |
* Iff RectType == RectType::kStroke, then strokeSize is set to the device |
| 170 |
* width and height of the stroke. |
| 171 |
*/ |
| 172 |
static RectType ComputeRectType(const SkRect&, |
| 173 |
const SkPaint&, |
| 174 |
const SkMatrix&, |
| 175 |
SkPoint* strokeSize); |
| 176 |
|
| 177 |
using BlitterChooser = SkBlitter*(const SkPixmap& dst, |
| 178 |
const SkMatrix& ctm, |
| 179 |
const SkPaint&, |
| 180 |
SkArenaAlloc*, |
| 181 |
SkDrawCoverage drawCoverage, |
| 182 |
sk_sp<SkShader> clipShader, |
| 183 |
const SkSurfaceProps&, |
| 184 |
const SkRect& devBounds); |
| 185 |
|
| 186 |
/* If dstOrNull is null, computes a dst by mapping the bitmap's bounds through the matrix. */ |
| 187 |
void drawBitmap(const SkBitmap&, |
| 188 |
const SkMatrix&, |
| 189 |
const SkRect* dstOrNull, |
| 190 |
const SkSamplingOptions&, |
| 191 |
const SkPaint&, |
| 192 |
sk_sp<SkMipmap>) const override; |
| 193 |
void drawSprite(const SkBitmap&, int x, int y, const SkPaint&) const; |
| 194 |
void drawGlyphRunList(SkCanvas* canvas, |
| 195 |
GlyphRunListPainter* glyphPainter, |
| 196 |
const sktext::GlyphRunList& glyphRunList, |
| 197 |
const SkPaint& paint) const; |
| 198 |
|
| 199 |
void paintMasks(SkZip<const SkGlyph*, SkPoint> accepted, const SkPaint& paint) const override; |
| 200 |
|
| 201 |
void drawPoints(SkCanvas::PointMode, SkSpan<const SkPoint>, const SkPaint&, SkDevice*) const; |
| 202 |
/* If skipColorXform, skips color conversion when assigning per-vertex colors */ |
| 203 |
void drawVertices(const SkVertices*, |
| 204 |
sk_sp<SkBlender>, |
| 205 |
const SkPaint&, |
| 206 |
bool skipColorXform) const; |
| 207 |
void drawAtlas(SkSpan<const SkRSXform>, SkSpan<const SkRect>, SkSpan<const SkColor>, |
| 208 |
sk_sp<SkBlender>, const SkPaint&); |
| 209 |
|
| 210 |
void drawDevMask(const SkMask& mask, const SkPaint&, const SkMatrix*) const; |
| 211 |
void drawBitmapAsMask(const SkBitmap&, const SkSamplingOptions&, const SkPaint&, |
| 212 |
const SkMatrix* paintMatrix) const; |
| 213 |
|
| 214 |
private: |
| 215 |
void drawPath(const SkPath&, |
| 216 |
const SkPaint&, |
| 217 |
const SkMatrix* preMatrix, |
| 218 |
SkDrawCoverage drawCoverage, |
| 219 |
SkBlitter* customBlitter = nullptr) const; |
| 220 |
|
| 221 |
void drawLine(const SkPoint[2], const SkPaint&) const; |
| 222 |
|
| 223 |
void drawDevPath(const SkPathRaw&, |
| 224 |
const SkPaint& paint, |
| 225 |
SkDrawCoverage drawCoverage, |
| 226 |
SkBlitter* customBlitter, |
| 227 |
bool doFill) const; |
| 228 |
/** |
| 229 |
* Return the current clip bounds, in local coordinates, with slop to account |
| 230 |
* for antialiasing or hairlines (i.e. device-bounds outset by 1, and then |
| 231 |
* run through the inverse of the matrix). |
| 232 |
* |
| 233 |
* If the matrix cannot be inverted, or the current clip is empty, return |
| 234 |
* false and ignore bounds parameter. |
| 235 |
*/ |
| 236 |
[[nodiscard]] bool computeConservativeLocalClipBounds(SkRect* bounds) const; |
| 237 |
|
| 238 |
void drawFixedVertices(const SkVertices* vertices, |
| 239 |
sk_sp<SkBlender> blender, |
| 240 |
const SkPaint& paint, |
| 241 |
const SkMatrix& ctmInverse, |
| 242 |
const SkPoint* dev2, |
| 243 |
const SkPoint3* dev3, |
| 244 |
SkArenaAlloc* outerAlloc, |
| 245 |
bool skipColorXform) const; |
| 246 |
|
| 247 |
public: |
| 248 |
SkPixmap fDst; |
| 249 |
BlitterChooser* fBlitterChooser{nullptr}; // required |
| 250 |
const SkMatrix* fCTM{nullptr}; // required |
| 251 |
const SkRasterClip* fRC{nullptr}; // required |
| 252 |
const SkSurfaceProps* fProps{nullptr}; // optional |
| 253 |
|
| 254 |
const ContextImpl* fCtx{nullptr}; // optional for now |
| 255 |
|
| 256 |
#ifdef SK_DEBUG |
| 257 |
void validate() const; |
| 258 |
#else |
| 259 |
void validate() const {} |
| 260 |
#endif |
| 261 |
}; |
| 262 |
|
| 263 |
} // namespace skcpu |
| 264 |
|
| 265 |
#endif |