Skip to content

Commit 30f28f1

Browse files
chinmaygardednfield
authored andcommitted
All creation of pipeline variants from a pipeline.
1 parent 603b915 commit 30f28f1

File tree

9 files changed

+87
-19
lines changed

9 files changed

+87
-19
lines changed

impeller/base/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ impeller_component("base") {
1010
"allocation.h",
1111
"base.h",
1212
"config.h",
13+
"promise.cc",
14+
"promise.h",
1315
"strings.cc",
1416
"strings.h",
1517
"validation.cc",

impeller/base/base.h

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
#pragma once
66

7+
#include "impeller/base/promise.h"
78
#include "impeller/base/strings.h"
89
#include "impeller/base/validation.h"

impeller/base/promise.cc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 "impeller/base/promise.h"
6+
7+
namespace impeller {
8+
9+
//
10+
11+
} // namespace impeller

impeller/base/promise.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
#pragma once
6+
7+
#include <future>
8+
9+
namespace impeller {
10+
11+
template <class T>
12+
std::future<T> RealizedFuture(T t) {
13+
std::promise<T> promise;
14+
auto future = promise.get_future();
15+
promise.set_value(std::move(t));
16+
return future;
17+
}
18+
19+
} // namespace impeller

impeller/renderer/backend/metal/pipeline_library_mtl.mm

+19-10
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,33 @@
8181
// created till the first instance of the creation invokes its completion
8282
// callback.
8383

84-
auto thiz = shared_from_this();
84+
auto weak_this = weak_from_this();
8585

8686
auto completion_handler =
8787
^(id<MTLRenderPipelineState> _Nullable render_pipeline_state,
8888
NSError* _Nullable error) {
8989
if (error != nil) {
90-
FML_LOG(ERROR) << "Could not create render pipeline: "
90+
VALIDATION_LOG << "Could not create render pipeline: "
9191
<< error.localizedDescription.UTF8String;
9292
promise->set_value(nullptr);
93-
} else {
94-
auto new_pipeline = std::shared_ptr<PipelineMTL>(new PipelineMTL(
95-
descriptor, //
96-
render_pipeline_state, //
97-
CreateDepthStencilDescriptor(descriptor, device_) //
98-
));
99-
promise->set_value(new_pipeline);
100-
this->SavePipeline(descriptor, new_pipeline);
93+
return;
10194
}
95+
96+
auto strong_this = weak_this.lock();
97+
if (!strong_this) {
98+
VALIDATION_LOG << "Library was collected before a pending pipeline "
99+
"creation could finish.";
100+
promise->set_value(nullptr);
101+
return;
102+
}
103+
auto new_pipeline = std::shared_ptr<PipelineMTL>(new PipelineMTL(
104+
weak_this,
105+
descriptor, //
106+
render_pipeline_state, //
107+
CreateDepthStencilDescriptor(descriptor, device_) //
108+
));
109+
promise->set_value(new_pipeline);
110+
this->SavePipeline(descriptor, new_pipeline);
102111
};
103112
[device_ newRenderPipelineStateWithDescriptor:GetMTLRenderPipelineDescriptor(
104113
descriptor)

impeller/renderer/backend/metal/pipeline_mtl.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class PipelineMTL final : public Pipeline,
3030
id<MTLDepthStencilState> depth_stencil_state_;
3131
bool is_valid_ = false;
3232

33-
PipelineMTL(PipelineDescriptor desc,
33+
PipelineMTL(std::weak_ptr<PipelineLibrary> library,
34+
PipelineDescriptor desc,
3435
id<MTLRenderPipelineState> state,
3536
id<MTLDepthStencilState> depth_stencil_state);
3637

impeller/renderer/backend/metal/pipeline_mtl.mm

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
namespace impeller {
88

9-
PipelineMTL::PipelineMTL(PipelineDescriptor desc,
9+
PipelineMTL::PipelineMTL(std::weak_ptr<PipelineLibrary> library,
10+
PipelineDescriptor desc,
1011
id<MTLRenderPipelineState> state,
1112
id<MTLDepthStencilState> depth_stencil_state)
12-
: Pipeline(std::move(desc)),
13+
: Pipeline(std::move(library), std::move(desc)),
1314
pipeline_state_(state),
1415
depth_stencil_state_(depth_stencil_state) {
1516
if (!pipeline_state_) {

impeller/renderer/pipeline.cc

+25-5
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44

55
#include "impeller/renderer/pipeline.h"
66

7+
#include "impeller/base/base.h"
78
#include "impeller/renderer/context.h"
89
#include "impeller/renderer/pipeline_library.h"
910

1011
namespace impeller {
1112

12-
Pipeline::Pipeline(PipelineDescriptor desc) : desc_(std::move(desc)) {}
13+
Pipeline::Pipeline(std::weak_ptr<PipelineLibrary> library,
14+
PipelineDescriptor desc)
15+
: library_(std::move(library)), desc_(std::move(desc)) {}
1316

1417
Pipeline::~Pipeline() = default;
1518

1619
PipelineFuture CreatePipelineFuture(const Context& context,
1720
std::optional<PipelineDescriptor> desc) {
1821
if (!context.IsValid()) {
19-
std::promise<std::shared_ptr<Pipeline>> promise;
20-
auto future = promise.get_future();
21-
promise.set_value(nullptr);
22-
return future;
22+
return RealizedFuture<std::shared_ptr<Pipeline>>(nullptr);
2323
}
2424

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

32+
PipelineFuture Pipeline::CreateVariant(
33+
std::function<void(PipelineDescriptor& desc)> descriptor_callback) const {
34+
if (!descriptor_callback) {
35+
return RealizedFuture<std::shared_ptr<Pipeline>>(nullptr);
36+
}
37+
38+
auto copied_desc = desc_;
39+
40+
descriptor_callback(copied_desc);
41+
42+
auto library = library_.lock();
43+
if (!library) {
44+
VALIDATION_LOG << "The library from which this pipeline was created was "
45+
"already collected.";
46+
return RealizedFuture<std::shared_ptr<Pipeline>>(nullptr);
47+
}
48+
49+
return library->GetRenderPipeline(std::move(copied_desc));
50+
}
51+
3252
} // namespace impeller

impeller/renderer/pipeline.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ class Pipeline {
4444

4545
const PipelineDescriptor& GetDescriptor() const;
4646

47+
PipelineFuture CreateVariant(
48+
std::function<void(PipelineDescriptor& desc)> descriptor_callback) const;
49+
4750
protected:
48-
Pipeline(PipelineDescriptor desc);
51+
Pipeline(std::weak_ptr<PipelineLibrary> library, PipelineDescriptor desc);
4952

5053
private:
54+
const std::weak_ptr<PipelineLibrary> library_;
5155
const PipelineDescriptor desc_;
5256

5357
FML_DISALLOW_COPY_AND_ASSIGN(Pipeline);

0 commit comments

Comments
 (0)