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