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