[Impeller] Add a way to query the gfx backend type. (flutter/engine#43837)

I've been trying to avoid this, but since the context type may vary
through runtime decisions, we just need something like this to select
which shader data to pull from the Dart GPU bundle at runtime. One
alternative would be to add a special omni shader concept to the HAL,
but that seems worse than just letting renderers key with a backend enum
sometimes.

Cautioned against using it for cap checks in the doc string.
This commit is contained in:
Brandon DeRosier
2023-07-21 13:11:32 -07:00
committed by GitHub
parent abcd4b44bf
commit 61b006d0ed
10 changed files with 49 additions and 0 deletions

View File

@@ -7,6 +7,7 @@
#include <string>
#include <vector>
#include "gmock/gmock-function-mocker.h"
#include "gmock/gmock.h"
#include "impeller/renderer/command_buffer.h"
#include "impeller/renderer/context.h"
@@ -64,6 +65,8 @@ class ContextMock : public Context {
public:
MOCK_CONST_METHOD0(DescribeGpuModel, std::string());
MOCK_CONST_METHOD0(GetBackendType, Context::BackendType());
MOCK_CONST_METHOD0(IsValid, bool());
MOCK_CONST_METHOD0(GetCapabilities,

View File

@@ -86,6 +86,10 @@ ContextGLES::ContextGLES(std::unique_ptr<ProcTableGLES> gl,
ContextGLES::~ContextGLES() = default;
Context::BackendType ContextGLES::GetBackendType() const {
return Context::BackendType::kOpenGLES;
}
const ReactorGLES::Ref& ContextGLES::GetReactor() const {
return reactor_;
}

View File

@@ -28,6 +28,9 @@ class ContextGLES final : public Context,
// |Context|
~ContextGLES() override;
// |Context|
BackendType GetBackendType() const override;
const ReactorGLES::Ref& GetReactor() const;
std::optional<ReactorGLES::WorkerID> AddReactorWorker(

View File

@@ -52,6 +52,9 @@ class ContextMTL final : public Context,
// |Context|
~ContextMTL() override;
// |Context|
BackendType GetBackendType() const override;
id<MTLDevice> GetMTLDevice() const;
// |Context|

View File

@@ -286,6 +286,10 @@ std::shared_ptr<ContextMTL> ContextMTL::Create(
ContextMTL::~ContextMTL() = default;
Context::BackendType ContextMTL::GetBackendType() const {
return Context::BackendType::kMetal;
}
// |Context|
std::string ContextMTL::DescribeGpuModel() const {
return std::string([[device_ name] UTF8String]);

View File

@@ -120,6 +120,10 @@ ContextVK::~ContextVK() {
CommandPoolVK::ClearAllPools(this);
}
Context::BackendType ContextVK::GetBackendType() const {
return Context::BackendType::kVulkan;
}
void ContextVK::Setup(Settings settings) {
TRACE_EVENT0("impeller", "ContextVK::Setup");

View File

@@ -55,6 +55,9 @@ class ContextVK final : public Context,
// |Context|
~ContextVK() override;
// |Context|
BackendType GetBackendType() const override;
// |Context|
std::string DescribeGpuModel() const override;

View File

@@ -43,11 +43,32 @@ class Allocator;
/// `//impeller/renderer/backend`.
class Context {
public:
enum class BackendType {
kMetal,
kOpenGLES,
kVulkan,
};
//----------------------------------------------------------------------------
/// @brief Destroys an Impeller context.
///
virtual ~Context();
//----------------------------------------------------------------------------
/// @brief Get the graphics backend of an Impeller context.
///
/// This is useful for cases where a renderer needs to track and
/// lookup backend-specific resources, like shaders or uniform
/// layout information.
///
/// It's not recommended to use this as a substitute for
/// per-backend capability checking. Instead, check for specific
/// capabilities via `GetCapabilities()`.
///
/// @return The graphics backend of the `Context`.
///
virtual BackendType GetBackendType() const = 0;
// TODO(129920): Refactor and move to capabilities.
virtual std::string DescribeGpuModel() const = 0;

View File

@@ -86,6 +86,8 @@ class MockCommandBuffer : public CommandBuffer {
class MockImpellerContext : public Context {
public:
MOCK_CONST_METHOD0(GetBackendType, Context::BackendType());
MOCK_CONST_METHOD0(DescribeGpuModel, std::string());
MOCK_CONST_METHOD0(IsValid, bool());

View File

@@ -113,6 +113,8 @@ class TestImpellerContext : public impeller::Context {
public:
TestImpellerContext() = default;
BackendType GetBackendType() const override { return BackendType::kMetal; }
std::string DescribeGpuModel() const override { return "TestGpu"; }
bool IsValid() const override { return true; }