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 {
}
base::TimeDelta DataFetcherSharedMemoryBase::GetInterval() const {
return base::TimeDelta::FromMilliseconds(kInertialSensorIntervalMillis);
return base::TimeDelta::FromMicroseconds(kInertialSensorIntervalMicroseconds);
}
base::SharedMemory* DataFetcherSharedMemoryBase::GetSharedMemory(
......
......@@ -67,7 +67,7 @@ class FakeDataFetcher : public DataFetcherSharedMemoryBase {
DeviceMotionHardwareBuffer* buffer = GetMotionBuffer();
ASSERT_TRUE(buffer);
buffer->seqlock.WriteBegin();
buffer->data.interval = kInertialSensorIntervalMillis;
buffer->data.interval = kInertialSensorIntervalMicroseconds / 1000.;
buffer->seqlock.WriteEnd();
updated_motion_.Signal();
}
......@@ -343,7 +343,7 @@ TEST(DataFetcherSharedMemoryBaseTest, DoesStartMotion) {
EXPECT_TRUE(fake_data_fetcher.StartFetchingDeviceData(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.StopFetchingDeviceData(CONSUMER_TYPE_MOTION);
......@@ -388,7 +388,7 @@ TEST(DataFetcherSharedMemoryBaseTest, DoesPollMotion) {
fake_data_fetcher.WaitForStart(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.StopFetchingDeviceData(CONSUMER_TYPE_MOTION);
......@@ -452,7 +452,7 @@ TEST(DataFetcherSharedMemoryBaseTest, DoesPollMotionAndOrientation) {
fake_data_fetcher.WaitForUpdate(CONSUMER_TYPE_MOTION);
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.StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION);
......
......@@ -5,6 +5,8 @@
#ifndef CONTENT_BROWSER_DEVICE_SENSORS_INERTIAL_SENSOR_CONSTS_H_
#define CONTENT_BROWSER_DEVICE_SENSORS_INERTIAL_SENSOR_CONSTS_H_
#include "base/time/time.h"
namespace content {
// Constants related to the Device {Motion|Orientation|Light} APIs.
......@@ -15,14 +17,19 @@ enum ConsumerType {
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
// matching value |DeviceSensorEventPump::kDefaultPumpDelayMillis| in
// content/renderer/device_sensors/device_sensor_event_pump.cc.
const int kInertialSensorIntervalMillis = 50;
// Corresponding |kDefaultLightPumpDelayMillis| is in
// matching value |DeviceSensorEventPump::kDefaultPumpFrequencyHz| in
// content/renderer/device_orientation/device_sensor_event_pump.cc.
const int kInertialSensorSamplingRateHz = 60;
const int kInertialSensorIntervalMicroseconds =
base::Time::kMicrosecondsPerSecond / kInertialSensorSamplingRateHz;
// Corresponding |kDefaultLightPumpFrequencyHz| is in
// 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
......
......@@ -150,14 +150,14 @@ void SensorManagerAndroid::GotLight(JNIEnv*, jobject, double value) {
bool SensorManagerAndroid::Start(EventType event_type) {
DCHECK(!device_sensors_.is_null());
int rate_in_milliseconds = (event_type == kTypeLight)
? kLightSensorIntervalMillis
: kInertialSensorIntervalMillis;
int rate_in_microseconds = (event_type == kTypeLight)
? kLightSensorIntervalMicroseconds
: kInertialSensorIntervalMicroseconds;
return Java_DeviceSensors_start(AttachCurrentThread(),
device_sensors_.obj(),
reinterpret_cast<intptr_t>(this),
static_cast<jint>(event_type),
rate_in_milliseconds);
rate_in_microseconds);
}
void SensorManagerAndroid::Stop(EventType event_type) {
......@@ -250,7 +250,8 @@ void SensorManagerAndroid::CheckMotionBufferReadyToRead() {
received_motion_data_[RECEIVED_MOTION_DATA_ROTATION_RATE] ==
number_active_device_motion_sensors_) {
device_motion_buffer_->seqlock.WriteBegin();
device_motion_buffer_->data.interval = kInertialSensorIntervalMillis;
device_motion_buffer_->data.interval =
kInertialSensorIntervalMicroseconds / 1000.;
device_motion_buffer_->seqlock.WriteEnd();
SetMotionBufferReadyStatus(true);
......
......@@ -86,7 +86,8 @@ TEST_F(AndroidSensorManagerTest, ThreeDeviceMotionSensorsActive) {
ASSERT_TRUE(motion_buffer_->data.hasRotationRateBeta);
ASSERT_EQ(9, motion_buffer_->data.rotationRateGamma);
ASSERT_TRUE(motion_buffer_->data.hasRotationRateGamma);
ASSERT_EQ(kInertialSensorIntervalMillis, motion_buffer_->data.interval);
ASSERT_EQ(kInertialSensorIntervalMicroseconds / 1000.,
motion_buffer_->data.interval);
sensorManager.StopFetchingDeviceMotionData();
ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive);
......@@ -105,7 +106,8 @@ TEST_F(AndroidSensorManagerTest, TwoDeviceMotionSensorsActive) {
sensorManager.GotAccelerationIncludingGravity(0, 0, 1, 2, 3);
ASSERT_TRUE(motion_buffer_->data.allAvailableSensorsAreActive);
ASSERT_EQ(kInertialSensorIntervalMillis, motion_buffer_->data.interval);
ASSERT_EQ(kInertialSensorIntervalMicroseconds / 1000.,
motion_buffer_->data.interval);
sensorManager.StopFetchingDeviceMotionData();
ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive);
......@@ -118,7 +120,8 @@ TEST_F(AndroidSensorManagerTest, ZeroDeviceMotionSensorsActive) {
sensorManager.StartFetchingDeviceMotionData(motion_buffer_.get());
ASSERT_TRUE(motion_buffer_->data.allAvailableSensorsAreActive);
ASSERT_EQ(kInertialSensorIntervalMillis, motion_buffer_->data.interval);
ASSERT_EQ(kInertialSensorIntervalMicroseconds / 1000.,
motion_buffer_->data.interval);
sensorManager.StopFetchingDeviceMotionData();
ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive);
......
......@@ -92,26 +92,27 @@ class DeviceSensors implements SensorEventListener {
* for events, the old callback is unregistered first.
*
* @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.
* @param eventType Type of event to listen to, can be either DEVICE_ORIENTATION or
* DEVICE_MOTION or DEVICE_LIGHT.
* @return True on success.
*/
@CalledByNative
public boolean start(long nativePtr, int eventType, int rateInMilliseconds) {
public boolean start(long nativePtr, int eventType, int rateInMicroseconds) {
boolean success = false;
synchronized (mNativePtrLock) {
switch (eventType) {
case DEVICE_ORIENTATION:
success = registerSensors(DEVICE_ORIENTATION_SENSORS, rateInMilliseconds, true);
success = registerSensors(DEVICE_ORIENTATION_SENSORS, rateInMicroseconds,
true);
break;
case DEVICE_MOTION:
// 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;
case DEVICE_LIGHT:
success = registerSensors(DEVICE_LIGHT_SENSORS, rateInMilliseconds, true);
success = registerSensors(DEVICE_LIGHT_SENSORS, rateInMicroseconds, true);
break;
default:
Log.e(TAG, "Unknown event type: " + eventType);
......@@ -376,19 +377,19 @@ class DeviceSensors implements SensorEventListener {
/**
* @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
* activated. When false the method return true if at least one
* sensor in sensorTypes could be activated.
*/
private boolean registerSensors(Set<Integer> sensorTypes, int rateInMilliseconds,
private boolean registerSensors(Set<Integer> sensorTypes, int rateInMicroseconds,
boolean failOnMissingSensor) {
Set<Integer> sensorsToActivate = new HashSet<Integer>(sensorTypes);
sensorsToActivate.removeAll(mActiveSensors);
boolean success = false;
for (Integer sensorType : sensorsToActivate) {
boolean result = registerForSensorType(sensorType, rateInMilliseconds);
boolean result = registerForSensorType(sensorType, rateInMicroseconds);
if (!result && failOnMissingSensor) {
// restore the previous state upon failure
unregisterSensors(sensorsToActivate);
......@@ -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();
if (sensorManager == null) {
return false;
}
final int rateInMicroseconds = 1000 * rateInMilliseconds;
return sensorManager.registerListener(this, type, rateInMicroseconds, getHandler());
}
......
......@@ -4,13 +4,16 @@
#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/public/renderer/render_thread.h"
#include "third_party/WebKit/public/platform/WebDeviceLightListener.h"
namespace {
// Default delay between subsequent firing of DeviceLight events.
const int kDefaultLightPumpDelayMillis = 200;
// Default rate for firing of DeviceLight events.
const int kDefaultLightPumpFrequencyHz = 5;
const int kDefaultLightPumpDelayMicroseconds =
base::Time::kMicrosecondsPerSecond / kDefaultLightPumpFrequencyHz;
} // namespace
namespace content {
......@@ -18,7 +21,7 @@ namespace content {
DeviceLightEventPump::DeviceLightEventPump(RenderThread* thread)
: DeviceSensorEventPump<blink::WebDeviceLightListener>(thread),
last_seen_data_(-1) {
pump_delay_millis_ = kDefaultLightPumpDelayMillis;
pump_delay_microseconds_ = kDefaultLightPumpDelayMicroseconds;
}
DeviceLightEventPump::~DeviceLightEventPump() {
......
......@@ -6,6 +6,7 @@
#define CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_SENSOR_EVENT_PUMP_H_
#include "base/memory/shared_memory.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "content/public/renderer/platform_event_observer.h"
......@@ -15,8 +16,10 @@ template <typename ListenerType>
class CONTENT_EXPORT DeviceSensorEventPump
: NON_EXPORTED_BASE(public PlatformEventObserver<ListenerType>) {
public:
// Default delay between subsequent firing of events.
static const int kDefaultPumpDelayMillis = 50;
// Default rate for firing events.
static const int kDefaultPumpFrequencyHz = 60;
static const int kDefaultPumpDelayMicroseconds =
base::Time::kMicrosecondsPerSecond / kDefaultPumpFrequencyHz;
// PlatformEventObserver
virtual void Start(blink::WebPlatformEventListener* listener) OVERRIDE {
......@@ -49,9 +52,8 @@ class CONTENT_EXPORT DeviceSensorEventPump
protected:
explicit DeviceSensorEventPump(RenderThread* thread)
: PlatformEventObserver<ListenerType>(thread),
pump_delay_millis_(kDefaultPumpDelayMillis),
state_(STOPPED) {
}
pump_delay_microseconds_(kDefaultPumpDelayMicroseconds),
state_(STOPPED) {}
virtual ~DeviceSensorEventPump() {
}
......@@ -77,8 +79,9 @@ class CONTENT_EXPORT DeviceSensorEventPump
if (InitializeReader(handle)) {
timer_.Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(pump_delay_millis_),
this, &DeviceSensorEventPump::FireEvent);
base::TimeDelta::FromMicroseconds(pump_delay_microseconds_),
this,
&DeviceSensorEventPump::FireEvent);
state_ = RUNNING;
}
}
......@@ -86,7 +89,7 @@ class CONTENT_EXPORT DeviceSensorEventPump
virtual void FireEvent() = 0;
virtual bool InitializeReader(base::SharedMemoryHandle handle) = 0;
int pump_delay_millis_;
int pump_delay_microseconds_;
PumpState state_;
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