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" |
| 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 Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 20 | |
| 21 | class GrContext; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 22 | class SkImage; |
| 23 | class SkPicture; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 24 | struct SkYUVAIndex; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 25 | |
| 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 Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 43 | // are done, the PromiseImageCallbackContext is freed and its GrBackendTexture removed |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 44 | // 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. |
| 48 | class DDLPromiseImageHelper { |
| 49 | public: |
Brian Salomon | 7d88f31 | 2019-02-28 10:03:03 -0500 | [diff] [blame] | 50 | DDLPromiseImageHelper() = default; |
| 51 | ~DDLPromiseImageHelper() = default; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 52 | |
| 53 | // Convert the SkPicture into SkData replacing all the SkImages with an index. |
| 54 | sk_sp<SkData> deflateSKP(const SkPicture* inputPicture); |
| 55 | |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 56 | void createCallbackContexts(GrContext*); |
| 57 | |
| 58 | void uploadAllToGPU(SkTaskGroup*, GrContext*); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 59 | |
| 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 | |
| 68 | private: |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 69 | // This class acts as a proxy for a GrBackendTexture that is part of an image. |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 70 | // Whenever a promise image is created for the image, the promise image receives a ref to |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 71 | // 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 Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 73 | // 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 Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 78 | PromiseImageCallbackContext(GrContext* context, GrBackendFormat backendFormat) |
| 79 | : fContext(context) |
| 80 | , fBackendFormat(backendFormat) {} |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 81 | |
| 82 | ~PromiseImageCallbackContext(); |
| 83 | |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 84 | const GrBackendFormat& backendFormat() const { return fBackendFormat; } |
| 85 | |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 86 | void setBackendTexture(const GrBackendTexture& backendTexture); |
| 87 | |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 88 | sk_sp<SkPromiseImageTexture> fulfill() { |
| 89 | SkASSERT(fPromiseImageTexture); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 90 | SkASSERT(fUnreleasedFulfills >= 0); |
| 91 | ++fUnreleasedFulfills; |
| 92 | ++fTotalFulfills; |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 93 | return fPromiseImageTexture; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 96 | 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 Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 109 | const SkPromiseImageTexture* promiseImageTexture() const { |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 110 | return fPromiseImageTexture.get(); |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 111 | } |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 112 | |
| 113 | private: |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 114 | GrContext* fContext; |
| 115 | GrBackendFormat fBackendFormat; |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 116 | sk_sp<SkPromiseImageTexture> fPromiseImageTexture; |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 117 | int fNumImages = 0; |
| 118 | int fTotalFulfills = 0; |
| 119 | int fTotalReleases = 0; |
| 120 | int fUnreleasedFulfills = 0; |
| 121 | int fDoneCnt = 0; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 122 | |
| 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 Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 131 | 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 Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 152 | const SkYUVAIndex* yuvaIndices() const { |
| 153 | SkASSERT(this->isYUV()); |
| 154 | return fYUVAIndices; |
| 155 | } |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 156 | const SkPixmap& yuvPixmap(int index) const { |
| 157 | SkASSERT(this->isYUV()); |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 158 | SkASSERT(index >= 0 && index < SkYUVASizeInfo::kMaxCount); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 159 | 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 Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 167 | SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1)); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 168 | fCallbackContexts[index] = callbackContext; |
| 169 | } |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 170 | PromiseImageCallbackContext* callbackContext(int index) const { |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 171 | SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1)); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 172 | return fCallbackContexts[index].get(); |
| 173 | } |
| 174 | sk_sp<PromiseImageCallbackContext> refCallbackContext(int index) const { |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 175 | SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1)); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 176 | return fCallbackContexts[index]; |
| 177 | } |
| 178 | |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 179 | const GrBackendFormat& backendFormat(int index) const { |
| 180 | SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1)); |
| 181 | return fCallbackContexts[index]->backendFormat(); |
| 182 | } |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 183 | const SkPromiseImageTexture* promiseTexture(int index) const { |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 184 | SkASSERT(index >= 0 && index < (this->isYUV() ? SkYUVASizeInfo::kMaxCount : 1)); |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 185 | return fCallbackContexts[index]->promiseImageTexture(); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void setNormalBitmap(const SkBitmap& bm) { fBitmap = bm; } |
| 189 | |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 190 | void setYUVData(sk_sp<SkCachedData> yuvData, |
| 191 | SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount], |
| 192 | SkYUVColorSpace cs) { |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 193 | fYUVData = yuvData; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 194 | memcpy(fYUVAIndices, yuvaIndices, sizeof(fYUVAIndices)); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 195 | fYUVColorSpace = cs; |
| 196 | } |
| 197 | void addYUVPlane(int index, const SkImageInfo& ii, const void* plane, size_t widthBytes) { |
| 198 | SkASSERT(this->isYUV()); |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 199 | SkASSERT(index >= 0 && index < SkYUVASizeInfo::kMaxCount); |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 200 | 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 Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 215 | SkYUVAIndex fYUVAIndices[SkYUVAIndex::kIndexCount]; |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 216 | SkPixmap fYUVPlanes[SkYUVASizeInfo::kMaxCount]; |
Robert Phillips | e8e2bb1 | 2018-09-27 14:26:47 -0400 | [diff] [blame] | 217 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 218 | // Up to SkYUVASizeInfo::kMaxCount for a YUVA image. Only one for a normal image. |
| 219 | sk_sp<PromiseImageCallbackContext> fCallbackContexts[SkYUVASizeInfo::kMaxCount]; |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 220 | }; |
| 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 Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 230 | static void CreateBETexturesForPromiseImage(GrContext*, PromiseImageInfo*); |
| 231 | |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 232 | static sk_sp<SkPromiseImageTexture> PromiseImageFulfillProc(void* textureContext) { |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 233 | auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext); |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 234 | return callbackContext->fulfill(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 235 | } |
| 236 | |
Brian Salomon | 3f4cd77 | 2019-01-11 16:03:19 -0500 | [diff] [blame] | 237 | static void PromiseImageReleaseProc(void* textureContext) { |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 238 | auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 239 | callbackContext->release(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static void PromiseImageDoneProc(void* textureContext) { |
| 243 | auto callbackContext = static_cast<PromiseImageCallbackContext*>(textureContext); |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 244 | callbackContext->done(); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 245 | callbackContext->unref(); |
| 246 | } |
| 247 | |
Robert Phillips | 923181b | 2020-02-14 12:36:37 -0500 | [diff] [blame] | 248 | 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] | 249 | |
| 250 | bool isValidID(int id) const { return id >= 0 && id < fImageInfo.count(); } |
| 251 | const PromiseImageInfo& getInfo(int id) const { return fImageInfo[id]; } |
Brian Salomon | cdd8a0a | 2019-01-10 12:09:52 -0500 | [diff] [blame] | 252 | void uploadImage(GrContext*, PromiseImageInfo*); |
Robert Phillips | 9660108 | 2018-05-29 16:13:26 -0400 | [diff] [blame] | 253 | |
| 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 |