1 /*
2  * Copyright 2022 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 "MockHidlEvsCamera.h"
20 #include "MockHidlEvsDisplay.h"
21 #include "MockHidlEvsEnumerator.h"
22 
23 #include <android-base/thread_annotations.h>
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26 #include <vndk/hardware_buffer.h>
27 
28 #include <thread>
29 #include <unordered_map>
30 #include <unordered_set>
31 
32 namespace aidl::android::automotive::evs::implementation {
33 
34 namespace hidlevs = ::android::hardware::automotive::evs;
35 
36 class MockHidlEvsHal {
37 public:
MockHidlEvsHal(size_t numCameras,size_t numDisplays)38     MockHidlEvsHal(size_t numCameras, size_t numDisplays) :
39           mNumCameras(numCameras), mNumDisplays(numDisplays) {}
40     ~MockHidlEvsHal();
41 
42     void initialize();
43     ::android::sp<hidlevs::V1_1::IEvsEnumerator> getEnumerator();
44 
45     bool addMockCameraDevice(const std::string& deviceId);
46     void removeMockCameraDevice(const std::string& deviceId);
47     bool addMockDisplayDevice(int id);
48     void removeMockDisplayDevice(int id);
49     size_t setNumberOfFramesToSend(size_t n);
50 
51 private:
52     bool buildCameraMetadata(int32_t width, int32_t height, int32_t format,
53                              ::android::hardware::hidl_vec<uint8_t>* out);
54     void configureCameras(size_t n);
55     void configureDisplays(size_t n);
56     void configureEnumerator();
57     void forwardFrames(size_t numberOfFramesToForward, const std::string& deviceId);
58     size_t initializeBufferPool(size_t size);
59     void deinitializeBufferPoolLocked() REQUIRES(mLock);
60 
61     ::android::sp<NiceMockHidlEvsEnumerator> mMockHidlEvsEnumerator;
62     std::vector<::android::sp<NiceMockHidlEvsCamera>> mMockHidlEvsCameras;
63     std::vector<::android::sp<NiceMockHidlEvsDisplay>> mMockHidlEvsDisplays;
64     std::unordered_map<std::string, ::android::sp<hidlevs::V1_1::IEvsCameraStream>> mCameraClient;
65 
66     struct CameraRecord {
67         hidlevs::V1_1::CameraDesc desc;
68         ::android::wp<hidlevs::V1_1::IEvsCamera> activeInstance;
69 
CameraRecordCameraRecord70         CameraRecord(hidlevs::V1_1::CameraDesc& desc) : desc(desc) {}
71     };
72 
73     mutable std::mutex mLock;
74     std::condition_variable mBufferAvailableSignal;
75 
76     std::map<std::string, CameraRecord> mCameraList;
77     std::map<int32_t, ::android::hardware::hidl_vec<uint8_t>> mCameraExtendedInfo;
78     std::map<hidlevs::V1_1::CameraParam, int32_t> mCameraParams;
79     std::vector<hidlevs::V1_1::BufferDesc> mBufferPool GUARDED_BY(mLock);
80     std::vector<hidlevs::V1_1::BufferDesc> mBuffersInUse GUARDED_BY(mLock);
81     std::unordered_map<size_t, AHardwareBuffer*> mBufferRecord GUARDED_BY(mLock);
82     ::android::wp<hidlevs::V1_0::IEvsDisplay> mActiveDisplay;
83     bool mDisplayOwnedExclusively;
84 
85     hidlevs::V1_0::DisplayState mCurrentDisplayState = hidlevs::V1_0::DisplayState::NOT_OPEN;
86 
87     size_t mNumCameras = 0;
88     size_t mNumDisplays = 0;
89     size_t mBufferPoolSize = 0;
90     size_t mNumberOfFramesToSend = 5;
91 
92     enum class StreamState { kStopped, kRunning, kStopping };
93     std::unordered_map<std::string, std::atomic<StreamState>> mStreamState GUARDED_BY(mLock);
94     std::unordered_map<std::string, bool> mMockDeviceStatus GUARDED_BY(mLock);
95     std::unordered_map<std::string, size_t> mCameraBufferPoolSize GUARDED_BY(mLock);
96     std::unordered_map<std::string, std::thread> mCameraFrameThread GUARDED_BY(mLock);
97 };
98 
99 }  // namespace aidl::android::automotive::evs::implementation
100