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