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_1_0.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_1_0 {
37 public:
MockHidlEvsHal_1_0(size_t numCameras,size_t numDisplays)38     MockHidlEvsHal_1_0(size_t numCameras, size_t numDisplays) :
39           mNumCameras(numCameras), mNumDisplays(numDisplays) {}
40     ~MockHidlEvsHal_1_0();
41 
42     void initialize();
43     ::android::sp<hidlevs::V1_0::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     void configureCameras(size_t n);
53     void configureDisplays(size_t n);
54     void configureEnumerator();
55     void forwardFrames(size_t numberOfFramesToForward, const std::string& deviceId);
56     size_t initializeBufferPool(size_t size);
57     void deinitializeBufferPoolLocked() REQUIRES(mLock);
58 
59     ::android::sp<NiceMockHidlEvsEnumerator_1_0> mMockHidlEvsEnumerator;
60     std::vector<::android::sp<NiceMockHidlEvsCamera>> mMockHidlEvsCameras;
61     std::vector<::android::sp<NiceMockHidlEvsDisplay>> mMockHidlEvsDisplays;
62     std::unordered_map<std::string, ::android::sp<hidlevs::V1_0::IEvsCameraStream>> mCameraClient;
63 
64     struct CameraRecord {
65         hidlevs::V1_0::CameraDesc desc;
66         ::android::wp<hidlevs::V1_0::IEvsCamera> activeInstance;
67 
CameraRecordCameraRecord68         CameraRecord(hidlevs::V1_0::CameraDesc& desc) : desc(desc) {}
69     };
70 
71     mutable std::mutex mLock;
72     std::condition_variable mBufferAvailableSignal;
73 
74     std::map<std::string, CameraRecord> mCameraList;
75     std::map<int32_t, ::android::hardware::hidl_vec<uint8_t>> mCameraExtendedInfo;
76     std::vector<hidlevs::V1_0::BufferDesc> mBufferPool GUARDED_BY(mLock);
77     std::vector<hidlevs::V1_0::BufferDesc> mBuffersInUse GUARDED_BY(mLock);
78     std::unordered_map<size_t, AHardwareBuffer*> mBufferRecord GUARDED_BY(mLock);
79     ::android::wp<hidlevs::V1_0::IEvsDisplay> mActiveDisplay;
80 
81     hidlevs::V1_0::DisplayState mCurrentDisplayState = hidlevs::V1_0::DisplayState::NOT_OPEN;
82 
83     size_t mNumCameras = 0;
84     size_t mNumDisplays = 0;
85     size_t mBufferPoolSize = 0;
86     size_t mNumberOfFramesToSend = 5;
87 
88     enum class StreamState { kStopped, kRunning, kStopping };
89     std::unordered_map<std::string, std::atomic<StreamState>> mStreamState GUARDED_BY(mLock);
90     std::unordered_map<std::string, bool> mMockDeviceStatus GUARDED_BY(mLock);
91     std::unordered_map<std::string, size_t> mCameraBufferPoolSize GUARDED_BY(mLock);
92     std::unordered_map<std::string, std::thread> mCameraFrameThread GUARDED_BY(mLock);
93 };
94 
95 }  // namespace aidl::android::automotive::evs::implementation
96