OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_orientation_event_pump.h" | 5 #include "content/renderer/device_sensors/device_orientation_event_pump.h" |
6 | 6 |
7 #include <string.h> | 7 #include "base/memory/ptr_util.h" |
8 | |
9 #include <cmath> | |
10 | |
11 #include "content/public/renderer/render_thread.h" | 8 #include "content/public/renderer/render_thread.h" |
9 #include "content/renderer/device_sensors/device_orientation_util.h" | |
12 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic eOrientationListener.h" | 10 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic eOrientationListener.h" |
13 | 11 |
14 namespace content { | 12 namespace content { |
15 | 13 |
16 const double DeviceOrientationEventPumpBase::kOrientationThreshold = 0.1; | 14 DeviceOrientationEventPump::DeviceOrientationEventPump(RenderThread* thread) |
17 | |
18 DeviceOrientationEventPumpBase::DeviceOrientationEventPumpBase( | |
19 RenderThread* thread) | |
20 : DeviceSensorEventPump<blink::WebDeviceOrientationListener>(thread) {} | 15 : DeviceSensorEventPump<blink::WebDeviceOrientationListener>(thread) {} |
21 | 16 |
22 DeviceOrientationEventPumpBase::~DeviceOrientationEventPumpBase() {} | 17 DeviceOrientationEventPump::~DeviceOrientationEventPump() {} |
23 | 18 |
24 void DeviceOrientationEventPumpBase::FireEvent() { | 19 void DeviceOrientationEventPump::SendStartMessage() { |
20 sensors_.push_back(base::MakeUnique<SensorEntry>(this)); | |
21 sensors_.push_back(base::MakeUnique<SensorEntry>(this)); | |
22 sensors_.push_back(base::MakeUnique<SensorEntry>(this)); | |
23 sensors_.push_back(base::MakeUnique<SensorEntry>(this)); | |
24 | |
25 // For DeviceOrientation event we use a 3-way fallback approach where up to | |
26 // 3 different sets of sensors are attempted if necessary. The sensors to be | |
27 // used for orientation are determined in the following order: | |
28 // A: RELATIVE_ORIENTATION (relative) | |
29 // B: ABSOLUTE_ORIENTATION (absolute) | |
30 // C: combination of ACCELEROMETER and MAGNETOMETER (absolute) | |
31 GetSensor(device::mojom::SensorType::RELATIVE_ORIENTATION, sensors_[0].get()); | |
32 GetSensor(device::mojom::SensorType::ABSOLUTE_ORIENTATION, sensors_[1].get()); | |
33 GetSensor(device::mojom::SensorType::ACCELEROMETER, sensors_[2].get()); | |
34 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
| |
35 } | |
36 | |
37 void DeviceOrientationEventPump::SendFakeDataForTesting(void* fake_data) { | |
38 blink::WebDeviceOrientationData data = | |
39 *static_cast<blink::WebDeviceOrientationData*>(fake_data); | |
40 listener()->DidChangeDeviceOrientation(data); | |
41 } | |
42 | |
43 bool DeviceOrientationEventPump::CanStart() const { | |
44 DCHECK_EQ(4u, sensors_.size()); | |
45 return (sensors_[0]->active || sensors_[1]->active || | |
46 (sensors_[2]->active && sensors_[3]->active)); | |
47 } | |
48 | |
49 void DeviceOrientationEventPump::FireEvent() { | |
50 blink::WebDeviceOrientationData data; | |
51 | |
25 DCHECK(listener()); | 52 DCHECK(listener()); |
26 device::OrientationData data; | 53 |
27 if (reader_->GetLatestData(&data) && ShouldFireEvent(data)) { | 54 if (GetDataFromSharedMemory(&data) && IsSignificantlyDifferent(data_, data)) { |
28 memcpy(&data_, &data, sizeof(data)); | 55 data_ = data; |
29 listener()->DidChangeDeviceOrientation(data); | 56 listener()->DidChangeDeviceOrientation(data); |
30 } | 57 } |
31 } | 58 } |
32 | 59 |
33 static bool IsSignificantlyDifferent(bool hasAngle1, double angle1, | 60 bool DeviceOrientationEventPump::GetDataFromSharedMemory( |
34 bool hasAngle2, double angle2) { | 61 blink::WebDeviceOrientationData* data) { |
35 if (hasAngle1 != hasAngle2) | 62 DCHECK_EQ(4u, sensors_.size()); |
63 | |
64 if (sensors_[0]->SensorReadingUpdated()) { | |
65 DCHECK_EQ(device::mojom::SensorType::RELATIVE_ORIENTATION, | |
66 sensors_[0]->type); | |
67 ComputeDeviceOrientationFromQuaternion( | |
68 sensors_[0]->reading.values[0].value(), | |
69 sensors_[0]->reading.values[1].value(), | |
70 sensors_[0]->reading.values[2].value(), | |
71 sensors_[0]->reading.values[3].value(), &data->alpha, &data->beta, | |
72 &data->gamma); | |
73 data->has_alpha = true; | |
74 data->has_beta = true; | |
75 data->has_gamma = true; | |
76 data->absolute = false; | |
36 return true; | 77 return true; |
37 return (hasAngle1 && | 78 } else if (sensors_[1]->SensorReadingUpdated()) { |
38 std::fabs(angle1 - angle2) >= | 79 DCHECK_EQ(device::mojom::SensorType::ABSOLUTE_ORIENTATION, |
39 DeviceOrientationEventPumpBase::kOrientationThreshold); | 80 sensors_[1]->type); |
40 } | 81 ComputeDeviceOrientationFromQuaternion( |
41 | 82 sensors_[1]->reading.values[0].value(), |
42 bool DeviceOrientationEventPumpBase::ShouldFireEvent( | 83 sensors_[1]->reading.values[1].value(), |
43 const device::OrientationData& data) const { | 84 sensors_[1]->reading.values[2].value(), |
44 if (!data.all_available_sensors_are_active) | 85 sensors_[1]->reading.values[3].value(), &data->alpha, &data->beta, |
45 return false; | 86 &data->gamma); |
46 | 87 data->has_alpha = true; |
47 if (!data.has_alpha && !data.has_beta && !data.has_gamma) { | 88 data->has_beta = true; |
48 // no data can be provided, this is an all-null event. | 89 data->has_gamma = true; |
90 data->absolute = true; | |
91 return true; | |
92 } else if (sensors_[2]->SensorReadingUpdated() && | |
93 sensors_[3]->SensorReadingUpdated()) { | |
94 DCHECK_EQ(device::mojom::SensorType::ACCELEROMETER, sensors_[2]->type); | |
95 DCHECK_EQ(device::mojom::SensorType::MAGNETOMETER, sensors_[3]->type); | |
96 std::vector<double> R; | |
97 if (!ComputeDeviceOrientationFromGravityAndGeomagnetic( | |
98 sensors_[2]->reading.values[0].value(), | |
99 sensors_[2]->reading.values[1].value(), | |
100 sensors_[2]->reading.values[2].value(), | |
101 sensors_[3]->reading.values[0].value(), | |
102 sensors_[3]->reading.values[1].value(), | |
103 sensors_[3]->reading.values[2].value(), &data->alpha, &data->beta, | |
104 &data->gamma)) { | |
105 return false; | |
106 } | |
107 data->has_alpha = true; | |
108 data->has_beta = true; | |
109 data->has_gamma = true; | |
110 data->absolute = true; | |
49 return true; | 111 return true; |
50 } | 112 } |
51 | 113 |
52 return IsSignificantlyDifferent(data_.has_alpha, data_.alpha, data.has_alpha, | 114 return false; |
53 data.alpha) || | |
54 IsSignificantlyDifferent(data_.has_beta, data_.beta, data.has_beta, | |
55 data.beta) || | |
56 IsSignificantlyDifferent(data_.has_gamma, data_.gamma, data.has_gamma, | |
57 data.gamma); | |
58 } | |
59 | |
60 bool DeviceOrientationEventPumpBase::InitializeReader( | |
61 base::SharedMemoryHandle handle) { | |
62 memset(&data_, 0, sizeof(data_)); | |
63 if (!reader_) | |
64 reader_.reset(new DeviceOrientationSharedMemoryReader()); | |
65 return reader_->Initialize(handle); | |
66 } | |
67 | |
68 void DeviceOrientationEventPumpBase::SendFakeDataForTesting(void* fake_data) { | |
69 device::OrientationData data = | |
70 *static_cast<device::OrientationData*>(fake_data); | |
71 | |
72 listener()->DidChangeDeviceOrientation(data); | |
73 } | 115 } |
74 | 116 |
75 } // namespace content | 117 } // namespace content |
OLD | NEW |