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