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