Run the rasterizer on the platform thread (flutter/engine#18841)
This commit is contained in:
@@ -46,6 +46,10 @@ bool AndroidExternalViewEmbedder::SubmitFrame(
|
||||
// TODO(egarciad): Implement hybrid composition.
|
||||
// https://github.com/flutter/flutter/issues/55270
|
||||
TRACE_EVENT0("flutter", "AndroidExternalViewEmbedder::SubmitFrame");
|
||||
if (should_run_rasterizer_on_platform_thread_) {
|
||||
// Don't submit the current frame if the frame will be resubmitted.
|
||||
return true;
|
||||
}
|
||||
for (size_t i = 0; i < composition_order_.size(); i++) {
|
||||
int64_t view_id = composition_order_[i];
|
||||
frame->SkiaCanvas()->drawPicture(
|
||||
@@ -57,6 +61,26 @@ bool AndroidExternalViewEmbedder::SubmitFrame(
|
||||
// |ExternalViewEmbedder|
|
||||
PostPrerollResult AndroidExternalViewEmbedder::PostPrerollAction(
|
||||
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) {
|
||||
// This frame may remove existing platform views that aren't contained
|
||||
// in `composition_order_`.
|
||||
//
|
||||
// If this frame doesn't have platform views, it's still required to keep
|
||||
// the rasterizer running on the platform thread for at least one more
|
||||
// frame.
|
||||
//
|
||||
// To keep the rasterizer running on the platform thread one more frame,
|
||||
// `kDefaultMergedLeaseDuration` must be at least `1`.
|
||||
bool has_platform_views = composition_order_.size() > 0;
|
||||
if (has_platform_views) {
|
||||
if (raster_thread_merger->IsMerged()) {
|
||||
raster_thread_merger->ExtendLeaseTo(kDefaultMergedLeaseDuration);
|
||||
} else {
|
||||
// Merge the raster and platform threads in `EndFrame`.
|
||||
should_run_rasterizer_on_platform_thread_ = true;
|
||||
CancelFrame();
|
||||
return PostPrerollResult::kResubmitFrame;
|
||||
}
|
||||
}
|
||||
return PostPrerollResult::kSuccess;
|
||||
}
|
||||
|
||||
@@ -86,6 +110,11 @@ void AndroidExternalViewEmbedder::CancelFrame() {
|
||||
|
||||
// |ExternalViewEmbedder|
|
||||
void AndroidExternalViewEmbedder::EndFrame(
|
||||
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) {}
|
||||
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) {
|
||||
if (should_run_rasterizer_on_platform_thread_) {
|
||||
raster_thread_merger->MergeWithLease(kDefaultMergedLeaseDuration);
|
||||
should_run_rasterizer_on_platform_thread_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
@@ -48,6 +48,17 @@ class AndroidExternalViewEmbedder : public ExternalViewEmbedder {
|
||||
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) override;
|
||||
|
||||
private:
|
||||
// The number of frames the rasterizer task runner will continue
|
||||
// to run on the platform thread after no platform view is rendered.
|
||||
//
|
||||
// Note: this is an arbitrary number that attempts to account for cases
|
||||
// where the platform view might be momentarily off the screen.
|
||||
static const int kDefaultMergedLeaseDuration = 10;
|
||||
|
||||
// Whether the rasterizer task runner should run on the platform thread.
|
||||
// When this is true, the current frame is cancelled and resubmitted.
|
||||
bool should_run_rasterizer_on_platform_thread_ = false;
|
||||
|
||||
// The size of the background canvas.
|
||||
SkISize frame_size_;
|
||||
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "flutter/fml/raster_thread_merger.h"
|
||||
#include "flutter/fml/thread.h"
|
||||
#include "flutter/shell/platform/android/external_view_embedder/external_view_embedder.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace flutter {
|
||||
namespace testing {
|
||||
@@ -48,5 +49,56 @@ TEST(AndroidExternalViewEmbedder, CancelFrame) {
|
||||
ASSERT_EQ(0UL, canvases.size());
|
||||
}
|
||||
|
||||
TEST(AndroidExternalViewEmbedder, RasterizerRunsOnPlatformThread) {
|
||||
auto embedder = new AndroidExternalViewEmbedder();
|
||||
auto platform_thread = new fml::Thread("platform");
|
||||
auto rasterizer_thread = new fml::Thread("rasterizer");
|
||||
auto platform_queue_id = platform_thread->GetTaskRunner()->GetTaskQueueId();
|
||||
auto rasterizer_queue_id =
|
||||
rasterizer_thread->GetTaskRunner()->GetTaskQueueId();
|
||||
|
||||
auto raster_thread_merger = fml::MakeRefCounted<fml::RasterThreadMerger>(
|
||||
platform_queue_id, rasterizer_queue_id);
|
||||
ASSERT_FALSE(raster_thread_merger->IsMerged());
|
||||
|
||||
embedder->BeginFrame(SkISize::Make(10, 20), nullptr, 1.0);
|
||||
// Push a platform view.
|
||||
embedder->PrerollCompositeEmbeddedView(
|
||||
0, std::make_unique<EmbeddedViewParams>());
|
||||
|
||||
auto postpreroll_result = embedder->PostPrerollAction(raster_thread_merger);
|
||||
ASSERT_EQ(PostPrerollResult::kResubmitFrame, postpreroll_result);
|
||||
ASSERT_TRUE(embedder->SubmitFrame(nullptr, nullptr));
|
||||
|
||||
embedder->EndFrame(raster_thread_merger);
|
||||
ASSERT_TRUE(raster_thread_merger->IsMerged());
|
||||
|
||||
int pending_frames = 0;
|
||||
while (raster_thread_merger->IsMerged()) {
|
||||
raster_thread_merger->DecrementLease();
|
||||
pending_frames++;
|
||||
}
|
||||
ASSERT_EQ(10, pending_frames); // kDefaultMergedLeaseDuration
|
||||
}
|
||||
|
||||
TEST(AndroidExternalViewEmbedder, RasterizerRunsOnRasterizerThread) {
|
||||
auto embedder = new AndroidExternalViewEmbedder();
|
||||
auto platform_thread = new fml::Thread("platform");
|
||||
auto rasterizer_thread = new fml::Thread("rasterizer");
|
||||
auto platform_queue_id = platform_thread->GetTaskRunner()->GetTaskQueueId();
|
||||
auto rasterizer_queue_id =
|
||||
rasterizer_thread->GetTaskRunner()->GetTaskQueueId();
|
||||
|
||||
auto raster_thread_merger = fml::MakeRefCounted<fml::RasterThreadMerger>(
|
||||
platform_queue_id, rasterizer_queue_id);
|
||||
ASSERT_FALSE(raster_thread_merger->IsMerged());
|
||||
|
||||
PostPrerollResult result = embedder->PostPrerollAction(raster_thread_merger);
|
||||
ASSERT_EQ(PostPrerollResult::kSuccess, result);
|
||||
|
||||
embedder->EndFrame(raster_thread_merger);
|
||||
ASSERT_FALSE(raster_thread_merger->IsMerged());
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
|
||||
@@ -209,12 +209,11 @@ class FlutterPlatformViewsController {
|
||||
std::map<int64_t, int64_t> clip_count_;
|
||||
SkISize frame_size_;
|
||||
|
||||
// This is the number of frames the task runners will stay
|
||||
// merged after a frame where we see a mutation to the embedded views.
|
||||
// Note: This number was arbitrarily picked. The rationale being
|
||||
// merge-unmerge are not zero cost operations. To account for cases
|
||||
// like animating platform views, we picked it to be > 2, as we would
|
||||
// want to avoid merge-unmerge during each frame with a mutation.
|
||||
// The number of frames the rasterizer task runner will continue
|
||||
// to run on the platform thread after no platform view is rendered.
|
||||
//
|
||||
// Note: this is an arbitrary number that attempts to account for cases
|
||||
// where the platform view might be momentarily off the screen.
|
||||
static const int kDefaultMergedLeaseDuration = 10;
|
||||
|
||||
// Method channel `OnDispose` calls adds the views to be disposed to this set to be disposed on
|
||||
|
||||
Reference in New Issue
Block a user