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

Side by Side Diff: device/vr/android/gvr/gvr_gamepad_data_fetcher.cc

Issue 2658643003: Refactor GvrDelegate ownership into GvrDelegateProvider and fix more threading violations. (Closed)
Patch Set: Address comments Created 3 years, 10 months 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
« no previous file with comments | « device/vr/android/gvr/gvr_gamepad_data_fetcher.h ('k') | device/vr/test/fake_vr_device.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "device/vr/android/gvr/gvr_gamepad_data_fetcher.h" 5 #include "device/vr/android/gvr/gvr_gamepad_data_fetcher.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 9
10 #include "device/vr/android/gvr/gvr_delegate.h" 10 #include "device/vr/android/gvr/gvr_delegate.h"
11 #include "third_party/WebKit/public/platform/WebGamepads.h" 11 #include "third_party/WebKit/public/platform/WebGamepads.h"
12 #include "third_party/gvr-android-sdk/src/libraries/headers/vr/gvr/capi/include/ gvr.h"
12 13
13 namespace device { 14 namespace device {
14 15
15 namespace { 16 namespace {
16 17
17 void CopyToWebUString(blink::WebUChar* dest, 18 void CopyToWebUString(blink::WebUChar* dest,
18 size_t dest_length, 19 size_t dest_length,
19 base::string16 src) { 20 base::string16 src) {
20 static_assert(sizeof(base::string16::value_type) == sizeof(blink::WebUChar), 21 static_assert(sizeof(base::string16::value_type) == sizeof(blink::WebUChar),
21 "Mismatched string16/WebUChar size."); 22 "Mismatched string16/WebUChar size.");
22 23
23 const size_t str_to_copy = std::min(src.size(), dest_length - 1); 24 const size_t str_to_copy = std::min(src.size(), dest_length - 1);
24 memcpy(dest, src.data(), str_to_copy * sizeof(base::string16::value_type)); 25 memcpy(dest, src.data(), str_to_copy * sizeof(base::string16::value_type));
25 dest[str_to_copy] = 0; 26 dest[str_to_copy] = 0;
26 } 27 }
27 28
28 } // namespace 29 } // namespace
29 30
30 using namespace blink; 31 using namespace blink;
31 32
32 GvrGamepadDataFetcher::Factory::Factory(GvrDelegate* delegate, 33 GvrGamepadDataFetcher::Factory::Factory(gvr_context* context,
33 unsigned int display_id) 34 unsigned int display_id)
34 : delegate_(delegate), display_id_(display_id) {} 35 : context_(context), display_id_(display_id) {}
35 36
36 GvrGamepadDataFetcher::Factory::~Factory() {} 37 GvrGamepadDataFetcher::Factory::~Factory() {}
37 38
38 std::unique_ptr<GamepadDataFetcher> 39 std::unique_ptr<GamepadDataFetcher>
39 GvrGamepadDataFetcher::Factory::CreateDataFetcher() { 40 GvrGamepadDataFetcher::Factory::CreateDataFetcher() {
40 if (!delegate_) 41 return base::MakeUnique<GvrGamepadDataFetcher>(context_, display_id_);
41 return nullptr;
42 return base::MakeUnique<GvrGamepadDataFetcher>(delegate_, display_id_);
43 } 42 }
44 43
45 GamepadSource GvrGamepadDataFetcher::Factory::source() { 44 GamepadSource GvrGamepadDataFetcher::Factory::source() {
46 return GAMEPAD_SOURCE_GVR; 45 return GAMEPAD_SOURCE_GVR;
47 } 46 }
48 47
49 GvrGamepadDataFetcher::GvrGamepadDataFetcher(GvrDelegate* delegate, 48 GvrGamepadDataFetcher::GvrGamepadDataFetcher(gvr_context* context,
50 unsigned int display_id) 49 unsigned int display_id)
51 : display_id_(display_id) { 50 : display_id_(display_id) {
52 gvr::GvrApi* gvr_api = delegate->gvr_api();
53 controller_api_.reset(new gvr::ControllerApi()); 51 controller_api_.reset(new gvr::ControllerApi());
54 int32_t options = gvr::ControllerApi::DefaultOptions(); 52 int32_t options = gvr::ControllerApi::DefaultOptions();
55 options |= GVR_CONTROLLER_ENABLE_GYRO; 53 options |= GVR_CONTROLLER_ENABLE_GYRO;
56 bool success = controller_api_->Init(options, gvr_api->GetContext()); 54
55 // TODO(mthiesse): Use of the gvr_context on multiple threads isn't guaranteed
56 // to be threadsafe. All gvr context usage should be moved to VR Shell's GL
57 // thread. crbug.com/674594
58 std::unique_ptr<gvr::GvrApi> gvr = gvr::GvrApi::WrapNonOwned(context);
59 // TODO(bajones): Monitor changes to the controller handedness.
60 handedness_ = gvr->GetUserPrefs().GetControllerHandedness();
61
62 bool success = controller_api_->Init(options, context);
57 if (!success) 63 if (!success)
58 controller_api_.reset(nullptr); 64 controller_api_.reset(nullptr);
59
60 // TODO(bajones): Monitor changes to the controller handedness.
61 handedness_ = gvr_api->GetUserPrefs().GetControllerHandedness();
62 } 65 }
63 66
64 GvrGamepadDataFetcher::~GvrGamepadDataFetcher() {} 67 GvrGamepadDataFetcher::~GvrGamepadDataFetcher() {}
65 68
66 GamepadSource GvrGamepadDataFetcher::source() { 69 GamepadSource GvrGamepadDataFetcher::source() {
67 return GAMEPAD_SOURCE_GVR; 70 return GAMEPAD_SOURCE_GVR;
68 } 71 }
69 72
70 void GvrGamepadDataFetcher::OnAddedToProvider() { 73 void GvrGamepadDataFetcher::OnAddedToProvider() {
71 PauseHint(false); 74 PauseHint(false);
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 return; 147 return;
145 148
146 if (paused) { 149 if (paused) {
147 controller_api_->Pause(); 150 controller_api_->Pause();
148 } else { 151 } else {
149 controller_api_->Resume(); 152 controller_api_->Resume();
150 } 153 }
151 } 154 }
152 155
153 } // namespace device 156 } // namespace device
OLDNEW
« no previous file with comments | « device/vr/android/gvr/gvr_gamepad_data_fetcher.h ('k') | device/vr/test/fake_vr_device.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698