1 /*
2  * Copyright (C) 2013 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 //#define LOG_NDEBUG 0
17 #define LOG_TAG "ExampleCamera"
18 
19 #include <stdint.h>
20 
21 #include <log/log.h>
22 
23 #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
24 #include <utils/Trace.h>
25 
26 #include <system/camera_metadata.h>
27 #include "Camera.h"
28 #include "ExampleCamera.h"
29 
30 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
31 
32 namespace default_camera_hal {
33 
ExampleCamera(int id)34 ExampleCamera::ExampleCamera(int id) : Camera(id)
35 {
36 }
37 
~ExampleCamera()38 ExampleCamera::~ExampleCamera()
39 {
40 }
41 
initStaticInfo()42 camera_metadata_t *ExampleCamera::initStaticInfo()
43 {
44     /*
45      * Setup static camera info.  This will have to customized per camera
46      * device.
47      */
48     Metadata m;
49 
50     /* android.control */
51     int32_t android_control_ae_available_target_fps_ranges[] = {30, 30};
52     m.addInt32(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
53             ARRAY_SIZE(android_control_ae_available_target_fps_ranges),
54             android_control_ae_available_target_fps_ranges);
55 
56     int32_t android_control_ae_compensation_range[] = {-4, 4};
57     m.addInt32(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
58             ARRAY_SIZE(android_control_ae_compensation_range),
59             android_control_ae_compensation_range);
60 
61     camera_metadata_rational_t android_control_ae_compensation_step[] = {{2,1}};
62     m.addRational(ANDROID_CONTROL_AE_COMPENSATION_STEP,
63             ARRAY_SIZE(android_control_ae_compensation_step),
64             android_control_ae_compensation_step);
65 
66     int32_t android_control_max_regions[] = {/*AE*/ 1,/*AWB*/ 1,/*AF*/ 1};
67     m.addInt32(ANDROID_CONTROL_MAX_REGIONS,
68             ARRAY_SIZE(android_control_max_regions),
69             android_control_max_regions);
70 
71     /* android.jpeg */
72     int32_t android_jpeg_available_thumbnail_sizes[] = {0, 0, 128, 96};
73     m.addInt32(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
74             ARRAY_SIZE(android_jpeg_available_thumbnail_sizes),
75             android_jpeg_available_thumbnail_sizes);
76 
77     int32_t android_jpeg_max_size[] = {13 * 1024 * 1024}; // 13MB
78     m.addInt32(ANDROID_JPEG_MAX_SIZE,
79             ARRAY_SIZE(android_jpeg_max_size),
80             android_jpeg_max_size);
81 
82     /* android.lens */
83     float android_lens_info_available_focal_lengths[] = {1.0};
84     m.addFloat(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
85             ARRAY_SIZE(android_lens_info_available_focal_lengths),
86             android_lens_info_available_focal_lengths);
87 
88     /* android.request */
89     int32_t android_request_max_num_output_streams[] = {0, 3, 1};
90     m.addInt32(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
91             ARRAY_SIZE(android_request_max_num_output_streams),
92             android_request_max_num_output_streams);
93 
94     /* android.scaler */
95     int32_t android_scaler_available_formats[] = {
96             HAL_PIXEL_FORMAT_RAW16,
97             HAL_PIXEL_FORMAT_BLOB,
98             HAL_PIXEL_FORMAT_RGBA_8888,
99             HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
100             // These are handled by YCbCr_420_888
101             //        HAL_PIXEL_FORMAT_YV12,
102             //        HAL_PIXEL_FORMAT_YCrCb_420_SP,
103             HAL_PIXEL_FORMAT_YCbCr_420_888};
104     m.addInt32(ANDROID_SCALER_AVAILABLE_FORMATS,
105             ARRAY_SIZE(android_scaler_available_formats),
106             android_scaler_available_formats);
107 
108     int64_t android_scaler_available_jpeg_min_durations[] = {1};
109     m.addInt64(ANDROID_SCALER_AVAILABLE_JPEG_MIN_DURATIONS,
110             ARRAY_SIZE(android_scaler_available_jpeg_min_durations),
111             android_scaler_available_jpeg_min_durations);
112 
113     int32_t android_scaler_available_jpeg_sizes[] = {640, 480};
114     m.addInt32(ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
115             ARRAY_SIZE(android_scaler_available_jpeg_sizes),
116             android_scaler_available_jpeg_sizes);
117 
118     float android_scaler_available_max_digital_zoom[] = {1};
119     m.addFloat(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
120             ARRAY_SIZE(android_scaler_available_max_digital_zoom),
121             android_scaler_available_max_digital_zoom);
122 
123     int64_t android_scaler_available_processed_min_durations[] = {1};
124     m.addInt64(ANDROID_SCALER_AVAILABLE_PROCESSED_MIN_DURATIONS,
125             ARRAY_SIZE(android_scaler_available_processed_min_durations),
126             android_scaler_available_processed_min_durations);
127 
128     int32_t android_scaler_available_processed_sizes[] = {640, 480};
129     m.addInt32(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
130             ARRAY_SIZE(android_scaler_available_processed_sizes),
131             android_scaler_available_processed_sizes);
132 
133     int64_t android_scaler_available_raw_min_durations[] = {1};
134     m.addInt64(ANDROID_SCALER_AVAILABLE_RAW_MIN_DURATIONS,
135             ARRAY_SIZE(android_scaler_available_raw_min_durations),
136             android_scaler_available_raw_min_durations);
137 
138     int32_t android_scaler_available_raw_sizes[] = {640, 480};
139     m.addInt32(ANDROID_SCALER_AVAILABLE_RAW_SIZES,
140             ARRAY_SIZE(android_scaler_available_raw_sizes),
141             android_scaler_available_raw_sizes);
142 
143     /* android.sensor */
144 
145     int32_t android_sensor_info_active_array_size[] = {0, 0, 640, 480};
146     m.addInt32(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
147             ARRAY_SIZE(android_sensor_info_active_array_size),
148             android_sensor_info_active_array_size);
149 
150     int32_t android_sensor_info_sensitivity_range[] =
151             {100, 1600};
152     m.addInt32(ANDROID_SENSOR_INFO_SENSITIVITY_RANGE,
153             ARRAY_SIZE(android_sensor_info_sensitivity_range),
154             android_sensor_info_sensitivity_range);
155 
156     int64_t android_sensor_info_max_frame_duration[] = {30000000000};
157     m.addInt64(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
158             ARRAY_SIZE(android_sensor_info_max_frame_duration),
159             android_sensor_info_max_frame_duration);
160 
161     float android_sensor_info_physical_size[] = {3.2, 2.4};
162     m.addFloat(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
163             ARRAY_SIZE(android_sensor_info_physical_size),
164             android_sensor_info_physical_size);
165 
166     int32_t android_sensor_info_pixel_array_size[] = {640, 480};
167     m.addInt32(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
168             ARRAY_SIZE(android_sensor_info_pixel_array_size),
169             android_sensor_info_pixel_array_size);
170 
171     int32_t android_sensor_orientation[] = {0};
172     m.addInt32(ANDROID_SENSOR_ORIENTATION,
173             ARRAY_SIZE(android_sensor_orientation),
174             android_sensor_orientation);
175 
176     /* End of static camera characteristics */
177 
178     return clone_camera_metadata(m.get());
179 }
180 
initDevice()181 int ExampleCamera::initDevice()
182 {
183     int res;
184     Metadata base;
185 
186     // Create standard settings templates from copies of base metadata
187     // TODO: use vendor tags in base metadata
188     res = base.add1UInt8(ANDROID_CONTROL_MODE, ANDROID_CONTROL_MODE_OFF);
189     if (res)
190         return res;
191 
192     // Use base settings to create all other templates and set them
193     res = setPreviewTemplate(base);
194     if (res)
195         return res;
196     res = setStillTemplate(base);
197     if (res)
198         return res;
199     res = setRecordTemplate(base);
200     if (res)
201         return res;
202     res = setSnapshotTemplate(base);
203     if (res)
204         return res;
205     res = setZslTemplate(base);
206     if (res)
207         return res;
208 
209     return 0;
210 }
211 
setPreviewTemplate(Metadata m)212 int ExampleCamera::setPreviewTemplate(Metadata m)
213 {
214     // Setup default preview controls
215     int res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
216                             ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW);
217 
218     if (res)
219         return res;
220     // TODO: set fast auto-focus, auto-whitebalance, auto-exposure, auto flash
221     return setTemplate(CAMERA3_TEMPLATE_PREVIEW, m.get());
222 }
223 
setStillTemplate(Metadata m)224 int ExampleCamera::setStillTemplate(Metadata m)
225 {
226     int res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
227                             ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE);
228     // Setup default still capture controls
229     if (res)
230         return res;
231     // TODO: set fast auto-focus, auto-whitebalance, auto-exposure, auto flash
232     return setTemplate(CAMERA3_TEMPLATE_STILL_CAPTURE, m.get());
233 }
234 
setRecordTemplate(Metadata m)235 int ExampleCamera::setRecordTemplate(Metadata m)
236 {
237     int res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
238                             ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD);
239     // Setup default video record controls
240     if (res)
241         return res;
242     // TODO: set slow auto-focus, auto-whitebalance, auto-exposure, flash off
243     return setTemplate(CAMERA3_TEMPLATE_VIDEO_RECORD, m.get());
244 }
245 
setSnapshotTemplate(Metadata m)246 int ExampleCamera::setSnapshotTemplate(Metadata m)
247 {
248     int res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
249                             ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT);
250     // Setup default video snapshot controls
251     if (res)
252         return res;
253     // TODO: set slow auto-focus, auto-whitebalance, auto-exposure, flash off
254     return setTemplate(CAMERA3_TEMPLATE_VIDEO_SNAPSHOT, m.get());
255 }
256 
setZslTemplate(Metadata m)257 int ExampleCamera::setZslTemplate(Metadata m)
258 {
259     int res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
260                             ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG);
261     // Setup default zero shutter lag controls
262     if (res)
263         return res;
264     // TODO: set reprocessing parameters for zsl input queue
265     return setTemplate(CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG, m.get());
266 }
267 
isValidCaptureSettings(const camera_metadata_t *)268 bool ExampleCamera::isValidCaptureSettings(
269         const camera_metadata_t* /*settings*/)
270 {
271     // TODO: reject settings that cannot be captured
272     return true;
273 }
274 
275 } // namespace default_camera_hal
276