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
8 #include "include/core/SkOverdrawCanvas.h"
9
10 #include "include/core/SkColorFilter.h"
11 #include "include/core/SkDrawable.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkRRect.h"
14 #include "include/core/SkRSXform.h"
15 #include "include/core/SkTextBlob.h"
16 #include "include/private/base/SkTo.h"
17 #include "src/core/SkDevice.h"
18 #include "src/core/SkDrawShadowInfo.h"
19 #include "src/core/SkGlyphBuffer.h"
20 #include "src/core/SkGlyphRunPainter.h"
21 #include "src/core/SkImagePriv.h"
22 #include "src/core/SkLatticeIter.h"
23 #include "src/core/SkStrikeCache.h"
24 #include "src/core/SkTextBlobPriv.h"
25 #include "src/text/GlyphRun.h"
26 #include "src/utils/SkPatchUtils.h"
27
SkOverdrawCanvas(SkCanvas * canvas)28 SkOverdrawCanvas::SkOverdrawCanvas(SkCanvas* canvas)
29 : INHERITED(canvas->onImageInfo().width(), canvas->onImageInfo().height())
30 {
31 // Non-drawing calls that SkOverdrawCanvas does not override (translate, save, etc.)
32 // will pass through to the input canvas.
33 this->addCanvas(canvas);
34
35 static constexpr float kIncrementAlpha[] = {
36 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
37 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
38 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
39 0.0f, 0.0f, 0.0f, 0.0f, 1.0f/255,
40 };
41
42 fPaint.setAntiAlias(false);
43 fPaint.setBlendMode(SkBlendMode::kPlus);
44 fPaint.setColorFilter(SkColorFilters::Matrix(kIncrementAlpha));
45 }
46
47 namespace {
48 class TextDevice : public SkNoPixelsDevice, public SkGlyphRunListPainterCPU::BitmapDevicePainter {
49 public:
TextDevice(SkCanvas * overdrawCanvas,const SkSurfaceProps & props)50 TextDevice(SkCanvas* overdrawCanvas, const SkSurfaceProps& props)
51 : SkNoPixelsDevice{SkIRect::MakeWH(32767, 32767), props},
52 fOverdrawCanvas{overdrawCanvas},
53 fPainter{props, kN32_SkColorType, nullptr} {}
54
paintMasks(SkZip<const SkGlyph *,SkPoint> accepted,const SkPaint & paint) const55 void paintMasks(SkZip<const SkGlyph*, SkPoint> accepted, const SkPaint& paint) const override {
56 for (auto [glyph, pos] : accepted) {
57 SkMask mask = glyph->mask(pos);
58 // We need to ignore any matrix on the overdraw canvas (it's already been baked into
59 // our glyph positions). Otherwise, the CTM is double-applied. (skbug.com/13732)
60 fOverdrawCanvas->save();
61 fOverdrawCanvas->resetMatrix();
62 fOverdrawCanvas->drawRect(SkRect::Make(mask.fBounds), SkPaint());
63 fOverdrawCanvas->restore();
64 }
65 }
66
drawBitmap(const SkBitmap &,const SkMatrix &,const SkRect * dstOrNull,const SkSamplingOptions &,const SkPaint &) const67 void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
68 const SkSamplingOptions&, const SkPaint&) const override {}
69
onDrawGlyphRunList(SkCanvas * canvas,const sktext::GlyphRunList & glyphRunList,const SkPaint & initialPaint,const SkPaint & drawingPaint)70 void onDrawGlyphRunList(SkCanvas* canvas,
71 const sktext::GlyphRunList& glyphRunList,
72 const SkPaint& initialPaint,
73 const SkPaint& drawingPaint) override {
74 SkASSERT(!glyphRunList.hasRSXForm());
75 fPainter.drawForBitmapDevice(canvas, this, glyphRunList, drawingPaint,
76 fOverdrawCanvas->getTotalMatrix());
77 }
78
79 private:
80 SkCanvas* const fOverdrawCanvas;
81 SkGlyphRunListPainterCPU fPainter;
82 };
83 } // namespace
84
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)85 void SkOverdrawCanvas::onDrawTextBlob(
86 const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) {
87 sktext::GlyphRunBuilder b;
88 auto glyphRunList = b.blobToGlyphRunList(*blob, {x, y});
89 this->onDrawGlyphRunList(glyphRunList, paint);
90 }
91
onDrawGlyphRunList(const sktext::GlyphRunList & glyphRunList,const SkPaint & paint)92 void SkOverdrawCanvas::onDrawGlyphRunList(
93 const sktext::GlyphRunList& glyphRunList,
94 const SkPaint& paint) {
95 SkSurfaceProps props{0, kUnknown_SkPixelGeometry};
96 this->getProps(&props);
97 TextDevice device{this, props};
98
99 device.drawGlyphRunList(this, glyphRunList, paint, paint);
100 }
101
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode blendMode,const SkPaint &)102 void SkOverdrawCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
103 const SkPoint texCoords[4], SkBlendMode blendMode,
104 const SkPaint&) {
105 fList[0]->onDrawPatch(cubics, colors, texCoords, blendMode, fPaint);
106 }
107
onDrawPaint(const SkPaint & paint)108 void SkOverdrawCanvas::onDrawPaint(const SkPaint& paint) {
109 if (0 == paint.getColor() && !paint.getColorFilter() && !paint.getShader()) {
110 // This is a clear, ignore it.
111 } else {
112 fList[0]->onDrawPaint(this->overdrawPaint(paint));
113 }
114 }
115
onDrawBehind(const SkPaint & paint)116 void SkOverdrawCanvas::onDrawBehind(const SkPaint& paint) {
117 fList[0]->onDrawBehind(this->overdrawPaint(paint));
118 }
119
onDrawRect(const SkRect & rect,const SkPaint & paint)120 void SkOverdrawCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
121 fList[0]->onDrawRect(rect, this->overdrawPaint(paint));
122 }
123
onDrawRegion(const SkRegion & region,const SkPaint & paint)124 void SkOverdrawCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
125 fList[0]->onDrawRegion(region, this->overdrawPaint(paint));
126 }
127
onDrawOval(const SkRect & oval,const SkPaint & paint)128 void SkOverdrawCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
129 fList[0]->onDrawOval(oval, this->overdrawPaint(paint));
130 }
131
onDrawArc(const SkRect & arc,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)132 void SkOverdrawCanvas::onDrawArc(const SkRect& arc, SkScalar startAngle, SkScalar sweepAngle,
133 bool useCenter, const SkPaint& paint) {
134 fList[0]->onDrawArc(arc, startAngle, sweepAngle, useCenter, this->overdrawPaint(paint));
135 }
136
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)137 void SkOverdrawCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
138 const SkPaint& paint) {
139 fList[0]->onDrawDRRect(outer, inner, this->overdrawPaint(paint));
140 }
141
onDrawRRect(const SkRRect & rect,const SkPaint & paint)142 void SkOverdrawCanvas::onDrawRRect(const SkRRect& rect, const SkPaint& paint) {
143 fList[0]->onDrawRRect(rect, this->overdrawPaint(paint));
144 }
145
onDrawPoints(PointMode mode,size_t count,const SkPoint points[],const SkPaint & paint)146 void SkOverdrawCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint points[],
147 const SkPaint& paint) {
148 fList[0]->onDrawPoints(mode, count, points, this->overdrawPaint(paint));
149 }
150
onDrawVerticesObject(const SkVertices * vertices,SkBlendMode blendMode,const SkPaint & paint)151 void SkOverdrawCanvas::onDrawVerticesObject(const SkVertices* vertices,
152 SkBlendMode blendMode, const SkPaint& paint) {
153 fList[0]->onDrawVerticesObject(vertices, blendMode, this->overdrawPaint(paint));
154 }
155
onDrawAtlas2(const SkImage * image,const SkRSXform xform[],const SkRect texs[],const SkColor colors[],int count,SkBlendMode mode,const SkSamplingOptions & sampling,const SkRect * cull,const SkPaint * paint)156 void SkOverdrawCanvas::onDrawAtlas2(const SkImage* image, const SkRSXform xform[],
157 const SkRect texs[], const SkColor colors[], int count,
158 SkBlendMode mode, const SkSamplingOptions& sampling,
159 const SkRect* cull, const SkPaint* paint) {
160 SkPaint* paintPtr = &fPaint;
161 SkPaint storage;
162 if (paint) {
163 storage = this->overdrawPaint(*paint);
164 paintPtr = &storage;
165 }
166
167 fList[0]->onDrawAtlas2(image, xform, texs, colors, count, mode, sampling, cull, paintPtr);
168 }
169
onDrawPath(const SkPath & path,const SkPaint & paint)170 void SkOverdrawCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
171 fList[0]->onDrawPath(path, fPaint);
172 }
173
onDrawImage2(const SkImage * image,SkScalar x,SkScalar y,const SkSamplingOptions &,const SkPaint *)174 void SkOverdrawCanvas::onDrawImage2(const SkImage* image, SkScalar x, SkScalar y,
175 const SkSamplingOptions&, const SkPaint*) {
176 fList[0]->onDrawRect(SkRect::MakeXYWH(x, y, image->width(), image->height()), fPaint);
177 }
178
onDrawImageRect2(const SkImage * image,const SkRect & src,const SkRect & dst,const SkSamplingOptions &,const SkPaint *,SrcRectConstraint)179 void SkOverdrawCanvas::onDrawImageRect2(const SkImage* image, const SkRect& src, const SkRect& dst,
180 const SkSamplingOptions&, const SkPaint*, SrcRectConstraint) {
181 fList[0]->onDrawRect(dst, fPaint);
182 }
183
onDrawImageLattice2(const SkImage * image,const Lattice & lattice,const SkRect & dst,SkFilterMode,const SkPaint *)184 void SkOverdrawCanvas::onDrawImageLattice2(const SkImage* image, const Lattice& lattice,
185 const SkRect& dst, SkFilterMode, const SkPaint*) {
186 SkIRect bounds;
187 Lattice latticePlusBounds = lattice;
188 if (!latticePlusBounds.fBounds) {
189 bounds = SkIRect::MakeWH(image->width(), image->height());
190 latticePlusBounds.fBounds = &bounds;
191 }
192
193 if (SkLatticeIter::Valid(image->width(), image->height(), latticePlusBounds)) {
194 SkLatticeIter iter(latticePlusBounds, dst);
195
196 SkRect ignored, iterDst;
197 while (iter.next(&ignored, &iterDst)) {
198 fList[0]->onDrawRect(iterDst, fPaint);
199 }
200 } else {
201 fList[0]->onDrawRect(dst, fPaint);
202 }
203 }
204
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)205 void SkOverdrawCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
206 drawable->draw(this, matrix);
207 }
208
onDrawPicture(const SkPicture *,const SkMatrix *,const SkPaint *)209 void SkOverdrawCanvas::onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) {
210 SkASSERT(false);
211 return;
212 }
213
onDrawAnnotation(const SkRect &,const char[],SkData *)214 void SkOverdrawCanvas::onDrawAnnotation(const SkRect&, const char[], SkData*) {}
215
onDrawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)216 void SkOverdrawCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
217 SkRect bounds;
218 SkDrawShadowMetrics::GetLocalBounds(path, rec, this->getTotalMatrix(), &bounds);
219 fList[0]->onDrawRect(bounds, fPaint);
220 }
221
onDrawEdgeAAQuad(const SkRect & rect,const SkPoint clip[4],QuadAAFlags aa,const SkColor4f & color,SkBlendMode mode)222 void SkOverdrawCanvas::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
223 QuadAAFlags aa, const SkColor4f& color, SkBlendMode mode) {
224 if (clip) {
225 fList[0]->onDrawPath(SkPath::Polygon(clip, 4, true), fPaint);
226 } else {
227 fList[0]->onDrawRect(rect, fPaint);
228 }
229 }
230
onDrawEdgeAAImageSet2(const ImageSetEntry set[],int count,const SkPoint dstClips[],const SkMatrix preViewMatrices[],const SkSamplingOptions & sampling,const SkPaint * paint,SrcRectConstraint constraint)231 void SkOverdrawCanvas::onDrawEdgeAAImageSet2(const ImageSetEntry set[], int count,
232 const SkPoint dstClips[],
233 const SkMatrix preViewMatrices[],
234 const SkSamplingOptions& sampling,
235 const SkPaint* paint,
236 SrcRectConstraint constraint) {
237 int clipIndex = 0;
238 for (int i = 0; i < count; ++i) {
239 if (set[i].fMatrixIndex >= 0) {
240 fList[0]->save();
241 fList[0]->concat(preViewMatrices[set[i].fMatrixIndex]);
242 }
243 if (set[i].fHasClip) {
244 fList[0]->onDrawPath(SkPath::Polygon(dstClips + clipIndex, 4, true), fPaint);
245 clipIndex += 4;
246 } else {
247 fList[0]->onDrawRect(set[i].fDstRect, fPaint);
248 }
249 if (set[i].fMatrixIndex >= 0) {
250 fList[0]->restore();
251 }
252 }
253 }
254
overdrawPaint(const SkPaint & paint)255 inline SkPaint SkOverdrawCanvas::overdrawPaint(const SkPaint& paint) {
256 SkPaint newPaint = fPaint;
257 newPaint.setStyle(paint.getStyle());
258 newPaint.setStrokeWidth(paint.getStrokeWidth());
259 return newPaint;
260 }
261