Skip to content

Commit

Permalink
Allow for dynamic thread merging on IOS for embedded view mutations (f…
Browse files Browse the repository at this point in the history
…lutter#9819)

After pre-roll we know if there have been any mutations made to the IOS embedded UIViews. If there are any mutations and the thread configuration is such chat the mutations will be committed on an illegal thread (GPU thread), we merge the threads and keep them merged until the lease expires. The lease is currently set to expire after 10 frames of no mutations. If there are any mutations in the interim we extend the lease.

TaskRunnerMerger will ultimately be responsible for enforcing the correct thread configurations.

This configuration will be inactive even after this change since still use the same thread when we create the iOS engine. That is slated to change in the coming PRs.
  • Loading branch information
iskakaushik authored Aug 12, 2019
1 parent c92a0d9 commit 971a639
Show file tree
Hide file tree
Showing 21 changed files with 529 additions and 52 deletions.
3 changes: 3 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ FILE: ../../../flutter/fml/eintr_wrapper.h
FILE: ../../../flutter/fml/file.cc
FILE: ../../../flutter/fml/file.h
FILE: ../../../flutter/fml/file_unittest.cc
FILE: ../../../flutter/fml/gpu_thread_merger.cc
FILE: ../../../flutter/fml/gpu_thread_merger.h
FILE: ../../../flutter/fml/gpu_thread_merger_unittests.cc
FILE: ../../../flutter/fml/icu_util.cc
FILE: ../../../flutter/fml/icu_util.h
FILE: ../../../flutter/fml/log_level.h
Expand Down
23 changes: 17 additions & 6 deletions flow/compositor_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ std::unique_ptr<CompositorContext::ScopedFrame> CompositorContext::AcquireFrame(
SkCanvas* canvas,
ExternalViewEmbedder* view_embedder,
const SkMatrix& root_surface_transformation,
bool instrumentation_enabled) {
return std::make_unique<ScopedFrame>(*this, gr_context, canvas, view_embedder,
root_surface_transformation,
instrumentation_enabled);
bool instrumentation_enabled,
fml::RefPtr<fml::GpuThreadMerger> gpu_thread_merger) {
return std::make_unique<ScopedFrame>(
*this, gr_context, canvas, view_embedder, root_surface_transformation,
instrumentation_enabled, gpu_thread_merger);
}

CompositorContext::ScopedFrame::ScopedFrame(
Expand All @@ -46,13 +47,15 @@ CompositorContext::ScopedFrame::ScopedFrame(
SkCanvas* canvas,
ExternalViewEmbedder* view_embedder,
const SkMatrix& root_surface_transformation,
bool instrumentation_enabled)
bool instrumentation_enabled,
fml::RefPtr<fml::GpuThreadMerger> gpu_thread_merger)
: context_(context),
gr_context_(gr_context),
canvas_(canvas),
view_embedder_(view_embedder),
root_surface_transformation_(root_surface_transformation),
instrumentation_enabled_(instrumentation_enabled) {
instrumentation_enabled_(instrumentation_enabled),
gpu_thread_merger_(gpu_thread_merger) {
context_.BeginFrame(*this, instrumentation_enabled_);
}

Expand All @@ -64,6 +67,14 @@ RasterStatus CompositorContext::ScopedFrame::Raster(
flutter::LayerTree& layer_tree,
bool ignore_raster_cache) {
layer_tree.Preroll(*this, ignore_raster_cache);
PostPrerollResult post_preroll_result = PostPrerollResult::kSuccess;
if (view_embedder_ && gpu_thread_merger_) {
post_preroll_result = view_embedder_->PostPrerollAction(gpu_thread_merger_);
}

if (post_preroll_result == PostPrerollResult::kResubmitFrame) {
return RasterStatus::kResubmit;
}
// Clearing canvas after preroll reduces one render target switch when preroll
// paints some raster cache.
if (canvas()) {
Expand Down
22 changes: 19 additions & 3 deletions flow/compositor_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "flutter/flow/instrumentation.h"
#include "flutter/flow/raster_cache.h"
#include "flutter/flow/texture.h"
#include "flutter/fml/gpu_thread_merger.h"
#include "flutter/fml/macros.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
Expand All @@ -20,7 +21,19 @@ namespace flutter {

class LayerTree;

enum class RasterStatus { kSuccess, kFailed };
enum class RasterStatus {
// Frame has successfully rasterized.
kSuccess,
// Frame needs to be resubmitted for rasterization. This is
// currently only called when thread configuration change occurs.
kResubmit,
// Frame has been successfully rasterized, but "there are additional items in
// the pipeline waiting to be consumed. This is currently
// only called when thread configuration change occurs.
kEnqueuePipeline,
// Failed to rasterize the frame.
kFailed
};

class CompositorContext {
public:
Expand All @@ -31,7 +44,8 @@ class CompositorContext {
SkCanvas* canvas,
ExternalViewEmbedder* view_embedder,
const SkMatrix& root_surface_transformation,
bool instrumentation_enabled);
bool instrumentation_enabled,
fml::RefPtr<fml::GpuThreadMerger> gpu_thread_merger);

virtual ~ScopedFrame();

Expand All @@ -57,6 +71,7 @@ class CompositorContext {
ExternalViewEmbedder* view_embedder_;
const SkMatrix& root_surface_transformation_;
const bool instrumentation_enabled_;
fml::RefPtr<fml::GpuThreadMerger> gpu_thread_merger_;

FML_DISALLOW_COPY_AND_ASSIGN(ScopedFrame);
};
Expand All @@ -70,7 +85,8 @@ class CompositorContext {
SkCanvas* canvas,
ExternalViewEmbedder* view_embedder,
const SkMatrix& root_surface_transformation,
bool instrumentation_enabled);
bool instrumentation_enabled,
fml::RefPtr<fml::GpuThreadMerger> gpu_thread_merger);

void OnGrContextCreated();

Expand Down
16 changes: 12 additions & 4 deletions flow/embedded_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <vector>

#include "flutter/fml/gpu_thread_merger.h"
#include "flutter/fml/memory/ref_counted.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPath.h"
Expand Down Expand Up @@ -182,6 +183,8 @@ class EmbeddedViewParams {
}
};

enum class PostPrerollResult { kResubmitFrame, kSuccess };

// This is only used on iOS when running in a non headless mode,
// in this case ExternalViewEmbedder is a reference to the
// FlutterPlatformViewsController which is owned by FlutterViewController.
Expand All @@ -191,10 +194,6 @@ class ExternalViewEmbedder {
public:
ExternalViewEmbedder() = default;

// This will return true after pre-roll if any of the embedded views
// have mutated for last layer tree.
virtual bool HasPendingViewOperations() = 0;

// Call this in-lieu of |SubmitFrame| to clear pre-roll state and
// sets the stage for the next pre-roll.
virtual void CancelFrame() = 0;
Expand All @@ -205,6 +204,15 @@ class ExternalViewEmbedder {
int view_id,
std::unique_ptr<EmbeddedViewParams> params) = 0;

// This needs to get called after |Preroll| finishes on the layer tree.
// Returns kResubmitFrame if the frame needs to be processed again, this is
// after it does any requisite tasks needed to bring itself to a valid state.
// Returns kSuccess if the view embedder is already in a valid state.
virtual PostPrerollResult PostPrerollAction(
fml::RefPtr<fml::GpuThreadMerger> gpu_thread_merger) {
return PostPrerollResult::kSuccess;
}

virtual std::vector<SkCanvas*> GetCurrentCanvases() = 0;

// Must be called on the UI thread.
Expand Down
3 changes: 3 additions & 0 deletions fml/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ source_set("fml") {
"eintr_wrapper.h",
"file.cc",
"file.h",
"gpu_thread_merger.cc",
"gpu_thread_merger.h",
"icu_util.cc",
"icu_util.h",
"log_level.h",
Expand Down Expand Up @@ -200,6 +202,7 @@ executable("fml_unittests") {
"base32_unittest.cc",
"command_line_unittest.cc",
"file_unittest.cc",
"gpu_thread_merger_unittests.cc",
"memory/ref_counted_unittest.cc",
"memory/weak_ptr_unittest.cc",
"message_loop_task_queues_merge_unmerge_unittests.cc",
Expand Down
74 changes: 74 additions & 0 deletions fml/gpu_thread_merger.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#define FML_USED_ON_EMBEDDER

#include "flutter/fml/gpu_thread_merger.h"
#include "flutter/fml/message_loop_impl.h"

namespace fml {

const int GpuThreadMerger::kLeaseNotSet = -1;

GpuThreadMerger::GpuThreadMerger(fml::TaskQueueId platform_queue_id,
fml::TaskQueueId gpu_queue_id)
: platform_queue_id_(platform_queue_id),
gpu_queue_id_(gpu_queue_id),
task_queues_(fml::MessageLoopTaskQueues::GetInstance()),
lease_term_(kLeaseNotSet) {
is_merged_ = task_queues_->Owns(platform_queue_id_, gpu_queue_id_);
}

void GpuThreadMerger::MergeWithLease(size_t lease_term) {
FML_DCHECK(lease_term > 0) << "lease_term should be positive.";
if (!is_merged_) {
is_merged_ = task_queues_->Merge(platform_queue_id_, gpu_queue_id_);
lease_term_ = lease_term;
}
}

bool GpuThreadMerger::IsOnRasterizingThread() {
const auto current_queue_id = MessageLoop::GetCurrentTaskQueueId();
if (is_merged_) {
return current_queue_id == platform_queue_id_;
} else {
return current_queue_id == gpu_queue_id_;
}
}

void GpuThreadMerger::ExtendLeaseTo(size_t lease_term) {
FML_DCHECK(lease_term > 0) << "lease_term should be positive.";
if (lease_term_ != kLeaseNotSet && (int)lease_term > lease_term_) {
lease_term_ = lease_term;
}
}

bool GpuThreadMerger::IsMerged() const {
return is_merged_;
}

GpuThreadStatus GpuThreadMerger::DecrementLease() {
if (!is_merged_) {
return GpuThreadStatus::kRemainsUnmerged;
}

// we haven't been set to merge.
if (lease_term_ == kLeaseNotSet) {
return GpuThreadStatus::kRemainsUnmerged;
}

FML_DCHECK(lease_term_ > 0)
<< "lease_term should always be positive when merged.";
lease_term_--;
if (lease_term_ == 0) {
bool success = task_queues_->Unmerge(platform_queue_id_);
FML_CHECK(success) << "Unable to un-merge the GPU and platform threads.";
is_merged_ = false;
return GpuThreadStatus::kUnmergedNow;
}

return GpuThreadStatus::kRemainsMerged;
}

} // namespace fml
59 changes: 59 additions & 0 deletions fml/gpu_thread_merger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FML_SHELL_COMMON_TASK_RUNNER_MERGER_H_
#define FML_SHELL_COMMON_TASK_RUNNER_MERGER_H_

#include "flutter/fml/macros.h"
#include "flutter/fml/memory/ref_counted.h"
#include "flutter/fml/message_loop_task_queues.h"

namespace fml {

class MessageLoopImpl;

enum class GpuThreadStatus { kRemainsMerged, kRemainsUnmerged, kUnmergedNow };

class GpuThreadMerger : public fml::RefCountedThreadSafe<GpuThreadMerger> {
public:
// Merges the GPU thread into platform thread for the duration of
// the lease term. Lease is managed by the caller by either calling
// |ExtendLeaseTo| or |DecrementLease|.
// When the caller merges with a lease term of say 2. The threads
// are going to remain merged until 2 invocations of |DecreaseLease|,
// unless an |ExtendLeaseTo| gets called.
void MergeWithLease(size_t lease_term);

void ExtendLeaseTo(size_t lease_term);

// Returns |GpuThreadStatus::kUnmergedNow| if this call resulted in splitting
// the GPU and platform threads. Reduces the lease term by 1.
GpuThreadStatus DecrementLease();

bool IsMerged() const;

GpuThreadMerger(fml::TaskQueueId platform_queue_id,
fml::TaskQueueId gpu_queue_id);

// Returns true if the the current thread owns rasterizing.
// When the threads are merged, platform thread owns rasterizing.
// When un-merged, gpu thread owns rasterizing.
bool IsOnRasterizingThread();

private:
static const int kLeaseNotSet;
fml::TaskQueueId platform_queue_id_;
fml::TaskQueueId gpu_queue_id_;
fml::RefPtr<fml::MessageLoopTaskQueues> task_queues_;
std::atomic_int lease_term_;
bool is_merged_;

FML_FRIEND_REF_COUNTED_THREAD_SAFE(GpuThreadMerger);
FML_FRIEND_MAKE_REF_COUNTED(GpuThreadMerger);
FML_DISALLOW_COPY_AND_ASSIGN(GpuThreadMerger);
};

} // namespace fml

#endif // FML_SHELL_COMMON_TASK_RUNNER_MERGER_H_
Loading

0 comments on commit 971a639

Please sign in to comment.