xref: /aosp_15_r20/hardware/interfaces/automotive/evs/aidl/impl/default/include/EvsMockCamera.h (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
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 "ConfigManager.h"
20 #include "EvsCamera.h"
21 
22 #include <aidl/android/hardware/automotive/evs/CameraDesc.h>
23 #include <aidl/android/hardware/automotive/evs/CameraParam.h>
24 #include <aidl/android/hardware/automotive/evs/IEvsCameraStream.h>
25 #include <aidl/android/hardware/automotive/evs/IEvsDisplay.h>
26 #include <aidl/android/hardware/automotive/evs/ParameterRange.h>
27 #include <aidl/android/hardware/automotive/evs/Stream.h>
28 #include <android/hardware_buffer.h>
29 #include <ui/GraphicBuffer.h>
30 
31 #include <cstdint>
32 #include <memory>
33 #include <thread>
34 #include <unordered_map>
35 #include <vector>
36 
37 namespace aidl::android::hardware::automotive::evs::implementation {
38 
39 class EvsMockCamera : public EvsCamera {
40   private:
41     using Base = EvsCamera;
42 
43   public:
44     EvsMockCamera(Sigil sigil, const char* deviceName,
45                   std::unique_ptr<ConfigManager::CameraInfo>& camInfo);
46     EvsMockCamera(const EvsMockCamera&) = delete;
47     EvsMockCamera& operator=(const EvsMockCamera&) = delete;
48 
49     // Methods from ::android::hardware::automotive::evs::IEvsCamera follow.
50     ndk::ScopedAStatus forcePrimaryClient(
51             const std::shared_ptr<evs::IEvsDisplay>& display) override;
52     ndk::ScopedAStatus getCameraInfo(evs::CameraDesc* _aidl_return) override;
53     ndk::ScopedAStatus getExtendedInfo(int32_t opaqueIdentifier,
54                                        std::vector<uint8_t>* value) override;
55     ndk::ScopedAStatus getIntParameter(evs::CameraParam id, std::vector<int32_t>* value) override;
56     ndk::ScopedAStatus getIntParameterRange(evs::CameraParam id,
57                                             evs::ParameterRange* _aidl_return) override;
58     ndk::ScopedAStatus getParameterList(std::vector<evs::CameraParam>* _aidl_return) override;
59     ndk::ScopedAStatus getPhysicalCameraInfo(const std::string& deviceId,
60                                              evs::CameraDesc* _aidl_return) override;
61     ndk::ScopedAStatus setExtendedInfo(int32_t opaqueIdentifier,
62                                        const std::vector<uint8_t>& opaqueValue) override;
63     ndk::ScopedAStatus setIntParameter(evs::CameraParam id, int32_t value,
64                                        std::vector<int32_t>* effectiveValue) override;
65     ndk::ScopedAStatus setPrimaryClient() override;
66     ndk::ScopedAStatus unsetPrimaryClient() override;
67 
getId()68     std::string getId() override { return mDescription.id; }
69 
getDesc()70     const CameraDesc& getDesc() { return mDescription; }
71 
72     static std::shared_ptr<EvsMockCamera> Create(const char* deviceName);
73     static std::shared_ptr<EvsMockCamera> Create(
74             const char* deviceName, std::unique_ptr<ConfigManager::CameraInfo>& camInfo,
75             const evs::Stream* streamCfg = nullptr);
76 
77   private:
78     void generateFrames();
79     void fillMockFrame(buffer_handle_t handle, const AHardwareBuffer_Desc* pDesc);
80 
81     ::android::status_t allocateOneFrame(buffer_handle_t* handle) override;
82 
83     bool startVideoStreamImpl_locked(const std::shared_ptr<evs::IEvsCameraStream>& receiver,
84                                      ndk::ScopedAStatus& status,
85                                      std::unique_lock<std::mutex>& lck) override;
86 
87     bool stopVideoStreamImpl_locked(ndk::ScopedAStatus& status,
88                                     std::unique_lock<std::mutex>& lck) override;
89 
90     bool postVideoStreamStop_locked(ndk::ScopedAStatus& status,
91                                     std::unique_lock<std::mutex>& lck) override;
92 
93     void initializeParameters();
94 
95     CameraDesc mDescription = {};  // The properties of this camera
96 
97     std::thread mCaptureThread;  // The thread we'll use to synthesize frames
98 
99     // The callback used to deliver each frame
100     std::shared_ptr<evs::IEvsCameraStream> mStream;
101 
102     // Horizontal pixel count in the buffers
103     uint32_t mWidth = 0;
104     // Vertical pixel count in the buffers
105     uint32_t mHeight = 0;
106     // Values from android_pixel_format_t
107     uint32_t mFormat = HAL_PIXEL_FORMAT_RGBA_8888;
108     // Values from from Gralloc.h
109     uint64_t mUsage =
110             GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
111     // Bytes per line in the buffers
112     uint32_t mStride = 0;
113 
114     // Static camera module information
115     std::unique_ptr<ConfigManager::CameraInfo>& mCameraInfo;
116 
117     // For the extended info
118     std::unordered_map<uint32_t, std::vector<uint8_t>> mExtInfo;
119 
120     // For the camera parameters.
121     struct CameraParameterDesc {
122         CameraParameterDesc(int min = 0, int max = 0, int step = 0, int value = 0) {
123             this->range.min = min;
124             this->range.max = max;
125             this->range.step = step;
126             this->value = value;
127         }
128 
129         ParameterRange range;
130         int32_t value;
131     };
132     std::unordered_map<CameraParam, std::shared_ptr<CameraParameterDesc>> mParams;
133 };
134 
135 }  // namespace aidl::android::hardware::automotive::evs::implementation
136