Chromium Code Reviews| Index: content/browser/device_orientation/data_fetcher_shared_memory_win.cc |
| diff --git a/content/browser/device_orientation/data_fetcher_impl_win.cc b/content/browser/device_orientation/data_fetcher_shared_memory_win.cc |
| similarity index 59% |
| copy from content/browser/device_orientation/data_fetcher_impl_win.cc |
| copy to content/browser/device_orientation/data_fetcher_shared_memory_win.cc |
| index d08ed446ad623f4c8e577cc7fd97fdf01f14f559..e39b8b08c3439905c7e8390fa7f74495104de48c 100644 |
| --- a/content/browser/device_orientation/data_fetcher_impl_win.cc |
| +++ b/content/browser/device_orientation/data_fetcher_shared_memory_win.cc |
| @@ -1,8 +1,8 @@ |
| -// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "content/browser/device_orientation/data_fetcher_impl_win.h" |
| +#include "data_fetcher_shared_memory.h" |
| #include <InitGuid.h> |
| #include <PortableDeviceTypes.h> |
| @@ -11,22 +11,20 @@ |
| #include "base/logging.h" |
| #include "base/win/iunknown_impl.h" |
| #include "base/win/windows_version.h" |
| -#include "content/browser/device_orientation/orientation.h" |
| namespace { |
| -// This should match ProviderImpl::kDesiredSamplingIntervalMs. |
| const int kPeriodInMilliseconds = 100; |
| } // namespace |
| namespace content { |
| -class DataFetcherImplWin::SensorEventSink : public ISensorEvents, |
| - public base::win::IUnknownImpl { |
| +class DataFetcherSharedMemory::SensorEventSink |
| + : public ISensorEvents, public base::win::IUnknownImpl { |
| public: |
| - explicit SensorEventSink(DataFetcherImplWin* const fetcher) |
| - : fetcher_(fetcher) {} |
| + explicit SensorEventSink(DeviceOrientationHardwareBuffer* buffer) |
| + : buffer_(buffer) {} |
| virtual ~SensorEventSink() {} |
| @@ -61,28 +59,45 @@ class DataFetcherImplWin::SensorEventSink : public ISensorEvents, |
| return E_INVALIDARG; |
| PROPVARIANT value = {}; |
| - scoped_refptr<Orientation> orientation = new Orientation(); |
| + double alpha = 0; |
| + 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.
|
| + double beta = 0; |
| + bool hasBeta = false; |
| + double gamma = 0; |
| + bool hasGamma = false; |
| if (SUCCEEDED(new_data->GetSensorValue( |
| - SENSOR_DATA_TYPE_TILT_X_DEGREES, &value))) { |
| - orientation->set_beta(value.fltVal); |
| + SENSOR_DATA_TYPE_TILT_X_DEGREES, &value))) { |
| + beta = value.fltVal; |
| + hasBeta = true; |
| } |
| PropVariantClear(&value); |
| if (SUCCEEDED(new_data->GetSensorValue( |
| - SENSOR_DATA_TYPE_TILT_Y_DEGREES, &value))) { |
| - orientation->set_gamma(value.fltVal); |
| + SENSOR_DATA_TYPE_TILT_Y_DEGREES, &value))) { |
| + gamma = value.fltVal; |
| + hasGamma = true; |
| } |
| PropVariantClear(&value); |
| if (SUCCEEDED(new_data->GetSensorValue( |
| SENSOR_DATA_TYPE_TILT_Z_DEGREES, &value))) { |
| - orientation->set_alpha(value.fltVal); |
| + alpha = value.fltVal; |
| + hasAlpha = true; |
| } |
| PropVariantClear(&value); |
| - orientation->set_absolute(true); |
| - fetcher_->OnOrientationData(orientation.get()); |
| + buffer_->seqlock.WriteBegin(); |
| + buffer_->data.alpha = alpha; |
| + buffer_->data.hasAlpha = hasAlpha; |
| + buffer_->data.beta = beta; |
| + buffer_->data.hasBeta = hasBeta; |
| + buffer_->data.gamma = gamma; |
| + buffer_->data.hasGamma = hasGamma; |
| + buffer_->data.absolute = true; |
| + buffer_->data.hasAbsolute = hasAlpha || hasBeta || hasGamma; |
| + buffer_->data.allAvailableSensorsAreActive = true; |
| + buffer_->seqlock.WriteEnd(); |
| return S_OK; |
| } |
| @@ -96,56 +111,34 @@ class DataFetcherImplWin::SensorEventSink : public ISensorEvents, |
| } |
| private: |
| - DataFetcherImplWin* const fetcher_; |
| + DeviceOrientationHardwareBuffer* buffer_; |
| DISALLOW_COPY_AND_ASSIGN(SensorEventSink); |
| }; |
| -// Create a DataFetcherImplWin object and return NULL if no valid sensor found. |
| -// static |
| -DataFetcher* DataFetcherImplWin::Create() { |
| - scoped_ptr<DataFetcherImplWin> fetcher(new DataFetcherImplWin); |
| - if (fetcher->Initialize()) |
| - return fetcher.release(); |
| - LOG(ERROR) << "DataFetcherImplWin::Initialize failed!"; |
| - return NULL; |
| +DataFetcherSharedMemory::DataFetcherSharedMemory() { |
| } |
| -DataFetcherImplWin::~DataFetcherImplWin() { |
| - if (sensor_) |
| - sensor_->SetEventSink(NULL); |
| -} |
| - |
| -DataFetcherImplWin::DataFetcherImplWin() { |
| -} |
| - |
| -void DataFetcherImplWin::OnOrientationData(Orientation* orientation) { |
| - // This method is called on Windows sensor thread. |
| - base::AutoLock autolock(next_orientation_lock_); |
| - next_orientation_ = orientation; |
| -} |
| - |
| -const DeviceData* DataFetcherImplWin::GetDeviceData(DeviceData::Type type) { |
| - if (type != DeviceData::kTypeOrientation) |
| - return NULL; |
| - return GetOrientation(); |
| -} |
| - |
| -const Orientation* DataFetcherImplWin::GetOrientation() { |
| - if (next_orientation_.get()) { |
| - base::AutoLock autolock(next_orientation_lock_); |
| - next_orientation_.swap(current_orientation_); |
| - } |
| - if (!current_orientation_.get()) |
| - return new Orientation(); |
| - return current_orientation_.get(); |
| +DataFetcherSharedMemory::~DataFetcherSharedMemory() { |
| } |
| -bool DataFetcherImplWin::Initialize() { |
| +bool DataFetcherSharedMemory::Start(ConsumerType consumer_type) { |
| if (base::win::GetVersion() < base::win::VERSION_WIN7) |
| return false; |
| + if (consumer_type != CONSUMER_TYPE_ORIENTATION) |
| + return false; |
| + |
| + // TODO(timvolodine): replace 3 lines below with: |
| + // orientation_buffer_ = static_cast<DeviceOrientationHardwareBuffer*>( |
| + // GetSharedMemoryBuffer(consumer_type)); |
| + // DCHECK(orientation_buffer_); |
| + // once https://codereview.chromium.org/23684005/ lands. |
| + orientation_buffer_ = static_cast<DeviceOrientationHardwareBuffer*>( |
| + InitSharedMemoryBuffer(consumer_type, |
| + sizeof(DeviceOrientationHardwareBuffer))); |
| + |
| base::win::ScopedComPtr<ISensorManager> sensor_manager; |
| HRESULT hr = sensor_manager.CreateInstance(CLSID_SensorManager); |
| if (FAILED(hr) || !sensor_manager) |
| @@ -170,13 +163,15 @@ bool DataFetcherImplWin::Initialize() { |
| base::win::ScopedComPtr<IPortableDeviceValues> device_values; |
| if (SUCCEEDED(device_values.CreateInstance(CLSID_PortableDeviceValues))) { |
| if (SUCCEEDED(device_values->SetUnsignedIntegerValue( |
| - SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, kPeriodInMilliseconds))) { |
| + SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, kPeriodInMilliseconds))) { |
| base::win::ScopedComPtr<IPortableDeviceValues> return_values; |
| sensor_->SetProperties(device_values.get(), return_values.Receive()); |
| } |
| } |
| - scoped_refptr<SensorEventSink> sensor_event_impl(new SensorEventSink(this)); |
| + scoped_refptr<SensorEventSink> sensor_event_impl(new SensorEventSink( |
| + orientation_buffer_)); |
| + |
| base::win::ScopedComPtr<ISensorEvents> sensor_events; |
| hr = sensor_event_impl->QueryInterface( |
| __uuidof(ISensorEvents), sensor_events.ReceiveVoid()); |
| @@ -190,4 +185,23 @@ bool DataFetcherImplWin::Initialize() { |
| return true; |
| } |
| +bool DataFetcherSharedMemory::Stop(ConsumerType consumer_type) { |
| + if (consumer_type != CONSUMER_TYPE_ORIENTATION) |
| + return false; |
| + |
| + if (sensor_) |
| + sensor_->SetEventSink(NULL); |
| + |
| + if (orientation_buffer_) { |
| + orientation_buffer_->seqlock.WriteBegin(); |
| + orientation_buffer_->data.allAvailableSensorsAreActive = false; |
| + orientation_buffer_->seqlock.WriteEnd(); |
| + orientation_buffer_ = NULL; |
| + } |
| + |
| + return true; |
| +} |
| + |
| + |
| } // namespace content |
| + |