Skia
2DGraphicsLibrary
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkCanvas.h
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 
11 #include "SkBlendMode.h"
12 #include "SkClipOp.h"
13 #include "SkDeque.h"
14 #include "SkPaint.h"
15 #include "SkRasterHandleAllocator.h"
16 #include "SkSurfaceProps.h"
17 
18 class GrContext;
19 class GrRenderTargetContext;
20 class SkBaseDevice;
21 class SkBitmap;
22 class SkClipStack;
23 class SkData;
24 class SkDraw;
25 class SkDrawable;
26 class SkDrawFilter;
27 struct SkDrawShadowRec;
28 class SkImage;
29 class SkImageFilter;
30 class SkLights;
31 class SkMetaData;
32 class SkPath;
33 class SkPicture;
34 class SkPixmap;
35 class SkRasterClip;
36 class SkRegion;
37 class SkRRect;
38 struct SkRSXform;
39 class SkSurface;
40 class SkSurface_Base;
41 class SkTextBlob;
42 class SkVertices;
43 
59 class SK_API SkCanvas : SkNoncopyable {
60  enum PrivateSaveLayerFlags {
61  kDontClipToLayer_PrivateSaveLayerFlag = 1U << 31,
62  };
63 
64 public:
80  static std::unique_ptr<SkCanvas> MakeRasterDirect(const SkImageInfo& info, void* pixels,
81  size_t rowBytes);
82 
83  static std::unique_ptr<SkCanvas> MakeRasterDirectN32(int width, int height, SkPMColor* pixels,
84  size_t rowBytes) {
85  return MakeRasterDirect(SkImageInfo::MakeN32Premul(width, height), pixels, rowBytes);
86  }
87 
92  SkCanvas();
93 
99  SkCanvas(int width, int height, const SkSurfaceProps* props = NULL);
100 
105  explicit SkCanvas(SkBaseDevice* device);
106 
111  explicit SkCanvas(const SkBitmap& bitmap);
112 
113 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
114  enum class ColorBehavior {
115  kLegacy,
116  };
117 
123  SkCanvas(const SkBitmap& bitmap, ColorBehavior behavior);
124 #endif
125 
131  SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props);
132 
133  virtual ~SkCanvas();
134 
135  SkMetaData& getMetaData();
136 
141  SkImageInfo imageInfo() const;
142 
148  bool getProps(SkSurfaceProps* props) const;
149 
151 
157  void flush();
158 
164  virtual SkISize getBaseLayerSize() const;
165 
175  sk_sp<SkSurface> makeSurface(const SkImageInfo& info, const SkSurfaceProps* props = nullptr);
176 
181  virtual GrContext* getGrContext();
182 
184 
196  void* accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin = NULL);
197 
198  SkRasterHandleAllocator::Handle accessTopRasterHandle() const;
199 
211  bool peekPixels(SkPixmap* pixmap);
212 
232  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
233  int srcX, int srcY);
234  bool readPixels(const SkPixmap& pixmap, int srcX, int srcY);
235  bool readPixels(const SkBitmap& bitmap, int srcX, int srcY);
236 
255  bool writePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes, int x, int y);
256 
261  bool writePixels(const SkBitmap& bitmap, int x, int y);
262 
264 
274  int save();
275 
288  int saveLayer(const SkRect* bounds, const SkPaint* paint);
289  int saveLayer(const SkRect& bounds, const SkPaint* paint) {
290  return this->saveLayer(&bounds, paint);
291  }
292 
298  int saveLayerPreserveLCDTextRequests(const SkRect* bounds, const SkPaint* paint);
299 
311  int saveLayerAlpha(const SkRect* bounds, U8CPU alpha);
312 
313  enum {
314  kIsOpaque_SaveLayerFlag = 1 << 0,
315  kPreserveLCDText_SaveLayerFlag = 1 << 1,
316 
318  kInitWithPrevious_SaveLayerFlag = 1 << 2,
319 
320 #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
321  kDontClipToLayer_Legacy_SaveLayerFlag = kDontClipToLayer_PrivateSaveLayerFlag,
322 #endif
323  };
324  typedef uint32_t SaveLayerFlags;
325 
326  struct SaveLayerRec {
327  SaveLayerRec() {}
328  SaveLayerRec(const SkRect* bounds, const SkPaint* paint, SaveLayerFlags saveLayerFlags = 0)
329  : fBounds(bounds)
330  , fPaint(paint)
331  , fSaveLayerFlags(saveLayerFlags)
332  {}
333  SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop,
334  SaveLayerFlags saveLayerFlags)
335  : fBounds(bounds)
336  , fPaint(paint)
337  , fBackdrop(backdrop)
338  , fSaveLayerFlags(saveLayerFlags)
339  {}
340 
341  // EXPERIMENTAL: not ready for general use.
342  SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop,
343  const SkImage* clipMask, const SkMatrix* clipMatrix,
344  SaveLayerFlags saveLayerFlags)
345  : fBounds(bounds)
346  , fPaint(paint)
347  , fBackdrop(backdrop)
348  , fClipMask(clipMask)
349  , fClipMatrix(clipMatrix)
350  , fSaveLayerFlags(saveLayerFlags)
351  {}
352 
353  const SkRect* fBounds = nullptr; // optional
354  const SkPaint* fPaint = nullptr; // optional
355  const SkImageFilter* fBackdrop = nullptr; // optional
356  const SkImage* fClipMask = nullptr; // optional
357  const SkMatrix* fClipMatrix = nullptr; // optional -- only used with fClipMask
358  SaveLayerFlags fSaveLayerFlags = 0;
359  };
360 
361  int saveLayer(const SaveLayerRec& layerRec);
362 
368  void restore();
369 
374  int getSaveCount() const;
375 
382  void restoreToCount(int saveCount);
383 
388  void translate(SkScalar dx, SkScalar dy);
389 
394  void scale(SkScalar sx, SkScalar sy);
395 
399  void rotate(SkScalar degrees);
400 
406  void rotate(SkScalar degrees, SkScalar px, SkScalar py);
407 
412  void skew(SkScalar sx, SkScalar sy);
413 
417  void concat(const SkMatrix& matrix);
418 
422  void setMatrix(const SkMatrix& matrix);
423 
426  void resetMatrix();
427 
434  void clipRect(const SkRect& rect, SkClipOp op, bool doAntiAlias);
435  void clipRect(const SkRect& rect, SkClipOp op) {
436  this->clipRect(rect, op, false);
437  }
438  void clipRect(const SkRect& rect, bool doAntiAlias = false) {
439  this->clipRect(rect, SkClipOp::kIntersect, doAntiAlias);
440  }
441 
451  void androidFramework_setDeviceClipRestriction(const SkIRect& rect);
452 
459  void clipRRect(const SkRRect& rrect, SkClipOp op, bool doAntiAlias);
460  void clipRRect(const SkRRect& rrect, SkClipOp op) {
461  this->clipRRect(rrect, op, false);
462  }
463  void clipRRect(const SkRRect& rrect, bool doAntiAlias = false) {
464  this->clipRRect(rrect, SkClipOp::kIntersect, doAntiAlias);
465  }
466 
473  void clipPath(const SkPath& path, SkClipOp op, bool doAntiAlias);
474  void clipPath(const SkPath& path, SkClipOp op) {
475  this->clipPath(path, op, false);
476  }
477  void clipPath(const SkPath& path, bool doAntiAlias = false) {
478  this->clipPath(path, SkClipOp::kIntersect, doAntiAlias);
479  }
480 
484  void setAllowSimplifyClip(bool allow) {
485  fAllowSimplifyClip = allow;
486  }
487 
495  void clipRegion(const SkRegion& deviceRgn, SkClipOp op = SkClipOp::kIntersect);
496 
505  bool quickReject(const SkRect& rect) const;
506 
517  bool quickReject(const SkPath& path) const;
518 
523  SkRect getLocalClipBounds() const { return this->onGetLocalClipBounds(); }
524 
528  bool getLocalClipBounds(SkRect* bounds) const {
529  *bounds = this->onGetLocalClipBounds();
530  return !bounds->isEmpty();
531  }
532 
537  SkIRect getDeviceClipBounds() const { return this->onGetDeviceClipBounds(); }
538 
542  bool getDeviceClipBounds(SkIRect* bounds) const {
543  *bounds = this->onGetDeviceClipBounds();
544  return !bounds->isEmpty();
545  }
546 
552  void drawColor(SkColor color, SkBlendMode mode = SkBlendMode::kSrcOver);
553 
558  void clear(SkColor color) {
559  this->drawColor(color, SkBlendMode::kSrc);
560  }
561 
574  void discard() { this->onDiscard(); }
575 
581  void drawPaint(const SkPaint& paint);
582 
583  enum PointMode {
589  kPolygon_PointMode
590  };
591 
613  void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint);
614 
617  void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint);
618  void drawPoint(SkPoint p, const SkPaint& paint) {
619  this->drawPoint(p.x(), p.y(), paint);
620  }
621 
631  void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint& paint);
632  void drawLine(SkPoint p0, SkPoint p1, const SkPaint& paint) {
633  this->drawLine(p0.x(), p0.y(), p1.x(), p1.y(), paint);
634  }
635 
641  void drawRect(const SkRect& rect, const SkPaint& paint);
642 
648  void drawIRect(const SkIRect& rect, const SkPaint& paint) {
649  SkRect r;
650  r.set(rect); // promotes the ints to scalars
651  this->drawRect(r, paint);
652  }
653 
658  void drawRegion(const SkRegion& region, const SkPaint& paint);
659 
665  void drawOval(const SkRect& oval, const SkPaint& paint);
666 
674  void drawRRect(const SkRRect& rrect, const SkPaint& paint);
675 
680  void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint);
681 
690  void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint& paint);
691  void drawCircle(SkPoint center, SkScalar radius, const SkPaint& paint) {
692  this->drawCircle(center.x(), center.y(), radius, paint);
693  }
694 
709  void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
710  bool useCenter, const SkPaint& paint);
711 
719  void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry, const SkPaint& paint);
720 
726  void drawPath(const SkPath& path, const SkPaint& paint);
727 
736  void drawImage(const SkImage* image, SkScalar left, SkScalar top, const SkPaint* paint = NULL);
737  void drawImage(const sk_sp<SkImage>& image, SkScalar left, SkScalar top,
738  const SkPaint* paint = NULL) {
739  this->drawImage(image.get(), left, top, paint);
740  }
741 
762 
769  };
770 
782  void drawImageRect(const SkImage* image, const SkRect& src, const SkRect& dst,
783  const SkPaint* paint,
784  SrcRectConstraint constraint = kStrict_SrcRectConstraint);
785  // variant that takes src SkIRect
786  void drawImageRect(const SkImage* image, const SkIRect& isrc, const SkRect& dst,
787  const SkPaint* paint,
788  SrcRectConstraint constraint = kStrict_SrcRectConstraint);
789  // variant that assumes src == image-bounds
790  void drawImageRect(const SkImage* image, const SkRect& dst, const SkPaint* paint,
791  SrcRectConstraint constraint = kStrict_SrcRectConstraint);
792 
793  void drawImageRect(const sk_sp<SkImage>& image, const SkRect& src, const SkRect& dst,
794  const SkPaint* paint,
795  SrcRectConstraint constraint = kStrict_SrcRectConstraint) {
796  this->drawImageRect(image.get(), src, dst, paint, constraint);
797  }
798  void drawImageRect(const sk_sp<SkImage>& image, const SkIRect& isrc, const SkRect& dst,
799  const SkPaint* paint,
800  SrcRectConstraint constraint = kStrict_SrcRectConstraint) {
801  this->drawImageRect(image.get(), isrc, dst, paint, constraint);
802  }
803  void drawImageRect(const sk_sp<SkImage>& image, const SkRect& dst, const SkPaint* paint,
804  SrcRectConstraint constraint = kStrict_SrcRectConstraint) {
805  this->drawImageRect(image.get(), dst, paint, constraint);
806  }
807 
822  void drawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst,
823  const SkPaint* paint = nullptr);
824  void drawImageNine(const sk_sp<SkImage>& image, const SkIRect& center, const SkRect& dst,
825  const SkPaint* paint = nullptr) {
826  this->drawImageNine(image.get(), center, dst, paint);
827  }
828 
845  void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
846  const SkPaint* paint = NULL);
847 
859  void drawBitmapRect(const SkBitmap& bitmap, const SkRect& src, const SkRect& dst,
860  const SkPaint* paint,
861  SrcRectConstraint constraint = kStrict_SrcRectConstraint);
862  // variant where src is SkIRect
863  void drawBitmapRect(const SkBitmap& bitmap, const SkIRect& isrc, const SkRect& dst,
864  const SkPaint* paint,
865  SrcRectConstraint constraint = kStrict_SrcRectConstraint);
866  void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst, const SkPaint* paint,
867  SrcRectConstraint constraint = kStrict_SrcRectConstraint);
868 
883  void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
884  const SkPaint* paint = NULL);
885 
892  struct Lattice {
893  enum Flags : uint8_t {
894  // If set, indicates that we should not draw corresponding rect.
895  kTransparent_Flags = 1 << 0,
896  };
897 
898  // An array of x-coordinates that divide the bitmap vertically.
899  // These must be unique, increasing, and in the set [fBounds.fLeft, fBounds.fRight).
900  // Does not have ownership.
901  const int* fXDivs;
902 
903  // An array of y-coordinates that divide the bitmap horizontally.
904  // These must be unique, increasing, and in the set [fBounds.fTop, fBounds.fBottom).
905  // Does not have ownership.
906  const int* fYDivs;
907 
908  // If non-null, the length of this array must be equal to
909  // (fXCount + 1) * (fYCount + 1). Note that we allow the first rect
910  // in each direction to be empty (ex: fXDivs[0] = fBounds.fLeft).
911  // In this case, the caller still must specify a flag (as a placeholder)
912  // for these empty rects.
913  // The flags correspond to the rects in the lattice, first moving
914  // left to right and then top to bottom.
915  const Flags* fFlags;
916 
917  // The number of fXDivs.
918  int fXCount;
919 
920  // The number of fYDivs.
921  int fYCount;
922 
923  // The bound to draw from. Must be contained by the src that is being drawn,
924  // non-empty, and non-inverted.
925  // If nullptr, the bounds are the entire src.
926  const SkIRect* fBounds;
927  };
928 
944  void drawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, const SkRect& dst,
945  const SkPaint* paint = nullptr);
946  void drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst,
947  const SkPaint* paint = nullptr);
948 
957  void drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
958  const SkPaint& paint);
959 
967  void drawString(const char* string, SkScalar x, SkScalar y, const SkPaint& paint) {
968  if (!string) {
969  return;
970  }
971  this->drawText(string, strlen(string), x, y, paint);
972  }
973 
981  void drawString(const SkString& string, SkScalar x, SkScalar y, const SkPaint& paint);
982 
990  void drawPosText(const void* text, size_t byteLength, const SkPoint pos[],
991  const SkPaint& paint);
992 
1002  void drawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], SkScalar constY,
1003  const SkPaint& paint);
1004 
1017  void drawTextOnPathHV(const void* text, size_t byteLength, const SkPath& path, SkScalar hOffset,
1018  SkScalar vOffset, const SkPaint& paint);
1019 
1030  void drawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
1031  const SkMatrix* matrix, const SkPaint& paint);
1032 
1038  void drawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
1039  const SkRect* cullRect, const SkPaint& paint);
1040 
1047  void drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint);
1048  void drawTextBlob(const sk_sp<SkTextBlob>& blob, SkScalar x, SkScalar y, const SkPaint& paint) {
1049  this->drawTextBlob(blob.get(), x, y, paint);
1050  }
1051 
1058  void drawPicture(const SkPicture* picture) {
1059  this->drawPicture(picture, NULL, NULL);
1060  }
1061  void drawPicture(const sk_sp<SkPicture>& picture) {
1062  this->drawPicture(picture.get());
1063  }
1064 
1077  void drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint);
1078  void drawPicture(const sk_sp<SkPicture>& picture, const SkMatrix* matrix, const SkPaint* paint) {
1079  this->drawPicture(picture.get(), matrix, paint);
1080  }
1081 
1090  void drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint);
1091  void drawVertices(const sk_sp<SkVertices>& vertices, SkBlendMode mode, const SkPaint& paint);
1092 
1106  void drawPatch(const SkPoint cubics[12], const SkColor colors[4],
1107  const SkPoint texCoords[4], SkBlendMode mode, const SkPaint& paint);
1108  void drawPatch(const SkPoint cubics[12], const SkColor colors[4],
1109  const SkPoint texCoords[4], const SkPaint& paint) {
1110  this->drawPatch(cubics, colors, texCoords, SkBlendMode::kModulate, paint);
1111  }
1112 
1130  void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
1131  const SkColor colors[], int count, SkBlendMode mode, const SkRect* cullRect,
1132  const SkPaint* paint);
1133  void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[],
1134  const SkColor colors[], int count, SkBlendMode mode, const SkRect* cullRect,
1135  const SkPaint* paint) {
1136  this->drawAtlas(atlas.get(), xform, tex, colors, count, mode, cullRect, paint);
1137  }
1138  void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], int count,
1139  const SkRect* cullRect, const SkPaint* paint) {
1140  this->drawAtlas(atlas, xform, tex, nullptr, count, SkBlendMode::kDst, cullRect, paint);
1141  }
1142  void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[],
1143  int count, const SkRect* cullRect, const SkPaint* paint) {
1144  this->drawAtlas(atlas.get(), xform, tex, nullptr, count, SkBlendMode::kDst,
1145  cullRect, paint);
1146  }
1147 
1156  void drawDrawable(SkDrawable* drawable, const SkMatrix* matrix = NULL);
1157  void drawDrawable(SkDrawable* drawable, SkScalar x, SkScalar y);
1158 
1169  void drawAnnotation(const SkRect& rect, const char key[], SkData* value);
1170  void drawAnnotation(const SkRect& rect, const char key[], const sk_sp<SkData>& value) {
1171  this->drawAnnotation(rect, key, value.get());
1172  }
1173 
1175 
1176 #ifdef SK_SUPPORT_LEGACY_DRAWFILTER
1177 
1181  SkDrawFilter* getDrawFilter() const;
1182 
1191  virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter);
1192 #endif
1193 
1202  virtual bool isClipEmpty() const;
1203 
1208  virtual bool isClipRect() const;
1209 
1214  const SkMatrix& getTotalMatrix() const;
1215 
1217 
1218  // don't call
1219  GrRenderTargetContext* internal_private_accessTopLayerRenderTargetContext();
1220 
1221  // don't call
1222  static void Internal_Private_SetIgnoreSaveLayerBounds(bool);
1223  static bool Internal_Private_GetIgnoreSaveLayerBounds();
1224  static void Internal_Private_SetTreatSpriteAsBitmap(bool);
1225  static bool Internal_Private_GetTreatSpriteAsBitmap();
1226 
1227  // TEMP helpers until we switch virtual over to const& for src-rect
1228  void legacy_drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1229  const SkPaint* paint,
1230  SrcRectConstraint constraint = kStrict_SrcRectConstraint);
1231  void legacy_drawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
1232  const SkPaint* paint,
1233  SrcRectConstraint constraint = kStrict_SrcRectConstraint);
1234 
1239  void temporary_internal_getRgnClip(SkRegion* region);
1240 
1241  void private_draw_shadow_rec(const SkPath&, const SkDrawShadowRec&);
1242 
1243 protected:
1244  // default impl defers to getDevice()->newSurface(info)
1245  virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props);
1246 
1247  // default impl defers to its device
1248  virtual bool onPeekPixels(SkPixmap* pixmap);
1249  virtual bool onAccessTopLayerPixels(SkPixmap* pixmap);
1250  virtual SkImageInfo onImageInfo() const;
1251  virtual bool onGetProps(SkSurfaceProps* props) const;
1252  virtual void onFlush();
1253 
1254  // Subclass save/restore notifiers.
1255  // Overriders should call the corresponding INHERITED method up the inheritance chain.
1256  // getSaveLayerStrategy()'s return value may suppress full layer allocation.
1257  enum SaveLayerStrategy {
1258  kFullLayer_SaveLayerStrategy,
1259  kNoLayer_SaveLayerStrategy,
1260  };
1261 
1262  virtual void willSave() {}
1263  // Overriders should call the corresponding INHERITED method up the inheritance chain.
1264  virtual SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& ) {
1265  return kFullLayer_SaveLayerStrategy;
1266  }
1267  virtual void willRestore() {}
1268  virtual void didRestore() {}
1269  virtual void didConcat(const SkMatrix& ) {}
1270  virtual void didSetMatrix(const SkMatrix& ) {}
1271  virtual void didTranslate(SkScalar dx, SkScalar dy) {
1272  this->didConcat(SkMatrix::MakeTrans(dx, dy));
1273  }
1274 
1275  virtual SkRect onGetLocalClipBounds() const;
1276  virtual SkIRect onGetDeviceClipBounds() const;
1277 
1278 
1279  virtual void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value);
1280  virtual void onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint);
1281 
1282  virtual void onDrawText(const void* text, size_t byteLength, SkScalar x,
1283  SkScalar y, const SkPaint& paint);
1284 
1285  virtual void onDrawPosText(const void* text, size_t byteLength,
1286  const SkPoint pos[], const SkPaint& paint);
1287 
1288  virtual void onDrawPosTextH(const void* text, size_t byteLength,
1289  const SkScalar xpos[], SkScalar constY,
1290  const SkPaint& paint);
1291 
1292  virtual void onDrawTextOnPath(const void* text, size_t byteLength,
1293  const SkPath& path, const SkMatrix* matrix,
1294  const SkPaint& paint);
1295  virtual void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
1296  const SkRect* cullRect, const SkPaint& paint);
1297 
1298  virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1299  const SkPaint& paint);
1300 
1301  virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
1302  const SkPoint texCoords[4], SkBlendMode mode, const SkPaint& paint);
1303 
1304  virtual void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix);
1305 
1306  virtual void onDrawPaint(const SkPaint& paint);
1307  virtual void onDrawRect(const SkRect& rect, const SkPaint& paint);
1308  virtual void onDrawRegion(const SkRegion& region, const SkPaint& paint);
1309  virtual void onDrawOval(const SkRect& rect, const SkPaint& paint);
1310  virtual void onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
1311  bool useCenter, const SkPaint& paint);
1312  virtual void onDrawRRect(const SkRRect& rrect, const SkPaint& paint);
1313  virtual void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
1314  const SkPaint& paint);
1315  virtual void onDrawVerticesObject(const SkVertices* vertices, SkBlendMode mode,
1316  const SkPaint& paint);
1317  virtual void onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect rect[],
1318  const SkColor colors[], int count, SkBlendMode mode,
1319  const SkRect* cull, const SkPaint* paint);
1320  virtual void onDrawPath(const SkPath& path, const SkPaint& paint);
1321  virtual void onDrawImage(const SkImage* image, SkScalar dx, SkScalar dy, const SkPaint* paint);
1322  virtual void onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1323  const SkPaint* paint, SrcRectConstraint constraint);
1324  virtual void onDrawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst,
1325  const SkPaint* paint);
1326  virtual void onDrawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst,
1327  const SkPaint* paint);
1328 
1329  virtual void onDrawBitmap(const SkBitmap& bitmap, SkScalar dx, SkScalar dy,
1330  const SkPaint* paint);
1331  virtual void onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
1332  const SkPaint* paint, SrcRectConstraint constraint);
1333  virtual void onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
1334  const SkPaint* paint);
1335  virtual void onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice,
1336  const SkRect& dst, const SkPaint* paint);
1337  virtual void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&);
1338 
1339  enum ClipEdgeStyle {
1340  kHard_ClipEdgeStyle,
1341  kSoft_ClipEdgeStyle
1342  };
1343 
1344  virtual void onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle);
1345  virtual void onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle);
1346  virtual void onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle);
1347  virtual void onClipRegion(const SkRegion& deviceRgn, SkClipOp op);
1348 
1349  virtual void onDiscard();
1350 
1351  virtual void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
1352  const SkPaint* paint);
1353 
1354  // Clip rectangle bounds. Called internally by saveLayer.
1355  // returns false if the entire rectangle is entirely clipped out
1356  // If non-NULL, The imageFilter parameter will be used to expand the clip
1357  // and offscreen bounds for any margin required by the filter DAG.
1358  bool clipRectBounds(const SkRect* bounds, SaveLayerFlags flags, SkIRect* intersection,
1359  const SkImageFilter* imageFilter = NULL);
1360 
1361 private:
1368  class LayerIter /*: SkNoncopyable*/ {
1369  public:
1371  LayerIter(SkCanvas*);
1372  ~LayerIter();
1373 
1375  bool done() const { return fDone; }
1377  void next();
1378 
1379  // These reflect the current device in the iterator
1380 
1381  SkBaseDevice* device() const;
1382  const SkMatrix& matrix() const;
1383  void clip(SkRegion*) const;
1384  const SkPaint& paint() const;
1385  int x() const;
1386  int y() const;
1387 
1388  private:
1389  // used to embed the SkDrawIter object directly in our instance, w/o
1390  // having to expose that class def to the public. There is an assert
1391  // in our constructor to ensure that fStorage is large enough
1392  // (though needs to be a compile-time-assert!). We use intptr_t to work
1393  // safely with 32 and 64 bit machines (to ensure the storage is enough)
1394  intptr_t fStorage[32];
1395  class SkDrawIter* fImpl; // this points at fStorage
1396  SkPaint fDefaultPaint;
1397  bool fDone;
1398  };
1399 
1400  static bool BoundsAffectsClip(SaveLayerFlags);
1401  static SaveLayerFlags LegacySaveFlagsToSaveLayerFlags(uint32_t legacySaveFlags);
1402 
1403  static void DrawDeviceWithFilter(SkBaseDevice* src, const SkImageFilter* filter,
1404  SkBaseDevice* dst, const SkIPoint& dstOrigin,
1405  const SkMatrix& ctm);
1406 
1411  };
1412 
1413  // notify our surface (if we have one) that we are about to draw, so it
1414  // can perform copy-on-write or invalidate any cached images
1415  void predrawNotify(bool willOverwritesEntireSurface = false);
1416  void predrawNotify(const SkRect* rect, const SkPaint* paint, ShaderOverrideOpacity);
1417  void predrawNotify(const SkRect* rect, const SkPaint* paint, bool shaderOverrideIsOpaque) {
1418  this->predrawNotify(rect, paint, shaderOverrideIsOpaque ? kOpaque_ShaderOverrideOpacity
1419  : kNotOpaque_ShaderOverrideOpacity);
1420  }
1421 
1422  SkBaseDevice* getDevice() const;
1423  SkBaseDevice* getTopDevice() const;
1424 
1425  class MCRec;
1426 
1427  SkDeque fMCStack;
1428  // points to top of stack
1429  MCRec* fMCRec;
1430  // the first N recs that can fit here mean we won't call malloc
1431  enum {
1432  kMCRecSize = 128, // most recent measurement
1433  kMCRecCount = 32, // common depth for save/restores
1434  kDeviceCMSize = 224, // most recent measurement
1435  };
1436  intptr_t fMCRecStorage[kMCRecSize * kMCRecCount / sizeof(intptr_t)];
1437  intptr_t fDeviceCMStorage[kDeviceCMSize / sizeof(intptr_t)];
1438 
1439  const SkSurfaceProps fProps;
1440 
1441  int fSaveCount; // value returned by getSaveCount()
1442 
1443  SkMetaData* fMetaData;
1444  std::unique_ptr<SkRasterHandleAllocator> fAllocator;
1445 
1446  SkSurface_Base* fSurfaceBase;
1447  SkSurface_Base* getSurfaceBase() const { return fSurfaceBase; }
1448  void setSurfaceBase(SkSurface_Base* sb) {
1449  fSurfaceBase = sb;
1450  }
1451  friend class SkSurface_Base;
1452  friend class SkSurface_Gpu;
1453 
1454  SkIRect fClipRestrictionRect = SkIRect::MakeEmpty();
1455 
1456  void doSave();
1457  void checkForDeferredSave();
1458  void internalSetMatrix(const SkMatrix&);
1459 
1460  friend class SkDrawIter; // needs setupDrawForLayerDevice()
1461  friend class AutoDrawLooper;
1462  friend class SkDebugCanvas; // needs experimental fAllowSimplifyClip
1463  friend class SkSurface_Raster; // needs getDevice()
1464  friend class SkNoDrawCanvas; // InitFlags
1465  friend class SkPictureImageFilter; // SkCanvas(SkBaseDevice*, SkSurfaceProps*, InitFlags)
1466  friend class SkPictureRecord; // predrawNotify (why does it need it? <reed>)
1467  friend class SkPicturePlayback; // SaveFlagsToSaveLayerFlags
1468  friend class SkOverdrawCanvas;
1469  friend class SkRasterHandleAllocator;
1470 
1471  enum InitFlags {
1472  kDefault_InitFlags = 0,
1473  kConservativeRasterClip_InitFlag = 1 << 0,
1474  };
1475  SkCanvas(const SkIRect& bounds, InitFlags);
1476  SkCanvas(SkBaseDevice* device, InitFlags);
1477  SkCanvas(const SkBitmap&, std::unique_ptr<SkRasterHandleAllocator>,
1478  SkRasterHandleAllocator::Handle);
1479 
1480  void resetForNextPicture(const SkIRect& bounds);
1481 
1482  // needs gettotalclip()
1483  friend class SkCanvasStateUtils;
1484 
1485  // call this each time we attach ourselves to a device
1486  // - constructor
1487  // - internalSaveLayer
1488  void setupDevice(SkBaseDevice*);
1489 
1490  SkBaseDevice* init(SkBaseDevice*, InitFlags);
1491 
1496  SkIRect getTopLayerBounds() const;
1497 
1498  void internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src,
1499  const SkRect& dst, const SkPaint* paint,
1500  SrcRectConstraint);
1501  void internalDrawPaint(const SkPaint& paint);
1502  void internalSaveLayer(const SaveLayerRec&, SaveLayerStrategy);
1503  void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*, SkImage* clipImage,
1504  const SkMatrix& clipMatrix);
1505 
1506  // shared by save() and saveLayer()
1507  void internalSave();
1508  void internalRestore();
1509 
1510  /*
1511  * Returns true if drawing the specified rect (or all if it is null) with the specified
1512  * paint (or default if null) would overwrite the entire root device of the canvas
1513  * (i.e. the canvas' surface if it had one).
1514  */
1515  bool wouldOverwriteEntireSurface(const SkRect*, const SkPaint*, ShaderOverrideOpacity) const;
1516 
1520  bool canDrawBitmapAsSprite(SkScalar x, SkScalar y, int w, int h, const SkPaint&);
1521 
1526  bool androidFramework_isClipAA() const;
1527 
1533  SkRect fDeviceClipBounds;
1534 
1535  bool fAllowSoftClip;
1536  bool fAllowSimplifyClip;
1537 
1538  class AutoValidateClip : ::SkNoncopyable {
1539  public:
1540  explicit AutoValidateClip(SkCanvas* canvas) : fCanvas(canvas) {
1541  fCanvas->validateClip();
1542  }
1543  ~AutoValidateClip() { fCanvas->validateClip(); }
1544 
1545  private:
1546  const SkCanvas* fCanvas;
1547  };
1548 
1549 #ifdef SK_DEBUG
1550  void validateClip() const;
1551 #else
1552  void validateClip() const {}
1553 #endif
1554 
1555  typedef SkRefCnt INHERITED;
1556 };
1557 
1562 class SkAutoCanvasRestore : SkNoncopyable {
1563 public:
1564  SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas), fSaveCount(0) {
1565  if (fCanvas) {
1566  fSaveCount = canvas->getSaveCount();
1567  if (doSave) {
1568  canvas->save();
1569  }
1570  }
1571  }
1572  ~SkAutoCanvasRestore() {
1573  if (fCanvas) {
1574  fCanvas->restoreToCount(fSaveCount);
1575  }
1576  }
1577 
1582  void restore() {
1583  if (fCanvas) {
1584  fCanvas->restoreToCount(fSaveCount);
1585  fCanvas = NULL;
1586  }
1587  }
1588 
1589 private:
1590  SkCanvas* fCanvas;
1591  int fSaveCount;
1592 };
1593 #define SkAutoCanvasRestore(...) SK_REQUIRE_LOCAL_VAR(SkAutoCanvasRestore)
1594 
1595 #endif
bool getLocalClipBounds(SkRect *bounds) const
Returns true if the clip bounds are non-empty.
Definition: SkCanvas.h:528
void discard()
This makes the contents of the canvas undefined.
Definition: SkCanvas.h:574
A Canvas encapsulates all of the state about drawing into a device (bitmap).
Definition: SkCanvas.h:59
bool getDeviceClipBounds(SkIRect *bounds) const
Returns true if the clip bounds are non-empty.
Definition: SkCanvas.h:542
Definition: SkCanvas.h:326
Definition: SkPictureImageFilter.h:14
int getSaveCount() const
Returns the number of matrix/clip states on the SkCanvas' private stack.
The SkRRect class represents a rounded rect with a potentially different radii for each corner...
Definition: SkRRect.h:47
The SkPath class encapsulates compound (multiple contour) geometric paths consisting of straight line...
Definition: SkPath.h:25
Describe an image's dimensions and pixel type.
Definition: SkImageInfo.h:181
SkTextBlob combines multiple text runs into an immutable, ref-counted structure.
Definition: SkTextBlob.h:24
If kFast is specified, the implementation may sample outside of the src-rect (if specified) by half t...
Definition: SkCanvas.h:768
After calling saveLayer(), there can be any number of devices that make up the top-most drawing area...
Definition: SkCanvas.h:1368
SkIRect getDeviceClipBounds() const
Return the bounds of the current clip in device coordinates.
Definition: SkCanvas.h:537
SkRect getLocalClipBounds() const
Return the bounds of the current clip in local coordinates.
Definition: SkCanvas.h:523
bool fIsScaleTranslate
Keep track of the device clip bounds and if the matrix is scale-translate.
Definition: SkCanvas.h:1532
PointMode
Definition: SkCanvas.h:583
Describes properties and constraints of a given SkSurface.
Definition: SkSurfaceProps.h:51
Pairs SkImageInfo with actual pixels and rowbytes.
Definition: SkPixmap.h:23
Definition: SkPoint.h:156
void setAllowSimplifyClip(bool allow)
EXPERIMENTAL – only used for testing Set to simplify clip stack using path ops.
Definition: SkCanvas.h:484
there is no overriding shader (bitmap or image)
Definition: SkCanvas.h:1408
The SkMatrix class holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:28
SkSurface is responsible for managing the pixels that a canvas draws into.
Definition: SkSurface.h:32
Definition: SkRefCnt.h:125
The SkPaint class holds the style and color information about how to draw geometries, text and bitmaps.
Definition: SkPaint.h:45
Definition: SkLights.h:20
void drawPicture(const SkPicture *picture)
Draw the picture into this canvas.
Definition: SkCanvas.h:1058
ShaderOverrideOpacity
Definition: SkCanvas.h:1407
An SkPicture records drawing commands made to a canvas to be played back at a later time...
Definition: SkPicture.h:38
Definition: SkMetaData.h:17
Definition: SkCanvas.h:1538
SkData holds an immutable data buffer.
Definition: SkData.h:22
static SkImageInfo MakeN32Premul(int width, int height, sk_sp< SkColorSpace > cs=nullptr)
Sets colortype to the native ARGB32 type, and the alphatype to premul.
Definition: SkImageInfo.h:212
bool done() const
Return true if the iterator is done.
Definition: SkCanvas.h:1375
A compressed form of a rotation+scale matrix.
Definition: SkRSXform.h:21
the overriding shader may not be opaque
Definition: SkCanvas.h:1410
Specifies coordinates to divide a bitmap into (xCount*yCount) rects.
Definition: SkCanvas.h:892
void clear(SkColor color)
Helper method for drawing a color in SRC mode, completely replacing all the pixels in the current cli...
Definition: SkCanvas.h:558
The SkBitmap class specifies a raster bitmap.
Definition: SkBitmap.h:37
Base class for image filters.
Definition: SkImageFilter.h:36
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:246
bool isEmpty() const
Return true if the rectangle's width or height are <= 0.
Definition: SkRect.h:103
drawPoints draws each pair of points as a line segment
Definition: SkCanvas.h:587
DEPRECATED - use SkPaintFilterCanvas instead.
Definition: SkDrawFilter.h:26
Definition: SkDeque.h:26
Definition: GrContext.h:47
Definition: SkSize.h:13
SrcRectConstraint
Controls the behavior at the edge of the src-rect, when specified in drawImageRect, trading off speed for exactness.
Definition: SkCanvas.h:755
The SkRegion class encapsulates the geometric region used to specify clipping areas for drawing...
Definition: SkRegion.h:30
An immutable set of vertex data that can be used with SkCanvas::drawVertices.
Definition: SkVertices.h:20
uint32_t SkPMColor
32 bit ARGB color value, premultiplied.
Definition: SkColor.h:161
the overriding shader is opaque
Definition: SkCanvas.h:1409
Base-class for objects that draw into SkCanvas.
Definition: SkDrawable.h:26
void drawIRect(const SkIRect &rect, const SkPaint &paint)
Draw the specified rectangle using the specified paint.
Definition: SkCanvas.h:648
uint32_t SkColor
32 bit ARGB color value, not premultiplied.
Definition: SkColor.h:28
drawPoints draws each point separately
Definition: SkCanvas.h:585
If kStrict is specified, the implementation must respect the src-rect (if specified) strictly...
Definition: SkCanvas.h:761
void restoreToCount(int saveCount)
Efficient way to pop any calls to save() that happened after the save count reached saveCount...
Definition: SkRect.h:404
Stack helper class to automatically call restoreToCount() on the canvas when this object goes out of ...
Definition: SkCanvas.h:1562
void restore()
Perform the restore now, instead of waiting for the destructor.
Definition: SkCanvas.h:1582
int save()
This call saves the current matrix, clip, and drawFilter, and pushes a copy onto a private stack...
If a client wants to control the allocation of raster layers in a canvas, it should subclass SkRaster...
Definition: SkRasterHandleAllocator.h:36
SkIRect holds four 32 bit integer coordinates for a rectangle.
Definition: SkRect.h:20
unsigned U8CPU
Fast type for unsigned 8 bits.
Definition: SkTypes.h:191
bool isEmpty() const
Return true if the rectangle's width or height are <= 0.
Definition: SkRect.h:472
SkImage is an abstraction for drawing a rectagle of pixels, though the particular type of image could...
Definition: SkImage.h:48
SkIPoint holds two 32 bit integer coordinates.
Definition: SkPoint.h:40
Light weight class for managing strings.
Definition: SkString.h:121
void drawString(const char *string, SkScalar x, SkScalar y, const SkPaint &paint)
Draw null-terminated UTF-8 string, with origin at (x,y), using the specified paint.
Definition: SkCanvas.h:967