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

Commit 9f36408

Browse files
authored
[Rasterizer] Make resubmit information temporary (#42001)
This PR refactors `Rasterizer` so that the resubmit information is returned as a temporary struct instead of stored permanently as class member. This limits the lifetime of these values and reduces class members. Additionally, this benefits the multi-view project: if the rasterizer is going to store multiple surfaces, what variables should be stored in a map from view IDs? Should all surfaces have a to-be-submitted record? The answer is "no", because these information is only temporary. This PR should not need unit tests since it's only a refactor. ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See [testing the engine] for instructions on writing and running engine tests. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I signed the [CLA]. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat
1 parent 99f2653 commit 9f36408

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

shell/common/rasterizer.cc

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,19 @@ RasterStatus Rasterizer::Draw(
193193
.GetRasterTaskRunner()
194194
->RunsTasksOnCurrentThread());
195195

196-
RasterStatus raster_status = RasterStatus::kFailed;
196+
DoDrawResult draw_result;
197197
LayerTreePipeline::Consumer consumer =
198-
[&](std::unique_ptr<LayerTreeItem> item) {
198+
[this, &draw_result,
199+
&discard_callback](std::unique_ptr<LayerTreeItem> item) {
199200
std::unique_ptr<LayerTree> layer_tree = std::move(item->layer_tree);
200201
std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder =
201202
std::move(item->frame_timings_recorder);
202203
float device_pixel_ratio = item->device_pixel_ratio;
203204
if (discard_callback(*layer_tree.get())) {
204-
raster_status = RasterStatus::kDiscarded;
205+
draw_result.raster_status = RasterStatus::kDiscarded;
205206
} else {
206-
raster_status = DoDraw(std::move(frame_timings_recorder),
207-
std::move(layer_tree), device_pixel_ratio);
207+
draw_result = DoDraw(std::move(frame_timings_recorder),
208+
std::move(layer_tree), device_pixel_ratio);
208209
}
209210
};
210211

@@ -215,18 +216,15 @@ RasterStatus Rasterizer::Draw(
215216
// if the raster status is to resubmit the frame, we push the frame to the
216217
// front of the queue and also change the consume status to more available.
217218

218-
bool should_resubmit_frame = ShouldResubmitFrame(raster_status);
219+
bool should_resubmit_frame = ShouldResubmitFrame(draw_result.raster_status);
219220
if (should_resubmit_frame) {
220-
auto resubmitted_layer_tree_item = std::make_unique<LayerTreeItem>(
221-
std::move(resubmitted_layer_tree_), std::move(resubmitted_recorder_),
222-
resubmitted_pixel_ratio_);
223221
auto front_continuation = pipeline->ProduceIfEmpty();
224-
PipelineProduceResult result =
225-
front_continuation.Complete(std::move(resubmitted_layer_tree_item));
226-
if (result.success) {
222+
PipelineProduceResult pipeline_result = front_continuation.Complete(
223+
std::move(draw_result.resubmitted_layer_tree_item));
224+
if (pipeline_result.success) {
227225
consume_result = PipelineConsumeResult::MoreAvailable;
228226
}
229-
} else if (raster_status == RasterStatus::kEnqueuePipeline) {
227+
} else if (draw_result.raster_status == RasterStatus::kEnqueuePipeline) {
230228
consume_result = PipelineConsumeResult::MoreAvailable;
231229
}
232230

@@ -255,7 +253,7 @@ RasterStatus Rasterizer::Draw(
255253
break;
256254
}
257255

258-
return raster_status;
256+
return draw_result.raster_status;
259257
}
260258

261259
bool Rasterizer::ShouldResubmitFrame(const RasterStatus& raster_status) {
@@ -376,7 +374,7 @@ fml::Milliseconds Rasterizer::GetFrameBudget() const {
376374
return delegate_.GetFrameBudget();
377375
};
378376

379-
RasterStatus Rasterizer::DoDraw(
377+
Rasterizer::DoDrawResult Rasterizer::DoDraw(
380378
std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder,
381379
std::unique_ptr<flutter::LayerTree> layer_tree,
382380
float device_pixel_ratio) {
@@ -387,7 +385,9 @@ RasterStatus Rasterizer::DoDraw(
387385
->RunsTasksOnCurrentThread());
388386

389387
if (!layer_tree || !surface_) {
390-
return RasterStatus::kFailed;
388+
return DoDrawResult{
389+
.raster_status = RasterStatus::kFailed,
390+
};
391391
}
392392

393393
PersistentCache* persistent_cache = PersistentCache::GetCacheForProcess();
@@ -399,13 +399,18 @@ RasterStatus Rasterizer::DoDraw(
399399
last_layer_tree_ = std::move(layer_tree);
400400
last_device_pixel_ratio_ = device_pixel_ratio;
401401
} else if (ShouldResubmitFrame(raster_status)) {
402-
resubmitted_pixel_ratio_ = device_pixel_ratio;
403-
resubmitted_layer_tree_ = std::move(layer_tree);
404-
resubmitted_recorder_ = frame_timings_recorder->CloneUntil(
405-
FrameTimingsRecorder::State::kBuildEnd);
406-
return raster_status;
402+
return DoDrawResult{
403+
.raster_status = raster_status,
404+
.resubmitted_layer_tree_item = std::make_unique<LayerTreeItem>(
405+
std::move(layer_tree),
406+
frame_timings_recorder->CloneUntil(
407+
FrameTimingsRecorder::State::kBuildEnd),
408+
device_pixel_ratio),
409+
};
407410
} else if (raster_status == RasterStatus::kDiscarded) {
408-
return raster_status;
411+
return DoDrawResult{
412+
.raster_status = raster_status,
413+
};
409414
}
410415

411416
if (persistent_cache->IsDumpingSkp() &&
@@ -473,11 +478,15 @@ RasterStatus Rasterizer::DoDraw(
473478
if (raster_thread_merger_) {
474479
if (raster_thread_merger_->DecrementLease() ==
475480
fml::RasterThreadStatus::kUnmergedNow) {
476-
return RasterStatus::kEnqueuePipeline;
481+
return DoDrawResult{
482+
.raster_status = RasterStatus::kEnqueuePipeline,
483+
};
477484
}
478485
}
479486

480-
return raster_status;
487+
return DoDrawResult{
488+
.raster_status = raster_status,
489+
};
481490
}
482491

483492
RasterStatus Rasterizer::DrawToSurface(

shell/common/rasterizer.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,19 @@ class Rasterizer final : public SnapshotDelegate,
500500
void DisableThreadMergerIfNeeded();
501501

502502
private:
503+
// The result of `DoDraw`.
504+
//
505+
// Normally `DoDraw` returns simply a raster status. However, sometimes we
506+
// need to attempt to rasterize the layer tree again. This happens when
507+
// layer_tree has not successfully rasterized due to changes in the thread
508+
// configuration, in which case the resubmitted task will be inserted to the
509+
// front of the pipeline.
510+
struct DoDrawResult {
511+
RasterStatus raster_status = RasterStatus::kFailed;
512+
513+
std::unique_ptr<LayerTreeItem> resubmitted_layer_tree_item;
514+
};
515+
503516
// |SnapshotDelegate|
504517
std::unique_ptr<GpuImageResult> MakeSkiaGpuImage(
505518
sk_sp<DisplayList> display_list,
@@ -554,7 +567,7 @@ class Rasterizer final : public SnapshotDelegate,
554567
GrDirectContext* surface_context,
555568
bool compressed);
556569

557-
RasterStatus DoDraw(
570+
DoDrawResult DoDraw(
558571
std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder,
559572
std::unique_ptr<flutter::LayerTree> layer_tree,
560573
float device_pixel_ratio);
@@ -581,12 +594,6 @@ class Rasterizer final : public SnapshotDelegate,
581594
// This is the last successfully rasterized layer tree.
582595
std::unique_ptr<flutter::LayerTree> last_layer_tree_;
583596
float last_device_pixel_ratio_;
584-
// Set when we need attempt to rasterize the layer tree again. This layer_tree
585-
// has not successfully rasterized. This can happen due to the change in the
586-
// thread configuration. This will be inserted to the front of the pipeline.
587-
std::unique_ptr<flutter::LayerTree> resubmitted_layer_tree_;
588-
std::unique_ptr<FrameTimingsRecorder> resubmitted_recorder_;
589-
float resubmitted_pixel_ratio_;
590597
fml::closure next_frame_callback_;
591598
bool user_override_resource_cache_bytes_;
592599
std::optional<size_t> max_cache_bytes_;

0 commit comments

Comments
 (0)