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 "SkOverdrawCanvas.h"
9
10 #include "SkColorFilter.h"
11 #include "SkDrawShadowInfo.h"
12 #include "SkDrawable.h"
13 #include "SkFindAndPlaceGlyph.h"
14 #include "SkImagePriv.h"
15 #include "SkLatticeIter.h"
16 #include "SkPatchUtils.h"
17 #include "SkPath.h"
18 #include "SkRRect.h"
19 #include "SkRSXform.h"
20 #include "SkStrikeCache.h"
21 #include "SkTextBlob.h"
22 #include "SkTextBlobPriv.h"
23 #include "SkTo.h"
24
25 namespace {
26 class ProcessOneGlyphBounds {
27 public:
ProcessOneGlyphBounds(SkOverdrawCanvas * canvas)28 ProcessOneGlyphBounds(SkOverdrawCanvas* canvas)
29 : fCanvas(canvas)
30 {}
31
operator ()(const SkGlyph & glyph,SkPoint position,SkPoint rounding)32 void operator()(const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
33 int left = SkScalarFloorToInt(position.fX) + glyph.fLeft;
34 int top = SkScalarFloorToInt(position.fY) + glyph.fTop;
35 int right = left + glyph.fWidth;
36 int bottom = top + glyph.fHeight;
37 fCanvas->onDrawRect(SkRect::MakeLTRB(left, top, right, bottom), SkPaint());
38 }
39
40 private:
41 SkOverdrawCanvas* fCanvas;
42 };
43 };
44
SkOverdrawCanvas(SkCanvas * canvas)45 SkOverdrawCanvas::SkOverdrawCanvas(SkCanvas* canvas)
46 : INHERITED(canvas->onImageInfo().width(), canvas->onImageInfo().height())
47 {
48 // Non-drawing calls that SkOverdrawCanvas does not override (translate, save, etc.)
49 // will pass through to the input canvas.
50 this->addCanvas(canvas);
51
52 static constexpr float kIncrementAlpha[] = {
53 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
54 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
55 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
56 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
57 };
58
59 fPaint.setAntiAlias(false);
60 fPaint.setBlendMode(SkBlendMode::kPlus);
61 fPaint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(kIncrementAlpha));
62 }
63
drawPosTextCommon(const SkGlyphID glyphs[],int count,const SkScalar pos[],int scalarsPerPos,const SkPoint & offset,const SkFont & font,const SkPaint & paint)64 void SkOverdrawCanvas::drawPosTextCommon(const SkGlyphID glyphs[], int count, const SkScalar pos[],
65 int scalarsPerPos, const SkPoint& offset,
66 const SkFont& font, const SkPaint& paint) {
67 ProcessOneGlyphBounds processBounds(this);
68 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
69 this->getProps(&props);
70 auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(
71 font, paint, props,
72 SkScalerContextFlags::kNone, this->getTotalMatrix());
73 SkFindAndPlaceGlyph::ProcessPosText(glyphs, count,
74 SkPoint::Make(0, 0), SkMatrix(), (const SkScalar*) pos, 2,
75 cache.get(), processBounds);
76 }
77
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)78 void SkOverdrawCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
79 const SkPaint& paint) {
80 SkTextBlobRunIterator it(blob);
81 for (;!it.done(); it.next()) {
82 const SkPoint& offset = it.offset();
83 switch (it.positioning()) {
84 case SkTextBlobRunIterator::kDefault_Positioning:
85 SK_ABORT("This canvas does not support draw text.");
86 break;
87 case SkTextBlobRunIterator::kHorizontal_Positioning:
88 this->drawPosTextCommon(it.glyphs(), it.glyphCount(), it.pos(), 1,
89 SkPoint::Make(x, y + offset.y()), it.font(), paint);
90 break;
91 case SkTextBlobRunIterator::kFull_Positioning:
92 this->drawPosTextCommon(it.glyphs(), it.glyphCount(), it.pos(), 2, {x, y},
93 it.font(), paint);
94 break;
95 case SkTextBlobRunIterator::kRSXform_Positioning:
96 // unimplemented ...
97 break;
98 }
99 }
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
onDrawEdgeAARect(const SkRect & rect,SkCanvas::QuadAAFlags aa,SkColor color,SkBlendMode mode)124 void SkOverdrawCanvas::onDrawEdgeAARect(const SkRect& rect, SkCanvas::QuadAAFlags aa, SkColor color,
125 SkBlendMode mode) {
126 fList[0]->onDrawRect(rect, fPaint);
127 }
128
onDrawRegion(const SkRegion & region,const SkPaint & paint)129 void SkOverdrawCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
130 fList[0]->onDrawRegion(region, this->overdrawPaint(paint));
131 }
132
onDrawOval(const SkRect & oval,const SkPaint & paint)133 void SkOverdrawCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
134 fList[0]->onDrawOval(oval, this->overdrawPaint(paint));
135 }
136
onDrawArc(const SkRect & arc,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)137 void SkOverdrawCanvas::onDrawArc(const SkRect& arc, SkScalar startAngle, SkScalar sweepAngle,
138 bool useCenter, const SkPaint& paint) {
139 fList[0]->onDrawArc(arc, startAngle, sweepAngle, useCenter, this->overdrawPaint(paint));
140 }
141
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)142 void SkOverdrawCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
143 const SkPaint& paint) {
144 fList[0]->onDrawDRRect(outer, inner, this->overdrawPaint(paint));
145 }
146
onDrawRRect(const SkRRect & rect,const SkPaint & paint)147 void SkOverdrawCanvas::onDrawRRect(const SkRRect& rect, const SkPaint& paint) {
148 fList[0]->onDrawRRect(rect, this->overdrawPaint(paint));
149 }
150
onDrawPoints(PointMode mode,size_t count,const SkPoint points[],const SkPaint & paint)151 void SkOverdrawCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint points[],
152 const SkPaint& paint) {
153 fList[0]->onDrawPoints(mode, count, points, this->overdrawPaint(paint));
154 }
155
onDrawVerticesObject(const SkVertices * vertices,const SkVertices::Bone bones[],int boneCount,SkBlendMode blendMode,const SkPaint & paint)156 void SkOverdrawCanvas::onDrawVerticesObject(const SkVertices* vertices,
157 const SkVertices::Bone bones[], int boneCount,
158 SkBlendMode blendMode, const SkPaint& paint) {
159 fList[0]->onDrawVerticesObject(vertices,
160 bones,
161 boneCount,
162 blendMode,
163 this->overdrawPaint(paint));
164 }
165
onDrawAtlas(const SkImage * image,const SkRSXform xform[],const SkRect texs[],const SkColor colors[],int count,SkBlendMode mode,const SkRect * cull,const SkPaint * paint)166 void SkOverdrawCanvas::onDrawAtlas(const SkImage* image, const SkRSXform xform[],
167 const SkRect texs[], const SkColor colors[], int count,
168 SkBlendMode mode, const SkRect* cull, const SkPaint* paint) {
169 SkPaint* paintPtr = &fPaint;
170 SkPaint storage;
171 if (paint) {
172 storage = this->overdrawPaint(*paint);
173 paintPtr = &storage;
174 }
175
176 fList[0]->onDrawAtlas(image, xform, texs, colors, count, mode, cull, paintPtr);
177 }
178
onDrawPath(const SkPath & path,const SkPaint & paint)179 void SkOverdrawCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
180 fList[0]->onDrawPath(path, fPaint);
181 }
182
onDrawImage(const SkImage * image,SkScalar x,SkScalar y,const SkPaint *)183 void SkOverdrawCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint*) {
184 fList[0]->onDrawRect(SkRect::MakeXYWH(x, y, image->width(), image->height()), fPaint);
185 }
186
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint *,SrcRectConstraint)187 void SkOverdrawCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
188 const SkPaint*, SrcRectConstraint) {
189 fList[0]->onDrawRect(dst, fPaint);
190 }
191
onDrawImageNine(const SkImage *,const SkIRect &,const SkRect & dst,const SkPaint *)192 void SkOverdrawCanvas::onDrawImageNine(const SkImage*, const SkIRect&, const SkRect& dst,
193 const SkPaint*) {
194 fList[0]->onDrawRect(dst, fPaint);
195 }
196
onDrawImageLattice(const SkImage * image,const Lattice & lattice,const SkRect & dst,const SkPaint *)197 void SkOverdrawCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice,
198 const SkRect& dst, const SkPaint*) {
199 SkIRect bounds;
200 Lattice latticePlusBounds = lattice;
201 if (!latticePlusBounds.fBounds) {
202 bounds = SkIRect::MakeWH(image->width(), image->height());
203 latticePlusBounds.fBounds = &bounds;
204 }
205
206 if (SkLatticeIter::Valid(image->width(), image->height(), latticePlusBounds)) {
207 SkLatticeIter iter(latticePlusBounds, dst);
208
209 SkRect dummy, iterDst;
210 while (iter.next(&dummy, &iterDst)) {
211 fList[0]->onDrawRect(iterDst, fPaint);
212 }
213 } else {
214 fList[0]->onDrawRect(dst, fPaint);
215 }
216 }
217
onDrawImageSet(const ImageSetEntry set[],int count,SkFilterQuality,SkBlendMode)218 void SkOverdrawCanvas::onDrawImageSet(const ImageSetEntry set[], int count, SkFilterQuality,
219 SkBlendMode) {
220 for (int i = 0; i < count; ++i) {
221 fList[0]->onDrawRect(set[i].fDstRect, fPaint);
222 }
223 }
224
onDrawBitmap(const SkBitmap & bitmap,SkScalar x,SkScalar y,const SkPaint *)225 void SkOverdrawCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
226 const SkPaint*) {
227 fList[0]->onDrawRect(SkRect::MakeXYWH(x, y, bitmap.width(), bitmap.height()), fPaint);
228 }
229
onDrawBitmapRect(const SkBitmap &,const SkRect *,const SkRect & dst,const SkPaint *,SrcRectConstraint)230 void SkOverdrawCanvas::onDrawBitmapRect(const SkBitmap&, const SkRect*, const SkRect& dst,
231 const SkPaint*, SrcRectConstraint) {
232 fList[0]->onDrawRect(dst, fPaint);
233 }
234
onDrawBitmapNine(const SkBitmap &,const SkIRect &,const SkRect & dst,const SkPaint *)235 void SkOverdrawCanvas::onDrawBitmapNine(const SkBitmap&, const SkIRect&, const SkRect& dst,
236 const SkPaint*) {
237 fList[0]->onDrawRect(dst, fPaint);
238 }
239
onDrawBitmapLattice(const SkBitmap & bitmap,const Lattice & lattice,const SkRect & dst,const SkPaint * paint)240 void SkOverdrawCanvas::onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice,
241 const SkRect& dst, const SkPaint* paint) {
242 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, kNever_SkCopyPixelsMode);
243 this->onDrawImageLattice(image.get(), lattice, dst, paint);
244 }
245
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)246 void SkOverdrawCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
247 drawable->draw(this, matrix);
248 }
249
onDrawPicture(const SkPicture *,const SkMatrix *,const SkPaint *)250 void SkOverdrawCanvas::onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) {
251 SkASSERT(false);
252 return;
253 }
254
onDrawAnnotation(const SkRect &,const char[],SkData *)255 void SkOverdrawCanvas::onDrawAnnotation(const SkRect&, const char[], SkData*) {}
256
onDrawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)257 void SkOverdrawCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
258 SkRect bounds;
259 SkDrawShadowMetrics::GetLocalBounds(path, rec, this->getTotalMatrix(), &bounds);
260 fList[0]->onDrawRect(bounds, fPaint);
261 }
262
overdrawPaint(const SkPaint & paint)263 inline SkPaint SkOverdrawCanvas::overdrawPaint(const SkPaint& paint) {
264 SkPaint newPaint = fPaint;
265 newPaint.setStyle(paint.getStyle());
266 newPaint.setStrokeWidth(paint.getStrokeWidth());
267 return newPaint;
268 }
269