Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrSWMaskHelper.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2012 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
9
18#include "src/gpu/ganesh/SkGr.h"
21
22static SkPaint get_paint(GrAA aa, uint8_t alpha) {
24 paint.setBlendMode(SkBlendMode::kSrc); // "Replace" mode
25 paint.setAntiAlias(GrAA::kYes == aa);
26 // SkPaint's color is unpremul so this will produce alpha in every channel.
27 paint.setColor(SkColorSetARGB(alpha, 255, 255, 255));
28 return paint;
29}
30
31/**
32 * Draw a single rect element of the clip stack into the accumulation bitmap
33 */
34void GrSWMaskHelper::drawRect(const SkRect& rect, const SkMatrix& matrix, GrAA aa, uint8_t alpha) {
35 SkMatrix translatedMatrix = matrix;
36 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
37 fDraw.fCTM = &translatedMatrix;
38
39 fDraw.drawRect(rect, get_paint(aa, alpha));
40}
41
42void GrSWMaskHelper::drawRRect(const SkRRect& rrect, const SkMatrix& matrix,
43 GrAA aa, uint8_t alpha) {
44 SkMatrix translatedMatrix = matrix;
45 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
46 fDraw.fCTM = &translatedMatrix;
47
48 fDraw.drawRRect(rrect, get_paint(aa, alpha));
49}
50
51/**
52 * Draw a single path element of the clip stack into the accumulation bitmap
53 */
54void GrSWMaskHelper::drawShape(const GrStyledShape& shape, const SkMatrix& matrix,
55 GrAA aa, uint8_t alpha) {
56 SkPaint paint = get_paint(aa, alpha);
57 paint.setPathEffect(shape.style().refPathEffect());
59
60 SkMatrix translatedMatrix = matrix;
61 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
62 fDraw.fCTM = &translatedMatrix;
63
64 SkPath path;
65 shape.asPath(&path);
66 if (0xFF == alpha) {
67 SkASSERT(0xFF == paint.getAlpha());
68 fDraw.drawPathCoverage(path, paint);
69 } else {
70 fDraw.drawPath(path, paint);
71 }
72}
73
74void GrSWMaskHelper::drawShape(const GrShape& shape, const SkMatrix& matrix,
75 GrAA aa, uint8_t alpha) {
76 SkPaint paint = get_paint(aa, alpha);
77
78 SkMatrix translatedMatrix = matrix;
79 translatedMatrix.postTranslate(fTranslate.fX, fTranslate.fY);
80 fDraw.fCTM = &translatedMatrix;
81
82 if (shape.inverted()) {
83 if (shape.isEmpty() || shape.isLine() || shape.isPoint()) {
84 // These shapes are empty for simple fills, so when inverted, cover everything
85 fDraw.drawPaint(paint);
86 return;
87 }
88 // Else fall through to the draw method using asPath(), which will toggle fill type properly
89 } else if (shape.isEmpty() || shape.isLine() || shape.isPoint()) {
90 // Do nothing, these shapes do not cover any pixels for simple fills
91 return;
92 } else if (shape.isRect()) {
93 fDraw.drawRect(shape.rect(), paint);
94 return;
95 } else if (shape.isRRect()) {
96 fDraw.drawRRect(shape.rrect(), paint);
97 return;
98 }
99
100 // A complex, or inverse-filled shape, so go through drawPath.
101 SkPath path;
102 shape.asPath(&path);
103 if (0xFF == alpha) {
104 SkASSERT(0xFF == paint.getAlpha());
105 fDraw.drawPathCoverage(path, paint);
106 } else {
107 fDraw.drawPath(path, paint);
108 }
109}
110
111bool GrSWMaskHelper::init(const SkIRect& resultBounds) {
112 // We will need to translate draws so the bound's UL corner is at the origin
113 fTranslate = {-SkIntToScalar(resultBounds.fLeft), -SkIntToScalar(resultBounds.fTop)};
114 SkIRect bounds = SkIRect::MakeWH(resultBounds.width(), resultBounds.height());
115
116 const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(bounds.width(), bounds.height());
117 if (!fPixels->tryAlloc(bmImageInfo)) {
118 return false;
119 }
120 fPixels->erase(0);
121
123 fDraw.fDst = *fPixels;
124 fRasterClip.setRect(bounds);
125 fDraw.fRC = &fRasterClip;
126 return true;
127}
128
130 SkImageInfo ii = SkImageInfo::MakeA8(fPixels->width(), fPixels->height());
131 size_t rowBytes = fPixels->rowBytes();
132
134 SkAssertResult(bitmap.installPixels(ii, fPixels->detachPixels(), rowBytes,
135 [](void* addr, void* context) { sk_free(addr); },
136 nullptr));
137 bitmap.setImmutable();
138
139 return std::get<0>(GrMakeUncachedBitmapProxyView(rContext, bitmap, skgpu::Mipmapped::kNo, fit));
140}
static SkPaint get_paint(GrAA aa, uint8_t alpha)
GrAA
#define SkAssertResult(cond)
Definition SkAssert.h:123
#define SkASSERT(cond)
Definition SkAssert.h:116
SkBackingFit
SkBlitter * SkA8Blitter_Choose(const SkPixmap &dst, const SkMatrix &ctm, const SkPaint &paint, SkArenaAlloc *alloc, bool drawCoverage, sk_sp< SkShader > clipShader, const SkSurfaceProps &)
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.h:49
std::tuple< GrSurfaceProxyView, GrColorType > GrMakeUncachedBitmapProxyView(GrRecordingContext *rContext, const SkBitmap &bitmap, skgpu::Mipmapped mipmapped, SkBackingFit fit, skgpu::Budgeted budgeted)
Definition SkGr.cpp:253
#define SkIntToScalar(x)
Definition SkScalar.h:57
void drawRect(const SkRect &rect, const SkMatrix &matrix, GrAA, uint8_t alpha)
void drawRRect(const SkRRect &rrect, const SkMatrix &matrix, GrAA, uint8_t alpha)
void drawShape(const GrStyledShape &, const SkMatrix &matrix, GrAA, uint8_t alpha)
bool init(const SkIRect &resultBounds)
GrSurfaceProxyView toTextureView(GrRecordingContext *, SkBackingFit fit)
SkRect & rect()
Definition GrShape.h:134
bool isRRect() const
Definition GrShape.h:87
bool isLine() const
Definition GrShape.h:90
SkRRect & rrect()
Definition GrShape.h:137
bool isRect() const
Definition GrShape.h:86
bool isPoint() const
Definition GrShape.h:85
bool inverted() const
Definition GrShape.h:99
void asPath(SkPath *out, bool simpleFill=true) const
Definition GrShape.cpp:421
bool isEmpty() const
Definition GrShape.h:84
sk_sp< SkPathEffect > refPathEffect() const
Definition GrStyle.h:120
const SkStrokeRec & strokeRec() const
Definition GrStyle.h:140
void asPath(SkPath *out) const
const GrStyle & style() const
bool tryAlloc(const SkImageInfo &)
void drawRRect(const SkRRect &, const SkPaint &) const
BlitterChooser * fBlitterChooser
Definition SkDrawBase.h:152
void drawPath(const SkPath &path, const SkPaint &paint, const SkMatrix *prePathMatrix=nullptr, bool pathIsMutable=false) const
Definition SkDrawBase.h:58
void drawPaint(const SkPaint &) const
const SkRasterClip * fRC
Definition SkDrawBase.h:154
void drawPathCoverage(const SkPath &src, const SkPaint &paint, SkBlitter *customBlitter=nullptr) const
Definition SkDrawBase.h:69
void drawRect(const SkRect &prePaintRect, const SkPaint &, const SkMatrix *paintMatrix, const SkRect *postPaintRect) const
SkPixmap fDst
Definition SkDrawBase.h:151
const SkMatrix * fCTM
Definition SkDrawBase.h:153
SkMatrix & postTranslate(SkScalar dx, SkScalar dy)
Definition SkMatrix.cpp:281
bool erase(SkColor color, const SkIRect &subset) const
Definition SkPixmap.cpp:742
size_t rowBytes() const
Definition SkPixmap.h:145
int width() const
Definition SkPixmap.h:160
int height() const
Definition SkPixmap.h:166
bool setRect(const SkIRect &)
void applyToPaint(SkPaint *paint) const
const Paint & paint
constexpr int32_t height() const
Definition SkRect.h:165
int32_t fTop
smaller y-axis bounds
Definition SkRect.h:34
constexpr int32_t width() const
Definition SkRect.h:158
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition SkRect.h:56
int32_t fLeft
smaller x-axis bounds
Definition SkRect.h:33
static SkImageInfo MakeA8(int width, int height)
float fX
x-axis value
float fY
y-axis value