OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/vr_shell/vr_shell_gl.h" | 5 #include "chrome/browser/android/vr_shell/vr_shell_gl.h" |
6 | 6 |
7 #include <chrono> | 7 #include <chrono> |
8 #include <limits> | 8 #include <limits> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 quat.qw = 0.0f; | 101 quat.qw = 0.0f; |
102 } else { | 102 } else { |
103 quat.qx = vec.y(); | 103 quat.qx = vec.y(); |
104 quat.qy = -vec.x(); | 104 quat.qy = -vec.x(); |
105 quat.qz = 0.0f; | 105 quat.qz = 0.0f; |
106 vr::NormalizeQuat(&quat); | 106 vr::NormalizeQuat(&quat); |
107 } | 107 } |
108 return quat; | 108 return quat; |
109 } | 109 } |
110 | 110 |
| 111 gvr::Mat4f PerspectiveMatrixFromView(const gvr::Rectf& fov, |
| 112 float z_near, |
| 113 float z_far) { |
| 114 gvr::Mat4f result; |
| 115 const float x_left = -std::tan(fov.left * M_PI / 180.0f) * z_near; |
| 116 const float x_right = std::tan(fov.right * M_PI / 180.0f) * z_near; |
| 117 const float y_bottom = -std::tan(fov.bottom * M_PI / 180.0f) * z_near; |
| 118 const float y_top = std::tan(fov.top * M_PI / 180.0f) * z_near; |
| 119 |
| 120 DCHECK(x_left < x_right && y_bottom < y_top && z_near < z_far && |
| 121 z_near > 0.0f && z_far > 0.0f); |
| 122 const float X = (2 * z_near) / (x_right - x_left); |
| 123 const float Y = (2 * z_near) / (y_top - y_bottom); |
| 124 const float A = (x_right + x_left) / (x_right - x_left); |
| 125 const float B = (y_top + y_bottom) / (y_top - y_bottom); |
| 126 const float C = (z_near + z_far) / (z_near - z_far); |
| 127 const float D = (2 * z_near * z_far) / (z_near - z_far); |
| 128 |
| 129 for (int i = 0; i < 4; ++i) { |
| 130 for (int j = 0; j < 4; ++j) { |
| 131 result.m[i][j] = 0.0f; |
| 132 } |
| 133 } |
| 134 result.m[0][0] = X; |
| 135 result.m[0][2] = A; |
| 136 result.m[1][1] = Y; |
| 137 result.m[1][2] = B; |
| 138 result.m[2][2] = C; |
| 139 result.m[2][3] = D; |
| 140 result.m[3][2] = -1; |
| 141 |
| 142 return result; |
| 143 } |
| 144 |
111 std::unique_ptr<blink::WebMouseEvent> MakeMouseEvent(WebInputEvent::Type type, | 145 std::unique_ptr<blink::WebMouseEvent> MakeMouseEvent(WebInputEvent::Type type, |
112 double timestamp, | 146 double timestamp, |
113 float x, | 147 float x, |
114 float y) { | 148 float y) { |
115 std::unique_ptr<blink::WebMouseEvent> mouse_event(new blink::WebMouseEvent( | 149 std::unique_ptr<blink::WebMouseEvent> mouse_event(new blink::WebMouseEvent( |
116 type, blink::WebInputEvent::kNoModifiers, timestamp)); | 150 type, blink::WebInputEvent::kNoModifiers, timestamp)); |
117 mouse_event->pointer_type = blink::WebPointerProperties::PointerType::kMouse; | 151 mouse_event->pointer_type = blink::WebPointerProperties::PointerType::kMouse; |
118 mouse_event->SetPositionInWidget(x, y); | 152 mouse_event->SetPositionInWidget(x, y); |
119 mouse_event->click_count = 1; | 153 mouse_event->click_count = 1; |
120 | 154 |
(...skipping 22 matching lines...) Expand all Loading... |
143 } | 177 } |
144 | 178 |
145 void GvrMatToMatf(const gvr::Mat4f& in, vr::Mat4f* out) { | 179 void GvrMatToMatf(const gvr::Mat4f& in, vr::Mat4f* out) { |
146 // If our std::array implementation doesn't have any non-data members, we can | 180 // If our std::array implementation doesn't have any non-data members, we can |
147 // just cast the gvr matrix to an std::array. | 181 // just cast the gvr matrix to an std::array. |
148 static_assert(sizeof(in) == sizeof(*out), | 182 static_assert(sizeof(in) == sizeof(*out), |
149 "Cannot reinterpret gvr::Mat4f as vr::Matf"); | 183 "Cannot reinterpret gvr::Mat4f as vr::Matf"); |
150 *out = *reinterpret_cast<vr::Mat4f*>(const_cast<gvr::Mat4f*>(&in)); | 184 *out = *reinterpret_cast<vr::Mat4f*>(const_cast<gvr::Mat4f*>(&in)); |
151 } | 185 } |
152 | 186 |
153 gvr::Rectf GvrRectFromGfxRect(gfx::RectF rect) { | 187 gvr::Rectf UVFromGfxRect(gfx::RectF rect) { |
154 // gvr::Rectf bottom/top are reverse of gfx::RectF bottom/top. | 188 return {rect.x(), rect.x() + rect.width(), 1.0f - rect.bottom(), |
155 return {rect.x(), rect.x() + rect.width(), rect.y(), rect.bottom()}; | 189 1.0f - rect.y()}; |
156 } | 190 } |
157 | 191 |
158 gfx::RectF GfxRectFromGvrRect(gvr::Rectf rect) { | 192 gfx::RectF GfxRectFromUV(gvr::Rectf rect) { |
159 return gfx::RectF(rect.left, rect.bottom, rect.right - rect.left, | 193 return gfx::RectF(rect.left, 1.0 - rect.top, rect.right - rect.left, |
160 rect.top - rect.bottom); | 194 rect.top - rect.bottom); |
161 } | 195 } |
162 | 196 |
163 } // namespace | 197 } // namespace |
164 | 198 |
165 VrShellGl::VrShellGl( | 199 VrShellGl::VrShellGl( |
166 const base::WeakPtr<VrShell>& weak_vr_shell, | 200 const base::WeakPtr<VrShell>& weak_vr_shell, |
167 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, | 201 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, |
168 gvr_context* gvr_api, | 202 gvr_context* gvr_api, |
169 bool initially_web_vr, | 203 bool initially_web_vr, |
(...skipping 608 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
778 if (index < frame_index) | 812 if (index < frame_index) |
779 index += max; | 813 index += max; |
780 // If the pending bounds change is for an upcoming frame | 814 // If the pending bounds change is for an upcoming frame |
781 // within our buffer size, wait to apply it. Otherwise, apply | 815 // within our buffer size, wait to apply it. Otherwise, apply |
782 // it immediately. This guarantees that even if we miss many | 816 // it immediately. This guarantees that even if we miss many |
783 // frames, the queue can't fill up with stale bounds. | 817 // frames, the queue can't fill up with stale bounds. |
784 if (index > frame_index && index <= frame_index + kPoseRingBufferSize) | 818 if (index > frame_index && index <= frame_index + kPoseRingBufferSize) |
785 break; | 819 break; |
786 | 820 |
787 const WebVrBounds& bounds = pending_bounds_.front().second; | 821 const WebVrBounds& bounds = pending_bounds_.front().second; |
788 webvr_left_viewport_->SetSourceUv(GvrRectFromGfxRect(bounds.left_bounds)); | 822 webvr_left_viewport_->SetSourceUv(UVFromGfxRect(bounds.left_bounds)); |
789 webvr_right_viewport_->SetSourceUv( | 823 webvr_right_viewport_->SetSourceUv(UVFromGfxRect(bounds.right_bounds)); |
790 GvrRectFromGfxRect(bounds.right_bounds)); | |
791 DVLOG(1) << __FUNCTION__ << ": resize from pending_bounds to " | 824 DVLOG(1) << __FUNCTION__ << ": resize from pending_bounds to " |
792 << bounds.source_size.width() << "x" | 825 << bounds.source_size.width() << "x" |
793 << bounds.source_size.height(); | 826 << bounds.source_size.height(); |
794 CreateOrResizeWebVRSurface(bounds.source_size); | 827 CreateOrResizeWebVRSurface(bounds.source_size); |
795 pending_bounds_.pop(); | 828 pending_bounds_.pop(); |
796 } | 829 } |
797 buffer_viewport_list_->SetBufferViewport(GVR_LEFT_EYE, | 830 buffer_viewport_list_->SetBufferViewport(GVR_LEFT_EYE, |
798 *webvr_left_viewport_); | 831 *webvr_left_viewport_); |
799 buffer_viewport_list_->SetBufferViewport(GVR_RIGHT_EYE, | 832 buffer_viewport_list_->SetBufferViewport(GVR_RIGHT_EYE, |
800 *webvr_right_viewport_); | 833 *webvr_right_viewport_); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
882 if (!surfaceless_rendering_) { | 915 if (!surfaceless_rendering_) { |
883 // TODO(mthiesse): Support asynchronous SwapBuffers. | 916 // TODO(mthiesse): Support asynchronous SwapBuffers. |
884 TRACE_EVENT0("gpu", "VrShellGl::SwapBuffers"); | 917 TRACE_EVENT0("gpu", "VrShellGl::SwapBuffers"); |
885 surface_->SwapBuffers(); | 918 surface_->SwapBuffers(); |
886 } | 919 } |
887 | 920 |
888 #if DCHECK_IS_ON() | 921 #if DCHECK_IS_ON() |
889 // After saving the timestamp, fps will be available via GetFPS(). | 922 // After saving the timestamp, fps will be available via GetFPS(). |
890 // TODO(vollick): enable rendering of this framerate in a HUD. | 923 // TODO(vollick): enable rendering of this framerate in a HUD. |
891 fps_meter_->AddFrame(current_time); | 924 fps_meter_->AddFrame(current_time); |
892 LOG(ERROR) << "fps: " << fps_meter_->GetFPS(); | 925 DVLOG(1) << "fps: " << fps_meter_->GetFPS(); |
893 #endif | 926 #endif |
894 } | 927 } |
895 | 928 |
896 void VrShellGl::DrawWorldElements(const vr::Mat4f& head_pose) { | 929 void VrShellGl::DrawWorldElements(const vr::Mat4f& head_pose) { |
897 TRACE_EVENT0("gpu", "VrShellGl::DrawWorldElements"); | 930 TRACE_EVENT0("gpu", "VrShellGl::DrawWorldElements"); |
898 | 931 |
899 if (ShouldDrawWebVr()) { | 932 if (ShouldDrawWebVr()) { |
900 // WebVR is incompatible with 3D world compositing since the | 933 // WebVR is incompatible with 3D world compositing since the |
901 // depth buffer was already populated with unknown scaling - the | 934 // depth buffer was already populated with unknown scaling - the |
902 // WebVR app has full control over zNear/zFar. Just leave the | 935 // WebVR app has full control over zNear/zFar. Just leave the |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
951 | 984 |
952 for (auto eye : {GVR_LEFT_EYE, GVR_RIGHT_EYE}) { | 985 for (auto eye : {GVR_LEFT_EYE, GVR_RIGHT_EYE}) { |
953 buffer_viewport_list_->GetBufferViewport(eye + viewport_offset, | 986 buffer_viewport_list_->GetBufferViewport(eye + viewport_offset, |
954 buffer_viewport_.get()); | 987 buffer_viewport_.get()); |
955 | 988 |
956 vr::Mat4f eye_view_matrix; | 989 vr::Mat4f eye_view_matrix; |
957 vr::Mat4f eye_matrix; | 990 vr::Mat4f eye_matrix; |
958 GvrMatToMatf(gvr_api_->GetEyeFromHeadMatrix(eye), &eye_matrix); | 991 GvrMatToMatf(gvr_api_->GetEyeFromHeadMatrix(eye), &eye_matrix); |
959 vr::MatrixMul(eye_matrix, head_pose, &eye_view_matrix); | 992 vr::MatrixMul(eye_matrix, head_pose, &eye_view_matrix); |
960 | 993 |
961 const gfx::RectF& rect = | 994 const gfx::RectF& rect = GfxRectFromUV(buffer_viewport_->GetSourceUv()); |
962 GfxRectFromGvrRect(buffer_viewport_->GetSourceUv()); | |
963 const gfx::Rect& pixel_rect = CalculatePixelSpaceRect(render_size, rect); | 995 const gfx::Rect& pixel_rect = CalculatePixelSpaceRect(render_size, rect); |
964 glViewport(pixel_rect.x(), pixel_rect.y(), pixel_rect.width(), | 996 glViewport(pixel_rect.x(), pixel_rect.y(), pixel_rect.width(), |
965 pixel_rect.height()); | 997 pixel_rect.height()); |
966 | 998 |
967 vr::Mat4f render_matrix; | 999 vr::Mat4f render_matrix; |
968 vr::Mat4f perspective_matrix; | 1000 vr::Mat4f perspective_matrix; |
969 vr::PerspectiveMatrixFromView( | 1001 GvrMatToMatf(PerspectiveMatrixFromView(buffer_viewport_->GetSourceFov(), |
970 GfxRectFromGvrRect(buffer_viewport_->GetSourceFov()), kZNear, kZFar, | 1002 kZNear, kZFar), |
971 &perspective_matrix); | 1003 &perspective_matrix); |
| 1004 |
972 vr::MatrixMul(perspective_matrix, eye_view_matrix, &render_matrix); | 1005 vr::MatrixMul(perspective_matrix, eye_view_matrix, &render_matrix); |
973 | 1006 |
974 DrawElements(render_matrix, elementsInDrawOrder); | 1007 DrawElements(render_matrix, elementsInDrawOrder); |
975 if (draw_cursor) { | 1008 if (draw_cursor) { |
976 DrawCursor(render_matrix); | 1009 DrawCursor(render_matrix); |
977 DrawController(render_matrix); | 1010 DrawController(render_matrix); |
978 } | 1011 } |
979 } | 1012 } |
980 } | 1013 } |
981 | 1014 |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1200 | 1233 |
1201 void VrShellGl::SetWebVrMode(bool enabled) { | 1234 void VrShellGl::SetWebVrMode(bool enabled) { |
1202 web_vr_mode_ = enabled; | 1235 web_vr_mode_ = enabled; |
1203 } | 1236 } |
1204 | 1237 |
1205 void VrShellGl::UpdateWebVRTextureBounds(int16_t frame_index, | 1238 void VrShellGl::UpdateWebVRTextureBounds(int16_t frame_index, |
1206 const gfx::RectF& left_bounds, | 1239 const gfx::RectF& left_bounds, |
1207 const gfx::RectF& right_bounds, | 1240 const gfx::RectF& right_bounds, |
1208 const gfx::Size& source_size) { | 1241 const gfx::Size& source_size) { |
1209 if (frame_index < 0) { | 1242 if (frame_index < 0) { |
1210 webvr_left_viewport_->SetSourceUv(GvrRectFromGfxRect(left_bounds)); | 1243 webvr_left_viewport_->SetSourceUv(UVFromGfxRect(left_bounds)); |
1211 webvr_right_viewport_->SetSourceUv(GvrRectFromGfxRect(right_bounds)); | 1244 webvr_right_viewport_->SetSourceUv(UVFromGfxRect(right_bounds)); |
1212 CreateOrResizeWebVRSurface(source_size); | 1245 CreateOrResizeWebVRSurface(source_size); |
1213 } else { | 1246 } else { |
1214 pending_bounds_.emplace( | 1247 pending_bounds_.emplace( |
1215 frame_index, WebVrBounds(left_bounds, right_bounds, source_size)); | 1248 frame_index, WebVrBounds(left_bounds, right_bounds, source_size)); |
1216 } | 1249 } |
1217 } | 1250 } |
1218 | 1251 |
1219 void VrShellGl::ContentBoundsChanged(int width, int height) { | 1252 void VrShellGl::ContentBoundsChanged(int width, int height) { |
1220 TRACE_EVENT0("gpu", "VrShellGl::ContentBoundsChanged"); | 1253 TRACE_EVENT0("gpu", "VrShellGl::ContentBoundsChanged"); |
1221 content_tex_css_width_ = width; | 1254 content_tex_css_width_ = width; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1342 // InitializeGl. Revisit if the initialization order changes. | 1375 // InitializeGl. Revisit if the initialization order changes. |
1343 device::mojom::VRDisplayInfoPtr info = | 1376 device::mojom::VRDisplayInfoPtr info = |
1344 device::GvrDelegate::CreateVRDisplayInfo(gvr_api_.get(), | 1377 device::GvrDelegate::CreateVRDisplayInfo(gvr_api_.get(), |
1345 webvr_surface_size_, device_id); | 1378 webvr_surface_size_, device_id); |
1346 main_thread_task_runner_->PostTask( | 1379 main_thread_task_runner_->PostTask( |
1347 FROM_HERE, | 1380 FROM_HERE, |
1348 base::Bind(&RunVRDisplayInfoCallback, callback, base::Passed(&info))); | 1381 base::Bind(&RunVRDisplayInfoCallback, callback, base::Passed(&info))); |
1349 } | 1382 } |
1350 | 1383 |
1351 } // namespace vr_shell | 1384 } // namespace vr_shell |
OLD | NEW |