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