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 <string>
20 #include <unordered_map>
21 
22 #include <android-base/unique_fd.h>
23 
24 #include "HwCamera.h"
25 #include "AFStateMachine.h"
26 
27 namespace android {
28 namespace hardware {
29 namespace camera {
30 namespace provider {
31 namespace implementation {
32 namespace hw {
33 
34 struct QemuCamera : public HwCamera {
35     struct Parameters {
36         std::string name;
37         std::vector<Rect<uint16_t>> supportedResolutions;
38         std::vector<Rect<uint16_t>> availableThumbnailResolutions;
39         Rect<uint16_t> sensorSize;
40         bool isBackFacing;
41     };
42 
43     explicit QemuCamera(const Parameters& params);
44 
45     std::tuple<PixelFormat, BufferUsage, Dataspace, int32_t>
46         overrideStreamParams(PixelFormat, BufferUsage, Dataspace) const override;
47 
48     bool configure(const CameraMetadata& sessionParams, size_t nStreams,
49                    const Stream* streams, const HalStream* halStreams) override;
50     void close() override;
51 
52     std::tuple<int64_t, int64_t, CameraMetadata, std::vector<StreamBuffer>,
53                std::vector<DelayedStreamBuffer>>
54         processCaptureRequest(CameraMetadata, Span<CachedStreamBuffer*>) override;
55 
56     // metadata
57     uint32_t getAvailableCapabilitiesBitmap() const override;
58     Span<const std::pair<int32_t, int32_t>> getTargetFpsRanges() const override;
59     Span<const Rect<uint16_t>> getAvailableThumbnailSizes() const override;
60     bool isBackFacing() const override;
61     Span<const float> getAvailableApertures() const override;
62     std::tuple<int32_t, int32_t, int32_t> getMaxNumOutputStreams() const override;
63     Span<const PixelFormat> getSupportedPixelFormats() const override;
64     Span<const Rect<uint16_t>> getSupportedResolutions() const override;
65     int64_t getMinFrameDurationNs() const override;
66     Rect<uint16_t> getSensorSize() const override;
67     uint8_t getSensorColorFilterArrangement() const override;
68     std::pair<int32_t, int32_t> getSensorSensitivityRange() const override;
69     std::pair<int64_t, int64_t> getSensorExposureTimeRange() const override;
70     int64_t getSensorMaxFrameDuration() const override;
71 
72     std::pair<int32_t, int32_t> getDefaultTargetFpsRange(RequestTemplate) const override;
73     float getDefaultAperture() const override;
74     int64_t getDefaultSensorExpTime() const override;
75     int64_t getDefaultSensorFrameDuration() const override;
76     int32_t getDefaultSensorSensitivity() const override;
77 
78 private:
79     struct StreamInfo {
80         Rect<uint16_t> size;
81         PixelFormat pixelFormat;
82         uint32_t blobBufferSize;
83     };
84 
85     void captureFrame(const StreamInfo& si,
86                       CachedStreamBuffer* csb,
87                       std::vector<StreamBuffer>* outputBuffers,
88                       std::vector<DelayedStreamBuffer>* delayedOutputBuffers) const;
89     bool captureFrameYUV(const StreamInfo& si, CachedStreamBuffer* dst) const;
90     bool captureFrameRGBA(const StreamInfo& si, CachedStreamBuffer* dst) const;
91     DelayedStreamBuffer captureFrameRAW16(const StreamInfo& si,
92                                           CachedStreamBuffer* csb) const;
93     DelayedStreamBuffer captureFrameJpeg(const StreamInfo& si,
94                                          CachedStreamBuffer* csb) const;
95     const native_handle_t* captureFrameForCompressing(Rect<uint16_t> dim,
96                                                       PixelFormat bufferFormat,
97                                                       uint32_t qemuFormat) const;
98     bool queryFrame(Rect<uint16_t> dim, uint32_t pixelFormat,
99                     float exposureComp, uint64_t dataOffset) const;
100     static float calculateExposureComp(int64_t exposureNs, int sensorSensitivity,
101                                        float aperture);
102     CameraMetadata applyMetadata(const CameraMetadata& metadata);
103     CameraMetadata updateCaptureResultMetadata();
104 
105     const Parameters& mParams;
106     AFStateMachine mAFStateMachine;
107     std::unordered_map<int32_t, StreamInfo> mStreamInfoCache;
108     base::unique_fd mQemuChannel;
109     CameraMetadata mCaptureResultMetadata;
110 
111     int64_t mFrameDurationNs = 0;
112     int64_t mSensorExposureDurationNs = 0;
113     int32_t mSensorSensitivity = 0;
114     float mAperture = 0;
115     float mExposureComp = 0;
116 };
117 
118 }  // namespace hw
119 }  // namespace implementation
120 }  // namespace provider
121 }  // namespace camera
122 }  // namespace hardware
123 }  // namespace android
124