Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
image_descriptor.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_DESCRIPTOR_H_
6#define FLUTTER_LIB_UI_PAINTING_IMAGE_DESCRIPTOR_H_
7
8#include <cstdint>
9#include <memory>
10#include <optional>
11
12#include "flutter/fml/macros.h"
13#include "flutter/lib/ui/dart_wrapper.h"
14#include "flutter/lib/ui/painting/image_generator_registry.h"
15#include "flutter/lib/ui/painting/immutable_buffer.h"
22
23namespace flutter {
24
25/// @brief Creates an image descriptor for encoded or decoded image data,
26/// describing the width, height, and bytes per pixel for that image.
27/// This class will hold a reference on the underlying image data, and
28/// in the case of compressed data, an `ImageGenerator` for the data.
29/// The Codec initialization actually happens in initEncoded, making
30/// `initstantiateCodec` a lightweight operation.
31/// @see `ImageGenerator`
32class ImageDescriptor : public RefCountedDartWrappable<ImageDescriptor> {
33 public:
34 ~ImageDescriptor() override = default;
35
36 // This must be kept in sync with the enum in painting.dart
42
43 /// @brief Asynchronously initializes an ImageDescriptor for an encoded
44 /// image, as long as the format is recognized by an encoder installed
45 /// in the `ImageGeneratorRegistry`. Calling this method will create
46 /// an `ImageGenerator` and read EXIF corrected dimensions from the
47 /// image data.
48 /// @see `ImageGeneratorRegistry`
49 static Dart_Handle initEncoded(Dart_Handle descriptor_handle,
50 ImmutableBuffer* immutable_buffer,
51 Dart_Handle callback_handle);
52
53 /// @brief Synchronously initializes an `ImageDescriptor` for decompressed
54 /// image data as specified by the `PixelFormat`.
55 static void initRaw(Dart_Handle descriptor_handle,
57 int width,
58 int height,
59 int row_bytes,
60 PixelFormat pixel_format);
61
62 /// @brief Associates a flutter::Codec object with the dart.ui Codec handle.
63 void instantiateCodec(Dart_Handle codec, int target_width, int target_height);
64
65 /// @brief The width of this image, EXIF oriented if applicable.
66 int width() const { return image_info_.width(); }
67
68 /// @brief The height of this image. EXIF oriented if applicable.
69 int height() const { return image_info_.height(); }
70
71 /// @brief The bytes per pixel of the image.
72 int bytesPerPixel() const { return image_info_.bytesPerPixel(); }
73
74 /// @brief The byte length of the first row of the image.
75 /// Defaults to width() * 4.
76 int row_bytes() const {
77 return row_bytes_.value_or(
78 static_cast<size_t>(image_info_.width() * image_info_.bytesPerPixel()));
79 }
80
81 /// @brief Whether the given `target_width` or `target_height` differ from
82 /// `width()` and `height()` respectively.
83 bool should_resize(int target_width, int target_height) const {
84 return target_width != width() || target_height != height();
85 }
86
87 /// @brief The underlying buffer for this image.
88 sk_sp<SkData> data() const { return buffer_; }
89
90 sk_sp<SkImage> image() const;
91
92 /// @brief Whether this descriptor represents compressed (encoded) data or
93 /// not.
94 bool is_compressed() const { return !!generator_; }
95
96 /// @brief The orientation corrected image info for this image.
97 const SkImageInfo& image_info() const { return image_info_; }
98
99 /// @brief Gets the scaled dimensions of this image, if backed by an
100 /// `ImageGenerator` that can perform efficient subpixel scaling.
101 /// @see `ImageGenerator::GetScaledDimensions`
103 if (generator_) {
104 return generator_->GetScaledDimensions(scale);
105 }
106 return image_info_.dimensions();
107 }
108
109 /// @brief Gets pixels for this image transformed based on the EXIF
110 /// orientation tag, if applicable.
111 bool get_pixels(const SkPixmap& pixmap) const;
112
113 void dispose() {
114 buffer_.reset();
115 generator_.reset();
117 }
118
119 private:
121 const SkImageInfo& image_info,
122 std::optional<size_t> row_bytes);
124 std::shared_ptr<ImageGenerator> generator);
125
126 sk_sp<SkData> buffer_;
127 std::shared_ptr<ImageGenerator> generator_;
128 const SkImageInfo image_info_;
129 std::optional<size_t> row_bytes_;
130
131 const SkImageInfo CreateImageInfo() const;
132
133 DEFINE_WRAPPERTYPEINFO();
136
138};
139
140} // namespace flutter
141
142#endif // FLUTTER_LIB_UI_PAINTING_IMAGE_DESCRIPTOR_H_
Creates an image descriptor for encoded or decoded image data, describing the width,...
sk_sp< SkImage > image() const
const SkImageInfo & image_info() const
The orientation corrected image info for this image.
~ImageDescriptor() override=default
void instantiateCodec(Dart_Handle codec, int target_width, int target_height)
Associates a flutter::Codec object with the dart.ui Codec handle.
int width() const
The width of this image, EXIF oriented if applicable.
SkISize get_scaled_dimensions(float scale)
Gets the scaled dimensions of this image, if backed by an ImageGenerator that can perform efficient s...
int row_bytes() const
The byte length of the first row of the image. Defaults to width() * 4.
int height() const
The height of this image. EXIF oriented if applicable.
bool should_resize(int target_width, int target_height) const
Whether the given target_width or target_height differ from width() and height() respectively.
bool get_pixels(const SkPixmap &pixmap) const
Gets pixels for this image transformed based on the EXIF orientation tag, if applicable.
int bytesPerPixel() const
The bytes per pixel of the image.
static Dart_Handle initEncoded(Dart_Handle descriptor_handle, ImmutableBuffer *immutable_buffer, Dart_Handle callback_handle)
Asynchronously initializes an ImageDescriptor for an encoded image, as long as the format is recogniz...
static void initRaw(Dart_Handle descriptor_handle, const fml::RefPtr< ImmutableBuffer > &data, int width, int height, int row_bytes, PixelFormat pixel_format)
Synchronously initializes an ImageDescriptor for decompressed image data as specified by the PixelFor...
bool is_compressed() const
Whether this descriptor represents compressed (encoded) data or not.
sk_sp< SkData > data() const
The underlying buffer for this image.
void reset(T *ptr=nullptr)
Definition SkRefCnt.h:310
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
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
#define FML_FRIEND_MAKE_REF_COUNTED(T)
int32_t height
int32_t width
const Scalar scale
int bytesPerPixel() const
SkISize dimensions() const
int width() const
int height() const