DDLPromiseImageHelper.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright 2018 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 PromiseImageHelper_DEFINED
  8. #define PromiseImageHelper_DEFINED
  9. #include "include/core/SkBitmap.h"
  10. #include "include/core/SkDeferredDisplayListRecorder.h"
  11. #include "include/core/SkPromiseImageTexture.h"
  12. #include "include/core/SkYUVAIndex.h"
  13. #include "include/core/SkYUVASizeInfo.h"
  14. #include "include/gpu/GrBackendSurface.h"
  15. #include "include/private/SkTArray.h"
  16. #include "src/core/SkCachedData.h"
  17. #include "src/core/SkTLazy.h"
  18. class GrContext;
  19. class SkImage;
  20. class SkPicture;
  21. struct SkYUVAIndex;
  22. // This class consolidates tracking & extraction of the original image data from an skp,
  23. // the upload of said data to the GPU and the fulfillment of promise images.
  24. //
  25. // The way this works is:
  26. // the original skp is converted to SkData and all its image info is extracted into this
  27. // class and only indices into this class are left in the SkData (via deflateSKP)
  28. //
  29. // Prior to replaying in threads, all the images stored in this class are uploaded to the
  30. // gpu and PromiseImageCallbackContexts are created for them (via uploadAllToGPU)
  31. //
  32. // Each thread reinflates the SkData into an SkPicture replacing all the indices w/
  33. // promise images (all using the same GrBackendTexture and getting a ref to the
  34. // appropriate PromiseImageCallbackContext) (via reinflateSKP).
  35. //
  36. // This class is then reset - dropping all of its refs on the PromiseImageCallbackContexts
  37. //
  38. // Each done callback unrefs its PromiseImageCallbackContext so, once all the promise images
  39. // are done, the PromiseImageCallbackContext is freed and its GrBackendTexture removed
  40. // from VRAM
  41. //
  42. // Note: if DDLs are going to be replayed multiple times, the reset call can be delayed until
  43. // all the replaying is complete. This will pin the GrBackendTextures in VRAM.
  44. class DDLPromiseImageHelper {
  45. public:
  46. DDLPromiseImageHelper() = default;
  47. ~DDLPromiseImageHelper() = default;
  48. // Convert the SkPicture into SkData replacing all the SkImages with an index.
  49. sk_sp<SkData> deflateSKP(const SkPicture* inputPicture);
  50. void uploadAllToGPU(GrContext* context);
  51. // reinflate a deflated SKP, replacing all the indices with promise images.
  52. sk_sp<SkPicture> reinflateSKP(SkDeferredDisplayListRecorder*,
  53. SkData* compressedPicture,
  54. SkTArray<sk_sp<SkImage>>* promiseImages) const;
  55. // Remove this class' refs on the PromiseImageCallbackContexts
  56. void reset() { fImageInfo.reset(); }
  57. private:
  58. // This class acts as a proxy for a GrBackendTexture that is part of an image.
  59. // Whenever a promise image is created for the image, the promise image receives a ref to
  60. // potentially several of these objects. Once all the promise images receive their done
  61. // callbacks this object is deleted - removing the GrBackendTexture from VRAM.
  62. // Note that while the DDLs are being created in the threads, the PromiseImageHelper holds
  63. // a ref on all the PromiseImageCallbackContexts. However, once all the threads are done
  64. // it drops all of its refs (via "reset").
  65. class PromiseImageCallbackContext : public SkRefCnt {
  66. public:
  67. PromiseImageCallbackContext(GrContext* context) : fContext(context) {}
  68. ~PromiseImageCallbackContext();
  69. void setBackendTexture(const GrBackendTexture& backendTexture);
  70. sk_sp<SkPromiseImageTexture> fulfill() {
  71. SkASSERT(fPromiseImageTexture);
  72. SkASSERT(fUnreleasedFulfills >= 0);
  73. ++fUnreleasedFulfills;
  74. ++fTotalFulfills;
  75. return fPromiseImageTexture;
  76. }
  77. void release() {
  78. SkASSERT(fUnreleasedFulfills > 0);
  79. --fUnreleasedFulfills;
  80. ++fTotalReleases;
  81. }
  82. void done() {
  83. ++fDoneCnt;
  84. SkASSERT(fDoneCnt <= fNumImages);
  85. }
  86. void wasAddedToImage() { fNumImages++; }
  87. const SkPromiseImageTexture* promiseImageTexture() const {
  88. return fPromiseImageTexture.get();
  89. }
  90. private:
  91. GrContext* fContext;
  92. sk_sp<SkPromiseImageTexture> fPromiseImageTexture;
  93. int fNumImages = 0;
  94. int fTotalFulfills = 0;
  95. int fTotalReleases = 0;
  96. int fUnreleasedFulfills = 0;
  97. int fDoneCnt = 0;
  98. typedef SkRefCnt INHERITED;
  99. };
  100. // This is the information extracted into this class from the parsing of the skp file.
  101. // Once it has all been uploaded to the GPU and distributed to the promise images, it
  102. // is all dropped via "reset".
  103. class PromiseImageInfo {
  104. public:
  105. PromiseImageInfo(int index, uint32_t originalUniqueID, const SkImageInfo& ii)
  106. : fIndex(index)
  107. , fOriginalUniqueID(originalUniqueID)
  108. , fImageInfo(ii) {
  109. }
  110. ~PromiseImageInfo() {}
  111. int index() const { return fIndex; }
  112. uint32_t originalUniqueID() const { return fOriginalUniqueID; }
  113. bool isYUV() const { return SkToBool(fYUVData.get()); }
  114. int overallWidth() const { return fImageInfo.width(); }
  115. int overallHeight() const { return fImageInfo.height(); }
  116. SkColorType overallColorType() const { return fImageInfo.colorType(); }
  117. SkAlphaType overallAlphaType() const { return fImageInfo.alphaType(); }
  118. sk_sp<SkColorSpace> refOverallColorSpace() const { return fImageInfo.refColorSpace(); }
  119. SkYUVColorSpace yuvColorSpace() const {
  120. SkASSERT(this->isYUV());
  121. return fYUVColorSpace;
  122. }
  123. const SkYUVAIndex* yuvaIndices() const {
  124. SkASSERT(this->isYUV());
  125. return fYUVAIndices;
  126. }
  127. const SkPixmap& yuvPixmap(int index) const {
  128. SkASSERT(this->isYUV());
  129. SkASSERT(index >= 0 && index < SkYUVASizeInfo::kMaxCount);
  130. return fYUVPlanes[index];
  131. }
  132. const SkBitmap& normalBitmap() const {
  133. SkASSERT(!this->isYUV());
  134. return fBitmap;
  135. }
  136. void setCallbackContext(int index, sk_sp<PromiseImageCallbackContext> callbackContext) {
  137. SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1));
  138. fCallbackContexts[index] = callbackContext;
  139. }
  140. PromiseImageCallbackContext* callbackContext(int index) const {
  141. SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1));
  142. return fCallbackContexts[index].get();
  143. }
  144. sk_sp<PromiseImageCallbackContext> refCallbackContext(int index) const {
  145. SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1));
  146. return fCallbackContexts[index];
  147. }
  148. const SkPromiseImageTexture* promiseTexture(int index) const {
  149. SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1));
  150. return fCallbackContexts[index]->promiseImageTexture();
  151. }
  152. void setNormalBitmap(const SkBitmap& bm) { fBitmap = bm; }
  153. void setYUVData(sk_sp<SkCachedData> yuvData,
  154. SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
  155. SkYUVColorSpace cs) {
  156. fYUVData = yuvData;
  157. memcpy(fYUVAIndices, yuvaIndices, sizeof(fYUVAIndices));
  158. fYUVColorSpace = cs;
  159. }
  160. void addYUVPlane(int index, const SkImageInfo& ii, const void* plane, size_t widthBytes) {
  161. SkASSERT(this->isYUV());
  162. SkASSERT(index >= 0 && index < SkYUVASizeInfo::kMaxCount);
  163. fYUVPlanes[index].reset(ii, plane, widthBytes);
  164. }
  165. private:
  166. const int fIndex; // index in the 'fImageInfo' array
  167. const uint32_t fOriginalUniqueID; // original ID for deduping
  168. const SkImageInfo fImageInfo; // info for the overarching image
  169. // CPU-side cache of a normal SkImage's contents
  170. SkBitmap fBitmap;
  171. // CPU-side cache of a YUV SkImage's contents
  172. sk_sp<SkCachedData> fYUVData; // when !null, this is a YUV image
  173. SkYUVColorSpace fYUVColorSpace = kJPEG_SkYUVColorSpace;
  174. SkYUVAIndex fYUVAIndices[SkYUVAIndex::kIndexCount];
  175. SkPixmap fYUVPlanes[SkYUVASizeInfo::kMaxCount];
  176. // Up to SkYUVASizeInfo::kMaxCount for a YUVA image. Only one for a normal image.
  177. sk_sp<PromiseImageCallbackContext> fCallbackContexts[SkYUVASizeInfo::kMaxCount];
  178. };
  179. // This stack-based context allows each thread to re-inflate the image indices into
  180. // promise images while still using the same GrBackendTexture.
  181. struct PerRecorderContext {
  182. SkDeferredDisplayListRecorder* fRecorder;
  183. const DDLPromiseImageHelper* fHelper;
  184. SkTArray<sk_sp<SkImage>>* fPromiseImages;
  185. };
  186. static sk_sp<SkPromiseImageTexture> PromiseImageFulfillProc(void* textureContext) {
  187. auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
  188. return callbackContext->fulfill();
  189. }
  190. static void PromiseImageReleaseProc(void* textureContext) {
  191. auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
  192. callbackContext->release();
  193. }
  194. static void PromiseImageDoneProc(void* textureContext) {
  195. auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
  196. callbackContext->done();
  197. callbackContext->unref();
  198. }
  199. static sk_sp<SkImage> PromiseImageCreator(const void* rawData, size_t length, void* ctxIn);
  200. bool isValidID(int id) const { return id >= 0 && id < fImageInfo.count(); }
  201. const PromiseImageInfo& getInfo(int id) const { return fImageInfo[id]; }
  202. void uploadImage(GrContext*, PromiseImageInfo*);
  203. // returns -1 if not found
  204. int findImage(SkImage* image) const;
  205. // returns -1 on failure
  206. int addImage(SkImage* image);
  207. // returns -1 on failure
  208. int findOrDefineImage(SkImage* image);
  209. SkTArray<PromiseImageInfo> fImageInfo;
  210. };
  211. #endif