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

Side by Side Diff: content/browser/device_orientation/data_fetcher_shared_memory_win.cc

Issue 22926032: Win: implement shared memory Device Orientation fetcher. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@Aug-23-mac-fetcher
Patch Set: modified to use InitSharedMemoryBuffer() to compile on win Created 7 years, 3 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
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "content/browser/device_orientation/data_fetcher_impl_win.h" 5 #include "data_fetcher_shared_memory.h"
6 6
7 #include <InitGuid.h> 7 #include <InitGuid.h>
8 #include <PortableDeviceTypes.h> 8 #include <PortableDeviceTypes.h>
9 #include <Sensors.h> 9 #include <Sensors.h>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/win/iunknown_impl.h" 12 #include "base/win/iunknown_impl.h"
13 #include "base/win/windows_version.h" 13 #include "base/win/windows_version.h"
14 #include "content/browser/device_orientation/orientation.h"
15 14
16 namespace { 15 namespace {
17 16
18 // This should match ProviderImpl::kDesiredSamplingIntervalMs.
19 const int kPeriodInMilliseconds = 100; 17 const int kPeriodInMilliseconds = 100;
20 18
21 } // namespace 19 } // namespace
22 20
23 namespace content { 21 namespace content {
24 22
25 class DataFetcherImplWin::SensorEventSink : public ISensorEvents, 23 class DataFetcherSharedMemory::SensorEventSink
26 public base::win::IUnknownImpl { 24 : public ISensorEvents, public base::win::IUnknownImpl {
27 public: 25 public:
28 explicit SensorEventSink(DataFetcherImplWin* const fetcher) 26 explicit SensorEventSink(DeviceOrientationHardwareBuffer* buffer)
29 : fetcher_(fetcher) {} 27 : buffer_(buffer) {}
30 28
31 virtual ~SensorEventSink() {} 29 virtual ~SensorEventSink() {}
32 30
33 // IUnknown interface 31 // IUnknown interface
34 virtual ULONG STDMETHODCALLTYPE AddRef() OVERRIDE { 32 virtual ULONG STDMETHODCALLTYPE AddRef() OVERRIDE {
35 return IUnknownImpl::AddRef(); 33 return IUnknownImpl::AddRef();
36 } 34 }
37 35
38 virtual ULONG STDMETHODCALLTYPE Release() OVERRIDE { 36 virtual ULONG STDMETHODCALLTYPE Release() OVERRIDE {
39 return IUnknownImpl::Release(); 37 return IUnknownImpl::Release();
(...skipping 14 matching lines...) Expand all
54 IPortableDeviceValues* event_data) OVERRIDE { 52 IPortableDeviceValues* event_data) OVERRIDE {
55 return S_OK; 53 return S_OK;
56 } 54 }
57 55
58 STDMETHODIMP OnDataUpdated(ISensor* sensor, 56 STDMETHODIMP OnDataUpdated(ISensor* sensor,
59 ISensorDataReport* new_data) OVERRIDE { 57 ISensorDataReport* new_data) OVERRIDE {
60 if (NULL == new_data || NULL == sensor) 58 if (NULL == new_data || NULL == sensor)
61 return E_INVALIDARG; 59 return E_INVALIDARG;
62 60
63 PROPVARIANT value = {}; 61 PROPVARIANT value = {};
64 scoped_refptr<Orientation> orientation = new Orientation(); 62 double alpha = 0;
63 bool hasAlpha = false;
bulach 2013/09/02 13:29:34 nit: has"_a"lpha (ditto for _b and _g below)
timvolodine 2013/09/03 17:14:51 Done.
64 double beta = 0;
65 bool hasBeta = false;
66 double gamma = 0;
67 bool hasGamma = false;
65 68
66 if (SUCCEEDED(new_data->GetSensorValue( 69 if (SUCCEEDED(new_data->GetSensorValue(
67 SENSOR_DATA_TYPE_TILT_X_DEGREES, &value))) { 70 SENSOR_DATA_TYPE_TILT_X_DEGREES, &value))) {
68 orientation->set_beta(value.fltVal); 71 beta = value.fltVal;
72 hasBeta = true;
69 } 73 }
70 PropVariantClear(&value); 74 PropVariantClear(&value);
71 75
72 if (SUCCEEDED(new_data->GetSensorValue( 76 if (SUCCEEDED(new_data->GetSensorValue(
73 SENSOR_DATA_TYPE_TILT_Y_DEGREES, &value))) { 77 SENSOR_DATA_TYPE_TILT_Y_DEGREES, &value))) {
74 orientation->set_gamma(value.fltVal); 78 gamma = value.fltVal;
79 hasGamma = true;
75 } 80 }
76 PropVariantClear(&value); 81 PropVariantClear(&value);
77 82
78 if (SUCCEEDED(new_data->GetSensorValue( 83 if (SUCCEEDED(new_data->GetSensorValue(
79 SENSOR_DATA_TYPE_TILT_Z_DEGREES, &value))) { 84 SENSOR_DATA_TYPE_TILT_Z_DEGREES, &value))) {
80 orientation->set_alpha(value.fltVal); 85 alpha = value.fltVal;
86 hasAlpha = true;
81 } 87 }
82 PropVariantClear(&value); 88 PropVariantClear(&value);
83 89
84 orientation->set_absolute(true); 90 buffer_->seqlock.WriteBegin();
85 fetcher_->OnOrientationData(orientation.get()); 91 buffer_->data.alpha = alpha;
92 buffer_->data.hasAlpha = hasAlpha;
93 buffer_->data.beta = beta;
94 buffer_->data.hasBeta = hasBeta;
95 buffer_->data.gamma = gamma;
96 buffer_->data.hasGamma = hasGamma;
97 buffer_->data.absolute = true;
98 buffer_->data.hasAbsolute = hasAlpha || hasBeta || hasGamma;
99 buffer_->data.allAvailableSensorsAreActive = true;
100 buffer_->seqlock.WriteEnd();
86 101
87 return S_OK; 102 return S_OK;
88 } 103 }
89 104
90 STDMETHODIMP OnLeave(REFSENSOR_ID sensor_id) OVERRIDE { 105 STDMETHODIMP OnLeave(REFSENSOR_ID sensor_id) OVERRIDE {
91 return S_OK; 106 return S_OK;
92 } 107 }
93 108
94 STDMETHODIMP OnStateChanged(ISensor* sensor, SensorState state) OVERRIDE { 109 STDMETHODIMP OnStateChanged(ISensor* sensor, SensorState state) OVERRIDE {
95 return S_OK; 110 return S_OK;
96 } 111 }
97 112
98 private: 113 private:
99 DataFetcherImplWin* const fetcher_; 114 DeviceOrientationHardwareBuffer* buffer_;
100 115
101 DISALLOW_COPY_AND_ASSIGN(SensorEventSink); 116 DISALLOW_COPY_AND_ASSIGN(SensorEventSink);
102 }; 117 };
103 118
104 // Create a DataFetcherImplWin object and return NULL if no valid sensor found.
105 // static
106 DataFetcher* DataFetcherImplWin::Create() {
107 scoped_ptr<DataFetcherImplWin> fetcher(new DataFetcherImplWin);
108 if (fetcher->Initialize())
109 return fetcher.release();
110 119
111 LOG(ERROR) << "DataFetcherImplWin::Initialize failed!"; 120 DataFetcherSharedMemory::DataFetcherSharedMemory() {
112 return NULL;
113 } 121 }
114 122
115 DataFetcherImplWin::~DataFetcherImplWin() { 123 DataFetcherSharedMemory::~DataFetcherSharedMemory() {
116 if (sensor_)
117 sensor_->SetEventSink(NULL);
118 } 124 }
119 125
120 DataFetcherImplWin::DataFetcherImplWin() { 126 bool DataFetcherSharedMemory::Start(ConsumerType consumer_type) {
121 }
122
123 void DataFetcherImplWin::OnOrientationData(Orientation* orientation) {
124 // This method is called on Windows sensor thread.
125 base::AutoLock autolock(next_orientation_lock_);
126 next_orientation_ = orientation;
127 }
128
129 const DeviceData* DataFetcherImplWin::GetDeviceData(DeviceData::Type type) {
130 if (type != DeviceData::kTypeOrientation)
131 return NULL;
132 return GetOrientation();
133 }
134
135 const Orientation* DataFetcherImplWin::GetOrientation() {
136 if (next_orientation_.get()) {
137 base::AutoLock autolock(next_orientation_lock_);
138 next_orientation_.swap(current_orientation_);
139 }
140 if (!current_orientation_.get())
141 return new Orientation();
142 return current_orientation_.get();
143 }
144
145 bool DataFetcherImplWin::Initialize() {
146 if (base::win::GetVersion() < base::win::VERSION_WIN7) 127 if (base::win::GetVersion() < base::win::VERSION_WIN7)
147 return false; 128 return false;
148 129
130 if (consumer_type != CONSUMER_TYPE_ORIENTATION)
131 return false;
132
133 // TODO(timvolodine): replace 3 lines below with:
134 // orientation_buffer_ = static_cast<DeviceOrientationHardwareBuffer*>(
135 // GetSharedMemoryBuffer(consumer_type));
136 // DCHECK(orientation_buffer_);
137 // once https://codereview.chromium.org/23684005/ lands.
138 orientation_buffer_ = static_cast<DeviceOrientationHardwareBuffer*>(
139 InitSharedMemoryBuffer(consumer_type,
140 sizeof(DeviceOrientationHardwareBuffer)));
141
149 base::win::ScopedComPtr<ISensorManager> sensor_manager; 142 base::win::ScopedComPtr<ISensorManager> sensor_manager;
150 HRESULT hr = sensor_manager.CreateInstance(CLSID_SensorManager); 143 HRESULT hr = sensor_manager.CreateInstance(CLSID_SensorManager);
151 if (FAILED(hr) || !sensor_manager) 144 if (FAILED(hr) || !sensor_manager)
152 return false; 145 return false;
153 146
154 base::win::ScopedComPtr<ISensorCollection> sensor_collection; 147 base::win::ScopedComPtr<ISensorCollection> sensor_collection;
155 hr = sensor_manager->GetSensorsByType( 148 hr = sensor_manager->GetSensorsByType(
156 SENSOR_TYPE_INCLINOMETER_3D, sensor_collection.Receive()); 149 SENSOR_TYPE_INCLINOMETER_3D, sensor_collection.Receive());
157 150
158 if (FAILED(hr) || !sensor_collection) 151 if (FAILED(hr) || !sensor_collection)
159 return false; 152 return false;
160 153
161 ULONG count = 0; 154 ULONG count = 0;
162 hr = sensor_collection->GetCount(&count); 155 hr = sensor_collection->GetCount(&count);
163 if (FAILED(hr) || !count) 156 if (FAILED(hr) || !count)
164 return false; 157 return false;
165 158
166 hr = sensor_collection->GetAt(0, sensor_.Receive()); 159 hr = sensor_collection->GetAt(0, sensor_.Receive());
167 if (FAILED(hr) || !sensor_) 160 if (FAILED(hr) || !sensor_)
168 return false; 161 return false;
169 162
170 base::win::ScopedComPtr<IPortableDeviceValues> device_values; 163 base::win::ScopedComPtr<IPortableDeviceValues> device_values;
171 if (SUCCEEDED(device_values.CreateInstance(CLSID_PortableDeviceValues))) { 164 if (SUCCEEDED(device_values.CreateInstance(CLSID_PortableDeviceValues))) {
172 if (SUCCEEDED(device_values->SetUnsignedIntegerValue( 165 if (SUCCEEDED(device_values->SetUnsignedIntegerValue(
173 SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, kPeriodInMilliseconds))) { 166 SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, kPeriodInMilliseconds))) {
174 base::win::ScopedComPtr<IPortableDeviceValues> return_values; 167 base::win::ScopedComPtr<IPortableDeviceValues> return_values;
175 sensor_->SetProperties(device_values.get(), return_values.Receive()); 168 sensor_->SetProperties(device_values.get(), return_values.Receive());
176 } 169 }
177 } 170 }
178 171
179 scoped_refptr<SensorEventSink> sensor_event_impl(new SensorEventSink(this)); 172 scoped_refptr<SensorEventSink> sensor_event_impl(new SensorEventSink(
173 orientation_buffer_));
174
180 base::win::ScopedComPtr<ISensorEvents> sensor_events; 175 base::win::ScopedComPtr<ISensorEvents> sensor_events;
181 hr = sensor_event_impl->QueryInterface( 176 hr = sensor_event_impl->QueryInterface(
182 __uuidof(ISensorEvents), sensor_events.ReceiveVoid()); 177 __uuidof(ISensorEvents), sensor_events.ReceiveVoid());
183 if (FAILED(hr) || !sensor_events) 178 if (FAILED(hr) || !sensor_events)
184 return false; 179 return false;
185 180
186 hr = sensor_->SetEventSink(sensor_events); 181 hr = sensor_->SetEventSink(sensor_events);
187 if (FAILED(hr)) 182 if (FAILED(hr))
188 return false; 183 return false;
189 184
190 return true; 185 return true;
191 } 186 }
192 187
188 bool DataFetcherSharedMemory::Stop(ConsumerType consumer_type) {
189 if (consumer_type != CONSUMER_TYPE_ORIENTATION)
190 return false;
191
192 if (sensor_)
193 sensor_->SetEventSink(NULL);
194
195 if (orientation_buffer_) {
196 orientation_buffer_->seqlock.WriteBegin();
197 orientation_buffer_->data.allAvailableSensorsAreActive = false;
198 orientation_buffer_->seqlock.WriteEnd();
199 orientation_buffer_ = NULL;
200 }
201
202 return true;
203 }
204
205
193 } // namespace content 206 } // namespace content
207
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698