Flutter Engine
flutter::SingleFrameCodec Class Reference

#include <single_frame_codec.h>

Inheritance diagram for flutter::SingleFrameCodec:
flutter::Codec flutter::RefCountedDartWrappable< Codec > fml::RefCountedThreadSafe< Codec > tonic::DartWrappable fml::internal::RefCountedThreadSafeBase

Public Member Functions

 SingleFrameCodec (fml::RefPtr< ImageDescriptor > descriptor, uint32_t target_width, uint32_t target_height)
 
 ~SingleFrameCodec () override
 
int frameCount () const override
 
int repetitionCount () const override
 
Dart_Handle getNextFrame (Dart_Handle args) override
 
size_t GetAllocationSize () const override
 
- Public Member Functions inherited from flutter::Codec
void dispose ()
 
- Public Member Functions inherited from flutter::RefCountedDartWrappable< Codec >
virtual void RetainDartWrappableReference () const override
 
virtual void ReleaseDartWrappableReference () const override
 
- Public Member Functions inherited from fml::RefCountedThreadSafe< Codec >
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
 

Additional Inherited Members

- Public Types inherited from tonic::DartWrappable
enum  DartNativeFields {
  kPeerIndex,
  kWrapperInfoIndex,
  kNumberOfNativeFields
}
 
- Static Public Member Functions inherited from flutter::Codec
static void RegisterNatives (tonic::DartLibraryNatives *natives)
 
- Protected Member Functions inherited from fml::RefCountedThreadSafe< Codec >
 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

Definition at line 16 of file single_frame_codec.h.

Constructor & Destructor Documentation

◆ SingleFrameCodec()

flutter::SingleFrameCodec::SingleFrameCodec ( fml::RefPtr< ImageDescriptor descriptor,
uint32_t  target_width,
uint32_t  target_height 
)

Definition at line 12 of file single_frame_codec.cc.

References ~SingleFrameCodec().

15  : status_(Status::kNew),
16  descriptor_(std::move(descriptor)),
17  target_width_(target_width),
18  target_height_(target_height) {}

◆ ~SingleFrameCodec()

flutter::SingleFrameCodec::~SingleFrameCodec ( )
overridedefault

Referenced by SingleFrameCodec().

Member Function Documentation

◆ frameCount()

int flutter::SingleFrameCodec::frameCount ( ) const
overridevirtual

Implements flutter::Codec.

Definition at line 22 of file single_frame_codec.cc.

22  {
23  return 1;
24 }

◆ GetAllocationSize()

size_t flutter::SingleFrameCodec::GetAllocationSize ( ) const
overridevirtual

Reimplemented from tonic::DartWrappable.

Definition at line 110 of file single_frame_codec.cc.

110  {
111  const auto& data_size = descriptor_->GetAllocationSize();
112  const auto frame_byte_size =
113  cached_image_ ? cached_image_->GetAllocationSize() : 0;
114  return data_size + frame_byte_size + sizeof(this);
115 }

◆ getNextFrame()

Dart_Handle flutter::SingleFrameCodec::getNextFrame ( Dart_Handle  args)
overridevirtual

Implements flutter::Codec.

Definition at line 30 of file single_frame_codec.cc.

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

30  {
31  if (!Dart_IsClosure(callback_handle)) {
32  return tonic::ToDart("Callback must be a function");
33  }
34 
35  if (status_ == Status::kComplete) {
36  tonic::DartInvoke(callback_handle,
37  {tonic::ToDart(cached_image_), tonic::ToDart(0)});
38  return Dart_Null();
39  }
40 
41  // This has to be valid because this method is called from Dart.
42  auto dart_state = UIDartState::Current();
43 
44  pending_callbacks_.emplace_back(dart_state, callback_handle);
45 
46  if (status_ == Status::kInProgress) {
47  // Another call to getNextFrame is in progress and will invoke the
48  // pending callbacks when decoding completes.
49  return Dart_Null();
50  }
51 
52  auto decoder = dart_state->GetImageDecoder();
53 
54  if (!decoder) {
55  return tonic::ToDart("Image decoder not available.");
56  }
57 
58  // The SingleFrameCodec must be deleted on the UI thread. Allocate a RefPtr
59  // on the heap to ensure that the SingleFrameCodec remains alive until the
60  // decoder callback is invoked on the UI thread. The callback can then
61  // drop the reference.
62  fml::RefPtr<SingleFrameCodec>* raw_codec_ref =
64 
65  decoder->Decode(
66  descriptor_, target_width_, target_height_, [raw_codec_ref](auto image) {
67  std::unique_ptr<fml::RefPtr<SingleFrameCodec>> codec_ref(raw_codec_ref);
68  fml::RefPtr<SingleFrameCodec> codec(std::move(*codec_ref));
69 
70  auto state = codec->pending_callbacks_.front().dart_state().lock();
71 
72  if (!state) {
73  // This is probably because the isolate has been terminated before the
74  // image could be decoded.
75 
76  return;
77  }
78 
79  tonic::DartState::Scope scope(state.get());
80 
81  if (image.get()) {
82  auto canvas_image = fml::MakeRefCounted<CanvasImage>();
83  canvas_image->set_image(std::move(image));
84 
85  codec->cached_image_ = std::move(canvas_image);
86  }
87 
88  // The cached frame is now available and should be returned to any
89  // future callers.
90  codec->status_ = Status::kComplete;
91 
92  // Invoke any callbacks that were provided before the frame was decoded.
93  for (const DartPersistentValue& callback : codec->pending_callbacks_) {
95  callback.value(),
96  {tonic::ToDart(codec->cached_image_), tonic::ToDart(0)});
97  }
98  codec->pending_callbacks_.clear();
99  });
100 
101  // The encoded data is no longer needed now that it has been handed off
102  // to the decoder.
103  descriptor_ = nullptr;
104 
105  status_ = Status::kInProgress;
106 
107  return Dart_Null();
108 }
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()

◆ repetitionCount()

int flutter::SingleFrameCodec::repetitionCount ( ) const
overridevirtual

Implements flutter::Codec.

Definition at line 26 of file single_frame_codec.cc.

26  {
27  return 0;
28 }

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