1/*
2 * Copyright (C) 2012 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#ifndef CAMERA_H_
18#define CAMERA_H_
19
20#include <hardware/hardware.h>
21#include <hardware/camera3.h>
22#include <utils/Mutex.h>
23#include "Metadata.h"
24#include "Stream.h"
25
26namespace default_camera_hal {
27// Camera represents a physical camera on a device.
28// This is constructed when the HAL module is loaded, one per physical camera.
29// It is opened by the framework, and must be closed before it can be opened
30// again.
31// This is an abstract class, containing all logic and data shared between all
32// camera devices (front, back, etc) and common to the ISP.
33class Camera {
34    public:
35        // id is used to distinguish cameras. 0 <= id < NUM_CAMERAS.
36        // module is a handle to the HAL module, used when the device is opened.
37        Camera(int id);
38        virtual ~Camera();
39
40        // Common Camera Device Operations (see <hardware/camera_common.h>)
41        int open(const hw_module_t *module, hw_device_t **device);
42        int getInfo(struct camera_info *info);
43        int close();
44
45        // Camera v3 Device Operations (see <hardware/camera3.h>)
46        int initialize(const camera3_callback_ops_t *callback_ops);
47        int configureStreams(camera3_stream_configuration_t *stream_list);
48        int registerStreamBuffers(const camera3_stream_buffer_set_t *buf_set);
49        const camera_metadata_t *constructDefaultRequestSettings(int type);
50        int processCaptureRequest(camera3_capture_request_t *request);
51        void dump(int fd);
52
53
54    protected:
55        // Initialize static camera characteristics for individual device
56        virtual camera_metadata_t *initStaticInfo() = 0;
57        // Verify settings are valid for a capture
58        virtual bool isValidCaptureSettings(const camera_metadata_t *) = 0;
59        // Separate initialization method for individual devices when opened
60        virtual int initDevice() = 0;
61        // Accessor used by initDevice() to set the templates' metadata
62        int setTemplate(int type, camera_metadata_t *static_info);
63        // Prettyprint template names
64        const char* templateToString(int type);
65
66    private:
67        // Camera device handle returned to framework for use
68        camera3_device_t mDevice;
69        // Reuse a stream already created by this device
70        Stream *reuseStream(camera3_stream_t *astream);
71        // Destroy all streams in a stream array, and the array itself
72        void destroyStreams(Stream **array, int count);
73        // Verify a set of streams is valid in aggregate
74        bool isValidStreamSet(Stream **array, int count);
75        // Calculate usage and max_bufs of each stream
76        void setupStreams(Stream **array, int count);
77        // Copy new settings for re-use and clean up old settings.
78        void setSettings(const camera_metadata_t *new_settings);
79        // Verify settings are valid for reprocessing an input buffer
80        bool isValidReprocessSettings(const camera_metadata_t *settings);
81        // Process an output buffer
82        int processCaptureBuffer(const camera3_stream_buffer_t *in,
83                camera3_stream_buffer_t *out);
84        // Send a shutter notify message with start of exposure time
85        void notifyShutter(uint32_t frame_number, uint64_t timestamp);
86        // Is type a valid template type (and valid index into mTemplates)
87        bool isValidTemplateType(int type);
88
89        // Identifier used by framework to distinguish cameras
90        const int mId;
91        // Metadata containing persistent camera characteristics
92        Metadata mMetadata;
93        // camera_metadata structure containing static characteristics
94        camera_metadata_t *mStaticInfo;
95        // Busy flag indicates camera is in use
96        bool mBusy;
97        // Camera device operations handle shared by all devices
98        const static camera3_device_ops_t sOps;
99        // Methods used to call back into the framework
100        const camera3_callback_ops_t *mCallbackOps;
101        // Lock protecting the Camera object for modifications
102        android::Mutex mDeviceLock;
103        // Lock protecting only static camera characteristics, which may
104        // be accessed without the camera device open
105        android::Mutex mStaticInfoLock;
106        // Array of handles to streams currently in use by the device
107        Stream **mStreams;
108        // Number of streams in mStreams
109        int mNumStreams;
110        // Static array of standard camera settings templates
111        camera_metadata_t *mTemplates[CAMERA3_TEMPLATE_COUNT];
112        // Most recent request settings seen, memoized to be reused
113        camera_metadata_t *mSettings;
114};
115} // namespace default_camera_hal
116
117#endif // CAMERA_H_
118