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