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_COMMON_HAL_TYPES_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_COMMON_HAL_TYPES_H_
19 
20 #include <cutils/native_handle.h>
21 #include <system/graphics-base-v1.0.h>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include "hal_camera_metadata.h"
27 
28 namespace android {
29 namespace google_camera_hal {
30 
31 using ::android::status_t;
32 
33 // Used to identify an invalid buffer handle.
34 static constexpr buffer_handle_t kInvalidBufferHandle = nullptr;
35 
36 // See the definition of
37 // ::android::hardware::camera::common::V1_0::TorchMode
38 enum class TorchMode : uint32_t {
39   kOff = 0,
40   kOn = 1,
41 };
42 
43 // See the definition of
44 // ::hardware::camera::common::V1_0::CameraDeviceStatus
45 enum class CameraDeviceStatus : uint32_t {
46   kNotPresent = 0,
47   kPresent,
48   kEnumerating,
49 };
50 
51 // See the definition of
52 // ::hardware::camera::common::V1_0::TorchModeStatus
53 enum class TorchModeStatus : uint32_t {
54   kNotAvailable = 0,
55   kAvailableOff,
56   kAvailableOn,
57 };
58 
59 // See the definition of
60 // ::android::hardware::camera::common::V1_0::CameraResourceCost
61 struct CameraResourceCost {
62   uint32_t resource_cost = 0;
63   std::vector<uint32_t> conflicting_devices;
64 };
65 
66 // See the definition of
67 // ::android::hardware::camera::common::V1_0::CameraMetadataType
68 enum class CameraMetadataType : uint32_t {
69   kByte = 0,
70   kInt32,
71   kFloat,
72   kInt64,
73   kDouble,
74   kRational,
75 };
76 
77 // See the definition of
78 // ::android::hardware::camera::common::V1_0::VendorTag
79 struct VendorTag {
80   uint32_t tag_id = 0;
81   std::string tag_name;
82   CameraMetadataType tag_type = CameraMetadataType::kByte;
83 };
84 
85 // See the definition of
86 // ::android::hardware::camera::common::V1_0::VendorTagSection
87 struct VendorTagSection {
88   std::string section_name;
89   std::vector<VendorTag> tags;
90 };
91 
92 // See the definition of
93 // ::android::hardware::camera::device::V3_2::StreamType;
94 enum class StreamType : uint32_t {
95   kOutput = 0,
96   kInput,
97 };
98 
99 // See the definition of
100 // ::android::hardware::camera::device::V3_2::StreamRotation;
101 enum class StreamRotation : uint32_t {
102   kRotation0 = 0,
103   kRotation90,
104   kRotation180,
105   kRotation270,
106 };
107 
108 // See the definition of
109 // ::android::hardware::camera::device::V3_7::Stream;
110 struct Stream {
111   int32_t id = -1;
112   StreamType stream_type = StreamType::kOutput;
113   uint32_t width = 0;
114   uint32_t height = 0;
115   android_pixel_format_t format = HAL_PIXEL_FORMAT_RGBA_8888;
116   uint64_t usage = 0;
117   android_dataspace_t data_space = HAL_DATASPACE_UNKNOWN;
118   StreamRotation rotation = StreamRotation::kRotation0;
119   bool is_physical_camera_stream = false;
120   uint32_t physical_camera_id = 0;
121   uint32_t buffer_size = 0;
122   int32_t group_id = -1;
123   bool used_in_max_resolution_mode = false;
124   bool used_in_default_resolution_mode = true;
125 };
126 
127 // See the definition of
128 // ::android::hardware::camera::device::V3_2::StreamConfigurationMode;
129 enum class StreamConfigurationMode : uint32_t {
130   kNormal = 0,
131   kConstrainedHighSpeed,
132 };
133 
134 // See the definition of
135 // ::android::hardware::camera::device::V3_7::StreamConfiguration;
136 struct StreamConfiguration {
137   std::vector<Stream> streams;
138   StreamConfigurationMode operation_mode;
139   std::unique_ptr<HalCameraMetadata> session_params;
140   uint32_t stream_config_counter = 0;
141   bool multi_resolution_input_image = false;
142 };
143 
144 struct CameraIdAndStreamConfiguration {
145   uint32_t camera_id = 0;
146   StreamConfiguration stream_configuration;
147 };
148 
149 // See the definition of
150 // ::android::hardware::camera::device::V3_4::HalStream
151 struct HalStream {
152   int32_t id = -1;
153   android_pixel_format_t override_format = HAL_PIXEL_FORMAT_RGBA_8888;
154   uint64_t producer_usage = 0;
155   uint64_t consumer_usage = 0;
156   uint32_t max_buffers = 0;
157   android_dataspace_t override_data_space = HAL_DATASPACE_UNKNOWN;
158   bool is_physical_camera_stream = false;
159   uint32_t physical_camera_id = 0;
160 };
161 
162 // See the definition of
163 // ::android::hardware::camera::device::V3_2::BufferCache
164 struct BufferCache {
165   int32_t stream_id = -1;
166   uint64_t buffer_id = 0;
167 
168   bool operator==(const BufferCache& other) const {
169     return stream_id == other.stream_id && buffer_id == other.buffer_id;
170   }
171 };
172 
173 // See the definition of
174 // ::android::hardware::camera::device::V3_2::BufferStatus
175 enum class BufferStatus : uint32_t {
176   kOk = 0,
177   kError,
178 };
179 
180 // See the definition of
181 // ::android::hardware::camera::device::V3_2::StreamBuffer
182 struct StreamBuffer {
183   int32_t stream_id = -1;
184   uint64_t buffer_id = 0;
185   buffer_handle_t buffer = nullptr;
186   BufferStatus status = BufferStatus::kOk;
187 
188   // The fences are owned by the caller. If they will be used after a call
189   // returns, the callee should duplicate it.
190   const native_handle_t* acquire_fence = nullptr;
191   const native_handle_t* release_fence = nullptr;
192 };
193 
194 // See the definition of
195 // ::android::hardware::camera::device::V3_4::CaptureRequest
196 struct CaptureRequest {
197   uint32_t frame_number = 0;
198   std::unique_ptr<HalCameraMetadata> settings;
199 
200   // If empty, the output buffers are captured from the camera sensors. If
201   // not empty, the output buffers are captured from the input buffers.
202   std::vector<StreamBuffer> input_buffers;
203 
204   // The metadata of the input_buffers. This is used for multi-frame merging
205   // like HDR+. The input_buffer_metadata at entry k must be for the input
206   // buffer at entry k in the input_buffers.
207   std::vector<std::unique_ptr<HalCameraMetadata>> input_buffer_metadata;
208 
209   std::vector<StreamBuffer> output_buffers;
210 
211   // Maps from physical camera ID to physical camera settings.
212   std::unordered_map<uint32_t, std::unique_ptr<HalCameraMetadata>>
213       physical_camera_settings;
214 
215   uint32_t input_width;
216   uint32_t input_height;
217 };
218 
219 // See the definition of
220 // ::android::hardware::camera::device::V3_2::RequestTemplate
221 enum class RequestTemplate : uint32_t {
222   kPreview = 1,
223   kStillCapture = 2,
224   kVideoRecord = 3,
225   kVideoSnapshot = 4,
226   kZeroShutterLag = 5,
227   kManual = 6,
228   kVendorTemplateStart = 0x40000000,
229 };
230 
231 // See the definition of
232 // ::android::hardware::camera::device::V3_2::MsgType
233 enum class MessageType : uint32_t {
234   kError = 1,
235   kShutter = 2,
236 };
237 
238 // See the definition of
239 // ::android::hardware::camera::device::V3_2::ErrorCode
240 enum class ErrorCode : uint32_t {
241   kErrorDevice = 1,
242   kErrorRequest = 2,
243   kErrorResult = 3,
244   kErrorBuffer = 4,
245 };
246 
247 // See the definition of
248 // ::android::hardware::camera::device::V3_2::ErrorMsg
249 struct ErrorMessage {
250   uint32_t frame_number = 0;
251   int32_t error_stream_id = -1;
252   ErrorCode error_code = ErrorCode::kErrorDevice;
253 };
254 
255 // See the definition of
256 // ::android::hardware::camera::device::V3_2::ShutterMsg
257 struct ShutterMessage {
258   uint32_t frame_number = 0;
259   uint64_t timestamp_ns = 0;
260 };
261 
262 // See the definition of
263 // ::android::hardware::camera::device::V3_2::NotifyMsg
264 struct NotifyMessage {
265   MessageType type = MessageType::kError;
266 
267   union Message {
268     ErrorMessage error;
269     ShutterMessage shutter;
270   } message;
271 };
272 
273 // See the definition of
274 // ::android::hardware::camera::device::V3_4::PhysicalCameraMetadata
275 struct PhysicalCameraMetadata {
276   uint32_t physical_camera_id = 0;
277   std::unique_ptr<HalCameraMetadata> metadata;
278 };
279 
280 // See the definition of
281 // ::android::hardware::camera::device::V3_4::CaptureResult
282 struct CaptureResult {
283   uint32_t frame_number = 0;
284   std::unique_ptr<HalCameraMetadata> result_metadata;
285   std::vector<StreamBuffer> output_buffers;
286   std::vector<StreamBuffer> input_buffers;
287   uint32_t partial_result = 0;
288   std::vector<PhysicalCameraMetadata> physical_metadata;
289 };
290 
291 struct Rect {
292   uint32_t left;
293   uint32_t top;
294   uint32_t right;
295   uint32_t bottom;
296 };
297 
298 struct WeightedRect : Rect {
299   int32_t weight;
300 };
301 
302 struct Dimension {
303   uint32_t width = 0;
304   uint32_t height = 0;
305 };
306 
307 struct Point {
308   uint32_t x;
309   uint32_t y;
310 };
311 
312 struct PointI {
313   int32_t x;
314   int32_t y;
315 };
316 
317 struct PointF {
318   float x = 0.0f;
319   float y = 0.0f;
320 };
321 
322 // Hash function for std::pair
323 struct PairHash {
324   template <class T1, class T2>
operatorPairHash325   std::size_t operator()(const std::pair<T1, T2>& p) const {
326     return std::hash<T1>{}(p.first) ^ std::hash<T2>{}(p.second);
327   }
328 };
329 
330 // See the definition of
331 // ::android::hardware::camera::device::V3_5::BufferRequestStatus;
332 enum class BufferRequestStatus : uint32_t {
333   kOk = 0,
334   kFailedPartial = 1,
335   kFailedConfiguring = 2,
336   kFailedIllegalArgs = 3,
337   kFailedUnknown = 4,
338 };
339 
340 // See the definition of
341 // ::android::hardware::camera::device::V3_5::StreamBufferRequestError
342 enum class StreamBufferRequestError : uint32_t {
343   kOk = 0,
344   kNoBufferAvailable = 1,
345   kMaxBufferExceeded = 2,
346   kStreamDisconnected = 3,
347   kUnknownError = 4,
348 };
349 
350 // See the definition of
351 // ::android::hardware::camera::device::V3_5::BufferRequest
352 struct BufferRequest {
353   int32_t stream_id = -1;
354   uint32_t num_buffers_requested = 0;
355 };
356 
357 // See the definition of
358 // ::android::hardware::camera::device::V3_5::StreamBuffersVal
359 struct BuffersValue {
360   StreamBufferRequestError error = StreamBufferRequestError::kUnknownError;
361   std::vector<StreamBuffer> buffers;
362 };
363 
364 // See the definition of
365 // ::android::hardware::camera::device::V3_5::StreamBufferRet
366 struct BufferReturn {
367   int32_t stream_id = -1;
368   BuffersValue val;
369 };
370 
371 // See the definition of
372 // ::android::hardware::camera::provider::V2_5::DeviceState
373 enum class DeviceState : uint64_t {
374   kNormal = 0ull,
375   kBackCovered = 1ull,
376   kFrontCovered = 2ull,
377   kFolded = 4ull
378 };
379 
380 // Callback function invoked to process capture results.
381 using ProcessCaptureResultFunc =
382     std::function<void(std::unique_ptr<CaptureResult> /*result*/)>;
383 
384 // Callback function invoked to notify messages.
385 using NotifyFunc = std::function<void(const NotifyMessage& /*message*/)>;
386 
387 // HAL buffer allocation descriptor
388 struct HalBufferDescriptor {
389   int32_t stream_id = -1;
390   uint32_t width = 0;
391   uint32_t height = 0;
392   android_pixel_format_t format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
393   uint64_t producer_flags = 0;
394   uint64_t consumer_flags = 0;
395   uint32_t immediate_num_buffers = 0;
396   uint32_t max_num_buffers = 0;
397   uint64_t allocator_id_ = 0;
398 };
399 
400 // Callback function invoked to request stream buffers.
401 using RequestStreamBuffersFunc = std::function<BufferRequestStatus(
402     const std::vector<BufferRequest>& /*buffer_requests*/,
403     std::vector<BufferReturn>* /*buffer_returns*/)>;
404 
405 // Callback function invoked to return stream buffers.
406 using ReturnStreamBuffersFunc =
407     std::function<void(const std::vector<StreamBuffer>& /*buffers*/)>;
408 
409 struct ZoomRatioRange {
410   float min = 1.0f;
411   float max = 1.0f;
412 };
413 
414 }  // namespace google_camera_hal
415 }  // namespace android
416 
417 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_COMMON_HAL_TYPES_H_
418