Commit 150650db authored by jdarpinian's avatar jdarpinian Committed by Commit bot

Increase device orientation event frequency to 60 Hz to match...

Increase device orientation event frequency to 60 Hz to match requestAnimationFrame rate on most hardware. Change constants to microseconds for increased precision.

BUG=413327

Review URL: https://codereview.chromium.org/604483003

Cr-Commit-Position: refs/heads/master@{#297332}
parent 4f801272
...@@ -207,7 +207,7 @@ DataFetcherSharedMemoryBase::GetType() const { ...@@ -207,7 +207,7 @@ DataFetcherSharedMemoryBase::GetType() const {
} }
base::TimeDelta DataFetcherSharedMemoryBase::GetInterval() const { base::TimeDelta DataFetcherSharedMemoryBase::GetInterval() const {
return base::TimeDelta::FromMilliseconds(kInertialSensorIntervalMillis); return base::TimeDelta::FromMicroseconds(kInertialSensorIntervalMicroseconds);
} }
base::SharedMemory* DataFetcherSharedMemoryBase::GetSharedMemory( base::SharedMemory* DataFetcherSharedMemoryBase::GetSharedMemory(
......
...@@ -67,7 +67,7 @@ class FakeDataFetcher : public DataFetcherSharedMemoryBase { ...@@ -67,7 +67,7 @@ class FakeDataFetcher : public DataFetcherSharedMemoryBase {
DeviceMotionHardwareBuffer* buffer = GetMotionBuffer(); DeviceMotionHardwareBuffer* buffer = GetMotionBuffer();
ASSERT_TRUE(buffer); ASSERT_TRUE(buffer);
buffer->seqlock.WriteBegin(); buffer->seqlock.WriteBegin();
buffer->data.interval = kInertialSensorIntervalMillis; buffer->data.interval = kInertialSensorIntervalMicroseconds / 1000.;
buffer->seqlock.WriteEnd(); buffer->seqlock.WriteEnd();
updated_motion_.Signal(); updated_motion_.Signal();
} }
...@@ -343,8 +343,8 @@ TEST(DataFetcherSharedMemoryBaseTest, DoesStartMotion) { ...@@ -343,8 +343,8 @@ TEST(DataFetcherSharedMemoryBaseTest, DoesStartMotion) {
EXPECT_TRUE(fake_data_fetcher.StartFetchingDeviceData(CONSUMER_TYPE_MOTION)); EXPECT_TRUE(fake_data_fetcher.StartFetchingDeviceData(CONSUMER_TYPE_MOTION));
fake_data_fetcher.WaitForStart(CONSUMER_TYPE_MOTION); fake_data_fetcher.WaitForStart(CONSUMER_TYPE_MOTION);
EXPECT_EQ(kInertialSensorIntervalMillis, EXPECT_EQ(kInertialSensorIntervalMicroseconds / 1000.,
fake_data_fetcher.GetMotionBuffer()->data.interval); fake_data_fetcher.GetMotionBuffer()->data.interval);
fake_data_fetcher.StopFetchingDeviceData(CONSUMER_TYPE_MOTION); fake_data_fetcher.StopFetchingDeviceData(CONSUMER_TYPE_MOTION);
fake_data_fetcher.WaitForStop(CONSUMER_TYPE_MOTION); fake_data_fetcher.WaitForStop(CONSUMER_TYPE_MOTION);
...@@ -388,8 +388,8 @@ TEST(DataFetcherSharedMemoryBaseTest, DoesPollMotion) { ...@@ -388,8 +388,8 @@ TEST(DataFetcherSharedMemoryBaseTest, DoesPollMotion) {
fake_data_fetcher.WaitForStart(CONSUMER_TYPE_MOTION); fake_data_fetcher.WaitForStart(CONSUMER_TYPE_MOTION);
fake_data_fetcher.WaitForUpdate(CONSUMER_TYPE_MOTION); fake_data_fetcher.WaitForUpdate(CONSUMER_TYPE_MOTION);
EXPECT_EQ(kInertialSensorIntervalMillis, EXPECT_EQ(kInertialSensorIntervalMicroseconds / 1000.,
fake_data_fetcher.GetMotionBuffer()->data.interval); fake_data_fetcher.GetMotionBuffer()->data.interval);
fake_data_fetcher.StopFetchingDeviceData(CONSUMER_TYPE_MOTION); fake_data_fetcher.StopFetchingDeviceData(CONSUMER_TYPE_MOTION);
fake_data_fetcher.WaitForStop(CONSUMER_TYPE_MOTION); fake_data_fetcher.WaitForStop(CONSUMER_TYPE_MOTION);
...@@ -452,8 +452,8 @@ TEST(DataFetcherSharedMemoryBaseTest, DoesPollMotionAndOrientation) { ...@@ -452,8 +452,8 @@ TEST(DataFetcherSharedMemoryBaseTest, DoesPollMotionAndOrientation) {
fake_data_fetcher.WaitForUpdate(CONSUMER_TYPE_MOTION); fake_data_fetcher.WaitForUpdate(CONSUMER_TYPE_MOTION);
EXPECT_EQ(1, fake_data_fetcher.GetOrientationBuffer()->data.alpha); EXPECT_EQ(1, fake_data_fetcher.GetOrientationBuffer()->data.alpha);
EXPECT_EQ(kInertialSensorIntervalMillis, EXPECT_EQ(kInertialSensorIntervalMicroseconds / 1000.,
fake_data_fetcher.GetMotionBuffer()->data.interval); fake_data_fetcher.GetMotionBuffer()->data.interval);
fake_data_fetcher.StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION); fake_data_fetcher.StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION);
fake_data_fetcher.StopFetchingDeviceData(CONSUMER_TYPE_MOTION); fake_data_fetcher.StopFetchingDeviceData(CONSUMER_TYPE_MOTION);
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef CONTENT_BROWSER_DEVICE_SENSORS_INERTIAL_SENSOR_CONSTS_H_ #ifndef CONTENT_BROWSER_DEVICE_SENSORS_INERTIAL_SENSOR_CONSTS_H_
#define CONTENT_BROWSER_DEVICE_SENSORS_INERTIAL_SENSOR_CONSTS_H_ #define CONTENT_BROWSER_DEVICE_SENSORS_INERTIAL_SENSOR_CONSTS_H_
#include "base/time/time.h"
namespace content { namespace content {
// Constants related to the Device {Motion|Orientation|Light} APIs. // Constants related to the Device {Motion|Orientation|Light} APIs.
...@@ -15,14 +17,19 @@ enum ConsumerType { ...@@ -15,14 +17,19 @@ enum ConsumerType {
CONSUMER_TYPE_LIGHT = 1 << 2, CONSUMER_TYPE_LIGHT = 1 << 2,
}; };
// Specifies the minimal interval between subsequent sensor data updates. // Specifies the sampling rate for sensor data updates.
// Note that when changing this value it is desirable to have an adequate // Note that when changing this value it is desirable to have an adequate
// matching value |DeviceSensorEventPump::kDefaultPumpDelayMillis| in // matching value |DeviceSensorEventPump::kDefaultPumpFrequencyHz| in
// content/renderer/device_sensors/device_sensor_event_pump.cc. // content/renderer/device_orientation/device_sensor_event_pump.cc.
const int kInertialSensorIntervalMillis = 50; const int kInertialSensorSamplingRateHz = 60;
// Corresponding |kDefaultLightPumpDelayMillis| is in const int kInertialSensorIntervalMicroseconds =
base::Time::kMicrosecondsPerSecond / kInertialSensorSamplingRateHz;
// Corresponding |kDefaultLightPumpFrequencyHz| is in
// content/renderer/device_sensors/device_light_event_pump.cc. // content/renderer/device_sensors/device_light_event_pump.cc.
const int kLightSensorIntervalMillis = 200; const int kLightSensorSamplingRateHz = 5;
const int kLightSensorIntervalMicroseconds =
base::Time::kMicrosecondsPerSecond / kLightSensorSamplingRateHz;
} // namespace content } // namespace content
......
...@@ -150,14 +150,14 @@ void SensorManagerAndroid::GotLight(JNIEnv*, jobject, double value) { ...@@ -150,14 +150,14 @@ void SensorManagerAndroid::GotLight(JNIEnv*, jobject, double value) {
bool SensorManagerAndroid::Start(EventType event_type) { bool SensorManagerAndroid::Start(EventType event_type) {
DCHECK(!device_sensors_.is_null()); DCHECK(!device_sensors_.is_null());
int rate_in_milliseconds = (event_type == kTypeLight) int rate_in_microseconds = (event_type == kTypeLight)
? kLightSensorIntervalMillis ? kLightSensorIntervalMicroseconds
: kInertialSensorIntervalMillis; : kInertialSensorIntervalMicroseconds;
return Java_DeviceSensors_start(AttachCurrentThread(), return Java_DeviceSensors_start(AttachCurrentThread(),
device_sensors_.obj(), device_sensors_.obj(),
reinterpret_cast<intptr_t>(this), reinterpret_cast<intptr_t>(this),
static_cast<jint>(event_type), static_cast<jint>(event_type),
rate_in_milliseconds); rate_in_microseconds);
} }
void SensorManagerAndroid::Stop(EventType event_type) { void SensorManagerAndroid::Stop(EventType event_type) {
...@@ -250,7 +250,8 @@ void SensorManagerAndroid::CheckMotionBufferReadyToRead() { ...@@ -250,7 +250,8 @@ void SensorManagerAndroid::CheckMotionBufferReadyToRead() {
received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] == received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] ==
number_active_device_motion_sensors_) { number_active_device_motion_sensors_) {
device_motion_buffer_->seqlock.WriteBegin(); device_motion_buffer_->seqlock.WriteBegin();
device_motion_buffer_->data.interval = kInertialSensorIntervalMillis; device_motion_buffer_->data.interval =
kInertialSensorIntervalMicroseconds / 1000.;
device_motion_buffer_->seqlock.WriteEnd(); device_motion_buffer_->seqlock.WriteEnd();
SetMotionBufferReadyStatus(true); SetMotionBufferReadyStatus(true);
......
...@@ -86,7 +86,8 @@ TEST_F(AndroidSensorManagerTest, ThreeDeviceMotionSensorsActive) { ...@@ -86,7 +86,8 @@ TEST_F(AndroidSensorManagerTest, ThreeDeviceMotionSensorsActive) {
ASSERT_TRUE(motion_buffer_->data.hasRotationRateBeta); ASSERT_TRUE(motion_buffer_->data.hasRotationRateBeta);
ASSERT_EQ(9, motion_buffer_->data.rotationRateGamma); ASSERT_EQ(9, motion_buffer_->data.rotationRateGamma);
ASSERT_TRUE(motion_buffer_->data.hasRotationRateGamma); ASSERT_TRUE(motion_buffer_->data.hasRotationRateGamma);
ASSERT_EQ(kInertialSensorIntervalMillis, motion_buffer_->data.interval); ASSERT_EQ(kInertialSensorIntervalMicroseconds / 1000.,
motion_buffer_->data.interval);
sensorManager.StopFetchingDeviceMotionData(); sensorManager.StopFetchingDeviceMotionData();
ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive); ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive);
...@@ -105,7 +106,8 @@ TEST_F(AndroidSensorManagerTest, TwoDeviceMotionSensorsActive) { ...@@ -105,7 +106,8 @@ TEST_F(AndroidSensorManagerTest, TwoDeviceMotionSensorsActive) {
sensorManager.GotAccelerationIncludingGravity(0, 0, 1, 2, 3); sensorManager.GotAccelerationIncludingGravity(0, 0, 1, 2, 3);
ASSERT_TRUE(motion_buffer_->data.allAvailableSensorsAreActive); ASSERT_TRUE(motion_buffer_->data.allAvailableSensorsAreActive);
ASSERT_EQ(kInertialSensorIntervalMillis, motion_buffer_->data.interval); ASSERT_EQ(kInertialSensorIntervalMicroseconds / 1000.,
motion_buffer_->data.interval);
sensorManager.StopFetchingDeviceMotionData(); sensorManager.StopFetchingDeviceMotionData();
ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive); ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive);
...@@ -118,7 +120,8 @@ TEST_F(AndroidSensorManagerTest, ZeroDeviceMotionSensorsActive) { ...@@ -118,7 +120,8 @@ TEST_F(AndroidSensorManagerTest, ZeroDeviceMotionSensorsActive) {
sensorManager.StartFetchingDeviceMotionData(motion_buffer_.get()); sensorManager.StartFetchingDeviceMotionData(motion_buffer_.get());
ASSERT_TRUE(motion_buffer_->data.allAvailableSensorsAreActive); ASSERT_TRUE(motion_buffer_->data.allAvailableSensorsAreActive);
ASSERT_EQ(kInertialSensorIntervalMillis, motion_buffer_->data.interval); ASSERT_EQ(kInertialSensorIntervalMicroseconds / 1000.,
motion_buffer_->data.interval);
sensorManager.StopFetchingDeviceMotionData(); sensorManager.StopFetchingDeviceMotionData();
ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive); ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive);
......
...@@ -92,26 +92,27 @@ class DeviceSensors implements SensorEventListener { ...@@ -92,26 +92,27 @@ class DeviceSensors implements SensorEventListener {
* for events, the old callback is unregistered first. * for events, the old callback is unregistered first.
* *
* @param nativePtr Value to pass to nativeGotOrientation() for each event. * @param nativePtr Value to pass to nativeGotOrientation() for each event.
* @param rateInMilliseconds Requested callback rate in milliseconds. The * @param rateInMicroseconds Requested callback rate in microseconds. The
* actual rate may be higher. Unwanted events should be ignored. * actual rate may be higher. Unwanted events should be ignored.
* @param eventType Type of event to listen to, can be either DEVICE_ORIENTATION or * @param eventType Type of event to listen to, can be either DEVICE_ORIENTATION or
* DEVICE_MOTION or DEVICE_LIGHT. * DEVICE_MOTION or DEVICE_LIGHT.
* @return True on success. * @return True on success.
*/ */
@CalledByNative @CalledByNative
public boolean start(long nativePtr, int eventType, int rateInMilliseconds) { public boolean start(long nativePtr, int eventType, int rateInMicroseconds) {
boolean success = false; boolean success = false;
synchronized (mNativePtrLock) { synchronized (mNativePtrLock) {
switch (eventType) { switch (eventType) {
case DEVICE_ORIENTATION: case DEVICE_ORIENTATION:
success = registerSensors(DEVICE_ORIENTATION_SENSORS, rateInMilliseconds, true); success = registerSensors(DEVICE_ORIENTATION_SENSORS, rateInMicroseconds,
true);
break; break;
case DEVICE_MOTION: case DEVICE_MOTION:
// note: device motion spec does not require all sensors to be available // note: device motion spec does not require all sensors to be available
success = registerSensors(DEVICE_MOTION_SENSORS, rateInMilliseconds, false); success = registerSensors(DEVICE_MOTION_SENSORS, rateInMicroseconds, false);
break; break;
case DEVICE_LIGHT: case DEVICE_LIGHT:
success = registerSensors(DEVICE_LIGHT_SENSORS, rateInMilliseconds, true); success = registerSensors(DEVICE_LIGHT_SENSORS, rateInMicroseconds, true);
break; break;
default: default:
Log.e(TAG, "Unknown event type: " + eventType); Log.e(TAG, "Unknown event type: " + eventType);
...@@ -376,19 +377,19 @@ class DeviceSensors implements SensorEventListener { ...@@ -376,19 +377,19 @@ class DeviceSensors implements SensorEventListener {
/** /**
* @param sensorTypes List of sensors to activate. * @param sensorTypes List of sensors to activate.
* @param rateInMilliseconds Intended delay (in milliseconds) between sensor readings. * @param rateInMicroseconds Intended delay (in microseconds) between sensor readings.
* @param failOnMissingSensor If true the method returns true only if all sensors could be * @param failOnMissingSensor If true the method returns true only if all sensors could be
* activated. When false the method return true if at least one * activated. When false the method return true if at least one
* sensor in sensorTypes could be activated. * sensor in sensorTypes could be activated.
*/ */
private boolean registerSensors(Set<Integer> sensorTypes, int rateInMilliseconds, private boolean registerSensors(Set<Integer> sensorTypes, int rateInMicroseconds,
boolean failOnMissingSensor) { boolean failOnMissingSensor) {
Set<Integer> sensorsToActivate = new HashSet<Integer>(sensorTypes); Set<Integer> sensorsToActivate = new HashSet<Integer>(sensorTypes);
sensorsToActivate.removeAll(mActiveSensors); sensorsToActivate.removeAll(mActiveSensors);
boolean success = false; boolean success = false;
for (Integer sensorType : sensorsToActivate) { for (Integer sensorType : sensorsToActivate) {
boolean result = registerForSensorType(sensorType, rateInMilliseconds); boolean result = registerForSensorType(sensorType, rateInMicroseconds);
if (!result && failOnMissingSensor) { if (!result && failOnMissingSensor) {
// restore the previous state upon failure // restore the previous state upon failure
unregisterSensors(sensorsToActivate); unregisterSensors(sensorsToActivate);
...@@ -411,12 +412,11 @@ class DeviceSensors implements SensorEventListener { ...@@ -411,12 +412,11 @@ class DeviceSensors implements SensorEventListener {
} }
} }
private boolean registerForSensorType(int type, int rateInMilliseconds) { private boolean registerForSensorType(int type, int rateInMicroseconds) {
SensorManagerProxy sensorManager = getSensorManagerProxy(); SensorManagerProxy sensorManager = getSensorManagerProxy();
if (sensorManager == null) { if (sensorManager == null) {
return false; return false;
} }
final int rateInMicroseconds = 1000 * rateInMilliseconds;
return sensorManager.registerListener(this, type, rateInMicroseconds, getHandler()); return sensorManager.registerListener(this, type, rateInMicroseconds, getHandler());
} }
......
...@@ -4,13 +4,16 @@ ...@@ -4,13 +4,16 @@
#include "content/renderer/device_sensors/device_light_event_pump.h" #include "content/renderer/device_sensors/device_light_event_pump.h"
#include "base/time/time.h"
#include "content/common/device_sensors/device_light_messages.h" #include "content/common/device_sensors/device_light_messages.h"
#include "content/public/renderer/render_thread.h" #include "content/public/renderer/render_thread.h"
#include "third_party/WebKit/public/platform/WebDeviceLightListener.h" #include "third_party/WebKit/public/platform/WebDeviceLightListener.h"
namespace { namespace {
// Default delay between subsequent firing of DeviceLight events. // Default rate for firing of DeviceLight events.
const int kDefaultLightPumpDelayMillis = 200; const int kDefaultLightPumpFrequencyHz = 5;
const int kDefaultLightPumpDelayMicroseconds =
base::Time::kMicrosecondsPerSecond / kDefaultLightPumpFrequencyHz;
} // namespace } // namespace
namespace content { namespace content {
...@@ -18,7 +21,7 @@ namespace content { ...@@ -18,7 +21,7 @@ namespace content {
DeviceLightEventPump::DeviceLightEventPump(RenderThread* thread) DeviceLightEventPump::DeviceLightEventPump(RenderThread* thread)
: DeviceSensorEventPump<blink::WebDeviceLightListener>(thread), : DeviceSensorEventPump<blink::WebDeviceLightListener>(thread),
last_seen_data_(-1) { last_seen_data_(-1) {
pump_delay_millis_ = kDefaultLightPumpDelayMillis; pump_delay_microseconds_ = kDefaultLightPumpDelayMicroseconds;
} }
DeviceLightEventPump::~DeviceLightEventPump() { DeviceLightEventPump::~DeviceLightEventPump() {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_ #define CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_
#include "base/memory/shared_memory.h" #include "base/memory/shared_memory.h"
#include "base/time/time.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "content/public/renderer/platform_event_observer.h" #include "content/public/renderer/platform_event_observer.h"
...@@ -15,8 +16,10 @@ template <typename ListenerType> ...@@ -15,8 +16,10 @@ template <typename ListenerType>
class CONTENT_EXPORT DeviceSensorEventPump class CONTENT_EXPORT DeviceSensorEventPump
: NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) { : NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) {
public: public:
// Default delay between subsequent firing of events. // Default rate for firing events.
static const int kDefaultPumpDelayMillis = 50; static const int kDefaultPumpFrequencyHz = 60;
static const int kDefaultPumpDelayMicroseconds =
base::Time::kMicrosecondsPerSecond / kDefaultPumpFrequencyHz;
// PlatformEventObserver // PlatformEventObserver
virtual void Start(blink::WebPlatformEventListener* listener) OVERRIDE { virtual void Start(blink::WebPlatformEventListener* listener) OVERRIDE {
...@@ -49,9 +52,8 @@ class CONTENT_EXPORT DeviceSensorEventPump ...@@ -49,9 +52,8 @@ class CONTENT_EXPORT DeviceSensorEventPump
protected: protected:
explicit DeviceSensorEventPump(RenderThread* thread) explicit DeviceSensorEventPump(RenderThread* thread)
: PlatformEventObserver<ListenerType>(thread), : PlatformEventObserver<ListenerType>(thread),
pump_delay_millis_(kDefaultPumpDelayMillis), pump_delay_microseconds_(kDefaultPumpDelayMicroseconds),
state_(STOPPED) { state_(STOPPED) {}
}
virtual ~DeviceSensorEventPump() { virtual ~DeviceSensorEventPump() {
} }
...@@ -77,8 +79,9 @@ class CONTENT_EXPORT DeviceSensorEventPump ...@@ -77,8 +79,9 @@ class CONTENT_EXPORT DeviceSensorEventPump
if (InitializeReader(handle)) { if (InitializeReader(handle)) {
timer_.Start(FROM_HERE, timer_.Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(pump_delay_millis_), base::TimeDelta::FromMicroseconds(pump_delay_microseconds_),
this, &DeviceSensorEventPump::FireEvent); this,
&DeviceSensorEventPump::FireEvent);
state_ = RUNNING; state_ = RUNNING;
} }
} }
...@@ -86,7 +89,7 @@ class CONTENT_EXPORT DeviceSensorEventPump ...@@ -86,7 +89,7 @@ class CONTENT_EXPORT DeviceSensorEventPump
virtual void FireEvent() = 0; virtual void FireEvent() = 0;
virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0; virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0;
int pump_delay_millis_; int pump_delay_microseconds_;
PumpState state_; PumpState state_;
base::RepeatingTimer<DeviceSensorEventPump> timer_; base::RepeatingTimer<DeviceSensorEventPump> timer_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment