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

Commit ae2c065

Browse files
committed
Right before dart_ui.cc
1 parent 30b95c6 commit ae2c065

File tree

6 files changed

+30
-22
lines changed

6 files changed

+30
-22
lines changed

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;
@@ -67,26 +60,30 @@ Dart_Handle Scene::toImageSync(uint32_t width,
6760
uint32_t height,
6861
Dart_Handle raw_image_handle) {
6962
TRACE_EVENT0("flutter", "Scene::toImageSync");
63+
double pixel_ratio = 1.0f; // TODO(dkwingsmt)
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,
8176
Dart_Handle raw_image_callback) {
8277
TRACE_EVENT0("flutter", "Scene::toImage");
78+
double pixel_ratio = 1.0f; // TODO(dkwingsmt)
8379

8480
if (!layer_tree_config_) {
8581
return tonic::ToDart("Scene's layer tree has been taken away.");
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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ 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,
@@ -47,14 +48,15 @@ class Scene : public RefCountedDartWrappable<Scene> {
4748

4849
void RasterizeToImage(uint32_t width,
4950
uint32_t height,
51+
float pixel_ratio,
5052
Dart_Handle raw_image_handle);
5153

5254
std::unique_ptr<LayerTree> BuildLayerTree(uint32_t width,
5355
uint32_t height,
5456
float pixel_ratio);
5557

58+
// No longer valid after calling `takeLayerTreeConfig`.
5659
std::unique_ptr<flutter::LayerTree::Config> layer_tree_config_;
57-
float device_pixel_ratio_;
5860
};
5961

6062
} // 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/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)