Skip to content

Commit

Permalink
All creation of pipeline variants from a pipeline.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent 603b915 commit 30f28f1
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 19 deletions.
2 changes: 2 additions & 0 deletions impeller/base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ impeller_component("base") {
"allocation.h",
"base.h",
"config.h",
"promise.cc",
"promise.h",
"strings.cc",
"strings.h",
"validation.cc",
Expand Down
1 change: 1 addition & 0 deletions impeller/base/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

#pragma once

#include "impeller/base/promise.h"
#include "impeller/base/strings.h"
#include "impeller/base/validation.h"
11 changes: 11 additions & 0 deletions impeller/base/promise.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "impeller/base/promise.h"

namespace impeller {

//

} // namespace impeller
19 changes: 19 additions & 0 deletions impeller/base/promise.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#pragma once

#include <future>

namespace impeller {

template <class T>
std::future<T> RealizedFuture(T t) {
std::promise<T> promise;
auto future = promise.get_future();
promise.set_value(std::move(t));
return future;
}

} // namespace impeller
29 changes: 19 additions & 10 deletions impeller/renderer/backend/metal/pipeline_library_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,33 @@
// created till the first instance of the creation invokes its completion
// callback.

auto thiz = shared_from_this();
auto weak_this = weak_from_this();

auto completion_handler =
^(id<MTLRenderPipelineState> _Nullable render_pipeline_state,
NSError* _Nullable error) {
if (error != nil) {
FML_LOG(ERROR) << "Could not create render pipeline: "
VALIDATION_LOG << "Could not create render pipeline: "
<< error.localizedDescription.UTF8String;
promise->set_value(nullptr);
} else {
auto new_pipeline = std::shared_ptr<PipelineMTL>(new PipelineMTL(
descriptor, //
render_pipeline_state, //
CreateDepthStencilDescriptor(descriptor, device_) //
));
promise->set_value(new_pipeline);
this->SavePipeline(descriptor, new_pipeline);
return;
}

auto strong_this = weak_this.lock();
if (!strong_this) {
VALIDATION_LOG << "Library was collected before a pending pipeline "
"creation could finish.";
promise->set_value(nullptr);
return;
}
auto new_pipeline = std::shared_ptr<PipelineMTL>(new PipelineMTL(
weak_this,
descriptor, //
render_pipeline_state, //
CreateDepthStencilDescriptor(descriptor, device_) //
));
promise->set_value(new_pipeline);
this->SavePipeline(descriptor, new_pipeline);
};
[device_ newRenderPipelineStateWithDescriptor:GetMTLRenderPipelineDescriptor(
descriptor)
Expand Down
3 changes: 2 additions & 1 deletion impeller/renderer/backend/metal/pipeline_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class PipelineMTL final : public Pipeline,
id<MTLDepthStencilState> depth_stencil_state_;
bool is_valid_ = false;

PipelineMTL(PipelineDescriptor desc,
PipelineMTL(std::weak_ptr<PipelineLibrary> library,
PipelineDescriptor desc,
id<MTLRenderPipelineState> state,
id<MTLDepthStencilState> depth_stencil_state);

Expand Down
5 changes: 3 additions & 2 deletions impeller/renderer/backend/metal/pipeline_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

namespace impeller {

PipelineMTL::PipelineMTL(PipelineDescriptor desc,
PipelineMTL::PipelineMTL(std::weak_ptr<PipelineLibrary> library,
PipelineDescriptor desc,
id<MTLRenderPipelineState> state,
id<MTLDepthStencilState> depth_stencil_state)
: Pipeline(std::move(desc)),
: Pipeline(std::move(library), std::move(desc)),
pipeline_state_(state),
depth_stencil_state_(depth_stencil_state) {
if (!pipeline_state_) {
Expand Down
30 changes: 25 additions & 5 deletions impeller/renderer/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

#include "impeller/renderer/pipeline.h"

#include "impeller/base/base.h"
#include "impeller/renderer/context.h"
#include "impeller/renderer/pipeline_library.h"

namespace impeller {

Pipeline::Pipeline(PipelineDescriptor desc) : desc_(std::move(desc)) {}
Pipeline::Pipeline(std::weak_ptr<PipelineLibrary> library,
PipelineDescriptor desc)
: library_(std::move(library)), desc_(std::move(desc)) {}

Pipeline::~Pipeline() = default;

PipelineFuture CreatePipelineFuture(const Context& context,
std::optional<PipelineDescriptor> desc) {
if (!context.IsValid()) {
std::promise<std::shared_ptr<Pipeline>> promise;
auto future = promise.get_future();
promise.set_value(nullptr);
return future;
return RealizedFuture<std::shared_ptr<Pipeline>>(nullptr);
}

return context.GetPipelineLibrary()->GetRenderPipeline(std::move(desc));
Expand All @@ -29,4 +29,24 @@ const PipelineDescriptor& Pipeline::GetDescriptor() const {
return desc_;
}

PipelineFuture Pipeline::CreateVariant(
std::function<void(PipelineDescriptor& desc)> descriptor_callback) const {
if (!descriptor_callback) {
return RealizedFuture<std::shared_ptr<Pipeline>>(nullptr);
}

auto copied_desc = desc_;

descriptor_callback(copied_desc);

auto library = library_.lock();
if (!library) {
VALIDATION_LOG << "The library from which this pipeline was created was "
"already collected.";
return RealizedFuture<std::shared_ptr<Pipeline>>(nullptr);
}

return library->GetRenderPipeline(std::move(copied_desc));
}

} // namespace impeller
6 changes: 5 additions & 1 deletion impeller/renderer/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ class Pipeline {

const PipelineDescriptor& GetDescriptor() const;

PipelineFuture CreateVariant(
std::function<void(PipelineDescriptor& desc)> descriptor_callback) const;

protected:
Pipeline(PipelineDescriptor desc);
Pipeline(std::weak_ptr<PipelineLibrary> library, PipelineDescriptor desc);

private:
const std::weak_ptr<PipelineLibrary> library_;
const PipelineDescriptor desc_;

FML_DISALLOW_COPY_AND_ASSIGN(Pipeline);
Expand Down

0 comments on commit 30f28f1

Please sign in to comment.