Flutter Engine
flutter::ImageDescriptor Class Reference

#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,
  kWrapperInfoIndex,
  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
 
bool should_resize (int target_width, int target_height) const
 
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)
 
bool get_pixels (const SkPixmap &pixmap) const
 
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)
 
static void initRaw (Dart_Handle descriptor_handle, fml::RefPtr< ImmutableBuffer > data, int width, int height, int row_bytes, PixelFormat pixel_format)
 
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 SkCodec and SkImageGenerator for the data. The Codec initialization actually happens in initEncoded, making initstantiateCodec a lightweight operation.

Definition at line 30 of file image_descriptor.h.

Member Enumeration Documentation

◆ PixelFormat

Enumerator
kRGBA8888 
kBGRA8888 

Definition at line 35 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 66 of file image_descriptor.h.

66 { return image_info_.bytesPerPixel(); }

◆ data()

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

The underlying buffer for this image.

Definition at line 83 of file image_descriptor.h.

References image().

83 { return buffer_; }

◆ dispose()

void flutter::ImageDescriptor::dispose ( )
inline

Definition at line 106 of file image_descriptor.h.

References tonic::DartWrappable::ClearDartWrapper().

106  {
108  generator_.reset();
109  platform_image_generator_.reset();
110  }

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

References FML_DCHECK.

Referenced by get_scaled_dimensions(), and image().

188  {
189  if (generator_) {
190  return generator_->getPixels(pixmap.info(), pixmap.writable_addr(),
191  pixmap.rowBytes());
192  }
193  FML_DCHECK(platform_image_generator_);
194  return platform_image_generator_->getPixels(pixmap);
195 }
#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 a codec that can perform efficient subpixel scaling.

Definition at line 95 of file image_descriptor.h.

References get_pixels().

95  {
96  if (generator_) {
97  return generator_->getScaledDimensions(scale);
98  }
99  return image_info_.dimensions();
100  }

◆ GetAllocationSize()

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

Reimplemented from tonic::DartWrappable.

Definition at line 112 of file image_descriptor.h.

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

112  {
113  return sizeof(ImageDescriptor) + sizeof(SkImageInfo) + buffer_->size();
114  }

◆ height()

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

The height of this image. EXIF oriented if applicable.

Definition at line 63 of file image_descriptor.h.

Referenced by should_resize().

63 { return image_info_.height(); }

◆ image()

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

Definition at line 171 of file image_descriptor.cc.

References FML_LOG, and get_pixels().

Referenced by data().

171  {
172  SkBitmap bitmap;
173  if (!bitmap.tryAllocPixels(image_info_)) {
174  FML_LOG(ERROR) << "Failed to allocate memory for bitmap of size "
175  << image_info_.computeMinByteSize() << "B";
176  return nullptr;
177  }
178 
179  const auto& pixmap = bitmap.pixmap();
180  if (!get_pixels(pixmap)) {
181  FML_LOG(ERROR) << "Failed to get pixels for image.";
182  return nullptr;
183  }
184  bitmap.setImmutable();
185  return SkImage::MakeFromBitmap(bitmap);
186 }
#define FML_LOG(severity)
Definition: logging.h:65
bool get_pixels(const SkPixmap &pixmap) const

◆ image_info()

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

The orientation corrected image info for this image.

Definition at line 91 of file image_descriptor.h.

Referenced by GetAllocationSize(), and RegisterNatives().

91 { 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 supported by Skia.

Calling this method will result in creating an SkCodec and SkImageGenerator to read EXIF corrected dimensions from the image data.

Definition at line 88 of file image_descriptor.cc.

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

Referenced by RegisterNatives().

88  {
89  Dart_Handle callback_handle = Dart_GetNativeArgument(args, 2);
90  if (!Dart_IsClosure(callback_handle)) {
91  Dart_SetReturnValue(args, tonic::ToDart("Callback must be a function"));
92  return;
93  }
94 
95  Dart_Handle descriptor_handle = Dart_GetNativeArgument(args, 0);
96  ImmutableBuffer* immutable_buffer =
98  Dart_GetNativeArgument(args, 1));
99 
100  if (!immutable_buffer) {
101  Dart_SetReturnValue(args,
102  tonic::ToDart("Buffer parameter must not be null"));
103  return;
104  }
105 
106  // This call will succeed if Skia has a built-in codec for this.
107  // If it fails, we will check if the platform knows how to decode this image.
108  std::unique_ptr<SkCodec> codec =
109  SkCodec::MakeFromData(immutable_buffer->data());
110  fml::RefPtr<ImageDescriptor> descriptor;
111  if (!codec) {
112  std::unique_ptr<SkImageGenerator> generator =
113  PLATFORM_IMAGE_GENERATOR(immutable_buffer->data());
114  if (!generator) {
115  // We don't have a Skia codec for this image, and the platform doesn't
116  // know how to decode it.
117  Dart_SetReturnValue(args, tonic::ToDart("Invalid image data"));
118  return;
119  }
120  descriptor = fml::MakeRefCounted<ImageDescriptor>(immutable_buffer->data(),
121  std::move(generator));
122  } else {
123  descriptor = fml::MakeRefCounted<ImageDescriptor>(immutable_buffer->data(),
124  std::move(codec));
125  }
126 
127  FML_DCHECK(descriptor);
128 
129  descriptor->AssociateWithDartWrapper(descriptor_handle);
130  tonic::DartInvoke(callback_handle, {Dart_TypeVoid()});
131 }
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
#define PLATFORM_IMAGE_GENERATOR(data)
Dart_Handle ToDart(const T &object)

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

References color_type, FML_DCHECK, and row_bytes().

138  {
139  SkColorType color_type = kUnknown_SkColorType;
140  switch (pixel_format) {
141  case PixelFormat::kRGBA8888:
142  color_type = kRGBA_8888_SkColorType;
143  break;
144  case PixelFormat::kBGRA8888:
145  color_type = kBGRA_8888_SkColorType;
146  break;
147  }
148  FML_DCHECK(color_type != kUnknown_SkColorType);
149  auto image_info =
150  SkImageInfo::Make(width, height, color_type, kPremul_SkAlphaType);
151  auto descriptor = fml::MakeRefCounted<ImageDescriptor>(
152  data->data(), std::move(image_info),
153  row_bytes == -1 ? std::nullopt : std::optional<size_t>(row_bytes));
154  descriptor->AssociateWithDartWrapper(descriptor_handle);
155 }
#define FML_DCHECK(condition)
Definition: logging.h:86
uint32_t color_type
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 157 of file image_descriptor.cc.

159  {
160  fml::RefPtr<Codec> ui_codec;
161  if (!generator_ || generator_->getFrameCount() == 1) {
162  ui_codec = fml::MakeRefCounted<SingleFrameCodec>(
163  static_cast<fml::RefPtr<ImageDescriptor>>(this), target_width,
164  target_height);
165  } else {
166  ui_codec = fml::MakeRefCounted<MultiFrameCodec>(generator_);
167  }
168  ui_codec->AssociateWithDartWrapper(codec_handle);
169 }

◆ is_compressed()

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

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

Definition at line 88 of file image_descriptor.h.

88 { return generator_ || platform_image_generator_; }

◆ RegisterNatives()

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

Definition at line 44 of file image_descriptor.cc.

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

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

44  {
45  natives->Register(
46  {{"ImageDescriptor_initEncoded", ImageDescriptor::initEncoded, 3, true},
48 }
#define FOR_EACH_BINDING(V)
static void initEncoded(Dart_NativeArguments args)
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 71 of file image_descriptor.h.

Referenced by GetAllocationSize(), initRaw(), and RegisterNatives().

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

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

References height(), and width().

78  {
79  return target_width != width() || target_height != height();
80  }
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 60 of file image_descriptor.h.

Referenced by should_resize().

60 { return image_info_.width(); }

Friends And Related Function Documentation

◆ ImageDecoderFixtureTest

friend class ImageDecoderFixtureTest
friend

Definition at line 138 of file image_descriptor.h.


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