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   static std::unique_ptr<ZslResultDispatcher> Create(
47       uint32_t partial_result_count,
48       ProcessCaptureResultFunc process_capture_result, NotifyFunc notify);
49 
50   virtual ~ZslResultDispatcher() = default;
51 
52   // Add a pending request. This tells ZslResultDispatcher 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   // Treat the request as zsl request if is_zsl_request is true
56   status_t AddPendingRequest(const CaptureRequest& pending_request,
57                              bool is_zsl_request);
58 
59   // Add a ready result. If the result doesn't belong to a pending request that
60   // was previously added via AddPendingRequest(), an error will be returned.
61   status_t AddResult(std::unique_ptr<CaptureResult> result);
62 
63   // Add a shutter for a frame number. If the frame number doesn't belong to a
64   // pending request that was previously added via AddPendingRequest(), an error
65   // will be returned.
66   status_t AddShutter(uint32_t frame_number, int64_t timestamp_ns);
67 
68   // Add an error notification for a frame number. When this is called, we no
69   // longer wait for a shutter message or result metadata for the given frame.
70   status_t AddError(const ErrorMessage& error);
71 
72   // Remove a pending request.
73   void RemovePendingRequest(uint32_t frame_number);
74 
75  protected:
76   ZslResultDispatcher(ProcessCaptureResultFunc process_capture_result,
77                       NotifyFunc notify);
78 
79  private:
80   status_t Initialize(uint32_t partial_result_count);
81 
82   // Invoked when receiving a result from ResultDispatcher class.
83   void ProcessCaptureResult(std::unique_ptr<CaptureResult> result);
84 
85   // Invoked when receiving a message from ResultDispatcher.
86   void NotifyHalMessage(const NotifyMessage& message);
87 
88   // Return true if this frame is zsl request.
89   bool IsZslFrame(uint32_t frame_number);
90 
91   std::unique_ptr<ResultDispatcher> normal_result_dispatcher_;
92   std::unique_ptr<ResultDispatcher> zsl_result_dispatcher_;
93 
94   std::mutex process_capture_result_lock_;
95   // The following callbacks must be protected by process_capture_result_lock_.
96   // Pass this callback function to ResultDispatcher class
97   ProcessCaptureResultFunc process_capture_result_;
98 
99   std::mutex result_lock_;
100   // The following callbacks must be protected by result_lock_.
101   // Pass this callback function to ResultDispatcher class
102   NotifyFunc notify_;
103 
104   // Record the callback function for framework callback
105   ProcessCaptureResultFunc device_session_process_capture_result_;
106   NotifyFunc device_session_notify_;
107 
108   std::mutex zsl_frames_lock_;
109   // Store the frame number of zsl requests
110   // Protected by zsl_frames_lock_.
111   std::vector<uint32_t> zsl_frames_;
112 };
113 
114 }  // namespace google_camera_hal
115 }  // namespace android
116 
117 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_UTILS_ZSL_RESULT_DISPATCHER_H_
118