Carve some helper functions off of GPUSink

I'm adding a new DDL Sink and would like to reuse this functionality.

Change-Id: I9f4535ca5e0f36925bf896cb0076eab73fe60fd1
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/270639
Commit-Queue: Robert Phillips <robertphillips@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
This commit is contained in:
Robert Phillips 2020-02-13 13:38:46 -05:00 committed by Skia Commit-Bot
parent f23fd48ac1
commit 7b2fcfbc5a
3 changed files with 74 additions and 43 deletions

View File

@ -915,7 +915,7 @@ static void push_sink(const SkCommandLineConfig& config, Sink* s) {
SkString log;
Result result = sink->draw(justOneRect, &bitmap, &stream, &log);
if (result.isFatal()) {
info("Could not run %s: %s\n", config.getTag().c_str(), result.c_str());
info("Could not run %s: %s\n%s\n", config.getTag().c_str(), result.c_str(), log.c_str());
exit(1);
}

View File

@ -79,6 +79,7 @@ static DEFINE_bool(RAW_threading, true, "Allow RAW decodes to run on multiple th
DECLARE_int(gpuThreads);
using sk_gpu_test::GrContextFactory;
using sk_gpu_test::ContextInfo;
namespace DM {
@ -1368,6 +1369,65 @@ Result GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream* dstStream, SkStri
return this->onDraw(src, dst, dstStream, log, fBaseContextOptions);
}
sk_sp<SkSurface> GPUSink::createDstSurface(GrContext* context, SkISize size,
GrBackendTexture* backendTexture,
GrBackendRenderTarget* backendRT,
SkString* log) const {
sk_sp<SkSurface> surface;
SkImageInfo info = SkImageInfo::Make(size, fColorType, fAlphaType, fColorSpace);
uint32_t flags = fUseDIText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0;
SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
const int maxDimension = context->priv().caps()->maxTextureSize();
if (maxDimension < std::max(size.width(), size.height())) {
log->appendf("Src (%dx%d) too large to create a texture.\n", size.width(), size.height());
return nullptr;
}
switch (fSurfType) {
case SkCommandLineConfigGpu::SurfType::kDefault:
surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, fSampleCount,
&props);
break;
case SkCommandLineConfigGpu::SurfType::kBackendTexture:
*backendTexture = context->createBackendTexture(
info.width(), info.height(), info.colorType(), SkColors::kTransparent,
GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
surface = SkSurface::MakeFromBackendTexture(context, *backendTexture,
kTopLeft_GrSurfaceOrigin, fSampleCount,
fColorType, info.refColorSpace(), &props);
break;
case SkCommandLineConfigGpu::SurfType::kBackendRenderTarget:
if (1 == fSampleCount) {
auto colorType = SkColorTypeToGrColorType(info.colorType());
*backendRT = context->priv().getGpu()->createTestingOnlyBackendRenderTarget(
info.width(), info.height(), colorType);
surface = SkSurface::MakeFromBackendRenderTarget(
context, *backendRT, kBottomLeft_GrSurfaceOrigin, info.colorType(),
info.refColorSpace(), &props);
}
break;
}
return surface;
}
bool GPUSink::readBack(SkSurface* surface, SkBitmap* dst) const {
SkCanvas* canvas = surface->getCanvas();
SkISize size = surface->imageInfo().dimensions();
SkImageInfo info = SkImageInfo::Make(size, fColorType, fAlphaType, fColorSpace);
if (info.colorType() == kRGB_565_SkColorType || info.colorType() == kARGB_4444_SkColorType ||
info.colorType() == kRGB_888x_SkColorType) {
// We don't currently support readbacks into these formats on the GPU backend. Convert to
// 32 bit.
info = SkImageInfo::Make(size, kRGBA_8888_SkColorType, kPremul_SkAlphaType, fColorSpace);
}
dst->allocPixels(info);
return canvas->readPixels(*dst, 0, 0);
}
Result GPUSink::onDraw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log,
const GrContextOptions& baseOptions,
std::function<void(GrContext*)> initContext) const {
@ -1381,46 +1441,15 @@ Result GPUSink::onDraw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log,
SkASSERT(exec == grOptions.fExecutor);
GrContextFactory factory(grOptions);
const SkISize size = src.size();
SkImageInfo info = SkImageInfo::Make(size, fColorType, fAlphaType, fColorSpace);
sk_sp<SkSurface> surface;
GrContext* context = factory.getContextInfo(fContextType, fContextOverrides).grContext();
if (initContext) {
initContext(context);
}
const int maxDimension = context->priv().caps()->maxTextureSize();
if (maxDimension < std::max(size.width(), size.height())) {
return Result::Skip("Src too large to create a texture.\n");
}
uint32_t flags = fUseDIText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0;
SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
GrBackendTexture backendTexture;
GrBackendRenderTarget backendRT;
switch (fSurfType) {
case SkCommandLineConfigGpu::SurfType::kDefault:
surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, fSampleCount,
&props);
break;
case SkCommandLineConfigGpu::SurfType::kBackendTexture:
backendTexture = context->createBackendTexture(
info.width(), info.height(), info.colorType(), SkColors::kTransparent,
GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
surface = SkSurface::MakeFromBackendTexture(context, backendTexture,
kTopLeft_GrSurfaceOrigin, fSampleCount,
fColorType, info.refColorSpace(), &props);
break;
case SkCommandLineConfigGpu::SurfType::kBackendRenderTarget:
if (1 == fSampleCount) {
auto colorType = SkColorTypeToGrColorType(info.colorType());
backendRT = context->priv().getGpu()->createTestingOnlyBackendRenderTarget(
info.width(), info.height(), colorType);
surface = SkSurface::MakeFromBackendRenderTarget(
context, backendRT, kBottomLeft_GrSurfaceOrigin, info.colorType(),
info.refColorSpace(), &props);
}
break;
}
sk_sp<SkSurface> surface = this->createDstSurface(context, src.size(),
&backendTexture, &backendRT, log);
if (!surface) {
return Result::Fatal("Could not create a surface.");
}
@ -1437,14 +1466,11 @@ Result GPUSink::onDraw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log,
canvas->getGrContext()->priv().dumpCacheStats(log);
canvas->getGrContext()->priv().dumpGpuStats(log);
}
if (info.colorType() == kRGB_565_SkColorType || info.colorType() == kARGB_4444_SkColorType ||
info.colorType() == kRGB_888x_SkColorType) {
// We don't currently support readbacks into these formats on the GPU backend. Convert to
// 32 bit.
info = SkImageInfo::Make(size, kRGBA_8888_SkColorType, kPremul_SkAlphaType, fColorSpace);
if (!this->readBack(surface.get(), dst)) {
return Result::Fatal("Could not readback from surface.");
}
dst->allocPixels(info);
canvas->readPixels(*dst, 0, 0);
if (FLAGS_abandonGpuContext) {
factory.abandonContexts();
} else if (FLAGS_releaseAndAbandonGpuContext) {

View File

@ -366,7 +366,7 @@ public:
std::function<void(GrContext*)> initContext = nullptr) const;
sk_gpu_test::GrContextFactory::ContextType contextType() const { return fContextType; }
const sk_gpu_test::GrContextFactory::ContextOverrides& contextOverrides() {
const sk_gpu_test::GrContextFactory::ContextOverrides& contextOverrides() const {
return fContextOverrides;
}
SkCommandLineConfigGpu::SurfType surfType() const { return fSurfType; }
@ -383,6 +383,11 @@ public:
return SkColorInfo(fColorType, fAlphaType, fColorSpace);
}
protected:
sk_sp<SkSurface> createDstSurface(GrContext*, SkISize size, GrBackendTexture*,
GrBackendRenderTarget*, SkString* log) const;
bool readBack(SkSurface*, SkBitmap* dst) const;
private:
sk_gpu_test::GrContextFactory::ContextType fContextType;
sk_gpu_test::GrContextFactory::ContextOverrides fContextOverrides;