blob: 63ea0ec2204634994b1b40a656f90e76d682f252 [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"
Brian Salomonbe0e42c2020-08-27 11:00:04 -040014#include "include/core/SkYUVAPixmaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/gpu/GrBackendSurface.h"
16#include "include/private/SkTArray.h"
17#include "src/core/SkCachedData.h"
18#include "src/core/SkTLazy.h"
Robert Phillips96601082018-05-29 16:13:26 -040019
Adlai Hollerb2705682020-10-20 10:11:53 -040020class GrDirectContext;
Robert Phillips96601082018-05-29 16:13:26 -040021class SkImage;
Mike Reed13711eb2020-07-14 17:16:32 -040022class SkMipmap;
Robert Phillips96601082018-05-29 16:13:26 -040023class SkPicture;
Robert Phillips1a578572020-07-13 13:17:09 -040024class SkTaskGroup;
Robert Phillips96601082018-05-29 16:13:26 -040025
Robert Phillips11c67672020-04-23 15:10:03 -040026// This class acts as a proxy for a GrBackendTexture that backs an image.
27// Whenever a promise image is created for the image, the promise image receives a ref to
28// potentially several of these objects. Once all the promise images receive their done
29// callbacks this object is deleted - removing the GrBackendTexture from VRAM.
30// Note that while the DDLs are being created in the threads, the PromiseImageHelper holds
31// a ref on all the PromiseImageCallbackContexts. However, once all the threads are done
32// it drops all of its refs (via "reset").
33class PromiseImageCallbackContext : public SkRefCnt {
34public:
Robert Phillipsd5f3c982020-07-07 13:18:47 -040035 PromiseImageCallbackContext(GrDirectContext* direct, GrBackendFormat backendFormat)
36 : fContext(direct)
Robert Phillips11c67672020-04-23 15:10:03 -040037 , fBackendFormat(backendFormat) {}
38
Brian Salomond0072812020-07-21 17:03:56 -040039 ~PromiseImageCallbackContext() override;
Robert Phillips11c67672020-04-23 15:10:03 -040040
41 const GrBackendFormat& backendFormat() const { return fBackendFormat; }
42
43 void setBackendTexture(const GrBackendTexture& backendTexture);
44
Robert Phillipsd5f3c982020-07-07 13:18:47 -040045 void destroyBackendTexture();
Robert Phillips11c67672020-04-23 15:10:03 -040046
47 sk_sp<SkPromiseImageTexture> fulfill() {
Robert Phillips11c67672020-04-23 15:10:03 -040048 ++fTotalFulfills;
49 return fPromiseImageTexture;
50 }
51
52 void release() {
Robert Phillips11c67672020-04-23 15:10:03 -040053 ++fDoneCnt;
Brian Salomon96796122021-01-19 12:11:07 -050054 SkASSERT(fDoneCnt <= fNumImages);
Robert Phillips11c67672020-04-23 15:10:03 -040055 }
56
57 void wasAddedToImage() { fNumImages++; }
58
59 const SkPromiseImageTexture* promiseImageTexture() const {
60 return fPromiseImageTexture.get();
61 }
62
63 static sk_sp<SkPromiseImageTexture> PromiseImageFulfillProc(void* textureContext) {
64 auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
65 return callbackContext->fulfill();
66 }
67
68 static void PromiseImageReleaseProc(void* textureContext) {
69 auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext);
70 callbackContext->release();
Robert Phillips11c67672020-04-23 15:10:03 -040071 callbackContext->unref();
72 }
73
74private:
Robert Phillipsd5f3c982020-07-07 13:18:47 -040075 GrDirectContext* fContext;
Robert Phillips11c67672020-04-23 15:10:03 -040076 GrBackendFormat fBackendFormat;
77 sk_sp<SkPromiseImageTexture> fPromiseImageTexture;
78 int fNumImages = 0;
79 int fTotalFulfills = 0;
Robert Phillips11c67672020-04-23 15:10:03 -040080 int fDoneCnt = 0;
81
John Stiles7571f9e2020-09-02 22:42:33 -040082 using INHERITED = SkRefCnt;
Robert Phillips11c67672020-04-23 15:10:03 -040083};
84
Robert Phillips96601082018-05-29 16:13:26 -040085// This class consolidates tracking & extraction of the original image data from an skp,
86// the upload of said data to the GPU and the fulfillment of promise images.
87//
88// The way this works is:
89// the original skp is converted to SkData and all its image info is extracted into this
Robert Phillips0d8722c2021-03-29 13:29:40 -040090// class and only indices into this class are left in the SkData
91// the PromiseImageCallbackContexts are created for each image
92// the SkData is then reinflated into an SkPicture with promise images replacing all the indices
93// (all in recreateSKP)
Robert Phillips96601082018-05-29 16:13:26 -040094//
Robert Phillips0d8722c2021-03-29 13:29:40 -040095// Prior to replaying in threads, all the images are uploaded to the gpu
96// (in uploadAllToGPU)
Robert Phillips96601082018-05-29 16:13:26 -040097//
98// This class is then reset - dropping all of its refs on the PromiseImageCallbackContexts
99//
100// Each done callback unrefs its PromiseImageCallbackContext so, once all the promise images
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400101// are done, the PromiseImageCallbackContext is freed and its GrBackendTexture removed
Robert Phillips96601082018-05-29 16:13:26 -0400102// from VRAM
103//
104// Note: if DDLs are going to be replayed multiple times, the reset call can be delayed until
105// all the replaying is complete. This will pin the GrBackendTextures in VRAM.
106class DDLPromiseImageHelper {
107public:
Brian Salomon59c60b02020-09-01 15:01:15 -0400108 DDLPromiseImageHelper(const SkYUVAPixmapInfo::SupportedDataTypes& supportedYUVADataTypes)
109 : fSupportedYUVADataTypes(supportedYUVADataTypes) {}
Brian Salomon7d88f312019-02-28 10:03:03 -0500110 ~DDLPromiseImageHelper() = default;
Robert Phillips96601082018-05-29 16:13:26 -0400111
Robert Phillips0d8722c2021-03-29 13:29:40 -0400112 // Convert the input SkPicture into a new one which has promise images rather than live
113 // images.
114 sk_sp<SkPicture> recreateSKP(GrDirectContext*, SkPicture*);
Robert Phillips923181b2020-02-14 12:36:37 -0500115
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400116 void uploadAllToGPU(SkTaskGroup*, GrDirectContext*);
117 void deleteAllFromGPU(SkTaskGroup*, GrDirectContext*);
Robert Phillips96601082018-05-29 16:13:26 -0400118
Robert Phillips0d8722c2021-03-29 13:29:40 -0400119 // Remove this class' refs on the promise images and the PromiseImageCallbackContexts
120 void reset() {
121 fImageInfo.reset();
122 fPromiseImages.reset();
123 }
Robert Phillips96601082018-05-29 16:13:26 -0400124
125private:
Robert Phillips0d8722c2021-03-29 13:29:40 -0400126 void createCallbackContexts(GrDirectContext*);
127 // reinflate a deflated SKP, replacing all the indices with promise images.
128 sk_sp<SkPicture> reinflateSKP(sk_sp<GrContextThreadSafeProxy>, SkData* deflatedSKP);
129
Robert Phillips96601082018-05-29 16:13:26 -0400130 // This is the information extracted into this class from the parsing of the skp file.
131 // Once it has all been uploaded to the GPU and distributed to the promise images, it
132 // is all dropped via "reset".
133 class PromiseImageInfo {
134 public:
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400135 PromiseImageInfo(int index, uint32_t originalUniqueID, const SkImageInfo& ii);
136 PromiseImageInfo(PromiseImageInfo&& other);
137 ~PromiseImageInfo();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400138
139 int index() const { return fIndex; }
140 uint32_t originalUniqueID() const { return fOriginalUniqueID; }
Brian Salomon5660e8b2020-08-25 12:40:32 -0400141 bool isYUV() const { return fYUVAPixmaps.isValid(); }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400142
Adlai Holler55aaefe2021-03-03 16:12:56 -0700143 SkISize overallDimensions() const { return fImageInfo.dimensions(); }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400144 SkColorType overallColorType() const { return fImageInfo.colorType(); }
145 SkAlphaType overallAlphaType() const { return fImageInfo.alphaType(); }
146 sk_sp<SkColorSpace> refOverallColorSpace() const { return fImageInfo.refColorSpace(); }
147
Brian Salomonbd3792d2020-11-10 14:17:58 -0500148 const SkYUVAInfo& yuvaInfo() const { return fYUVAPixmaps.yuvaInfo(); }
149
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400150 const SkPixmap& yuvPixmap(int index) const {
151 SkASSERT(this->isYUV());
Brian Salomon5660e8b2020-08-25 12:40:32 -0400152 return fYUVAPixmaps.planes()[index];
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400153 }
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400154
155 const SkBitmap& baseLevel() const {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400156 SkASSERT(!this->isYUV());
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400157 return fBaseLevel;
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400158 }
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400159 // This returns an array of all the available mipLevels - suitable for passing into
160 // createBackendTexture.
John Stilesec9b4aa2020-08-07 13:05:14 -0400161 std::unique_ptr<SkPixmap[]> normalMipLevels() const;
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400162 int numMipLevels() const;
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400163
164 void setCallbackContext(int index, sk_sp<PromiseImageCallbackContext> callbackContext) {
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500165 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400166 fCallbackContexts[index] = callbackContext;
167 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500168 PromiseImageCallbackContext* callbackContext(int index) const {
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500169 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400170 return fCallbackContexts[index].get();
171 }
172 sk_sp<PromiseImageCallbackContext> refCallbackContext(int index) const {
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500173 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400174 return fCallbackContexts[index];
175 }
176
Brian Salomon7e67dca2020-07-21 09:27:25 -0400177 GrMipmapped mipMapped(int index) const {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400178 if (this->isYUV()) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400179 return GrMipmapped::kNo;
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400180 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400181 return fMipLevels ? GrMipmapped::kYes : GrMipmapped::kNo;
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400182 }
Robert Phillips923181b2020-02-14 12:36:37 -0500183 const GrBackendFormat& backendFormat(int index) const {
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500184 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
Robert Phillips923181b2020-02-14 12:36:37 -0500185 return fCallbackContexts[index]->backendFormat();
186 }
Brian Salomon3f4cd772019-01-11 16:03:19 -0500187 const SkPromiseImageTexture* promiseTexture(int index) const {
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500188 SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1));
Brian Salomon3f4cd772019-01-11 16:03:19 -0500189 return fCallbackContexts[index]->promiseImageTexture();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400190 }
191
Mike Reed13711eb2020-07-14 17:16:32 -0400192 void setMipLevels(const SkBitmap& baseLevel, std::unique_ptr<SkMipmap> mipLevels);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400193
Brian Salomonefb5f072020-07-28 21:06:43 -0400194 /** Takes ownership of the plane data. */
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400195 void setYUVPlanes(SkYUVAPixmaps yuvaPixmaps) { fYUVAPixmaps = std::move(yuvaPixmaps); }
Brian Salomon5660e8b2020-08-25 12:40:32 -0400196
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400197 private:
198 const int fIndex; // index in the 'fImageInfo' array
199 const uint32_t fOriginalUniqueID; // original ID for deduping
200
201 const SkImageInfo fImageInfo; // info for the overarching image
202
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400203 // CPU-side cache of a normal SkImage's mipmap levels
204 SkBitmap fBaseLevel;
Mike Reed13711eb2020-07-14 17:16:32 -0400205 std::unique_ptr<SkMipmap> fMipLevels;
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400206
207 // CPU-side cache of a YUV SkImage's contents
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400208 SkYUVAPixmaps fYUVAPixmaps;
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400209
Jim Van Verthe24b5872018-10-29 16:26:02 -0400210 // Up to SkYUVASizeInfo::kMaxCount for a YUVA image. Only one for a normal image.
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500211 sk_sp<PromiseImageCallbackContext> fCallbackContexts[SkYUVAInfo::kMaxPlanes];
Robert Phillips96601082018-05-29 16:13:26 -0400212 };
213
Adlai Holler55aaefe2021-03-03 16:12:56 -0700214 struct DeserialImageProcContext {
215 sk_sp<GrContextThreadSafeProxy> fThreadSafeProxy;
Robert Phillips0d8722c2021-03-29 13:29:40 -0400216 DDLPromiseImageHelper* fHelper;
Robert Phillips96601082018-05-29 16:13:26 -0400217 };
218
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400219 static void CreateBETexturesForPromiseImage(GrDirectContext*, PromiseImageInfo*);
Brian Salomonbd3792d2020-11-10 14:17:58 -0500220 static void DeleteBETexturesForPromiseImage(PromiseImageInfo*);
Robert Phillips923181b2020-02-14 12:36:37 -0500221
Robert Phillips923181b2020-02-14 12:36:37 -0500222 static sk_sp<SkImage> CreatePromiseImages(const void* rawData, size_t length, void* ctxIn);
Robert Phillips96601082018-05-29 16:13:26 -0400223
224 bool isValidID(int id) const { return id >= 0 && id < fImageInfo.count(); }
225 const PromiseImageInfo& getInfo(int id) const { return fImageInfo[id]; }
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400226 void uploadImage(GrDirectContext*, PromiseImageInfo*);
Robert Phillips96601082018-05-29 16:13:26 -0400227
228 // returns -1 if not found
229 int findImage(SkImage* image) const;
230
231 // returns -1 on failure
232 int addImage(SkImage* image);
233
234 // returns -1 on failure
235 int findOrDefineImage(SkImage* image);
236
Brian Salomon59c60b02020-09-01 15:01:15 -0400237 SkYUVAPixmapInfo::SupportedDataTypes fSupportedYUVADataTypes;
Robert Phillips0d8722c2021-03-29 13:29:40 -0400238 SkTArray<PromiseImageInfo> fImageInfo;
239
240 // TODO: review the use of 'fPromiseImages' - it doesn't seem useful/necessary
241 SkTArray<sk_sp<SkImage>> fPromiseImages; // All the promise images in the
242 // reconstituted picture
Robert Phillips96601082018-05-29 16:13:26 -0400243};
244
245#endif