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