Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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_0_EVSCAMERA_H |
| 18 | #define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSCAMERA_H |
| 19 | |
| 20 | #include <android/hardware/automotive/evs/1.0/types.h> |
| 21 | #include <android/hardware/automotive/evs/1.0/IEvsCamera.h> |
| 22 | #include <ui/GraphicBuffer.h> |
| 23 | |
| 24 | #include <thread> |
| 25 | |
| 26 | |
| 27 | namespace android { |
| 28 | namespace hardware { |
| 29 | namespace automotive { |
| 30 | namespace evs { |
| 31 | namespace V1_0 { |
| 32 | namespace implementation { |
| 33 | |
| 34 | |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 35 | // From EvsEnumerator.h |
| 36 | class EvsEnumerator; |
| 37 | |
| 38 | |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 39 | class EvsCamera : public IEvsCamera { |
| 40 | public: |
| 41 | // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCamera follow. |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 42 | Return<void> getCameraInfo(getCameraInfo_cb _hidl_cb) override; |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 43 | Return <EvsResult> setMaxFramesInFlight(uint32_t bufferCount) override; |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 44 | Return <EvsResult> startVideoStream(const ::android::sp<IEvsCameraStream>& stream) override; |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 45 | Return<void> doneWithFrame(const BufferDesc& buffer) override; |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 46 | Return<void> stopVideoStream() override; |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 47 | Return <int32_t> getExtendedInfo(uint32_t opaqueIdentifier) override; |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 48 | Return <EvsResult> setExtendedInfo(uint32_t opaqueIdentifier, int32_t opaqueValue) override; |
| 49 | |
| 50 | // Implementation details |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 51 | EvsCamera(const char *id); |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 52 | virtual ~EvsCamera() override; |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 53 | void forceShutdown(); // This gets called if another caller "steals" ownership of the camera |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 54 | |
| 55 | const CameraDesc& getDesc() { return mDescription; }; |
| 56 | |
| 57 | static const char kCameraName_Backup[]; |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | // These three functions are expected to be called while mAccessLock is held |
| 61 | bool setAvailableFrames_Locked(unsigned bufferCount); |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 62 | unsigned increaseAvailableFrames_Locked(unsigned numToAdd); |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 63 | unsigned decreaseAvailableFrames_Locked(unsigned numToRemove); |
| 64 | |
| 65 | void generateFrames(); |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 66 | void fillTestFrame(const BufferDesc& buff); |
| 67 | |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 68 | sp<EvsEnumerator> mEnumerator; // The enumerator object that created this camera |
| 69 | |
| 70 | CameraDesc mDescription = {}; // The properties of this camera |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 71 | |
| 72 | std::thread mCaptureThread; // The thread we'll use to synthesize frames |
| 73 | |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 74 | uint32_t mWidth = 0; // Horizontal pixel count in the buffers |
| 75 | uint32_t mHeight = 0; // Vertical pixel count in the buffers |
| 76 | uint32_t mFormat = 0; // Values from android_pixel_format_t [TODO: YUV? Leave opaque?] |
| 77 | uint32_t mUsage = 0; // Values from from Gralloc.h |
| 78 | uint32_t mStride = 0; // Bytes per line in the buffers |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 79 | |
| 80 | sp <IEvsCameraStream> mStream = nullptr; // The callback used to deliver each frame |
| 81 | |
| 82 | struct BufferRecord { |
| 83 | buffer_handle_t handle; |
| 84 | bool inUse; |
| 85 | |
| 86 | explicit BufferRecord(buffer_handle_t h) : handle(h), inUse(false) {}; |
| 87 | }; |
| 88 | |
| 89 | std::vector <BufferRecord> mBuffers; // Graphics buffers to transfer images |
| 90 | unsigned mFramesAllowed; // How many buffers are we currently using |
| 91 | unsigned mFramesInUse; // How many buffers are currently outstanding |
| 92 | |
| 93 | enum StreamStateValues { |
| 94 | STOPPED, |
| 95 | RUNNING, |
| 96 | STOPPING, |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 97 | DEAD, |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 98 | }; |
| 99 | StreamStateValues mStreamState; |
| 100 | |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 101 | // Synchronization necessary to deconflict mCaptureThread from the main service thread |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 102 | std::mutex mAccessLock; |
| 103 | }; |
| 104 | |
| 105 | } // namespace implementation |
| 106 | } // namespace V1_0 |
| 107 | } // namespace evs |
| 108 | } // namespace automotive |
| 109 | } // namespace hardware |
| 110 | } // namespace android |
| 111 | |
| 112 | #endif // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSCAMERA_H |