2018-05-29 20:13:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/DDLPromiseImageHelper.h"
|
2018-05-29 20:13:26 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkDeferredDisplayListRecorder.h"
|
2019-09-30 17:21:25 +00:00
|
|
|
#include "include/core/SkPicture.h"
|
|
|
|
#include "include/core/SkSerialProcs.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkYUVAIndex.h"
|
|
|
|
#include "include/core/SkYUVASizeInfo.h"
|
|
|
|
#include "include/gpu/GrContext.h"
|
|
|
|
#include "src/core/SkCachedData.h"
|
2020-04-15 19:54:34 +00:00
|
|
|
#include "src/core/SkMipMap.h"
|
2020-02-14 17:36:37 +00:00
|
|
|
#include "src/core/SkTaskGroup.h"
|
|
|
|
#include "src/gpu/GrContextPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/image/SkImage_Base.h"
|
|
|
|
#include "src/image/SkImage_GpuYUVA.h"
|
2018-05-29 20:13:26 +00:00
|
|
|
|
2020-04-17 20:20:55 +00:00
|
|
|
DDLPromiseImageHelper::PromiseImageInfo::PromiseImageInfo(int index,
|
|
|
|
uint32_t originalUniqueID,
|
|
|
|
const SkImageInfo& ii)
|
|
|
|
: fIndex(index)
|
|
|
|
, fOriginalUniqueID(originalUniqueID)
|
|
|
|
, fImageInfo(ii) {
|
|
|
|
}
|
|
|
|
|
|
|
|
DDLPromiseImageHelper::PromiseImageInfo::PromiseImageInfo(PromiseImageInfo&& other)
|
|
|
|
: fIndex(other.fIndex)
|
|
|
|
, fOriginalUniqueID(other.fOriginalUniqueID)
|
|
|
|
, fImageInfo(other.fImageInfo)
|
|
|
|
, fBaseLevel(other.fBaseLevel)
|
|
|
|
, fMipLevels(std::move(other.fMipLevels))
|
|
|
|
, fYUVData(std::move(other.fYUVData))
|
|
|
|
, fYUVColorSpace(other.fYUVColorSpace) {
|
|
|
|
memcpy(fYUVAIndices, other.fYUVAIndices, sizeof(fYUVAIndices));
|
|
|
|
for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) {
|
|
|
|
fYUVPlanes[i] = other.fYUVPlanes[i];
|
|
|
|
fCallbackContexts[i] = std::move(other.fCallbackContexts[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DDLPromiseImageHelper::PromiseImageInfo::~PromiseImageInfo() {}
|
|
|
|
|
|
|
|
const std::unique_ptr<SkPixmap[]> DDLPromiseImageHelper::PromiseImageInfo::normalMipLevels() const {
|
|
|
|
SkASSERT(!this->isYUV());
|
|
|
|
std::unique_ptr<SkPixmap[]> pixmaps(new SkPixmap[this->numMipLevels()]);
|
|
|
|
pixmaps[0] = fBaseLevel.pixmap();
|
|
|
|
if (fMipLevels) {
|
|
|
|
for (int i = 0; i < fMipLevels->countLevels(); ++i) {
|
|
|
|
SkMipMap::Level mipLevel;
|
|
|
|
fMipLevels->getLevel(i, &mipLevel);
|
|
|
|
pixmaps[i+1] = mipLevel.fPixmap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pixmaps;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DDLPromiseImageHelper::PromiseImageInfo::numMipLevels() const {
|
|
|
|
SkASSERT(!this->isYUV());
|
|
|
|
return fMipLevels ? fMipLevels->countLevels()+1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DDLPromiseImageHelper::PromiseImageInfo::setMipLevels(const SkBitmap& baseLevel,
|
|
|
|
std::unique_ptr<SkMipMap> mipLevels) {
|
|
|
|
fBaseLevel = baseLevel;
|
|
|
|
fMipLevels = std::move(mipLevels);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2020-04-23 19:10:03 +00:00
|
|
|
PromiseImageCallbackContext::~PromiseImageCallbackContext() {
|
2019-01-10 17:09:52 +00:00
|
|
|
SkASSERT(fDoneCnt == fNumImages);
|
|
|
|
SkASSERT(!fUnreleasedFulfills);
|
|
|
|
SkASSERT(fTotalReleases == fTotalFulfills);
|
|
|
|
SkASSERT(!fTotalFulfills || fDoneCnt);
|
2018-05-29 20:13:26 +00:00
|
|
|
|
2019-01-11 21:03:19 +00:00
|
|
|
if (fPromiseImageTexture) {
|
2019-05-20 12:38:07 +00:00
|
|
|
fContext->deleteBackendTexture(fPromiseImageTexture->backendTexture());
|
2018-05-29 20:13:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 19:10:03 +00:00
|
|
|
void PromiseImageCallbackContext::setBackendTexture(const GrBackendTexture& backendTexture) {
|
2019-02-28 15:03:03 +00:00
|
|
|
SkASSERT(!fPromiseImageTexture);
|
2020-02-14 17:36:37 +00:00
|
|
|
SkASSERT(fBackendFormat == backendTexture.getBackendFormat());
|
2019-01-11 21:03:19 +00:00
|
|
|
fPromiseImageTexture = SkPromiseImageTexture::Make(backendTexture);
|
2019-01-10 17:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 20:13:26 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
sk_sp<SkData> DDLPromiseImageHelper::deflateSKP(const SkPicture* inputPicture) {
|
|
|
|
SkSerialProcs procs;
|
|
|
|
|
|
|
|
procs.fImageCtx = this;
|
|
|
|
procs.fImageProc = [](SkImage* image, void* ctx) -> sk_sp<SkData> {
|
|
|
|
auto helper = static_cast<DDLPromiseImageHelper*>(ctx);
|
|
|
|
|
|
|
|
int id = helper->findOrDefineImage(image);
|
|
|
|
|
2019-12-16 20:09:57 +00:00
|
|
|
// Even if 'id' is invalid (i.e., -1) write it to the SKP
|
|
|
|
return SkData::MakeWithCopy(&id, sizeof(id));
|
2018-05-29 20:13:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return inputPicture->serialize(&procs);
|
|
|
|
}
|
|
|
|
|
2019-06-10 19:09:34 +00:00
|
|
|
static GrBackendTexture create_yuva_texture(GrContext* context, const SkPixmap& pm,
|
2018-12-06 18:11:53 +00:00
|
|
|
const SkYUVAIndex yuvaIndices[4], int texIndex) {
|
|
|
|
SkASSERT(texIndex >= 0 && texIndex <= 3);
|
2019-09-04 19:05:35 +00:00
|
|
|
|
|
|
|
#ifdef SK_DEBUG
|
2018-12-06 18:11:53 +00:00
|
|
|
int channelCount = 0;
|
|
|
|
for (int i = 0; i < SkYUVAIndex::kIndexCount; ++i) {
|
|
|
|
if (yuvaIndices[i].fIndex == texIndex) {
|
|
|
|
++channelCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (2 == channelCount) {
|
2019-09-19 20:05:48 +00:00
|
|
|
SkASSERT(kR8G8_unorm_SkColorType == pm.colorType());
|
2018-12-06 18:11:53 +00:00
|
|
|
}
|
2019-09-04 19:05:35 +00:00
|
|
|
#endif
|
|
|
|
|
2019-09-30 17:21:25 +00:00
|
|
|
return context->createBackendTexture(&pm, 1, GrRenderable::kNo, GrProtected::kNo);
|
2018-12-06 18:11:53 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 17:36:37 +00:00
|
|
|
/*
|
|
|
|
* Create backend textures and upload data to them for all the textures required to satisfy
|
|
|
|
* a single promise image.
|
|
|
|
* For YUV textures this will result in up to 4 actual textures.
|
|
|
|
*/
|
|
|
|
void DDLPromiseImageHelper::CreateBETexturesForPromiseImage(GrContext* context,
|
|
|
|
PromiseImageInfo* info) {
|
|
|
|
SkASSERT(context->priv().asDirectContext());
|
|
|
|
|
|
|
|
if (info->isYUV()) {
|
|
|
|
int numPixmaps;
|
|
|
|
SkAssertResult(SkYUVAIndex::AreValidIndices(info->yuvaIndices(), &numPixmaps));
|
|
|
|
for (int j = 0; j < numPixmaps; ++j) {
|
|
|
|
const SkPixmap& yuvPixmap = info->yuvPixmap(j);
|
|
|
|
|
|
|
|
PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
|
|
|
|
SkASSERT(callbackContext);
|
|
|
|
|
2020-04-15 19:54:34 +00:00
|
|
|
// DDL TODO: what should we do with mipmapped YUV images
|
2020-02-14 17:36:37 +00:00
|
|
|
callbackContext->setBackendTexture(create_yuva_texture(context, yuvPixmap,
|
|
|
|
info->yuvaIndices(), j));
|
|
|
|
SkASSERT(callbackContext->promiseImageTexture());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
|
|
|
|
if (!callbackContext) {
|
|
|
|
// This texture would've been too large to fit on the GPU
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-17 20:20:55 +00:00
|
|
|
std::unique_ptr<SkPixmap[]> mipLevels = info->normalMipLevels();
|
2020-04-15 19:54:34 +00:00
|
|
|
|
2020-04-17 20:20:55 +00:00
|
|
|
GrBackendTexture backendTex = context->createBackendTexture(mipLevels.get(),
|
|
|
|
info->numMipLevels(),
|
2020-04-15 19:54:34 +00:00
|
|
|
GrRenderable::kNo,
|
|
|
|
GrProtected::kNo);
|
2020-02-14 17:36:37 +00:00
|
|
|
SkASSERT(backendTex.isValid());
|
|
|
|
|
|
|
|
callbackContext->setBackendTexture(backendTex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 15:27:07 +00:00
|
|
|
void DDLPromiseImageHelper::DeleteBETexturesForPromiseImage(GrContext* context,
|
|
|
|
PromiseImageInfo* info) {
|
|
|
|
SkASSERT(context->priv().asDirectContext());
|
|
|
|
|
|
|
|
if (info->isYUV()) {
|
|
|
|
int numPixmaps;
|
|
|
|
SkAssertResult(SkYUVAIndex::AreValidIndices(info->yuvaIndices(), &numPixmaps));
|
|
|
|
for (int j = 0; j < numPixmaps; ++j) {
|
|
|
|
PromiseImageCallbackContext* callbackContext = info->callbackContext(j);
|
|
|
|
SkASSERT(callbackContext);
|
|
|
|
|
|
|
|
callbackContext->destroyBackendTexture();
|
|
|
|
SkASSERT(!callbackContext->promiseImageTexture());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PromiseImageCallbackContext* callbackContext = info->callbackContext(0);
|
|
|
|
if (!callbackContext) {
|
|
|
|
// This texture would've been too large to fit on the GPU
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callbackContext->destroyBackendTexture();
|
|
|
|
SkASSERT(!callbackContext->promiseImageTexture());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 17:36:37 +00:00
|
|
|
void DDLPromiseImageHelper::createCallbackContexts(GrContext* context) {
|
|
|
|
const GrCaps* caps = context->priv().caps();
|
|
|
|
const int maxDimension = caps->maxTextureSize();
|
|
|
|
|
2019-01-10 16:33:06 +00:00
|
|
|
for (int i = 0; i < fImageInfo.count(); ++i) {
|
2020-02-14 17:36:37 +00:00
|
|
|
PromiseImageInfo& info = fImageInfo[i];
|
2018-05-29 20:13:26 +00:00
|
|
|
|
2018-09-27 18:26:47 +00:00
|
|
|
if (info.isYUV()) {
|
2018-10-18 18:36:59 +00:00
|
|
|
int numPixmaps;
|
|
|
|
SkAssertResult(SkYUVAIndex::AreValidIndices(info.yuvaIndices(), &numPixmaps));
|
2020-02-14 17:36:37 +00:00
|
|
|
|
2018-10-18 18:36:59 +00:00
|
|
|
for (int j = 0; j < numPixmaps; ++j) {
|
2018-09-27 18:26:47 +00:00
|
|
|
const SkPixmap& yuvPixmap = info.yuvPixmap(j);
|
|
|
|
|
2020-02-14 17:36:37 +00:00
|
|
|
GrBackendFormat backendFormat = context->defaultBackendFormat(yuvPixmap.colorType(),
|
|
|
|
GrRenderable::kNo);
|
2018-12-06 18:11:53 +00:00
|
|
|
|
2020-02-14 17:36:37 +00:00
|
|
|
sk_sp<PromiseImageCallbackContext> callbackContext(
|
|
|
|
new PromiseImageCallbackContext(context, backendFormat));
|
2018-09-27 18:26:47 +00:00
|
|
|
|
2020-02-14 17:36:37 +00:00
|
|
|
info.setCallbackContext(j, std::move(callbackContext));
|
2018-09-27 18:26:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-04-17 20:20:55 +00:00
|
|
|
const SkBitmap& baseLevel = info.baseLevel();
|
2018-09-27 18:26:47 +00:00
|
|
|
|
2020-02-14 17:36:37 +00:00
|
|
|
// TODO: explicitly mark the PromiseImageInfo as too big and check in uploadAllToGPU
|
2020-04-17 20:20:55 +00:00
|
|
|
if (maxDimension < std::max(baseLevel.width(), baseLevel.height())) {
|
2020-02-14 17:36:37 +00:00
|
|
|
// This won't fit on the GPU. Fallback to a raster-backed image per tile.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-17 20:20:55 +00:00
|
|
|
GrBackendFormat backendFormat = context->defaultBackendFormat(baseLevel.colorType(),
|
2020-02-14 17:36:37 +00:00
|
|
|
GrRenderable::kNo);
|
|
|
|
if (!caps->isFormatTexturable(backendFormat)) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-06-10 19:09:34 +00:00
|
|
|
|
2020-02-14 17:36:37 +00:00
|
|
|
sk_sp<PromiseImageCallbackContext> callbackContext(
|
|
|
|
new PromiseImageCallbackContext(context, backendFormat));
|
2018-09-27 18:26:47 +00:00
|
|
|
|
2020-02-14 17:36:37 +00:00
|
|
|
info.setCallbackContext(0, std::move(callbackContext));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DDLPromiseImageHelper::uploadAllToGPU(SkTaskGroup* taskGroup, GrContext* context) {
|
|
|
|
SkASSERT(context->priv().asDirectContext());
|
|
|
|
|
|
|
|
if (taskGroup) {
|
|
|
|
for (int i = 0; i < fImageInfo.count(); ++i) {
|
|
|
|
PromiseImageInfo* info = &fImageInfo[i];
|
|
|
|
|
2020-02-26 15:27:07 +00:00
|
|
|
taskGroup->add([context, info]() { CreateBETexturesForPromiseImage(context, info); });
|
2020-02-14 17:36:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < fImageInfo.count(); ++i) {
|
|
|
|
CreateBETexturesForPromiseImage(context, &fImageInfo[i]);
|
2018-09-27 18:26:47 +00:00
|
|
|
}
|
2019-01-10 17:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-27 18:26:47 +00:00
|
|
|
|
2020-02-26 15:27:07 +00:00
|
|
|
void DDLPromiseImageHelper::deleteAllFromGPU(SkTaskGroup* taskGroup, GrContext* context) {
|
|
|
|
SkASSERT(context->priv().asDirectContext());
|
|
|
|
|
|
|
|
if (taskGroup) {
|
|
|
|
for (int i = 0; i < fImageInfo.count(); ++i) {
|
|
|
|
PromiseImageInfo* info = &fImageInfo[i];
|
|
|
|
|
|
|
|
taskGroup->add([context, info]() { DeleteBETexturesForPromiseImage(context, info); });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < fImageInfo.count(); ++i) {
|
|
|
|
DeleteBETexturesForPromiseImage(context, &fImageInfo[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 20:13:26 +00:00
|
|
|
sk_sp<SkPicture> DDLPromiseImageHelper::reinflateSKP(
|
|
|
|
SkDeferredDisplayListRecorder* recorder,
|
|
|
|
SkData* compressedPictureData,
|
|
|
|
SkTArray<sk_sp<SkImage>>* promiseImages) const {
|
|
|
|
PerRecorderContext perRecorderContext { recorder, this, promiseImages };
|
|
|
|
|
|
|
|
SkDeserialProcs procs;
|
|
|
|
procs.fImageCtx = (void*) &perRecorderContext;
|
2020-02-14 17:36:37 +00:00
|
|
|
procs.fImageProc = CreatePromiseImages;
|
2018-05-29 20:13:26 +00:00
|
|
|
|
|
|
|
return SkPicture::MakeFromData(compressedPictureData, &procs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This generates promise images to replace the indices in the compressed picture. This
|
|
|
|
// reconstitution is performed separately in each thread so we end up with multiple
|
|
|
|
// promise images referring to the same GrBackendTexture.
|
2020-02-14 17:36:37 +00:00
|
|
|
sk_sp<SkImage> DDLPromiseImageHelper::CreatePromiseImages(const void* rawData,
|
2018-05-29 20:13:26 +00:00
|
|
|
size_t length, void* ctxIn) {
|
|
|
|
PerRecorderContext* perRecorderContext = static_cast<PerRecorderContext*>(ctxIn);
|
|
|
|
const DDLPromiseImageHelper* helper = perRecorderContext->fHelper;
|
|
|
|
SkDeferredDisplayListRecorder* recorder = perRecorderContext->fRecorder;
|
|
|
|
|
|
|
|
SkASSERT(length == sizeof(int));
|
|
|
|
|
|
|
|
const int* indexPtr = static_cast<const int*>(rawData);
|
2019-12-16 20:09:57 +00:00
|
|
|
if (!helper->isValidID(*indexPtr)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-05-29 20:13:26 +00:00
|
|
|
|
|
|
|
const DDLPromiseImageHelper::PromiseImageInfo& curImage = helper->getInfo(*indexPtr);
|
|
|
|
|
2020-02-14 17:36:37 +00:00
|
|
|
// If there is no callback context that means 'createCallbackContexts' determined the
|
|
|
|
// texture wouldn't fit on the GPU. Create a separate bitmap-backed image for each thread.
|
|
|
|
if (!curImage.isYUV() && !curImage.callbackContext(0)) {
|
2020-04-17 20:20:55 +00:00
|
|
|
SkASSERT(curImage.baseLevel().isImmutable());
|
|
|
|
return SkImage::MakeFromBitmap(curImage.baseLevel());
|
2018-09-27 18:26:47 +00:00
|
|
|
}
|
2020-02-14 17:36:37 +00:00
|
|
|
|
2018-09-27 18:26:47 +00:00
|
|
|
SkASSERT(curImage.index() == *indexPtr);
|
|
|
|
|
|
|
|
sk_sp<SkImage> image;
|
|
|
|
if (curImage.isYUV()) {
|
2018-10-29 20:26:02 +00:00
|
|
|
GrBackendFormat backendFormats[SkYUVASizeInfo::kMaxCount];
|
|
|
|
void* contexts[SkYUVASizeInfo::kMaxCount] = { nullptr, nullptr, nullptr, nullptr };
|
|
|
|
SkISize sizes[SkYUVASizeInfo::kMaxCount];
|
2018-10-18 18:36:59 +00:00
|
|
|
// TODO: store this value somewhere?
|
|
|
|
int textureCount;
|
|
|
|
SkAssertResult(SkYUVAIndex::AreValidIndices(curImage.yuvaIndices(), &textureCount));
|
|
|
|
for (int i = 0; i < textureCount; ++i) {
|
2020-02-14 17:36:37 +00:00
|
|
|
backendFormats[i] = curImage.backendFormat(i);
|
2018-12-14 14:18:50 +00:00
|
|
|
SkASSERT(backendFormats[i].isValid());
|
2018-09-27 18:26:47 +00:00
|
|
|
contexts[i] = curImage.refCallbackContext(i).release();
|
2018-10-24 14:32:20 +00:00
|
|
|
sizes[i].set(curImage.yuvPixmap(i).width(), curImage.yuvPixmap(i).height());
|
2018-10-18 18:36:59 +00:00
|
|
|
}
|
2018-10-29 20:26:02 +00:00
|
|
|
for (int i = textureCount; i < SkYUVASizeInfo::kMaxCount; ++i) {
|
2018-10-24 14:32:20 +00:00
|
|
|
sizes[i] = SkISize::MakeEmpty();
|
2018-09-27 18:26:47 +00:00
|
|
|
}
|
2018-10-18 16:13:18 +00:00
|
|
|
|
2019-03-08 18:28:46 +00:00
|
|
|
image = recorder->makeYUVAPromiseTexture(
|
|
|
|
curImage.yuvColorSpace(),
|
|
|
|
backendFormats,
|
|
|
|
sizes,
|
|
|
|
curImage.yuvaIndices(),
|
|
|
|
curImage.overallWidth(),
|
|
|
|
curImage.overallHeight(),
|
|
|
|
GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
|
|
|
|
curImage.refOverallColorSpace(),
|
2020-04-23 19:10:03 +00:00
|
|
|
PromiseImageCallbackContext::PromiseImageFulfillProc,
|
|
|
|
PromiseImageCallbackContext::PromiseImageReleaseProc,
|
|
|
|
PromiseImageCallbackContext::PromiseImageDoneProc,
|
2019-03-08 18:28:46 +00:00
|
|
|
contexts,
|
|
|
|
SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
|
2019-01-10 17:09:52 +00:00
|
|
|
for (int i = 0; i < textureCount; ++i) {
|
|
|
|
curImage.callbackContext(i)->wasAddedToImage();
|
|
|
|
}
|
2019-03-04 17:18:53 +00:00
|
|
|
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
{
|
|
|
|
// By the peekProxy contract this image should not have a single backing proxy so
|
|
|
|
// should return null. The call should also not trigger the conversion to RGBA.
|
|
|
|
SkImage_GpuYUVA* yuva = reinterpret_cast<SkImage_GpuYUVA*>(image.get());
|
|
|
|
SkASSERT(!yuva->peekProxy());
|
|
|
|
SkASSERT(!yuva->peekProxy()); // the first call didn't force a conversion to RGBA
|
|
|
|
}
|
|
|
|
#endif
|
2018-09-27 18:26:47 +00:00
|
|
|
} else {
|
2020-02-14 17:36:37 +00:00
|
|
|
GrBackendFormat backendFormat = curImage.backendFormat(0);
|
2018-12-14 14:18:50 +00:00
|
|
|
SkASSERT(backendFormat.isValid());
|
2018-09-27 18:26:47 +00:00
|
|
|
|
|
|
|
// Each DDL recorder gets its own ref on the promise callback context for the
|
|
|
|
// promise images it creates.
|
2019-03-08 18:28:46 +00:00
|
|
|
image = recorder->makePromiseTexture(
|
|
|
|
backendFormat,
|
|
|
|
curImage.overallWidth(),
|
|
|
|
curImage.overallHeight(),
|
2020-04-17 20:20:55 +00:00
|
|
|
curImage.mipMapped(0),
|
2019-03-08 18:28:46 +00:00
|
|
|
GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
|
|
|
|
curImage.overallColorType(),
|
|
|
|
curImage.overallAlphaType(),
|
|
|
|
curImage.refOverallColorSpace(),
|
2020-04-23 19:10:03 +00:00
|
|
|
PromiseImageCallbackContext::PromiseImageFulfillProc,
|
|
|
|
PromiseImageCallbackContext::PromiseImageReleaseProc,
|
|
|
|
PromiseImageCallbackContext::PromiseImageDoneProc,
|
2019-03-08 18:28:46 +00:00
|
|
|
(void*)curImage.refCallbackContext(0).release(),
|
|
|
|
SkDeferredDisplayListRecorder::PromiseImageApiVersion::kNew);
|
2019-01-10 17:09:52 +00:00
|
|
|
curImage.callbackContext(0)->wasAddedToImage();
|
2018-05-29 20:13:26 +00:00
|
|
|
}
|
|
|
|
perRecorderContext->fPromiseImages->push_back(image);
|
|
|
|
SkASSERT(image);
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DDLPromiseImageHelper::findImage(SkImage* image) const {
|
|
|
|
for (int i = 0; i < fImageInfo.count(); ++i) {
|
2018-09-27 18:26:47 +00:00
|
|
|
if (fImageInfo[i].originalUniqueID() == image->uniqueID()) { // trying to dedup here
|
|
|
|
SkASSERT(fImageInfo[i].index() == i);
|
|
|
|
SkASSERT(this->isValidID(i) && this->isValidID(fImageInfo[i].index()));
|
2018-05-29 20:13:26 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DDLPromiseImageHelper::addImage(SkImage* image) {
|
2018-09-27 18:26:47 +00:00
|
|
|
SkImage_Base* ib = as_IB(image);
|
|
|
|
|
|
|
|
SkImageInfo overallII = SkImageInfo::Make(image->width(), image->height(),
|
2019-05-13 19:59:10 +00:00
|
|
|
image->colorType() == kBGRA_8888_SkColorType
|
|
|
|
? kRGBA_8888_SkColorType
|
|
|
|
: image->colorType(),
|
|
|
|
image->alphaType(),
|
2018-09-27 18:26:47 +00:00
|
|
|
image->refColorSpace());
|
|
|
|
|
|
|
|
PromiseImageInfo& newImageInfo = fImageInfo.emplace_back(fImageInfo.count(),
|
|
|
|
image->uniqueID(),
|
|
|
|
overallII);
|
|
|
|
|
2018-10-29 20:26:02 +00:00
|
|
|
SkYUVASizeInfo yuvaSizeInfo;
|
2018-10-18 18:36:59 +00:00
|
|
|
SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount];
|
2018-09-27 18:26:47 +00:00
|
|
|
SkYUVColorSpace yuvColorSpace;
|
2018-10-29 20:26:02 +00:00
|
|
|
const void* planes[SkYUVASizeInfo::kMaxCount];
|
2018-10-18 18:36:59 +00:00
|
|
|
sk_sp<SkCachedData> yuvData = ib->getPlanes(&yuvaSizeInfo, yuvaIndices, &yuvColorSpace, planes);
|
2018-09-27 18:26:47 +00:00
|
|
|
if (yuvData) {
|
2018-10-18 18:36:59 +00:00
|
|
|
newImageInfo.setYUVData(std::move(yuvData), yuvaIndices, yuvColorSpace);
|
|
|
|
|
2018-10-22 18:12:03 +00:00
|
|
|
// determine colortypes from index data
|
2019-09-04 19:05:35 +00:00
|
|
|
// for testing we only ever use A8, RG_88
|
2018-10-29 20:26:02 +00:00
|
|
|
SkColorType colorTypes[SkYUVASizeInfo::kMaxCount] = {
|
2018-10-22 18:12:03 +00:00
|
|
|
kUnknown_SkColorType, kUnknown_SkColorType,
|
|
|
|
kUnknown_SkColorType, kUnknown_SkColorType
|
|
|
|
};
|
|
|
|
for (int yuvIndex = 0; yuvIndex < SkYUVAIndex::kIndexCount; ++yuvIndex) {
|
|
|
|
int texIdx = yuvaIndices[yuvIndex].fIndex;
|
|
|
|
if (texIdx < 0) {
|
|
|
|
SkASSERT(SkYUVAIndex::kA_Index == yuvIndex);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (kUnknown_SkColorType == colorTypes[texIdx]) {
|
|
|
|
colorTypes[texIdx] = kAlpha_8_SkColorType;
|
|
|
|
} else {
|
2019-09-19 20:05:48 +00:00
|
|
|
colorTypes[texIdx] = kR8G8_unorm_SkColorType;
|
2018-10-22 18:12:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 20:26:02 +00:00
|
|
|
for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) {
|
2018-10-22 18:12:03 +00:00
|
|
|
if (yuvaSizeInfo.fSizes[i].isEmpty()) {
|
|
|
|
SkASSERT(!yuvaSizeInfo.fWidthBytes[i] && kUnknown_SkColorType == colorTypes[i]);
|
2018-10-18 18:36:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-09-27 18:26:47 +00:00
|
|
|
|
2018-10-18 18:36:59 +00:00
|
|
|
SkImageInfo planeII = SkImageInfo::Make(yuvaSizeInfo.fSizes[i].fWidth,
|
|
|
|
yuvaSizeInfo.fSizes[i].fHeight,
|
2018-10-22 18:12:03 +00:00
|
|
|
colorTypes[i],
|
2018-10-18 18:36:59 +00:00
|
|
|
kUnpremul_SkAlphaType);
|
|
|
|
newImageInfo.addYUVPlane(i, planeII, planes[i], yuvaSizeInfo.fWidthBytes[i]);
|
2018-09-27 18:26:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sk_sp<SkImage> rasterImage = image->makeRasterImage(); // force decoding of lazy images
|
2019-12-16 16:22:17 +00:00
|
|
|
if (!rasterImage) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-05-29 20:13:26 +00:00
|
|
|
|
2018-09-27 18:26:47 +00:00
|
|
|
SkBitmap tmp;
|
|
|
|
tmp.allocPixels(overallII);
|
2018-05-29 20:13:26 +00:00
|
|
|
|
2018-09-27 18:26:47 +00:00
|
|
|
if (!rasterImage->readPixels(tmp.pixmap(), 0, 0)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-05-29 20:13:26 +00:00
|
|
|
|
2018-09-27 18:26:47 +00:00
|
|
|
tmp.setImmutable();
|
2020-04-17 20:20:55 +00:00
|
|
|
|
|
|
|
// Given how the DDL testing harness works (i.e., only modifying the SkImages w/in an
|
|
|
|
// SKP) we don't know if a given SkImage will require mipmapping. To work around this
|
|
|
|
// we just try to create all the backend textures as mipmapped but, failing that, fall
|
|
|
|
// back to un-mipped.
|
|
|
|
std::unique_ptr<SkMipMap> mipmaps(SkMipMap::Build(tmp.pixmap(), nullptr));
|
|
|
|
|
|
|
|
newImageInfo.setMipLevels(tmp, std::move(mipmaps));
|
2018-05-29 20:13:26 +00:00
|
|
|
}
|
2018-09-27 18:26:47 +00:00
|
|
|
// In either case newImageInfo's PromiseImageCallbackContext is filled in by uploadAllToGPU
|
2018-05-29 20:13:26 +00:00
|
|
|
|
|
|
|
return fImageInfo.count()-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DDLPromiseImageHelper::findOrDefineImage(SkImage* image) {
|
|
|
|
int preExistingID = this->findImage(image);
|
|
|
|
if (preExistingID >= 0) {
|
|
|
|
SkASSERT(this->isValidID(preExistingID));
|
|
|
|
return preExistingID;
|
|
|
|
}
|
|
|
|
|
|
|
|
int newID = this->addImage(image);
|
|
|
|
return newID;
|
|
|
|
}
|