SkPictureData.h revision 303044579913eacc177d4b28a674121725c565bb
1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkPictureData_DEFINED
9#define SkPictureData_DEFINED
10
11#include "SkBitmap.h"
12#include "SkPicture.h"
13#include "SkPictureContentInfo.h"
14#include "SkPictureFlat.h"
15
16class SkData;
17class SkPictureRecord;
18class SkReader32;
19class SkStream;
20class SkWStream;
21class SkBBoxHierarchy;
22class SkMatrix;
23class SkPaint;
24class SkPath;
25class SkReadBuffer;
26class SkTextBlob;
27
28struct SkPictInfo {
29    enum Flags {
30        kCrossProcess_Flag      = 1 << 0,
31        kScalarIsFloat_Flag     = 1 << 1,
32        kPtrIs64Bit_Flag        = 1 << 2,
33    };
34
35    char        fMagic[8];
36    uint32_t    fVersion;
37    SkRect      fCullRect;
38    uint32_t    fFlags;
39};
40
41#define SK_PICT_READER_TAG     SkSetFourByteTag('r', 'e', 'a', 'd')
42#define SK_PICT_FACTORY_TAG    SkSetFourByteTag('f', 'a', 'c', 't')
43#define SK_PICT_TYPEFACE_TAG   SkSetFourByteTag('t', 'p', 'f', 'c')
44#define SK_PICT_PICTURE_TAG    SkSetFourByteTag('p', 'c', 't', 'r')
45
46// This tag specifies the size of the ReadBuffer, needed for the following tags
47#define SK_PICT_BUFFER_SIZE_TAG     SkSetFourByteTag('a', 'r', 'a', 'y')
48// these are all inside the ARRAYS tag
49#define SK_PICT_BITMAP_BUFFER_TAG   SkSetFourByteTag('b', 't', 'm', 'p')
50#define SK_PICT_PAINT_BUFFER_TAG    SkSetFourByteTag('p', 'n', 't', ' ')
51#define SK_PICT_PATH_BUFFER_TAG     SkSetFourByteTag('p', 't', 'h', ' ')
52#define SK_PICT_TEXTBLOB_BUFFER_TAG SkSetFourByteTag('b', 'l', 'o', 'b')
53
54// Always write this guy last (with no length field afterwards)
55#define SK_PICT_EOF_TAG     SkSetFourByteTag('e', 'o', 'f', ' ')
56
57class SkPictureData {
58public:
59    SkPictureData(const SkPictureRecord& record, const SkPictInfo&, bool deepCopyOps);
60    static SkPictureData* CreateFromStream(SkStream*,
61                                           const SkPictInfo&,
62                                           SkPicture::InstallPixelRefProc);
63    static SkPictureData* CreateFromBuffer(SkReadBuffer&, const SkPictInfo&);
64
65    virtual ~SkPictureData();
66
67    void serialize(SkWStream*, SkPicture::EncodeBitmap) const;
68    void flatten(SkWriteBuffer&) const;
69
70    bool containsBitmaps() const;
71
72    bool hasText() const { return fContentInfo.hasText(); }
73
74    int opCount() const { return fContentInfo.numOperations(); }
75
76    const SkData* opData() const { return fOpData; }
77
78protected:
79    explicit SkPictureData(const SkPictInfo& info);
80
81    bool parseStream(SkStream*, SkPicture::InstallPixelRefProc);
82    bool parseBuffer(SkReadBuffer& buffer);
83
84public:
85    const SkBitmap& getBitmap(SkReader32* reader) const {
86        const int index = reader->readInt();
87        return fBitmaps[index];
88    }
89
90    const SkPath& getPath(SkReader32* reader) const {
91        int index = reader->readInt() - 1;
92        return fPaths[index];
93    }
94
95    const SkPicture* getPicture(SkReader32* reader) const {
96        int index = reader->readInt();
97        SkASSERT(index > 0 && index <= fPictureCount);
98        return fPictureRefs[index - 1];
99    }
100
101    const SkPaint* getPaint(SkReader32* reader) const {
102        int index = reader->readInt();
103        if (index == 0) {
104            return NULL;
105        }
106        return &fPaints[index - 1];
107    }
108
109    const SkTextBlob* getTextBlob(SkReader32* reader) const {
110        int index = reader->readInt();
111        SkASSERT(index > 0 && index <= fTextBlobCount);
112        return fTextBlobRefs[index - 1];
113    }
114
115#if SK_SUPPORT_GPU
116    /**
117     * sampleCount is the number of samples-per-pixel or zero if non-MSAA.
118     * It is defaulted to be zero.
119     */
120    bool suitableForGpuRasterization(GrContext* context, const char **reason,
121                                     int sampleCount = 0) const;
122
123    /**
124     * Calls getRecommendedSampleCount with GrPixelConfig and dpi to calculate sampleCount
125     * and then calls the above version of suitableForGpuRasterization
126     */
127    bool suitableForGpuRasterization(GrContext* context, const char **reason,
128                                     GrPixelConfig config, SkScalar dpi) const;
129
130    bool suitableForLayerOptimization() const;
131#endif
132
133private:
134    void init();
135
136    // these help us with reading/writing
137    bool parseStreamTag(SkStream*, uint32_t tag, uint32_t size, SkPicture::InstallPixelRefProc);
138    bool parseBufferTag(SkReadBuffer&, uint32_t tag, uint32_t size);
139    void flattenToBuffer(SkWriteBuffer&) const;
140
141    // Only used by getBitmap() if the passed in index is SkBitmapHeap::INVALID_SLOT. This empty
142    // bitmap allows playback to draw nothing and move on.
143    SkBitmap fBadBitmap;
144
145    SkTArray<SkBitmap> fBitmaps;
146    SkTArray<SkPaint>  fPaints;
147    SkTArray<SkPath>   fPaths;
148
149    SkData* fOpData;    // opcodes and parameters
150
151    const SkPicture** fPictureRefs;
152    int fPictureCount;
153    const SkTextBlob** fTextBlobRefs;
154    int fTextBlobCount;
155
156    SkPictureContentInfo fContentInfo;
157
158    SkTypefacePlayback fTFPlayback;
159    SkFactoryPlayback* fFactoryPlayback;
160
161    const SkPictInfo fInfo;
162
163    static void WriteFactories(SkWStream* stream, const SkFactorySet& rec);
164    static void WriteTypefaces(SkWStream* stream, const SkRefCntSet& rec);
165
166    void initForPlayback() const;
167};
168
169#endif
170