Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
context.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 <future>
8
11#include "fml/make_copyable.h"
17
18namespace flutter {
19namespace gpu {
20
22 auto& capabilities = context.GetCapabilities();
23 return capabilities->SupportsOffscreenMSAA() &&
24 !capabilities->SupportsImplicitResolvingMSAA();
25}
26
28
29std::shared_ptr<impeller::Context> Context::default_context_;
30
31void Context::SetOverrideContext(std::shared_ptr<impeller::Context> context) {
32 default_context_ = std::move(context);
33}
34
35std::shared_ptr<impeller::Context> Context::GetOverrideContext() {
36 return default_context_;
37}
38
39std::shared_ptr<impeller::Context> Context::GetDefaultContext(
40 std::optional<std::string>& out_error) {
41 auto override_context = GetOverrideContext();
42 if (override_context) {
43 return override_context;
44 }
45
46 auto dart_state = flutter::UIDartState::Current();
47 if (!dart_state->IsImpellerEnabled()) {
48 out_error =
49 "Flutter GPU requires the Impeller rendering backend, but Impeller is "
50 "not enabled. For more details about where Impeller is available and "
51 "how to enable it, see: https://docs.flutter.dev/perf/impeller";
52 return nullptr;
53 }
54 if (!dart_state->IsFlutterGPUEnabled()) {
55 out_error =
56 "Flutter GPU must be enabled via the Flutter GPU manifest "
57 "setting. This can be done either via command line argument "
58 "--enable-flutter-gpu or "
59 "by adding the FLTEnableFlutterGPU key set to true in the Info.plist "
60 "on iOS/macOS, or the io.flutter.embedding.android.EnableFlutterGPU "
61 "metadata key to true in the AndroidManifest.xml on Android.";
62 return nullptr;
63 }
64 // Grab the Impeller context from the IO manager.
65 std::promise<std::shared_ptr<impeller::Context>> context_promise;
66 auto impeller_context_future = context_promise.get_future();
68 dart_state->GetTaskRunners().GetIOTaskRunner(),
69 fml::MakeCopyable([promise = std::move(context_promise),
70 io_manager = dart_state->GetIOManager()]() mutable {
71 promise.set_value(io_manager ? io_manager->GetImpellerContext()
72 : nullptr);
73 }));
74 auto context = impeller_context_future.get();
75
76 if (!context) {
77 out_error = "Unable to retrieve the Impeller context.";
78 }
79 return context;
80}
81
82Context::Context(std::shared_ptr<impeller::Context> context)
83 : context_(std::move(context)) {}
84
85Context::~Context() = default;
86
88 return *context_;
89}
90
91std::shared_ptr<impeller::Context>& Context::GetContextShared() {
92 return context_;
93}
94
95} // namespace gpu
96} // namespace flutter
97
98//----------------------------------------------------------------------------
99/// Exports
100///
101
102Dart_Handle InternalFlutterGpu_Context_InitializeDefault(Dart_Handle wrapper) {
103 std::optional<std::string> out_error;
104 auto impeller_context = flutter::gpu::Context::GetDefaultContext(out_error);
105 if (out_error.has_value()) {
106 return tonic::ToDart(out_error.value());
107 }
108
109 auto res = fml::MakeRefCounted<flutter::gpu::Context>(impeller_context);
110 res->AssociateWithDartWrapper(wrapper);
111
112 return Dart_Null();
113}
114
116 flutter::gpu::Context* wrapper) {
117 return static_cast<int>(flutter::gpu::FromImpellerPixelFormat(
118 wrapper->GetContext().GetCapabilities()->GetDefaultColorFormat()));
119}
120
122 flutter::gpu::Context* wrapper) {
123 return static_cast<int>(flutter::gpu::FromImpellerPixelFormat(
124 wrapper->GetContext().GetCapabilities()->GetDefaultStencilFormat()));
125}
126
128 flutter::gpu::Context* wrapper) {
129 return static_cast<int>(flutter::gpu::FromImpellerPixelFormat(
130 wrapper->GetContext().GetCapabilities()->GetDefaultDepthStencilFormat()));
131}
132
134 flutter::gpu::Context* wrapper) {
135 return wrapper->GetContext().GetCapabilities()->GetMinimumUniformAlignment();
136}
137
142
144 flutter::gpu::Context* wrapper) {
145 return wrapper->GetContext()
147 ->SupportsFramebufferRenderMipmap();
148}
149
151 flutter::gpu::Context* wrapper) {
152 return wrapper->GetContext()
154 ->SupportsManuallyMippedTextures();
155}
156
158 flutter::gpu::Context* wrapper,
159 int family) {
160 return wrapper->GetContext().GetCapabilities()->SupportsTextureCompression(
162}
163
165 flutter::gpu::Context* wrapper,
166 int format,
167 bool render_target,
168 bool shader_read,
169 bool shader_write) {
170 const impeller::PixelFormat impeller_format =
172 if (impeller_format == impeller::PixelFormat::kUnknown) {
173 return false;
174 }
175 // Compressed formats are sample-only: shader_read must be true, and
176 // render-target or shader-write usage is never available.
177 if (impeller::IsCompressed(impeller_format)) {
178 if (render_target || shader_write || !shader_read) {
179 return false;
180 }
181 return wrapper->GetContext().GetCapabilities()->SupportsTextureCompression(
183 }
184 // For uncompressed formats, today's Impeller capability surface does not
185 // expose a per-format usage query, so this returns true. As that surface
186 // grows it should be wired in here.
187 return true;
188}
static UIDartState * Current()
static std::shared_ptr< impeller::Context > GetOverrideContext()
Definition context.cc:35
static void SetOverrideContext(std::shared_ptr< impeller::Context > context)
Definition context.cc:31
std::shared_ptr< impeller::Context > & GetContextShared()
Definition context.cc:91
impeller::Context & GetContext()
Definition context.cc:87
Context(std::shared_ptr< impeller::Context > context)
Definition context.cc:82
static std::shared_ptr< impeller::Context > GetDefaultContext(std::optional< std::string > &out_error)
Definition context.cc:39
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
To do anything rendering related with Impeller, you need a context.
Definition context.h:69
virtual const std::shared_ptr< const Capabilities > & GetCapabilities() const =0
Get the capabilities of Impeller context. All optionally supported feature of the platform,...
#define IMPLEMENT_WRAPPERTYPEINFO(LibraryName, ClassName)
uint32_t uint32_t * format
bool InternalFlutterGpu_Context_SupportsTextureCompression(flutter::gpu::Context *wrapper, int family)
Definition context.cc:157
bool InternalFlutterGpu_Context_GetSupportsFramebufferRenderMipmap(flutter::gpu::Context *wrapper)
Definition context.cc:143
int InternalFlutterGpu_Context_GetMinimumUniformByteAlignment(flutter::gpu::Context *wrapper)
Definition context.cc:133
int InternalFlutterGpu_Context_GetDefaultDepthStencilFormat(flutter::gpu::Context *wrapper)
Definition context.cc:127
Dart_Handle InternalFlutterGpu_Context_InitializeDefault(Dart_Handle wrapper)
Definition context.cc:102
int InternalFlutterGpu_Context_GetDefaultColorFormat(flutter::gpu::Context *wrapper)
Definition context.cc:115
bool InternalFlutterGpu_Context_GetSupportsManuallyMippedTextures(flutter::gpu::Context *wrapper)
Definition context.cc:150
bool InternalFlutterGpu_Context_GetSupportsOffscreenMSAA(flutter::gpu::Context *wrapper)
Definition context.cc:138
bool InternalFlutterGpu_Context_SupportsTextureFormat(flutter::gpu::Context *wrapper, int format, bool render_target, bool shader_read, bool shader_write)
Definition context.cc:164
int InternalFlutterGpu_Context_GetDefaultStencilFormat(flutter::gpu::Context *wrapper)
Definition context.cc:121
constexpr impeller::PixelFormat ToImpellerPixelFormat(FlutterGPUPixelFormat value)
Definition formats.h:104
bool SupportsNormalOffscreenMSAA(const impeller::Context &context)
Definition context.cc:21
constexpr FlutterGPUPixelFormat FromImpellerPixelFormat(impeller::PixelFormat value)
Definition formats.h:176
constexpr impeller::CompressedTextureFamily ToImpellerCompressedTextureFamily(FlutterGPUTextureCompressionFamily value)
Definition formats.h:84
internal::CopyableLambda< T > MakeCopyable(T lambda)
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition formats.h:99
constexpr CompressedTextureFamily CompressedTextureFamilyForFormat(PixelFormat format)
The compression family that format belongs to. Only valid for formats where IsCompressed is true.
Definition formats.h:185
constexpr bool IsCompressed(PixelFormat format)
Whether format is a block-compressed format.
Definition formats.h:158
Definition ref_ptr.h:261
Dart_Handle ToDart(const T &object)
std::shared_ptr< ContextGLES > context