blob: 648b86a5c03569ab04256089e51360c82f7000df [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 Salomonefb5f072020-07-28 21:06:43 -040016#include "src/codec/SkCodecImageGenerator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkCachedData.h"
Mike Reed13711eb2020-07-14 17:16:32 -040018#include "src/core/SkMipmap.h"
Robert Phillips923181b2020-02-14 12:36:37 -050019#include "src/core/SkTaskGroup.h"
Adlai Hollera0693042020-10-14 11:23:11 -040020#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/image/SkImage_Base.h"
22#include "src/image/SkImage_GpuYUVA.h"
Robert Phillips96601082018-05-29 16:13:26 -040023
Robert Phillipsf95e2f42020-04-17 16:20:55 -040024DDLPromiseImageHelper::PromiseImageInfo::PromiseImageInfo(int index,
25 uint32_t originalUniqueID,
26 const SkImageInfo& ii)
27 : fIndex(index)
28 , fOriginalUniqueID(originalUniqueID)
29 , fImageInfo(ii) {
30}
31
32DDLPromiseImageHelper::PromiseImageInfo::PromiseImageInfo(PromiseImageInfo&& other)
33 : fIndex(other.fIndex)
34 , fOriginalUniqueID(other.fOriginalUniqueID)
35 , fImageInfo(other.fImageInfo)
36 , fBaseLevel(other.fBaseLevel)
37 , fMipLevels(std::move(other.fMipLevels))
Brian Salomon5660e8b2020-08-25 12:40:32 -040038 , fYUVAPixmaps(std::move(other.fYUVAPixmaps)) {
Robert Phillipsf95e2f42020-04-17 16:20:55 -040039 for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) {
Robert Phillipsf95e2f42020-04-17 16:20:55 -040040 fCallbackContexts[i] = std::move(other.fCallbackContexts[i]);
41 }
Brian Salomon5660e8b2020-08-25 12:40:32 -040042 std::copy_n(other.fYUVAIndices, SkYUVAIndex::kIndexCount, fYUVAIndices);
Robert Phillipsf95e2f42020-04-17 16:20:55 -040043}
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
Brian Salomon5660e8b2020-08-25 12:40:32 -040072void DDLPromiseImageHelper::PromiseImageInfo::initYUVAIndices() {
73 SkYUVASizeInfo unusedSizeInfo;
Brian Salomonbe0e42c2020-08-27 11:00:04 -040074 fYUVAPixmaps.toLegacy(&unusedSizeInfo, fYUVAIndices);
Brian Salomon5660e8b2020-08-25 12:40:32 -040075 // We can wind up with alpha pixmaps that become red textures. If so, update YUVA indices.
76 // Note: This goes away when we start creating YUVA images using SkYUVAInfo.
77 for (int i = 0; i < SkYUVAIndex::kIndexCount; ++i) {
78 if (fYUVAIndices[i].fIndex >= 0) {
79 int textureIdx = fYUVAIndices[i].fIndex;
80 const auto& format = fCallbackContexts[textureIdx]->backendFormat();
81 if (fYUVAIndices[i].fChannel == SkColorChannel::kA &&
82 format.channelMask() == SkColorChannelFlag::kRed_SkColorChannelFlag) {
83 fYUVAIndices[i].fChannel = SkColorChannel::kR;
84 }
Brian Salomonefb5f072020-07-28 21:06:43 -040085 }
Brian Salomonefb5f072020-07-28 21:06:43 -040086 }
87}
88
Robert Phillipsf95e2f42020-04-17 16:20:55 -040089///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Phillips11c67672020-04-23 15:10:03 -040090PromiseImageCallbackContext::~PromiseImageCallbackContext() {
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050091 SkASSERT(fDoneCnt == fNumImages);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050092 SkASSERT(!fTotalFulfills || fDoneCnt);
Robert Phillips96601082018-05-29 16:13:26 -040093
Brian Salomon3f4cd772019-01-11 16:03:19 -050094 if (fPromiseImageTexture) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -040095 fContext->deleteBackendTexture(fPromiseImageTexture->backendTexture());
Robert Phillips96601082018-05-29 16:13:26 -040096 }
97}
98
Robert Phillips11c67672020-04-23 15:10:03 -040099void PromiseImageCallbackContext::setBackendTexture(const GrBackendTexture& backendTexture) {
Brian Salomon7d88f312019-02-28 10:03:03 -0500100 SkASSERT(!fPromiseImageTexture);
Robert Phillips923181b2020-02-14 12:36:37 -0500101 SkASSERT(fBackendFormat == backendTexture.getBackendFormat());
Brian Salomon3f4cd772019-01-11 16:03:19 -0500102 fPromiseImageTexture = SkPromiseImageTexture::Make(backendTexture);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500103}
104
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400105void PromiseImageCallbackContext::destroyBackendTexture() {
106 SkASSERT(!fPromiseImageTexture || fPromiseImageTexture->unique());
107
108 if (fPromiseImageTexture) {
109 fContext->deleteBackendTexture(fPromiseImageTexture->backendTexture());
110 }
111 fPromiseImageTexture = nullptr;
112}
113
Robert Phillips96601082018-05-29 16:13:26 -0400114///////////////////////////////////////////////////////////////////////////////////////////////////
115
116sk_sp<SkData> DDLPromiseImageHelper::deflateSKP(const SkPicture* inputPicture) {
117 SkSerialProcs procs;
118
119 procs.fImageCtx = this;
120 procs.fImageProc = [](SkImage* image, void* ctx) -> sk_sp<SkData> {
121 auto helper = static_cast<DDLPromiseImageHelper*>(ctx);
122
123 int id = helper->findOrDefineImage(image);
Robert Phillips96601082018-05-29 16:13:26 -0400124
Robert Phillips6bad7052019-12-16 15:09:57 -0500125 // Even if 'id' is invalid (i.e., -1) write it to the SKP
126 return SkData::MakeWithCopy(&id, sizeof(id));
Robert Phillips96601082018-05-29 16:13:26 -0400127 };
128
129 return inputPicture->serialize(&procs);
130}
131
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400132static GrBackendTexture create_yuva_texture(GrDirectContext* direct, const SkPixmap& pm,
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500133 const SkYUVAIndex yuvaIndices[4], int texIndex) {
134 SkASSERT(texIndex >= 0 && texIndex <= 3);
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400135
136#ifdef SK_DEBUG
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500137 int channelCount = 0;
138 for (int i = 0; i < SkYUVAIndex::kIndexCount; ++i) {
139 if (yuvaIndices[i].fIndex == texIndex) {
140 ++channelCount;
141 }
142 }
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500143 if (2 == channelCount) {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400144 SkASSERT(kR8G8_unorm_SkColorType == pm.colorType());
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500145 }
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400146#endif
Greg Danielc1ad77c2020-05-06 11:40:03 -0400147 bool finishedBECreate = false;
148 auto markFinished = [](void* context) {
149 *(bool*)context = true;
150 };
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400151 auto beTex = direct->createBackendTexture(&pm, 1, GrRenderable::kNo, GrProtected::kNo,
152 markFinished, &finishedBECreate);
Greg Danielc1ad77c2020-05-06 11:40:03 -0400153 if (beTex.isValid()) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400154 direct->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400155 while (!finishedBECreate) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400156 direct->checkAsyncWorkCompletion();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400157 }
158 }
159 return beTex;
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500160}
161
Robert Phillips923181b2020-02-14 12:36:37 -0500162/*
163 * Create backend textures and upload data to them for all the textures required to satisfy
164 * a single promise image.
165 * For YUV textures this will result in up to 4 actual textures.
166 */
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400167void DDLPromiseImageHelper::CreateBETexturesForPromiseImage(GrDirectContext* direct,
Robert Phillips923181b2020-02-14 12:36:37 -0500168 PromiseImageInfo* info) {
Robert Phillips923181b2020-02-14 12:36:37 -0500169 if (info->isYUV()) {
170 int numPixmaps;
171 SkAssertResult(SkYUVAIndex::AreValidIndices(info->yuvaIndices(), &numPixmaps));
172 for (int j = 0; j < numPixmaps; ++j) {
173 const SkPixmap& yuvPixmap = info->yuvPixmap(j);
174
175 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
176 SkASSERT(callbackContext);
177
Robert Phillips4508eb92020-04-15 15:54:34 -0400178 // DDL TODO: what should we do with mipmapped YUV images
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400179 callbackContext->setBackendTexture(create_yuva_texture(direct, yuvPixmap,
Robert Phillips923181b2020-02-14 12:36:37 -0500180 info->yuvaIndices(), j));
181 SkASSERT(callbackContext->promiseImageTexture());
182 }
183 } else {
184 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
185 if (!callbackContext) {
186 // This texture would've been too large to fit on the GPU
187 return;
188 }
189
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400190 std::unique_ptr<SkPixmap[]> mipLevels = info->normalMipLevels();
Robert Phillips923181b2020-02-14 12:36:37 -0500191
Greg Danielc1ad77c2020-05-06 11:40:03 -0400192 bool finishedBECreate = false;
193 auto markFinished = [](void* context) {
194 *(bool*)context = true;
195 };
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400196 auto backendTex = direct->createBackendTexture(mipLevels.get(), info->numMipLevels(),
197 GrRenderable::kNo, GrProtected::kNo,
198 markFinished, &finishedBECreate);
Robert Phillips923181b2020-02-14 12:36:37 -0500199 SkASSERT(backendTex.isValid());
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400200 direct->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400201 while (!finishedBECreate) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400202 direct->checkAsyncWorkCompletion();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400203 }
Robert Phillips923181b2020-02-14 12:36:37 -0500204
205 callbackContext->setBackendTexture(backendTex);
206 }
207}
208
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400209void DDLPromiseImageHelper::DeleteBETexturesForPromiseImage(GrDirectContext* direct,
Robert Phillips19f466d2020-02-26 10:27:07 -0500210 PromiseImageInfo* info) {
Robert Phillips19f466d2020-02-26 10:27:07 -0500211 if (info->isYUV()) {
212 int numPixmaps;
213 SkAssertResult(SkYUVAIndex::AreValidIndices(info->yuvaIndices(), &numPixmaps));
214 for (int j = 0; j < numPixmaps; ++j) {
215 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
216 SkASSERT(callbackContext);
217
218 callbackContext->destroyBackendTexture();
219 SkASSERT(!callbackContext->promiseImageTexture());
220 }
221 } else {
222 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
223 if (!callbackContext) {
224 // This texture would've been too large to fit on the GPU
225 return;
226 }
227
228 callbackContext->destroyBackendTexture();
229 SkASSERT(!callbackContext->promiseImageTexture());
230 }
231}
232
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400233void DDLPromiseImageHelper::createCallbackContexts(GrDirectContext* direct) {
234 const GrCaps* caps = direct->priv().caps();
Robert Phillips923181b2020-02-14 12:36:37 -0500235 const int maxDimension = caps->maxTextureSize();
236
237 for (int i = 0; i < fImageInfo.count(); ++i) {
238 PromiseImageInfo& info = fImageInfo[i];
239
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400240 if (info.isYUV()) {
Brian Salomon5660e8b2020-08-25 12:40:32 -0400241 int numPixmaps = info.numYUVAPlanes();
Robert Phillips923181b2020-02-14 12:36:37 -0500242
Jim Van Verth8f11e432018-10-18 14:36:59 -0400243 for (int j = 0; j < numPixmaps; ++j) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400244 const SkPixmap& yuvPixmap = info.yuvPixmap(j);
Robert Phillips96601082018-05-29 16:13:26 -0400245
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400246 GrBackendFormat backendFormat = direct->defaultBackendFormat(yuvPixmap.colorType(),
247 GrRenderable::kNo);
Robert Phillips923181b2020-02-14 12:36:37 -0500248
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400249 sk_sp<PromiseImageCallbackContext> callbackContext(
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400250 new PromiseImageCallbackContext(direct, backendFormat));
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500251
Robert Phillips923181b2020-02-14 12:36:37 -0500252 info.setCallbackContext(j, std::move(callbackContext));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400253 }
Brian Salomon5660e8b2020-08-25 12:40:32 -0400254 info.initYUVAIndices();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400255 } else {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400256 const SkBitmap& baseLevel = info.baseLevel();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400257
Robert Phillips923181b2020-02-14 12:36:37 -0500258 // TODO: explicitly mark the PromiseImageInfo as too big and check in uploadAllToGPU
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400259 if (maxDimension < std::max(baseLevel.width(), baseLevel.height())) {
Robert Phillips923181b2020-02-14 12:36:37 -0500260 // This won't fit on the GPU. Fallback to a raster-backed image per tile.
261 continue;
262 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400263
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400264 GrBackendFormat backendFormat = direct->defaultBackendFormat(baseLevel.colorType(),
265 GrRenderable::kNo);
Robert Phillips923181b2020-02-14 12:36:37 -0500266 if (!caps->isFormatTexturable(backendFormat)) {
267 continue;
268 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400269
Robert Phillips923181b2020-02-14 12:36:37 -0500270 sk_sp<PromiseImageCallbackContext> callbackContext(
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400271 new PromiseImageCallbackContext(direct, backendFormat));
Robert Phillips923181b2020-02-14 12:36:37 -0500272
273 info.setCallbackContext(0, std::move(callbackContext));
274 }
275 }
276}
277
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400278void DDLPromiseImageHelper::uploadAllToGPU(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips923181b2020-02-14 12:36:37 -0500279 if (taskGroup) {
280 for (int i = 0; i < fImageInfo.count(); ++i) {
281 PromiseImageInfo* info = &fImageInfo[i];
282
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400283 taskGroup->add([direct, info]() { CreateBETexturesForPromiseImage(direct, info); });
Robert Phillips923181b2020-02-14 12:36:37 -0500284 }
285 } else {
286 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400287 CreateBETexturesForPromiseImage(direct, &fImageInfo[i]);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400288 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500289 }
290}
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400291
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400292void DDLPromiseImageHelper::deleteAllFromGPU(SkTaskGroup* taskGroup, GrDirectContext* direct) {
Robert Phillips19f466d2020-02-26 10:27:07 -0500293 if (taskGroup) {
294 for (int i = 0; i < fImageInfo.count(); ++i) {
295 PromiseImageInfo* info = &fImageInfo[i];
296
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400297 taskGroup->add([direct, info]() { DeleteBETexturesForPromiseImage(direct, info); });
Robert Phillips19f466d2020-02-26 10:27:07 -0500298 }
299 } else {
300 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipsd5f3c982020-07-07 13:18:47 -0400301 DeleteBETexturesForPromiseImage(direct, &fImageInfo[i]);
Robert Phillips19f466d2020-02-26 10:27:07 -0500302 }
303 }
304}
305
Robert Phillips96601082018-05-29 16:13:26 -0400306sk_sp<SkPicture> DDLPromiseImageHelper::reinflateSKP(
307 SkDeferredDisplayListRecorder* recorder,
308 SkData* compressedPictureData,
309 SkTArray<sk_sp<SkImage>>* promiseImages) const {
310 PerRecorderContext perRecorderContext { recorder, this, promiseImages };
311
312 SkDeserialProcs procs;
313 procs.fImageCtx = (void*) &perRecorderContext;
Robert Phillips923181b2020-02-14 12:36:37 -0500314 procs.fImageProc = CreatePromiseImages;
Robert Phillips96601082018-05-29 16:13:26 -0400315
316 return SkPicture::MakeFromData(compressedPictureData, &procs);
317}
318
319// This generates promise images to replace the indices in the compressed picture. This
320// reconstitution is performed separately in each thread so we end up with multiple
321// promise images referring to the same GrBackendTexture.
Robert Phillips923181b2020-02-14 12:36:37 -0500322sk_sp<SkImage> DDLPromiseImageHelper::CreatePromiseImages(const void* rawData,
Robert Phillips96601082018-05-29 16:13:26 -0400323 size_t length, void* ctxIn) {
324 PerRecorderContext* perRecorderContext = static_cast<PerRecorderContext*>(ctxIn);
325 const DDLPromiseImageHelper* helper = perRecorderContext->fHelper;
326 SkDeferredDisplayListRecorder* recorder = perRecorderContext->fRecorder;
327
328 SkASSERT(length == sizeof(int));
329
330 const int* indexPtr = static_cast<const int*>(rawData);
Robert Phillips6bad7052019-12-16 15:09:57 -0500331 if (!helper->isValidID(*indexPtr)) {
332 return nullptr;
333 }
Robert Phillips96601082018-05-29 16:13:26 -0400334
335 const DDLPromiseImageHelper::PromiseImageInfo& curImage = helper->getInfo(*indexPtr);
336
Robert Phillips923181b2020-02-14 12:36:37 -0500337 // If there is no callback context that means 'createCallbackContexts' determined the
338 // texture wouldn't fit on the GPU. Create a separate bitmap-backed image for each thread.
339 if (!curImage.isYUV() && !curImage.callbackContext(0)) {
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400340 SkASSERT(curImage.baseLevel().isImmutable());
341 return SkImage::MakeFromBitmap(curImage.baseLevel());
Robert Phillips96601082018-05-29 16:13:26 -0400342 }
Robert Phillips923181b2020-02-14 12:36:37 -0500343
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400344 SkASSERT(curImage.index() == *indexPtr);
Robert Phillips96601082018-05-29 16:13:26 -0400345
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400346 sk_sp<SkImage> image;
347 if (curImage.isYUV()) {
Jim Van Verthe24b5872018-10-29 16:26:02 -0400348 GrBackendFormat backendFormats[SkYUVASizeInfo::kMaxCount];
349 void* contexts[SkYUVASizeInfo::kMaxCount] = { nullptr, nullptr, nullptr, nullptr };
350 SkISize sizes[SkYUVASizeInfo::kMaxCount];
Jim Van Verth8f11e432018-10-18 14:36:59 -0400351 // TODO: store this value somewhere?
352 int textureCount;
353 SkAssertResult(SkYUVAIndex::AreValidIndices(curImage.yuvaIndices(), &textureCount));
354 for (int i = 0; i < textureCount; ++i) {
Robert Phillips923181b2020-02-14 12:36:37 -0500355 backendFormats[i] = curImage.backendFormat(i);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500356 SkASSERT(backendFormats[i].isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400357 contexts[i] = curImage.refCallbackContext(i).release();
Jim Van Verthf9f07352018-10-24 10:32:20 -0400358 sizes[i].set(curImage.yuvPixmap(i).width(), curImage.yuvPixmap(i).height());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400359 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400360 for (int i = textureCount; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verthf9f07352018-10-24 10:32:20 -0400361 sizes[i] = SkISize::MakeEmpty();
Jim Van Verth8f11e432018-10-18 14:36:59 -0400362 }
Jim Van Verthf99a6742018-10-18 16:13:18 +0000363
Brian Salomon0cc57542019-03-08 13:28:46 -0500364 image = recorder->makeYUVAPromiseTexture(
365 curImage.yuvColorSpace(),
366 backendFormats,
367 sizes,
368 curImage.yuvaIndices(),
369 curImage.overallWidth(),
370 curImage.overallHeight(),
371 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
372 curImage.refOverallColorSpace(),
Robert Phillips11c67672020-04-23 15:10:03 -0400373 PromiseImageCallbackContext::PromiseImageFulfillProc,
374 PromiseImageCallbackContext::PromiseImageReleaseProc,
Brian Salomonf1432742020-11-09 15:40:27 -0500375 contexts);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500376 for (int i = 0; i < textureCount; ++i) {
377 curImage.callbackContext(i)->wasAddedToImage();
378 }
Robert Phillips193c4212019-03-04 12:18:53 -0500379
380#ifdef SK_DEBUG
381 {
382 // By the peekProxy contract this image should not have a single backing proxy so
383 // should return null. The call should also not trigger the conversion to RGBA.
384 SkImage_GpuYUVA* yuva = reinterpret_cast<SkImage_GpuYUVA*>(image.get());
385 SkASSERT(!yuva->peekProxy());
386 SkASSERT(!yuva->peekProxy()); // the first call didn't force a conversion to RGBA
387 }
388#endif
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400389 } else {
John Stiles31954bf2020-08-07 17:35:54 -0400390 const GrBackendFormat& backendFormat = curImage.backendFormat(0);
Brian Salomonf391d0f2018-12-14 09:18:50 -0500391 SkASSERT(backendFormat.isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400392
393 // Each DDL recorder gets its own ref on the promise callback context for the
394 // promise images it creates.
Brian Salomonf1432742020-11-09 15:40:27 -0500395 image = recorder->makePromiseTexture(backendFormat,
396 curImage.overallWidth(),
397 curImage.overallHeight(),
398 curImage.mipMapped(0),
399 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
400 curImage.overallColorType(),
401 curImage.overallAlphaType(),
402 curImage.refOverallColorSpace(),
403 PromiseImageCallbackContext::PromiseImageFulfillProc,
404 PromiseImageCallbackContext::PromiseImageReleaseProc,
405 (void*)curImage.refCallbackContext(0).release());
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500406 curImage.callbackContext(0)->wasAddedToImage();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400407 }
Robert Phillips96601082018-05-29 16:13:26 -0400408 perRecorderContext->fPromiseImages->push_back(image);
409 SkASSERT(image);
410 return image;
411}
412
413int DDLPromiseImageHelper::findImage(SkImage* image) const {
414 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400415 if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here
416 SkASSERT(fImageInfo[i].index() == i);
417 SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index()));
Robert Phillips96601082018-05-29 16:13:26 -0400418 return i;
419 }
420 }
421 return -1;
422}
423
424int DDLPromiseImageHelper::addImage(SkImage* image) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400425 SkImage_Base* ib = as_IB(image);
Robert Phillips96601082018-05-29 16:13:26 -0400426
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400427 SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(),
Robert Phillips13371a12019-05-13 15:59:10 -0400428 image->colorType() == kBGRA_8888_SkColorType
429 ? kRGBA_8888_SkColorType
430 : image->colorType(),
431 image->alphaType(),
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400432 image->refColorSpace());
Robert Phillips96601082018-05-29 16:13:26 -0400433
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400434 PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.count(),
435 image->uniqueID(),
436 overallII);
Robert Phillips96601082018-05-29 16:13:26 -0400437
Brian Salomonefb5f072020-07-28 21:06:43 -0400438 auto codec = SkCodecImageGenerator::MakeFromEncodedCodec(ib->refEncodedData());
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400439 SkYUVAPixmapInfo yuvaInfo;
Brian Salomon59c60b02020-09-01 15:01:15 -0400440 if (codec && codec->queryYUVAInfo(fSupportedYUVADataTypes, &yuvaInfo)) {
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400441 auto yuvaPixmaps = SkYUVAPixmaps::Allocate(yuvaInfo);
442 SkAssertResult(codec->getYUVAPlanes(yuvaPixmaps));
Brian Salomon5660e8b2020-08-25 12:40:32 -0400443 SkASSERT(yuvaPixmaps.isValid());
444 newImageInfo.setYUVPlanes(std::move(yuvaPixmaps));
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400445 } else {
446 sk_sp<SkImage> rasterImage = image->makeRasterImage(); // force decoding of lazy images
Robert Phillipse84bffc2019-12-16 11:22:17 -0500447 if (!rasterImage) {
448 return -1;
449 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400450
451 SkBitmap tmp;
452 tmp.allocPixels(overallII);
453
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400454 if (!rasterImage->readPixels(nullptr, tmp.pixmap(), 0, 0)) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400455 return -1;
456 }
457
458 tmp.setImmutable();
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400459
460 // Given how the DDL testing harness works (i.e., only modifying the SkImages w/in an
461 // SKP) we don't know if a given SkImage will require mipmapping. To work around this
462 // we just try to create all the backend textures as mipmapped but, failing that, fall
463 // back to un-mipped.
Mike Reed13711eb2020-07-14 17:16:32 -0400464 std::unique_ptr<SkMipmap> mipmaps(SkMipmap::Build(tmp.pixmap(), nullptr));
Robert Phillipsf95e2f42020-04-17 16:20:55 -0400465
466 newImageInfo.setMipLevels(tmp, std::move(mipmaps));
Robert Phillips96601082018-05-29 16:13:26 -0400467 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400468 // In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU
Robert Phillips96601082018-05-29 16:13:26 -0400469
470 return fImageInfo.count()-1;
471}
472
473int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) {
474 int preExistingID = this->findImage(image);
475 if (preExistingID >= 0) {
476 SkASSERT(this->isValidID(preExistingID));
477 return preExistingID;
478 }
479
480 int newID = this->addImage(image);
Robert Phillips96601082018-05-29 16:13:26 -0400481 return newID;
482}