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 |
Robert Phillips | 0d8722c | 2021-03-29 13:29:40 -0400 | [diff] [blame] | 90 | // 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 Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 94 | // |
Robert Phillips | 0d8722c | 2021-03-29 13:29:40 -0400 | [diff] [blame] | 95 | // Prior to replaying in threads, all the images are uploaded to the gpu |
| 96 | // (in uploadAllToGPU) |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 97 | // |
| 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 Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 101 | // are done, the PromiseImageCallbackContext is freed and its GrBackendTexture removed |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 102 | // 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. |
| 106 | class DDLPromiseImageHelper { |
| 107 | public: |
Brian Salomon | 59c60b0 | 2020-09-01 15:01:15 -0400 | [diff] [blame] | 108 | DDLPromiseImageHelper(const SkYUVAPixmapInfo::SupportedDataTypes& supportedYUVADataTypes) |
| 109 | : fSupportedYUVADataTypes(supportedYUVADataTypes) {} |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 110 | ~DDLPromiseImageHelper() = default; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 111 | |
Robert Phillips | 0d8722c | 2021-03-29 13:29:40 -0400 | [diff] [blame] | 112 | // 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 Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 115 | |
Robert Phillips | d5f3c98 | 2020-07-07 13:18:47 -0400 | [diff] [blame] | 116 | void uploadAllToGPU(SkTaskGroup*, GrDirectContext*); |
| 117 | void deleteAllFromGPU(SkTaskGroup*, GrDirectContext*); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 118 | |
Robert Phillips | 0d8722c | 2021-03-29 13:29:40 -0400 | [diff] [blame] | 119 | // Remove this class' refs on the promise images and the PromiseImageCallbackContexts |
| 120 | void reset() { |
| 121 | fImageInfo.reset(); |
| 122 | fPromiseImages.reset(); |
| 123 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 124 | |
| 125 | private: |
Robert Phillips | 0d8722c | 2021-03-29 13:29:40 -0400 | [diff] [blame] | 126 | 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 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; |
Robert Phillips | 0d8722c | 2021-03-29 13:29:40 -0400 | [diff] [blame] | 216 | DDLPromiseImageHelper* fHelper; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 217 | }; |
| 218 | |
Robert Phillips | d5f3c98 | 2020-07-07 13:18:47 -0400 | [diff] [blame] | 219 | static void CreateBETexturesForPromiseImage(GrDirectContext*, PromiseImageInfo*); |
Brian Salomon | bd3792d | 2020-11-10 14:17:58 -0500 | [diff] [blame] | 220 | static void DeleteBETexturesForPromiseImage(PromiseImageInfo*); |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 221 | |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 222 | 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] | 223 | |
| 224 | bool isValidID(int id) const { return id >= 0 && id < fImageInfo.count(); } |
| 225 | const PromiseImageInfo& getInfo(int id) const { return fImageInfo[id]; } |
Robert Phillips | d5f3c98 | 2020-07-07 13:18:47 -0400 | [diff] [blame] | 226 | void uploadImage(GrDirectContext*, PromiseImageInfo*); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 227 | |
| 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 Salomon | 59c60b0 | 2020-09-01 15:01:15 -0400 | [diff] [blame] | 237 | SkYUVAPixmapInfo::SupportedDataTypes fSupportedYUVADataTypes; |
Robert Phillips | 0d8722c | 2021-03-29 13:29:40 -0400 | [diff] [blame] | 238 | 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 Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | #endif |