xref: /aosp_15_r20/external/skia/src/core/SkOverdrawCanvas.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SkBlendMode.h"
11 #include "include/core/SkColorFilter.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkDrawable.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkMatrix.h"
17 #include "include/core/SkPath.h"
18 #include "include/core/SkRSXform.h"
19 #include "include/core/SkRect.h"
20 #include "include/core/SkSurfaceProps.h"
21 #include "include/private/base/SkAssert.h"
22 #include "include/private/base/SkPoint_impl.h"
23 #include "include/private/base/SkTDArray.h"
24 #include "src/base/SkZip.h"
25 #include "src/core/SkDevice.h"
26 #include "src/core/SkDrawShadowInfo.h"
27 #include "src/core/SkGlyph.h"
28 #include "src/core/SkGlyphRunPainter.h"
29 #include "src/core/SkLatticeIter.h"
30 #include "src/core/SkMask.h"
31 #include "src/text/GlyphRun.h"
32 
33 class SkBitmap;
34 class SkData;
35 class SkPicture;
36 class SkRRect;
37 class SkRegion;
38 class SkTextBlob;
39 class SkVertices;
40 
SkOverdrawCanvas(SkCanvas * canvas)41 SkOverdrawCanvas::SkOverdrawCanvas(SkCanvas* canvas)
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);
56     fPaint.setBlendMode(SkBlendMode::kPlus);
57     fPaint.setColorFilter(SkColorFilters::Matrix(kIncrementAlpha));
58 }
59 
60 namespace {
61 class TextDevice : public SkNoPixelsDevice, public SkGlyphRunListPainterCPU::BitmapDevicePainter {
62 public:
TextDevice(SkCanvas * overdrawCanvas,const SkSurfaceProps & props)63     TextDevice(SkCanvas* overdrawCanvas, const SkSurfaceProps& props)
64             : SkNoPixelsDevice{SkIRect::MakeWH(32767, 32767), props},
65               fOverdrawCanvas{overdrawCanvas},
66               fPainter{props, kN32_SkColorType, nullptr} {}
67 
paintMasks(SkZip<const SkGlyph *,SkPoint> accepted,const SkPaint & paint) const68     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 
drawBitmap(const SkBitmap &,const SkMatrix &,const SkRect * dstOrNull,const SkSamplingOptions &,const SkPaint &) const80     void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
81                     const SkSamplingOptions&, const SkPaint&) const override {}
82 
onDrawGlyphRunList(SkCanvas * canvas,const sktext::GlyphRunList & glyphRunList,const SkPaint & paint)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 
91 private:
92     SkCanvas* const fOverdrawCanvas;
93     SkGlyphRunListPainterCPU fPainter;
94 };
95 }  // namespace
96 
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)97 void SkOverdrawCanvas::onDrawTextBlob(
98         const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) {
99     sktext::GlyphRunBuilder b;
100     auto glyphRunList = b.blobToGlyphRunList(*blob, {x, y});
101     this->onDrawGlyphRunList(glyphRunList, paint);
102 }
103 
onDrawGlyphRunList(const sktext::GlyphRunList & glyphRunList,const SkPaint & paint)104 void SkOverdrawCanvas::onDrawGlyphRunList(const sktext::GlyphRunList& glyphRunList,
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 
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode blendMode,const SkPaint &)113 void 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 
onDrawPaint(const SkPaint & paint)119 void SkOverdrawCanvas::onDrawPaint(const SkPaint& paint) {
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 
onDrawBehind(const SkPaint & paint)127 void SkOverdrawCanvas::onDrawBehind(const SkPaint& paint) {
128     fList[0]->onDrawBehind(this->overdrawPaint(paint));
129 }
130 
onDrawRect(const SkRect & rect,const SkPaint & paint)131 void SkOverdrawCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
132     fList[0]->onDrawRect(rect, this->overdrawPaint(paint));
133 }
134 
onDrawRegion(const SkRegion & region,const SkPaint & paint)135 void SkOverdrawCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
136     fList[0]->onDrawRegion(region, this->overdrawPaint(paint));
137 }
138 
onDrawOval(const SkRect & oval,const SkPaint & paint)139 void SkOverdrawCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
140     fList[0]->onDrawOval(oval, this->overdrawPaint(paint));
141 }
142 
onDrawArc(const SkRect & arc,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)143 void 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 
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)148 void SkOverdrawCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
149                                     const SkPaint& paint) {
150     fList[0]->onDrawDRRect(outer, inner, this->overdrawPaint(paint));
151 }
152 
onDrawRRect(const SkRRect & rect,const SkPaint & paint)153 void SkOverdrawCanvas::onDrawRRect(const SkRRect& rect, const SkPaint& paint) {
154     fList[0]->onDrawRRect(rect, this->overdrawPaint(paint));
155 }
156 
onDrawPoints(PointMode mode,size_t count,const SkPoint points[],const SkPaint & paint)157 void SkOverdrawCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint points[],
158                                     const SkPaint& paint) {
159     fList[0]->onDrawPoints(mode, count, points, this->overdrawPaint(paint));
160 }
161 
onDrawVerticesObject(const SkVertices * vertices,SkBlendMode blendMode,const SkPaint & paint)162 void SkOverdrawCanvas::onDrawVerticesObject(const SkVertices* vertices,
163                                             SkBlendMode blendMode, const SkPaint& paint) {
164     fList[0]->onDrawVerticesObject(vertices, blendMode, this->overdrawPaint(paint));
165 }
166 
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)167 void SkOverdrawCanvas::onDrawAtlas2(const SkImage* image, const SkRSXform xform[],
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 
onDrawPath(const SkPath & path,const SkPaint & paint)181 void SkOverdrawCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
182     fList[0]->onDrawPath(path, fPaint);
183 }
184 
onDrawImage2(const SkImage * image,SkScalar x,SkScalar y,const SkSamplingOptions &,const SkPaint *)185 void SkOverdrawCanvas::onDrawImage2(const SkImage* image, SkScalar x, SkScalar y,
186                                     const SkSamplingOptions&, const SkPaint*) {
187     fList[0]->onDrawRect(SkRect::MakeXYWH(x, y, image->width(), image->height()), fPaint);
188 }
189 
onDrawImageRect2(const SkImage * image,const SkRect & src,const SkRect & dst,const SkSamplingOptions &,const SkPaint *,SrcRectConstraint)190 void SkOverdrawCanvas::onDrawImageRect2(const SkImage* image, const SkRect& src, const SkRect& dst,
191                                         const SkSamplingOptions&, const SkPaint*, SrcRectConstraint) {
192     fList[0]->onDrawRect(dst, fPaint);
193 }
194 
onDrawImageLattice2(const SkImage * image,const Lattice & lattice,const SkRect & dst,SkFilterMode,const SkPaint *)195 void SkOverdrawCanvas::onDrawImageLattice2(const SkImage* image, const Lattice& lattice,
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 
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)216 void SkOverdrawCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
217     drawable->draw(this, matrix);
218 }
219 
onDrawPicture(const SkPicture *,const SkMatrix *,const SkPaint *)220 void SkOverdrawCanvas::onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) {
221     SkASSERT(false);
222 }
223 
onDrawAnnotation(const SkRect &,const char[],SkData *)224 void SkOverdrawCanvas::onDrawAnnotation(const SkRect&, const char[], SkData*) {}
225 
onDrawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)226 void SkOverdrawCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
227     SkRect bounds;
228     SkDrawShadowMetrics::GetLocalBounds(path, rec, this->getTotalMatrix(), &bounds);
229     fList[0]->onDrawRect(bounds, fPaint);
230 }
231 
onDrawEdgeAAQuad(const SkRect & rect,const SkPoint clip[4],QuadAAFlags aa,const SkColor4f & color,SkBlendMode mode)232 void SkOverdrawCanvas::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
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 
onDrawEdgeAAImageSet2(const ImageSetEntry set[],int count,const SkPoint dstClips[],const SkMatrix preViewMatrices[],const SkSamplingOptions & sampling,const SkPaint * paint,SrcRectConstraint constraint)241 void SkOverdrawCanvas::onDrawEdgeAAImageSet2(const ImageSetEntry set[], int count,
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 
overdrawPaint(const SkPaint & paint)265 inline SkPaint SkOverdrawCanvas::overdrawPaint(const SkPaint& paint) {
266     SkPaint newPaint = fPaint;
267     newPaint.setStyle(paint.getStyle());
268     newPaint.setStrokeWidth(paint.getStrokeWidth());
269     return newPaint;
270 }
271