Flutter Engine
The Flutter Engine
image_generator.h
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#ifndef FLUTTER_LIB_UI_PAINTING_IMAGE_GENERATOR_H_
6#define FLUTTER_LIB_UI_PAINTING_IMAGE_GENERATOR_H_
7
8#include <optional>
9#include "flutter/fml/macros.h"
17
18namespace flutter {
19
20/// @brief The minimal interface necessary for defining a decoder that can be
21/// used for both single and multi-frame image decoding. Image
22/// generators can also optionally support decoding into a subscaled
23/// buffer. Implementers of `ImageGenerator` regularly keep internal
24/// state which is not thread safe, and so aliasing and parallel access
25/// should never be done with `ImageGenerator`s.
26/// @see `ImageGenerator::GetScaledDimensions`
28 public:
29 /// Frame count value to denote infinite looping.
30 const static unsigned int kInfinitePlayCount =
32
33 /// @brief Info about a single frame in the context of a multi-frame image,
34 /// useful for animation and blending.
35 struct FrameInfo {
36 /// The frame index of the frame that, if any, this frame needs to be
37 /// blended with.
38 std::optional<unsigned int> required_frame;
39
40 /// Number of milliseconds to show this frame. 0 means only show it for one
41 /// frame.
42 unsigned int duration;
43
44 /// How this frame should be modified before decoding the next one.
46
47 /// The region of the frame that is affected by the disposal method.
48 std::optional<SkIRect> disposal_rect;
49
50 /// How this frame should be blended with the previous frame.
52 };
53
54 virtual ~ImageGenerator();
55
56 /// @brief Returns basic information about the contents of the encoded
57 /// image. This information can almost always be collected by just
58 /// interpreting the header of a decoded image.
59 /// @return Size and color information describing the image.
60 /// @note This method is executed on the UI thread and used for layout
61 /// purposes by the framework, and so this method should not perform
62 /// long synchronous tasks.
63 virtual const SkImageInfo& GetInfo() = 0;
64
65 /// @brief Get the number of frames that the encoded image stores. This
66 /// method is always expected to be called before `GetFrameInfo`, as
67 /// the underlying image decoder may interpret frame information that
68 /// is then used when calling `GetFrameInfo`.
69 /// @return The number of frames that the encoded image stores. This will
70 /// always be 1 for single-frame images.
71 virtual unsigned int GetFrameCount() const = 0;
72
73 /// @brief The number of times an animated image should play through before
74 /// playback stops.
75 /// @return If this image is animated, the number of times the animation
76 /// should play through is returned, otherwise it'll just return 1.
77 /// If the animation should loop forever, `kInfinitePlayCount` is
78 /// returned.
79 virtual unsigned int GetPlayCount() const = 0;
80
81 /// @brief Get information about a single frame in the context of a
82 /// multi-frame image, useful for animation and frame blending.
83 /// This method should only ever be called after `GetFrameCount`
84 /// has been called. This information is nonsensical for
85 /// single-frame images.
86 /// @param[in] frame_index The index of the frame to get information about.
87 /// @return Information about the given frame. If the image is
88 /// single-frame, a default result is returned.
89 /// @see `GetFrameCount`
90 virtual const FrameInfo GetFrameInfo(unsigned int frame_index) = 0;
91
92 /// @brief Given a scale value, find the closest image size that can be
93 /// used for efficiently decoding the image. If subpixel image
94 /// decoding is not supported by the decoder, this method should
95 /// just return the original image size.
96 /// @param[in] scale The desired scale factor of the image for decoding.
97 /// @return The closest image size that can be used for efficiently
98 /// decoding the image.
99 /// @note This method is called prior to `GetPixels` in order to query
100 /// for supported sizes.
101 /// @see `GetPixels`
103
104 /// @brief Decode the image into a given buffer. This method is currently
105 /// always used for sub-pixel image decoding. For full-sized still
106 /// images, `GetImage` is always attempted first.
107 /// @param[in] info The desired size and color info of the decoded
108 /// image to be returned. The implementation of
109 /// `GetScaledDimensions` determines which sizes are
110 /// supported by the image decoder.
111 /// @param[in] pixels The location where the raw decoded image data
112 /// should be written.
113 /// @param[in] row_bytes The total number of bytes that should make up a
114 /// single row of decoded image data
115 /// (i.e. width * bytes_per_pixel).
116 /// @param[in] frame_index Which frame to decode. This is only useful for
117 /// multi-frame images.
118 /// @param[in] prior_frame Optional frame index parameter for multi-frame
119 /// images which specifies the previous frame that
120 /// should be use for blending. This hints to the
121 /// decoder that it should use a previously cached
122 /// frame instead of decoding dependency frame(s).
123 /// If an empty value is supplied, the decoder should
124 /// decode any necessary frames first.
125 /// @return True if the image was successfully decoded.
126 /// @note This method performs potentially long synchronous work, and so
127 /// it should never be executed on the UI thread. Image decoders
128 /// do not require GPU acceleration, and so threads without a GPU
129 /// context may also be used.
130 /// @see `GetScaledDimensions`
131 virtual bool GetPixels(
132 const SkImageInfo& info,
133 void* pixels,
134 size_t row_bytes,
135 unsigned int frame_index = 0,
136 std::optional<unsigned int> prior_frame = std::nullopt) = 0;
137
138 /// @brief Creates an `SkImage` based on the current `ImageInfo` of this
139 /// `ImageGenerator`.
140 /// @return A new `SkImage` containing the decoded image data.
142};
143
145 public:
147
149 std::unique_ptr<SkImageGenerator> generator);
150
151 // |ImageGenerator|
152 const SkImageInfo& GetInfo() override;
153
154 // |ImageGenerator|
155 unsigned int GetFrameCount() const override;
156
157 // |ImageGenerator|
158 unsigned int GetPlayCount() const override;
159
160 // |ImageGenerator|
162 unsigned int frame_index) override;
163
164 // |ImageGenerator|
165 SkISize GetScaledDimensions(float desired_scale) override;
166
167 // |ImageGenerator|
168 bool GetPixels(
169 const SkImageInfo& info,
170 void* pixels,
171 size_t row_bytes,
172 unsigned int frame_index = 0,
173 std::optional<unsigned int> prior_frame = std::nullopt) override;
174
175 static std::unique_ptr<ImageGenerator> MakeFromGenerator(
176 std::unique_ptr<SkImageGenerator> generator);
177
178 private:
179 FML_DISALLOW_COPY_ASSIGN_AND_MOVE(BuiltinSkiaImageGenerator);
180 std::unique_ptr<SkImageGenerator> generator_;
181};
182
184 public:
186
187 explicit BuiltinSkiaCodecImageGenerator(std::unique_ptr<SkCodec> codec);
188
190
191 // |ImageGenerator|
192 const SkImageInfo& GetInfo() override;
193
194 // |ImageGenerator|
195 unsigned int GetFrameCount() const override;
196
197 // |ImageGenerator|
198 unsigned int GetPlayCount() const override;
199
200 // |ImageGenerator|
202 unsigned int frame_index) override;
203
204 // |ImageGenerator|
205 SkISize GetScaledDimensions(float desired_scale) override;
206
207 // |ImageGenerator|
208 bool GetPixels(
209 const SkImageInfo& info,
210 void* pixels,
211 size_t row_bytes,
212 unsigned int frame_index = 0,
213 std::optional<unsigned int> prior_frame = std::nullopt) override;
214
215 static std::unique_ptr<ImageGenerator> MakeFromData(sk_sp<SkData> data);
216
217 private:
218 FML_DISALLOW_COPY_ASSIGN_AND_MOVE(BuiltinSkiaCodecImageGenerator);
219 std::unique_ptr<SkCodec> codec_;
220 SkImageInfo image_info_;
221};
222
223} // namespace flutter
224
225#endif // FLUTTER_LIB_UI_PAINTING_IMAGE_GENERATOR_H_
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
unsigned int GetPlayCount() const override
The number of times an animated image should play through before playback stops.
unsigned int GetFrameCount() const override
Get the number of frames that the encoded image stores. This method is always expected to be called b...
bool GetPixels(const SkImageInfo &info, void *pixels, size_t row_bytes, unsigned int frame_index=0, std::optional< unsigned int > prior_frame=std::nullopt) override
Decode the image into a given buffer. This method is currently always used for sub-pixel image decodi...
const ImageGenerator::FrameInfo GetFrameInfo(unsigned int frame_index) override
Get information about a single frame in the context of a multi-frame image, useful for animation and ...
BuiltinSkiaCodecImageGenerator(std::unique_ptr< SkCodec > codec)
const SkImageInfo & GetInfo() override
Returns basic information about the contents of the encoded image. This information can almost always...
static std::unique_ptr< ImageGenerator > MakeFromData(sk_sp< SkData > data)
SkISize GetScaledDimensions(float desired_scale) override
Given a scale value, find the closest image size that can be used for efficiently decoding the image....
SkISize GetScaledDimensions(float desired_scale) override
Given a scale value, find the closest image size that can be used for efficiently decoding the image....
unsigned int GetPlayCount() const override
The number of times an animated image should play through before playback stops.
const SkImageInfo & GetInfo() override
Returns basic information about the contents of the encoded image. This information can almost always...
const ImageGenerator::FrameInfo GetFrameInfo(unsigned int frame_index) override
Get information about a single frame in the context of a multi-frame image, useful for animation and ...
bool GetPixels(const SkImageInfo &info, void *pixels, size_t row_bytes, unsigned int frame_index=0, std::optional< unsigned int > prior_frame=std::nullopt) override
Decode the image into a given buffer. This method is currently always used for sub-pixel image decodi...
unsigned int GetFrameCount() const override
Get the number of frames that the encoded image stores. This method is always expected to be called b...
BuiltinSkiaImageGenerator(std::unique_ptr< SkImageGenerator > generator)
static std::unique_ptr< ImageGenerator > MakeFromGenerator(std::unique_ptr< SkImageGenerator > generator)
The minimal interface necessary for defining a decoder that can be used for both single and multi-fra...
static const unsigned int kInfinitePlayCount
Frame count value to denote infinite looping.
sk_sp< SkImage > GetImage()
Creates an SkImage based on the current ImageInfo of this ImageGenerator.
virtual SkISize GetScaledDimensions(float scale)=0
Given a scale value, find the closest image size that can be used for efficiently decoding the image....
virtual const SkImageInfo & GetInfo()=0
Returns basic information about the contents of the encoded image. This information can almost always...
virtual unsigned int GetFrameCount() const =0
Get the number of frames that the encoded image stores. This method is always expected to be called b...
virtual const FrameInfo GetFrameInfo(unsigned int frame_index)=0
Get information about a single frame in the context of a multi-frame image, useful for animation and ...
virtual unsigned int GetPlayCount() const =0
The number of times an animated image should play through before playback stops.
virtual bool GetPixels(const SkImageInfo &info, void *pixels, size_t row_bytes, unsigned int frame_index=0, std::optional< unsigned int > prior_frame=std::nullopt)=0
Decode the image into a given buffer. This method is currently always used for sub-pixel image decodi...
static float max(float r, float g, float b)
Definition: hsl.cpp:49
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
Info about a single frame in the context of a multi-frame image, useful for animation and blending.
SkCodecAnimation::DisposalMethod disposal_method
How this frame should be modified before decoding the next one.
std::optional< unsigned int > required_frame
std::optional< SkIRect > disposal_rect
The region of the frame that is affected by the disposal method.
SkCodecAnimation::Blend blend_mode
How this frame should be blended with the previous frame.