blob: 1db8d40cf4f63a80a99cd2acf42e99e4bd7f14ab [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
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070019#ifndef V4L2_CAMERA_HAL_V4L2_CAMERA_H_
20#define V4L2_CAMERA_HAL_V4L2_CAMERA_H_
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070021
Ari Hausman-Cohen49925842016-06-21 14:07:58 -070022#include <array>
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -070023#include <condition_variable>
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -080024#include <map>
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070025#include <queue>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070026#include <string>
27
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070028#include <camera/CameraMetadata.h>
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -070029#include <utils/StrongPointer.h>
30#include <utils/Thread.h>
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070031
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070032#include "camera.h"
33#include "common.h"
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070034#include "metadata/metadata.h"
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070035#include "v4l2_wrapper.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070036
37namespace v4l2_camera_hal {
38// V4L2Camera is a specific V4L2-supported camera device. The Camera object
39// contains all logic common between all cameras (e.g. front and back cameras),
40// while a specific camera device (e.g. V4L2Camera) holds all specific
41// metadata and logic about that device.
42class V4L2Camera : public default_camera_hal::Camera {
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -070043 public:
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070044 // Use this method to create V4L2Camera objects. Functionally equivalent
45 // to "new V4L2Camera", except that it may return nullptr in case of failure.
46 static V4L2Camera* NewV4L2Camera(int id, const std::string path);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070047 ~V4L2Camera();
48
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -070049 private:
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070050 // Constructor private to allow failing on bad input.
51 // Use NewV4L2Camera instead.
Ari Hausman-Cohen9430ad92016-08-24 14:00:32 -070052 V4L2Camera(int id,
53 std::shared_ptr<V4L2Wrapper> v4l2_wrapper,
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070054 std::unique_ptr<Metadata> metadata);
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070055
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070056 // default_camera_hal::Camera virtual methods.
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070057 // Connect to the device: open dev nodes, etc.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070058 int connect() override;
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070059 // Disconnect from the device: close dev nodes, etc.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070060 void disconnect() override;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070061 // Initialize static camera characteristics for individual device.
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070062 int initStaticInfo(android::CameraMetadata* out) override;
63 // Initialize a template of the given type.
64 int initTemplate(int type, android::CameraMetadata* out) override;
65 // Initialize device info: resource cost and conflicting devices
66 // (/conflicting devices length).
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070067 void initDeviceInfo(camera_info_t* info) override;
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070068 // Extra initialization of device when opened.
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070069 int initDevice() override;
Ari Hausman-Cohenef523102016-11-21 17:02:01 -080070 // Verify stream configuration dataspaces and rotation values
71 bool validateDataspacesAndRotations(
72 const camera3_stream_configuration_t* stream_config) override;
73 // Set up the streams, including seting usage & max_buffers
74 int setupStreams(camera3_stream_configuration_t* stream_config) override;
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070075 // Verify settings are valid for a capture or reprocessing.
Ari Hausman-Cohenef523102016-11-21 17:02:01 -080076 bool isValidRequestSettings(const android::CameraMetadata& settings) override;
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070077 // Enqueue a request to receive data from the camera.
78 int enqueueRequest(
79 std::shared_ptr<default_camera_hal::CaptureRequest> request) override;
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -080080 // Flush in flight buffers.
81 int flushBuffers() override;
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070082
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -070083 // Async request processing helpers.
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070084 // Dequeue a request from the waiting queue.
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -070085 // Blocks until a request is available.
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070086 std::shared_ptr<default_camera_hal::CaptureRequest> dequeueRequest();
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -070087
88 // Thread functions. Return true to loop, false to exit.
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070089 // Pass buffers for enqueued requests to the device.
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -070090 bool enqueueRequestBuffers();
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070091 // Retreive buffers from the device.
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -070092 bool dequeueRequestBuffers();
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070093
Ari Hausman-Cohen660f8b82016-07-19 17:27:52 -070094 // V4L2 helper.
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070095 std::shared_ptr<V4L2Wrapper> device_;
96 std::unique_ptr<V4L2Wrapper::Connection> connection_;
97 std::unique_ptr<Metadata> metadata_;
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070098 std::mutex request_queue_lock_;
99 std::queue<std::shared_ptr<default_camera_hal::CaptureRequest>>
100 request_queue_;
101 std::mutex in_flight_lock_;
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800102 // Maps buffer index : request.
103 std::map<uint32_t, std::shared_ptr<default_camera_hal::CaptureRequest>>
104 in_flight_;
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700105 // Threads require holding an Android strong pointer.
106 android::sp<android::Thread> buffer_enqueuer_;
107 android::sp<android::Thread> buffer_dequeuer_;
108 std::condition_variable requests_available_;
109 std::condition_variable buffers_in_flight_;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700110
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700111 int32_t max_input_streams_;
112 std::array<int, 3> max_output_streams_; // {raw, non-stalling, stalling}.
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700113
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700114 DISALLOW_COPY_AND_ASSIGN(V4L2Camera);
115};
116
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700117} // namespace v4l2_camera_hal
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700118
Ari Hausman-Cohen71cb8742016-09-22 11:12:00 -0700119#endif // V4L2_CAMERA_HAL_V4L2_CAMERA_H_