1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2011 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef SkPictureData_DEFINED
9cb93a386Sopenharmony_ci#define SkPictureData_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/core/SkBitmap.h"
12cb93a386Sopenharmony_ci#include "include/core/SkDrawable.h"
13cb93a386Sopenharmony_ci#include "include/core/SkPicture.h"
14cb93a386Sopenharmony_ci#include "include/private/SkTArray.h"
15cb93a386Sopenharmony_ci#include "src/core/SkPictureFlat.h"
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_ci#include <memory>
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ciclass SkData;
20cb93a386Sopenharmony_ciclass SkPictureRecord;
21cb93a386Sopenharmony_cistruct SkSerialProcs;
22cb93a386Sopenharmony_ciclass SkStream;
23cb93a386Sopenharmony_ciclass SkWStream;
24cb93a386Sopenharmony_ciclass SkBBoxHierarchy;
25cb93a386Sopenharmony_ciclass SkMatrix;
26cb93a386Sopenharmony_ciclass SkPaint;
27cb93a386Sopenharmony_ciclass SkPath;
28cb93a386Sopenharmony_ciclass SkReadBuffer;
29cb93a386Sopenharmony_ciclass SkTextBlob;
30cb93a386Sopenharmony_ci
31cb93a386Sopenharmony_cistruct SkPictInfo {
32cb93a386Sopenharmony_ci    SkPictInfo() : fVersion(~0U) {}
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_ci    uint32_t getVersion() const {
35cb93a386Sopenharmony_ci        SkASSERT(fVersion != ~0U);
36cb93a386Sopenharmony_ci        return fVersion;
37cb93a386Sopenharmony_ci    }
38cb93a386Sopenharmony_ci
39cb93a386Sopenharmony_ci    void setVersion(uint32_t version) {
40cb93a386Sopenharmony_ci        SkASSERT(version != ~0U);
41cb93a386Sopenharmony_ci        fVersion = version;
42cb93a386Sopenharmony_ci    }
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_cipublic:
45cb93a386Sopenharmony_ci    char        fMagic[8];
46cb93a386Sopenharmony_ciprivate:
47cb93a386Sopenharmony_ci    uint32_t    fVersion;
48cb93a386Sopenharmony_cipublic:
49cb93a386Sopenharmony_ci    SkRect      fCullRect;
50cb93a386Sopenharmony_ci};
51cb93a386Sopenharmony_ci
52cb93a386Sopenharmony_ci#define SK_PICT_READER_TAG     SkSetFourByteTag('r', 'e', 'a', 'd')
53cb93a386Sopenharmony_ci#define SK_PICT_FACTORY_TAG    SkSetFourByteTag('f', 'a', 'c', 't')
54cb93a386Sopenharmony_ci#define SK_PICT_TYPEFACE_TAG   SkSetFourByteTag('t', 'p', 'f', 'c')
55cb93a386Sopenharmony_ci#define SK_PICT_PICTURE_TAG    SkSetFourByteTag('p', 'c', 't', 'r')
56cb93a386Sopenharmony_ci#define SK_PICT_DRAWABLE_TAG   SkSetFourByteTag('d', 'r', 'a', 'w')
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci// This tag specifies the size of the ReadBuffer, needed for the following tags
59cb93a386Sopenharmony_ci#define SK_PICT_BUFFER_SIZE_TAG     SkSetFourByteTag('a', 'r', 'a', 'y')
60cb93a386Sopenharmony_ci// these are all inside the ARRAYS tag
61cb93a386Sopenharmony_ci#define SK_PICT_PAINT_BUFFER_TAG    SkSetFourByteTag('p', 'n', 't', ' ')
62cb93a386Sopenharmony_ci#define SK_PICT_PATH_BUFFER_TAG     SkSetFourByteTag('p', 't', 'h', ' ')
63cb93a386Sopenharmony_ci#define SK_PICT_TEXTBLOB_BUFFER_TAG SkSetFourByteTag('b', 'l', 'o', 'b')
64cb93a386Sopenharmony_ci#define SK_PICT_VERTICES_BUFFER_TAG SkSetFourByteTag('v', 'e', 'r', 't')
65cb93a386Sopenharmony_ci#define SK_PICT_IMAGE_BUFFER_TAG    SkSetFourByteTag('i', 'm', 'a', 'g')
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci// Always write this last (with no length field afterwards)
68cb93a386Sopenharmony_ci#define SK_PICT_EOF_TAG     SkSetFourByteTag('e', 'o', 'f', ' ')
69cb93a386Sopenharmony_ci
70cb93a386Sopenharmony_citemplate <typename T>
71cb93a386Sopenharmony_ciT* read_index_base_1_or_null(SkReadBuffer* reader, const SkTArray<sk_sp<T>>& array) {
72cb93a386Sopenharmony_ci    int index = reader->readInt();
73cb93a386Sopenharmony_ci    return reader->validate(index > 0 && index <= array.count()) ? array[index - 1].get() : nullptr;
74cb93a386Sopenharmony_ci}
75cb93a386Sopenharmony_ci
76cb93a386Sopenharmony_ciclass SkPictureData {
77cb93a386Sopenharmony_cipublic:
78cb93a386Sopenharmony_ci    SkPictureData(const SkPictureRecord& record, const SkPictInfo&);
79cb93a386Sopenharmony_ci    // Does not affect ownership of SkStream.
80cb93a386Sopenharmony_ci    static SkPictureData* CreateFromStream(SkStream*,
81cb93a386Sopenharmony_ci                                           const SkPictInfo&,
82cb93a386Sopenharmony_ci                                           const SkDeserialProcs&,
83cb93a386Sopenharmony_ci                                           SkTypefacePlayback*);
84cb93a386Sopenharmony_ci    static SkPictureData* CreateFromBuffer(SkReadBuffer&, const SkPictInfo&);
85cb93a386Sopenharmony_ci
86cb93a386Sopenharmony_ci    void serialize(SkWStream*, const SkSerialProcs&, SkRefCntSet*, bool textBlobsOnly=false) const;
87cb93a386Sopenharmony_ci    void flatten(SkWriteBuffer&) const;
88cb93a386Sopenharmony_ci
89cb93a386Sopenharmony_ci    const sk_sp<SkData>& opData() const { return fOpData; }
90cb93a386Sopenharmony_ci
91cb93a386Sopenharmony_ciprotected:
92cb93a386Sopenharmony_ci    explicit SkPictureData(const SkPictInfo& info);
93cb93a386Sopenharmony_ci
94cb93a386Sopenharmony_ci    // Does not affect ownership of SkStream.
95cb93a386Sopenharmony_ci    bool parseStream(SkStream*, const SkDeserialProcs&, SkTypefacePlayback*);
96cb93a386Sopenharmony_ci    bool parseBuffer(SkReadBuffer& buffer);
97cb93a386Sopenharmony_ci
98cb93a386Sopenharmony_cipublic:
99cb93a386Sopenharmony_ci    const SkImage* getImage(SkReadBuffer* reader) const {
100cb93a386Sopenharmony_ci        // images are written base-0, unlike paths, pictures, drawables, etc.
101cb93a386Sopenharmony_ci        const int index = reader->readInt();
102cb93a386Sopenharmony_ci        return reader->validateIndex(index, fImages.count()) ? fImages[index].get() : nullptr;
103cb93a386Sopenharmony_ci    }
104cb93a386Sopenharmony_ci
105cb93a386Sopenharmony_ci    const SkPath& getPath(SkReadBuffer* reader) const {
106cb93a386Sopenharmony_ci        int index = reader->readInt();
107cb93a386Sopenharmony_ci        return reader->validate(index > 0 && index <= fPaths.count()) ?
108cb93a386Sopenharmony_ci                fPaths[index - 1] : fEmptyPath;
109cb93a386Sopenharmony_ci    }
110cb93a386Sopenharmony_ci
111cb93a386Sopenharmony_ci    const SkPicture* getPicture(SkReadBuffer* reader) const {
112cb93a386Sopenharmony_ci        return read_index_base_1_or_null(reader, fPictures);
113cb93a386Sopenharmony_ci    }
114cb93a386Sopenharmony_ci
115cb93a386Sopenharmony_ci    SkDrawable* getDrawable(SkReadBuffer* reader) const {
116cb93a386Sopenharmony_ci        return read_index_base_1_or_null(reader, fDrawables);
117cb93a386Sopenharmony_ci    }
118cb93a386Sopenharmony_ci
119cb93a386Sopenharmony_ci    // Return a paint if one was used for this op, or nullptr if none was used.
120cb93a386Sopenharmony_ci    const SkPaint* optionalPaint(SkReadBuffer* reader) const;
121cb93a386Sopenharmony_ci
122cb93a386Sopenharmony_ci    // Return the paint used for this op, invalidating the SkReadBuffer if there appears to be none.
123cb93a386Sopenharmony_ci    // The returned paint is always safe to use.
124cb93a386Sopenharmony_ci    const SkPaint& requiredPaint(SkReadBuffer* reader) const;
125cb93a386Sopenharmony_ci
126cb93a386Sopenharmony_ci    const SkTextBlob* getTextBlob(SkReadBuffer* reader) const {
127cb93a386Sopenharmony_ci        return read_index_base_1_or_null(reader, fTextBlobs);
128cb93a386Sopenharmony_ci    }
129cb93a386Sopenharmony_ci
130cb93a386Sopenharmony_ci    const SkVertices* getVertices(SkReadBuffer* reader) const {
131cb93a386Sopenharmony_ci        return read_index_base_1_or_null(reader, fVertices);
132cb93a386Sopenharmony_ci    }
133cb93a386Sopenharmony_ci
134cb93a386Sopenharmony_ciprivate:
135cb93a386Sopenharmony_ci    // these help us with reading/writing
136cb93a386Sopenharmony_ci    // Does not affect ownership of SkStream.
137cb93a386Sopenharmony_ci    bool parseStreamTag(SkStream*, uint32_t tag, uint32_t size,
138cb93a386Sopenharmony_ci                        const SkDeserialProcs&, SkTypefacePlayback*);
139cb93a386Sopenharmony_ci    void parseBufferTag(SkReadBuffer&, uint32_t tag, uint32_t size);
140cb93a386Sopenharmony_ci    void flattenToBuffer(SkWriteBuffer&, bool textBlobsOnly) const;
141cb93a386Sopenharmony_ci
142cb93a386Sopenharmony_ci    SkTArray<SkPaint>  fPaints;
143cb93a386Sopenharmony_ci    SkTArray<SkPath>   fPaths;
144cb93a386Sopenharmony_ci
145cb93a386Sopenharmony_ci    sk_sp<SkData>   fOpData;    // opcodes and parameters
146cb93a386Sopenharmony_ci
147cb93a386Sopenharmony_ci    const SkPath    fEmptyPath;
148cb93a386Sopenharmony_ci    const SkBitmap  fEmptyBitmap;
149cb93a386Sopenharmony_ci
150cb93a386Sopenharmony_ci    SkTArray<sk_sp<const SkPicture>>   fPictures;
151cb93a386Sopenharmony_ci    SkTArray<sk_sp<SkDrawable>>        fDrawables;
152cb93a386Sopenharmony_ci    SkTArray<sk_sp<const SkTextBlob>>  fTextBlobs;
153cb93a386Sopenharmony_ci    SkTArray<sk_sp<const SkVertices>>  fVertices;
154cb93a386Sopenharmony_ci    SkTArray<sk_sp<const SkImage>>     fImages;
155cb93a386Sopenharmony_ci
156cb93a386Sopenharmony_ci    SkTypefacePlayback                 fTFPlayback;
157cb93a386Sopenharmony_ci    std::unique_ptr<SkFactoryPlayback> fFactoryPlayback;
158cb93a386Sopenharmony_ci
159cb93a386Sopenharmony_ci    const SkPictInfo fInfo;
160cb93a386Sopenharmony_ci
161cb93a386Sopenharmony_ci    static void WriteFactories(SkWStream* stream, const SkFactorySet& rec);
162cb93a386Sopenharmony_ci    static void WriteTypefaces(SkWStream* stream, const SkRefCntSet& rec, const SkSerialProcs&);
163cb93a386Sopenharmony_ci
164cb93a386Sopenharmony_ci    void initForPlayback() const;
165cb93a386Sopenharmony_ci};
166cb93a386Sopenharmony_ci
167cb93a386Sopenharmony_ci#endif
168