Flutter Engine
The Flutter Engine
Classes | Public Types | Public Member Functions | List of all members
flutter::Pipeline< R > Class Template Reference

#include <pipeline.h>

Classes

class  ProducerContinuation
 

Public Types

using Resource = R
 
using ResourcePtr = std::unique_ptr< Resource >
 
using Consumer = std::function< void(ResourcePtr)>
 

Public Member Functions

 Pipeline (uint32_t depth)
 
 ~Pipeline ()=default
 
bool IsValid () const
 
ProducerContinuation Produce ()
 
ProducerContinuation ProduceIfEmpty ()
 
PipelineConsumeResult Consume (const Consumer &consumer)
 

Detailed Description

template<class R>
class flutter::Pipeline< R >

A thread-safe queue of resources for a single consumer and a single producer, with a maximum queue depth.

Pipelines support two key operations: produce and consume.

The consumer calls |Consume| to wait for a resource to be produced and consume it when ready.

The producer calls |Produce| to generate a ProducerContinuation which provides a means to enqueue a resource in the pipeline, if the pipeline is below its maximum depth. When the resource has been prepared, the producer calls Complete on the continuation, which enqueues the resource and signals the waiting consumer.

Pipelines generate the following tracing information:

The primary use of this class is as the frame pipeline used in Flutter's animator/rasterizer.

Definition at line 64 of file pipeline.h.

Member Typedef Documentation

◆ Consumer

template<class R >
using flutter::Pipeline< R >::Consumer = std::function<void(ResourcePtr)>

Definition at line 180 of file pipeline.h.

◆ Resource

template<class R >
using flutter::Pipeline< R >::Resource = R

Definition at line 66 of file pipeline.h.

◆ ResourcePtr

template<class R >
using flutter::Pipeline< R >::ResourcePtr = std::unique_ptr<Resource>

Definition at line 67 of file pipeline.h.

Constructor & Destructor Documentation

◆ Pipeline()

template<class R >
flutter::Pipeline< R >::Pipeline ( uint32_t  depth)
inlineexplicit

Definition at line 131 of file pipeline.h.

132 : empty_(depth), available_(0), inflight_(0) {}

◆ ~Pipeline()

template<class R >
flutter::Pipeline< R >::~Pipeline ( )
default

Member Function Documentation

◆ Consume()

template<class R >
PipelineConsumeResult flutter::Pipeline< R >::Consume ( const Consumer consumer)
inline
Note
Procedure doesn't copy all closures.

Definition at line 183 of file pipeline.h.

183 {
184 if (consumer == nullptr) {
186 }
187
188 if (!available_.TryWait()) {
190 }
191
193 size_t trace_id = 0;
194 size_t items_count = 0;
195
196 {
197 std::scoped_lock lock(queue_mutex_);
198 std::tie(resource, trace_id) = std::move(queue_.front());
199 queue_.pop_front();
200 items_count = queue_.size();
201 }
202
203 consumer(std::move(resource));
204
205 empty_.Signal();
206 --inflight_;
207
208 TRACE_FLOW_END("flutter", "PipelineItem", trace_id);
209 TRACE_EVENT_ASYNC_END0("flutter", "PipelineItem", trace_id);
210
211 return items_count > 0 ? PipelineConsumeResult::MoreAvailable
213 }
static SkString resource(SkPDFResourceType type, int index)
std::unique_ptr< Resource > ResourcePtr
Definition: pipeline.h:67
void Signal()
Increment the count by one. Any pending Waits will be resolved at this point.
Definition: semaphore.cc:189
bool TryWait()
Decrement the counts if it is greater than zero. Returns false if the counter is already at zero.
Definition: semaphore.cc:185
#define TRACE_EVENT_ASYNC_END0(category_group, name, id)
Definition: trace_event.h:161
#define TRACE_FLOW_END(category, name, id)
Definition: trace_event.h:196

◆ IsValid()

template<class R >
bool flutter::Pipeline< R >::IsValid ( ) const
inline

Definition at line 136 of file pipeline.h.

136{ return empty_.IsValid() && available_.IsValid(); }
bool IsValid() const
Check if the underlying semaphore handle could be created. Failure modes are platform specific and ma...
Definition: semaphore.cc:177

◆ Produce()

template<class R >
ProducerContinuation flutter::Pipeline< R >::Produce ( )
inline

Creates a ProducerContinuation that a producer can use to add a resource to the queue.

If the queue is already at its maximum depth, the ProducerContinuation is returned with success = false.

Definition at line 143 of file pipeline.h.

143 {
144 if (!empty_.TryWait()) {
145 return {};
146 }
147 ++inflight_;
148 FML_TRACE_COUNTER("flutter", "Pipeline Depth",
149 reinterpret_cast<int64_t>(this), //
150 "frames in flight", inflight_.load() //
151 );
152
153 return ProducerContinuation{
154 std::bind(&Pipeline::ProducerCommit, this, std::placeholders::_1,
155 std::placeholders::_2), // continuation
156 GetNextPipelineTraceID()}; // trace id
157 }
size_t GetNextPipelineTraceID()
Definition: pipeline.cc:9
#define FML_TRACE_COUNTER(category_group, name, counter_id, arg1,...)
Definition: trace_event.h:85

◆ ProduceIfEmpty()

template<class R >
ProducerContinuation flutter::Pipeline< R >::ProduceIfEmpty ( )
inline

Creates a ProducerContinuation that will only push the task if the queue is empty.

Prefer using |Produce|. ProducerContinuation returned by this method doesn't guarantee that the frame will be rendered.

Definition at line 164 of file pipeline.h.

164 {
165 if (!empty_.TryWait()) {
166 return {};
167 }
168 ++inflight_;
169 FML_TRACE_COUNTER("flutter", "Pipeline Depth",
170 reinterpret_cast<int64_t>(this), //
171 "frames in flight", inflight_.load() //
172 );
173
174 return ProducerContinuation{
175 std::bind(&Pipeline::ProducerCommitIfEmpty, this, std::placeholders::_1,
176 std::placeholders::_2), // continuation
177 GetNextPipelineTraceID()}; // trace id
178 }

The documentation for this class was generated from the following file: