Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkBitmap.h" |
| 12 | #include "include/core/SkDeferredDisplayListRecorder.h" |
| 13 | #include "include/core/SkPromiseImageTexture.h" |
Brian Salomon | be0e42c | 2020-08-27 11:00:04 -0400 | [diff] [blame] | 14 | #include "include/core/SkYUVAPixmaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/gpu/GrBackendSurface.h" |
| 16 | #include "include/private/SkTArray.h" |
| 17 | #include "src/core/SkCachedData.h" |
| 18 | #include "src/core/SkTLazy.h" |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 19 | |
Adlai Holler | b270568 | 2020-10-20 10:11:53 -0400 | [diff] [blame] | 20 | class GrDirectContext; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 21 | class SkImage; |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 22 | class SkMipmap; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 23 | class SkPicture; |
Robert Phillips | 1a57857 | 2020-07-13 13:17:09 -0400 | [diff] [blame] | 24 | class SkTaskGroup; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 25 | |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 26 | // 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"). |
| 33 | class PromiseImageCallbackContext : public SkRefCnt { |
| 34 | public: |
Robert Phillips | d5f3c98 | 2020-07-07 13:18:47 -0400 | [diff] [blame] | 35 | PromiseImageCallbackContext(GrDirectContext* direct, GrBackendFormat backendFormat) |
| 36 | : fContext(direct) |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 37 | , fBackendFormat(backendFormat) {} |
| 38 | |
Brian Salomon | d007281 | 2020-07-21 17:03:56 -0400 | [diff] [blame] | 39 | ~PromiseImageCallbackContext() override; |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 40 | |
| 41 | const GrBackendFormat& backendFormat() const { return fBackendFormat; } |
| 42 | |
| 43 | void setBackendTexture(const GrBackendTexture& backendTexture); |
| 44 | |
Robert Phillips | d5f3c98 | 2020-07-07 13:18:47 -0400 | [diff] [blame] | 45 | void destroyBackendTexture(); |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 46 | |
| 47 | sk_sp<SkPromiseImageTexture> fulfill() { |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 48 | ++fTotalFulfills; |
| 49 | return fPromiseImageTexture; |
| 50 | } |
| 51 | |
| 52 | void release() { |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 53 | ++fDoneCnt; |
Brian Salomon | 9679612 | 2021-01-19 12:11:07 -0500 | [diff] [blame] | 54 | SkASSERT(fDoneCnt <= fNumImages); |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 55 | } |
| 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 Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 71 | callbackContext->unref(); |
| 72 | } |
| 73 | |
| 74 | private: |
Robert Phillips | d5f3c98 | 2020-07-07 13:18:47 -0400 | [diff] [blame] | 75 | GrDirectContext* fContext; |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 76 | GrBackendFormat fBackendFormat; |
| 77 | sk_sp<SkPromiseImageTexture> fPromiseImageTexture; |
| 78 | int fNumImages = 0; |
| 79 | int fTotalFulfills = 0; |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 80 | int fDoneCnt = 0; |
| 81 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 82 | using INHERITED = SkRefCnt; |
Robert Phillips | 11c6767 | 2020-04-23 15:10:03 -0400 | [diff] [blame] | 83 | }; |
| 84 | |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 85 | // 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 |
| 90 | // class and only indices into this class are left in the SkData (via deflateSKP) |
| 91 | // |
| 92 | // Prior to replaying in threads, all the images stored in this class are uploaded to the |
| 93 | // gpu and PromiseImageCallbackContexts are created for them (via uploadAllToGPU) |
| 94 | // |
| 95 | // Each thread reinflates the SkData into an SkPicture replacing all the indices w/ |
| 96 | // promise images (all using the same GrBackendTexture and getting a ref to the |
| 97 | // appropriate PromiseImageCallbackContext) (via reinflateSKP). |
| 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 |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 102 | // are done, the PromiseImageCallbackContext is freed and its GrBackendTexture removed |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 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: |
Brian Salomon | 59c60b0 | 2020-09-01 15:01:15 -0400 | [diff] [blame] | 109 | DDLPromiseImageHelper(const SkYUVAPixmapInfo::SupportedDataTypes& supportedYUVADataTypes) |
| 110 | : fSupportedYUVADataTypes(supportedYUVADataTypes) {} |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 111 | ~DDLPromiseImageHelper() = default; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 112 | |
| 113 | // Convert the SkPicture into SkData replacing all the SkImages with an index. |
| 114 | sk_sp<SkData> deflateSKP(const SkPicture* inputPicture); |
| 115 | |
Robert Phillips | d5f3c98 | 2020-07-07 13:18:47 -0400 | [diff] [blame] | 116 | void createCallbackContexts(GrDirectContext*); |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 117 | |
Robert Phillips | d5f3c98 | 2020-07-07 13:18:47 -0400 | [diff] [blame] | 118 | void uploadAllToGPU(SkTaskGroup*, GrDirectContext*); |
| 119 | void deleteAllFromGPU(SkTaskGroup*, GrDirectContext*); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 120 | |
| 121 | // reinflate a deflated SKP, replacing all the indices with promise images. |
Adlai Holler | 55aaefe | 2021-03-03 16:12:56 -0700 | [diff] [blame^] | 122 | sk_sp<SkPicture> reinflateSKP(sk_sp<GrContextThreadSafeProxy>, |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 123 | SkData* compressedPicture, |
| 124 | SkTArray<sk_sp<SkImage>>* promiseImages) const; |
| 125 | |
| 126 | // Remove this class' refs on the PromiseImageCallbackContexts |
| 127 | void reset() { fImageInfo.reset(); } |
| 128 | |
| 129 | private: |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 130 | // 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 Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 135 | PromiseImageInfo(int index, uint32_t originalUniqueID, const SkImageInfo& ii); |
| 136 | PromiseImageInfo(PromiseImageInfo&& other); |
| 137 | ~PromiseImageInfo(); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 138 | |
| 139 | int index() const { return fIndex; } |
| 140 | uint32_t originalUniqueID() const { return fOriginalUniqueID; } |
Brian Salomon | 5660e8b | 2020-08-25 12:40:32 -0400 | [diff] [blame] | 141 | bool isYUV() const { return fYUVAPixmaps.isValid(); } |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 142 | |
Adlai Holler | 55aaefe | 2021-03-03 16:12:56 -0700 | [diff] [blame^] | 143 | SkISize overallDimensions() const { return fImageInfo.dimensions(); } |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 144 | SkColorType overallColorType() const { return fImageInfo.colorType(); } |
| 145 | SkAlphaType overallAlphaType() const { return fImageInfo.alphaType(); } |
| 146 | sk_sp<SkColorSpace> refOverallColorSpace() const { return fImageInfo.refColorSpace(); } |
| 147 | |
Brian Salomon | bd3792d | 2020-11-10 14:17:58 -0500 | [diff] [blame] | 148 | const SkYUVAInfo& yuvaInfo() const { return fYUVAPixmaps.yuvaInfo(); } |
| 149 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 150 | const SkPixmap& yuvPixmap(int index) const { |
| 151 | SkASSERT(this->isYUV()); |
Brian Salomon | 5660e8b | 2020-08-25 12:40:32 -0400 | [diff] [blame] | 152 | return fYUVAPixmaps.planes()[index]; |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 153 | } |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 154 | |
| 155 | const SkBitmap& baseLevel() const { |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 156 | SkASSERT(!this->isYUV()); |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 157 | return fBaseLevel; |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 158 | } |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 159 | // This returns an array of all the available mipLevels - suitable for passing into |
| 160 | // createBackendTexture. |
John Stiles | ec9b4aa | 2020-08-07 13:05:14 -0400 | [diff] [blame] | 161 | std::unique_ptr<SkPixmap[]> normalMipLevels() const; |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 162 | int numMipLevels() const; |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 163 | |
| 164 | void setCallbackContext(int index, sk_sp<PromiseImageCallbackContext> callbackContext) { |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 165 | SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1)); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 166 | fCallbackContexts[index] = callbackContext; |
| 167 | } |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 168 | PromiseImageCallbackContext* callbackContext(int index) const { |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 169 | SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1)); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 170 | return fCallbackContexts[index].get(); |
| 171 | } |
| 172 | sk_sp<PromiseImageCallbackContext> refCallbackContext(int index) const { |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 173 | SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1)); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 174 | return fCallbackContexts[index]; |
| 175 | } |
| 176 | |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 177 | GrMipmapped mipMapped(int index) const { |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 178 | if (this->isYUV()) { |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 179 | return GrMipmapped::kNo; |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 180 | } |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 181 | return fMipLevels ? GrMipmapped::kYes : GrMipmapped::kNo; |
Robert Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 182 | } |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 183 | const GrBackendFormat& backendFormat(int index) const { |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 184 | SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1)); |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 185 | return fCallbackContexts[index]->backendFormat(); |
| 186 | } |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 187 | const SkPromiseImageTexture* promiseTexture(int index) const { |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 188 | SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVAInfo::kMaxPlanes : 1)); |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 189 | return fCallbackContexts[index]->promiseImageTexture(); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 190 | } |
| 191 | |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 192 | void setMipLevels(const SkBitmap& baseLevel, std::unique_ptr<SkMipmap> mipLevels); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 193 | |
Brian Salomon | efb5f07 | 2020-07-28 21:06:43 -0400 | [diff] [blame] | 194 | /** Takes ownership of the plane data. */ |
Brian Salomon | be0e42c | 2020-08-27 11:00:04 -0400 | [diff] [blame] | 195 | void setYUVPlanes(SkYUVAPixmaps yuvaPixmaps) { fYUVAPixmaps = std::move(yuvaPixmaps); } |
Brian Salomon | 5660e8b | 2020-08-25 12:40:32 -0400 | [diff] [blame] | 196 | |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 197 | 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 Phillips | f95e2f4 | 2020-04-17 16:20:55 -0400 | [diff] [blame] | 203 | // CPU-side cache of a normal SkImage's mipmap levels |
| 204 | SkBitmap fBaseLevel; |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 205 | std::unique_ptr<SkMipmap> fMipLevels; |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 206 | |
| 207 | // CPU-side cache of a YUV SkImage's contents |
Brian Salomon | be0e42c | 2020-08-27 11:00:04 -0400 | [diff] [blame] | 208 | SkYUVAPixmaps fYUVAPixmaps; |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 209 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 210 | // Up to SkYUVASizeInfo::kMaxCount for a YUVA image. Only one for a normal image. |
Brian Salomon | 0c0b5a6 | 2021-01-11 14:40:44 -0500 | [diff] [blame] | 211 | sk_sp<PromiseImageCallbackContext> fCallbackContexts[SkYUVAInfo::kMaxPlanes]; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 212 | }; |
| 213 | |
Adlai Holler | 55aaefe | 2021-03-03 16:12:56 -0700 | [diff] [blame^] | 214 | struct DeserialImageProcContext { |
| 215 | sk_sp<GrContextThreadSafeProxy> fThreadSafeProxy; |
| 216 | const DDLPromiseImageHelper* fHelper; |
| 217 | SkTArray<sk_sp<SkImage>>* fPromiseImages; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 218 | }; |
| 219 | |
Robert Phillips | d5f3c98 | 2020-07-07 13:18:47 -0400 | [diff] [blame] | 220 | static void CreateBETexturesForPromiseImage(GrDirectContext*, PromiseImageInfo*); |
Brian Salomon | bd3792d | 2020-11-10 14:17:58 -0500 | [diff] [blame] | 221 | static void DeleteBETexturesForPromiseImage(PromiseImageInfo*); |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 222 | |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 223 | static sk_sp<SkImage> CreatePromiseImages(const void* rawData, size_t length, void* ctxIn); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 224 | |
| 225 | bool isValidID(int id) const { return id >= 0 && id < fImageInfo.count(); } |
| 226 | const PromiseImageInfo& getInfo(int id) const { return fImageInfo[id]; } |
Robert Phillips | d5f3c98 | 2020-07-07 13:18:47 -0400 | [diff] [blame] | 227 | void uploadImage(GrDirectContext*, PromiseImageInfo*); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 228 | |
| 229 | // returns -1 if not found |
| 230 | int findImage(SkImage* image) const; |
| 231 | |
| 232 | // returns -1 on failure |
| 233 | int addImage(SkImage* image); |
| 234 | |
| 235 | // returns -1 on failure |
| 236 | int findOrDefineImage(SkImage* image); |
| 237 | |
Brian Salomon | 59c60b0 | 2020-09-01 15:01:15 -0400 | [diff] [blame] | 238 | SkYUVAPixmapInfo::SupportedDataTypes fSupportedYUVADataTypes; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 239 | SkTArray<PromiseImageInfo> fImageInfo; |
| 240 | }; |
| 241 | |
| 242 | #endif |