Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit e4873bb

Browse files
authored
[Impeller] Remove all double empties (#43345)
Unwrap optionals that already have an empty state that must be checked, like `std::optional<std::shared_ptr<T>>` and `std::optional<std::function<T>>`.
1 parent d333434 commit e4873bb

11 files changed

Lines changed: 59 additions & 66 deletions

File tree

impeller/aiks/aiks_unittests.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,7 @@ TEST_P(AiksTest, PaintWithFilters) {
19441944

19451945
ASSERT_TRUE(paint.HasColorFilter());
19461946

1947-
paint.color_filter = std::nullopt;
1947+
paint.color_filter = nullptr;
19481948

19491949
ASSERT_FALSE(paint.HasColorFilter());
19501950
}
@@ -1963,7 +1963,7 @@ TEST_P(AiksTest, OpacityPeepHoleApplicationTest) {
19631963
auto delegate = std::make_shared<OpacityPeepholePassDelegate>(paint, rect);
19641964
ASSERT_FALSE(delegate->CanCollapseIntoParentPass(entity_pass.get()));
19651965

1966-
paint.color_filter = std::nullopt;
1966+
paint.color_filter = nullptr;
19671967
paint.image_filter = [](const FilterInput::Ref& input,
19681968
const Matrix& effect_transform, bool is_subpass) {
19691969
return FilterContents::MakeGaussianBlur(
@@ -1975,7 +1975,7 @@ TEST_P(AiksTest, OpacityPeepHoleApplicationTest) {
19751975
delegate = std::make_shared<OpacityPeepholePassDelegate>(paint, rect);
19761976
ASSERT_FALSE(delegate->CanCollapseIntoParentPass(entity_pass.get()));
19771977

1978-
paint.image_filter = std::nullopt;
1978+
paint.image_filter = nullptr;
19791979
paint.color = Color::Red();
19801980

19811981
// Paint has no alpha, can't elide;

impeller/aiks/paint.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ std::shared_ptr<Contents> Paint::WithImageFilter(
7474
std::shared_ptr<Contents> input,
7575
const Matrix& effect_transform,
7676
bool is_subpass) const {
77-
if (image_filter.has_value()) {
78-
const ImageFilterProc& filter = image_filter.value();
79-
input = filter(FilterInput::Make(input), effect_transform, is_subpass);
77+
if (image_filter) {
78+
input =
79+
image_filter(FilterInput::Make(input), effect_transform, is_subpass);
8080
}
8181
return input;
8282
}
@@ -89,9 +89,8 @@ std::shared_ptr<Contents> Paint::WithColorFilter(
8989
if (color_source.GetType() == ColorSource::Type::kImage) {
9090
return input;
9191
}
92-
if (color_filter.has_value()) {
93-
const ColorFilterProc& filter = color_filter.value();
94-
auto color_filter_contents = filter(FilterInput::Make(input));
92+
if (color_filter) {
93+
auto color_filter_contents = color_filter(FilterInput::Make(input));
9594
if (color_filter_contents) {
9695
color_filter_contents->SetAbsorbOpacity(absorb_opacity);
9796
}
@@ -166,7 +165,7 @@ std::shared_ptr<FilterContents> Paint::MaskBlurDescriptor::CreateMaskBlur(
166165
}
167166

168167
bool Paint::HasColorFilter() const {
169-
return color_filter.has_value();
168+
return !!color_filter;
170169
}
171170

172171
} // namespace impeller

impeller/aiks/paint.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ struct Paint {
6161
BlendMode blend_mode = BlendMode::kSourceOver;
6262
bool invert_colors = false;
6363

64-
std::optional<ImageFilterProc> image_filter;
65-
std::optional<ColorFilterProc> color_filter;
64+
ImageFilterProc image_filter = nullptr;
65+
ColorFilterProc color_filter = nullptr;
6666
std::optional<MaskBlurDescriptor> mask_blur_descriptor;
6767

6868
/// @brief Wrap this paint's configured filters to the given contents.

impeller/aiks/paint_pass_delegate.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ bool OpacityPeepholePassDelegate::CanCollapseIntoParentPass(
8080
// OpacityPeepholePassDelegate will only get used if the pass's blend mode is
8181
// SourceOver, so no need to check here.
8282
if (paint_.color.alpha <= 0.0 || paint_.color.alpha >= 1.0 ||
83-
paint_.image_filter.has_value() || paint_.color_filter.has_value()) {
83+
paint_.image_filter || paint_.color_filter) {
8484
return false;
8585
}
8686

impeller/display_list/dl_dispatcher.cc

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,10 @@ void DlDispatcher::setColorSource(const flutter::DlColorSource* source) {
474474
}
475475
}
476476

477-
static std::optional<Paint::ColorFilterProc> ToColorFilterProc(
477+
static Paint::ColorFilterProc ToColorFilterProc(
478478
const flutter::DlColorFilter* filter) {
479479
if (filter == nullptr) {
480-
return std::nullopt;
480+
return nullptr;
481481
}
482482
switch (filter->type()) {
483483
case flutter::DlColorFilterType::kBlend: {
@@ -507,7 +507,7 @@ static std::optional<Paint::ColorFilterProc> ToColorFilterProc(
507507
return ColorFilterContents::MakeLinearToSrgbFilter({std::move(input)});
508508
};
509509
}
510-
return std::nullopt;
510+
return nullptr;
511511
}
512512

513513
// |flutter::DlOpReceiver|
@@ -565,10 +565,10 @@ void DlDispatcher::setMaskFilter(const flutter::DlMaskFilter* filter) {
565565
}
566566
}
567567

568-
static std::optional<Paint::ImageFilterProc> ToImageFilterProc(
568+
static Paint::ImageFilterProc ToImageFilterProc(
569569
const flutter::DlImageFilter* filter) {
570570
if (filter == nullptr) {
571-
return std::nullopt;
571+
return nullptr;
572572
}
573573

574574
switch (filter->type()) {
@@ -592,7 +592,7 @@ static std::optional<Paint::ImageFilterProc> ToImageFilterProc(
592592
auto dilate = filter->asDilate();
593593
FML_DCHECK(dilate);
594594
if (dilate->radius_x() < 0 || dilate->radius_y() < 0) {
595-
return std::nullopt;
595+
return nullptr;
596596
}
597597
auto radius_x = Radius(dilate->radius_x());
598598
auto radius_y = Radius(dilate->radius_y());
@@ -609,7 +609,7 @@ static std::optional<Paint::ImageFilterProc> ToImageFilterProc(
609609
auto erode = filter->asErode();
610610
FML_DCHECK(erode);
611611
if (erode->radius_x() < 0 || erode->radius_y() < 0) {
612-
return std::nullopt;
612+
return nullptr;
613613
}
614614
auto radius_x = Radius(erode->radius_x());
615615
auto radius_y = Radius(erode->radius_y());
@@ -641,17 +641,16 @@ static std::optional<Paint::ImageFilterProc> ToImageFilterProc(
641641
auto inner = compose->inner();
642642
auto outer_proc = ToImageFilterProc(outer.get());
643643
auto inner_proc = ToImageFilterProc(inner.get());
644-
if (!outer_proc.has_value()) {
644+
if (!outer_proc) {
645645
return inner_proc;
646646
}
647-
if (!inner_proc.has_value()) {
647+
if (!inner_proc) {
648648
return outer_proc;
649649
}
650-
FML_DCHECK(outer_proc.has_value() && inner_proc.has_value());
651-
return [outer_filter = outer_proc.value(),
652-
inner_filter = inner_proc.value()](FilterInput::Ref input,
653-
const Matrix& effect_transform,
654-
bool is_subpass) {
650+
FML_DCHECK(outer_proc && inner_proc);
651+
return [outer_filter = outer_proc, inner_filter = inner_proc](
652+
FilterInput::Ref input, const Matrix& effect_transform,
653+
bool is_subpass) {
655654
auto contents =
656655
inner_filter(std::move(input), effect_transform, is_subpass);
657656
contents = outer_filter(FilterInput::Make(contents), effect_transform,
@@ -665,10 +664,10 @@ static std::optional<Paint::ImageFilterProc> ToImageFilterProc(
665664
FML_DCHECK(color_filter_image_filter);
666665
auto color_filter_proc =
667666
ToColorFilterProc(color_filter_image_filter->color_filter().get());
668-
if (!color_filter_proc.has_value()) {
669-
return std::nullopt;
667+
if (!color_filter_proc) {
668+
return nullptr;
670669
}
671-
return [color_filter = color_filter_proc.value()](
670+
return [color_filter = color_filter_proc](
672671
FilterInput::Ref input, const Matrix& effect_transform,
673672
bool is_subpass) { return color_filter(std::move(input)); };
674673
break;
@@ -680,13 +679,13 @@ static std::optional<Paint::ImageFilterProc> ToImageFilterProc(
680679
FML_DCHECK(internal_filter);
681680

682681
auto image_filter_proc = ToImageFilterProc(internal_filter.get());
683-
if (!image_filter_proc.has_value()) {
684-
return std::nullopt;
682+
if (!image_filter_proc) {
683+
return nullptr;
685684
}
686685

687686
auto matrix = ToMatrix(local_matrix_filter->matrix());
688687

689-
return [matrix, filter_proc = image_filter_proc.value()](
688+
return [matrix, filter_proc = image_filter_proc](
690689
FilterInput::Ref input, const Matrix& effect_transform,
691690
bool is_subpass) {
692691
std::shared_ptr<FilterContents> filter =

impeller/entity/contents/atlas_contents.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,10 @@ bool AtlasColorContents::Render(const ContentContext& renderer,
389389
std::vector<Rect> texture_coords;
390390
std::vector<Matrix> transforms;
391391
std::vector<Color> colors;
392-
if (subatlas_.has_value()) {
393-
auto subatlas = subatlas_.value();
394-
texture_coords = subatlas->sub_texture_coords;
395-
colors = subatlas->sub_colors;
396-
transforms = subatlas->sub_transforms;
392+
if (subatlas_) {
393+
texture_coords = subatlas_->sub_texture_coords;
394+
colors = subatlas_->sub_colors;
395+
transforms = subatlas_->sub_transforms;
397396
} else {
398397
texture_coords = parent_.GetTextureCoordinates();
399398
transforms = parent_.GetTransforms();

impeller/entity/contents/atlas_contents.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class AtlasColorContents final : public Contents {
149149
const AtlasContents& parent_;
150150
Scalar alpha_ = 1.0;
151151
Rect coverage_;
152-
std::optional<std::shared_ptr<SubAtlasResult>> subatlas_ = std::nullopt;
152+
std::shared_ptr<SubAtlasResult> subatlas_;
153153

154154
FML_DISALLOW_COPY_AND_ASSIGN(AtlasColorContents);
155155
};

impeller/entity/contents/tiled_texture_contents.cc

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,16 @@ void TiledTextureContents::SetSamplerDescriptor(SamplerDescriptor desc) {
5555
sampler_descriptor_ = std::move(desc);
5656
}
5757

58-
void TiledTextureContents::SetColorFilter(
59-
std::optional<ColorFilterProc> color_filter) {
58+
void TiledTextureContents::SetColorFilter(ColorFilterProc color_filter) {
6059
color_filter_ = std::move(color_filter);
6160
}
6261

63-
std::optional<std::shared_ptr<Texture>>
64-
TiledTextureContents::CreateFilterTexture(
62+
std::shared_ptr<Texture> TiledTextureContents::CreateFilterTexture(
6563
const ContentContext& renderer) const {
66-
if (!color_filter_.has_value()) {
67-
return std::nullopt;
64+
if (!color_filter_) {
65+
return nullptr;
6866
}
69-
const ColorFilterProc& filter = color_filter_.value();
70-
auto color_filter_contents = filter(FilterInput::Make(texture_));
67+
auto color_filter_contents = color_filter_(FilterInput::Make(texture_));
7168
auto snapshot = color_filter_contents->RenderToSnapshot(
7269
renderer, // renderer
7370
Entity(), // entity
@@ -78,7 +75,7 @@ TiledTextureContents::CreateFilterTexture(
7875
if (snapshot.has_value()) {
7976
return snapshot.value().texture;
8077
}
81-
return std::nullopt;
78+
return nullptr;
8279
}
8380

8481
SamplerDescriptor TiledTextureContents::CreateDescriptor(
@@ -107,7 +104,7 @@ bool TiledTextureContents::IsOpaque() const {
107104
y_tile_mode_ == Entity::TileMode::kDecal) {
108105
return false;
109106
}
110-
if (color_filter_.has_value()) {
107+
if (color_filter_) {
111108
return false;
112109
}
113110
return texture_->IsOpaque();
@@ -170,13 +167,13 @@ bool TiledTextureContents::Render(const ContentContext& renderer,
170167
cmd, host_buffer.EmplaceUniform(frag_info));
171168
}
172169

173-
if (color_filter_.has_value()) {
170+
if (color_filter_) {
174171
auto filtered_texture = CreateFilterTexture(renderer);
175-
if (!filtered_texture.has_value()) {
172+
if (!filtered_texture) {
176173
return false;
177174
}
178175
FS::BindTextureSampler(
179-
cmd, filtered_texture.value(),
176+
cmd, filtered_texture,
180177
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
181178
CreateDescriptor(renderer.GetDeviceCapabilities())));
182179
} else {

impeller/entity/contents/tiled_texture_contents.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class TiledTextureContents final : public ColorSourceContents {
5151
///
5252
/// This may not be a performance improvement if the image is tiled into a
5353
/// much smaller size that its original texture size.
54-
void SetColorFilter(std::optional<ColorFilterProc> color_filter);
54+
void SetColorFilter(ColorFilterProc color_filter);
5555

5656
// |Contents|
5757
std::optional<Snapshot> RenderToSnapshot(
@@ -63,7 +63,7 @@ class TiledTextureContents final : public ColorSourceContents {
6363
const std::string& label = "Tiled Texture Snapshot") const override;
6464

6565
private:
66-
std::optional<std::shared_ptr<Texture>> CreateFilterTexture(
66+
std::shared_ptr<Texture> CreateFilterTexture(
6767
const ContentContext& renderer) const;
6868

6969
SamplerDescriptor CreateDescriptor(const Capabilities& capabilities) const;
@@ -74,7 +74,7 @@ class TiledTextureContents final : public ColorSourceContents {
7474
SamplerDescriptor sampler_descriptor_ = {};
7575
Entity::TileMode x_tile_mode_ = Entity::TileMode::kClamp;
7676
Entity::TileMode y_tile_mode_ = Entity::TileMode::kClamp;
77-
std::optional<ColorFilterProc> color_filter_;
77+
ColorFilterProc color_filter_ = nullptr;
7878

7979
FML_DISALLOW_COPY_AND_ASSIGN(TiledTextureContents);
8080
};

lib/ui/painting/image_decoder_impeller.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,10 @@ DecompressResult ImageDecoderImpeller::DecompressTexture(
208208

209209
if (bitmap->dimensions() == target_size) {
210210
auto buffer = bitmap_allocator->GetDeviceBuffer();
211-
if (!buffer.has_value()) {
211+
if (!buffer) {
212212
return DecompressResult{.decode_error = "Unable to get device buffer"};
213213
}
214-
return DecompressResult{.device_buffer = buffer.value(),
214+
return DecompressResult{.device_buffer = buffer,
215215
.sk_bitmap = bitmap,
216216
.image_info = bitmap->info()};
217217
}
@@ -240,10 +240,10 @@ DecompressResult ImageDecoderImpeller::DecompressTexture(
240240
scaled_bitmap->setImmutable();
241241

242242
auto buffer = scaled_allocator->GetDeviceBuffer();
243-
if (!buffer.has_value()) {
243+
if (!buffer) {
244244
return DecompressResult{.decode_error = "Unable to get device buffer"};
245245
}
246-
return DecompressResult{.device_buffer = buffer.value(),
246+
return DecompressResult{.device_buffer = buffer,
247247
.sk_bitmap = scaled_bitmap,
248248
.image_info = scaled_bitmap->info()};
249249
}
@@ -508,10 +508,10 @@ ImpellerAllocator::ImpellerAllocator(
508508
std::shared_ptr<impeller::Allocator> allocator)
509509
: allocator_(std::move(allocator)) {}
510510

511-
std::optional<std::shared_ptr<impeller::DeviceBuffer>>
512-
ImpellerAllocator::GetDeviceBuffer() const {
513-
if (buffer_.has_value()) {
514-
buffer_.value()->Flush();
511+
std::shared_ptr<impeller::DeviceBuffer> ImpellerAllocator::GetDeviceBuffer()
512+
const {
513+
if (buffer_) {
514+
buffer_->Flush();
515515
}
516516
return buffer_;
517517
}

0 commit comments

Comments
 (0)