Flutter Engine
 
Loading...
Searching...
No Matches
android_context_gl_impeller.cc
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
6
12#include "impeller/entity/gles/entity_shaders_gles.h"
13#include "impeller/entity/gles/framebuffer_blend_shaders_gles.h"
14#if !SLIMPELLER
15#include "impeller/entity/gles3/entity_shaders_gles.h"
16#include "impeller/entity/gles3/framebuffer_blend_shaders_gles.h"
17#endif // !SLIMPELLER
18
19namespace flutter {
20
23 public:
24 ReactorWorker() = default;
25
26 // |impeller::ReactorGLES::Worker|
27 ~ReactorWorker() override = default;
28
29 // |impeller::ReactorGLES::Worker|
31 const impeller::ReactorGLES& reactor) const override {
32 impeller::ReaderLock lock(mutex_);
33 auto found = reactions_allowed_.find(std::this_thread::get_id());
34 if (found == reactions_allowed_.end()) {
35 return false;
36 }
37 return found->second;
38 }
39
41 impeller::WriterLock lock(mutex_);
42 reactions_allowed_[std::this_thread::get_id()] = allowed;
43 }
44
45 private:
46 mutable impeller::RWMutex mutex_;
47 std::map<std::thread::id, bool> reactions_allowed_ IPLR_GUARDED_BY(mutex_);
48
50};
51
52static std::shared_ptr<impeller::Context> CreateImpellerContext(
53 const std::shared_ptr<impeller::ReactorGLES::Worker>& worker,
54 bool enable_gpu_tracing) {
55 auto proc_table = std::make_unique<impeller::ProcTableGLES>(
57
58 if (!proc_table->IsValid()) {
59 FML_LOG(ERROR) << "Could not create OpenGL proc table.";
60 return nullptr;
61 }
62 std::vector<std::shared_ptr<fml::Mapping>> gles2_shader_mappings = {
63 std::make_shared<fml::NonOwnedMapping>(
64 impeller_entity_shaders_gles_data,
65 impeller_entity_shaders_gles_length),
66 std::make_shared<fml::NonOwnedMapping>(
67 impeller_framebuffer_blend_shaders_gles_data,
68 impeller_framebuffer_blend_shaders_gles_length),
69 };
70
71// To maximally reduce code size, only load the older GLES2 shaders.
72#if !SLIMPELLER
73 bool is_gles3 = proc_table->GetDescription()->GetGlVersion().IsAtLeast(
74 impeller::Version{3, 0, 0});
75
76 std::vector<std::shared_ptr<fml::Mapping>> gles3_shader_mappings = {
77 std::make_shared<fml::NonOwnedMapping>(
78 impeller_entity_shaders_gles3_data,
79 impeller_entity_shaders_gles3_length),
80 std::make_shared<fml::NonOwnedMapping>(
81 impeller_framebuffer_blend_shaders_gles3_data,
82 impeller_framebuffer_blend_shaders_gles3_length),
83 };
84
85 auto context = impeller::ContextGLES::Create(
86 impeller::Flags{}, std::move(proc_table),
87 is_gles3 ? gles3_shader_mappings : gles2_shader_mappings,
88 enable_gpu_tracing);
89#else
90 auto context =
91 impeller::ContextGLES::Create(impeller::Flags{}, std::move(proc_table),
92 gles2_shader_mappings, enable_gpu_tracing);
93#endif // !SLIMPELLER
94
95 if (!context) {
96 FML_LOG(ERROR) << "Could not create OpenGLES Impeller Context.";
97 return nullptr;
98 }
99
100 if (!context->AddReactorWorker(worker).has_value()) {
101 FML_LOG(ERROR) << "Could not add reactor worker.";
102 return nullptr;
103 }
104 FML_LOG(IMPORTANT) << "Using the Impeller rendering backend (OpenGLES).";
105 return context;
106}
107
109 std::unique_ptr<impeller::egl::Display> display,
110 bool enable_gpu_tracing)
112 reactor_worker_(std::shared_ptr<ReactorWorker>(new ReactorWorker())),
113 display_(std::move(display)) {
114 if (!display_ || !display_->IsValid()) {
115 FML_LOG(ERROR) << "Could not create context with invalid EGL display.";
116 return;
117 }
118
125
127 std::unique_ptr<impeller::egl::Config> onscreen_config =
128 display_->ChooseConfig(desc);
129
130 if (!onscreen_config) {
132 onscreen_config = display_->ChooseConfig(desc);
133 }
134
135 if (!onscreen_config) {
136 // Fallback for Android emulator.
138 onscreen_config = display_->ChooseConfig(desc);
139 if (onscreen_config) {
140 FML_LOG(INFO) << "Warning: This device doesn't support MSAA for onscreen "
141 "framebuffers. Falling back to a single sample.";
142 } else {
143 FML_LOG(ERROR) << "Could not choose onscreen config.";
144 return;
145 }
146 }
147
149 auto offscreen_config = display_->ChooseConfig(desc);
150 if (!offscreen_config) {
151 FML_LOG(ERROR) << "Could not choose offscreen config.";
152 return;
153 }
154
155 auto onscreen_context = display_->CreateContext(*onscreen_config, nullptr);
156 if (!onscreen_context) {
157 FML_LOG(ERROR) << "Could not create onscreen context.";
158 return;
159 }
160
161 auto offscreen_context =
162 display_->CreateContext(*offscreen_config, onscreen_context.get());
163 if (!offscreen_context) {
164 FML_LOG(ERROR) << "Could not create offscreen context.";
165 return;
166 }
167
168 // Creating the impeller::Context requires a current context, which requires
169 // some surface.
170 auto offscreen_surface =
171 display_->CreatePixelBufferSurface(*offscreen_config, 1u, 1u);
172 if (!offscreen_context->MakeCurrent(*offscreen_surface)) {
173 FML_LOG(ERROR) << "Could not make offscreen context current.";
174 return;
175 }
176
177 auto impeller_context =
178 CreateImpellerContext(reactor_worker_, enable_gpu_tracing);
179
180 if (!impeller_context) {
181 FML_LOG(ERROR) << "Could not create Impeller context.";
182 return;
183 }
184
185 if (!offscreen_context->ClearCurrent()) {
186 FML_LOG(ERROR) << "Could not clear offscreen context.";
187 return;
188 }
189 // Setup context listeners.
191 [worker =
192 reactor_worker_](impeller::egl ::Context::LifecycleEvent event) {
193 switch (event) {
195 worker->SetReactionsAllowedOnCurrentThread(true);
196 break;
198 worker->SetReactionsAllowedOnCurrentThread(false);
199 break;
200 }
201 };
202 if (!onscreen_context->AddLifecycleListener(listener).has_value() ||
203 !offscreen_context->AddLifecycleListener(listener).has_value()) {
204 FML_LOG(ERROR) << "Could not add lifecycle listeners";
205 }
206
207 onscreen_config_ = std::move(onscreen_config);
208 offscreen_config_ = std::move(offscreen_config);
209 onscreen_context_ = std::move(onscreen_context);
210 offscreen_context_ = std::move(offscreen_context);
211 SetImpellerContext(impeller_context);
212
213 is_valid_ = true;
214}
215
217
219 return is_valid_;
220}
221
223 if (!offscreen_context_) {
224 return false;
225 }
226
227 return offscreen_context_->ClearCurrent();
228}
229
231 impeller::egl::Surface* offscreen_surface) {
232 if (!offscreen_context_ || !offscreen_surface) {
233 return false;
234 }
235
236 return offscreen_context_->MakeCurrent(*offscreen_surface);
237}
238
239std::unique_ptr<impeller::egl::Surface>
241 return display_->CreatePixelBufferSurface(*offscreen_config_, 1u, 1u);
242}
243
245 impeller::egl::Surface* onscreen_surface) {
246 if (!onscreen_surface || !onscreen_context_) {
247 return false;
248 }
249
250 return onscreen_context_->MakeCurrent(*onscreen_surface);
251}
252
254 if (!onscreen_context_) {
255 return false;
256 }
257
258 return onscreen_context_->ClearCurrent();
259}
260
261std::unique_ptr<impeller::egl::Surface>
263 return display_->CreateWindowSurface(*onscreen_config_, window);
264}
265
269
270} // namespace flutter
static std::shared_ptr< impeller::ContextMTL > CreateImpellerContext()
bool CanReactorReactOnCurrentThreadNow(const impeller::ReactorGLES &reactor) const override
Determines the ability of the worker to service a reaction on the current thread. The OpenGL context ...
std::unique_ptr< impeller::egl::Surface > CreateOnscreenSurface(EGLNativeWindowType window)
bool OnscreenContextMakeCurrent(impeller::egl::Surface *onscreen_surface)
AndroidRenderingAPI RenderingApi() const override
bool ResourceContextMakeCurrent(impeller::egl::Surface *offscreen_surface)
std::unique_ptr< impeller::egl::Surface > CreateOffscreenSurface()
AndroidContextGLImpeller(std::unique_ptr< impeller::egl::Display > display, bool enable_gpu_tracing)
Holds state that is shared across Android surfaces.
void SetImpellerContext(const std::shared_ptr< impeller::Context > &impeller_context)
static std::shared_ptr< ContextGLES > Create(const Flags &flags, std::unique_ptr< ProcTableGLES > gl, const std::vector< std::shared_ptr< fml::Mapping > > &shader_libraries, bool enable_gpu_tracing)
A delegate implemented by a thread on which an OpenGL context is current. There may be multiple worke...
The reactor attempts to make thread-safe usage of OpenGL ES easier to reason about.
std::function< void(LifecycleEvent)> LifecycleListener
Definition context.h:75
An instance of an EGL surface. There is no ability to create surfaces directly. Instead,...
Definition surface.h:18
GLFWwindow * window
Definition main.cc:60
#define FML_LOG(severity)
Definition logging.h:101
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
EGLDisplay display_
std::function< void *(const char *)> CreateProcAddressResolver()
Creates a proc address resolver that resolves function pointers to EGL and OpenGL (ES) procs.
Definition egl.cc:12
Definition ref_ptr.h:261
#define IPLR_GUARDED_BY(x)