blob: 89dca3c80f72a385599b2c2b1652ce6847b29028 [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()) {
131 while (!finishedBECreate) {
132 context->checkAsyncWorkCompletion();
133 }
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 */
143void DDLPromiseImageHelper::CreateBETexturesForPromiseImage(GrContext* context,
144 PromiseImageInfo* info) {
145 SkASSERT(context->priv().asDirectContext());
Robert Phillips96601082018-05-29 16:13:26 -0400146
Robert Phillips923181b2020-02-14 12:36:37 -0500147 if (info->isYUV()) {
148 int numPixmaps;
149 SkAssertResult(SkYUVAIndex::AreValidIndices(info->yuvaIndices(), &numPixmaps));
150 for (int j = 0; j < numPixmaps; ++j) {
151 const SkPixmap& yuvPixmap = info->yuvPixmap(j);
152
153 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
154 SkASSERT(callbackContext);
155
Robert Phillips4508eb92020-04-15 15:54:34 -0400156 // DDL TODO: what should we do with mipmapped YUV images
Robert Phillips923181b2020-02-14 12:36:37 -0500157 callbackContext->setBackendTexture(create_yuva_texture(context, yuvPixmap,
158 info->yuvaIndices(), j));
159 SkASSERT(callbackContext->promiseImageTexture());
160 }
161 } else {
162 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
163 if (!callbackContext) {
164 // This texture would've been too large to fit on the GPU
165 return;
166 }
167
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400168 std::unique_ptr<SkPixmap[]> mipLevels = info->normalMipLevels();
Robert Phillips923181b2020-02-14 12:36:37 -0500169
Greg Danielc1ad77c2020-05-06 11:40:03 -0400170 bool finishedBECreate = false;
171 auto markFinished = [](void* context) {
172 *(bool*)context = true;
173 };
174 auto backendTex = context->createBackendTexture(mipLevels.get(), info->numMipLevels(),
175 GrRenderable::kNo, GrProtected::kNo,
176 markFinished, &finishedBECreate);
Robert Phillips923181b2020-02-14 12:36:37 -0500177 SkASSERT(backendTex.isValid());
Greg Danielc1ad77c2020-05-06 11:40:03 -0400178 while (!finishedBECreate) {
179 context->checkAsyncWorkCompletion();
180 }
Robert Phillips923181b2020-02-14 12:36:37 -0500181
182 callbackContext->setBackendTexture(backendTex);
183 }
184}
185
Robert Phillips19f466d2020-02-26 10:27:07 -0500186void DDLPromiseImageHelper::DeleteBETexturesForPromiseImage(GrContext* context,
187 PromiseImageInfo* info) {
188 SkASSERT(context->priv().asDirectContext());
189
190 if (info->isYUV()) {
191 int numPixmaps;
192 SkAssertResult(SkYUVAIndex::AreValidIndices(info->yuvaIndices(), &numPixmaps));
193 for (int j = 0; j < numPixmaps; ++j) {
194 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
195 SkASSERT(callbackContext);
196
197 callbackContext->destroyBackendTexture();
198 SkASSERT(!callbackContext->promiseImageTexture());
199 }
200 } else {
201 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
202 if (!callbackContext) {
203 // This texture would've been too large to fit on the GPU
204 return;
205 }
206
207 callbackContext->destroyBackendTexture();
208 SkASSERT(!callbackContext->promiseImageTexture());
209 }
210}
211
Robert Phillips923181b2020-02-14 12:36:37 -0500212void DDLPromiseImageHelper::createCallbackContexts(GrContext* context) {
213 const GrCaps* caps = context->priv().caps();
214 const int maxDimension = caps->maxTextureSize();
215
216 for (int i = 0; i < fImageInfo.count(); ++i) {
217 PromiseImageInfo& info = fImageInfo[i];
218
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400219 if (info.isYUV()) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400220 int numPixmaps;
221 SkAssertResult(SkYUVAIndex::AreValidIndices(info.yuvaIndices(), &numPixmaps));
Robert Phillips923181b2020-02-14 12:36:37 -0500222
Jim Van Verth8f11e432018-10-18 14:36:59 -0400223 for (int j = 0; j < numPixmaps; ++j) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400224 const SkPixmap& yuvPixmap = info.yuvPixmap(j);
Robert Phillips96601082018-05-29 16:13:26 -0400225
Robert Phillips923181b2020-02-14 12:36:37 -0500226 GrBackendFormat backendFormat = context->defaultBackendFormat(yuvPixmap.colorType(),
227 GrRenderable::kNo);
228
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400229 sk_sp<PromiseImageCallbackContext> callbackContext(
Robert Phillips923181b2020-02-14 12:36:37 -0500230 new PromiseImageCallbackContext(context, backendFormat));
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500231
Robert Phillips923181b2020-02-14 12:36:37 -0500232 info.setCallbackContext(j, std::move(callbackContext));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400233 }
234 } else {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400235 const SkBitmap& baseLevel = info.baseLevel();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400236
Robert Phillips923181b2020-02-14 12:36:37 -0500237 // TODO: explicitly mark the PromiseImageInfo as too big and check in uploadAllToGPU
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400238 if (maxDimension < std::max(baseLevel.width(), baseLevel.height())) {
Robert Phillips923181b2020-02-14 12:36:37 -0500239 // This won't fit on the GPU. Fallback to a raster-backed image per tile.
240 continue;
241 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400242
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400243 GrBackendFormat backendFormat = context->defaultBackendFormat(baseLevel.colorType(),
Robert Phillips923181b2020-02-14 12:36:37 -0500244 GrRenderable::kNo);
245 if (!caps->isFormatTexturable(backendFormat)) {
246 continue;
247 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400248
Robert Phillips923181b2020-02-14 12:36:37 -0500249 sk_sp<PromiseImageCallbackContext> callbackContext(
250 new PromiseImageCallbackContext(context, backendFormat));
251
252 info.setCallbackContext(0, std::move(callbackContext));
253 }
254 }
255}
256
257void DDLPromiseImageHelper::uploadAllToGPU(SkTaskGroup* taskGroup, GrContext* context) {
258 SkASSERT(context->priv().asDirectContext());
259
260 if (taskGroup) {
261 for (int i = 0; i < fImageInfo.count(); ++i) {
262 PromiseImageInfo* info = &fImageInfo[i];
263
Robert Phillips19f466d2020-02-26 10:27:07 -0500264 taskGroup->add([context, info]() { CreateBETexturesForPromiseImage(context, info); });
Robert Phillips923181b2020-02-14 12:36:37 -0500265 }
266 } else {
267 for (int i = 0; i < fImageInfo.count(); ++i) {
268 CreateBETexturesForPromiseImage(context, &fImageInfo[i]);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400269 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500270 }
271}
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400272
Robert Phillips19f466d2020-02-26 10:27:07 -0500273void DDLPromiseImageHelper::deleteAllFromGPU(SkTaskGroup* taskGroup, GrContext* context) {
274 SkASSERT(context->priv().asDirectContext());
275
276 if (taskGroup) {
277 for (int i = 0; i < fImageInfo.count(); ++i) {
278 PromiseImageInfo* info = &fImageInfo[i];
279
280 taskGroup->add([context, info]() { DeleteBETexturesForPromiseImage(context, info); });
281 }
282 } else {
283 for (int i = 0; i < fImageInfo.count(); ++i) {
284 DeleteBETexturesForPromiseImage(context, &fImageInfo[i]);
285 }
286 }
287}
288
Robert Phillips96601082018-05-29 16:13:26 -0400289sk_sp<SkPicture> DDLPromiseImageHelper::reinflateSKP(
290 SkDeferredDisplayListRecorder* recorder,
291 SkData* compressedPictureData,
292 SkTArray<sk_sp<SkImage>>* promiseImages) const {
293 PerRecorderContext perRecorderContext { recorder, this, promiseImages };
294
295 SkDeserialProcs procs;
296 procs.fImageCtx = (void*) &perRecorderContext;
Robert Phillips923181b2020-02-14 12:36:37 -0500297 procs.fImageProc = CreatePromiseImages;
Robert Phillips96601082018-05-29 16:13:26 -0400298
299 return SkPicture::MakeFromData(compressedPictureData, &procs);
300}
301
302// This generates promise images to replace the indices in the compressed picture. This
303// reconstitution is performed separately in each thread so we end up with multiple
304// promise images referring to the same GrBackendTexture.
Robert Phillips923181b2020-02-14 12:36:37 -0500305sk_sp<SkImage> DDLPromiseImageHelper::CreatePromiseImages(const void* rawData,
Robert Phillips96601082018-05-29 16:13:26 -0400306 size_t length, void* ctxIn) {
307 PerRecorderContext* perRecorderContext = static_cast<PerRecorderContext*>(ctxIn);
308 const DDLPromiseImageHelper* helper = perRecorderContext->fHelper;
309 SkDeferredDisplayListRecorder* recorder = perRecorderContext->fRecorder;
310
311 SkASSERT(length == sizeof(int));
312
313 const int* indexPtr = static_cast<const int*>(rawData);
Robert Phillips6bad7052019-12-16 15:09:57 -0500314 if (!helper->isValidID(*indexPtr)) {
315 return nullptr;
316 }
Robert Phillips96601082018-05-29 16:13:26 -0400317
318 const DDLPromiseImageHelper::PromiseImageInfo& curImage = helper->getInfo(*indexPtr);
319
Robert Phillips923181b2020-02-14 12:36:37 -0500320 // If there is no callback context that means 'createCallbackContexts' determined the
321 // texture wouldn't fit on the GPU. Create a separate bitmap-backed image for each thread.
322 if (!curImage.isYUV() && !curImage.callbackContext(0)) {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400323 SkASSERT(curImage.baseLevel().isImmutable());
324 return SkImage::MakeFromBitmap(curImage.baseLevel());
Robert Phillips96601082018-05-29 16:13:26 -0400325 }
Robert Phillips923181b2020-02-14 12:36:37 -0500326
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400327 SkASSERT(curImage.index() == *indexPtr);
Robert Phillips96601082018-05-29 16:13:26 -0400328
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400329 sk_sp<SkImage> image;
330 if (curImage.isYUV()) {
Jim Van Verthe24b5872018-10-29 16:26:02 -0400331 GrBackendFormat backendFormats[SkYUVASizeInfo::kMaxCount];
332 void* contexts[SkYUVASizeInfo::kMaxCount] = { nullptr, nullptr, nullptr, nullptr };
333 SkISize sizes[SkYUVASizeInfo::kMaxCount];
Jim Van Verth8f11e432018-10-18 14:36:59 -0400334 // TODO: store this value somewhere?
335 int textureCount;
336 SkAssertResult(SkYUVAIndex::AreValidIndices(curImage.yuvaIndices(), &textureCount));
337 for (int i = 0; i < textureCount; ++i) {
Robert Phillips923181b2020-02-14 12:36:37 -0500338 backendFormats[i] = curImage.backendFormat(i);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500339 SkASSERT(backendFormats[i].isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400340 contexts[i] = curImage.refCallbackContext(i).release();
Jim Van Verthf9f07352018-10-24 10:32:20 -0400341 sizes[i].set(curImage.yuvPixmap(i).width(), curImage.yuvPixmap(i).height());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400342 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400343 for (int i = textureCount; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verthf9f07352018-10-24 10:32:20 -0400344 sizes[i] = SkISize::MakeEmpty();
Jim Van Verth8f11e432018-10-18 14:36:59 -0400345 }
Jim Van Verthf99a6742018-10-18 16:13:18 +0000346
Brian Salomon0cc57542019-03-08 13:28:46 -0500347 image = recorder->makeYUVAPromiseTexture(
348 curImage.yuvColorSpace(),
349 backendFormats,
350 sizes,
351 curImage.yuvaIndices(),
352 curImage.overallWidth(),
353 curImage.overallHeight(),
354 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
355 curImage.refOverallColorSpace(),
Robert Phillips11c67672020-04-23 15:10:03 -0400356 PromiseImageCallbackContext::PromiseImageFulfillProc,
357 PromiseImageCallbackContext::PromiseImageReleaseProc,
358 PromiseImageCallbackContext::PromiseImageDoneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500359 contexts,
360 SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500361 for (int i = 0; i < textureCount; ++i) {
362 curImage.callbackContext(i)->wasAddedToImage();
363 }
Robert Phillips193c4212019-03-04 12:18:53 -0500364
365#ifdef SK_DEBUG
366 {
367 // By the peekProxy contract this image should not have a single backing proxy so
368 // should return null. The call should also not trigger the conversion to RGBA.
369 SkImage_GpuYUVA* yuva = reinterpret_cast<SkImage_GpuYUVA*>(image.get());
370 SkASSERT(!yuva->peekProxy());
371 SkASSERT(!yuva->peekProxy()); // the first call didn't force a conversion to RGBA
372 }
373#endif
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400374 } else {
Robert Phillips923181b2020-02-14 12:36:37 -0500375 GrBackendFormat backendFormat = curImage.backendFormat(0);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500376 SkASSERT(backendFormat.isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400377
378 // Each DDL recorder gets its own ref on the promise callback context for the
379 // promise images it creates.
Brian Salomon0cc57542019-03-08 13:28:46 -0500380 image = recorder->makePromiseTexture(
381 backendFormat,
382 curImage.overallWidth(),
383 curImage.overallHeight(),
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400384 curImage.mipMapped(0),
Brian Salomon0cc57542019-03-08 13:28:46 -0500385 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
386 curImage.overallColorType(),
387 curImage.overallAlphaType(),
388 curImage.refOverallColorSpace(),
Robert Phillips11c67672020-04-23 15:10:03 -0400389 PromiseImageCallbackContext::PromiseImageFulfillProc,
390 PromiseImageCallbackContext::PromiseImageReleaseProc,
391 PromiseImageCallbackContext::PromiseImageDoneProc,
Brian Salomon0cc57542019-03-08 13:28:46 -0500392 (void*)curImage.refCallbackContext(0).release(),
393 SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500394 curImage.callbackContext(0)->wasAddedToImage();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400395 }
Robert Phillips96601082018-05-29 16:13:26 -0400396 perRecorderContext->fPromiseImages->push_back(image);
397 SkASSERT(image);
398 return image;
399}
400
401int DDLPromiseImageHelper::findImage(SkImage* image) const {
402 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400403 if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here
404 SkASSERT(fImageInfo[i].index() == i);
405 SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index()));
Robert Phillips96601082018-05-29 16:13:26 -0400406 return i;
407 }
408 }
409 return -1;
410}
411
412int DDLPromiseImageHelper::addImage(SkImage* image) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400413 SkImage_Base* ib = as_IB(image);
Robert Phillips96601082018-05-29 16:13:26 -0400414
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400415 SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(),
Robert Phillips13371a12019-05-13 15:59:10 -0400416 image->colorType() == kBGRA_8888_SkColorType
417 ? kRGBA_8888_SkColorType
418 : image->colorType(),
419 image->alphaType(),
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400420 image->refColorSpace());
Robert Phillips96601082018-05-29 16:13:26 -0400421
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400422 PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.count(),
423 image->uniqueID(),
424 overallII);
Robert Phillips96601082018-05-29 16:13:26 -0400425
Jim Van Verthe24b5872018-10-29 16:26:02 -0400426 SkYUVASizeInfo yuvaSizeInfo;
Jim Van Verth8f11e432018-10-18 14:36:59 -0400427 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount];
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400428 SkYUVColorSpace yuvColorSpace;
Jim Van Verthe24b5872018-10-29 16:26:02 -0400429 const void* planes[SkYUVASizeInfo::kMaxCount];
Jim Van Verth8f11e432018-10-18 14:36:59 -0400430 sk_sp<SkCachedData> yuvData = ib->getPlanes(&yuvaSizeInfo, yuvaIndices, &yuvColorSpace, planes);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400431 if (yuvData) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400432 newImageInfo.setYUVData(std::move(yuvData), yuvaIndices, yuvColorSpace);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400433
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400434 // determine colortypes from index data
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400435 // for testing we only ever use A8, RG_88
Jim Van Verthe24b5872018-10-29 16:26:02 -0400436 SkColorType colorTypes[SkYUVASizeInfo::kMaxCount] = {
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400437 kUnknown_SkColorType, kUnknown_SkColorType,
438 kUnknown_SkColorType, kUnknown_SkColorType
439 };
440 for (int yuvIndex = 0; yuvIndex < SkYUVAIndex::kIndexCount; ++yuvIndex) {
441 int texIdx = yuvaIndices[yuvIndex].fIndex;
442 if (texIdx < 0) {
443 SkASSERT(SkYUVAIndex::kA_Index == yuvIndex);
444 continue;
445 }
446 if (kUnknown_SkColorType == colorTypes[texIdx]) {
447 colorTypes[texIdx] = kAlpha_8_SkColorType;
448 } else {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400449 colorTypes[texIdx] = kR8G8_unorm_SkColorType;
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400450 }
451 }
452
Jim Van Verthe24b5872018-10-29 16:26:02 -0400453 for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400454 if (yuvaSizeInfo.fSizes[i].isEmpty()) {
455 SkASSERT(!yuvaSizeInfo.fWidthBytes[i] && kUnknown_SkColorType == colorTypes[i]);
Jim Van Verth8f11e432018-10-18 14:36:59 -0400456 continue;
457 }
458
459 SkImageInfo planeII = SkImageInfo::Make(yuvaSizeInfo.fSizes[i].fWidth,
460 yuvaSizeInfo.fSizes[i].fHeight,
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400461 colorTypes[i],
Jim Van Verth8f11e432018-10-18 14:36:59 -0400462 kUnpremul_SkAlphaType);
463 newImageInfo.addYUVPlane(i, planeII, planes[i], yuvaSizeInfo.fWidthBytes[i]);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400464 }
465 } else {
466 sk_sp<SkImage> rasterImage = image->makeRasterImage(); // force decoding of lazy images
Robert Phillipse84bffc2019-12-16 11:22:17 -0500467 if (!rasterImage) {
468 return -1;
469 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400470
471 SkBitmap tmp;
472 tmp.allocPixels(overallII);
473
474 if (!rasterImage->readPixels(tmp.pixmap(), 0, 0)) {
475 return -1;
476 }
477
478 tmp.setImmutable();
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400479
480 // Given how the DDL testing harness works (i.e., only modifying the SkImages w/in an
481 // SKP) we don't know if a given SkImage will require mipmapping. To work around this
482 // we just try to create all the backend textures as mipmapped but, failing that, fall
483 // back to un-mipped.
484 std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(tmp.pixmap(), nullptr));
485
486 newImageInfo.setMipLevels(tmp, std::move(mipmaps));
Robert Phillips96601082018-05-29 16:13:26 -0400487 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400488 // In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU
Robert Phillips96601082018-05-29 16:13:26 -0400489
490 return fImageInfo.count()-1;
491}
492
493int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) {
494 int preExistingID = this->findImage(image);
495 if (preExistingID >= 0) {
496 SkASSERT(this->isValidID(preExistingID));
497 return preExistingID;
498 }
499
500 int newID = this->addImage(image);
Robert Phillips96601082018-05-29 16:13:26 -0400501 return newID;
502}