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