SkPicture.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright 2007 The Android Open Source Project
  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. #include "include/core/SkPicture.h"
  8. #include "include/core/SkImageGenerator.h"
  9. #include "include/core/SkPictureRecorder.h"
  10. #include "include/core/SkSerialProcs.h"
  11. #include "include/private/SkTo.h"
  12. #include "src/core/SkMathPriv.h"
  13. #include "src/core/SkPictureCommon.h"
  14. #include "src/core/SkPictureData.h"
  15. #include "src/core/SkPicturePlayback.h"
  16. #include "src/core/SkPicturePriv.h"
  17. #include "src/core/SkPictureRecord.h"
  18. #include <atomic>
  19. // When we read/write the SkPictInfo via a stream, we have a sentinel byte right after the info.
  20. // Note: in the read/write buffer versions, we have a slightly different convention:
  21. // We have a sentinel int32_t:
  22. // 0 : failure
  23. // 1 : PictureData
  24. // <0 : -size of the custom data
  25. enum {
  26. kFailure_TrailingStreamByteAfterPictInfo = 0, // nothing follows
  27. kPictureData_TrailingStreamByteAfterPictInfo = 1, // SkPictureData follows
  28. kCustom_TrailingStreamByteAfterPictInfo = 2, // -size32 follows
  29. };
  30. /* SkPicture impl. This handles generic responsibilities like unique IDs and serialization. */
  31. SkPicture::SkPicture() {
  32. static std::atomic<uint32_t> nextID{1};
  33. do {
  34. fUniqueID = nextID.fetch_add(+1, std::memory_order_relaxed);
  35. } while (fUniqueID == 0);
  36. }
  37. static const char kMagic[] = { 's', 'k', 'i', 'a', 'p', 'i', 'c', 't' };
  38. SkPictInfo SkPicture::createHeader() const {
  39. SkPictInfo info;
  40. // Copy magic bytes at the beginning of the header
  41. static_assert(sizeof(kMagic) == 8, "");
  42. static_assert(sizeof(kMagic) == sizeof(info.fMagic), "");
  43. memcpy(info.fMagic, kMagic, sizeof(kMagic));
  44. // Set picture info after magic bytes in the header
  45. info.setVersion(CURRENT_PICTURE_VERSION);
  46. info.fCullRect = this->cullRect();
  47. return info;
  48. }
  49. bool SkPicture::IsValidPictInfo(const SkPictInfo& info) {
  50. if (0 != memcmp(info.fMagic, kMagic, sizeof(kMagic))) {
  51. return false;
  52. }
  53. if (info.getVersion() < MIN_PICTURE_VERSION || info.getVersion() > CURRENT_PICTURE_VERSION) {
  54. return false;
  55. }
  56. return true;
  57. }
  58. bool SkPicture::StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) {
  59. if (!stream) {
  60. return false;
  61. }
  62. SkPictInfo info;
  63. SkASSERT(sizeof(kMagic) == sizeof(info.fMagic));
  64. if (stream->read(&info.fMagic, sizeof(kMagic)) != sizeof(kMagic)) {
  65. return false;
  66. }
  67. uint32_t version;
  68. if (!stream->readU32(&version)) { return false; }
  69. info.setVersion(version);
  70. if (!stream->readScalar(&info.fCullRect.fLeft )) { return false; }
  71. if (!stream->readScalar(&info.fCullRect.fTop )) { return false; }
  72. if (!stream->readScalar(&info.fCullRect.fRight )) { return false; }
  73. if (!stream->readScalar(&info.fCullRect.fBottom)) { return false; }
  74. if (info.getVersion() < SkReadBuffer::kRemoveHeaderFlags_Version) {
  75. if (!stream->readU32(nullptr)) { return false; }
  76. }
  77. if (!IsValidPictInfo(info)) { return false; }
  78. if (pInfo) { *pInfo = info; }
  79. return true;
  80. }
  81. bool SkPicture_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) {
  82. return SkPicture::StreamIsSKP(stream, pInfo);
  83. }
  84. bool SkPicture::BufferIsSKP(SkReadBuffer* buffer, SkPictInfo* pInfo) {
  85. SkPictInfo info;
  86. SkASSERT(sizeof(kMagic) == sizeof(info.fMagic));
  87. if (!buffer->readByteArray(&info.fMagic, sizeof(kMagic))) {
  88. return false;
  89. }
  90. info.setVersion(buffer->readUInt());
  91. buffer->readRect(&info.fCullRect);
  92. if (info.getVersion() < SkReadBuffer::kRemoveHeaderFlags_Version) {
  93. (void)buffer->readUInt(); // used to be flags
  94. }
  95. if (IsValidPictInfo(info)) {
  96. if (pInfo) { *pInfo = info; }
  97. return true;
  98. }
  99. return false;
  100. }
  101. sk_sp<SkPicture> SkPicture::Forwardport(const SkPictInfo& info,
  102. const SkPictureData* data,
  103. SkReadBuffer* buffer) {
  104. if (!data) {
  105. return nullptr;
  106. }
  107. if (!data->opData()) {
  108. return nullptr;
  109. }
  110. SkPicturePlayback playback(data);
  111. SkPictureRecorder r;
  112. playback.draw(r.beginRecording(info.fCullRect), nullptr/*no callback*/, buffer);
  113. return r.finishRecordingAsPicture();
  114. }
  115. sk_sp<SkPicture> SkPicture::MakeFromStream(SkStream* stream, const SkDeserialProcs* procs) {
  116. return MakeFromStream(stream, procs, nullptr);
  117. }
  118. sk_sp<SkPicture> SkPicture::MakeFromData(const void* data, size_t size,
  119. const SkDeserialProcs* procs) {
  120. if (!data) {
  121. return nullptr;
  122. }
  123. SkMemoryStream stream(data, size);
  124. return MakeFromStream(&stream, procs, nullptr);
  125. }
  126. sk_sp<SkPicture> SkPicture::MakeFromData(const SkData* data, const SkDeserialProcs* procs) {
  127. if (!data) {
  128. return nullptr;
  129. }
  130. SkMemoryStream stream(data->data(), data->size());
  131. return MakeFromStream(&stream, procs, nullptr);
  132. }
  133. sk_sp<SkPicture> SkPicture::MakeFromStream(SkStream* stream, const SkDeserialProcs* procsPtr,
  134. SkTypefacePlayback* typefaces) {
  135. SkPictInfo info;
  136. if (!StreamIsSKP(stream, &info)) {
  137. return nullptr;
  138. }
  139. SkDeserialProcs procs;
  140. if (procsPtr) {
  141. procs = *procsPtr;
  142. }
  143. uint8_t trailingStreamByteAfterPictInfo;
  144. if (!stream->readU8(&trailingStreamByteAfterPictInfo)) { return nullptr; }
  145. switch (trailingStreamByteAfterPictInfo) {
  146. case kPictureData_TrailingStreamByteAfterPictInfo: {
  147. std::unique_ptr<SkPictureData> data(
  148. SkPictureData::CreateFromStream(stream, info, procs, typefaces));
  149. return Forwardport(info, data.get(), nullptr);
  150. }
  151. case kCustom_TrailingStreamByteAfterPictInfo: {
  152. int32_t ssize;
  153. if (!stream->readS32(&ssize) || ssize >= 0 || !procs.fPictureProc) {
  154. return nullptr;
  155. }
  156. size_t size = sk_negate_to_size_t(ssize);
  157. auto data = SkData::MakeUninitialized(size);
  158. if (stream->read(data->writable_data(), size) != size) {
  159. return nullptr;
  160. }
  161. return procs.fPictureProc(data->data(), size, procs.fPictureCtx);
  162. }
  163. default: // fall through to error return
  164. break;
  165. }
  166. return nullptr;
  167. }
  168. sk_sp<SkPicture> SkPicturePriv::MakeFromBuffer(SkReadBuffer& buffer) {
  169. SkPictInfo info;
  170. if (!SkPicture::BufferIsSKP(&buffer, &info)) {
  171. return nullptr;
  172. }
  173. // size should be 0, 1, or negative
  174. int32_t ssize = buffer.read32();
  175. if (ssize < 0) {
  176. const SkDeserialProcs& procs = buffer.getDeserialProcs();
  177. if (!procs.fPictureProc) {
  178. return nullptr;
  179. }
  180. size_t size = sk_negate_to_size_t(ssize);
  181. return procs.fPictureProc(buffer.skip(size), size, procs.fPictureCtx);
  182. }
  183. if (ssize != 1) {
  184. // 1 is the magic 'size' that means SkPictureData follows
  185. return nullptr;
  186. }
  187. std::unique_ptr<SkPictureData> data(SkPictureData::CreateFromBuffer(buffer, info));
  188. return SkPicture::Forwardport(info, data.get(), &buffer);
  189. }
  190. SkPictureData* SkPicture::backport() const {
  191. SkPictInfo info = this->createHeader();
  192. SkPictureRecord rec(SkISize::Make(info.fCullRect.width(), info.fCullRect.height()), 0/*flags*/);
  193. rec.beginRecording();
  194. this->playback(&rec);
  195. rec.endRecording();
  196. return new SkPictureData(rec, info);
  197. }
  198. void SkPicture::serialize(SkWStream* stream, const SkSerialProcs* procs) const {
  199. this->serialize(stream, procs, nullptr);
  200. }
  201. sk_sp<SkData> SkPicture::serialize(const SkSerialProcs* procs) const {
  202. SkDynamicMemoryWStream stream;
  203. this->serialize(&stream, procs, nullptr);
  204. return stream.detachAsData();
  205. }
  206. static sk_sp<SkData> custom_serialize(const SkPicture* picture, const SkSerialProcs& procs) {
  207. if (procs.fPictureProc) {
  208. auto data = procs.fPictureProc(const_cast<SkPicture*>(picture), procs.fPictureCtx);
  209. if (data) {
  210. size_t size = data->size();
  211. if (!SkTFitsIn<int32_t>(size) || size <= 1) {
  212. return SkData::MakeEmpty();
  213. }
  214. return data;
  215. }
  216. }
  217. return nullptr;
  218. }
  219. static bool write_pad32(SkWStream* stream, const void* data, size_t size) {
  220. if (!stream->write(data, size)) {
  221. return false;
  222. }
  223. if (size & 3) {
  224. uint32_t zero = 0;
  225. return stream->write(&zero, 4 - (size & 3));
  226. }
  227. return true;
  228. }
  229. // Private serialize.
  230. // SkPictureData::serialize makes a first pass on all subpictures, indicatewd by textBlobsOnly=true,
  231. // to fill typefaceSet.
  232. void SkPicture::serialize(SkWStream* stream, const SkSerialProcs* procsPtr,
  233. SkRefCntSet* typefaceSet, bool textBlobsOnly) const {
  234. SkSerialProcs procs;
  235. if (procsPtr) {
  236. procs = *procsPtr;
  237. }
  238. SkPictInfo info = this->createHeader();
  239. stream->write(&info, sizeof(info));
  240. if (auto custom = custom_serialize(this, procs)) {
  241. int32_t size = SkToS32(custom->size());
  242. if (size == 0) {
  243. stream->write8(kFailure_TrailingStreamByteAfterPictInfo);
  244. return;
  245. }
  246. stream->write8(kCustom_TrailingStreamByteAfterPictInfo);
  247. stream->write32(-size); // negative for custom format
  248. write_pad32(stream, custom->data(), size);
  249. return;
  250. }
  251. std::unique_ptr<SkPictureData> data(this->backport());
  252. if (data) {
  253. stream->write8(kPictureData_TrailingStreamByteAfterPictInfo);
  254. data->serialize(stream, procs, typefaceSet, textBlobsOnly);
  255. } else {
  256. stream->write8(kFailure_TrailingStreamByteAfterPictInfo);
  257. }
  258. }
  259. void SkPicturePriv::Flatten(const sk_sp<const SkPicture> picture, SkWriteBuffer& buffer) {
  260. SkPictInfo info = picture->createHeader();
  261. std::unique_ptr<SkPictureData> data(picture->backport());
  262. buffer.writeByteArray(&info.fMagic, sizeof(info.fMagic));
  263. buffer.writeUInt(info.getVersion());
  264. buffer.writeRect(info.fCullRect);
  265. if (auto custom = custom_serialize(picture.get(), buffer.fProcs)) {
  266. int32_t size = SkToS32(custom->size());
  267. buffer.write32(-size); // negative for custom format
  268. buffer.writePad32(custom->data(), size);
  269. return;
  270. }
  271. if (data) {
  272. buffer.write32(1); // special size meaning SkPictureData
  273. data->flatten(buffer);
  274. } else {
  275. buffer.write32(0); // signal no content
  276. }
  277. }
  278. sk_sp<SkPicture> SkPicture::MakePlaceholder(SkRect cull) {
  279. struct Placeholder : public SkPicture {
  280. explicit Placeholder(SkRect cull) : fCull(cull) {}
  281. void playback(SkCanvas*, AbortCallback*) const override { }
  282. // approximateOpCount() needs to be greater than kMaxPictureOpsToUnrollInsteadOfRef
  283. // in SkCanvas.cpp to avoid that unrolling. SK_MaxS32 can't not be big enough!
  284. int approximateOpCount() const override { return SK_MaxS32; }
  285. size_t approximateBytesUsed() const override { return sizeof(*this); }
  286. SkRect cullRect() const override { return fCull; }
  287. SkRect fCull;
  288. };
  289. return sk_make_sp<Placeholder>(cull);
  290. }