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 SkBaseDevice;
51 class SkBitmap;
52 class SkBlender;
53 class SkData;
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 
77 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(SK_GANESH)
79 #endif
80 
81 // TODO:
82 // This is not ideal but Chrome is depending on a forward decl of GrSlug here.
83 // It should be removed once Chrome has migrated to sktext::gpu::Slug.
84 using GrSlug = sktext::gpu::Slug;
85 
108 class SK_API SkCanvas {
109 public:
110 
136  static std::unique_ptr<SkCanvas> MakeRasterDirect(const SkImageInfo& info, void* pixels,
137  size_t rowBytes,
138  const SkSurfaceProps* props = nullptr);
139 
165  static std::unique_ptr<SkCanvas> MakeRasterDirectN32(int width, int height, SkPMColor* pixels,
166  size_t rowBytes) {
167  return MakeRasterDirect(SkImageInfo::MakeN32Premul(width, height), pixels, rowBytes);
168  }
169 
178 
196  SkCanvas(int width, int height, const SkSurfaceProps* props = nullptr);
197 
200  explicit SkCanvas(sk_sp<SkBaseDevice> device);
201 
216  explicit SkCanvas(const SkBitmap& bitmap);
217 
218 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
221  enum class ColorBehavior {
222  kLegacy,
223  };
224 
231  SkCanvas(const SkBitmap& bitmap, ColorBehavior behavior);
232 #endif
233 
248  SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props);
249 
255  virtual ~SkCanvas();
256 
265 
276  bool getProps(SkSurfaceProps* props) const;
277 
284 
292 
300  void flush();
301 
310  virtual SkISize getBaseLayerSize() const;
311 
324  sk_sp<SkSurface> makeSurface(const SkImageInfo& info, const SkSurfaceProps* props = nullptr);
325 
333 
338  virtual skgpu::graphite::Recorder* recorder();
339 
344 
361  void* accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin = nullptr);
362 
375 
391  bool peekPixels(SkPixmap* pixmap);
392 
427  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
428  int srcX, int srcY);
429 
465  bool readPixels(const SkPixmap& pixmap, int srcX, int srcY);
466 
502  bool readPixels(const SkBitmap& bitmap, int srcX, int srcY);
503 
540  bool writePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes, int x, int y);
541 
580  bool writePixels(const SkBitmap& bitmap, int x, int y);
581 
598  int save();
599 
622  int saveLayer(const SkRect* bounds, const SkPaint* paint);
623 
643  int saveLayer(const SkRect& bounds, const SkPaint* paint) {
644  return this->saveLayer(&bounds, paint);
645  }
646 
669  int saveLayerAlphaf(const SkRect* bounds, float alpha);
670  // Helper that accepts an int between 0 and 255, and divides it by 255.0
671  int saveLayerAlpha(const SkRect* bounds, U8CPU alpha) {
672  return this->saveLayerAlphaf(bounds, alpha * (1.0f / 255));
673  }
674 
681  kPreserveLCDText_SaveLayerFlag = 1 << 1,
682  kInitWithPrevious_SaveLayerFlag = 1 << 2,
683  // instead of matching previous layer's colortype, use F16
684  kF16ColorType = 1 << 4,
685  };
686 
687  typedef uint32_t SaveLayerFlags;
688 
692  struct SaveLayerRec {
698 
706  SaveLayerRec(const SkRect* bounds, const SkPaint* paint, SaveLayerFlags saveLayerFlags = 0)
707  : SaveLayerRec(bounds, paint, nullptr, 1.f, saveLayerFlags) {}
708 
721  SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop,
722  SaveLayerFlags saveLayerFlags)
723  : SaveLayerRec(bounds, paint, backdrop, 1.f, saveLayerFlags) {}
724 
726  const SkRect* fBounds = nullptr;
727 
729  const SkPaint* fPaint = nullptr;
730 
737  const SkImageFilter* fBackdrop = nullptr;
738 
740  SaveLayerFlags fSaveLayerFlags = 0;
741 
742  private:
743  friend class SkCanvas;
744  friend class SkCanvasPriv;
745 
746  SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop,
747  SkScalar backdropScale, SaveLayerFlags saveLayerFlags)
748  : fBounds(bounds)
749  , fPaint(paint)
750  , fBackdrop(backdrop)
751  , fSaveLayerFlags(saveLayerFlags)
752  , fExperimentalBackdropScale(backdropScale) {}
753 
754  // Relative scale factor that the image content used to initialize the layer when the
755  // kInitFromPrevious flag or a backdrop filter is used.
756  SkScalar fExperimentalBackdropScale = 1.f;
757  };
758 
777  int saveLayer(const SaveLayerRec& layerRec);
778 
788  void restore();
789 
798  int getSaveCount() const;
799 
810  void restoreToCount(int saveCount);
811 
826 
840  void scale(SkScalar sx, SkScalar sy);
841 
854  void rotate(SkScalar degrees);
855 
872  void rotate(SkScalar degrees, SkScalar px, SkScalar py);
873 
888  void skew(SkScalar sx, SkScalar sy);
889 
899  void concat(const SkMatrix& matrix);
900  void concat(const SkM44&);
901 
909  void setMatrix(const SkM44& matrix);
910 
911  // DEPRECATED -- use SkM44 version
912  void setMatrix(const SkMatrix& matrix);
913 
919  void resetMatrix();
920 
931  void clipRect(const SkRect& rect, SkClipOp op, bool doAntiAlias);
932 
940  void clipRect(const SkRect& rect, SkClipOp op) {
941  this->clipRect(rect, op, false);
942  }
943 
952  void clipRect(const SkRect& rect, bool doAntiAlias = false) {
953  this->clipRect(rect, SkClipOp::kIntersect, doAntiAlias);
954  }
955 
956  void clipIRect(const SkIRect& irect, SkClipOp op = SkClipOp::kIntersect) {
957  this->clipRect(SkRect::Make(irect), op, false);
958  }
959 
973 
985  void clipRRect(const SkRRect& rrect, SkClipOp op, bool doAntiAlias);
986 
994  void clipRRect(const SkRRect& rrect, SkClipOp op) {
995  this->clipRRect(rrect, op, false);
996  }
997 
1005  void clipRRect(const SkRRect& rrect, bool doAntiAlias = false) {
1006  this->clipRRect(rrect, SkClipOp::kIntersect, doAntiAlias);
1007  }
1008 
1021  void clipPath(const SkPath& path, SkClipOp op, bool doAntiAlias);
1022 
1034  void clipPath(const SkPath& path, SkClipOp op) {
1035  this->clipPath(path, op, false);
1036  }
1037 
1048  void clipPath(const SkPath& path, bool doAntiAlias = false) {
1049  this->clipPath(path, SkClipOp::kIntersect, doAntiAlias);
1050  }
1051 
1053 
1063  void clipRegion(const SkRegion& deviceRgn, SkClipOp op = SkClipOp::kIntersect);
1064 
1075  bool quickReject(const SkRect& rect) const;
1076 
1087  bool quickReject(const SkPath& path) const;
1088 
1100 
1110  bool getLocalClipBounds(SkRect* bounds) const {
1111  *bounds = this->getLocalClipBounds();
1112  return !bounds->isEmpty();
1113  }
1114 
1125 
1134  bool getDeviceClipBounds(SkIRect* bounds) const {
1135  *bounds = this->getDeviceClipBounds();
1136  return !bounds->isEmpty();
1137  }
1138 
1148  this->drawColor(SkColor4f::FromColor(color), mode);
1149  }
1150 
1158 
1164  void clear(SkColor color) {
1165  this->clear(SkColor4f::FromColor(color));
1166  }
1167 
1173  void clear(const SkColor4f& color) {
1174  this->drawColor(color, SkBlendMode::kSrc);
1175  }
1176 
1189  void discard() { this->onDiscard(); }
1190 
1199  void drawPaint(const SkPaint& paint);
1200 
1205  enum PointMode {
1209  };
1210 
1242  void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint);
1243 
1258  void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint);
1259 
1271  void drawPoint(SkPoint p, const SkPaint& paint) {
1272  this->drawPoint(p.x(), p.y(), paint);
1273  }
1274 
1288  void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint& paint);
1289 
1299  void drawLine(SkPoint p0, SkPoint p1, const SkPaint& paint) {
1300  this->drawLine(p0.x(), p0.y(), p1.x(), p1.y(), paint);
1301  }
1302 
1313  void drawRect(const SkRect& rect, const SkPaint& paint);
1314 
1323  void drawIRect(const SkIRect& rect, const SkPaint& paint) {
1324  SkRect r;
1325  r.set(rect); // promotes the ints to scalars
1326  this->drawRect(r, paint);
1327  }
1328 
1339  void drawRegion(const SkRegion& region, const SkPaint& paint);
1340 
1350  void drawOval(const SkRect& oval, const SkPaint& paint);
1351 
1364  void drawRRect(const SkRRect& rrect, const SkPaint& paint);
1365 
1385  void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint);
1386 
1399  void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint& paint);
1400 
1410  void drawCircle(SkPoint center, SkScalar radius, const SkPaint& paint) {
1411  this->drawCircle(center.x(), center.y(), radius, paint);
1412  }
1413 
1434  void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
1435  bool useCenter, const SkPaint& paint);
1436 
1454  void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry, const SkPaint& paint);
1455 
1470  void drawPath(const SkPath& path, const SkPaint& paint);
1471 
1472  void drawImage(const SkImage* image, SkScalar left, SkScalar top) {
1473  this->drawImage(image, left, top, SkSamplingOptions(), nullptr);
1474  }
1475  void drawImage(const sk_sp<SkImage>& image, SkScalar left, SkScalar top) {
1476  this->drawImage(image.get(), left, top, SkSamplingOptions(), nullptr);
1477  }
1478 
1488  };
1489 
1491  const SkPaint* = nullptr);
1492  void drawImage(const sk_sp<SkImage>& image, SkScalar x, SkScalar y,
1493  const SkSamplingOptions& sampling, const SkPaint* paint = nullptr) {
1494  this->drawImage(image.get(), x, y, sampling, paint);
1495  }
1496  void drawImageRect(const SkImage*, const SkRect& src, const SkRect& dst,
1497  const SkSamplingOptions&, const SkPaint*, SrcRectConstraint);
1498  void drawImageRect(const SkImage*, const SkRect& dst, const SkSamplingOptions&,
1499  const SkPaint* = nullptr);
1500  void drawImageRect(const sk_sp<SkImage>& image, const SkRect& src, const SkRect& dst,
1501  const SkSamplingOptions& sampling, const SkPaint* paint,
1502  SrcRectConstraint constraint) {
1503  this->drawImageRect(image.get(), src, dst, sampling, paint, constraint);
1504  }
1505  void drawImageRect(const sk_sp<SkImage>& image, const SkRect& dst,
1506  const SkSamplingOptions& sampling, const SkPaint* paint = nullptr) {
1507  this->drawImageRect(image.get(), dst, sampling, paint);
1508  }
1509 
1533  void drawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst,
1534  SkFilterMode filter, const SkPaint* paint = nullptr);
1535 
1545  struct Lattice {
1546 
1551  enum RectType : uint8_t {
1552  kDefault = 0,
1555  };
1556 
1557  const int* fXDivs;
1558  const int* fYDivs;
1560  int fXCount;
1561  int fYCount;
1562  const SkIRect* fBounds;
1563  const SkColor* fColors;
1564  };
1565 
1593  void drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst,
1594  SkFilterMode filter, const SkPaint* paint = nullptr);
1595  void drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst) {
1596  this->drawImageLattice(image, lattice, dst, SkFilterMode::kNearest, nullptr);
1597  }
1598 
1602  enum QuadAAFlags : unsigned {
1603  kLeft_QuadAAFlag = 0b0001,
1604  kTop_QuadAAFlag = 0b0010,
1605  kRight_QuadAAFlag = 0b0100,
1606  kBottom_QuadAAFlag = 0b1000,
1607 
1608  kNone_QuadAAFlags = 0b0000,
1609  kAll_QuadAAFlags = 0b1111,
1610  };
1611 
1613  struct SK_API ImageSetEntry {
1614  ImageSetEntry(sk_sp<const SkImage> image, const SkRect& srcRect, const SkRect& dstRect,
1615  int matrixIndex, float alpha, unsigned aaFlags, bool hasClip);
1616 
1617  ImageSetEntry(sk_sp<const SkImage> image, const SkRect& srcRect, const SkRect& dstRect,
1618  float alpha, unsigned aaFlags);
1619 
1624 
1628  int fMatrixIndex = -1; // Index into the preViewMatrices arg, or < 0
1629  float fAlpha = 1.f;
1630  unsigned fAAFlags = kNone_QuadAAFlags; // QuadAAFlags
1631  bool fHasClip = false; // True to use next 4 points in dstClip arg as quad
1632  };
1633 
1653  void experimental_DrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], QuadAAFlags aaFlags,
1654  const SkColor4f& color, SkBlendMode mode);
1655  void experimental_DrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], QuadAAFlags aaFlags,
1656  SkColor color, SkBlendMode mode) {
1657  this->experimental_DrawEdgeAAQuad(rect, clip, aaFlags, SkColor4f::FromColor(color), mode);
1658  }
1659 
1688  void experimental_DrawEdgeAAImageSet(const ImageSetEntry imageSet[], int cnt,
1689  const SkPoint dstClips[], const SkMatrix preViewMatrices[],
1690  const SkSamplingOptions&, const SkPaint* paint = nullptr,
1691  SrcRectConstraint constraint = kStrict_SrcRectConstraint);
1692 
1720  void drawSimpleText(const void* text, size_t byteLength, SkTextEncoding encoding,
1721  SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint);
1722 
1747  void drawString(const char str[], SkScalar x, SkScalar y, const SkFont& font,
1748  const SkPaint& paint) {
1749  this->drawSimpleText(str, strlen(str), SkTextEncoding::kUTF8, x, y, font, paint);
1750  }
1751 
1776  void drawString(const SkString& str, SkScalar x, SkScalar y, const SkFont& font,
1777  const SkPaint& paint) {
1778  this->drawSimpleText(str.c_str(), str.size(), SkTextEncoding::kUTF8, x, y, font, paint);
1779  }
1780 
1803  void drawGlyphs(int count, const SkGlyphID glyphs[], const SkPoint positions[],
1804  const uint32_t clusters[], int textByteCount, const char utf8text[],
1805  SkPoint origin, const SkFont& font, const SkPaint& paint);
1806 
1825  void drawGlyphs(int count, const SkGlyphID glyphs[], const SkPoint positions[],
1826  SkPoint origin, const SkFont& font, const SkPaint& paint);
1827 
1847  void drawGlyphs(int count, const SkGlyphID glyphs[], const SkRSXform xforms[],
1848  SkPoint origin, const SkFont& font, const SkPaint& paint);
1849 
1873  void drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint);
1874 
1893  void drawTextBlob(const sk_sp<SkTextBlob>& blob, SkScalar x, SkScalar y, const SkPaint& paint) {
1894  this->drawTextBlob(blob.get(), x, y, paint);
1895  }
1896 
1905  void drawPicture(const SkPicture* picture) {
1906  this->drawPicture(picture, nullptr, nullptr);
1907  }
1908 
1917  void drawPicture(const sk_sp<SkPicture>& picture) {
1918  this->drawPicture(picture.get());
1919  }
1920 
1935  void drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint);
1936 
1949  void drawPicture(const sk_sp<SkPicture>& picture, const SkMatrix* matrix,
1950  const SkPaint* paint) {
1951  this->drawPicture(picture.get(), matrix, paint);
1952  }
1953 
1972  void drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint);
1973 
1992  void drawVertices(const sk_sp<SkVertices>& vertices, SkBlendMode mode, const SkPaint& paint);
1993 
1994 #if defined(SK_ENABLE_SKSL)
2014  void drawMesh(const SkMesh& mesh, sk_sp<SkBlender> blender, const SkPaint& paint);
2015 #endif
2016 
2046  void drawPatch(const SkPoint cubics[12], const SkColor colors[4],
2047  const SkPoint texCoords[4], SkBlendMode mode, const SkPaint& paint);
2048 
2074  void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
2075  const SkColor colors[], int count, SkBlendMode mode,
2076  const SkSamplingOptions& sampling, const SkRect* cullRect, const SkPaint* paint);
2077 
2091  void drawDrawable(SkDrawable* drawable, const SkMatrix* matrix = nullptr);
2092 
2106  void drawDrawable(SkDrawable* drawable, SkScalar x, SkScalar y);
2107 
2120  void drawAnnotation(const SkRect& rect, const char key[], SkData* value);
2121 
2132  void drawAnnotation(const SkRect& rect, const char key[], const sk_sp<SkData>& value) {
2133  this->drawAnnotation(rect, key, value.get());
2134  }
2135 
2146  virtual bool isClipEmpty() const;
2147 
2155  virtual bool isClipRect() const;
2156 
2163 
2168  return this->getLocalToDevice().asM33();
2169  }
2170 
2171 #ifdef SK_SUPPORT_LEGACY_GETTOTALMATRIX
2182 #endif
2183 
2185 
2186 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(SK_GANESH)
2187  // These methods exist to support WebView in Android Framework.
2188  SkIRect topLayerBounds() const;
2189  GrBackendRenderTarget topLayerBackendRenderTarget() const;
2190 #endif
2191 
2197 
2198  void private_draw_shadow_rec(const SkPath&, const SkDrawShadowRec&);
2199 
2200 
2201 protected:
2202  // default impl defers to getDevice()->newSurface(info)
2203  virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props);
2204 
2205  // default impl defers to its device
2206  virtual bool onPeekPixels(SkPixmap* pixmap);
2207  virtual bool onAccessTopLayerPixels(SkPixmap* pixmap);
2208  virtual SkImageInfo onImageInfo() const;
2209  virtual bool onGetProps(SkSurfaceProps* props, bool top) const;
2210  virtual void onFlush();
2211 
2212  // Subclass save/restore notifiers.
2213  // Overriders should call the corresponding INHERITED method up the inheritance chain.
2214  // getSaveLayerStrategy()'s return value may suppress full layer allocation.
2218  };
2219 
2220  virtual void willSave() {}
2221  // Overriders should call the corresponding INHERITED method up the inheritance chain.
2223  return kFullLayer_SaveLayerStrategy;
2224  }
2225 
2226  // returns true if we should actually perform the saveBehind, or false if we should just save.
2227  virtual bool onDoSaveBehind(const SkRect*) { return true; }
2228  virtual void willRestore() {}
2229  virtual void didRestore() {}
2230 
2231  virtual void didConcat44(const SkM44&) {}
2232  virtual void didSetM44(const SkM44&) {}
2233  virtual void didTranslate(SkScalar, SkScalar) {}
2234  virtual void didScale(SkScalar, SkScalar) {}
2235 
2236  // NOTE: If you are adding a new onDraw virtual to SkCanvas, PLEASE add an override to
2237  // SkCanvasVirtualEnforcer (in SkCanvasVirtualEnforcer.h). This ensures that subclasses using
2238  // that mechanism will be required to implement the new function.
2239  virtual void onDrawPaint(const SkPaint& paint);
2240  virtual void onDrawBehind(const SkPaint& paint);
2241  virtual void onDrawRect(const SkRect& rect, const SkPaint& paint);
2242  virtual void onDrawRRect(const SkRRect& rrect, const SkPaint& paint);
2243  virtual void onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint);
2244  virtual void onDrawOval(const SkRect& rect, const SkPaint& paint);
2245  virtual void onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
2246  bool useCenter, const SkPaint& paint);
2247  virtual void onDrawPath(const SkPath& path, const SkPaint& paint);
2248  virtual void onDrawRegion(const SkRegion& region, const SkPaint& paint);
2249 
2250  virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
2251  const SkPaint& paint);
2252 
2253  virtual void onDrawGlyphRunList(const sktext::GlyphRunList& glyphRunList, const SkPaint& paint);
2254 
2255  virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
2256  const SkPoint texCoords[4], SkBlendMode mode, const SkPaint& paint);
2257  virtual void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
2258  const SkPaint& paint);
2259 
2260  virtual void onDrawImage2(const SkImage*, SkScalar dx, SkScalar dy, const SkSamplingOptions&,
2261  const SkPaint*);
2262  virtual void onDrawImageRect2(const SkImage*, const SkRect& src, const SkRect& dst,
2263  const SkSamplingOptions&, const SkPaint*, SrcRectConstraint);
2264  virtual void onDrawImageLattice2(const SkImage*, const Lattice&, const SkRect& dst,
2265  SkFilterMode, const SkPaint*);
2266  virtual void onDrawAtlas2(const SkImage*, const SkRSXform[], const SkRect src[],
2267  const SkColor[], int count, SkBlendMode, const SkSamplingOptions&,
2268  const SkRect* cull, const SkPaint*);
2269  virtual void onDrawEdgeAAImageSet2(const ImageSetEntry imageSet[], int count,
2270  const SkPoint dstClips[], const SkMatrix preViewMatrices[],
2271  const SkSamplingOptions&, const SkPaint*,
2273 
2274  virtual void onDrawVerticesObject(const SkVertices* vertices, SkBlendMode mode,
2275  const SkPaint& paint);
2276 #ifdef SK_ENABLE_SKSL
2277  virtual void onDrawMesh(const SkMesh&, sk_sp<SkBlender>, const SkPaint&);
2278 #endif
2279  virtual void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value);
2280  virtual void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&);
2281 
2282  virtual void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix);
2283  virtual void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
2284  const SkPaint* paint);
2285 
2286  virtual void onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], QuadAAFlags aaFlags,
2287  const SkColor4f& color, SkBlendMode mode);
2288 
2291  kSoft_ClipEdgeStyle
2292  };
2293 
2294  virtual void onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle);
2295  virtual void onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle);
2296  virtual void onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle);
2298  virtual void onClipRegion(const SkRegion& deviceRgn, SkClipOp op);
2299  virtual void onResetClip();
2300 
2301  virtual void onDiscard();
2302 
2303 #if (defined(SK_GANESH) || defined(SK_GRAPHITE))
2306  virtual sk_sp<sktext::gpu::Slug> onConvertGlyphRunListToSlug(
2307  const sktext::GlyphRunList& glyphRunList, const SkPaint& paint);
2308 
2311  virtual void onDrawSlug(const sktext::gpu::Slug* slug);
2312 #endif
2313 
2314 private:
2315 
2316  enum ShaderOverrideOpacity {
2317  kNone_ShaderOverrideOpacity,
2318  kOpaque_ShaderOverrideOpacity,
2319  kNotOpaque_ShaderOverrideOpacity,
2320  };
2321 
2322  // notify our surface (if we have one) that we are about to draw, so it
2323  // can perform copy-on-write or invalidate any cached images
2324  // returns false if the copy failed
2325  bool SK_WARN_UNUSED_RESULT predrawNotify(bool willOverwritesEntireSurface = false);
2326  bool SK_WARN_UNUSED_RESULT predrawNotify(const SkRect*, const SkPaint*, ShaderOverrideOpacity);
2327 
2328  enum class CheckForOverwrite : bool {
2329  kNo = false,
2330  kYes = true
2331  };
2332  // call the appropriate predrawNotify and create a layer if needed.
2333  std::optional<AutoLayerForImageFilter> aboutToDraw(
2334  SkCanvas* canvas,
2335  const SkPaint& paint,
2336  const SkRect* rawBounds = nullptr,
2337  CheckForOverwrite = CheckForOverwrite::kNo,
2338  ShaderOverrideOpacity = kNone_ShaderOverrideOpacity);
2339 
2340  // The bottom-most device in the stack, only changed by init(). Image properties and the final
2341  // canvas pixels are determined by this device.
2342  SkBaseDevice* baseDevice() const {
2343  SkASSERT(fBaseDevice);
2344  return fBaseDevice.get();
2345  }
2346 
2347  // The top-most device in the stack, will change within saveLayer()'s. All drawing and clipping
2348  // operations should route to this device.
2349  SkBaseDevice* topDevice() const;
2350 
2351  // Canvases maintain a sparse stack of layers, where the top-most layer receives the drawing,
2352  // clip, and matrix commands. There is a layer per call to saveLayer() using the
2353  // kFullLayer_SaveLayerStrategy.
2354  struct Layer {
2355  sk_sp<SkBaseDevice> fDevice;
2356  sk_sp<SkImageFilter> fImageFilter; // applied to layer *before* being drawn by paint
2357  SkPaint fPaint;
2358  bool fDiscard;
2359 
2360  Layer(sk_sp<SkBaseDevice> device, sk_sp<SkImageFilter> imageFilter, const SkPaint& paint);
2361  };
2362 
2363  // Encapsulate state needed to restore from saveBehind()
2364  struct BackImage {
2365  // Out of line to avoid including SkSpecialImage.h
2366  BackImage(sk_sp<SkSpecialImage>, SkIPoint);
2367  BackImage(const BackImage&);
2368  BackImage(BackImage&&);
2369  BackImage& operator=(const BackImage&);
2370  ~BackImage();
2371 
2372  sk_sp<SkSpecialImage> fImage;
2373  SkIPoint fLoc;
2374  };
2375 
2376  class MCRec {
2377  public:
2378  // If not null, this MCRec corresponds with the saveLayer() record that made the layer.
2379  // The base "layer" is not stored here, since it is stored inline in SkCanvas and has no
2380  // restoration behavior.
2381  std::unique_ptr<Layer> fLayer;
2382 
2383  // This points to the device of the top-most layer (which may be lower in the stack), or
2384  // to the canvas's fBaseDevice. The MCRec does not own the device.
2385  SkBaseDevice* fDevice;
2386 
2387  std::unique_ptr<BackImage> fBackImage;
2388  SkM44 fMatrix;
2389  int fDeferredSaveCount = 0;
2390 
2391  MCRec(SkBaseDevice* device);
2392  MCRec(const MCRec* prev);
2393  ~MCRec();
2394 
2395  void newLayer(sk_sp<SkBaseDevice> layerDevice,
2396  sk_sp<SkImageFilter> filter,
2397  const SkPaint& restorePaint);
2398 
2399  void reset(SkBaseDevice* device);
2400  };
2401 
2402  // the first N recs that can fit here mean we won't call malloc
2403  static constexpr int kMCRecSize = 96; // most recent measurement
2404  static constexpr int kMCRecCount = 32; // common depth for save/restores
2405 
2406  intptr_t fMCRecStorage[kMCRecSize * kMCRecCount / sizeof(intptr_t)];
2407 
2408  SkDeque fMCStack;
2409  // points to top of stack
2410  MCRec* fMCRec;
2411 
2412  // Installed via init()
2413  sk_sp<SkBaseDevice> fBaseDevice;
2414  const SkSurfaceProps fProps;
2415 
2416  int fSaveCount; // value returned by getSaveCount()
2417 
2418  std::unique_ptr<SkRasterHandleAllocator> fAllocator;
2419 
2420  SkSurface_Base* fSurfaceBase;
2421  SkSurface_Base* getSurfaceBase() const { return fSurfaceBase; }
2422  void setSurfaceBase(SkSurface_Base* sb) {
2423  fSurfaceBase = sb;
2424  }
2425  friend class SkSurface_Base;
2426  friend class SkSurface_Ganesh;
2427 
2428  SkIRect fClipRestrictionRect = SkIRect::MakeEmpty();
2429  int fClipRestrictionSaveCount = -1;
2430 
2431  void doSave();
2432  void checkForDeferredSave();
2433  void internalSetMatrix(const SkM44&);
2434 
2435  friend class SkAndroidFrameworkUtils;
2436  friend class SkCanvasPriv; // needs to expose android functions for testing outside android
2437  friend class AutoLayerForImageFilter;
2438  friend class SkSurface_Raster; // needs getDevice()
2439  friend class SkNoDrawCanvas; // needs resetForNextPicture()
2440  friend class SkNWayCanvas;
2441  friend class SkPictureRecord; // predrawNotify (why does it need it? <reed>)
2442  friend class SkOverdrawCanvas;
2444  friend class SkRecords::Draw;
2445  template <typename Key>
2446  friend class SkTestCanvas;
2447 
2448 protected:
2449  // For use by SkNoDrawCanvas (via SkCanvasVirtualEnforcer, which can't be a friend)
2450  SkCanvas(const SkIRect& bounds);
2451 private:
2452  SkCanvas(const SkBitmap&, std::unique_ptr<SkRasterHandleAllocator>,
2454 
2455  SkCanvas(SkCanvas&&) = delete;
2456  SkCanvas(const SkCanvas&) = delete;
2457  SkCanvas& operator=(SkCanvas&&) = delete;
2458  SkCanvas& operator=(const SkCanvas&) = delete;
2459 
2460 #if (defined(SK_GANESH) || defined(SK_GRAPHITE))
2461  friend class sktext::gpu::Slug;
2465  sk_sp<sktext::gpu::Slug> convertBlobToSlug(const SkTextBlob& blob, SkPoint origin,
2466  const SkPaint& paint);
2467 
2471  void drawSlug(const sktext::gpu::Slug* slug);
2472 #endif
2473 
2482  int only_axis_aligned_saveBehind(const SkRect* subset);
2483 
2488  void drawClippedToSaveBehind(const SkPaint&);
2489 
2490  void resetForNextPicture(const SkIRect& bounds);
2491 
2492  // needs gettotalclip()
2493  friend class SkCanvasStateUtils;
2494 
2495  void init(sk_sp<SkBaseDevice>);
2496 
2497  // All base onDrawX() functions should call this and skip drawing if it returns true.
2498  // If 'matrix' is non-null, it maps the paint's fast bounds before checking for quick rejection
2499  bool internalQuickReject(const SkRect& bounds, const SkPaint& paint,
2500  const SkMatrix* matrix = nullptr);
2501 
2502  void internalDrawPaint(const SkPaint& paint);
2503  void internalSaveLayer(const SaveLayerRec&, SaveLayerStrategy);
2504  void internalSaveBehind(const SkRect*);
2505 
2506  void internalConcat44(const SkM44&);
2507 
2508  // shared by save() and saveLayer()
2509  void internalSave();
2510  void internalRestore();
2511 
2512  enum class DeviceCompatibleWithFilter : bool {
2513  // Check the src device's local-to-device matrix for compatibility with the filter, and if
2514  // it is not compatible, introduce an intermediate image and transformation that allows the
2515  // filter to be evaluated on the modified src content.
2516  kUnknown = false,
2517  // Assume that the src device's local-to-device matrix is compatible with the filter.
2518  kYes = true
2519  };
2531  void internalDrawDeviceWithFilter(SkBaseDevice* src, SkBaseDevice* dst,
2532  const SkImageFilter* filter, const SkPaint& paint,
2533  DeviceCompatibleWithFilter compat,
2534  SkScalar scaleFactor = 1.f);
2535 
2536  /*
2537  * Returns true if drawing the specified rect (or all if it is null) with the specified
2538  * paint (or default if null) would overwrite the entire root device of the canvas
2539  * (i.e. the canvas' surface if it had one).
2540  */
2541  bool wouldOverwriteEntireSurface(const SkRect*, const SkPaint*, ShaderOverrideOpacity) const;
2542 
2546  bool canDrawBitmapAsSprite(SkScalar x, SkScalar y, int w, int h, const SkSamplingOptions&,
2547  const SkPaint&);
2548 
2553  bool androidFramework_isClipAA() const;
2554 
2560  void internal_private_resetClip();
2561 
2562  virtual SkPaintFilterCanvas* internal_private_asPaintFilterCanvas() const { return nullptr; }
2563 
2564  // Keep track of the device clip bounds in the canvas' global space to reject draws before
2565  // invoking the top-level device.
2566  SkRect fQuickRejectBounds;
2567 
2568  // Compute the clip's bounds based on all clipped SkDevice's reported device bounds transformed
2569  // into the canvas' global space.
2570  SkRect computeDeviceClipBounds(bool outsetForAA=true) const;
2571 
2572  class AutoUpdateQRBounds;
2573  void validateClip() const;
2574 
2575  std::unique_ptr<sktext::GlyphRunBuilder> fScratchGlyphRunBuilder;
2576 
2577  using INHERITED = SkRefCnt;
2578 };
2579 
2586 public:
2587 
2594  SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas), fSaveCount(0) {
2595  if (fCanvas) {
2596  fSaveCount = canvas->getSaveCount();
2597  if (doSave) {
2598  canvas->save();
2599  }
2600  }
2601  }
2602 
2607  if (fCanvas) {
2608  fCanvas->restoreToCount(fSaveCount);
2609  }
2610  }
2611 
2615  void restore() {
2616  if (fCanvas) {
2617  fCanvas->restoreToCount(fSaveCount);
2618  fCanvas = nullptr;
2619  }
2620  }
2621 
2622 private:
2623  SkCanvas* fCanvas;
2624  int fSaveCount;
2625 
2627  SkAutoCanvasRestore(const SkAutoCanvasRestore&) = delete;
2628  SkAutoCanvasRestore& operator=(SkAutoCanvasRestore&&) = delete;
2629  SkAutoCanvasRestore& operator=(const SkAutoCanvasRestore&) = delete;
2630 };
2631 
2632 #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
sktext::gpu::Slug GrSlug
Definition: SkCanvas.h:84
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:179
Definition: GrBackendSurface.h:77
Definition: GrRecordingContext.h:45
Stack helper class calls SkCanvas::restoreToCount when SkAutoCanvasRestore goes out of scope.
Definition: SkCanvas.h:2585
void restore()
Restores SkCanvas to saved state immediately.
Definition: SkCanvas.h:2615
SkAutoCanvasRestore(SkCanvas *canvas, bool doSave)
Preserves SkCanvas::save() count.
Definition: SkCanvas.h:2594
~SkAutoCanvasRestore()
Restores SkCanvas to saved state.
Definition: SkCanvas.h:2606
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:108
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:165
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:1048
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 GrRecordingContext * recordingContext()
Returns GPU context of the GPU surface associated with SkCanvas.
virtual void onClipRRect(const SkRRect &rrect, SkClipOp op, ClipEdgeStyle edgeStyle)
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....
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:1505
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)
virtual skgpu::graphite::Recorder * recorder()
Returns Recorder for the GPU surface associated with SkCanvas.
void translate(SkScalar dx, SkScalar dy)
Translates SkMatrix by dx along the x-axis and dy along the y-axis.
void drawPoint(SkPoint p, const SkPaint &paint)
Draws point p using clip, SkMatrix and SkPaint paint.
Definition: SkCanvas.h:1271
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:956
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:1147
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:1917
void flush()
Triggers the immediate execution of all pending draw operations.
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:940
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:1173
SkMatrix getLocalToDeviceAs3x3() const
Throws away the 3rd row and column in the matrix, so be warned.
Definition: SkCanvas.h:2167
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:1189
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 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:1776
virtual void onDrawDRRect(const SkRRect &outer, const SkRRect &inner, const SkPaint &paint)
virtual void didScale(SkScalar, SkScalar)
Definition: SkCanvas.h:2234
virtual void didSetM44(const SkM44 &)
Definition: SkCanvas.h:2232
virtual void onFlush()
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:1110
virtual void didRestore()
Definition: SkCanvas.h:2229
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:994
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:2222
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:1034
void drawColor(const SkColor4f &color, SkBlendMode mode=SkBlendMode::kSrcOver)
Fills clip with color color.
int saveLayerAlpha(const SkRect *bounds, U8CPU alpha)
Definition: SkCanvas.h:671
void clipRect(const SkRect &rect, bool doAntiAlias=false)
Replaces clip with the intersection of clip and rect.
Definition: SkCanvas.h:952
SrcRectConstraint
Definition: SkCanvas.h:1485
@ kStrict_SrcRectConstraint
sample only inside bounds; slower
Definition: SkCanvas.h:1486
@ kFast_SrcRectConstraint
sample outside bounds; faster
Definition: SkCanvas.h:1487
int saveLayer(const SkRect &bounds, const SkPaint *paint)
Saves SkMatrix and clip, and allocates a SkSurface for subsequent drawing.
Definition: SkCanvas.h:643
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:2215
@ kFullLayer_SaveLayerStrategy
Definition: SkCanvas.h:2216
@ kNoLayer_SaveLayerStrategy
Definition: SkCanvas.h:2217
virtual void onClipRect(const SkRect &rect, SkClipOp op, ClipEdgeStyle edgeStyle)
virtual SkImageInfo onImageInfo() const
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:1323
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:1164
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:687
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:1500
bool getDeviceClipBounds(SkIRect *bounds) const
Returns SkIRect bounds of clip, unaffected by SkMatrix.
Definition: SkCanvas.h:1134
SkSurface * getSurface() const
Sometimes a canvas is owned by a surface.
virtual void willRestore()
Definition: SkCanvas.h:2228
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:1655
virtual void didTranslate(SkScalar, SkScalar)
Definition: SkCanvas.h:2233
void drawImage(const sk_sp< SkImage > &image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1475
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:1949
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:1005
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:680
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:2231
SkCanvas(sk_sp< SkBaseDevice > device)
Private.
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:1595
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:1410
void drawDRRect(const SkRRect &outer, const SkRRect &inner, const SkPaint &paint)
Draws SkRRect outer and inner using clip, SkMatrix, and SkPaint paint.
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:1492
ClipEdgeStyle
Definition: SkCanvas.h:2289
@ kHard_ClipEdgeStyle
Definition: SkCanvas.h:2290
virtual void onDrawGlyphRunList(const sktext::GlyphRunList &glyphRunList, const SkPaint &paint)
virtual bool onDoSaveBehind(const SkRect *)
Definition: SkCanvas.h:2227
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:1747
void drawPicture(const SkPicture *picture)
Draws SkPicture picture, using clip and SkMatrix.
Definition: SkCanvas.h:1905
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:2220
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:1299
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:1893
PointMode
Definition: SkCanvas.h:1205
@ kLines_PointMode
draw each pair of points as a line segment
Definition: SkCanvas.h:1207
@ kPolygon_PointMode
draw the array of points as a open polygon
Definition: SkCanvas.h:1208
@ kPoints_PointMode
draw each point separately
Definition: SkCanvas.h:1206
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:1472
QuadAAFlags
Experimental.
Definition: SkCanvas.h:1602
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:2132
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:26
Base class for image filters.
Definition: SkImageFilter.h:30
SkImage describes a two dimensional array of pixels to draw.
Definition: SkImage.h:255
4x4 matrix used by SkCanvas and other parts of Skia.
Definition: SkM44.h:146
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
Captures all drawing commands.
Definition: SkOverdrawCanvas.h:19
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:39
SkPixmap provides a utility to pair SkImageInfo with pixels and row bytes.
Definition: SkPixmap.h:41
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
Definition: SkRefCnt.h:119
SkRegion describes the set of pixels used to clip SkCanvas.
Definition: SkRegion.h:24
Shaders specify the source color(s) for what is being drawn.
Definition: SkShader.h:39
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:168
SkTextBlob combines multiple text runs into an immutable container.
Definition: SkTextBlob.h:32
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:1613
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:1626
ImageSetEntry(const ImageSetEntry &)
SkRect fDstRect
Definition: SkCanvas.h:1627
sk_sp< const SkImage > fImage
Definition: SkCanvas.h:1625
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:1545
int fYCount
number of y-coordinates
Definition: SkCanvas.h:1561
const SkIRect * fBounds
source bounds to draw from
Definition: SkCanvas.h:1562
RectType
Definition: SkCanvas.h:1551
@ kFixedColor
draws one of fColors into lattice rectangle
Definition: SkCanvas.h:1554
@ kTransparent
skips lattice rectangle by making it transparent
Definition: SkCanvas.h:1553
const int * fYDivs
y-axis values dividing bitmap
Definition: SkCanvas.h:1558
int fXCount
number of x-coordinates
Definition: SkCanvas.h:1560
const RectType * fRectTypes
array of fill types
Definition: SkCanvas.h:1559
const SkColor * fColors
array of colors
Definition: SkCanvas.h:1563
const int * fXDivs
x-axis values dividing bitmap
Definition: SkCanvas.h:1557
SaveLayerRec contains the state used to create the layer.
Definition: SkCanvas.h:692
SaveLayerRec(const SkRect *bounds, const SkPaint *paint, SaveLayerFlags saveLayerFlags=0)
Sets fBounds, fPaint, and fSaveLayerFlags; sets fBackdrop to nullptr.
Definition: SkCanvas.h:706
SaveLayerRec(const SkRect *bounds, const SkPaint *paint, const SkImageFilter *backdrop, SaveLayerFlags saveLayerFlags)
Sets fBounds, fPaint, fBackdrop, and fSaveLayerFlags.
Definition: SkCanvas.h:721
SaveLayerRec()
Sets fBounds, fPaint, and fBackdrop to nullptr.
Definition: SkCanvas.h:697
RGBA color value, holding four floating point components.
SkIPoint holds two 32-bit integer coordinates.
Definition: SkPoint.h:27
SkIRect holds four 32-bit integer coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:31
bool isEmpty() const
Returns true if width() or height() are zero or negative.
Definition: SkRect.h:203
static constexpr SkIRect SK_WARN_UNUSED_RESULT MakeEmpty()
Returns constructed SkIRect set to (0, 0, 0, 0).
Definition: SkRect.h:44
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,...
SkPoint holds two 32-bit floating point coordinates.
Definition: SkPoint.h:162
constexpr SkScalar y() const
Returns y-axis value of SkPoint or vector.
Definition: SkPoint.h:186
constexpr SkScalar x() const
Returns x-axis value of SkPoint or vector.
Definition: SkPoint.h:180
A compressed form of a rotation+scale matrix.
Definition: SkRSXform.h:21
SkRect holds four SkScalar coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:583
static SkRect Make(const SkISize &size)
Returns constructed SkIRect set to (0, 0, size.width(), size.height()).
Definition: SkRect.h:672
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:696
void set(const SkIRect &src)
Sets SkRect to src, promoting src members from integer to scalar.
Definition: SkRect.h:863
Definition: SkSamplingOptions.h:58