blob: dc0118516446973b709b69f06c5fde83ac87fe2b [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
Robert Phillips0a15cc62019-07-30 12:49:10 -040086 GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRG_88,
87 GrRenderable::kNo);
Robert Phillipsf0313ee2019-05-21 13:51:11 -040088 tex = gpu->createBackendTexture(pm.width(), pm.height(), format,
89 GrMipMapped::kNo, GrRenderable::kNo,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040090 pixels, 2 * pm.width(), nullptr, GrProtected::kNo);
Jim Van Verth60ac5d02018-12-06 13:11:53 -050091 } else {
Robert Phillipsda2e67a2019-07-01 15:04:06 -040092 tex = context->priv().createBackendTexture(&pm, 1, GrRenderable::kNo, GrProtected::kNo);
Jim Van Verth60ac5d02018-12-06 13:11:53 -050093 }
94 return tex;
95}
96
Robert Phillips96601082018-05-29 16:13:26 -040097void DDLPromiseImageHelper::uploadAllToGPU(GrContext* context) {
Brian Salomon426ba462019-01-10 16:33:06 +000098 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillips96601082018-05-29 16:13:26 -040099 const PromiseImageInfo& info = fImageInfo[i];
100
101 // DDL TODO: how can we tell if we need mipmapping!
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400102 if (info.isYUV()) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400103 int numPixmaps;
104 SkAssertResult(SkYUVAIndex::AreValidIndices(info.yuvaIndices(), &numPixmaps));
105 for (int j = 0; j < numPixmaps; ++j) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400106 const SkPixmap& yuvPixmap = info.yuvPixmap(j);
Robert Phillips96601082018-05-29 16:13:26 -0400107
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400108 sk_sp<PromiseImageCallbackContext> callbackContext(
109 new PromiseImageCallbackContext(context));
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500110
Robert Phillipscb1adb42019-06-10 15:09:34 -0400111 callbackContext->setBackendTexture(create_yuva_texture(context, yuvPixmap,
Jim Van Verth60ac5d02018-12-06 13:11:53 -0500112 info.yuvaIndices(), j));
Brian Salomon3f4cd772019-01-11 16:03:19 -0500113 SkASSERT(callbackContext->promiseImageTexture());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400114
115 fImageInfo[i].setCallbackContext(j, std::move(callbackContext));
116 }
117 } else {
118 sk_sp<PromiseImageCallbackContext> callbackContext(
119 new PromiseImageCallbackContext(context));
120
121 const SkBitmap& bm = info.normalBitmap();
122
Robert Phillipscb1adb42019-06-10 15:09:34 -0400123 GrBackendTexture backendTex = context->priv().createBackendTexture(
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400124 &bm.pixmap(), 1, GrRenderable::kNo,
125 GrProtected::kNo);
Robert Phillipscb1adb42019-06-10 15:09:34 -0400126
127 callbackContext->setBackendTexture(backendTex);
128
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400129 // The GMs sometimes request too large an image
130 //SkAssertResult(callbackContext->backendTexture().isValid());
131
132 fImageInfo[i].setCallbackContext(0, std::move(callbackContext));
133 }
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500134 }
135}
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400136
Robert Phillips96601082018-05-29 16:13:26 -0400137sk_sp<SkPicture> DDLPromiseImageHelper::reinflateSKP(
138 SkDeferredDisplayListRecorder* recorder,
139 SkData* compressedPictureData,
140 SkTArray<sk_sp<SkImage>>* promiseImages) const {
141 PerRecorderContext perRecorderContext { recorder, this, promiseImages };
142
143 SkDeserialProcs procs;
144 procs.fImageCtx = (void*) &perRecorderContext;
145 procs.fImageProc = PromiseImageCreator;
146
147 return SkPicture::MakeFromData(compressedPictureData, &procs);
148}
149
150// This generates promise images to replace the indices in the compressed picture. This
151// reconstitution is performed separately in each thread so we end up with multiple
152// promise images referring to the same GrBackendTexture.
153sk_sp<SkImage> DDLPromiseImageHelper::PromiseImageCreator(const void* rawData,
154 size_t length, void* ctxIn) {
155 PerRecorderContext* perRecorderContext = static_cast<PerRecorderContext*>(ctxIn);
156 const DDLPromiseImageHelper* helper = perRecorderContext->fHelper;
157 SkDeferredDisplayListRecorder* recorder = perRecorderContext->fRecorder;
158
159 SkASSERT(length == sizeof(int));
160
161 const int* indexPtr = static_cast<const int*>(rawData);
162 SkASSERT(helper->isValidID(*indexPtr));
163
164 const DDLPromiseImageHelper::PromiseImageInfo& curImage = helper->getInfo(*indexPtr);
165
Brian Salomon3f4cd772019-01-11 16:03:19 -0500166 if (!curImage.promiseTexture(0)) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400167 SkASSERT(!curImage.isYUV());
Robert Phillips96601082018-05-29 16:13:26 -0400168 // We weren't able to make a backend texture for this SkImage. In this case we create
169 // a separate bitmap-backed image for each thread.
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400170 SkASSERT(curImage.normalBitmap().isImmutable());
171 return SkImage::MakeFromBitmap(curImage.normalBitmap());
Robert Phillips96601082018-05-29 16:13:26 -0400172 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400173 SkASSERT(curImage.index() == *indexPtr);
Robert Phillips96601082018-05-29 16:13:26 -0400174
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400175 sk_sp<SkImage> image;
176 if (curImage.isYUV()) {
Jim Van Verthe24b5872018-10-29 16:26:02 -0400177 GrBackendFormat backendFormats[SkYUVASizeInfo::kMaxCount];
178 void* contexts[SkYUVASizeInfo::kMaxCount] = { nullptr, nullptr, nullptr, nullptr };
179 SkISize sizes[SkYUVASizeInfo::kMaxCount];
Jim Van Verth8f11e432018-10-18 14:36:59 -0400180 // TODO: store this value somewhere?
181 int textureCount;
182 SkAssertResult(SkYUVAIndex::AreValidIndices(curImage.yuvaIndices(), &textureCount));
183 for (int i = 0; i < textureCount; ++i) {
Brian Salomon3f4cd772019-01-11 16:03:19 -0500184 const GrBackendTexture& backendTex = curImage.promiseTexture(i)->backendTexture();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500185 backendFormats[i] = backendTex.getBackendFormat();
186 SkASSERT(backendFormats[i].isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400187 contexts[i] = curImage.refCallbackContext(i).release();
Jim Van Verthf9f07352018-10-24 10:32:20 -0400188 sizes[i].set(curImage.yuvPixmap(i).width(), curImage.yuvPixmap(i).height());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400189 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400190 for (int i = textureCount; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verthf9f07352018-10-24 10:32:20 -0400191 sizes[i] = SkISize::MakeEmpty();
Jim Van Verth8f11e432018-10-18 14:36:59 -0400192 }
Jim Van Verthf99a6742018-10-18 16:13:18 +0000193
Brian Salomon0cc57542019-03-08 13:28:46 -0500194 image = recorder->makeYUVAPromiseTexture(
195 curImage.yuvColorSpace(),
196 backendFormats,
197 sizes,
198 curImage.yuvaIndices(),
199 curImage.overallWidth(),
200 curImage.overallHeight(),
201 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
202 curImage.refOverallColorSpace(),
203 DDLPromiseImageHelper::PromiseImageFulfillProc,
204 DDLPromiseImageHelper::PromiseImageReleaseProc,
205 DDLPromiseImageHelper::PromiseImageDoneProc,
206 contexts,
207 SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500208 for (int i = 0; i < textureCount; ++i) {
209 curImage.callbackContext(i)->wasAddedToImage();
210 }
Robert Phillips193c4212019-03-04 12:18:53 -0500211
212#ifdef SK_DEBUG
213 {
214 // By the peekProxy contract this image should not have a single backing proxy so
215 // should return null. The call should also not trigger the conversion to RGBA.
216 SkImage_GpuYUVA* yuva = reinterpret_cast<SkImage_GpuYUVA*>(image.get());
217 SkASSERT(!yuva->peekProxy());
218 SkASSERT(!yuva->peekProxy()); // the first call didn't force a conversion to RGBA
219 }
220#endif
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400221 } else {
Brian Salomon3f4cd772019-01-11 16:03:19 -0500222 const GrBackendTexture& backendTex = curImage.promiseTexture(0)->backendTexture();
Brian Salomonf391d0f2018-12-14 09:18:50 -0500223 GrBackendFormat backendFormat = backendTex.getBackendFormat();
224 SkASSERT(backendFormat.isValid());
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400225
226 // Each DDL recorder gets its own ref on the promise callback context for the
227 // promise images it creates.
228 // DDL TODO: sort out mipmapping
Brian Salomon0cc57542019-03-08 13:28:46 -0500229 image = recorder->makePromiseTexture(
230 backendFormat,
231 curImage.overallWidth(),
232 curImage.overallHeight(),
233 GrMipMapped::kNo,
234 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
235 curImage.overallColorType(),
236 curImage.overallAlphaType(),
237 curImage.refOverallColorSpace(),
238 DDLPromiseImageHelper::PromiseImageFulfillProc,
239 DDLPromiseImageHelper::PromiseImageReleaseProc,
240 DDLPromiseImageHelper::PromiseImageDoneProc,
241 (void*)curImage.refCallbackContext(0).release(),
242 SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
Brian Salomoncdd8a0a2019-01-10 12:09:52 -0500243 curImage.callbackContext(0)->wasAddedToImage();
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400244 }
Robert Phillips96601082018-05-29 16:13:26 -0400245 perRecorderContext->fPromiseImages->push_back(image);
246 SkASSERT(image);
247 return image;
248}
249
250int DDLPromiseImageHelper::findImage(SkImage* image) const {
251 for (int i = 0; i < fImageInfo.count(); ++i) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400252 if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here
253 SkASSERT(fImageInfo[i].index() == i);
254 SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index()));
Robert Phillips96601082018-05-29 16:13:26 -0400255 return i;
256 }
257 }
258 return -1;
259}
260
261int DDLPromiseImageHelper::addImage(SkImage* image) {
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400262 SkImage_Base* ib = as_IB(image);
Robert Phillips96601082018-05-29 16:13:26 -0400263
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400264 SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(),
Robert Phillips13371a12019-05-13 15:59:10 -0400265 image->colorType() == kBGRA_8888_SkColorType
266 ? kRGBA_8888_SkColorType
267 : image->colorType(),
268 image->alphaType(),
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400269 image->refColorSpace());
Robert Phillips96601082018-05-29 16:13:26 -0400270
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400271 PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.count(),
272 image->uniqueID(),
273 overallII);
Robert Phillips96601082018-05-29 16:13:26 -0400274
Jim Van Verthe24b5872018-10-29 16:26:02 -0400275 SkYUVASizeInfo yuvaSizeInfo;
Jim Van Verth8f11e432018-10-18 14:36:59 -0400276 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount];
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400277 SkYUVColorSpace yuvColorSpace;
Jim Van Verthe24b5872018-10-29 16:26:02 -0400278 const void* planes[SkYUVASizeInfo::kMaxCount];
Jim Van Verth8f11e432018-10-18 14:36:59 -0400279 sk_sp<SkCachedData> yuvData = ib->getPlanes(&yuvaSizeInfo, yuvaIndices, &yuvColorSpace, planes);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400280 if (yuvData) {
Jim Van Verth8f11e432018-10-18 14:36:59 -0400281 newImageInfo.setYUVData(std::move(yuvData), yuvaIndices, yuvColorSpace);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400282
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400283 // determine colortypes from index data
284 // for testing we only ever use A8 or RGBA8888
Jim Van Verthe24b5872018-10-29 16:26:02 -0400285 SkColorType colorTypes[SkYUVASizeInfo::kMaxCount] = {
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400286 kUnknown_SkColorType, kUnknown_SkColorType,
287 kUnknown_SkColorType, kUnknown_SkColorType
288 };
289 for (int yuvIndex = 0; yuvIndex < SkYUVAIndex::kIndexCount; ++yuvIndex) {
290 int texIdx = yuvaIndices[yuvIndex].fIndex;
291 if (texIdx < 0) {
292 SkASSERT(SkYUVAIndex::kA_Index == yuvIndex);
293 continue;
294 }
295 if (kUnknown_SkColorType == colorTypes[texIdx]) {
296 colorTypes[texIdx] = kAlpha_8_SkColorType;
297 } else {
298 colorTypes[texIdx] = kRGBA_8888_SkColorType;
299 }
300 }
301
Jim Van Verthe24b5872018-10-29 16:26:02 -0400302 for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) {
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400303 if (yuvaSizeInfo.fSizes[i].isEmpty()) {
304 SkASSERT(!yuvaSizeInfo.fWidthBytes[i] && kUnknown_SkColorType == colorTypes[i]);
Jim Van Verth8f11e432018-10-18 14:36:59 -0400305 continue;
306 }
307
308 SkImageInfo planeII = SkImageInfo::Make(yuvaSizeInfo.fSizes[i].fWidth,
309 yuvaSizeInfo.fSizes[i].fHeight,
Jim Van Verthb7f0b9c2018-10-22 14:12:03 -0400310 colorTypes[i],
Jim Van Verth8f11e432018-10-18 14:36:59 -0400311 kUnpremul_SkAlphaType);
312 newImageInfo.addYUVPlane(i, planeII, planes[i], yuvaSizeInfo.fWidthBytes[i]);
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400313 }
314 } else {
315 sk_sp<SkImage> rasterImage = image->makeRasterImage(); // force decoding of lazy images
316
317 SkBitmap tmp;
318 tmp.allocPixels(overallII);
319
320 if (!rasterImage->readPixels(tmp.pixmap(), 0, 0)) {
321 return -1;
322 }
323
324 tmp.setImmutable();
325 newImageInfo.setNormalBitmap(tmp);
Robert Phillips96601082018-05-29 16:13:26 -0400326 }
Robert Phillipse8e2bb12018-09-27 14:26:47 -0400327 // In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU
Robert Phillips96601082018-05-29 16:13:26 -0400328
329 return fImageInfo.count()-1;
330}
331
332int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) {
333 int preExistingID = this->findImage(image);
334 if (preExistingID >= 0) {
335 SkASSERT(this->isValidID(preExistingID));
336 return preExistingID;
337 }
338
339 int newID = this->addImage(image);
340 SkASSERT(this->isValidID(newID));
341 return newID;
342}