Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: content/browser/device_sensors/sensor_manager_chromeos.cc

Issue 680383007: DeviceOrientation API on ChromeOS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reduce Singleton Usage Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/device_sensors/sensor_manager_chromeos.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/memory/singleton.h"
10 #include "content/public/browser/sensor_manager_delegate_chromeos.h"
11 #include "ui/gfx/geometry/vector3d_f.h"
12
13 namespace {
14 // Conversion ratio from radians to degrees.
15 const double kRad2deg = 180.0 / M_PI;
16 }
17
18 namespace content {
19
20 SensorManagerChromeOS* SensorManagerChromeOS::GetInstance() {
21 return Singleton<SensorManagerChromeOS,
22 LeakySingletonTraits<SensorManagerChromeOS>>::get();
23 }
24
25 void SensorManagerChromeOS::SetDelegate(
26 SensorManagerDelegateChromeOS* delegate) {
27 delegate_ = delegate;
28 }
29
30 bool SensorManagerChromeOS::StartFetchingDeviceOrientationData(
31 DeviceOrientationHardwareBuffer* buffer) {
32 DCHECK(buffer);
33 if (!delegate_)
34 return false;
35
36 {
37 // Lock must be released before providing the callback to prevent potential
38 // deadlock.
39 base::AutoLock autolock(orientation_buffer_lock_);
40 orientation_buffer_ = buffer;
41
42 // No compass information, so we cannot provide absolute orientation.
43 orientation_buffer_->seqlock.WriteBegin();
44 orientation_buffer_->data.absolute = false;
45 orientation_buffer_->data.hasAbsolute = true;
46 orientation_buffer_->seqlock.WriteEnd();
47 }
48
49 OrientationDataCallback callback = base::Bind(
50 &SensorManagerChromeOS::OnAccelerationIncludingGravity, callback_ref_);
51 delegate_->StartFetchingDeviceOrientationData(callback);
52
53 return true;
54 }
55
56 void SensorManagerChromeOS::StopFetchingDeviceOrientationData() {
57 if (!delegate_)
58 return;
59 delegate_->StopFetchingDeviceOrientationData();
60 base::AutoLock autolock(orientation_buffer_lock_);
61 if (!orientation_buffer_)
62 return;
63 // Null message to signify that sensors are no longer available
64 orientation_buffer_->seqlock.WriteBegin();
65 orientation_buffer_->data.allAvailableSensorsAreActive = false;
66 orientation_buffer_->seqlock.WriteEnd();
67 orientation_buffer_ = nullptr;
68 }
69
70 SensorManagerChromeOS::~SensorManagerChromeOS() {
71 }
72
73 SensorManagerChromeOS::SensorManagerChromeOS()
74 : orientation_buffer_(nullptr), callback_ref_(this), delegate_(nullptr) {
75 }
76
77 void SensorManagerChromeOS::OnAccelerationIncludingGravity(double x,
78 double y,
79 double z) {
80 base::AutoLock autolock(orientation_buffer_lock_);
81 if (!orientation_buffer_)
82 return;
83 // Create a unit vector for trigonometry
84 // TODO(jonross): Stop reversing signs for vector components once
85 // accelerometer values have been fixed. crbug.com/431391
86 // Ternaries are to remove -0.0f which gives incorrect trigonometrical
87 // results.
88 gfx::Vector3dF data(x, y ? -y : 0.0f, z ? -z : 0.0f);
89 data.Scale(1.0f / data.Length());
90
91 // Transform accelerometer to W3C angles, using the Z-X-Y Eulerangles matrix.
92 // x = sin(gamma)
93 // y = -cos(gamma) * sin(beta)
94 // z = cos(beta) * cos(gamma)
95 // With only accelerometer alpha cannot be provided.
96 double beta = kRad2deg * atan2(data.y(), data.z());
97 double gamma = kRad2deg * asin(data.x());
98
99 // Convert beta and gamma to fit the intervals in the specification. Beta is
100 // [-180, 180) and gamma is [-90, 90).
101 if (beta >= 180.0f)
102 beta = -180.0f;
103 if (gamma >= 90.0f)
104 gamma = -90.0f;
105 // TODO: move chromeos/accelerometer to /content/browser/device_sensors/ and
106 // have it generate the beta and gamma values. crbug.com/431865
107 orientation_buffer_->seqlock.WriteBegin();
108 orientation_buffer_->data.beta = beta;
109 orientation_buffer_->data.hasBeta = true;
110 orientation_buffer_->data.gamma = gamma;
111 orientation_buffer_->data.hasGamma = true;
112 orientation_buffer_->data.allAvailableSensorsAreActive = true;
113 orientation_buffer_->seqlock.WriteEnd();
114 }
115
116 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698