blob: 1fabde942f87631e17daeee98e95bb50207537e7 [file] [log] [blame]
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001/*
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-Cohen49925842016-06-21 14:07:58 -070022#include <array>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070023#include <string>
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070024#include <vector>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070025
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070026#include <nativehelper/ScopedFd.h>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070027#include <system/camera_metadata.h>
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070028
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070029#include "ArrayVector.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070030#include "Camera.h"
31#include "Common.h"
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070032#include "V4L2Gralloc.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070033
34namespace 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.
39class V4L2Camera : public default_camera_hal::Camera {
40public:
41 V4L2Camera(int id, const std::string path);
42 ~V4L2Camera();
43
44private:
45 // default_camera_hal::Camera virtual methods.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070046 // Connect to the device: open dev nodes, etc.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070047 int connect() override;
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070048 // Disconnect from the device: close dev nodes, etc.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070049 void disconnect() override;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070050 // Initialize static camera characteristics for individual device.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070051 int initStaticInfo(camera_metadata_t** out) override;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070052 // Initialize device info: facing, orientation, resource cost,
53 // and conflicting devices (/conflicting devices length).
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070054 void initDeviceInfo(camera_info_t* info) override;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070055 // Initialize whole device (templates/etc) when opened.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070056 int initDevice() override;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070057 // Verify stream configuration is device-compatible.
58 bool isSupportedStreamSet(default_camera_hal::Stream** streams,
59 int count, uint32_t mode) override;
60 // Set up the device for a stream, and get the maximum number of
61 // buffers that stream can handle (max_buffers is an output parameter).
62 int setupStream(default_camera_hal::Stream* stream,
63 uint32_t* max_buffers) override;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070064 // Verify settings are valid for a capture with this device.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070065 bool isValidCaptureSettings(const camera_metadata_t* settings) override;
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -070066 // Enqueue a buffer to receive data from the camera.
67 int enqueueBuffer(const camera3_stream_buffer_t* camera_buffer) override;
68 // Get the shutter time and updated settings for the most recent frame.
69 // The metadata parameter is both an input and output; frame-specific
70 // result fields should be appended to what is passed in.
71 int getResultSettings(camera_metadata_t** metadata, uint64_t* timestamp);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070072
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070073 // Helper functions for V4L2 functionality.
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -070074 int streamOn();
75 int streamOff();
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070076 int setFormat(const default_camera_hal::Stream* stream);
77 int setupBuffers();
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -070078 int dequeueBuffer(v4l2_buffer* buffer);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070079
80 // HAL <-> V4L2 conversions.
81 uint32_t halToV4L2PixelFormat(int hal_format); // 0 for unrecognized.
82 int V4L2ToHalPixelFormat(uint32_t v4l2_format); // -1 for unrecognized.
83
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070084 // The camera device path. For example, /dev/video0.
85 const std::string mDevicePath;
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070086 // The opened device fd.
87 ScopedFd mDeviceFd;
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070088 // Gralloc helper.
89 V4L2Gralloc mGralloc;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070090 // Lock protecting use of the device.
91 android::Mutex mDeviceLock;
92 // Wrapper around ioctl.
93 template<typename T>
94 int ioctlLocked(int request, T data);
95
96 // Current output stream settings.
Ari Hausman-Cohen44d84322016-07-11 11:08:26 -070097 uint32_t mOutStreamType;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070098 uint32_t mOutStreamFormat;
99 uint32_t mOutStreamWidth;
100 uint32_t mOutStreamHeight;
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -0700101 uint32_t mOutStreamBytesPerLine;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700102 uint32_t mOutStreamMaxBuffers;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700103
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700104 bool mTemplatesInitialized;
105 int initTemplates();
106
107 // Camera characteristics.
108 bool mCharacteristicsInitialized; // If false, characteristics are invalid.
109 // Fixed characteristics.
110 float mAperture;
111 float mFilterDensity;
112 float mFocalLength;
113 int32_t mOrientation;
114 std::array<float, 2> mPhysicalSize; // {width, height}, in mm.
115 std::array<int32_t, 4> mPixelArraySize; // {xmin, ymin, width, height}.
116 uint8_t mCropType;
117 float mMaxZoom;
118 std::array<int32_t, 2> mAeCompensationRange; // {min, max}.
119 camera_metadata_rational mAeCompensationStep;
120 uint8_t mAeLockAvailable;
121 uint8_t mAwbLockAvailable;
122 uint8_t mFlashAvailable;
123 float mFocusDistance;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700124 int32_t mMaxRawOutputStreams;
125 int32_t mMaxYuvOutputStreams;
126 int32_t mMaxJpegOutputStreams;
127 int32_t mMaxInputStreams;
128 std::vector<int32_t> mSupportedFormats;
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700129 // Variable characteristics available options.
130 std::vector<uint8_t> mAeModes;
131 std::vector<uint8_t> mAeAntibandingModes;
132 std::vector<uint8_t> mAfModes;
133 std::vector<uint8_t> mAwbModes;
134 std::vector<uint8_t> mSceneModes;
135 std::vector<uint8_t> mControlModes;
136 std::vector<uint8_t> mEffects;
137 std::vector<uint8_t> mLeds;
138 std::vector<uint8_t> mOpticalStabilizationModes;
139 std::vector<uint8_t> mVideoStabilizationModes;
140 // {format, width, height, direction} (input or output).
141 ArrayVector<int32_t, 4> mStreamConfigs;
142 // {format, width, height, duration} (duration in ns).
143 ArrayVector<int64_t, 4> mMinFrameDurations;
144 int64_t mMaxFrameDuration;
145 // {format, width, height, duration} (duration in ns).
146 ArrayVector<int64_t, 4> mStallDurations;
147 // {min, max} (in fps).
148 ArrayVector<int32_t, 2> mFpsRanges;
149
150 // Initialize characteristics and set mCharacteristicsInitialized to True.
151 int initCharacteristics();
152
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700153 DISALLOW_COPY_AND_ASSIGN(V4L2Camera);
154};
155
156} // namespace v4l2_camera_hal
157
158#endif // V4L2_CAMERA_H