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

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: Re-implemented, rAF covers all sensors now 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
11 namespace blink {
12
13 using ReportingMode = device::mojom::blink::ReportingMode;
Reilly Grant (use Gerrit) 2016/12/16 00:48:55 Move outside of the namespace blink block and chan
14
15 SensorReadingUpdater::SensorReadingUpdater(SensorProxy* sensorProxy)
16 : m_sensorProxy(sensorProxy), m_rafCallbackId(0) {}
17
18 void SensorReadingUpdater::start() {
19 if (!m_rafCallbackId)
20 m_rafCallbackId = m_sensorProxy->document()->requestAnimationFrame(this);
21 }
22
23 void SensorReadingUpdater::stop() {
24 if (m_rafCallbackId) { // Cancel pending request.
25 m_sensorProxy->document()->cancelAnimationFrame(m_rafCallbackId);
26 m_rafCallbackId = 0;
27 }
28 }
29
30 DEFINE_TRACE(SensorReadingUpdater) {
31 visitor->trace(m_sensorProxy);
32 FrameRequestCallback::trace(visitor);
33 }
34
35 class SensorReadingUpdaterContinuous : public SensorReadingUpdater {
36 public:
37 explicit SensorReadingUpdaterContinuous(SensorProxy* sensorProxy)
38 : SensorReadingUpdater(sensorProxy) {}
39
40 DEFINE_INLINE_VIRTUAL_TRACE() { SensorReadingUpdater::trace(visitor); }
41
42 protected:
43 void handleEvent(double highResTimeMs) override {
44 if (!m_sensorProxy->isActive())
45 return;
46
47 m_rafCallbackId = m_sensorProxy->document()->requestAnimationFrame(this);
48 m_sensorProxy->updateSensorReading();
49 m_sensorProxy->notifySensorChanged(highResTimeMs * 0.001);
50 }
51 };
52
53 // New data is fetched from shared buffer only once after 'start()'
54 // call. Further, notification is send until every client is updated
55 // (i.e. until longest notification period elapses) rAF stops after that.
56 class SensorReadingUpdaterOnChange : public SensorReadingUpdater {
57 public:
58 explicit SensorReadingUpdaterOnChange(SensorProxy* sensorProxy)
59 : SensorReadingUpdater(sensorProxy),
60 m_newDataArrivedTime(0.0),
61 m_newDataArrived(false) {}
62
63 DEFINE_INLINE_VIRTUAL_TRACE() { SensorReadingUpdater::trace(visitor); }
64
65 void start() override {
66 m_newDataArrived = true;
67 SensorReadingUpdater::start();
68 }
69
70 protected:
71 void handleEvent(double highResTimeMs) override {
72 if (!m_sensorProxy->isActive())
73 return;
74
75 double timestampSec = highResTimeMs * 0.001;
76
77 if (m_newDataArrived) {
78 m_newDataArrived = false;
79 m_sensorProxy->updateSensorReading();
80 m_newDataArrivedTime = timestampSec;
81 m_sensorProxy->notifySensorChanged(timestampSec);
Reilly Grant (use Gerrit) 2016/12/16 00:48:55 Should we be calling notifySensorChanged() twice h
82 }
83 m_sensorProxy->notifySensorChanged(timestampSec);
84
85 DCHECK_GT(m_sensorProxy->frequenciesUsed().front(), 0.0);
86 double longestNotificationPeriod =
87 1 / m_sensorProxy->frequenciesUsed().front();
88 bool requestNextFrame =
89 timestampSec - m_newDataArrivedTime <= longestNotificationPeriod;
90 m_rafCallbackId =
91 requestNextFrame
92 ? m_sensorProxy->document()->requestAnimationFrame(this)
93 : 0;
94 }
95
96 private:
97 double m_newDataArrivedTime;
98 bool m_newDataArrived;
99 };
100
101 // static
102 SensorReadingUpdater* SensorReadingUpdater::create(SensorProxy* proxy,
103 ReportingMode mode) {
104 if (mode == ReportingMode::CONTINUOUS)
105 return new SensorReadingUpdaterContinuous(proxy);
106 return new SensorReadingUpdaterOnChange(proxy);
107 }
108
109 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698