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