blob: 255156fdd3b467e928d07cc3b9f16f586fee3657 [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
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070019#ifndef DEFAULT_CAMERA_HAL_CAMERA_H_
20#define DEFAULT_CAMERA_HAL_CAMERA_H_
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070021
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070022#include <camera/CameraMetadata.h>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070023#include <hardware/hardware.h>
24#include <hardware/camera3.h>
25#include <utils/Mutex.h>
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070026
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070027#include "capture_request.h"
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070028#include "metadata/metadata.h"
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070029#include "stream.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070030
31namespace default_camera_hal {
32// Camera represents a physical camera on a device.
33// This is constructed when the HAL module is loaded, one per physical camera.
34// TODO(b/29185945): Support hotplugging.
35// It is opened by the framework, and must be closed before it can be opened
36// again.
37// This is an abstract class, containing all logic and data shared between all
38// camera devices (front, back, etc) and common to the ISP.
39class Camera {
40 public:
41 // id is used to distinguish cameras. 0 <= id < NUM_CAMERAS.
42 // module is a handle to the HAL module, used when the device is opened.
43 Camera(int id);
44 virtual ~Camera();
45
46 // Common Camera Device Operations (see <hardware/camera_common.h>)
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070047 int openDevice(const hw_module_t *module, hw_device_t **device);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070048 int getInfo(struct camera_info *info);
49 int close();
50
51 // Camera v3 Device Operations (see <hardware/camera3.h>)
52 int initialize(const camera3_callback_ops_t *callback_ops);
53 int configureStreams(camera3_stream_configuration_t *stream_list);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070054 const camera_metadata_t *constructDefaultRequestSettings(int type);
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070055 int processCaptureRequest(camera3_capture_request_t *temp_request);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070056 void dump(int fd);
57
58
59 protected:
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070060 // Connect to the device: open dev nodes, etc.
61 virtual int connect() = 0;
62 // Disconnect from the device: close dev nodes, etc.
63 virtual void disconnect() = 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070064 // Initialize static camera characteristics for individual device
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070065 virtual int initStaticInfo(android::CameraMetadata* out) = 0;
66 // Initialize a template of the given type
67 virtual int initTemplate(int type, android::CameraMetadata* out) = 0;
68 // Initialize device info: resource cost and conflicting devices
69 // (/conflicting devices length)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070070 virtual void initDeviceInfo(struct camera_info *info) = 0;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -070071 // Verify stream configuration is device-compatible
72 virtual bool isSupportedStreamSet(Stream** streams, int count,
73 uint32_t mode) = 0;
74 // Set up the device for a stream, and get the maximum number of
75 // buffers that stream can handle (max_buffers is an output parameter)
76 virtual int setupStream(Stream* stream, uint32_t* max_buffers) = 0;
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070077 // Verify settings are valid for a capture or reprocessing
78 virtual bool isValidRequest(const CaptureRequest& request) = 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070079 // Separate initialization method for individual devices when opened
80 virtual int initDevice() = 0;
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -070081 // Enqueue a request to receive data from the camera
82 virtual int enqueueRequest(
83 std::shared_ptr<CaptureRequest> request) = 0;
84
85 // Callback for when the device has filled in the requested data.
86 // Fills in the result struct, validates the data, sends appropriate
87 // notifications, and returns the result to the framework.
88 void completeRequest(
89 std::shared_ptr<CaptureRequest> request, int err);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070090 // Prettyprint template names
91 const char* templateToString(int type);
92
93 private:
94 // Camera device handle returned to framework for use
95 camera3_device_t mDevice;
96 // Reuse a stream already created by this device
97 Stream *reuseStream(camera3_stream_t *astream);
98 // Destroy all streams in a stream array, and the array itself
99 void destroyStreams(Stream **array, int count);
100 // Verify a set of streams is valid in aggregate
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700101 bool isValidStreamSet(Stream **array, int count, uint32_t mode);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700102 // Calculate usage and max_bufs of each stream
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700103 int setupStreams(Stream **array, int count);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700104 // Verify settings are valid for reprocessing an input buffer
105 bool isValidReprocessSettings(const camera_metadata_t *settings);
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700106 // Pre-process an output buffer
107 int preprocessCaptureBuffer(camera3_stream_buffer_t *buffer);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700108 // Send a shutter notify message with start of exposure time
109 void notifyShutter(uint32_t frame_number, uint64_t timestamp);
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700110 // Send an error message and return the errored out result.
111 void completeRequestWithError(std::shared_ptr<CaptureRequest> request);
112 // Send a capture result for a request.
113 void sendResult(std::shared_ptr<CaptureRequest> request);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700114 // Is type a valid template type (and valid index into mTemplates)
115 bool isValidTemplateType(int type);
116
117 // Identifier used by framework to distinguish cameras
118 const int mId;
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700119 // CameraMetadata containing static characteristics
120 std::unique_ptr<const android::CameraMetadata> mStaticInfo;
121 // Flag indicating if settings have been set since
122 // the last configure_streams() call.
123 bool mSettingsSet;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700124 // Busy flag indicates camera is in use
125 bool mBusy;
126 // Camera device operations handle shared by all devices
127 const static camera3_device_ops_t sOps;
128 // Methods used to call back into the framework
129 const camera3_callback_ops_t *mCallbackOps;
130 // Lock protecting the Camera object for modifications
131 android::Mutex mDeviceLock;
132 // Lock protecting only static camera characteristics, which may
133 // be accessed without the camera device open
134 android::Mutex mStaticInfoLock;
135 // Array of handles to streams currently in use by the device
136 Stream **mStreams;
137 // Number of streams in mStreams
138 int mNumStreams;
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700139 // Standard camera settings templates
140 std::unique_ptr<const android::CameraMetadata> mTemplates[CAMERA3_TEMPLATE_COUNT];
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700141};
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700142} // namespace default_camera_hal
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700143
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700144#endif // DEFAULT_CAMERA_HAL_CAMERA_H_