blob: 7fff09e60c13e06eab8e4d5c446d94245826bfc1 [file] [log] [blame]
Robert Phillips96601082018-05-29 16:13:26 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkBitmap.h"
12#include "include/core/SkDeferredDisplayListRecorder.h"
13#include "include/core/SkPromiseImageTexture.h"
14#include "include/core/SkYUVAIndex.h"
15#include "include/core/SkYUVASizeInfo.h"
16#include "include/gpu/GrBackendSurface.h"
17#include "include/private/SkTArray.h"
18#include "src/core/SkCachedData.h"
19#include "src/core/SkTLazy.h"
Robert Phillips96601082018-05-29 16:13:26 -040020
21class GrContext;
Robert Phillips96601082018-05-29 16:13:26 -040022class SkImage;
23class SkPicture;
Jim Van Verth8f11e432018-10-18 14:36:59 -040024struct SkYUVAIndex;
Robert Phillips96601082018-05-29 16:13:26 -040025
26// This class consolidates tracking & extraction of the original image data from an skp,
27// the upload of said data to the GPU and the fulfillment of promise images.
28//
29// The way this works is:
30// the original skp is converted to SkData and all its image info is extracted into this
31// class and only indices into this class are left in the SkData (via deflateSKP)
32//
33// Prior to replaying in threads, all the images stored in this class are uploaded to the
34// gpu and PromiseImageCallbackContexts are created for them (via uploadAllToGPU)
35//
36// Each thread reinflates the SkData into an SkPicture replacing all the indices w/
37// promise images (all using the same GrBackendTexture and getting a ref to the
38// appropriate PromiseImageCallbackContext) (via reinflateSKP).
39//
40// This class is then reset - dropping all of its refs on the PromiseImageCallbackContexts
41//
42// Each done callback unrefs its PromiseImageCallbackContext so, once all the promise images
Robert Phillipse8e2bb12018-09-27 14:26:47 -040043// are done, the PromiseImageCallbackContext is freed and its GrBackendTexture removed
Robert Phillips96601082018-05-29 16:13:26 -040044// from VRAM
45//
46// Note: if DDLs are going to be replayed multiple times, the reset call can be delayed until
47// all the replaying is complete. This will pin the GrBackendTextures in VRAM.
48class DDLPromiseImageHelper {
49public:
Brian Salomon7d88f312019-02-28 10:03:03 -050050 DDLPromiseImageHelper() = default;
51 ~DDLPromiseImageHelper() = default;
Robert Phillips96601082018-05-29 16:13:26 -040052
53 // Convert the SkPicture into SkData replacing all the SkImages with an index.
54 sk_sp<SkData> deflateSKP(const SkPicture* inputPicture);
55
Robert Phillips923181b2020-02-14 12:36:37 -050056 void createCallbackContexts(GrContext*);
57
58 void uploadAllToGPU(SkTaskGroup*, GrContext*);
Robert Phillips96601082018-05-29 16:13:26 -040059
60 // reinflate a deflated SKP, replacing all the indices with promise images.
61 sk_sp<SkPicture> reinflateSKP(SkDeferredDisplayListRecorder*,
62 SkData* compressedPicture,
63 SkTArray<sk_sp<SkImage>>* promiseImages) const;
64
65 // Remove this class' refs on the PromiseImageCallbackContexts
66 void reset() { fImageInfo.reset(); }
67
68private:
Robert Phillipse8e2bb12018-09-27 14:26:47 -040069 // This class acts as a proxy for a GrBackendTexture that is part of an image.
Robert Phillips96601082018-05-29 16:13:26 -040070 // Whenever a promise image is created for the image, the promise image receives a ref to
Robert Phillipse8e2bb12018-09-27 14:26:47 -040071 // potentially several of these objects. Once all the promise images receive their done
72 // callbacks this object is deleted - removing the GrBackendTexture from VRAM.
Robert Phillips96601082018-05-29 16:13:26 -040073 // Note that while the DDLs are being created in the threads, the PromiseImageHelper holds
74 // a ref on all the PromiseImageCallbackContexts. However, once all the threads are done
75 // it drops all of its refs (via "reset").
76 class PromiseImageCallbackContext : public SkRefCnt {
77 public:
Robert Phillips923181b2020-02-14 12:36:37 -050078 PromiseImageCallbackContext(GrContext* context, GrBackendFormat backendFormat)
79 : fContext(context)
80 , fBackendFormat(backendFormat) {}
Robert Phillips96601082018-05-29 16:13:26 -040081
82 ~PromiseImageCallbackContext();
83
Robert Phillips923181b2020-02-14 12:36:37 -050084 const GrBackendFormat& backendFormat() const { return fBackendFormat; }
85
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050086 void setBackendTexture(const GrBackendTexture& backendTexture);
87
Brian Salomon3f4cd772019-01-11 16:03:19 -050088 sk_sp<SkPromiseImageTexture> fulfill() {
89 SkASSERT(fPromiseImageTexture);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050090 SkASSERT(fUnreleasedFulfills >= 0);
91 ++fUnreleasedFulfills;
92 ++fTotalFulfills;
Brian Salomon3f4cd772019-01-11 16:03:19 -050093 return fPromiseImageTexture;
Robert Phillips96601082018-05-29 16:13:26 -040094 }
95
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050096 void release() {
97 SkASSERT(fUnreleasedFulfills > 0);
98 --fUnreleasedFulfills;
99 ++fTotalReleases;
100 }
101
102 void done() {
103 ++fDoneCnt;
104 SkASSERT(fDoneCnt <= fNumImages);
105 }
106
107 void wasAddedToImage() { fNumImages++; }
108
Brian Salomon3f4cd772019-01-11 16:03:19 -0500109 const SkPromiseImageTexture* promiseImageTexture() const {
Robert Phillips923181b2020-02-14 12:36:37 -0500110 return fPromiseImageTexture.get();
Brian Salomon3f4cd772019-01-11 16:03:19 -0500111 }
Robert Phillips96601082018-05-29 16:13:26 -0400112
113 private:
Robert Phillips923181b2020-02-14 12:36:37 -0500114 GrContext* fContext;
115 GrBackendFormat fBackendFormat;
Brian Salomon3f4cd772019-01-11 16:03:19 -0500116 sk_sp<SkPromiseImageTexture> fPromiseImageTexture;
Robert Phillips923181b2020-02-14 12:36:37 -0500117 int fNumImages = 0;
118 int fTotalFulfills = 0;
119 int fTotalReleases = 0;
120 int fUnreleasedFulfills = 0;
121 int fDoneCnt = 0;
Robert Phillips96601082018-05-29 16:13:26 -0400122
123 typedef SkRefCnt INHERITED;
124 };
125
126 // This is the information extracted into this class from the parsing of the skp file.
127 // Once it has all been uploaded to the GPU and distributed to the promise images, it
128 // is all dropped via "reset".
129 class PromiseImageInfo {
130 public:
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400131 PromiseImageInfo(int index, uint32_t originalUniqueID, const SkImageInfo& ii)
132 : fIndex(index)
133 , fOriginalUniqueID(originalUniqueID)
134 , fImageInfo(ii) {
135 }
136 ~PromiseImageInfo() {}
137
138 int index() const { return fIndex; }
139 uint32_t originalUniqueID() const { return fOriginalUniqueID; }
140 bool isYUV() const { return SkToBool(fYUVData.get()); }
141
142 int overallWidth() const { return fImageInfo.width(); }
143 int overallHeight() const { return fImageInfo.height(); }
144 SkColorType overallColorType() const { return fImageInfo.colorType(); }
145 SkAlphaType overallAlphaType() const { return fImageInfo.alphaType(); }
146 sk_sp<SkColorSpace> refOverallColorSpace() const { return fImageInfo.refColorSpace(); }
147
148 SkYUVColorSpace yuvColorSpace() const {
149 SkASSERT(this->isYUV());
150 return fYUVColorSpace;
151 }
Jim Van Verth8f11e432018-10-18 14:36:59 -0400152 const SkYUVAIndex* yuvaIndices() const {
153 SkASSERT(this->isYUV());
154 return fYUVAIndices;
155 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400156 const SkPixmap& yuvPixmap(int index) const {
157 SkASSERT(this->isYUV());
Jim Van Verthe24b5872018-10-29 16:26:02 -0400158 SkASSERT(index >= 0 && index < SkYUVASizeInfo::kMaxCount);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400159 return fYUVPlanes[index];
160 }
161 const SkBitmap& normalBitmap() const {
162 SkASSERT(!this->isYUV());
163 return fBitmap;
164 }
165
166 void setCallbackContext(int index, sk_sp<PromiseImageCallbackContext> callbackContext) {
Jim Van Verthe24b5872018-10-29 16:26:02 -0400167 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400168 fCallbackContexts[index] = callbackContext;
169 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500170 PromiseImageCallbackContext* callbackContext(int index) const {
Jim Van Verthe24b5872018-10-29 16:26:02 -0400171 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400172 return fCallbackContexts[index].get();
173 }
174 sk_sp<PromiseImageCallbackContext> refCallbackContext(int index) const {
Jim Van Verthe24b5872018-10-29 16:26:02 -0400175 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400176 return fCallbackContexts[index];
177 }
178
Robert Phillips923181b2020-02-14 12:36:37 -0500179 const GrBackendFormat& backendFormat(int index) const {
180 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1));
181 return fCallbackContexts[index]->backendFormat();
182 }
Brian Salomon3f4cd772019-01-11 16:03:19 -0500183 const SkPromiseImageTexture* promiseTexture(int index) const {
Jim Van Verthe24b5872018-10-29 16:26:02 -0400184 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1));
Brian Salomon3f4cd772019-01-11 16:03:19 -0500185 return fCallbackContexts[index]->promiseImageTexture();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400186 }
187
188 void setNormalBitmap(const SkBitmap& bm) { fBitmap = bm; }
189
Jim Van Verth8f11e432018-10-18 14:36:59 -0400190 void setYUVData(sk_sp<SkCachedData> yuvData,
191 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
192 SkYUVColorSpace cs) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400193 fYUVData = yuvData;
Jim Van Verth8f11e432018-10-18 14:36:59 -0400194 memcpy(fYUVAIndices, yuvaIndices, sizeof(fYUVAIndices));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400195 fYUVColorSpace = cs;
196 }
197 void addYUVPlane(int index, const SkImageInfo& ii, const void* plane, size_t widthBytes) {
198 SkASSERT(this->isYUV());
Jim Van Verthe24b5872018-10-29 16:26:02 -0400199 SkASSERT(index >= 0 && index < SkYUVASizeInfo::kMaxCount);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400200 fYUVPlanes[index].reset(ii, plane, widthBytes);
201 }
202
203 private:
204 const int fIndex; // index in the 'fImageInfo' array
205 const uint32_t fOriginalUniqueID; // original ID for deduping
206
207 const SkImageInfo fImageInfo; // info for the overarching image
208
209 // CPU-side cache of a normal SkImage's contents
210 SkBitmap fBitmap;
211
212 // CPU-side cache of a YUV SkImage's contents
213 sk_sp<SkCachedData> fYUVData; // when !null, this is a YUV image
214 SkYUVColorSpace fYUVColorSpace = kJPEG_SkYUVColorSpace;
Jim Van Verth8f11e432018-10-18 14:36:59 -0400215 SkYUVAIndex fYUVAIndices[SkYUVAIndex::kIndexCount];
Jim Van Verthe24b5872018-10-29 16:26:02 -0400216 SkPixmap fYUVPlanes[SkYUVASizeInfo::kMaxCount];
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400217
Jim Van Verthe24b5872018-10-29 16:26:02 -0400218 // Up to SkYUVASizeInfo::kMaxCount for a YUVA image. Only one for a normal image.
219 sk_sp<PromiseImageCallbackContext> fCallbackContexts[SkYUVASizeInfo::kMaxCount];
Robert Phillips96601082018-05-29 16:13:26 -0400220 };
221
222 // This stack-based context allows each thread to re-inflate the image indices into
223 // promise images while still using the same GrBackendTexture.
224 struct PerRecorderContext {
225 SkDeferredDisplayListRecorder* fRecorder;
226 const DDLPromiseImageHelper* fHelper;
227 SkTArray<sk_sp<SkImage>>* fPromiseImages;
228 };
229
Robert Phillips923181b2020-02-14 12:36:37 -0500230 static void CreateBETexturesForPromiseImage(GrContext*, PromiseImageInfo*);
231
Brian Salomon3f4cd772019-01-11 16:03:19 -0500232 static sk_sp<SkPromiseImageTexture> PromiseImageFulfillProc(void* textureContext) {
Robert Phillips96601082018-05-29 16:13:26 -0400233 auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
Brian Salomon3f4cd772019-01-11 16:03:19 -0500234 return callbackContext->fulfill();
Robert Phillips96601082018-05-29 16:13:26 -0400235 }
236
Brian Salomon3f4cd772019-01-11 16:03:19 -0500237 static void PromiseImageReleaseProc(void* textureContext) {
Robert Phillips96601082018-05-29 16:13:26 -0400238 auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500239 callbackContext->release();
Robert Phillips96601082018-05-29 16:13:26 -0400240 }
241
242 static void PromiseImageDoneProc(void* textureContext) {
243 auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500244 callbackContext->done();
Robert Phillips96601082018-05-29 16:13:26 -0400245 callbackContext->unref();
246 }
247
Robert Phillips923181b2020-02-14 12:36:37 -0500248 static sk_sp<SkImage> CreatePromiseImages(const void* rawData, size_t length, void* ctxIn);
Robert Phillips96601082018-05-29 16:13:26 -0400249
250 bool isValidID(int id) const { return id >= 0 && id < fImageInfo.count(); }
251 const PromiseImageInfo& getInfo(int id) const { return fImageInfo[id]; }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500252 void uploadImage(GrContext*, PromiseImageInfo*);
Robert Phillips96601082018-05-29 16:13:26 -0400253
254 // returns -1 if not found
255 int findImage(SkImage* image) const;
256
257 // returns -1 on failure
258 int addImage(SkImage* image);
259
260 // returns -1 on failure
261 int findOrDefineImage(SkImage* image);
262
263 SkTArray<PromiseImageInfo> fImageInfo;
264};
265
266#endif