Skia
2D Graphics Library
SkCanvas.h
Go to the documentation of this file.
1 /*
2  * Copyright 2006 The Android Open Source Project
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 #ifndef SkCanvas_DEFINED
9 #define SkCanvas_DEFINED
10 
12 #include "include/core/SkClipOp.h"
13 #include "include/core/SkColor.h"
17 #include "include/core/SkM44.h"
18 #include "include/core/SkMatrix.h"
19 #include "include/core/SkPaint.h"
20 #include "include/core/SkPoint.h"
22 #include "include/core/SkRect.h"
23 #include "include/core/SkRefCnt.h"
25 #include "include/core/SkScalar.h"
26 #include "include/core/SkSize.h"
27 #include "include/core/SkString.h"
29 #include "include/core/SkTypes.h"
30 #include "include/private/base/SkCPUTypes.h"
31 #include "include/private/base/SkDeque.h"
32 
33 #include <cstdint>
34 #include <cstring>
35 #include <memory>
36 #include <optional>
37 
38 #ifndef SK_SUPPORT_LEGACY_GETTOTALMATRIX
39 #define SK_SUPPORT_LEGACY_GETTOTALMATRIX
40 #endif
41 
42 namespace sktext {
43 class GlyphRunBuilder;
44 class GlyphRunList;
45 }
46 
47 class AutoLayerForImageFilter;
48 class GrRecordingContext;
49 
50 class SkBitmap;
51 class SkBlender;
52 class SkData;
53 class SkDevice;
54 class SkDrawable;
55 class SkFont;
56 class SkImage;
57 class SkMesh;
58 class SkPaintFilterCanvas;
59 class SkPath;
60 class SkPicture;
61 class SkPixmap;
62 class SkRRect;
63 class SkRegion;
64 class SkShader;
65 class SkSpecialImage;
66 class SkSurface;
67 class SkSurface_Base;
68 class SkTextBlob;
69 class SkVertices;
70 struct SkDrawShadowRec;
71 struct SkRSXform;
72 
73 namespace skgpu::graphite { class Recorder; }
74 namespace sktext::gpu { class Slug; }
75 namespace SkRecords { class Draw; }
76 
99 class SK_API SkCanvas {
100 public:
101 
127  static std::unique_ptr<SkCanvas> MakeRasterDirect(const SkImageInfo& info, void* pixels,
128  size_t rowBytes,
129  const SkSurfaceProps* props = nullptr);
130 
156  static std::unique_ptr<SkCanvas> MakeRasterDirectN32(int width, int height, SkPMColor* pixels,
157  size_t rowBytes) {
158  return MakeRasterDirect(SkImageInfo::MakeN32Premul(width, height), pixels, rowBytes);
159  }
160 
169 
187  SkCanvas(int width, int height, const SkSurfaceProps* props = nullptr);
188 
191  explicit SkCanvas(sk_sp<SkDevice> device);
192 
207  explicit SkCanvas(const SkBitmap& bitmap);
208 
209 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
212  enum class ColorBehavior {
213  kLegacy,
214  };
215 
222  SkCanvas(const SkBitmap& bitmap, ColorBehavior behavior);
223 #endif
224 
239  SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props);
240 
246  virtual ~SkCanvas();
247 
256 
267  bool getProps(SkSurfaceProps* props) const;
268 
275 
283 
292  virtual SkISize getBaseLayerSize() const;
293 
306  sk_sp<SkSurface> makeSurface(const SkImageInfo& info, const SkSurfaceProps* props = nullptr);
307 
315 
316 
321  virtual skgpu::graphite::Recorder* recorder() const;
322 
327 
344  void* accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin = nullptr);
345 
358 
374  bool peekPixels(SkPixmap* pixmap);
375 
410  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
411  int srcX, int srcY);
412 
448  bool readPixels(const SkPixmap& pixmap, int srcX, int srcY);
449 
485  bool readPixels(const SkBitmap& bitmap, int srcX, int srcY);
486 
523  bool writePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes, int x, int y);
524 
563  bool writePixels(const SkBitmap& bitmap, int x, int y);
564 
581  int save();
582 
605  int saveLayer(const SkRect* bounds, const SkPaint* paint);
606 
626  int saveLayer(const SkRect& bounds, const SkPaint* paint) {
627  return this->saveLayer(&bounds, paint);
628  }
629 
652  int saveLayerAlphaf(const SkRect* bounds, float alpha);
653  // Helper that accepts an int between 0 and 255, and divides it by 255.0
654  int saveLayerAlpha(const SkRect* bounds, U8CPU alpha) {
655  return this->saveLayerAlphaf(bounds, alpha * (1.0f / 255));
656  }
657 
664  kPreserveLCDText_SaveLayerFlag = 1 << 1,
665  kInitWithPrevious_SaveLayerFlag = 1 << 2,
666  // instead of matching previous layer's colortype, use F16
667  kF16ColorType = 1 << 4,
668  };
669 
670  typedef uint32_t SaveLayerFlags;
671 
675  struct SaveLayerRec {
681 
689  SaveLayerRec(const SkRect* bounds, const SkPaint* paint, SaveLayerFlags saveLayerFlags = 0)
690  : SaveLayerRec(bounds, paint, nullptr, 1.f, saveLayerFlags) {}
691 
704  SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop,
705  SaveLayerFlags saveLayerFlags)
706  : SaveLayerRec(bounds, paint, backdrop, 1.f, saveLayerFlags) {}
707 
709  const SkRect* fBounds = nullptr;
710 
712  const SkPaint* fPaint = nullptr;
713 
720  const SkImageFilter* fBackdrop = nullptr;
721 
723  SaveLayerFlags fSaveLayerFlags = 0;
724 
725  private:
726  friend class SkCanvas;
727  friend class SkCanvasPriv;
728 
729  SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop,
730  SkScalar backdropScale, SaveLayerFlags saveLayerFlags)
731  : fBounds(bounds)
732  , fPaint(paint)
733  , fBackdrop(backdrop)
734  , fSaveLayerFlags(saveLayerFlags)
735  , fExperimentalBackdropScale(backdropScale) {}
736 
737  // Relative scale factor that the image content used to initialize the layer when the
738  // kInitFromPrevious flag or a backdrop filter is used.
739  SkScalar fExperimentalBackdropScale = 1.f;
740  };
741 
760  int saveLayer(const SaveLayerRec& layerRec);
761 
771  void restore();
772 
781  int getSaveCount() const;
782 
793  void restoreToCount(int saveCount);
794 
809 
823  void scale(SkScalar sx, SkScalar sy);
824 
837  void rotate(SkScalar degrees);
838 
855  void rotate(SkScalar degrees, SkScalar px, SkScalar py);
856 
871  void skew(SkScalar sx, SkScalar sy);
872 
882  void concat(const SkMatrix& matrix);
883  void concat(const SkM44&);
884 
892  void setMatrix(const SkM44& matrix);
893 
894  // DEPRECATED -- use SkM44 version
895  void setMatrix(const SkMatrix& matrix);
896 
902  void resetMatrix();
903 
914  void clipRect(const SkRect& rect, SkClipOp op, bool doAntiAlias);
915 
923  void clipRect(const SkRect& rect, SkClipOp op) {
924  this->clipRect(rect, op, false);
925  }
926 
935  void clipRect(const SkRect& rect, bool doAntiAlias = false) {
936  this->clipRect(rect, SkClipOp::kIntersect, doAntiAlias);
937  }
938 
939  void clipIRect(const SkIRect& irect, SkClipOp op = SkClipOp::kIntersect) {
940  this->clipRect(SkRect::Make(irect), op, false);
941  }
942 
956 
968  void clipRRect(const SkRRect& rrect, SkClipOp op, bool doAntiAlias);
969 
977  void clipRRect(const SkRRect& rrect, SkClipOp op) {
978  this->clipRRect(rrect, op, false);
979  }
980 
988  void clipRRect(const SkRRect& rrect, bool doAntiAlias = false) {
989  this->clipRRect(rrect, SkClipOp::kIntersect, doAntiAlias);
990  }
991 
1004  void clipPath(const SkPath& path, SkClipOp op, bool doAntiAlias);
1005 
1017  void clipPath(const SkPath& path, SkClipOp op) {
1018  this->clipPath(path, op, false);
1019  }
1020 
1031  void clipPath(const SkPath& path, bool doAntiAlias = false) {
1032  this->clipPath(path, SkClipOp::kIntersect, doAntiAlias);
1033  }
1034 
1036 
1046  void clipRegion(const SkRegion& deviceRgn, SkClipOp op = SkClipOp::kIntersect);
1047 
1058  bool quickReject(const SkRect& rect) const;
1059 
1070  bool quickReject(const SkPath& path) const;
1071 
1083 
1093  bool getLocalClipBounds(SkRect* bounds) const {
1094  *bounds = this->getLocalClipBounds();
1095  return !bounds->isEmpty();
1096  }
1097 
1108 
1117  bool getDeviceClipBounds(SkIRect* bounds) const {
1118  *bounds = this->getDeviceClipBounds();
1119  return !bounds->isEmpty();
1120  }
1121 
1131  this->drawColor(SkColor4f::FromColor(color), mode);
1132  }
1133 
1141 
1147  void clear(SkColor color) {
1148  this->clear(SkColor4f::FromColor(color));
1149  }
1150 
1156  void clear(const SkColor4f& color) {
1157  this->drawColor(color, SkBlendMode::kSrc);
1158  }
1159 
1172  void discard() { this->onDiscard(); }
1173 
1182  void drawPaint(const SkPaint& paint);
1183 
1188  enum PointMode {
1192  };
1193 
1225  void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint);
1226 
1241  void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint);
1242 
1254  void drawPoint(SkPoint p, const SkPaint& paint) {
1255  this->drawPoint(p.x(), p.y(), paint);
1256  }
1257 
1271  void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint& paint);
1272 
1282  void drawLine(SkPoint p0, SkPoint p1, const SkPaint& paint) {
1283  this->drawLine(p0.x(), p0.y(), p1.x(), p1.y(), paint);
1284  }
1285 
1296  void drawRect(const SkRect& rect, const SkPaint& paint);
1297 
1306  void drawIRect(const SkIRect& rect, const SkPaint& paint) {
1307  SkRect r;
1308  r.set(rect); // promotes the ints to scalars
1309  this->drawRect(r, paint);
1310  }
1311 
1322  void drawRegion(const SkRegion& region, const SkPaint& paint);
1323 
1333  void drawOval(const SkRect& oval, const SkPaint& paint);
1334 
1347  void drawRRect(const SkRRect& rrect, const SkPaint& paint);
1348 
1368  void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint);
1369 
1382  void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint& paint);
1383 
1393  void drawCircle(SkPoint center, SkScalar radius, const SkPaint& paint) {
1394  this->drawCircle(center.x(), center.y(), radius, paint);
1395  }
1396 
1417  void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
1418  bool useCenter, const SkPaint& paint);
1419 
1437  void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry, const SkPaint& paint);
1438 
1453  void drawPath(const SkPath& path, const SkPaint& paint);
1454 
1455  void drawImage(const SkImage* image, SkScalar left, SkScalar top) {
1456  this->drawImage(image, left, top, SkSamplingOptions(), nullptr);
1457  }
1458  void drawImage(const sk_sp<SkImage>& image, SkScalar left, SkScalar top) {
1459  this->drawImage(image.get(), left, top, SkSamplingOptions(), nullptr);
1460  }
1461 
1471  };
1472 
1474  const SkPaint* = nullptr);
1475  void drawImage(const sk_sp<SkImage>& image, SkScalar x, SkScalar y,
1476  const SkSamplingOptions& sampling, const SkPaint* paint = nullptr) {
1477  this->drawImage(image.get(), x, y, sampling, paint);
1478  }
1479  void drawImageRect(const SkImage*, const SkRect& src, const SkRect& dst,
1480  const SkSamplingOptions&, const SkPaint*, SrcRectConstraint);
1481  void drawImageRect(const SkImage*, const SkRect& dst, const SkSamplingOptions&,
1482  const SkPaint* = nullptr);
1483  void drawImageRect(const sk_sp<SkImage>& image, const SkRect& src, const SkRect& dst,
1484  const SkSamplingOptions& sampling, const SkPaint* paint,
1485  SrcRectConstraint constraint) {
1486  this->drawImageRect(image.get(), src, dst, sampling, paint, constraint);
1487  }
1488  void drawImageRect(const sk_sp<SkImage>& image, const SkRect& dst,
1489  const SkSamplingOptions& sampling, const SkPaint* paint = nullptr) {
1490  this->drawImageRect(image.get(), dst, sampling, paint);
1491  }
1492 
1516  void drawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst,
1517  SkFilterMode filter, const SkPaint* paint = nullptr);
1518 
1528  struct Lattice {
1529 
1534  enum RectType : uint8_t {
1535  kDefault = 0,
1538  };
1539 
1540  const int* fXDivs;
1541  const int* fYDivs;
1543  int fXCount;
1544  int fYCount;
1545  const SkIRect* fBounds;
1546  const SkColor* fColors;
1547  };
1548 
1576  void drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst,
1577  SkFilterMode filter, const SkPaint* paint = nullptr);
1578  void drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst) {
1579  this->drawImageLattice(image, lattice, dst, SkFilterMode::kNearest, nullptr);
1580  }
1581 
1585  enum QuadAAFlags : unsigned {
1586  kLeft_QuadAAFlag = 0b0001,
1587  kTop_QuadAAFlag = 0b0010,
1588  kRight_QuadAAFlag = 0b0100,
1589  kBottom_QuadAAFlag = 0b1000,
1590 
1591  kNone_QuadAAFlags = 0b0000,
1592  kAll_QuadAAFlags = 0b1111,
1593  };
1594 
1596  struct SK_API ImageSetEntry {
1597  ImageSetEntry(sk_sp<const SkImage> image, const SkRect& srcRect, const SkRect& dstRect,
1598  int matrixIndex, float alpha, unsigned aaFlags, bool hasClip);
1599 
1600  ImageSetEntry(sk_sp<const SkImage> image, const SkRect& srcRect, const SkRect& dstRect,
1601  float alpha, unsigned aaFlags);
1602 
1607 
1611  int fMatrixIndex = -1; // Index into the preViewMatrices arg, or < 0
1612  float fAlpha = 1.f;
1613  unsigned fAAFlags = kNone_QuadAAFlags; // QuadAAFlags
1614  bool fHasClip = false; // True to use next 4 points in dstClip arg as quad
1615  };
1616 
1636  void experimental_DrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], QuadAAFlags aaFlags,
1637  const SkColor4f& color, SkBlendMode mode);
1638  void experimental_DrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], QuadAAFlags aaFlags,
1639  SkColor color, SkBlendMode mode) {
1640  this->experimental_DrawEdgeAAQuad(rect, clip, aaFlags, SkColor4f::FromColor(color), mode);
1641  }
1642 
1671  void experimental_DrawEdgeAAImageSet(const ImageSetEntry imageSet[], int cnt,
1672  const SkPoint dstClips[], const SkMatrix preViewMatrices[],
1673  const SkSamplingOptions&, const SkPaint* paint = nullptr,
1674  SrcRectConstraint constraint = kStrict_SrcRectConstraint);
1675 
1703  void drawSimpleText(const void* text, size_t byteLength, SkTextEncoding encoding,
1704  SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint);
1705 
1730  void drawString(const char str[], SkScalar x, SkScalar y, const SkFont& font,
1731  const SkPaint& paint) {
1732  this->drawSimpleText(str, strlen(str), SkTextEncoding::kUTF8, x, y, font, paint);
1733  }
1734 
1759  void drawString(const SkString& str, SkScalar x, SkScalar y, const SkFont& font,
1760  const SkPaint& paint) {
1761  this->drawSimpleText(str.c_str(), str.size(), SkTextEncoding::kUTF8, x, y, font, paint);
1762  }
1763 
1786  void drawGlyphs(int count, const SkGlyphID glyphs[], const SkPoint positions[],
1787  const uint32_t clusters[], int textByteCount, const char utf8text[],
1788  SkPoint origin, const SkFont& font, const SkPaint& paint);
1789 
1808  void drawGlyphs(int count, const SkGlyphID glyphs[], const SkPoint positions[],
1809  SkPoint origin, const SkFont& font, const SkPaint& paint);
1810 
1830  void drawGlyphs(int count, const SkGlyphID glyphs[], const SkRSXform xforms[],
1831  SkPoint origin, const SkFont& font, const SkPaint& paint);
1832 
1856  void drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint);
1857 
1876  void drawTextBlob(const sk_sp<SkTextBlob>& blob, SkScalar x, SkScalar y, const SkPaint& paint) {
1877  this->drawTextBlob(blob.get(), x, y, paint);
1878  }
1879 
1888  void drawPicture(const SkPicture* picture) {
1889  this->drawPicture(picture, nullptr, nullptr);
1890  }
1891 
1900  void drawPicture(const sk_sp<SkPicture>& picture) {
1901  this->drawPicture(picture.get());
1902  }
1903 
1918  void drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint);
1919 
1932  void drawPicture(const sk_sp<SkPicture>& picture, const SkMatrix* matrix,
1933  const SkPaint* paint) {
1934  this->drawPicture(picture.get(), matrix, paint);
1935  }
1936 
1955  void drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint);
1956 
1975  void drawVertices(const sk_sp<SkVertices>& vertices, SkBlendMode mode, const SkPaint& paint);
1976 
1997  void drawMesh(const SkMesh& mesh, sk_sp<SkBlender> blender, const SkPaint& paint);
1998 
2028  void drawPatch(const SkPoint cubics[12], const SkColor colors[4],
2029  const SkPoint texCoords[4], SkBlendMode mode, const SkPaint& paint);
2030 
2056  void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
2057  const SkColor colors[], int count, SkBlendMode mode,
2058  const SkSamplingOptions& sampling, const SkRect* cullRect, const SkPaint* paint);
2059 
2073  void drawDrawable(SkDrawable* drawable, const SkMatrix* matrix = nullptr);
2074 
2088  void drawDrawable(SkDrawable* drawable, SkScalar x, SkScalar y);
2089 
2102  void drawAnnotation(const SkRect& rect, const char key[], SkData* value);
2103 
2114  void drawAnnotation(const SkRect& rect, const char key[], const sk_sp<SkData>& value) {
2115  this->drawAnnotation(rect, key, value.get());
2116  }
2117 
2128  virtual bool isClipEmpty() const;
2129 
2137  virtual bool isClipRect() const;
2138 
2145 
2150  return this->getLocalToDevice().asM33();
2151  }
2152 
2153 #ifdef SK_SUPPORT_LEGACY_GETTOTALMATRIX
2164 #endif
2165 
2167 
2173 
2174  void private_draw_shadow_rec(const SkPath&, const SkDrawShadowRec&);
2175 
2176 
2177 protected:
2178  // default impl defers to getDevice()->newSurface(info)
2179  virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props);
2180 
2181  // default impl defers to its device
2182  virtual bool onPeekPixels(SkPixmap* pixmap);
2183  virtual bool onAccessTopLayerPixels(SkPixmap* pixmap);
2184  virtual SkImageInfo onImageInfo() const;
2185  virtual bool onGetProps(SkSurfaceProps* props, bool top) const;
2186 
2187  // Subclass save/restore notifiers.
2188  // Overriders should call the corresponding INHERITED method up the inheritance chain.
2189  // getSaveLayerStrategy()'s return value may suppress full layer allocation.
2193  };
2194 
2195  virtual void willSave() {}
2196  // Overriders should call the corresponding INHERITED method up the inheritance chain.
2198  return kFullLayer_SaveLayerStrategy;
2199  }
2200 
2201  // returns true if we should actually perform the saveBehind, or false if we should just save.
2202  virtual bool onDoSaveBehind(const SkRect*) { return true; }
2203  virtual void willRestore() {}
2204  virtual void didRestore() {}
2205 
2206  virtual void didConcat44(const SkM44&) {}
2207  virtual void didSetM44(const SkM44&) {}
2208  virtual void didTranslate(SkScalar, SkScalar) {}
2209  virtual void didScale(SkScalar, SkScalar) {}
2210 
2211  // NOTE: If you are adding a new onDraw virtual to SkCanvas, PLEASE add an override to
2212  // SkCanvasVirtualEnforcer (in SkCanvasVirtualEnforcer.h). This ensures that subclasses using
2213  // that mechanism will be required to implement the new function.
2214  virtual void onDrawPaint(const SkPaint& paint);
2215  virtual void onDrawBehind(const SkPaint& paint);
2216  virtual void onDrawRect(const SkRect& rect, const SkPaint& paint);
2217  virtual void onDrawRRect(const SkRRect& rrect, const SkPaint& paint);
2218  virtual void onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint);
2219  virtual void onDrawOval(const SkRect& rect, const SkPaint& paint);
2220  virtual void onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
2221  bool useCenter, const SkPaint& paint);
2222  virtual void onDrawPath(const SkPath& path, const SkPaint& paint);
2223  virtual void onDrawRegion(const SkRegion& region, const SkPaint& paint);
2224 
2225  virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
2226  const SkPaint& paint);
2227 
2228  virtual void onDrawGlyphRunList(const sktext::GlyphRunList& glyphRunList, const SkPaint& paint);
2229 
2230  virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
2231  const SkPoint texCoords[4], SkBlendMode mode, const SkPaint& paint);
2232  virtual void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
2233  const SkPaint& paint);
2234 
2235  virtual void onDrawImage2(const SkImage*, SkScalar dx, SkScalar dy, const SkSamplingOptions&,
2236  const SkPaint*);
2237  virtual void onDrawImageRect2(const SkImage*, const SkRect& src, const SkRect& dst,
2238  const SkSamplingOptions&, const SkPaint*, SrcRectConstraint);
2239  virtual void onDrawImageLattice2(const SkImage*, const Lattice&, const SkRect& dst,
2240  SkFilterMode, const SkPaint*);
2241  virtual void onDrawAtlas2(const SkImage*, const SkRSXform[], const SkRect src[],
2242  const SkColor[], int count, SkBlendMode, const SkSamplingOptions&,
2243  const SkRect* cull, const SkPaint*);
2244  virtual void onDrawEdgeAAImageSet2(const ImageSetEntry imageSet[], int count,
2245  const SkPoint dstClips[], const SkMatrix preViewMatrices[],
2246  const SkSamplingOptions&, const SkPaint*,
2248 
2249  virtual void onDrawVerticesObject(const SkVertices* vertices, SkBlendMode mode,
2250  const SkPaint& paint);
2251  virtual void onDrawMesh(const SkMesh&, sk_sp<SkBlender>, const SkPaint&);
2252  virtual void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value);
2253  virtual void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&);
2254 
2255  virtual void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix);
2256  virtual void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
2257  const SkPaint* paint);
2258 
2259  virtual void onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], QuadAAFlags aaFlags,
2260  const SkColor4f& color, SkBlendMode mode);
2261 
2264  kSoft_ClipEdgeStyle
2265  };
2266 
2267  virtual void onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle);
2268  virtual void onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle);
2269  virtual void onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle);
2271  virtual void onClipRegion(const SkRegion& deviceRgn, SkClipOp op);
2272  virtual void onResetClip();
2273 
2274  virtual void onDiscard();
2275 
2279  const sktext::GlyphRunList& glyphRunList, const SkPaint& paint);
2280 
2283  virtual void onDrawSlug(const sktext::gpu::Slug* slug);
2284 
2285 private:
2286 
2287  enum ShaderOverrideOpacity {
2288  kNone_ShaderOverrideOpacity,
2289  kOpaque_ShaderOverrideOpacity,
2290  kNotOpaque_ShaderOverrideOpacity,
2291  };
2292 
2293  // notify our surface (if we have one) that we are about to draw, so it
2294  // can perform copy-on-write or invalidate any cached images
2295  // returns false if the copy failed
2296  [[nodiscard]] bool predrawNotify(bool willOverwritesEntireSurface = false);
2297  [[nodiscard]] bool predrawNotify(const SkRect*, const SkPaint*, ShaderOverrideOpacity);
2298 
2299  enum class CheckForOverwrite : bool {
2300  kNo = false,
2301  kYes = true
2302  };
2303  // call the appropriate predrawNotify and create a layer if needed.
2304  std::optional<AutoLayerForImageFilter> aboutToDraw(
2305  SkCanvas* canvas,
2306  const SkPaint& paint,
2307  const SkRect* rawBounds = nullptr,
2308  CheckForOverwrite = CheckForOverwrite::kNo,
2309  ShaderOverrideOpacity = kNone_ShaderOverrideOpacity);
2310 
2311  // The bottom-most device in the stack, only changed by init(). Image properties and the final
2312  // canvas pixels are determined by this device.
2313  SkDevice* rootDevice() const {
2314  SkASSERT(fRootDevice);
2315  return fRootDevice.get();
2316  }
2317 
2318  // The top-most device in the stack, will change within saveLayer()'s. All drawing and clipping
2319  // operations should route to this device.
2320  SkDevice* topDevice() const;
2321 
2322  // Canvases maintain a sparse stack of layers, where the top-most layer receives the drawing,
2323  // clip, and matrix commands. There is a layer per call to saveLayer() using the
2324  // kFullLayer_SaveLayerStrategy.
2325  struct Layer {
2326  sk_sp<SkDevice> fDevice;
2327  sk_sp<SkImageFilter> fImageFilter; // applied to layer *before* being drawn by paint
2328  SkPaint fPaint;
2329  bool fIsCoverage;
2330  bool fDiscard;
2331 
2332  Layer(sk_sp<SkDevice> device,
2333  sk_sp<SkImageFilter> imageFilter,
2334  const SkPaint& paint,
2335  bool isCoverage);
2336  };
2337 
2338  // Encapsulate state needed to restore from saveBehind()
2339  struct BackImage {
2340  // Out of line to avoid including SkSpecialImage.h
2341  BackImage(sk_sp<SkSpecialImage>, SkIPoint);
2342  BackImage(const BackImage&);
2343  BackImage(BackImage&&);
2344  BackImage& operator=(const BackImage&);
2345  ~BackImage();
2346 
2347  sk_sp<SkSpecialImage> fImage;
2348  SkIPoint fLoc;
2349  };
2350 
2351  class MCRec {
2352  public:
2353  // If not null, this MCRec corresponds with the saveLayer() record that made the layer.
2354  // The base "layer" is not stored here, since it is stored inline in SkCanvas and has no
2355  // restoration behavior.
2356  std::unique_ptr<Layer> fLayer;
2357 
2358  // This points to the device of the top-most layer (which may be lower in the stack), or
2359  // to the canvas's fRootDevice. The MCRec does not own the device.
2360  SkDevice* fDevice;
2361 
2362  std::unique_ptr<BackImage> fBackImage;
2363  SkM44 fMatrix;
2364  int fDeferredSaveCount = 0;
2365 
2366  MCRec(SkDevice* device);
2367  MCRec(const MCRec* prev);
2368  ~MCRec();
2369 
2370  void newLayer(sk_sp<SkDevice> layerDevice,
2371  sk_sp<SkImageFilter> filter,
2372  const SkPaint& restorePaint,
2373  bool layerIsCoverage);
2374 
2375  void reset(SkDevice* device);
2376  };
2377 
2378  // the first N recs that can fit here mean we won't call malloc
2379  static constexpr int kMCRecSize = 96; // most recent measurement
2380  static constexpr int kMCRecCount = 32; // common depth for save/restores
2381 
2382  intptr_t fMCRecStorage[kMCRecSize * kMCRecCount / sizeof(intptr_t)];
2383 
2384  SkDeque fMCStack;
2385  // points to top of stack
2386  MCRec* fMCRec;
2387 
2388  // Installed via init()
2389  sk_sp<SkDevice> fRootDevice;
2390  const SkSurfaceProps fProps;
2391 
2392  int fSaveCount; // value returned by getSaveCount()
2393 
2394  std::unique_ptr<SkRasterHandleAllocator> fAllocator;
2395 
2396  SkSurface_Base* fSurfaceBase;
2397  SkSurface_Base* getSurfaceBase() const { return fSurfaceBase; }
2398  void setSurfaceBase(SkSurface_Base* sb) {
2399  fSurfaceBase = sb;
2400  }
2401  friend class SkSurface_Base;
2402  friend class SkSurface_Ganesh;
2403 
2404  SkIRect fClipRestrictionRect = SkIRect::MakeEmpty();
2405  int fClipRestrictionSaveCount = -1;
2406 
2407  void doSave();
2408  void checkForDeferredSave();
2409  void internalSetMatrix(const SkM44&);
2410 
2411  friend class SkAndroidFrameworkUtils;
2412  friend class SkCanvasPriv; // needs to expose android functions for testing outside android
2413  friend class AutoLayerForImageFilter;
2414  friend class SkSurface_Raster; // needs getDevice()
2415  friend class SkNoDrawCanvas; // needs resetForNextPicture()
2416  friend class SkNWayCanvas;
2417  friend class SkPictureRecord; // predrawNotify (why does it need it? <reed>)
2418  friend class SkOverdrawCanvas;
2420  friend class SkRecords::Draw;
2421  template <typename Key>
2422  friend class SkTestCanvas;
2423 
2424 protected:
2425  // For use by SkNoDrawCanvas (via SkCanvasVirtualEnforcer, which can't be a friend)
2426  SkCanvas(const SkIRect& bounds);
2427 private:
2428  SkCanvas(const SkBitmap&, std::unique_ptr<SkRasterHandleAllocator>,
2430 
2431  SkCanvas(SkCanvas&&) = delete;
2432  SkCanvas(const SkCanvas&) = delete;
2433  SkCanvas& operator=(SkCanvas&&) = delete;
2434  SkCanvas& operator=(const SkCanvas&) = delete;
2435 
2436  friend class sktext::gpu::Slug;
2437  friend class SkPicturePlayback;
2441  sk_sp<sktext::gpu::Slug> convertBlobToSlug(const SkTextBlob& blob, SkPoint origin,
2442  const SkPaint& paint);
2443 
2447  void drawSlug(const sktext::gpu::Slug* slug);
2448 
2457  int only_axis_aligned_saveBehind(const SkRect* subset);
2458 
2463  void drawClippedToSaveBehind(const SkPaint&);
2464 
2465  void resetForNextPicture(const SkIRect& bounds);
2466 
2467  // needs gettotalclip()
2468  friend class SkCanvasStateUtils;
2469 
2470  void init(sk_sp<SkDevice>);
2471 
2472  // All base onDrawX() functions should call this and skip drawing if it returns true.
2473  // If 'matrix' is non-null, it maps the paint's fast bounds before checking for quick rejection
2474  bool internalQuickReject(const SkRect& bounds, const SkPaint& paint,
2475  const SkMatrix* matrix = nullptr);
2476 
2477  void internalDrawPaint(const SkPaint& paint);
2478  void internalSaveLayer(const SaveLayerRec&, SaveLayerStrategy, bool coverageOnly=false);
2479  void internalSaveBehind(const SkRect*);
2480 
2481  void internalConcat44(const SkM44&);
2482 
2483  // shared by save() and saveLayer()
2484  void internalSave();
2485  void internalRestore();
2486 
2487  enum class DeviceCompatibleWithFilter : bool {
2488  // Check the src device's local-to-device matrix for compatibility with the filter, and if
2489  // it is not compatible, introduce an intermediate image and transformation that allows the
2490  // filter to be evaluated on the modified src content.
2491  kUnknown = false,
2492  // Assume that the src device's local-to-device matrix is compatible with the filter.
2493  kYes = true
2494  };
2506  void internalDrawDeviceWithFilter(SkDevice* src, SkDevice* dst,
2507  const SkImageFilter* filter, const SkPaint& paint,
2508  DeviceCompatibleWithFilter compat,
2509  SkScalar scaleFactor = 1.f,
2510  bool srcIsCoverageLayer = false);
2511 
2512  /*
2513  * Returns true if drawing the specified rect (or all if it is null) with the specified
2514  * paint (or default if null) would overwrite the entire root device of the canvas
2515  * (i.e. the canvas' surface if it had one).
2516  */
2517  bool wouldOverwriteEntireSurface(const SkRect*, const SkPaint*, ShaderOverrideOpacity) const;
2518 
2522  bool canDrawBitmapAsSprite(SkScalar x, SkScalar y, int w, int h, const SkSamplingOptions&,
2523  const SkPaint&);
2524 
2529  bool androidFramework_isClipAA() const;
2530 
2536  void internal_private_resetClip();
2537 
2538  virtual SkPaintFilterCanvas* internal_private_asPaintFilterCanvas() const { return nullptr; }
2539 
2540  // Keep track of the device clip bounds in the canvas' global space to reject draws before
2541  // invoking the top-level device.
2542  SkRect fQuickRejectBounds;
2543 
2544  // Compute the clip's bounds based on all clipped SkDevice's reported device bounds transformed
2545  // into the canvas' global space.
2546  SkRect computeDeviceClipBounds(bool outsetForAA=true) const;
2547 
2548  class AutoUpdateQRBounds;
2549  void validateClip() const;
2550 
2551  std::unique_ptr<sktext::GlyphRunBuilder> fScratchGlyphRunBuilder;
2552 };
2553 
2560 public:
2561 
2568  SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas), fSaveCount(0) {
2569  if (fCanvas) {
2570  fSaveCount = canvas->getSaveCount();
2571  if (doSave) {
2572  canvas->save();
2573  }
2574  }
2575  }
2576 
2581  if (fCanvas) {
2582  fCanvas->restoreToCount(fSaveCount);
2583  }
2584  }
2585 
2589  void restore() {
2590  if (fCanvas) {
2591  fCanvas->restoreToCount(fSaveCount);
2592  fCanvas = nullptr;
2593  }
2594  }
2595 
2596 private:
2597  SkCanvas* fCanvas;
2598  int fSaveCount;
2599 
2601  SkAutoCanvasRestore(const SkAutoCanvasRestore&) = delete;
2602  SkAutoCanvasRestore& operator=(SkAutoCanvasRestore&&) = delete;
2603  SkAutoCanvasRestore& operator=(const SkAutoCanvasRestore&) = delete;
2604 };
2605 
2606 #endif
SkBlendMode
Blends are operators that take in two colors (source, destination) and return a new color.
Definition: SkBlendMode.h:38
@ kSrcOver
r = s + (1-sa)*d
SkClipOp
Definition: SkClipOp.h:13
Types, consts, functions, and macros for colors.
uint32_t SkColor
32-bit ARGB color value, unpremultiplied.
Definition: SkColor.h:37
uint32_t SkPMColor
32-bit ARGB color value, premultiplied.
Definition: SkColor.h:205
SkTextEncoding
Definition: SkFontTypes.h:11
@ kUTF8
uses bytes to represent UTF-8 or ASCII
@ kYes
Do pre-clip the geometry before applying the (perspective) matrix.
@ kNo
Don't pre-clip the geometry before applying the (perspective) matrix.
SkFilterMode
Definition: SkSamplingOptions.h:16
float SkScalar
Definition: SkScalar.h:14
uint16_t SkGlyphID
16 bit unsigned integer to hold a glyph index
Definition: SkTypes.h:171
Definition: GrRecordingContext.h:42
Stack helper class calls SkCanvas::restoreToCount when SkAutoCanvasRestore goes out of scope.
Definition: SkCanvas.h:2559
void restore()
Restores SkCanvas to saved state immediately.
Definition: SkCanvas.h:2589
SkAutoCanvasRestore(SkCanvas *canvas, bool doSave)
Preserves SkCanvas::save() count.
Definition: SkCanvas.h:2568
~SkAutoCanvasRestore()
Restores SkCanvas to saved state.
Definition: SkCanvas.h:2580
SkBitmap describes a two-dimensional raster pixel array.
Definition: SkBitmap.h:59
SkBlender represents a custom blend function in the Skia pipeline.
Definition: SkBlender.h:19
SkCanvas provides an interface for drawing, and how the drawing is clipped and transformed.
Definition: SkCanvas.h:99
static std::unique_ptr< SkCanvas > MakeRasterDirectN32(int width, int height, SkPMColor *pixels, size_t rowBytes)
Allocates raster SkCanvas specified by inline image specification.
Definition: SkCanvas.h:156
virtual void onDrawPath(const SkPath &path, const SkPaint &paint)
virtual void onDrawVerticesObject(const SkVertices *vertices, SkBlendMode mode, const SkPaint &paint)
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Saves SkMatrix and clip, and allocates a SkSurface for subsequent drawing.
virtual ~SkCanvas()
Draws saved layers, if any.
virtual void onDrawEdgeAAQuad(const SkRect &rect, const SkPoint clip[4], QuadAAFlags aaFlags, const SkColor4f &color, SkBlendMode mode)
bool getProps(SkSurfaceProps *props) const
Copies SkSurfaceProps, if SkCanvas is associated with raster surface or GPU surface,...
SkRasterHandleAllocator::Handle accessTopRasterHandle() const
Returns custom context that tracks the SkMatrix and clip.
void clipPath(const SkPath &path, bool doAntiAlias=false)
Replaces clip with the intersection of clip and path.
Definition: SkCanvas.h:1031
void drawRect(const SkRect &rect, const SkPaint &paint)
Draws SkRect rect using clip, SkMatrix, and SkPaint paint.
void drawOval(const SkRect &oval, const SkPaint &paint)
Draws oval oval using clip, SkMatrix, and SkPaint.
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Replaces clip with the intersection or difference of clip and rect, with an aliased or anti-aliased c...
virtual void onClipRRect(const SkRRect &rrect, SkClipOp op, ClipEdgeStyle edgeStyle)
virtual void onDrawMesh(const SkMesh &, sk_sp< SkBlender >, const SkPaint &)
bool peekPixels(SkPixmap *pixmap)
Returns true if SkCanvas has direct access to its pixels.
void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint &paint)
Draws pts using clip, SkMatrix and SkPaint paint.
void concat(const SkM44 &)
virtual void onResetClip()
SkSurfaceProps getTopProps() const
Returns the SkSurfaceProps associated with the canvas that are currently active (i....
virtual void onDrawSlug(const sktext::gpu::Slug *slug)
void drawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], SkBlendMode mode, const SkPaint &paint)
Draws a Coons patch: the interpolation of four cubics with shared corners, associating a color,...
void drawImageRect(const sk_sp< SkImage > &image, const SkRect &dst, const SkSamplingOptions &sampling, const SkPaint *paint=nullptr)
Definition: SkCanvas.h:1488
void restore()
Removes changes to SkMatrix and clip since SkCanvas state was last saved.
void drawPoint(SkScalar x, SkScalar y, const SkPaint &paint)
Draws point at (x, y) using clip, SkMatrix and SkPaint paint.
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Draws text, with origin at (x, y), using clip, SkMatrix, SkFont font, and SkPaint paint.
void rotate(SkScalar degrees, SkScalar px, SkScalar py)
Rotates SkMatrix by degrees about a point at (px, py).
virtual void onDrawRRect(const SkRRect &rrect, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
Translates SkMatrix by dx along the x-axis and dy along the y-axis.
virtual skgpu::graphite::Recorder * recorder() const
Returns Recorder for the GPU surface associated with SkCanvas.
void drawPoint(SkPoint p, const SkPaint &paint)
Draws point p using clip, SkMatrix and SkPaint paint.
Definition: SkCanvas.h:1254
void drawGlyphs(int count, const SkGlyphID glyphs[], const SkRSXform xforms[], SkPoint origin, const SkFont &font, const SkPaint &paint)
Draws count glyphs, at positions relative to origin styled with font and paint.
void clipIRect(const SkIRect &irect, SkClipOp op=SkClipOp::kIntersect)
Definition: SkCanvas.h:939
sk_sp< SkSurface > makeSurface(const SkImageInfo &info, const SkSurfaceProps *props=nullptr)
Creates SkSurface matching info and props, and associates it with SkCanvas.
static std::unique_ptr< SkCanvas > MakeRasterDirect(const SkImageInfo &info, void *pixels, size_t rowBytes, const SkSurfaceProps *props=nullptr)
Allocates raster SkCanvas that will draw directly into pixels.
virtual bool isClipRect() const
Returns true if clip is SkRect and not empty.
void drawImageNine(const SkImage *image, const SkIRect &center, const SkRect &dst, SkFilterMode filter, const SkPaint *paint=nullptr)
Draws SkImage image stretched proportionally to fit into SkRect dst.
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Fills clip with color color.
Definition: SkCanvas.h:1130
virtual void onDrawRect(const SkRect &rect, const SkPaint &paint)
void drawPicture(const sk_sp< SkPicture > &picture)
Draws SkPicture picture, using clip and SkMatrix.
Definition: SkCanvas.h:1900
void drawDrawable(SkDrawable *drawable, SkScalar x, SkScalar y)
Draws SkDrawable drawable using clip and SkMatrix, offset by (x, y).
void androidFramework_setDeviceClipRestriction(const SkIRect &rect)
Sets the maximum clip rectangle, which can be set by clipRect(), clipRRect() and clipPath() and inter...
void clipRect(const SkRect &rect, SkClipOp op)
Replaces clip with the intersection or difference of clip and rect.
Definition: SkCanvas.h:923
int saveLayer(const SaveLayerRec &layerRec)
Saves SkMatrix and clip, and allocates SkSurface for subsequent drawing.
virtual bool isClipEmpty() const
Returns true if clip is empty; that is, nothing will draw.
void drawImage(const SkImage *, SkScalar x, SkScalar y, const SkSamplingOptions &, const SkPaint *=nullptr)
void drawGlyphs(int count, const SkGlyphID glyphs[], const SkPoint positions[], SkPoint origin, const SkFont &font, const SkPaint &paint)
Draws count glyphs, at positions relative to origin styled with font and paint.
void clear(const SkColor4f &color)
Fills clip with color color using SkBlendMode::kSrc.
Definition: SkCanvas.h:1156
SkMatrix getLocalToDeviceAs3x3() const
Throws away the 3rd row and column in the matrix, so be warned.
Definition: SkCanvas.h:2149
bool writePixels(const SkImageInfo &info, const void *pixels, size_t rowBytes, int x, int y)
Copies SkRect from pixels to SkCanvas.
void discard()
Makes SkCanvas contents undefined.
Definition: SkCanvas.h:1172
virtual void onDrawEdgeAAImageSet2(const ImageSetEntry imageSet[], int count, const SkPoint dstClips[], const SkMatrix preViewMatrices[], const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
SkRect getLocalClipBounds() const
Returns bounds of clip, transformed by inverse of SkMatrix.
virtual GrRecordingContext * recordingContext() const
Returns Ganesh context of the GPU surface associated with SkCanvas.
virtual sk_sp< sktext::gpu::Slug > onConvertGlyphRunListToSlug(const sktext::GlyphRunList &glyphRunList, const SkPaint &paint)
virtual void onClipPath(const SkPath &path, SkClipOp op, ClipEdgeStyle edgeStyle)
void drawString(const SkString &str, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Draws SkString, with origin at (x, y), using clip, SkMatrix, SkFont font, and SkPaint paint.
Definition: SkCanvas.h:1759
virtual void onDrawDRRect(const SkRRect &outer, const SkRRect &inner, const SkPaint &paint)
virtual void didScale(SkScalar, SkScalar)
Definition: SkCanvas.h:2209
virtual void didSetM44(const SkM44 &)
Definition: SkCanvas.h:2207
virtual void onDrawArc(const SkRect &rect, SkScalar startAngle, SkScalar sweepAngle, bool useCenter, const SkPaint &paint)
virtual bool onGetProps(SkSurfaceProps *props, bool top) const
bool getLocalClipBounds(SkRect *bounds) const
Returns bounds of clip, transformed by inverse of SkMatrix.
Definition: SkCanvas.h:1093
virtual void didRestore()
Definition: SkCanvas.h:2204
void drawPaint(const SkPaint &paint)
Fills clip with SkPaint paint.
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
Draws line segment from (x0, y0) to (x1, y1) using clip, SkMatrix, and SkPaint paint.
void private_draw_shadow_rec(const SkPath &, const SkDrawShadowRec &)
void drawDrawable(SkDrawable *drawable, const SkMatrix *matrix=nullptr)
Draws SkDrawable drawable using clip and SkMatrix, concatenated with optional matrix.
void * accessTopLayerPixels(SkImageInfo *info, size_t *rowBytes, SkIPoint *origin=nullptr)
Returns the pixel base address, SkImageInfo, rowBytes, and origin if the pixels can be read directly.
void drawAnnotation(const SkRect &rect, const char key[], SkData *value)
Associates SkRect on SkCanvas with an annotation; a key-value pair, where the key is a null-terminate...
bool writePixels(const SkBitmap &bitmap, int x, int y)
Copies SkRect from pixels to SkCanvas.
virtual void onDrawDrawable(SkDrawable *drawable, const SkMatrix *matrix)
virtual bool onAccessTopLayerPixels(SkPixmap *pixmap)
void clipRRect(const SkRRect &rrect, SkClipOp op)
Replaces clip with the intersection or difference of clip and rrect.
Definition: SkCanvas.h:977
virtual void onDrawPicture(const SkPicture *picture, const SkMatrix *matrix, const SkPaint *paint)
bool quickReject(const SkPath &path) const
Returns true if path, transformed by SkMatrix, can be quickly determined to be outside of clip.
virtual bool onPeekPixels(SkPixmap *pixmap)
bool readPixels(const SkPixmap &pixmap, int srcX, int srcY)
Copies SkRect of pixels from SkCanvas into pixmap.
virtual SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec &)
Definition: SkCanvas.h:2197
SkCanvas(const SkBitmap &bitmap, const SkSurfaceProps &props)
Constructs a canvas that draws into bitmap.
void clipPath(const SkPath &path, SkClipOp op)
Replaces clip with the intersection or difference of clip and path.
Definition: SkCanvas.h:1017
void drawColor(const SkColor4f &color, SkBlendMode mode=SkBlendMode::kSrcOver)
Fills clip with color color.
int saveLayerAlpha(const SkRect *bounds, U8CPU alpha)
Definition: SkCanvas.h:654
void clipRect(const SkRect &rect, bool doAntiAlias=false)
Replaces clip with the intersection of clip and rect.
Definition: SkCanvas.h:935
SrcRectConstraint
Definition: SkCanvas.h:1468
@ kStrict_SrcRectConstraint
sample only inside bounds; slower
Definition: SkCanvas.h:1469
@ kFast_SrcRectConstraint
sample outside bounds; faster
Definition: SkCanvas.h:1470
int saveLayer(const SkRect &bounds, const SkPaint *paint)
Saves SkMatrix and clip, and allocates a SkSurface for subsequent drawing.
Definition: SkCanvas.h:626
void drawGlyphs(int count, const SkGlyphID glyphs[], const SkPoint positions[], const uint32_t clusters[], int textByteCount, const char utf8text[], SkPoint origin, const SkFont &font, const SkPaint &paint)
Draws count glyphs, at positions relative to origin styled with font and paint with supporting utf8 a...
SkM44 getLocalToDevice() const
Returns the current transform from local coordinates to the 'device', which for most purposes means p...
void clipRegion(const SkRegion &deviceRgn, SkClipOp op=SkClipOp::kIntersect)
Replaces clip with the intersection or difference of clip and SkRegion deviceRgn.
void experimental_DrawEdgeAAQuad(const SkRect &rect, const SkPoint clip[4], QuadAAFlags aaFlags, const SkColor4f &color, SkBlendMode mode)
This is an experimental API for the SkiaRenderer Chromium project, and its API will surely evolve if ...
SaveLayerStrategy
Definition: SkCanvas.h:2190
@ kFullLayer_SaveLayerStrategy
Definition: SkCanvas.h:2191
@ kNoLayer_SaveLayerStrategy
Definition: SkCanvas.h:2192
virtual void onClipRect(const SkRect &rect, SkClipOp op, ClipEdgeStyle edgeStyle)
virtual SkImageInfo onImageInfo() const
void drawMesh(const SkMesh &mesh, sk_sp< SkBlender > blender, const SkPaint &paint)
Experimental, under active development, and subject to change without notice.
virtual void onDrawOval(const SkRect &rect, const SkPaint &paint)
void drawIRect(const SkIRect &rect, const SkPaint &paint)
Draws SkIRect rect using clip, SkMatrix, and SkPaint paint.
Definition: SkCanvas.h:1306
virtual void onDrawImageLattice2(const SkImage *, const Lattice &, const SkRect &dst, SkFilterMode, const SkPaint *)
void clear(SkColor color)
Fills clip with color color using SkBlendMode::kSrc.
Definition: SkCanvas.h:1147
virtual void onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
virtual void onDrawBehind(const SkPaint &paint)
virtual void onDrawShadowRec(const SkPath &, const SkDrawShadowRec &)
uint32_t SaveLayerFlags
Definition: SkCanvas.h:670
virtual void onDrawAtlas2(const SkImage *, const SkRSXform[], const SkRect src[], const SkColor[], int count, SkBlendMode, const SkSamplingOptions &, const SkRect *cull, const SkPaint *)
void drawImageRect(const sk_sp< SkImage > &image, const SkRect &src, const SkRect &dst, const SkSamplingOptions &sampling, const SkPaint *paint, SrcRectConstraint constraint)
Definition: SkCanvas.h:1483
bool getDeviceClipBounds(SkIRect *bounds) const
Returns SkIRect bounds of clip, unaffected by SkMatrix.
Definition: SkCanvas.h:1117
SkSurface * getSurface() const
Sometimes a canvas is owned by a surface.
virtual void willRestore()
Definition: SkCanvas.h:2203
int getSaveCount() const
Returns the number of saved states, each containing: SkMatrix and clip.
void experimental_DrawEdgeAAImageSet(const ImageSetEntry imageSet[], int cnt, const SkPoint dstClips[], const SkMatrix preViewMatrices[], const SkSamplingOptions &, const SkPaint *paint=nullptr, SrcRectConstraint constraint=kStrict_SrcRectConstraint)
This is an bulk variant of experimental_DrawEdgeAAQuad() that renders 'cnt' textured quads.
void rotate(SkScalar degrees)
Rotates SkMatrix by degrees.
void restoreToCount(int saveCount)
Restores state to SkMatrix and clip values when save(), saveLayer(), saveLayerPreserveLCDTextRequests...
virtual sk_sp< SkSurface > onNewSurface(const SkImageInfo &info, const SkSurfaceProps &props)
void experimental_DrawEdgeAAQuad(const SkRect &rect, const SkPoint clip[4], QuadAAFlags aaFlags, SkColor color, SkBlendMode mode)
Definition: SkCanvas.h:1638
virtual void didTranslate(SkScalar, SkScalar)
Definition: SkCanvas.h:2208
void drawImage(const sk_sp< SkImage > &image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1458
void drawRRect(const SkRRect &rrect, const SkPaint &paint)
Draws SkRRect rrect using clip, SkMatrix, and SkPaint paint.
SkMatrix getTotalMatrix() const
DEPRECATED Legacy version of getLocalToDevice(), which strips away any Z information,...
void drawRoundRect(const SkRect &rect, SkScalar rx, SkScalar ry, const SkPaint &paint)
Draws SkRRect bounded by SkRect rect, with corner radii (rx, ry) using clip, SkMatrix,...
virtual void onDrawAnnotation(const SkRect &rect, const char key[], SkData *value)
void drawArc(const SkRect &oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter, const SkPaint &paint)
Draws arc using clip, SkMatrix, and SkPaint paint.
void drawImageLattice(const SkImage *image, const Lattice &lattice, const SkRect &dst, SkFilterMode filter, const SkPaint *paint=nullptr)
Draws SkImage image stretched proportionally to fit into SkRect dst.
void drawPicture(const sk_sp< SkPicture > &picture, const SkMatrix *matrix, const SkPaint *paint)
Draws SkPicture picture, using clip and SkMatrix; transforming picture with SkMatrix matrix,...
Definition: SkCanvas.h:1932
void clipRRect(const SkRRect &rrect, bool doAntiAlias=false)
Replaces clip with the intersection of clip and rrect, with an aliased or anti-aliased clip edge.
Definition: SkCanvas.h:988
void resetMatrix()
Sets SkMatrix to the identity matrix.
virtual void onDiscard()
void clipPath(const SkPath &path, SkClipOp op, bool doAntiAlias)
Replaces clip with the intersection or difference of clip and path, with an aliased or anti-aliased c...
void setMatrix(const SkMatrix &matrix)
SkIRect getDeviceClipBounds() const
Returns SkIRect bounds of clip, unaffected by SkMatrix.
SaveLayerFlagsSet
Definition: SkCanvas.h:663
virtual void onDrawRegion(const SkRegion &region, const SkPaint &paint)
virtual void onDrawImageRect2(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
void drawRegion(const SkRegion &region, const SkPaint &paint)
Draws SkRegion region using clip, SkMatrix, and SkPaint paint.
virtual void onClipRegion(const SkRegion &deviceRgn, SkClipOp op)
virtual void didConcat44(const SkM44 &)
Definition: SkCanvas.h:2206
void drawPicture(const SkPicture *picture, const SkMatrix *matrix, const SkPaint *paint)
Draws SkPicture picture, using clip and SkMatrix; transforming picture with SkMatrix matrix,...
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
int save()
Saves SkMatrix and clip.
void drawPath(const SkPath &path, const SkPaint &paint)
Draws SkPath path using clip, SkMatrix, and SkPaint paint.
void drawAtlas(const SkImage *atlas, const SkRSXform xform[], const SkRect tex[], const SkColor colors[], int count, SkBlendMode mode, const SkSamplingOptions &sampling, const SkRect *cullRect, const SkPaint *paint)
Draws a set of sprites from atlas, using clip, SkMatrix, and optional SkPaint paint.
void setMatrix(const SkM44 &matrix)
Replaces SkMatrix with matrix.
void drawImageLattice(const SkImage *image, const Lattice &lattice, const SkRect &dst)
Definition: SkCanvas.h:1578
void temporary_internal_getRgnClip(SkRegion *region)
Returns the global clip as a region.
virtual void onDrawImage2(const SkImage *, SkScalar dx, SkScalar dy, const SkSamplingOptions &, const SkPaint *)
SkCanvas(const SkIRect &bounds)
void drawCircle(SkPoint center, SkScalar radius, const SkPaint &paint)
Draws circle at center with radius using clip, SkMatrix, and SkPaint paint.
Definition: SkCanvas.h:1393
void drawDRRect(const SkRRect &outer, const SkRRect &inner, const SkPaint &paint)
Draws SkRRect outer and inner using clip, SkMatrix, and SkPaint paint.
SkCanvas(sk_sp< SkDevice > device)
Private.
virtual void onDrawPaint(const SkPaint &paint)
void scale(SkScalar sx, SkScalar sy)
Scales SkMatrix by sx on the x-axis and sy on the y-axis.
void concat(const SkMatrix &matrix)
Replaces SkMatrix with matrix premultiplied with existing SkMatrix.
virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], SkBlendMode mode, const SkPaint &paint)
void drawImage(const sk_sp< SkImage > &image, SkScalar x, SkScalar y, const SkSamplingOptions &sampling, const SkPaint *paint=nullptr)
Definition: SkCanvas.h:1475
ClipEdgeStyle
Definition: SkCanvas.h:2262
@ kHard_ClipEdgeStyle
Definition: SkCanvas.h:2263
virtual void onDrawGlyphRunList(const sktext::GlyphRunList &glyphRunList, const SkPaint &paint)
virtual bool onDoSaveBehind(const SkRect *)
Definition: SkCanvas.h:2202
SkCanvas(int width, int height, const SkSurfaceProps *props=nullptr)
Creates SkCanvas of the specified dimensions without a SkSurface.
SkSurfaceProps getBaseProps() const
Returns the SkSurfaceProps associated with the canvas (i.e., at the base of the layer stack).
virtual void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint &paint)
void drawVertices(const sk_sp< SkVertices > &vertices, SkBlendMode mode, const SkPaint &paint)
Draws SkVertices vertices, a triangle mesh, using clip and SkMatrix.
virtual SkISize getBaseLayerSize() const
Gets the size of the base or root layer in global canvas coordinates.
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Draws null terminated string, with origin at (x, y), using clip, SkMatrix, SkFont font,...
Definition: SkCanvas.h:1730
void drawPicture(const SkPicture *picture)
Draws SkPicture picture, using clip and SkMatrix.
Definition: SkCanvas.h:1888
bool readPixels(const SkBitmap &bitmap, int srcX, int srcY)
Copies SkRect of pixels from SkCanvas into bitmap.
void clipShader(sk_sp< SkShader >, SkClipOp=SkClipOp::kIntersect)
virtual void willSave()
Definition: SkCanvas.h:2195
void drawVertices(const SkVertices *vertices, SkBlendMode mode, const SkPaint &paint)
Draws SkVertices vertices, a triangle mesh, using clip and SkMatrix.
virtual void onClipShader(sk_sp< SkShader >, SkClipOp)
void drawLine(SkPoint p0, SkPoint p1, const SkPaint &paint)
Draws line segment from p0 to p1 using clip, SkMatrix, and SkPaint paint.
Definition: SkCanvas.h:1282
SkImageInfo imageInfo() const
Returns SkImageInfo for SkCanvas.
void drawTextBlob(const sk_sp< SkTextBlob > &blob, SkScalar x, SkScalar y, const SkPaint &paint)
Draws SkTextBlob blob at (x, y), using clip, SkMatrix, and SkPaint paint.
Definition: SkCanvas.h:1876
PointMode
Definition: SkCanvas.h:1188
@ kLines_PointMode
draw each pair of points as a line segment
Definition: SkCanvas.h:1190
@ kPolygon_PointMode
draw the array of points as a open polygon
Definition: SkCanvas.h:1191
@ kPoints_PointMode
draw each point separately
Definition: SkCanvas.h:1189
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY)
Copies SkRect of pixels from SkCanvas into dstPixels.
void skew(SkScalar sx, SkScalar sy)
Skews SkMatrix by sx on the x-axis and sy on the y-axis.
SkCanvas(const SkBitmap &bitmap)
Constructs a canvas that draws into bitmap.
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
Draws SkTextBlob blob at (x, y), using clip, SkMatrix, and SkPaint paint.
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1455
QuadAAFlags
Experimental.
Definition: SkCanvas.h:1585
bool quickReject(const SkRect &rect) const
Returns true if SkRect rect, transformed by SkMatrix, can be quickly determined to be outside of clip...
void drawImageRect(const SkImage *, const SkRect &dst, const SkSamplingOptions &, const SkPaint *=nullptr)
SkCanvas()
Creates an empty SkCanvas with no backing device or pixels, with a width and height of zero.
void clipRRect(const SkRRect &rrect, SkClipOp op, bool doAntiAlias)
Replaces clip with the intersection or difference of clip and rrect, with an aliased or anti-aliased ...
int saveLayerAlphaf(const SkRect *bounds, float alpha)
Saves SkMatrix and clip, and allocates SkSurface for subsequent drawing.
void drawAnnotation(const SkRect &rect, const char key[], const sk_sp< SkData > &value)
Associates SkRect on SkCanvas when an annotation; a key-value pair, where the key is a null-terminate...
Definition: SkCanvas.h:2114
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
Draws circle at (cx, cy) with radius using clip, SkMatrix, and SkPaint paint.
SkData holds an immutable data buffer.
Definition: SkData.h:25
Base-class for objects that draw into SkCanvas.
Definition: SkDrawable.h:37
SkFont controls options applied when drawing and measuring text.
Definition: SkFont.h:36
Base class for image filters.
Definition: SkImageFilter.h:35
SkImage describes a two dimensional array of pixels to draw.
Definition: SkImage.h:270
4x4 matrix used by SkCanvas and other parts of Skia.
Definition: SkM44.h:150
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
A vertex buffer, a topology, optionally an index buffer, and a compatible SkMeshSpecification.
Definition: SkMesh.h:261
Captures all drawing commands.
Definition: SkOverdrawCanvas.h:44
SkPaint controls options applied when drawing.
Definition: SkPaint.h:44
SkPath contain geometry.
Definition: SkPath.h:58
SkPicture records drawing commands made to SkCanvas.
Definition: SkPicture.h:44
SkPixmap provides a utility to pair SkImageInfo with pixels and row bytes.
Definition: SkPixmap.h:40
SkRRect describes a rounded rectangle with a bounds and a pair of radii for each corner.
Definition: SkRRect.h:35
If a client wants to control the allocation of raster layers in a canvas, it should subclass SkRaster...
Definition: SkRasterHandleAllocator.h:37
void * Handle
Definition: SkRasterHandleAllocator.h:42
SkRegion describes the set of pixels used to clip SkCanvas.
Definition: SkRegion.h:30
Shaders specify the source color(s) for what is being drawn.
Definition: SkShader.h:35
Light weight class for managing strings.
Definition: SkString.h:118
size_t size() const
Definition: SkString.h:131
const char * c_str() const
Definition: SkString.h:133
Describes properties and constraints of a given SkSurface.
Definition: SkSurfaceProps.h:52
SkSurface is responsible for managing the pixels that a canvas draws into.
Definition: SkSurface.h:161
SkTextBlob combines multiple text runs into an immutable container.
Definition: SkTextBlob.h:41
An immutable set of vertex data that can be used with SkCanvas::drawVertices.
Definition: SkVertices.h:24
T * get() const
Definition: SkRefCnt.h:303
Definition: SkCanvas.h:75
Definition: SkCanvas.h:73
Definition: SkCanvas.h:74
Definition: SkCanvas.h:42
This is used by the experimental API below.
Definition: SkCanvas.h:1596
ImageSetEntry(sk_sp< const SkImage > image, const SkRect &srcRect, const SkRect &dstRect, int matrixIndex, float alpha, unsigned aaFlags, bool hasClip)
ImageSetEntry & operator=(const ImageSetEntry &)
SkRect fSrcRect
Definition: SkCanvas.h:1609
ImageSetEntry(const ImageSetEntry &)
SkRect fDstRect
Definition: SkCanvas.h:1610
sk_sp< const SkImage > fImage
Definition: SkCanvas.h:1608
ImageSetEntry(sk_sp< const SkImage > image, const SkRect &srcRect, const SkRect &dstRect, float alpha, unsigned aaFlags)
SkCanvas::Lattice divides SkBitmap or SkImage into a rectangular grid.
Definition: SkCanvas.h:1528
int fYCount
number of y-coordinates
Definition: SkCanvas.h:1544
const SkIRect * fBounds
source bounds to draw from
Definition: SkCanvas.h:1545
RectType
Definition: SkCanvas.h:1534
@ kFixedColor
draws one of fColors into lattice rectangle
Definition: SkCanvas.h:1537
@ kTransparent
skips lattice rectangle by making it transparent
Definition: SkCanvas.h:1536
const int * fYDivs
y-axis values dividing bitmap
Definition: SkCanvas.h:1541
int fXCount
number of x-coordinates
Definition: SkCanvas.h:1543
const RectType * fRectTypes
array of fill types
Definition: SkCanvas.h:1542
const SkColor * fColors
array of colors
Definition: SkCanvas.h:1546
const int * fXDivs
x-axis values dividing bitmap
Definition: SkCanvas.h:1540
SaveLayerRec contains the state used to create the layer.
Definition: SkCanvas.h:675
SaveLayerRec(const SkRect *bounds, const SkPaint *paint, SaveLayerFlags saveLayerFlags=0)
Sets fBounds, fPaint, and fSaveLayerFlags; sets fBackdrop to nullptr.
Definition: SkCanvas.h:689
SaveLayerRec(const SkRect *bounds, const SkPaint *paint, const SkImageFilter *backdrop, SaveLayerFlags saveLayerFlags)
Sets fBounds, fPaint, fBackdrop, and fSaveLayerFlags.
Definition: SkCanvas.h:704
SaveLayerRec()
Sets fBounds, fPaint, and fBackdrop to nullptr.
Definition: SkCanvas.h:680
RGBA color value, holding four floating point components.
SkIRect holds four 32-bit integer coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:32
static constexpr SkIRect MakeEmpty()
Returns constructed SkIRect set to (0, 0, 0, 0).
Definition: SkRect.h:45
bool isEmpty() const
Returns true if width() or height() are zero or negative.
Definition: SkRect.h:202
Definition: SkSize.h:15
Describes pixel dimensions and encoding.
Definition: SkImageInfo.h:194
static SkImageInfo MakeN32Premul(int width, int height)
Creates SkImageInfo from integral dimensions width and height, kN32_SkColorType, kPremul_SkAlphaType,...
A compressed form of a rotation+scale matrix.
Definition: SkRSXform.h:21
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582
static SkRect Make(const SkISize &size)
Returns constructed SkIRect set to (0, 0, size.width(), size.height()).
Definition: SkRect.h:669
bool isEmpty() const
Returns true if fLeft is equal to or greater than fRight, or if fTop is equal to or greater than fBot...
Definition: SkRect.h:693
void set(const SkIRect &src)
Sets SkRect to src, promoting src members from integer to float.
Definition: SkRect.h:860
Definition: SkSamplingOptions.h:58