Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #ifndef ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSCAMERA_H |
| 18 | #define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSCAMERA_H |
| 19 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 20 | #include "ConfigManager.h" |
| 21 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 22 | #include <android/hardware/automotive/evs/1.1/IEvsCamera.h> |
| 23 | #include <android/hardware/automotive/evs/1.1/IEvsCameraStream.h> |
Changyeon Jo | 043a7a0 | 2020-01-02 17:55:55 -0800 | [diff] [blame] | 24 | #include <android/hardware/automotive/evs/1.1/IEvsDisplay.h> |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 25 | #include <android/hardware/automotive/evs/1.1/types.h> |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 26 | #include <ui/GraphicBuffer.h> |
| 27 | |
| 28 | #include <thread> |
| 29 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 30 | namespace android::hardware::automotive::evs::V1_1::implementation { |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 31 | |
| 32 | // From EvsEnumerator.h |
| 33 | class EvsEnumerator; |
| 34 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 35 | class EvsCamera : public IEvsCamera { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 36 | public: |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 37 | // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCamera follow. |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 38 | Return<void> getCameraInfo(getCameraInfo_cb _hidl_cb) override; |
| 39 | Return<V1_0::EvsResult> setMaxFramesInFlight(uint32_t bufferCount) override; |
| 40 | Return<V1_0::EvsResult> startVideoStream(const sp<V1_0::IEvsCameraStream>& stream) override; |
| 41 | Return<void> stopVideoStream() override; |
| 42 | Return<void> doneWithFrame(const V1_0::BufferDesc& buffer) override; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 43 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 44 | Return<int32_t> getExtendedInfo(uint32_t opaqueIdentifier) override; |
| 45 | Return<V1_0::EvsResult> setExtendedInfo(uint32_t opaqueIdentifier, |
| 46 | int32_t opaqueValue) override; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 47 | |
| 48 | // Methods from ::android::hardware::automotive::evs::V1_1::IEvsCamera follow. |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 49 | Return<void> getCameraInfo_1_1(getCameraInfo_1_1_cb _hidl_cb) override; |
| 50 | Return<void> getPhysicalCameraInfo(const hidl_string& id, |
| 51 | getPhysicalCameraInfo_cb _hidl_cb) override; |
| 52 | Return<V1_0::EvsResult> pauseVideoStream() override; |
| 53 | Return<V1_0::EvsResult> resumeVideoStream() override; |
| 54 | Return<V1_0::EvsResult> doneWithFrame_1_1(const hidl_vec<BufferDesc>& buffer) override; |
| 55 | Return<V1_0::EvsResult> setMaster() override; |
| 56 | Return<V1_0::EvsResult> forceMaster(const sp<V1_0::IEvsDisplay>& display) override; |
| 57 | Return<V1_0::EvsResult> unsetMaster() override; |
| 58 | Return<void> getParameterList(getParameterList_cb _hidl_cb) override; |
| 59 | Return<void> getIntParameterRange(CameraParam id, getIntParameterRange_cb _hidl_cb) override; |
| 60 | Return<void> setIntParameter(CameraParam id, int32_t value, |
| 61 | setIntParameter_cb _hidl_cb) override; |
| 62 | Return<void> getIntParameter(CameraParam id, getIntParameter_cb _hidl_cb) override; |
| 63 | Return<V1_0::EvsResult> setExtendedInfo_1_1(uint32_t opaqueIdentifier, |
| 64 | const hidl_vec<uint8_t>& opaqueValue) override; |
| 65 | Return<void> getExtendedInfo_1_1(uint32_t opaqueIdentifier, |
| 66 | getExtendedInfo_1_1_cb _hidl_cb) override; |
| 67 | Return<void> importExternalBuffers(const hidl_vec<BufferDesc>& buffers, |
| 68 | importExternalBuffers_cb _hidl_cb) override; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 69 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 70 | static sp<EvsCamera> Create(const char* deviceName); |
| 71 | static sp<EvsCamera> Create(const char* deviceName, |
| 72 | std::unique_ptr<ConfigManager::CameraInfo>& camInfo, |
| 73 | const Stream* streamCfg = nullptr); |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 74 | EvsCamera(const EvsCamera&) = delete; |
| 75 | EvsCamera& operator=(const EvsCamera&) = delete; |
| 76 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 77 | virtual ~EvsCamera() override; |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 78 | void forceShutdown(); // This gets called if another caller "steals" ownership of the camera |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 79 | |
| 80 | const CameraDesc& getDesc() { return mDescription; }; |
| 81 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 82 | private: |
| 83 | EvsCamera(const char* id, std::unique_ptr<ConfigManager::CameraInfo>& camInfo); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 84 | // These three functions are expected to be called while mAccessLock is held |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 85 | // |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 86 | bool setAvailableFrames_Locked(unsigned bufferCount); |
| 87 | unsigned increaseAvailableFrames_Locked(unsigned numToAdd); |
| 88 | unsigned decreaseAvailableFrames_Locked(unsigned numToRemove); |
| 89 | |
| 90 | void generateFrames(); |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 91 | void fillTestFrame(const V1_0::BufferDesc& buff); |
| 92 | void fillTestFrame(const BufferDesc& buff); |
| 93 | void returnBufferLocked(const uint32_t bufferId, const buffer_handle_t memHandle); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 94 | |
| 95 | sp<EvsEnumerator> mEnumerator; // The enumerator object that created this camera |
| 96 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 97 | CameraDesc mDescription = {}; // The properties of this camera |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 98 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 99 | std::thread mCaptureThread; // The thread we'll use to synthesize frames |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 100 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 101 | uint32_t mWidth = 0; // Horizontal pixel count in the buffers |
| 102 | uint32_t mHeight = 0; // Vertical pixel count in the buffers |
| 103 | uint32_t mFormat = 0; // Values from android_pixel_format_t |
| 104 | uint64_t mUsage = 0; // Values from from Gralloc.h |
| 105 | uint32_t mStride = 0; // Bytes per line in the buffers |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 106 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 107 | sp<IEvsCameraStream> mStream = nullptr; // The callback used to deliver each frame |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 108 | |
| 109 | struct BufferRecord { |
| 110 | buffer_handle_t handle; |
| 111 | bool inUse; |
| 112 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 113 | explicit BufferRecord(buffer_handle_t h) : handle(h), inUse(false){}; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 116 | std::vector<BufferRecord> mBuffers; // Graphics buffers to transfer images |
| 117 | unsigned mFramesAllowed; // How many buffers are we currently using |
| 118 | unsigned mFramesInUse; // How many buffers are currently outstanding |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 119 | |
| 120 | enum StreamStateValues { |
| 121 | STOPPED, |
| 122 | RUNNING, |
| 123 | STOPPING, |
| 124 | DEAD, |
| 125 | }; |
| 126 | StreamStateValues mStreamState; |
| 127 | |
| 128 | // Synchronization necessary to deconflict mCaptureThread from the main service thread |
| 129 | std::mutex mAccessLock; |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 130 | |
| 131 | // Static camera module information |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 132 | std::unique_ptr<ConfigManager::CameraInfo>& mCameraInfo; |
| 133 | |
| 134 | // For the extended info |
| 135 | std::unordered_map<uint32_t, hidl_vec<uint8_t>> mExtInfo; |
| 136 | std::unordered_map<CameraParam, int32_t> mParams; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 139 | } // namespace android::hardware::automotive::evs::V1_1::implementation |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 140 | |
| 141 | #endif // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_EVSCAMERA_H |