1 /* 2 * Copyright (C) 2019 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 HARDWARE_GOOGLE_CAMERA_HAL_UTILS_RESULT_DISPATCHER_H_ 18 #define HARDWARE_GOOGLE_CAMERA_HAL_UTILS_RESULT_DISPATCHER_H_ 19 20 #include <map> 21 #include <string> 22 #include <string_view> 23 #include <thread> 24 25 #include "hal_types.h" 26 27 namespace android { 28 namespace google_camera_hal { 29 30 // ResultDispatcher dispatches capture results in the order of frame numbers, 31 // including result metadata, shutters, and stream buffers. 32 // 33 // The client can add results and shutters via AddResult() (or AddBatchResult()) 34 // and AddShutter() in any order. ResultDispatcher will invoke 35 // ProcessCaptureResultFunc (or ProcessBatchCaptureResultFunc) and NotifyFunc to 36 // notify result metadata, shutters, and stream buffers in the in the order of 37 // increasing frame numbers. 38 class ResultDispatcher { 39 public: 40 // Create a ResultDispatcher. 41 // partial_result_count is the partial result count. 42 // process_capture_result is the callback to notify a capture result. 43 // process_batch_capture_result is the callback to notify multiple capture 44 // results at once. 45 // stream_config is the session stream configuration. 46 // notify is the function to notify shutter messages. 47 // If process_batch_capture_result is not null, it has the priority over 48 // process_capture_result. 49 static std::unique_ptr<ResultDispatcher> Create( 50 uint32_t partial_result_count, 51 ProcessCaptureResultFunc process_capture_result, 52 ProcessBatchCaptureResultFunc process_batch_capture_result, 53 NotifyFunc notify, const StreamConfiguration& stream_config, 54 std::string_view name = "ResultDispatcher"); 55 56 virtual ~ResultDispatcher(); 57 58 // Add a pending request. This tells ResultDispatcher to watch for 59 // the shutter, result metadata, and stream buffers for this request, 60 // that will be added later via AddResult() and AddShutter(). 61 status_t AddPendingRequest(const CaptureRequest& pending_request); 62 63 // Add a ready result. If the result doesn't belong to a pending request that 64 // was previously added via AddPendingRequest(), an error will be returned. 65 status_t AddResult(std::unique_ptr<CaptureResult> result); 66 67 // Add a batch of results which contains multiple ready results. 68 status_t AddBatchResult(std::vector<std::unique_ptr<CaptureResult>> results); 69 70 // Add a shutter for a frame number. If the frame number doesn't belong to a 71 // pending request that was previously added via AddPendingRequest(), an error 72 // will be returned. 73 status_t AddShutter(uint32_t frame_number, int64_t timestamp_ns, 74 int64_t readout_timestamp_ns); 75 76 // Add an error notification for a frame number. When this is called, we no 77 // longer wait for a shutter message or result metadata for the given frame. 78 status_t AddError(const ErrorMessage& error); 79 80 // Remove a pending request. 81 void RemovePendingRequest(uint32_t frame_number); 82 83 ResultDispatcher(uint32_t partial_result_count, 84 ProcessCaptureResultFunc process_capture_result, 85 ProcessBatchCaptureResultFunc process_batch_capture_result, 86 NotifyFunc notify, const StreamConfiguration& stream_config, 87 std::string_view name = "ResultDispatcher"); 88 89 private: 90 static constexpr uint32_t kCallbackThreadTimeoutMs = 500; 91 const uint32_t kPartialResultCount; 92 93 // Define the stream key types. Single stream type is for normal streams. 94 // Group stream type is for the group streams of multi-resolution streams. 95 enum class StreamKeyType : uint32_t { 96 kSingleStream = 0, 97 kGroupStream, 98 }; 99 100 // The key of the stream_pending_buffers_map_, which has different types. 101 // Type kSingleStream indicates the StreamKey represents a single stream, and 102 // the id will be the stream id. 103 // Type kGroupStream indicates the StreamKey represents a stream group, and 104 // the id will be the stream group id. All of the buffers of certain stream 105 // group will be tracked together, as there's only one buffer from the group 106 // streams should be returned each request. 107 typedef std::pair</*id=*/int32_t, StreamKeyType> StreamKey; 108 109 // Define a pending shutter that will be ready later when AddShutter() is 110 // called. 111 struct PendingShutter { 112 int64_t timestamp_ns = 0; 113 int64_t readout_timestamp_ns = 0; 114 bool ready = false; 115 }; 116 117 // Define a pending buffer that will be ready later when AddResult() is called. 118 struct PendingBuffer { 119 StreamBuffer buffer = {}; 120 bool is_input = false; 121 bool ready = false; 122 }; 123 124 // Define a pending final result metadata that will be ready later when 125 // AddResult() is called. 126 struct PendingFinalResultMetadata { 127 std::unique_ptr<HalCameraMetadata> metadata; 128 std::vector<PhysicalCameraMetadata> physical_metadata; 129 bool ready = false; 130 }; 131 132 // Add a pending request for a frame. Must be protected with result_lock_. 133 status_t AddPendingRequestLocked(const CaptureRequest& pending_request); 134 135 // Add a pending shutter for a frame. Must be protected with result_lock_. 136 status_t AddPendingShutterLocked(uint32_t frame_number); 137 138 // Add a pending final metadata for a frame. Must be protected with 139 // result_lock_. 140 status_t AddPendingFinalResultMetadataLocked(uint32_t frame_number); 141 142 // Add a pending buffer for a frame. Must be protected with result_lock_. 143 status_t AddPendingBufferLocked(uint32_t frame_number, 144 const StreamBuffer& buffer, bool is_input); 145 146 // Remove pending shutter, result metadata, and buffers for a frame number. 147 void RemovePendingRequestLocked(uint32_t frame_number); 148 149 // Add result metadata and buffers to the storage to send them from the notify 150 // callback thread. 151 status_t AddResultImpl(std::unique_ptr<CaptureResult> result); 152 153 // Compose a capture result which contains a result metadata. 154 std::unique_ptr<CaptureResult> MakeResultMetadata( 155 uint32_t frame_number, std::unique_ptr<HalCameraMetadata> metadata, 156 std::vector<PhysicalCameraMetadata> physical_metadata, 157 uint32_t partial_result); 158 159 // Invoke the capture result callback to notify capture results. 160 void NotifyCaptureResults(std::vector<std::unique_ptr<CaptureResult>> results); 161 162 status_t AddFinalResultMetadata( 163 uint32_t frame_number, std::unique_ptr<HalCameraMetadata> final_metadata, 164 std::vector<PhysicalCameraMetadata> physical_metadata); 165 166 status_t AddResultMetadata( 167 uint32_t frame_number, std::unique_ptr<HalCameraMetadata> metadata, 168 std::vector<PhysicalCameraMetadata> physical_metadata, 169 uint32_t partial_result); 170 171 status_t AddBuffer(uint32_t frame_number, StreamBuffer buffer); 172 173 // Get a shutter message that is ready to be notified via notify_. 174 status_t GetReadyShutterMessage(NotifyMessage* message); 175 176 // Get a final metadata that is ready to be notified via the capture result callback. 177 status_t GetReadyFinalMetadata( 178 uint32_t* frame_number, std::unique_ptr<HalCameraMetadata>* final_metadata, 179 std::vector<PhysicalCameraMetadata>* physical_metadata); 180 181 // Get a result with a buffer that is ready to be notified via the capture 182 // result callback. 183 status_t GetReadyBufferResult(std::unique_ptr<CaptureResult>* result); 184 185 // Check all pending shutters and invoke notify_ with shutters that are ready. 186 void NotifyShutters(); 187 188 // Send partial result callbacks if `results` contains partial result metadata. 189 void NotifyBatchPartialResultMetadata( 190 std::vector<std::unique_ptr<CaptureResult>>& results); 191 192 // Check all pending final result metadata and invoke the capture result 193 // callback with final result metadata that are ready. 194 void NotifyFinalResultMetadata(); 195 196 // Check all pending buffers and invoke notify_ with buffers that are ready. 197 void NotifyBuffers(); 198 199 // Thread loop to check pending shutters, result metadata, and buffers. It 200 // notifies the client when one is ready. 201 void NotifyCallbackThreadLoop(); 202 203 void PrintTimeoutMessages(); 204 205 // Initialize the group stream ids map if needed. Must be protected with result_lock_. 206 void InitializeGroupStreamIdsMap(const StreamConfiguration& stream_config); 207 208 // Name used for debugging purpose to disambiguate multiple ResultDispatchers. 209 std::string name_; 210 211 std::mutex result_lock_; 212 213 // Maps from frame numbers to pending shutters. 214 // Protected by result_lock_. 215 std::map<uint32_t, PendingShutter> pending_shutters_; 216 217 // Create a StreamKey for a stream 218 inline StreamKey CreateStreamKey(int32_t stream_id) const; 219 220 // Dump a StreamKey to a debug string 221 inline std::string DumpStreamKey(const StreamKey& stream_key) const; 222 223 // Maps from a stream or a stream group to "a map from a frame number to a 224 // pending buffer". Protected by result_lock_. 225 // For single streams, pending buffers would be tracked by streams. 226 // For multi-resolution streams, camera HAL can return only one stream buffer 227 // within the same stream group each request. So all of the buffers of certain 228 // stream group will be tracked together via a single map. 229 std::map<StreamKey, std::map<uint32_t, PendingBuffer>> 230 stream_pending_buffers_map_; 231 232 // Maps from a stream ID to pending result metadata. 233 // Protected by result_lock_. 234 std::map<uint32_t, PendingFinalResultMetadata> pending_final_metadata_; 235 236 std::mutex process_capture_result_lock_; 237 ProcessCaptureResultFunc process_capture_result_; 238 ProcessBatchCaptureResultFunc process_batch_capture_result_; 239 NotifyFunc notify_; 240 241 // A thread to run NotifyCallbackThreadLoop(). 242 std::thread notify_callback_thread_; 243 244 std::mutex notify_callback_lock_; 245 246 // Condition to wake up notify_callback_thread_. Used with notify_callback_lock. 247 std::condition_variable notify_callback_condition_; 248 249 // Protected by notify_callback_lock. 250 bool notify_callback_thread_exiting_ = false; 251 252 // State of callback thread is notified or not. 253 volatile bool is_result_shutter_updated_ = false; 254 255 // A map of group streams only, from stream ID to the group ID it belongs. 256 std::map</*stream id=*/int32_t, /*group id=*/int32_t> group_stream_map_; 257 }; 258 259 } // namespace google_camera_hal 260 } // namespace android 261 262 #endif // HARDWARE_GOOGLE_CAMERA_HAL_UTILS_RESULT_DISPATCHER_H_ 263