diff --git a/engine/src/flutter/lib/ui/painting.dart b/engine/src/flutter/lib/ui/painting.dart index 4eb52748c4..14f80f9db8 100644 --- a/engine/src/flutter/lib/ui/painting.dart +++ b/engine/src/flutter/lib/ui/painting.dart @@ -1649,6 +1649,15 @@ abstract class Picture extends NativeFieldWrapperClass2 { /// object. To create a Picture object, use a [PictureRecorder]. Picture(); // (this constructor is here just so we can document it) + /// Creates an image from this picture. + /// + /// The picture is rasterized using the number of pixels specified by the + /// given width and height. + /// + /// Although the image is returned synchronously, the picture is actually + /// rasterized the first time the image is drawn and then cached. + Image toImage(int width, int height) native "Picture_toImage"; + /// Release the resources used by this object. The object is no longer usable /// after this method is called. void dispose() native "Picture_dispose"; diff --git a/engine/src/flutter/lib/ui/painting/picture.cc b/engine/src/flutter/lib/ui/painting/picture.cc index 9bcee7b6ca..a63970dadf 100644 --- a/engine/src/flutter/lib/ui/painting/picture.cc +++ b/engine/src/flutter/lib/ui/painting/picture.cc @@ -7,16 +7,19 @@ #include "flutter/common/threads.h" #include "flutter/lib/ui/painting/canvas.h" #include "flutter/lib/ui/painting/utils.h" +#include "lib/tonic/converter/dart_converter.h" #include "lib/tonic/dart_args.h" #include "lib/tonic/dart_binding_macros.h" -#include "lib/tonic/converter/dart_converter.h" #include "lib/tonic/dart_library_natives.h" +#include "third_party/skia/include/core/SkImage.h" namespace blink { IMPLEMENT_WRAPPERTYPEINFO(ui, Picture); -#define FOR_EACH_BINDING(V) V(Picture, dispose) +#define FOR_EACH_BINDING(V) \ + V(Picture, toImage) \ + V(Picture, dispose) DART_BIND_ALL(Picture, FOR_EACH_BINDING) @@ -32,6 +35,15 @@ Picture::~Picture() { SkiaUnrefOnIOThread(&picture_); } +ftl::RefPtr Picture::toImage(int width, int height) { + ftl::RefPtr image = CanvasImage::Create(); + // TODO(abarth): We should pass in an SkColorSpace at some point. + image->set_image( + SkImage::MakeFromPicture(picture_, SkISize::Make(width, height), nullptr, + nullptr, SkImage::BitDepth::kU8, nullptr)); + return image; +} + void Picture::dispose() { ClearDartWrapper(); } diff --git a/engine/src/flutter/lib/ui/painting/picture.h b/engine/src/flutter/lib/ui/painting/picture.h index 99c747e517..96e4e2d7ec 100644 --- a/engine/src/flutter/lib/ui/painting/picture.h +++ b/engine/src/flutter/lib/ui/painting/picture.h @@ -5,6 +5,7 @@ #ifndef FLUTTER_LIB_UI_PAINTING_PICTURE_H_ #define FLUTTER_LIB_UI_PAINTING_PICTURE_H_ +#include "flutter/lib/ui/painting/image.h" #include "lib/tonic/dart_wrappable.h" #include "third_party/skia/include/core/SkPicture.h" @@ -26,6 +27,8 @@ class Picture : public ftl::RefCountedThreadSafe, const sk_sp& picture() const { return picture_; } + ftl::RefPtr toImage(int width, int height); + void dispose(); static void RegisterNatives(tonic::DartLibraryNatives* natives);