SkPaintFilterCanvas.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright 2015 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/utils/SkPaintFilterCanvas.h"
  8. #include "include/core/SkPaint.h"
  9. #include "include/core/SkPixmap.h"
  10. #include "include/core/SkSurface.h"
  11. #include "src/core/SkTLazy.h"
  12. class SkPaintFilterCanvas::AutoPaintFilter {
  13. public:
  14. AutoPaintFilter(const SkPaintFilterCanvas* canvas, const SkPaint* paint)
  15. : fPaint(paint ? *paint : SkPaint()) {
  16. fShouldDraw = canvas->onFilter(fPaint);
  17. }
  18. AutoPaintFilter(const SkPaintFilterCanvas* canvas, const SkPaint& paint)
  19. : AutoPaintFilter(canvas, &paint) { }
  20. const SkPaint& paint() const { return fPaint; }
  21. bool shouldDraw() const { return fShouldDraw; }
  22. private:
  23. SkPaint fPaint;
  24. bool fShouldDraw;
  25. };
  26. SkPaintFilterCanvas::SkPaintFilterCanvas(SkCanvas *canvas)
  27. : SkCanvasVirtualEnforcer<SkNWayCanvas>(canvas->imageInfo().width(),
  28. canvas->imageInfo().height()) {
  29. // Transfer matrix & clip state before adding the target canvas.
  30. this->clipRect(SkRect::Make(canvas->getDeviceClipBounds()));
  31. this->setMatrix(canvas->getTotalMatrix());
  32. this->addCanvas(canvas);
  33. }
  34. void SkPaintFilterCanvas::onDrawPaint(const SkPaint& paint) {
  35. AutoPaintFilter apf(this, paint);
  36. if (apf.shouldDraw()) {
  37. this->SkNWayCanvas::onDrawPaint(apf.paint());
  38. }
  39. }
  40. void SkPaintFilterCanvas::onDrawBehind(const SkPaint& paint) {
  41. AutoPaintFilter apf(this, paint);
  42. if (apf.shouldDraw()) {
  43. this->SkNWayCanvas::onDrawBehind(apf.paint());
  44. }
  45. }
  46. void SkPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
  47. const SkPaint& paint) {
  48. AutoPaintFilter apf(this, paint);
  49. if (apf.shouldDraw()) {
  50. this->SkNWayCanvas::onDrawPoints(mode, count, pts, apf.paint());
  51. }
  52. }
  53. void SkPaintFilterCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
  54. AutoPaintFilter apf(this, paint);
  55. if (apf.shouldDraw()) {
  56. this->SkNWayCanvas::onDrawRect(rect, apf.paint());
  57. }
  58. }
  59. void SkPaintFilterCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
  60. AutoPaintFilter apf(this, paint);
  61. if (apf.shouldDraw()) {
  62. this->SkNWayCanvas::onDrawRRect(rrect, apf.paint());
  63. }
  64. }
  65. void SkPaintFilterCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
  66. const SkPaint& paint) {
  67. AutoPaintFilter apf(this, paint);
  68. if (apf.shouldDraw()) {
  69. this->SkNWayCanvas::onDrawDRRect(outer, inner, apf.paint());
  70. }
  71. }
  72. void SkPaintFilterCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
  73. AutoPaintFilter apf(this, paint);
  74. if (apf.shouldDraw()) {
  75. this->SkNWayCanvas::onDrawRegion(region, apf.paint());
  76. }
  77. }
  78. void SkPaintFilterCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
  79. AutoPaintFilter apf(this, paint);
  80. if (apf.shouldDraw()) {
  81. this->SkNWayCanvas::onDrawOval(rect, apf.paint());
  82. }
  83. }
  84. void SkPaintFilterCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
  85. bool useCenter, const SkPaint& paint) {
  86. AutoPaintFilter apf(this, paint);
  87. if (apf.shouldDraw()) {
  88. this->SkNWayCanvas::onDrawArc(rect, startAngle, sweepAngle, useCenter, apf.paint());
  89. }
  90. }
  91. void SkPaintFilterCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
  92. AutoPaintFilter apf(this, paint);
  93. if (apf.shouldDraw()) {
  94. this->SkNWayCanvas::onDrawPath(path, apf.paint());
  95. }
  96. }
  97. void SkPaintFilterCanvas::onDrawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
  98. const SkPaint* paint) {
  99. AutoPaintFilter apf(this, paint);
  100. if (apf.shouldDraw()) {
  101. this->SkNWayCanvas::onDrawBitmap(bm, left, top, &apf.paint());
  102. }
  103. }
  104. void SkPaintFilterCanvas::onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& dst,
  105. const SkPaint* paint, SrcRectConstraint constraint) {
  106. AutoPaintFilter apf(this, paint);
  107. if (apf.shouldDraw()) {
  108. this->SkNWayCanvas::onDrawBitmapRect(bm, src, dst, &apf.paint(), constraint);
  109. }
  110. }
  111. void SkPaintFilterCanvas::onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center,
  112. const SkRect& dst, const SkPaint* paint) {
  113. AutoPaintFilter apf(this, paint);
  114. if (apf.shouldDraw()) {
  115. this->SkNWayCanvas::onDrawBitmapNine(bm, center, dst, &apf.paint());
  116. }
  117. }
  118. void SkPaintFilterCanvas::onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice,
  119. const SkRect& dst, const SkPaint* paint) {
  120. AutoPaintFilter apf(this, paint);
  121. if (apf.shouldDraw()) {
  122. this->SkNWayCanvas::onDrawBitmapLattice(bitmap, lattice, dst, &apf.paint());
  123. }
  124. }
  125. void SkPaintFilterCanvas::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
  126. const SkPaint* paint) {
  127. AutoPaintFilter apf(this, paint);
  128. if (apf.shouldDraw()) {
  129. this->SkNWayCanvas::onDrawImage(image, left, top, &apf.paint());
  130. }
  131. }
  132. void SkPaintFilterCanvas::onDrawImageRect(const SkImage* image, const SkRect* src,
  133. const SkRect& dst, const SkPaint* paint,
  134. SrcRectConstraint constraint) {
  135. AutoPaintFilter apf(this, paint);
  136. if (apf.shouldDraw()) {
  137. this->SkNWayCanvas::onDrawImageRect(image, src, dst, &apf.paint(), constraint);
  138. }
  139. }
  140. void SkPaintFilterCanvas::onDrawImageNine(const SkImage* image, const SkIRect& center,
  141. const SkRect& dst, const SkPaint* paint) {
  142. AutoPaintFilter apf(this, paint);
  143. if (apf.shouldDraw()) {
  144. this->SkNWayCanvas::onDrawImageNine(image, center, dst, &apf.paint());
  145. }
  146. }
  147. void SkPaintFilterCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice,
  148. const SkRect& dst, const SkPaint* paint) {
  149. AutoPaintFilter apf(this, paint);
  150. if (apf.shouldDraw()) {
  151. this->SkNWayCanvas::onDrawImageLattice(image, lattice, dst, &apf.paint());
  152. }
  153. }
  154. void SkPaintFilterCanvas::onDrawVerticesObject(const SkVertices* vertices,
  155. const SkVertices::Bone bones[], int boneCount,
  156. SkBlendMode bmode, const SkPaint& paint) {
  157. AutoPaintFilter apf(this, paint);
  158. if (apf.shouldDraw()) {
  159. this->SkNWayCanvas::onDrawVerticesObject(vertices, bones, boneCount, bmode, apf.paint());
  160. }
  161. }
  162. void SkPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColor colors[],
  163. const SkPoint texCoords[], SkBlendMode bmode,
  164. const SkPaint& paint) {
  165. AutoPaintFilter apf(this, paint);
  166. if (apf.shouldDraw()) {
  167. this->SkNWayCanvas::onDrawPatch(cubics, colors, texCoords, bmode, apf.paint());
  168. }
  169. }
  170. void SkPaintFilterCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* m,
  171. const SkPaint* paint) {
  172. AutoPaintFilter apf(this, paint);
  173. if (apf.shouldDraw()) {
  174. this->SkNWayCanvas::onDrawPicture(picture, m, &apf.paint());
  175. }
  176. }
  177. void SkPaintFilterCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
  178. // There is no paint to filter in this case, but we can still filter on type.
  179. // Subclasses need to unroll the drawable explicity (by overriding this method) in
  180. // order to actually filter nested content.
  181. AutoPaintFilter apf(this, nullptr);
  182. if (apf.shouldDraw()) {
  183. this->SkNWayCanvas::onDrawDrawable(drawable, matrix);
  184. }
  185. }
  186. void SkPaintFilterCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
  187. const SkPaint& paint) {
  188. AutoPaintFilter apf(this, paint);
  189. if (apf.shouldDraw()) {
  190. this->SkNWayCanvas::onDrawTextBlob(blob, x, y, apf.paint());
  191. }
  192. }
  193. void SkPaintFilterCanvas::onDrawAtlas(const SkImage* image, const SkRSXform xform[],
  194. const SkRect tex[], const SkColor colors[], int count,
  195. SkBlendMode bmode, const SkRect* cull, const SkPaint* paint) {
  196. AutoPaintFilter apf(this, paint);
  197. if (apf.shouldDraw()) {
  198. this->SkNWayCanvas::onDrawAtlas(image, xform, tex, colors, count, bmode, cull, &apf.paint());
  199. }
  200. }
  201. void SkPaintFilterCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
  202. this->SkNWayCanvas::onDrawAnnotation(rect, key, value);
  203. }
  204. void SkPaintFilterCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
  205. this->SkNWayCanvas::onDrawShadowRec(path, rec);
  206. }
  207. void SkPaintFilterCanvas::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
  208. QuadAAFlags aa, SkColor color, SkBlendMode mode) {
  209. SkPaint paint;
  210. paint.setColor(color);
  211. paint.setBlendMode(mode);
  212. AutoPaintFilter apf(this, paint);
  213. if (apf.shouldDraw()) {
  214. this->SkNWayCanvas::onDrawEdgeAAQuad(rect, clip, aa, apf.paint().getColor(),
  215. apf.paint().getBlendMode());
  216. }
  217. }
  218. void SkPaintFilterCanvas::onDrawEdgeAAImageSet(const ImageSetEntry set[], int count,
  219. const SkPoint dstClips[],
  220. const SkMatrix preViewMatrices[],
  221. const SkPaint* paint, SrcRectConstraint constraint) {
  222. AutoPaintFilter apf(this, paint);
  223. if (apf.shouldDraw()) {
  224. this->SkNWayCanvas::onDrawEdgeAAImageSet(
  225. set, count, dstClips, preViewMatrices, &apf.paint(), constraint);
  226. }
  227. }
  228. sk_sp<SkSurface> SkPaintFilterCanvas::onNewSurface(const SkImageInfo& info,
  229. const SkSurfaceProps& props) {
  230. return proxy()->makeSurface(info, &props);
  231. }
  232. bool SkPaintFilterCanvas::onPeekPixels(SkPixmap* pixmap) {
  233. return proxy()->peekPixels(pixmap);
  234. }
  235. bool SkPaintFilterCanvas::onAccessTopLayerPixels(SkPixmap* pixmap) {
  236. SkImageInfo info;
  237. size_t rowBytes;
  238. void* addr = proxy()->accessTopLayerPixels(&info, &rowBytes);
  239. if (!addr) {
  240. return false;
  241. }
  242. pixmap->reset(info, addr, rowBytes);
  243. return true;
  244. }
  245. SkImageInfo SkPaintFilterCanvas::onImageInfo() const {
  246. return proxy()->imageInfo();
  247. }
  248. bool SkPaintFilterCanvas::onGetProps(SkSurfaceProps* props) const {
  249. return proxy()->getProps(props);
  250. }