blob: 5cfbb6e8d8cc9e71746d9114508d8eb18e35a168 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/image/SkImage_Base.h"
18#include "src/image/SkImage_GpuYUVA.h"
Robert Phillips96601082018-05-29 16:13:26 -040019
20DDLPromiseImageHelper::PromiseImageCallbackContext::~PromiseImageCallbackContext() {
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050021 SkASSERT(fDoneCnt == fNumImages);
22 SkASSERT(!fUnreleasedFulfills);
23 SkASSERT(fTotalReleases == fTotalFulfills);
24 SkASSERT(!fTotalFulfills || fDoneCnt);
Robert Phillips96601082018-05-29 16:13:26 -040025
Brian Salomon3f4cd772019-01-11 16:03:19 -050026 if (fPromiseImageTexture) {
Robert Phillips5c7a25b2019-05-20 08:38:07 -040027 fContext->deleteBackendTexture(fPromiseImageTexture->backendTexture());
Robert Phillips96601082018-05-29 16:13:26 -040028 }
29}
30
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050031void DDLPromiseImageHelper::PromiseImageCallbackContext::setBackendTexture(
32 const GrBackendTexture& backendTexture) {
Brian Salomon7d88f312019-02-28 10:03:03 -050033 SkASSERT(!fPromiseImageTexture);
Brian Salomon3f4cd772019-01-11 16:03:19 -050034 fPromiseImageTexture = SkPromiseImageTexture::Make(backendTexture);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -050035}
36
Robert Phillips96601082018-05-29 16:13:26 -040037///////////////////////////////////////////////////////////////////////////////////////////////////
38
39sk_sp<SkData> DDLPromiseImageHelper::deflateSKP(const SkPicture* inputPicture) {
40 SkSerialProcs procs;
41
42 procs.fImageCtx = this;
43 procs.fImageProc = [](SkImage* image, void* ctx) -> sk_sp<SkData> {
44 auto helper = static_cast<DDLPromiseImageHelper*>(ctx);
45
46 int id = helper->findOrDefineImage(image);
47 if (id >= 0) {
48 SkASSERT(helper->isValidID(id));
49 return SkData::MakeWithCopy(&id, sizeof(id));
50 }
51
52 return nullptr;
53 };
54
55 return inputPicture->serialize(&procs);
56}
57
Robert Phillipscb1adb42019-06-10 15:09:34 -040058static GrBackendTexture create_yuva_texture(GrContext* context, const SkPixmap& pm,
Jim Van Verth60ac5d02018-12-06 13:11:53 -050059 const SkYUVAIndex yuvaIndices[4], int texIndex) {
60 SkASSERT(texIndex >= 0 && texIndex <= 3);
Robert Phillipsd470e1b2019-09-04 15:05:35 -040061
62#ifdef SK_DEBUG
Jim Van Verth60ac5d02018-12-06 13:11:53 -050063 int channelCount = 0;
64 for (int i = 0; i < SkYUVAIndex::kIndexCount; ++i) {
65 if (yuvaIndices[i].fIndex == texIndex) {
66 ++channelCount;
67 }
68 }
Jim Van Verth60ac5d02018-12-06 13:11:53 -050069 if (2 == channelCount) {
Robert Phillipsea1b30b2019-09-19 16:05:48 -040070 SkASSERT(kR8G8_unorm_SkColorType == pm.colorType());
Jim Van Verth60ac5d02018-12-06 13:11:53 -050071 }
Robert Phillipsd470e1b2019-09-04 15:05:35 -040072#endif
73
Robert Phillips66944402019-09-30 13:21:25 -040074 return context->createBackendTexture(&pm, 1, GrRenderable::kNo, GrProtected::kNo);
Jim Van Verth60ac5d02018-12-06 13:11:53 -050075}
76
Robert Phillips96601082018-05-29 16:13:26 -040077void DDLPromiseImageHelper::uploadAllToGPU(GrContext* context) {
Brian Salomon426ba462019-01-10 16:33:06 +000078 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -040079 const PromiseImageInfo& info = fImageInfo[i];
80
81 // DDL TODO: how can we tell if we need mipmapping!
Robert Phillipse8e2bb12018-09-27 14:26:47 -040082 if (info.isYUV()) {
Jim Van Verth8f11e432018-10-18 14:36:59 -040083 int numPixmaps;
84 SkAssertResult(SkYUVAIndex::AreValidIndices(info.yuvaIndices(), &numPixmaps));
85 for (int j = 0; j < numPixmaps; ++j) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -040086 const SkPixmap& yuvPixmap = info.yuvPixmap(j);
Robert Phillips96601082018-05-29 16:13:26 -040087
Robert Phillipse8e2bb12018-09-27 14:26:47 -040088 sk_sp<PromiseImageCallbackContext> callbackContext(
89 new PromiseImageCallbackContext(context));
Jim Van Verth60ac5d02018-12-06 13:11:53 -050090
Robert Phillipscb1adb42019-06-10 15:09:34 -040091 callbackContext->setBackendTexture(create_yuva_texture(context, yuvPixmap,
Jim Van Verth60ac5d02018-12-06 13:11:53 -050092 info.yuvaIndices(), j));
Brian Salomon3f4cd772019-01-11 16:03:19 -050093 SkASSERT(callbackContext->promiseImageTexture());
Robert Phillipse8e2bb12018-09-27 14:26:47 -040094
95 fImageInfo[i].setCallbackContext(j, std::move(callbackContext));
96 }
97 } else {
98 sk_sp<PromiseImageCallbackContext> callbackContext(
99 new PromiseImageCallbackContext(context));
100
101 const SkBitmap& bm = info.normalBitmap();
102
Robert Phillips66944402019-09-30 13:21:25 -0400103 GrBackendTexture backendTex = context->createBackendTexture(
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400104 &bm.pixmap(), 1, GrRenderable::kNo,
105 GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -0400106
107 callbackContext->setBackendTexture(backendTex);
108
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400109 // The GMs sometimes request too large an image
110 //SkAssertResult(callbackContext->backendTexture().isValid());
111
112 fImageInfo[i].setCallbackContext(0, std::move(callbackContext));
113 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500114 }
115}
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400116
Robert Phillips96601082018-05-29 16:13:26 -0400117sk_sp<SkPicture> DDLPromiseImageHelper::reinflateSKP(
118 SkDeferredDisplayListRecorder* recorder,
119 SkData* compressedPictureData,
120 SkTArray<sk_sp<SkImage>>* promiseImages) const {
121 PerRecorderContext perRecorderContext { recorder, this, promiseImages };
122
123 SkDeserialProcs procs;
124 procs.fImageCtx = (void*) &perRecorderContext;
125 procs.fImageProc = PromiseImageCreator;
126
127 return SkPicture::MakeFromData(compressedPictureData, &procs);
128}
129
130// This generates promise images to replace the indices in the compressed picture. This
131// reconstitution is performed separately in each thread so we end up with multiple
132// promise images referring to the same GrBackendTexture.
133sk_sp<SkImage> DDLPromiseImageHelper::PromiseImageCreator(const void* rawData,
134 size_t length, void* ctxIn) {
135 PerRecorderContext* perRecorderContext = static_cast<PerRecorderContext*>(ctxIn);
136 const DDLPromiseImageHelper* helper = perRecorderContext->fHelper;
137 SkDeferredDisplayListRecorder* recorder = perRecorderContext->fRecorder;
138
139 SkASSERT(length == sizeof(int));
140
141 const int* indexPtr = static_cast<const int*>(rawData);
142 SkASSERT(helper->isValidID(*indexPtr));
143
144 const DDLPromiseImageHelper::PromiseImageInfo& curImage = helper->getInfo(*indexPtr);
145
Brian Salomon3f4cd772019-01-11 16:03:19 -0500146 if (!curImage.promiseTexture(0)) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400147 SkASSERT(!curImage.isYUV());
Robert Phillips96601082018-05-29 16:13:26 -0400148 // We weren't able to make a backend texture for this SkImage. In this case we create
149 // a separate bitmap-backed image for each thread.
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400150 SkASSERT(curImage.normalBitmap().isImmutable());
151 return SkImage::MakeFromBitmap(curImage.normalBitmap());
Robert Phillips96601082018-05-29 16:13:26 -0400152 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400153 SkASSERT(curImage.index() == *indexPtr);
Robert Phillips96601082018-05-29 16:13:26 -0400154
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400155 sk_sp<SkImage> image;
156 if (curImage.isYUV()) {
Jim Van Verthe24b5872018-10-29 16:26:02 -0400157 GrBackendFormat backendFormats[SkYUVASizeInfo::kMaxCount];
158 void* contexts[SkYUVASizeInfo::kMaxCount] = { nullptr, nullptr, nullptr, nullptr };
159 SkISize sizes[SkYUVASizeInfo::kMaxCount];
Jim Van Verth8f11e432018-10-18 14:36:59 -0400160 // TODO: store this value somewhere?
161 int textureCount;
162 SkAssertResult(SkYUVAIndex::AreValidIndices(curImage.yuvaIndices(), &textureCount));
163 for (int i = 0; i < textureCount; ++i) {
Brian Salomon3f4cd772019-01-11 16:03:19 -0500164 const GrBackendTexture& backendTex = curImage.promiseTexture(i)->backendTexture();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500165 backendFormats[i] = backendTex.getBackendFormat();
166 SkASSERT(backendFormats[i].isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400167 contexts[i] = curImage.refCallbackContext(i).release();
Jim Van Verthf9f07352018-10-24 10:32:20 -0400168 sizes[i].set(curImage.yuvPixmap(i).width(), curImage.yuvPixmap(i).height());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400169 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400170 for (int i = textureCount; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verthf9f07352018-10-24 10:32:20 -0400171 sizes[i] = SkISize::MakeEmpty();
Jim Van Verth8f11e432018-10-18 14:36:59 -0400172 }
Jim Van Verthf99a6742018-10-18 16:13:18 +0000173
Brian Salomon0cc57542019-03-08 13:28:46 -0500174 image = recorder->makeYUVAPromiseTexture(
175 curImage.yuvColorSpace(),
176 backendFormats,
177 sizes,
178 curImage.yuvaIndices(),
179 curImage.overallWidth(),
180 curImage.overallHeight(),
181 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
182 curImage.refOverallColorSpace(),
183 DDLPromiseImageHelper::PromiseImageFulfillProc,
184 DDLPromiseImageHelper::PromiseImageReleaseProc,
185 DDLPromiseImageHelper::PromiseImageDoneProc,
186 contexts,
187 SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500188 for (int i = 0; i < textureCount; ++i) {
189 curImage.callbackContext(i)->wasAddedToImage();
190 }
Robert Phillips193c4212019-03-04 12:18:53 -0500191
192#ifdef SK_DEBUG
193 {
194 // By the peekProxy contract this image should not have a single backing proxy so
195 // should return null. The call should also not trigger the conversion to RGBA.
196 SkImage_GpuYUVA* yuva = reinterpret_cast<SkImage_GpuYUVA*>(image.get());
197 SkASSERT(!yuva->peekProxy());
198 SkASSERT(!yuva->peekProxy()); // the first call didn't force a conversion to RGBA
199 }
200#endif
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400201 } else {
Brian Salomon3f4cd772019-01-11 16:03:19 -0500202 const GrBackendTexture& backendTex = curImage.promiseTexture(0)->backendTexture();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500203 GrBackendFormat backendFormat = backendTex.getBackendFormat();
204 SkASSERT(backendFormat.isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400205
206 // Each DDL recorder gets its own ref on the promise callback context for the
207 // promise images it creates.
208 // DDL TODO: sort out mipmapping
Brian Salomon0cc57542019-03-08 13:28:46 -0500209 image = recorder->makePromiseTexture(
210 backendFormat,
211 curImage.overallWidth(),
212 curImage.overallHeight(),
213 GrMipMapped::kNo,
214 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
215 curImage.overallColorType(),
216 curImage.overallAlphaType(),
217 curImage.refOverallColorSpace(),
218 DDLPromiseImageHelper::PromiseImageFulfillProc,
219 DDLPromiseImageHelper::PromiseImageReleaseProc,
220 DDLPromiseImageHelper::PromiseImageDoneProc,
221 (void*)curImage.refCallbackContext(0).release(),
222 SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500223 curImage.callbackContext(0)->wasAddedToImage();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400224 }
Robert Phillips96601082018-05-29 16:13:26 -0400225 perRecorderContext->fPromiseImages->push_back(image);
226 SkASSERT(image);
227 return image;
228}
229
230int DDLPromiseImageHelper::findImage(SkImage* image) const {
231 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400232 if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here
233 SkASSERT(fImageInfo[i].index() == i);
234 SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index()));
Robert Phillips96601082018-05-29 16:13:26 -0400235 return i;
236 }
237 }
238 return -1;
239}
240
241int DDLPromiseImageHelper::addImage(SkImage* image) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400242 SkImage_Base* ib = as_IB(image);
Robert Phillips96601082018-05-29 16:13:26 -0400243
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400244 SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(),
Robert Phillips13371a12019-05-13 15:59:10 -0400245 image->colorType() == kBGRA_8888_SkColorType
246 ? kRGBA_8888_SkColorType
247 : image->colorType(),
248 image->alphaType(),
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400249 image->refColorSpace());
Robert Phillips96601082018-05-29 16:13:26 -0400250
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400251 PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.count(),
252 image->uniqueID(),
253 overallII);
Robert Phillips96601082018-05-29 16:13:26 -0400254
Jim Van Verthe24b5872018-10-29 16:26:02 -0400255 SkYUVASizeInfo yuvaSizeInfo;
Jim Van Verth8f11e432018-10-18 14:36:59 -0400256 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount];
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400257 SkYUVColorSpace yuvColorSpace;
Jim Van Verthe24b5872018-10-29 16:26:02 -0400258 const void* planes[SkYUVASizeInfo::kMaxCount];
Jim Van Verth8f11e432018-10-18 14:36:59 -0400259 sk_sp<SkCachedData> yuvData = ib->getPlanes(&yuvaSizeInfo, yuvaIndices, &yuvColorSpace, planes);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400260 if (yuvData) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400261 newImageInfo.setYUVData(std::move(yuvData), yuvaIndices, yuvColorSpace);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400262
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400263 // determine colortypes from index data
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400264 // for testing we only ever use A8, RG_88
Jim Van Verthe24b5872018-10-29 16:26:02 -0400265 SkColorType colorTypes[SkYUVASizeInfo::kMaxCount] = {
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400266 kUnknown_SkColorType, kUnknown_SkColorType,
267 kUnknown_SkColorType, kUnknown_SkColorType
268 };
269 for (int yuvIndex = 0; yuvIndex < SkYUVAIndex::kIndexCount; ++yuvIndex) {
270 int texIdx = yuvaIndices[yuvIndex].fIndex;
271 if (texIdx < 0) {
272 SkASSERT(SkYUVAIndex::kA_Index == yuvIndex);
273 continue;
274 }
275 if (kUnknown_SkColorType == colorTypes[texIdx]) {
276 colorTypes[texIdx] = kAlpha_8_SkColorType;
277 } else {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400278 colorTypes[texIdx] = kR8G8_unorm_SkColorType;
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400279 }
280 }
281
Jim Van Verthe24b5872018-10-29 16:26:02 -0400282 for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400283 if (yuvaSizeInfo.fSizes[i].isEmpty()) {
284 SkASSERT(!yuvaSizeInfo.fWidthBytes[i] && kUnknown_SkColorType == colorTypes[i]);
Jim Van Verth8f11e432018-10-18 14:36:59 -0400285 continue;
286 }
287
288 SkImageInfo planeII = SkImageInfo::Make(yuvaSizeInfo.fSizes[i].fWidth,
289 yuvaSizeInfo.fSizes[i].fHeight,
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400290 colorTypes[i],
Jim Van Verth8f11e432018-10-18 14:36:59 -0400291 kUnpremul_SkAlphaType);
292 newImageInfo.addYUVPlane(i, planeII, planes[i], yuvaSizeInfo.fWidthBytes[i]);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400293 }
294 } else {
295 sk_sp<SkImage> rasterImage = image->makeRasterImage(); // force decoding of lazy images
296
297 SkBitmap tmp;
298 tmp.allocPixels(overallII);
299
300 if (!rasterImage->readPixels(tmp.pixmap(), 0, 0)) {
301 return -1;
302 }
303
304 tmp.setImmutable();
305 newImageInfo.setNormalBitmap(tmp);
Robert Phillips96601082018-05-29 16:13:26 -0400306 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400307 // In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU
Robert Phillips96601082018-05-29 16:13:26 -0400308
309 return fImageInfo.count()-1;
310}
311
312int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) {
313 int preExistingID = this->findImage(image);
314 if (preExistingID >= 0) {
315 SkASSERT(this->isValidID(preExistingID));
316 return preExistingID;
317 }
318
319 int newID = this->addImage(image);
320 SkASSERT(this->isValidID(newID));
321 return newID;
322}