Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
embedder_surface_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
7#include <utility>
8
10#include "impeller/entity/gles/entity_shaders_gles.h"
11#include "impeller/entity/gles/framebuffer_blend_shaders_gles.h"
12#include "impeller/entity/gles/modern_shaders_gles.h"
13#include "impeller/entity/gles3/entity_shaders_gles.h"
14#include "impeller/entity/gles3/framebuffer_blend_shaders_gles.h"
15#include "impeller/entity/gles3/modern_shaders_gles.h"
18
19namespace flutter {
20
21namespace {
22std::vector<std::shared_ptr<fml::Mapping>> GetShaderMappings(bool is_gles3) {
23 if (is_gles3) {
24 return {
25 std::make_shared<fml::NonOwnedMapping>(
26 impeller_entity_shaders_gles3_data,
27 impeller_entity_shaders_gles3_length),
28 std::make_shared<fml::NonOwnedMapping>(
29 impeller_modern_shaders_gles3_data,
30 impeller_modern_shaders_gles3_length),
31 std::make_shared<fml::NonOwnedMapping>(
32 impeller_framebuffer_blend_shaders_gles3_data,
33 impeller_framebuffer_blend_shaders_gles3_length),
34 };
35 }
36 return {
37 std::make_shared<fml::NonOwnedMapping>(
38 impeller_entity_shaders_gles_data,
39 impeller_entity_shaders_gles_length),
40 std::make_shared<fml::NonOwnedMapping>(
41 impeller_modern_shaders_gles_data,
42 impeller_modern_shaders_gles_length),
43 std::make_shared<fml::NonOwnedMapping>(
44 impeller_framebuffer_blend_shaders_gles_data,
45 impeller_framebuffer_blend_shaders_gles_length),
46 };
47}
48} // namespace
49
51 public:
52 ReactorWorker() = default;
53
54 // |ReactorGLES::Worker|
56 const impeller::ReactorGLES& reactor) const override {
57 impeller::ReaderLock lock(mutex_);
58 auto found = reactions_allowed_.find(std::this_thread::get_id());
59 if (found == reactions_allowed_.end()) {
60 return false;
61 }
62 return found->second;
63 }
64
66 impeller::WriterLock lock(mutex_);
67 reactions_allowed_[std::this_thread::get_id()] = allowed;
68 }
69
70 private:
71 mutable impeller::RWMutex mutex_;
72 std::map<std::thread::id, bool> reactions_allowed_ IPLR_GUARDED_BY(mutex_);
73
75};
76
79 bool fbo_reset_after_present,
80 std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder,
81 std::shared_ptr<fml::BasicTaskRunner> io_task_runner,
82 impeller::Flags impeller_flags)
83 : gl_dispatch_table_(std::move(gl_dispatch_table)),
84 fbo_reset_after_present_(fbo_reset_after_present),
85 external_view_embedder_(std::move(external_view_embedder)),
86 worker_(std::make_shared<ReactorWorker>()) {
87 // Make sure all required members of the dispatch table are checked.
88 if (!gl_dispatch_table_.gl_make_current_callback ||
89 !gl_dispatch_table_.gl_clear_current_callback ||
90 !gl_dispatch_table_.gl_present_callback ||
91 !gl_dispatch_table_.gl_fbo_callback ||
92 !gl_dispatch_table_.gl_populate_existing_damage ||
93 !gl_dispatch_table_.gl_proc_resolver) {
94 return;
95 }
96 // Certain GL backends need to made current before any GL
97 // state can be accessed.
98 gl_dispatch_table_.gl_make_current_callback();
99
100 auto gl = std::make_unique<impeller::ProcTableGLES>(
101 gl_dispatch_table_.gl_proc_resolver);
102 if (!gl->IsValid()) {
103 return;
104 }
105
106 const auto is_gles3 =
107 gl->GetDescription()->GetGlVersion().IsAtLeast(impeller::Version(3));
108 const auto shader_mappings = GetShaderMappings(is_gles3);
109
110 impeller_context_ = impeller::ContextGLES::Create(
111 impeller_flags, std::move(gl), shader_mappings,
112 /*enable_gpu_tracing=*/false, std::move(io_task_runner));
113
114 if (!impeller_context_) {
115 FML_LOG(ERROR) << "Could not create Impeller context.";
116 return;
117 }
118
119 auto worker_id = impeller_context_->AddReactorWorker(worker_);
120 if (!worker_id.has_value()) {
121 FML_LOG(ERROR) << "Could not add reactor worker.";
122 return;
123 }
124
125 gl_dispatch_table_.gl_clear_current_callback();
126 if (impeller_flags.use_sdfs) {
127 FML_LOG(IMPORTANT) << "Using the Impeller rendering backend (OpenGLESSDF).";
128 } else {
129 FML_LOG(IMPORTANT) << "Using the Impeller rendering backend (OpenGLES).";
130 }
131 valid_ = true;
132}
133
135
136// |EmbedderSurface|
137bool EmbedderSurfaceGLImpeller::IsValid() const {
138 return valid_;
139}
140
141// |GPUSurfaceGLDelegate|
142std::unique_ptr<GLContextResult>
143EmbedderSurfaceGLImpeller::GLContextMakeCurrent() {
144 worker_->SetReactionsAllowedOnCurrentThread(true);
145 return std::make_unique<GLContextDefaultResult>(
146 gl_dispatch_table_.gl_make_current_callback());
147}
148
149// |GPUSurfaceGLDelegate|
150bool EmbedderSurfaceGLImpeller::GLContextClearCurrent() {
151 worker_->SetReactionsAllowedOnCurrentThread(false);
152 return gl_dispatch_table_.gl_clear_current_callback();
153}
154
155// |GPUSurfaceGLDelegate|
156bool EmbedderSurfaceGLImpeller::GLContextPresent(
157 const GLPresentInfo& present_info) {
158 // Pass the present information to the embedder present callback.
159 return gl_dispatch_table_.gl_present_callback(present_info);
160}
161
162// |GPUSurfaceGLDelegate|
163GLFBOInfo EmbedderSurfaceGLImpeller::GLContextFBO(
164 GLFrameInfo frame_info) const {
165 // Get the FBO ID using the gl_fbo_callback and then get exiting damage by
166 // passing that ID to the gl_populate_existing_damage.
167 return gl_dispatch_table_.gl_populate_existing_damage(
168 gl_dispatch_table_.gl_fbo_callback(frame_info));
169}
170
171// |GPUSurfaceGLDelegate|
172bool EmbedderSurfaceGLImpeller::GLContextFBOResetAfterPresent() const {
173 return fbo_reset_after_present_;
174}
175
176// |GPUSurfaceGLDelegate|
177DlMatrix EmbedderSurfaceGLImpeller::GLContextSurfaceTransformation() const {
178 auto callback = gl_dispatch_table_.gl_surface_transformation_callback;
179 if (!callback) {
180 return DlMatrix();
181 }
182 return callback();
183}
184
185// |GPUSurfaceGLDelegate|
187EmbedderSurfaceGLImpeller::GetGLProcResolver() const {
188 return gl_dispatch_table_.gl_proc_resolver;
189}
190
191// |GPUSurfaceGLDelegate|
192SurfaceFrame::FramebufferInfo
193EmbedderSurfaceGLImpeller::GLContextFramebufferInfo() const {
194 // Enable partial repaint by default on the embedders.
195 auto info = SurfaceFrame::FramebufferInfo{};
196 info.supports_readback = true;
197 info.supports_partial_repaint =
198 gl_dispatch_table_.gl_populate_existing_damage != nullptr;
199 return info;
200}
201
202// |EmbedderSurface|
203std::unique_ptr<Surface> EmbedderSurfaceGLImpeller::CreateGPUSurface() {
204 // Ensure that the GL context is current before creating the GPU surface.
205 // GPUSurfaceGLImpeller initialization will set up shader pipelines, and the
206 // current thread needs to be able to execute reactor operations.
207 GLContextMakeCurrent();
208
209 return std::make_unique<GPUSurfaceGLImpeller>(
210 this, // GPU surface GL delegate
211 impeller_context_, // Impeller context
212 !external_view_embedder_ // render to surface
213 );
214}
215
216// |EmbedderSurface|
217std::shared_ptr<impeller::Context>
218EmbedderSurfaceGLImpeller::CreateImpellerContext() const {
219 return impeller_context_;
220}
221
222// |EmbedderSurface|
223sk_sp<GrDirectContext> EmbedderSurfaceGLImpeller::CreateResourceContext()
224 const {
225 if (gl_dispatch_table_.gl_make_resource_current_callback()) {
226 worker_->SetReactionsAllowedOnCurrentThread(true);
227 } else {
228 FML_DLOG(ERROR) << "Could not make the resource context current.";
229 worker_->SetReactionsAllowedOnCurrentThread(false);
230 }
231 return nullptr;
232}
233
234// |EmbedderSurface|
235void EmbedderSurfaceGLImpeller::ReleaseResourceContext() const {
236 worker_->SetReactionsAllowedOnCurrentThread(false);
237 gl_dispatch_table_.gl_clear_current_callback();
238}
239
240} // namespace flutter
EmbedderSurfaceGLImpeller(EmbedderSurfaceGLSkia::GLDispatchTable gl_dispatch_table, bool fbo_reset_after_present, std::shared_ptr< EmbedderExternalViewEmbedder > external_view_embedder, std::shared_ptr< fml::BasicTaskRunner > io_task_runner, impeller::Flags impeller_flags={})
std::function< void *(const char *)> GLProcResolver
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 ...
void SetReactionsAllowedOnCurrentThread(bool allowed)
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, std::shared_ptr< fml::BasicTaskRunner > io_task_runner=nullptr)
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.
FlutterDesktopBinaryReply callback
#define FML_DLOG(severity)
Definition logging.h:121
#define FML_LOG(severity)
Definition logging.h:101
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
impeller::Matrix DlMatrix
Definition ref_ptr.h:261
std::shared_ptr< ReactorGLES > reactor
std::function< GLFBOInfo(intptr_t)> gl_populate_existing_damage
std::function< intptr_t(GLFrameInfo)> gl_fbo_callback
std::function< bool(GLPresentInfo)> gl_present_callback
std::function< void *(const char *)> gl_proc_resolver
std::function< DlMatrix(void)> gl_surface_transformation_callback
bool use_sdfs
Use SDFs for rendering.
Definition flags.h:11
#define IPLR_GUARDED_BY(x)