1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef VENDOR_GOOGLE_CAMERA_SENSOR_LISTENER_GOOG_SENSOR_SYNC_H_
18 #define VENDOR_GOOGLE_CAMERA_SENSOR_LISTENER_GOOG_SENSOR_SYNC_H_
19 
20 #include "goog_sensor_wrapper.h"
21 
22 namespace android {
23 namespace camera_sensor_listener {
24 
25 // Vsync sensor listener class.
26 // It will create a Vsync listener for specific physical camera to receive
27 // Vsync timestamps.
28 // Sample usage:
29 //   sp<GoogSensorSync> sync_ptr = GoogSensorSync::Create(0);
30 //   if (sync_ptr->GetSensorenablingStatus()) {
31 //     int64_t sof_vsync = sync_ptr->SyncTimestamp(sof_boottime);
32 //   }
33 class GoogSensorSync : public GoogSensorWrapper {
34  public:
35   // Return a StrongPointer pointing to newly created GoogSensorSync instance.
36   // Input:
37   //   cam_id: physical camera id associated with Vsync sensor.
38   //   event_queue_size: size of event queue to hold incoming Vsync events.
39   static sp<GoogSensorSync> Create(
40       uint8_t cam_id, size_t event_queue_size = kDefaultEventQueueSize);
41 
42   // Destructor.
43   // Destroy and free the resources of a GoogSensorSync.
44   ~GoogSensorSync();
45 
46   // Get whether sensor is enabled.
47   // Return true if sensor is enabled, false otherwise.
GetSensorEnablingStatus()48   bool GetSensorEnablingStatus() const {
49     return IsEnabled();
50   }
51 
52   // Get latest n vsync timestamps, boottime_timestamps, frame_ids and
53   // their arrival times.
54   // If output vectors are not empty, latest_n_vsync_timestamps,
55   // latest_n_frame_ids, latest_n_boottime_timestamps and
56   // latest_n_arrival_timestamps will be cleared first.
57   // If total samples in event_deque_ is smaller than num_sample,
58   // latest_n_vsync_timestamps->size(), latest_n_frame_ids->size(),
59   // latest_n_boottime_timestamps->size() and
60   // latest_n_arrival_timestamps->size() will be
61   // equal to event_deque_.size().
62   // Input:
63   //   num_sample: number of latest samples to query.
64   // Outputs:
65   //   latest_n_vsync_timestamps: pointer of vector to hold vsync timestamps.
66   //   latest_n_frame_ids: pointer of vector to hold frame ids.
67   //   latest_n_boottime_timestamps: pointer of vector to hold boottime
68   //     timestamps.
69   //   latest_n_arrival_timestamps: pointer of vector to hold arrival times.
70   //   First element of output vectors hold earliest samples.
71   void GetLatestNSamples(int num_sample,
72                          std::vector<int64_t>* latest_n_vsync_timestamps,
73                          std::vector<int64_t>* latest_n_frame_ids,
74                          std::vector<int64_t>* latest_n_boottime_timestamps,
75                          std::vector<int64_t>* latest_n_arrival_timestamps) const;
76 
77   // Synchronize timestamp with sensor_sync. Vsync must be enabled when this
78   // is called.
79   // Inputs:
80   //   timestamp: sof timestamp from clock_boottime to be synced.
81   // Return:
82   //   synced timestamp from Vsync sensor if successfully synced,
83   //   original input timestamp if unable to sync.
84   int64_t SyncTimestamp(int64_t timestamp);
85 
86   // Find event with nearing timestamp that is less than kMaxTimeDriftNs.
87   // Input:
88   //   timestamp: target boottime timestamp.
89   // Return:
90   //   ExtendedSensorEvent if found; std::nullopt if not found.
91   std::optional<ExtendedSensorEvent> FindNearestEvent(int64_t timestamp);
92 
93   // Utility function, extract sof boottime timestamp and frame id from vsync
94   // sensor event's data field.
95   // Input:
96   //   event: received vsync sensor event.
97   // Outputs:
98   //   frame_id: pointer to data field holding frame id.
99   //   timestamp_boottime: pointer to data field holding sof timestamp from
100   //   clock_boottime.
101   void ExtractFrameIdAndBoottimeTimestamp(const ExtendedSensorEvent& event,
102                                           int64_t* frame_id,
103                                           int64_t* timestamp_boottime) const;
104 
105   // For AoC sensor service, VSYNC sensor event payload only contains an event
106   // counter. The function only applies to slider, P21 or other devices with
107   // AoC sensor service.
108   // Input:
109   //   event: received vsync sensor event.
110   // Return:
111   //   event counter.
112   uint64_t ExtractAocEventCounter(const ExtendedSensorEvent& event);
113 
114   // Find corresponding vsync sof timestamp through exact matching with input
115   // boottime sof timestamp and frame id.
116   // Inputs:
117   //   timestamp: sof timestamp from clock_boottime to be matched.
118   //   frame_id: frame id that sof timestamp is associated with.
119   // Return:
120   //   matched sof timestamp from clock_vsync if both timestamp and frame_id are
121   //   the same with those carried by vsync events, otherwise return the input
122   //   timestamp.
123   int64_t MatchTimestamp(int64_t timestamp, int64_t frame_id);
124 
125  protected:
126   // Get Vsync sensor handle.
127   virtual int32_t GetSensorHandle() final;
128 
129  private:
130   // Constructor.
131   // Create and initialize a GoogSensorSync.
132   // Inputs:
133   //   cam_id: Id to identify different cameras (front/back, etc.).
134   //     Usually starts from 0, but need to check camera id definitions with
135   //     individual device.
136   //   event_queue_size: size of event queue to hold incoming Vsync events.
137   GoogSensorSync(uint8_t cam_id, size_t event_queue_size);
138 
139   // Default sensor event queue size is set to 5.
140   static constexpr size_t kDefaultEventQueueSize = 5;
141 
142   // Maximum tolerated delta between camera time and sensor time (in ns).
143   static constexpr int64_t kMaxTimeDriftNs = 10000000;
144 
145   // Threshold in logging failed SyncTimestamp.
146   static constexpr int32_t kFailureThreshold = 100;
147 
148   // Counter for number of SyncTimeStamp() called.
149   int32_t sync_cnt_ = 0;
150 
151   // Out of all SyncTimeStamp(), number of timestamps that failed to sync.
152   int32_t sync_failure_cnt_ = 0;
153 
154   int32_t match_cnt_ = 0;
155 
156   int32_t match_failure_cnt_ = 0;
157 
158   // The id of the camera linked to this vsync signal.
159   uint8_t cam_id_;
160 };
161 
162 }  // namespace camera_sensor_listener
163 }  // namespace android
164 
165 #endif  // VENDOR_GOOGLE_CAMERA_SENSOR_LISTENER_GOOG_SENSOR_SYNC_H_
166