SkReadBuffer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #ifndef SkReadBuffer_DEFINED
  8. #define SkReadBuffer_DEFINED
  9. #include "include/core/SkColorFilter.h"
  10. #include "include/core/SkDrawLooper.h"
  11. #include "include/core/SkFont.h"
  12. #include "include/core/SkImageFilter.h"
  13. #include "include/core/SkPath.h"
  14. #include "include/core/SkPathEffect.h"
  15. #include "include/core/SkPicture.h"
  16. #include "include/core/SkRefCnt.h"
  17. #include "include/core/SkSerialProcs.h"
  18. #include "src/core/SkMaskFilterBase.h"
  19. #include "src/core/SkPaintPriv.h"
  20. #include "src/core/SkReader32.h"
  21. #include "src/core/SkWriteBuffer.h"
  22. #include "src/shaders/SkShaderBase.h"
  23. class SkData;
  24. class SkImage;
  25. #ifndef SK_DISABLE_READBUFFER
  26. class SkReadBuffer {
  27. public:
  28. SkReadBuffer();
  29. SkReadBuffer(const void* data, size_t size);
  30. enum Version {
  31. kTileModeInBlurImageFilter_Version = 56,
  32. kTileInfoInSweepGradient_Version = 57,
  33. k2PtConicalNoFlip_Version = 58,
  34. kRemovePictureImageFilterLocalSpace = 59,
  35. kRemoveHeaderFlags_Version = 60,
  36. kTwoColorDrawShadow_Version = 61,
  37. kDontNegateImageSize_Version = 62,
  38. kStoreImageBounds_Version = 63,
  39. kRemoveOccluderFromBlurMaskFilter = 64,
  40. kFloat4PaintColor_Version = 65,
  41. kSaveBehind_Version = 66,
  42. kSerializeFonts_Version = 67,
  43. kPaintDoesntSerializeFonts_Version = 68,
  44. };
  45. /**
  46. * Returns true IFF the version is older than the specified version.
  47. */
  48. bool isVersionLT(Version targetVersion) const {
  49. SkASSERT(targetVersion > 0);
  50. return fVersion > 0 && fVersion < targetVersion;
  51. }
  52. uint32_t getVersion() const { return fVersion; }
  53. /** This may be called at most once; most clients of SkReadBuffer should not mess with it. */
  54. void setVersion(int version) {
  55. SkASSERT(0 == fVersion || version == fVersion);
  56. fVersion = version;
  57. }
  58. size_t size() const { return fReader.size(); }
  59. size_t offset() const { return fReader.offset(); }
  60. bool eof() { return fReader.eof(); }
  61. const void* skip(size_t size);
  62. const void* skip(size_t count, size_t size); // does safe multiply
  63. size_t available() const { return fReader.available(); }
  64. template <typename T> const T* skipT() {
  65. return static_cast<const T*>(this->skip(sizeof(T)));
  66. }
  67. template <typename T> const T* skipT(size_t count) {
  68. return static_cast<const T*>(this->skip(count, sizeof(T)));
  69. }
  70. // primitives
  71. bool readBool();
  72. SkColor readColor();
  73. int32_t readInt();
  74. SkScalar readScalar();
  75. uint32_t readUInt();
  76. int32_t read32();
  77. template <typename T> T read32LE(T max) {
  78. uint32_t value = this->readUInt();
  79. if (!this->validate(value <= static_cast<uint32_t>(max))) {
  80. value = 0;
  81. }
  82. return static_cast<T>(value);
  83. }
  84. // peek
  85. uint8_t peekByte();
  86. void readString(SkString* string);
  87. // common data structures
  88. void readColor4f(SkColor4f* color);
  89. void readPoint(SkPoint* point);
  90. SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; }
  91. void readPoint3(SkPoint3* point);
  92. void readMatrix(SkMatrix* matrix);
  93. void readIRect(SkIRect* rect);
  94. void readRect(SkRect* rect);
  95. void readRRect(SkRRect* rrect);
  96. void readRegion(SkRegion* region);
  97. void readPath(SkPath* path);
  98. SkReadPaintResult readPaint(SkPaint* paint, SkFont* font) {
  99. return SkPaintPriv::Unflatten(paint, *this, font);
  100. }
  101. SkFlattenable* readFlattenable(SkFlattenable::Type);
  102. template <typename T> sk_sp<T> readFlattenable() {
  103. return sk_sp<T>((T*)this->readFlattenable(T::GetFlattenableType()));
  104. }
  105. sk_sp<SkColorFilter> readColorFilter() { return this->readFlattenable<SkColorFilter>(); }
  106. sk_sp<SkDrawLooper> readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); }
  107. sk_sp<SkImageFilter> readImageFilter() { return this->readFlattenable<SkImageFilter>(); }
  108. sk_sp<SkMaskFilter> readMaskFilter() { return this->readFlattenable<SkMaskFilterBase>(); }
  109. sk_sp<SkPathEffect> readPathEffect() { return this->readFlattenable<SkPathEffect>(); }
  110. sk_sp<SkShader> readShader() { return this->readFlattenable<SkShaderBase>(); }
  111. // Reads SkAlign4(bytes), but will only copy bytes into the buffer.
  112. bool readPad32(void* buffer, size_t bytes);
  113. // binary data and arrays
  114. bool readByteArray(void* value, size_t size);
  115. bool readColorArray(SkColor* colors, size_t size);
  116. bool readColor4fArray(SkColor4f* colors, size_t size);
  117. bool readIntArray(int32_t* values, size_t size);
  118. bool readPointArray(SkPoint* points, size_t size);
  119. bool readScalarArray(SkScalar* values, size_t size);
  120. sk_sp<SkData> readByteArrayAsData();
  121. // helpers to get info about arrays and binary data
  122. uint32_t getArrayCount();
  123. // If there is a real error (e.g. data is corrupted) this returns null. If the image cannot
  124. // be created (e.g. it was not originally encoded) then this returns an image that doesn't
  125. // draw.
  126. sk_sp<SkImage> readImage();
  127. sk_sp<SkTypeface> readTypeface();
  128. void setTypefaceArray(sk_sp<SkTypeface> array[], int count) {
  129. fTFArray = array;
  130. fTFCount = count;
  131. }
  132. /**
  133. * Call this with a pre-loaded array of Factories, in the same order as
  134. * were created/written by the writer. SkPicture uses this.
  135. */
  136. void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
  137. fFactoryArray = array;
  138. fFactoryCount = count;
  139. }
  140. void setDeserialProcs(const SkDeserialProcs& procs);
  141. const SkDeserialProcs& getDeserialProcs() const { return fProcs; }
  142. /**
  143. * If isValid is false, sets the buffer to be "invalid". Returns true if the buffer
  144. * is still valid.
  145. */
  146. bool validate(bool isValid) {
  147. if (!isValid) {
  148. this->setInvalid();
  149. }
  150. return !fError;
  151. }
  152. /**
  153. * Helper function to do a preflight check before a large allocation or read.
  154. * Returns true if there is enough bytes in the buffer to read n elements of T.
  155. * If not, the buffer will be "invalid" and false will be returned.
  156. */
  157. template <typename T>
  158. bool validateCanReadN(size_t n) {
  159. return this->validate(n <= (fReader.available() / sizeof(T)));
  160. }
  161. bool isValid() const { return !fError; }
  162. bool validateIndex(int index, int count) {
  163. return this->validate(index >= 0 && index < count);
  164. }
  165. // Utilities that mark the buffer invalid if the requested value is out-of-range
  166. // If the read value is outside of the range, validate(false) is called, and min
  167. // is returned, else the value is returned.
  168. int32_t checkInt(int min, int max);
  169. template <typename T> T checkRange(T min, T max) {
  170. return static_cast<T>(this->checkInt(static_cast<int32_t>(min),
  171. static_cast<int32_t>(max)));
  172. }
  173. SkFilterQuality checkFilterQuality();
  174. private:
  175. const char* readString(size_t* length);
  176. void setInvalid();
  177. bool readArray(void* value, size_t size, size_t elementSize);
  178. void setMemory(const void*, size_t);
  179. SkReader32 fReader;
  180. // Only used if we do not have an fFactoryArray.
  181. SkTHashMap<uint32_t, SkFlattenable::Factory> fFlattenableDict;
  182. int fVersion;
  183. sk_sp<SkTypeface>* fTFArray;
  184. int fTFCount;
  185. SkFlattenable::Factory* fFactoryArray;
  186. int fFactoryCount;
  187. SkDeserialProcs fProcs;
  188. static bool IsPtrAlign4(const void* ptr) {
  189. return SkIsAlign4((uintptr_t)ptr);
  190. }
  191. bool fError = false;
  192. };
  193. #else // #ifndef SK_DISABLE_READBUFFER
  194. class SkReadBuffer {
  195. public:
  196. SkReadBuffer() {}
  197. SkReadBuffer(const void*, size_t) {}
  198. enum Version {
  199. kTileModeInBlurImageFilter_Version = 56,
  200. kTileInfoInSweepGradient_Version = 57,
  201. k2PtConicalNoFlip_Version = 58,
  202. kRemovePictureImageFilterLocalSpace = 59,
  203. kRemoveHeaderFlags_Version = 60,
  204. kTwoColorDrawShadow_Version = 61,
  205. kDontNegateImageSize_Version = 62,
  206. kStoreImageBounds_Version = 63,
  207. kRemoveOccluderFromBlurMaskFilter = 64,
  208. kFloat4PaintColor_Version = 65,
  209. kSaveBehind_Version = 66,
  210. kSerializeFonts_Version = 67,
  211. kPaintDoesntSerializeFonts_Version = 68,
  212. };
  213. bool isVersionLT(Version) const { return false; }
  214. uint32_t getVersion() const { return 0xffffffff; }
  215. void setVersion(int) {}
  216. size_t size() const { return 0; }
  217. size_t offset() const { return 0; }
  218. bool eof() { return true; }
  219. size_t available() const { return 0; }
  220. const void* skip(size_t) { return nullptr; }
  221. const void* skip(size_t, size_t) { return nullptr; }
  222. template <typename T> const T* skipT() { return nullptr; }
  223. template <typename T> const T* skipT(size_t) { return nullptr; }
  224. bool readBool() { return 0; }
  225. SkColor readColor() { return 0; }
  226. int32_t readInt() { return 0; }
  227. SkScalar readScalar() { return 0; }
  228. uint32_t readUInt() { return 0; }
  229. int32_t read32() { return 0; }
  230. template <typename T> T read32LE(T max) { return max; }
  231. uint8_t peekByte() { return 0; }
  232. void readColor4f(SkColor4f* out) { *out = SkColor4f{0,0,0,0}; }
  233. void readPoint (SkPoint* out) { *out = SkPoint{0,0}; }
  234. void readPoint3 (SkPoint3* out) { *out = SkPoint3{0,0,0}; }
  235. void readMatrix (SkMatrix* out) { *out = SkMatrix::I(); }
  236. void readIRect (SkIRect* out) { *out = SkIRect{0,0,0,0}; }
  237. void readRect (SkRect* out) { *out = SkRect{0,0,0,0}; }
  238. void readRRect (SkRRect* out) { *out = SkRRect(); }
  239. void readRegion (SkRegion* out) { *out = SkRegion(); }
  240. void readString (SkString* out) { *out = SkString(); }
  241. void readPath (SkPath* out) { *out = SkPath(); }
  242. SkReadPaintResult readPaint (SkPaint* out, SkFont* font) {
  243. *out = SkPaint();
  244. if (font) {
  245. *font = SkFont();
  246. }
  247. return kFailed_ReadPaint;
  248. }
  249. SkPoint readPoint() { return {0,0}; }
  250. SkFlattenable* readFlattenable(SkFlattenable::Type) { return nullptr; }
  251. template <typename T> sk_sp<T> readFlattenable() { return nullptr; }
  252. sk_sp<SkColorFilter> readColorFilter() { return nullptr; }
  253. sk_sp<SkDrawLooper> readDrawLooper() { return nullptr; }
  254. sk_sp<SkImageFilter> readImageFilter() { return nullptr; }
  255. sk_sp<SkMaskFilter> readMaskFilter() { return nullptr; }
  256. sk_sp<SkPathEffect> readPathEffect() { return nullptr; }
  257. sk_sp<SkShader> readShader() { return nullptr; }
  258. bool readPad32 (void*, size_t) { return false; }
  259. bool readByteArray (void*, size_t) { return false; }
  260. bool readColorArray (SkColor*, size_t) { return false; }
  261. bool readColor4fArray(SkColor4f*, size_t) { return false; }
  262. bool readIntArray (int32_t*, size_t) { return false; }
  263. bool readPointArray (SkPoint*, size_t) { return false; }
  264. bool readScalarArray (SkScalar*, size_t) { return false; }
  265. sk_sp<SkData> readByteArrayAsData() { return nullptr; }
  266. uint32_t getArrayCount() { return 0; }
  267. sk_sp<SkImage> readImage() { return nullptr; }
  268. sk_sp<SkTypeface> readTypeface() { return nullptr; }
  269. bool validate(bool) { return false; }
  270. template <typename T> bool validateCanReadN(size_t) { return false; }
  271. bool isValid() const { return false; }
  272. bool validateIndex(int, int) { return false; }
  273. int32_t checkInt(int min, int) { return min; }
  274. template <typename T> T checkRange(T min, T) { return min; }
  275. SkFilterQuality checkFilterQuality() { return SkFilterQuality::kNone_SkFilterQuality; }
  276. void setTypefaceArray(sk_sp<SkTypeface>[], int) {}
  277. void setFactoryPlayback(SkFlattenable::Factory[], int) {}
  278. void setDeserialProcs(const SkDeserialProcs&) {}
  279. const SkDeserialProcs& getDeserialProcs() const {
  280. static const SkDeserialProcs procs;
  281. return procs;
  282. }
  283. };
  284. #endif // #ifndef SK_DISABLE_READBUFFER
  285. #endif // SkReadBuffer_DEFINED