Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement toGpuImage, a synchronous, GPU-resident version of #33736

Merged
merged 28 commits into from Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 16 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
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
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
15 changes: 15 additions & 0 deletions display_list/display_list_image.h
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,16 @@ class DlImage : public SkRefCnt {
///
SkIRect bounds() const;

//----------------------------------------------------------------------------
/// @return Whether the image method is only safe to use on the raster
Copy link
Member

Choose a reason for hiding this comment

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

The owning context of this image, not really whether. the owning context describes where this image is safe to use. though maybe that last part could be more defined

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

/// task runner.
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
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
16 changes: 15 additions & 1 deletion lib/ui/compositing.dart
Expand Up @@ -21,6 +21,20 @@ 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._();
_toGpuImage(width, height, image);
return Image._(image, image.width, image.height);
}
void _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 +49,7 @@ class Scene extends NativeFieldWrapperClass1 {
if (image == null) {
callback(null);
} else {
callback(Image._(image));
callback(Image._(image, image.width, image.height));
}
}),
);
Expand Down
18 changes: 18 additions & 0 deletions lib/ui/compositing/scene.cc
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,23 @@ void Scene::dispose() {
ClearDartWrapper();
}

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

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);
}

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
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