Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Member Functions | List of all members
flutter::Picture Class Reference

#include <picture.h>

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

Public Member Functions

 ~Picture () override
 
sk_sp< DisplayListdisplay_list () const
 
Dart_Handle toImage (uint32_t width, uint32_t height, Dart_Handle raw_image_callback)
 
void toImageSync (uint32_t width, uint32_t height, Dart_Handle raw_image_handle)
 
void dispose ()
 
size_t GetAllocationSize () const
 
- Public Member Functions inherited from flutter::RefCountedDartWrappable< Picture >
virtual void RetainDartWrappableReference () const override
 
virtual void ReleaseDartWrappableReference () const override
 
- Public Member Functions inherited from fml::RefCountedThreadSafe< Picture >
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
 
virtual void RetainDartWrappableReference () const =0
 
virtual void ReleaseDartWrappableReference () 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 CreateAndAssociateWithDartWrapper (Dart_Handle dart_handle, sk_sp< DisplayList > display_list)
 
static void RasterizeToImageSync (sk_sp< DisplayList > display_list, uint32_t width, uint32_t height, Dart_Handle raw_image_handle)
 
static Dart_Handle RasterizeToImage (const sk_sp< DisplayList > &display_list, uint32_t width, uint32_t height, Dart_Handle raw_image_callback)
 
static Dart_Handle RasterizeLayerTreeToImage (std::unique_ptr< LayerTree > layer_tree, Dart_Handle raw_image_callback)
 
static Dart_Handle DoRasterizeToImage (const sk_sp< DisplayList > &display_list, std::unique_ptr< LayerTree > layer_tree, uint32_t width, uint32_t height, Dart_Handle raw_image_callback)
 

Additional Inherited Members

- Public Types inherited from tonic::DartWrappable
enum  DartNativeFields { kPeerIndex , kNumberOfNativeFields }
 
- Protected Member Functions inherited from fml::RefCountedThreadSafe< Picture >
 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 17 of file picture.h.

Constructor & Destructor Documentation

◆ ~Picture()

flutter::Picture::~Picture ( )
overridedefault

Member Function Documentation

◆ CreateAndAssociateWithDartWrapper()

void flutter::Picture::CreateAndAssociateWithDartWrapper ( Dart_Handle  dart_handle,
sk_sp< DisplayList display_list 
)
static

Definition at line 29 of file picture.cc.

31 {
32 FML_DCHECK(display_list->isUIThreadSafe());
33 auto canvas_picture = fml::MakeRefCounted<Picture>(std::move(display_list));
34 canvas_picture->AssociateWithDartWrapper(dart_handle);
35}
sk_sp< DisplayList > display_list() const
Definition: picture.h:27
#define FML_DCHECK(condition)
Definition: logging.h:103

◆ display_list()

sk_sp< DisplayList > flutter::Picture::display_list ( ) const
inline

Definition at line 27 of file picture.h.

27{ return display_list_; }

◆ dispose()

void flutter::Picture::dispose ( )

Definition at line 108 of file picture.cc.

108 {
109 display_list_.reset();
111}

◆ DoRasterizeToImage()

Dart_Handle flutter::Picture::DoRasterizeToImage ( const sk_sp< DisplayList > &  display_list,
std::unique_ptr< LayerTree layer_tree,
uint32_t  width,
uint32_t  height,
Dart_Handle  raw_image_callback 
)
static

Definition at line 138 of file picture.cc.

142 {
143 // Either display_list or layer_tree should be provided.
144 FML_DCHECK((display_list == nullptr) != (layer_tree == nullptr));
145
146 if (Dart_IsNull(raw_image_callback) || !Dart_IsClosure(raw_image_callback)) {
147 return tonic::ToDart("Image callback was invalid");
148 }
149
150 if (width == 0 || height == 0) {
151 return tonic::ToDart("Image dimensions for scene were invalid.");
152 }
153
154 auto* dart_state = UIDartState::Current();
155 auto image_callback = std::make_unique<tonic::DartPersistentValue>(
156 dart_state, raw_image_callback);
157 auto unref_queue = dart_state->GetSkiaUnrefQueue();
158 auto ui_task_runner = dart_state->GetTaskRunners().GetUITaskRunner();
159 auto raster_task_runner = dart_state->GetTaskRunners().GetRasterTaskRunner();
160 auto snapshot_delegate = dart_state->GetSnapshotDelegate();
161
162 // We can't create an image on this task runner because we don't have a
163 // graphics context. Even if we did, it would be slow anyway. Also, this
164 // thread owns the sole reference to the layer tree. So we do it in the
165 // raster thread.
166
167 auto ui_task =
168 // The static leak checker gets confused by the use of fml::MakeCopyable.
169 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
170 fml::MakeCopyable([image_callback = std::move(image_callback),
171 unref_queue](sk_sp<DlImage> image) mutable {
172 auto dart_state = image_callback->dart_state().lock();
173 if (!dart_state) {
174 // The root isolate could have died in the meantime.
175 return;
176 }
177 tonic::DartState::Scope scope(dart_state);
178
179 if (!image) {
180 tonic::DartInvoke(image_callback->Get(), {Dart_Null()});
181 return;
182 }
183
184 if (!image->isUIThreadSafe()) {
185 // All images with impeller textures should already be safe.
186 FML_DCHECK(image->impeller_texture() == nullptr);
187 image =
188 DlImageGPU::Make({image->skia_image(), std::move(unref_queue)});
189 }
190
191 auto dart_image = CanvasImage::Create();
192 dart_image->set_image(image);
193 auto* raw_dart_image = tonic::ToDart(dart_image);
194
195 // All done!
196 tonic::DartInvoke(image_callback->Get(), {raw_dart_image});
197
198 // image_callback is associated with the Dart isolate and must be
199 // deleted on the UI thread.
200 image_callback.reset();
201 });
202
203 // Kick things off on the raster rask runner.
205 raster_task_runner,
206 fml::MakeCopyable([ui_task_runner, snapshot_delegate, display_list, width,
207 height, ui_task,
208 layer_tree = std::move(layer_tree)]() mutable {
209 auto picture_bounds = SkISize::Make(width, height);
211 sk_sp<DisplayList> snapshot_display_list = display_list;
212 if (layer_tree) {
213 FML_DCHECK(picture_bounds == layer_tree->frame_size());
214 snapshot_display_list =
215 layer_tree->Flatten(SkRect::MakeWH(width, height),
216 snapshot_delegate->GetTextureRegistry(),
217 snapshot_delegate->GetGrContext());
218 }
219 snapshot_delegate->MakeRasterSnapshot(
220 snapshot_display_list, picture_bounds,
221 [ui_task_runner, ui_task](const sk_sp<DlImage>& image) {
223 ui_task_runner, [ui_task, image]() { ui_task(image); });
224 });
225 }));
226
227 return Dart_Null();
228}
static fml::RefPtr< CanvasImage > Create()
Definition: image.h:28
static sk_sp< DlImageGPU > Make(SkiaGPUObject< SkImage > image)
static UIDartState * Current()
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
Definition: task_runner.cc:55
DART_EXPORT Dart_Handle Dart_Null(void)
DART_EXPORT bool Dart_IsNull(Dart_Handle object)
DART_EXPORT bool Dart_IsClosure(Dart_Handle object)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57
Dart_Handle ToDart(const T &object)
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)
Definition: dart_invoke.cc:20
int32_t height
int32_t width
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609

◆ GetAllocationSize()

size_t flutter::Picture::GetAllocationSize ( ) const

Definition at line 113 of file picture.cc.

113 {
114 if (display_list_) {
115 return display_list_->bytes() + sizeof(Picture);
116 } else {
117 return sizeof(Picture);
118 }
119}

◆ RasterizeLayerTreeToImage()

Dart_Handle flutter::Picture::RasterizeLayerTreeToImage ( std::unique_ptr< LayerTree layer_tree,
Dart_Handle  raw_image_callback 
)
static

Definition at line 129 of file picture.cc.

131 {
132 FML_DCHECK(layer_tree != nullptr);
133 auto frame_size = layer_tree->frame_size();
134 return DoRasterizeToImage(nullptr, std::move(layer_tree), frame_size.width(),
135 frame_size.height(), raw_image_callback);
136}
static Dart_Handle DoRasterizeToImage(const sk_sp< DisplayList > &display_list, std::unique_ptr< LayerTree > layer_tree, uint32_t width, uint32_t height, Dart_Handle raw_image_callback)
Definition: picture.cc:138

◆ RasterizeToImage()

Dart_Handle flutter::Picture::RasterizeToImage ( const sk_sp< DisplayList > &  display_list,
uint32_t  width,
uint32_t  height,
Dart_Handle  raw_image_callback 
)
static

Definition at line 121 of file picture.cc.

124 {
126 raw_image_callback);
127}

◆ RasterizeToImageSync()

void flutter::Picture::RasterizeToImageSync ( sk_sp< DisplayList display_list,
uint32_t  width,
uint32_t  height,
Dart_Handle  raw_image_handle 
)
static

Definition at line 87 of file picture.cc.

90 {
91 auto* dart_state = UIDartState::Current();
92 if (!dart_state) {
93 return;
94 }
95 auto unref_queue = dart_state->GetSkiaUnrefQueue();
96 auto snapshot_delegate = dart_state->GetSnapshotDelegate();
97 auto raster_task_runner = dart_state->GetTaskRunners().GetRasterTaskRunner();
98
100 auto dl_image = CreateDeferredImage(
101 dart_state->IsImpellerEnabled(), std::move(display_list), width, height,
102 std::move(snapshot_delegate), std::move(raster_task_runner),
103 std::move(unref_queue));
104 image->set_image(dl_image);
105 image->AssociateWithDartWrapper(raw_image_handle);
106}
static sk_sp< DlImage > CreateDeferredImage(bool impeller, std::unique_ptr< LayerTree > layer_tree, fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > snapshot_delegate, fml::RefPtr< fml::TaskRunner > raster_task_runner, fml::RefPtr< SkiaUnrefQueue > unref_queue)
Definition: scene.cc:74

◆ toImage()

Dart_Handle flutter::Picture::toImage ( uint32_t  width,
uint32_t  height,
Dart_Handle  raw_image_callback 
)

Definition at line 42 of file picture.cc.

44 {
45 if (!display_list_) {
46 return tonic::ToDart("Picture is null");
47 }
48 return RasterizeToImage(display_list_, width, height, raw_image_callback);
49}
static Dart_Handle RasterizeToImage(const sk_sp< DisplayList > &display_list, uint32_t width, uint32_t height, Dart_Handle raw_image_callback)
Definition: picture.cc:121

◆ toImageSync()

void flutter::Picture::toImageSync ( uint32_t  width,
uint32_t  height,
Dart_Handle  raw_image_handle 
)

Definition at line 51 of file picture.cc.

53 {
54 FML_DCHECK(display_list_);
55 RasterizeToImageSync(display_list_, width, height, raw_image_handle);
56}
static void RasterizeToImageSync(sk_sp< DisplayList > display_list, uint32_t width, uint32_t height, Dart_Handle raw_image_handle)
Definition: picture.cc:87

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