Skia
2DGraphicsLibrary
 All Classes 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 "SkTypes.h"
12 #include "SkBitmap.h"
13 #include "SkDeque.h"
14 #include "SkImage.h"
15 #include "SkPaint.h"
16 #include "SkRefCnt.h"
17 #include "SkRegion.h"
18 #include "SkSurfaceProps.h"
19 #include "SkXfermode.h"
20 #include "SkLights.h"
21 
22 class GrContext;
23 class GrDrawContext;
24 class SkBaseDevice;
26 class SkClipStack;
27 class SkData;
28 class SkDraw;
29 class SkDrawable;
30 class SkDrawFilter;
31 class SkImageFilter;
32 class SkMetaData;
33 class SkPath;
34 class SkPicture;
35 class SkPixmap;
36 class SkRasterClip;
37 class SkRRect;
38 struct SkRSXform;
39 class SkSurface;
40 class SkSurface_Base;
41 class SkTextBlob;
42 
58 class SK_API SkCanvas : public SkRefCnt {
59  enum PrivateSaveLayerFlags {
60  kDontClipToLayer_PrivateSaveLayerFlag = 1U << 31,
61  };
62 
63 public:
79  static SkCanvas* NewRasterDirect(const SkImageInfo&, void*, size_t);
80 
81  static SkCanvas* NewRasterDirectN32(int width, int height, SkPMColor* pixels, size_t rowBytes) {
82  return NewRasterDirect(SkImageInfo::MakeN32Premul(width, height), pixels, rowBytes);
83  }
84 
89  SkCanvas();
90 
96  SkCanvas(int width, int height, const SkSurfaceProps* = NULL);
97 
102  explicit SkCanvas(SkBaseDevice* device);
103 
108  explicit SkCanvas(const SkBitmap& bitmap);
109 
115  SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props);
116 
117  virtual ~SkCanvas();
118 
119  SkMetaData& getMetaData();
120 
125  SkImageInfo imageInfo() const;
126 
132  bool getProps(SkSurfaceProps*) const;
133 
135 
141  void flush();
142 
148  virtual SkISize getBaseLayerSize() const;
149 
153  SkISize getDeviceSize() const { return this->getBaseLayerSize(); }
154 
161 #ifndef SK_SUPPORT_LEGACY_GETDEVICE
162 protected: // Can we make this private?
163 #endif
164  SkBaseDevice* getDevice() const;
165 public:
166  SkBaseDevice* getDevice_just_for_deprecated_compatibility_testing() const {
167  return this->getDevice();
168  }
169 
183 #ifndef SK_SUPPORT_LEGACY_GETTOPDEVICE
184 private:
185 #endif
186  SkBaseDevice* getTopDevice(bool updateMatrixClip = false) const;
187 public:
188 
198  sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps* = nullptr);
199 #ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API
200  SkSurface* newSurface(const SkImageInfo& info, const SkSurfaceProps* props = NULL);
201 #endif
202 
207  GrContext* getGrContext();
208 
210 
222  void* accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin = NULL);
223 
235  bool peekPixels(SkPixmap*);
236 
237 #ifdef SK_SUPPORT_LEGACY_PEEKPIXELS_PARMS
238  const void* peekPixels(SkImageInfo* info, size_t* rowBytes);
239 #endif
240 
260  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
261  int srcX, int srcY);
262 
268  bool readPixels(SkBitmap* bitmap, int srcX, int srcY);
269 
276  bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
277 
296  bool writePixels(const SkImageInfo&, const void* pixels, size_t rowBytes, int x, int y);
297 
302  bool writePixels(const SkBitmap& bitmap, int x, int y);
303 
305 
315  int save();
316 
329  int saveLayer(const SkRect* bounds, const SkPaint* paint);
330  int saveLayer(const SkRect& bounds, const SkPaint* paint) {
331  return this->saveLayer(&bounds, paint);
332  }
333 
339  int saveLayerPreserveLCDTextRequests(const SkRect* bounds, const SkPaint* paint);
340 
352  int saveLayerAlpha(const SkRect* bounds, U8CPU alpha);
353 
354  enum {
355  kIsOpaque_SaveLayerFlag = 1 << 0,
356  kPreserveLCDText_SaveLayerFlag = 1 << 1,
357 
358 #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
359  kDontClipToLayer_Legacy_SaveLayerFlag = kDontClipToLayer_PrivateSaveLayerFlag,
360 #endif
361  };
362  typedef uint32_t SaveLayerFlags;
363 
364  struct SaveLayerRec {
365  SaveLayerRec()
366  : fBounds(nullptr), fPaint(nullptr), fBackdrop(nullptr), fSaveLayerFlags(0)
367  {}
368  SaveLayerRec(const SkRect* bounds, const SkPaint* paint, SaveLayerFlags saveLayerFlags = 0)
369  : fBounds(bounds)
370  , fPaint(paint)
371  , fBackdrop(nullptr)
372  , fSaveLayerFlags(saveLayerFlags)
373  {}
374  SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop,
375  SaveLayerFlags saveLayerFlags)
376  : fBounds(bounds)
377  , fPaint(paint)
378  , fBackdrop(backdrop)
379  , fSaveLayerFlags(saveLayerFlags)
380  {}
381 
382  const SkRect* fBounds; // optional
383  const SkPaint* fPaint; // optional
384  const SkImageFilter* fBackdrop; // optional
385  SaveLayerFlags fSaveLayerFlags;
386  };
387 
388  int saveLayer(const SaveLayerRec&);
389 
395  void restore();
396 
401  int getSaveCount() const;
402 
409  void restoreToCount(int saveCount);
410 
415  void translate(SkScalar dx, SkScalar dy);
416 
421  void scale(SkScalar sx, SkScalar sy);
422 
426  void rotate(SkScalar degrees);
427 
433  void rotate(SkScalar degrees, SkScalar px, SkScalar py);
434 
439  void skew(SkScalar sx, SkScalar sy);
440 
444  void concat(const SkMatrix& matrix);
445 
449  void setMatrix(const SkMatrix& matrix);
450 
453  void resetMatrix();
454 
455 #ifdef SK_EXPERIMENTAL_SHADOWING
456 
461  void translateZ(SkScalar z);
462 
466  void setLights(sk_sp<SkLights> lights);
467 
470  sk_sp<SkLights> getLights() const;
471 #endif
472 
479  void clipRect(const SkRect& rect,
481  bool doAntiAlias = false);
482 
489  void clipRRect(const SkRRect& rrect,
491  bool doAntiAlias = false);
492 
499  void clipPath(const SkPath& path,
501  bool doAntiAlias = false);
502 
507  void setAllowSoftClip(bool allow) {
508  fAllowSoftClip = allow;
509  }
510 
514  void setAllowSimplifyClip(bool allow) {
515  fAllowSimplifyClip = allow;
516  }
517 
525  void clipRegion(const SkRegion& deviceRgn,
527 
533  void setClipRegion(const SkRegion& deviceRgn) {
534  this->clipRegion(deviceRgn, SkRegion::kReplace_Op);
535  }
536 
545  bool quickReject(const SkRect& rect) const;
546 
557  bool quickReject(const SkPath& path) const;
558 
564  virtual bool getClipBounds(SkRect* bounds) const;
565 
570  virtual bool getClipDeviceBounds(SkIRect* bounds) const;
571 
572 
581  void drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b,
583 
589  void drawColor(SkColor color, SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode);
590 
595  void clear(SkColor color) {
596  this->drawColor(color, SkXfermode::kSrc_Mode);
597  }
598 
611  void discard() { this->onDiscard(); }
612 
618  void drawPaint(const SkPaint& paint);
619 
620  enum PointMode {
626  kPolygon_PointMode
627  };
628 
650  void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint);
651 
655  void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint);
656 
662  void drawPoint(SkScalar x, SkScalar y, SkColor color);
663 
673  void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1,
674  const SkPaint& paint);
675 
681  void drawRect(const SkRect& rect, const SkPaint& paint);
682 
688  void drawIRect(const SkIRect& rect, const SkPaint& paint) {
689  SkRect r;
690  r.set(rect); // promotes the ints to scalars
691  this->drawRect(r, paint);
692  }
693 
702  void drawRectCoords(SkScalar left, SkScalar top, SkScalar right,
703  SkScalar bottom, const SkPaint& paint);
704 
710  void drawOval(const SkRect& oval, const SkPaint&);
711 
719  void drawRRect(const SkRRect& rrect, const SkPaint& paint);
720 
725  void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint&);
726 
735  void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius,
736  const SkPaint& paint);
737 
749  void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
750  bool useCenter, const SkPaint& paint);
751 
759  void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry,
760  const SkPaint& paint);
761 
767  void drawPath(const SkPath& path, const SkPaint& paint);
768 
777  void drawImage(const SkImage* image, SkScalar left, SkScalar top, const SkPaint* paint = NULL);
778  void drawImage(const sk_sp<SkImage>& image, SkScalar left, SkScalar top,
779  const SkPaint* paint = NULL) {
780  this->drawImage(image.get(), left, top, paint);
781  }
782 
803 
810  };
811 
823  void drawImageRect(const SkImage* image, const SkRect& src, const SkRect& dst,
824  const SkPaint* paint,
825  SrcRectConstraint constraint = kStrict_SrcRectConstraint);
826  // variant that takes src SkIRect
827  void drawImageRect(const SkImage* image, const SkIRect& isrc, const SkRect& dst,
828  const SkPaint* paint, SrcRectConstraint = kStrict_SrcRectConstraint);
829  // variant that assumes src == image-bounds
830  void drawImageRect(const SkImage* image, const SkRect& dst, const SkPaint* paint,
831  SrcRectConstraint = kStrict_SrcRectConstraint);
832 
833  void drawImageRect(const sk_sp<SkImage>& image, const SkRect& src, const SkRect& dst,
834  const SkPaint* paint,
835  SrcRectConstraint constraint = kStrict_SrcRectConstraint) {
836  this->drawImageRect(image.get(), src, dst, paint, constraint);
837  }
838  void drawImageRect(const sk_sp<SkImage>& image, const SkIRect& isrc, const SkRect& dst,
839  const SkPaint* paint, SrcRectConstraint cons = kStrict_SrcRectConstraint) {
840  this->drawImageRect(image.get(), isrc, dst, paint, cons);
841  }
842  void drawImageRect(const sk_sp<SkImage>& image, const SkRect& dst, const SkPaint* paint,
843  SrcRectConstraint cons = kStrict_SrcRectConstraint) {
844  this->drawImageRect(image.get(), dst, paint, cons);
845  }
846 
861  void drawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
862  const SkPaint* paint = nullptr);
863  void drawImageNine(const sk_sp<SkImage>& image, const SkIRect& center, const SkRect& dst,
864  const SkPaint* paint = nullptr) {
865  this->drawImageNine(image.get(), center, dst, paint);
866  }
867 
884  void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
885  const SkPaint* paint = NULL);
886 
898  void drawBitmapRect(const SkBitmap& bitmap, const SkRect& src, const SkRect& dst,
899  const SkPaint* paint, SrcRectConstraint = kStrict_SrcRectConstraint);
900  // variant where src is SkIRect
901  void drawBitmapRect(const SkBitmap& bitmap, const SkIRect& isrc, const SkRect& dst,
902  const SkPaint* paint, SrcRectConstraint = kStrict_SrcRectConstraint);
903  void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst, const SkPaint* paint,
904  SrcRectConstraint = kStrict_SrcRectConstraint);
905 
920  void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
921  const SkPaint* paint = NULL);
922 
926  struct Lattice {
927  // An array of x-coordinates that divide the bitmap vertically.
928  // These must be unique, increasing, and in the set [0, width].
929  // Does not have ownership.
930  const int* fXDivs;
931 
932  // The number of fXDivs.
933  int fXCount;
934 
935  // An array of y-coordinates that divide the bitmap horizontally.
936  // These must be unique, increasing, and in the set [0, height].
937  // Does not have ownership.
938  const int* fYDivs;
939 
940  // The number of fYDivs.
941  int fYCount;
942  };
943 
959  void drawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, const SkRect& dst,
960  const SkPaint* paint = nullptr);
961  void drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst,
962  const SkPaint* paint = nullptr);
963 
972  void drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
973  const SkPaint& paint);
974 
982  void drawPosText(const void* text, size_t byteLength, const SkPoint pos[],
983  const SkPaint& paint);
984 
994  void drawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], SkScalar constY,
995  const SkPaint& paint);
996 
1009  void drawTextOnPathHV(const void* text, size_t byteLength, const SkPath& path, SkScalar hOffset,
1010  SkScalar vOffset, const SkPaint& paint);
1011 
1022  void drawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
1023  const SkMatrix* matrix, const SkPaint& paint);
1024 
1030  void drawTextRSXform(const void* text, size_t byteLength, const SkRSXform[],
1031  const SkRect* cullRect, const SkPaint& paint);
1032 
1039  void drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint);
1040  void drawTextBlob(const sk_sp<SkTextBlob>& blob, SkScalar x, SkScalar y, const SkPaint& paint) {
1041  this->drawTextBlob(blob.get(), x, y, paint);
1042  }
1043 
1050  void drawPicture(const SkPicture* picture) {
1051  this->drawPicture(picture, NULL, NULL);
1052  }
1053  void drawPicture(const sk_sp<SkPicture>& picture) {
1054  this->drawPicture(picture.get());
1055  }
1056 
1069  void drawPicture(const SkPicture*, const SkMatrix* matrix, const SkPaint* paint);
1070  void drawPicture(const sk_sp<SkPicture>& picture, const SkMatrix* matrix, const SkPaint* paint) {
1071  this->drawPicture(picture.get(), matrix, paint);
1072  }
1073 
1074 #ifdef SK_EXPERIMENTAL_SHADOWING
1075 
1092  void drawShadowedPicture(const SkPicture*,
1093  const SkMatrix* matrix,
1094  const SkPaint* paint);
1095  void drawShadowedPicture(const sk_sp<SkPicture>& picture,
1096  const SkMatrix* matrix,
1097  const SkPaint* paint) {
1098  this->drawShadowedPicture(picture.get(), matrix, paint);
1099  }
1100 #endif
1101 
1102  enum VertexMode {
1103  kTriangles_VertexMode,
1104  kTriangleStrip_VertexMode,
1105  kTriangleFan_VertexMode
1106  };
1107 
1131  void drawVertices(VertexMode vmode, int vertexCount,
1132  const SkPoint vertices[], const SkPoint texs[],
1133  const SkColor colors[], SkXfermode* xmode,
1134  const uint16_t indices[], int indexCount,
1135  const SkPaint& paint);
1136  void drawVertices(VertexMode vmode, int vertexCount,
1137  const SkPoint vertices[], const SkPoint texs[],
1138  const SkColor colors[], const sk_sp<SkXfermode>& xmode,
1139  const uint16_t indices[], int indexCount,
1140  const SkPaint& paint) {
1141  this->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode.get(),
1142  indices, indexCount, paint);
1143  }
1144 
1158  void drawPatch(const SkPoint cubics[12], const SkColor colors[4],
1159  const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint);
1160  void drawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4],
1161  const sk_sp<SkXfermode>& xmode, const SkPaint& paint) {
1162  this->drawPatch(cubics, colors, texCoords, xmode.get(), paint);
1163  }
1164 
1182  void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
1183  const SkColor colors[], int count, SkXfermode::Mode, const SkRect* cullRect,
1184  const SkPaint* paint);
1185 
1186  void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], int count,
1187  const SkRect* cullRect, const SkPaint* paint) {
1188  this->drawAtlas(atlas, xform, tex, NULL, count, SkXfermode::kDst_Mode, cullRect, paint);
1189  }
1190 
1191  void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[],
1192  const SkColor colors[], int count, SkXfermode::Mode mode, const SkRect* cull,
1193  const SkPaint* paint) {
1194  this->drawAtlas(atlas.get(), xform, tex, colors, count, mode, cull, paint);
1195  }
1196  void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[],
1197  int count, const SkRect* cullRect, const SkPaint* paint) {
1198  this->drawAtlas(atlas.get(), xform, tex, nullptr, count, SkXfermode::kDst_Mode,
1199  cullRect, paint);
1200  }
1201 
1210  void drawDrawable(SkDrawable* drawable, const SkMatrix* = NULL);
1211  void drawDrawable(SkDrawable*, SkScalar x, SkScalar y);
1212 
1223  void drawAnnotation(const SkRect&, const char key[], SkData* value);
1224  void drawAnnotation(const SkRect& rect, const char key[], const sk_sp<SkData>& value) {
1225  this->drawAnnotation(rect, key, value.get());
1226  }
1227 
1229 #ifdef SK_INTERNAL
1230 #ifndef SK_SUPPORT_LEGACY_DRAWFILTER
1231  #define SK_SUPPORT_LEGACY_DRAWFILTER
1232 #endif
1233 #endif
1234 
1235 #ifdef SK_SUPPORT_LEGACY_DRAWFILTER
1236 
1240  SK_ATTR_EXTERNALLY_DEPRECATED("getDrawFilter use is deprecated")
1241  SkDrawFilter* getDrawFilter() const;
1242 
1251  SK_ATTR_EXTERNALLY_DEPRECATED("setDrawFilter use is deprecated")
1252  virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter);
1253 #endif
1254 
1263  virtual bool isClipEmpty() const;
1264 
1269  virtual bool isClipRect() const;
1270 
1275  const SkMatrix& getTotalMatrix() const;
1276 
1282  const SkClipStack* getClipStack() const {
1283  return fClipStack;
1284  }
1285 
1286  typedef SkCanvasClipVisitor ClipVisitor;
1292  void replayClips(ClipVisitor*) const;
1293 
1295 
1296  // don't call
1297  GrDrawContext* internal_private_accessTopLayerDrawContext();
1298 
1299  // don't call
1300  static void Internal_Private_SetIgnoreSaveLayerBounds(bool);
1301  static bool Internal_Private_GetIgnoreSaveLayerBounds();
1302  static void Internal_Private_SetTreatSpriteAsBitmap(bool);
1303  static bool Internal_Private_GetTreatSpriteAsBitmap();
1304 
1305  // TEMP helpers until we switch virtual over to const& for src-rect
1306  void legacy_drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1307  const SkPaint* paint,
1308  SrcRectConstraint constraint = kStrict_SrcRectConstraint);
1309  void legacy_drawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
1310  const SkPaint* paint,
1311  SrcRectConstraint constraint = kStrict_SrcRectConstraint);
1312 
1313  // expose minimum amount of information necessary for transitional refactoring
1317  void temporary_internal_describeTopLayer(SkMatrix* matrix, SkIRect* clip_bounds);
1318 
1319 protected:
1320 #ifdef SK_EXPERIMENTAL_SHADOWING
1321 
1323  SkScalar getZ() const;
1324 
1325  sk_sp<SkLights> fLights;
1326 #endif
1327 
1328  // default impl defers to getDevice()->newSurface(info)
1329  virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&);
1330 
1331  // default impl defers to its device
1332  virtual bool onPeekPixels(SkPixmap*);
1333  virtual bool onAccessTopLayerPixels(SkPixmap*);
1334  virtual SkImageInfo onImageInfo() const;
1335  virtual bool onGetProps(SkSurfaceProps*) const;
1336  virtual void onFlush();
1337 
1338  // Subclass save/restore notifiers.
1339  // Overriders should call the corresponding INHERITED method up the inheritance chain.
1340  // getSaveLayerStrategy()'s return value may suppress full layer allocation.
1341  enum SaveLayerStrategy {
1342  kFullLayer_SaveLayerStrategy,
1343  kNoLayer_SaveLayerStrategy,
1344  };
1345 
1346  virtual void willSave() {}
1347  // Overriders should call the corresponding INHERITED method up the inheritance chain.
1348  virtual SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) {
1349  return kFullLayer_SaveLayerStrategy;
1350  }
1351  virtual void willRestore() {}
1352  virtual void didRestore() {}
1353  virtual void didConcat(const SkMatrix&) {}
1354  virtual void didSetMatrix(const SkMatrix&) {}
1355  virtual void didTranslate(SkScalar dx, SkScalar dy) {
1356  this->didConcat(SkMatrix::MakeTrans(dx, dy));
1357  }
1358 
1359 #ifdef SK_EXPERIMENTAL_SHADOWING
1360  virtual void didTranslateZ(SkScalar) {}
1361 #endif
1362 
1363  virtual void onDrawAnnotation(const SkRect&, const char key[], SkData* value);
1364  virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&);
1365 
1366  virtual void onDrawText(const void* text, size_t byteLength, SkScalar x,
1367  SkScalar y, const SkPaint& paint);
1368 
1369  virtual void onDrawPosText(const void* text, size_t byteLength,
1370  const SkPoint pos[], const SkPaint& paint);
1371 
1372  virtual void onDrawPosTextH(const void* text, size_t byteLength,
1373  const SkScalar xpos[], SkScalar constY,
1374  const SkPaint& paint);
1375 
1376  virtual void onDrawTextOnPath(const void* text, size_t byteLength,
1377  const SkPath& path, const SkMatrix* matrix,
1378  const SkPaint& paint);
1379  virtual void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform[],
1380  const SkRect* cullRect, const SkPaint& paint);
1381 
1382  virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1383  const SkPaint& paint);
1384 
1385  virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
1386  const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint);
1387 
1388  virtual void onDrawDrawable(SkDrawable*, const SkMatrix*);
1389 
1390  virtual void onDrawPaint(const SkPaint&);
1391  virtual void onDrawRect(const SkRect&, const SkPaint&);
1392  virtual void onDrawOval(const SkRect&, const SkPaint&);
1393  virtual void onDrawArc(const SkRect&, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
1394  const SkPaint&);
1395  virtual void onDrawRRect(const SkRRect&, const SkPaint&);
1396  virtual void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&);
1397  virtual void onDrawVertices(VertexMode, int vertexCount, const SkPoint vertices[],
1398  const SkPoint texs[], const SkColor colors[], SkXfermode*,
1399  const uint16_t indices[], int indexCount, const SkPaint&);
1400 
1401  virtual void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[],
1402  int count, SkXfermode::Mode, const SkRect* cull, const SkPaint*);
1403  virtual void onDrawPath(const SkPath&, const SkPaint&);
1404  virtual void onDrawImage(const SkImage*, SkScalar dx, SkScalar dy, const SkPaint*);
1405  virtual void onDrawImageRect(const SkImage*, const SkRect*, const SkRect&, const SkPaint*,
1406  SrcRectConstraint);
1407  virtual void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
1408  const SkPaint*);
1409  virtual void onDrawImageLattice(const SkImage*, const Lattice& lattice, const SkRect& dst,
1410  const SkPaint*);
1411 
1412  virtual void onDrawBitmap(const SkBitmap&, SkScalar dx, SkScalar dy, const SkPaint*);
1413  virtual void onDrawBitmapRect(const SkBitmap&, const SkRect*, const SkRect&, const SkPaint*,
1414  SrcRectConstraint);
1415  virtual void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
1416  const SkPaint*);
1417  virtual void onDrawBitmapLattice(const SkBitmap&, const Lattice& lattice, const SkRect& dst,
1418  const SkPaint*);
1419 
1420  enum ClipEdgeStyle {
1421  kHard_ClipEdgeStyle,
1422  kSoft_ClipEdgeStyle
1423  };
1424 
1425  virtual void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle);
1426  virtual void onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle);
1427  virtual void onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle);
1428  virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op);
1429 
1430  virtual void onDiscard();
1431 
1432  virtual void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*);
1433 
1434 #ifdef SK_EXPERIMENTAL_SHADOWING
1435  virtual void onDrawShadowedPicture(const SkPicture*,
1436  const SkMatrix*,
1437  const SkPaint*);
1438 #endif
1439 
1440  // Returns the canvas to be used by DrawIter. Default implementation
1441  // returns this. Subclasses that encapsulate an indirect canvas may
1442  // need to overload this method. The impl must keep track of this, as it
1443  // is not released or deleted by the caller.
1444  virtual SkCanvas* canvasForDrawIter();
1445 
1446  // Clip rectangle bounds. Called internally by saveLayer.
1447  // returns false if the entire rectangle is entirely clipped out
1448  // If non-NULL, The imageFilter parameter will be used to expand the clip
1449  // and offscreen bounds for any margin required by the filter DAG.
1450  bool clipRectBounds(const SkRect* bounds, SaveLayerFlags, SkIRect* intersection,
1451  const SkImageFilter* imageFilter = NULL);
1452 
1453 private:
1460  class LayerIter /*: SkNoncopyable*/ {
1461  public:
1463  LayerIter(SkCanvas*);
1464  ~LayerIter();
1465 
1467  bool done() const { return fDone; }
1469  void next();
1470 
1471  // These reflect the current device in the iterator
1472 
1473  SkBaseDevice* device() const;
1474  const SkMatrix& matrix() const;
1475  const SkRasterClip& clip() const;
1476  const SkPaint& paint() const;
1477  int x() const;
1478  int y() const;
1479 
1480  private:
1481  // used to embed the SkDrawIter object directly in our instance, w/o
1482  // having to expose that class def to the public. There is an assert
1483  // in our constructor to ensure that fStorage is large enough
1484  // (though needs to be a compile-time-assert!). We use intptr_t to work
1485  // safely with 32 and 64 bit machines (to ensure the storage is enough)
1486  intptr_t fStorage[32];
1487  class SkDrawIter* fImpl; // this points at fStorage
1488  SkPaint fDefaultPaint;
1489  bool fDone;
1490  };
1491 
1492  static bool BoundsAffectsClip(SaveLayerFlags);
1493  static SaveLayerFlags LegacySaveFlagsToSaveLayerFlags(uint32_t legacySaveFlags);
1494 
1495  static void DrawDeviceWithFilter(SkBaseDevice* src, const SkImageFilter* filter,
1496  SkBaseDevice* dst, const SkMatrix& ctm,
1497  const SkClipStack* clipStack);
1498 
1503  };
1504 
1505  // notify our surface (if we have one) that we are about to draw, so it
1506  // can perform copy-on-write or invalidate any cached images
1507  void predrawNotify(bool willOverwritesEntireSurface = false);
1508  void predrawNotify(const SkRect* rect, const SkPaint* paint, ShaderOverrideOpacity);
1509  void predrawNotify(const SkRect* rect, const SkPaint* paint, bool shaderOverrideIsOpaque) {
1510  this->predrawNotify(rect, paint, shaderOverrideIsOpaque ? kOpaque_ShaderOverrideOpacity
1511  : kNotOpaque_ShaderOverrideOpacity);
1512  }
1513 
1514  class MCRec;
1515 
1516  SkAutoTUnref<SkClipStack> fClipStack;
1517  SkDeque fMCStack;
1518  // points to top of stack
1519  MCRec* fMCRec;
1520  // the first N recs that can fit here mean we won't call malloc
1521  enum {
1522  kMCRecSize = 128, // most recent measurement
1523  kMCRecCount = 32, // common depth for save/restores
1524  kDeviceCMSize = 176, // most recent measurement
1525  };
1526  intptr_t fMCRecStorage[kMCRecSize * kMCRecCount / sizeof(intptr_t)];
1527  intptr_t fDeviceCMStorage[kDeviceCMSize / sizeof(intptr_t)];
1528 
1529  const SkSurfaceProps fProps;
1530 
1531  int fSaveCount; // value returned by getSaveCount()
1532 
1533  SkMetaData* fMetaData;
1534 
1535  SkSurface_Base* fSurfaceBase;
1536  SkSurface_Base* getSurfaceBase() const { return fSurfaceBase; }
1537  void setSurfaceBase(SkSurface_Base* sb) {
1538  fSurfaceBase = sb;
1539  }
1540  friend class SkSurface_Base;
1541  friend class SkSurface_Gpu;
1542 
1543  bool fDeviceCMDirty; // cleared by updateDeviceCMCache()
1544  void updateDeviceCMCache();
1545 
1546  void doSave();
1547  void checkForDeferredSave();
1548  void internalSetMatrix(const SkMatrix&);
1549 
1550  friend class SkDrawIter; // needs setupDrawForLayerDevice()
1551  friend class AutoDrawLooper;
1552  friend class SkLua; // needs top layer size and offset
1553  friend class SkDebugCanvas; // needs experimental fAllowSimplifyClip
1554  friend class SkSurface_Raster; // needs getDevice()
1555  friend class SkRecorder; // InitFlags
1556  friend class SkLiteRecorder; // InitFlags
1557  friend class SkNoSaveLayerCanvas; // InitFlags
1558  friend class SkPictureImageFilter; // SkCanvas(SkBaseDevice*, SkSurfaceProps*, InitFlags)
1559  friend class SkPictureRecord; // predrawNotify (why does it need it? <reed>)
1560  friend class SkPicturePlayback; // SaveFlagsToSaveLayerFlags
1561 
1562  enum InitFlags {
1563  kDefault_InitFlags = 0,
1564  kConservativeRasterClip_InitFlag = 1 << 0,
1565  };
1566  SkCanvas(const SkIRect& bounds, InitFlags);
1567  SkCanvas(SkBaseDevice* device, InitFlags);
1568 
1569  void resetForNextPicture(const SkIRect& bounds);
1570 
1571  // needs gettotalclip()
1572  friend class SkCanvasStateUtils;
1573 
1574  // call this each time we attach ourselves to a device
1575  // - constructor
1576  // - internalSaveLayer
1577  void setupDevice(SkBaseDevice*);
1578 
1579  SkBaseDevice* init(SkBaseDevice*, InitFlags);
1580 
1585  SkIRect getTopLayerBounds() const;
1586 
1587  void internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src,
1588  const SkRect& dst, const SkPaint* paint,
1589  SrcRectConstraint);
1590  void internalDrawPaint(const SkPaint& paint);
1591  void internalSaveLayer(const SaveLayerRec&, SaveLayerStrategy);
1592  void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*);
1593 
1594  // shared by save() and saveLayer()
1595  void internalSave();
1596  void internalRestore();
1597  static void DrawRect(const SkDraw& draw, const SkPaint& paint,
1598  const SkRect& r, SkScalar textSize);
1599  static void DrawTextDecorations(const SkDraw& draw, const SkPaint& paint,
1600  const char text[], size_t byteLength,
1601  SkScalar x, SkScalar y);
1602 
1603  // only for canvasutils
1604  const SkRegion& internal_private_getTotalClip() const;
1605 
1606  /*
1607  * Returns true if drawing the specified rect (or all if it is null) with the specified
1608  * paint (or default if null) would overwrite the entire root device of the canvas
1609  * (i.e. the canvas' surface if it had one).
1610  */
1611  bool wouldOverwriteEntireSurface(const SkRect*, const SkPaint*, ShaderOverrideOpacity) const;
1612 
1616  bool canDrawBitmapAsSprite(SkScalar x, SkScalar y, int w, int h, const SkPaint&);
1617 
1618 
1624  SkRect fDeviceClipBounds;
1625 
1626  bool fAllowSoftClip;
1627  bool fAllowSimplifyClip;
1628  const bool fConservativeRasterClip;
1629 
1630  class AutoValidateClip : ::SkNoncopyable {
1631  public:
1632  explicit AutoValidateClip(SkCanvas* canvas) : fCanvas(canvas) {
1633  fCanvas->validateClip();
1634  }
1635  ~AutoValidateClip() { fCanvas->validateClip(); }
1636 
1637  private:
1638  const SkCanvas* fCanvas;
1639  };
1640 
1641 #ifdef SK_DEBUG
1642  void validateClip() const;
1643 #else
1644  void validateClip() const {}
1645 #endif
1646 
1647  typedef SkRefCnt INHERITED;
1648 };
1649 
1654 class SkAutoCanvasRestore : SkNoncopyable {
1655 public:
1656  SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas), fSaveCount(0) {
1657  if (fCanvas) {
1658  fSaveCount = canvas->getSaveCount();
1659  if (doSave) {
1660  canvas->save();
1661  }
1662  }
1663  }
1664  ~SkAutoCanvasRestore() {
1665  if (fCanvas) {
1666  fCanvas->restoreToCount(fSaveCount);
1667  }
1668  }
1669 
1674  void restore() {
1675  if (fCanvas) {
1676  fCanvas->restoreToCount(fSaveCount);
1677  fCanvas = NULL;
1678  }
1679  }
1680 
1681 private:
1682  SkCanvas* fCanvas;
1683  int fSaveCount;
1684 };
1685 #define SkAutoCanvasRestore(...) SK_REQUIRE_LOCAL_VAR(SkAutoCanvasRestore)
1686 
1688 public:
1689  virtual ~SkCanvasClipVisitor();
1690  virtual void clipRect(const SkRect&, SkRegion::Op, bool antialias) = 0;
1691  virtual void clipRRect(const SkRRect&, SkRegion::Op, bool antialias) = 0;
1692  virtual void clipPath(const SkPath&, SkRegion::Op, bool antialias) = 0;
1693 };
1694 
1695 #endif
void discard()
This makes the contents of the canvas undefined.
Definition: SkCanvas.h:611
A Canvas encapsulates all of the state about drawing into a device (bitmap).
Definition: SkCanvas.h:58
[Sa, Sc]
Definition: SkXfermode.h:73
Definition: SkCanvas.h:364
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:48
The SkPath class encapsulates compound (multiple contour) geometric paths consisting of straight line...
Definition: SkPath.h:27
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:22
Mode
List of predefined xfermodes.
Definition: SkXfermode.h:71
If kFast is specified, the implementation may sample outside of the src-rect (if specified) by half t...
Definition: SkCanvas.h:809
After calling saveLayer(), there can be any number of devices that make up the top-most drawing area...
Definition: SkCanvas.h:1460
[Sa + Da * (1 - Sa), Sc + Dc * (1 - Sa)]
Definition: SkXfermode.h:75
bool fIsScaleTranslate
Keep track of the device clip bounds and if the matrix is scale-translate.
Definition: SkCanvas.h:1623
PointMode
Definition: SkCanvas.h:620
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:514
there is no overriding shader (bitmap or image)
Definition: SkCanvas.h:1500
The SkMatrix class holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:26
SkSurface represents the backend/results of drawing to a canvas.
Definition: SkSurface.h:31
Definition: SkRefCnt.h:135
The SkPaint class holds the style and color information about how to draw geometries, text and bitmaps.
Definition: SkPaint.h:46
void drawPicture(const SkPicture *picture)
Draw the picture into this canvas.
Definition: SkCanvas.h:1050
ShaderOverrideOpacity
Definition: SkCanvas.h:1499
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:1630
SkXfermode is the base class for objects that are called to implement custom "transfer-modes" in the ...
Definition: SkXfermode.h:34
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:1467
A compressed form of a rotation+scale matrix.
Definition: SkRSXform.h:20
void setClipRegion(const SkRegion &deviceRgn)
Helper for clipRegion(rgn, kReplace_Op).
Definition: SkCanvas.h:533
the overriding shader may not be opaque
Definition: SkCanvas.h:1502
Specifies coordinates to divide a bitmap into (xCount*yCount) rects.
Definition: SkCanvas.h:926
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:595
The SkBitmap class specifies a raster bitmap.
Definition: SkBitmap.h:41
Base class for image filters.
Definition: SkImageFilter.h:35
const SkClipStack * getClipStack() const
Return the clip stack.
Definition: SkCanvas.h:1282
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:258
drawPoints draws each pair of points as a line segment
Definition: SkCanvas.h:624
Definition: GrDrawContext.h:51
DEPRECATED - use SkPaintFilterCanvas instead.
Definition: SkDrawFilter.h:26
Definition: SkDeque.h:26
Definition: GrContext.h:48
SkISize getDeviceSize() const
DEPRECATED: call getBaseLayerSize.
Definition: SkCanvas.h:153
SrcRectConstraint
Controls the behavior at the edge of the src-rect, when specified in drawImageRect, trading off speed for exactness.
Definition: SkCanvas.h:796
The SkRegion class encapsulates the geometric region used to specify clipping areas for drawing...
Definition: SkRegion.h:30
replace the dst region with the op region
Definition: SkRegion.h:246
uint32_t SkPMColor
32 bit ARGB color value, premultiplied.
Definition: SkColor.h:147
the overriding shader is opaque
Definition: SkCanvas.h:1501
Base-class for objects that draw into SkCanvas.
Definition: SkDrawable.h:25
void drawIRect(const SkIRect &rect, const SkPaint &paint)
Draw the specified rectangle using the specified paint.
Definition: SkCanvas.h:688
uint32_t SkColor
32 bit ARGB color value, not premultiplied.
Definition: SkColor.h:28
Definition: SkDevice.h:28
drawPoints draws each point separately
Definition: SkCanvas.h:622
Definition: SkDraw.h:30
If kStrict is specified, the implementation must respect the src-rect (if specified) strictly...
Definition: SkCanvas.h:802
void restoreToCount(int saveCount)
Efficient way to pop any calls to save() that happened after the save count reached saveCount...
Definition: SkRect.h:390
Stack helper class to automatically call restoreToCount() on the canvas when this object goes out of ...
Definition: SkCanvas.h:1654
intersect the two regions
Definition: SkRegion.h:241
void setAllowSoftClip(bool allow)
EXPERIMENTAL – only used for testing Set to false to force clips to be hard, even if doAntiAlias=true...
Definition: SkCanvas.h:507
void restore()
Perform the restore now, instead of waiting for the destructor.
Definition: SkCanvas.h:1674
int save()
This call saves the current matrix, clip, and drawFilter, and pushes a copy onto a private stack...
[Da, Dc]
Definition: SkXfermode.h:74
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:251
Definition: SkClipStack.h:26
SkImage is an abstraction for drawing a rectagle of pixels, though the particular type of image could...
Definition: SkImage.h:45
SkIPoint holds two 32 bit integer coordinates.
Definition: SkPoint.h:40
Op
The logical operations that can be performed when combining two regions.
Definition: SkRegion.h:239
Definition: SkCanvas.h:1687