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_BASIC_CAPTURE_SESSION_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_BASIC_CAPTURE_SESSION_H_
19 
20 #include <vector>
21 
22 #include "camera_buffer_allocator_hwl.h"
23 #include "camera_device_session_hwl.h"
24 #include "capture_session.h"
25 #include "hal_types.h"
26 #include "hwl_types.h"
27 #include "request_processor.h"
28 #include "result_dispatcher.h"
29 #include "result_processor.h"
30 
31 namespace android {
32 namespace google_camera_hal {
33 
34 // BasicCaptureSession implements a CaptureSession that contains a single
35 // process chain that consists of
36 //
37 //   BasicRequestProcessor -> RealtimeProcessBlock -> BasicResultProcessor
38 //
39 // It only supports a single physical camera device session.
40 class BasicCaptureSession : public CaptureSession {
41  public:
42   // Return if the device session HWL and stream configuration are supported.
43   static bool IsStreamConfigurationSupported(
44       CameraDeviceSessionHwl* device_session_hwl,
45       const StreamConfiguration& stream_config);
46 
47   // Create a BasicCaptureSession.
48   //
49   // device_session_hwl is owned by the caller and must be valid during the
50   // lifetime of BasicCaptureSession.
51   // stream_config is the stream configuration.
52   // process_capture_result is the callback function to notify results.
53   // process_batch_capture_result is the callback function to notify batched
54   // results.
55   // notify is the callback function to notify messages.
56   // hal_configured_streams will be filled with HAL configured streams.
57   // camera_allocator_hwl is owned by the caller and must be valid during the
58   // lifetime of BasicCaptureSession
59   static std::unique_ptr<CaptureSession> Create(
60       CameraDeviceSessionHwl* device_session_hwl,
61       const StreamConfiguration& stream_config,
62       ProcessCaptureResultFunc process_capture_result,
63       ProcessBatchCaptureResultFunc process_batch_capture_result,
64       NotifyFunc notify, HwlSessionCallback session_callback,
65       std::vector<HalStream>* hal_configured_streams,
66       CameraBufferAllocatorHwl* camera_allocator_hwl = nullptr);
67 
68   virtual ~BasicCaptureSession();
69 
70   // Override functions in CaptureSession start.
71   status_t ProcessRequest(const CaptureRequest& request) override;
72 
73   status_t Flush() override;
74   // Override functions in CaptureSession end.
75 
76   void RepeatingRequestEnd(int32_t frame_number,
77                            const std::vector<int32_t>& stream_ids) override;
78 
79  protected:
80   BasicCaptureSession() = default;
81 
82  private:
83   status_t Initialize(CameraDeviceSessionHwl* device_session_hwl,
84                       const StreamConfiguration& stream_config,
85                       ProcessCaptureResultFunc process_capture_result,
86                       ProcessBatchCaptureResultFunc process_batch_capture_result,
87                       NotifyFunc notify,
88                       std::vector<HalStream>* hal_configured_streams);
89 
90   // Configure streams for request processor and process block.
91   status_t ConfigureStreams(const StreamConfiguration& stream_config,
92                             RequestProcessor* request_processor,
93                             ProcessBlock* process_block);
94 
95   // Build pipelines and return HAL configured streams.
96   status_t BuildPipelines(ProcessBlock* process_block,
97                           std::vector<HalStream>* hal_configured_streams);
98 
99   // Connect the process chain.
100   status_t ConnectProcessChain(RequestProcessor* request_processor,
101                                std::unique_ptr<ProcessBlock> process_block,
102                                std::unique_ptr<ResultProcessor> result_processor);
103 
104   void ProcessCaptureResult(std::unique_ptr<CaptureResult> result);
105   void Notify(const NotifyMessage& message);
106   void ProcessBatchCaptureResult(
107       std::vector<std::unique_ptr<CaptureResult>> results);
108 
109   std::unique_ptr<RequestProcessor> request_processor_;
110 
111   // device_session_hwl_ is owned by the client.
112   CameraDeviceSessionHwl* device_session_hwl_ = nullptr;
113   std::unique_ptr<InternalStreamManager> internal_stream_manager_;
114   std::unique_ptr<ResultDispatcher> result_dispatcher_;
115 };
116 
117 }  // namespace google_camera_hal
118 }  // namespace android
119 
120 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_BASIC_CAPTURE_SESSION_H_
121