blob: c4ff9186804b6ed6320f47ce645187de41faa0a3 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tools/DDLPromiseImageHelper.h"
Robert Phillips96601082018-05-29 16:13:26 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkDeferredDisplayListRecorder.h"
Robert Phillips66944402019-09-30 13:21:25 -040011#include "include/core/SkPicture.h"
12#include "include/core/SkSerialProcs.h"
Robert Phillipsd5f3c982020-07-07 13:18:47 -040013#include "include/gpu/GrDirectContext.h"
Brian Salomonbd3792d2020-11-10 14:17:58 -050014#include "include/gpu/GrYUVABackendTextures.h"
Brian Salomonefb5f072020-07-28 21:06:43 -040015#include "src/codec/SkCodecImageGenerator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkCachedData.h"
Mike Reed13711eb2020-07-14 17:16:32 -040017#include "src/core/SkMipmap.h"
Robert Phillips923181b2020-02-14 12:36:37 -050018#include "src/core/SkTaskGroup.h"
Adlai Hollera0693042020-10-14 11:23:11 -040019#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/image/SkImage_Base.h"
21#include "src/image/SkImage_GpuYUVA.h"
Robert Phillips96601082018-05-29 16:13:26 -040022
Robert Phillipsf95e2f42020-04-17 16:20:55 -040023DDLPromiseImageHelper::PromiseImageInfo::PromiseImageInfo(int index,
24 uint32_t originalUniqueID,
25 const SkImageInfo& ii)
26 : fIndex(index)
27 , fOriginalUniqueID(originalUniqueID)
28 , fImageInfo(ii) {
29}
30
31DDLPromiseImageHelper::PromiseImageInfo::PromiseImageInfo(PromiseImageInfo&& other)
32 : fIndex(other.fIndex)
33 , fOriginalUniqueID(other.fOriginalUniqueID)
34 , fImageInfo(other.fImageInfo)
35 , fBaseLevel(other.fBaseLevel)
36 , fMipLevels(std::move(other.fMipLevels))
Brian Salomon5660e8b2020-08-25 12:40:32 -040037 , fYUVAPixmaps(std::move(other.fYUVAPixmaps)) {
Brian Salomon0c0b5a62021-01-11 14:40:44 -050038 for (int i = 0; i < SkYUVAInfo::kMaxPlanes; ++i) {
Robert Phillipsf95e2f42020-04-17 16:20:55 -040039 fCallbackContexts[i] = std::move(other.fCallbackContexts[i]);
40 }
41}
42
43DDLPromiseImageHelper::PromiseImageInfo::~PromiseImageInfo() {}
44
John Stilesec9b4aa2020-08-07 13:05:14 -040045std::unique_ptr<SkPixmap[]> DDLPromiseImageHelper::PromiseImageInfo::normalMipLevels() const {
Robert Phillipsf95e2f42020-04-17 16:20:55 -040046 SkASSERT(!this->isYUV());
47 std::unique_ptr<SkPixmap[]> pixmaps(new SkPixmap[this->numMipLevels()]);
48 pixmaps[0] = fBaseLevel.pixmap();
49 if (fMipLevels) {
50 for (int i = 0; i < fMipLevels->countLevels(); ++i) {
Mike Reed13711eb2020-07-14 17:16:32 -040051 SkMipmap::Level mipLevel;
Robert Phillipsf95e2f42020-04-17 16:20:55 -040052 fMipLevels->getLevel(i, &mipLevel);
53 pixmaps[i+1] = mipLevel.fPixmap;
54 }
55 }
56 return pixmaps;
57}
58
59int DDLPromiseImageHelper::PromiseImageInfo::numMipLevels() const {
60 SkASSERT(!this->isYUV());
61 return fMipLevels ? fMipLevels->countLevels()+1 : 1;
62}
63
64void DDLPromiseImageHelper::PromiseImageInfo::setMipLevels(const SkBitmap& baseLevel,
Mike Reed13711eb2020-07-14 17:16:32 -040065 std::unique_ptr<SkMipmap> mipLevels) {
Robert Phillipsf95e2f42020-04-17 16:20:55 -040066 fBaseLevel = baseLevel;
67 fMipLevels = std::move(mipLevels);
68}
69
70///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips11c67672020-04-23 15:10:03 -040071PromiseImageCallbackContext::~PromiseImageCallbackContext() {
Brian Salomon96796122021-01-19 12:11:07 -050072 SkASSERT(fDoneCnt == fNumImages);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050073 SkASSERT(!fTotalFulfills || fDoneCnt);
Robert Phillips96601082018-05-29 16:13:26 -040074
Brian Salomon3f4cd772019-01-11 16:03:19 -050075 if (fPromiseImageTexture) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -040076 fContext->deleteBackendTexture(fPromiseImageTexture->backendTexture());
Robert Phillips96601082018-05-29 16:13:26 -040077 }
78}
79
Robert Phillips11c67672020-04-23 15:10:03 -040080void PromiseImageCallbackContext::setBackendTexture(const GrBackendTexture& backendTexture) {
Brian Salomon7d88f312019-02-28 10:03:03 -050081 SkASSERT(!fPromiseImageTexture);
Robert Phillips923181b2020-02-14 12:36:37 -050082 SkASSERT(fBackendFormat == backendTexture.getBackendFormat());
Brian Salomon3f4cd772019-01-11 16:03:19 -050083 fPromiseImageTexture = SkPromiseImageTexture::Make(backendTexture);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050084}
85
Robert Phillipsd5f3c982020-07-07 13:18:47 -040086void PromiseImageCallbackContext::destroyBackendTexture() {
87 SkASSERT(!fPromiseImageTexture || fPromiseImageTexture->unique());
88
89 if (fPromiseImageTexture) {
90 fContext->deleteBackendTexture(fPromiseImageTexture->backendTexture());
91 }
92 fPromiseImageTexture = nullptr;
93}
94
Robert Phillips96601082018-05-29 16:13:26 -040095///////////////////////////////////////////////////////////////////////////////////////////////////
96
97sk_sp<SkData> DDLPromiseImageHelper::deflateSKP(const SkPicture* inputPicture) {
98 SkSerialProcs procs;
99
100 procs.fImageCtx = this;
101 procs.fImageProc = [](SkImage* image, void* ctx) -> sk_sp<SkData> {
102 auto helper = static_cast<DDLPromiseImageHelper*>(ctx);
103
104 int id = helper->findOrDefineImage(image);
Robert Phillips96601082018-05-29 16:13:26 -0400105
Robert Phillips6bad7052019-12-16 15:09:57 -0500106 // Even if 'id' is invalid (i.e., -1) write it to the SKP
107 return SkData::MakeWithCopy(&id, sizeof(id));
Robert Phillips96601082018-05-29 16:13:26 -0400108 };
109
110 return inputPicture->serialize(&procs);
111}
112
Brian Salomonbd3792d2020-11-10 14:17:58 -0500113static GrBackendTexture create_yuva_texture(GrDirectContext* direct,
114 const SkPixmap& pm,
115 int texIndex) {
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500116 SkASSERT(texIndex >= 0 && texIndex <= 3);
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400117
Greg Danielc1ad77c2020-05-06 11:40:03 -0400118 bool finishedBECreate = false;
119 auto markFinished = [](void* context) {
120 *(bool*)context = true;
121 };
Brian Salomonb5f880a2020-12-07 11:30:16 -0500122 auto beTex = direct->createBackendTexture(pm,
123 kTopLeft_GrSurfaceOrigin,
124 GrRenderable::kNo,
125 GrProtected::kNo,
126 markFinished,
127 &finishedBECreate);
Greg Danielc1ad77c2020-05-06 11:40:03 -0400128 if (beTex.isValid()) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400129 direct->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400130 while (!finishedBECreate) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400131 direct->checkAsyncWorkCompletion();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400132 }
133 }
134 return beTex;
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500135}
136
Robert Phillips923181b2020-02-14 12:36:37 -0500137/*
138 * Create backend textures and upload data to them for all the textures required to satisfy
139 * a single promise image.
140 * For YUV textures this will result in up to 4 actual textures.
141 */
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400142void DDLPromiseImageHelper::CreateBETexturesForPromiseImage(GrDirectContext* direct,
Robert Phillips923181b2020-02-14 12:36:37 -0500143 PromiseImageInfo* info) {
Robert Phillips923181b2020-02-14 12:36:37 -0500144 if (info->isYUV()) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500145 int numPixmaps = info->yuvaInfo().numPlanes();
Robert Phillips923181b2020-02-14 12:36:37 -0500146 for (int j = 0; j < numPixmaps; ++j) {
147 const SkPixmap& yuvPixmap = info->yuvPixmap(j);
148
149 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
150 SkASSERT(callbackContext);
151
Robert Phillips4508eb92020-04-15 15:54:34 -0400152 // DDL TODO: what should we do with mipmapped YUV images
Brian Salomonbd3792d2020-11-10 14:17:58 -0500153 callbackContext->setBackendTexture(create_yuva_texture(direct, yuvPixmap, j));
Robert Phillips923181b2020-02-14 12:36:37 -0500154 SkASSERT(callbackContext->promiseImageTexture());
155 }
156 } else {
157 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
158 if (!callbackContext) {
159 // This texture would've been too large to fit on the GPU
160 return;
161 }
162
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400163 std::unique_ptr<SkPixmap[]> mipLevels = info->normalMipLevels();
Robert Phillips923181b2020-02-14 12:36:37 -0500164
Greg Danielc1ad77c2020-05-06 11:40:03 -0400165 bool finishedBECreate = false;
166 auto markFinished = [](void* context) {
167 *(bool*)context = true;
168 };
Brian Salomonb5f880a2020-12-07 11:30:16 -0500169 auto backendTex = direct->createBackendTexture(mipLevels.get(),
170 info->numMipLevels(),
171 kTopLeft_GrSurfaceOrigin,
172 GrRenderable::kNo,
173 GrProtected::kNo,
174 markFinished,
175 &finishedBECreate);
Robert Phillips923181b2020-02-14 12:36:37 -0500176 SkASSERT(backendTex.isValid());
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400177 direct->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400178 while (!finishedBECreate) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400179 direct->checkAsyncWorkCompletion();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400180 }
Robert Phillips923181b2020-02-14 12:36:37 -0500181
182 callbackContext->setBackendTexture(backendTex);
183 }
184}
185
Brian Salomonbd3792d2020-11-10 14:17:58 -0500186void DDLPromiseImageHelper::DeleteBETexturesForPromiseImage(PromiseImageInfo* info) {
Robert Phillips19f466d2020-02-26 10:27:07 -0500187 if (info->isYUV()) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500188 int numPixmaps = info->yuvaInfo().numPlanes();
Robert Phillips19f466d2020-02-26 10:27:07 -0500189 for (int j = 0; j < numPixmaps; ++j) {
190 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
191 SkASSERT(callbackContext);
192
193 callbackContext->destroyBackendTexture();
194 SkASSERT(!callbackContext->promiseImageTexture());
195 }
196 } else {
197 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
198 if (!callbackContext) {
199 // This texture would've been too large to fit on the GPU
200 return;
201 }
202
203 callbackContext->destroyBackendTexture();
204 SkASSERT(!callbackContext->promiseImageTexture());
205 }
206}
207
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400208void DDLPromiseImageHelper::createCallbackContexts(GrDirectContext* direct) {
209 const GrCaps* caps = direct->priv().caps();
Robert Phillips923181b2020-02-14 12:36:37 -0500210 const int maxDimension = caps->maxTextureSize();
211
212 for (int i = 0; i < fImageInfo.count(); ++i) {
213 PromiseImageInfo& info = fImageInfo[i];
214
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400215 if (info.isYUV()) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500216 int numPixmaps = info.yuvaInfo().numPlanes();
Robert Phillips923181b2020-02-14 12:36:37 -0500217
Jim Van Verth8f11e432018-10-18 14:36:59 -0400218 for (int j = 0; j < numPixmaps; ++j) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400219 const SkPixmap& yuvPixmap = info.yuvPixmap(j);
Robert Phillips96601082018-05-29 16:13:26 -0400220
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400221 GrBackendFormat backendFormat = direct->defaultBackendFormat(yuvPixmap.colorType(),
222 GrRenderable::kNo);
Robert Phillips923181b2020-02-14 12:36:37 -0500223
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400224 sk_sp<PromiseImageCallbackContext> callbackContext(
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400225 new PromiseImageCallbackContext(direct, backendFormat));
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500226
Robert Phillips923181b2020-02-14 12:36:37 -0500227 info.setCallbackContext(j, std::move(callbackContext));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400228 }
229 } else {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400230 const SkBitmap& baseLevel = info.baseLevel();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400231
Robert Phillips923181b2020-02-14 12:36:37 -0500232 // TODO: explicitly mark the PromiseImageInfo as too big and check in uploadAllToGPU
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400233 if (maxDimension < std::max(baseLevel.width(), baseLevel.height())) {
Robert Phillips923181b2020-02-14 12:36:37 -0500234 // This won't fit on the GPU. Fallback to a raster-backed image per tile.
235 continue;
236 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400237
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400238 GrBackendFormat backendFormat = direct->defaultBackendFormat(baseLevel.colorType(),
239 GrRenderable::kNo);
Robert Phillips923181b2020-02-14 12:36:37 -0500240 if (!caps->isFormatTexturable(backendFormat)) {
241 continue;
242 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400243
Robert Phillips923181b2020-02-14 12:36:37 -0500244 sk_sp<PromiseImageCallbackContext> callbackContext(
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400245 new PromiseImageCallbackContext(direct, backendFormat));
Robert Phillips923181b2020-02-14 12:36:37 -0500246
247 info.setCallbackContext(0, std::move(callbackContext));
248 }
249 }
250}
251
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400252void DDLPromiseImageHelper::uploadAllToGPU(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips923181b2020-02-14 12:36:37 -0500253 if (taskGroup) {
254 for (int i = 0; i < fImageInfo.count(); ++i) {
255 PromiseImageInfo* info = &fImageInfo[i];
256
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400257 taskGroup->add([direct, info]() { CreateBETexturesForPromiseImage(direct, info); });
Robert Phillips923181b2020-02-14 12:36:37 -0500258 }
259 } else {
260 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400261 CreateBETexturesForPromiseImage(direct, &fImageInfo[i]);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400262 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500263 }
264}
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400265
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400266void DDLPromiseImageHelper::deleteAllFromGPU(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips19f466d2020-02-26 10:27:07 -0500267 if (taskGroup) {
268 for (int i = 0; i < fImageInfo.count(); ++i) {
269 PromiseImageInfo* info = &fImageInfo[i];
270
Brian Salomonbd3792d2020-11-10 14:17:58 -0500271 taskGroup->add([info]() { DeleteBETexturesForPromiseImage(info); });
Robert Phillips19f466d2020-02-26 10:27:07 -0500272 }
273 } else {
274 for (int i = 0; i < fImageInfo.count(); ++i) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500275 DeleteBETexturesForPromiseImage(&fImageInfo[i]);
Robert Phillips19f466d2020-02-26 10:27:07 -0500276 }
277 }
278}
279
Robert Phillips96601082018-05-29 16:13:26 -0400280sk_sp<SkPicture> DDLPromiseImageHelper::reinflateSKP(
281 SkDeferredDisplayListRecorder* recorder,
282 SkData* compressedPictureData,
283 SkTArray<sk_sp<SkImage>>* promiseImages) const {
284 PerRecorderContext perRecorderContext { recorder, this, promiseImages };
285
286 SkDeserialProcs procs;
287 procs.fImageCtx = (void*) &perRecorderContext;
Robert Phillips923181b2020-02-14 12:36:37 -0500288 procs.fImageProc = CreatePromiseImages;
Robert Phillips96601082018-05-29 16:13:26 -0400289
290 return SkPicture::MakeFromData(compressedPictureData, &procs);
291}
292
293// This generates promise images to replace the indices in the compressed picture. This
294// reconstitution is performed separately in each thread so we end up with multiple
295// promise images referring to the same GrBackendTexture.
Robert Phillips923181b2020-02-14 12:36:37 -0500296sk_sp<SkImage> DDLPromiseImageHelper::CreatePromiseImages(const void* rawData,
Robert Phillips96601082018-05-29 16:13:26 -0400297 size_t length, void* ctxIn) {
298 PerRecorderContext* perRecorderContext = static_cast<PerRecorderContext*>(ctxIn);
299 const DDLPromiseImageHelper* helper = perRecorderContext->fHelper;
300 SkDeferredDisplayListRecorder* recorder = perRecorderContext->fRecorder;
301
302 SkASSERT(length == sizeof(int));
303
304 const int* indexPtr = static_cast<const int*>(rawData);
Robert Phillips6bad7052019-12-16 15:09:57 -0500305 if (!helper->isValidID(*indexPtr)) {
306 return nullptr;
307 }
Robert Phillips96601082018-05-29 16:13:26 -0400308
309 const DDLPromiseImageHelper::PromiseImageInfo& curImage = helper->getInfo(*indexPtr);
310
Robert Phillips923181b2020-02-14 12:36:37 -0500311 // If there is no callback context that means 'createCallbackContexts' determined the
312 // texture wouldn't fit on the GPU. Create a separate bitmap-backed image for each thread.
313 if (!curImage.isYUV() && !curImage.callbackContext(0)) {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400314 SkASSERT(curImage.baseLevel().isImmutable());
Mike Reeddc607e32020-12-23 11:50:36 -0500315 return curImage.baseLevel().asImage();
Robert Phillips96601082018-05-29 16:13:26 -0400316 }
Robert Phillips923181b2020-02-14 12:36:37 -0500317
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400318 SkASSERT(curImage.index() == *indexPtr);
Robert Phillips96601082018-05-29 16:13:26 -0400319
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400320 sk_sp<SkImage> image;
321 if (curImage.isYUV()) {
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500322 GrBackendFormat backendFormats[SkYUVAInfo::kMaxPlanes];
Brian Salomonbd3792d2020-11-10 14:17:58 -0500323 const SkYUVAInfo& yuvaInfo = curImage.yuvaInfo();
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500324 void* contexts[SkYUVAInfo::kMaxPlanes] = {nullptr, nullptr, nullptr, nullptr};
Brian Salomonbd3792d2020-11-10 14:17:58 -0500325 int textureCount = yuvaInfo.numPlanes();
Jim Van Verth8f11e432018-10-18 14:36:59 -0400326 for (int i = 0; i < textureCount; ++i) {
Robert Phillips923181b2020-02-14 12:36:37 -0500327 backendFormats[i] = curImage.backendFormat(i);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400328 contexts[i] = curImage.refCallbackContext(i).release();
329 }
Brian Salomonbd3792d2020-11-10 14:17:58 -0500330 GrYUVABackendTextureInfo yuvaBackendTextures(yuvaInfo,
331 backendFormats,
332 GrMipmapped::kNo,
333 kTopLeft_GrSurfaceOrigin);
Jim Van Verthf99a6742018-10-18 16:13:18 +0000334
Brian Salomon0cc57542019-03-08 13:28:46 -0500335 image = recorder->makeYUVAPromiseTexture(
Brian Salomonbd3792d2020-11-10 14:17:58 -0500336 yuvaBackendTextures,
Brian Salomon0cc57542019-03-08 13:28:46 -0500337 curImage.refOverallColorSpace(),
Robert Phillips11c67672020-04-23 15:10:03 -0400338 PromiseImageCallbackContext::PromiseImageFulfillProc,
339 PromiseImageCallbackContext::PromiseImageReleaseProc,
Brian Salomonf1432742020-11-09 15:40:27 -0500340 contexts);
Brian Salomon72068172020-12-17 09:38:59 -0500341 if (!image) {
342 return nullptr;
343 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500344 for (int i = 0; i < textureCount; ++i) {
345 curImage.callbackContext(i)->wasAddedToImage();
346 }
Robert Phillips193c4212019-03-04 12:18:53 -0500347
348#ifdef SK_DEBUG
349 {
350 // By the peekProxy contract this image should not have a single backing proxy so
351 // should return null. The call should also not trigger the conversion to RGBA.
352 SkImage_GpuYUVA* yuva = reinterpret_cast<SkImage_GpuYUVA*>(image.get());
353 SkASSERT(!yuva->peekProxy());
354 SkASSERT(!yuva->peekProxy()); // the first call didn't force a conversion to RGBA
355 }
356#endif
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400357 } else {
John Stiles31954bf2020-08-07 17:35:54 -0400358 const GrBackendFormat& backendFormat = curImage.backendFormat(0);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500359 SkASSERT(backendFormat.isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400360
361 // Each DDL recorder gets its own ref on the promise callback context for the
362 // promise images it creates.
Brian Salomonf1432742020-11-09 15:40:27 -0500363 image = recorder->makePromiseTexture(backendFormat,
364 curImage.overallWidth(),
365 curImage.overallHeight(),
366 curImage.mipMapped(0),
367 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
368 curImage.overallColorType(),
369 curImage.overallAlphaType(),
370 curImage.refOverallColorSpace(),
371 PromiseImageCallbackContext::PromiseImageFulfillProc,
372 PromiseImageCallbackContext::PromiseImageReleaseProc,
373 (void*)curImage.refCallbackContext(0).release());
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500374 curImage.callbackContext(0)->wasAddedToImage();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400375 }
Robert Phillips96601082018-05-29 16:13:26 -0400376 perRecorderContext->fPromiseImages->push_back(image);
377 SkASSERT(image);
378 return image;
379}
380
381int DDLPromiseImageHelper::findImage(SkImage* image) const {
382 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400383 if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here
384 SkASSERT(fImageInfo[i].index() == i);
385 SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index()));
Robert Phillips96601082018-05-29 16:13:26 -0400386 return i;
387 }
388 }
389 return -1;
390}
391
392int DDLPromiseImageHelper::addImage(SkImage* image) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400393 SkImage_Base* ib = as_IB(image);
Robert Phillips96601082018-05-29 16:13:26 -0400394
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400395 SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(),
Robert Phillips13371a12019-05-13 15:59:10 -0400396 image->colorType() == kBGRA_8888_SkColorType
397 ? kRGBA_8888_SkColorType
398 : image->colorType(),
399 image->alphaType(),
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400400 image->refColorSpace());
Robert Phillips96601082018-05-29 16:13:26 -0400401
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400402 PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.count(),
403 image->uniqueID(),
404 overallII);
Robert Phillips96601082018-05-29 16:13:26 -0400405
Brian Salomonefb5f072020-07-28 21:06:43 -0400406 auto codec = SkCodecImageGenerator::MakeFromEncodedCodec(ib->refEncodedData());
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400407 SkYUVAPixmapInfo yuvaInfo;
Brian Salomon59c60b02020-09-01 15:01:15 -0400408 if (codec && codec->queryYUVAInfo(fSupportedYUVADataTypes, &yuvaInfo)) {
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400409 auto yuvaPixmaps = SkYUVAPixmaps::Allocate(yuvaInfo);
410 SkAssertResult(codec->getYUVAPlanes(yuvaPixmaps));
Brian Salomon5660e8b2020-08-25 12:40:32 -0400411 SkASSERT(yuvaPixmaps.isValid());
412 newImageInfo.setYUVPlanes(std::move(yuvaPixmaps));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400413 } else {
414 sk_sp<SkImage> rasterImage = image->makeRasterImage(); // force decoding of lazy images
Robert Phillipse84bffc2019-12-16 11:22:17 -0500415 if (!rasterImage) {
416 return -1;
417 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400418
419 SkBitmap tmp;
420 tmp.allocPixels(overallII);
421
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400422 if (!rasterImage->readPixels(nullptr, tmp.pixmap(), 0, 0)) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400423 return -1;
424 }
425
426 tmp.setImmutable();
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400427
428 // Given how the DDL testing harness works (i.e., only modifying the SkImages w/in an
429 // SKP) we don't know if a given SkImage will require mipmapping. To work around this
430 // we just try to create all the backend textures as mipmapped but, failing that, fall
431 // back to un-mipped.
Mike Reed13711eb2020-07-14 17:16:32 -0400432 std::unique_ptr<SkMipmap> mipmaps(SkMipmap::Build(tmp.pixmap(), nullptr));
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400433
434 newImageInfo.setMipLevels(tmp, std::move(mipmaps));
Robert Phillips96601082018-05-29 16:13:26 -0400435 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400436 // In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU
Robert Phillips96601082018-05-29 16:13:26 -0400437
438 return fImageInfo.count()-1;
439}
440
441int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) {
442 int preExistingID = this->findImage(image);
443 if (preExistingID >= 0) {
444 SkASSERT(this->isValidID(preExistingID));
445 return preExistingID;
446 }
447
448 int newID = this->addImage(image);
Robert Phillips96601082018-05-29 16:13:26 -0400449 return newID;
450}