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_ZSL_RESULT_DISPATCHER_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_UTILS_ZSL_RESULT_DISPATCHER_H_
19 
20 #include <map>
21 #include <thread>
22 
23 #include "hal_types.h"
24 #include "result_dispatcher.h"
25 
26 namespace android {
27 namespace google_camera_hal {
28 
29 // ZslResultDispatcher dispatches capture results of zsl requests and none-zsl
30 // requests in the order of frame numbers, including result metadata, shutters,
31 // and stream buffers.
32 //
33 // The client can add results and shutters via AddResult() and AddShutter() in
34 // any order. ZslResultDispatcher 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 ZslResultDispatcher {
38  public:
39   // Create a ZslResultDispatcher.
40   // partial_result_count is the partial result count.
41   // process_capture_result is the function to notify capture results.
42   // notify is the function to notify shutter messages.
43   // Treat ZSL requests and normal requests separately.
44   // For ZSL requests, it returns zsl shutter and zsl results in order
45   // and is not blocked by normal shutter and results.
46   // stream_config is the session stream configuration.
47   static std::unique_ptr<ZslResultDispatcher> Create(
48       uint32_t partial_result_count,
49       ProcessCaptureResultFunc process_capture_result, NotifyFunc notify,
50       const StreamConfiguration& stream_config);
51 
52   virtual ~ZslResultDispatcher() = default;
53 
54   // Add a pending request. This tells ZslResultDispatcher to watch for
55   // the shutter, result metadata, and stream buffers for this request,
56   // that will be added later via AddResult() and AddShutter().
57   // Treat the request as zsl request if is_zsl_request is true
58   status_t AddPendingRequest(const CaptureRequest& pending_request,
59                              bool is_zsl_request);
60 
61   // Add a ready result. If the result doesn't belong to a pending request that
62   // was previously added via AddPendingRequest(), an error will be returned.
63   status_t AddResult(std::unique_ptr<CaptureResult> result);
64 
65   // Add a shutter for a frame number. If the frame number doesn't belong to a
66   // pending request that was previously added via AddPendingRequest(), an error
67   // will be returned.
68   status_t AddShutter(uint32_t frame_number, int64_t timestamp_ns,
69                       int64_t readout_timestamp_ns);
70 
71   // Add an error notification for a frame number. When this is called, we no
72   // longer wait for a shutter message or result metadata for the given frame.
73   status_t AddError(const ErrorMessage& error);
74 
75   // Remove a pending request.
76   void RemovePendingRequest(uint32_t frame_number);
77 
78  protected:
79   ZslResultDispatcher(ProcessCaptureResultFunc process_capture_result,
80                       NotifyFunc notify);
81 
82  private:
83   status_t Initialize(uint32_t partial_result_count,
84                       const StreamConfiguration& stream_config);
85 
86   // Invoked when receiving a result from ResultDispatcher class.
87   void ProcessCaptureResult(std::unique_ptr<CaptureResult> result);
88 
89   // Invoked when receiving a message from ResultDispatcher.
90   void NotifyHalMessage(const NotifyMessage& message);
91 
92   // Return true if this frame is zsl request.
93   bool IsZslFrame(uint32_t frame_number);
94 
95   std::unique_ptr<ResultDispatcher> normal_result_dispatcher_;
96   std::unique_ptr<ResultDispatcher> zsl_result_dispatcher_;
97 
98   std::mutex process_capture_result_lock_;
99   // The following callbacks must be protected by process_capture_result_lock_.
100   // Pass this callback function to ResultDispatcher class
101   ProcessCaptureResultFunc process_capture_result_;
102 
103   std::mutex result_lock_;
104   // The following callbacks must be protected by result_lock_.
105   // Pass this callback function to ResultDispatcher class
106   NotifyFunc notify_;
107 
108   // Record the callback function for framework callback
109   ProcessCaptureResultFunc device_session_process_capture_result_;
110   NotifyFunc device_session_notify_;
111 
112   std::mutex zsl_frames_lock_;
113   // Store the frame number of zsl requests
114   // Protected by zsl_frames_lock_.
115   std::vector<uint32_t> zsl_frames_;
116 };
117 
118 }  // namespace google_camera_hal
119 }  // namespace android
120 
121 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_UTILS_ZSL_RESULT_DISPATCHER_H_
122