Flutter Engine
The Flutter Engine
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
5#include "flutter/lib/gpu/context.h"
6
7#include <future>
8
9#include "flutter/lib/gpu/formats.h"
10#include "flutter/lib/ui/ui_dart_state.h"
11#include "fml/make_copyable.h"
13
14namespace flutter {
15namespace gpu {
16
18
19std::shared_ptr<impeller::Context> Context::default_context_;
20
21void Context::SetOverrideContext(std::shared_ptr<impeller::Context> context) {
22 default_context_ = std::move(context);
23}
24
25std::shared_ptr<impeller::Context> Context::GetOverrideContext() {
26 return default_context_;
27}
28
29std::shared_ptr<impeller::Context> Context::GetDefaultContext(
30 std::optional<std::string>& out_error) {
31 auto override_context = GetOverrideContext();
32 if (override_context) {
33 return override_context;
34 }
35
36 auto dart_state = flutter::UIDartState::Current();
37 if (!dart_state->IsImpellerEnabled()) {
38 out_error =
39 "Flutter GPU requires the Impeller rendering backend to be enabled.";
40 return nullptr;
41 }
42 // Grab the Impeller context from the IO manager.
43 std::promise<std::shared_ptr<impeller::Context>> context_promise;
44 auto impeller_context_future = context_promise.get_future();
46 dart_state->GetTaskRunners().GetIOTaskRunner(),
47 fml::MakeCopyable([promise = std::move(context_promise),
48 io_manager = dart_state->GetIOManager()]() mutable {
49 promise.set_value(io_manager ? io_manager->GetImpellerContext()
50 : nullptr);
51 }));
52 auto context = impeller_context_future.get();
53
54 if (!context) {
55 out_error = "Unable to retrieve the Impeller context.";
56 }
57 return context;
58}
59
60Context::Context(std::shared_ptr<impeller::Context> context)
61 : context_(std::move(context)) {}
62
63Context::~Context() = default;
64
65std::shared_ptr<impeller::Context> Context::GetContext() {
66 return context_;
67}
68
69} // namespace gpu
70} // namespace flutter
71
72//----------------------------------------------------------------------------
73/// Exports
74///
75
77 std::optional<std::string> out_error;
78 auto impeller_context = flutter::gpu::Context::GetDefaultContext(out_error);
79 if (out_error.has_value()) {
80 return tonic::ToDart(out_error.value());
81 }
82
83 auto res = fml::MakeRefCounted<flutter::gpu::Context>(impeller_context);
84 res->AssociateWithDartWrapper(wrapper);
85
86 return Dart_Null();
87}
88
90 flutter::gpu::Context* wrapper) {
91 return static_cast<int>(flutter::gpu::FromImpellerPixelFormat(
92 wrapper->GetContext()->GetCapabilities()->GetDefaultColorFormat()));
93}
94
96 flutter::gpu::Context* wrapper) {
97 return static_cast<int>(flutter::gpu::FromImpellerPixelFormat(
98 wrapper->GetContext()->GetCapabilities()->GetDefaultStencilFormat()));
99}
100
102 flutter::gpu::Context* wrapper) {
103 return static_cast<int>(flutter::gpu::FromImpellerPixelFormat(
104 wrapper->GetContext()
105 ->GetCapabilities()
106 ->GetDefaultDepthStencilFormat()));
107}
static UIDartState * Current()
static std::shared_ptr< impeller::Context > GetOverrideContext()
Definition: context.cc:25
Context(std::shared_ptr< impeller::Context > context)
Definition: context.cc:60
std::shared_ptr< impeller::Context > GetContext()
Definition: context.cc:65
static std::shared_ptr< impeller::Context > GetDefaultContext(std::optional< std::string > &out_error)
Definition: context.cc:29
static void SetOverrideContext(std::shared_ptr< impeller::Context > context)
Definition: context.cc:21
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
Definition: task_runner.cc:55
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT Dart_Handle Dart_Null(void)
int InternalFlutterGpu_Context_GetDefaultDepthStencilFormat(flutter::gpu::Context *wrapper)
Definition: context.cc:101
Dart_Handle InternalFlutterGpu_Context_InitializeDefault(Dart_Handle wrapper)
Definition: context.cc:76
int InternalFlutterGpu_Context_GetDefaultColorFormat(flutter::gpu::Context *wrapper)
Definition: context.cc:89
int InternalFlutterGpu_Context_GetDefaultStencilFormat(flutter::gpu::Context *wrapper)
Definition: context.cc:95
constexpr FlutterGPUPixelFormat FromImpellerPixelFormat(impeller::PixelFormat value)
Definition: formats.h:102
IMPLEMENT_WRAPPERTYPEINFO(flutter_gpu, CommandBuffer)
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57
Definition: ref_ptr.h:256
Dart_Handle ToDart(const T &object)