From 2cad9d208b8e6de8e7b8c4f642a59e59d3cad9ab Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Wed, 20 Oct 2021 14:46:25 -0700 Subject: [PATCH] Fix bug where native image decoders aren't working in release mode (#29254) --- lib/ui/painting/image_generator_registry.cc | 4 +- lib/ui/painting/image_generator_registry.h | 1 + .../image_generator_registry_unittests.cc | 37 +++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/ui/painting/image_generator_registry.cc b/lib/ui/painting/image_generator_registry.cc index f48cfd5f8bb50..3d5d4cf5ecb98 100644 --- a/lib/ui/painting/image_generator_registry.cc +++ b/lib/ui/painting/image_generator_registry.cc @@ -4,7 +4,6 @@ #include -#include "flutter/fml/trace_event.h" #include "flutter/lib/ui/painting/image_generator_registry.h" #include "third_party/skia/include/codec/SkCodec.h" #include "third_party/skia/include/core/SkImageGenerator.h" @@ -48,8 +47,7 @@ ImageGeneratorRegistry::~ImageGeneratorRegistry() = default; void ImageGeneratorRegistry::AddFactory(ImageGeneratorFactory factory, int32_t priority) { - image_generator_factories_.insert( - {factory, priority, fml::tracing::TraceNonce()}); + image_generator_factories_.insert({factory, priority, ++nonce_}); } std::shared_ptr diff --git a/lib/ui/painting/image_generator_registry.h b/lib/ui/painting/image_generator_registry.h index 8c0d7d0f136b5..b04dc1cc24eba 100644 --- a/lib/ui/painting/image_generator_registry.h +++ b/lib/ui/painting/image_generator_registry.h @@ -84,6 +84,7 @@ class ImageGeneratorRegistry { using FactorySet = std::set; FactorySet image_generator_factories_; + size_t nonce_; fml::WeakPtrFactory weak_factory_; }; diff --git a/lib/ui/painting/image_generator_registry_unittests.cc b/lib/ui/painting/image_generator_registry_unittests.cc index 925eb919ba231..8c0879848df2f 100644 --- a/lib/ui/painting/image_generator_registry_unittests.cc +++ b/lib/ui/painting/image_generator_registry_unittests.cc @@ -111,5 +111,42 @@ TEST_F(ShellTest, DefaultGeneratorsTakePrecedentOverNegativePriority) { ASSERT_EQ(result->GetInfo().width(), 3024); } +TEST_F(ShellTest, DefaultGeneratorsTakePrecedentOverZeroPriority) { + ImageGeneratorRegistry registry; + + registry.AddFactory( + [](sk_sp buffer) { + return std::make_unique(1337); + }, + 0); + + // Fetch the generator and query for basic info. + auto result = registry.CreateCompatibleGenerator(LoadValidImageFixture()); + // If the real width of the image pops out, then the default generator was + // returned rather than the fake one. + ASSERT_EQ(result->GetInfo().width(), 3024); +} + +TEST_F(ShellTest, ImageGeneratorsWithSamePriorityCascadeChronologically) { + ImageGeneratorRegistry registry; + + // Add 2 factories with the same high priority. + registry.AddFactory( + [](sk_sp buffer) { + return std::make_unique(1337); + }, + 5); + registry.AddFactory( + [](sk_sp buffer) { + return std::make_unique(7777); + }, + 5); + + // Feed empty data so that Skia's image generators will reject it, but ours + // won't. + auto result = registry.CreateCompatibleGenerator(SkData::MakeEmpty()); + ASSERT_EQ(result->GetInfo().width(), 1337); +} + } // namespace testing } // namespace flutter