Flutter Engine
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< SkPicture > picture () const
 
sk_sp< DisplayListdisplay_list () const
 
Dart_Handle toImage (uint32_t width, uint32_t height, Dart_Handle raw_image_callback)
 
void dispose ()
 
size_t GetAllocationSize () const override
 
- 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
 
Dart_Handle CreateDartWrapper (DartState *dart_state)
 
void AssociateWithDartWrapper (Dart_Handle wrappable)
 
void ClearDartWrapper ()
 
Dart_WeakPersistentHandle dart_wrapper () const
 

Static Public Member Functions

static fml::RefPtr< PictureCreate (Dart_Handle dart_handle, flutter::SkiaGPUObject< SkPicture > picture)
 
static fml::RefPtr< PictureCreate (Dart_Handle dart_handle, flutter::SkiaGPUObject< DisplayList > display_list)
 
static void RegisterNatives (tonic::DartLibraryNatives *natives)
 
static Dart_Handle RasterizeToImage (sk_sp< SkPicture > picture, uint32_t width, uint32_t height, Dart_Handle raw_image_callback)
 
static Dart_Handle RasterizeToImage (std::function< void(SkCanvas *)> draw_callback, 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 22 of file picture.h.

Constructor & Destructor Documentation

◆ ~Picture()

flutter::Picture::~Picture ( )
overridedefault

Referenced by Create().

Member Function Documentation

◆ Create() [1/2]

fml::RefPtr< Picture > flutter::Picture::Create ( Dart_Handle  dart_handle,
flutter::SkiaGPUObject< SkPicture >  picture 
)
static

Definition at line 31 of file picture.cc.

References tonic::DartWrappable::AssociateWithDartWrapper().

Referenced by flutter::PictureRecorder::endRecording().

33  {
34  auto canvas_picture = fml::MakeRefCounted<Picture>(std::move(picture));
35 
36  canvas_picture->AssociateWithDartWrapper(dart_handle);
37  return canvas_picture;
38 }

◆ Create() [2/2]

fml::RefPtr< Picture > flutter::Picture::Create ( Dart_Handle  dart_handle,
flutter::SkiaGPUObject< DisplayList display_list 
)
static

Definition at line 40 of file picture.cc.

References display_list(), picture(), and ~Picture().

42  {
43  auto canvas_picture = fml::MakeRefCounted<Picture>(std::move(display_list));
44 
45  canvas_picture->AssociateWithDartWrapper(dart_handle);
46  return canvas_picture;
47 }

◆ display_list()

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

Definition at line 35 of file picture.h.

References function, height, and width.

Referenced by flutter::SceneBuilder::addPicture(), Create(), and flutter::Canvas::drawPicture().

35  {
36  return display_list_.skia_object();
37  }

◆ dispose()

void flutter::Picture::dispose ( )

Definition at line 75 of file picture.cc.

References tonic::DartWrappable::ClearDartWrapper(), and flutter::SkiaGPUObject< T >::reset().

75  {
76  picture_.reset();
77  display_list_.reset();
79 }

◆ GetAllocationSize()

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

Reimplemented from tonic::DartWrappable.

Definition at line 81 of file picture.cc.

References flutter::SkiaGPUObject< T >::skia_object().

81  {
82  if (auto picture = picture_.skia_object()) {
83  return picture->approximateBytesUsed() + sizeof(Picture);
84  } else if (auto display_list = display_list_.skia_object()) {
85  return display_list->bytes() + sizeof(Picture);
86  } else {
87  return sizeof(Picture);
88  }
89 }
sk_sp< SkPicture > picture() const
Definition: picture.h:34
sk_sp< DisplayList > display_list() const
Definition: picture.h:35
sk_sp< SkiaObjectType > skia_object() const

◆ picture()

sk_sp<SkPicture> flutter::Picture::picture ( ) const
inline

Definition at line 34 of file picture.h.

Referenced by flutter::SceneBuilder::addPicture(), Create(), flutter::Canvas::drawPicture(), and flutter::ImageFilter::initPicture().

34 { return picture_.skia_object(); }
sk_sp< SkiaObjectType > skia_object() const

◆ RasterizeToImage() [1/2]

Dart_Handle flutter::Picture::RasterizeToImage ( sk_sp< SkPicture >  picture,
uint32_t  width,
uint32_t  height,
Dart_Handle  raw_image_callback 
)
static

Definition at line 91 of file picture.cc.

References height, and width.

Referenced by flutter::Scene::toImage(), and toImage().

94  {
95  return RasterizeToImage(
96  [picture](SkCanvas* canvas) { canvas->drawPicture(picture); }, width,
97  height, raw_image_callback);
98 }
sk_sp< SkPicture > picture() const
Definition: picture.h:34
int32_t width
int32_t height
static Dart_Handle RasterizeToImage(sk_sp< SkPicture > picture, uint32_t width, uint32_t height, Dart_Handle raw_image_callback)
Definition: picture.cc:91

◆ RasterizeToImage() [2/2]

Dart_Handle flutter::Picture::RasterizeToImage ( std::function< void(SkCanvas *)>  draw_callback,
uint32_t  width,
uint32_t  height,
Dart_Handle  raw_image_callback 
)
static

Definition at line 100 of file picture.cc.

References flutter::CanvasImage::Create(), flutter::UIDartState::Current(), tonic::DartInvoke(), fml::MakeCopyable(), fml::TaskRunner::RunNowOrPostTask(), and tonic::ToDart().

104  {
105  if (Dart_IsNull(raw_image_callback) || !Dart_IsClosure(raw_image_callback)) {
106  return tonic::ToDart("Image callback was invalid");
107  }
108 
109  if (width == 0 || height == 0) {
110  return tonic::ToDart("Image dimensions for scene were invalid.");
111  }
112 
113  auto* dart_state = UIDartState::Current();
114  auto image_callback = std::make_unique<tonic::DartPersistentValue>(
115  dart_state, raw_image_callback);
116  auto unref_queue = dart_state->GetSkiaUnrefQueue();
117  auto ui_task_runner = dart_state->GetTaskRunners().GetUITaskRunner();
118  auto raster_task_runner = dart_state->GetTaskRunners().GetRasterTaskRunner();
119  auto snapshot_delegate = dart_state->GetSnapshotDelegate();
120 
121  // We can't create an image on this task runner because we don't have a
122  // graphics context. Even if we did, it would be slow anyway. Also, this
123  // thread owns the sole reference to the layer tree. So we flatten the layer
124  // tree into a picture and use that as the thread transport mechanism.
125 
126  auto picture_bounds = SkISize::Make(width, height);
127 
128  auto ui_task = fml::MakeCopyable([image_callback = std::move(image_callback),
129  unref_queue](
130  sk_sp<SkImage> raster_image) mutable {
131  auto dart_state = image_callback->dart_state().lock();
132  if (!dart_state) {
133  // The root isolate could have died in the meantime.
134  return;
135  }
136  tonic::DartState::Scope scope(dart_state);
137 
138  if (!raster_image) {
139  tonic::DartInvoke(image_callback->Get(), {Dart_Null()});
140  return;
141  }
142 
143  auto dart_image = CanvasImage::Create();
144  dart_image->set_image({std::move(raster_image), std::move(unref_queue)});
145  auto* raw_dart_image = tonic::ToDart(std::move(dart_image));
146 
147  // All done!
148  tonic::DartInvoke(image_callback->Get(), {raw_dart_image});
149 
150  // image_callback is associated with the Dart isolate and must be deleted
151  // on the UI thread.
152  image_callback.reset();
153  });
154 
155  // Kick things off on the raster rask runner.
157  raster_task_runner, [ui_task_runner, snapshot_delegate, draw_callback,
158  picture_bounds, ui_task] {
159  sk_sp<SkImage> raster_image = snapshot_delegate->MakeRasterSnapshot(
160  draw_callback, picture_bounds);
161 
163  ui_task_runner,
164  [ui_task, raster_image]() { ui_task(raster_image); });
165  });
166 
167  return Dart_Null();
168 }
static void RunNowOrPostTask(fml::RefPtr< fml::TaskRunner > runner, const fml::closure &task)
Definition: task_runner.cc:55
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)
Definition: dart_invoke.cc:20
static fml::RefPtr< CanvasImage > Create()
Definition: image.h:25
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57
int32_t width
int32_t height
Dart_Handle ToDart(const T &object)
static UIDartState * Current()

◆ RegisterNatives()

static void flutter::Picture::RegisterNatives ( tonic::DartLibraryNatives natives)
static

◆ toImage()

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

Definition at line 57 of file picture.cc.

References height, RasterizeToImage(), flutter::SkiaGPUObject< T >::skia_object(), tonic::ToDart(), and width.

59  {
60  if (display_list_.skia_object()) {
61  return RasterizeToImage(
62  [display_list = display_list_.skia_object()](SkCanvas* canvas) {
63  display_list->RenderTo(canvas);
64  },
65  width, height, raw_image_callback);
66  } else {
67  if (!picture_.skia_object()) {
68  return tonic::ToDart("Picture is null");
69  }
70  return RasterizeToImage(picture_.skia_object(), width, height,
71  raw_image_callback);
72  }
73 }
sk_sp< DisplayList > display_list() const
Definition: picture.h:35
sk_sp< SkiaObjectType > skia_object() const
int32_t width
int32_t height
Dart_Handle ToDart(const T &object)
static Dart_Handle RasterizeToImage(sk_sp< SkPicture > picture, uint32_t width, uint32_t height, Dart_Handle raw_image_callback)
Definition: picture.cc:91

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