Index: content/renderer/device_sensors/device_orientation_event_pump.cc |
diff --git a/content/renderer/device_sensors/device_orientation_event_pump.cc b/content/renderer/device_sensors/device_orientation_event_pump.cc |
index 2ad8e773ee2bbdc0c2f8ca8670e83ee0d7bcdc2b..5932255442ed2aa436ff60f007f7308a028153d7 100644 |
--- a/content/renderer/device_sensors/device_orientation_event_pump.cc |
+++ b/content/renderer/device_sensors/device_orientation_event_pump.cc |
@@ -2,74 +2,116 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "device_orientation_event_pump.h" |
- |
-#include <string.h> |
- |
-#include <cmath> |
+#include "content/renderer/device_sensors/device_orientation_event_pump.h" |
+#include "base/memory/ptr_util.h" |
#include "content/public/renderer/render_thread.h" |
+#include "content/renderer/device_sensors/device_orientation_util.h" |
#include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceOrientationListener.h" |
namespace content { |
-const double DeviceOrientationEventPumpBase::kOrientationThreshold = 0.1; |
- |
-DeviceOrientationEventPumpBase::DeviceOrientationEventPumpBase( |
- RenderThread* thread) |
+DeviceOrientationEventPump::DeviceOrientationEventPump(RenderThread* thread) |
: DeviceSensorEventPump<blink::WebDeviceOrientationListener>(thread) {} |
-DeviceOrientationEventPumpBase::~DeviceOrientationEventPumpBase() {} |
+DeviceOrientationEventPump::~DeviceOrientationEventPump() {} |
+ |
+void DeviceOrientationEventPump::SendStartMessage() { |
+ sensors_.push_back(base::MakeUnique<SensorEntry>(this)); |
+ sensors_.push_back(base::MakeUnique<SensorEntry>(this)); |
+ sensors_.push_back(base::MakeUnique<SensorEntry>(this)); |
+ sensors_.push_back(base::MakeUnique<SensorEntry>(this)); |
+ |
+ // For DeviceOrientation event we use a 3-way fallback approach where up to |
+ // 3 different sets of sensors are attempted if necessary. The sensors to be |
+ // used for orientation are determined in the following order: |
+ // A: RELATIVE_ORIENTATION (relative) |
+ // B: ABSOLUTE_ORIENTATION (absolute) |
+ // C: combination of ACCELEROMETER and MAGNETOMETER (absolute) |
+ GetSensor(device::mojom::SensorType::RELATIVE_ORIENTATION, sensors_[0].get()); |
+ GetSensor(device::mojom::SensorType::ABSOLUTE_ORIENTATION, sensors_[1].get()); |
+ GetSensor(device::mojom::SensorType::ACCELEROMETER, sensors_[2].get()); |
+ GetSensor(device::mojom::SensorType::MAGNETOMETER, sensors_[3].get()); |
Reilly Grant (use Gerrit)
2017/05/19 20:20:11
We don't want to activate all four of these sensor
juncai
2017/05/20 00:55:02
Will fix it in the device orientation refactoring
|
+} |
-void DeviceOrientationEventPumpBase::FireEvent() { |
- DCHECK(listener()); |
- device::OrientationData data; |
- if (reader_->GetLatestData(&data) && ShouldFireEvent(data)) { |
- memcpy(&data_, &data, sizeof(data)); |
- listener()->DidChangeDeviceOrientation(data); |
- } |
+void DeviceOrientationEventPump::SendFakeDataForTesting(void* fake_data) { |
+ blink::WebDeviceOrientationData data = |
+ *static_cast<blink::WebDeviceOrientationData*>(fake_data); |
+ listener()->DidChangeDeviceOrientation(data); |
} |
-static bool IsSignificantlyDifferent(bool hasAngle1, double angle1, |
- bool hasAngle2, double angle2) { |
- if (hasAngle1 != hasAngle2) |
- return true; |
- return (hasAngle1 && |
- std::fabs(angle1 - angle2) >= |
- DeviceOrientationEventPumpBase::kOrientationThreshold); |
+bool DeviceOrientationEventPump::CanStart() const { |
+ DCHECK_EQ(4u, sensors_.size()); |
+ return (sensors_[0]->active || sensors_[1]->active || |
+ (sensors_[2]->active && sensors_[3]->active)); |
} |
-bool DeviceOrientationEventPumpBase::ShouldFireEvent( |
- const device::OrientationData& data) const { |
- if (!data.all_available_sensors_are_active) |
- return false; |
+void DeviceOrientationEventPump::FireEvent() { |
+ blink::WebDeviceOrientationData data; |
- if (!data.has_alpha && !data.has_beta && !data.has_gamma) { |
- // no data can be provided, this is an all-null event. |
- return true; |
- } |
- |
- return IsSignificantlyDifferent(data_.has_alpha, data_.alpha, data.has_alpha, |
- data.alpha) || |
- IsSignificantlyDifferent(data_.has_beta, data_.beta, data.has_beta, |
- data.beta) || |
- IsSignificantlyDifferent(data_.has_gamma, data_.gamma, data.has_gamma, |
- data.gamma); |
-} |
+ DCHECK(listener()); |
-bool DeviceOrientationEventPumpBase::InitializeReader( |
- base::SharedMemoryHandle handle) { |
- memset(&data_, 0, sizeof(data_)); |
- if (!reader_) |
- reader_.reset(new DeviceOrientationSharedMemoryReader()); |
- return reader_->Initialize(handle); |
+ if (GetDataFromSharedMemory(&data) && IsSignificantlyDifferent(data_, data)) { |
+ data_ = data; |
+ listener()->DidChangeDeviceOrientation(data); |
+ } |
} |
-void DeviceOrientationEventPumpBase::SendFakeDataForTesting(void* fake_data) { |
- device::OrientationData data = |
- *static_cast<device::OrientationData*>(fake_data); |
+bool DeviceOrientationEventPump::GetDataFromSharedMemory( |
+ blink::WebDeviceOrientationData* data) { |
+ DCHECK_EQ(4u, sensors_.size()); |
+ |
+ if (sensors_[0]->SensorReadingUpdated()) { |
+ DCHECK_EQ(device::mojom::SensorType::RELATIVE_ORIENTATION, |
+ sensors_[0]->type); |
+ ComputeDeviceOrientationFromQuaternion( |
+ sensors_[0]->reading.values[0].value(), |
+ sensors_[0]->reading.values[1].value(), |
+ sensors_[0]->reading.values[2].value(), |
+ sensors_[0]->reading.values[3].value(), &data->alpha, &data->beta, |
+ &data->gamma); |
+ data->has_alpha = true; |
+ data->has_beta = true; |
+ data->has_gamma = true; |
+ data->absolute = false; |
+ return true; |
+ } else if (sensors_[1]->SensorReadingUpdated()) { |
+ DCHECK_EQ(device::mojom::SensorType::ABSOLUTE_ORIENTATION, |
+ sensors_[1]->type); |
+ ComputeDeviceOrientationFromQuaternion( |
+ sensors_[1]->reading.values[0].value(), |
+ sensors_[1]->reading.values[1].value(), |
+ sensors_[1]->reading.values[2].value(), |
+ sensors_[1]->reading.values[3].value(), &data->alpha, &data->beta, |
+ &data->gamma); |
+ data->has_alpha = true; |
+ data->has_beta = true; |
+ data->has_gamma = true; |
+ data->absolute = true; |
+ return true; |
+ } else if (sensors_[2]->SensorReadingUpdated() && |
+ sensors_[3]->SensorReadingUpdated()) { |
+ DCHECK_EQ(device::mojom::SensorType::ACCELEROMETER, sensors_[2]->type); |
+ DCHECK_EQ(device::mojom::SensorType::MAGNETOMETER, sensors_[3]->type); |
+ std::vector<double> R; |
+ if (!ComputeDeviceOrientationFromGravityAndGeomagnetic( |
+ sensors_[2]->reading.values[0].value(), |
+ sensors_[2]->reading.values[1].value(), |
+ sensors_[2]->reading.values[2].value(), |
+ sensors_[3]->reading.values[0].value(), |
+ sensors_[3]->reading.values[1].value(), |
+ sensors_[3]->reading.values[2].value(), &data->alpha, &data->beta, |
+ &data->gamma)) { |
+ return false; |
+ } |
+ data->has_alpha = true; |
+ data->has_beta = true; |
+ data->has_gamma = true; |
+ data->absolute = true; |
+ return true; |
+ } |
- listener()->DidChangeDeviceOrientation(data); |
+ return false; |
} |
} // namespace content |