Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 337f35a

Browse files
committed
Connect
1 parent 30b95c6 commit 337f35a

File tree

8 files changed

+42
-32
lines changed

8 files changed

+42
-32
lines changed

lib/ui/compositing.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ class Scene extends NativeFieldWrapperClass1 {
2121
/// Synchronously creates a handle to an image from this scene.
2222
///
2323
/// {@macro dart.ui.painting.Picture.toImageSync}
24-
Image toImageSync(int width, int height) {
24+
Image toImageSync(int width, int height, [double pixelRatio = 1.0]) {
2525
if (width <= 0 || height <= 0) {
2626
throw Exception('Invalid image dimensions.');
2727
}
2828

2929
final _Image image = _Image._();
30-
final String? result = _toImageSync(width, height, image);
30+
final String? result = _toImageSync(width, height, pixelRatio, image);
3131
if (result != null) {
3232
throw PictureRasterizationException._(result);
3333
}
3434
return Image._(image, image.width, image.height);
3535
}
3636

37-
@Native<Handle Function(Pointer<Void>, Uint32, Uint32, Handle)>(symbol: 'Scene::toImageSync')
38-
external String? _toImageSync(int width, int height, _Image outImage);
37+
@Native<Handle Function(Pointer<Void>, Uint32, Uint32, Double, Handle)>(symbol: 'Scene::toImageSync')
38+
external String? _toImageSync(int width, int height, double pixelRatio, _Image outImage);
3939

4040
/// Creates a raster image representation of the current state of the scene.
4141
///
@@ -44,11 +44,11 @@ class Scene extends NativeFieldWrapperClass1 {
4444
/// Callers must dispose the [Image] when they are done with it. If the result
4545
/// will be shared with other methods or classes, [Image.clone] should be used
4646
/// and each handle created must be disposed.
47-
Future<Image> toImage(int width, int height) {
47+
Future<Image> toImage(int width, int height, [double pixelRatio = 1.0]) {
4848
if (width <= 0 || height <= 0) {
4949
throw Exception('Invalid image dimensions.');
5050
}
51-
return _futurize((_Callback<Image?> callback) => _toImage(width, height, (_Image? image) {
51+
return _futurize((_Callback<Image?> callback) => _toImage(width, height, pixelRatio, (_Image? image) {
5252
if (image == null) {
5353
callback(null);
5454
} else {
@@ -58,8 +58,8 @@ class Scene extends NativeFieldWrapperClass1 {
5858
);
5959
}
6060

61-
@Native<Handle Function(Pointer<Void>, Uint32, Uint32, Handle)>(symbol: 'Scene::toImage')
62-
external String? _toImage(int width, int height, _Callback<_Image?> callback);
61+
@Native<Handle Function(Pointer<Void>, Uint32, Uint32, Double, Handle)>(symbol: 'Scene::toImage')
62+
external String? _toImage(int width, int height, double pixelRatio, _Callback<_Image?> callback);
6363

6464
/// Releases the resources used by this scene.
6565
///

lib/ui/compositing/scene.cc

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ Scene::Scene(std::shared_ptr<flutter::Layer> rootLayer,
4040
uint32_t rasterizerTracingThreshold,
4141
bool checkerboardRasterCacheImages,
4242
bool checkerboardOffscreenLayers) {
43-
// Currently only supports a single window.
44-
auto viewport_metrics = UIDartState::Current()
45-
->platform_configuration()
46-
->get_window(0)
47-
->viewport_metrics();
48-
device_pixel_ratio_ = static_cast<float>(viewport_metrics.device_pixel_ratio);
49-
5043
layer_tree_config_ = std::make_unique<LayerTree::Config>();
5144
layer_tree_config_->root_layer = std::move(rootLayer);
5245
layer_tree_config_->rasterizer_tracing_threshold = rasterizerTracingThreshold;
@@ -65,19 +58,22 @@ void Scene::dispose() {
6558

6659
Dart_Handle Scene::toImageSync(uint32_t width,
6760
uint32_t height,
61+
double pixel_ratio,
6862
Dart_Handle raw_image_handle) {
6963
TRACE_EVENT0("flutter", "Scene::toImageSync");
7064

7165
if (!layer_tree_config_) {
7266
return tonic::ToDart("Scene's layer tree has been taken away.");
7367
}
7468

75-
Scene::RasterizeToImage(width, height, raw_image_handle);
69+
Scene::RasterizeToImage(width, height, static_cast<float>(pixel_ratio),
70+
raw_image_handle);
7671
return Dart_Null();
7772
}
7873

7974
Dart_Handle Scene::toImage(uint32_t width,
8075
uint32_t height,
76+
double pixel_ratio,
8177
Dart_Handle raw_image_callback) {
8278
TRACE_EVENT0("flutter", "Scene::toImage");
8379

@@ -86,7 +82,8 @@ Dart_Handle Scene::toImage(uint32_t width,
8682
}
8783

8884
return Picture::RasterizeLayerTreeToImage(
89-
BuildLayerTree(width, height, device_pixel_ratio_), raw_image_callback);
85+
BuildLayerTree(width, height, static_cast<float>(pixel_ratio)),
86+
raw_image_callback);
9087
}
9188

9289
static sk_sp<DlImage> CreateDeferredImage(
@@ -114,6 +111,7 @@ static sk_sp<DlImage> CreateDeferredImage(
114111

115112
void Scene::RasterizeToImage(uint32_t width,
116113
uint32_t height,
114+
float pixel_ratio,
117115
Dart_Handle raw_image_handle) {
118116
auto* dart_state = UIDartState::Current();
119117
if (!dart_state) {
@@ -126,17 +124,17 @@ void Scene::RasterizeToImage(uint32_t width,
126124
auto image = CanvasImage::Create();
127125
auto dl_image = CreateDeferredImage(
128126
dart_state->IsImpellerEnabled(),
129-
BuildLayerTree(width, height, device_pixel_ratio_),
130-
std::move(snapshot_delegate), std::move(raster_task_runner),
131-
std::move(unref_queue));
127+
BuildLayerTree(width, height, pixel_ratio), std::move(snapshot_delegate),
128+
std::move(raster_task_runner), std::move(unref_queue));
132129
image->set_image(dl_image);
133130
image->AssociateWithDartWrapper(raw_image_handle);
134131
}
135132

136133
std::unique_ptr<flutter::LayerTree> Scene::takeLayerTree(uint64_t width,
137-
uint64_t height) {
134+
uint64_t height,
135+
float pixel_ratio) {
138136
if (layer_tree_config_ != nullptr) {
139-
auto layer_tree = BuildLayerTree(width, height, device_pixel_ratio_);
137+
auto layer_tree = BuildLayerTree(width, height, pixel_ratio);
140138
// TODO(dkwingsmt): We don't need to reset here. But certain unit tests test
141139
// it. Let's keep it this way for now.
142140
layer_tree_config_.reset();
@@ -149,6 +147,7 @@ std::unique_ptr<flutter::LayerTree> Scene::takeLayerTree(uint64_t width,
149147
std::unique_ptr<LayerTree> Scene::BuildLayerTree(uint32_t width,
150148
uint32_t height,
151149
float pixel_ratio) {
150+
FML_CHECK(layer_tree_config_ != nullptr);
152151
return std::make_unique<LayerTree>(*layer_tree_config_,
153152
SkISize::Make(width, height), pixel_ratio);
154153
}

lib/ui/compositing/scene.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ class Scene : public RefCountedDartWrappable<Scene> {
2727
bool checkerboardOffscreenLayers);
2828

2929
std::unique_ptr<flutter::LayerTree> takeLayerTree(uint64_t width,
30-
uint64_t height);
30+
uint64_t height,
31+
float pixel_ratio);
3132

3233
Dart_Handle toImageSync(uint32_t width,
3334
uint32_t height,
35+
double pixel_ratio,
3436
Dart_Handle raw_image_handle);
3537

3638
Dart_Handle toImage(uint32_t width,
3739
uint32_t height,
40+
double pixel_ratio,
3841
Dart_Handle raw_image_handle);
3942

4043
void dispose();
@@ -47,14 +50,15 @@ class Scene : public RefCountedDartWrappable<Scene> {
4750

4851
void RasterizeToImage(uint32_t width,
4952
uint32_t height,
53+
float pixel_ratio,
5054
Dart_Handle raw_image_handle);
5155

5256
std::unique_ptr<LayerTree> BuildLayerTree(uint32_t width,
5357
uint32_t height,
5458
float pixel_ratio);
5559

60+
// No longer valid after calling `takeLayerTreeConfig`.
5661
std::unique_ptr<flutter::LayerTree::Config> layer_tree_config_;
57-
float device_pixel_ratio_;
5862
};
5963

6064
} // namespace flutter

lib/ui/compositing/scene_builder_unittests.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ TEST_F(ShellTest, SceneBuilderBuildAndSceneDisposeReleasesLayerStack) {
5959

6060
auto validate_scene_has_no_layers =
6161
[message_latch, &retained_scene](Dart_NativeArguments args) {
62-
EXPECT_FALSE(retained_scene->takeLayerTree(100, 100));
62+
EXPECT_FALSE(retained_scene->takeLayerTree(100, 100, 1.0f));
6363
retained_scene->Release();
6464
retained_scene = nullptr;
6565
message_latch->Signal();

lib/ui/dart_ui.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ typedef CanvasPath Path;
293293
V(SceneBuilder, setCheckerboardRasterCacheImages, 2) \
294294
V(SceneBuilder, setRasterizerTracingThreshold, 2) \
295295
V(Scene, dispose, 1) \
296-
V(Scene, toImage, 4) \
297-
V(Scene, toImageSync, 4) \
296+
V(Scene, toImage, 5) \
297+
V(Scene, toImageSync, 5) \
298298
V(SemanticsUpdateBuilder, build, 2) \
299299
V(SemanticsUpdateBuilder, updateCustomAction, 5) \
300300
V(SemanticsUpdateBuilder, updateNode, 36) \

lib/ui/painting/picture.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ Dart_Handle Picture::RasterizeToImage(const sk_sp<DisplayList>& display_list,
125125
Dart_Handle Picture::RasterizeLayerTreeToImage(
126126
std::unique_ptr<LayerTree> layer_tree,
127127
Dart_Handle raw_image_callback) {
128+
FML_DCHECK(layer_tree != nullptr);
128129
auto frame_size = layer_tree->frame_size();
129130
return DoRasterizeToImage(nullptr, std::move(layer_tree), frame_size.width(),
130131
frame_size.height(), raw_image_callback);
@@ -135,6 +136,9 @@ Dart_Handle Picture::DoRasterizeToImage(const sk_sp<DisplayList>& display_list,
135136
uint32_t width,
136137
uint32_t height,
137138
Dart_Handle raw_image_callback) {
139+
// Either display_list or layer_tree should be provided.
140+
FML_DCHECK((display_list == nullptr) != (layer_tree == nullptr));
141+
138142
if (Dart_IsNull(raw_image_callback) || !Dart_IsClosure(raw_image_callback)) {
139143
return tonic::ToDart("Image callback was invalid");
140144
}
@@ -198,10 +202,11 @@ Dart_Handle Picture::DoRasterizeToImage(const sk_sp<DisplayList>& display_list,
198202
fml::TaskRunner::RunNowOrPostTask(
199203
raster_task_runner,
200204
fml::MakeCopyable([ui_task_runner, snapshot_delegate, display_list,
201-
picture_bounds, ui_task,
205+
&picture_bounds, ui_task,
202206
layer_tree = std::move(layer_tree)]() mutable {
203207
sk_sp<DlImage> image;
204208
if (layer_tree) {
209+
FML_DCHECK(picture_bounds == layer_tree->frame_size());
205210
auto display_list = layer_tree->Flatten(
206211
SkRect::MakeWH(picture_bounds.width(), picture_bounds.height()),
207212
snapshot_delegate->GetTextureRegistry(),

lib/ui/painting/picture.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ class Picture : public RefCountedDartWrappable<Picture> {
5252
std::unique_ptr<LayerTree> layer_tree,
5353
Dart_Handle raw_image_callback);
5454

55-
// Callers may provide either a display list or a layer tree. If a layer tree
56-
// is provided, it will be flattened on the raster thread. In this case the
57-
// display list will be ignored.
55+
// Callers may provide either a display list or a layer tree, but not both.
56+
//
57+
// If a layer tree is provided, it will be flattened on the raster thread, and
58+
// picture_bounds should be the layer tree's frame_size().
5859
static Dart_Handle DoRasterizeToImage(const sk_sp<DisplayList>& display_list,
5960
std::unique_ptr<LayerTree> layer_tree,
6061
uint32_t width,

runtime/runtime_controller.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ void RuntimeController::Render(Scene* scene) {
325325
}
326326
const auto& viewport_metrics = window->viewport_metrics();
327327
client_.Render(scene->takeLayerTree(viewport_metrics.physical_width,
328-
viewport_metrics.physical_height));
328+
viewport_metrics.physical_height,
329+
viewport_metrics.device_pixel_ratio));
329330
}
330331

331332
// |PlatformConfigurationClient|

0 commit comments

Comments
 (0)