blob: d4b72728d6fe903a7e506a8f6ed0cee4debe0123 [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// Modified from hardware/libhardware/modules/camera/Camera.h
18
19#ifndef CAMERA_H_
20#define CAMERA_H_
21
22#include <hardware/hardware.h>
23#include <hardware/camera3.h>
24#include <utils/Mutex.h>
25#include "Stream.h"
26
27namespace default_camera_hal {
28// Camera represents a physical camera on a device.
29// This is constructed when the HAL module is loaded, one per physical camera.
30// TODO(b/29185945): Support hotplugging.
31// It is opened by the framework, and must be closed before it can be opened
32// again.
33// This is an abstract class, containing all logic and data shared between all
34// camera devices (front, back, etc) and common to the ISP.
35class Camera {
36 public:
37 // id is used to distinguish cameras. 0 <= id < NUM_CAMERAS.
38 // module is a handle to the HAL module, used when the device is opened.
39 Camera(int id);
40 virtual ~Camera();
41
42 // Common Camera Device Operations (see <hardware/camera_common.h>)
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070043 int openDevice(const hw_module_t *module, hw_device_t **device);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070044 int getInfo(struct camera_info *info);
45 int close();
46
47 // Camera v3 Device Operations (see <hardware/camera3.h>)
48 int initialize(const camera3_callback_ops_t *callback_ops);
49 int configureStreams(camera3_stream_configuration_t *stream_list);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070050 const camera_metadata_t *constructDefaultRequestSettings(int type);
51 int processCaptureRequest(camera3_capture_request_t *request);
52 void dump(int fd);
53
54
55 protected:
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070056 // Connect to the device: open dev nodes, etc.
57 virtual int connect() = 0;
58 // Disconnect from the device: close dev nodes, etc.
59 virtual void disconnect() = 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070060 // Initialize static camera characteristics for individual device
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070061 virtual int initStaticInfo(camera_metadata_t **out) = 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070062 // Initialize device info: facing, orientation, resource cost,
63 // and conflicting devices (/conflicting devices length)
64 virtual void initDeviceInfo(struct camera_info *info) = 0;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070065 // Verify stream configuration is device-compatible
66 virtual bool isSupportedStreamSet(Stream** streams, int count,
67 uint32_t mode) = 0;
68 // Set up the device for a stream, and get the maximum number of
69 // buffers that stream can handle (max_buffers is an output parameter)
70 virtual int setupStream(Stream* stream, uint32_t* max_buffers) = 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070071 // Verify settings are valid for a capture
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -070072 virtual bool isValidCaptureSettings(
73 const camera_metadata_t *settings) = 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070074 // Separate initialization method for individual devices when opened
75 virtual int initDevice() = 0;
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -070076 // Enqueue a buffer to receive data from the camera
77 virtual int enqueueBuffer(
78 const camera3_stream_buffer_t *camera_buffer) = 0;
79 // Get the shutter time and updated settings for the most recent frame.
80 // The metadata parameter is both an input and output; frame-specific
81 // result fields should be appended to what is passed in.
82 virtual int getResultSettings(camera_metadata_t **metadata,
83 uint64_t *timestamp) = 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070084 // Accessor used by initDevice() to set the templates' metadata
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070085 int setTemplate(int type, const camera_metadata_t *static_info);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070086 // Prettyprint template names
87 const char* templateToString(int type);
88
89 private:
90 // Camera device handle returned to framework for use
91 camera3_device_t mDevice;
92 // Reuse a stream already created by this device
93 Stream *reuseStream(camera3_stream_t *astream);
94 // Destroy all streams in a stream array, and the array itself
95 void destroyStreams(Stream **array, int count);
96 // Verify a set of streams is valid in aggregate
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070097 bool isValidStreamSet(Stream **array, int count, uint32_t mode);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070098 // Calculate usage and max_bufs of each stream
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070099 int setupStreams(Stream **array, int count);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700100 // Copy new settings for re-use and clean up old settings.
101 void setSettings(const camera_metadata_t *new_settings);
102 // Verify settings are valid for reprocessing an input buffer
103 bool isValidReprocessSettings(const camera_metadata_t *settings);
104 // Process an output buffer
105 int processCaptureBuffer(const camera3_stream_buffer_t *in,
106 camera3_stream_buffer_t *out);
107 // Send a shutter notify message with start of exposure time
108 void notifyShutter(uint32_t frame_number, uint64_t timestamp);
109 // Is type a valid template type (and valid index into mTemplates)
110 bool isValidTemplateType(int type);
111
112 // Identifier used by framework to distinguish cameras
113 const int mId;
114 // camera_metadata structure containing static characteristics
115 camera_metadata_t *mStaticInfo;
116 // Busy flag indicates camera is in use
117 bool mBusy;
118 // Camera device operations handle shared by all devices
119 const static camera3_device_ops_t sOps;
120 // Methods used to call back into the framework
121 const camera3_callback_ops_t *mCallbackOps;
122 // Lock protecting the Camera object for modifications
123 android::Mutex mDeviceLock;
124 // Lock protecting only static camera characteristics, which may
125 // be accessed without the camera device open
126 android::Mutex mStaticInfoLock;
127 // Array of handles to streams currently in use by the device
128 Stream **mStreams;
129 // Number of streams in mStreams
130 int mNumStreams;
131 // Static array of standard camera settings templates
132 camera_metadata_t *mTemplates[CAMERA3_TEMPLATE_COUNT];
133 // Most recent request settings seen, memoized to be reused
134 camera_metadata_t *mSettings;
135};
136} // namespace default_camera_hal
137
138#endif // CAMERA_H_