SkPicture.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright 2007 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. /* Generated by tools/bookmaker from include/core/SkPicture.h and docs/SkPicture_Reference.bmh
  8. on 2018-08-10 12:59:44. Additional documentation and examples can be found at:
  9. https://skia.org/user/api/SkPicture_Reference
  10. You may edit either file directly. Structural changes to public interfaces require
  11. editing both files. After editing docs/SkPicture_Reference.bmh, run:
  12. bookmaker -b docs -i include/core/SkPicture.h -p
  13. to create an updated version of this file.
  14. */
  15. #ifndef SkPicture_DEFINED
  16. #define SkPicture_DEFINED
  17. #include "SkRefCnt.h"
  18. #include "SkRect.h"
  19. #include "SkTypes.h"
  20. class SkCanvas;
  21. class SkData;
  22. struct SkDeserialProcs;
  23. class SkImage;
  24. struct SkSerialProcs;
  25. class SkStream;
  26. class SkWStream;
  27. /** \class SkPicture
  28. SkPicture records drawing commands made to SkCanvas. The command stream may be
  29. played in whole or in part at a later time.
  30. SkPicture is an abstract class. SkPicture may be generated by SkPictureRecorder
  31. or SkDrawable, or from SkPicture previously saved to SkData or SkStream.
  32. SkPicture may contain any SkCanvas drawing command, as well as one or more
  33. SkCanvas matrix or SkCanvas clip. SkPicture has a cull SkRect, which is used as
  34. a bounding box hint. To limit SkPicture bounds, use SkCanvas clip when
  35. recording or drawing SkPicture.
  36. */
  37. class SK_API SkPicture : public SkRefCnt {
  38. public:
  39. /** Recreates SkPicture that was serialized into a stream. Returns constructed SkPicture
  40. if successful; otherwise, returns nullptr. Fails if data does not permit
  41. constructing valid SkPicture.
  42. procs->fPictureProc permits supplying a custom function to decode SkPicture.
  43. If procs->fPictureProc is nullptr, default decoding is used. procs->fPictureCtx
  44. may be used to provide user context to procs->fPictureProc; procs->fPictureProc
  45. is called with a pointer to data, data byte length, and user context.
  46. @param stream container for serial data
  47. @param procs custom serial data decoders; may be nullptr
  48. @return SkPicture constructed from stream data
  49. */
  50. static sk_sp<SkPicture> MakeFromStream(SkStream* stream,
  51. const SkDeserialProcs* procs = nullptr);
  52. /** Recreates SkPicture that was serialized into data. Returns constructed SkPicture
  53. if successful; otherwise, returns nullptr. Fails if data does not permit
  54. constructing valid SkPicture.
  55. procs->fPictureProc permits supplying a custom function to decode SkPicture.
  56. If procs->fPictureProc is nullptr, default decoding is used. procs->fPictureCtx
  57. may be used to provide user context to procs->fPictureProc; procs->fPictureProc
  58. is called with a pointer to data, data byte length, and user context.
  59. @param data container for serial data
  60. @param procs custom serial data decoders; may be nullptr
  61. @return SkPicture constructed from data
  62. */
  63. static sk_sp<SkPicture> MakeFromData(const SkData* data,
  64. const SkDeserialProcs* procs = nullptr);
  65. /**
  66. @param data pointer to serial data
  67. @param size size of data
  68. @param procs custom serial data decoders; may be nullptr
  69. @return SkPicture constructed from data
  70. */
  71. static sk_sp<SkPicture> MakeFromData(const void* data, size_t size,
  72. const SkDeserialProcs* procs = nullptr);
  73. /** \class SkPicture::AbortCallback
  74. AbortCallback is an abstract class. An implementation of AbortCallback may
  75. passed as a parameter to SkPicture::playback, to stop it before all drawing
  76. commands have been processed.
  77. If AbortCallback::abort returns true, SkPicture::playback is interrupted.
  78. */
  79. class SK_API AbortCallback {
  80. public:
  81. /** Has no effect.
  82. @return abstract class cannot be instantiated
  83. */
  84. AbortCallback() {}
  85. /** Has no effect.
  86. */
  87. virtual ~AbortCallback() {}
  88. /** Stops SkPicture playback when some condition is met. A subclass of
  89. AbortCallback provides an override for abort() that can stop SkPicture::playback.
  90. The part of SkPicture drawn when aborted is undefined. SkPicture instantiations are
  91. free to stop drawing at different points during playback.
  92. If the abort happens inside one or more calls to SkCanvas::save(), stack
  93. of SkCanvas matrix and SkCanvas clip values is restored to its state before
  94. SkPicture::playback was called.
  95. @return true to stop playback
  96. */
  97. virtual bool abort() = 0;
  98. };
  99. /** Replays the drawing commands on the specified canvas. In the case that the
  100. commands are recorded, each command in the SkPicture is sent separately to canvas.
  101. To add a single command to draw SkPicture to recording canvas, call
  102. SkCanvas::drawPicture instead.
  103. @param canvas receiver of drawing commands
  104. @param callback allows interruption of playback
  105. */
  106. virtual void playback(SkCanvas* canvas, AbortCallback* callback = nullptr) const = 0;
  107. /** Returns cull SkRect for this picture, passed in when SkPicture was created.
  108. Returned SkRect does not specify clipping SkRect for SkPicture; cull is hint
  109. of SkPicture bounds.
  110. SkPicture is free to discard recorded drawing commands that fall outside
  111. cull.
  112. @return bounds passed when SkPicture was created
  113. */
  114. virtual SkRect cullRect() const = 0;
  115. /** Returns a non-zero value unique among SkPicture in Skia process.
  116. @return identifier for SkPicture
  117. */
  118. uint32_t uniqueID() const;
  119. /** Returns storage containing SkData describing SkPicture, using optional custom
  120. encoders.
  121. procs->fPictureProc permits supplying a custom function to encode SkPicture.
  122. If procs->fPictureProc is nullptr, default encoding is used. procs->fPictureCtx
  123. may be used to provide user context to procs->fPictureProc; procs->fPictureProc
  124. is called with a pointer to SkPicture and user context.
  125. @param procs custom serial data encoders; may be nullptr
  126. @return storage containing serialized SkPicture
  127. */
  128. sk_sp<SkData> serialize(const SkSerialProcs* procs = nullptr) const;
  129. /** Writes picture to stream, using optional custom encoders.
  130. procs->fPictureProc permits supplying a custom function to encode SkPicture.
  131. If procs->fPictureProc is nullptr, default encoding is used. procs->fPictureCtx
  132. may be used to provide user context to procs->fPictureProc; procs->fPictureProc
  133. is called with a pointer to SkPicture and user context.
  134. @param stream writable serial data stream
  135. @param procs custom serial data encoders; may be nullptr
  136. */
  137. void serialize(SkWStream* stream, const SkSerialProcs* procs = nullptr) const;
  138. /** Returns a placeholder SkPicture. Result does not draw, and contains only
  139. cull SkRect, a hint of its bounds. Result is immutable; it cannot be changed
  140. later. Result identifier is unique.
  141. Returned placeholder can be intercepted during playback to insert other
  142. commands into SkCanvas draw stream.
  143. @param cull placeholder dimensions
  144. @return placeholder with unique identifier
  145. */
  146. static sk_sp<SkPicture> MakePlaceholder(SkRect cull);
  147. /** Returns the approximate number of operations in SkPicture. Returned value
  148. may be greater or less than the number of SkCanvas calls
  149. recorded: some calls may be recorded as more than one operation, other
  150. calls may be optimized away.
  151. @return approximate operation count
  152. */
  153. virtual int approximateOpCount() const = 0;
  154. /** Returns the approximate byte size of SkPicture. Does not include large objects
  155. referenced by SkPicture.
  156. @return approximate size
  157. */
  158. virtual size_t approximateBytesUsed() const = 0;
  159. private:
  160. // Subclass whitelist.
  161. SkPicture();
  162. friend class SkBigPicture;
  163. friend class SkEmptyPicture;
  164. friend class SkPicturePriv;
  165. template <typename> friend class SkMiniPicture;
  166. void serialize(SkWStream*, const SkSerialProcs*, class SkRefCntSet* typefaces) const;
  167. static sk_sp<SkPicture> MakeFromStream(SkStream*, const SkDeserialProcs*,
  168. class SkTypefacePlayback*);
  169. friend class SkPictureData;
  170. /** Return true if the SkStream/Buffer represents a serialized picture, and
  171. fills out SkPictInfo. After this function returns, the data source is not
  172. rewound so it will have to be manually reset before passing to
  173. MakeFromStream or MakeFromBuffer. Note, MakeFromStream and
  174. MakeFromBuffer perform this check internally so these entry points are
  175. intended for stand alone tools.
  176. If false is returned, SkPictInfo is unmodified.
  177. */
  178. static bool StreamIsSKP(SkStream*, struct SkPictInfo*);
  179. static bool BufferIsSKP(class SkReadBuffer*, struct SkPictInfo*);
  180. friend bool SkPicture_StreamIsSKP(SkStream*, struct SkPictInfo*);
  181. // Returns NULL if this is not an SkBigPicture.
  182. virtual const class SkBigPicture* asSkBigPicture() const { return nullptr; }
  183. friend struct SkPathCounter;
  184. // V35: Store SkRect (rather then width & height) in header
  185. // V36: Remove (obsolete) alphatype from SkColorTable
  186. // V37: Added shadow only option to SkDropShadowImageFilter (last version to record CLEAR)
  187. // V38: Added PictureResolution option to SkPictureImageFilter
  188. // V39: Added FilterLevel option to SkPictureImageFilter
  189. // V40: Remove UniqueID serialization from SkImageFilter.
  190. // V41: Added serialization of SkBitmapSource's filterQuality parameter
  191. // V42: Added a bool to SkPictureShader serialization to indicate did-we-serialize-a-picture?
  192. // V43: Added DRAW_IMAGE and DRAW_IMAGE_RECT opt codes to serialized data
  193. // V44: Move annotations from paint to drawAnnotation
  194. // V45: Add invNormRotation to SkLightingShader.
  195. // V46: Add drawTextRSXform
  196. // V47: Add occluder rect to SkBlurMaskFilter
  197. // V48: Read and write extended SkTextBlobs.
  198. // V49: Gradients serialized as SkColor4f + SkColorSpace
  199. // V50: SkXfermode -> SkBlendMode
  200. // V51: more SkXfermode -> SkBlendMode
  201. // V52: Remove SkTextBlob::fRunCount
  202. // V53: SaveLayerRec clip mask
  203. // V54: ComposeShader can use a Mode or a Lerp
  204. // V55: Drop blendmode[] from MergeImageFilter
  205. // V56: Add TileMode in SkBlurImageFilter.
  206. // V57: Sweep tiling info.
  207. // V58: No more 2pt conical flipping.
  208. // V59: No more LocalSpace option on PictureImageFilter
  209. // V60: Remove flags in picture header
  210. // V61: Change SkDrawPictureRec to take two colors rather than two alphas
  211. // V62: Don't negate size of custom encoded images (don't write origin x,y either)
  212. // V63: Store image bounds (including origin) instead of just width/height to support subsets
  213. // V64: Remove occluder feature from blur maskFilter
  214. // Only SKPs within the min/current picture version range (inclusive) can be read.
  215. static const uint32_t MIN_PICTURE_VERSION = 56; // august 2017
  216. static const uint32_t CURRENT_PICTURE_VERSION = 65;
  217. static_assert(MIN_PICTURE_VERSION <= 62, "Remove kFontAxes_bad from SkFontDescriptor.cpp");
  218. static bool IsValidPictInfo(const struct SkPictInfo& info);
  219. static sk_sp<SkPicture> Forwardport(const struct SkPictInfo&,
  220. const class SkPictureData*,
  221. class SkReadBuffer* buffer);
  222. struct SkPictInfo createHeader() const;
  223. class SkPictureData* backport() const;
  224. mutable uint32_t fUniqueID;
  225. };
  226. #endif