Flutter Engine
The Flutter Engine
image_generator_registry_unittests.cc
Go to the documentation of this file.
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 "flutter/lib/ui/painting/image_generator_registry.h"
6
7#include "flutter/fml/mapping.h"
8#include "flutter/shell/common/shell_test.h"
9#include "flutter/testing/testing.h"
10
12
13namespace flutter {
14namespace testing {
15
17 auto fixture_mapping = OpenFixtureAsMapping("DashInNooglerHat.jpg");
18
19 // Remap to sk_sp<SkData>.
20 SkData::ReleaseProc on_release = [](const void* ptr, void* context) -> void {
21 delete reinterpret_cast<fml::FileMapping*>(context);
22 };
23 auto data = SkData::MakeWithProc(fixture_mapping->GetMapping(),
24 fixture_mapping->GetSize(), on_release,
25 fixture_mapping.get());
26
27 if (data) {
28 fixture_mapping.release();
29 }
30
31 return data;
32}
33
34TEST_F(ShellTest, CreateCompatibleReturnsBuiltinImageGeneratorForValidImage) {
36
37 // Fetch the generator and query for basic info
39 auto result = registry.CreateCompatibleGenerator(data);
40 auto info = result->GetInfo();
41 ASSERT_EQ(info.width(), 3024);
42 ASSERT_EQ(info.height(), 4032);
43}
44
45TEST_F(ShellTest, CreateCompatibleReturnsNullptrForInvalidImage) {
48 ASSERT_EQ(result, nullptr);
49}
50
52 public:
53 explicit FakeImageGenerator(int identifiableFakeWidth)
54 : info_(SkImageInfo::Make(identifiableFakeWidth,
55 identifiableFakeWidth,
59 const SkImageInfo& GetInfo() { return info_; }
60
61 unsigned int GetFrameCount() const { return 1; }
62
63 unsigned int GetPlayCount() const { return 1; }
64
65 const ImageGenerator::FrameInfo GetFrameInfo(unsigned int frame_index) {
66 return {std::nullopt, 0, SkCodecAnimation::DisposalMethod::kKeep};
67 }
68
70 return SkISize::Make(info_.width(), info_.height());
71 }
72
74 void* pixels,
75 size_t row_bytes,
76 unsigned int frame_index,
77 std::optional<unsigned int> prior_frame) {
78 return false;
79 };
80
81 private:
82 SkImageInfo info_;
83};
84
85TEST_F(ShellTest, PositivePriorityTakesPrecedentOverDefaultGenerators) {
87
88 const int fake_width = 1337;
89 registry.AddFactory(
90 [fake_width](const sk_sp<SkData>& buffer) {
91 return std::make_unique<FakeImageGenerator>(fake_width);
92 },
93 1);
94
95 // Fetch the generator and query for basic info.
97 ASSERT_EQ(result->GetInfo().width(), fake_width);
98}
99
100TEST_F(ShellTest, DefaultGeneratorsTakePrecedentOverNegativePriority) {
101 ImageGeneratorRegistry registry;
102
103 registry.AddFactory(
104 [](const sk_sp<SkData>& buffer) {
105 return std::make_unique<FakeImageGenerator>(1337);
106 },
107 -1);
108
109 // Fetch the generator and query for basic info.
111 // If the real width of the image pops out, then the default generator was
112 // returned rather than the fake one.
113 ASSERT_EQ(result->GetInfo().width(), 3024);
114}
115
116TEST_F(ShellTest, DefaultGeneratorsTakePrecedentOverZeroPriority) {
117 ImageGeneratorRegistry registry;
118
119 registry.AddFactory(
120 [](const sk_sp<SkData>& buffer) {
121 return std::make_unique<FakeImageGenerator>(1337);
122 },
123 0);
124
125 // Fetch the generator and query for basic info.
127 // If the real width of the image pops out, then the default generator was
128 // returned rather than the fake one.
129 ASSERT_EQ(result->GetInfo().width(), 3024);
130}
131
132TEST_F(ShellTest, ImageGeneratorsWithSamePriorityCascadeChronologically) {
133 ImageGeneratorRegistry registry;
134
135 // Add 2 factories with the same high priority.
136 registry.AddFactory(
137 [](const sk_sp<SkData>& buffer) {
138 return std::make_unique<FakeImageGenerator>(1337);
139 },
140 5);
141 registry.AddFactory(
142 [](const sk_sp<SkData>& buffer) {
143 return std::make_unique<FakeImageGenerator>(7777);
144 },
145 5);
146
147 // Feed empty data so that Skia's image generators will reject it, but ours
148 // won't.
150 ASSERT_EQ(result->GetInfo().width(), 1337);
151}
152
153} // namespace testing
154} // namespace flutter
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
SkAlphaType
Definition: SkAlphaType.h:26
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
SkColorType
Definition: SkColorType.h:19
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
void(* ReleaseProc)(const void *ptr, void *context)
Definition: SkData.h:78
static sk_sp< SkData > MakeWithProc(const void *ptr, size_t length, ReleaseProc proc, void *ctx)
Definition: SkData.cpp:128
static sk_sp< SkData > MakeEmpty()
Definition: SkData.cpp:94
Keeps a priority-ordered registry of image generator builders to be used when decoding images....
std::shared_ptr< ImageGenerator > CreateCompatibleGenerator(const sk_sp< SkData > &buffer)
Walks the list of image generator builders in descending priority order until a compatible ImageGener...
void AddFactory(ImageGeneratorFactory factory, int32_t priority)
Install a new factory for image generators.
The minimal interface necessary for defining a decoder that can be used for both single and multi-fra...
unsigned int GetPlayCount() const
The number of times an animated image should play through before playback stops.
bool GetPixels(const SkImageInfo &info, void *pixels, size_t row_bytes, unsigned int frame_index, std::optional< unsigned int > prior_frame)
Decode the image into a given buffer. This method is currently always used for sub-pixel image decodi...
const SkImageInfo & GetInfo()
Returns basic information about the contents of the encoded image. This information can almost always...
unsigned int GetFrameCount() const
Get the number of frames that the encoded image stores. This method is always expected to be called b...
SkISize GetScaledDimensions(float scale)
Given a scale value, find the closest image size that can be used for efficiently decoding the image....
const ImageGenerator::FrameInfo GetFrameInfo(unsigned int frame_index)
Get information about a single frame in the context of a multi-frame image, useful for animation and ...
GAsyncResult * result
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
TEST_F(DisplayListTest, Defaults)
std::unique_ptr< fml::Mapping > OpenFixtureAsMapping(const std::string &fixture_name)
Opens a fixture of the given file name and returns a mapping to its contents.
Definition: testing.cc:59
static sk_sp< SkData > LoadValidImageFixture()
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition: switches.h:41
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
const Scalar scale
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
int width() const
Definition: SkImageInfo.h:365
int height() const
Definition: SkImageInfo.h:371
Info about a single frame in the context of a multi-frame image, useful for animation and blending.