blob: e17dd1aec3a26a60b7709be7b8b4dc42c6c4f740 [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"
15#include "include/gpu/GrContext.h"
16#include "src/core/SkCachedData.h"
Robert Phillips4508eb92020-04-15 15:54:34 -040017#include "src/core/SkMipMap.h"
Robert Phillips923181b2020-02-14 12:36:37 -050018#include "src/core/SkTaskGroup.h"
19#include "src/gpu/GrContextPriv.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))
37 , fYUVData(std::move(other.fYUVData))
38 , fYUVColorSpace(other.fYUVColorSpace) {
39 memcpy(fYUVAIndices, other.fYUVAIndices, sizeof(fYUVAIndices));
40 for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) {
41 fYUVPlanes[i] = other.fYUVPlanes[i];
42 fCallbackContexts[i] = std::move(other.fCallbackContexts[i]);
43 }
44}
45
46DDLPromiseImageHelper::PromiseImageInfo::~PromiseImageInfo() {}
47
48const std::unique_ptr<SkPixmap[]> DDLPromiseImageHelper::PromiseImageInfo::normalMipLevels() const {
49 SkASSERT(!this->isYUV());
50 std::unique_ptr<SkPixmap[]> pixmaps(new SkPixmap[this->numMipLevels()]);
51 pixmaps[0] = fBaseLevel.pixmap();
52 if (fMipLevels) {
53 for (int i = 0; i < fMipLevels->countLevels(); ++i) {
54 SkMipMap::Level mipLevel;
55 fMipLevels->getLevel(i, &mipLevel);
56 pixmaps[i+1] = mipLevel.fPixmap;
57 }
58 }
59 return pixmaps;
60}
61
62int DDLPromiseImageHelper::PromiseImageInfo::numMipLevels() const {
63 SkASSERT(!this->isYUV());
64 return fMipLevels ? fMipLevels->countLevels()+1 : 1;
65}
66
67void DDLPromiseImageHelper::PromiseImageInfo::setMipLevels(const SkBitmap& baseLevel,
68 std::unique_ptr<SkMipMap> mipLevels) {
69 fBaseLevel = baseLevel;
70 fMipLevels = std::move(mipLevels);
71}
72
73///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips11c67672020-04-23 15:10:03 -040074PromiseImageCallbackContext::~PromiseImageCallbackContext() {
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050075 SkASSERT(fDoneCnt == fNumImages);
76 SkASSERT(!fUnreleasedFulfills);
77 SkASSERT(fTotalReleases == fTotalFulfills);
78 SkASSERT(!fTotalFulfills || fDoneCnt);
Robert Phillips96601082018-05-29 16:13:26 -040079
Brian Salomon3f4cd772019-01-11 16:03:19 -050080 if (fPromiseImageTexture) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -040081 fContext->deleteBackendTexture(fPromiseImageTexture->backendTexture());
Robert Phillips96601082018-05-29 16:13:26 -040082 }
83}
84
Robert Phillips11c67672020-04-23 15:10:03 -040085void PromiseImageCallbackContext::setBackendTexture(const GrBackendTexture& backendTexture) {
Brian Salomon7d88f312019-02-28 10:03:03 -050086 SkASSERT(!fPromiseImageTexture);
Robert Phillips923181b2020-02-14 12:36:37 -050087 SkASSERT(fBackendFormat == backendTexture.getBackendFormat());
Brian Salomon3f4cd772019-01-11 16:03:19 -050088 fPromiseImageTexture = SkPromiseImageTexture::Make(backendTexture);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050089}
90
Robert Phillips96601082018-05-29 16:13:26 -040091///////////////////////////////////////////////////////////////////////////////////////////////////
92
93sk_sp<SkData> DDLPromiseImageHelper::deflateSKP(const SkPicture* inputPicture) {
94 SkSerialProcs procs;
95
96 procs.fImageCtx = this;
97 procs.fImageProc = [](SkImage* image, void* ctx) -> sk_sp<SkData> {
98 auto helper = static_cast<DDLPromiseImageHelper*>(ctx);
99
100 int id = helper->findOrDefineImage(image);
Robert Phillips96601082018-05-29 16:13:26 -0400101
Robert Phillips6bad7052019-12-16 15:09:57 -0500102 // Even if 'id' is invalid (i.e., -1) write it to the SKP
103 return SkData::MakeWithCopy(&id, sizeof(id));
Robert Phillips96601082018-05-29 16:13:26 -0400104 };
105
106 return inputPicture->serialize(&procs);
107}
108
Robert Phillipscb1adb42019-06-10 15:09:34 -0400109static GrBackendTexture create_yuva_texture(GrContext* context, const SkPixmap& pm,
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500110 const SkYUVAIndex yuvaIndices[4], int texIndex) {
111 SkASSERT(texIndex >= 0 && texIndex <= 3);
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400112
113#ifdef SK_DEBUG
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500114 int channelCount = 0;
115 for (int i = 0; i < SkYUVAIndex::kIndexCount; ++i) {
116 if (yuvaIndices[i].fIndex == texIndex) {
117 ++channelCount;
118 }
119 }
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500120 if (2 == channelCount) {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400121 SkASSERT(kR8G8_unorm_SkColorType == pm.colorType());
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500122 }
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400123#endif
Greg Danielc1ad77c2020-05-06 11:40:03 -0400124 bool finishedBECreate = false;
125 auto markFinished = [](void* context) {
126 *(bool*)context = true;
127 };
128 auto beTex = context->createBackendTexture(&pm, 1, GrRenderable::kNo, GrProtected::kNo,
129 markFinished, &finishedBECreate);
130 if (beTex.isValid()) {
Greg Danielb5776552020-06-19 20:21:08 -0400131 context->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400132 while (!finishedBECreate) {
133 context->checkAsyncWorkCompletion();
134 }
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 */
144void DDLPromiseImageHelper::CreateBETexturesForPromiseImage(GrContext* context,
145 PromiseImageInfo* info) {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400146 SkASSERT(context->asDirectContext());
Robert Phillips96601082018-05-29 16:13:26 -0400147
Robert Phillips923181b2020-02-14 12:36:37 -0500148 if (info->isYUV()) {
149 int numPixmaps;
150 SkAssertResult(SkYUVAIndex::AreValidIndices(info->yuvaIndices(), &numPixmaps));
151 for (int j = 0; j < numPixmaps; ++j) {
152 const SkPixmap& yuvPixmap = info->yuvPixmap(j);
153
154 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
155 SkASSERT(callbackContext);
156
Robert Phillips4508eb92020-04-15 15:54:34 -0400157 // DDL TODO: what should we do with mipmapped YUV images
Robert Phillips923181b2020-02-14 12:36:37 -0500158 callbackContext->setBackendTexture(create_yuva_texture(context, yuvPixmap,
159 info->yuvaIndices(), j));
160 SkASSERT(callbackContext->promiseImageTexture());
161 }
162 } else {
163 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
164 if (!callbackContext) {
165 // This texture would've been too large to fit on the GPU
166 return;
167 }
168
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400169 std::unique_ptr<SkPixmap[]> mipLevels = info->normalMipLevels();
Robert Phillips923181b2020-02-14 12:36:37 -0500170
Greg Danielc1ad77c2020-05-06 11:40:03 -0400171 bool finishedBECreate = false;
172 auto markFinished = [](void* context) {
173 *(bool*)context = true;
174 };
175 auto backendTex = context->createBackendTexture(mipLevels.get(), info->numMipLevels(),
176 GrRenderable::kNo, GrProtected::kNo,
177 markFinished, &finishedBECreate);
Robert Phillips923181b2020-02-14 12:36:37 -0500178 SkASSERT(backendTex.isValid());
Greg Danielb5776552020-06-19 20:21:08 -0400179 context->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400180 while (!finishedBECreate) {
181 context->checkAsyncWorkCompletion();
182 }
Robert Phillips923181b2020-02-14 12:36:37 -0500183
184 callbackContext->setBackendTexture(backendTex);
185 }
186}
187
Robert Phillips19f466d2020-02-26 10:27:07 -0500188void DDLPromiseImageHelper::DeleteBETexturesForPromiseImage(GrContext* context,
189 PromiseImageInfo* info) {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400190 SkASSERT(context->asDirectContext());
Robert Phillips19f466d2020-02-26 10:27:07 -0500191
192 if (info->isYUV()) {
193 int numPixmaps;
194 SkAssertResult(SkYUVAIndex::AreValidIndices(info->yuvaIndices(), &numPixmaps));
195 for (int j = 0; j < numPixmaps; ++j) {
196 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
197 SkASSERT(callbackContext);
198
199 callbackContext->destroyBackendTexture();
200 SkASSERT(!callbackContext->promiseImageTexture());
201 }
202 } else {
203 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
204 if (!callbackContext) {
205 // This texture would've been too large to fit on the GPU
206 return;
207 }
208
209 callbackContext->destroyBackendTexture();
210 SkASSERT(!callbackContext->promiseImageTexture());
211 }
212}
213
Robert Phillips923181b2020-02-14 12:36:37 -0500214void DDLPromiseImageHelper::createCallbackContexts(GrContext* context) {
215 const GrCaps* caps = context->priv().caps();
216 const int maxDimension = caps->maxTextureSize();
217
218 for (int i = 0; i < fImageInfo.count(); ++i) {
219 PromiseImageInfo& info = fImageInfo[i];
220
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400221 if (info.isYUV()) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400222 int numPixmaps;
223 SkAssertResult(SkYUVAIndex::AreValidIndices(info.yuvaIndices(), &numPixmaps));
Robert Phillips923181b2020-02-14 12:36:37 -0500224
Jim Van Verth8f11e432018-10-18 14:36:59 -0400225 for (int j = 0; j < numPixmaps; ++j) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400226 const SkPixmap& yuvPixmap = info.yuvPixmap(j);
Robert Phillips96601082018-05-29 16:13:26 -0400227
Robert Phillips923181b2020-02-14 12:36:37 -0500228 GrBackendFormat backendFormat = context->defaultBackendFormat(yuvPixmap.colorType(),
229 GrRenderable::kNo);
230
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400231 sk_sp<PromiseImageCallbackContext> callbackContext(
Robert Phillips923181b2020-02-14 12:36:37 -0500232 new PromiseImageCallbackContext(context, backendFormat));
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500233
Robert Phillips923181b2020-02-14 12:36:37 -0500234 info.setCallbackContext(j, std::move(callbackContext));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400235 }
236 } else {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400237 const SkBitmap& baseLevel = info.baseLevel();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400238
Robert Phillips923181b2020-02-14 12:36:37 -0500239 // TODO: explicitly mark the PromiseImageInfo as too big and check in uploadAllToGPU
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400240 if (maxDimension < std::max(baseLevel.width(), baseLevel.height())) {
Robert Phillips923181b2020-02-14 12:36:37 -0500241 // This won't fit on the GPU. Fallback to a raster-backed image per tile.
242 continue;
243 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400244
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400245 GrBackendFormat backendFormat = context->defaultBackendFormat(baseLevel.colorType(),
Robert Phillips923181b2020-02-14 12:36:37 -0500246 GrRenderable::kNo);
247 if (!caps->isFormatTexturable(backendFormat)) {
248 continue;
249 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400250
Robert Phillips923181b2020-02-14 12:36:37 -0500251 sk_sp<PromiseImageCallbackContext> callbackContext(
252 new PromiseImageCallbackContext(context, backendFormat));
253
254 info.setCallbackContext(0, std::move(callbackContext));
255 }
256 }
257}
258
259void DDLPromiseImageHelper::uploadAllToGPU(SkTaskGroup* taskGroup, GrContext* context) {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400260 SkASSERT(context->asDirectContext());
Robert Phillips923181b2020-02-14 12:36:37 -0500261
262 if (taskGroup) {
263 for (int i = 0; i < fImageInfo.count(); ++i) {
264 PromiseImageInfo* info = &fImageInfo[i];
265
Robert Phillips19f466d2020-02-26 10:27:07 -0500266 taskGroup->add([context, info]() { CreateBETexturesForPromiseImage(context, info); });
Robert Phillips923181b2020-02-14 12:36:37 -0500267 }
268 } else {
269 for (int i = 0; i < fImageInfo.count(); ++i) {
270 CreateBETexturesForPromiseImage(context, &fImageInfo[i]);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400271 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500272 }
273}
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400274
Robert Phillips19f466d2020-02-26 10:27:07 -0500275void DDLPromiseImageHelper::deleteAllFromGPU(SkTaskGroup* taskGroup, GrContext* context) {
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400276 SkASSERT(context->asDirectContext());
Robert Phillips19f466d2020-02-26 10:27:07 -0500277
278 if (taskGroup) {
279 for (int i = 0; i < fImageInfo.count(); ++i) {
280 PromiseImageInfo* info = &fImageInfo[i];
281
282 taskGroup->add([context, info]() { DeleteBETexturesForPromiseImage(context, info); });
283 }
284 } else {
285 for (int i = 0; i < fImageInfo.count(); ++i) {
286 DeleteBETexturesForPromiseImage(context, &fImageInfo[i]);
287 }
288 }
289}
290
Robert Phillips96601082018-05-29 16:13:26 -0400291sk_sp<SkPicture> DDLPromiseImageHelper::reinflateSKP(
292 SkDeferredDisplayListRecorder* recorder,
293 SkData* compressedPictureData,
294 SkTArray<sk_sp<SkImage>>* promiseImages) const {
295 PerRecorderContext perRecorderContext { recorder, this, promiseImages };
296
297 SkDeserialProcs procs;
298 procs.fImageCtx = (void*) &perRecorderContext;
Robert Phillips923181b2020-02-14 12:36:37 -0500299 procs.fImageProc = CreatePromiseImages;
Robert Phillips96601082018-05-29 16:13:26 -0400300
301 return SkPicture::MakeFromData(compressedPictureData, &procs);
302}
303
304// This generates promise images to replace the indices in the compressed picture. This
305// reconstitution is performed separately in each thread so we end up with multiple
306// promise images referring to the same GrBackendTexture.
Robert Phillips923181b2020-02-14 12:36:37 -0500307sk_sp<SkImage> DDLPromiseImageHelper::CreatePromiseImages(const void* rawData,
Robert Phillips96601082018-05-29 16:13:26 -0400308 size_t length, void* ctxIn) {
309 PerRecorderContext* perRecorderContext = static_cast<PerRecorderContext*>(ctxIn);
310 const DDLPromiseImageHelper* helper = perRecorderContext->fHelper;
311 SkDeferredDisplayListRecorder* recorder = perRecorderContext->fRecorder;
312
313 SkASSERT(length == sizeof(int));
314
315 const int* indexPtr = static_cast<const int*>(rawData);
Robert Phillips6bad7052019-12-16 15:09:57 -0500316 if (!helper->isValidID(*indexPtr)) {
317 return nullptr;
318 }
Robert Phillips96601082018-05-29 16:13:26 -0400319
320 const DDLPromiseImageHelper::PromiseImageInfo& curImage = helper->getInfo(*indexPtr);
321
Robert Phillips923181b2020-02-14 12:36:37 -0500322 // If there is no callback context that means 'createCallbackContexts' determined the
323 // texture wouldn't fit on the GPU. Create a separate bitmap-backed image for each thread.
324 if (!curImage.isYUV() && !curImage.callbackContext(0)) {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400325 SkASSERT(curImage.baseLevel().isImmutable());
326 return SkImage::MakeFromBitmap(curImage.baseLevel());
Robert Phillips96601082018-05-29 16:13:26 -0400327 }
Robert Phillips923181b2020-02-14 12:36:37 -0500328
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400329 SkASSERT(curImage.index() == *indexPtr);
Robert Phillips96601082018-05-29 16:13:26 -0400330
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400331 sk_sp<SkImage> image;
332 if (curImage.isYUV()) {
Jim Van Verthe24b5872018-10-29 16:26:02 -0400333 GrBackendFormat backendFormats[SkYUVASizeInfo::kMaxCount];
334 void* contexts[SkYUVASizeInfo::kMaxCount] = { nullptr, nullptr, nullptr, nullptr };
335 SkISize sizes[SkYUVASizeInfo::kMaxCount];
Jim Van Verth8f11e432018-10-18 14:36:59 -0400336 // TODO: store this value somewhere?
337 int textureCount;
338 SkAssertResult(SkYUVAIndex::AreValidIndices(curImage.yuvaIndices(), &textureCount));
339 for (int i = 0; i < textureCount; ++i) {
Robert Phillips923181b2020-02-14 12:36:37 -0500340 backendFormats[i] = curImage.backendFormat(i);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500341 SkASSERT(backendFormats[i].isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400342 contexts[i] = curImage.refCallbackContext(i).release();
Jim Van Verthf9f07352018-10-24 10:32:20 -0400343 sizes[i].set(curImage.yuvPixmap(i).width(), curImage.yuvPixmap(i).height());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400344 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400345 for (int i = textureCount; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verthf9f07352018-10-24 10:32:20 -0400346 sizes[i] = SkISize::MakeEmpty();
Jim Van Verth8f11e432018-10-18 14:36:59 -0400347 }
Jim Van Verthf99a6742018-10-18 16:13:18 +0000348
Brian Salomon0cc57542019-03-08 13:28:46 -0500349 image = recorder->makeYUVAPromiseTexture(
350 curImage.yuvColorSpace(),
351 backendFormats,
352 sizes,
353 curImage.yuvaIndices(),
354 curImage.overallWidth(),
355 curImage.overallHeight(),
356 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
357 curImage.refOverallColorSpace(),
Robert Phillips11c67672020-04-23 15:10:03 -0400358 PromiseImageCallbackContext::PromiseImageFulfillProc,
359 PromiseImageCallbackContext::PromiseImageReleaseProc,
360 PromiseImageCallbackContext::PromiseImageDoneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500361 contexts,
362 SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500363 for (int i = 0; i < textureCount; ++i) {
364 curImage.callbackContext(i)->wasAddedToImage();
365 }
Robert Phillips193c4212019-03-04 12:18:53 -0500366
367#ifdef SK_DEBUG
368 {
369 // By the peekProxy contract this image should not have a single backing proxy so
370 // should return null. The call should also not trigger the conversion to RGBA.
371 SkImage_GpuYUVA* yuva = reinterpret_cast<SkImage_GpuYUVA*>(image.get());
372 SkASSERT(!yuva->peekProxy());
373 SkASSERT(!yuva->peekProxy()); // the first call didn't force a conversion to RGBA
374 }
375#endif
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400376 } else {
Robert Phillips923181b2020-02-14 12:36:37 -0500377 GrBackendFormat backendFormat = curImage.backendFormat(0);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500378 SkASSERT(backendFormat.isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400379
380 // Each DDL recorder gets its own ref on the promise callback context for the
381 // promise images it creates.
Brian Salomon0cc57542019-03-08 13:28:46 -0500382 image = recorder->makePromiseTexture(
383 backendFormat,
384 curImage.overallWidth(),
385 curImage.overallHeight(),
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400386 curImage.mipMapped(0),
Brian Salomon0cc57542019-03-08 13:28:46 -0500387 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
388 curImage.overallColorType(),
389 curImage.overallAlphaType(),
390 curImage.refOverallColorSpace(),
Robert Phillips11c67672020-04-23 15:10:03 -0400391 PromiseImageCallbackContext::PromiseImageFulfillProc,
392 PromiseImageCallbackContext::PromiseImageReleaseProc,
393 PromiseImageCallbackContext::PromiseImageDoneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500394 (void*)curImage.refCallbackContext(0).release(),
395 SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500396 curImage.callbackContext(0)->wasAddedToImage();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400397 }
Robert Phillips96601082018-05-29 16:13:26 -0400398 perRecorderContext->fPromiseImages->push_back(image);
399 SkASSERT(image);
400 return image;
401}
402
403int DDLPromiseImageHelper::findImage(SkImage* image) const {
404 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400405 if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here
406 SkASSERT(fImageInfo[i].index() == i);
407 SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index()));
Robert Phillips96601082018-05-29 16:13:26 -0400408 return i;
409 }
410 }
411 return -1;
412}
413
414int DDLPromiseImageHelper::addImage(SkImage* image) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400415 SkImage_Base* ib = as_IB(image);
Robert Phillips96601082018-05-29 16:13:26 -0400416
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400417 SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(),
Robert Phillips13371a12019-05-13 15:59:10 -0400418 image->colorType() == kBGRA_8888_SkColorType
419 ? kRGBA_8888_SkColorType
420 : image->colorType(),
421 image->alphaType(),
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400422 image->refColorSpace());
Robert Phillips96601082018-05-29 16:13:26 -0400423
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400424 PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.count(),
425 image->uniqueID(),
426 overallII);
Robert Phillips96601082018-05-29 16:13:26 -0400427
Jim Van Verthe24b5872018-10-29 16:26:02 -0400428 SkYUVASizeInfo yuvaSizeInfo;
Jim Van Verth8f11e432018-10-18 14:36:59 -0400429 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount];
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400430 SkYUVColorSpace yuvColorSpace;
Jim Van Verthe24b5872018-10-29 16:26:02 -0400431 const void* planes[SkYUVASizeInfo::kMaxCount];
Jim Van Verth8f11e432018-10-18 14:36:59 -0400432 sk_sp<SkCachedData> yuvData = ib->getPlanes(&yuvaSizeInfo, yuvaIndices, &yuvColorSpace, planes);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400433 if (yuvData) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400434 newImageInfo.setYUVData(std::move(yuvData), yuvaIndices, yuvColorSpace);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400435
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400436 // determine colortypes from index data
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400437 // for testing we only ever use A8, RG_88
Jim Van Verthe24b5872018-10-29 16:26:02 -0400438 SkColorType colorTypes[SkYUVASizeInfo::kMaxCount] = {
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400439 kUnknown_SkColorType, kUnknown_SkColorType,
440 kUnknown_SkColorType, kUnknown_SkColorType
441 };
442 for (int yuvIndex = 0; yuvIndex < SkYUVAIndex::kIndexCount; ++yuvIndex) {
443 int texIdx = yuvaIndices[yuvIndex].fIndex;
444 if (texIdx < 0) {
445 SkASSERT(SkYUVAIndex::kA_Index == yuvIndex);
446 continue;
447 }
448 if (kUnknown_SkColorType == colorTypes[texIdx]) {
449 colorTypes[texIdx] = kAlpha_8_SkColorType;
450 } else {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400451 colorTypes[texIdx] = kR8G8_unorm_SkColorType;
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400452 }
453 }
454
Jim Van Verthe24b5872018-10-29 16:26:02 -0400455 for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400456 if (yuvaSizeInfo.fSizes[i].isEmpty()) {
457 SkASSERT(!yuvaSizeInfo.fWidthBytes[i] && kUnknown_SkColorType == colorTypes[i]);
Jim Van Verth8f11e432018-10-18 14:36:59 -0400458 continue;
459 }
460
461 SkImageInfo planeII = SkImageInfo::Make(yuvaSizeInfo.fSizes[i].fWidth,
462 yuvaSizeInfo.fSizes[i].fHeight,
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400463 colorTypes[i],
Jim Van Verth8f11e432018-10-18 14:36:59 -0400464 kUnpremul_SkAlphaType);
465 newImageInfo.addYUVPlane(i, planeII, planes[i], yuvaSizeInfo.fWidthBytes[i]);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400466 }
467 } else {
468 sk_sp<SkImage> rasterImage = image->makeRasterImage(); // force decoding of lazy images
Robert Phillipse84bffc2019-12-16 11:22:17 -0500469 if (!rasterImage) {
470 return -1;
471 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400472
473 SkBitmap tmp;
474 tmp.allocPixels(overallII);
475
476 if (!rasterImage->readPixels(tmp.pixmap(), 0, 0)) {
477 return -1;
478 }
479
480 tmp.setImmutable();
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400481
482 // Given how the DDL testing harness works (i.e., only modifying the SkImages w/in an
483 // SKP) we don't know if a given SkImage will require mipmapping. To work around this
484 // we just try to create all the backend textures as mipmapped but, failing that, fall
485 // back to un-mipped.
486 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(tmp.pixmap(), nullptr));
487
488 newImageInfo.setMipLevels(tmp, std::move(mipmaps));
Robert Phillips96601082018-05-29 16:13:26 -0400489 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400490 // In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU
Robert Phillips96601082018-05-29 16:13:26 -0400491
492 return fImageInfo.count()-1;
493}
494
495int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) {
496 int preExistingID = this->findImage(image);
497 if (preExistingID >= 0) {
498 SkASSERT(this->isValidID(preExistingID));
499 return preExistingID;
500 }
501
502 int newID = this->addImage(image);
Robert Phillips96601082018-05-29 16:13:26 -0400503 return newID;
504}