Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [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 | // Loosely based on hardware/libhardware/modules/camera/ExampleCamera.h |
| 18 | |
| 19 | #ifndef V4L2_CAMERA_H |
| 20 | #define V4L2_CAMERA_H |
| 21 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 22 | #include <array> |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 23 | #include <string> |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 24 | #include <vector> |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 25 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 26 | #include <nativehelper/ScopedFd.h> |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 27 | #include <system/camera_metadata.h> |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 28 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 29 | #include "ArrayVector.h" |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 30 | #include "Camera.h" |
| 31 | #include "Common.h" |
Ari Hausman-Cohen | 3eece6f | 2016-07-21 11:08:24 -0700 | [diff] [blame] | 32 | #include "V4L2Gralloc.h" |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 33 | |
| 34 | namespace v4l2_camera_hal { |
| 35 | // V4L2Camera is a specific V4L2-supported camera device. The Camera object |
| 36 | // contains all logic common between all cameras (e.g. front and back cameras), |
| 37 | // while a specific camera device (e.g. V4L2Camera) holds all specific |
| 38 | // metadata and logic about that device. |
| 39 | class V4L2Camera : public default_camera_hal::Camera { |
| 40 | public: |
Ari Hausman-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame^] | 41 | // Use this method to create V4L2Camera objects. Functionally equivalent |
| 42 | // to "new V4L2Camera", except that it may return nullptr in case of failure. |
| 43 | static V4L2Camera* NewV4L2Camera(int id, const std::string path); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 44 | ~V4L2Camera(); |
| 45 | |
| 46 | private: |
Ari Hausman-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame^] | 47 | // Constructor private to allow failing on bad input. |
| 48 | // Use NewV4L2Camera instead. |
| 49 | V4L2Camera(int id, const std::string path, |
| 50 | std::unique_ptr<V4L2Gralloc> gralloc); |
| 51 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 52 | // default_camera_hal::Camera virtual methods. |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 53 | // Connect to the device: open dev nodes, etc. |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 54 | int connect() override; |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 55 | // Disconnect from the device: close dev nodes, etc. |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 56 | void disconnect() override; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 57 | // Initialize static camera characteristics for individual device. |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 58 | int initStaticInfo(camera_metadata_t** out) override; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 59 | // Initialize device info: facing, orientation, resource cost, |
| 60 | // and conflicting devices (/conflicting devices length). |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 61 | void initDeviceInfo(camera_info_t* info) override; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 62 | // Initialize whole device (templates/etc) when opened. |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 63 | int initDevice() override; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 64 | // Verify stream configuration is device-compatible. |
| 65 | bool isSupportedStreamSet(default_camera_hal::Stream** streams, |
| 66 | int count, uint32_t mode) override; |
| 67 | // Set up the device for a stream, and get the maximum number of |
| 68 | // buffers that stream can handle (max_buffers is an output parameter). |
| 69 | int setupStream(default_camera_hal::Stream* stream, |
| 70 | uint32_t* max_buffers) override; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 71 | // Verify settings are valid for a capture with this device. |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 72 | bool isValidCaptureSettings(const camera_metadata_t* settings) override; |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 73 | // Enqueue a buffer to receive data from the camera. |
| 74 | int enqueueBuffer(const camera3_stream_buffer_t* camera_buffer) override; |
| 75 | // Get the shutter time and updated settings for the most recent frame. |
| 76 | // The metadata parameter is both an input and output; frame-specific |
| 77 | // result fields should be appended to what is passed in. |
| 78 | int getResultSettings(camera_metadata_t** metadata, uint64_t* timestamp); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 79 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 80 | // Helper functions for V4L2 functionality. |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 81 | int streamOn(); |
| 82 | int streamOff(); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 83 | int setFormat(const default_camera_hal::Stream* stream); |
| 84 | int setupBuffers(); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 85 | int dequeueBuffer(v4l2_buffer* buffer); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 86 | |
| 87 | // HAL <-> V4L2 conversions. |
| 88 | uint32_t halToV4L2PixelFormat(int hal_format); // 0 for unrecognized. |
| 89 | int V4L2ToHalPixelFormat(uint32_t v4l2_format); // -1 for unrecognized. |
| 90 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 91 | // The camera device path. For example, /dev/video0. |
| 92 | const std::string mDevicePath; |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 93 | // The opened device fd. |
| 94 | ScopedFd mDeviceFd; |
Ari Hausman-Cohen | 3eece6f | 2016-07-21 11:08:24 -0700 | [diff] [blame] | 95 | // Gralloc helper. |
Ari Hausman-Cohen | 681eaa2 | 2016-07-21 16:28:17 -0700 | [diff] [blame^] | 96 | std::unique_ptr<V4L2Gralloc> mGralloc; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 97 | // Lock protecting use of the device. |
| 98 | android::Mutex mDeviceLock; |
| 99 | // Wrapper around ioctl. |
| 100 | template<typename T> |
| 101 | int ioctlLocked(int request, T data); |
| 102 | |
| 103 | // Current output stream settings. |
Ari Hausman-Cohen | 44d8432 | 2016-07-11 11:08:26 -0700 | [diff] [blame] | 104 | uint32_t mOutStreamType; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 105 | uint32_t mOutStreamFormat; |
| 106 | uint32_t mOutStreamWidth; |
| 107 | uint32_t mOutStreamHeight; |
Ari Hausman-Cohen | 3eece6f | 2016-07-21 11:08:24 -0700 | [diff] [blame] | 108 | uint32_t mOutStreamBytesPerLine; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 109 | uint32_t mOutStreamMaxBuffers; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 110 | |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 111 | bool mTemplatesInitialized; |
| 112 | int initTemplates(); |
| 113 | |
| 114 | // Camera characteristics. |
| 115 | bool mCharacteristicsInitialized; // If false, characteristics are invalid. |
| 116 | // Fixed characteristics. |
| 117 | float mAperture; |
| 118 | float mFilterDensity; |
| 119 | float mFocalLength; |
| 120 | int32_t mOrientation; |
| 121 | std::array<float, 2> mPhysicalSize; // {width, height}, in mm. |
| 122 | std::array<int32_t, 4> mPixelArraySize; // {xmin, ymin, width, height}. |
| 123 | uint8_t mCropType; |
| 124 | float mMaxZoom; |
| 125 | std::array<int32_t, 2> mAeCompensationRange; // {min, max}. |
| 126 | camera_metadata_rational mAeCompensationStep; |
| 127 | uint8_t mAeLockAvailable; |
| 128 | uint8_t mAwbLockAvailable; |
| 129 | uint8_t mFlashAvailable; |
| 130 | float mFocusDistance; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 131 | int32_t mMaxRawOutputStreams; |
| 132 | int32_t mMaxYuvOutputStreams; |
| 133 | int32_t mMaxJpegOutputStreams; |
| 134 | int32_t mMaxInputStreams; |
| 135 | std::vector<int32_t> mSupportedFormats; |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 136 | // Variable characteristics available options. |
| 137 | std::vector<uint8_t> mAeModes; |
| 138 | std::vector<uint8_t> mAeAntibandingModes; |
| 139 | std::vector<uint8_t> mAfModes; |
| 140 | std::vector<uint8_t> mAwbModes; |
| 141 | std::vector<uint8_t> mSceneModes; |
| 142 | std::vector<uint8_t> mControlModes; |
| 143 | std::vector<uint8_t> mEffects; |
| 144 | std::vector<uint8_t> mLeds; |
| 145 | std::vector<uint8_t> mOpticalStabilizationModes; |
| 146 | std::vector<uint8_t> mVideoStabilizationModes; |
| 147 | // {format, width, height, direction} (input or output). |
| 148 | ArrayVector<int32_t, 4> mStreamConfigs; |
| 149 | // {format, width, height, duration} (duration in ns). |
| 150 | ArrayVector<int64_t, 4> mMinFrameDurations; |
| 151 | int64_t mMaxFrameDuration; |
| 152 | // {format, width, height, duration} (duration in ns). |
| 153 | ArrayVector<int64_t, 4> mStallDurations; |
| 154 | // {min, max} (in fps). |
| 155 | ArrayVector<int32_t, 2> mFpsRanges; |
| 156 | |
| 157 | // Initialize characteristics and set mCharacteristicsInitialized to True. |
| 158 | int initCharacteristics(); |
| 159 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 160 | DISALLOW_COPY_AND_ASSIGN(V4L2Camera); |
| 161 | }; |
| 162 | |
| 163 | } // namespace v4l2_camera_hal |
| 164 | |
| 165 | #endif // V4L2_CAMERA_H |