Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(482)

Side by Side Diff: third_party/WebKit/Source/modules/vr/VRController.cpp

Issue 2420743003: mojo VR interface simplified (Closed)
Patch Set: Add PRE_FINALIZER for VRController according to haraken@ comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "modules/vr/VRController.h" 5 #include "modules/vr/VRController.h"
6 6
7 #include "core/dom/DOMException.h" 7 #include "core/dom/DOMException.h"
8 #include "core/dom/Document.h" 8 #include "core/dom/Document.h"
9 #include "core/frame/LocalFrame.h" 9 #include "core/frame/LocalFrame.h"
10 #include "modules/vr/NavigatorVR.h" 10 #include "modules/vr/NavigatorVR.h"
11 #include "modules/vr/VRGetDevicesCallback.h" 11 #include "modules/vr/VRGetDevicesCallback.h"
12 #include "public/platform/InterfaceProvider.h" 12 #include "public/platform/InterfaceProvider.h"
13 13
14 #include "wtf/Assertions.h" 14 #include "wtf/Assertions.h"
15 15
16 namespace blink { 16 namespace blink {
17 17
18 VRController::VRController(NavigatorVR* navigatorVR) 18 VRController::VRController(NavigatorVR* navigatorVR)
19 : ContextLifecycleObserver(navigatorVR->document()), 19 : ContextLifecycleObserver(navigatorVR->document()),
20 m_navigatorVR(navigatorVR), 20 m_navigatorVR(navigatorVR),
21 m_displaySynced(false),
21 m_binding(this) { 22 m_binding(this) {
22 navigatorVR->document()->frame()->interfaceProvider()->getInterface( 23 navigatorVR->document()->frame()->interfaceProvider()->getInterface(
23 mojo::GetProxy(&m_service)); 24 mojo::GetProxy(&m_service));
24 m_service->SetClient(m_binding.CreateInterfacePtrAndBind()); 25 m_service->SetClient(
26 m_binding.CreateInterfacePtrAndBind(),
27 convertToBaseCallback(
28 WTF::bind(&VRController::onDisplaysSynced, wrapPersistent(this))));
haraken 2016/11/02 08:25:11 You need to call ThreadState::current()->registerP
shaobo.yan 2016/11/02 09:22:38 Thx for telling me that!
25 } 29 }
26 30
27 VRController::~VRController() {} 31 VRController::~VRController() {}
28 32
29 void VRController::getDisplays(ScriptPromiseResolver* resolver) { 33 void VRController::getDisplays(ScriptPromiseResolver* resolver) {
30 if (!m_service) { 34 if (!m_service) {
31 DOMException* exception = DOMException::create( 35 DOMException* exception = DOMException::create(
32 InvalidStateError, "The service is no longer active."); 36 InvalidStateError, "The service is no longer active.");
33 resolver->reject(exception); 37 resolver->reject(exception);
34 return; 38 return;
35 } 39 }
36 40
37 m_pendingGetDevicesCallbacks.append( 41 // If we've previously synced the VRDisplays just return the current list.
38 WTF::wrapUnique(new VRGetDevicesCallback(resolver))); 42 if (m_displaySynced) {
39 m_service->GetDisplays(convertToBaseCallback( 43 resolver->resolve(m_displays);
40 WTF::bind(&VRController::onGetDisplays, wrapPersistent(this))));
41 }
42
43 device::blink::VRPosePtr VRController::getPose(unsigned index) {
44 if (!m_service)
45 return nullptr;
46
47 device::blink::VRPosePtr pose;
48 m_service->GetPose(index, &pose);
49 return pose;
50 }
51
52 void VRController::resetPose(unsigned index) {
53 if (!m_service)
54 return;
55
56 m_service->ResetPose(index);
57 }
58
59 void VRController::requestPresent(ScriptPromiseResolver* resolver,
60 unsigned index,
61 bool secureOrigin) {
62 if (!m_service) {
63 DOMException* exception = DOMException::create(
64 InvalidStateError, "The service is no longer active.");
65 resolver->reject(exception);
66 ReportPresentationResult(PresentationResult::ServiceInactive);
67 return; 44 return;
68 } 45 }
69 46
70 m_service->RequestPresent( 47 // Otherwise we're still waiting for the full list of displays to be populated
71 index, secureOrigin, 48 // so queue up the promise for resolution when onDisplaysSynced is called.
72 convertToBaseCallback(WTF::bind(&VRController::onPresentComplete, 49 m_pendingGetDevicesCallbacks.append(
73 wrapPersistent(this), 50 WTF::wrapUnique(new VRGetDevicesCallback(resolver)));
74 wrapPersistent(resolver), index)));
75 } 51 }
76 52
77 void VRController::exitPresent(unsigned index) { 53 // Each time a new VRDisplay is connected we'll recieve a VRDisplayPtr for it
78 if (!m_service) 54 // here. Upon calling SetClient in the constructor we should receive one call
79 return; 55 // for each VRDisplay that was already connected at the time.
56 void VRController::OnDisplayConnected(
57 device::mojom::blink::VRDisplayPtr display,
58 device::mojom::blink::VRDisplayInfoPtr displayInfo) {
59 VRDisplay* vrDisplay = new VRDisplay(m_navigatorVR, std::move(display));
60 vrDisplay->update(displayInfo);
61 vrDisplay->onDisplayConnected();
62 m_displays.append(vrDisplay);
80 63
81 m_service->ExitPresent(index); 64 if (m_displays.size() == m_displaysSyncedNum) {
82 } 65 m_displaySynced = true;
83 66 onGetDisplays();
84 void VRController::submitFrame(unsigned index, device::blink::VRPosePtr pose) {
85 if (!m_service)
86 return;
87
88 m_service->SubmitFrame(index, std::move(pose));
89 }
90
91 void VRController::updateLayerBounds(
92 unsigned index,
93 device::blink::VRLayerBoundsPtr leftBounds,
94 device::blink::VRLayerBoundsPtr rightBounds) {
95 if (!m_service)
96 return;
97
98 m_service->UpdateLayerBounds(index, std::move(leftBounds),
99 std::move(rightBounds));
100 }
101
102 VRDisplay* VRController::createOrUpdateDisplay(
103 const device::blink::VRDisplayPtr& display) {
104 VRDisplay* vrDisplay = getDisplayForIndex(display->index);
105 if (!vrDisplay) {
106 vrDisplay = new VRDisplay(m_navigatorVR);
107 m_displays.append(vrDisplay);
108 }
109
110 vrDisplay->update(display);
111 return vrDisplay;
112 }
113
114 VRDisplayVector VRController::updateDisplays(
115 mojo::WTFArray<device::blink::VRDisplayPtr> displays) {
116 VRDisplayVector vrDisplays;
117
118 for (const auto& display : displays.PassStorage()) {
119 VRDisplay* vrDisplay = createOrUpdateDisplay(display);
120 vrDisplays.append(vrDisplay);
121 }
122
123 return vrDisplays;
124 }
125
126 VRDisplay* VRController::getDisplayForIndex(unsigned index) {
127 VRDisplay* display;
128 for (size_t i = 0; i < m_displays.size(); ++i) {
129 display = m_displays[i];
130 if (display->displayId() == index) {
131 return display;
132 }
133 }
134
135 return 0;
136 }
137
138 void VRController::onGetDisplays(
139 mojo::WTFArray<device::blink::VRDisplayPtr> displays) {
140 VRDisplayVector outDisplays = updateDisplays(std::move(displays));
141
142 std::unique_ptr<VRGetDevicesCallback> callback =
143 m_pendingGetDevicesCallbacks.takeFirst();
144 if (!callback)
145 return;
146
147 callback->onSuccess(outDisplays);
148 }
149
150 void VRController::onPresentComplete(ScriptPromiseResolver* resolver,
151 unsigned index,
152 bool success) {
153 VRDisplay* vrDisplay = getDisplayForIndex(index);
154 if (!vrDisplay) {
155 DOMException* exception =
156 DOMException::create(InvalidStateError, "VRDisplay not found.");
157 resolver->reject(exception);
158 ReportPresentationResult(PresentationResult::VRDisplayNotFound);
159 return;
160 }
161
162 if (success) {
163 vrDisplay->beginPresent(resolver);
164 } else {
165 vrDisplay->forceExitPresent();
166 DOMException* exception = DOMException::create(
167 NotAllowedError, "Presentation request was denied.");
168 ReportPresentationResult(PresentationResult::RequestDenied);
169 resolver->reject(exception);
170 } 67 }
171 } 68 }
172 69
173 void VRController::OnDisplayChanged(device::blink::VRDisplayPtr display) { 70 // Called when the VRService has called OnDisplayConnected for all active
174 VRDisplay* vrDisplay = getDisplayForIndex(display->index); 71 // VRDisplays.
175 if (!vrDisplay) 72 void VRController::onDisplaysSynced(unsigned num) {
176 return; 73 m_displaysSyncedNum = num;
177 74 if (m_displaysSyncedNum == m_displays.size()) {
178 vrDisplay->update(display); 75 m_displaySynced = true;
76 onGetDisplays();
77 }
179 } 78 }
180 79
181 void VRController::OnExitPresent(unsigned index) { 80 void VRController::onGetDisplays() {
182 VRDisplay* vrDisplay = getDisplayForIndex(index); 81 while (!m_pendingGetDevicesCallbacks.isEmpty()) {
183 if (vrDisplay) 82 std::unique_ptr<VRGetDevicesCallback> callback =
184 vrDisplay->forceExitPresent(); 83 m_pendingGetDevicesCallbacks.takeFirst();
185 } 84 callback->onSuccess(m_displays);
186 85 }
187 void VRController::OnDisplayConnected(device::blink::VRDisplayPtr display) {
188 VRDisplay* vrDisplay = createOrUpdateDisplay(display);
189 if (!vrDisplay)
190 return;
191
192 m_navigatorVR->fireVREvent(VRDisplayEvent::create(
193 EventTypeNames::vrdisplayconnect, true, false, vrDisplay, "connect"));
194 }
195
196 void VRController::OnDisplayDisconnected(unsigned index) {
197 VRDisplay* vrDisplay = getDisplayForIndex(index);
198 if (!vrDisplay)
199 return;
200
201 vrDisplay->disconnected();
202
203 m_navigatorVR->fireVREvent(
204 VRDisplayEvent::create(EventTypeNames::vrdisplaydisconnect, true, false,
205 vrDisplay, "disconnect"));
206 } 86 }
207 87
208 void VRController::contextDestroyed() { 88 void VRController::contextDestroyed() {
209 // If the document context was destroyed, shut down the client connection 89 // If the document context was destroyed, shut down the client connection
210 // and never call the mojo service again. 90 // and never call the mojo service again.
91 m_service.reset();
211 m_binding.Close(); 92 m_binding.Close();
212 m_service.reset(); 93
94 // Shutdown all displays' message pipe
95 for (size_t i = 0; i < m_displays.size(); ++i)
96 m_displays[i]->shutdownMessagePipe();
213 97
214 // The context is not automatically cleared, so do it manually. 98 // The context is not automatically cleared, so do it manually.
215 ContextLifecycleObserver::clearContext(); 99 ContextLifecycleObserver::clearContext();
216 } 100 }
217 101
218 DEFINE_TRACE(VRController) { 102 DEFINE_TRACE(VRController) {
219 visitor->trace(m_navigatorVR); 103 visitor->trace(m_navigatorVR);
220 visitor->trace(m_displays); 104 visitor->trace(m_displays);
221 105
222 ContextLifecycleObserver::trace(visitor); 106 ContextLifecycleObserver::trace(visitor);
223 } 107 }
224 108
225 } // namespace blink 109 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698