Commit f50cb6e5 authored by Harvey Yang's avatar Harvey Yang Committed by Commit Bot

generic_sensor: Extract Functions into PlatformSensorProviderLinuxBase

As Linux and ChromeOS shares the same algorithms for the fusion sensors,
this commit extracts IsFusionSensorType and CreateFusionSensor into a
base class PlatformSensorProviderLinuxBase.

BUG=chromium:1116940
TEST=unit tests

Change-Id: I074b2be07a3ccb2a7ad220cb7fafcbb11faa67ad
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2500391Reviewed-by: default avatarRaphael Kubo da Costa <raphael.kubo.da.costa@intel.com>
Commit-Queue: Cheng-Hao Yang <chenghaoyang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821296}
parent 37be332c
...@@ -105,6 +105,8 @@ source_set("generic_sensor") { ...@@ -105,6 +105,8 @@ source_set("generic_sensor") {
"linux/sensor_device_manager.h", "linux/sensor_device_manager.h",
"platform_sensor_provider_linux.cc", "platform_sensor_provider_linux.cc",
"platform_sensor_provider_linux.h", "platform_sensor_provider_linux.h",
"platform_sensor_provider_linux_base.cc",
"platform_sensor_provider_linux_base.h",
] ]
} }
} }
......
...@@ -12,15 +12,9 @@ ...@@ -12,15 +12,9 @@
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "base/task/thread_pool.h" #include "base/task/thread_pool.h"
#include "base/task_runner_util.h" #include "base/task_runner_util.h"
#include "services/device/generic_sensor/absolute_orientation_euler_angles_fusion_algorithm_using_accelerometer_and_magnetometer.h"
#include "services/device/generic_sensor/linear_acceleration_fusion_algorithm_using_accelerometer.h"
#include "services/device/generic_sensor/linux/sensor_data_linux.h" #include "services/device/generic_sensor/linux/sensor_data_linux.h"
#include "services/device/generic_sensor/orientation_quaternion_fusion_algorithm_using_euler_angles.h"
#include "services/device/generic_sensor/platform_sensor_fusion.h"
#include "services/device/generic_sensor/platform_sensor_linux.h" #include "services/device/generic_sensor/platform_sensor_linux.h"
#include "services/device/generic_sensor/platform_sensor_reader_linux.h" #include "services/device/generic_sensor/platform_sensor_reader_linux.h"
#include "services/device/generic_sensor/relative_orientation_euler_angles_fusion_algorithm_using_accelerometer.h"
#include "services/device/generic_sensor/relative_orientation_euler_angles_fusion_algorithm_using_accelerometer_and_gyroscope.h"
namespace device { namespace device {
namespace { namespace {
...@@ -29,18 +23,6 @@ constexpr base::TaskTraits kBlockingTaskRunnerTraits = { ...@@ -29,18 +23,6 @@ constexpr base::TaskTraits kBlockingTaskRunnerTraits = {
base::MayBlock(), base::TaskPriority::USER_VISIBLE, base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}; base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN};
bool IsFusionSensorType(mojom::SensorType type) {
switch (type) {
case mojom::SensorType::LINEAR_ACCELERATION:
case mojom::SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES:
case mojom::SensorType::ABSOLUTE_ORIENTATION_QUATERNION:
case mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES:
case mojom::SensorType::RELATIVE_ORIENTATION_QUATERNION:
return true;
default:
return false;
}
}
} // namespace } // namespace
PlatformSensorProviderLinux::PlatformSensorProviderLinux() PlatformSensorProviderLinux::PlatformSensorProviderLinux()
...@@ -94,10 +76,16 @@ void PlatformSensorProviderLinux::FreeResources() { ...@@ -94,10 +76,16 @@ void PlatformSensorProviderLinux::FreeResources() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
} }
bool PlatformSensorProviderLinux::IsSensorTypeAvailable(
mojom::SensorType type) const {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
return GetSensorDevice(type);
}
SensorInfoLinux* PlatformSensorProviderLinux::GetSensorDevice( SensorInfoLinux* PlatformSensorProviderLinux::GetSensorDevice(
mojom::SensorType type) { mojom::SensorType type) const {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
auto sensor = sensor_devices_by_type_.find(type); const auto sensor = sensor_devices_by_type_.find(type);
if (sensor == sensor_devices_by_type_.end()) if (sensor == sensor_devices_by_type_.end())
return nullptr; return nullptr;
return sensor->second.get(); return sensor->second.get();
...@@ -178,47 +166,4 @@ void PlatformSensorProviderLinux::OnDeviceRemoved( ...@@ -178,47 +166,4 @@ void PlatformSensorProviderLinux::OnDeviceRemoved(
} }
} }
void PlatformSensorProviderLinux::CreateFusionSensor(
mojom::SensorType type,
SensorReadingSharedBuffer* reading_buffer,
CreateSensorCallback callback) {
DCHECK(IsFusionSensorType(type));
std::unique_ptr<PlatformSensorFusionAlgorithm> fusion_algorithm;
switch (type) {
case mojom::SensorType::LINEAR_ACCELERATION:
fusion_algorithm = std::make_unique<
LinearAccelerationFusionAlgorithmUsingAccelerometer>();
break;
case mojom::SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES:
fusion_algorithm = std::make_unique<
AbsoluteOrientationEulerAnglesFusionAlgorithmUsingAccelerometerAndMagnetometer>();
break;
case mojom::SensorType::ABSOLUTE_ORIENTATION_QUATERNION:
fusion_algorithm = std::make_unique<
OrientationQuaternionFusionAlgorithmUsingEulerAngles>(
true /* absolute */);
break;
case mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES:
if (GetSensorDevice(mojom::SensorType::GYROSCOPE)) {
fusion_algorithm = std::make_unique<
RelativeOrientationEulerAnglesFusionAlgorithmUsingAccelerometerAndGyroscope>();
} else {
fusion_algorithm = std::make_unique<
RelativeOrientationEulerAnglesFusionAlgorithmUsingAccelerometer>();
}
break;
case mojom::SensorType::RELATIVE_ORIENTATION_QUATERNION:
fusion_algorithm = std::make_unique<
OrientationQuaternionFusionAlgorithmUsingEulerAngles>(
false /* absolute */);
break;
default:
NOTREACHED();
}
DCHECK(fusion_algorithm);
PlatformSensorFusion::Create(
reading_buffer, this, std::move(fusion_algorithm), std::move(callback));
}
} // namespace device } // namespace device
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef SERVICES_DEVICE_GENERIC_SENSOR_PUBLIC_PLATFORM_SENSOR_PROVIDER_LINUX_H_ #ifndef SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_PROVIDER_LINUX_H_
#define SERVICES_DEVICE_GENERIC_SENSOR_PUBLIC_PLATFORM_SENSOR_PROVIDER_LINUX_H_ #define SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_PROVIDER_LINUX_H_
#include "services/device/generic_sensor/platform_sensor_provider.h" #include "services/device/generic_sensor/platform_sensor_provider_linux_base.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
...@@ -15,7 +15,7 @@ namespace device { ...@@ -15,7 +15,7 @@ namespace device {
struct SensorInfoLinux; struct SensorInfoLinux;
class PlatformSensorProviderLinux : public PlatformSensorProvider, class PlatformSensorProviderLinux : public PlatformSensorProviderLinuxBase,
public SensorDeviceManager::Delegate { public SensorDeviceManager::Delegate {
public: public:
PlatformSensorProviderLinux(); PlatformSensorProviderLinux();
...@@ -26,11 +26,12 @@ class PlatformSensorProviderLinux : public PlatformSensorProvider, ...@@ -26,11 +26,12 @@ class PlatformSensorProviderLinux : public PlatformSensorProvider,
std::unique_ptr<SensorDeviceManager> sensor_device_manager); std::unique_ptr<SensorDeviceManager> sensor_device_manager);
protected: protected:
// PlatformSensorProviderLinuxBase overrides:
void CreateSensorInternal(mojom::SensorType type, void CreateSensorInternal(mojom::SensorType type,
SensorReadingSharedBuffer* reading_buffer, SensorReadingSharedBuffer* reading_buffer,
CreateSensorCallback callback) override; CreateSensorCallback callback) override;
void FreeResources() override; void FreeResources() override;
bool IsSensorTypeAvailable(mojom::SensorType type) const override;
private: private:
friend class PlatformSensorAndProviderLinuxTest; friend class PlatformSensorAndProviderLinuxTest;
...@@ -46,7 +47,7 @@ class PlatformSensorProviderLinux : public PlatformSensorProvider, ...@@ -46,7 +47,7 @@ class PlatformSensorProviderLinux : public PlatformSensorProvider,
// If a request cannot be processed immediately, returns nullptr and // If a request cannot be processed immediately, returns nullptr and
// all the requests stored in |requests_map_| are processed after // all the requests stored in |requests_map_| are processed after
// enumeration is ready. // enumeration is ready.
SensorInfoLinux* GetSensorDevice(mojom::SensorType type); SensorInfoLinux* GetSensorDevice(mojom::SensorType type) const;
// Processed stored requests in |request_map_|. // Processed stored requests in |request_map_|.
void ProcessStoredRequests(); void ProcessStoredRequests();
...@@ -55,17 +56,13 @@ class PlatformSensorProviderLinux : public PlatformSensorProvider, ...@@ -55,17 +56,13 @@ class PlatformSensorProviderLinux : public PlatformSensorProvider,
void CreateSensorAndNotify(mojom::SensorType type, void CreateSensorAndNotify(mojom::SensorType type,
SensorInfoLinux* sensor_device); SensorInfoLinux* sensor_device);
// SensorDeviceManager::Delegate implements: // SensorDeviceManager::Delegate overrides:
void OnSensorNodesEnumerated() override; void OnSensorNodesEnumerated() override;
void OnDeviceAdded(mojom::SensorType type, void OnDeviceAdded(mojom::SensorType type,
std::unique_ptr<SensorInfoLinux> sensor_device) override; std::unique_ptr<SensorInfoLinux> sensor_device) override;
void OnDeviceRemoved(mojom::SensorType type, void OnDeviceRemoved(mojom::SensorType type,
const std::string& device_node) override; const std::string& device_node) override;
void CreateFusionSensor(mojom::SensorType type,
SensorReadingSharedBuffer* reading_buffer,
CreateSensorCallback callback);
// Set to true when enumeration is ready. // Set to true when enumeration is ready.
bool sensor_nodes_enumerated_; bool sensor_nodes_enumerated_;
...@@ -91,4 +88,4 @@ class PlatformSensorProviderLinux : public PlatformSensorProvider, ...@@ -91,4 +88,4 @@ class PlatformSensorProviderLinux : public PlatformSensorProvider,
} // namespace device } // namespace device
#endif // SERVICES_DEVICE_GENERIC_SENSOR_PUBLIC_PLATFORM_SENSOR_PROVIDER_LINUX_H_ #endif // SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_PROVIDER_LINUX_H_
// Copyright 2020 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 "services/device/generic_sensor/platform_sensor_provider_linux_base.h"
#include <memory>
#include <utility>
#include "services/device/generic_sensor/absolute_orientation_euler_angles_fusion_algorithm_using_accelerometer_and_magnetometer.h"
#include "services/device/generic_sensor/linear_acceleration_fusion_algorithm_using_accelerometer.h"
#include "services/device/generic_sensor/orientation_quaternion_fusion_algorithm_using_euler_angles.h"
#include "services/device/generic_sensor/platform_sensor_fusion.h"
#include "services/device/generic_sensor/relative_orientation_euler_angles_fusion_algorithm_using_accelerometer.h"
#include "services/device/generic_sensor/relative_orientation_euler_angles_fusion_algorithm_using_accelerometer_and_gyroscope.h"
namespace device {
// static
bool PlatformSensorProviderLinuxBase::IsFusionSensorType(
mojom::SensorType type) {
switch (type) {
case mojom::SensorType::LINEAR_ACCELERATION:
case mojom::SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES:
case mojom::SensorType::ABSOLUTE_ORIENTATION_QUATERNION:
case mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES:
case mojom::SensorType::RELATIVE_ORIENTATION_QUATERNION:
return true;
default:
return false;
}
}
void PlatformSensorProviderLinuxBase::CreateFusionSensor(
mojom::SensorType type,
SensorReadingSharedBuffer* reading_buffer,
CreateSensorCallback callback) {
DCHECK(IsFusionSensorType(type));
std::unique_ptr<PlatformSensorFusionAlgorithm> fusion_algorithm;
switch (type) {
case mojom::SensorType::LINEAR_ACCELERATION:
fusion_algorithm = std::make_unique<
LinearAccelerationFusionAlgorithmUsingAccelerometer>();
break;
case mojom::SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES:
fusion_algorithm = std::make_unique<
AbsoluteOrientationEulerAnglesFusionAlgorithmUsingAccelerometerAndMagnetometer>();
break;
case mojom::SensorType::ABSOLUTE_ORIENTATION_QUATERNION:
fusion_algorithm = std::make_unique<
OrientationQuaternionFusionAlgorithmUsingEulerAngles>(
true /* absolute */);
break;
case mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES:
if (IsSensorTypeAvailable(mojom::SensorType::GYROSCOPE)) {
fusion_algorithm = std::make_unique<
RelativeOrientationEulerAnglesFusionAlgorithmUsingAccelerometerAndGyroscope>();
} else {
fusion_algorithm = std::make_unique<
RelativeOrientationEulerAnglesFusionAlgorithmUsingAccelerometer>();
}
break;
case mojom::SensorType::RELATIVE_ORIENTATION_QUATERNION:
fusion_algorithm = std::make_unique<
OrientationQuaternionFusionAlgorithmUsingEulerAngles>(
false /* absolute */);
break;
default:
NOTREACHED();
}
DCHECK(fusion_algorithm);
PlatformSensorFusion::Create(
reading_buffer, this, std::move(fusion_algorithm), std::move(callback));
}
} // namespace device
// Copyright 2020 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.
#ifndef SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_PROVIDER_LINUX_BASE_H_
#define SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_PROVIDER_LINUX_BASE_H_
#include "services/device/generic_sensor/platform_sensor_provider.h"
namespace device {
class PlatformSensorProviderLinuxBase : public PlatformSensorProvider {
protected:
static bool IsFusionSensorType(mojom::SensorType type);
void CreateFusionSensor(mojom::SensorType type,
SensorReadingSharedBuffer* reading_buffer,
CreateSensorCallback callback);
virtual bool IsSensorTypeAvailable(mojom::SensorType type) const = 0;
};
} // namespace device
#endif // SERVICES_DEVICE_GENERIC_SENSOR_PLATFORM_SENSOR_PROVIDER_LINUX_BASE_H_
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