SkPicturePlayback.h revision 4f6318d2e64c96fba955ea9154829aee37e155b8
1 2/* 3 * Copyright 2011 Google Inc. 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#ifndef SkPicturePlayback_DEFINED 9#define SkPicturePlayback_DEFINED 10 11#include "SkPicture.h" 12#include "SkReader32.h" 13 14#include "SkBitmap.h" 15#include "SkData.h" 16#include "SkMatrix.h" 17#include "SkReadBuffer.h" 18#include "SkPaint.h" 19#include "SkPath.h" 20#include "SkPathHeap.h" 21#include "SkRegion.h" 22#include "SkRRect.h" 23#include "SkPictureFlat.h" 24 25#ifdef SK_BUILD_FOR_ANDROID 26#include "SkThread.h" 27#endif 28 29class SkPictureRecord; 30class SkStream; 31class SkWStream; 32class SkBBoxHierarchy; 33class SkPictureStateTree; 34 35struct SkPictInfo { 36 enum Flags { 37 kCrossProcess_Flag = 1 << 0, 38 kScalarIsFloat_Flag = 1 << 1, 39 kPtrIs64Bit_Flag = 1 << 2, 40 }; 41 42 uint32_t fVersion; 43 uint32_t fWidth; 44 uint32_t fHeight; 45 uint32_t fFlags; 46}; 47 48/** 49 * Container for data that is needed to deep copy a SkPicture. The container 50 * enables the data to be generated once and reused for subsequent copies. 51 */ 52struct SkPictCopyInfo { 53 SkPictCopyInfo() : initialized(false), controller(1024) {} 54 55 bool initialized; 56 SkChunkFlatController controller; 57 SkTDArray<SkFlatData*> paintData; 58}; 59 60class SkPicturePlayback { 61public: 62 SkPicturePlayback(); 63 SkPicturePlayback(const SkPicturePlayback& src, SkPictCopyInfo* deepCopyInfo = NULL); 64 explicit SkPicturePlayback(const SkPictureRecord& record, bool deepCopy = false); 65 static SkPicturePlayback* CreateFromStream(SkStream*, const SkPictInfo&, 66 SkPicture::InstallPixelRefProc); 67 static SkPicturePlayback* CreateFromBuffer(SkReadBuffer&); 68 69 virtual ~SkPicturePlayback(); 70 71 void draw(SkCanvas& canvas, SkDrawPictureCallback*); 72 73 void serialize(SkWStream*, SkPicture::EncodeBitmap) const; 74 void flatten(SkWriteBuffer&) const; 75 76 void dumpSize() const; 77 78 bool containsBitmaps() const; 79 80#ifdef SK_BUILD_FOR_ANDROID 81 // Can be called in the middle of playback (the draw() call). WIll abort the 82 // drawing and return from draw() after the "current" op code is done 83 void abort() { fAbortCurrentPlayback = true; } 84#endif 85 86protected: 87 bool parseStream(SkStream*, const SkPictInfo&, 88 SkPicture::InstallPixelRefProc); 89 bool parseBuffer(SkReadBuffer& buffer); 90#ifdef SK_DEVELOPER 91 virtual bool preDraw(int opIndex, int type); 92 virtual void postDraw(int opIndex); 93#endif 94 95private: 96 class TextContainer { 97 public: 98 size_t length() { return fByteLength; } 99 const void* text() { return (const void*) fText; } 100 size_t fByteLength; 101 const char* fText; 102 }; 103 104 const SkBitmap& getBitmap(SkReader32& reader) { 105 const int index = reader.readInt(); 106 if (SkBitmapHeap::INVALID_SLOT == index) { 107#ifdef SK_DEBUG 108 SkDebugf("An invalid bitmap was recorded!\n"); 109#endif 110 return fBadBitmap; 111 } 112 return (*fBitmaps)[index]; 113 } 114 115 void getMatrix(SkReader32& reader, SkMatrix* matrix) { 116 reader.readMatrix(matrix); 117 } 118 119 const SkPath& getPath(SkReader32& reader) { 120 return (*fPathHeap)[reader.readInt() - 1]; 121 } 122 123 SkPicture& getPicture(SkReader32& reader) { 124 int index = reader.readInt(); 125 SkASSERT(index > 0 && index <= fPictureCount); 126 return *fPictureRefs[index - 1]; 127 } 128 129 const SkPaint* getPaint(SkReader32& reader) { 130 int index = reader.readInt(); 131 if (index == 0) { 132 return NULL; 133 } 134 return &(*fPaints)[index - 1]; 135 } 136 137 const SkRect* getRectPtr(SkReader32& reader) { 138 if (reader.readBool()) { 139 return &reader.skipT<SkRect>(); 140 } else { 141 return NULL; 142 } 143 } 144 145 const SkIRect* getIRectPtr(SkReader32& reader) { 146 if (reader.readBool()) { 147 return &reader.skipT<SkIRect>(); 148 } else { 149 return NULL; 150 } 151 } 152 153 void getRegion(SkReader32& reader, SkRegion* region) { 154 reader.readRegion(region); 155 } 156 157 void getText(SkReader32& reader, TextContainer* text) { 158 size_t length = text->fByteLength = reader.readInt(); 159 text->fText = (const char*)reader.skip(length); 160 } 161 162 void init(); 163 164#ifdef SK_DEBUG_SIZE 165public: 166 int size(size_t* sizePtr); 167 int bitmaps(size_t* size); 168 int paints(size_t* size); 169 int paths(size_t* size); 170#endif 171 172#ifdef SK_DEBUG_DUMP 173private: 174 void dumpBitmap(const SkBitmap& bitmap) const; 175 void dumpMatrix(const SkMatrix& matrix) const; 176 void dumpPaint(const SkPaint& paint) const; 177 void dumpPath(const SkPath& path) const; 178 void dumpPicture(const SkPicture& picture) const; 179 void dumpRegion(const SkRegion& region) const; 180 int dumpDrawType(char* bufferPtr, char* buffer, DrawType drawType); 181 int dumpInt(char* bufferPtr, char* buffer, char* name); 182 int dumpRect(char* bufferPtr, char* buffer, char* name); 183 int dumpPoint(char* bufferPtr, char* buffer, char* name); 184 void dumpPointArray(char** bufferPtrPtr, char* buffer, int count); 185 int dumpPtr(char* bufferPtr, char* buffer, char* name, void* ptr); 186 int dumpRectPtr(char* bufferPtr, char* buffer, char* name); 187 int dumpScalar(char* bufferPtr, char* buffer, char* name); 188 void dumpText(char** bufferPtrPtr, char* buffer); 189 void dumpStream(); 190 191public: 192 void dump() const; 193#endif 194 195private: // these help us with reading/writing 196 bool parseStreamTag(SkStream*, const SkPictInfo&, uint32_t tag, size_t size, 197 SkPicture::InstallPixelRefProc); 198 bool parseBufferTag(SkReadBuffer&, uint32_t tag, size_t size); 199 void flattenToBuffer(SkWriteBuffer&) const; 200 201private: 202 // Only used by getBitmap() if the passed in index is SkBitmapHeap::INVALID_SLOT. This empty 203 // bitmap allows playback to draw nothing and move on. 204 SkBitmap fBadBitmap; 205 206 SkAutoTUnref<SkBitmapHeap> fBitmapHeap; 207 SkAutoTUnref<SkPathHeap> fPathHeap; 208 209 SkTRefArray<SkBitmap>* fBitmaps; 210 SkTRefArray<SkPaint>* fPaints; 211 212 SkData* fOpData; // opcodes and parameters 213 214 SkPicture** fPictureRefs; 215 int fPictureCount; 216 217 SkBBoxHierarchy* fBoundingHierarchy; 218 SkPictureStateTree* fStateTree; 219 220 SkTypefacePlayback fTFPlayback; 221 SkFactoryPlayback* fFactoryPlayback; 222#ifdef SK_BUILD_FOR_ANDROID 223 SkMutex fDrawMutex; 224 bool fAbortCurrentPlayback; 225#endif 226}; 227 228#endif 229