blob: a9cb6da0d88cb8d9f80b84652d71f6623109d43c [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 };
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400124 auto beTex = direct->createBackendTexture(&pm, 1, GrRenderable::kNo, GrProtected::kNo,
125 markFinished, &finishedBECreate);
Greg Danielc1ad77c2020-05-06 11:40:03 -0400126 if (beTex.isValid()) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400127 direct->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400128 while (!finishedBECreate) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400129 direct->checkAsyncWorkCompletion();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400130 }
131 }
132 return beTex;
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500133}
134
Robert Phillips923181b2020-02-14 12:36:37 -0500135/*
136 * Create backend textures and upload data to them for all the textures required to satisfy
137 * a single promise image.
138 * For YUV textures this will result in up to 4 actual textures.
139 */
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400140void DDLPromiseImageHelper::CreateBETexturesForPromiseImage(GrDirectContext* direct,
Robert Phillips923181b2020-02-14 12:36:37 -0500141 PromiseImageInfo* info) {
Robert Phillips923181b2020-02-14 12:36:37 -0500142 if (info->isYUV()) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500143 int numPixmaps = info->yuvaInfo().numPlanes();
Robert Phillips923181b2020-02-14 12:36:37 -0500144 for (int j = 0; j < numPixmaps; ++j) {
145 const SkPixmap& yuvPixmap = info->yuvPixmap(j);
146
147 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
148 SkASSERT(callbackContext);
149
Robert Phillips4508eb92020-04-15 15:54:34 -0400150 // DDL TODO: what should we do with mipmapped YUV images
Brian Salomonbd3792d2020-11-10 14:17:58 -0500151 callbackContext->setBackendTexture(create_yuva_texture(direct, yuvPixmap, j));
Robert Phillips923181b2020-02-14 12:36:37 -0500152 SkASSERT(callbackContext->promiseImageTexture());
153 }
154 } else {
155 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
156 if (!callbackContext) {
157 // This texture would've been too large to fit on the GPU
158 return;
159 }
160
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400161 std::unique_ptr<SkPixmap[]> mipLevels = info->normalMipLevels();
Robert Phillips923181b2020-02-14 12:36:37 -0500162
Greg Danielc1ad77c2020-05-06 11:40:03 -0400163 bool finishedBECreate = false;
164 auto markFinished = [](void* context) {
165 *(bool*)context = true;
166 };
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400167 auto backendTex = direct->createBackendTexture(mipLevels.get(), info->numMipLevels(),
168 GrRenderable::kNo, GrProtected::kNo,
169 markFinished, &finishedBECreate);
Robert Phillips923181b2020-02-14 12:36:37 -0500170 SkASSERT(backendTex.isValid());
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400171 direct->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400172 while (!finishedBECreate) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400173 direct->checkAsyncWorkCompletion();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400174 }
Robert Phillips923181b2020-02-14 12:36:37 -0500175
176 callbackContext->setBackendTexture(backendTex);
177 }
178}
179
Brian Salomonbd3792d2020-11-10 14:17:58 -0500180void DDLPromiseImageHelper::DeleteBETexturesForPromiseImage(PromiseImageInfo* info) {
Robert Phillips19f466d2020-02-26 10:27:07 -0500181 if (info->isYUV()) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500182 int numPixmaps = info->yuvaInfo().numPlanes();
Robert Phillips19f466d2020-02-26 10:27:07 -0500183 for (int j = 0; j < numPixmaps; ++j) {
184 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
185 SkASSERT(callbackContext);
186
187 callbackContext->destroyBackendTexture();
188 SkASSERT(!callbackContext->promiseImageTexture());
189 }
190 } else {
191 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
192 if (!callbackContext) {
193 // This texture would've been too large to fit on the GPU
194 return;
195 }
196
197 callbackContext->destroyBackendTexture();
198 SkASSERT(!callbackContext->promiseImageTexture());
199 }
200}
201
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400202void DDLPromiseImageHelper::createCallbackContexts(GrDirectContext* direct) {
203 const GrCaps* caps = direct->priv().caps();
Robert Phillips923181b2020-02-14 12:36:37 -0500204 const int maxDimension = caps->maxTextureSize();
205
206 for (int i = 0; i < fImageInfo.count(); ++i) {
207 PromiseImageInfo& info = fImageInfo[i];
208
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400209 if (info.isYUV()) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500210 int numPixmaps = info.yuvaInfo().numPlanes();
Robert Phillips923181b2020-02-14 12:36:37 -0500211
Jim Van Verth8f11e432018-10-18 14:36:59 -0400212 for (int j = 0; j < numPixmaps; ++j) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400213 const SkPixmap& yuvPixmap = info.yuvPixmap(j);
Robert Phillips96601082018-05-29 16:13:26 -0400214
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400215 GrBackendFormat backendFormat = direct->defaultBackendFormat(yuvPixmap.colorType(),
216 GrRenderable::kNo);
Robert Phillips923181b2020-02-14 12:36:37 -0500217
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400218 sk_sp<PromiseImageCallbackContext> callbackContext(
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400219 new PromiseImageCallbackContext(direct, backendFormat));
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500220
Robert Phillips923181b2020-02-14 12:36:37 -0500221 info.setCallbackContext(j, std::move(callbackContext));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400222 }
223 } else {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400224 const SkBitmap& baseLevel = info.baseLevel();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400225
Robert Phillips923181b2020-02-14 12:36:37 -0500226 // TODO: explicitly mark the PromiseImageInfo as too big and check in uploadAllToGPU
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400227 if (maxDimension < std::max(baseLevel.width(), baseLevel.height())) {
Robert Phillips923181b2020-02-14 12:36:37 -0500228 // This won't fit on the GPU. Fallback to a raster-backed image per tile.
229 continue;
230 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400231
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400232 GrBackendFormat backendFormat = direct->defaultBackendFormat(baseLevel.colorType(),
233 GrRenderable::kNo);
Robert Phillips923181b2020-02-14 12:36:37 -0500234 if (!caps->isFormatTexturable(backendFormat)) {
235 continue;
236 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400237
Robert Phillips923181b2020-02-14 12:36:37 -0500238 sk_sp<PromiseImageCallbackContext> callbackContext(
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400239 new PromiseImageCallbackContext(direct, backendFormat));
Robert Phillips923181b2020-02-14 12:36:37 -0500240
241 info.setCallbackContext(0, std::move(callbackContext));
242 }
243 }
244}
245
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400246void DDLPromiseImageHelper::uploadAllToGPU(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips923181b2020-02-14 12:36:37 -0500247 if (taskGroup) {
248 for (int i = 0; i < fImageInfo.count(); ++i) {
249 PromiseImageInfo* info = &fImageInfo[i];
250
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400251 taskGroup->add([direct, info]() { CreateBETexturesForPromiseImage(direct, info); });
Robert Phillips923181b2020-02-14 12:36:37 -0500252 }
253 } else {
254 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400255 CreateBETexturesForPromiseImage(direct, &fImageInfo[i]);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400256 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500257 }
258}
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400259
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400260void DDLPromiseImageHelper::deleteAllFromGPU(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips19f466d2020-02-26 10:27:07 -0500261 if (taskGroup) {
262 for (int i = 0; i < fImageInfo.count(); ++i) {
263 PromiseImageInfo* info = &fImageInfo[i];
264
Brian Salomonbd3792d2020-11-10 14:17:58 -0500265 taskGroup->add([info]() { DeleteBETexturesForPromiseImage(info); });
Robert Phillips19f466d2020-02-26 10:27:07 -0500266 }
267 } else {
268 for (int i = 0; i < fImageInfo.count(); ++i) {
Brian Salomonbd3792d2020-11-10 14:17:58 -0500269 DeleteBETexturesForPromiseImage(&fImageInfo[i]);
Robert Phillips19f466d2020-02-26 10:27:07 -0500270 }
271 }
272}
273
Robert Phillips96601082018-05-29 16:13:26 -0400274sk_sp<SkPicture> DDLPromiseImageHelper::reinflateSKP(
275 SkDeferredDisplayListRecorder* recorder,
276 SkData* compressedPictureData,
277 SkTArray<sk_sp<SkImage>>* promiseImages) const {
278 PerRecorderContext perRecorderContext { recorder, this, promiseImages };
279
280 SkDeserialProcs procs;
281 procs.fImageCtx = (void*) &perRecorderContext;
Robert Phillips923181b2020-02-14 12:36:37 -0500282 procs.fImageProc = CreatePromiseImages;
Robert Phillips96601082018-05-29 16:13:26 -0400283
284 return SkPicture::MakeFromData(compressedPictureData, &procs);
285}
286
287// This generates promise images to replace the indices in the compressed picture. This
288// reconstitution is performed separately in each thread so we end up with multiple
289// promise images referring to the same GrBackendTexture.
Robert Phillips923181b2020-02-14 12:36:37 -0500290sk_sp<SkImage> DDLPromiseImageHelper::CreatePromiseImages(const void* rawData,
Robert Phillips96601082018-05-29 16:13:26 -0400291 size_t length, void* ctxIn) {
292 PerRecorderContext* perRecorderContext = static_cast<PerRecorderContext*>(ctxIn);
293 const DDLPromiseImageHelper* helper = perRecorderContext->fHelper;
294 SkDeferredDisplayListRecorder* recorder = perRecorderContext->fRecorder;
295
296 SkASSERT(length == sizeof(int));
297
298 const int* indexPtr = static_cast<const int*>(rawData);
Robert Phillips6bad7052019-12-16 15:09:57 -0500299 if (!helper->isValidID(*indexPtr)) {
300 return nullptr;
301 }
Robert Phillips96601082018-05-29 16:13:26 -0400302
303 const DDLPromiseImageHelper::PromiseImageInfo& curImage = helper->getInfo(*indexPtr);
304
Robert Phillips923181b2020-02-14 12:36:37 -0500305 // If there is no callback context that means 'createCallbackContexts' determined the
306 // texture wouldn't fit on the GPU. Create a separate bitmap-backed image for each thread.
307 if (!curImage.isYUV() && !curImage.callbackContext(0)) {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400308 SkASSERT(curImage.baseLevel().isImmutable());
309 return SkImage::MakeFromBitmap(curImage.baseLevel());
Robert Phillips96601082018-05-29 16:13:26 -0400310 }
Robert Phillips923181b2020-02-14 12:36:37 -0500311
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400312 SkASSERT(curImage.index() == *indexPtr);
Robert Phillips96601082018-05-29 16:13:26 -0400313
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400314 sk_sp<SkImage> image;
315 if (curImage.isYUV()) {
Jim Van Verthe24b5872018-10-29 16:26:02 -0400316 GrBackendFormat backendFormats[SkYUVASizeInfo::kMaxCount];
Brian Salomonbd3792d2020-11-10 14:17:58 -0500317 const SkYUVAInfo& yuvaInfo = curImage.yuvaInfo();
Jim Van Verthe24b5872018-10-29 16:26:02 -0400318 void* contexts[SkYUVASizeInfo::kMaxCount] = { nullptr, nullptr, nullptr, nullptr };
Brian Salomonbd3792d2020-11-10 14:17:58 -0500319 int textureCount = yuvaInfo.numPlanes();
Jim Van Verth8f11e432018-10-18 14:36:59 -0400320 for (int i = 0; i < textureCount; ++i) {
Robert Phillips923181b2020-02-14 12:36:37 -0500321 backendFormats[i] = curImage.backendFormat(i);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400322 contexts[i] = curImage.refCallbackContext(i).release();
323 }
Brian Salomonbd3792d2020-11-10 14:17:58 -0500324 GrYUVABackendTextureInfo yuvaBackendTextures(yuvaInfo,
325 backendFormats,
326 GrMipmapped::kNo,
327 kTopLeft_GrSurfaceOrigin);
Jim Van Verthf99a6742018-10-18 16:13:18 +0000328
Brian Salomon0cc57542019-03-08 13:28:46 -0500329 image = recorder->makeYUVAPromiseTexture(
Brian Salomonbd3792d2020-11-10 14:17:58 -0500330 yuvaBackendTextures,
Brian Salomon0cc57542019-03-08 13:28:46 -0500331 curImage.refOverallColorSpace(),
Robert Phillips11c67672020-04-23 15:10:03 -0400332 PromiseImageCallbackContext::PromiseImageFulfillProc,
333 PromiseImageCallbackContext::PromiseImageReleaseProc,
Brian Salomonf1432742020-11-09 15:40:27 -0500334 contexts);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500335 for (int i = 0; i < textureCount; ++i) {
336 curImage.callbackContext(i)->wasAddedToImage();
337 }
Robert Phillips193c4212019-03-04 12:18:53 -0500338
339#ifdef SK_DEBUG
340 {
341 // By the peekProxy contract this image should not have a single backing proxy so
342 // should return null. The call should also not trigger the conversion to RGBA.
343 SkImage_GpuYUVA* yuva = reinterpret_cast<SkImage_GpuYUVA*>(image.get());
344 SkASSERT(!yuva->peekProxy());
345 SkASSERT(!yuva->peekProxy()); // the first call didn't force a conversion to RGBA
346 }
347#endif
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400348 } else {
John Stiles31954bf2020-08-07 17:35:54 -0400349 const GrBackendFormat& backendFormat = curImage.backendFormat(0);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500350 SkASSERT(backendFormat.isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400351
352 // Each DDL recorder gets its own ref on the promise callback context for the
353 // promise images it creates.
Brian Salomonf1432742020-11-09 15:40:27 -0500354 image = recorder->makePromiseTexture(backendFormat,
355 curImage.overallWidth(),
356 curImage.overallHeight(),
357 curImage.mipMapped(0),
358 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
359 curImage.overallColorType(),
360 curImage.overallAlphaType(),
361 curImage.refOverallColorSpace(),
362 PromiseImageCallbackContext::PromiseImageFulfillProc,
363 PromiseImageCallbackContext::PromiseImageReleaseProc,
364 (void*)curImage.refCallbackContext(0).release());
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500365 curImage.callbackContext(0)->wasAddedToImage();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400366 }
Robert Phillips96601082018-05-29 16:13:26 -0400367 perRecorderContext->fPromiseImages->push_back(image);
368 SkASSERT(image);
369 return image;
370}
371
372int DDLPromiseImageHelper::findImage(SkImage* image) const {
373 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400374 if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here
375 SkASSERT(fImageInfo[i].index() == i);
376 SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index()));
Robert Phillips96601082018-05-29 16:13:26 -0400377 return i;
378 }
379 }
380 return -1;
381}
382
383int DDLPromiseImageHelper::addImage(SkImage* image) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400384 SkImage_Base* ib = as_IB(image);
Robert Phillips96601082018-05-29 16:13:26 -0400385
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400386 SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(),
Robert Phillips13371a12019-05-13 15:59:10 -0400387 image->colorType() == kBGRA_8888_SkColorType
388 ? kRGBA_8888_SkColorType
389 : image->colorType(),
390 image->alphaType(),
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400391 image->refColorSpace());
Robert Phillips96601082018-05-29 16:13:26 -0400392
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400393 PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.count(),
394 image->uniqueID(),
395 overallII);
Robert Phillips96601082018-05-29 16:13:26 -0400396
Brian Salomonefb5f072020-07-28 21:06:43 -0400397 auto codec = SkCodecImageGenerator::MakeFromEncodedCodec(ib->refEncodedData());
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400398 SkYUVAPixmapInfo yuvaInfo;
Brian Salomon59c60b02020-09-01 15:01:15 -0400399 if (codec && codec->queryYUVAInfo(fSupportedYUVADataTypes, &yuvaInfo)) {
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400400 auto yuvaPixmaps = SkYUVAPixmaps::Allocate(yuvaInfo);
401 SkAssertResult(codec->getYUVAPlanes(yuvaPixmaps));
Brian Salomon5660e8b2020-08-25 12:40:32 -0400402 SkASSERT(yuvaPixmaps.isValid());
403 newImageInfo.setYUVPlanes(std::move(yuvaPixmaps));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400404 } else {
405 sk_sp<SkImage> rasterImage = image->makeRasterImage(); // force decoding of lazy images
Robert Phillipse84bffc2019-12-16 11:22:17 -0500406 if (!rasterImage) {
407 return -1;
408 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400409
410 SkBitmap tmp;
411 tmp.allocPixels(overallII);
412
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400413 if (!rasterImage->readPixels(nullptr, tmp.pixmap(), 0, 0)) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400414 return -1;
415 }
416
417 tmp.setImmutable();
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400418
419 // Given how the DDL testing harness works (i.e., only modifying the SkImages w/in an
420 // SKP) we don't know if a given SkImage will require mipmapping. To work around this
421 // we just try to create all the backend textures as mipmapped but, failing that, fall
422 // back to un-mipped.
Mike Reed13711eb2020-07-14 17:16:32 -0400423 std::unique_ptr<SkMipmap> mipmaps(SkMipmap::Build(tmp.pixmap(), nullptr));
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400424
425 newImageInfo.setMipLevels(tmp, std::move(mipmaps));
Robert Phillips96601082018-05-29 16:13:26 -0400426 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400427 // In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU
Robert Phillips96601082018-05-29 16:13:26 -0400428
429 return fImageInfo.count()-1;
430}
431
432int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) {
433 int preExistingID = this->findImage(image);
434 if (preExistingID >= 0) {
435 SkASSERT(this->isValidID(preExistingID));
436 return preExistingID;
437 }
438
439 int newID = this->addImage(image);
Robert Phillips96601082018-05-29 16:13:26 -0400440 return newID;
441}