[impeller] OpenGL ES: Refactor handle table and debug label management. (flutter/engine#33598)

Acquires fewer locks and doesn't throw GL errors when setting labels on
create-on-bind resources that haven't been bound yet. In such cases, the
label will be set on the next reaction.
This commit is contained in:
Chinmay Garde
2022-05-25 11:38:03 -07:00
committed by GitHub
parent 882de90924
commit 97bbedec8c
10 changed files with 179 additions and 137 deletions

View File

@@ -86,12 +86,11 @@ bool DeviceBufferGLES::BindAndUploadDataIfNecessary(BindingType type) const {
gl.BindBuffer(target_type, buffer.value());
if (upload_generation_ != generation_) {
TRACE_EVENT0("impeller", "BufferData");
TRACE_EVENT1("impeller", "BufferData", "Bytes",
std::to_string(backing_store_->GetLength()).c_str());
gl.BufferData(target_type, backing_store_->GetLength(),
backing_store_->GetBuffer(), GL_STATIC_DRAW);
upload_generation_ = generation_;
reactor_->SetDebugLabel(handle_, label_);
}
return true;
@@ -99,10 +98,7 @@ bool DeviceBufferGLES::BindAndUploadDataIfNecessary(BindingType type) const {
// |DeviceBuffer|
bool DeviceBufferGLES::SetLabel(const std::string& label) {
label_ = label;
if (upload_generation_ > 0) {
reactor_->SetDebugLabel(handle_, label_);
}
reactor_->SetDebugLabel(handle_, label);
return true;
}

View File

@@ -38,7 +38,6 @@ class DeviceBufferGLES final
private:
ReactorGLES::Ref reactor_;
HandleGLES handle_;
std::string label_;
mutable std::shared_ptr<Allocation> backing_store_;
mutable uint32_t generation_ = 0;
mutable uint32_t upload_generation_ = 0;

View File

@@ -4,8 +4,26 @@
#include "impeller/renderer/backend/gles/handle_gles.h"
#include "flutter/fml/logging.h"
namespace impeller {
//
std::string HandleTypeToString(HandleType type) {
switch (type) {
case HandleType::kUnknown:
return "Unknown";
case HandleType::kTexture:
return "Texture";
case HandleType::kBuffer:
return "Buffer";
case HandleType::kProgram:
return "Program";
case HandleType::kRenderBuffer:
return "RenderBuffer";
case HandleType::kFrameBuffer:
return "Framebuffer";
}
FML_UNREACHABLE();
}
} // namespace impeller

View File

@@ -5,6 +5,8 @@
#pragma once
#include <optional>
#include <sstream>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
@@ -25,51 +27,61 @@ enum class HandleType {
kFrameBuffer,
};
std::string HandleTypeToString(HandleType type);
class ReactorGLES;
struct HandleGLES {
HandleType type = HandleType::kUnknown;
std::optional<UniqueID> name;
static HandleGLES DeadHandle() {
return HandleGLES{HandleType::kUnknown, std::nullopt};
}
constexpr bool IsDead() const { return !name_.has_value(); }
constexpr bool IsDead() const { return !name.has_value(); }
struct Hash {
std::size_t operator()(const HandleGLES& handle) const {
return fml::HashCombine(
std::underlying_type_t<decltype(handle.type)>(handle.type),
handle.name_);
handle.name);
}
};
struct Equal {
bool operator()(const HandleGLES& lhs, const HandleGLES& rhs) const {
return lhs.type == rhs.type && lhs.name_ == rhs.name_;
return lhs.type == rhs.type && lhs.name == rhs.name;
}
};
private:
friend class ReactorGLES;
std::optional<UniqueID> name_;
HandleGLES(HandleType p_type, UniqueID p_name)
: type(p_type), name_(p_name) {}
HandleGLES(HandleType p_type, UniqueID p_name) : type(p_type), name(p_name) {}
HandleGLES(HandleType p_type, std::optional<UniqueID> p_name)
: type(p_type), name_(p_name) {}
: type(p_type), name(p_name) {}
static HandleGLES Create(HandleType type) {
return HandleGLES{type, UniqueID{}};
}
};
using GLESHandleSet =
std::unordered_set<HandleGLES, HandleGLES::Hash, HandleGLES::Equal>;
template <class T>
using GLESHandleMap =
std::unordered_map<HandleGLES, T, HandleGLES::Hash, HandleGLES::Equal>;
} // namespace impeller
namespace std {
inline std::ostream& operator<<(std::ostream& out,
const impeller::HandleGLES& handle) {
out << HandleTypeToString(handle.type) << "(";
if (handle.IsDead()) {
out << "DEAD";
} else {
out << handle.name.value().id;
}
out << ")";
return out;
}
} // namespace std

View File

@@ -249,23 +249,50 @@ static std::optional<GLenum> ToDebugIdentifier(DebugResourceType type) {
FML_UNREACHABLE();
}
void ProcTableGLES::SetDebugLabel(DebugResourceType type,
static bool ResourceIsLive(const ProcTableGLES& gl,
DebugResourceType type,
GLint name) {
switch (type) {
case DebugResourceType::kTexture:
return gl.IsTexture(name);
case DebugResourceType::kBuffer:
return gl.IsBuffer(name);
case DebugResourceType::kProgram:
return gl.IsProgram(name);
case DebugResourceType::kShader:
return gl.IsShader(name);
case DebugResourceType::kRenderBuffer:
return gl.IsRenderbuffer(name);
case DebugResourceType::kFrameBuffer:
return gl.IsFramebuffer(name);
}
FML_UNREACHABLE();
}
bool ProcTableGLES::SetDebugLabel(DebugResourceType type,
GLint name,
const std::string& label) const {
if (debug_label_max_length_ <= 0) {
return;
return true;
}
if (!ObjectLabelKHR.IsAvailable()) {
return true;
}
if (!ResourceIsLive(*this, type, name)) {
return false;
}
const auto identifier = ToDebugIdentifier(type);
const auto label_length =
std::min<GLsizei>(debug_label_max_length_ - 1, label.size());
if (!identifier.has_value()) {
return;
return true;
}
ObjectLabelKHR(identifier.value(), // identifier
name, // name
label_length, // length
label.data() // label
);
return true;
}
void ProcTableGLES::PushDebugGroup(const std::string& label) const {

View File

@@ -27,11 +27,9 @@ struct AutoErrorCheck {
~AutoErrorCheck() {
if (error_fn) {
auto error = error_fn();
if (error != GL_NO_ERROR) {
FML_LOG(ERROR) << "GL Error " << GLErrorToString(error) << "(" << error
<< ")"
<< " encountered on call to " << name;
}
FML_CHECK(error == GL_NO_ERROR)
<< "GL Error " << GLErrorToString(error) << "(" << error << ")"
<< " encountered on call to " << name;
}
}
};
@@ -130,8 +128,12 @@ struct GLProc {
PROC(GetShaderiv); \
PROC(GetString); \
PROC(GetUniformLocation); \
PROC(IsBuffer); \
PROC(IsFramebuffer); \
PROC(IsProgram); \
PROC(IsRenderbuffer); \
PROC(IsShader); \
PROC(IsTexture); \
PROC(LinkProgram); \
PROC(RenderbufferStorage); \
PROC(Scissor); \
@@ -191,7 +193,7 @@ class ProcTableGLES {
bool IsCurrentFramebufferComplete() const;
void SetDebugLabel(DebugResourceType type,
bool SetDebugLabel(DebugResourceType type,
GLint name,
const std::string& label) const;

View File

@@ -41,7 +41,7 @@ bool ReactorGLES::RemoveWorker(WorkerID worker) {
bool ReactorGLES::HasPendingOperations() const {
Lock ops_lock(ops_mutex_);
return !pending_operations_.empty() || !gl_handles_to_collect_.empty();
return !ops_.empty();
}
const ProcTableGLES& ReactorGLES::GetProcTable() const {
@@ -51,10 +51,19 @@ const ProcTableGLES& ReactorGLES::GetProcTable() const {
std::optional<GLuint> ReactorGLES::GetGLHandle(const HandleGLES& handle) const {
ReaderLock handles_lock(handles_mutex_);
auto found = live_gl_handles_.find(handle);
if (found != live_gl_handles_.end()) {
return found->second;
if (auto found = handles_.find(handle); found != handles_.end()) {
if (found->second.pending_collection) {
VALIDATION_LOG
<< "Attempted to acquire a handle that was pending collection.";
return std::nullopt;
}
if (!found->second.name.has_value()) {
VALIDATION_LOG << "Attempt to acquire a handle outside of an operation.";
return std::nullopt;
}
return found->second.name;
}
VALIDATION_LOG << "Attempted to acquire an invalid GL handle.";
return std::nullopt;
}
@@ -64,7 +73,7 @@ bool ReactorGLES::AddOperation(Operation operation) {
}
{
Lock ops_lock(ops_mutex_);
pending_operations_.emplace_back(std::move(operation));
ops_.emplace_back(std::move(operation));
}
// Attempt a reaction if able but it is not an error if this isn't possible.
[[maybe_unused]] auto result = React();
@@ -129,31 +138,25 @@ HandleGLES ReactorGLES::CreateHandle(HandleType type) {
return HandleGLES::DeadHandle();
}
WriterLock handles_lock(handles_mutex_);
live_gl_handles_[new_handle] =
in_reaction_ ? CreateGLHandle(GetProcTable(), type) : std::nullopt;
auto gl_handle = CanReactOnCurrentThread()
? CreateGLHandle(GetProcTable(), type)
: std::nullopt;
handles_[new_handle] = LiveHandle{std::move(gl_handle)};
return new_handle;
}
void ReactorGLES::CollectHandle(HandleGLES handle) {
WriterLock handles_lock(handles_mutex_);
auto live_handle = live_gl_handles_.find(handle);
if (live_handle == live_gl_handles_.end()) {
return;
if (auto found = handles_.find(handle); found != handles_.end()) {
found->second.pending_collection = true;
}
if (live_handle->second.has_value()) {
Lock ops_lock(ops_mutex_);
gl_handles_to_collect_[live_handle->first] = live_handle->second.value();
}
live_gl_handles_.erase(live_handle);
}
bool ReactorGLES::React() {
TRACE_EVENT0("impeller", "ReactorGLES::React");
if (!CanReactOnCurrentThread()) {
return false;
}
in_reaction_ = true;
fml::ScopedCleanupClosure reset_in_reaction([&]() { in_reaction_ = false; });
TRACE_EVENT0("impeller", "ReactorGLES::React");
while (HasPendingOperations()) {
if (!ReactOnce()) {
return false;
@@ -184,85 +187,63 @@ bool ReactorGLES::ReactOnce() {
if (!IsValid()) {
return false;
}
TRACE_EVENT0("impeller", __FUNCTION__);
return ConsolidateHandles() && FlushOps();
}
bool ReactorGLES::ConsolidateHandles() {
TRACE_EVENT0("impeller", __FUNCTION__);
const auto& gl = GetProcTable();
//----------------------------------------------------------------------------
/// Collect all the handles for whom there is a GL handle sibling.
///
decltype(gl_handles_to_collect_) gl_handles_to_collect;
{
Lock ops_lock(ops_mutex_);
std::swap(gl_handles_to_collect_, gl_handles_to_collect);
FML_DCHECK(gl_handles_to_collect_.empty());
}
for (const auto& handle_to_collect : gl_handles_to_collect) {
if (!CollectGLHandle(gl, // proc table
handle_to_collect.first.type, // handle type
handle_to_collect.second // GL handle name
)) {
VALIDATION_LOG << "Could not collect GL handle.";
return false;
}
}
//----------------------------------------------------------------------------
/// Make sure all pending handles have a GL handle sibling.
///
{
WriterLock handles_lock(handles_mutex_);
for (auto& live_handle : live_gl_handles_) {
if (live_handle.second.has_value()) {
// Already a realized GL handle.
continue;
WriterLock handles_lock(handles_mutex_);
std::vector<HandleGLES> handles_to_delete;
for (auto& handle : handles_) {
// Collect dead handles.
if (handle.second.pending_collection) {
// This could be false if the handle was created and collected without
// use. We still need to get rid of map entry.
if (handle.second.name.has_value()) {
CollectGLHandle(gl, handle.first.type, handle.second.name.value());
}
auto gl_handle = CreateGLHandle(gl, live_handle.first.type);
if (!gl_handle.has_value()) {
handles_to_delete.push_back(handle.first);
continue;
}
// Create live handles.
if (!handle.second.name.has_value()) {
auto gl_handle = CreateGLHandle(gl, handle.first.type);
if (!gl_handle) {
VALIDATION_LOG << "Could not create GL handle.";
return false;
}
live_handle.second = gl_handle;
handle.second.name = std::move(gl_handle);
}
// Set pending debug labels.
if (handle.second.pending_debug_label.has_value()) {
if (gl.SetDebugLabel(ToDebugResourceType(handle.first.type),
handle.second.name.value(),
handle.second.pending_debug_label.value())) {
handle.second.pending_debug_label = std::nullopt;
}
}
}
for (const auto& handle_to_delete : handles_to_delete) {
handles_.erase(handle_to_delete);
}
return true;
}
//----------------------------------------------------------------------------
/// Flush all pending operations in order.
///
decltype(pending_operations_) pending_operations;
bool ReactorGLES::FlushOps() {
TRACE_EVENT0("impeller", __FUNCTION__);
// Do NOT hold the ops or handles locks while performing operations in case
// the ops enqueue more ops.
decltype(ops_) ops;
{
Lock ops_lock(ops_mutex_);
std::swap(pending_operations_, pending_operations);
FML_DCHECK(pending_operations_.empty());
std::swap(ops_, ops);
}
for (const auto& operation : pending_operations) {
for (const auto& op : ops) {
TRACE_EVENT0("impeller", "ReactorGLES::Operation");
operation(*this);
op(*this);
}
//----------------------------------------------------------------------------
/// Make sure all pending debug labels have been flushed.
///
decltype(pending_debug_labels_) pending_debug_labels;
{
WriterLock handles_lock(handles_mutex_);
std::swap(pending_debug_labels_, pending_debug_labels);
FML_DCHECK(pending_debug_labels_.empty());
}
if (!pending_debug_labels.empty()) {
ReaderLock handles_lock(handles_mutex_);
for (const auto& label : pending_debug_labels) {
auto live_handle = live_gl_handles_.find(label.first);
if (live_handle == live_gl_handles_.end() ||
!live_handle->second.has_value()) {
continue;
}
gl.SetDebugLabel(ToDebugResourceType(label.first.type), // type
live_handle->second.value(), // name
label.second // label
);
}
}
return true;
}
@@ -270,14 +251,13 @@ void ReactorGLES::SetDebugLabel(const HandleGLES& handle, std::string label) {
if (!can_set_debug_labels_) {
return;
}
if (label.empty()) {
return;
}
if (handle.IsDead()) {
return;
}
WriterLock handles_lock(handles_mutex_);
pending_debug_labels_[handle] = std::move(label);
if (auto found = handles_.find(handle); found != handles_.end()) {
found->second.pending_debug_label = std::move(label);
}
}
bool ReactorGLES::CanReactOnCurrentThread() const {

View File

@@ -56,26 +56,38 @@ class ReactorGLES {
[[nodiscard]] bool React();
private:
struct LiveHandle {
std::optional<GLuint> name;
std::optional<std::string> pending_debug_label;
bool pending_collection = false;
LiveHandle() = default;
explicit LiveHandle(std::optional<GLuint> p_name)
: name(std::move(p_name)) {}
constexpr bool IsLive() const { return name.has_value(); }
};
std::unique_ptr<ProcTableGLES> proc_table_;
mutable Mutex ops_mutex_;
std::vector<Operation> pending_operations_ IPLR_GUARDED_BY(ops_mutex_);
GLESHandleMap<GLuint> gl_handles_to_collect_ IPLR_GUARDED_BY(ops_mutex_);
std::vector<Operation> ops_ IPLR_GUARDED_BY(ops_mutex_);
// Make sure the container is one where erasing items during iteration doesn't
// invalidate other iterators.
using LiveHandles = std::unordered_map<HandleGLES,
LiveHandle,
HandleGLES::Hash,
HandleGLES::Equal>;
mutable RWMutex handles_mutex_;
GLESHandleMap<std::optional<GLuint>> live_gl_handles_
IPLR_GUARDED_BY(handles_mutex_);
GLESHandleMap<std::string> pending_debug_labels_
IPLR_GUARDED_BY(handles_mutex_);
LiveHandles handles_ IPLR_GUARDED_BY(handles_mutex_);
mutable Mutex workers_mutex_;
mutable std::map<WorkerID, std::weak_ptr<Worker>> workers_
IPLR_GUARDED_BY(workers_mutex_);
// TODO(csg): Make this thread safe.
bool in_reaction_ = false;
bool can_set_debug_labels_ = false;
bool is_valid_ = false;
bool ReactOnce();
@@ -84,6 +96,10 @@ class ReactorGLES {
bool CanReactOnCurrentThread() const;
bool ConsolidateHandles();
bool FlushOps();
FML_DISALLOW_COPY_AND_ASSIGN(ReactorGLES);
};

View File

@@ -69,10 +69,7 @@ bool TextureGLES::IsValid() const {
// |Texture|
void TextureGLES::SetLabel(const std::string_view& label) {
label_ = std::string{label.data(), label.size()};
if (contents_initialized_) {
reactor_->SetDebugLabel(handle_, label_);
}
reactor_->SetDebugLabel(handle_, std::string{label.data(), label.size()});
}
struct TexImage2DData {
@@ -247,9 +244,6 @@ bool TextureGLES::OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
};
contents_initialized_ = reactor_->AddOperation(texture_upload);
if (contents_initialized_) {
reactor_->SetDebugLabel(handle_, label_);
}
return contents_initialized_;
}
@@ -342,7 +336,6 @@ void TextureGLES::InitializeContentsIfNecessary() const {
break;
}
reactor_->SetDebugLabel(handle_, label_);
}
bool TextureGLES::Bind() const {

View File

@@ -55,7 +55,6 @@ class TextureGLES final : public Texture,
HandleGLES handle_;
mutable bool contents_initialized_ = false;
const bool is_wrapped_;
std::string label_;
bool is_valid_ = false;
TextureGLES(std::shared_ptr<ReactorGLES> reactor,