blob: 48f54f1bb2731158d93a5bab70dbd77409da2a17 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkYUVAIndex.h"
14#include "include/core/SkYUVASizeInfo.h"
Robert Phillipsd5f3c982020-07-07 13:18:47 -040015#include "include/gpu/GrDirectContext.h"
Brian Salomonbd3792d2020-11-10 14:17:58 -050016#include "include/gpu/GrYUVABackendTextures.h"
Brian Salomonefb5f072020-07-28 21:06:43 -040017#include "src/codec/SkCodecImageGenerator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/core/SkCachedData.h"
Mike Reed13711eb2020-07-14 17:16:32 -040019#include "src/core/SkMipmap.h"
Robert Phillips923181b2020-02-14 12:36:37 -050020#include "src/core/SkTaskGroup.h"
Adlai Hollera0693042020-10-14 11:23:11 -040021#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/image/SkImage_Base.h"
23#include "src/image/SkImage_GpuYUVA.h"
Robert Phillips96601082018-05-29 16:13:26 -040024
Robert Phillipsf95e2f42020-04-17 16:20:55 -040025DDLPromiseImageHelper::PromiseImageInfo::PromiseImageInfo(int index,
26 uint32_t originalUniqueID,
27 const SkImageInfo& ii)
28 : fIndex(index)
29 , fOriginalUniqueID(originalUniqueID)
30 , fImageInfo(ii) {
31}
32
33DDLPromiseImageHelper::PromiseImageInfo::PromiseImageInfo(PromiseImageInfo&& other)
34 : fIndex(other.fIndex)
35 , fOriginalUniqueID(other.fOriginalUniqueID)
36 , fImageInfo(other.fImageInfo)
37 , fBaseLevel(other.fBaseLevel)
38 , fMipLevels(std::move(other.fMipLevels))
Brian Salomon5660e8b2020-08-25 12:40:32 -040039 , fYUVAPixmaps(std::move(other.fYUVAPixmaps)) {
Robert Phillipsf95e2f42020-04-17 16:20:55 -040040 for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) {
Robert Phillipsf95e2f42020-04-17 16:20:55 -040041 fCallbackContexts[i] = std::move(other.fCallbackContexts[i]);
42 }
43}
44
45DDLPromiseImageHelper::PromiseImageInfo::~PromiseImageInfo() {}
46
John Stilesec9b4aa2020-08-07 13:05:14 -040047std::unique_ptr<SkPixmap[]> DDLPromiseImageHelper::PromiseImageInfo::normalMipLevels() const {
Robert Phillipsf95e2f42020-04-17 16:20:55 -040048 SkASSERT(!this->isYUV());
49 std::unique_ptr<SkPixmap[]> pixmaps(new SkPixmap[this->numMipLevels()]);
50 pixmaps[0] = fBaseLevel.pixmap();
51 if (fMipLevels) {
52 for (int i = 0; i < fMipLevels->countLevels(); ++i) {
Mike Reed13711eb2020-07-14 17:16:32 -040053 SkMipmap::Level mipLevel;
Robert Phillipsf95e2f42020-04-17 16:20:55 -040054 fMipLevels->getLevel(i, &mipLevel);
55 pixmaps[i+1] = mipLevel.fPixmap;
56 }
57 }
58 return pixmaps;
59}
60
61int DDLPromiseImageHelper::PromiseImageInfo::numMipLevels() const {
62 SkASSERT(!this->isYUV());
63 return fMipLevels ? fMipLevels->countLevels()+1 : 1;
64}
65
66void DDLPromiseImageHelper::PromiseImageInfo::setMipLevels(const SkBitmap& baseLevel,
Mike Reed13711eb2020-07-14 17:16:32 -040067 std::unique_ptr<SkMipmap> mipLevels) {
Robert Phillipsf95e2f42020-04-17 16:20:55 -040068 fBaseLevel = baseLevel;
69 fMipLevels = std::move(mipLevels);
70}
71
72///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips11c67672020-04-23 15:10:03 -040073PromiseImageCallbackContext::~PromiseImageCallbackContext() {
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050074 SkASSERT(fDoneCnt == fNumImages);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050075 SkASSERT(!fTotalFulfills || fDoneCnt);
Robert Phillips96601082018-05-29 16:13:26 -040076
Brian Salomon3f4cd772019-01-11 16:03:19 -050077 if (fPromiseImageTexture) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -040078 fContext->deleteBackendTexture(fPromiseImageTexture->backendTexture());
Robert Phillips96601082018-05-29 16:13:26 -040079 }
80}
81
Robert Phillips11c67672020-04-23 15:10:03 -040082void PromiseImageCallbackContext::setBackendTexture(const GrBackendTexture& backendTexture) {
Brian Salomon7d88f312019-02-28 10:03:03 -050083 SkASSERT(!fPromiseImageTexture);
Robert Phillips923181b2020-02-14 12:36:37 -050084 SkASSERT(fBackendFormat == backendTexture.getBackendFormat());
Brian Salomon3f4cd772019-01-11 16:03:19 -050085 fPromiseImageTexture = SkPromiseImageTexture::Make(backendTexture);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050086}
87
Robert Phillipsd5f3c982020-07-07 13:18:47 -040088void PromiseImageCallbackContext::destroyBackendTexture() {
89 SkASSERT(!fPromiseImageTexture || fPromiseImageTexture->unique());
90
91 if (fPromiseImageTexture) {
92 fContext->deleteBackendTexture(fPromiseImageTexture->backendTexture());
93 }
94 fPromiseImageTexture = nullptr;
95}
96
Robert Phillips96601082018-05-29 16:13:26 -040097///////////////////////////////////////////////////////////////////////////////////////////////////
98
99sk_sp<SkData> DDLPromiseImageHelper::deflateSKP(const SkPicture* inputPicture) {
100 SkSerialProcs procs;
101
102 procs.fImageCtx = this;
103 procs.fImageProc = [](SkImage* image, void* ctx) -> sk_sp<SkData> {
104 auto helper = static_cast<DDLPromiseImageHelper*>(ctx);
105
106 int id = helper->findOrDefineImage(image);
Robert Phillips96601082018-05-29 16:13:26 -0400107
Robert Phillips6bad7052019-12-16 15:09:57 -0500108 // Even if 'id' is invalid (i.e., -1) write it to the SKP
109 return SkData::MakeWithCopy(&id, sizeof(id));
Robert Phillips96601082018-05-29 16:13:26 -0400110 };
111
112 return inputPicture->serialize(&procs);
113}
114
Brian Salomonbd3792d2020-11-10 14:17:58 -0500115static GrBackendTexture create_yuva_texture(GrDirectContext* direct,
116 const SkPixmap& pm,
117 int texIndex) {
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500118 SkASSERT(texIndex >= 0 && texIndex <= 3);
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400119
Greg Danielc1ad77c2020-05-06 11:40:03 -0400120 bool finishedBECreate = false;
121 auto markFinished = [](void* context) {
122 *(bool*)context = true;
123 };
Brian Salomonb5f880a2020-12-07 11:30:16 -0500124 auto beTex = direct->createBackendTexture(pm,
125 kTopLeft_GrSurfaceOrigin,
126 GrRenderable::kNo,
127 GrProtected::kNo,
128 markFinished,
129 &finishedBECreate);
Greg Danielc1ad77c2020-05-06 11:40:03 -0400130 if (beTex.isValid()) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400131 direct->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400132 while (!finishedBECreate) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400133 direct->checkAsyncWorkCompletion();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400134 }
135 }
136 return beTex;
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500137}
138
Robert Phillips923181b2020-02-14 12:36:37 -0500139/*
140 * Create backend textures and upload data to them for all the textures required to satisfy
141 * a single promise image.
142 * For YUV textures this will result in up to 4 actual textures.
143 */
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400144void DDLPromiseImageHelper::CreateBETexturesForPromiseImage(GrDirectContext* direct,
Robert Phillips923181b2020-02-14 12:36:37 -0500145 PromiseImageInfo* info) {
Robert Phillips923181b2020-02-14 12:36:37 -0500146 if (info->isYUV()) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500147 int numPixmaps = info->yuvaInfo().numPlanes();
Robert Phillips923181b2020-02-14 12:36:37 -0500148 for (int j = 0; j < numPixmaps; ++j) {
149 const SkPixmap& yuvPixmap = info->yuvPixmap(j);
150
151 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
152 SkASSERT(callbackContext);
153
Robert Phillips4508eb92020-04-15 15:54:34 -0400154 // DDL TODO: what should we do with mipmapped YUV images
Brian Salomonbd3792d2020-11-10 14:17:58 -0500155 callbackContext->setBackendTexture(create_yuva_texture(direct, yuvPixmap, j));
Robert Phillips923181b2020-02-14 12:36:37 -0500156 SkASSERT(callbackContext->promiseImageTexture());
157 }
158 } else {
159 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
160 if (!callbackContext) {
161 // This texture would've been too large to fit on the GPU
162 return;
163 }
164
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400165 std::unique_ptr<SkPixmap[]> mipLevels = info->normalMipLevels();
Robert Phillips923181b2020-02-14 12:36:37 -0500166
Greg Danielc1ad77c2020-05-06 11:40:03 -0400167 bool finishedBECreate = false;
168 auto markFinished = [](void* context) {
169 *(bool*)context = true;
170 };
Brian Salomonb5f880a2020-12-07 11:30:16 -0500171 auto backendTex = direct->createBackendTexture(mipLevels.get(),
172 info->numMipLevels(),
173 kTopLeft_GrSurfaceOrigin,
174 GrRenderable::kNo,
175 GrProtected::kNo,
176 markFinished,
177 &finishedBECreate);
Robert Phillips923181b2020-02-14 12:36:37 -0500178 SkASSERT(backendTex.isValid());
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400179 direct->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400180 while (!finishedBECreate) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400181 direct->checkAsyncWorkCompletion();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400182 }
Robert Phillips923181b2020-02-14 12:36:37 -0500183
184 callbackContext->setBackendTexture(backendTex);
185 }
186}
187
Brian Salomonbd3792d2020-11-10 14:17:58 -0500188void DDLPromiseImageHelper::DeleteBETexturesForPromiseImage(PromiseImageInfo* info) {
Robert Phillips19f466d2020-02-26 10:27:07 -0500189 if (info->isYUV()) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500190 int numPixmaps = info->yuvaInfo().numPlanes();
Robert Phillips19f466d2020-02-26 10:27:07 -0500191 for (int j = 0; j < numPixmaps; ++j) {
192 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
193 SkASSERT(callbackContext);
194
195 callbackContext->destroyBackendTexture();
196 SkASSERT(!callbackContext->promiseImageTexture());
197 }
198 } else {
199 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
200 if (!callbackContext) {
201 // This texture would've been too large to fit on the GPU
202 return;
203 }
204
205 callbackContext->destroyBackendTexture();
206 SkASSERT(!callbackContext->promiseImageTexture());
207 }
208}
209
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400210void DDLPromiseImageHelper::createCallbackContexts(GrDirectContext* direct) {
211 const GrCaps* caps = direct->priv().caps();
Robert Phillips923181b2020-02-14 12:36:37 -0500212 const int maxDimension = caps->maxTextureSize();
213
214 for (int i = 0; i < fImageInfo.count(); ++i) {
215 PromiseImageInfo& info = fImageInfo[i];
216
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400217 if (info.isYUV()) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500218 int numPixmaps = info.yuvaInfo().numPlanes();
Robert Phillips923181b2020-02-14 12:36:37 -0500219
Jim Van Verth8f11e432018-10-18 14:36:59 -0400220 for (int j = 0; j < numPixmaps; ++j) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400221 const SkPixmap& yuvPixmap = info.yuvPixmap(j);
Robert Phillips96601082018-05-29 16:13:26 -0400222
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400223 GrBackendFormat backendFormat = direct->defaultBackendFormat(yuvPixmap.colorType(),
224 GrRenderable::kNo);
Robert Phillips923181b2020-02-14 12:36:37 -0500225
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400226 sk_sp<PromiseImageCallbackContext> callbackContext(
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400227 new PromiseImageCallbackContext(direct, backendFormat));
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500228
Robert Phillips923181b2020-02-14 12:36:37 -0500229 info.setCallbackContext(j, std::move(callbackContext));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400230 }
231 } else {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400232 const SkBitmap& baseLevel = info.baseLevel();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400233
Robert Phillips923181b2020-02-14 12:36:37 -0500234 // TODO: explicitly mark the PromiseImageInfo as too big and check in uploadAllToGPU
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400235 if (maxDimension < std::max(baseLevel.width(), baseLevel.height())) {
Robert Phillips923181b2020-02-14 12:36:37 -0500236 // This won't fit on the GPU. Fallback to a raster-backed image per tile.
237 continue;
238 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400239
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400240 GrBackendFormat backendFormat = direct->defaultBackendFormat(baseLevel.colorType(),
241 GrRenderable::kNo);
Robert Phillips923181b2020-02-14 12:36:37 -0500242 if (!caps->isFormatTexturable(backendFormat)) {
243 continue;
244 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400245
Robert Phillips923181b2020-02-14 12:36:37 -0500246 sk_sp<PromiseImageCallbackContext> callbackContext(
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400247 new PromiseImageCallbackContext(direct, backendFormat));
Robert Phillips923181b2020-02-14 12:36:37 -0500248
249 info.setCallbackContext(0, std::move(callbackContext));
250 }
251 }
252}
253
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400254void DDLPromiseImageHelper::uploadAllToGPU(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips923181b2020-02-14 12:36:37 -0500255 if (taskGroup) {
256 for (int i = 0; i < fImageInfo.count(); ++i) {
257 PromiseImageInfo* info = &fImageInfo[i];
258
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400259 taskGroup->add([direct, info]() { CreateBETexturesForPromiseImage(direct, info); });
Robert Phillips923181b2020-02-14 12:36:37 -0500260 }
261 } else {
262 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400263 CreateBETexturesForPromiseImage(direct, &fImageInfo[i]);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400264 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500265 }
266}
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400267
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400268void DDLPromiseImageHelper::deleteAllFromGPU(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips19f466d2020-02-26 10:27:07 -0500269 if (taskGroup) {
270 for (int i = 0; i < fImageInfo.count(); ++i) {
271 PromiseImageInfo* info = &fImageInfo[i];
272
Brian Salomonbd3792d2020-11-10 14:17:58 -0500273 taskGroup->add([info]() { DeleteBETexturesForPromiseImage(info); });
Robert Phillips19f466d2020-02-26 10:27:07 -0500274 }
275 } else {
276 for (int i = 0; i < fImageInfo.count(); ++i) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500277 DeleteBETexturesForPromiseImage(&fImageInfo[i]);
Robert Phillips19f466d2020-02-26 10:27:07 -0500278 }
279 }
280}
281
Robert Phillips96601082018-05-29 16:13:26 -0400282sk_sp<SkPicture> DDLPromiseImageHelper::reinflateSKP(
283 SkDeferredDisplayListRecorder* recorder,
284 SkData* compressedPictureData,
285 SkTArray<sk_sp<SkImage>>* promiseImages) const {
286 PerRecorderContext perRecorderContext { recorder, this, promiseImages };
287
288 SkDeserialProcs procs;
289 procs.fImageCtx = (void*) &perRecorderContext;
Robert Phillips923181b2020-02-14 12:36:37 -0500290 procs.fImageProc = CreatePromiseImages;
Robert Phillips96601082018-05-29 16:13:26 -0400291
292 return SkPicture::MakeFromData(compressedPictureData, &procs);
293}
294
295// This generates promise images to replace the indices in the compressed picture. This
296// reconstitution is performed separately in each thread so we end up with multiple
297// promise images referring to the same GrBackendTexture.
Robert Phillips923181b2020-02-14 12:36:37 -0500298sk_sp<SkImage> DDLPromiseImageHelper::CreatePromiseImages(const void* rawData,
Robert Phillips96601082018-05-29 16:13:26 -0400299 size_t length, void* ctxIn) {
300 PerRecorderContext* perRecorderContext = static_cast<PerRecorderContext*>(ctxIn);
301 const DDLPromiseImageHelper* helper = perRecorderContext->fHelper;
302 SkDeferredDisplayListRecorder* recorder = perRecorderContext->fRecorder;
303
304 SkASSERT(length == sizeof(int));
305
306 const int* indexPtr = static_cast<const int*>(rawData);
Robert Phillips6bad7052019-12-16 15:09:57 -0500307 if (!helper->isValidID(*indexPtr)) {
308 return nullptr;
309 }
Robert Phillips96601082018-05-29 16:13:26 -0400310
311 const DDLPromiseImageHelper::PromiseImageInfo& curImage = helper->getInfo(*indexPtr);
312
Robert Phillips923181b2020-02-14 12:36:37 -0500313 // If there is no callback context that means 'createCallbackContexts' determined the
314 // texture wouldn't fit on the GPU. Create a separate bitmap-backed image for each thread.
315 if (!curImage.isYUV() && !curImage.callbackContext(0)) {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400316 SkASSERT(curImage.baseLevel().isImmutable());
317 return SkImage::MakeFromBitmap(curImage.baseLevel());
Robert Phillips96601082018-05-29 16:13:26 -0400318 }
Robert Phillips923181b2020-02-14 12:36:37 -0500319
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400320 SkASSERT(curImage.index() == *indexPtr);
Robert Phillips96601082018-05-29 16:13:26 -0400321
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400322 sk_sp<SkImage> image;
323 if (curImage.isYUV()) {
Jim Van Verthe24b5872018-10-29 16:26:02 -0400324 GrBackendFormat backendFormats[SkYUVASizeInfo::kMaxCount];
Brian Salomonbd3792d2020-11-10 14:17:58 -0500325 const SkYUVAInfo& yuvaInfo = curImage.yuvaInfo();
Jim Van Verthe24b5872018-10-29 16:26:02 -0400326 void* contexts[SkYUVASizeInfo::kMaxCount] = { nullptr, nullptr, nullptr, nullptr };
Brian Salomonbd3792d2020-11-10 14:17:58 -0500327 int textureCount = yuvaInfo.numPlanes();
Jim Van Verth8f11e432018-10-18 14:36:59 -0400328 for (int i = 0; i < textureCount; ++i) {
Robert Phillips923181b2020-02-14 12:36:37 -0500329 backendFormats[i] = curImage.backendFormat(i);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400330 contexts[i] = curImage.refCallbackContext(i).release();
331 }
Brian Salomonbd3792d2020-11-10 14:17:58 -0500332 GrYUVABackendTextureInfo yuvaBackendTextures(yuvaInfo,
333 backendFormats,
334 GrMipmapped::kNo,
335 kTopLeft_GrSurfaceOrigin);
Jim Van Verthf99a6742018-10-18 16:13:18 +0000336
Brian Salomon0cc57542019-03-08 13:28:46 -0500337 image = recorder->makeYUVAPromiseTexture(
Brian Salomonbd3792d2020-11-10 14:17:58 -0500338 yuvaBackendTextures,
Brian Salomon0cc57542019-03-08 13:28:46 -0500339 curImage.refOverallColorSpace(),
Robert Phillips11c67672020-04-23 15:10:03 -0400340 PromiseImageCallbackContext::PromiseImageFulfillProc,
341 PromiseImageCallbackContext::PromiseImageReleaseProc,
Brian Salomonf1432742020-11-09 15:40:27 -0500342 contexts);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500343 for (int i = 0; i < textureCount; ++i) {
344 curImage.callbackContext(i)->wasAddedToImage();
345 }
Robert Phillips193c4212019-03-04 12:18:53 -0500346
347#ifdef SK_DEBUG
348 {
349 // By the peekProxy contract this image should not have a single backing proxy so
350 // should return null. The call should also not trigger the conversion to RGBA.
351 SkImage_GpuYUVA* yuva = reinterpret_cast<SkImage_GpuYUVA*>(image.get());
352 SkASSERT(!yuva->peekProxy());
353 SkASSERT(!yuva->peekProxy()); // the first call didn't force a conversion to RGBA
354 }
355#endif
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400356 } else {
John Stiles31954bf2020-08-07 17:35:54 -0400357 const GrBackendFormat& backendFormat = curImage.backendFormat(0);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500358 SkASSERT(backendFormat.isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400359
360 // Each DDL recorder gets its own ref on the promise callback context for the
361 // promise images it creates.
Brian Salomonf1432742020-11-09 15:40:27 -0500362 image = recorder->makePromiseTexture(backendFormat,
363 curImage.overallWidth(),
364 curImage.overallHeight(),
365 curImage.mipMapped(0),
366 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
367 curImage.overallColorType(),
368 curImage.overallAlphaType(),
369 curImage.refOverallColorSpace(),
370 PromiseImageCallbackContext::PromiseImageFulfillProc,
371 PromiseImageCallbackContext::PromiseImageReleaseProc,
372 (void*)curImage.refCallbackContext(0).release());
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500373 curImage.callbackContext(0)->wasAddedToImage();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400374 }
Robert Phillips96601082018-05-29 16:13:26 -0400375 perRecorderContext->fPromiseImages->push_back(image);
376 SkASSERT(image);
377 return image;
378}
379
380int DDLPromiseImageHelper::findImage(SkImage* image) const {
381 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400382 if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here
383 SkASSERT(fImageInfo[i].index() == i);
384 SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index()));
Robert Phillips96601082018-05-29 16:13:26 -0400385 return i;
386 }
387 }
388 return -1;
389}
390
391int DDLPromiseImageHelper::addImage(SkImage* image) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400392 SkImage_Base* ib = as_IB(image);
Robert Phillips96601082018-05-29 16:13:26 -0400393
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400394 SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(),
Robert Phillips13371a12019-05-13 15:59:10 -0400395 image->colorType() == kBGRA_8888_SkColorType
396 ? kRGBA_8888_SkColorType
397 : image->colorType(),
398 image->alphaType(),
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400399 image->refColorSpace());
Robert Phillips96601082018-05-29 16:13:26 -0400400
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400401 PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.count(),
402 image->uniqueID(),
403 overallII);
Robert Phillips96601082018-05-29 16:13:26 -0400404
Brian Salomonefb5f072020-07-28 21:06:43 -0400405 auto codec = SkCodecImageGenerator::MakeFromEncodedCodec(ib->refEncodedData());
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400406 SkYUVAPixmapInfo yuvaInfo;
Brian Salomon59c60b02020-09-01 15:01:15 -0400407 if (codec && codec->queryYUVAInfo(fSupportedYUVADataTypes, &yuvaInfo)) {
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400408 auto yuvaPixmaps = SkYUVAPixmaps::Allocate(yuvaInfo);
409 SkAssertResult(codec->getYUVAPlanes(yuvaPixmaps));
Brian Salomon5660e8b2020-08-25 12:40:32 -0400410 SkASSERT(yuvaPixmaps.isValid());
411 newImageInfo.setYUVPlanes(std::move(yuvaPixmaps));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400412 } else {
413 sk_sp<SkImage> rasterImage = image->makeRasterImage(); // force decoding of lazy images
Robert Phillipse84bffc2019-12-16 11:22:17 -0500414 if (!rasterImage) {
415 return -1;
416 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400417
418 SkBitmap tmp;
419 tmp.allocPixels(overallII);
420
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400421 if (!rasterImage->readPixels(nullptr, tmp.pixmap(), 0, 0)) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400422 return -1;
423 }
424
425 tmp.setImmutable();
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400426
427 // Given how the DDL testing harness works (i.e., only modifying the SkImages w/in an
428 // SKP) we don't know if a given SkImage will require mipmapping. To work around this
429 // we just try to create all the backend textures as mipmapped but, failing that, fall
430 // back to un-mipped.
Mike Reed13711eb2020-07-14 17:16:32 -0400431 std::unique_ptr<SkMipmap> mipmaps(SkMipmap::Build(tmp.pixmap(), nullptr));
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400432
433 newImageInfo.setMipLevels(tmp, std::move(mipmaps));
Robert Phillips96601082018-05-29 16:13:26 -0400434 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400435 // In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU
Robert Phillips96601082018-05-29 16:13:26 -0400436
437 return fImageInfo.count()-1;
438}
439
440int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) {
441 int preExistingID = this->findImage(image);
442 if (preExistingID >= 0) {
443 SkASSERT(this->isValidID(preExistingID));
444 return preExistingID;
445 }
446
447 int newID = this->addImage(image);
Robert Phillips96601082018-05-29 16:13:26 -0400448 return newID;
449}