SkPicture.h revision 643b8bd6617e333f7a970a57ad9166f3d6675d1a
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 "SkImageDecoder.h" 15#include "SkRefCnt.h" 16 17#if SK_SUPPORT_GPU 18class GrContext; 19#endif 20 21class SkBBHFactory; 22class SkBBoxHierarchy; 23class SkCanvas; 24class SkDrawPictureCallback; 25class SkData; 26class SkPathHeap; 27class SkPicturePlayback; 28class SkPictureRecord; 29class SkStream; 30class SkWStream; 31 32struct SkPictInfo; 33 34/** \class SkPicture 35 36 The SkPicture class records the drawing commands made to a canvas, to 37 be played back at a later time. 38*/ 39class SK_API SkPicture : public SkRefCnt { 40public: 41 SK_DECLARE_INST_COUNT(SkPicture) 42 43 // AccelData provides a base class for device-specific acceleration 44 // data. It is added to the picture via a call to a device's optimize 45 // method. 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 typedef SkRefCnt INHERITED; 62 }; 63 64 SkPicture(); 65 /** Make a copy of the contents of src. If src records more drawing after 66 this call, those elements will not appear in this picture. 67 */ 68 SkPicture(const SkPicture& src); 69 70 /** PRIVATE / EXPERIMENTAL -- do not call */ 71 void EXPERIMENTAL_addAccelData(const AccelData* data) const { 72 SkRefCnt_SafeAssign(fAccelData, data); 73 } 74 /** PRIVATE / EXPERIMENTAL -- do not call */ 75 const AccelData* EXPERIMENTAL_getAccelData(AccelData::Key key) const { 76 if (NULL != fAccelData && fAccelData->getKey() == key) { 77 return fAccelData; 78 } 79 return NULL; 80 } 81 82 /** 83 * Function signature defining a function that sets up an SkBitmap from encoded data. On 84 * success, the SkBitmap should have its Config, width, height, rowBytes and pixelref set. 85 * If the installed pixelref has decoded the data into pixels, then the src buffer need not be 86 * copied. If the pixelref defers the actual decode until its lockPixels() is called, then it 87 * must make a copy of the src buffer. 88 * @param src Encoded data. 89 * @param length Size of the encoded data, in bytes. 90 * @param dst SkBitmap to install the pixel ref on. 91 * @param bool Whether or not a pixel ref was successfully installed. 92 */ 93 typedef bool (*InstallPixelRefProc)(const void* src, size_t length, SkBitmap* dst); 94 95 /** 96 * Recreate a picture that was serialized into a stream. 97 * @param SkStream Serialized picture data. 98 * @param proc Function pointer for installing pixelrefs on SkBitmaps representing the 99 * encoded bitmap data from the stream. 100 * @return A new SkPicture representing the serialized data, or NULL if the stream is 101 * invalid. 102 */ 103 static SkPicture* CreateFromStream(SkStream*, 104 InstallPixelRefProc proc = &SkImageDecoder::DecodeMemory); 105 106 /** 107 * Recreate a picture that was serialized into a buffer. If the creation requires bitmap 108 * decoding, the decoder must be set on the SkReadBuffer parameter by calling 109 * SkReadBuffer::setBitmapDecoder() before calling SkPicture::CreateFromBuffer(). 110 * @param SkReadBuffer Serialized picture data. 111 * @return A new SkPicture representing the serialized data, or NULL if the buffer is 112 * invalid. 113 */ 114 static SkPicture* CreateFromBuffer(SkReadBuffer&); 115 116 virtual ~SkPicture(); 117 118 /** 119 * Swap the contents of the two pictures. Guaranteed to succeed. 120 */ 121 void swap(SkPicture& other); 122 123 /** 124 * Creates a thread-safe clone of the picture that is ready for playback. 125 */ 126 SkPicture* clone() const; 127 128 /** 129 * Creates multiple thread-safe clones of this picture that are ready for 130 * playback. The resulting clones are stored in the provided array of 131 * SkPictures. 132 */ 133 void clone(SkPicture* pictures, int count) const; 134 135 // TODO: kUsePathBoundsForClip_RecordingFlag no longer belongs in 136 // SkPicture. It should be moved to SkPictureRecorder (or just made 137 // the default behavior). 138 enum RecordingFlags { 139 /* This flag specifies that when clipPath() is called, the path will 140 be faithfully recorded, but the recording canvas' current clip will 141 only see the path's bounds. This speeds up the recording process 142 without compromising the fidelity of the playback. The only side- 143 effect for recording is that calling getTotalClip() or related 144 clip-query calls will reflect the path's bounds, not the actual 145 path. 146 */ 147 kUsePathBoundsForClip_RecordingFlag = 0x01 148 }; 149 150 /** Replays the drawing commands on the specified canvas. 151 @param canvas the canvas receiving the drawing commands. 152 */ 153 void draw(SkCanvas* canvas, SkDrawPictureCallback* = NULL) const; 154 155 /** Return the width of the picture's recording canvas. This 156 value reflects what was passed to setSize(), and does not necessarily 157 reflect the bounds of what has been recorded into the picture. 158 @return the width of the picture's recording canvas 159 */ 160 int width() const { return fWidth; } 161 162 /** Return the height of the picture's recording canvas. This 163 value reflects what was passed to setSize(), and does not necessarily 164 reflect the bounds of what has been recorded into the picture. 165 @return the height of the picture's recording canvas 166 */ 167 int height() const { return fHeight; } 168 169 /** Return a non-zero, unique value representing the picture. This call is 170 only valid when not recording. Between a beginRecording/endRecording 171 pair it will just return 0 (the invalid ID). Each beginRecording/ 172 endRecording pair will cause a different generation ID to be returned. 173 */ 174 uint32_t uniqueID() const; 175 176 /** 177 * Function to encode an SkBitmap to an SkData. A function with this 178 * signature can be passed to serialize() and SkWriteBuffer. 179 * Returning NULL will tell the SkWriteBuffer to use 180 * SkBitmap::flatten() to store the bitmap. 181 * 182 * @param pixelRefOffset DEPRECATED -- caller assumes it will return 0. 183 * @return SkData If non-NULL, holds encoded data representing the passed 184 * in bitmap. The caller is responsible for calling unref(). 185 */ 186 typedef SkData* (*EncodeBitmap)(size_t* pixelRefOffset, const SkBitmap& bm); 187 188 /** 189 * Serialize to a stream. If non NULL, encoder will be used to encode 190 * any bitmaps in the picture. 191 * encoder will never be called with a NULL pixelRefOffset. 192 */ 193 void serialize(SkWStream*, EncodeBitmap encoder = NULL) const; 194 195 /** 196 * Serialize to a buffer. 197 */ 198 void flatten(SkWriteBuffer&) const; 199 200 /** 201 * Returns true if any bitmaps may be produced when this SkPicture 202 * is replayed. 203 * Returns false if called while still recording. 204 */ 205 bool willPlayBackBitmaps() const; 206 207#ifdef SK_BUILD_FOR_ANDROID 208 /** Signals that the caller is prematurely done replaying the drawing 209 commands. This can be called from a canvas virtual while the picture 210 is drawing. Has no effect if the picture is not drawing. 211 @deprecated preserving for legacy purposes 212 */ 213 void abortPlayback(); 214#endif 215 216 /** Return true if the SkStream/Buffer represents a serialized picture, and 217 fills out SkPictInfo. After this function returns, the data source is not 218 rewound so it will have to be manually reset before passing to 219 CreateFromStream or CreateFromBuffer. Note, CreateFromStream and 220 CreateFromBuffer perform this check internally so these entry points are 221 intended for stand alone tools. 222 If false is returned, SkPictInfo is unmodified. 223 */ 224 static bool InternalOnly_StreamIsSKP(SkStream*, SkPictInfo*); 225 static bool InternalOnly_BufferIsSKP(SkReadBuffer&, SkPictInfo*); 226 227 /** Return true if the picture is suitable for rendering on the GPU. 228 */ 229 230#if SK_SUPPORT_GPU 231 bool suitableForGpuRasterization(GrContext*, const char ** = NULL) const; 232#endif 233 234protected: 235 // V2 : adds SkPixelRef's generation ID. 236 // V3 : PictInfo tag at beginning, and EOF tag at the end 237 // V4 : move SkPictInfo to be the header 238 // V5 : don't read/write FunctionPtr on cross-process (we can detect that) 239 // V6 : added serialization of SkPath's bounds (and packed its flags tighter) 240 // V7 : changed drawBitmapRect(IRect) to drawBitmapRectToRect(Rect) 241 // V8 : Add an option for encoding bitmaps 242 // V9 : Allow the reader and writer of an SKP disagree on whether to support 243 // SK_SUPPORT_HINTING_SCALE_FACTOR 244 // V10: add drawRRect, drawOval, clipRRect 245 // V11: modify how readBitmap and writeBitmap store their info. 246 // V12: add conics to SkPath, use new SkPathRef flattening 247 // V13: add flag to drawBitmapRectToRect 248 // parameterize blurs by sigma rather than radius 249 // V14: Add flags word to PathRef serialization 250 // V15: Remove A1 bitmap config (and renumber remaining configs) 251 // V16: Move SkPath's isOval flag to SkPathRef 252 // V17: SkPixelRef now writes SkImageInfo 253 // V18: SkBitmap now records x,y for its pixelref origin, instead of offset. 254 // V19: encode matrices and regions into the ops stream 255 // V20: added bool to SkPictureImageFilter's serialization (to allow SkPicture serialization) 256 // V21: add pushCull, popCull 257 // V22: SK_PICT_FACTORY_TAG's size is now the chunk size in bytes 258 // V23: SkPaint::FilterLevel became a real enum 259 // V24: SkTwoPointConicalGradient now has fFlipped flag for gradient flipping 260 // V25: SkDashPathEffect now only writes phase and interval array when flattening 261 // V26: Removed boolean from SkColorShader for inheriting color from SkPaint. 262 // V27: Remove SkUnitMapper from gradients (and skia). 263 // V28: No longer call bitmap::flatten inside SkWriteBuffer::writeBitmap. 264 265 // Note: If the picture version needs to be increased then please follow the 266 // steps to generate new SKPs in (only accessible to Googlers): http://goo.gl/qATVcw 267 268 // Only SKPs within the min/current picture version range (inclusive) can be read. 269 static const uint32_t MIN_PICTURE_VERSION = 19; 270 static const uint32_t CURRENT_PICTURE_VERSION = 28; 271 272 mutable uint32_t fUniqueID; 273 274 // fPlayback, fRecord, fWidth & fHeight are protected to allow derived classes to 275 // install their own SkPicturePlayback-derived players,SkPictureRecord-derived 276 // recorders and set the picture size 277 SkPicturePlayback* fPlayback; 278 int fWidth, fHeight; 279 mutable const AccelData* fAccelData; 280 281 void needsNewGenID() { fUniqueID = SK_InvalidGenID; } 282 283 // Create a new SkPicture from an existing SkPicturePlayback. Ref count of 284 // playback is unchanged. 285 SkPicture(SkPicturePlayback*, int width, int height); 286 287private: 288 friend class SkPictureRecord; 289 friend class SkPictureTester; // for unit testing 290 291 SkAutoTUnref<SkPathHeap> fPathHeap; // reference counted 292 293 // ContentInfo is not serialized! It is intended solely for use 294 // with suitableForGpuRasterization. 295 class ContentInfo { 296 public: 297 ContentInfo() { this->reset(); } 298 299 ContentInfo(const ContentInfo& src) { this->set(src); } 300 301 void set(const ContentInfo& src) { 302 fNumPaintWithPathEffectUses = src.fNumPaintWithPathEffectUses; 303 fNumAAConcavePaths = src.fNumAAConcavePaths; 304 fNumAAHairlineConcavePaths = src.fNumAAHairlineConcavePaths; 305 } 306 307 void reset() { 308 fNumPaintWithPathEffectUses = 0; 309 fNumAAConcavePaths = 0; 310 fNumAAHairlineConcavePaths = 0; 311 } 312 313 void swap(ContentInfo* other) { 314 SkTSwap(fNumPaintWithPathEffectUses, other->fNumPaintWithPathEffectUses); 315 SkTSwap(fNumAAConcavePaths, other->fNumAAConcavePaths); 316 SkTSwap(fNumAAHairlineConcavePaths, other->fNumAAHairlineConcavePaths); 317 } 318 319 // This field is incremented every time a paint with a path effect is 320 // used (i.e., it is not a de-duplicated count) 321 int fNumPaintWithPathEffectUses; 322 // This field is incremented every time an anti-aliased drawPath call is 323 // issued with a concave path 324 int fNumAAConcavePaths; 325 // This field is incremented every time a drawPath call is 326 // issued for a hairline stroked concave path. 327 int fNumAAHairlineConcavePaths; 328 }; 329 330 ContentInfo fContentInfo; 331 332 void incPaintWithPathEffectUses() { 333 ++fContentInfo.fNumPaintWithPathEffectUses; 334 } 335 int numPaintWithPathEffectUses() const { 336 return fContentInfo.fNumPaintWithPathEffectUses; 337 } 338 339 void incAAConcavePaths() { 340 ++fContentInfo.fNumAAConcavePaths; 341 } 342 int numAAConcavePaths() const { 343 return fContentInfo.fNumAAConcavePaths; 344 } 345 346 void incAAHairlineConcavePaths() { 347 ++fContentInfo.fNumAAHairlineConcavePaths; 348 SkASSERT(fContentInfo.fNumAAHairlineConcavePaths <= fContentInfo.fNumAAConcavePaths); 349 } 350 int numAAHairlineConcavePaths() const { 351 return fContentInfo.fNumAAHairlineConcavePaths; 352 } 353 354 const SkPath& getPath(int index) const; 355 int addPathToHeap(const SkPath& path); 356 357 void flattenToBuffer(SkWriteBuffer& buffer) const; 358 bool parseBufferTag(SkReadBuffer& buffer, uint32_t tag, uint32_t size); 359 360 static void WriteTagSize(SkWriteBuffer& buffer, uint32_t tag, size_t size); 361 static void WriteTagSize(SkWStream* stream, uint32_t tag, size_t size); 362 363 void initForPlayback() const; 364 void dumpSize() const; 365 366 // An OperationList encapsulates a set of operation offsets into the picture byte 367 // stream along with the CTMs needed for those operation. 368 class OperationList : ::SkNoncopyable { 369 public: 370 virtual ~OperationList() {} 371 372 // If valid returns false then there is no optimization data 373 // present. All the draw operations need to be issued. 374 virtual bool valid() const { return false; } 375 376 // The following three entry points should only be accessed if 377 // 'valid' returns true. 378 virtual int numOps() const { SkASSERT(false); return 0; }; 379 // The offset in the picture of the operation to execute. 380 virtual uint32_t offset(int index) const { SkASSERT(false); return 0; }; 381 // The CTM that must be installed for the operation to behave correctly 382 virtual const SkMatrix& matrix(int index) const { SkASSERT(false); return SkMatrix::I(); } 383 384 static const OperationList& InvalidList(); 385 }; 386 387 /** PRIVATE / EXPERIMENTAL -- do not call 388 Return the operations required to render the content inside 'queryRect'. 389 */ 390 const OperationList& EXPERIMENTAL_getActiveOps(const SkIRect& queryRect) const; 391 392 /** PRIVATE / EXPERIMENTAL -- do not call 393 Return the ID of the operation currently being executed when playing 394 back. 0 indicates no call is active. 395 */ 396 size_t EXPERIMENTAL_curOpID() const; 397 398 void createHeader(SkPictInfo* info) const; 399 static bool IsValidPictInfo(const SkPictInfo& info); 400 static SkPicturePlayback* FakeEndRecording(const SkPicture* resourceSrc, 401 const SkPictureRecord& record, 402 bool deepCopy); 403 404 friend class SkFlatPicture; 405 friend class SkPicturePlayback; 406 friend class SkPictureRecorder; 407 friend class SkGpuDevice; 408 friend class GrGatherCanvas; 409 friend class GrGatherDevice; 410 friend class SkDebugCanvas; 411 412 typedef SkRefCnt INHERITED; 413}; 414 415/** 416 * Subclasses of this can be passed to canvas.drawPicture. During the drawing 417 * of the picture, this callback will periodically be invoked. If its 418 * abortDrawing() returns true, then picture playback will be interrupted. 419 * 420 * The resulting drawing is undefined, as there is no guarantee how often the 421 * callback will be invoked. If the abort happens inside some level of nested 422 * calls to save(), restore will automatically be called to return the state 423 * to the same level it was before the drawPicture call was made. 424 */ 425class SK_API SkDrawPictureCallback { 426public: 427 SkDrawPictureCallback() {} 428 virtual ~SkDrawPictureCallback() {} 429 430 virtual bool abortDrawing() = 0; 431}; 432 433#endif 434