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