Flutter Impeller
reactor_gles.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <functional>
8 #include <memory>
9 #include <vector>
10 
11 #include "flutter/fml/closure.h"
12 #include "flutter/fml/macros.h"
13 #include "impeller/base/thread.h"
16 
17 namespace impeller {
18 
19 class ReactorGLES {
20  public:
21  using WorkerID = UniqueID;
22 
23  class Worker {
24  public:
25  virtual ~Worker() = default;
26 
28  const ReactorGLES& reactor) const = 0;
29  };
30 
31  using Ref = std::shared_ptr<ReactorGLES>;
32 
33  ReactorGLES(std::unique_ptr<ProcTableGLES> gl);
34 
35  ~ReactorGLES();
36 
37  bool IsValid() const;
38 
39  WorkerID AddWorker(std::weak_ptr<Worker> worker);
40 
41  bool RemoveWorker(WorkerID);
42 
43  const ProcTableGLES& GetProcTable() const;
44 
45  std::optional<GLuint> GetGLHandle(const HandleGLES& handle) const;
46 
48 
49  void CollectHandle(HandleGLES handle);
50 
51  void SetDebugLabel(const HandleGLES& handle, std::string label);
52 
53  using Operation = std::function<void(const ReactorGLES& reactor)>;
54  [[nodiscard]] bool AddOperation(Operation operation);
55 
56  [[nodiscard]] bool React();
57 
58  private:
59  struct LiveHandle {
60  std::optional<GLuint> name;
61  std::optional<std::string> pending_debug_label;
62  bool pending_collection = false;
63 
64  LiveHandle() = default;
65 
66  explicit LiveHandle(std::optional<GLuint> p_name)
67  : name(std::move(p_name)) {}
68 
69  constexpr bool IsLive() const { return name.has_value(); }
70  };
71 
72  std::unique_ptr<ProcTableGLES> proc_table_;
73 
74  mutable Mutex ops_mutex_;
75  std::vector<Operation> ops_ IPLR_GUARDED_BY(ops_mutex_);
76 
77  // Make sure the container is one where erasing items during iteration doesn't
78  // invalidate other iterators.
79  using LiveHandles = std::unordered_map<HandleGLES,
80  LiveHandle,
81  HandleGLES::Hash,
82  HandleGLES::Equal>;
83  mutable RWMutex handles_mutex_;
84  LiveHandles handles_ IPLR_GUARDED_BY(handles_mutex_);
85 
86  mutable Mutex workers_mutex_;
87  mutable std::map<WorkerID, std::weak_ptr<Worker>> workers_
88  IPLR_GUARDED_BY(workers_mutex_);
89 
90  bool can_set_debug_labels_ = false;
91  bool is_valid_ = false;
92 
93  bool ReactOnce();
94 
95  bool HasPendingOperations() const;
96 
97  bool CanReactOnCurrentThread() const;
98 
99  bool ConsolidateHandles();
100 
101  bool FlushOps();
102 
103  FML_DISALLOW_COPY_AND_ASSIGN(ReactorGLES);
104 };
105 
106 } // namespace impeller
impeller::ReactorGLES::Operation
std::function< void(const ReactorGLES &reactor)> Operation
Definition: reactor_gles.h:53
impeller::ReactorGLES::GetProcTable
const ProcTableGLES & GetProcTable() const
Definition: reactor_gles.cc:47
impeller::ReactorGLES::ReactorGLES
ReactorGLES(std::unique_ptr< ProcTableGLES > gl)
Definition: reactor_gles.cc:14
impeller::ReactorGLES::SetDebugLabel
void SetDebugLabel(const HandleGLES &handle, std::string label)
Definition: reactor_gles.cc:250
impeller::ReactorGLES::Worker::CanReactorReactOnCurrentThreadNow
virtual bool CanReactorReactOnCurrentThreadNow(const ReactorGLES &reactor) const =0
impeller::ReactorGLES::CreateHandle
HandleGLES CreateHandle(HandleType type)
Definition: reactor_gles.cc:132
impeller::ReactorGLES::Ref
std::shared_ptr< ReactorGLES > Ref
Definition: reactor_gles.h:31
impeller::HandleType
HandleType
Definition: handle_gles.h:21
impeller::ReactorGLES::Worker::~Worker
virtual ~Worker()=default
impeller::ReactorGLES::Worker
Definition: reactor_gles.h:23
impeller::ReactorGLES::React
bool React()
Definition: reactor_gles.cc:155
impeller::ReactorGLES::~ReactorGLES
~ReactorGLES()
impeller::HandleGLES
Definition: handle_gles.h:34
impeller::ProcTableGLES
Definition: proc_table_gles.h:188
impeller::ReactorGLES::RemoveWorker
bool RemoveWorker(WorkerID)
Definition: reactor_gles.cc:37
impeller::ReactorGLES::IsValid
bool IsValid() const
Definition: reactor_gles.cc:26
proc_table_gles.h
impeller::ReactorGLES::CollectHandle
void CollectHandle(HandleGLES handle)
Definition: reactor_gles.cc:148
impeller::ReactorGLES::AddOperation
bool AddOperation(Operation operation)
Definition: reactor_gles.cc:70
impeller::ReactorGLES::AddWorker
WorkerID AddWorker(std::weak_ptr< Worker > worker)
Definition: reactor_gles.cc:30
handle_gles.h
std
Definition: comparable.h:98
impeller::ReactorGLES
Definition: reactor_gles.h:19
impeller::UniqueID
Definition: comparable.h:19
thread.h
impeller
Definition: aiks_context.cc:10
impeller::ReactorGLES::GetGLHandle
std::optional< GLuint > GetGLHandle(const HandleGLES &handle) const
Definition: reactor_gles.cc:52