Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
single_frame_codec.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
9
10namespace flutter {
11
13 const fml::RefPtr<ImageDescriptor>& descriptor,
14 uint32_t target_width,
15 uint32_t target_height,
17 : descriptor_(descriptor),
18 target_width_(target_width),
19 target_height_(target_height),
20 target_format_(target_format) {}
21
23
25 return 1;
26}
27
29 return 0;
30}
31
32Dart_Handle SingleFrameCodec::getNextFrame(Dart_Handle callback_handle) {
33 if (!Dart_IsClosure(callback_handle)) {
34 return tonic::ToDart("Callback must be a function");
35 }
36
37 if (status_ == Status::kComplete) {
38 if (!cached_image_->image()) {
39 return tonic::ToDart("Decoded image has been disposed");
40 }
41 tonic::DartInvoke(callback_handle, {tonic::ToDart(cached_image_),
43 return Dart_Null();
44 }
45
46 // This has to be valid because this method is called from Dart.
47 auto dart_state = UIDartState::Current();
48
49 pending_callbacks_.emplace_back(dart_state, callback_handle);
50
51 if (status_ == Status::kInProgress) {
52 // Another call to getNextFrame is in progress and will invoke the
53 // pending callbacks when decoding completes.
54 return Dart_Null();
55 }
56
57 auto decoder = dart_state->GetImageDecoder();
58
59 if (!decoder) {
60 return tonic::ToDart(
61 "Failed to access the internal image decoder "
62 "registry on this isolate. Please file a bug on "
63 "https://github.com/flutter/flutter/issues.");
64 }
65
66 // The SingleFrameCodec must be deleted on the UI thread. Allocate a RefPtr
67 // on the heap to ensure that the SingleFrameCodec remains alive until the
68 // decoder callback is invoked on the UI thread. The callback can then
69 // drop the reference.
70 fml::RefPtr<SingleFrameCodec>* raw_codec_ref =
72
73 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
74 decoder->Decode(
75 descriptor_,
76 {.target_width = target_width_,
77 .target_height = target_height_,
78 .target_format = target_format_},
79 [raw_codec_ref](const auto& image, const auto& decode_error) {
80 std::unique_ptr<fml::RefPtr<SingleFrameCodec>> codec_ref(raw_codec_ref);
81 fml::RefPtr<SingleFrameCodec> codec(std::move(*codec_ref));
82
83 auto state = codec->pending_callbacks_.front().dart_state().lock();
84
85 if (!state) {
86 // This is probably because the isolate has been terminated before the
87 // image could be decoded.
88
89 return;
90 }
91
92 tonic::DartState::Scope scope(state.get());
93
94 if (image) {
95 auto canvas_image = fml::MakeRefCounted<CanvasImage>();
96 canvas_image->set_image(std::move(image));
97
98 codec->cached_image_ = std::move(canvas_image);
99 }
100
101 // The cached frame is now available and should be returned to any
102 // future callers.
103 codec->status_ = Status::kComplete;
104
105 // Invoke any callbacks that were provided before the frame was decoded.
107 codec->pending_callbacks_) {
109 {tonic::ToDart(codec->cached_image_),
110 tonic::ToDart(0), tonic::ToDart(decode_error)});
111 }
112 codec->pending_callbacks_.clear();
113 });
114
115 // The encoded data is no longer needed now that it has been handed off
116 // to the decoder.
117 descriptor_ = nullptr;
118
119 status_ = Status::kInProgress;
120
121 return Dart_Null();
122}
123
124} // namespace flutter
int frameCount() const override
int repetitionCount() const override
SingleFrameCodec(const fml::RefPtr< ImageDescriptor > &descriptor, uint32_t target_width, uint32_t target_height, ImageDecoder::TargetPixelFormat destination_format)
Dart_Handle getNextFrame(Dart_Handle args) override
static UIDartState * Current()
FlutterVulkanImage * image
FlutterDesktopBinaryReply callback
Dart_Handle ToDart(const T &object)
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)