Flutter Engine
The Flutter Engine
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
5#include "flutter/lib/ui/painting/single_frame_codec.h"
6
7#include "flutter/lib/ui/ui_dart_state.h"
9
10namespace flutter {
11
13 const fml::RefPtr<ImageDescriptor>& descriptor,
14 uint32_t target_width,
15 uint32_t target_height)
16 : descriptor_(descriptor),
17 target_width_(target_width),
18 target_height_(target_height) {}
19
21
23 return 1;
24}
25
27 return 0;
28}
29
31 if (!Dart_IsClosure(callback_handle)) {
32 return tonic::ToDart("Callback must be a function");
33 }
34
35 if (status_ == Status::kComplete) {
36 if (!cached_image_->image()) {
37 return tonic::ToDart("Decoded image has been disposed");
38 }
39 tonic::DartInvoke(callback_handle, {tonic::ToDart(cached_image_),
41 return Dart_Null();
42 }
43
44 // This has to be valid because this method is called from Dart.
45 auto dart_state = UIDartState::Current();
46
47 pending_callbacks_.emplace_back(dart_state, callback_handle);
48
49 if (status_ == Status::kInProgress) {
50 // Another call to getNextFrame is in progress and will invoke the
51 // pending callbacks when decoding completes.
52 return Dart_Null();
53 }
54
55 auto decoder = dart_state->GetImageDecoder();
56
57 if (!decoder) {
58 return tonic::ToDart(
59 "Failed to access the internal image decoder "
60 "registry on this isolate. Please file a bug on "
61 "https://github.com/flutter/flutter/issues.");
62 }
63
64 // The SingleFrameCodec must be deleted on the UI thread. Allocate a RefPtr
65 // on the heap to ensure that the SingleFrameCodec remains alive until the
66 // decoder callback is invoked on the UI thread. The callback can then
67 // drop the reference.
68 fml::RefPtr<SingleFrameCodec>* raw_codec_ref =
70
71 decoder->Decode(
72 descriptor_, target_width_, target_height_,
73 [raw_codec_ref](auto image, auto decode_error) {
74 std::unique_ptr<fml::RefPtr<SingleFrameCodec>> codec_ref(raw_codec_ref);
75 fml::RefPtr<SingleFrameCodec> codec(std::move(*codec_ref));
76
77 auto state = codec->pending_callbacks_.front().dart_state().lock();
78
79 if (!state) {
80 // This is probably because the isolate has been terminated before the
81 // image could be decoded.
82
83 return;
84 }
85
86 tonic::DartState::Scope scope(state.get());
87
88 if (image) {
89 auto canvas_image = fml::MakeRefCounted<CanvasImage>();
90 canvas_image->set_image(std::move(image));
91
92 codec->cached_image_ = std::move(canvas_image);
93 }
94
95 // The cached frame is now available and should be returned to any
96 // future callers.
97 codec->status_ = Status::kComplete;
98
99 // Invoke any callbacks that were provided before the frame was decoded.
101 codec->pending_callbacks_) {
103 {tonic::ToDart(codec->cached_image_),
104 tonic::ToDart(0), tonic::ToDart(decode_error)});
105 }
106 codec->pending_callbacks_.clear();
107 });
108
109 // The encoded data is no longer needed now that it has been handed off
110 // to the decoder.
111 descriptor_ = nullptr;
112
113 status_ = Status::kInProgress;
114
115 return Dart_Null();
116}
117
118} // namespace flutter
SingleFrameCodec(const fml::RefPtr< ImageDescriptor > &descriptor, uint32_t target_width, uint32_t target_height)
int frameCount() const override
int repetitionCount() const override
Dart_Handle getNextFrame(Dart_Handle args) override
static UIDartState * Current()
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT Dart_Handle Dart_Null(void)
DART_EXPORT bool Dart_IsClosure(Dart_Handle object)
AtkStateType state
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
sk_sp< const SkImage > image
Definition: SkRecords.h:269
Dart_Handle ToDart(const T &object)
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)
Definition: dart_invoke.cc:20