From f0ccf7a9a36ba5cff376870c0b08b18f34972fad Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Mon, 20 Nov 2023 16:43:28 -0800 Subject: [PATCH] [Impeller] Try replacing `*desc` with value/is_valid checks. (flutter/engine#48194) Some discussion here: https://discord.com/channels/608014603317936148/1175215129135153202. In short, `*` is [undefined behavior](https://en.cppreference.com/w/cpp/utility/optional/operator*). After talking to @bdero we are considering that this should never happen. I thought of a few different approaches here, none of them are great. However given that this class is used quite minimally, this seems to jive with similar patterns we're using in scene/scene_context. --- Feel free to push back or suggest alternatives, mostly proposing this to get brain juices flowing. --- .../flutter/impeller/scene/scene_context.cc | 17 +++++++++++++-- .../flutter/impeller/scene/scene_context.h | 21 +++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/engine/src/flutter/impeller/scene/scene_context.cc b/engine/src/flutter/impeller/scene/scene_context.cc index 66c8b07db1..baece93283 100644 --- a/engine/src/flutter/impeller/scene/scene_context.cc +++ b/engine/src/flutter/impeller/scene/scene_context.cc @@ -40,11 +40,24 @@ SceneContext::SceneContext(std::shared_ptr context) return; } - pipelines_[{PipelineKey{GeometryType::kUnskinned, MaterialType::kUnlit}}] = + auto unskinned_variant = MakePipelineVariants( *context_); - pipelines_[{PipelineKey{GeometryType::kSkinned, MaterialType::kUnlit}}] = + if (!unskinned_variant) { + FML_LOG(ERROR) << "Could not create unskinned pipeline variant."; + return; + } + pipelines_[{PipelineKey{GeometryType::kUnskinned, MaterialType::kUnlit}}] = + std::move(unskinned_variant); + + auto skinned_variant = MakePipelineVariants(*context_); + if (!skinned_variant) { + FML_LOG(ERROR) << "Could not create skinned pipeline variant."; + return; + } + pipelines_[{PipelineKey{GeometryType::kSkinned, MaterialType::kUnlit}}] = + std::move(skinned_variant); { impeller::TextureDescriptor texture_descriptor; diff --git a/engine/src/flutter/impeller/scene/scene_context.h b/engine/src/flutter/impeller/scene/scene_context.h index 615ec4a4be..f85663b426 100644 --- a/engine/src/flutter/impeller/scene/scene_context.h +++ b/engine/src/flutter/impeller/scene/scene_context.h @@ -67,9 +67,14 @@ class SceneContext { public: explicit PipelineVariantsT(Context& context) { auto desc = PipelineT::Builder::MakeDefaultPipelineDescriptor(context); + if (!desc.has_value()) { + is_valid_ = false; + return; + } // Apply default ContentContextOptions to the descriptor. SceneContextOptions{}.ApplyToPipelineDescriptor( - *context.GetCapabilities(), *desc); + /*capabilities=*/*context.GetCapabilities(), + /*desc=*/desc.value()); variants_[{}] = std::make_unique(context, desc); }; @@ -99,7 +104,10 @@ class SceneContext { return variant_pipeline; } + bool IsValid() const { return is_valid_; } + private: + bool is_valid_ = true; std::unordered_map, SceneContextOptions::Hash, @@ -108,10 +116,19 @@ class SceneContext { }; template + /// Creates a PipelineVariantsT for the given vertex and fragment shaders. + /// + /// If a pipeline could not be created, returns nullptr. std::unique_ptr MakePipelineVariants(Context& context) { + auto pipeline = + PipelineVariantsT>( + context); + if (!pipeline.IsValid()) { + return nullptr; + } return std::make_unique< PipelineVariantsT>>( - context); + std::move(pipeline)); } std::unordered_map