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"
14
15namespace flutter {
16namespace gpu {
17
19 auto& capabilities = context.GetCapabilities();
20 return capabilities->SupportsOffscreenMSAA() &&
21 !capabilities->SupportsImplicitResolvingMSAA();
22}
23
25
26std::shared_ptr<impeller::Context> Context::default_context_;
27
28void Context::SetOverrideContext(std::shared_ptr<impeller::Context> context) {
29 default_context_ = std::move(context);
30}
31
32std::shared_ptr<impeller::Context> Context::GetOverrideContext() {
33 return default_context_;
34}
35
36std::shared_ptr<impeller::Context> Context::GetDefaultContext(
37 std::optional<std::string>& out_error) {
38 auto override_context = GetOverrideContext();
39 if (override_context) {
40 return override_context;
41 }
42
43 auto dart_state = flutter::UIDartState::Current();
44 if (!dart_state->IsImpellerEnabled()) {
45 out_error =
46 "Flutter GPU requires the Impeller rendering backend, but Impeller is "
47 "not enabled. For more details about where Impeller is available and "
48 "how to enable it, see: https://docs.flutter.dev/perf/impeller";
49 return nullptr;
50 }
51 if (!dart_state->IsFlutterGPUEnabled()) {
52 out_error =
53 "Flutter GPU must be enabled via the Flutter GPU manifest "
54 "setting. This can be done either via command line argument "
55 "--enable-flutter-gpu or "
56 "by adding the FLTEnableFlutterGPU key set to true in the Info.plist "
57 "on iOS/macOS, or the io.flutter.embedding.android.EnableFlutterGPU "
58 "metadata key to true in the AndroidManifest.xml on Android.";
59 return nullptr;
60 }
61 // Grab the Impeller context from the IO manager.
62 std::promise<std::shared_ptr<impeller::Context>> context_promise;
63 auto impeller_context_future = context_promise.get_future();
65 dart_state->GetTaskRunners().GetIOTaskRunner(),
66 fml::MakeCopyable([promise = std::move(context_promise),
67 io_manager = dart_state->GetIOManager()]() mutable {
68 promise.set_value(io_manager ? io_manager->GetImpellerContext()
69 : nullptr);
70 }));
71 auto context = impeller_context_future.get();
72
73 if (!context) {
74 out_error = "Unable to retrieve the Impeller context.";
75 }
76 return context;
77}
78
79Context::Context(std::shared_ptr<impeller::Context> context)
80 : context_(std::move(context)) {}
81
82Context::~Context() = default;
83
85 return *context_;
86}
87
88std::shared_ptr<impeller::Context>& Context::GetContextShared() {
89 return context_;
90}
91
92} // namespace gpu
93} // namespace flutter
94
95//----------------------------------------------------------------------------
96/// Exports
97///
98
99Dart_Handle InternalFlutterGpu_Context_InitializeDefault(Dart_Handle wrapper) {
100 std::optional<std::string> out_error;
101 auto impeller_context = flutter::gpu::Context::GetDefaultContext(out_error);
102 if (out_error.has_value()) {
103 return tonic::ToDart(out_error.value());
104 }
105
106 auto res = fml::MakeRefCounted<flutter::gpu::Context>(impeller_context);
107 res->AssociateWithDartWrapper(wrapper);
108
109 return Dart_Null();
110}
111
113 flutter::gpu::Context* wrapper) {
114 return static_cast<int>(flutter::gpu::FromImpellerPixelFormat(
115 wrapper->GetContext().GetCapabilities()->GetDefaultColorFormat()));
116}
117
119 flutter::gpu::Context* wrapper) {
120 return static_cast<int>(flutter::gpu::FromImpellerPixelFormat(
121 wrapper->GetContext().GetCapabilities()->GetDefaultStencilFormat()));
122}
123
125 flutter::gpu::Context* wrapper) {
126 return static_cast<int>(flutter::gpu::FromImpellerPixelFormat(
127 wrapper->GetContext().GetCapabilities()->GetDefaultDepthStencilFormat()));
128}
129
131 flutter::gpu::Context* wrapper) {
132 return wrapper->GetContext().GetCapabilities()->GetMinimumUniformAlignment();
133}
134
static UIDartState * Current()
static std::shared_ptr< impeller::Context > GetOverrideContext()
Definition context.cc:32
static void SetOverrideContext(std::shared_ptr< impeller::Context > context)
Definition context.cc:28
std::shared_ptr< impeller::Context > & GetContextShared()
Definition context.cc:88
impeller::Context & GetContext()
Definition context.cc:84
Context(std::shared_ptr< impeller::Context > context)
Definition context.cc:79
static std::shared_ptr< impeller::Context > GetDefaultContext(std::optional< std::string > &out_error)
Definition context.cc:36
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)
int InternalFlutterGpu_Context_GetMinimumUniformByteAlignment(flutter::gpu::Context *wrapper)
Definition context.cc:130
int InternalFlutterGpu_Context_GetDefaultDepthStencilFormat(flutter::gpu::Context *wrapper)
Definition context.cc:124
Dart_Handle InternalFlutterGpu_Context_InitializeDefault(Dart_Handle wrapper)
Definition context.cc:99
int InternalFlutterGpu_Context_GetDefaultColorFormat(flutter::gpu::Context *wrapper)
Definition context.cc:112
bool InternalFlutterGpu_Context_GetSupportsOffscreenMSAA(flutter::gpu::Context *wrapper)
Definition context.cc:135
int InternalFlutterGpu_Context_GetDefaultStencilFormat(flutter::gpu::Context *wrapper)
Definition context.cc:118
bool SupportsNormalOffscreenMSAA(const impeller::Context &context)
Definition context.cc:18
constexpr FlutterGPUPixelFormat FromImpellerPixelFormat(impeller::PixelFormat value)
Definition formats.h:102
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition ref_ptr.h:261
Dart_Handle ToDart(const T &object)