Skip to content

Commit

Permalink
gpu: Integrate viz threads with Adpf.
Browse files Browse the repository at this point in the history
Set up a RenderingPipeline for viz display compositor in the GPU process
to provide feedback to ADPF.

R=backer@chromium.org, boliu@chromium.org

Bug: 1157620
Change-Id: Iaef939903af0492d8c40d8d7c9ed00a7d6bc9b67
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2595007
Reviewed-by: Bo <boliu@chromium.org>
Reviewed-by: Jonathan Backer <backer@chromium.org>
Commit-Queue: Bo <boliu@chromium.org>
Auto-Submit: Khushal <khushalsagar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#860004}
  • Loading branch information
Bo Liu authored and Chromium LUCI CQ committed Mar 4, 2021
1 parent 0162e72 commit 6bc7619
Show file tree
Hide file tree
Showing 20 changed files with 130 additions and 22 deletions.
2 changes: 2 additions & 0 deletions android_webview/browser/gfx/display_scheduler_webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class DisplaySchedulerWebView : public viz::DisplaySchedulerBase {
void DidSwapBuffers() override;
void DidReceiveSwapBuffersAck() override {}
void OutputSurfaceLost() override;
void SetGpuLatency(base::TimeDelta gpu_latency) override {}

// DisplayDamageTrackerObserver implementation.
void OnDisplayDamaged(viz::SurfaceId surface_id) override;
void OnRootFrameMissing(bool missing) override {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ void VizCompositorThreadRunnerWebView::PostTaskAndBlock(

VizCompositorThreadRunnerWebView::~VizCompositorThreadRunnerWebView() = default;

base::PlatformThreadId VizCompositorThreadRunnerWebView::thread_id() {
DCHECK(viz_thread_.IsRunning());
return viz_thread_.GetThreadId();
}

base::SingleThreadTaskRunner* VizCompositorThreadRunnerWebView::task_runner() {
return viz_task_runner_.get();
}
Expand All @@ -110,7 +115,8 @@ void VizCompositorThreadRunnerWebView::CreateFrameSinkManager(
void VizCompositorThreadRunnerWebView::CreateFrameSinkManager(
viz::mojom::FrameSinkManagerParamsPtr params,
gpu::CommandBufferTaskExecutor* task_executor,
viz::GpuServiceImpl* gpu_service) {
viz::GpuServiceImpl* gpu_service,
gfx::RenderingPipeline* gpu_pipeline) {
viz_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ class VizCompositorThreadRunnerWebView : public viz::VizCompositorThreadRunner {
base::OnceClosure task);

// viz::VizCompositorThreadRunner overrides.
base::PlatformThreadId thread_id() override;
base::SingleThreadTaskRunner* task_runner() override;
void CreateFrameSinkManager(
viz::mojom::FrameSinkManagerParamsPtr params) override;
void CreateFrameSinkManager(viz::mojom::FrameSinkManagerParamsPtr params,
gpu::CommandBufferTaskExecutor* task_executor,
viz::GpuServiceImpl* gpu_service) override;
viz::GpuServiceImpl* gpu_service,
gfx::RenderingPipeline* gpu_pipeline) override;
#if BUILDFLAG(USE_VIZ_DEVTOOLS)
void CreateVizDevTools(viz::mojom::VizDevToolsParamsPtr params) override;
#endif
Expand Down
8 changes: 8 additions & 0 deletions components/viz/common/features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

namespace features {

// Enables the use of CPU scheduling APIs on Android.
const base::Feature kAdpf{"Adpf", base::FEATURE_DISABLED_BY_DEFAULT};

const base::Feature kEnableOverlayPrioritization {
"EnableOverlayPrioritization",
#if BUILDFLAG(USE_CHROMEOS_PROTECTED_MEDIA)
Expand Down Expand Up @@ -122,6 +125,11 @@ const base::Feature kUseX11Present{"UseX11Present",
const base::Feature kWebViewVulkanIntermediateBuffer{
"WebViewVulkanIntermediateBuffer", base::FEATURE_DISABLED_BY_DEFAULT};

bool IsAdpfEnabled() {
// TODO(crbug.com/1157620): Limit this to correct android version.
return base::FeatureList::IsEnabled(kAdpf);
}

bool IsOverlayPrioritizationEnabled() {
return base::FeatureList::IsEnabled(kEnableOverlayPrioritization);
}
Expand Down
2 changes: 2 additions & 0 deletions components/viz/common/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace features {

VIZ_COMMON_EXPORT extern const base::Feature kAdpf;
VIZ_COMMON_EXPORT extern const base::Feature kEnableOverlayPrioritization;
VIZ_COMMON_EXPORT extern const base::Feature kUseSkiaRenderer;
VIZ_COMMON_EXPORT extern const base::Feature kRecordSkPicture;
Expand All @@ -38,6 +39,7 @@ VIZ_COMMON_EXPORT extern const base::Feature kUseX11Present;
#endif
VIZ_COMMON_EXPORT extern const base::Feature kWebViewVulkanIntermediateBuffer;

VIZ_COMMON_EXPORT bool IsAdpfEnabled();
VIZ_COMMON_EXPORT bool IsVizHitTestingDebugEnabled();
VIZ_COMMON_EXPORT bool IsUsingSkiaRenderer();
#if defined(OS_ANDROID)
Expand Down
18 changes: 16 additions & 2 deletions components/viz/service/display/display.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,25 @@ void Display::PresentationGroupTiming::OnSwap(gfx::SwapTimings timings) {
}

void Display::PresentationGroupTiming::OnPresent(
const gfx::PresentationFeedback& feedback) {
const gfx::PresentationFeedback& feedback,
DisplaySchedulerBase* scheduler) {
for (auto& presentation_helper : presentation_helpers_) {
presentation_helper->DidPresent(draw_start_timestamp_, swap_timings_,
feedback);
}

if (feedback.ready_timestamp.is_null())
return;

auto gpu_latency = feedback.ready_timestamp - swap_timings_.swap_start;
// TODO(crbug.com/1157620): Move this check to SanitizePresentationFeedback
// to handle all incorrect feedback cases.
if (gpu_latency < base::TimeDelta::FromSeconds(0)) {
DLOG(ERROR) << "Gpu latency is negative : "
<< gpu_latency.InMillisecondsF();
return;
}
scheduler->SetGpuLatency(gpu_latency);
}

Display::Display(
Expand Down Expand Up @@ -991,7 +1005,7 @@ void Display::DidReceivePresentationFeedback(
}
}

presentation_group_timing.OnPresent(copy_feedback);
presentation_group_timing.OnPresent(copy_feedback, scheduler_.get());
pending_presentation_group_timings_.pop_front();
}

Expand Down
3 changes: 2 additions & 1 deletion components/viz/service/display/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ class VIZ_SERVICE_EXPORT Display : public DisplaySchedulerClient,
void OnDraw(base::TimeTicks draw_start_timestamp);
void OnSwap(gfx::SwapTimings timings);
bool HasSwapped() const { return !swap_timings_.is_null(); }
void OnPresent(const gfx::PresentationFeedback& feedback);
void OnPresent(const gfx::PresentationFeedback& feedback,
DisplaySchedulerBase* scheduler);

base::TimeTicks draw_start_timestamp() const {
return draw_start_timestamp_;
Expand Down
19 changes: 18 additions & 1 deletion components/viz/service/display/display_scheduler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "components/viz/service/display/display_scheduler.h"

#include <algorithm>

#include "base/auto_reset.h"
#include "base/trace_event/trace_event.h"

Expand Down Expand Up @@ -40,10 +42,12 @@ class DisplayScheduler::BeginFrameObserver : public BeginFrameObserverBase {
DisplayScheduler::DisplayScheduler(BeginFrameSource* begin_frame_source,
base::SingleThreadTaskRunner* task_runner,
int max_pending_swaps,
bool wait_for_all_surfaces_before_draw)
bool wait_for_all_surfaces_before_draw,
gfx::RenderingPipeline* gpu_pipeline)
: begin_frame_observer_(std::make_unique<BeginFrameObserver>(this)),
begin_frame_source_(begin_frame_source),
task_runner_(task_runner),
gpu_pipeline_(gpu_pipeline),
inside_surface_damaged_(false),
visible_(false),
output_surface_lost_(false),
Expand Down Expand Up @@ -126,6 +130,11 @@ void DisplayScheduler::OutputSurfaceLost() {
ScheduleBeginFrameDeadline();
}

void DisplayScheduler::SetGpuLatency(base::TimeDelta gpu_latency) {
if (gpu_pipeline_)
gpu_pipeline_->SetGpuLatency(gpu_latency);
}

bool DisplayScheduler::DrawAndSwap() {
TRACE_EVENT0("viz", "DisplayScheduler::DrawAndSwap");
DCHECK_LT(pending_swaps_, max_pending_swaps_);
Expand Down Expand Up @@ -179,6 +188,9 @@ bool DisplayScheduler::OnBeginFrame(const BeginFrameArgs& args) {
current_begin_frame_args_.deadline -=
BeginFrameArgs::DefaultEstimatedDisplayDrawTime(save_args.interval);
inside_begin_frame_deadline_interval_ = true;
if (gpu_pipeline_)
gpu_pipeline_->SetTargetDuration(save_args.interval);

UpdateHasPendingSurfaces();
ScheduleBeginFrameDeadline();

Expand All @@ -202,13 +214,16 @@ void DisplayScheduler::StartObservingBeginFrames() {
if (!observing_begin_frame_source_) {
begin_frame_source_->AddObserver(begin_frame_observer_.get());
observing_begin_frame_source_ = true;
if (gpu_pipeline_)
gpu_pipeline_active_.emplace(gpu_pipeline_);
}
}

void DisplayScheduler::StopObservingBeginFrames() {
if (observing_begin_frame_source_) {
begin_frame_source_->RemoveObserver(begin_frame_observer_.get());
observing_begin_frame_source_ = false;
gpu_pipeline_active_.reset();

// A missed BeginFrame may be queued, so drop that too if we're going to
// stop listening.
Expand Down Expand Up @@ -373,6 +388,8 @@ void DisplayScheduler::OnBeginFrameDeadline() {

bool did_draw = AttemptDrawAndSwap();
DidFinishFrame(did_draw);
if (gpu_pipeline_)
gpu_pipeline_->NotifyFrameFinished();
}

void DisplayScheduler::DidFinishFrame(bool did_draw) {
Expand Down
8 changes: 7 additions & 1 deletion components/viz/service/display/display_scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "components/viz/common/surfaces/surface_id.h"
#include "components/viz/service/display/display_scheduler_base.h"
#include "components/viz/service/viz_service_export.h"
#include "ui/gfx/rendering_pipeline.h"

namespace viz {

Expand All @@ -27,7 +28,8 @@ class VIZ_SERVICE_EXPORT DisplayScheduler : public DisplaySchedulerBase {
DisplayScheduler(BeginFrameSource* begin_frame_source,
base::SingleThreadTaskRunner* task_runner,
int max_pending_swaps,
bool wait_for_all_surfaces_before_draw = false);
bool wait_for_all_surfaces_before_draw = false,
gfx::RenderingPipeline* gpu_pipeline = nullptr);
~DisplayScheduler() override;

// DisplaySchedulerBase implementation.
Expand All @@ -37,6 +39,7 @@ class VIZ_SERVICE_EXPORT DisplayScheduler : public DisplaySchedulerBase {
void DidSwapBuffers() override;
void DidReceiveSwapBuffersAck() override;
void OutputSurfaceLost() override;
void SetGpuLatency(base::TimeDelta gpu_latency) override;

// DisplayDamageTrackerObserver implementation.
void OnDisplayDamaged(SurfaceId surface_id) override;
Expand Down Expand Up @@ -95,6 +98,9 @@ class VIZ_SERVICE_EXPORT DisplayScheduler : public DisplaySchedulerBase {
std::unique_ptr<BeginFrameObserver> begin_frame_observer_;
BeginFrameSource* begin_frame_source_;
base::SingleThreadTaskRunner* task_runner_;
gfx::RenderingPipeline* gpu_pipeline_;
base::Optional<gfx::RenderingPipeline::ScopedPipelineActive>
gpu_pipeline_active_;

BeginFrameArgs current_begin_frame_args_;
base::RepeatingClosure begin_frame_deadline_closure_;
Expand Down
1 change: 1 addition & 0 deletions components/viz/service/display/display_scheduler_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class VIZ_SERVICE_EXPORT DisplaySchedulerBase
virtual void DidSwapBuffers() = 0;
virtual void DidReceiveSwapBuffersAck() = 0;
virtual void OutputSurfaceLost() = 0;
virtual void SetGpuLatency(base::TimeDelta gpu_latency) = 0;

protected:
DisplaySchedulerClient* client_ = nullptr;
Expand Down
5 changes: 3 additions & 2 deletions components/viz/service/frame_sinks/frame_sink_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ FrameSinkManagerImpl::FrameSinkManagerImpl(const InitParams& params)
run_all_compositor_stages_before_draw_(
params.run_all_compositor_stages_before_draw),
log_capture_pipeline_in_webrtc_(params.log_capture_pipeline_in_webrtc),
debug_settings_(params.debug_renderer_settings) {
debug_settings_(params.debug_renderer_settings),
gpu_pipeline_(params.gpu_pipeline) {
surface_manager_.AddObserver(&hit_test_manager_);
surface_manager_.AddObserver(this);
}
Expand Down Expand Up @@ -176,7 +177,7 @@ void FrameSinkManagerImpl::CreateRootCompositorFrameSink(
// Creating RootCompositorFrameSinkImpl can fail and return null.
auto root_compositor_frame_sink = RootCompositorFrameSinkImpl::Create(
std::move(params), this, output_surface_provider_, restart_id_,
run_all_compositor_stages_before_draw_, &debug_settings_);
run_all_compositor_stages_before_draw_, &debug_settings_, gpu_pipeline_);

if (root_compositor_frame_sink)
root_sink_map_[frame_sink_id] = std::move(root_compositor_frame_sink);
Expand Down
3 changes: 3 additions & 0 deletions components/viz/service/frame_sinks/frame_sink_manager_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class VIZ_SERVICE_EXPORT FrameSinkManagerImpl
bool run_all_compositor_stages_before_draw = false;
bool log_capture_pipeline_in_webrtc = false;
DebugRendererSettings debug_renderer_settings;
gfx::RenderingPipeline* gpu_pipeline = nullptr;
};
explicit FrameSinkManagerImpl(const InitParams& params);
// TODO(kylechar): Cleanup tests and remove this constructor.
Expand Down Expand Up @@ -373,6 +374,8 @@ class VIZ_SERVICE_EXPORT FrameSinkManagerImpl

base::ObserverList<FrameSinkObserver>::Unchecked observer_list_;

gfx::RenderingPipeline* gpu_pipeline_ = nullptr;

DISALLOW_COPY_AND_ASSIGN(FrameSinkManagerImpl);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ RootCompositorFrameSinkImpl::Create(
OutputSurfaceProvider* output_surface_provider,
uint32_t restart_id,
bool run_all_compositor_stages_before_draw,
const DebugRendererSettings* debug_settings) {
const DebugRendererSettings* debug_settings,
gfx::RenderingPipeline* gpu_pipeline) {
// First create an output surface.
mojo::Remote<mojom::DisplayClient> display_client(
std::move(params->display_client));
Expand Down Expand Up @@ -127,7 +128,7 @@ RootCompositorFrameSinkImpl::Create(

auto scheduler = std::make_unique<DisplayScheduler>(
begin_frame_source, task_runner.get(), max_frames_pending,
run_all_compositor_stages_before_draw);
run_all_compositor_stages_before_draw, gpu_pipeline);

#if !defined(OS_APPLE)
auto* output_surface_ptr = output_surface.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include "services/viz/privileged/mojom/compositing/frame_sink_manager.mojom.h"
#include "services/viz/public/mojom/compositing/compositor_frame_sink.mojom.h"

namespace gfx {
class RenderingPipeline;
}

namespace viz {

class Display;
Expand All @@ -47,7 +51,8 @@ class RootCompositorFrameSinkImpl : public mojom::CompositorFrameSink,
OutputSurfaceProvider* output_surface_provider,
uint32_t restart_id,
bool run_all_compositor_stages_before_draw,
const DebugRendererSettings* debug_settings);
const DebugRendererSettings* debug_settings,
gfx::RenderingPipeline* gpu_pipeline);

~RootCompositorFrameSinkImpl() override;

Expand Down
8 changes: 7 additions & 1 deletion components/viz/service/main/viz_compositor_thread_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ namespace base {
class SingleThreadTaskRunner;
}

namespace gfx {
class RenderingPipeline;
}

namespace gpu {
class CommandBufferTaskExecutor;
} // namespace gpu
Expand All @@ -31,6 +35,7 @@ class VizCompositorThreadRunner {

// Returns the TaskRunner for VizCompositorThread.
virtual base::SingleThreadTaskRunner* task_runner() = 0;
virtual base::PlatformThreadId thread_id() = 0;

// Creates FrameSinkManager from |params|. The version with |gpu_service| and
// |task_executor| supports both GPU and software compositing, while the
Expand All @@ -41,7 +46,8 @@ class VizCompositorThreadRunner {
virtual void CreateFrameSinkManager(
mojom::FrameSinkManagerParamsPtr params,
gpu::CommandBufferTaskExecutor* task_executor,
GpuServiceImpl* gpu_service) = 0;
GpuServiceImpl* gpu_service,
gfx::RenderingPipeline* gpu_pipeline) = 0;

#if BUILDFLAG(USE_VIZ_DEVTOOLS)
virtual void CreateVizDevTools(mojom::VizDevToolsParamsPtr params) = 0;
Expand Down
Loading

0 comments on commit 6bc7619

Please sign in to comment.