Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkOverdrawCanvas.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 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
17#include "include/core/SkPath.h"
19#include "include/core/SkRect.h"
24#include "src/base/SkZip.h"
25#include "src/core/SkDevice.h"
27#include "src/core/SkGlyph.h"
30#include "src/core/SkMask.h"
31#include "src/text/GlyphRun.h"
32
33class SkBitmap;
34class SkData;
35class SkPicture;
36class SkRRect;
37class SkRegion;
38class SkTextBlob;
39class SkVertices;
40
42 : INHERITED(canvas->onImageInfo().width(), canvas->onImageInfo().height())
43{
44 // Non-drawing calls that SkOverdrawCanvas does not override (translate, save, etc.)
45 // will pass through to the input canvas.
46 this->addCanvas(canvas);
47
48 static constexpr float kIncrementAlpha[] = {
49 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
50 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
51 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
52 0.0f, 0.0f, 0.0f, 0.0f, 1.0f/255,
53 };
54
55 fPaint.setAntiAlias(false);
57 fPaint.setColorFilter(SkColorFilters::Matrix(kIncrementAlpha));
58}
59
60namespace {
62public:
63 TextDevice(SkCanvas* overdrawCanvas, const SkSurfaceProps& props)
64 : SkNoPixelsDevice{SkIRect::MakeWH(32767, 32767), props},
65 fOverdrawCanvas{overdrawCanvas},
66 fPainter{props, kN32_SkColorType, nullptr} {}
67
68 void paintMasks(SkZip<const SkGlyph*, SkPoint> accepted, const SkPaint& paint) const override {
69 for (auto [glyph, pos] : accepted) {
70 SkMask mask = glyph->mask(pos);
71 // We need to ignore any matrix on the overdraw canvas (it's already been baked into
72 // our glyph positions). Otherwise, the CTM is double-applied. (skbug.com/13732)
73 fOverdrawCanvas->save();
74 fOverdrawCanvas->resetMatrix();
75 fOverdrawCanvas->drawRect(SkRect::Make(mask.fBounds), SkPaint());
76 fOverdrawCanvas->restore();
77 }
78 }
79
80 void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
81 const SkSamplingOptions&, const SkPaint&) const override {}
82
83 void onDrawGlyphRunList(SkCanvas* canvas,
84 const sktext::GlyphRunList& glyphRunList,
85 const SkPaint& paint) override {
86 SkASSERT(!glyphRunList.hasRSXForm());
87 fPainter.drawForBitmapDevice(
88 canvas, this, glyphRunList, paint, fOverdrawCanvas->getTotalMatrix());
89 }
90
91private:
92 SkCanvas* const fOverdrawCanvas;
94};
95} // namespace
96
98 const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) {
100 auto glyphRunList = b.blobToGlyphRunList(*blob, {x, y});
101 this->onDrawGlyphRunList(glyphRunList, paint);
102}
103
105 const SkPaint& paint) {
106 SkSurfaceProps props;
107 this->getProps(&props);
108 TextDevice device{this, props};
109
110 device.drawGlyphRunList(this, glyphRunList, paint);
111}
112
113void SkOverdrawCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
114 const SkPoint texCoords[4], SkBlendMode blendMode,
115 const SkPaint&) {
116 fList[0]->onDrawPatch(cubics, colors, texCoords, blendMode, fPaint);
117}
118
120 if (0 == paint.getColor() && !paint.getColorFilter() && !paint.getShader()) {
121 // This is a clear, ignore it.
122 } else {
123 fList[0]->onDrawPaint(this->overdrawPaint(paint));
124 }
125}
126
128 fList[0]->onDrawBehind(this->overdrawPaint(paint));
129}
130
132 fList[0]->onDrawRect(rect, this->overdrawPaint(paint));
133}
134
136 fList[0]->onDrawRegion(region, this->overdrawPaint(paint));
137}
138
140 fList[0]->onDrawOval(oval, this->overdrawPaint(paint));
141}
142
143void SkOverdrawCanvas::onDrawArc(const SkRect& arc, SkScalar startAngle, SkScalar sweepAngle,
144 bool useCenter, const SkPaint& paint) {
145 fList[0]->onDrawArc(arc, startAngle, sweepAngle, useCenter, this->overdrawPaint(paint));
146}
147
148void SkOverdrawCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
149 const SkPaint& paint) {
150 fList[0]->onDrawDRRect(outer, inner, this->overdrawPaint(paint));
151}
152
154 fList[0]->onDrawRRect(rect, this->overdrawPaint(paint));
155}
156
158 const SkPaint& paint) {
159 fList[0]->onDrawPoints(mode, count, points, this->overdrawPaint(paint));
160}
161
163 SkBlendMode blendMode, const SkPaint& paint) {
164 fList[0]->onDrawVerticesObject(vertices, blendMode, this->overdrawPaint(paint));
165}
166
168 const SkRect texs[], const SkColor colors[], int count,
169 SkBlendMode mode, const SkSamplingOptions& sampling,
170 const SkRect* cull, const SkPaint* paint) {
171 SkPaint* paintPtr = &fPaint;
172 SkPaint storage;
173 if (paint) {
174 storage = this->overdrawPaint(*paint);
175 paintPtr = &storage;
176 }
177
178 fList[0]->onDrawAtlas2(image, xform, texs, colors, count, mode, sampling, cull, paintPtr);
179}
180
182 fList[0]->onDrawPath(path, fPaint);
183}
184
186 const SkSamplingOptions&, const SkPaint*) {
187 fList[0]->onDrawRect(SkRect::MakeXYWH(x, y, image->width(), image->height()), fPaint);
188}
189
192 fList[0]->onDrawRect(dst, fPaint);
193}
194
196 const SkRect& dst, SkFilterMode, const SkPaint*) {
197 SkIRect bounds;
198 Lattice latticePlusBounds = lattice;
199 if (!latticePlusBounds.fBounds) {
200 bounds = SkIRect::MakeWH(image->width(), image->height());
201 latticePlusBounds.fBounds = &bounds;
202 }
203
204 if (SkLatticeIter::Valid(image->width(), image->height(), latticePlusBounds)) {
205 SkLatticeIter iter(latticePlusBounds, dst);
206
207 SkRect ignored, iterDst;
208 while (iter.next(&ignored, &iterDst)) {
209 fList[0]->onDrawRect(iterDst, fPaint);
210 }
211 } else {
212 fList[0]->onDrawRect(dst, fPaint);
213 }
214}
215
217 drawable->draw(this, matrix);
218}
219
221 SkASSERT(false);
222}
223
224void SkOverdrawCanvas::onDrawAnnotation(const SkRect&, const char[], SkData*) {}
225
227 SkRect bounds;
228 SkDrawShadowMetrics::GetLocalBounds(path, rec, this->getTotalMatrix(), &bounds);
229 fList[0]->onDrawRect(bounds, fPaint);
230}
231
233 QuadAAFlags aa, const SkColor4f& color, SkBlendMode mode) {
234 if (clip) {
235 fList[0]->onDrawPath(SkPath::Polygon(clip, 4, true), fPaint);
236 } else {
237 fList[0]->onDrawRect(rect, fPaint);
238 }
239}
240
242 const SkPoint dstClips[],
243 const SkMatrix preViewMatrices[],
244 const SkSamplingOptions& sampling,
245 const SkPaint* paint,
246 SrcRectConstraint constraint) {
247 int clipIndex = 0;
248 for (int i = 0; i < count; ++i) {
249 if (set[i].fMatrixIndex >= 0) {
250 fList[0]->save();
251 fList[0]->concat(preViewMatrices[set[i].fMatrixIndex]);
252 }
253 if (set[i].fHasClip) {
254 fList[0]->onDrawPath(SkPath::Polygon(dstClips + clipIndex, 4, true), fPaint);
255 clipIndex += 4;
256 } else {
257 fList[0]->onDrawRect(set[i].fDstRect, fPaint);
258 }
259 if (set[i].fMatrixIndex >= 0) {
260 fList[0]->restore();
261 }
262 }
263}
264
265inline SkPaint SkOverdrawCanvas::overdrawPaint(const SkPaint& paint) {
266 SkPaint newPaint = fPaint;
267 newPaint.setStyle(paint.getStyle());
268 newPaint.setStrokeWidth(paint.getStrokeWidth());
269 return newPaint;
270}
int count
static const int points[]
SkPoint pos
SkColor4f color
#define SkASSERT(cond)
Definition SkAssert.h:116
SkBlendMode
Definition SkBlendMode.h:38
@ kPlus
r = min(s + d, 1)
uint32_t SkColor
Definition SkColor.h:37
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
SkFilterMode
bool getProps(SkSurfaceProps *props) const
friend class SkOverdrawCanvas
Definition SkCanvas.h:2494
SrcRectConstraint
Definition SkCanvas.h:1541
SkMatrix getTotalMatrix() const
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
void draw(SkCanvas *, const SkMatrix *=nullptr)
int width() const
Definition SkImage.h:285
int height() const
Definition SkImage.h:291
bool next(SkIRect *src, SkRect *dst, bool *isFixedColor=nullptr, SkColor *fixedColor=nullptr)
static bool Valid(int imageWidth, int imageHeight, const SkCanvas::Lattice &lattice)
virtual void addCanvas(SkCanvas *)
SkTDArray< SkCanvas * > fList
void onDrawImage2(const SkImage *, SkScalar, SkScalar, const SkSamplingOptions &, const SkPaint *) override
void onDrawTextBlob(const SkTextBlob *, SkScalar, SkScalar, const SkPaint &) override
void onDrawAnnotation(const SkRect &, const char key[], SkData *value) override
void onDrawImageRect2(const SkImage *, const SkRect &, const SkRect &, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint) override
void onDrawImageLattice2(const SkImage *, const Lattice &, const SkRect &, SkFilterMode, const SkPaint *) override
void onDrawOval(const SkRect &, const SkPaint &) override
void onDrawPoints(PointMode, size_t, const SkPoint[], const SkPaint &) override
void onDrawPath(const SkPath &, const SkPaint &) override
void onDrawRRect(const SkRRect &, const SkPaint &) override
void onDrawPatch(const SkPoint[12], const SkColor[4], const SkPoint[4], SkBlendMode, const SkPaint &) override
void onDrawShadowRec(const SkPath &, const SkDrawShadowRec &) override
void onDrawEdgeAAImageSet2(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[], const SkSamplingOptions &, const SkPaint *, SrcRectConstraint) override
void onDrawVerticesObject(const SkVertices *, SkBlendMode, const SkPaint &) override
void onDrawGlyphRunList(const sktext::GlyphRunList &glyphRunList, const SkPaint &paint) override
void onDrawRegion(const SkRegion &, const SkPaint &) override
void onDrawEdgeAAQuad(const SkRect &, const SkPoint[4], SkCanvas::QuadAAFlags, const SkColor4f &, SkBlendMode) override
void onDrawArc(const SkRect &, SkScalar, SkScalar, bool, const SkPaint &) override
void onDrawPaint(const SkPaint &) override
void onDrawRect(const SkRect &, const SkPaint &) override
void onDrawDrawable(SkDrawable *, const SkMatrix *) override
void onDrawPicture(const SkPicture *, const SkMatrix *, const SkPaint *) override
void onDrawDRRect(const SkRRect &, const SkRRect &, const SkPaint &) override
void onDrawBehind(const SkPaint &paint) override
void onDrawAtlas2(const SkImage *, const SkRSXform[], const SkRect[], const SkColor[], int, SkBlendMode, const SkSamplingOptions &, const SkRect *, const SkPaint *) override
void setStyle(Style style)
Definition SkPaint.cpp:105
void setAntiAlias(bool aa)
Definition SkPaint.h:170
void setBlendMode(SkBlendMode mode)
Definition SkPaint.cpp:151
void setColorFilter(sk_sp< SkColorFilter > colorFilter)
void setStrokeWidth(SkScalar width)
Definition SkPaint.cpp:159
static SkPath Polygon(const SkPoint pts[], int count, bool isClosed, SkPathFillType=SkPathFillType::kWinding, bool isVolatile=false)
Definition SkPath.cpp:3546
Definition SkZip.h:25
const GlyphRunList & blobToGlyphRunList(const SkTextBlob &blob, SkPoint origin)
Definition GlyphRun.cpp:233
bool hasRSXForm() const
Definition GlyphRun.h:105
const Paint & paint
VkDevice device
Definition main.cc:53
sk_sp< SkImage > image
Definition examples.cpp:29
float SkScalar
Definition extension.cpp:12
static bool b
double y
double x
void GetLocalBounds(const SkPath &path, const SkDrawShadowRec &rec, const SkMatrix &ctm, SkRect *bounds)
int32_t height
int32_t width
const SkIRect * fBounds
source bounds to draw from
Definition SkCanvas.h:1618
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition SkRect.h:56
const SkIRect fBounds
Definition SkMask.h:42
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659