Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,8 @@ FILE: ../../../flutter/lib/ui/painting/codec.cc
FILE: ../../../flutter/lib/ui/painting/codec.h
FILE: ../../../flutter/lib/ui/painting/color_filter.cc
FILE: ../../../flutter/lib/ui/painting/color_filter.h
FILE: ../../../flutter/lib/ui/painting/display_list_deferred_image_gpu.cc
FILE: ../../../flutter/lib/ui/painting/display_list_deferred_image_gpu.h
FILE: ../../../flutter/lib/ui/painting/display_list_image_gpu.cc
FILE: ../../../flutter/lib/ui/painting/display_list_image_gpu.h
FILE: ../../../flutter/lib/ui/painting/engine_layer.cc
Expand Down
4 changes: 4 additions & 0 deletions display_list/display_list_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ SkIRect DlImage::bounds() const {
return SkIRect::MakeSize(dimensions());
}

std::optional<std::string> DlImage::get_error() const {
return std::nullopt;
}

} // namespace flutter
16 changes: 16 additions & 0 deletions display_list/display_list_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#define FLUTTER_DISPLAY_LIST_DISPLAY_LIST_IMAGE_H_

#include <memory>
#include <optional>
#include <string>

#include "flutter/fml/macros.h"
#include "include/core/SkRefCnt.h"
Expand All @@ -27,6 +29,9 @@ namespace flutter {
///
class DlImage : public SkRefCnt {
public:
// Describes which GPU context owns this image.
enum class OwningContext { kRaster, kIO };

static sk_sp<DlImage> Make(const SkImage* image);

static sk_sp<DlImage> Make(sk_sp<SkImage> image);
Expand Down Expand Up @@ -81,6 +86,17 @@ class DlImage : public SkRefCnt {
///
SkIRect bounds() const;

//----------------------------------------------------------------------------
/// @return Specifies which context was used to create this image. The
/// image must be collected on the same task runner as its
/// context.
virtual OwningContext owning_context() const { return OwningContext::kIO; }

//----------------------------------------------------------------------------
/// @return An error, if any, that occurred when trying to create the
/// image.
virtual std::optional<std::string> get_error() const;

protected:
DlImage();
};
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ source_set("ui") {
"painting/codec.h",
"painting/color_filter.cc",
"painting/color_filter.h",
"painting/display_list_deferred_image_gpu.cc",
"painting/display_list_deferred_image_gpu.h",
"painting/display_list_image_gpu.cc",
"painting/display_list_image_gpu.h",
"painting/engine_layer.cc",
Expand Down
19 changes: 18 additions & 1 deletion lib/ui/compositing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ class Scene extends NativeFieldWrapperClass1 {
@pragma('vm:entry-point')
Scene._();

/// Creates a GPU resident image from this scene.
///
/// {@macro dart.ui.painting.Picture.toGpuImage}
Image toGpuImage(int width, int height) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not very sure how shall we use this new API to reuse expensive drawings across frames. Is there an example? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that would probably come at a higher level in the framework or in a package.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I can contribute once I understand it.

if (width <= 0 || height <= 0) {
throw Exception('Invalid image dimensions.');
}

final _Image image = _Image._();
final String? result = _toGpuImage(width, height, image);
if (result != null) {
throw PictureRasterizationException._(result);
}
return Image._(image, image.width, image.height);
}
String? _toGpuImage(int width, int height, _Image outImage) native 'Scene_toGpuImage';

/// Creates a raster image representation of the current state of the scene.
/// This is a slow operation that is performed on a background thread.
///
Expand All @@ -35,7 +52,7 @@ class Scene extends NativeFieldWrapperClass1 {
if (image == null) {
callback(null);
} else {
callback(Image._(image));
callback(Image._(image, image.width, image.height));
}
}),
);
Expand Down
19 changes: 19 additions & 0 deletions lib/ui/compositing/scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace flutter {
IMPLEMENT_WRAPPERTYPEINFO(ui, Scene);

#define FOR_EACH_BINDING(V) \
V(Scene, toGpuImage) \
V(Scene, toImage) \
V(Scene, dispose)

Expand Down Expand Up @@ -66,6 +67,24 @@ void Scene::dispose() {
ClearDartWrapper();
}

Dart_Handle Scene::toGpuImage(uint32_t width,
uint32_t height,
Dart_Handle raw_image_handle) {
TRACE_EVENT0("flutter", "Scene::toGpuImage");

if (!layer_tree_) {
return tonic::ToDart("Scene did not contain a layer tree.");
}

auto picture = layer_tree_->Flatten(SkRect::MakeWH(width, height));
if (!picture) {
return tonic::ToDart("Could not flatten scene into a layer tree.");
}

Picture::RasterizeToGpuImage(picture, width, height, raw_image_handle);
return Dart_Null();
}

Dart_Handle Scene::toImage(uint32_t width,
uint32_t height,
Dart_Handle raw_image_callback) {
Expand Down
4 changes: 4 additions & 0 deletions lib/ui/compositing/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class Scene : public RefCountedDartWrappable<Scene> {

std::unique_ptr<flutter::LayerTree> takeLayerTree();

Dart_Handle toGpuImage(uint32_t width,
uint32_t height,
Dart_Handle raw_image_handle);

Dart_Handle toImage(uint32_t width,
uint32_t height,
Dart_Handle image_callback);
Expand Down
Loading