Flutter Engine
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< ImageDescriptor > tonic::DartWrappable fml::internal::RefCountedThreadSafeBase

Public Types

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

Public Member Functions

 ~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. More...
 
int width () const
 The width of this image, EXIF oriented if applicable. More...
 
int height () const
 The height of this image. EXIF oriented if applicable. More...
 
int bytesPerPixel () const
 The bytes per pixel of the image. More...
 
int row_bytes () const
 The byte length of the first row of the image. Defaults to width() * 4. More...
 
bool should_resize (int target_width, int target_height) const
 Whether the given target_width or target_height differ from width() and height() respectively. More...
 
sk_sp< SkData > data () const
 The underlying buffer for this image. More...
 
sk_sp< SkImage > image () const
 
bool is_compressed () const
 Whether this descriptor represents compressed (encoded) data or not. More...
 
const SkImageInfo & image_info () const
 The orientation corrected image info for this image. More...
 
SkISize get_scaled_dimensions (float scale)
 Gets the scaled dimensions of this image, if backed by an ImageGenerator that can perform efficient subpixel scaling. More...
 
bool get_pixels (const SkPixmap &pixmap) const
 Gets pixels for this image transformed based on the EXIF orientation tag, if applicable. More...
 
void dispose ()
 
size_t GetAllocationSize () const override
 
- 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< ImageDescriptor >
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 void initEncoded (Dart_NativeArguments args)
 Asynchronously initlializes 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. More...
 
static void initRaw (Dart_Handle descriptor_handle, 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 PixelFormat. More...
 
static void RegisterNatives (tonic::DartLibraryNatives *natives)
 

Friends

class ImageDecoderFixtureTest
 

Additional Inherited Members

- Protected Member Functions inherited from fml::RefCountedThreadSafe< ImageDescriptor >
 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 31 of file image_descriptor.h.

Member Enumeration Documentation

◆ PixelFormat

Enumerator
kRGBA8888 
kBGRA8888 

Definition at line 36 of file image_descriptor.h.

Constructor & Destructor Documentation

◆ ~ImageDescriptor()

flutter::ImageDescriptor::~ImageDescriptor ( )
overridedefault

Member Function Documentation

◆ bytesPerPixel()

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

The bytes per pixel of the image.

Definition at line 68 of file image_descriptor.h.

68 { return image_info_.bytesPerPixel(); }

◆ data()

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

The underlying buffer for this image.

Definition at line 84 of file image_descriptor.h.

References image().

Referenced by flutter::ImageFromDecompressedData().

84 { return buffer_; }

◆ dispose()

void flutter::ImageDescriptor::dispose ( )
inline

Definition at line 109 of file image_descriptor.h.

References tonic::DartWrappable::ClearDartWrapper().

109  {
110  buffer_.reset();
111  generator_.reset();
113  }

◆ 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 146 of file image_descriptor.cc.

References FML_DCHECK.

Referenced by get_scaled_dimensions(), and flutter::ImageFromCompressedData().

146  {
147  FML_DCHECK(generator_);
148  return generator_->GetPixels(pixmap.info(), pixmap.writable_addr(),
149  pixmap.rowBytes());
150 }
#define FML_DCHECK(condition)
Definition: logging.h:86

◆ 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 98 of file image_descriptor.h.

References get_pixels().

Referenced by flutter::ImageFromCompressedData().

98  {
99  if (generator_) {
100  return generator_->GetScaledDimensions(scale);
101  }
102  return image_info_.dimensions();
103  }

◆ GetAllocationSize()

size_t flutter::ImageDescriptor::GetAllocationSize ( ) const
inlineoverridevirtual

Reimplemented from tonic::DartWrappable.

Definition at line 115 of file image_descriptor.h.

References buffer, image_info(), RegisterNatives(), and row_bytes().

115  {
116  return sizeof(ImageDescriptor) + sizeof(SkImageInfo) + buffer_->size();
117  }

◆ height()

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

The height of this image. EXIF oriented if applicable.

Definition at line 65 of file image_descriptor.h.

Referenced by should_resize().

65 { return image_info_.height(); }

◆ image()

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

Definition at line 142 of file image_descriptor.cc.

Referenced by data(), and flutter::ImageFromCompressedData().

142  {
143  return generator_->GetImage();
144 }

◆ image_info()

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

The orientation corrected image info for this image.

Definition at line 93 of file image_descriptor.h.

Referenced by GetAllocationSize(), flutter::ImageFromCompressedData(), flutter::ImageFromDecompressedData(), and RegisterNatives().

93 { return image_info_; }

◆ initEncoded()

void flutter::ImageDescriptor::initEncoded ( Dart_NativeArguments  args)
static

Asynchronously initlializes 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 56 of file image_descriptor.cc.

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

Referenced by RegisterNatives().

56  {
57  Dart_Handle callback_handle = Dart_GetNativeArgument(args, 2);
58  if (!Dart_IsClosure(callback_handle)) {
59  Dart_SetReturnValue(args, tonic::ToDart("Callback must be a function"));
60  return;
61  }
62 
63  Dart_Handle descriptor_handle = Dart_GetNativeArgument(args, 0);
64  ImmutableBuffer* immutable_buffer =
66  Dart_GetNativeArgument(args, 1));
67 
68  if (!immutable_buffer) {
69  Dart_SetReturnValue(args,
70  tonic::ToDart("Buffer parameter must not be null"));
71  return;
72  }
73 
74  // This has to be valid because this method is called from Dart.
75  auto dart_state = UIDartState::Current();
76  auto registry = dart_state->GetImageGeneratorRegistry();
77 
78  if (!registry) {
79  Dart_SetReturnValue(
80  args, tonic::ToDart("Failed to access the internal image decoder "
81  "registry on this isolate. Please file a bug on "
82  "https://github.com/flutter/flutter/issues."));
83  return;
84  }
85 
86  auto generator =
87  registry->CreateCompatibleGenerator(immutable_buffer->data());
88 
89  if (!generator) {
90  // No compatible image decoder was found.
91  Dart_SetReturnValue(args, tonic::ToDart("Invalid image data"));
92  return;
93  }
94 
95  auto descriptor = fml::MakeRefCounted<ImageDescriptor>(
96  immutable_buffer->data(), std::move(generator));
97 
98  FML_DCHECK(descriptor);
99 
100  descriptor->AssociateWithDartWrapper(descriptor_handle);
101  tonic::DartInvoke(callback_handle, {Dart_TypeVoid()});
102 }
G_BEGIN_DECLS FlValue * args
#define FML_DCHECK(condition)
Definition: logging.h:86
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)
Definition: dart_invoke.cc:20
Dart_Handle ToDart(const T &object)
static UIDartState * Current()

◆ initRaw()

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

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

Definition at line 104 of file image_descriptor.cc.

References color_type, FML_DCHECK, and row_bytes().

109  {
110  SkColorType color_type = kUnknown_SkColorType;
111  switch (pixel_format) {
112  case PixelFormat::kRGBA8888:
113  color_type = kRGBA_8888_SkColorType;
114  break;
115  case PixelFormat::kBGRA8888:
116  color_type = kBGRA_8888_SkColorType;
117  break;
118  }
119  FML_DCHECK(color_type != kUnknown_SkColorType);
120  auto image_info =
121  SkImageInfo::Make(width, height, color_type, kPremul_SkAlphaType);
122  auto descriptor = fml::MakeRefCounted<ImageDescriptor>(
123  data->data(), std::move(image_info),
124  row_bytes == -1 ? std::nullopt : std::optional<size_t>(row_bytes));
125  descriptor->AssociateWithDartWrapper(descriptor_handle);
126 }
#define FML_DCHECK(condition)
Definition: logging.h:86
uint32_t color_type
int row_bytes() const
The byte length of the first row of the image. Defaults to width() * 4.
const SkImageInfo & image_info() const
The orientation corrected image info for this image.
int height() const
The height of this image. EXIF oriented if applicable.
int width() const
The width of this image, EXIF oriented if applicable.

◆ instantiateCodec()

void flutter::ImageDescriptor::instantiateCodec ( Dart_Handle  codec,
int  target_width,
int  target_height 
)

Associates a flutter::Codec object with the dart.ui Codec handle.

Definition at line 128 of file image_descriptor.cc.

130  {
131  fml::RefPtr<Codec> ui_codec;
132  if (!generator_ || generator_->GetFrameCount() == 1) {
133  ui_codec = fml::MakeRefCounted<SingleFrameCodec>(
134  static_cast<fml::RefPtr<ImageDescriptor>>(this), target_width,
135  target_height);
136  } else {
137  ui_codec = fml::MakeRefCounted<MultiFrameCodec>(generator_);
138  }
139  ui_codec->AssociateWithDartWrapper(codec_handle);
140 }

◆ is_compressed()

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

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

Definition at line 90 of file image_descriptor.h.

90 { return !!generator_; }

◆ RegisterNatives()

void flutter::ImageDescriptor::RegisterNatives ( tonic::DartLibraryNatives natives)
static

Definition at line 30 of file image_descriptor.cc.

References buffer, DART_REGISTER_NATIVE, FML_DCHECK, FOR_EACH_BINDING, image_info(), initEncoded(), and row_bytes().

Referenced by GetAllocationSize(), and flutter::DartUI::InitForGlobal().

30  {
31  natives->Register(
32  {{"ImageDescriptor_initEncoded", ImageDescriptor::initEncoded, 3, true},
34 }
#define FOR_EACH_BINDING(V)
static void initEncoded(Dart_NativeArguments args)
Asynchronously initlializes an ImageDescriptor for an encoded image, as long as the format is recogni...
void Register(std::initializer_list< Entry > entries)
#define DART_REGISTER_NATIVE(CLASS, METHOD)

◆ 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 72 of file image_descriptor.h.

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

72  {
73  return row_bytes_.value_or(
74  static_cast<size_t>(image_info_.width() * image_info_.bytesPerPixel()));
75  }

◆ 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 79 of file image_descriptor.h.

References height(), and width().

Referenced by flutter::ImageFromCompressedData().

79  {
80  return target_width != width() || target_height != height();
81  }
int height() const
The height of this image. EXIF oriented if applicable.
int width() const
The width of this image, EXIF oriented if applicable.

◆ width()

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

The width of this image, EXIF oriented if applicable.

Definition at line 62 of file image_descriptor.h.

Referenced by should_resize().

62 { return image_info_.width(); }

Friends And Related Function Documentation

◆ ImageDecoderFixtureTest

friend class ImageDecoderFixtureTest
friend

Definition at line 139 of file image_descriptor.h.


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