| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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_ANDROID_VR_SHELL_VR_SHELL_GL_H_ |
| 6 #define CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_GL_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/cancelable_callback.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/single_thread_task_runner.h" |
| 15 #include "device/vr/android/gvr/gvr_delegate.h" |
| 16 #include "third_party/gvr-android-sdk/src/ndk/include/vr/gvr/capi/include/gvr.h" |
| 17 #include "third_party/gvr-android-sdk/src/ndk/include/vr/gvr/capi/include/gvr_ty
pes.h" |
| 18 #include "ui/gfx/native_widget_types.h" |
| 19 |
| 20 namespace blink { |
| 21 class WebInputEvent; |
| 22 } |
| 23 |
| 24 namespace gl { |
| 25 class GLContext; |
| 26 class GLSurface; |
| 27 class ScopedJavaSurface; |
| 28 class SurfaceTexture; |
| 29 } |
| 30 |
| 31 namespace vr_shell { |
| 32 |
| 33 class UiScene; |
| 34 class VrController; |
| 35 class VrInputManager; |
| 36 class VrShell; |
| 37 class VrShellRenderer; |
| 38 struct ContentRectangle; |
| 39 |
| 40 // This class manages all GLThread owned objects and GL rendering for VrShell. |
| 41 // It is not threadsafe and must only be used on the GL thread. |
| 42 class VrShellGl { |
| 43 public: |
| 44 enum class InputTarget { |
| 45 NONE = 0, |
| 46 CONTENT, |
| 47 UI |
| 48 }; |
| 49 |
| 50 VrShellGl( |
| 51 VrShell* vr_shell, |
| 52 const base::WeakPtr<VrShell>& weak_vr_shell, |
| 53 const base::WeakPtr<VrInputManager>& content_input_manager, |
| 54 const base::WeakPtr<VrInputManager>& ui_input_manager, |
| 55 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, |
| 56 gvr_context* gvr_api); |
| 57 ~VrShellGl(); |
| 58 |
| 59 bool Initialize(); |
| 60 |
| 61 void DrawFrame(); |
| 62 |
| 63 void OnTriggerEvent(); |
| 64 void OnPause(); |
| 65 void OnResume(); |
| 66 |
| 67 void SetWebVrMode(bool enabled); |
| 68 void ContentBoundsChanged(int width, int height); |
| 69 void ContentPhysicalBoundsChanged(int width, int height); |
| 70 void UIBoundsChanged(int width, int height); |
| 71 void UIPhysicalBoundsChanged(int width, int height); |
| 72 base::WeakPtr<VrShellGl> GetWeakPtr(); |
| 73 |
| 74 void UpdateWebVRTextureBounds(const gvr::Rectf& left_bounds, |
| 75 const gvr::Rectf& right_bounds); |
| 76 gvr::GvrApi* gvr_api(); |
| 77 void SetGvrPoseForWebVr(const gvr::Mat4f& pose, uint32_t pose_num); |
| 78 void SetWebVRRenderSurfaceSize(int width, int height); |
| 79 gvr::Sizei GetWebVRCompositorSurfaceSize(); |
| 80 |
| 81 UiScene* GetScene() { return scene_.get(); } |
| 82 |
| 83 private: |
| 84 bool InitializeGl(); |
| 85 void GvrInit(gvr_context* gvr_api); |
| 86 void InitializeRenderer(); |
| 87 void DrawVrShell(const gvr::Mat4f& head_pose, gvr::Frame &frame); |
| 88 void DrawUiView(const gvr::Mat4f* head_pose, |
| 89 const std::vector<const ContentRectangle*>& elements, |
| 90 const gvr::Sizei& render_size, int viewport_offset); |
| 91 void DrawElements(const gvr::Mat4f& render_matrix, |
| 92 const std::vector<const ContentRectangle*>& elements); |
| 93 void DrawCursor(const gvr::Mat4f& render_matrix); |
| 94 void DrawWebVr(); |
| 95 bool WebVrPoseByteIsValid(int pose_index_byte); |
| 96 |
| 97 void UpdateController(const gvr::Vec3f& forward_vector); |
| 98 void SendEventsToTarget(InputTarget input_target, int pixel_x, |
| 99 int pixel_y); |
| 100 void SendGesture(InputTarget input_target, |
| 101 std::unique_ptr<blink::WebInputEvent> event); |
| 102 |
| 103 void OnUIFrameAvailable(); |
| 104 void OnContentFrameAvailable(); |
| 105 |
| 106 void UpdateVSyncParameters(const base::TimeTicks timebase, |
| 107 const base::TimeDelta interval); |
| 108 void ScheduleNextDrawFrame(); |
| 109 |
| 110 void ForceExitVR(); |
| 111 |
| 112 // samplerExternalOES texture data for UI content image. |
| 113 int ui_texture_id_ = 0; |
| 114 // samplerExternalOES texture data for main content image. |
| 115 int content_texture_id_ = 0; |
| 116 |
| 117 std::unique_ptr<UiScene> scene_; |
| 118 |
| 119 scoped_refptr<gl::GLSurface> surface_; |
| 120 scoped_refptr<gl::GLContext> context_; |
| 121 scoped_refptr<gl::SurfaceTexture> ui_surface_texture_; |
| 122 scoped_refptr<gl::SurfaceTexture> content_surface_texture_; |
| 123 |
| 124 std::unique_ptr<gl::ScopedJavaSurface> ui_surface_; |
| 125 std::unique_ptr<gl::ScopedJavaSurface> content_surface_; |
| 126 |
| 127 std::unique_ptr<gvr::GvrApi> gvr_api_; |
| 128 std::unique_ptr<gvr::BufferViewportList> buffer_viewport_list_; |
| 129 std::unique_ptr<gvr::BufferViewport> buffer_viewport_; |
| 130 std::unique_ptr<gvr::BufferViewport> headlocked_left_viewport_; |
| 131 std::unique_ptr<gvr::BufferViewport> headlocked_right_viewport_; |
| 132 std::unique_ptr<gvr::BufferViewport> webvr_left_viewport_; |
| 133 std::unique_ptr<gvr::BufferViewport> webvr_right_viewport_; |
| 134 std::unique_ptr<gvr::SwapChain> swap_chain_; |
| 135 |
| 136 // Current sizes for the render buffers. |
| 137 gvr::Sizei render_size_primary_; |
| 138 gvr::Sizei render_size_headlocked_; |
| 139 |
| 140 // Intended size for the primary render buffer by UI mode. |
| 141 // For WebVR, a size of 0x0 is used to indicate "not yet ready" |
| 142 // to suppress rendering while still initializing. |
| 143 gvr::Sizei render_size_primary_webvr_ = device::kInvalidRenderTargetSize; |
| 144 gvr::Sizei render_size_primary_vrshell_; |
| 145 |
| 146 std::unique_ptr<VrShellRenderer> vr_shell_renderer_; |
| 147 |
| 148 bool touch_pending_ = false; |
| 149 gvr::Quatf controller_quat_; |
| 150 |
| 151 gvr::Vec3f target_point_; |
| 152 const ContentRectangle* target_element_ = nullptr; |
| 153 InputTarget current_input_target_ = InputTarget::NONE; |
| 154 int ui_tex_css_width_ = 0; |
| 155 int ui_tex_css_height_ = 0; |
| 156 int content_tex_css_width_ = 0; |
| 157 int content_tex_css_height_ = 0; |
| 158 gvr::Sizei content_tex_physical_size_ = {0, 0}; |
| 159 gvr::Sizei ui_tex_physical_size_ = {0, 0}; |
| 160 |
| 161 // The pose ring buffer size must be a power of two to avoid glitches when |
| 162 // the pose index wraps around. It should be large enough to handle the |
| 163 // current backlog of poses which is 2-3 frames. |
| 164 static constexpr int kPoseRingBufferSize = 8; |
| 165 std::vector<gvr::Mat4f> webvr_head_pose_; |
| 166 std::vector<bool> webvr_head_pose_valid_; |
| 167 jint webvr_texture_id_ = 0; |
| 168 |
| 169 std::unique_ptr<VrController> controller_; |
| 170 |
| 171 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 172 base::CancelableClosure draw_task_; |
| 173 base::TimeTicks vsync_timebase_; |
| 174 base::TimeDelta vsync_interval_; |
| 175 |
| 176 // TODO(mthiesse): Remove thread-unsafe VrShell usage. |
| 177 VrShell* vr_shell_; |
| 178 base::WeakPtr<VrShell> weak_vr_shell_; |
| 179 base::WeakPtr<VrInputManager> content_input_manager_; |
| 180 base::WeakPtr<VrInputManager> ui_input_manager_; |
| 181 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
| 182 |
| 183 base::WeakPtrFactory<VrShellGl> weak_ptr_factory_; |
| 184 |
| 185 DISALLOW_COPY_AND_ASSIGN(VrShellGl); |
| 186 }; |
| 187 |
| 188 } // namespace vr_shell |
| 189 |
| 190 #endif // CHROME_BROWSER_ANDROID_VR_SHELL_VR_SHELL_GL_H_ |
| OLD | NEW |