blob: 3acb3412c4f49a33fe7a5a189bb20e843ec3e444 [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 Salomon72068172020-12-17 09:38:59 -050072 // See comment in release() about YUVA image creation failures.
73 // SkASSERT(fDoneCnt == fNumImages);1
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050074 SkASSERT(!fTotalFulfills || fDoneCnt);
Robert Phillips96601082018-05-29 16:13:26 -040075
Brian Salomon3f4cd772019-01-11 16:03:19 -050076 if (fPromiseImageTexture) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -040077 fContext->deleteBackendTexture(fPromiseImageTexture->backendTexture());
Robert Phillips96601082018-05-29 16:13:26 -040078 }
79}
80
Robert Phillips11c67672020-04-23 15:10:03 -040081void PromiseImageCallbackContext::setBackendTexture(const GrBackendTexture& backendTexture) {
Brian Salomon7d88f312019-02-28 10:03:03 -050082 SkASSERT(!fPromiseImageTexture);
Robert Phillips923181b2020-02-14 12:36:37 -050083 SkASSERT(fBackendFormat == backendTexture.getBackendFormat());
Brian Salomon3f4cd772019-01-11 16:03:19 -050084 fPromiseImageTexture = SkPromiseImageTexture::Make(backendTexture);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050085}
86
Robert Phillipsd5f3c982020-07-07 13:18:47 -040087void PromiseImageCallbackContext::destroyBackendTexture() {
88 SkASSERT(!fPromiseImageTexture || fPromiseImageTexture->unique());
89
90 if (fPromiseImageTexture) {
91 fContext->deleteBackendTexture(fPromiseImageTexture->backendTexture());
92 }
93 fPromiseImageTexture = nullptr;
94}
95
Robert Phillips96601082018-05-29 16:13:26 -040096///////////////////////////////////////////////////////////////////////////////////////////////////
97
98sk_sp<SkData> DDLPromiseImageHelper::deflateSKP(const SkPicture* inputPicture) {
99 SkSerialProcs procs;
100
101 procs.fImageCtx = this;
102 procs.fImageProc = [](SkImage* image, void* ctx) -> sk_sp<SkData> {
103 auto helper = static_cast<DDLPromiseImageHelper*>(ctx);
104
105 int id = helper->findOrDefineImage(image);
Robert Phillips96601082018-05-29 16:13:26 -0400106
Robert Phillips6bad7052019-12-16 15:09:57 -0500107 // Even if 'id' is invalid (i.e., -1) write it to the SKP
108 return SkData::MakeWithCopy(&id, sizeof(id));
Robert Phillips96601082018-05-29 16:13:26 -0400109 };
110
111 return inputPicture->serialize(&procs);
112}
113
Brian Salomonbd3792d2020-11-10 14:17:58 -0500114static GrBackendTexture create_yuva_texture(GrDirectContext* direct,
115 const SkPixmap& pm,
116 int texIndex) {
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500117 SkASSERT(texIndex >= 0 && texIndex <= 3);
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400118
Greg Danielc1ad77c2020-05-06 11:40:03 -0400119 bool finishedBECreate = false;
120 auto markFinished = [](void* context) {
121 *(bool*)context = true;
122 };
Brian Salomonb5f880a2020-12-07 11:30:16 -0500123 auto beTex = direct->createBackendTexture(pm,
124 kTopLeft_GrSurfaceOrigin,
125 GrRenderable::kNo,
126 GrProtected::kNo,
127 markFinished,
128 &finishedBECreate);
Greg Danielc1ad77c2020-05-06 11:40:03 -0400129 if (beTex.isValid()) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400130 direct->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400131 while (!finishedBECreate) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400132 direct->checkAsyncWorkCompletion();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400133 }
134 }
135 return beTex;
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500136}
137
Robert Phillips923181b2020-02-14 12:36:37 -0500138/*
139 * Create backend textures and upload data to them for all the textures required to satisfy
140 * a single promise image.
141 * For YUV textures this will result in up to 4 actual textures.
142 */
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400143void DDLPromiseImageHelper::CreateBETexturesForPromiseImage(GrDirectContext* direct,
Robert Phillips923181b2020-02-14 12:36:37 -0500144 PromiseImageInfo* info) {
Robert Phillips923181b2020-02-14 12:36:37 -0500145 if (info->isYUV()) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500146 int numPixmaps = info->yuvaInfo().numPlanes();
Robert Phillips923181b2020-02-14 12:36:37 -0500147 for (int j = 0; j < numPixmaps; ++j) {
148 const SkPixmap& yuvPixmap = info->yuvPixmap(j);
149
150 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
151 SkASSERT(callbackContext);
152
Robert Phillips4508eb92020-04-15 15:54:34 -0400153 // DDL TODO: what should we do with mipmapped YUV images
Brian Salomonbd3792d2020-11-10 14:17:58 -0500154 callbackContext->setBackendTexture(create_yuva_texture(direct, yuvPixmap, j));
Robert Phillips923181b2020-02-14 12:36:37 -0500155 SkASSERT(callbackContext->promiseImageTexture());
156 }
157 } else {
158 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
159 if (!callbackContext) {
160 // This texture would've been too large to fit on the GPU
161 return;
162 }
163
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400164 std::unique_ptr<SkPixmap[]> mipLevels = info->normalMipLevels();
Robert Phillips923181b2020-02-14 12:36:37 -0500165
Greg Danielc1ad77c2020-05-06 11:40:03 -0400166 bool finishedBECreate = false;
167 auto markFinished = [](void* context) {
168 *(bool*)context = true;
169 };
Brian Salomonb5f880a2020-12-07 11:30:16 -0500170 auto backendTex = direct->createBackendTexture(mipLevels.get(),
171 info->numMipLevels(),
172 kTopLeft_GrSurfaceOrigin,
173 GrRenderable::kNo,
174 GrProtected::kNo,
175 markFinished,
176 &finishedBECreate);
Robert Phillips923181b2020-02-14 12:36:37 -0500177 SkASSERT(backendTex.isValid());
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400178 direct->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400179 while (!finishedBECreate) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400180 direct->checkAsyncWorkCompletion();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400181 }
Robert Phillips923181b2020-02-14 12:36:37 -0500182
183 callbackContext->setBackendTexture(backendTex);
184 }
185}
186
Brian Salomonbd3792d2020-11-10 14:17:58 -0500187void DDLPromiseImageHelper::DeleteBETexturesForPromiseImage(PromiseImageInfo* info) {
Robert Phillips19f466d2020-02-26 10:27:07 -0500188 if (info->isYUV()) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500189 int numPixmaps = info->yuvaInfo().numPlanes();
Robert Phillips19f466d2020-02-26 10:27:07 -0500190 for (int j = 0; j < numPixmaps; ++j) {
191 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
192 SkASSERT(callbackContext);
193
194 callbackContext->destroyBackendTexture();
195 SkASSERT(!callbackContext->promiseImageTexture());
196 }
197 } else {
198 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
199 if (!callbackContext) {
200 // This texture would've been too large to fit on the GPU
201 return;
202 }
203
204 callbackContext->destroyBackendTexture();
205 SkASSERT(!callbackContext->promiseImageTexture());
206 }
207}
208
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400209void DDLPromiseImageHelper::createCallbackContexts(GrDirectContext* direct) {
210 const GrCaps* caps = direct->priv().caps();
Robert Phillips923181b2020-02-14 12:36:37 -0500211 const int maxDimension = caps->maxTextureSize();
212
213 for (int i = 0; i < fImageInfo.count(); ++i) {
214 PromiseImageInfo& info = fImageInfo[i];
215
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400216 if (info.isYUV()) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500217 int numPixmaps = info.yuvaInfo().numPlanes();
Robert Phillips923181b2020-02-14 12:36:37 -0500218
Jim Van Verth8f11e432018-10-18 14:36:59 -0400219 for (int j = 0; j < numPixmaps; ++j) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400220 const SkPixmap& yuvPixmap = info.yuvPixmap(j);
Robert Phillips96601082018-05-29 16:13:26 -0400221
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400222 GrBackendFormat backendFormat = direct->defaultBackendFormat(yuvPixmap.colorType(),
223 GrRenderable::kNo);
Robert Phillips923181b2020-02-14 12:36:37 -0500224
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400225 sk_sp<PromiseImageCallbackContext> callbackContext(
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400226 new PromiseImageCallbackContext(direct, backendFormat));
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500227
Robert Phillips923181b2020-02-14 12:36:37 -0500228 info.setCallbackContext(j, std::move(callbackContext));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400229 }
230 } else {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400231 const SkBitmap& baseLevel = info.baseLevel();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400232
Robert Phillips923181b2020-02-14 12:36:37 -0500233 // TODO: explicitly mark the PromiseImageInfo as too big and check in uploadAllToGPU
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400234 if (maxDimension < std::max(baseLevel.width(), baseLevel.height())) {
Robert Phillips923181b2020-02-14 12:36:37 -0500235 // This won't fit on the GPU. Fallback to a raster-backed image per tile.
236 continue;
237 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400238
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400239 GrBackendFormat backendFormat = direct->defaultBackendFormat(baseLevel.colorType(),
240 GrRenderable::kNo);
Robert Phillips923181b2020-02-14 12:36:37 -0500241 if (!caps->isFormatTexturable(backendFormat)) {
242 continue;
243 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400244
Robert Phillips923181b2020-02-14 12:36:37 -0500245 sk_sp<PromiseImageCallbackContext> callbackContext(
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400246 new PromiseImageCallbackContext(direct, backendFormat));
Robert Phillips923181b2020-02-14 12:36:37 -0500247
248 info.setCallbackContext(0, std::move(callbackContext));
249 }
250 }
251}
252
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400253void DDLPromiseImageHelper::uploadAllToGPU(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips923181b2020-02-14 12:36:37 -0500254 if (taskGroup) {
255 for (int i = 0; i < fImageInfo.count(); ++i) {
256 PromiseImageInfo* info = &fImageInfo[i];
257
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400258 taskGroup->add([direct, info]() { CreateBETexturesForPromiseImage(direct, info); });
Robert Phillips923181b2020-02-14 12:36:37 -0500259 }
260 } else {
261 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400262 CreateBETexturesForPromiseImage(direct, &fImageInfo[i]);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400263 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500264 }
265}
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400266
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400267void DDLPromiseImageHelper::deleteAllFromGPU(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips19f466d2020-02-26 10:27:07 -0500268 if (taskGroup) {
269 for (int i = 0; i < fImageInfo.count(); ++i) {
270 PromiseImageInfo* info = &fImageInfo[i];
271
Brian Salomonbd3792d2020-11-10 14:17:58 -0500272 taskGroup->add([info]() { DeleteBETexturesForPromiseImage(info); });
Robert Phillips19f466d2020-02-26 10:27:07 -0500273 }
274 } else {
275 for (int i = 0; i < fImageInfo.count(); ++i) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500276 DeleteBETexturesForPromiseImage(&fImageInfo[i]);
Robert Phillips19f466d2020-02-26 10:27:07 -0500277 }
278 }
279}
280
Robert Phillips96601082018-05-29 16:13:26 -0400281sk_sp<SkPicture> DDLPromiseImageHelper::reinflateSKP(
282 SkDeferredDisplayListRecorder* recorder,
283 SkData* compressedPictureData,
284 SkTArray<sk_sp<SkImage>>* promiseImages) const {
285 PerRecorderContext perRecorderContext { recorder, this, promiseImages };
286
287 SkDeserialProcs procs;
288 procs.fImageCtx = (void*) &perRecorderContext;
Robert Phillips923181b2020-02-14 12:36:37 -0500289 procs.fImageProc = CreatePromiseImages;
Robert Phillips96601082018-05-29 16:13:26 -0400290
291 return SkPicture::MakeFromData(compressedPictureData, &procs);
292}
293
294// This generates promise images to replace the indices in the compressed picture. This
295// reconstitution is performed separately in each thread so we end up with multiple
296// promise images referring to the same GrBackendTexture.
Robert Phillips923181b2020-02-14 12:36:37 -0500297sk_sp<SkImage> DDLPromiseImageHelper::CreatePromiseImages(const void* rawData,
Robert Phillips96601082018-05-29 16:13:26 -0400298 size_t length, void* ctxIn) {
299 PerRecorderContext* perRecorderContext = static_cast<PerRecorderContext*>(ctxIn);
300 const DDLPromiseImageHelper* helper = perRecorderContext->fHelper;
301 SkDeferredDisplayListRecorder* recorder = perRecorderContext->fRecorder;
302
303 SkASSERT(length == sizeof(int));
304
305 const int* indexPtr = static_cast<const int*>(rawData);
Robert Phillips6bad7052019-12-16 15:09:57 -0500306 if (!helper->isValidID(*indexPtr)) {
307 return nullptr;
308 }
Robert Phillips96601082018-05-29 16:13:26 -0400309
310 const DDLPromiseImageHelper::PromiseImageInfo& curImage = helper->getInfo(*indexPtr);
311
Robert Phillips923181b2020-02-14 12:36:37 -0500312 // If there is no callback context that means 'createCallbackContexts' determined the
313 // texture wouldn't fit on the GPU. Create a separate bitmap-backed image for each thread.
314 if (!curImage.isYUV() && !curImage.callbackContext(0)) {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400315 SkASSERT(curImage.baseLevel().isImmutable());
Mike Reeddc607e32020-12-23 11:50:36 -0500316 return curImage.baseLevel().asImage();
Robert Phillips96601082018-05-29 16:13:26 -0400317 }
Robert Phillips923181b2020-02-14 12:36:37 -0500318
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400319 SkASSERT(curImage.index() == *indexPtr);
Robert Phillips96601082018-05-29 16:13:26 -0400320
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400321 sk_sp<SkImage> image;
322 if (curImage.isYUV()) {
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500323 GrBackendFormat backendFormats[SkYUVAInfo::kMaxPlanes];
Brian Salomonbd3792d2020-11-10 14:17:58 -0500324 const SkYUVAInfo& yuvaInfo = curImage.yuvaInfo();
Brian Salomon0c0b5a62021-01-11 14:40:44 -0500325 void* contexts[SkYUVAInfo::kMaxPlanes] = {nullptr, nullptr, nullptr, nullptr};
Brian Salomonbd3792d2020-11-10 14:17:58 -0500326 int textureCount = yuvaInfo.numPlanes();
Jim Van Verth8f11e432018-10-18 14:36:59 -0400327 for (int i = 0; i < textureCount; ++i) {
Robert Phillips923181b2020-02-14 12:36:37 -0500328 backendFormats[i] = curImage.backendFormat(i);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400329 contexts[i] = curImage.refCallbackContext(i).release();
330 }
Brian Salomonbd3792d2020-11-10 14:17:58 -0500331 GrYUVABackendTextureInfo yuvaBackendTextures(yuvaInfo,
332 backendFormats,
333 GrMipmapped::kNo,
334 kTopLeft_GrSurfaceOrigin);
Jim Van Verthf99a6742018-10-18 16:13:18 +0000335
Brian Salomon0cc57542019-03-08 13:28:46 -0500336 image = recorder->makeYUVAPromiseTexture(
Brian Salomonbd3792d2020-11-10 14:17:58 -0500337 yuvaBackendTextures,
Brian Salomon0cc57542019-03-08 13:28:46 -0500338 curImage.refOverallColorSpace(),
Robert Phillips11c67672020-04-23 15:10:03 -0400339 PromiseImageCallbackContext::PromiseImageFulfillProc,
340 PromiseImageCallbackContext::PromiseImageReleaseProc,
Brian Salomonf1432742020-11-09 15:40:27 -0500341 contexts);
Brian Salomon72068172020-12-17 09:38:59 -0500342 if (!image) {
343 return nullptr;
344 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500345 for (int i = 0; i < textureCount; ++i) {
346 curImage.callbackContext(i)->wasAddedToImage();
347 }
Robert Phillips193c4212019-03-04 12:18:53 -0500348
349#ifdef SK_DEBUG
350 {
351 // By the peekProxy contract this image should not have a single backing proxy so
352 // should return null. The call should also not trigger the conversion to RGBA.
353 SkImage_GpuYUVA* yuva = reinterpret_cast<SkImage_GpuYUVA*>(image.get());
354 SkASSERT(!yuva->peekProxy());
355 SkASSERT(!yuva->peekProxy()); // the first call didn't force a conversion to RGBA
356 }
357#endif
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400358 } else {
John Stiles31954bf2020-08-07 17:35:54 -0400359 const GrBackendFormat& backendFormat = curImage.backendFormat(0);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500360 SkASSERT(backendFormat.isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400361
362 // Each DDL recorder gets its own ref on the promise callback context for the
363 // promise images it creates.
Brian Salomonf1432742020-11-09 15:40:27 -0500364 image = recorder->makePromiseTexture(backendFormat,
365 curImage.overallWidth(),
366 curImage.overallHeight(),
367 curImage.mipMapped(0),
368 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
369 curImage.overallColorType(),
370 curImage.overallAlphaType(),
371 curImage.refOverallColorSpace(),
372 PromiseImageCallbackContext::PromiseImageFulfillProc,
373 PromiseImageCallbackContext::PromiseImageReleaseProc,
374 (void*)curImage.refCallbackContext(0).release());
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500375 curImage.callbackContext(0)->wasAddedToImage();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400376 }
Robert Phillips96601082018-05-29 16:13:26 -0400377 perRecorderContext->fPromiseImages->push_back(image);
378 SkASSERT(image);
379 return image;
380}
381
382int DDLPromiseImageHelper::findImage(SkImage* image) const {
383 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400384 if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here
385 SkASSERT(fImageInfo[i].index() == i);
386 SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index()));
Robert Phillips96601082018-05-29 16:13:26 -0400387 return i;
388 }
389 }
390 return -1;
391}
392
393int DDLPromiseImageHelper::addImage(SkImage* image) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400394 SkImage_Base* ib = as_IB(image);
Robert Phillips96601082018-05-29 16:13:26 -0400395
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400396 SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(),
Robert Phillips13371a12019-05-13 15:59:10 -0400397 image->colorType() == kBGRA_8888_SkColorType
398 ? kRGBA_8888_SkColorType
399 : image->colorType(),
400 image->alphaType(),
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400401 image->refColorSpace());
Robert Phillips96601082018-05-29 16:13:26 -0400402
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400403 PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.count(),
404 image->uniqueID(),
405 overallII);
Robert Phillips96601082018-05-29 16:13:26 -0400406
Brian Salomonefb5f072020-07-28 21:06:43 -0400407 auto codec = SkCodecImageGenerator::MakeFromEncodedCodec(ib->refEncodedData());
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400408 SkYUVAPixmapInfo yuvaInfo;
Brian Salomon59c60b02020-09-01 15:01:15 -0400409 if (codec && codec->queryYUVAInfo(fSupportedYUVADataTypes, &yuvaInfo)) {
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400410 auto yuvaPixmaps = SkYUVAPixmaps::Allocate(yuvaInfo);
411 SkAssertResult(codec->getYUVAPlanes(yuvaPixmaps));
Brian Salomon5660e8b2020-08-25 12:40:32 -0400412 SkASSERT(yuvaPixmaps.isValid());
413 newImageInfo.setYUVPlanes(std::move(yuvaPixmaps));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400414 } else {
415 sk_sp<SkImage> rasterImage = image->makeRasterImage(); // force decoding of lazy images
Robert Phillipse84bffc2019-12-16 11:22:17 -0500416 if (!rasterImage) {
417 return -1;
418 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400419
420 SkBitmap tmp;
421 tmp.allocPixels(overallII);
422
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400423 if (!rasterImage->readPixels(nullptr, tmp.pixmap(), 0, 0)) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400424 return -1;
425 }
426
427 tmp.setImmutable();
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400428
429 // Given how the DDL testing harness works (i.e., only modifying the SkImages w/in an
430 // SKP) we don't know if a given SkImage will require mipmapping. To work around this
431 // we just try to create all the backend textures as mipmapped but, failing that, fall
432 // back to un-mipped.
Mike Reed13711eb2020-07-14 17:16:32 -0400433 std::unique_ptr<SkMipmap> mipmaps(SkMipmap::Build(tmp.pixmap(), nullptr));
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400434
435 newImageInfo.setMipLevels(tmp, std::move(mipmaps));
Robert Phillips96601082018-05-29 16:13:26 -0400436 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400437 // In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU
Robert Phillips96601082018-05-29 16:13:26 -0400438
439 return fImageInfo.count()-1;
440}
441
442int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) {
443 int preExistingID = this->findImage(image);
444 if (preExistingID >= 0) {
445 SkASSERT(this->isValidID(preExistingID));
446 return preExistingID;
447 }
448
449 int newID = this->addImage(image);
Robert Phillips96601082018-05-29 16:13:26 -0400450 return newID;
451}