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 
getDesc()68     const evs::CameraDesc& getDesc() { return mDescription; }
69 
70     static std::shared_ptr<EvsMockCamera> Create(const char* deviceName);
71     static std::shared_ptr<EvsMockCamera> Create(
72             const char* deviceName, std::unique_ptr<ConfigManager::CameraInfo>& camInfo,
73             const evs::Stream* streamCfg = nullptr);
74 
75   private:
76     void generateFrames();
77     void fillMockFrame(buffer_handle_t handle, const AHardwareBuffer_Desc* pDesc);
78 
79     ::android::status_t allocateOneFrame(buffer_handle_t* handle) override;
80 
81     bool startVideoStreamImpl_locked(const std::shared_ptr<evs::IEvsCameraStream>& receiver,
82                                      ndk::ScopedAStatus& status,
83                                      std::unique_lock<std::mutex>& lck) override;
84 
85     bool stopVideoStreamImpl_locked(ndk::ScopedAStatus& status,
86                                     std::unique_lock<std::mutex>& lck) override;
87 
88     bool postVideoStreamStop_locked(ndk::ScopedAStatus& status,
89                                     std::unique_lock<std::mutex>& lck) override;
90 
91     void initializeParameters();
92 
93     CameraDesc mDescription = {};  // The properties of this camera
94 
95     std::thread mCaptureThread;  // The thread we'll use to synthesize frames
96 
97     // The callback used to deliver each frame
98     std::shared_ptr<evs::IEvsCameraStream> mStream;
99 
100     // Horizontal pixel count in the buffers
101     uint32_t mWidth = 0;
102     // Vertical pixel count in the buffers
103     uint32_t mHeight = 0;
104     // Values from android_pixel_format_t
105     uint32_t mFormat = HAL_PIXEL_FORMAT_RGBA_8888;
106     // Values from from Gralloc.h
107     uint64_t mUsage =
108             GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
109     // Bytes per line in the buffers
110     uint32_t mStride = 0;
111 
112     // Static camera module information
113     std::unique_ptr<ConfigManager::CameraInfo>& mCameraInfo;
114 
115     // For the extended info
116     std::unordered_map<uint32_t, std::vector<uint8_t>> mExtInfo;
117 
118     // For the camera parameters.
119     struct CameraParameterDesc {
120         CameraParameterDesc(int min = 0, int max = 0, int step = 0, int value = 0) {
121             this->range.min = min;
122             this->range.max = max;
123             this->range.step = step;
124             this->value = value;
125         }
126 
127         ParameterRange range;
128         int32_t value;
129     };
130     std::unordered_map<CameraParam, std::shared_ptr<CameraParameterDesc>> mParams;
131 };
132 
133 }  // namespace aidl::android::hardware::automotive::evs::implementation
134