blob: df64e027b8f8b35984aa671a2c462f577e463ce1 [file] [log] [blame]
Aldo Culquicondorcc004c72018-08-02 14:50:021// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Aldo Culquicondor34610c1d2018-09-07 21:03:135#include "chrome/browser/vr/browser_renderer.h"
Aldo Culquicondorcc004c72018-08-02 14:50:026
7#include <utility>
8
Sebastien Marchandf1349f52019-01-25 03:16:419#include "base/bind.h"
Aldo Culquicondorcc004c72018-08-02 14:50:0210#include "base/time/time.h"
Aldo Culquicondor41532412018-08-07 22:36:4411#include "base/trace_event/common/trace_event_common.h"
James Cook500a245e2019-10-02 19:18:3512#include "base/trace_event/trace_event.h"
Aldo Culquicondor34610c1d2018-09-07 21:03:1313#include "chrome/browser/vr/browser_renderer_browser_interface.h"
Aldo Culquicondorb915d1ef2018-09-11 23:10:3514#include "chrome/browser/vr/input_delegate_for_testing.h"
Aldo Culquicondorcc004c72018-08-02 14:50:0215#include "chrome/browser/vr/input_event.h"
16#include "chrome/browser/vr/model/controller_model.h"
Aldo Culquicondor3c315ea02018-08-13 20:45:0917#include "chrome/browser/vr/model/reticle_model.h"
Aldo Culquicondor5dc384bf2018-08-22 23:42:3618#include "chrome/browser/vr/platform_ui_input_delegate.h"
Aldo Culquicondor3c315ea02018-08-13 20:45:0919#include "chrome/browser/vr/render_info.h"
Aldo Culquicondor5dc384bf2018-08-22 23:42:3620#include "chrome/browser/vr/scheduler_delegate.h"
Aldo Culquicondorcc004c72018-08-02 14:50:0221#include "chrome/browser/vr/ui_interface.h"
Aldo Culquicondor41532412018-08-07 22:36:4422#include "chrome/browser/vr/ui_test_input.h"
Aldo Culquicondorcc004c72018-08-02 14:50:0223
24namespace vr {
25
Aldo Culquicondor34610c1d2018-09-07 21:03:1326BrowserRenderer::BrowserRenderer(
27 std::unique_ptr<UiInterface> ui,
28 std::unique_ptr<SchedulerDelegate> scheduler_delegate,
29 std::unique_ptr<GraphicsDelegate> graphics_delegate,
Aldo Culquicondorb915d1ef2018-09-11 23:10:3530 std::unique_ptr<InputDelegate> input_delegate,
Aldo Culquicondor34610c1d2018-09-07 21:03:1331 BrowserRendererBrowserInterface* browser,
32 size_t sliding_time_size)
Bill Orrcee3abc2018-11-07 01:00:2233 : scheduler_delegate_(std::move(scheduler_delegate)),
Aldo Culquicondor34610c1d2018-09-07 21:03:1334 graphics_delegate_(std::move(graphics_delegate)),
Aldo Culquicondorb915d1ef2018-09-11 23:10:3535 input_delegate_(std::move(input_delegate)),
Aldo Culquicondor173c0a12018-08-16 15:39:0836 browser_(browser),
Aldo Culquicondor41532412018-08-07 22:36:4437 ui_processing_time_(sliding_time_size),
Aldo Culquicondor764626c2018-08-23 20:14:4738 ui_controller_update_time_(sliding_time_size),
Jeremy Roman495db682019-07-12 16:03:2439 ui_(std::move(ui)) {
Aldo Culquicondor34610c1d2018-09-07 21:03:1340 scheduler_delegate_->SetBrowserRenderer(this);
Aldo Culquicondor5dc384bf2018-08-22 23:42:3641}
42
Aldo Culquicondor34610c1d2018-09-07 21:03:1343BrowserRenderer::~BrowserRenderer() = default;
Aldo Culquicondorcc004c72018-08-02 14:50:0244
Aldo Culquicondor34610c1d2018-09-07 21:03:1345void BrowserRenderer::DrawBrowserFrame(base::TimeTicks current_time) {
Aldo Culquicondorb915d1ef2018-09-11 23:10:3546 Draw(kUiFrame, current_time, input_delegate_->GetHeadPose());
Aldo Culquicondorb8985eb2018-09-06 22:27:4447}
48
Aldo Culquicondorb915d1ef2018-09-11 23:10:3549void BrowserRenderer::DrawWebXrFrame(base::TimeTicks current_time,
50 const gfx::Transform& head_pose) {
51 Draw(kWebXrFrame, current_time, head_pose);
Aldo Culquicondorb8985eb2018-09-06 22:27:4452}
53
Aldo Culquicondorb915d1ef2018-09-11 23:10:3554void BrowserRenderer::Draw(FrameType frame_type,
55 base::TimeTicks current_time,
56 const gfx::Transform& head_pose) {
Aldo Culquicondor5dc384bf2018-08-22 23:42:3657 TRACE_EVENT1("gpu", __func__, "frame_type", frame_type);
Aldo Culquicondorb915d1ef2018-09-11 23:10:3558 const auto& render_info =
59 graphics_delegate_->GetRenderInfo(frame_type, head_pose);
Aldo Culquicondor5dc384bf2018-08-22 23:42:3660 UpdateUi(render_info, current_time, frame_type);
61 ui_->OnProjMatrixChanged(render_info.left_eye_model.proj_matrix);
62 bool use_quad_layer = ui_->IsContentVisibleAndOpaque() &&
Aldo Culquicondor34610c1d2018-09-07 21:03:1363 graphics_delegate_->IsContentQuadReady();
Aldo Culquicondor5dc384bf2018-08-22 23:42:3664 ui_->SetContentUsesQuadLayer(use_quad_layer);
65
Aldo Culquicondor34610c1d2018-09-07 21:03:1366 graphics_delegate_->InitializeBuffers();
bsheedyfa3e4422018-09-18 21:39:3167 graphics_delegate_->SetFrameDumpFilepathBase(
68 frame_buffer_dump_filepath_base_);
Aldo Culquicondorb915d1ef2018-09-11 23:10:3569 if (frame_type == kWebXrFrame) {
Aldo Culquicondor5dc384bf2018-08-22 23:42:3670 DCHECK(!use_quad_layer);
71 DrawWebXr();
72 if (ui_->HasWebXrOverlayElementsToDraw())
73 DrawWebXrOverlay(render_info);
74 } else {
75 if (use_quad_layer)
76 DrawContentQuad();
77 DrawBrowserUi(render_info);
78 }
79
Aldo Culquicondor764626c2018-08-23 20:14:4780 TRACE_COUNTER2("gpu", "VR UI timing (us)", "scene update",
81 ui_processing_time_.GetAverage().InMicroseconds(),
82 "controller",
83 ui_controller_update_time_.GetAverage().InMicroseconds());
Aldo Culquicondorb915d1ef2018-09-11 23:10:3584
bsheedyfa3e4422018-09-18 21:39:3185 ReportFrameBufferDumpForTesting();
Aldo Culquicondorb915d1ef2018-09-11 23:10:3586 scheduler_delegate_->SubmitDrawnFrame(frame_type, head_pose);
Aldo Culquicondor5dc384bf2018-08-22 23:42:3687}
88
Aldo Culquicondor34610c1d2018-09-07 21:03:1389void BrowserRenderer::DrawWebXr() {
Aldo Culquicondor5dc384bf2018-08-22 23:42:3690 TRACE_EVENT0("gpu", __func__);
Aldo Culquicondor34610c1d2018-09-07 21:03:1391 graphics_delegate_->PrepareBufferForWebXr();
Aldo Culquicondor5dc384bf2018-08-22 23:42:3692
93 int texture_id;
Aldo Culquicondor34610c1d2018-09-07 21:03:1394 GraphicsDelegate::Transform uv_transform;
95 graphics_delegate_->GetWebXrDrawParams(&texture_id, &uv_transform);
Aldo Culquicondor5dc384bf2018-08-22 23:42:3696 ui_->DrawWebXr(texture_id, uv_transform);
Aldo Culquicondor34610c1d2018-09-07 21:03:1397 graphics_delegate_->OnFinishedDrawingBuffer();
Aldo Culquicondor5dc384bf2018-08-22 23:42:3698}
99
Aldo Culquicondor34610c1d2018-09-07 21:03:13100void BrowserRenderer::DrawWebXrOverlay(const RenderInfo& render_info) {
Aldo Culquicondor5dc384bf2018-08-22 23:42:36101 TRACE_EVENT0("gpu", __func__);
102 // Calculate optimized viewport and corresponding render info.
Aldo Culquicondor34610c1d2018-09-07 21:03:13103 const auto& recommended_fovs = graphics_delegate_->GetRecommendedFovs();
Aldo Culquicondor5dc384bf2018-08-22 23:42:36104 const auto& fovs = ui_->GetMinimalFovForWebXrOverlayElements(
105 render_info.left_eye_model.view_matrix, recommended_fovs.first,
106 render_info.right_eye_model.view_matrix, recommended_fovs.second,
Aldo Culquicondor34610c1d2018-09-07 21:03:13107 graphics_delegate_->GetZNear());
Aldo Culquicondor5dc384bf2018-08-22 23:42:36108 const auto& webxr_overlay_render_info =
Aldo Culquicondor34610c1d2018-09-07 21:03:13109 graphics_delegate_->GetOptimizedRenderInfoForFovs(fovs);
Aldo Culquicondor5dc384bf2018-08-22 23:42:36110
Aldo Culquicondor34610c1d2018-09-07 21:03:13111 graphics_delegate_->PrepareBufferForWebXrOverlayElements();
Aldo Culquicondor5dc384bf2018-08-22 23:42:36112 ui_->DrawWebVrOverlayForeground(webxr_overlay_render_info);
Aldo Culquicondor34610c1d2018-09-07 21:03:13113 graphics_delegate_->OnFinishedDrawingBuffer();
Aldo Culquicondor5dc384bf2018-08-22 23:42:36114}
115
Aldo Culquicondor34610c1d2018-09-07 21:03:13116void BrowserRenderer::DrawContentQuad() {
Aldo Culquicondor5dc384bf2018-08-22 23:42:36117 TRACE_EVENT0("gpu", __func__);
Aldo Culquicondor34610c1d2018-09-07 21:03:13118 graphics_delegate_->PrepareBufferForContentQuadLayer(
Aldo Culquicondor5dc384bf2018-08-22 23:42:36119 ui_->GetContentWorldSpaceTransform());
120
Aldo Culquicondor34610c1d2018-09-07 21:03:13121 GraphicsDelegate::Transform uv_transform;
Aldo Culquicondor5dc384bf2018-08-22 23:42:36122 float border_x;
123 float border_y;
Aldo Culquicondor34610c1d2018-09-07 21:03:13124 graphics_delegate_->GetContentQuadDrawParams(&uv_transform, &border_x,
125 &border_y);
Aldo Culquicondor5dc384bf2018-08-22 23:42:36126 ui_->DrawContent(uv_transform, border_x, border_y);
Aldo Culquicondor34610c1d2018-09-07 21:03:13127 graphics_delegate_->OnFinishedDrawingBuffer();
Aldo Culquicondor5dc384bf2018-08-22 23:42:36128}
129
Aldo Culquicondor34610c1d2018-09-07 21:03:13130void BrowserRenderer::DrawBrowserUi(const RenderInfo& render_info) {
Aldo Culquicondor5dc384bf2018-08-22 23:42:36131 TRACE_EVENT0("gpu", __func__);
Aldo Culquicondor34610c1d2018-09-07 21:03:13132 graphics_delegate_->PrepareBufferForBrowserUi();
Aldo Culquicondor5dc384bf2018-08-22 23:42:36133 ui_->Draw(render_info);
Aldo Culquicondor34610c1d2018-09-07 21:03:13134 graphics_delegate_->OnFinishedDrawingBuffer();
Aldo Culquicondor5dc384bf2018-08-22 23:42:36135}
136
Aldo Culquicondor34610c1d2018-09-07 21:03:13137void BrowserRenderer::OnPause() {
Aldo Culquicondorb915d1ef2018-09-11 23:10:35138 DCHECK(input_delegate_);
139 input_delegate_->OnPause();
Aldo Culquicondor764626c2018-08-23 20:14:47140 scheduler_delegate_->OnPause();
Aldo Culquicondor880ba3d2018-08-08 19:35:14141 ui_->OnPause();
142}
143
Aldo Culquicondor34610c1d2018-09-07 21:03:13144void BrowserRenderer::OnResume() {
Aldo Culquicondorb915d1ef2018-09-11 23:10:35145 DCHECK(input_delegate_);
Aldo Culquicondor764626c2018-08-23 20:14:47146 scheduler_delegate_->OnResume();
Aldo Culquicondorb915d1ef2018-09-11 23:10:35147 input_delegate_->OnResume();
Aldo Culquicondor880ba3d2018-08-08 19:35:14148}
149
Aldo Culquicondor34610c1d2018-09-07 21:03:13150void BrowserRenderer::OnExitPresent() {
Aldo Culquicondor764626c2018-08-23 20:14:47151 scheduler_delegate_->OnExitPresent();
152}
153
Aldo Culquicondor34610c1d2018-09-07 21:03:13154void BrowserRenderer::OnTriggerEvent(bool pressed) {
Aldo Culquicondor2df78d12018-09-18 19:50:47155 input_delegate_->OnTriggerEvent(pressed);
Aldo Culquicondor764626c2018-08-23 20:14:47156}
157
Aldo Culquicondor34610c1d2018-09-07 21:03:13158void BrowserRenderer::SetWebXrMode(bool enabled) {
Aldo Culquicondor764626c2018-08-23 20:14:47159 scheduler_delegate_->SetWebXrMode(enabled);
160}
161
Aldo Culquicondor34610c1d2018-09-07 21:03:13162void BrowserRenderer::EnableAlertDialog(PlatformInputHandler* input_handler,
163 float width,
164 float height) {
Aldo Culquicondorb8985eb2018-09-06 22:27:44165 scheduler_delegate_->SetShowingVrDialog(true);
Aldo Culquicondor5dc384bf2018-08-22 23:42:36166 vr_dialog_input_delegate_ =
167 std::make_unique<PlatformUiInputDelegate>(input_handler);
168 vr_dialog_input_delegate_->SetSize(width, height);
Aldo Culquicondorb8985eb2018-09-06 22:27:44169 if (ui_->IsContentVisibleAndOpaque()) {
Aldo Culquicondor34610c1d2018-09-07 21:03:13170 auto content_width = graphics_delegate_->GetContentBufferWidth();
Aldo Culquicondorb8985eb2018-09-06 22:27:44171 DCHECK(content_width);
Aldo Culquicondor5dc384bf2018-08-22 23:42:36172 ui_->SetContentOverlayAlertDialogEnabled(
173 true, vr_dialog_input_delegate_.get(), width / content_width,
174 height / content_width);
175 } else {
176 ui_->SetAlertDialogEnabled(true, vr_dialog_input_delegate_.get(), width,
177 height);
178 }
179}
180
Aldo Culquicondor34610c1d2018-09-07 21:03:13181void BrowserRenderer::DisableAlertDialog() {
Aldo Culquicondor5dc384bf2018-08-22 23:42:36182 ui_->SetAlertDialogEnabled(false, nullptr, 0, 0);
183 vr_dialog_input_delegate_ = nullptr;
Aldo Culquicondorb8985eb2018-09-06 22:27:44184 scheduler_delegate_->SetShowingVrDialog(false);
Aldo Culquicondor5dc384bf2018-08-22 23:42:36185}
186
Aldo Culquicondor34610c1d2018-09-07 21:03:13187void BrowserRenderer::SetAlertDialogSize(float width, float height) {
Aldo Culquicondor5dc384bf2018-08-22 23:42:36188 if (vr_dialog_input_delegate_)
189 vr_dialog_input_delegate_->SetSize(width, height);
190 // If not floating, dialogs are rendered with a fixed width, so that only the
191 // ratio matters. But, if they are floating, its size should be relative to
192 // the contents. During a WebXR presentation, the contents are not present
193 // but, in this case, the dialogs are never floating.
Aldo Culquicondorb8985eb2018-09-06 22:27:44194 if (ui_->IsContentVisibleAndOpaque()) {
Aldo Culquicondor34610c1d2018-09-07 21:03:13195 auto content_width = graphics_delegate_->GetContentBufferWidth();
Aldo Culquicondorb8985eb2018-09-06 22:27:44196 DCHECK(content_width);
Aldo Culquicondor5dc384bf2018-08-22 23:42:36197 ui_->SetContentOverlayAlertDialogEnabled(
198 true, vr_dialog_input_delegate_.get(), width / content_width,
199 height / content_width);
200 } else {
201 ui_->SetAlertDialogEnabled(true, vr_dialog_input_delegate_.get(), width,
202 height);
203 }
204}
205
Aldo Culquicondor34610c1d2018-09-07 21:03:13206void BrowserRenderer::ResumeContentRendering() {
207 graphics_delegate_->ResumeContentRendering();
Aldo Culquicondor764626c2018-08-23 20:14:47208}
209
Aldo Culquicondor34610c1d2018-09-07 21:03:13210void BrowserRenderer::BufferBoundsChanged(
211 const gfx::Size& content_buffer_size,
212 const gfx::Size& overlay_buffer_size) {
213 graphics_delegate_->BufferBoundsChanged(content_buffer_size,
214 overlay_buffer_size);
Aldo Culquicondor764626c2018-08-23 20:14:47215}
216
Aldo Culquicondor34610c1d2018-09-07 21:03:13217base::WeakPtr<BrowserUiInterface> BrowserRenderer::GetBrowserUiWeakPtr() {
Aldo Culquicondor5dc384bf2018-08-22 23:42:36218 return ui_->GetBrowserUiWeakPtr();
219}
220
Aldo Culquicondor34610c1d2018-09-07 21:03:13221void BrowserRenderer::SetUiExpectingActivityForTesting(
Aldo Culquicondor41532412018-08-07 22:36:44222 UiTestActivityExpectation ui_expectation) {
223 DCHECK(ui_test_state_ == nullptr)
224 << "Attempted to set a UI activity expectation with one in progress";
225 ui_test_state_ = std::make_unique<UiTestState>();
226 ui_test_state_->quiescence_timeout_ms =
227 base::TimeDelta::FromMilliseconds(ui_expectation.quiescence_timeout_ms);
228}
229
bsheedyfa3e4422018-09-18 21:39:31230void BrowserRenderer::SaveNextFrameBufferToDiskForTesting(
231 std::string filepath_base) {
232 frame_buffer_dump_filepath_base_ = filepath_base;
233}
234
bsheedydc7c6cb2018-11-05 20:48:45235void BrowserRenderer::WatchElementForVisibilityStatusForTesting(
bsheedya2ff3432018-10-02 18:16:58236 VisibilityChangeExpectation visibility_expectation) {
237 DCHECK(ui_visibility_state_ == nullptr) << "Attempted to watch a UI element "
238 "for visibility changes with one "
239 "in progress";
240 ui_visibility_state_ = std::make_unique<UiVisibilityState>();
241 ui_visibility_state_->timeout_ms =
242 base::TimeDelta::FromMilliseconds(visibility_expectation.timeout_ms);
243 ui_visibility_state_->element_to_watch = visibility_expectation.element_name;
bsheedydc7c6cb2018-11-05 20:48:45244 ui_visibility_state_->expected_visibile = visibility_expectation.visibility;
bsheedya2ff3432018-10-02 18:16:58245}
246
Aldo Culquicondor34610c1d2018-09-07 21:03:13247void BrowserRenderer::AcceptDoffPromptForTesting() {
Aldo Culquicondor5dc384bf2018-08-22 23:42:36248 ui_->AcceptDoffPromptForTesting();
249}
250
bsheedyacb0f352019-01-16 23:30:14251void BrowserRenderer::SetBrowserRendererBrowserInterfaceForTesting(
Eric Lawrence138de76f2019-01-17 19:15:06252 BrowserRendererBrowserInterface* interface_ptr) {
253 browser_ = interface_ptr;
bsheedyacb0f352019-01-16 23:30:14254}
255
Aldo Culquicondor34610c1d2018-09-07 21:03:13256void BrowserRenderer::UpdateUi(const RenderInfo& render_info,
257 base::TimeTicks current_time,
Aldo Culquicondorb915d1ef2018-09-11 23:10:35258 FrameType frame_type) {
Aldo Culquicondor41532412018-08-07 22:36:44259 TRACE_EVENT0("gpu", __func__);
Aldo Culquicondor41532412018-08-07 22:36:44260
261 // Update the render position of all UI elements.
262 base::TimeTicks timing_start = base::TimeTicks::Now();
263 bool ui_updated = ui_->OnBeginFrame(current_time, render_info.head_pose);
264
265 // WebXR handles controller input in OnVsync.
266 base::TimeDelta controller_time;
Aldo Culquicondorb915d1ef2018-09-11 23:10:35267 if (frame_type == kUiFrame)
Aldo Culquicondor880ba3d2018-08-08 19:35:14268 controller_time = ProcessControllerInput(render_info, current_time);
Aldo Culquicondor41532412018-08-07 22:36:44269
270 if (ui_->SceneHasDirtyTextures()) {
Aldo Culquicondor34610c1d2018-09-07 21:03:13271 if (!graphics_delegate_->RunInSkiaContext(base::BindOnce(
Aldo Culquicondor7e4d79652018-08-15 19:11:54272 &UiInterface::UpdateSceneTextures, base::Unretained(ui_.get())))) {
Aldo Culquicondor764626c2018-08-23 20:14:47273 browser_->ForceExitVr();
Aldo Culquicondor41532412018-08-07 22:36:44274 return;
275 }
276 ui_updated = true;
277 }
278 ReportUiStatusForTesting(timing_start, ui_updated);
bsheedya2ff3432018-10-02 18:16:58279 ReportElementVisibilityStatusForTesting(timing_start);
Aldo Culquicondor41532412018-08-07 22:36:44280
281 base::TimeDelta scene_time = base::TimeTicks::Now() - timing_start;
282 // Don't double-count the controller time that was part of the scene time.
283 ui_processing_time_.AddSample(scene_time - controller_time);
Aldo Culquicondor41532412018-08-07 22:36:44284}
285
Aldo Culquicondor34610c1d2018-09-07 21:03:13286void BrowserRenderer::ProcessControllerInputForWebXr(
Klaus Weidnerd3e94342019-05-30 04:21:53287 const gfx::Transform& head_pose,
Aldo Culquicondor34610c1d2018-09-07 21:03:13288 base::TimeTicks current_time) {
Aldo Culquicondorbede7f322018-09-12 21:21:06289 TRACE_EVENT0("gpu", "Vr.ProcessControllerInputForWebXr");
Aldo Culquicondorb915d1ef2018-09-11 23:10:35290 DCHECK(input_delegate_);
Aldo Culquicondorcc004c72018-08-02 14:50:02291 DCHECK(ui_);
Aldo Culquicondor41532412018-08-07 22:36:44292 base::TimeTicks timing_start = base::TimeTicks::Now();
Aldo Culquicondorcc004c72018-08-02 14:50:02293
Klaus Weidnerd3e94342019-05-30 04:21:53294 input_delegate_->UpdateController(head_pose, current_time, true);
Aldo Culquicondorb915d1ef2018-09-11 23:10:35295 auto input_event_list = input_delegate_->GetGestures(current_time);
Aldo Culquicondor880ba3d2018-08-08 19:35:14296 ui_->HandleMenuButtonEvents(&input_event_list);
297
298 ui_controller_update_time_.AddSample(base::TimeTicks::Now() - timing_start);
Aldo Culquicondor764626c2018-08-23 20:14:47299
300 scheduler_delegate_->AddInputSourceState(
Aldo Culquicondorb915d1ef2018-09-11 23:10:35301 input_delegate_->GetInputSourceState());
Aldo Culquicondor764626c2018-08-23 20:14:47302}
303
Aldo Culquicondor34610c1d2018-09-07 21:03:13304void BrowserRenderer::ConnectPresentingService(
Aldo Culquicondor764626c2018-08-23 20:14:47305 device::mojom::VRDisplayInfoPtr display_info,
306 device::mojom::XRRuntimeSessionOptionsPtr options) {
Aldo Culquicondorb8985eb2018-09-06 22:27:44307 scheduler_delegate_->ConnectPresentingService(std::move(display_info),
308 std::move(options));
Aldo Culquicondor880ba3d2018-08-08 19:35:14309}
310
Aldo Culquicondor34610c1d2018-09-07 21:03:13311base::TimeDelta BrowserRenderer::ProcessControllerInput(
Aldo Culquicondor880ba3d2018-08-08 19:35:14312 const RenderInfo& render_info,
313 base::TimeTicks current_time) {
Aldo Culquicondorbede7f322018-09-12 21:21:06314 TRACE_EVENT0("gpu", "Vr.ProcessControllerInput");
Aldo Culquicondorb915d1ef2018-09-11 23:10:35315 DCHECK(input_delegate_);
Aldo Culquicondor880ba3d2018-08-08 19:35:14316 DCHECK(ui_);
317 base::TimeTicks timing_start = base::TimeTicks::Now();
318
Aldo Culquicondorb915d1ef2018-09-11 23:10:35319 input_delegate_->UpdateController(render_info.head_pose, current_time, false);
320 auto input_event_list = input_delegate_->GetGestures(current_time);
Aldo Culquicondor880ba3d2018-08-08 19:35:14321 ReticleModel reticle_model;
322 ControllerModel controller_model =
Aldo Culquicondorb915d1ef2018-09-11 23:10:35323 input_delegate_->GetControllerModel(render_info.head_pose);
Aldo Culquicondor880ba3d2018-08-08 19:35:14324 ui_->HandleInput(current_time, render_info, controller_model, &reticle_model,
325 &input_event_list);
Brandon Jones6e4b5ec2018-11-30 20:57:37326 std::vector<ControllerModel> controller_models;
327 controller_models.push_back(controller_model);
328 ui_->OnControllersUpdated(controller_models, reticle_model);
Aldo Culquicondor41532412018-08-07 22:36:44329
330 auto controller_time = base::TimeTicks::Now() - timing_start;
331 ui_controller_update_time_.AddSample(controller_time);
332 return controller_time;
333}
334
Aldo Culquicondor34610c1d2018-09-07 21:03:13335void BrowserRenderer::PerformControllerActionForTesting(
Aldo Culquicondor880ba3d2018-08-08 19:35:14336 ControllerTestInput controller_input) {
Aldo Culquicondorb915d1ef2018-09-11 23:10:35337 DCHECK(input_delegate_);
bsheedyfa3e4422018-09-18 21:39:31338 if (controller_input.action == VrControllerTestAction::kRevertToRealInput) {
Aldo Culquicondorb915d1ef2018-09-11 23:10:35339 if (using_input_delegate_for_testing_) {
340 DCHECK(static_cast<InputDelegateForTesting*>(input_delegate_.get())
341 ->IsQueueEmpty())
Aldo Culquicondor880ba3d2018-08-08 19:35:14342 << "Attempted to revert to using real controller with actions still "
343 "queued";
Aldo Culquicondorb915d1ef2018-09-11 23:10:35344 using_input_delegate_for_testing_ = false;
345 input_delegate_for_testing_.swap(input_delegate_);
bsheedyb3cc34d82018-11-15 22:20:40346 ui_->SetUiInputManagerForTesting(false);
Aldo Culquicondor880ba3d2018-08-08 19:35:14347 }
348 return;
349 }
Aldo Culquicondorb915d1ef2018-09-11 23:10:35350 if (!using_input_delegate_for_testing_) {
351 using_input_delegate_for_testing_ = true;
352 if (!input_delegate_for_testing_)
353 input_delegate_for_testing_ =
354 std::make_unique<InputDelegateForTesting>(ui_.get());
355 input_delegate_for_testing_.swap(input_delegate_);
bsheedyb3cc34d82018-11-15 22:20:40356 ui_->SetUiInputManagerForTesting(true);
Aldo Culquicondor880ba3d2018-08-08 19:35:14357 }
bsheedyfa3e4422018-09-18 21:39:31358 if (controller_input.action != VrControllerTestAction::kEnableMockedInput) {
Aldo Culquicondorb915d1ef2018-09-11 23:10:35359 static_cast<InputDelegateForTesting*>(input_delegate_.get())
Aldo Culquicondor880ba3d2018-08-08 19:35:14360 ->QueueControllerActionForTesting(controller_input);
361 }
362}
363
Aldo Culquicondor34610c1d2018-09-07 21:03:13364void BrowserRenderer::ReportUiStatusForTesting(
365 const base::TimeTicks& current_time,
366 bool ui_updated) {
Aldo Culquicondor41532412018-08-07 22:36:44367 if (ui_test_state_ == nullptr)
368 return;
369 base::TimeDelta time_since_start = current_time - ui_test_state_->start_time;
370 if (ui_updated) {
371 ui_test_state_->activity_started = true;
372 if (time_since_start > ui_test_state_->quiescence_timeout_ms) {
373 // The UI is being updated, but hasn't reached a stable state in the
374 // given time -> report timeout.
bsheedya2ff3432018-10-02 18:16:58375 ReportUiActivityResultForTesting(UiTestOperationResult::kTimeoutNoEnd);
Aldo Culquicondor41532412018-08-07 22:36:44376 }
377 } else {
378 if (ui_test_state_->activity_started) {
379 // The UI has been updated since the test requested notification of
380 // quiescence, but wasn't this frame -> report that the UI is quiescent.
bsheedya2ff3432018-10-02 18:16:58381 ReportUiActivityResultForTesting(UiTestOperationResult::kQuiescent);
Aldo Culquicondor41532412018-08-07 22:36:44382 } else if (time_since_start > ui_test_state_->quiescence_timeout_ms) {
383 // The UI has never been updated and we've reached the timeout.
bsheedya2ff3432018-10-02 18:16:58384 ReportUiActivityResultForTesting(UiTestOperationResult::kTimeoutNoStart);
Aldo Culquicondor41532412018-08-07 22:36:44385 }
386 }
387}
388
Aldo Culquicondor34610c1d2018-09-07 21:03:13389base::WeakPtr<BrowserRenderer> BrowserRenderer::GetWeakPtr() {
Aldo Culquicondor764626c2018-08-23 20:14:47390 return weak_ptr_factory_.GetWeakPtr();
391}
392
Aldo Culquicondor34610c1d2018-09-07 21:03:13393void BrowserRenderer::ReportUiActivityResultForTesting(
bsheedya2ff3432018-10-02 18:16:58394 UiTestOperationResult result) {
Aldo Culquicondor41532412018-08-07 22:36:44395 ui_test_state_ = nullptr;
bsheedyfa3e4422018-09-18 21:39:31396 browser_->ReportUiOperationResultForTesting(
397 UiTestOperationType::kUiActivityResult, result);
398}
399
400void BrowserRenderer::ReportFrameBufferDumpForTesting() {
401 if (frame_buffer_dump_filepath_base_.empty())
402 return;
403
404 frame_buffer_dump_filepath_base_.clear();
405 browser_->ReportUiOperationResultForTesting(
406 UiTestOperationType::kFrameBufferDumped,
bsheedya2ff3432018-10-02 18:16:58407 UiTestOperationResult::kQuiescent /* unused */);
408}
409
410void BrowserRenderer::ReportElementVisibilityStatusForTesting(
411 const base::TimeTicks& current_time) {
412 if (ui_visibility_state_ == nullptr)
413 return;
414 base::TimeDelta time_since_start =
415 current_time - ui_visibility_state_->start_time;
416 if (ui_->GetElementVisibilityForTesting(
bsheedydc7c6cb2018-11-05 20:48:45417 ui_visibility_state_->element_to_watch) ==
418 ui_visibility_state_->expected_visibile) {
bsheedya2ff3432018-10-02 18:16:58419 ReportElementVisibilityResultForTesting(
bsheedydc7c6cb2018-11-05 20:48:45420 UiTestOperationResult::kVisibilityMatch);
bsheedya2ff3432018-10-02 18:16:58421 } else if (time_since_start > ui_visibility_state_->timeout_ms) {
422 ReportElementVisibilityResultForTesting(
bsheedydc7c6cb2018-11-05 20:48:45423 UiTestOperationResult::kTimeoutNoVisibilityMatch);
bsheedya2ff3432018-10-02 18:16:58424 }
425}
426
427void BrowserRenderer::ReportElementVisibilityResultForTesting(
428 UiTestOperationResult result) {
429 ui_visibility_state_ = nullptr;
430 browser_->ReportUiOperationResultForTesting(
bsheedydc7c6cb2018-11-05 20:48:45431 UiTestOperationType::kElementVisibilityStatus, result);
Aldo Culquicondorcc004c72018-08-02 14:50:02432}
433
434} // namespace vr