| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2007 The Android Open Source Project | 2 * Copyright 2007 The Android Open Source Project |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 | |
| 9 #ifndef SkPicture_DEFINED | 8 #ifndef SkPicture_DEFINED |
| 10 #define SkPicture_DEFINED | 9 #define SkPicture_DEFINED |
| 11 | 10 |
| 12 #include "SkImageDecoder.h" | 11 #include "SkImageDecoder.h" |
| 13 #include "SkLazyPtr.h" | 12 #include "SkLazyPtr.h" |
| 14 #include "SkRefCnt.h" | 13 #include "SkRefCnt.h" |
| 15 #include "SkTDArray.h" | 14 #include "SkTypes.h" |
| 16 | 15 |
| 17 #if SK_SUPPORT_GPU | |
| 18 class GrContext; | 16 class GrContext; |
| 19 #endif | 17 class SkBigPicture; |
| 20 | |
| 21 class SkBitmap; | 18 class SkBitmap; |
| 22 class SkBBoxHierarchy; | |
| 23 class SkCanvas; | 19 class SkCanvas; |
| 24 class SkData; | |
| 25 class SkPictureData; | 20 class SkPictureData; |
| 26 class SkPixelSerializer; | 21 class SkPixelSerializer; |
| 27 class SkStream; | 22 class SkStream; |
| 28 class SkWStream; | 23 class SkWStream; |
| 29 | |
| 30 struct SkPictInfo; | 24 struct SkPictInfo; |
| 31 | 25 |
| 32 class SkRecord; | |
| 33 | |
| 34 namespace SkRecords { | |
| 35 class CollectLayers; | |
| 36 }; | |
| 37 | |
| 38 /** \class SkPicture | 26 /** \class SkPicture |
| 39 | 27 |
| 40 The SkPicture class records the drawing commands made to a canvas, to | 28 An SkPicture records drawing commands made to a canvas to be played back at
a later time. |
| 41 be played back at a later time. | 29 This base class handles serialization and a few other miscellany. |
| 42 */ | 30 */ |
| 31 // TODO(mtklein): logically this could be : public SkRefCnt, but that exposes |
| 32 // some ref-count adoption bugs in Blink. Keeping this using SkNVRefCnt |
| 33 // happens to stifle them for now. skia:3847 |
| 43 class SK_API SkPicture : public SkNVRefCnt<SkPicture> { | 34 class SK_API SkPicture : public SkNVRefCnt<SkPicture> { |
| 44 public: | 35 public: |
| 45 // AccelData provides a base class for device-specific acceleration data. | 36 virtual ~SkPicture(); |
| 46 class AccelData : public SkRefCnt { | |
| 47 public: | |
| 48 typedef uint8_t Domain; | |
| 49 typedef uint32_t Key; | |
| 50 | |
| 51 AccelData(Key key) : fKey(key) { } | |
| 52 | |
| 53 const Key& getKey() const { return fKey; } | |
| 54 | |
| 55 // This entry point allows user's to get a unique domain prefix | |
| 56 // for their keys | |
| 57 static Domain GenerateDomain(); | |
| 58 private: | |
| 59 Key fKey; | |
| 60 }; | |
| 61 | |
| 62 /** PRIVATE / EXPERIMENTAL -- do not call */ | |
| 63 const AccelData* EXPERIMENTAL_getAccelData(AccelData::Key) const; | |
| 64 | 37 |
| 65 /** | 38 /** |
| 66 * Function signature defining a function that sets up an SkBitmap from enc
oded data. On | 39 * Function signature defining a function that sets up an SkBitmap from enc
oded data. On |
| 67 * success, the SkBitmap should have its Config, width, height, rowBytes an
d pixelref set. | 40 * success, the SkBitmap should have its Config, width, height, rowBytes an
d pixelref set. |
| 68 * If the installed pixelref has decoded the data into pixels, then the src
buffer need not be | 41 * If the installed pixelref has decoded the data into pixels, then the src
buffer need not be |
| 69 * copied. If the pixelref defers the actual decode until its lockPixels()
is called, then it | 42 * copied. If the pixelref defers the actual decode until its lockPixels()
is called, then it |
| 70 * must make a copy of the src buffer. | 43 * must make a copy of the src buffer. |
| 71 * @param src Encoded data. | 44 * @param src Encoded data. |
| 72 * @param length Size of the encoded data, in bytes. | 45 * @param length Size of the encoded data, in bytes. |
| 73 * @param dst SkBitmap to install the pixel ref on. | 46 * @param dst SkBitmap to install the pixel ref on. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 89 /** | 62 /** |
| 90 * Recreate a picture that was serialized into a buffer. If the creation re
quires bitmap | 63 * Recreate a picture that was serialized into a buffer. If the creation re
quires bitmap |
| 91 * decoding, the decoder must be set on the SkReadBuffer parameter by calli
ng | 64 * decoding, the decoder must be set on the SkReadBuffer parameter by calli
ng |
| 92 * SkReadBuffer::setBitmapDecoder() before calling SkPicture::CreateFromBuf
fer(). | 65 * SkReadBuffer::setBitmapDecoder() before calling SkPicture::CreateFromBuf
fer(). |
| 93 * @param SkReadBuffer Serialized picture data. | 66 * @param SkReadBuffer Serialized picture data. |
| 94 * @return A new SkPicture representing the serialized data, or NULL if the
buffer is | 67 * @return A new SkPicture representing the serialized data, or NULL if the
buffer is |
| 95 * invalid. | 68 * invalid. |
| 96 */ | 69 */ |
| 97 static SkPicture* CreateFromBuffer(SkReadBuffer&); | 70 static SkPicture* CreateFromBuffer(SkReadBuffer&); |
| 98 | 71 |
| 99 ~SkPicture(); | |
| 100 | |
| 101 /** | 72 /** |
| 102 * Subclasses of this can be passed to playback(). During the playback | 73 * Subclasses of this can be passed to playback(). During the playback |
| 103 * of the picture, this callback will periodically be invoked. If its | 74 * of the picture, this callback will periodically be invoked. If its |
| 104 * abort() returns true, then picture playback will be interrupted. | 75 * abort() returns true, then picture playback will be interrupted. |
| 105 * | 76 * |
| 106 * The resulting drawing is undefined, as there is no guarantee how often th
e | 77 * The resulting drawing is undefined, as there is no guarantee how often th
e |
| 107 * callback will be invoked. If the abort happens inside some level of neste
d | 78 * callback will be invoked. If the abort happens inside some level of neste
d |
| 108 * calls to save(), restore will automatically be called to return the state | 79 * calls to save(), restore will automatically be called to return the state |
| 109 * to the same level it was before the playback call was made. | 80 * to the same level it was before the playback call was made. |
| 110 */ | 81 */ |
| 111 class SK_API AbortCallback { | 82 class SK_API AbortCallback { |
| 112 public: | 83 public: |
| 113 AbortCallback() {} | 84 AbortCallback() {} |
| 114 virtual ~AbortCallback() {} | 85 virtual ~AbortCallback() {} |
| 115 | |
| 116 virtual bool abort() = 0; | 86 virtual bool abort() = 0; |
| 117 }; | 87 }; |
| 118 | 88 |
| 119 /** Replays the drawing commands on the specified canvas. Note that | 89 /** Replays the drawing commands on the specified canvas. Note that |
| 120 this has the effect of unfurling this picture into the destination | 90 this has the effect of unfurling this picture into the destination |
| 121 canvas. Using the SkCanvas::drawPicture entry point gives the destinatio
n | 91 canvas. Using the SkCanvas::drawPicture entry point gives the destinatio
n |
| 122 canvas the option of just taking a ref. | 92 canvas the option of just taking a ref. |
| 123 @param canvas the canvas receiving the drawing commands. | 93 @param canvas the canvas receiving the drawing commands. |
| 124 @param callback a callback that allows interruption of playback | 94 @param callback a callback that allows interruption of playback |
| 125 */ | 95 */ |
| 126 void playback(SkCanvas* canvas, AbortCallback* = NULL) const; | 96 virtual void playback(SkCanvas*, AbortCallback* = NULL) const = 0; |
| 127 | 97 |
| 128 /** Return the cull rect used when creating this picture: { 0, 0, cullWidth,
cullHeight }. | 98 /** Return a cull rect for this picture. |
| 129 It does not necessarily reflect the bounds of what has been recorded int
o the picture. | 99 Ops recorded into this picture that attempt to draw outside the cull mig
ht not be drawn. |
| 130 @return the cull rect used to create this picture | 100 */ |
| 131 */ | 101 virtual SkRect cullRect() const = 0; |
| 132 SkRect cullRect() const { return fCullRect; } | |
| 133 | 102 |
| 134 /** Return a non-zero, unique value representing the picture. | 103 /** Returns a non-zero value unique among all pictures. */ |
| 135 */ | |
| 136 uint32_t uniqueID() const; | 104 uint32_t uniqueID() const; |
| 137 | 105 |
| 138 /** | 106 /** |
| 139 * Serialize to a stream. If non NULL, serializer will be used to serialize | 107 * Serialize to a stream. If non NULL, serializer will be used to serialize |
| 140 * any bitmaps in the picture. | 108 * any bitmaps in the picture. |
| 141 * | 109 * |
| 142 * TODO: Use serializer to serialize SkImages as well. | 110 * TODO: Use serializer to serialize SkImages as well. |
| 143 */ | 111 */ |
| 144 void serialize(SkWStream*, SkPixelSerializer* serializer = NULL) const; | 112 void serialize(SkWStream*, SkPixelSerializer* = NULL) const; |
| 145 | 113 |
| 146 /** | 114 /** |
| 147 * Serialize to a buffer. | 115 * Serialize to a buffer. |
| 148 */ | 116 */ |
| 149 void flatten(SkWriteBuffer&) const; | 117 void flatten(SkWriteBuffer&) const; |
| 150 | 118 |
| 151 /** | 119 /** |
| 152 * Returns true if any bitmaps may be produced when this SkPicture | 120 * Returns true if any bitmaps may be produced when this SkPicture |
| 153 * is replayed. | 121 * is replayed. |
| 154 */ | 122 */ |
| 155 bool willPlayBackBitmaps() const; | 123 virtual bool willPlayBackBitmaps() const = 0; |
| 124 |
| 125 /** Return the approximate number of operations in this picture. This |
| 126 * number may be greater or less than the number of SkCanvas calls |
| 127 * recorded: some calls may be recorded as more than one operation, or some |
| 128 * calls may be optimized away. |
| 129 */ |
| 130 virtual int approximateOpCount() const = 0; |
| 131 |
| 132 /** Return true if this picture contains text. |
| 133 */ |
| 134 virtual bool hasText() const = 0; |
| 135 |
| 136 /** Returns the approximate byte size of this picture, not including large r
ef'd objects. */ |
| 137 virtual size_t approximateBytesUsed() const = 0; |
| 156 | 138 |
| 157 /** Return true if the SkStream/Buffer represents a serialized picture, and | 139 /** Return true if the SkStream/Buffer represents a serialized picture, and |
| 158 fills out SkPictInfo. After this function returns, the data source is no
t | 140 fills out SkPictInfo. After this function returns, the data source is no
t |
| 159 rewound so it will have to be manually reset before passing to | 141 rewound so it will have to be manually reset before passing to |
| 160 CreateFromStream or CreateFromBuffer. Note, CreateFromStream and | 142 CreateFromStream or CreateFromBuffer. Note, CreateFromStream and |
| 161 CreateFromBuffer perform this check internally so these entry points are | 143 CreateFromBuffer perform this check internally so these entry points are |
| 162 intended for stand alone tools. | 144 intended for stand alone tools. |
| 163 If false is returned, SkPictInfo is unmodified. | 145 If false is returned, SkPictInfo is unmodified. |
| 164 */ | 146 */ |
| 165 static bool InternalOnly_StreamIsSKP(SkStream*, SkPictInfo*); | 147 static bool InternalOnly_StreamIsSKP(SkStream*, SkPictInfo*); |
| 166 static bool InternalOnly_BufferIsSKP(SkReadBuffer*, SkPictInfo*); | 148 static bool InternalOnly_BufferIsSKP(SkReadBuffer*, SkPictInfo*); |
| 167 | 149 |
| 168 /** Return true if the picture is suitable for rendering on the GPU. | 150 /** Return true if the picture is suitable for rendering on the GPU. */ |
| 169 */ | 151 bool suitableForGpuRasterization(GrContext*, const char** whyNot = NULL) con
st; |
| 170 | |
| 171 #if SK_SUPPORT_GPU | |
| 172 bool suitableForGpuRasterization(GrContext*, const char ** = NULL) const; | |
| 173 #endif | |
| 174 | |
| 175 /** Return the approximate number of operations in this picture. This | |
| 176 * number may be greater or less than the number of SkCanvas calls | |
| 177 * recorded: some calls may be recorded as more than one operation, or some | |
| 178 * calls may be optimized away. | |
| 179 */ | |
| 180 int approximateOpCount() const; | |
| 181 | |
| 182 /** Return true if this picture contains text. | |
| 183 */ | |
| 184 bool hasText() const; | |
| 185 | |
| 186 // An array of refcounted const SkPicture pointers. | |
| 187 class SnapshotArray : ::SkNoncopyable { | |
| 188 public: | |
| 189 SnapshotArray(const SkPicture* pics[], int count) : fPics(pics), fCount(
count) {} | |
| 190 ~SnapshotArray() { for (int i = 0; i < fCount; i++) { fPics[i]->unref();
} } | |
| 191 | |
| 192 const SkPicture* const* begin() const { return fPics; } | |
| 193 int count() const { return fCount; } | |
| 194 private: | |
| 195 SkAutoTMalloc<const SkPicture*> fPics; | |
| 196 int fCount; | |
| 197 }; | |
| 198 | 152 |
| 199 // Sent via SkMessageBus from destructor. | 153 // Sent via SkMessageBus from destructor. |
| 200 struct DeletionMessage { int32_t fUniqueID; }; | 154 struct DeletionMessage { int32_t fUniqueID; }; // TODO: -> uint32_t? |
| 155 |
| 156 // Returns NULL if this is not an SkBigPicture. |
| 157 virtual const SkBigPicture* asSkBigPicture() const { return NULL; } |
| 201 | 158 |
| 202 private: | 159 private: |
| 203 // V2 : adds SkPixelRef's generation ID. | 160 // Subclass whitelist. |
| 204 // V3 : PictInfo tag at beginning, and EOF tag at the end | 161 SkPicture(); |
| 205 // V4 : move SkPictInfo to be the header | 162 friend class SkBigPicture; |
| 206 // V5 : don't read/write FunctionPtr on cross-process (we can detect that) | 163 friend class SkEmptyPicture; |
| 207 // V6 : added serialization of SkPath's bounds (and packed its flags tighter
) | 164 template <typename> friend class SkMiniPicture; |
| 208 // V7 : changed drawBitmapRect(IRect) to drawBitmapRectToRect(Rect) | 165 |
| 209 // V8 : Add an option for encoding bitmaps | 166 virtual int numSlowPaths() const = 0; |
| 210 // V9 : Allow the reader and writer of an SKP disagree on whether to support | 167 friend struct SkPathCounter; |
| 211 // SK_SUPPORT_HINTING_SCALE_FACTOR | 168 |
| 212 // V10: add drawRRect, drawOval, clipRRect | |
| 213 // V11: modify how readBitmap and writeBitmap store their info. | |
| 214 // V12: add conics to SkPath, use new SkPathRef flattening | |
| 215 // V13: add flag to drawBitmapRectToRect | |
| 216 // parameterize blurs by sigma rather than radius | |
| 217 // V14: Add flags word to PathRef serialization | |
| 218 // V15: Remove A1 bitmap config (and renumber remaining configs) | |
| 219 // V16: Move SkPath's isOval flag to SkPathRef | |
| 220 // V17: SkPixelRef now writes SkImageInfo | |
| 221 // V18: SkBitmap now records x,y for its pixelref origin, instead of offset. | |
| 222 // V19: encode matrices and regions into the ops stream | |
| 223 // V20: added bool to SkPictureImageFilter's serialization (to allow SkPictu
re serialization) | |
| 224 // V21: add pushCull, popCull | |
| 225 // V22: SK_PICT_FACTORY_TAG's size is now the chunk size in bytes | |
| 226 // V23: SkPaint::FilterLevel became a real enum | |
| 227 // V24: SkTwoPointConicalGradient now has fFlipped flag for gradient flippin
g | |
| 228 // V25: SkDashPathEffect now only writes phase and interval array when flatt
ening | |
| 229 // V26: Removed boolean from SkColorShader for inheriting color from SkPaint
. | |
| 230 // V27: Remove SkUnitMapper from gradients (and skia). | |
| 231 // V28: No longer call bitmap::flatten inside SkWriteBuffer::writeBitmap. | |
| 232 // V29: Removed SaveFlags parameter from save(). | |
| 233 // V30: Remove redundant SkMatrix from SkLocalMatrixShader. | |
| 234 // V31: Add a serialized UniqueID to SkImageFilter. | |
| 235 // V32: Removed SkPaintOptionsAndroid from SkPaint | |
| 236 // V33: Serialize only public API of effects. | |
| 237 // V34: Add SkTextBlob serialization. | |
| 238 // V35: Store SkRect (rather then width & height) in header | 169 // V35: Store SkRect (rather then width & height) in header |
| 239 // V36: Remove (obsolete) alphatype from SkColorTable | 170 // V36: Remove (obsolete) alphatype from SkColorTable |
| 240 // V37: Added shadow only option to SkDropShadowImageFilter (last version to
record CLEAR) | 171 // V37: Added shadow only option to SkDropShadowImageFilter (last version to
record CLEAR) |
| 241 // V38: Added PictureResolution option to SkPictureImageFilter | 172 // V38: Added PictureResolution option to SkPictureImageFilter |
| 242 // V39: Added FilterLevel option to SkPictureImageFilter | 173 // V39: Added FilterLevel option to SkPictureImageFilter |
| 243 // V40: Remove UniqueID serialization from SkImageFilter. | 174 // V40: Remove UniqueID serialization from SkImageFilter. |
| 244 // V41: Added serialization of SkBitmapSource's filterQuality parameter | 175 // V41: Added serialization of SkBitmapSource's filterQuality parameter |
| 245 | 176 |
| 246 // Note: If the picture version needs to be increased then please follow the | |
| 247 // steps to generate new SKPs in (only accessible to Googlers): http://goo.g
l/qATVcw | |
| 248 | |
| 249 // Only SKPs within the min/current picture version range (inclusive) can be
read. | 177 // Only SKPs within the min/current picture version range (inclusive) can be
read. |
| 250 static const uint32_t MIN_PICTURE_VERSION = 35; // Produced by Chrome M3
9. | 178 static const uint32_t MIN_PICTURE_VERSION = 35; // Produced by Chrom
e M39. |
| 251 static const uint32_t CURRENT_PICTURE_VERSION = 41; | 179 static const uint32_t CURRENT_PICTURE_VERSION = 41; |
| 252 | 180 |
| 253 static_assert(MIN_PICTURE_VERSION <= 41, | 181 static_assert(MIN_PICTURE_VERSION <= 41, |
| 254 "Remove kFontFileName and related code from SkFontDescriptor.c
pp."); | 182 "Remove kFontFileName and related code from SkFontDescriptor.c
pp."); |
| 255 | 183 |
| 256 void createHeader(SkPictInfo* info) const; | |
| 257 static bool IsValidPictInfo(const SkPictInfo& info); | 184 static bool IsValidPictInfo(const SkPictInfo& info); |
| 185 static SkPicture* Forwardport(const SkPictInfo&, const SkPictureData*); |
| 258 | 186 |
| 259 // Takes ownership of the (optional) SnapshotArray. | 187 SkPictInfo createHeader() const; |
| 260 // For performance, we take ownership of the caller's refs on the SkRecord,
BBH, and AccelData. | 188 SkPictureData* backport() const; |
| 261 SkPicture(const SkRect& cullRect, | |
| 262 SkRecord*, | |
| 263 SnapshotArray*, | |
| 264 SkBBoxHierarchy*, | |
| 265 AccelData*, | |
| 266 size_t approxBytesUsedBySubPictures); | |
| 267 | 189 |
| 268 static SkPicture* Forwardport(const SkPictInfo&, const SkPictureData*); | 190 mutable uint32_t fUniqueID; |
| 269 static SkPictureData* Backport(const SkRecord&, const SkPictInfo&, | |
| 270 SkPicture const* const drawablePics[], int dr
awableCount); | |
| 271 | |
| 272 // uint32_t fRefCnt; from SkNVRefCnt<SkPicture> | |
| 273 mutable uint32_t fUniqueID; | |
| 274 const SkRect fCullRect; | |
| 275 SkAutoTUnref<const SkRecord> fRecord; | |
| 276 SkAutoTDelete<const SnapshotArray> fDrawablePicts; | |
| 277 SkAutoTUnref<const SkBBoxHierarchy> fBBH; | |
| 278 SkAutoTUnref<const AccelData> fAccelData; | |
| 279 const size_t fApproxBytesUsedBySubPictures; | |
| 280 | |
| 281 // helpers for fDrawablePicts | |
| 282 int drawableCount() const; | |
| 283 // will return NULL if drawableCount() returns 0 | |
| 284 SkPicture const* const* drawablePicts() const; | |
| 285 | |
| 286 struct PathCounter; | |
| 287 | |
| 288 struct Analysis { | |
| 289 Analysis() {} // Only used by SkPictureData codepath. | |
| 290 explicit Analysis(const SkRecord&); | |
| 291 | |
| 292 bool suitableForGpuRasterization(const char** reason, int sampleCount) c
onst; | |
| 293 | |
| 294 uint8_t fNumSlowPathsAndDashEffects; | |
| 295 bool fWillPlaybackBitmaps : 1; | |
| 296 bool fHasText : 1; | |
| 297 }; | |
| 298 SkLazyPtr<Analysis> fAnalysis; | |
| 299 const Analysis& analysis() const; | |
| 300 | |
| 301 friend class SkPictureRecorder; // SkRecord-based constructor. | |
| 302 friend class GrLayerHoister; // access to fRecord | |
| 303 friend class ReplaceDraw; | |
| 304 friend class SkPictureUtils; | |
| 305 friend class SkRecordedDrawable; | |
| 306 }; | 191 }; |
| 307 SK_COMPILE_ASSERT(sizeof(SkPicture) <= 88, SkPictureSize); | |
| 308 | 192 |
| 309 #endif | 193 #endif |
| OLD | NEW |