Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
flutter::ImageDescriptor Class Reference

Creates an image descriptor for encoded or decoded image data, describing the width, height, and bytes per pixel for that image. This class will hold a reference on the underlying image data, and in the case of compressed data, an ImageGenerator for the data. The Codec initialization actually happens in initEncoded, making initstantiateCodec a lightweight operation. More...

#include <image_descriptor.h>

Inheritance diagram for flutter::ImageDescriptor:
flutter::RefCountedDartWrappable< ImageDescriptor > fml::RefCountedThreadSafe< T > tonic::DartWrappable fml::internal::RefCountedThreadSafeBase

Classes

struct  ImageInfo
 

Public Types

enum  PixelFormat {
  kUnknown ,
  kRGBA8888 ,
  kBGRA8888 ,
  kRGBAFloat32 ,
  kR32Float ,
  kGray8
}
 
- Public Types inherited from tonic::DartWrappable
enum  DartNativeFields {
  kPeerIndex ,
  kNumberOfNativeFields
}
 

Public Member Functions

 ~ImageDescriptor () override=default
 
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.
 
int width () const
 The width of this image, EXIF oriented if applicable.
 
int height () const
 The height of this image. EXIF oriented if applicable.
 
int bytesPerPixel () const
 The bytes per pixel of the image.
 
int row_bytes () const
 The byte length of the first row of the image. Defaults to width() * 4.
 
bool should_resize (int target_width, int target_height) const
 Whether the given target_width or target_height differ from width() and height() respectively.
 
sk_sp< SkData > data () const
 The underlying buffer for this image.
 
sk_sp< SkImage > image () const
 
bool is_compressed () const
 Whether this descriptor represents compressed (encoded) data or not.
 
const ImageInfoimage_info () const
 The orientation corrected image info for this image.
 
SkISize get_scaled_dimensions (float scale)
 Gets the scaled dimensions of this image, if backed by an ImageGenerator that can perform efficient subpixel scaling.
 
bool get_pixels (const SkPixmap &pixmap) const
 Gets pixels for this image transformed based on the EXIF orientation tag, if applicable.
 
void dispose ()
 
- Public Member Functions inherited from flutter::RefCountedDartWrappable< ImageDescriptor >
virtual void RetainDartWrappableReference () const override
 
virtual void ReleaseDartWrappableReference () const override
 
- Public Member Functions inherited from fml::RefCountedThreadSafe< T >
void Release () const
 
- Public Member Functions inherited from fml::internal::RefCountedThreadSafeBase
void AddRef () const
 
bool HasOneRef () const
 
void AssertHasOneRef () const
 
- Public Member Functions inherited from tonic::DartWrappable
 DartWrappable ()
 
virtual const DartWrapperInfoGetDartWrapperInfo () const =0
 
Dart_Handle CreateDartWrapper (DartState *dart_state)
 
void AssociateWithDartWrapper (Dart_Handle wrappable)
 
void ClearDartWrapper ()
 
Dart_WeakPersistentHandle dart_wrapper () const
 

Static Public Member Functions

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 recognized by an encoder installed in the ImageGeneratorRegistry. Calling this method will create an ImageGenerator and read EXIF corrected dimensions from the image data.
 
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 PixelFormat.
 
static ImageInfo CreateImageInfo (const SkImageInfo &sk_image_info)
 
static SkImageInfo ToSkImageInfo (const ImageInfo &image_info)
 

Friends

class ImageDecoderFixtureTest
 

Additional Inherited Members

- Protected Member Functions inherited from fml::RefCountedThreadSafe< T >
 RefCountedThreadSafe ()
 
 ~RefCountedThreadSafe ()
 
- Protected Member Functions inherited from fml::internal::RefCountedThreadSafeBase
 RefCountedThreadSafeBase ()
 
 ~RefCountedThreadSafeBase ()
 
bool Release () const
 
void Adopt ()
 
- Protected Member Functions inherited from tonic::DartWrappable
virtual ~DartWrappable ()
 
- Static Protected Member Functions inherited from tonic::DartWrappable
static Dart_PersistentHandle GetTypeForWrapper (tonic::DartState *dart_state, const tonic::DartWrapperInfo &wrapper_info)
 

Detailed Description

Creates an image descriptor for encoded or decoded image data, describing the width, height, and bytes per pixel for that image. This class will hold a reference on the underlying image data, and in the case of compressed data, an ImageGenerator for the data. The Codec initialization actually happens in initEncoded, making initstantiateCodec a lightweight operation.

See also
ImageGenerator

Definition at line 33 of file image_descriptor.h.

Member Enumeration Documentation

◆ PixelFormat

Enumerator
kUnknown 
kRGBA8888 
kBGRA8888 
kRGBAFloat32 
kR32Float 
kGray8 

Definition at line 38 of file image_descriptor.h.

Constructor & Destructor Documentation

◆ ~ImageDescriptor()

flutter::ImageDescriptor::~ImageDescriptor ( )
overridedefault

Member Function Documentation

◆ bytesPerPixel()

int flutter::ImageDescriptor::bytesPerPixel ( ) const

The bytes per pixel of the image.

Definition at line 221 of file image_descriptor.cc.

221 {
222 switch (image_info_.format) {
223 case kUnknown:
224 return 0;
225 case kGray8:
226 return 1;
227 case kRGBA8888:
228 case kBGRA8888:
229 case kR32Float:
230 return 4;
231 case kRGBAFloat32:
232 return 16;
233 }
234}

References flutter::ImageDescriptor::ImageInfo::format, kBGRA8888, kGray8, kR32Float, kRGBA8888, kRGBAFloat32, and kUnknown.

Referenced by row_bytes().

◆ CreateImageInfo()

ImageDescriptor::ImageInfo flutter::ImageDescriptor::CreateImageInfo ( const SkImageInfo &  sk_image_info)
static

Definition at line 20 of file image_descriptor.cc.

21 {
23 switch (sk_image_info.colorType()) {
24 case kUnknown_SkColorType:
26 break;
27 case kRGBA_8888_SkColorType:
29 break;
30 case kBGRA_8888_SkColorType:
32 break;
33 case kRGBA_F32_SkColorType:
35 break;
36 case kGray_8_SkColorType:
37 format = kGray8;
38 break;
39 default:
40 FML_DCHECK(false) << "Unsupported pixel format: "
41 << sk_image_info.colorType();
43 }
44 return ImageInfo{
45 .width = static_cast<uint32_t>(sk_image_info.width()),
46 .height = static_cast<uint32_t>(sk_image_info.height()),
47 .format = format,
48 .alpha_type = sk_image_info.alphaType(),
49 .color_space = sk_image_info.refColorSpace(),
50 };
51}
int height() const
The height of this image. EXIF oriented if applicable.
uint32_t uint32_t * format
#define FML_DCHECK(condition)
Definition logging.h:122
uint32_t alpha_type

References alpha_type, FML_DCHECK, format, height(), kBGRA8888, kGray8, kRGBA8888, kRGBAFloat32, kUnknown, and flutter::ImageDescriptor::ImageInfo::width.

Referenced by flutter::testing::TEST_F(), and flutter::testing::TEST_F().

◆ data()

sk_sp< SkData > flutter::ImageDescriptor::data ( ) const
inline

The underlying buffer for this image.

Definition at line 104 of file image_descriptor.h.

104{ return buffer_; }

Referenced by flutter::ImageDecoderImpeller::DecompressTexture(), flutter::ImageFromDecompressedData(), and initRaw().

◆ dispose()

void flutter::ImageDescriptor::dispose ( )
inline

Definition at line 129 of file image_descriptor.h.

129 {
130 buffer_.reset();
131 generator_.reset();
133 }

References tonic::DartWrappable::ClearDartWrapper().

◆ get_pixels()

bool flutter::ImageDescriptor::get_pixels ( const SkPixmap &  pixmap) const

Gets pixels for this image transformed based on the EXIF orientation tag, if applicable.

Definition at line 215 of file image_descriptor.cc.

215 {
216 FML_DCHECK(generator_);
217 return generator_->GetPixels(pixmap.info(), pixmap.writable_addr(),
218 pixmap.rowBytes());
219}

References FML_DCHECK.

Referenced by flutter::ImageDecoderSkia::ImageFromCompressedData().

◆ get_scaled_dimensions()

SkISize flutter::ImageDescriptor::get_scaled_dimensions ( float  scale)
inline

Gets the scaled dimensions of this image, if backed by an ImageGenerator that can perform efficient subpixel scaling.

See also
ImageGenerator::GetScaledDimensions

Definition at line 118 of file image_descriptor.h.

118 {
119 if (generator_) {
120 return generator_->GetScaledDimensions(scale);
121 }
122 return SkISize::Make(image_info_.width, image_info_.height);
123 }

References flutter::ImageDescriptor::ImageInfo::height, and flutter::ImageDescriptor::ImageInfo::width.

Referenced by flutter::ImageDecoderImpeller::DecompressTexture(), and flutter::ImageDecoderSkia::ImageFromCompressedData().

◆ height()

int flutter::ImageDescriptor::height ( ) const
inline

The height of this image. EXIF oriented if applicable.

Definition at line 85 of file image_descriptor.h.

85{ return image_info_.height; }

References flutter::ImageDescriptor::ImageInfo::height.

Referenced by CreateImageInfo(), initRaw(), and should_resize().

◆ image()

sk_sp< SkImage > flutter::ImageDescriptor::image ( ) const

Definition at line 211 of file image_descriptor.cc.

211 {
212 return generator_->GetImage();
213}

Referenced by flutter::ImageDecoderSkia::ImageFromCompressedData().

◆ image_info()

const ImageInfo & flutter::ImageDescriptor::image_info ( ) const
inline

The orientation corrected image info for this image.

Definition at line 113 of file image_descriptor.h.

113{ return image_info_; }

Referenced by flutter::ImageDecoderImpeller::DecompressTexture(), flutter::ImageDecoderSkia::ImageFromCompressedData(), flutter::ImageFromDecompressedData(), initRaw(), and ToSkImageInfo().

◆ initEncoded()

Dart_Handle flutter::ImageDescriptor::initEncoded ( Dart_Handle  descriptor_handle,
ImmutableBuffer immutable_buffer,
Dart_Handle  callback_handle 
)
static

Asynchronously initializes an ImageDescriptor for an encoded image, as long as the format is recognized by an encoder installed in the ImageGeneratorRegistry. Calling this method will create an ImageGenerator and read EXIF corrected dimensions from the image data.

See also
ImageGeneratorRegistry

Definition at line 94 of file image_descriptor.cc.

96 {
97 if (!Dart_IsClosure(callback_handle)) {
98 return tonic::ToDart("Callback must be a function");
99 }
100
101 if (!immutable_buffer) {
102 return tonic::ToDart("Buffer parameter must not be null");
103 }
104
105 // This has to be valid because this method is called from Dart.
106 auto dart_state = UIDartState::Current();
107 auto registry = dart_state->GetImageGeneratorRegistry();
108
109 if (!registry) {
110 return tonic::ToDart(
111 "Failed to access the internal image decoder "
112 "registry on this isolate. Please file a bug on "
113 "https://github.com/flutter/flutter/issues.");
114 }
115
116 auto generator =
117 registry->CreateCompatibleGenerator(immutable_buffer->data());
118
119 if (!generator) {
120 // No compatible image decoder was found.
121 return tonic::ToDart("Invalid image data");
122 }
123
124 auto descriptor = fml::MakeRefCounted<ImageDescriptor>(
125 immutable_buffer->data(), std::move(generator));
126
127 FML_DCHECK(descriptor);
128
129 descriptor->AssociateWithDartWrapper(descriptor_handle);
130 tonic::DartInvoke(callback_handle, {Dart_TypeVoid()});
131
132 return Dart_Null();
133}
static UIDartState * Current()
Dart_Handle ToDart(const T &object)
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)

References flutter::UIDartState::Current(), tonic::DartInvoke(), flutter::ImmutableBuffer::data(), FML_DCHECK, and tonic::ToDart().

◆ initRaw()

void flutter::ImageDescriptor::initRaw ( Dart_Handle  descriptor_handle,
const fml::RefPtr< ImmutableBuffer > &  data,
int  width,
int  height,
int  row_bytes,
int  pixel_format 
)
static

Synchronously initializes an ImageDescriptor for decompressed image data as specified by the PixelFormat.

Definition at line 154 of file image_descriptor.cc.

159 {
160 ImageDescriptor::PixelFormat image_descriptor_pixel_format =
161 toImageDescriptorPixelFormat(pixel_format);
162 const ImageInfo image_info = {
163 .width = static_cast<uint32_t>(width),
164 .height = static_cast<uint32_t>(height),
165 .format = image_descriptor_pixel_format,
166 .alpha_type = image_descriptor_pixel_format == PixelFormat::kRGBAFloat32
167 ? kUnpremul_SkAlphaType
168 : kPremul_SkAlphaType,
169 .color_space = SkColorSpace::MakeSRGB(),
170 };
171
172 auto descriptor = fml::MakeRefCounted<ImageDescriptor>(
173 data->data(), image_info,
174 row_bytes == -1 ? std::nullopt : std::optional<size_t>(row_bytes));
175 descriptor->AssociateWithDartWrapper(descriptor_handle);
176}
int width() const
The width of this image, EXIF oriented if applicable.
int row_bytes() const
The byte length of the first row of the image. Defaults to width() * 4.
const ImageInfo & image_info() const
The orientation corrected image info for this image.
sk_sp< SkData > data() const
The underlying buffer for this image.
Definition ref_ptr.h:261

References data(), height(), image_info(), kRGBAFloat32, row_bytes(), flutter::ImageDescriptor::ImageInfo::width, and width().

◆ instantiateCodec()

void flutter::ImageDescriptor::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.

Definition at line 194 of file image_descriptor.cc.

197 {
198 fml::RefPtr<Codec> ui_codec;
199 if (!generator_ || generator_->GetFrameCount() == 1) {
200 ui_codec = fml::MakeRefCounted<SingleFrameCodec>(
201 static_cast<fml::RefPtr<ImageDescriptor>>(this), //
202 target_width, //
203 target_height, //
204 ToImageDecoderTargetPixelFormat(destination_format));
205 } else {
206 ui_codec = fml::MakeRefCounted<MultiFrameCodec>(generator_);
207 }
208 ui_codec->AssociateWithDartWrapper(codec_handle);
209}
ImageDecoder::TargetPixelFormat ToImageDecoderTargetPixelFormat(int32_t value)

References flutter::ToImageDecoderTargetPixelFormat().

◆ is_compressed()

bool flutter::ImageDescriptor::is_compressed ( ) const
inline

Whether this descriptor represents compressed (encoded) data or not.

Definition at line 110 of file image_descriptor.h.

110{ return !!generator_; }

Referenced by flutter::ImageDecoderImpeller::DecompressTexture().

◆ row_bytes()

int flutter::ImageDescriptor::row_bytes ( ) const
inline

The byte length of the first row of the image. Defaults to width() * 4.

Definition at line 92 of file image_descriptor.h.

92 {
93 return row_bytes_.value_or(
94 static_cast<size_t>(image_info_.width * bytesPerPixel()));
95 }
int bytesPerPixel() const
The bytes per pixel of the image.

References bytesPerPixel(), and flutter::ImageDescriptor::ImageInfo::width.

Referenced by flutter::ImageFromDecompressedData(), and initRaw().

◆ should_resize()

bool flutter::ImageDescriptor::should_resize ( int  target_width,
int  target_height 
) const
inline

Whether the given target_width or target_height differ from width() and height() respectively.

Definition at line 99 of file image_descriptor.h.

99 {
100 return target_width != width() || target_height != height();
101 }
int32_t height
int32_t width

References height(), and width().

Referenced by flutter::ImageDecoderSkia::ImageFromCompressedData().

◆ ToSkImageInfo()

SkImageInfo flutter::ImageDescriptor::ToSkImageInfo ( const ImageInfo image_info)
static

Definition at line 53 of file image_descriptor.cc.

53 {
54 SkColorType color_type = kUnknown_SkColorType;
55 switch (image_info.format) {
57 color_type = kUnknown_SkColorType;
58 break;
60 color_type = kRGBA_8888_SkColorType;
61 break;
63 color_type = kBGRA_8888_SkColorType;
64 break;
66 color_type = kRGBA_F32_SkColorType;
67 break;
69 color_type = kGray_8_SkColorType;
70 break;
72 FML_DCHECK(false) << "not a supported skia format";
73 break;
74 }
75 return SkImageInfo::Make(image_info.width, image_info.height, color_type,
77}
uint32_t color_type
const sk_sp< SkColorSpace > color_space

References flutter::ImageDescriptor::ImageInfo::alpha_type, flutter::ImageDescriptor::ImageInfo::color_space, color_type, FML_DCHECK, flutter::ImageDescriptor::ImageInfo::format, flutter::ImageDescriptor::ImageInfo::height, image_info(), kBGRA8888, kGray8, kR32Float, kRGBA8888, kRGBAFloat32, kUnknown, and flutter::ImageDescriptor::ImageInfo::width.

Referenced by flutter::ImageDecoderImpeller::DecompressTexture(), flutter::ImageDecoderSkia::ImageFromCompressedData(), and flutter::ImageFromDecompressedData().

◆ width()

int flutter::ImageDescriptor::width ( ) const
inline

The width of this image, EXIF oriented if applicable.

Definition at line 82 of file image_descriptor.h.

82{ return image_info_.width; }

References flutter::ImageDescriptor::ImageInfo::width.

Referenced by initRaw(), and should_resize().

Friends And Related Symbol Documentation

◆ ImageDecoderFixtureTest

friend class ImageDecoderFixtureTest
friend

Definition at line 154 of file image_descriptor.h.


The documentation for this class was generated from the following files: