SkOverdrawCanvas.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include "include/core/SkOverdrawCanvas.h"
  8. #include "include/core/SkColorFilter.h"
  9. #include "include/core/SkDrawable.h"
  10. #include "include/core/SkPath.h"
  11. #include "include/core/SkRRect.h"
  12. #include "include/core/SkRSXform.h"
  13. #include "include/core/SkTextBlob.h"
  14. #include "include/private/SkTo.h"
  15. #include "src/core/SkDevice.h"
  16. #include "src/core/SkDrawShadowInfo.h"
  17. #include "src/core/SkGlyphRunPainter.h"
  18. #include "src/core/SkImagePriv.h"
  19. #include "src/core/SkLatticeIter.h"
  20. #include "src/core/SkStrikeCache.h"
  21. #include "src/core/SkTextBlobPriv.h"
  22. #include "src/utils/SkPatchUtils.h"
  23. SkOverdrawCanvas::SkOverdrawCanvas(SkCanvas* canvas)
  24. : INHERITED(canvas->onImageInfo().width(), canvas->onImageInfo().height())
  25. {
  26. // Non-drawing calls that SkOverdrawCanvas does not override (translate, save, etc.)
  27. // will pass through to the input canvas.
  28. this->addCanvas(canvas);
  29. static constexpr float kIncrementAlpha[] = {
  30. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  31. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  32. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  33. 0.0f, 0.0f, 0.0f, 0.0f, 1.0f/255,
  34. };
  35. fPaint.setAntiAlias(false);
  36. fPaint.setBlendMode(SkBlendMode::kPlus);
  37. fPaint.setColorFilter(SkColorFilters::Matrix(kIncrementAlpha));
  38. }
  39. namespace {
  40. class TextDevice : public SkNoPixelsDevice, public SkGlyphRunListPainter::BitmapDevicePainter {
  41. public:
  42. TextDevice(SkCanvas* overdrawCanvas, const SkSurfaceProps& props)
  43. : SkNoPixelsDevice{SkIRect::MakeWH(32767, 32767), props},
  44. fOverdrawCanvas{overdrawCanvas},
  45. fPainter{props, kN32_SkColorType, nullptr, SkStrikeCache::GlobalStrikeCache()} {}
  46. void paintPaths(SkSpan<const SkPathPos> pathsAndPositions, SkScalar scale,
  47. const SkPaint& paint) const override {}
  48. void paintMasks(SkSpan<const SkMask> masks, const SkPaint& paint) const override {
  49. for (auto& mask : masks) {
  50. fOverdrawCanvas->drawRect(SkRect::Make(mask.fBounds), SkPaint());
  51. }
  52. }
  53. protected:
  54. void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override {
  55. fPainter.drawForBitmapDevice(glyphRunList, fOverdrawCanvas->getTotalMatrix(), this);
  56. }
  57. private:
  58. SkCanvas* const fOverdrawCanvas;
  59. SkGlyphRunListPainter fPainter;
  60. };
  61. } // namespace
  62. void SkOverdrawCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
  63. const SkPaint& paint) {
  64. SkGlyphRunBuilder b;
  65. SkSurfaceProps props{0, kUnknown_SkPixelGeometry};
  66. this->getProps(&props);
  67. TextDevice device{this, props};
  68. b.drawTextBlob(paint, *blob, {x, y}, &device);
  69. }
  70. void SkOverdrawCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
  71. const SkPoint texCoords[4], SkBlendMode blendMode,
  72. const SkPaint&) {
  73. fList[0]->onDrawPatch(cubics, colors, texCoords, blendMode, fPaint);
  74. }
  75. void SkOverdrawCanvas::onDrawPaint(const SkPaint& paint) {
  76. if (0 == paint.getColor() && !paint.getColorFilter() && !paint.getShader()) {
  77. // This is a clear, ignore it.
  78. } else {
  79. fList[0]->onDrawPaint(this->overdrawPaint(paint));
  80. }
  81. }
  82. void SkOverdrawCanvas::onDrawBehind(const SkPaint& paint) {
  83. fList[0]->onDrawBehind(this->overdrawPaint(paint));
  84. }
  85. void SkOverdrawCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
  86. fList[0]->onDrawRect(rect, this->overdrawPaint(paint));
  87. }
  88. void SkOverdrawCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
  89. fList[0]->onDrawRegion(region, this->overdrawPaint(paint));
  90. }
  91. void SkOverdrawCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
  92. fList[0]->onDrawOval(oval, this->overdrawPaint(paint));
  93. }
  94. void SkOverdrawCanvas::onDrawArc(const SkRect& arc, SkScalar startAngle, SkScalar sweepAngle,
  95. bool useCenter, const SkPaint& paint) {
  96. fList[0]->onDrawArc(arc, startAngle, sweepAngle, useCenter, this->overdrawPaint(paint));
  97. }
  98. void SkOverdrawCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
  99. const SkPaint& paint) {
  100. fList[0]->onDrawDRRect(outer, inner, this->overdrawPaint(paint));
  101. }
  102. void SkOverdrawCanvas::onDrawRRect(const SkRRect& rect, const SkPaint& paint) {
  103. fList[0]->onDrawRRect(rect, this->overdrawPaint(paint));
  104. }
  105. void SkOverdrawCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint points[],
  106. const SkPaint& paint) {
  107. fList[0]->onDrawPoints(mode, count, points, this->overdrawPaint(paint));
  108. }
  109. void SkOverdrawCanvas::onDrawVerticesObject(const SkVertices* vertices,
  110. const SkVertices::Bone bones[], int boneCount,
  111. SkBlendMode blendMode, const SkPaint& paint) {
  112. fList[0]->onDrawVerticesObject(vertices,
  113. bones,
  114. boneCount,
  115. blendMode,
  116. this->overdrawPaint(paint));
  117. }
  118. void SkOverdrawCanvas::onDrawAtlas(const SkImage* image, const SkRSXform xform[],
  119. const SkRect texs[], const SkColor colors[], int count,
  120. SkBlendMode mode, const SkRect* cull, const SkPaint* paint) {
  121. SkPaint* paintPtr = &fPaint;
  122. SkPaint storage;
  123. if (paint) {
  124. storage = this->overdrawPaint(*paint);
  125. paintPtr = &storage;
  126. }
  127. fList[0]->onDrawAtlas(image, xform, texs, colors, count, mode, cull, paintPtr);
  128. }
  129. void SkOverdrawCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
  130. fList[0]->onDrawPath(path, fPaint);
  131. }
  132. void SkOverdrawCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint*) {
  133. fList[0]->onDrawRect(SkRect::MakeXYWH(x, y, image->width(), image->height()), fPaint);
  134. }
  135. void SkOverdrawCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
  136. const SkPaint*, SrcRectConstraint) {
  137. fList[0]->onDrawRect(dst, fPaint);
  138. }
  139. void SkOverdrawCanvas::onDrawImageNine(const SkImage*, const SkIRect&, const SkRect& dst,
  140. const SkPaint*) {
  141. fList[0]->onDrawRect(dst, fPaint);
  142. }
  143. void SkOverdrawCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice,
  144. const SkRect& dst, const SkPaint*) {
  145. SkIRect bounds;
  146. Lattice latticePlusBounds = lattice;
  147. if (!latticePlusBounds.fBounds) {
  148. bounds = SkIRect::MakeWH(image->width(), image->height());
  149. latticePlusBounds.fBounds = &bounds;
  150. }
  151. if (SkLatticeIter::Valid(image->width(), image->height(), latticePlusBounds)) {
  152. SkLatticeIter iter(latticePlusBounds, dst);
  153. SkRect dummy, iterDst;
  154. while (iter.next(&dummy, &iterDst)) {
  155. fList[0]->onDrawRect(iterDst, fPaint);
  156. }
  157. } else {
  158. fList[0]->onDrawRect(dst, fPaint);
  159. }
  160. }
  161. void SkOverdrawCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
  162. const SkPaint*) {
  163. fList[0]->onDrawRect(SkRect::MakeXYWH(x, y, bitmap.width(), bitmap.height()), fPaint);
  164. }
  165. void SkOverdrawCanvas::onDrawBitmapRect(const SkBitmap&, const SkRect*, const SkRect& dst,
  166. const SkPaint*, SrcRectConstraint) {
  167. fList[0]->onDrawRect(dst, fPaint);
  168. }
  169. void SkOverdrawCanvas::onDrawBitmapNine(const SkBitmap&, const SkIRect&, const SkRect& dst,
  170. const SkPaint*) {
  171. fList[0]->onDrawRect(dst, fPaint);
  172. }
  173. void SkOverdrawCanvas::onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice,
  174. const SkRect& dst, const SkPaint* paint) {
  175. sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, kNever_SkCopyPixelsMode);
  176. this->onDrawImageLattice(image.get(), lattice, dst, paint);
  177. }
  178. void SkOverdrawCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
  179. drawable->draw(this, matrix);
  180. }
  181. void SkOverdrawCanvas::onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) {
  182. SkASSERT(false);
  183. return;
  184. }
  185. void SkOverdrawCanvas::onDrawAnnotation(const SkRect&, const char[], SkData*) {}
  186. void SkOverdrawCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
  187. SkRect bounds;
  188. SkDrawShadowMetrics::GetLocalBounds(path, rec, this->getTotalMatrix(), &bounds);
  189. fList[0]->onDrawRect(bounds, fPaint);
  190. }
  191. void SkOverdrawCanvas::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
  192. QuadAAFlags aa, SkColor color, SkBlendMode mode) {
  193. if (clip) {
  194. SkPath path;
  195. path.addPoly(clip, 4, true);
  196. fList[0]->onDrawPath(path, fPaint);
  197. } else {
  198. fList[0]->onDrawRect(rect, fPaint);
  199. }
  200. }
  201. void SkOverdrawCanvas::onDrawEdgeAAImageSet(const ImageSetEntry set[], int count,
  202. const SkPoint dstClips[],
  203. const SkMatrix preViewMatrices[], const SkPaint* paint,
  204. SrcRectConstraint constraint) {
  205. int clipIndex = 0;
  206. for (int i = 0; i < count; ++i) {
  207. if (set[i].fMatrixIndex >= 0) {
  208. fList[0]->save();
  209. fList[0]->concat(preViewMatrices[set[i].fMatrixIndex]);
  210. }
  211. if (set[i].fHasClip) {
  212. SkPath path;
  213. path.addPoly(dstClips + clipIndex, 4, true);
  214. clipIndex += 4;
  215. fList[0]->onDrawPath(path, fPaint);
  216. } else {
  217. fList[0]->onDrawRect(set[i].fDstRect, fPaint);
  218. }
  219. if (set[i].fMatrixIndex >= 0) {
  220. fList[0]->restore();
  221. }
  222. }
  223. }
  224. inline SkPaint SkOverdrawCanvas::overdrawPaint(const SkPaint& paint) {
  225. SkPaint newPaint = fPaint;
  226. newPaint.setStyle(paint.getStyle());
  227. newPaint.setStrokeWidth(paint.getStrokeWidth());
  228. return newPaint;
  229. }