2019-03-21 17:08:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 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/gpu/YUVUtils.h"
|
2019-03-21 17:08:36 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkData.h"
|
2020-07-29 01:06:43 +00:00
|
|
|
#include "include/gpu/GrRecordingContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/codec/SkCodecImageGenerator.h"
|
|
|
|
#include "src/gpu/GrContextPriv.h"
|
2020-07-29 01:06:43 +00:00
|
|
|
#include "src/gpu/GrRecordingContextPriv.h"
|
2019-03-21 17:08:36 +00:00
|
|
|
|
|
|
|
namespace sk_gpu_test {
|
|
|
|
|
2020-07-31 12:57:48 +00:00
|
|
|
std::unique_ptr<LazyYUVImage> LazyYUVImage::Make(sk_sp<SkData> data, GrMipmapped mipmapped) {
|
2019-03-21 17:08:36 +00:00
|
|
|
std::unique_ptr<LazyYUVImage> image(new LazyYUVImage());
|
2020-07-31 12:57:48 +00:00
|
|
|
if (image->reset(std::move(data), mipmapped)) {
|
2019-03-21 17:08:36 +00:00
|
|
|
return image;
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 01:06:43 +00:00
|
|
|
sk_sp<SkImage> LazyYUVImage::refImage(GrRecordingContext* rContext) {
|
|
|
|
if (this->ensureYUVImage(rContext)) {
|
2019-03-21 17:08:36 +00:00
|
|
|
return fYUVImage;
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 01:06:43 +00:00
|
|
|
const SkImage* LazyYUVImage::getImage(GrRecordingContext* rContext) {
|
|
|
|
if (this->ensureYUVImage(rContext)) {
|
2019-03-21 17:08:36 +00:00
|
|
|
return fYUVImage.get();
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 12:57:48 +00:00
|
|
|
bool LazyYUVImage::reset(sk_sp<SkData> data, GrMipmapped mipmapped) {
|
|
|
|
fMipmapped = mipmapped;
|
2019-03-21 17:08:36 +00:00
|
|
|
auto codec = SkCodecImageGenerator::MakeFromEncodedCodec(data);
|
|
|
|
if (!codec) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!codec->queryYUVA8(&fSizeInfo, fComponents, &fColorSpace)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fPlaneData.reset(fSizeInfo.computeTotalBytes());
|
|
|
|
void* planes[SkYUVASizeInfo::kMaxCount];
|
|
|
|
fSizeInfo.computePlanes(fPlaneData.get(), planes);
|
|
|
|
if (!codec->getYUVA8Planes(fSizeInfo, fComponents, planes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) {
|
|
|
|
if (fSizeInfo.fSizes[i].isEmpty()) {
|
|
|
|
fPlanes[i].reset();
|
|
|
|
} else {
|
|
|
|
SkASSERT(planes[i]);
|
|
|
|
auto planeInfo = SkImageInfo::Make(fSizeInfo.fSizes[i].fWidth,
|
|
|
|
fSizeInfo.fSizes[i].fHeight,
|
|
|
|
kGray_8_SkColorType, kOpaque_SkAlphaType, nullptr);
|
|
|
|
fPlanes[i].reset(planeInfo, planes[i], fSizeInfo.fWidthBytes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The SkPixmap data is fully configured now for MakeFromYUVAPixmaps once we get a GrContext
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-29 01:06:43 +00:00
|
|
|
bool LazyYUVImage::ensureYUVImage(GrRecordingContext* rContext) {
|
|
|
|
if (!rContext) {
|
2019-03-21 17:08:36 +00:00
|
|
|
return false; // Cannot make a YUV image from planes
|
|
|
|
}
|
2020-07-29 01:06:43 +00:00
|
|
|
if (fYUVImage && fYUVImage->isValid(rContext)) {
|
|
|
|
return true; // Have already made a YUV image valid for this context.
|
2019-03-21 17:08:36 +00:00
|
|
|
}
|
2020-07-29 01:06:43 +00:00
|
|
|
// Try to make a new YUV image for this context.
|
|
|
|
fYUVImage = SkImage::MakeFromYUVAPixmaps(rContext->priv().backdoor(),
|
|
|
|
fColorSpace,
|
|
|
|
fPlanes,
|
|
|
|
fComponents,
|
|
|
|
fSizeInfo.fSizes[0],
|
|
|
|
kTopLeft_GrSurfaceOrigin,
|
2020-07-31 12:57:48 +00:00
|
|
|
static_cast<bool>(fMipmapped),
|
2020-07-29 01:06:43 +00:00
|
|
|
false);
|
2019-03-21 17:08:36 +00:00
|
|
|
return fYUVImage != nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-19 18:27:14 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2020-07-17 15:59:01 +00:00
|
|
|
void YUVABackendReleaseContext::Unwind(GrDirectContext* dContext,
|
|
|
|
YUVABackendReleaseContext* beContext,
|
2020-06-26 14:01:19 +00:00
|
|
|
bool fullFlush) {
|
|
|
|
|
2020-06-19 18:27:14 +00:00
|
|
|
// Some backends (e.g., Vulkan) require that all work associated w/ texture
|
|
|
|
// creation be completed before deleting the textures.
|
2020-06-26 14:01:19 +00:00
|
|
|
if (fullFlush) {
|
|
|
|
// If the release context client performed some operations other than backend texture
|
|
|
|
// creation then we may require a full flush to ensure that all the work is completed.
|
2020-07-17 15:59:01 +00:00
|
|
|
dContext->flush();
|
|
|
|
dContext->submit(true);
|
2020-06-26 14:01:19 +00:00
|
|
|
} else {
|
2020-07-17 15:59:01 +00:00
|
|
|
dContext->submit();
|
2020-06-26 14:01:19 +00:00
|
|
|
|
|
|
|
while (!beContext->creationCompleted()) {
|
2020-07-17 15:59:01 +00:00
|
|
|
dContext->checkAsyncWorkCompletion();
|
2020-06-26 14:01:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-19 18:27:14 +00:00
|
|
|
|
|
|
|
delete beContext;
|
|
|
|
}
|
|
|
|
|
2020-07-17 15:59:01 +00:00
|
|
|
YUVABackendReleaseContext::YUVABackendReleaseContext(GrDirectContext* dContext)
|
|
|
|
: fDContext(dContext) {
|
2020-06-19 18:27:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
YUVABackendReleaseContext::~YUVABackendReleaseContext() {
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (fBETextures[i].isValid()) {
|
2020-06-26 14:01:19 +00:00
|
|
|
SkASSERT(fCreationComplete[i]);
|
2020-07-17 15:59:01 +00:00
|
|
|
fDContext->deleteBackendTexture(fBETextures[i]);
|
2020-06-19 18:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 14:01:19 +00:00
|
|
|
template<int I> static void CreationComplete(void* releaseContext) {
|
|
|
|
auto beContext = reinterpret_cast<YUVABackendReleaseContext*>(releaseContext);
|
|
|
|
beContext->setCreationComplete(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
GrGpuFinishedProc YUVABackendReleaseContext::CreationCompleteProc(int index) {
|
|
|
|
SkASSERT(index >= 0 && index < 4);
|
|
|
|
|
|
|
|
switch (index) {
|
|
|
|
case 0: return CreationComplete<0>;
|
|
|
|
case 1: return CreationComplete<1>;
|
|
|
|
case 2: return CreationComplete<2>;
|
|
|
|
case 3: return CreationComplete<3>;
|
|
|
|
}
|
|
|
|
|
|
|
|
SK_ABORT("Invalid YUVA Index.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-21 17:08:36 +00:00
|
|
|
} // namespace sk_gpu_test
|