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