Flutter Engine
The Flutter Engine
command_buffer.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/command_buffer.h"
6
7#include "dart_api.h"
8#include "fml/make_copyable.h"
13
14namespace flutter {
15namespace gpu {
16
18
20 std::shared_ptr<impeller::Context> context,
21 std::shared_ptr<impeller::CommandBuffer> command_buffer)
22 : context_(std::move(context)),
23 command_buffer_(std::move(command_buffer)) {}
24
26
27std::shared_ptr<impeller::CommandBuffer> CommandBuffer::GetCommandBuffer() {
28 return command_buffer_;
29}
30
32 std::shared_ptr<impeller::RenderPass> render_pass) {
33 encodables_.push_back(std::move(render_pass));
34}
35
37 for (auto& encodable : encodables_) {
38 encodable->EncodeCommands();
39 }
40
41 return context_->GetCommandQueue()->Submit({command_buffer_}).ok();
42}
43
45 const impeller::CommandBuffer::CompletionCallback& completion_callback) {
46 for (auto& encodable : encodables_) {
47 encodable->EncodeCommands();
48 }
49 return context_->GetCommandQueue()
50 ->Submit({command_buffer_}, completion_callback)
51 .ok();
52}
53
54} // namespace gpu
55} // namespace flutter
56
57//----------------------------------------------------------------------------
58/// Exports
59///
60
62 Dart_Handle wrapper,
63 flutter::gpu::Context* contextWrapper) {
64 auto res = fml::MakeRefCounted<flutter::gpu::CommandBuffer>(
65 contextWrapper->GetContext(),
66 contextWrapper->GetContext()->CreateCommandBuffer());
67 res->AssociateWithDartWrapper(wrapper);
68
69 return true;
70}
71
74 Dart_Handle completion_callback) {
75 if (Dart_IsNull(completion_callback)) {
76 bool success = wrapper->Submit();
77 if (!success) {
78 return tonic::ToDart("Failed to submit CommandBuffer");
79 }
80 return Dart_Null();
81 }
82
83 if (!Dart_IsClosure(completion_callback)) {
84 return tonic::ToDart("Completion callback must be a function");
85 }
86
87 auto dart_state = flutter::UIDartState::Current();
88 auto& task_runners = dart_state->GetTaskRunners();
89
90 auto persistent_completion_callback =
91 std::make_unique<tonic::DartPersistentValue>(dart_state,
92 completion_callback);
93
94 bool success = wrapper->Submit(fml::MakeCopyable(
95 [callback = std::move(persistent_completion_callback),
96 task_runners](impeller::CommandBuffer::Status status) mutable {
97 bool success = status != impeller::CommandBuffer::Status::kError;
98
99 auto ui_completion_task = fml::MakeCopyable(
100 [callback = std::move(callback), success]() mutable {
101 auto dart_state = callback->dart_state().lock();
102 if (!dart_state) {
103 // The root isolate could have died in the meantime.
104 return;
105 }
106 tonic::DartState::Scope scope(dart_state);
107
108 tonic::DartInvoke(callback->Get(), {tonic::ToDart(success)});
109
110 // callback is associated with the Dart isolate and must be
111 // deleted on the UI thread.
112 callback.reset();
113 });
114 task_runners.GetUITaskRunner()->PostTask(ui_completion_task);
115 }));
116 if (!success) {
117 return tonic::ToDart("Failed to submit CommandBuffer");
118 }
119 return Dart_Null();
120}
static bool ok(int result)
static UIDartState * Current()
CommandBuffer(std::shared_ptr< impeller::Context > context, std::shared_ptr< impeller::CommandBuffer > command_buffer)
std::shared_ptr< impeller::CommandBuffer > GetCommandBuffer()
void AddRenderPass(std::shared_ptr< impeller::RenderPass > render_pass)
std::shared_ptr< impeller::Context > GetContext()
Definition: context.cc:65
std::function< void(Status)> CompletionCallback
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT Dart_Handle Dart_Null(void)
DART_EXPORT bool Dart_IsNull(Dart_Handle object)
DART_EXPORT bool Dart_IsClosure(Dart_Handle object)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
Dart_Handle InternalFlutterGpu_CommandBuffer_Submit(flutter::gpu::CommandBuffer *wrapper, Dart_Handle completion_callback)
bool InternalFlutterGpu_CommandBuffer_Initialize(Dart_Handle wrapper, flutter::gpu::Context *contextWrapper)
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)
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)
Definition: dart_invoke.cc:20