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

Side by Side Diff: content/browser/device_orientation/data_fetcher_impl_android.h

Issue 18572014: Implement Android shared memory data fetcher for Device Motion. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@renderer-sync-12June-tryASYNC-2-bis-tryRebase-6
Patch Set: fixed some includes Created 7 years, 5 months 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ 5 #ifndef CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_
6 #define CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ 6 #define CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_
7 7
8 #include "base/android/scoped_java_ref.h" 8 #include "base/android/scoped_java_ref.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/synchronization/lock.h" 10 #include "base/synchronization/lock.h"
11 #include "content/browser/device_orientation/data_fetcher.h" 11 #include "content/browser/device_orientation/data_fetcher.h"
12 #include "content/browser/device_orientation/device_data.h" 12 #include "content/browser/device_orientation/device_data.h"
13 #include "content/common/device_motion_hardware_buffer.h"
13 14
14 namespace content { 15 namespace content {
15
16 class Orientation; 16 class Orientation;
17 17
18 // Android implementation of DeviceOrientation API. 18 // Android implementation of DeviceOrientation API.
19 19
20 // Android's SensorManager has a push API, whereas Chrome wants to pull data. 20 // Android's SensorManager has a push API, whereas Chrome wants to pull data.
21 // To fit them together, we store incoming sensor events in a 1-element buffer. 21 // To fit them together, we store incoming sensor events in a 1-element buffer.
22 // SensorManager calls SetOrientation() which pushes a new value (discarding the 22 // SensorManager calls SetOrientation() which pushes a new value (discarding the
23 // previous value if any). Chrome calls GetDeviceData() which reads the most 23 // previous value if any). Chrome calls GetDeviceData() which reads the most
24 // recent value. Repeated calls to GetDeviceData() will return the same value. 24 // recent value. Repeated calls to GetDeviceData() will return the same value.
25 25
26 // TODO(timvolodine): Move this class implementation to
27 // data_fetcher_shared_memory_android.cc, once Device Orientation switches to
28 // shared memory implementation.
26 class DataFetcherImplAndroid : public DataFetcher { 29 class DataFetcherImplAndroid : public DataFetcher {
27 public: 30 public:
28 // Must be called at startup, before Create(). 31 // Must be called at startup, before Create().
29 static void Init(JNIEnv* env); 32 static void Register(JNIEnv* env);
33
34 static DataFetcherImplAndroid* instance();
30 35
31 // Factory function. We'll listen for events for the lifetime of this object. 36 // Factory function. We'll listen for events for the lifetime of this object.
32 // Returns NULL on error. 37 // Returns NULL on error.
33 static DataFetcher* Create(); 38 static DataFetcher* Create();
34 39
35 virtual ~DataFetcherImplAndroid(); 40 virtual ~DataFetcherImplAndroid();
36 41
37 // Called from Java via JNI. 42 // Called from Java via JNI.
38 void GotOrientation(JNIEnv*, jobject, 43 void GotOrientation(JNIEnv*, jobject,
39 double alpha, double beta, double gamma); 44 double alpha, double beta, double gamma);
40 void GotAcceleration(JNIEnv*, jobject, 45 void GotAcceleration(JNIEnv*, jobject,
41 double x, double y, double z); 46 double x, double y, double z);
42 void GotAccelerationIncludingGravity(JNIEnv*, jobject, 47 void GotAccelerationIncludingGravity(JNIEnv*, jobject,
43 double x, double y, double z); 48 double x, double y, double z);
44 void GotRotationRate(JNIEnv*, jobject, 49 void GotRotationRate(JNIEnv*, jobject,
45 double alpha, double beta, double gamma); 50 double alpha, double beta, double gamma);
46 51
47 // Implementation of DataFetcher. 52 // Implementation of DataFetcher.
48 virtual const DeviceData* GetDeviceData(DeviceData::Type type) OVERRIDE; 53 virtual const DeviceData* GetDeviceData(DeviceData::Type type) OVERRIDE;
49 54
50 private: 55 // Implementation of the new API using shared memory.
bulach 2013/07/09 09:03:29 nit: just the name the base class, "new" will get
timvolodine 2013/07/09 18:59:56 Done.
56
57 virtual bool NeedsPolling() { return false; }
bulach 2013/07/09 09:03:29 nit: better move the implementation to the .cc fil
timvolodine 2013/07/09 18:59:56 Done.
58 virtual bool FetchDeviceMotionDataIntoBuffer();
59 virtual bool StartFetchingDeviceMotionData(
60 DeviceMotionHardwareBuffer* buffer);
61 virtual void StopFetchingDeviceMotionData();
62
63 protected:
51 DataFetcherImplAndroid(); 64 DataFetcherImplAndroid();
52 const Orientation* GetOrientation(); 65 const Orientation* GetOrientation();
53 66
54 // Wrappers for JNI methods. 67 // Wrappers for JNI methods.
55 // TODO(timvolodine): move the DeviceData::Type enum to the service class 68 // TODO(timvolodine): move the DeviceData::Type enum to the service class
56 // once it is implemented. 69 // once it is implemented.
57 bool Start(DeviceData::Type event_type, int rate_in_milliseconds); 70 virtual bool Start(DeviceData::Type event_type, int rate_in_milliseconds);
58 void Stop(DeviceData::Type event_type); 71 virtual void Stop(DeviceData::Type event_type);
59 72
60 virtual int GetNumberActiveDeviceMotionSensors(); 73 virtual int GetNumberActiveDeviceMotionSensors();
74 void CheckBufferReadyToRead();
75 void SetBufferReadyStatus(bool ready);
61 76
77 private:
78 enum {
79 DATA_ACCELERATION = 0,
bulach 2013/07/09 09:03:29 nit: I guess this would be clearer as RECEIVED_MOT
timvolodine 2013/07/09 18:59:56 Done.
80 DATA_ACCELERATION_INCLUDING_GRAVITY = 1,
81 DATA_ROTATION_RATE = 2,
82 DATA_MAX = 3,
83 };
62 // Value returned by GetDeviceData. 84 // Value returned by GetDeviceData.
63 scoped_refptr<Orientation> current_orientation_; 85 scoped_refptr<Orientation> current_orientation_;
64 86
65 // 1-element buffer, written by GotOrientation, read by GetDeviceData. 87 // 1-element buffer, written by GotOrientation, read by GetDeviceData.
66 base::Lock next_orientation_lock_; 88 base::Lock next_orientation_lock_;
67 scoped_refptr<Orientation> next_orientation_; 89 scoped_refptr<Orientation> next_orientation_;
68 90
69 // The Java provider of orientation info. 91 // The Java provider of orientation info.
70 base::android::ScopedJavaGlobalRef<jobject> device_orientation_; 92 base::android::ScopedJavaGlobalRef<jobject> device_orientation_;
93 int number_active_device_motion_sensors_;
94 int received_motion_data_[DATA_MAX];
95 DeviceMotionHardwareBuffer* device_motion_buffer_;
96 bool is_buffer_ready_;
71 97
72 DISALLOW_COPY_AND_ASSIGN(DataFetcherImplAndroid); 98 DISALLOW_COPY_AND_ASSIGN(DataFetcherImplAndroid);
73 }; 99 };
74 100
75 } // namespace content 101 } // namespace content
76 102
77 #endif // CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_ 103 #endif // CHROME_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_IMPL_ANDROID_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698