1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/core/SkPicturePlayback.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,248 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2011 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 +#ifndef SkPicturePlayback_DEFINED 1.12 +#define SkPicturePlayback_DEFINED 1.13 + 1.14 +#include "SkPicture.h" 1.15 +#include "SkReader32.h" 1.16 + 1.17 +#include "SkBitmap.h" 1.18 +#include "SkData.h" 1.19 +#include "SkMatrix.h" 1.20 +#include "SkReadBuffer.h" 1.21 +#include "SkPaint.h" 1.22 +#include "SkPath.h" 1.23 +#include "SkPathHeap.h" 1.24 +#include "SkRegion.h" 1.25 +#include "SkRRect.h" 1.26 +#include "SkPictureFlat.h" 1.27 + 1.28 +#ifdef SK_BUILD_FOR_ANDROID 1.29 +#include "SkThread.h" 1.30 +#endif 1.31 + 1.32 +class SkPictureRecord; 1.33 +class SkStream; 1.34 +class SkWStream; 1.35 +class SkBBoxHierarchy; 1.36 +class SkPictureStateTree; 1.37 +class SkOffsetTable; 1.38 + 1.39 +struct SkPictInfo { 1.40 + enum Flags { 1.41 + kCrossProcess_Flag = 1 << 0, 1.42 + kScalarIsFloat_Flag = 1 << 1, 1.43 + kPtrIs64Bit_Flag = 1 << 2, 1.44 + }; 1.45 + 1.46 + char fMagic[8]; 1.47 + uint32_t fVersion; 1.48 + uint32_t fWidth; 1.49 + uint32_t fHeight; 1.50 + uint32_t fFlags; 1.51 +}; 1.52 + 1.53 +#define SK_PICT_READER_TAG SkSetFourByteTag('r', 'e', 'a', 'd') 1.54 +#define SK_PICT_FACTORY_TAG SkSetFourByteTag('f', 'a', 'c', 't') 1.55 +#define SK_PICT_TYPEFACE_TAG SkSetFourByteTag('t', 'p', 'f', 'c') 1.56 +#define SK_PICT_PICTURE_TAG SkSetFourByteTag('p', 'c', 't', 'r') 1.57 + 1.58 +// This tag specifies the size of the ReadBuffer, needed for the following tags 1.59 +#define SK_PICT_BUFFER_SIZE_TAG SkSetFourByteTag('a', 'r', 'a', 'y') 1.60 +// these are all inside the ARRAYS tag 1.61 +#define SK_PICT_BITMAP_BUFFER_TAG SkSetFourByteTag('b', 't', 'm', 'p') 1.62 +#define SK_PICT_PAINT_BUFFER_TAG SkSetFourByteTag('p', 'n', 't', ' ') 1.63 +#define SK_PICT_PATH_BUFFER_TAG SkSetFourByteTag('p', 't', 'h', ' ') 1.64 + 1.65 +// Always write this guy last (with no length field afterwards) 1.66 +#define SK_PICT_EOF_TAG SkSetFourByteTag('e', 'o', 'f', ' ') 1.67 + 1.68 +/** 1.69 + * Container for data that is needed to deep copy a SkPicture. The container 1.70 + * enables the data to be generated once and reused for subsequent copies. 1.71 + */ 1.72 +struct SkPictCopyInfo { 1.73 + SkPictCopyInfo() : initialized(false), controller(1024) {} 1.74 + 1.75 + bool initialized; 1.76 + SkChunkFlatController controller; 1.77 + SkTDArray<SkFlatData*> paintData; 1.78 +}; 1.79 + 1.80 +class SkPicturePlayback { 1.81 +public: 1.82 + SkPicturePlayback(); 1.83 + SkPicturePlayback(const SkPicturePlayback& src, SkPictCopyInfo* deepCopyInfo = NULL); 1.84 + explicit SkPicturePlayback(const SkPictureRecord& record, bool deepCopy = false); 1.85 + static SkPicturePlayback* CreateFromStream(SkStream*, const SkPictInfo&, 1.86 + SkPicture::InstallPixelRefProc); 1.87 + static SkPicturePlayback* CreateFromBuffer(SkReadBuffer&); 1.88 + 1.89 + virtual ~SkPicturePlayback(); 1.90 + 1.91 + void draw(SkCanvas& canvas, SkDrawPictureCallback*); 1.92 + 1.93 + void serialize(SkWStream*, SkPicture::EncodeBitmap) const; 1.94 + void flatten(SkWriteBuffer&) const; 1.95 + 1.96 + void dumpSize() const; 1.97 + 1.98 + bool containsBitmaps() const; 1.99 + 1.100 +#ifdef SK_BUILD_FOR_ANDROID 1.101 + // Can be called in the middle of playback (the draw() call). WIll abort the 1.102 + // drawing and return from draw() after the "current" op code is done 1.103 + void abort() { fAbortCurrentPlayback = true; } 1.104 +#endif 1.105 + 1.106 +protected: 1.107 + bool parseStream(SkStream*, const SkPictInfo&, 1.108 + SkPicture::InstallPixelRefProc); 1.109 + bool parseBuffer(SkReadBuffer& buffer); 1.110 +#ifdef SK_DEVELOPER 1.111 + virtual bool preDraw(int opIndex, int type); 1.112 + virtual void postDraw(int opIndex); 1.113 +#endif 1.114 + 1.115 + void preLoadBitmaps(const SkTDArray<void*>& results); 1.116 + 1.117 +private: 1.118 + class TextContainer { 1.119 + public: 1.120 + size_t length() { return fByteLength; } 1.121 + const void* text() { return (const void*) fText; } 1.122 + size_t fByteLength; 1.123 + const char* fText; 1.124 + }; 1.125 + 1.126 + const SkBitmap& getBitmap(SkReader32& reader) { 1.127 + const int index = reader.readInt(); 1.128 + if (SkBitmapHeap::INVALID_SLOT == index) { 1.129 +#ifdef SK_DEBUG 1.130 + SkDebugf("An invalid bitmap was recorded!\n"); 1.131 +#endif 1.132 + return fBadBitmap; 1.133 + } 1.134 + return (*fBitmaps)[index]; 1.135 + } 1.136 + 1.137 + void getMatrix(SkReader32& reader, SkMatrix* matrix) { 1.138 + reader.readMatrix(matrix); 1.139 + } 1.140 + 1.141 + const SkPath& getPath(SkReader32& reader) { 1.142 + return (*fPathHeap)[reader.readInt() - 1]; 1.143 + } 1.144 + 1.145 + SkPicture& getPicture(SkReader32& reader) { 1.146 + int index = reader.readInt(); 1.147 + SkASSERT(index > 0 && index <= fPictureCount); 1.148 + return *fPictureRefs[index - 1]; 1.149 + } 1.150 + 1.151 + const SkPaint* getPaint(SkReader32& reader) { 1.152 + int index = reader.readInt(); 1.153 + if (index == 0) { 1.154 + return NULL; 1.155 + } 1.156 + return &(*fPaints)[index - 1]; 1.157 + } 1.158 + 1.159 + const SkRect* getRectPtr(SkReader32& reader) { 1.160 + if (reader.readBool()) { 1.161 + return &reader.skipT<SkRect>(); 1.162 + } else { 1.163 + return NULL; 1.164 + } 1.165 + } 1.166 + 1.167 + const SkIRect* getIRectPtr(SkReader32& reader) { 1.168 + if (reader.readBool()) { 1.169 + return &reader.skipT<SkIRect>(); 1.170 + } else { 1.171 + return NULL; 1.172 + } 1.173 + } 1.174 + 1.175 + void getRegion(SkReader32& reader, SkRegion* region) { 1.176 + reader.readRegion(region); 1.177 + } 1.178 + 1.179 + void getText(SkReader32& reader, TextContainer* text) { 1.180 + size_t length = text->fByteLength = reader.readInt(); 1.181 + text->fText = (const char*)reader.skip(length); 1.182 + } 1.183 + 1.184 + void init(); 1.185 + 1.186 +#ifdef SK_DEBUG_SIZE 1.187 +public: 1.188 + int size(size_t* sizePtr); 1.189 + int bitmaps(size_t* size); 1.190 + int paints(size_t* size); 1.191 + int paths(size_t* size); 1.192 +#endif 1.193 + 1.194 +#ifdef SK_DEBUG_DUMP 1.195 +private: 1.196 + void dumpBitmap(const SkBitmap& bitmap) const; 1.197 + void dumpMatrix(const SkMatrix& matrix) const; 1.198 + void dumpPaint(const SkPaint& paint) const; 1.199 + void dumpPath(const SkPath& path) const; 1.200 + void dumpPicture(const SkPicture& picture) const; 1.201 + void dumpRegion(const SkRegion& region) const; 1.202 + int dumpDrawType(char* bufferPtr, char* buffer, DrawType drawType); 1.203 + int dumpInt(char* bufferPtr, char* buffer, char* name); 1.204 + int dumpRect(char* bufferPtr, char* buffer, char* name); 1.205 + int dumpPoint(char* bufferPtr, char* buffer, char* name); 1.206 + void dumpPointArray(char** bufferPtrPtr, char* buffer, int count); 1.207 + int dumpPtr(char* bufferPtr, char* buffer, char* name, void* ptr); 1.208 + int dumpRectPtr(char* bufferPtr, char* buffer, char* name); 1.209 + int dumpScalar(char* bufferPtr, char* buffer, char* name); 1.210 + void dumpText(char** bufferPtrPtr, char* buffer); 1.211 + void dumpStream(); 1.212 + 1.213 +public: 1.214 + void dump() const; 1.215 +#endif 1.216 + 1.217 +private: // these help us with reading/writing 1.218 + bool parseStreamTag(SkStream*, const SkPictInfo&, uint32_t tag, size_t size, 1.219 + SkPicture::InstallPixelRefProc); 1.220 + bool parseBufferTag(SkReadBuffer&, uint32_t tag, size_t size); 1.221 + void flattenToBuffer(SkWriteBuffer&) const; 1.222 + 1.223 +private: 1.224 + // Only used by getBitmap() if the passed in index is SkBitmapHeap::INVALID_SLOT. This empty 1.225 + // bitmap allows playback to draw nothing and move on. 1.226 + SkBitmap fBadBitmap; 1.227 + 1.228 + SkAutoTUnref<SkBitmapHeap> fBitmapHeap; 1.229 + SkAutoTUnref<SkPathHeap> fPathHeap; 1.230 + 1.231 + SkTRefArray<SkBitmap>* fBitmaps; 1.232 + SkTRefArray<SkPaint>* fPaints; 1.233 + 1.234 + SkData* fOpData; // opcodes and parameters 1.235 + SkAutoTUnref<SkOffsetTable> fBitmapUseOffsets; 1.236 + 1.237 + SkPicture** fPictureRefs; 1.238 + int fPictureCount; 1.239 + 1.240 + SkBBoxHierarchy* fBoundingHierarchy; 1.241 + SkPictureStateTree* fStateTree; 1.242 + 1.243 + SkTypefacePlayback fTFPlayback; 1.244 + SkFactoryPlayback* fFactoryPlayback; 1.245 +#ifdef SK_BUILD_FOR_ANDROID 1.246 + SkMutex fDrawMutex; 1.247 + bool fAbortCurrentPlayback; 1.248 +#endif 1.249 +}; 1.250 + 1.251 +#endif