Debian Codegraph @

tools/ganesh/DDLPromiseImageHelper.cpp

raw
1 /*
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
8 #include "tools/ganesh/DDLPromiseImageHelper.h"
9
10 #include "include/core/SkPicture.h"
11 #include "include/core/SkSerialProcs.h"
12 #include "include/gpu/ganesh/GrContextThreadSafeProxy.h"
13 #include "include/gpu/ganesh/GrDirectContext.h"
14 #include "include/gpu/ganesh/GrYUVABackendTextures.h"
15 #include "include/gpu/ganesh/SkImageGanesh.h"
16 #include "include/private/chromium/SkImageChromium.h"
17 #include "src/codec/SkCodecImageGenerator.h"
18 #include "src/core/SkCachedData.h"
19 #include "src/core/SkMipmap.h"
20 #include "src/core/SkTaskGroup.h"
21 #include "src/gpu/ganesh/GrCaps.h"
22 #include "src/gpu/ganesh/GrDirectContextPriv.h"
23 #include "src/gpu/ganesh/image/SkImage_GaneshYUVA.h"
24 #include "src/image/SkImage_Base.h"
25
26 DDLPromiseImageHelper::PromiseImageInfo::PromiseImageInfo(int index,
27 uint32_t originalUniqueID,
28 const SkImageInfo& ii)
29 : fIndex(index)
30 , fOriginalUniqueID(originalUniqueID)
31 , fImageInfo(ii) {
32 }
33
34 DDLPromiseImageHelper::PromiseImageInfo::PromiseImageInfo(PromiseImageInfo&& other)
35 : fIndex(other.fIndex)
36 , fOriginalUniqueID(other.fOriginalUniqueID)
37 , fImageInfo(other.fImageInfo)
38 , fBaseLevel(other.fBaseLevel)
39 , fMipLevels(std::move(other.fMipLevels))
40 , fYUVAPixmaps(std::move(other.fYUVAPixmaps)) {
41 for (int i = 0; i < SkYUVAInfo::kMaxPlanes; ++i) {
42 fCallbackContexts[i] = std::move(other.fCallbackContexts[i]);
43 }
44 }
45
46 DDLPromiseImageHelper::PromiseImageInfo::~PromiseImageInfo() {}
47
48 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
62 int DDLPromiseImageHelper::PromiseImageInfo::numMipLevels() const {
63 SkASSERT(!this->isYUV());
64 return fMipLevels ? fMipLevels->countLevels()+1 : 1;
65 }
66
67 void DDLPromiseImageHelper::PromiseImageInfo::setMipLevels(const SkBitmap& baseLevel,
68 std::unique_ptr<SkMipmap> mipLevels) {
69 fBaseLevel = baseLevel;
70 fMipLevels = std::move(mipLevels);
71 }
72
73 ///////////////////////////////////////////////////////////////////////////////////////////////////
74 PromiseImageCallbackContext::~PromiseImageCallbackContext() {
75 SkASSERT(fDoneCnt == fNumImages);
76 SkASSERT(!fTotalFulfills || fDoneCnt);
77
78 if (fPromiseImageTexture) {
79 fContext->deleteBackendTexture(fPromiseImageTexture->backendTexture());
80 }
81 }
82
83 void PromiseImageCallbackContext::setBackendTexture(const GrBackendTexture& backendTexture) {
84 SkASSERT(!fPromiseImageTexture);
85 SkASSERT(fBackendFormat == backendTexture.getBackendFormat());
86 fPromiseImageTexture = GrPromiseImageTexture::Make(backendTexture);
87 }
88
89 void PromiseImageCallbackContext::destroyBackendTexture() {
90 SkASSERT(!fPromiseImageTexture || fPromiseImageTexture->unique());
91
92 if (fPromiseImageTexture) {
93 fContext->deleteBackendTexture(fPromiseImageTexture->backendTexture());
94 }
95 fPromiseImageTexture = nullptr;
96 }
97
98 ///////////////////////////////////////////////////////////////////////////////////////////////////
99
100 sk_sp<SkPicture> DDLPromiseImageHelper::recreateSKP(GrDirectContext* dContext,
101 SkPicture* inputPicture) {
102 SkSerialProcs procs;
103
104 procs.fImageCtx = this;
105 procs.fImageProc = [](SkImage* image, void* ctx) -> SkSerialReturnType {
106 auto helper = static_cast<DDLPromiseImageHelper*>(ctx);
107
108 int id = helper->findOrDefineImage(image);
109
110 // Even if 'id' is invalid (i.e., -1) write it to the SKP
111 return SkData::MakeWithCopy(&id, sizeof(id));
112 };
113
114 auto compressedPictureData = inputPicture->serialize(&procs);
115 if (!compressedPictureData) {
116 return nullptr;
117 }
118
119 this->createCallbackContexts(dContext);
120
121 return this->reinflateSKP(dContext->threadSafeProxy(), compressedPictureData.get());
122 }
123
124 static GrBackendTexture create_yuva_texture(GrDirectContext* direct,
125 const SkPixmap& pm,
126 int texIndex) {
127 SkASSERT(texIndex >= 0 && texIndex <= 3);
128
129 bool finishedBECreate = false;
130 auto markFinished = [](void* context) {
131 *(bool*)context = true;
132 };
133 auto beTex = direct->createBackendTexture(pm,
134 kTopLeft_GrSurfaceOrigin,
135 GrRenderable::kNo,
136 GrProtected::kNo,
137 markFinished,
138 &finishedBECreate,
139 /*label=*/"CreateYuvaTexture");
140 if (beTex.isValid()) {
141 direct->submit();
142 while (!finishedBECreate) {
143 direct->checkAsyncWorkCompletion();
144 }
145 }
146 return beTex;
147 }
148
149 /*
150 * Create backend textures and upload data to them for all the textures required to satisfy
151 * a single promise image.
152 * For YUV textures this will result in up to 4 actual textures.
153 */
154 void DDLPromiseImageHelper::CreateBETexturesForPromiseImage(GrDirectContext* direct,
155 PromiseImageInfo* info) {
156 if (info->isYUV()) {
157 int numPixmaps = info->yuvaInfo().numPlanes();
158 for (int j = 0; j < numPixmaps; ++j) {
159 const SkPixmap& yuvPixmap = info->yuvPixmap(j);
160
161 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
162 SkASSERT(callbackContext);
163
164 // DDL TODO: what should we do with mipmapped YUV images
165 callbackContext->setBackendTexture(create_yuva_texture(direct, yuvPixmap, j));
166 SkASSERT(callbackContext->promiseImageTexture());
167 }
168 } else {
169 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
170 if (!callbackContext) {
171 // This texture would've been too large to fit on the GPU
172 return;
173 }
174
175 std::unique_ptr<SkPixmap[]> mipLevels = info->normalMipLevels();
176
177 bool finishedBECreate = false;
178 auto markFinished = [](void* context) {
179 *(bool*)context = true;
180 };
181 auto backendTex = direct->createBackendTexture(mipLevels.get(),
182 info->numMipLevels(),
183 kTopLeft_GrSurfaceOrigin,
184 GrRenderable::kNo,
185 GrProtected::kNo,
186 markFinished,
187 &finishedBECreate,
188 /*label=*/"CreateBETexturesForPromiseImage");
189 SkASSERT(backendTex.isValid());
190 direct->submit();
191 while (!finishedBECreate) {
192 direct->checkAsyncWorkCompletion();
193 }
194
195 callbackContext->setBackendTexture(backendTex);
196 }
197 }
198
199 void DDLPromiseImageHelper::DeleteBETexturesForPromiseImage(PromiseImageInfo* info) {
200 if (info->isYUV()) {
201 int numPixmaps = info->yuvaInfo().numPlanes();
202 for (int j = 0; j < numPixmaps; ++j) {
203 PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
204 SkASSERT(callbackContext);
205
206 callbackContext->destroyBackendTexture();
207 SkASSERT(!callbackContext->promiseImageTexture());
208 }
209 } else {
210 PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
211 if (!callbackContext) {
212 // This texture would've been too large to fit on the GPU
213 return;
214 }
215
216 callbackContext->destroyBackendTexture();
217 SkASSERT(!callbackContext->promiseImageTexture());
218 }
219 }
220
221 void DDLPromiseImageHelper::createCallbackContexts(GrDirectContext* direct) {
222 const GrCaps* caps = direct->priv().caps();
223 const int maxDimension = caps->maxTextureSize();
224
225 for (int i = 0; i < fImageInfo.size(); ++i) {
226 PromiseImageInfo& info = fImageInfo[i];
227
228 if (info.isYUV()) {
229 int numPixmaps = info.yuvaInfo().numPlanes();
230
231 for (int j = 0; j < numPixmaps; ++j) {
232 const SkPixmap& yuvPixmap = info.yuvPixmap(j);
233
234 GrBackendFormat backendFormat = direct->defaultBackendFormat(yuvPixmap.colorType(),
235 GrRenderable::kNo);
236
237 sk_sp<PromiseImageCallbackContext> callbackContext(
238 new PromiseImageCallbackContext(direct, backendFormat));
239
240 info.setCallbackContext(j, std::move(callbackContext));
241 }
242 } else {
243 const SkBitmap& baseLevel = info.baseLevel();
244
245 // TODO: explicitly mark the PromiseImageInfo as too big and check in uploadAllToGPU
246 if (maxDimension < std::max(baseLevel.width(), baseLevel.height())) {
247 // This won't fit on the GPU. Fallback to a raster-backed image per tile.
248 continue;
249 }
250
251 GrBackendFormat backendFormat = direct->defaultBackendFormat(baseLevel.colorType(),
252 GrRenderable::kNo);
253 if (!caps->isFormatTexturable(backendFormat, GrTextureType::k2D)) {
254 continue;
255 }
256
257 sk_sp<PromiseImageCallbackContext> callbackContext(
258 new PromiseImageCallbackContext(direct, backendFormat));
259
260 info.setCallbackContext(0, std::move(callbackContext));
261 }
262 }
263 }
264
265 void DDLPromiseImageHelper::uploadAllToGPU(SkTaskGroup* taskGroup, GrDirectContext* direct) {
266 if (taskGroup) {
267 for (int i = 0; i < fImageInfo.size(); ++i) {
268 PromiseImageInfo* info = &fImageInfo[i];
269
270 taskGroup->add([direct, info]() { CreateBETexturesForPromiseImage(direct, info); });
271 }
272 } else {
273 for (int i = 0; i < fImageInfo.size(); ++i) {
274 CreateBETexturesForPromiseImage(direct, &fImageInfo[i]);
275 }
276 }
277 }
278
279 void DDLPromiseImageHelper::deleteAllFromGPU(SkTaskGroup* taskGroup, GrDirectContext* direct) {
280 if (taskGroup) {
281 for (int i = 0; i < fImageInfo.size(); ++i) {
282 PromiseImageInfo* info = &fImageInfo[i];
283
284 taskGroup->add([info]() { DeleteBETexturesForPromiseImage(info); });
285 }
286 } else {
287 for (int i = 0; i < fImageInfo.size(); ++i) {
288 DeleteBETexturesForPromiseImage(&fImageInfo[i]);
289 }
290 }
291 }
292
293 sk_sp<SkPicture> DDLPromiseImageHelper::reinflateSKP(
294 sk_sp<GrContextThreadSafeProxy> threadSafeProxy, const SkData* compressedPictureData) {
295 DeserialImageProcContext procContext { std::move(threadSafeProxy), this };
296
297 SkDeserialProcs procs;
298 procs.fImageCtx = (void*) &procContext;
299 procs.fImageProc = CreatePromiseImages;
300
301 return SkPicture::MakeFromData(compressedPictureData, &procs);
302 }
303
304 // This generates promise images to replace the indices in the compressed picture.
305 sk_sp<SkImage> DDLPromiseImageHelper::CreatePromiseImages(const void* rawData,
306 size_t length,
307 void* ctxIn) {
308 DeserialImageProcContext* procContext = static_cast<DeserialImageProcContext*>(ctxIn);
309 DDLPromiseImageHelper* helper = procContext->fHelper;
310
311 SkASSERT(length == sizeof(int));
312
313 const int* indexPtr = static_cast<const int*>(rawData);
314 if (!helper->isValidID(*indexPtr)) {
315 return nullptr;
316 }
317
318 const DDLPromiseImageHelper::PromiseImageInfo& curImage = helper->getInfo(*indexPtr);
319
320 // If there is no callback context that means 'createCallbackContexts' determined the
321 // texture wouldn't fit on the GPU. Create a bitmap-backed image.
322 if (!curImage.isYUV() && !curImage.callbackContext(0)) {
323 SkASSERT(curImage.baseLevel().isImmutable());
324 return curImage.baseLevel().asImage();
325 }
326
327 SkASSERT(curImage.index() == *indexPtr);
328
329 sk_sp<SkImage> image;
330 if (curImage.isYUV()) {
331 GrBackendFormat backendFormats[SkYUVAInfo::kMaxPlanes];
332 const SkYUVAInfo& yuvaInfo = curImage.yuvaInfo();
333 void* contexts[SkYUVAInfo::kMaxPlanes] = {nullptr, nullptr, nullptr, nullptr};
334 int textureCount = yuvaInfo.numPlanes();
335 for (int i = 0; i < textureCount; ++i) {
336 backendFormats[i] = curImage.backendFormat(i);
337 contexts[i] = curImage.refCallbackContext(i).release();
338 }
339 GrYUVABackendTextureInfo yuvaBackendTextures(
340 yuvaInfo, backendFormats, skgpu::Mipmapped::kNo, kTopLeft_GrSurfaceOrigin);
341 image = SkImages::PromiseTextureFromYUVA(
342 procContext->fThreadSafeProxy,
343 yuvaBackendTextures,
344 curImage.refOverallColorSpace(),
345 PromiseImageCallbackContext::PromiseImageFulfillProc,
346 PromiseImageCallbackContext::PromiseImageReleaseProc,
347 contexts);
348 if (!image) {
349 return nullptr;
350 }
351 for (int i = 0; i < textureCount; ++i) {
352 curImage.callbackContext(i)->wasAddedToImage();
353 }
354
355 } else {
356 const GrBackendFormat& backendFormat = curImage.backendFormat(0);
357 SkASSERT(backendFormat.isValid());
358
359 image = SkImages::PromiseTextureFrom(procContext->fThreadSafeProxy,
360 backendFormat,
361 curImage.overallDimensions(),
362 curImage.mipmapped(0),
363 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
364 curImage.overallColorType(),
365 curImage.overallAlphaType(),
366 curImage.refOverallColorSpace(),
367 PromiseImageCallbackContext::PromiseImageFulfillProc,
368 PromiseImageCallbackContext::PromiseImageReleaseProc,
369 (void*)curImage.refCallbackContext(0).release());
370 curImage.callbackContext(0)->wasAddedToImage();
371 }
372 helper->fPromiseImages.push_back(image);
373 SkASSERT(image);
374 return image;
375 }
376
377 int DDLPromiseImageHelper::findImage(SkImage* image) const {
378 for (int i = 0; i < fImageInfo.size(); ++i) {
379 if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here
380 SkASSERT(fImageInfo[i].index() == i);
381 SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index()));
382 return i;
383 }
384 }
385 return -1;
386 }
387
388 int DDLPromiseImageHelper::addImage(SkImage* image) {
389 SkImage_Base* ib = as_IB(image);
390
391 SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(),
392 image->colorType() == kBGRA_8888_SkColorType
393 ? kRGBA_8888_SkColorType
394 : image->colorType(),
395 image->alphaType(),
396 image->refColorSpace());
397
398 PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.size(),
399 image->uniqueID(),
400 overallII);
401
402 auto codec = SkCodecImageGenerator::MakeFromEncodedCodec(ib->refEncodedData());
403 SkYUVAPixmapInfo yuvaInfo;
404 if (codec && codec->queryYUVAInfo(fSupportedYUVADataTypes, &yuvaInfo)) {
405 auto yuvaPixmaps = SkYUVAPixmaps::Allocate(yuvaInfo);
406 if (!codec->getYUVAPlanes(yuvaPixmaps)) {
407 return -1;
408 }
409 SkASSERT(yuvaPixmaps.isValid());
410 newImageInfo.setYUVPlanes(std::move(yuvaPixmaps));
411 } else {
412 sk_sp<SkImage> rasterImage = image->makeRasterImage(nullptr); // force decoding of lazy images
413 if (!rasterImage) {
414 return -1;
415 }
416
417 SkBitmap tmp;
418 tmp.allocPixels(overallII);
419
420 if (!rasterImage->readPixels(nullptr, tmp.pixmap(), 0, 0)) {
421 return -1;
422 }
423
424 tmp.setImmutable();
425
426 // Given how the DDL testing harness works (i.e., only modifying the SkImages w/in an
427 // SKP) we don't know if a given SkImage will require mipmapping. To work around this
428 // we just try to create all the backend textures as mipmapped but, failing that, fall
429 // back to un-mipped.
430 std::unique_ptr<SkMipmap> mipmaps(SkMipmap::Build(tmp.pixmap(), nullptr));
431
432 newImageInfo.setMipLevels(tmp, std::move(mipmaps));
433 }
434 // In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU
435
436 return fImageInfo.size()-1;
437 }
438
439 int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) {
440 int preExistingID = this->findImage(image);
441 if (preExistingID >= 0) {
442 SkASSERT(this->isValidID(preExistingID));
443 return preExistingID;
444 }
445
446 int newID = this->addImage(image);
447 return newID;
448 }