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

Side by Side Diff: third_party/WebKit/Source/modules/sensor/SensorReadingUpdater.cpp

Issue 2551223003: [Sensors] Align sensor reading updates and 'onchange' notification with rAF. (Closed)
Patch Set: rebased Created 4 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 2016 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 "modules/sensor/SensorReadingUpdater.h"
6
7 #include "core/dom/Document.h"
8 #include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h"
9 #include "modules/sensor/SensorProxy.h"
10 #include "wtf/CurrentTime.h"
11
12 using device::mojom::blink::ReportingMode;
13
14 namespace blink {
15
16 SensorReadingUpdater::SensorReadingUpdater(SensorProxy* sensorProxy)
17 : m_sensorProxy(sensorProxy), m_hasPendingAnimationFrameTask(false) {}
18
19 void SensorReadingUpdater::enqueueAnimationFrameTask() {
20 if (m_hasPendingAnimationFrameTask)
21 return;
22
23 auto callback = WTF::bind(&SensorReadingUpdater::onAnimationFrame,
24 wrapWeakPersistent(this));
25 m_sensorProxy->document()->enqueueAnimationFrameTask(std::move(callback));
26 m_hasPendingAnimationFrameTask = true;
27 }
28
29 void SensorReadingUpdater::start() {
30 enqueueAnimationFrameTask();
31 }
32
33 void SensorReadingUpdater::onAnimationFrame() {
34 m_hasPendingAnimationFrameTask = false;
35 onAnimationFrameInternal();
36 }
37
38 DEFINE_TRACE(SensorReadingUpdater) {
39 visitor->trace(m_sensorProxy);
40 }
41
42 class SensorReadingUpdaterContinuous : public SensorReadingUpdater {
43 public:
44 explicit SensorReadingUpdaterContinuous(SensorProxy* sensorProxy)
45 : SensorReadingUpdater(sensorProxy) {}
46
47 DEFINE_INLINE_VIRTUAL_TRACE() { SensorReadingUpdater::trace(visitor); }
48
49 protected:
50 void onAnimationFrameInternal() override {
51 if (!m_sensorProxy->isActive())
52 return;
53
54 m_sensorProxy->updateSensorReading();
55 m_sensorProxy->notifySensorChanged(WTF::monotonicallyIncreasingTime());
56 enqueueAnimationFrameTask();
57 }
58 };
59
60 // New data is fetched from shared buffer only once after 'start()'
61 // call. Further, notification is send until every client is updated
62 // (i.e. until longest notification period elapses) rAF stops after that.
63 class SensorReadingUpdaterOnChange : public SensorReadingUpdater {
64 public:
65 explicit SensorReadingUpdaterOnChange(SensorProxy* sensorProxy)
66 : SensorReadingUpdater(sensorProxy),
67 m_newDataArrivedTime(0.0),
68 m_newDataArrived(false) {}
69
70 DEFINE_INLINE_VIRTUAL_TRACE() { SensorReadingUpdater::trace(visitor); }
71
72 void start() override {
73 m_newDataArrived = true;
74 SensorReadingUpdater::start();
75 }
76
77 protected:
78 void onAnimationFrameInternal() override {
79 if (!m_sensorProxy->isActive())
80 return;
81
82 double timestamp = WTF::monotonicallyIncreasingTime();
83
84 if (m_newDataArrived) {
85 m_newDataArrived = false;
86 m_sensorProxy->updateSensorReading();
87 m_newDataArrivedTime = timestamp;
88 }
89 m_sensorProxy->notifySensorChanged(timestamp);
90
91 DCHECK_GT(m_sensorProxy->frequenciesUsed().front(), 0.0);
92 double longestNotificationPeriod =
93 1 / m_sensorProxy->frequenciesUsed().front();
94
95 if (timestamp - m_newDataArrivedTime <= longestNotificationPeriod)
96 enqueueAnimationFrameTask();
97 }
98
99 private:
100 double m_newDataArrivedTime;
101 bool m_newDataArrived;
102 };
103
104 // static
105 SensorReadingUpdater* SensorReadingUpdater::create(SensorProxy* proxy,
106 ReportingMode mode) {
107 if (mode == ReportingMode::CONTINUOUS)
108 return new SensorReadingUpdaterContinuous(proxy);
109 return new SensorReadingUpdaterOnChange(proxy);
110 }
111
112 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698