From ed65339084c0da4b9711457e7d4747c9e072ca1e Mon Sep 17 00:00:00 2001 From: Robert Phillips Date: Fri, 10 Jul 2020 13:55:21 -0400 Subject: [PATCH] Update the sk_app WindowContext to hold a GrDirectContext This CL is working towards replacing GrContext with the GrDirectContext/ GrRecordingContext pair. Change-Id: Id4488e1280d76a16c37d58bd8d29fb7f8dde6b1d Reviewed-on: https://skia-review.googlesource.com/c/skia/+/301940 Reviewed-by: Adlai Holler Commit-Queue: Robert Phillips --- BUILD.gn | 1 + tools/sk_app/DawnWindowContext.cpp | 8 ++++++-- tools/sk_app/DawnWindowContext.h | 2 -- tools/sk_app/GLWindowContext.cpp | 13 ++++++++----- tools/sk_app/GLWindowContext.h | 2 -- tools/sk_app/MetalWindowContext.mm | 10 ++++++---- tools/sk_app/VulkanWindowContext.cpp | 5 ++++- tools/sk_app/Window.cpp | 4 ++-- tools/sk_app/Window.h | 4 ++-- tools/sk_app/WindowContext.cpp | 21 +++++++++++++++++++++ tools/sk_app/WindowContext.h | 15 +++++---------- tools/sk_app/win/D3D12WindowContext_win.cpp | 5 ++++- tools/viewer/Viewer.cpp | 14 +++++++------- 13 files changed, 66 insertions(+), 38 deletions(-) create mode 100644 tools/sk_app/WindowContext.cpp diff --git a/BUILD.gn b/BUILD.gn index af01fd2709..af9134b7a2 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -2247,6 +2247,7 @@ if (skia_enable_tools) { "tools/sk_app/RasterWindowContext.h", "tools/sk_app/Window.cpp", "tools/sk_app/Window.h", + "tools/sk_app/WindowContext.cpp", "tools/sk_app/WindowContext.h", ] libs = [] diff --git a/tools/sk_app/DawnWindowContext.cpp b/tools/sk_app/DawnWindowContext.cpp index 47fe43d79b..b203a96424 100644 --- a/tools/sk_app/DawnWindowContext.cpp +++ b/tools/sk_app/DawnWindowContext.cpp @@ -7,7 +7,7 @@ #include "include/core/SkSurface.h" #include "include/gpu/GrBackendSurface.h" -#include "include/gpu/GrContext.h" +#include "include/gpu/GrDirectContext.h" #include "src/core/SkAutoMalloc.h" #include "tools/sk_app/DawnWindowContext.h" @@ -31,11 +31,15 @@ DawnWindowContext::DawnWindowContext(const DisplayParams& params, } void DawnWindowContext::initializeContext(int width, int height) { + SkASSERT(!fContext); + fWidth = width; fHeight = height; fDevice = onInitializeContext(); - fContext = GrContext::MakeDawn(fDevice, fDisplayParams.fGrContextOptions); + // CONTEXT TODO: MakeDawn should return an sk_sp + auto tmp = GrContext::MakeDawn(fDevice, fDisplayParams.fGrContextOptions); + fContext = sk_ref_sp(tmp->asDirectContext()); if (!fContext) { return; } diff --git a/tools/sk_app/DawnWindowContext.h b/tools/sk_app/DawnWindowContext.h index bd3bb9adf3..ef4b717e74 100644 --- a/tools/sk_app/DawnWindowContext.h +++ b/tools/sk_app/DawnWindowContext.h @@ -15,8 +15,6 @@ #include "dawn_native/DawnNative.h" #include "dawn/dawn_wsi.h" -class GrContext; - namespace sk_app { class DawnWindowContext : public WindowContext { diff --git a/tools/sk_app/GLWindowContext.cpp b/tools/sk_app/GLWindowContext.cpp index 6e6130bb1a..e0c41b3add 100644 --- a/tools/sk_app/GLWindowContext.cpp +++ b/tools/sk_app/GLWindowContext.cpp @@ -9,7 +9,7 @@ #include "include/core/SkCanvas.h" #include "include/core/SkSurface.h" #include "include/gpu/GrBackendSurface.h" -#include "include/gpu/GrContext.h" +#include "include/gpu/GrDirectContext.h" #include "src/core/SkMathPriv.h" #include "src/gpu/GrCaps.h" #include "src/gpu/GrContextPriv.h" @@ -21,9 +21,9 @@ namespace sk_app { GLWindowContext::GLWindowContext(const DisplayParams& params) - : WindowContext(params) - , fBackendContext(nullptr) - , fSurface(nullptr) { + : WindowContext(params) + , fBackendContext(nullptr) + , fSurface(nullptr) { fDisplayParams.fMSAASampleCount = GrNextPow2(fDisplayParams.fMSAASampleCount); } @@ -31,7 +31,10 @@ void GLWindowContext::initializeContext() { SkASSERT(!fContext); fBackendContext = this->onInitializeContext(); - fContext = GrContext::MakeGL(fBackendContext, fDisplayParams.fGrContextOptions); + + // CONTEXT TODO: MakeGL should return an sk_sp + auto tmp = GrContext::MakeGL(fBackendContext, fDisplayParams.fGrContextOptions); + fContext = sk_ref_sp(tmp->asDirectContext()); if (!fContext && fDisplayParams.fMSAASampleCount > 1) { fDisplayParams.fMSAASampleCount /= 2; this->initializeContext(); diff --git a/tools/sk_app/GLWindowContext.h b/tools/sk_app/GLWindowContext.h index 8d3085b4e6..c519903006 100644 --- a/tools/sk_app/GLWindowContext.h +++ b/tools/sk_app/GLWindowContext.h @@ -16,8 +16,6 @@ #include "tools/sk_app/WindowContext.h" -class GrContext; - namespace sk_app { class GLWindowContext : public WindowContext { diff --git a/tools/sk_app/MetalWindowContext.mm b/tools/sk_app/MetalWindowContext.mm index 03bddf5f1b..354f35917d 100644 --- a/tools/sk_app/MetalWindowContext.mm +++ b/tools/sk_app/MetalWindowContext.mm @@ -8,7 +8,7 @@ #include "include/core/SkCanvas.h" #include "include/core/SkSurface.h" #include "include/gpu/GrBackendSurface.h" -#include "include/gpu/GrContext.h" +#include "include/gpu/GrDirectContext.h" #include "include/gpu/mtl/GrMtlTypes.h" #include "src/core/SkMathPriv.h" #include "src/gpu/GrCaps.h" @@ -22,8 +22,8 @@ using sk_app::MetalWindowContext; namespace sk_app { MetalWindowContext::MetalWindowContext(const DisplayParams& params) - : WindowContext(params) - , fValid(false) { + : WindowContext(params) + , fValid(false) { fDisplayParams.fMSAASampleCount = GrNextPow2(fDisplayParams.fMSAASampleCount); } @@ -48,8 +48,10 @@ void MetalWindowContext::initializeContext() { fValid = this->onInitializeContext(); - fContext = GrContext::MakeMetal((__bridge void*)fDevice, (__bridge void*)fQueue, + // CONTEXT TODO: MakeMetal should return an sk_sp + auto tmp = GrContext::MakeMetal((__bridge void*)fDevice, (__bridge void*)fQueue, fDisplayParams.fGrContextOptions); + fContext = sk_ref_sp(tmp->asDirectContext()); if (!fContext && fDisplayParams.fMSAASampleCount > 1) { fDisplayParams.fMSAASampleCount /= 2; this->initializeContext(); diff --git a/tools/sk_app/VulkanWindowContext.cpp b/tools/sk_app/VulkanWindowContext.cpp index 02284c9065..b017ac20bc 100644 --- a/tools/sk_app/VulkanWindowContext.cpp +++ b/tools/sk_app/VulkanWindowContext.cpp @@ -49,6 +49,7 @@ VulkanWindowContext::VulkanWindowContext(const DisplayParams& params, } void VulkanWindowContext::initializeContext() { + SkASSERT(!fContext); // any config code here (particularly for msaa)? PFN_vkGetInstanceProcAddr getInstanceProc = fGetInstanceProcAddr; @@ -117,7 +118,9 @@ void VulkanWindowContext::initializeContext() { GET_DEV_PROC(QueuePresentKHR); GET_DEV_PROC(GetDeviceQueue); - fContext = GrContext::MakeVulkan(backendContext, fDisplayParams.fGrContextOptions); + // CONTEXT TODO: MakeVulkan should return an sk_sp + auto tmp = GrContext::MakeVulkan(backendContext, fDisplayParams.fGrContextOptions); + fContext = sk_ref_sp(tmp->asDirectContext()); fSurface = fCreateVkSurfaceFn(fInstance); if (VK_NULL_HANDLE == fSurface) { diff --git a/tools/sk_app/Window.cpp b/tools/sk_app/Window.cpp index c8c6cd278d..3c5687cbb9 100644 --- a/tools/sk_app/Window.cpp +++ b/tools/sk_app/Window.cpp @@ -137,11 +137,11 @@ int Window::stencilBits() const { return fWindowContext->stencilBits(); } -GrContext* Window::getGrContext() const { +GrDirectContext* Window::directContext() const { if (!fWindowContext) { return nullptr; } - return fWindowContext->getGrContext(); + return fWindowContext->directContext(); } void Window::inval() { diff --git a/tools/sk_app/Window.h b/tools/sk_app/Window.h index f2d40360fc..9e64421523 100644 --- a/tools/sk_app/Window.h +++ b/tools/sk_app/Window.h @@ -16,7 +16,7 @@ #include "tools/skui/Key.h" #include "tools/skui/ModifierKey.h" -class GrContext; +class GrDirectContext; class SkCanvas; class SkSurface; class SkSurfaceProps; @@ -135,7 +135,7 @@ public: int stencilBits() const; // Returns null if there is not a GPU backend or if the backend is not yet created. - GrContext* getGrContext() const; + GrDirectContext* directContext() const; protected: Window(); diff --git a/tools/sk_app/WindowContext.cpp b/tools/sk_app/WindowContext.cpp new file mode 100644 index 0000000000..b470467386 --- /dev/null +++ b/tools/sk_app/WindowContext.cpp @@ -0,0 +1,21 @@ +/* + * Copyright 2020 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "tools/sk_app/WindowContext.h" + +#include "include/gpu/GrDirectContext.h" + +namespace sk_app { + +WindowContext::WindowContext(const DisplayParams& params) + : fDisplayParams(params) + , fSampleCount(1) + , fStencilBits(0) {} + +WindowContext::~WindowContext() {} + +} //namespace sk_app diff --git a/tools/sk_app/WindowContext.h b/tools/sk_app/WindowContext.h index 5b1058e3ee..0fec5e7366 100644 --- a/tools/sk_app/WindowContext.h +++ b/tools/sk_app/WindowContext.h @@ -9,24 +9,19 @@ #include "include/core/SkRefCnt.h" #include "include/core/SkSurfaceProps.h" -#include "include/gpu/GrContext.h" #include "include/gpu/GrTypes.h" #include "tools/sk_app/DisplayParams.h" +class GrDirectContext; class SkSurface; -class GrRenderTarget; namespace sk_app { class WindowContext { public: - WindowContext(const DisplayParams& params) - : fContext(nullptr) - , fDisplayParams(params) - , fSampleCount(1) - , fStencilBits(0) {} + WindowContext(const DisplayParams&); - virtual ~WindowContext() {} + virtual ~WindowContext(); virtual sk_sp getBackbufferSurface() = 0; @@ -39,7 +34,7 @@ public: const DisplayParams& getDisplayParams() { return fDisplayParams; } virtual void setDisplayParams(const DisplayParams& params) = 0; - GrContext* getGrContext() const { return fContext.get(); } + GrDirectContext* directContext() const { return fContext.get(); } int width() const { return fWidth; } int height() const { return fHeight; } @@ -49,7 +44,7 @@ public: protected: virtual bool isGpuContext() { return true; } - sk_sp fContext; + sk_sp fContext; int fWidth; int fHeight; diff --git a/tools/sk_app/win/D3D12WindowContext_win.cpp b/tools/sk_app/win/D3D12WindowContext_win.cpp index e092c22e81..8592158aa0 100644 --- a/tools/sk_app/win/D3D12WindowContext_win.cpp +++ b/tools/sk_app/win/D3D12WindowContext_win.cpp @@ -11,6 +11,7 @@ #include "tools/gpu/d3d/D3DTestUtils.h" #include "include/core/SkSurface.h" +#include "include/gpu/GrDirectContext.h" #include "include/gpu/d3d/GrD3DBackendContext.h" #include @@ -81,7 +82,9 @@ void D3D12WindowContext::initializeContext() { fDevice = backendContext.fDevice; fQueue = backendContext.fQueue; - fContext = GrContext::MakeDirect3D(backendContext, fDisplayParams.fGrContextOptions); + // CONTEXT TODO: MakeDirect3D should return an sk_sp + auto tmp = GrContext::MakeDirect3D(backendContext, fDisplayParams.fGrContextOptions); + fContext = sk_ref_sp(tmp->asDirectContext()); SkASSERT(fContext); // Make the swapchain diff --git a/tools/viewer/Viewer.cpp b/tools/viewer/Viewer.cpp index 9e20ddb707..cde26d1c83 100644 --- a/tools/viewer/Viewer.cpp +++ b/tools/viewer/Viewer.cpp @@ -11,7 +11,7 @@ #include "include/core/SkPictureRecorder.h" #include "include/core/SkStream.h" #include "include/core/SkSurface.h" -#include "include/gpu/GrContext.h" +#include "include/gpu/GrDirectContext.h" #include "include/private/SkTo.h" #include "include/utils/SkPaintFilterCanvas.h" #include "src/core/SkColorSpacePriv.h" @@ -1517,9 +1517,9 @@ void Viewer::onPaint(SkSurface* surface) { this->drawImGui(); - if (GrContext* ctx = fWindow->getGrContext()) { + if (auto direct = fWindow->directContext()) { // Clean out cache items that haven't been used in more than 10 seconds. - ctx->performDeferredCleanup(std::chrono::seconds(10)); + direct->performDeferredCleanup(std::chrono::seconds(10)); } } @@ -1743,7 +1743,7 @@ void Viewer::drawImGui() { ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver); DisplayParams params = fWindow->getRequestedDisplayParams(); bool paramsChanged = false; - const GrContext* ctx = fWindow->getGrContext(); + auto ctx = fWindow->directContext(); if (ImGui::Begin("Tools", &fShowImGuiDebugWindow, ImGuiWindowFlags_AlwaysVerticalScrollbar)) { @@ -2321,7 +2321,7 @@ void Viewer::drawImGui() { if (doLoad) { fPersistentCache.reset(); - fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting(); + ctx->priv().getGpu()->resetShaderCacheForTesting(); gLoadPending = true; } // We don't support updating SPIRV shaders. We could re-assemble them (with edits), @@ -2331,7 +2331,7 @@ void Viewer::drawImGui() { } if (doSave) { fPersistentCache.reset(); - fWindow->getGrContext()->priv().getGpu()->resetShaderCacheForTesting(); + ctx->priv().getGpu()->resetShaderCacheForTesting(); for (auto& entry : fCachedShaders) { SkSL::String backup = entry.fShader[kFragment_GrShaderType]; if (entry.fHovered) { @@ -2527,7 +2527,7 @@ void Viewer::updateUIState() { GpuPathRenderers pr = fWindow->getRequestedDisplayParams().fGrContextOptions.fGpuPathRenderers; WriteStateObject(writer, kPathRendererStateName, gPathRendererNames[pr].c_str(), [this](SkJSONWriter& writer) { - const GrContext* ctx = fWindow->getGrContext(); + auto ctx = fWindow->directContext(); if (!ctx) { writer.appendString("Software"); } else {