Flutter Engine
 
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->IsFlutterGPUEnabled()) {
45 out_error =
46 "Flutter GPU must be enabled via the Flutter GPU manifest "
47 "setting. This can be done either via command line argument "
48 "--enable-flutter-gpu or "
49 "by adding the FLTEnableFlutterGPU key set to true on iOS or "
50 "io.flutter.embedding.android.EnableFlutterGPU metadata key to true on "
51 "Android.";
52 return nullptr;
53 }
54 // Grab the Impeller context from the IO manager.
55 std::promise<std::shared_ptr<impeller::Context>> context_promise;
56 auto impeller_context_future = context_promise.get_future();
58 dart_state->GetTaskRunners().GetIOTaskRunner(),
59 fml::MakeCopyable([promise = std::move(context_promise),
60 io_manager = dart_state->GetIOManager()]() mutable {
61 promise.set_value(io_manager ? io_manager->GetImpellerContext()
62 : nullptr);
63 }));
64 auto context = impeller_context_future.get();
65
66 if (!context) {
67 out_error = "Unable to retrieve the Impeller context.";
68 }
69 return context;
70}
71
72Context::Context(std::shared_ptr<impeller::Context> context)
73 : context_(std::move(context)) {}
74
75Context::~Context() = default;
76
78 return *context_;
79}
80
81std::shared_ptr<impeller::Context>& Context::GetContextShared() {
82 return context_;
83}
84
85} // namespace gpu
86} // namespace flutter
87
88//----------------------------------------------------------------------------
89/// Exports
90///
91
92Dart_Handle InternalFlutterGpu_Context_InitializeDefault(Dart_Handle wrapper) {
93 std::optional<std::string> out_error;
94 auto impeller_context = flutter::gpu::Context::GetDefaultContext(out_error);
95 if (out_error.has_value()) {
96 return tonic::ToDart(out_error.value());
97 }
98
99 auto res = fml::MakeRefCounted<flutter::gpu::Context>(impeller_context);
100 res->AssociateWithDartWrapper(wrapper);
101
102 return Dart_Null();
103}
104
106 flutter::gpu::Context* wrapper) {
107 return static_cast<int>(flutter::gpu::FromImpellerPixelFormat(
108 wrapper->GetContext().GetCapabilities()->GetDefaultColorFormat()));
109}
110
112 flutter::gpu::Context* wrapper) {
113 return static_cast<int>(flutter::gpu::FromImpellerPixelFormat(
114 wrapper->GetContext().GetCapabilities()->GetDefaultStencilFormat()));
115}
116
118 flutter::gpu::Context* wrapper) {
119 return static_cast<int>(flutter::gpu::FromImpellerPixelFormat(
120 wrapper->GetContext().GetCapabilities()->GetDefaultDepthStencilFormat()));
121}
122
124 flutter::gpu::Context* wrapper) {
125 return wrapper->GetContext().GetCapabilities()->GetMinimumUniformAlignment();
126}
127
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:81
impeller::Context & GetContext()
Definition context.cc:77
Context(std::shared_ptr< impeller::Context > context)
Definition context.cc:72
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:65
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:123
int InternalFlutterGpu_Context_GetDefaultDepthStencilFormat(flutter::gpu::Context *wrapper)
Definition context.cc:117
Dart_Handle InternalFlutterGpu_Context_InitializeDefault(Dart_Handle wrapper)
Definition context.cc:92
int InternalFlutterGpu_Context_GetDefaultColorFormat(flutter::gpu::Context *wrapper)
Definition context.cc:105
bool InternalFlutterGpu_Context_GetSupportsOffscreenMSAA(flutter::gpu::Context *wrapper)
Definition context.cc:128
int InternalFlutterGpu_Context_GetDefaultStencilFormat(flutter::gpu::Context *wrapper)
Definition context.cc:111
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)