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_HDRPLUS_CAPTURE_SESSION_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_HDRPLUS_CAPTURE_SESSION_H_
19 
20 #include "camera_buffer_allocator_hwl.h"
21 #include "camera_device_session_hwl.h"
22 #include "capture_session.h"
23 #include "hwl_types.h"
24 #include "request_processor.h"
25 #include "result_dispatcher.h"
26 #include "result_processor.h"
27 #include "vendor_tag_types.h"
28 
29 namespace android {
30 namespace google_camera_hal {
31 
32 // HdrplusCaptureSession implements a CaptureSession that contains two
33 // process chains (realtime and HDR+)
34 //
35 // 1. RealtimeZslRequestProcessor -> RealtimeProcessBlock ->
36 //    RealtimeZslResultProcessor
37 // 2. HdrplusRequestProcessor -> HdrplusProcessBlock -> HdrplusResultProcessor
38 //
39 // It only supports a single physical camera device session.
40 class HdrplusCaptureSession : 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 HdrplusCaptureSession.
48   //
49   // device_session_hwl is owned by the caller and must be valid during the
50   // lifetime of HdrplusCaptureSession.
51   // stream_config is the stream configuration.
52   // process_capture_result is the callback function to notify results.
53   // notify is the callback function to notify messages.
54   // hal_configured_streams will be filled with HAL configured streams.
55   // camera_allocator_hwl is owned by the caller and must be valid during the
56   // lifetime of HdrplusCaptureSession
57   static std::unique_ptr<HdrplusCaptureSession> Create(
58       CameraDeviceSessionHwl* device_session_hwl,
59       const StreamConfiguration& stream_config,
60       ProcessCaptureResultFunc process_capture_result, NotifyFunc notify,
61       HwlSessionCallback session_callback,
62       std::vector<HalStream>* hal_configured_streams,
63       CameraBufferAllocatorHwl* camera_allocator_hwl = nullptr);
64 
65   virtual ~HdrplusCaptureSession();
66 
67   // Override functions in CaptureSession start.
68   status_t ProcessRequest(const CaptureRequest& request) override;
69 
70   status_t Flush() override;
71   // Override functions in CaptureSession end.
72 
73  protected:
74   HdrplusCaptureSession() = default;
75 
76  private:
77   static const uint32_t kRawBufferCount = 16;
78   static const uint32_t kRawMinBufferCount = 12;
79   static constexpr uint32_t kPartialResult = 1;
80   static const android_pixel_format_t kHdrplusRawFormat = HAL_PIXEL_FORMAT_RAW10;
81   status_t Initialize(CameraDeviceSessionHwl* device_session_hwl,
82                       const StreamConfiguration& stream_config,
83                       ProcessCaptureResultFunc process_capture_result,
84                       NotifyFunc notify,
85                       std::vector<HalStream>* hal_configured_streams);
86 
87   // Setup realtime process chain
88   status_t SetupRealtimeProcessChain(
89       const StreamConfiguration& stream_config,
90       ProcessCaptureResultFunc process_capture_result, NotifyFunc notify,
91       std::unique_ptr<ProcessBlock>* realtime_process_block,
92       std::unique_ptr<ResultProcessor>* realtime_result_processor,
93       int32_t* raw_stream_id);
94 
95   // Setup hdrplus process chain
96   status_t SetupHdrplusProcessChain(
97       const StreamConfiguration& stream_config,
98       ProcessCaptureResultFunc process_capture_result, NotifyFunc notify,
99       std::unique_ptr<ProcessBlock>* hdrplus_process_block,
100       std::unique_ptr<ResultProcessor>* hdrplus_result_processor,
101       int32_t raw_stream_id);
102 
103   // Configure streams for request processor and process block.
104   status_t ConfigureStreams(const StreamConfiguration& stream_config,
105                             RequestProcessor* request_processor,
106                             ProcessBlock* process_block, int32_t* raw_stream_id);
107 
108   // Configure hdrplus streams for request processor and process block.
109   status_t ConfigureHdrplusStreams(const StreamConfiguration& stream_config,
110                                    RequestProcessor* hdrplus_request_processor,
111                                    ProcessBlock* hdrplus_process_block);
112 
113   // Build pipelines and return HAL configured streams.
114   // Allocate internal raw buffer
115   status_t BuildPipelines(ProcessBlock* process_block,
116                           ProcessBlock* hdrplus_process_block,
117                           std::vector<HalStream>* hal_configured_streams);
118 
119   // Connect the process chain.
120   status_t ConnectProcessChain(RequestProcessor* request_processor,
121                                std::unique_ptr<ProcessBlock> process_block,
122                                std::unique_ptr<ResultProcessor> result_processor);
123 
124   // Purge the hal_configured_streams such that only framework streams are left
125   status_t PurgeHalConfiguredStream(
126       const StreamConfiguration& stream_config,
127       std::vector<HalStream>* hal_configured_streams);
128 
129   // Invoked when receiving a result from result processor.
130   void ProcessCaptureResult(std::unique_ptr<CaptureResult> result);
131 
132   // Invoked when reciving a message from result processor.
133   void NotifyHalMessage(const NotifyMessage& message);
134 
135   std::unique_ptr<RequestProcessor> request_processor_;
136 
137   std::unique_ptr<RequestProcessor> hdrplus_request_processor_;
138   // device_session_hwl_ is owned by the client.
139   CameraDeviceSessionHwl* device_session_hwl_ = nullptr;
140 
141   std::unique_ptr<InternalStreamManager> internal_stream_manager_;
142 
143   std::unique_ptr<ResultDispatcher> result_dispatcher_;
144 
145   std::mutex callback_lock_;
146   // The following callbacks must be protected by callback_lock_.
147   ProcessCaptureResultFunc process_capture_result_;
148   NotifyFunc notify_;
149   // For error notify to framework directly
150   NotifyFunc device_session_notify_;
151   // Use this stream id to check the request is HDR+ compatible
152   int32_t hal_preview_stream_id_ = -1;
153 
154   HdrMode hdr_mode_ = HdrMode::kHdrplusMode;
155 };
156 
157 }  // namespace google_camera_hal
158 }  // namespace android
159 
160 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_HDRPLUS_CAPTURE_SESSION_H_
161