• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const ShutterMessage& shutter);
69 
70   // Add an error notification for a frame number. When this is called, we no
71   // longer wait for a shutter message or result metadata for the given frame.
72   status_t AddError(const ErrorMessage& error);
73 
74   // Remove a pending request.
75   void RemovePendingRequest(uint32_t frame_number);
76 
77  protected:
78   ZslResultDispatcher(ProcessCaptureResultFunc process_capture_result,
79                       NotifyFunc notify);
80 
81  private:
82   status_t Initialize(uint32_t partial_result_count,
83                       const StreamConfiguration& stream_config);
84 
85   // Invoked when receiving a result from ResultDispatcher class.
86   void ProcessCaptureResult(std::unique_ptr<CaptureResult> result);
87 
88   // Invoked when receiving a message from ResultDispatcher.
89   void NotifyHalMessage(const NotifyMessage& message);
90 
91   // Return true if this frame is zsl request.
92   bool IsZslFrame(uint32_t frame_number);
93 
94   std::unique_ptr<ResultDispatcher> normal_result_dispatcher_;
95   std::unique_ptr<ResultDispatcher> zsl_result_dispatcher_;
96 
97   std::mutex process_capture_result_lock_;
98   // The following callbacks must be protected by process_capture_result_lock_.
99   // Pass this callback function to ResultDispatcher class
100   ProcessCaptureResultFunc process_capture_result_;
101 
102   std::mutex result_lock_;
103   // The following callbacks must be protected by result_lock_.
104   // Pass this callback function to ResultDispatcher class
105   NotifyFunc notify_;
106 
107   // Record the callback function for framework callback
108   ProcessCaptureResultFunc device_session_process_capture_result_;
109   NotifyFunc device_session_notify_;
110 
111   std::mutex zsl_frames_lock_;
112   // Store the frame number of zsl requests
113   // Protected by zsl_frames_lock_.
114   std::vector<uint32_t> zsl_frames_;
115 };
116 
117 }  // namespace google_camera_hal
118 }  // namespace android
119 
120 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_UTILS_ZSL_RESULT_DISPATCHER_H_
121