From 56e6286317d6fd8f36abadeea06c1faab4391f32 Mon Sep 17 00:00:00 2001 From: Jim Van Verth Date: Tue, 14 Jun 2022 08:51:56 -0400 Subject: [PATCH] [graphite] Add ContextOptions Bug: skia:13118 Change-Id: I0d6e0a9f542680456d3de82a32376722c5b9cd1d Reviewed-on: https://skia-review.googlesource.com/c/skia/+/549097 Reviewed-by: Greg Daniel Commit-Queue: Jim Van Verth --- gn/graphite.gni | 1 + include/gpu/graphite/Context.h | 3 ++- include/gpu/graphite/ContextOptions.h | 27 +++++++++++++++++++++ src/gpu/graphite/Caps.cpp | 10 +++++++- src/gpu/graphite/Caps.h | 20 ++++++++++++--- src/gpu/graphite/Context.cpp | 5 ++-- src/gpu/graphite/mtl/MtlCaps.h | 3 ++- src/gpu/graphite/mtl/MtlCaps.mm | 4 +-- src/gpu/graphite/mtl/MtlGpu.h | 3 ++- src/gpu/graphite/mtl/MtlGpu.mm | 9 ++++--- src/gpu/graphite/mtl/MtlGraphicsPipeline.mm | 2 +- src/gpu/graphite/mtl/MtlTrampoline.h | 3 ++- src/gpu/graphite/mtl/MtlTrampoline.mm | 5 ++-- tools/graphite/mtl/MtlTestContext.mm | 3 ++- tools/sk_app/GraphiteMetalWindowContext.mm | 4 ++- 15 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 include/gpu/graphite/ContextOptions.h diff --git a/gn/graphite.gni b/gn/graphite.gni index adf3914189..98c797505d 100644 --- a/gn/graphite.gni +++ b/gn/graphite.gni @@ -11,6 +11,7 @@ _include_private = get_path_info("../include/private/gpu/graphite", "abspath") skia_graphite_public = [ "$_include/BackendTexture.h", "$_include/Context.h", + "$_include/ContextOptions.h", "$_include/GraphiteTypes.h", "$_include/Recorder.h", "$_include/Recording.h", diff --git a/include/gpu/graphite/Context.h b/include/gpu/graphite/Context.h index 6d53b5d45a..23c1780dad 100644 --- a/include/gpu/graphite/Context.h +++ b/include/gpu/graphite/Context.h @@ -10,6 +10,7 @@ #include "include/core/SkRefCnt.h" #include "include/core/SkShader.h" +#include "include/gpu/graphite/ContextOptions.h" #include "include/gpu/graphite/GraphiteTypes.h" class SkBlenderID; @@ -39,7 +40,7 @@ public: ~Context(); #ifdef SK_METAL - static std::unique_ptr MakeMetal(const skgpu::graphite::MtlBackendContext&); + static std::unique_ptr MakeMetal(const MtlBackendContext&, const ContextOptions&); #endif BackendApi backend() const { return fBackend; } diff --git a/include/gpu/graphite/ContextOptions.h b/include/gpu/graphite/ContextOptions.h new file mode 100644 index 0000000000..0ec68b9fda --- /dev/null +++ b/include/gpu/graphite/ContextOptions.h @@ -0,0 +1,27 @@ +/* + * Copyright 2022 Google LLC + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef skgpu_graphite_ContextOptions_DEFINED +#define skgpu_graphite_ContextOptions_DEFINED + +namespace skgpu { class ShaderErrorHandler; } + +namespace skgpu::graphite { + +struct SK_API ContextOptions { + ContextOptions() {} + + /** + * If present, use this object to report shader compilation failures. If not, report failures + * via SkDebugf and assert. + */ + skgpu::ShaderErrorHandler* fShaderErrorHandler = nullptr; +}; + +} // namespace skgpu::graphite + +#endif // skgpu_graphite_ContextOptions diff --git a/src/gpu/graphite/Caps.cpp b/src/gpu/graphite/Caps.cpp index cb54d072d5..c6be6bd0e5 100644 --- a/src/gpu/graphite/Caps.cpp +++ b/src/gpu/graphite/Caps.cpp @@ -7,6 +7,8 @@ #include "src/gpu/graphite/Caps.h" +#include "include/gpu/ShaderErrorHandler.h" +#include "include/gpu/graphite/ContextOptions.h" #include "include/gpu/graphite/TextureInfo.h" #include "src/sksl/SkSLUtil.h" @@ -15,8 +17,14 @@ namespace skgpu::graphite { Caps::Caps() : fShaderCaps(std::make_unique()) {} Caps::~Caps() {} -void Caps::finishInitialization() { +void Caps::finishInitialization(const ContextOptions& options) { this->initSkCaps(fShaderCaps.get()); + + if (options.fShaderErrorHandler) { + fShaderErrorHandler = options.fShaderErrorHandler; + } else { + fShaderErrorHandler = DefaultShaderErrorHandler(); + } } bool Caps::isTexturable(const TextureInfo& info) const { diff --git a/src/gpu/graphite/Caps.h b/src/gpu/graphite/Caps.h index f8f6c0ab34..b6557fda37 100644 --- a/src/gpu/graphite/Caps.h +++ b/src/gpu/graphite/Caps.h @@ -16,12 +16,13 @@ #include "src/gpu/Swizzle.h" #include "src/gpu/graphite/ResourceTypes.h" -namespace SkSL { -struct ShaderCaps; -} +namespace SkSL { struct ShaderCaps; } + +namespace skgpu { class ShaderErrorHandler; } namespace skgpu::graphite { +struct ContextOptions; class GraphicsPipelineDesc; class GraphiteResourceKey; struct RenderPassDesc; @@ -78,12 +79,14 @@ public: // SkColorType and TextureInfo. skgpu::Swizzle getWriteSwizzle(SkColorType, const TextureInfo&) const; + skgpu::ShaderErrorHandler* shaderErrorHandler() const { return fShaderErrorHandler; } + protected: Caps(); // Subclasses must call this at the end of their init method in order to do final processing on // the caps. - void finishInitialization(); + void finishInitialization(const ContextOptions&); // TODO: This value should be set by some context option. For now just making it 4. uint32_t defaultMSAASamples() const { return 4; } @@ -111,6 +114,15 @@ protected: bool fClampToBorderSupport = true; + ////////////////////////////////////////////////////////////////////////////////////////// + // Client-provided Caps + + /** + * If present, use this object to report shader compilation failures. If not, report failures + * via SkDebugf and assert. + */ + ShaderErrorHandler* fShaderErrorHandler = nullptr; + private: virtual bool onIsTexturable(const TextureInfo&) const = 0; virtual const ColorTypeInfo* getColorTypeInfo(SkColorType, const TextureInfo&) const = 0; diff --git a/src/gpu/graphite/Context.cpp b/src/gpu/graphite/Context.cpp index d3686e30f5..8c6fee70dc 100644 --- a/src/gpu/graphite/Context.cpp +++ b/src/gpu/graphite/Context.cpp @@ -40,8 +40,9 @@ Context::Context(sk_sp gpu, BackendApi backend) Context::~Context() {} #ifdef SK_METAL -std::unique_ptr Context::MakeMetal(const MtlBackendContext& backendContext) { - sk_sp gpu = MtlTrampoline::MakeGpu(backendContext); +std::unique_ptr Context::MakeMetal(const MtlBackendContext& backendContext, + const ContextOptions& options) { + sk_sp gpu = MtlTrampoline::MakeGpu(backendContext, options); if (!gpu) { return nullptr; } diff --git a/src/gpu/graphite/mtl/MtlCaps.h b/src/gpu/graphite/mtl/MtlCaps.h index 0fe9009ffd..7fba82293d 100644 --- a/src/gpu/graphite/mtl/MtlCaps.h +++ b/src/gpu/graphite/mtl/MtlCaps.h @@ -15,10 +15,11 @@ #include "src/gpu/graphite/Caps.h" namespace skgpu::graphite { +struct ContextOptions; class MtlCaps final : public skgpu::graphite::Caps { public: - MtlCaps(const id); + MtlCaps(const id, const ContextOptions&); ~MtlCaps() override {} TextureInfo getDefaultSampledTextureInfo(SkColorType, diff --git a/src/gpu/graphite/mtl/MtlCaps.mm b/src/gpu/graphite/mtl/MtlCaps.mm index 3533066d8b..d8c314d9c1 100644 --- a/src/gpu/graphite/mtl/MtlCaps.mm +++ b/src/gpu/graphite/mtl/MtlCaps.mm @@ -17,7 +17,7 @@ namespace skgpu::graphite { -MtlCaps::MtlCaps(const id device) +MtlCaps::MtlCaps(const id device, const ContextOptions& options) : Caps() { this->initGPUFamily(device); this->initCaps(device); @@ -27,7 +27,7 @@ MtlCaps::MtlCaps(const id device) // Metal-specific MtlCaps - this->finishInitialization(); + this->finishInitialization(options); } // translates from older MTLFeatureSet interface to MTLGPUFamily interface diff --git a/src/gpu/graphite/mtl/MtlGpu.h b/src/gpu/graphite/mtl/MtlGpu.h index a3b2b7fcc6..95cc5be66e 100644 --- a/src/gpu/graphite/mtl/MtlGpu.h +++ b/src/gpu/graphite/mtl/MtlGpu.h @@ -18,10 +18,11 @@ #import namespace skgpu::graphite { +struct ContextOptions; class MtlGpu final : public Gpu { public: - static sk_sp Make(const MtlBackendContext&); + static sk_sp Make(const MtlBackendContext&, const ContextOptions&); ~MtlGpu() override; id device() const { return fDevice.get(); } diff --git a/src/gpu/graphite/mtl/MtlGpu.mm b/src/gpu/graphite/mtl/MtlGpu.mm index b10e9692d5..6fd7962b50 100644 --- a/src/gpu/graphite/mtl/MtlGpu.mm +++ b/src/gpu/graphite/mtl/MtlGpu.mm @@ -18,7 +18,8 @@ namespace skgpu::graphite { -sk_sp MtlGpu::Make(const MtlBackendContext& context) { +sk_sp MtlGpu::Make(const MtlBackendContext& context, + const ContextOptions& options) { // TODO: This was taken from GrMtlGpu.mm's Make, does graphite deserve a higher version? if (@available(macOS 10.14, iOS 11.0, *)) { // no warning needed @@ -35,9 +36,11 @@ sk_sp MtlGpu::Make(const MtlBackendContext& context) { sk_cfp> device = sk_ret_cfp((id)(context.fDevice.get())); sk_cfp> queue = sk_ret_cfp((id)(context.fQueue.get())); - sk_sp caps(new MtlCaps(device.get())); + sk_sp caps(new MtlCaps(device.get(), options)); - return sk_sp(new MtlGpu(std::move(device), std::move(queue), std::move(caps))); + return sk_sp(new MtlGpu(std::move(device), + std::move(queue), + std::move(caps))); } MtlGpu::MtlGpu(sk_cfp> device, diff --git a/src/gpu/graphite/mtl/MtlGraphicsPipeline.mm b/src/gpu/graphite/mtl/MtlGraphicsPipeline.mm index 6378f8692c..774a586d5c 100644 --- a/src/gpu/graphite/mtl/MtlGraphicsPipeline.mm +++ b/src/gpu/graphite/mtl/MtlGraphicsPipeline.mm @@ -509,7 +509,7 @@ sk_sp MtlGraphicsPipeline::Make( settings.fForceNoRTFlip = true; - ShaderErrorHandler* errorHandler = DefaultShaderErrorHandler(); + ShaderErrorHandler* errorHandler = gpu->caps()->shaderErrorHandler(); if (!SkSLToMSL(gpu, get_sksl_vs(pipelineDesc), SkSL::ProgramKind::kGraphiteVertex, diff --git a/src/gpu/graphite/mtl/MtlTrampoline.h b/src/gpu/graphite/mtl/MtlTrampoline.h index a664503fdc..5bafa0ee43 100644 --- a/src/gpu/graphite/mtl/MtlTrampoline.h +++ b/src/gpu/graphite/mtl/MtlTrampoline.h @@ -11,6 +11,7 @@ #include "include/core/SkRefCnt.h" namespace skgpu::graphite { +struct ContextOptions; class Gpu; struct MtlBackendContext; @@ -20,7 +21,7 @@ struct MtlBackendContext; */ class MtlTrampoline { public: - static sk_sp MakeGpu(const MtlBackendContext&); + static sk_sp MakeGpu(const MtlBackendContext&, const ContextOptions&); }; } // namespace skgpu::graphite diff --git a/src/gpu/graphite/mtl/MtlTrampoline.mm b/src/gpu/graphite/mtl/MtlTrampoline.mm index 064abc09dd..577be08774 100644 --- a/src/gpu/graphite/mtl/MtlTrampoline.mm +++ b/src/gpu/graphite/mtl/MtlTrampoline.mm @@ -10,8 +10,9 @@ #include "src/gpu/graphite/mtl/MtlGpu.h" namespace skgpu::graphite { -sk_sp MtlTrampoline::MakeGpu(const MtlBackendContext& backendContext) { - return MtlGpu::Make(backendContext); +sk_sp MtlTrampoline::MakeGpu(const MtlBackendContext& backendContext, + const ContextOptions& options) { + return MtlGpu::Make(backendContext, options); } } // namespace skgpu::graphite diff --git a/tools/graphite/mtl/MtlTestContext.mm b/tools/graphite/mtl/MtlTestContext.mm index c3c5e53fa1..08d17651fe 100644 --- a/tools/graphite/mtl/MtlTestContext.mm +++ b/tools/graphite/mtl/MtlTestContext.mm @@ -8,6 +8,7 @@ #include "tools/graphite/mtl/GraphiteMtlTestContext.h" #include "include/gpu/graphite/Context.h" +#include "include/gpu/graphite/ContextOptions.h" #include "include/gpu/graphite/mtl/MtlTypes.h" #ifdef SK_METAL @@ -48,7 +49,7 @@ std::unique_ptr MtlTestContext::Make() { } std::unique_ptr MtlTestContext::makeContext() { - return skgpu::graphite::Context::MakeMetal(fMtl); + return skgpu::graphite::Context::MakeMetal(fMtl, skgpu::graphite::ContextOptions{}); } } // namespace skiatest::graphite::mtl diff --git a/tools/sk_app/GraphiteMetalWindowContext.mm b/tools/sk_app/GraphiteMetalWindowContext.mm index 366de746ca..90a9edaa24 100644 --- a/tools/sk_app/GraphiteMetalWindowContext.mm +++ b/tools/sk_app/GraphiteMetalWindowContext.mm @@ -11,6 +11,7 @@ #include "include/gpu/graphite/BackendTexture.h" #include "include/gpu/graphite/Context.h" +#include "include/gpu/graphite/ContextOptions.h" #include "include/gpu/graphite/Recorder.h" #include "include/gpu/graphite/Recording.h" #include "include/gpu/graphite/SkStuff.h" @@ -53,7 +54,8 @@ void GraphiteMetalWindowContext::initializeContext() { skgpu::graphite::MtlBackendContext backendContext = {}; backendContext.fDevice.retain((skgpu::graphite::MtlHandle)fDevice.get()); backendContext.fQueue.retain((skgpu::graphite::MtlHandle)fQueue.get()); - fGraphiteContext = skgpu::graphite::Context::MakeMetal(backendContext); + fGraphiteContext = skgpu::graphite::Context::MakeMetal(backendContext, + skgpu::graphite::ContextOptions()); fGraphiteRecorder = fGraphiteContext->makeRecorder(); // TODO // if (!fGraphiteContext && fDisplayParams.fMSAASampleCount > 1) {