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,
  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 113 of file single_frame_codec.cc.

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

113  {
114  return 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 callback, flutter::UIDartState::Current(), tonic::DartInvoke(), state, 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(
56  "Failed to access the internal image decoder "
57  "registry on this isolate. Please file a bug on "
58  "https://github.com/flutter/flutter/issues.");
59  }
60 
61  // The SingleFrameCodec must be deleted on the UI thread. Allocate a RefPtr
62  // on the heap to ensure that the SingleFrameCodec remains alive until the
63  // decoder callback is invoked on the UI thread. The callback can then
64  // drop the reference.
65  fml::RefPtr<SingleFrameCodec>* raw_codec_ref =
67 
68  decoder->Decode(
69  descriptor_, target_width_, target_height_, [raw_codec_ref](auto image) {
70  std::unique_ptr<fml::RefPtr<SingleFrameCodec>> codec_ref(raw_codec_ref);
71  fml::RefPtr<SingleFrameCodec> codec(std::move(*codec_ref));
72 
73  auto state = codec->pending_callbacks_.front().dart_state().lock();
74 
75  if (!state) {
76  // This is probably because the isolate has been terminated before the
77  // image could be decoded.
78 
79  return;
80  }
81 
82  tonic::DartState::Scope scope(state.get());
83 
84  if (image.skia_object()) {
85  auto canvas_image = fml::MakeRefCounted<CanvasImage>();
86  canvas_image->set_image(std::move(image));
87 
88  codec->cached_image_ = std::move(canvas_image);
89  }
90 
91  // The cached frame is now available and should be returned to any
92  // future callers.
93  codec->status_ = Status::kComplete;
94 
95  // Invoke any callbacks that were provided before the frame was decoded.
96  for (const DartPersistentValue& callback : codec->pending_callbacks_) {
98  callback.value(),
99  {tonic::ToDart(codec->cached_image_), tonic::ToDart(0)});
100  }
101  codec->pending_callbacks_.clear();
102  });
103 
104  // The encoded data is no longer needed now that it has been handed off
105  // to the decoder.
106  descriptor_ = nullptr;
107 
108  status_ = Status::kInProgress;
109 
110  return Dart_Null();
111 }
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)
Definition: dart_invoke.cc:20
FlKeyEvent FlKeyResponderAsyncCallback callback
Dart_Handle ToDart(const T &object)
AtkStateType state
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: