Debian Codegraph @

tools/ganesh/DDLPromiseImageHelper.h

raw
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
8 #ifndef PromiseImageHelper_DEFINED
9 #define PromiseImageHelper_DEFINED
10
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkYUVAPixmaps.h"
14 #include "include/gpu/ganesh/GrBackendSurface.h"
15 #include "include/private/base/SkTArray.h"
16 #include "include/private/chromium/GrPromiseImageTexture.h"
17 #include "src/base/SkTLazy.h"
18 #include "src/core/SkCachedData.h"
19
20 class GrContextThreadSafeProxy;
21 class GrDirectContext;
22 class SkImage;
23 class SkMipmap;
24 class SkPicture;
25 class SkTaskGroup;
26
27 // This class acts as a proxy for a GrBackendTexture that backs an image.
28 // Whenever a promise image is created for the image, the promise image receives a ref to
29 // potentially several of these objects. Once all the promise images receive their done
30 // callbacks this object is deleted - removing the GrBackendTexture from VRAM.
31 // Note that while the DDLs are being created in the threads, the PromiseImageHelper holds
32 // a ref on all the PromiseImageCallbackContexts. However, once all the threads are done
33 // it drops all of its refs (via "reset").
34 class PromiseImageCallbackContext : public SkRefCnt {
35 public:
36 PromiseImageCallbackContext(GrDirectContext* direct, GrBackendFormat backendFormat)
37 : fContext(direct)
38 , fBackendFormat(backendFormat) {}
39
40 ~PromiseImageCallbackContext() override;
41
42 const GrBackendFormat& backendFormat() const { return fBackendFormat; }
43
44 void setBackendTexture(const GrBackendTexture& backendTexture);
45
46 void destroyBackendTexture();
47
48 sk_sp<GrPromiseImageTexture> fulfill() {
49 ++fTotalFulfills;
50 return fPromiseImageTexture;
51 }
52
53 void release() {
54 ++fDoneCnt;
55 SkASSERT(fDoneCnt <= fNumImages);
56 }
57
58 void wasAddedToImage() { fNumImages++; }
59
60 const GrPromiseImageTexture* promiseImageTexture() const {
61 return fPromiseImageTexture.get();
62 }
63
64 static sk_sp<GrPromiseImageTexture> PromiseImageFulfillProc(void* textureContext) {
65 auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
66 return callbackContext->fulfill();
67 }
68
69 static void PromiseImageReleaseProc(void* textureContext) {
70 auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
71 callbackContext->release();
72 callbackContext->unref();
73 }
74
75 private:
76 GrDirectContext* fContext;
77 GrBackendFormat fBackendFormat;
78 sk_sp<GrPromiseImageTexture> fPromiseImageTexture;
79 int fNumImages = 0;
80 int fTotalFulfills = 0;
81 int fDoneCnt = 0;
82
83 using INHERITED = SkRefCnt;
84 };
85
86 // This class consolidates tracking & extraction of the original image data from an skp,
87 // the upload of said data to the GPU and the fulfillment of promise images.
88 //
89 // The way this works is:
90 // the original skp is converted to SkData and all its image info is extracted into this
91 // class and only indices into this class are left in the SkData
92 // the PromiseImageCallbackContexts are created for each image
93 // the SkData is then reinflated into an SkPicture with promise images replacing all the indices
94 // (all in recreateSKP)
95 //
96 // Prior to replaying in threads, all the images are uploaded to the gpu
97 // (in uploadAllToGPU)
98 //
99 // This class is then reset - dropping all of its refs on the PromiseImageCallbackContexts
100 //
101 // Each done callback unrefs its PromiseImageCallbackContext so, once all the promise images
102 // are done, the PromiseImageCallbackContext is freed and its GrBackendTexture removed
103 // from VRAM
104 //
105 // Note: if DDLs are going to be replayed multiple times, the reset call can be delayed until
106 // all the replaying is complete. This will pin the GrBackendTextures in VRAM.
107 class DDLPromiseImageHelper {
108 public:
109 explicit DDLPromiseImageHelper(
110 const SkYUVAPixmapInfo::SupportedDataTypes& supportedYUVADataTypes)
111 : fSupportedYUVADataTypes(supportedYUVADataTypes) {}
112 ~DDLPromiseImageHelper() = default;
113
114 // Convert the input SkPicture into a new one which has promise images rather than live
115 // images.
116 sk_sp<SkPicture> recreateSKP(GrDirectContext*, SkPicture*);
117
118 void uploadAllToGPU(SkTaskGroup*, GrDirectContext*);
119 void deleteAllFromGPU(SkTaskGroup*, GrDirectContext*);
120
121 // Remove this class' refs on the promise images and the PromiseImageCallbackContexts
122 void reset() {
123 fImageInfo.clear();
124 fPromiseImages.clear();
125 }
126
127 private:
128 void createCallbackContexts(GrDirectContext*);
129 // reinflate a deflated SKP, replacing all the indices with promise images.
130 sk_sp<SkPicture> reinflateSKP(sk_sp<GrContextThreadSafeProxy>, const SkData* deflatedSKP);
131
132 // This is the information extracted into this class from the parsing of the skp file.
133 // Once it has all been uploaded to the GPU and distributed to the promise images, it
134 // is all dropped via "reset".
135 class PromiseImageInfo {
136 public:
137 PromiseImageInfo(int index, uint32_t originalUniqueID, const SkImageInfo& ii);
138 PromiseImageInfo(PromiseImageInfo&& other);
139 ~PromiseImageInfo();
140
141 int index() const { return fIndex; }
142 uint32_t originalUniqueID() const { return fOriginalUniqueID; }
143 bool isYUV() const { return fYUVAPixmaps.isValid(); }
144
145 SkISize overallDimensions() const { return fImageInfo.dimensions(); }
146 SkColorType overallColorType() const { return fImageInfo.colorType(); }
147 SkAlphaType overallAlphaType() const { return fImageInfo.alphaType(); }
148 sk_sp<SkColorSpace> refOverallColorSpace() const { return fImageInfo.refColorSpace(); }
149
150 const SkYUVAInfo& yuvaInfo() const { return fYUVAPixmaps.yuvaInfo(); }
151
152 const SkPixmap& yuvPixmap(int index) const {
153 SkASSERT(this->isYUV());
154 return fYUVAPixmaps.planes()[index];
155 }
156
157 const SkBitmap& baseLevel() const {
158 SkASSERT(!this->isYUV());
159 return fBaseLevel;
160 }
161 // This returns an array of all the available mipLevels - suitable for passing into
162 // createBackendTexture.
163 std::unique_ptr<SkPixmap[]> normalMipLevels() const;
164 int numMipLevels() const;
165
166 void setCallbackContext(int index, sk_sp<PromiseImageCallbackContext> callbackContext) {
167 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
168 fCallbackContexts[index] = callbackContext;
169 }
170 PromiseImageCallbackContext* callbackContext(int index) const {
171 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
172 return fCallbackContexts[index].get();
173 }
174 sk_sp<PromiseImageCallbackContext> refCallbackContext(int index) const {
175 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
176 return fCallbackContexts[index];
177 }
178
179 skgpu::Mipmapped mipmapped(int index) const {
180 if (this->isYUV()) {
181 return skgpu::Mipmapped::kNo;
182 }
183 return fMipLevels ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo;
184 }
185 const GrBackendFormat& backendFormat(int index) const {
186 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
187 return fCallbackContexts[index]->backendFormat();
188 }
189 const GrPromiseImageTexture* promiseTexture(int index) const {
190 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
191 return fCallbackContexts[index]->promiseImageTexture();
192 }
193
194 void setMipLevels(const SkBitmap& baseLevel, std::unique_ptr<SkMipmap> mipLevels);
195
196 /** Takes ownership of the plane data. */
197 void setYUVPlanes(SkYUVAPixmaps yuvaPixmaps) { fYUVAPixmaps = std::move(yuvaPixmaps); }
198
199 private:
200 const int fIndex; // index in the 'fImageInfo' array
201 const uint32_t fOriginalUniqueID; // original ID for deduping
202
203 const SkImageInfo fImageInfo; // info for the overarching image
204
205 // CPU-side cache of a normal SkImage's mipmap levels
206 SkBitmap fBaseLevel;
207 std::unique_ptr<SkMipmap> fMipLevels;
208
209 // CPU-side cache of a YUV SkImage's contents
210 SkYUVAPixmaps fYUVAPixmaps;
211
212 // Up to SkYUVASizeInfo::kMaxCount for a YUVA image. Only one for a normal image.
213 sk_sp<PromiseImageCallbackContext> fCallbackContexts[SkYUVAInfo::kMaxPlanes];
214 };
215
216 struct DeserialImageProcContext {
217 sk_sp<GrContextThreadSafeProxy> fThreadSafeProxy;
218 DDLPromiseImageHelper* fHelper;
219 };
220
221 static void CreateBETexturesForPromiseImage(GrDirectContext*, PromiseImageInfo*);
222 static void DeleteBETexturesForPromiseImage(PromiseImageInfo*);
223
224 static sk_sp<SkImage> CreatePromiseImages(const void* rawData, size_t length, void* ctxIn);
225
226 bool isValidID(int id) const { return id >= 0 && id < fImageInfo.size(); }
227 const PromiseImageInfo& getInfo(int id) const { return fImageInfo[id]; }
228 void uploadImage(GrDirectContext*, PromiseImageInfo*);
229
230 // returns -1 if not found
231 int findImage(SkImage* image) const;
232
233 // returns -1 on failure
234 int addImage(SkImage* image);
235
236 // returns -1 on failure
237 int findOrDefineImage(SkImage* image);
238
239 SkYUVAPixmapInfo::SupportedDataTypes fSupportedYUVADataTypes;
240 skia_private::TArray<PromiseImageInfo> fImageInfo;
241
242 // TODO: review the use of 'fPromiseImages' - it doesn't seem useful/necessary
243 skia_private::TArray<sk_sp<SkImage>> fPromiseImages; // All the promise images in the
244 // reconstituted picture
245 };
246
247 #endif