Add Picture.toImage (flutter/engine#3657)
Fixes https://github.com/flutter/flutter/issues/6774
This commit is contained in:
@@ -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";
|
||||
|
||||
@@ -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<CanvasImage> Picture::toImage(int width, int height) {
|
||||
ftl::RefPtr<CanvasImage> 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();
|
||||
}
|
||||
|
||||
@@ -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<Picture>,
|
||||
|
||||
const sk_sp<SkPicture>& picture() const { return picture_; }
|
||||
|
||||
ftl::RefPtr<CanvasImage> toImage(int width, int height);
|
||||
|
||||
void dispose();
|
||||
|
||||
static void RegisterNatives(tonic::DartLibraryNatives* natives);
|
||||
|
||||
Reference in New Issue
Block a user