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 "SkColorFilter.h"
9 #include "SkDrawable.h"
10 #include "SkFindAndPlaceGlyph.h"
11 #include "SkImagePriv.h"
12 #include "SkLatticeIter.h"
13 #include "SkOverdrawCanvas.h"
14 #include "SkPatchUtils.h"
15 #include "SkPath.h"
16 #include "SkRRect.h"
17 #include "SkRSXform.h"
18 #include "SkTextBlob.h"
19 #include "SkTextBlobRunIterator.h"
20
21 namespace {
22 class ProcessOneGlyphBounds {
23 public:
ProcessOneGlyphBounds(SkOverdrawCanvas * canvas)24 ProcessOneGlyphBounds(SkOverdrawCanvas* canvas)
25 : fCanvas(canvas)
26 {}
27
operator ()(const SkGlyph & glyph,SkPoint position,SkPoint rounding)28 void operator()(const SkGlyph& glyph, SkPoint position, SkPoint rounding) {
29 int left = SkScalarFloorToInt(position.fX) + glyph.fLeft;
30 int top = SkScalarFloorToInt(position.fY) + glyph.fTop;
31 int right = left + glyph.fWidth;
32 int bottom = top + glyph.fHeight;
33 fCanvas->onDrawRect(SkRect::MakeLTRB(left, top, right, bottom), SkPaint());
34 }
35
36 private:
37 SkOverdrawCanvas* fCanvas;
38 };
39 };
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,
53 };
54
55 fPaint.setAntiAlias(false);
56 fPaint.setBlendMode(SkBlendMode::kPlus);
57 fPaint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(kIncrementAlpha));
58 }
59
onDrawText(const void * text,size_t byteLength,SkScalar x,SkScalar y,const SkPaint & paint)60 void SkOverdrawCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
61 const SkPaint& paint) {
62 ProcessOneGlyphBounds processBounds(this);
63 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
64 this->getProps(&props);
65 SkAutoGlyphCache cache(paint, &props, 0, &this->getTotalMatrix());
66 SkFindAndPlaceGlyph::ProcessText(paint.getTextEncoding(), (const char*) text, byteLength,
67 SkPoint::Make(x, y), SkMatrix(), paint.getTextAlign(),
68 cache.get(), processBounds);
69 }
70
drawPosTextCommon(const void * text,size_t byteLength,const SkScalar pos[],int scalarsPerPos,const SkPoint & offset,const SkPaint & paint)71 void SkOverdrawCanvas::drawPosTextCommon(const void* text, size_t byteLength, const SkScalar pos[],
72 int scalarsPerPos, const SkPoint& offset,
73 const SkPaint& paint) {
74 ProcessOneGlyphBounds processBounds(this);
75 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
76 this->getProps(&props);
77 SkAutoGlyphCache cache(paint, &props, 0, &this->getTotalMatrix());
78 SkFindAndPlaceGlyph::ProcessPosText(paint.getTextEncoding(), (const char*) text, byteLength,
79 SkPoint::Make(0, 0), SkMatrix(), (const SkScalar*) pos, 2,
80 paint.getTextAlign(), cache.get(), processBounds);
81 }
82
onDrawPosText(const void * text,size_t byteLength,const SkPoint pos[],const SkPaint & paint)83 void SkOverdrawCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
84 const SkPaint& paint) {
85 this->drawPosTextCommon(text, byteLength, (SkScalar*) pos, 2, SkPoint::Make(0, 0), paint);
86 }
87
onDrawPosTextH(const void * text,size_t byteLength,const SkScalar xs[],SkScalar y,const SkPaint & paint)88 void SkOverdrawCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xs[],
89 SkScalar y, const SkPaint& paint) {
90 this->drawPosTextCommon(text, byteLength, (SkScalar*) xs, 1, SkPoint::Make(0, y), paint);
91 }
92
onDrawTextOnPath(const void * text,size_t byteLength,const SkPath & path,const SkMatrix * matrix,const SkPaint & paint)93 void SkOverdrawCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
94 const SkMatrix* matrix, const SkPaint& paint) {
95 SkASSERT(false);
96 return;
97 }
98
99 typedef int (*CountTextProc)(const char* text);
count_utf16(const char * text)100 static int count_utf16(const char* text) {
101 const uint16_t* prev = (uint16_t*)text;
102 (void)SkUTF16_NextUnichar(&prev);
103 return SkToInt((const char*)prev - text);
104 }
return_4(const char * text)105 static int return_4(const char* text) { return 4; }
return_2(const char * text)106 static int return_2(const char* text) { return 2; }
107
onDrawTextRSXform(const void * text,size_t byteLength,const SkRSXform xform[],const SkRect *,const SkPaint & paint)108 void SkOverdrawCanvas::onDrawTextRSXform(const void* text, size_t byteLength,
109 const SkRSXform xform[], const SkRect*,
110 const SkPaint& paint) {
111 CountTextProc proc = nullptr;
112 switch (paint.getTextEncoding()) {
113 case SkPaint::kUTF8_TextEncoding:
114 proc = SkUTF8_CountUTF8Bytes;
115 break;
116 case SkPaint::kUTF16_TextEncoding:
117 proc = count_utf16;
118 break;
119 case SkPaint::kUTF32_TextEncoding:
120 proc = return_4;
121 break;
122 case SkPaint::kGlyphID_TextEncoding:
123 proc = return_2;
124 break;
125 }
126 SkASSERT(proc);
127
128 SkMatrix matrix;
129 const void* stopText = (const char*)text + byteLength;
130 while ((const char*)text < (const char*)stopText) {
131 matrix.setRSXform(*xform++);
132 matrix.setConcat(this->getTotalMatrix(), matrix);
133 int subLen = proc((const char*)text);
134
135 this->save();
136 this->concat(matrix);
137 this->drawText(text, subLen, 0, 0, paint);
138 this->restore();
139
140 text = (const char*)text + subLen;
141 }
142 }
143
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)144 void SkOverdrawCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
145 const SkPaint& paint) {
146 SkPaint runPaint = paint;
147 SkTextBlobRunIterator it(blob);
148 for (;!it.done(); it.next()) {
149 size_t textLen = it.glyphCount() * sizeof(uint16_t);
150 const SkPoint& offset = it.offset();
151 it.applyFontToPaint(&runPaint);
152 switch (it.positioning()) {
153 case SkTextBlob::kDefault_Positioning:
154 this->onDrawText(it.glyphs(), textLen, x + offset.x(), y + offset.y(), runPaint);
155 break;
156 case SkTextBlob::kHorizontal_Positioning:
157 this->drawPosTextCommon(it.glyphs(), textLen, it.pos(), 1,
158 SkPoint::Make(x, y + offset.y()), runPaint);
159 break;
160 case SkTextBlob::kFull_Positioning:
161 this->drawPosTextCommon(it.glyphs(), textLen, it.pos(), 2, SkPoint::Make(x, y),
162 runPaint);
163 break;
164 default:
165 SkASSERT(false);
166 break;
167 }
168 }
169 }
170
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode blendMode,const SkPaint &)171 void SkOverdrawCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
172 const SkPoint texCoords[4], SkBlendMode blendMode,
173 const SkPaint&) {
174 fList[0]->onDrawPatch(cubics, colors, texCoords, blendMode, fPaint);
175 }
176
onDrawPaint(const SkPaint & paint)177 void SkOverdrawCanvas::onDrawPaint(const SkPaint& paint) {
178 if (0 == paint.getColor() && !paint.getColorFilter() && !paint.getShader()) {
179 // This is a clear, ignore it.
180 } else {
181 fList[0]->onDrawPaint(this->overdrawPaint(paint));
182 }
183 }
184
onDrawRect(const SkRect & rect,const SkPaint & paint)185 void SkOverdrawCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
186 fList[0]->onDrawRect(rect, this->overdrawPaint(paint));
187 }
188
onDrawRegion(const SkRegion & region,const SkPaint & paint)189 void SkOverdrawCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
190 fList[0]->onDrawRegion(region, this->overdrawPaint(paint));
191 }
192
onDrawOval(const SkRect & oval,const SkPaint & paint)193 void SkOverdrawCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
194 fList[0]->onDrawOval(oval, this->overdrawPaint(paint));
195 }
196
onDrawArc(const SkRect & arc,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)197 void SkOverdrawCanvas::onDrawArc(const SkRect& arc, SkScalar startAngle, SkScalar sweepAngle,
198 bool useCenter, const SkPaint& paint) {
199 fList[0]->onDrawArc(arc, startAngle, sweepAngle, useCenter, this->overdrawPaint(paint));
200 }
201
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)202 void SkOverdrawCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
203 const SkPaint& paint) {
204 fList[0]->onDrawDRRect(outer, inner, this->overdrawPaint(paint));
205 }
206
onDrawRRect(const SkRRect & rect,const SkPaint & paint)207 void SkOverdrawCanvas::onDrawRRect(const SkRRect& rect, const SkPaint& paint) {
208 fList[0]->onDrawRRect(rect, this->overdrawPaint(paint));
209 }
210
onDrawPoints(PointMode mode,size_t count,const SkPoint points[],const SkPaint & paint)211 void SkOverdrawCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint points[],
212 const SkPaint& paint) {
213 fList[0]->onDrawPoints(mode, count, points, this->overdrawPaint(paint));
214 }
215
onDrawVerticesObject(const SkVertices * vertices,SkBlendMode blendMode,const SkPaint & paint)216 void SkOverdrawCanvas::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode blendMode,
217 const SkPaint& paint) {
218 fList[0]->onDrawVerticesObject(vertices, blendMode, this->overdrawPaint(paint));
219 }
220
onDrawAtlas(const SkImage * image,const SkRSXform xform[],const SkRect texs[],const SkColor colors[],int count,SkBlendMode mode,const SkRect * cull,const SkPaint * paint)221 void SkOverdrawCanvas::onDrawAtlas(const SkImage* image, const SkRSXform xform[],
222 const SkRect texs[], const SkColor colors[], int count,
223 SkBlendMode mode, const SkRect* cull, const SkPaint* paint) {
224 SkPaint* paintPtr = &fPaint;
225 SkPaint storage;
226 if (paint) {
227 storage = this->overdrawPaint(*paint);
228 paintPtr = &storage;
229 }
230
231 fList[0]->onDrawAtlas(image, xform, texs, colors, count, mode, cull, paintPtr);
232 }
233
onDrawPath(const SkPath & path,const SkPaint & paint)234 void SkOverdrawCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
235 fList[0]->onDrawPath(path, fPaint);
236 }
237
onDrawImage(const SkImage * image,SkScalar x,SkScalar y,const SkPaint *)238 void SkOverdrawCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint*) {
239 fList[0]->onDrawRect(SkRect::MakeXYWH(x, y, image->width(), image->height()), fPaint);
240 }
241
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint *,SrcRectConstraint)242 void SkOverdrawCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
243 const SkPaint*, SrcRectConstraint) {
244 fList[0]->onDrawRect(dst, fPaint);
245 }
246
onDrawImageNine(const SkImage *,const SkIRect &,const SkRect & dst,const SkPaint *)247 void SkOverdrawCanvas::onDrawImageNine(const SkImage*, const SkIRect&, const SkRect& dst,
248 const SkPaint*) {
249 fList[0]->onDrawRect(dst, fPaint);
250 }
251
onDrawImageLattice(const SkImage * image,const Lattice & lattice,const SkRect & dst,const SkPaint *)252 void SkOverdrawCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice,
253 const SkRect& dst, const SkPaint*) {
254 SkIRect bounds;
255 Lattice latticePlusBounds = lattice;
256 if (!latticePlusBounds.fBounds) {
257 bounds = SkIRect::MakeWH(image->width(), image->height());
258 latticePlusBounds.fBounds = &bounds;
259 }
260
261 if (SkLatticeIter::Valid(image->width(), image->height(), latticePlusBounds)) {
262 SkLatticeIter iter(latticePlusBounds, dst);
263
264 SkRect dummy, iterDst;
265 while (iter.next(&dummy, &iterDst)) {
266 fList[0]->onDrawRect(iterDst, fPaint);
267 }
268 } else {
269 fList[0]->onDrawRect(dst, fPaint);
270 }
271 }
272
onDrawBitmap(const SkBitmap & bitmap,SkScalar x,SkScalar y,const SkPaint *)273 void SkOverdrawCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
274 const SkPaint*) {
275 fList[0]->onDrawRect(SkRect::MakeXYWH(x, y, bitmap.width(), bitmap.height()), fPaint);
276 }
277
onDrawBitmapRect(const SkBitmap &,const SkRect *,const SkRect & dst,const SkPaint *,SrcRectConstraint)278 void SkOverdrawCanvas::onDrawBitmapRect(const SkBitmap&, const SkRect*, const SkRect& dst,
279 const SkPaint*, SrcRectConstraint) {
280 fList[0]->onDrawRect(dst, fPaint);
281 }
282
onDrawBitmapNine(const SkBitmap &,const SkIRect &,const SkRect & dst,const SkPaint *)283 void SkOverdrawCanvas::onDrawBitmapNine(const SkBitmap&, const SkIRect&, const SkRect& dst,
284 const SkPaint*) {
285 fList[0]->onDrawRect(dst, fPaint);
286 }
287
onDrawBitmapLattice(const SkBitmap & bitmap,const Lattice & lattice,const SkRect & dst,const SkPaint * paint)288 void SkOverdrawCanvas::onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice,
289 const SkRect& dst, const SkPaint* paint) {
290 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, kNever_SkCopyPixelsMode);
291 this->onDrawImageLattice(image.get(), lattice, dst, paint);
292 }
293
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)294 void SkOverdrawCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
295 drawable->draw(this, matrix);
296 }
297
onDrawPicture(const SkPicture *,const SkMatrix *,const SkPaint *)298 void SkOverdrawCanvas::onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) {
299 SkASSERT(false);
300 return;
301 }
302
overdrawPaint(const SkPaint & paint)303 inline SkPaint SkOverdrawCanvas::overdrawPaint(const SkPaint& paint) {
304 SkPaint newPaint = fPaint;
305 newPaint.setStyle(paint.getStyle());
306 newPaint.setStrokeWidth(paint.getStrokeWidth());
307 return newPaint;
308 }
309