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
 
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 void RegisterNatives (tonic::DartLibraryNatives *natives)
 
static Dart_Handle RasterizeToImage (sk_sp< SkPicture > picture, uint32_t width, uint32_t height, Dart_Handle raw_image_callback)
 

Additional Inherited Members

- Public Types inherited from tonic::DartWrappable
enum  DartNativeFields {
  kPeerIndex,
  kWrapperInfoIndex,
  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 21 of file picture.h.

Constructor & Destructor Documentation

◆ ~Picture()

flutter::Picture::~Picture ( )
overridedefault

Referenced by Create().

Member Function Documentation

◆ Create()

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(), and ~Picture().

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 }

◆ dispose()

void flutter::Picture::dispose ( )

Definition at line 55 of file picture.cc.

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

55  {
56  picture_.reset();
58 }

◆ GetAllocationSize()

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

Reimplemented from tonic::DartWrappable.

Definition at line 60 of file picture.cc.

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

60  {
61  if (auto picture = picture_.get()) {
62  return picture->approximateBytesUsed() + sizeof(Picture);
63  } else {
64  return sizeof(Picture);
65  }
66 }
sk_sp< SkiaObjectType > get() const
sk_sp< SkPicture > picture() const
Definition: picture.h:30

◆ picture()

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

Definition at line 30 of file picture.h.

References height, and width.

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

30 { return picture_.get(); }
sk_sp< SkiaObjectType > get() const

◆ RasterizeToImage()

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

Definition at line 68 of file picture.cc.

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

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

71  {
72  if (Dart_IsNull(raw_image_callback) || !Dart_IsClosure(raw_image_callback)) {
73  return tonic::ToDart("Image callback was invalid");
74  }
75 
76  if (width == 0 || height == 0) {
77  return tonic::ToDart("Image dimensions for scene were invalid.");
78  }
79 
80  auto* dart_state = UIDartState::Current();
81  auto image_callback = std::make_unique<tonic::DartPersistentValue>(
82  dart_state, raw_image_callback);
83  auto unref_queue = dart_state->GetSkiaUnrefQueue();
84  auto ui_task_runner = dart_state->GetTaskRunners().GetUITaskRunner();
85  auto raster_task_runner = dart_state->GetTaskRunners().GetRasterTaskRunner();
86  auto snapshot_delegate = dart_state->GetSnapshotDelegate();
87 
88  // We can't create an image on this task runner because we don't have a
89  // graphics context. Even if we did, it would be slow anyway. Also, this
90  // thread owns the sole reference to the layer tree. So we flatten the layer
91  // tree into a picture and use that as the thread transport mechanism.
92 
93  auto picture_bounds = SkISize::Make(width, height);
94 
95  auto ui_task = fml::MakeCopyable([image_callback = std::move(image_callback),
96  unref_queue](
97  sk_sp<SkImage> raster_image) mutable {
98  auto dart_state = image_callback->dart_state().lock();
99  if (!dart_state) {
100  // The root isolate could have died in the meantime.
101  return;
102  }
103  tonic::DartState::Scope scope(dart_state);
104 
105  if (!raster_image) {
106  tonic::DartInvoke(image_callback->Get(), {Dart_Null()});
107  return;
108  }
109 
110  auto dart_image = CanvasImage::Create();
111  dart_image->set_image({std::move(raster_image), std::move(unref_queue)});
112  auto* raw_dart_image = tonic::ToDart(std::move(dart_image));
113 
114  // All done!
115  tonic::DartInvoke(image_callback->Get(), {raw_dart_image});
116 
117  // image_callback is associated with the Dart isolate and must be deleted
118  // on the UI thread.
119  image_callback.reset();
120  });
121 
122  // Kick things off on the raster rask runner.
124  raster_task_runner,
125  [ui_task_runner, snapshot_delegate, picture, picture_bounds, ui_task] {
126  sk_sp<SkImage> raster_image =
127  snapshot_delegate->MakeRasterSnapshot(picture, picture_bounds);
128 
130  ui_task_runner,
131  [ui_task, raster_image]() { ui_task(raster_image); });
132  });
133 
134  return Dart_Null();
135 }
static void RunNowOrPostTask(fml::RefPtr< fml::TaskRunner > runner, const fml::closure &task)
Definition: task_runner.cc:55
sk_sp< SkPicture > picture() const
Definition: picture.h:30
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 height
int32_t width
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 45 of file picture.cc.

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

47  {
48  if (!picture_.get()) {
49  return tonic::ToDart("Picture is null");
50  }
51 
52  return RasterizeToImage(picture_.get(), width, height, raw_image_callback);
53 }
sk_sp< SkiaObjectType > get() const
int32_t height
int32_t width
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:68

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