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
onDrawRect(const SkRect & rect,const SkPaint & paint)116 void SkOverdrawCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
117 fList[0]->onDrawRect(rect, this->overdrawPaint(paint));
118 }
119
onDrawEdgeAARect(const SkRect & rect,SkCanvas::QuadAAFlags aa,SkColor color,SkBlendMode mode)120 void SkOverdrawCanvas::onDrawEdgeAARect(const SkRect& rect, SkCanvas::QuadAAFlags aa, SkColor color,
121 SkBlendMode mode) {
122 fList[0]->onDrawRect(rect, fPaint);
123 }
124
onDrawRegion(const SkRegion & region,const SkPaint & paint)125 void SkOverdrawCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
126 fList[0]->onDrawRegion(region, this->overdrawPaint(paint));
127 }
128
onDrawOval(const SkRect & oval,const SkPaint & paint)129 void SkOverdrawCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
130 fList[0]->onDrawOval(oval, this->overdrawPaint(paint));
131 }
132
onDrawArc(const SkRect & arc,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)133 void SkOverdrawCanvas::onDrawArc(const SkRect& arc, SkScalar startAngle, SkScalar sweepAngle,
134 bool useCenter, const SkPaint& paint) {
135 fList[0]->onDrawArc(arc, startAngle, sweepAngle, useCenter, this->overdrawPaint(paint));
136 }
137
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)138 void SkOverdrawCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
139 const SkPaint& paint) {
140 fList[0]->onDrawDRRect(outer, inner, this->overdrawPaint(paint));
141 }
142
onDrawRRect(const SkRRect & rect,const SkPaint & paint)143 void SkOverdrawCanvas::onDrawRRect(const SkRRect& rect, const SkPaint& paint) {
144 fList[0]->onDrawRRect(rect, this->overdrawPaint(paint));
145 }
146
onDrawPoints(PointMode mode,size_t count,const SkPoint points[],const SkPaint & paint)147 void SkOverdrawCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint points[],
148 const SkPaint& paint) {
149 fList[0]->onDrawPoints(mode, count, points, this->overdrawPaint(paint));
150 }
151
onDrawVerticesObject(const SkVertices * vertices,const SkVertices::Bone bones[],int boneCount,SkBlendMode blendMode,const SkPaint & paint)152 void SkOverdrawCanvas::onDrawVerticesObject(const SkVertices* vertices,
153 const SkVertices::Bone bones[], int boneCount,
154 SkBlendMode blendMode, const SkPaint& paint) {
155 fList[0]->onDrawVerticesObject(vertices,
156 bones,
157 boneCount,
158 blendMode,
159 this->overdrawPaint(paint));
160 }
161
onDrawAtlas(const SkImage * image,const SkRSXform xform[],const SkRect texs[],const SkColor colors[],int count,SkBlendMode mode,const SkRect * cull,const SkPaint * paint)162 void SkOverdrawCanvas::onDrawAtlas(const SkImage* image, const SkRSXform xform[],
163 const SkRect texs[], const SkColor colors[], int count,
164 SkBlendMode mode, const SkRect* cull, const SkPaint* paint) {
165 SkPaint* paintPtr = &fPaint;
166 SkPaint storage;
167 if (paint) {
168 storage = this->overdrawPaint(*paint);
169 paintPtr = &storage;
170 }
171
172 fList[0]->onDrawAtlas(image, xform, texs, colors, count, mode, cull, paintPtr);
173 }
174
onDrawPath(const SkPath & path,const SkPaint & paint)175 void SkOverdrawCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
176 fList[0]->onDrawPath(path, fPaint);
177 }
178
onDrawImage(const SkImage * image,SkScalar x,SkScalar y,const SkPaint *)179 void SkOverdrawCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint*) {
180 fList[0]->onDrawRect(SkRect::MakeXYWH(x, y, image->width(), image->height()), fPaint);
181 }
182
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint *,SrcRectConstraint)183 void SkOverdrawCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
184 const SkPaint*, SrcRectConstraint) {
185 fList[0]->onDrawRect(dst, fPaint);
186 }
187
onDrawImageNine(const SkImage *,const SkIRect &,const SkRect & dst,const SkPaint *)188 void SkOverdrawCanvas::onDrawImageNine(const SkImage*, const SkIRect&, const SkRect& dst,
189 const SkPaint*) {
190 fList[0]->onDrawRect(dst, fPaint);
191 }
192
onDrawImageLattice(const SkImage * image,const Lattice & lattice,const SkRect & dst,const SkPaint *)193 void SkOverdrawCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice,
194 const SkRect& dst, const SkPaint*) {
195 SkIRect bounds;
196 Lattice latticePlusBounds = lattice;
197 if (!latticePlusBounds.fBounds) {
198 bounds = SkIRect::MakeWH(image->width(), image->height());
199 latticePlusBounds.fBounds = &bounds;
200 }
201
202 if (SkLatticeIter::Valid(image->width(), image->height(), latticePlusBounds)) {
203 SkLatticeIter iter(latticePlusBounds, dst);
204
205 SkRect dummy, iterDst;
206 while (iter.next(&dummy, &iterDst)) {
207 fList[0]->onDrawRect(iterDst, fPaint);
208 }
209 } else {
210 fList[0]->onDrawRect(dst, fPaint);
211 }
212 }
213
onDrawImageSet(const ImageSetEntry set[],int count,SkFilterQuality,SkBlendMode)214 void SkOverdrawCanvas::onDrawImageSet(const ImageSetEntry set[], int count, SkFilterQuality,
215 SkBlendMode) {
216 for (int i = 0; i < count; ++i) {
217 fList[0]->onDrawRect(set[i].fDstRect, fPaint);
218 }
219 }
220
onDrawBitmap(const SkBitmap & bitmap,SkScalar x,SkScalar y,const SkPaint *)221 void SkOverdrawCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
222 const SkPaint*) {
223 fList[0]->onDrawRect(SkRect::MakeXYWH(x, y, bitmap.width(), bitmap.height()), fPaint);
224 }
225
onDrawBitmapRect(const SkBitmap &,const SkRect *,const SkRect & dst,const SkPaint *,SrcRectConstraint)226 void SkOverdrawCanvas::onDrawBitmapRect(const SkBitmap&, const SkRect*, const SkRect& dst,
227 const SkPaint*, SrcRectConstraint) {
228 fList[0]->onDrawRect(dst, fPaint);
229 }
230
onDrawBitmapNine(const SkBitmap &,const SkIRect &,const SkRect & dst,const SkPaint *)231 void SkOverdrawCanvas::onDrawBitmapNine(const SkBitmap&, const SkIRect&, const SkRect& dst,
232 const SkPaint*) {
233 fList[0]->onDrawRect(dst, fPaint);
234 }
235
onDrawBitmapLattice(const SkBitmap & bitmap,const Lattice & lattice,const SkRect & dst,const SkPaint * paint)236 void SkOverdrawCanvas::onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice,
237 const SkRect& dst, const SkPaint* paint) {
238 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, kNever_SkCopyPixelsMode);
239 this->onDrawImageLattice(image.get(), lattice, dst, paint);
240 }
241
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)242 void SkOverdrawCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
243 drawable->draw(this, matrix);
244 }
245
onDrawPicture(const SkPicture *,const SkMatrix *,const SkPaint *)246 void SkOverdrawCanvas::onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) {
247 SkASSERT(false);
248 return;
249 }
250
onDrawAnnotation(const SkRect &,const char[],SkData *)251 void SkOverdrawCanvas::onDrawAnnotation(const SkRect&, const char[], SkData*) {}
252
onDrawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)253 void SkOverdrawCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
254 SkRect bounds;
255 SkDrawShadowMetrics::GetLocalBounds(path, rec, this->getTotalMatrix(), &bounds);
256 fList[0]->onDrawRect(bounds, fPaint);
257 }
258
overdrawPaint(const SkPaint & paint)259 inline SkPaint SkOverdrawCanvas::overdrawPaint(const SkPaint& paint) {
260 SkPaint newPaint = fPaint;
261 newPaint.setStyle(paint.getStyle());
262 newPaint.setStrokeWidth(paint.getStrokeWidth());
263 return newPaint;
264 }
265