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_GOOGLE_CAMERA_HAL_REALTIME_ZSL_RESULT_REQUEST_PROCESSOR_H_ 18 #define HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_REALTIME_ZSL_RESULT_REQUEST_PROCESSOR_H_ 19 20 #include <cstdint> 21 #include <shared_mutex> 22 #include <vector> 23 24 #include "hal_types.h" 25 #include "internal_stream_manager.h" 26 #include "realtime_zsl_result_processor.h" 27 #include "request_processor.h" 28 #include "result_processor.h" 29 30 namespace android { 31 namespace google_camera_hal { 32 33 // RealtimeZslResultRequestProcessor implements a RealtimeZslResultProcessor 34 // that return filled buffer and metadata to internal stream manager. It 35 // also implements a RequestProcess to forward the results. 36 class RealtimeZslResultRequestProcessor : public RealtimeZslResultProcessor, 37 RequestProcessor { 38 public: 39 static std::unique_ptr<RealtimeZslResultRequestProcessor> Create( 40 InternalStreamManager* internal_stream_manager, int32_t stream_id, 41 android_pixel_format_t pixel_format, uint32_t partial_result_count = 1); 42 43 virtual ~RealtimeZslResultRequestProcessor() = default; 44 45 // Override functions of RealtimeZslResultProcessor start. 46 void ProcessResult(ProcessBlockResult block_result) override; 47 48 void Notify(const ProcessBlockNotifyMessage& block_message) override; 49 // Override functions of RealtimeZslResultProcessor end. 50 51 // Override functions of RequestProcessor start. 52 status_t ConfigureStreams( 53 InternalStreamManager* internal_stream_manager, 54 const StreamConfiguration& stream_config, 55 StreamConfiguration* process_block_stream_config) override; 56 57 status_t SetProcessBlock(std::unique_ptr<ProcessBlock> process_block) override; 58 59 status_t ProcessRequest(const CaptureRequest& request) override; 60 61 status_t Flush() override; 62 63 void RepeatingRequestEnd(int32_t frame_number, 64 const std::vector<int32_t>& stream_ids) override; 65 // Override functions of RequestProcessor end. 66 67 void UpdateOutputBufferCount(int32_t frame_number, int output_buffer_count, 68 bool is_preview_intent); 69 70 protected: 71 RealtimeZslResultRequestProcessor( 72 InternalStreamManager* internal_stream_manager, int32_t stream_id, 73 uint32_t partial_result_count); 74 75 private: 76 std::shared_mutex process_block_shared_lock_; 77 78 // Protected by process_block_shared_lock_. 79 std::unique_ptr<ProcessBlock> process_block_; 80 81 // Simple wrapper struct to add partial result count to CaptureResult 82 struct RequestEntry { 83 std::unique_ptr<CaptureRequest> capture_request = nullptr; 84 uint32_t partial_results_received = 0; 85 bool zsl_buffer_received = false; 86 int framework_buffer_count = INT_MAX; 87 // Whether there were filled buffers that have been returned to internal 88 // stream manager. 89 bool has_returned_output_to_internal_stream_manager = false; 90 }; 91 92 bool AllDataCollected(const RequestEntry& request_entry) const; 93 94 // A helper function to combine information for the same frame number from 95 // `pending_error_frames_` and `pending_frame_number_to_requests_` to the 96 // `result`. This is a 3-way update, where `pending_request` info is copied to 97 // `error_entry` and `result`, and `pending_request` info gets reset. 98 void CombineErrorAndPendingEntriesToResult( 99 RequestEntry& error_entry, RequestEntry& pending_request, 100 std::unique_ptr<CaptureResult>& result) const; 101 102 // Returns result directly for frames with errors, if applicable. Call site 103 // must hold callback_lock_. 104 void ReturnResultDirectlyForFramesWithErrorsLocked( 105 RequestEntry& error_entry, RequestEntry& pending_request, 106 std::unique_ptr<CaptureResult> result); 107 108 // Results collected so far on a valid frame. Results are passed to the 109 // processor block once all items in the RequestEntry struct are complete - 110 // i.e. all buffers arrived an all partial results arrived. 111 std::unordered_map<uint32_t, RequestEntry> pending_frame_number_to_requests_; 112 // Results collected so far on a frame with an error. Each result item gets 113 // reported to the upper layer as it comes in, and once the RequestEntry 114 // struct is complete the entry is removed. 115 std::unordered_map<uint32_t, RequestEntry> pending_error_frames_; 116 }; 117 118 } // namespace google_camera_hal 119 } // namespace android 120 121 #endif // HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_REALTIME_ZSL_RESULT_REQUEST_PROCESSOR_H_ 122