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"
16#include "third_party/skia/include/core/SkData.h"
17#include "third_party/skia/include/core/SkImage.h"
18#include "third_party/skia/include/core/SkImageInfo.h"
19#include "third_party/skia/include/core/SkPixmap.h"
20#include "third_party/skia/include/core/SkSize.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`
102 SkISize get_scaled_dimensions(float scale) {
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:
120 ImageDescriptor(sk_sp<SkData> buffer,
121 const SkImageInfo& image_info,
122 std::optional<size_t> row_bytes);
123 ImageDescriptor(sk_sp<SkData> buffer,
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.
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition formats.h:99
#define FML_FRIEND_MAKE_REF_COUNTED(T)