blob: 29068430babe683b7e65e4897eb97c2ecbaabf30 [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"
11#include "include/core/SkYUVAIndex.h"
12#include "include/core/SkYUVASizeInfo.h"
13#include "include/gpu/GrContext.h"
14#include "src/core/SkCachedData.h"
15#include "src/gpu/GrContextPriv.h"
16#include "src/gpu/GrGpu.h"
17#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
Jim Van Verth60ac5d02018-12-06 13:11:53 -050058// needed until we have SkRG_88_ColorType;
Robert Phillipscb1adb42019-06-10 15:09:34 -040059static GrBackendTexture create_yuva_texture(GrContext* context, const SkPixmap& pm,
Jim Van Verth60ac5d02018-12-06 13:11:53 -050060 const SkYUVAIndex yuvaIndices[4], int texIndex) {
61 SkASSERT(texIndex >= 0 && texIndex <= 3);
62 int channelCount = 0;
63 for (int i = 0; i < SkYUVAIndex::kIndexCount; ++i) {
64 if (yuvaIndices[i].fIndex == texIndex) {
65 ++channelCount;
66 }
67 }
68 // Need to create an RG texture for two-channel planes
69 GrBackendTexture tex;
70 if (2 == channelCount) {
Robert Phillipscb1adb42019-06-10 15:09:34 -040071 const GrCaps* caps = context->priv().caps();
72 GrGpu* gpu = context->priv().getGpu();
73
Jim Van Verth60ac5d02018-12-06 13:11:53 -050074 SkASSERT(kRGBA_8888_SkColorType == pm.colorType());
75 SkAutoTMalloc<char> pixels(2 * pm.width()*pm.height());
76 char* currPixel = pixels;
77 for (int y = 0; y < pm.height(); ++y) {
78 for (int x = 0; x < pm.width(); ++x) {
79 SkColor color = pm.getColor(x, y);
80 currPixel[0] = SkColorGetR(color);
81 currPixel[1] = SkColorGetG(color);
82 currPixel += 2;
83 }
84 }
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040085
Greg Daniele877dce2019-07-11 10:52:43 -040086 GrBackendFormat format = caps->getBackendFormatFromColorType(GrColorType::kRG_88);
Robert Phillipsf0313ee2019-05-21 13:51:11 -040087 tex = gpu->createBackendTexture(pm.width(), pm.height(), format,
88 GrMipMapped::kNo, GrRenderable::kNo,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040089 pixels, 2 * pm.width(), nullptr, GrProtected::kNo);
Jim Van Verth60ac5d02018-12-06 13:11:53 -050090 } else {
Robert Phillipsda2e67a2019-07-01 15:04:06 -040091 tex = context->priv().createBackendTexture(&pm, 1, GrRenderable::kNo, GrProtected::kNo);
Jim Van Verth60ac5d02018-12-06 13:11:53 -050092 }
93 return tex;
94}
95
Robert Phillips96601082018-05-29 16:13:26 -040096void DDLPromiseImageHelper::uploadAllToGPU(GrContext* context) {
Brian Salomon426ba462019-01-10 16:33:06 +000097 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -040098 const PromiseImageInfo& info = fImageInfo[i];
99
100 // DDL TODO: how can we tell if we need mipmapping!
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400101 if (info.isYUV()) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400102 int numPixmaps;
103 SkAssertResult(SkYUVAIndex::AreValidIndices(info.yuvaIndices(), &numPixmaps));
104 for (int j = 0; j < numPixmaps; ++j) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400105 const SkPixmap& yuvPixmap = info.yuvPixmap(j);
Robert Phillips96601082018-05-29 16:13:26 -0400106
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400107 sk_sp<PromiseImageCallbackContext> callbackContext(
108 new PromiseImageCallbackContext(context));
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500109
Robert Phillipscb1adb42019-06-10 15:09:34 -0400110 callbackContext->setBackendTexture(create_yuva_texture(context, yuvPixmap,
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500111 info.yuvaIndices(), j));
Brian Salomon3f4cd772019-01-11 16:03:19 -0500112 SkASSERT(callbackContext->promiseImageTexture());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400113
114 fImageInfo[i].setCallbackContext(j, std::move(callbackContext));
115 }
116 } else {
117 sk_sp<PromiseImageCallbackContext> callbackContext(
118 new PromiseImageCallbackContext(context));
119
120 const SkBitmap& bm = info.normalBitmap();
121
Robert Phillipscb1adb42019-06-10 15:09:34 -0400122 GrBackendTexture backendTex = context->priv().createBackendTexture(
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400123 &bm.pixmap(), 1, GrRenderable::kNo,
124 GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -0400125
126 callbackContext->setBackendTexture(backendTex);
127
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400128 // The GMs sometimes request too large an image
129 //SkAssertResult(callbackContext->backendTexture().isValid());
130
131 fImageInfo[i].setCallbackContext(0, std::move(callbackContext));
132 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500133 }
134}
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400135
Robert Phillips96601082018-05-29 16:13:26 -0400136sk_sp<SkPicture> DDLPromiseImageHelper::reinflateSKP(
137 SkDeferredDisplayListRecorder* recorder,
138 SkData* compressedPictureData,
139 SkTArray<sk_sp<SkImage>>* promiseImages) const {
140 PerRecorderContext perRecorderContext { recorder, this, promiseImages };
141
142 SkDeserialProcs procs;
143 procs.fImageCtx = (void*) &perRecorderContext;
144 procs.fImageProc = PromiseImageCreator;
145
146 return SkPicture::MakeFromData(compressedPictureData, &procs);
147}
148
149// This generates promise images to replace the indices in the compressed picture. This
150// reconstitution is performed separately in each thread so we end up with multiple
151// promise images referring to the same GrBackendTexture.
152sk_sp<SkImage> DDLPromiseImageHelper::PromiseImageCreator(const void* rawData,
153 size_t length, void* ctxIn) {
154 PerRecorderContext* perRecorderContext = static_cast<PerRecorderContext*>(ctxIn);
155 const DDLPromiseImageHelper* helper = perRecorderContext->fHelper;
156 SkDeferredDisplayListRecorder* recorder = perRecorderContext->fRecorder;
157
158 SkASSERT(length == sizeof(int));
159
160 const int* indexPtr = static_cast<const int*>(rawData);
161 SkASSERT(helper->isValidID(*indexPtr));
162
163 const DDLPromiseImageHelper::PromiseImageInfo& curImage = helper->getInfo(*indexPtr);
164
Brian Salomon3f4cd772019-01-11 16:03:19 -0500165 if (!curImage.promiseTexture(0)) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400166 SkASSERT(!curImage.isYUV());
Robert Phillips96601082018-05-29 16:13:26 -0400167 // We weren't able to make a backend texture for this SkImage. In this case we create
168 // a separate bitmap-backed image for each thread.
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400169 SkASSERT(curImage.normalBitmap().isImmutable());
170 return SkImage::MakeFromBitmap(curImage.normalBitmap());
Robert Phillips96601082018-05-29 16:13:26 -0400171 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400172 SkASSERT(curImage.index() == *indexPtr);
Robert Phillips96601082018-05-29 16:13:26 -0400173
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400174 sk_sp<SkImage> image;
175 if (curImage.isYUV()) {
Jim Van Verthe24b5872018-10-29 16:26:02 -0400176 GrBackendFormat backendFormats[SkYUVASizeInfo::kMaxCount];
177 void* contexts[SkYUVASizeInfo::kMaxCount] = { nullptr, nullptr, nullptr, nullptr };
178 SkISize sizes[SkYUVASizeInfo::kMaxCount];
Jim Van Verth8f11e432018-10-18 14:36:59 -0400179 // TODO: store this value somewhere?
180 int textureCount;
181 SkAssertResult(SkYUVAIndex::AreValidIndices(curImage.yuvaIndices(), &textureCount));
182 for (int i = 0; i < textureCount; ++i) {
Brian Salomon3f4cd772019-01-11 16:03:19 -0500183 const GrBackendTexture& backendTex = curImage.promiseTexture(i)->backendTexture();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500184 backendFormats[i] = backendTex.getBackendFormat();
185 SkASSERT(backendFormats[i].isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400186 contexts[i] = curImage.refCallbackContext(i).release();
Jim Van Verthf9f07352018-10-24 10:32:20 -0400187 sizes[i].set(curImage.yuvPixmap(i).width(), curImage.yuvPixmap(i).height());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400188 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400189 for (int i = textureCount; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verthf9f07352018-10-24 10:32:20 -0400190 sizes[i] = SkISize::MakeEmpty();
Jim Van Verth8f11e432018-10-18 14:36:59 -0400191 }
Jim Van Verthf99a6742018-10-18 16:13:18 +0000192
Brian Salomon0cc57542019-03-08 13:28:46 -0500193 image = recorder->makeYUVAPromiseTexture(
194 curImage.yuvColorSpace(),
195 backendFormats,
196 sizes,
197 curImage.yuvaIndices(),
198 curImage.overallWidth(),
199 curImage.overallHeight(),
200 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
201 curImage.refOverallColorSpace(),
202 DDLPromiseImageHelper::PromiseImageFulfillProc,
203 DDLPromiseImageHelper::PromiseImageReleaseProc,
204 DDLPromiseImageHelper::PromiseImageDoneProc,
205 contexts,
206 SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500207 for (int i = 0; i < textureCount; ++i) {
208 curImage.callbackContext(i)->wasAddedToImage();
209 }
Robert Phillips193c4212019-03-04 12:18:53 -0500210
211#ifdef SK_DEBUG
212 {
213 // By the peekProxy contract this image should not have a single backing proxy so
214 // should return null. The call should also not trigger the conversion to RGBA.
215 SkImage_GpuYUVA* yuva = reinterpret_cast<SkImage_GpuYUVA*>(image.get());
216 SkASSERT(!yuva->peekProxy());
217 SkASSERT(!yuva->peekProxy()); // the first call didn't force a conversion to RGBA
218 }
219#endif
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400220 } else {
Brian Salomon3f4cd772019-01-11 16:03:19 -0500221 const GrBackendTexture& backendTex = curImage.promiseTexture(0)->backendTexture();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500222 GrBackendFormat backendFormat = backendTex.getBackendFormat();
223 SkASSERT(backendFormat.isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400224
225 // Each DDL recorder gets its own ref on the promise callback context for the
226 // promise images it creates.
227 // DDL TODO: sort out mipmapping
Brian Salomon0cc57542019-03-08 13:28:46 -0500228 image = recorder->makePromiseTexture(
229 backendFormat,
230 curImage.overallWidth(),
231 curImage.overallHeight(),
232 GrMipMapped::kNo,
233 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
234 curImage.overallColorType(),
235 curImage.overallAlphaType(),
236 curImage.refOverallColorSpace(),
237 DDLPromiseImageHelper::PromiseImageFulfillProc,
238 DDLPromiseImageHelper::PromiseImageReleaseProc,
239 DDLPromiseImageHelper::PromiseImageDoneProc,
240 (void*)curImage.refCallbackContext(0).release(),
241 SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500242 curImage.callbackContext(0)->wasAddedToImage();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400243 }
Robert Phillips96601082018-05-29 16:13:26 -0400244 perRecorderContext->fPromiseImages->push_back(image);
245 SkASSERT(image);
246 return image;
247}
248
249int DDLPromiseImageHelper::findImage(SkImage* image) const {
250 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400251 if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here
252 SkASSERT(fImageInfo[i].index() == i);
253 SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index()));
Robert Phillips96601082018-05-29 16:13:26 -0400254 return i;
255 }
256 }
257 return -1;
258}
259
260int DDLPromiseImageHelper::addImage(SkImage* image) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400261 SkImage_Base* ib = as_IB(image);
Robert Phillips96601082018-05-29 16:13:26 -0400262
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400263 SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(),
Robert Phillips13371a12019-05-13 15:59:10 -0400264 image->colorType() == kBGRA_8888_SkColorType
265 ? kRGBA_8888_SkColorType
266 : image->colorType(),
267 image->alphaType(),
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400268 image->refColorSpace());
Robert Phillips96601082018-05-29 16:13:26 -0400269
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400270 PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.count(),
271 image->uniqueID(),
272 overallII);
Robert Phillips96601082018-05-29 16:13:26 -0400273
Jim Van Verthe24b5872018-10-29 16:26:02 -0400274 SkYUVASizeInfo yuvaSizeInfo;
Jim Van Verth8f11e432018-10-18 14:36:59 -0400275 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount];
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400276 SkYUVColorSpace yuvColorSpace;
Jim Van Verthe24b5872018-10-29 16:26:02 -0400277 const void* planes[SkYUVASizeInfo::kMaxCount];
Jim Van Verth8f11e432018-10-18 14:36:59 -0400278 sk_sp<SkCachedData> yuvData = ib->getPlanes(&yuvaSizeInfo, yuvaIndices, &yuvColorSpace, planes);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400279 if (yuvData) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400280 newImageInfo.setYUVData(std::move(yuvData), yuvaIndices, yuvColorSpace);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400281
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400282 // determine colortypes from index data
283 // for testing we only ever use A8 or RGBA8888
Jim Van Verthe24b5872018-10-29 16:26:02 -0400284 SkColorType colorTypes[SkYUVASizeInfo::kMaxCount] = {
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400285 kUnknown_SkColorType, kUnknown_SkColorType,
286 kUnknown_SkColorType, kUnknown_SkColorType
287 };
288 for (int yuvIndex = 0; yuvIndex < SkYUVAIndex::kIndexCount; ++yuvIndex) {
289 int texIdx = yuvaIndices[yuvIndex].fIndex;
290 if (texIdx < 0) {
291 SkASSERT(SkYUVAIndex::kA_Index == yuvIndex);
292 continue;
293 }
294 if (kUnknown_SkColorType == colorTypes[texIdx]) {
295 colorTypes[texIdx] = kAlpha_8_SkColorType;
296 } else {
297 colorTypes[texIdx] = kRGBA_8888_SkColorType;
298 }
299 }
300
Jim Van Verthe24b5872018-10-29 16:26:02 -0400301 for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400302 if (yuvaSizeInfo.fSizes[i].isEmpty()) {
303 SkASSERT(!yuvaSizeInfo.fWidthBytes[i] && kUnknown_SkColorType == colorTypes[i]);
Jim Van Verth8f11e432018-10-18 14:36:59 -0400304 continue;
305 }
306
307 SkImageInfo planeII = SkImageInfo::Make(yuvaSizeInfo.fSizes[i].fWidth,
308 yuvaSizeInfo.fSizes[i].fHeight,
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400309 colorTypes[i],
Jim Van Verth8f11e432018-10-18 14:36:59 -0400310 kUnpremul_SkAlphaType);
311 newImageInfo.addYUVPlane(i, planeII, planes[i], yuvaSizeInfo.fWidthBytes[i]);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400312 }
313 } else {
314 sk_sp<SkImage> rasterImage = image->makeRasterImage(); // force decoding of lazy images
315
316 SkBitmap tmp;
317 tmp.allocPixels(overallII);
318
319 if (!rasterImage->readPixels(tmp.pixmap(), 0, 0)) {
320 return -1;
321 }
322
323 tmp.setImmutable();
324 newImageInfo.setNormalBitmap(tmp);
Robert Phillips96601082018-05-29 16:13:26 -0400325 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400326 // In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU
Robert Phillips96601082018-05-29 16:13:26 -0400327
328 return fImageInfo.count()-1;
329}
330
331int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) {
332 int preExistingID = this->findImage(image);
333 if (preExistingID >= 0) {
334 SkASSERT(this->isValidID(preExistingID));
335 return preExistingID;
336 }
337
338 int newID = this->addImage(image);
339 SkASSERT(this->isValidID(newID));
340 return newID;
341}