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 <unordered_map>
20 
21 #include <android-base/unique_fd.h>
22 
23 #include "abc3d.h"
24 
25 #include "AutoNativeHandle.h"
26 #include "AFStateMachine.h"
27 #include "HwCamera.h"
28 
29 namespace android {
30 namespace hardware {
31 namespace camera {
32 namespace provider {
33 namespace implementation {
34 namespace hw {
35 
36 struct FakeRotatingCamera : public HwCamera {
37     explicit FakeRotatingCamera(bool isBackFacing);
38     ~FakeRotatingCamera() override;
39 
40     std::tuple<PixelFormat, BufferUsage, Dataspace, int32_t>
41         overrideStreamParams(PixelFormat, BufferUsage, Dataspace) const override;
42 
43     bool configure(const CameraMetadata& sessionParams, size_t nStreams,
44                    const Stream* streams, const HalStream* halStreams) override;
45     void close() override;
46 
47     std::tuple<int64_t, int64_t, CameraMetadata, std::vector<StreamBuffer>,
48                std::vector<DelayedStreamBuffer>>
49         processCaptureRequest(CameraMetadata, Span<CachedStreamBuffer*>) override;
50 
51     // metadata
52     Span<const std::pair<int32_t, int32_t>> getTargetFpsRanges() const override;
53     Span<const Rect<uint16_t>> getAvailableThumbnailSizes() const override;
54     bool isBackFacing() const override;
55     Span<const float> getAvailableFocalLength() const override;
56     std::tuple<int32_t, int32_t, int32_t> getMaxNumOutputStreams() const override;
57     Span<const PixelFormat> getSupportedPixelFormats() const override;
58     Span<const Rect<uint16_t>> getSupportedResolutions() const override;
59     int64_t getMinFrameDurationNs() const override;
60     Rect<uint16_t> getSensorSize() const override;
61     uint8_t getSensorColorFilterArrangement() const override;
62     std::pair<int64_t, int64_t> getSensorExposureTimeRange() const override;
63     int64_t getSensorMaxFrameDuration() const override;
64     std::pair<int32_t, int32_t> getDefaultTargetFpsRange(RequestTemplate) const override;
65     int64_t getDefaultSensorExpTime() const override;
66     int64_t getDefaultSensorFrameDuration() const override;
67     float getDefaultFocalLength() const override;
68 
69 private:
70     struct StreamInfo {
71         std::unique_ptr<const native_handle_t,
72                         AutoAllocatorNativeHandleDeleter> rgbaBuffer;
73         BufferUsage usage;
74         Rect<uint16_t> size;
75         PixelFormat pixelFormat;
76         uint32_t blobBufferSize;
77     };
78 
79     struct SensorValues {
80         float accel[3];
81         float magnetic[3];
82         float rotation[3];
83     };
84 
85     struct RenderParams {
86         struct CameraParams {
87             float pos3[3];
88             float rotXYZ3[3];
89         };
90         CameraParams cameraParams;
91     };
92 
93     abc3d::EglCurrentContext initOpenGL();
94     void closeImpl(bool everything);
95 
96     void captureFrame(const StreamInfo& si,
97                       const RenderParams& renderParams,
98                       CachedStreamBuffer* csb,
99                       std::vector<StreamBuffer>* outputBuffers,
100                       std::vector<DelayedStreamBuffer>* delayedOutputBuffers) const;
101     bool captureFrameRGBA(const StreamInfo& si,
102                           const RenderParams& renderParams,
103                           CachedStreamBuffer* dst) const;
104     bool captureFrameYUV(const StreamInfo& si,
105                          const RenderParams& renderParams,
106                          CachedStreamBuffer* dst) const;
107     DelayedStreamBuffer captureFrameJpeg(const StreamInfo& si,
108                                          const RenderParams& renderParams,
109                                          CachedStreamBuffer* csb) const;
110     std::vector<uint8_t> captureFrameForCompressing(const StreamInfo& si,
111                                                     const RenderParams& renderParams) const;
112     bool renderIntoRGBA(const StreamInfo& si,
113                         const RenderParams& renderParams,
114                         const native_handle_t* rgbaBuffer) const;
115     bool drawScene(Rect<uint16_t> imageSize,
116                    const RenderParams& renderParams,
117                    bool isHardwareBuffer) const;
118     bool drawSceneImpl(const float pvMatrix44[]) const;
119     CameraMetadata applyMetadata(const CameraMetadata& metadata);
120     CameraMetadata updateCaptureResultMetadata();
121     bool readSensors(SensorValues* vals);
122 
123     const bool mIsBackFacing;
124     AFStateMachine mAFStateMachine;
125     std::unordered_map<int32_t, StreamInfo> mStreamInfoCache;
126     base::unique_fd mQemuChannel;
127 
128     abc3d::EglContext mEglContext;
129     abc3d::AutoTexture mGlTestPatternTexture;
130     GLint mGlProgramAttrPositionLoc;
131     GLint mGlProgramAttrTexCoordLoc;
132     GLint mGlProgramUniformTextureLoc;
133     GLint mGlProgramUniformPvmMatrixLoc;
134     abc3d::AutoProgram mGlProgram;
135 
136     CameraMetadata mCaptureResultMetadata;
137     int64_t mFrameDurationNs = 0;
138 };
139 
140 }  // namespace hw
141 }  // namespace implementation
142 }  // namespace provider
143 }  // namespace camera
144 }  // namespace hardware
145 }  // namespace android
146