1 /*
2  * Copyright (C) 2023 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 #pragma once
18 
19 #include <functional>
20 #include <memory>
21 #include <tuple>
22 #include <vector>
23 #include <stdint.h>
24 
25 #include <aidl/android/hardware/camera/device/CameraMetadata.h>
26 #include <aidl/android/hardware/camera/device/CaptureResult.h>
27 #include <aidl/android/hardware/camera/device/HalStream.h>
28 #include <aidl/android/hardware/camera/device/RequestTemplate.h>
29 #include <aidl/android/hardware/camera/device/Stream.h>
30 #include <aidl/android/hardware/camera/device/StreamBuffer.h>
31 #include <aidl/android/hardware/graphics/common/BufferUsage.h>
32 #include <aidl/android/hardware/graphics/common/Dataspace.h>
33 #include <aidl/android/hardware/graphics/common/PixelFormat.h>
34 
35 #include <cutils/native_handle.h>
36 #include <system/camera_metadata.h>
37 #include <system/graphics.h>
38 
39 #include "Rect.h"
40 #include "Span.h"
41 #include "CachedStreamBuffer.h"
42 
43 namespace android {
44 namespace hardware {
45 namespace camera {
46 namespace provider {
47 namespace implementation {
48 namespace hw {
49 
50 using aidl::android::hardware::camera::device::CameraMetadata;
51 using aidl::android::hardware::camera::device::HalStream;
52 using aidl::android::hardware::camera::device::RequestTemplate;
53 using aidl::android::hardware::camera::device::Stream;
54 using aidl::android::hardware::camera::device::StreamBuffer;
55 using aidl::android::hardware::graphics::common::BufferUsage;
56 using aidl::android::hardware::graphics::common::Dataspace;
57 using aidl::android::hardware::graphics::common::PixelFormat;
58 
59 struct HwCaptureRequest {
60     CameraMetadata metadataUpdate;
61     std::vector<CachedStreamBuffer*> buffers;
62     int32_t frameNumber;
63 };
64 
65 // pass `true` to process the buffer, pass `false` to return an error asap to
66 // release the underlying buffer to the framework.
67 using DelayedStreamBuffer = std::function<StreamBuffer(bool)>;
68 
69 struct HwCamera {
70     static constexpr int32_t kErrorBadFormat = -1;
71     static constexpr int32_t kErrorBadUsage = -2;
72     static constexpr int32_t kErrorBadDataspace = -3;
73 
~HwCameraHwCamera74     virtual ~HwCamera() {}
75 
76     virtual std::tuple<PixelFormat, BufferUsage, Dataspace, int32_t>
77         overrideStreamParams(PixelFormat, BufferUsage, Dataspace) const = 0;
78 
79     virtual bool configure(const CameraMetadata& sessionParams, size_t nStreams,
80                            const Stream* streams, const HalStream* halStreams) = 0;
81     virtual void close() = 0;
82 
83     virtual std::tuple<int64_t, int64_t, CameraMetadata, std::vector<StreamBuffer>,
84                        std::vector<DelayedStreamBuffer>>
85         processCaptureRequest(CameraMetadata, Span<CachedStreamBuffer*>) = 0;
86 
87     static int64_t getFrameDuration(const camera_metadata_t*, int64_t def,
88                                     int64_t min, int64_t max);
89 
90     static camera_metadata_enum_android_lens_state_t
91         getAfLensState(camera_metadata_enum_android_control_af_state_t);
92 
93     static bool compressJpeg(Rect<uint16_t> imageSize,
94                              const android_ycbcr& imageYcbcr,
95                              const CameraMetadata& metadata,
96                              const native_handle_t* jpegBuffer,
97                              size_t jpegBufferSize);
98 
99     static bool convertRGBAtoRAW16(Rect<uint16_t> imageSize,
100                                    const void* rgba,
101                                    const native_handle_t* raw16Buffer);
102 
103     ////////////////////////////////////////////////////////////////////////////
104     virtual Span<const std::pair<int32_t, int32_t>> getTargetFpsRanges() const = 0;
105     virtual std::tuple<int32_t, int32_t, int32_t, int32_t> getAeCompensationRange() const;
106     virtual std::pair<float, float> getZoomRatioRange() const;
107     virtual std::pair<int, int> getSupportedFlashStrength() const;
108     virtual Span<const Rect<uint16_t>> getAvailableThumbnailSizes() const = 0;
109     virtual int32_t getJpegMaxSize() const;
110     virtual bool isBackFacing() const = 0;
111     virtual Span<const float> getAvailableApertures() const;
112     virtual Span<const float> getAvailableFocalLength() const;
113     virtual float getHyperfocalDistance() const;
114     virtual float getMinimumFocusDistance() const;
115     virtual std::tuple<int32_t, int32_t, int32_t> getMaxNumOutputStreams() const = 0;
116     virtual int32_t getPipelineMaxDepth() const;
117     virtual uint32_t getAvailableCapabilitiesBitmap() const;
118     virtual Span<const PixelFormat> getSupportedPixelFormats() const = 0;
119     virtual Span<const Rect<uint16_t>> getSupportedResolutions() const = 0;
120     virtual float getMaxDigitalZoom() const;
121     virtual int64_t getMinFrameDurationNs() const = 0;
122     virtual int64_t getStallFrameDurationNs() const;
123     virtual int32_t getSensorOrientation() const;
124     virtual Rect<uint16_t> getSensorSize() const = 0;
125     virtual uint8_t getSensorColorFilterArrangement() const = 0;
126     virtual float getSensorDPI() const;
127     virtual std::pair<int32_t, int32_t> getSensorSensitivityRange() const;
128     virtual std::pair<int64_t, int64_t> getSensorExposureTimeRange() const = 0;
129     virtual int64_t getSensorMaxFrameDuration() const = 0;
130 
131     ////////////////////////////////////////////////////////////////////////////
132     virtual std::pair<int32_t, int32_t> getDefaultTargetFpsRange(RequestTemplate) const = 0;
133     virtual float getDefaultAperture() const;
134     virtual float getDefaultFocalLength() const;
135     virtual int32_t getDefaultSensorSensitivity() const;
136     virtual int64_t getDefaultSensorExpTime() const = 0;
137     virtual int64_t getDefaultSensorFrameDuration() const = 0;
138 };
139 
140 using HwCameraFactoryProduct = std::unique_ptr<HwCamera>;
141 using HwCameraFactory = std::function<HwCameraFactoryProduct()>;
142 
143 }  // namespace hw
144 }  // namespace implementation
145 }  // namespace provider
146 }  // namespace camera
147 }  // namespace hardware
148 }  // namespace android
149