Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
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"
16#include "third_party/skia/include/core/SkColorSpace.h"
17#include "third_party/skia/include/core/SkData.h"
18#include "third_party/skia/include/core/SkImage.h"
19#include "third_party/skia/include/core/SkImageInfo.h"
20#include "third_party/skia/include/core/SkPixmap.h"
21#include "third_party/skia/include/core/SkSize.h"
23
24namespace flutter {
25
26/// @brief Creates an image descriptor for encoded or decoded image data,
27/// describing the width, height, and bytes per pixel for that image.
28/// This class will hold a reference on the underlying image data, and
29/// in the case of compressed data, an `ImageGenerator` for the data.
30/// The Codec initialization actually happens in initEncoded, making
31/// `initstantiateCodec` a lightweight operation.
32/// @see `ImageGenerator`
33class ImageDescriptor : public RefCountedDartWrappable<ImageDescriptor> {
34 public:
35 ~ImageDescriptor() override = default;
36
37 // This must be kept in sync with the enum in painting.dart
39 // Error pixel format for Skia compatibility.
46 };
47
48 struct ImageInfo {
49 const uint32_t width;
50 const uint32_t height;
52 const SkAlphaType alpha_type;
53 const sk_sp<SkColorSpace> color_space;
54 };
55
56 /// @brief Asynchronously initializes an ImageDescriptor for an encoded
57 /// image, as long as the format is recognized by an encoder installed
58 /// in the `ImageGeneratorRegistry`. Calling this method will create
59 /// an `ImageGenerator` and read EXIF corrected dimensions from the
60 /// image data.
61 /// @see `ImageGeneratorRegistry`
62 static Dart_Handle initEncoded(Dart_Handle descriptor_handle,
63 ImmutableBuffer* immutable_buffer,
64 Dart_Handle callback_handle);
65
66 /// @brief Synchronously initializes an `ImageDescriptor` for decompressed
67 /// image data as specified by the `PixelFormat`.
68 static void initRaw(Dart_Handle descriptor_handle,
70 int width,
71 int height,
72 int row_bytes,
73 int pixel_format);
74
75 /// @brief Associates a flutter::Codec object with the dart.ui Codec handle.
76 void instantiateCodec(Dart_Handle codec,
77 int32_t target_width,
78 int32_t target_height,
79 int32_t destination_format);
80
81 /// @brief The width of this image, EXIF oriented if applicable.
82 int width() const { return image_info_.width; }
83
84 /// @brief The height of this image. EXIF oriented if applicable.
85 int height() const { return image_info_.height; }
86
87 /// @brief The bytes per pixel of the image.
88 int bytesPerPixel() const;
89
90 /// @brief The byte length of the first row of the image.
91 /// Defaults to width() * 4.
92 int row_bytes() const {
93 return row_bytes_.value_or(
94 static_cast<size_t>(image_info_.width * bytesPerPixel()));
95 }
96
97 /// @brief Whether the given `target_width` or `target_height` differ from
98 /// `width()` and `height()` respectively.
99 bool should_resize(int target_width, int target_height) const {
100 return target_width != width() || target_height != height();
101 }
102
103 /// @brief The underlying buffer for this image.
104 sk_sp<SkData> data() const { return buffer_; }
105
106 sk_sp<SkImage> image() const;
107
108 /// @brief Whether this descriptor represents compressed (encoded) data or
109 /// not.
110 bool is_compressed() const { return !!generator_; }
111
112 /// @brief The orientation corrected image info for this image.
113 const ImageInfo& image_info() const { return image_info_; }
114
115 /// @brief Gets the scaled dimensions of this image, if backed by an
116 /// `ImageGenerator` that can perform efficient subpixel scaling.
117 /// @see `ImageGenerator::GetScaledDimensions`
118 SkISize get_scaled_dimensions(float scale) {
119 if (generator_) {
120 return generator_->GetScaledDimensions(scale);
121 }
122 return SkISize::Make(image_info_.width, image_info_.height);
123 }
124
125 /// @brief Gets pixels for this image transformed based on the EXIF
126 /// orientation tag, if applicable.
127 bool get_pixels(const SkPixmap& pixmap) const;
128
129 void dispose() {
130 buffer_.reset();
131 generator_.reset();
133 }
134
135 static ImageInfo CreateImageInfo(const SkImageInfo& sk_image_info);
136 static SkImageInfo ToSkImageInfo(const ImageInfo& image_info);
137
138 private:
139 ImageDescriptor(sk_sp<SkData> buffer,
140 const ImageInfo& image_info,
141 std::optional<size_t> row_bytes);
142 ImageDescriptor(sk_sp<SkData> buffer,
143 std::shared_ptr<ImageGenerator> generator);
144
145 sk_sp<SkData> buffer_;
146 const ImageInfo image_info_;
147 std::shared_ptr<ImageGenerator> generator_;
148 std::optional<size_t> row_bytes_;
149
150 DEFINE_WRAPPERTYPEINFO();
153
155};
156
157} // namespace flutter
158
159#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
~ImageDescriptor() override=default
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...
void instantiateCodec(Dart_Handle codec, int32_t target_width, int32_t target_height, int32_t destination_format)
Associates a flutter::Codec object with the dart.ui Codec handle.
static SkImageInfo ToSkImageInfo(const ImageInfo &image_info)
int row_bytes() const
The byte length of the first row of the image. Defaults to width() * 4.
static ImageInfo CreateImageInfo(const SkImageInfo &sk_image_info)
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...
bool is_compressed() const
Whether this descriptor represents compressed (encoded) data or not.
static void initRaw(Dart_Handle descriptor_handle, const fml::RefPtr< ImmutableBuffer > &data, int width, int height, int row_bytes, int pixel_format)
Synchronously initializes an ImageDescriptor for decompressed image data as specified by the PixelFor...
const ImageInfo & image_info() const
The orientation corrected image info for this image.
sk_sp< SkData > data() const
The underlying buffer for this image.
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
#define FML_FRIEND_MAKE_REF_COUNTED(T)
const sk_sp< SkColorSpace > color_space