blob: 6be6a0d103e8a94e07e9ef993638c054916717e6 [file] [log] [blame]
Christopher Grantea69a472017-09-25 15:23:321// Copyright 2017 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
5#ifndef CHROME_BROWSER_VR_UI_H_
6#define CHROME_BROWSER_VR_UI_H_
7
8#include <memory>
9
10#include "base/macros.h"
11#include "base/memory/weak_ptr.h"
Ian Vollick8e4a824f2018-01-06 05:15:1212#include "base/version.h"
13#include "chrome/browser/vr/assets_load_status.h"
Ian Vollicka39bafec2017-09-28 02:27:5514#include "chrome/browser/vr/browser_ui_interface.h"
Yash Malikd862b31c2017-12-02 02:09:3215#include "chrome/browser/vr/keyboard_ui_interface.h"
Tibor Goldschwendtdf2bf012018-05-08 01:02:1316#include "chrome/browser/vr/model/tab_model.h"
Ian Vollick3f6c98042017-11-06 19:40:5017#include "chrome/browser/vr/platform_controller.h"
18#include "chrome/browser/vr/ui_element_renderer.h"
bsheedybfabd61d2018-04-16 19:02:5919#include "chrome/browser/vr/ui_test_input.h"
Christopher Grant7e73a4f2018-05-30 21:13:1420#include "chrome/browser/vr/vr_export.h"
Christopher Grantea69a472017-09-25 15:23:3221
22namespace vr {
bsheedybfabd61d2018-04-16 19:02:5923
Christopher Grant764281a92018-03-10 00:48:2524class AudioDelegate;
Christopher Grantea69a472017-09-25 15:23:3225class BrowserUiInterface;
Michael Thiessenbc382ac2018-04-20 20:39:1726class ContentElement;
Christopher Grantea69a472017-09-25 15:23:3227class ContentInputDelegate;
Biao She41db7d22018-05-14 14:08:3328class PlatformInputHandler;
Yash Malikd862b31c2017-12-02 02:09:3229class KeyboardDelegate;
Biao She41db7d22018-05-14 14:08:3330class PlatformUiInputDelegate;
Tibor Goldschwendt3d8447c2017-11-09 05:30:5031class SkiaSurfaceProvider;
Yash Malikd862b31c2017-12-02 02:09:3232class TextInputDelegate;
Christopher Grantea69a472017-09-25 15:23:3233class UiBrowserInterface;
34class UiInputManager;
35class UiRenderer;
36class UiScene;
Ian Vollick8e4a824f2018-01-06 05:15:1237struct Assets;
Ian Vollick3f6c98042017-11-06 19:40:5038struct ControllerModel;
Ian Vollicka39bafec2017-09-28 02:27:5539struct Model;
Kenneth Russell4c50a162017-11-08 23:29:0740struct OmniboxSuggestions;
Tibor Goldschwendt3d8447c2017-11-09 05:30:5041struct ReticleModel;
Christopher Grant12ed8a72017-10-31 17:41:4442
Christopher Grant7e73a4f2018-05-30 21:13:1443struct VR_EXPORT UiInitialState {
Michael Thiessenf7f988182018-04-27 01:16:4344 UiInitialState();
45 UiInitialState(const UiInitialState& other);
Christopher Grantea69a472017-09-25 15:23:3246 bool in_cct = false;
47 bool in_web_vr = false;
48 bool web_vr_autopresentation_expected = false;
Michael Thiessena0cdfa52017-10-16 16:23:3849 bool browsing_disabled = false;
Amirhossein Simjoured1b0d22017-11-08 15:19:0550 bool has_or_can_request_audio_permission = true;
Ian Vollick20673302017-11-22 20:43:3151 bool skips_redraw_when_not_dirty = false;
Tibor Goldschwendt4dafc602018-03-06 17:39:4452 bool assets_supported = false;
Christopher Grant6df268d2018-02-13 16:55:5453 bool supports_selection = true;
Yash Malikd68a05a2018-03-02 01:06:5454 bool needs_keyboard_update = false;
Michael Thiessenf7f988182018-04-27 01:16:4355 bool is_standalone_vr_device = false;
Tibor Goldschwendtdf2bf012018-05-08 01:02:1356 bool create_tabs_view = false;
Christopher Grantea69a472017-09-25 15:23:3257};
58
59// This class manages all GLThread owned objects and GL rendering for VrShell.
60// It is not threadsafe and must only be used on the GL thread.
Christopher Grant7e73a4f2018-05-30 21:13:1461class VR_EXPORT Ui : public BrowserUiInterface, public KeyboardUiInterface {
Christopher Grantea69a472017-09-25 15:23:3262 public:
63 Ui(UiBrowserInterface* browser,
Biao She41db7d22018-05-14 14:08:3364 PlatformInputHandler* content_input_forwarder,
Christopher Grant764281a92018-03-10 00:48:2565 KeyboardDelegate* keyboard_delegate,
66 TextInputDelegate* text_input_delegate,
67 AudioDelegate* audio_delegate,
Ian Vollick3f6c98042017-11-06 19:40:5068 const UiInitialState& ui_initial_state);
Ian Vollickf8030c52017-11-22 17:14:1469
70 Ui(UiBrowserInterface* browser,
71 std::unique_ptr<ContentInputDelegate> content_input_delegate,
Christopher Grant764281a92018-03-10 00:48:2572 KeyboardDelegate* keyboard_delegate,
73 TextInputDelegate* text_input_delegate,
74 AudioDelegate* audio_delegate,
Ian Vollickf8030c52017-11-22 17:14:1475 const UiInitialState& ui_initial_state);
76
Ian Vollicka39bafec2017-09-28 02:27:5577 ~Ui() override;
Christopher Grantea69a472017-09-25 15:23:3278
79 // TODO(crbug.com/767957): Refactor to hide these behind the UI interface.
80 UiScene* scene() { return scene_.get(); }
Ian Vollick826f56b02017-11-13 19:53:5781 UiElementRenderer* ui_element_renderer() {
82 return ui_element_renderer_.get();
83 }
Christopher Grantea69a472017-09-25 15:23:3284 UiRenderer* ui_renderer() { return ui_renderer_.get(); }
85 UiInputManager* input_manager() { return input_manager_.get(); }
86
Christopher Grant764281a92018-03-10 00:48:2587 base::WeakPtr<BrowserUiInterface> GetBrowserUiWeakPtr();
Ian Vollicka39bafec2017-09-28 02:27:5588
89 // BrowserUiInterface
Christopher Grantbd2f2692018-03-29 20:52:1690 void SetWebVrMode(bool enabled) override;
Ian Vollicka39bafec2017-09-28 02:27:5591 void SetFullscreen(bool enabled) override;
92 void SetToolbarState(const ToolbarState& state) override;
93 void SetIncognito(bool enabled) override;
Ian Vollicka39bafec2017-09-28 02:27:5594 void SetLoading(bool loading) override;
95 void SetLoadProgress(float progress) override;
96 void SetIsExiting() override;
97 void SetHistoryButtonsEnabled(bool can_go_back, bool can_go_forward) override;
Ian Vollick1cf746c2018-03-23 22:17:2598 void SetCapturingState(const CapturingStateModel& state) override;
Michael Thiessen8a9a1022018-02-21 22:17:1699 void ShowExitVrPrompt(UiUnsupportedMode reason) override;
Biao Shec85218742017-10-26 21:48:15100 void SetSpeechRecognitionEnabled(bool enabled) override;
Biao She537a7292017-11-10 18:18:42101 void SetRecognitionResult(const base::string16& result) override;
Biao Shec85218742017-10-26 21:48:15102 void OnSpeechRecognitionStateChanged(int new_state) override;
Christopher Grantb38b120c2017-11-17 19:51:22103 void SetOmniboxSuggestions(
104 std::unique_ptr<OmniboxSuggestions> suggestions) override;
Ian Vollick8e4a824f2018-01-06 05:15:12105 void OnAssetsLoaded(AssetsLoadStatus status,
106 std::unique_ptr<Assets> assets,
Tibor Goldschwendt444261f2018-03-01 22:36:37107 const base::Version& component_version) override;
108 void OnAssetsUnavailable() override;
Tibor Goldschwendtf3cd1b02018-05-14 22:42:46109 void WaitForAssets() override;
Michael Thiessenee351c12018-04-11 16:29:58110 void SetOverlayTextureEmpty(bool empty) override;
Tibor Goldschwendtdf2bf012018-05-08 01:02:13111 void ShowSoftInput(bool show) override;
112 void UpdateWebInputIndices(int selection_start,
113 int selection_end,
114 int composition_start,
115 int composition_end) override;
116 void AddOrUpdateTab(int id,
117 bool incognito,
118 const base::string16& title) override;
119 void RemoveTab(int id, bool incognito) override;
120 void RemoveAllTabs() override;
Ian Vollicka39bafec2017-09-28 02:27:55121
Yash Malik7fc0c952018-01-23 18:01:32122 // TODO(ymalik): We expose this to stop sending VSync to the WebVR page until
123 // the splash screen has been visible for its minimum duration. The visibility
124 // logic currently lives in the UI, and it'd be much cleaner if the UI didn't
125 // have to worry about this, and if it were told to hide the splash screen
126 // like other WebVR phases (e.g. OnWebVrFrameAvailable below).
127 bool CanSendWebVrVSync();
Yash Malik58e7aa52018-02-02 00:11:12128
Amirhossein Simjourd78bbcf2018-02-02 16:23:39129 void SetAlertDialogEnabled(bool enabled,
Biao She41db7d22018-05-14 14:08:33130 PlatformUiInputDelegate* delegate,
Amirhossein Simjour813c71e2018-03-29 18:49:46131 float width,
132 float height);
133 void SetAlertDialogSize(float width, float height);
134 void SetDialogLocation(float x, float y);
Amirhossein Simjourfb0c86e2018-04-23 22:44:55135 void SetDialogFloating(bool floating);
Biao She4a304be12018-03-29 22:25:41136 void ShowPlatformToast(const base::string16& text);
137 void CancelPlatformToast();
Ian Vollick3f6c98042017-11-06 19:40:50138 bool ShouldRenderWebVr();
Yash Malik7fc0c952018-01-23 18:01:32139
Michael Thiessen63961202018-01-23 01:17:53140 void OnGlInitialized(
141 unsigned int content_texture_id,
142 UiElementRenderer::TextureLocation content_location,
143 unsigned int content_overlay_texture_id,
144 UiElementRenderer::TextureLocation content_overlay_location,
Amirhossein Simjourd78bbcf2018-02-02 16:23:39145 unsigned int ui_texture_id,
Michael Thiessen63961202018-01-23 01:17:53146 bool use_ganesh);
Yash Malik7fc0c952018-01-23 18:01:32147
Ian Vollick3f6c98042017-11-06 19:40:50148 void OnAppButtonClicked();
Ian Vollicke4bfb432018-03-27 03:19:26149 void OnAppButtonSwipePerformed(PlatformController::SwipeDirection direction);
Ian Vollicka5cc8022017-11-01 14:04:47150 void OnControllerUpdated(const ControllerModel& controller_model,
Ian Vollick3f6c98042017-11-06 19:40:50151 const ReticleModel& reticle_model);
152 void OnProjMatrixChanged(const gfx::Transform& proj_matrix);
153 void OnWebVrFrameAvailable();
154 void OnWebVrTimedOut();
155 void OnWebVrTimeoutImminent();
156 bool IsControllerVisible() const;
Ian Vollicke4bfb432018-03-27 03:19:26157 bool IsAppButtonLongPressed() const;
Ian Vollick20673302017-11-22 20:43:31158 bool SkipsRedrawWhenNotDirty() const;
Ian Vollick3f6c98042017-11-06 19:40:50159 void OnSwapContents(int new_content_id);
160 void OnContentBoundsChanged(int width, int height);
161 void OnPlatformControllerInitialized(PlatformController* controller);
Yash Malik1a102732017-12-22 22:01:55162 void OnUiRequestedNavigation();
Brandon Jonese44b8a82018-05-24 23:32:13163 void SetFloorHeight(float floor_height);
Ian Vollicka39bafec2017-09-28 02:27:55164
Ian Vollick0375ab82017-11-20 19:58:05165 Model* model_for_test() { return model_.get(); }
166
Ian Vollick1b1360a2017-11-25 17:12:19167 void ReinitializeForTest(const UiInitialState& ui_initial_state);
Yash Malikb78e72a72018-02-21 09:14:10168 ContentInputDelegate* GetContentInputDelegateForTest() {
169 return content_input_delegate_.get();
170 }
Ian Vollick1b1360a2017-11-25 17:12:19171
Ian Vollick70a88892018-01-02 15:29:57172 void Dump(bool include_bindings);
Ian Vollickf8030c52017-11-22 17:14:14173
Yash Malikd862b31c2017-12-02 02:09:32174 // Keyboard input related.
175 void RequestFocus(int element_id);
Christopher Grantf4e347f22017-12-07 23:26:05176 void RequestUnfocus(int element_id);
Christopher Grantaa96b3e2018-02-07 19:56:48177 void OnInputEdited(const EditedText& info) override;
178 void OnInputCommitted(const EditedText& info) override;
Yash Malikd862b31c2017-12-02 02:09:32179 void OnKeyboardHidden() override;
180
Michael Thiessenc49a9fd2018-02-22 15:05:58181 void AcceptDoffPromptForTesting();
bsheedybfabd61d2018-04-16 19:02:59182 void PerformUiActionForTesting(UiTestInput test_input);
Michael Thiessenc49a9fd2018-02-22 15:05:58183
Michael Thiessenbc382ac2018-04-20 20:39:17184 bool IsContentVisibleAndOpaque();
185 bool IsContentOverlayTextureEmpty();
186 void SetContentUsesQuadLayer(bool uses_quad_buffers);
187 gfx::Transform GetContentWorldSpaceTransform();
188
Christopher Grantea69a472017-09-25 15:23:32189 private:
Christopher Grant27d7cd5c2018-05-02 21:00:03190 void OnSpeechRecognitionEnded();
Ian Vollick1b1360a2017-11-25 17:12:19191 void InitializeModel(const UiInitialState& ui_initial_state);
Ian Vollickd1389be2017-11-17 04:26:38192 UiBrowserInterface* browser_;
Michael Thiessenbc382ac2018-04-20 20:39:17193 ContentElement* GetContentElement();
Tibor Goldschwendtdf2bf012018-05-08 01:02:13194 std::vector<TabModel>::iterator FindTab(int id, std::vector<TabModel>* tabs);
Ian Vollickd1389be2017-11-17 04:26:38195
Christopher Grantea69a472017-09-25 15:23:32196 // This state may be further abstracted into a SkiaUi object.
Christopher Grant764281a92018-03-10 00:48:25197 std::unique_ptr<UiScene> scene_;
198 std::unique_ptr<Model> model_;
199 std::unique_ptr<ContentInputDelegate> content_input_delegate_;
200 std::unique_ptr<UiElementRenderer> ui_element_renderer_;
201 std::unique_ptr<UiInputManager> input_manager_;
202 std::unique_ptr<UiRenderer> ui_renderer_;
Tibor Goldschwendt3d8447c2017-11-09 05:30:50203 std::unique_ptr<SkiaSurfaceProvider> provider_;
Christopher Grantea69a472017-09-25 15:23:32204
Michael Thiessenbc382ac2018-04-20 20:39:17205 // Cache the content element so we don't have to get it multiple times per
206 // frame.
207 ContentElement* content_element_ = nullptr;
208
Christopher Grant764281a92018-03-10 00:48:25209 AudioDelegate* audio_delegate_ = nullptr;
210
Ian Vollicka39bafec2017-09-28 02:27:55211 base::WeakPtrFactory<Ui> weak_ptr_factory_;
212
Christopher Grantea69a472017-09-25 15:23:32213 DISALLOW_COPY_AND_ASSIGN(Ui);
214};
215
216} // namespace vr
217
218#endif // CHROME_BROWSER_VR_UI_H_