1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/vr/browser_renderer.h"
#include <utility>
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "base/trace_event/common/trace_event_common.h"
#include "base/trace_event/trace_event.h"
#include "chrome/browser/vr/render_info.h"
#include "chrome/browser/vr/ui_interface.h"
#include "chrome/browser/vr/ui_test_input.h"
namespace vr {
BrowserRenderer::BrowserRenderer(
std::unique_ptr<UiInterface> ui,
std::unique_ptr<GraphicsDelegate> graphics_delegate,
size_t sliding_time_size)
: graphics_delegate_(std::move(graphics_delegate)),
ui_processing_time_(sliding_time_size),
ui_(std::move(ui)) {}
BrowserRenderer::~BrowserRenderer() = default;
void BrowserRenderer::DrawBrowserFrame(base::TimeTicks current_time,
const gfx::Transform& head_pose) {
Draw(kUiFrame, current_time, head_pose);
}
void BrowserRenderer::DrawWebXrFrame(base::TimeTicks current_time,
const gfx::Transform& head_pose) {
Draw(kWebXrFrame, current_time, head_pose);
}
void BrowserRenderer::Draw(FrameType frame_type,
base::TimeTicks current_time,
const gfx::Transform& head_pose) {
TRACE_EVENT1("gpu", "BrowserRenderer::Draw", "frame_type", frame_type);
const auto& render_info =
graphics_delegate_->GetRenderInfo(frame_type, head_pose);
UpdateUi(render_info, current_time, frame_type);
if (frame_type == kWebXrFrame) {
if (ui_->HasWebXrOverlayElementsToDraw()) {
DrawWebXrOverlay(render_info);
}
} else {
DrawBrowserUi(render_info);
}
TRACE_COUNTER1("gpu", "VR UI timing (us)",
ui_processing_time_.GetAverage().InMicroseconds());
}
void BrowserRenderer::DrawWebXrOverlay(const RenderInfo& render_info) {
TRACE_EVENT0("gpu", "BrowserRenderer::DrawWebXrOverlay");
// Calculate optimized viewport and corresponding render info.
const auto& recommended_fovs = graphics_delegate_->GetRecommendedFovs();
const auto& fovs = ui_->GetMinimalFovForWebXrOverlayElements(
render_info.left_eye_model.view_matrix, recommended_fovs.first,
render_info.right_eye_model.view_matrix, recommended_fovs.second,
graphics_delegate_->GetZNear());
const auto& webxr_overlay_render_info =
graphics_delegate_->GetOptimizedRenderInfoForFovs(fovs);
ui_->DrawWebVrOverlayForeground(webxr_overlay_render_info);
}
void BrowserRenderer::DrawBrowserUi(const RenderInfo& render_info) {
TRACE_EVENT0("gpu", "BrowserRenderer::DrawBrowserUi");
ui_->Draw(render_info);
}
void BrowserRenderer::WatchElementForVisibilityStatusForTesting(
std::optional<UiVisibilityState> visibility_expectation) {
DCHECK(!ui_visibility_state_.has_value() ||
!visibility_expectation.has_value())
<< "Attempted to watch a UI element "
"for visibility changes with one "
"in progress";
ui_visibility_state_ = std::move(visibility_expectation);
}
void BrowserRenderer::UpdateUi(const RenderInfo& render_info,
base::TimeTicks current_time,
FrameType frame_type) {
TRACE_EVENT0("gpu", "UpdateUi");
// Update the render position of all UI elements.
base::TimeTicks timing_start = base::TimeTicks::Now();
ui_->OnBeginFrame(current_time, render_info.head_pose);
if (ui_->SceneHasDirtyTextures()) {
ui_->UpdateSceneTextures();
}
ReportElementVisibilityStatus(timing_start);
base::TimeDelta scene_time = base::TimeTicks::Now() - timing_start;
// Don't double-count the controller time that was part of the scene time.
ui_processing_time_.AddSample(scene_time);
}
void BrowserRenderer::ReportElementVisibilityStatus(
const base::TimeTicks& current_time) {
if (!ui_visibility_state_.has_value()) {
return;
}
base::TimeDelta time_since_start =
current_time - ui_visibility_state_->start_time;
if (ui_->GetElementVisibility(ui_visibility_state_->element_to_watch) ==
ui_visibility_state_->expected_visibile) {
ReportElementVisibilityResult(true); // IN-TEST
} else if (time_since_start > ui_visibility_state_->timeout_ms) {
ReportElementVisibilityResult(false); // IN-TEST
}
}
void BrowserRenderer::ReportElementVisibilityResult(bool result) {
// Grab the callback and then destroy 'ui_visibility_state_' to prevent
// re-entrant behavior being blocked by having the 'ui_visibility_state_' or
// overwriting it and then dropping our callback.
auto callback = std::move(ui_visibility_state_->on_visibility_change_result);
ui_visibility_state_ = std::nullopt;
std::move(callback).Run(result);
}
} // namespace vr
|