blob: c1b7b64b01f2c544f77ec93fe347d58e9bfb52e0 [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.cpp
18
Sergii Piatakov2ad591f2018-08-03 11:41:06 +030019//#define LOG_NDEBUG 0
20#define LOG_TAG "Camera"
21
Sergii Piatakovb8f073f2018-10-10 17:22:10 +030022#include "camera.h"
23
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070024#include <cstdlib>
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -070025#include <memory>
Sergii Piatakovb8f073f2018-10-10 17:22:10 +030026
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070027#include <hardware/camera3.h>
28#include <sync/sync.h>
29#include <system/camera_metadata.h>
30#include <system/graphics.h>
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070031#include "metadata/metadata_common.h"
Sergii Piatakovb8f073f2018-10-10 17:22:10 +030032#include "static_properties.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070033
34#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
35#include <utils/Trace.h>
36
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070037#define CAMERA_SYNC_TIMEOUT 5000 // in msecs
38
39namespace default_camera_hal {
40
41extern "C" {
42// Shim passed to the framework to close an opened device.
43static int close_device(hw_device_t* dev)
44{
45 camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev);
46 Camera* cam = static_cast<Camera*>(cam_dev->priv);
47 return cam->close();
48}
49} // extern "C"
50
51Camera::Camera(int id)
52 : mId(id),
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070053 mSettingsSet(false),
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070054 mBusy(false),
55 mCallbackOps(NULL),
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -070056 mInFlightTracker(new RequestTracker)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070057{
58 memset(&mTemplates, 0, sizeof(mTemplates));
59 memset(&mDevice, 0, sizeof(mDevice));
60 mDevice.common.tag = HARDWARE_DEVICE_TAG;
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070061 mDevice.common.version = CAMERA_DEVICE_API_VERSION_3_4;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070062 mDevice.common.close = close_device;
63 mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps);
64 mDevice.priv = this;
65}
66
67Camera::~Camera()
68{
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070069}
70
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070071int Camera::openDevice(const hw_module_t *module, hw_device_t **device)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070072{
73 ALOGI("%s:%d: Opening camera device", __func__, mId);
74 ATRACE_CALL();
75 android::Mutex::Autolock al(mDeviceLock);
76
77 if (mBusy) {
78 ALOGE("%s:%d: Error! Camera device already opened", __func__, mId);
79 return -EBUSY;
80 }
81
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070082 int connectResult = connect();
83 if (connectResult != 0) {
84 return connectResult;
85 }
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070086 mBusy = true;
87 mDevice.common.module = const_cast<hw_module_t*>(module);
88 *device = &mDevice.common;
89 return 0;
90}
91
92int Camera::getInfo(struct camera_info *info)
93{
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070094 info->device_version = mDevice.common.version;
95 initDeviceInfo(info);
Ari Hausman-Cohene31e1f32016-11-16 15:02:07 -080096 if (!mStaticInfo) {
97 int res = loadStaticInfo();
98 if (res) {
99 return res;
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700100 }
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700101 }
Ari Hausman-Cohene31e1f32016-11-16 15:02:07 -0800102 info->static_camera_characteristics = mStaticInfo->raw_metadata();
103 info->facing = mStaticInfo->facing();
104 info->orientation = mStaticInfo->orientation();
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700105
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700106 return 0;
107}
108
Ari Hausman-Cohene31e1f32016-11-16 15:02:07 -0800109int Camera::loadStaticInfo() {
110 // Using a lock here ensures |mStaticInfo| will only ever be set once,
111 // even in concurrent situations.
112 android::Mutex::Autolock al(mStaticInfoLock);
113
114 if (mStaticInfo) {
115 return 0;
116 }
117
118 std::unique_ptr<android::CameraMetadata> static_metadata =
119 std::make_unique<android::CameraMetadata>();
120 int res = initStaticInfo(static_metadata.get());
121 if (res) {
122 ALOGE("%s:%d: Failed to get static info from device.",
123 __func__, mId);
124 return res;
125 }
126
127 mStaticInfo.reset(StaticProperties::NewStaticProperties(
128 std::move(static_metadata)));
129 if (!mStaticInfo) {
130 ALOGE("%s:%d: Failed to initialize static properties from device metadata.",
131 __func__, mId);
132 return -ENODEV;
133 }
134
135 return 0;
136}
137
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700138int Camera::close()
139{
140 ALOGI("%s:%d: Closing camera device", __func__, mId);
141 ATRACE_CALL();
142 android::Mutex::Autolock al(mDeviceLock);
143
144 if (!mBusy) {
145 ALOGE("%s:%d: Error! Camera device not open", __func__, mId);
146 return -EINVAL;
147 }
148
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800149 flush();
Ari Hausman-Cohen5acb4002016-11-29 18:35:17 -0800150 disconnect();
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700151 mBusy = false;
152 return 0;
153}
154
155int Camera::initialize(const camera3_callback_ops_t *callback_ops)
156{
157 int res;
158
159 ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops);
160 mCallbackOps = callback_ops;
161 // per-device specific initialization
162 res = initDevice();
163 if (res != 0) {
164 ALOGE("%s:%d: Failed to initialize device!", __func__, mId);
165 return res;
166 }
167 return 0;
168}
169
170int Camera::configureStreams(camera3_stream_configuration_t *stream_config)
171{
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800172 android::Mutex::Autolock al(mDeviceLock);
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700173
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700174 ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config);
175 ATRACE_CALL();
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700176
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800177 // Check that there are no in-flight requests.
178 if (!mInFlightTracker->Empty()) {
179 ALOGE("%s:%d: Can't configure streams while frames are in flight.",
180 __func__, mId);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700181 return -EINVAL;
182 }
183
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800184 // Verify the set of streams in aggregate, and perform configuration if valid.
185 int res = validateStreamConfiguration(stream_config);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700186 if (res) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800187 ALOGE("%s:%d: Failed to validate stream set", __func__, mId);
188 } else {
189 // Set up all streams. Since they've been validated,
190 // this should only result in fatal (-ENODEV) errors.
191 // This occurs after validation to ensure that if there
192 // is a non-fatal error, the stream configuration doesn't change states.
193 res = setupStreams(stream_config);
194 if (res) {
195 ALOGE("%s:%d: Failed to setup stream set", __func__, mId);
196 }
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700197 }
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700198
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800199 // Set trackers based on result.
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700200 if (!res) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800201 // Success, set up the in-flight trackers for the new streams.
202 mInFlightTracker->SetStreamConfiguration(*stream_config);
203 // Must provide new settings for the new configuration.
204 mSettingsSet = false;
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700205 } else if (res != -EINVAL) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800206 // Fatal error, the old configuration is invalid.
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700207 mInFlightTracker->ClearStreamConfiguration();
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700208 }
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800209 // On a non-fatal error the old configuration, if any, remains valid.
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700210 return res;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700211}
212
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800213int Camera::validateStreamConfiguration(
214 const camera3_stream_configuration_t* stream_config)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700215{
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800216 // Check that the configuration is well-formed.
217 if (stream_config == nullptr) {
218 ALOGE("%s:%d: NULL stream configuration array", __func__, mId);
219 return -EINVAL;
220 } else if (stream_config->num_streams == 0) {
221 ALOGE("%s:%d: Empty stream configuration array", __func__, mId);
222 return -EINVAL;
223 } else if (stream_config->streams == nullptr) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700224 ALOGE("%s:%d: NULL stream configuration streams", __func__, mId);
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800225 return -EINVAL;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700226 }
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700227
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800228 // Check that the configuration is supported.
229 // Make sure static info has been initialized before trying to use it.
230 if (!mStaticInfo) {
231 int res = loadStaticInfo();
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700232 if (res) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800233 return res;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700234 }
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700235 }
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800236 if (!mStaticInfo->StreamConfigurationSupported(stream_config)) {
237 ALOGE("%s:%d: Stream configuration does not match static "
238 "metadata restrictions.", __func__, mId);
239 return -EINVAL;
240 }
241
242 // Dataspace support is poorly documented - unclear if the expectation
243 // is that a device supports ALL dataspaces that could match a given
244 // format. For now, defer to child class implementation.
245 // Rotation support isn't described by metadata, so must defer to device.
246 if (!validateDataspacesAndRotations(stream_config)) {
247 ALOGE("%s:%d: Device can not handle configuration "
248 "dataspaces or rotations.", __func__, mId);
249 return -EINVAL;
250 }
251
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700252 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700253}
254
255bool Camera::isValidTemplateType(int type)
256{
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700257 return type > 0 && type < CAMERA3_TEMPLATE_COUNT;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700258}
259
260const camera_metadata_t* Camera::constructDefaultRequestSettings(int type)
261{
262 ALOGV("%s:%d: type=%d", __func__, mId, type);
263
264 if (!isValidTemplateType(type)) {
265 ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type);
266 return NULL;
267 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700268
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700269 if (!mTemplates[type]) {
Ari Hausman-Cohene31e1f32016-11-16 15:02:07 -0800270 // Check if the device has the necessary features
271 // for the requested template. If not, don't bother.
272 if (!mStaticInfo->TemplateSupported(type)) {
273 ALOGW("%s:%d: Camera does not support template type %d",
274 __func__, mId, type);
275 return NULL;
276 }
277
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700278 // Initialize this template if it hasn't been initialized yet.
279 std::unique_ptr<android::CameraMetadata> new_template =
280 std::make_unique<android::CameraMetadata>();
281 int res = initTemplate(type, new_template.get());
282 if (res || !new_template) {
283 ALOGE("%s:%d: Failed to generate template of type: %d",
284 __func__, mId, type);
285 return NULL;
286 }
287 mTemplates[type] = std::move(new_template);
288 }
289
290 // The "locking" here only causes non-const methods to fail,
291 // which is not a problem since the CameraMetadata being locked
292 // is already const. Destructing automatically "unlocks".
293 return mTemplates[type]->getAndLock();
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700294}
295
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700296int Camera::processCaptureRequest(camera3_capture_request_t *temp_request)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700297{
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700298 int res;
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800299 // TODO(b/32917568): A capture request submitted or ongoing during a flush
300 // should be returned with an error; for now they are mutually exclusive.
301 android::Mutex::Autolock al(mFlushLock);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700302
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700303 ATRACE_CALL();
304
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700305 if (temp_request == NULL) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700306 ALOGE("%s:%d: NULL request recieved", __func__, mId);
307 return -EINVAL;
308 }
309
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700310 // Make a persistent copy of request, since otherwise it won't live
311 // past the end of this method.
312 std::shared_ptr<CaptureRequest> request = std::make_shared<CaptureRequest>(temp_request);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700313
Ari Hausman-Cohenad6fe2b2016-11-16 10:48:07 -0800314 ALOGV("%s:%d: frame: %d", __func__, mId, request->frame_number);
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700315
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800316 if (!mInFlightTracker->CanAddRequest(*request)) {
317 // Streams are full or frame number is not unique.
318 ALOGE("%s:%d: Can not add request.", __func__, mId);
319 return -EINVAL;
320 }
321
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700322 // Null/Empty indicates use last settings
323 if (request->settings.isEmpty() && !mSettingsSet) {
324 ALOGE("%s:%d: NULL settings without previous set Frame:%d",
325 __func__, mId, request->frame_number);
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700326 return -EINVAL;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700327 }
328
329 if (request->input_buffer != NULL) {
330 ALOGV("%s:%d: Reprocessing input buffer %p", __func__, mId,
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700331 request->input_buffer.get());
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700332 } else {
333 ALOGV("%s:%d: Capturing new frame.", __func__, mId);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700334 }
335
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800336 if (!isValidRequestSettings(request->settings)) {
337 ALOGE("%s:%d: Invalid request settings.", __func__, mId);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700338 return -EINVAL;
339 }
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700340
341 // Pre-process output buffers.
342 if (request->output_buffers.size() <= 0) {
Sergii Piatakova01d1f82018-08-03 17:37:05 +0300343 ALOGE("%s:%d: Invalid number of output buffers: %zu", __func__, mId,
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700344 request->output_buffers.size());
345 return -EINVAL;
346 }
347 for (auto& output_buffer : request->output_buffers) {
348 res = preprocessCaptureBuffer(&output_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700349 if (res)
350 return -ENODEV;
351 }
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700352
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700353 // Add the request to tracking.
354 if (!mInFlightTracker->Add(request)) {
355 ALOGE("%s:%d: Failed to track request for frame %d.",
356 __func__, mId, request->frame_number);
357 return -ENODEV;
358 }
359
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800360 // Valid settings have been provided (mSettingsSet is a misnomer;
361 // all that matters is that a previous request with valid settings
362 // has been passed to the device, not that they've been set).
363 mSettingsSet = true;
364
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700365 // Send the request off to the device for completion.
366 enqueueRequest(request);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700367
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700368 // Request is now in flight. The device will call completeRequest
369 // asynchronously when it is done filling buffers and metadata.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700370 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700371}
372
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700373void Camera::completeRequest(std::shared_ptr<CaptureRequest> request, int err)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700374{
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700375 if (!mInFlightTracker->Remove(request)) {
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800376 ALOGE("%s:%d: Completed request %p is not being tracked. "
377 "It may have been cleared out during a flush.",
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700378 __func__, mId, request.get());
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700379 return;
380 }
381
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700382 // Since |request| has been removed from the tracking, this method
383 // MUST call sendResult (can still return a result in an error state, e.g.
384 // through completeRequestWithError) so the frame doesn't get lost.
385
386 if (err) {
387 ALOGE("%s:%d: Error completing request for frame %d.",
388 __func__, mId, request->frame_number);
389 completeRequestWithError(request);
390 return;
391 }
392
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700393 // Notify the framework with the shutter time (extracted from the result).
394 int64_t timestamp = 0;
395 // TODO(b/31360070): The general metadata methods should be part of the
396 // default_camera_hal namespace, not the v4l2_camera_hal namespace.
397 int res = v4l2_camera_hal::SingleTagValue(
398 request->settings, ANDROID_SENSOR_TIMESTAMP, &timestamp);
399 if (res) {
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700400 ALOGE("%s:%d: Request for frame %d is missing required metadata.",
401 __func__, mId, request->frame_number);
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700402 // TODO(b/31653322): Send RESULT error.
403 // For now sending REQUEST error instead.
404 completeRequestWithError(request);
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700405 return;
406 }
407 notifyShutter(request->frame_number, timestamp);
408
409 // TODO(b/31653322): Check all returned buffers for errors
410 // (if any, send BUFFER error).
411
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700412 sendResult(request);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700413}
414
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800415int Camera::flush()
416{
417 ALOGV("%s:%d: Flushing.", __func__, mId);
418 // TODO(b/32917568): Synchronization. Behave "appropriately"
419 // (i.e. according to camera3.h) if process_capture_request()
420 // is called concurrently with this (in either order).
421 // Since the callback to completeRequest also may happen on a separate
422 // thread, this function should behave nicely concurrently with that too.
423 android::Mutex::Autolock al(mFlushLock);
424
425 std::set<std::shared_ptr<CaptureRequest>> requests;
426 mInFlightTracker->Clear(&requests);
427 for (auto& request : requests) {
428 // TODO(b/31653322): See camera3.h. Should return different error
429 // depending on status of the request.
430 completeRequestWithError(request);
431 }
432
Sergii Piatakova01d1f82018-08-03 17:37:05 +0300433 ALOGV("%s:%d: Flushed %zu requests.", __func__, mId, requests.size());
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800434
435 // Call down into the device flushing.
436 return flushBuffers();
437}
438
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700439int Camera::preprocessCaptureBuffer(camera3_stream_buffer_t *buffer)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700440{
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700441 int res;
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700442 // TODO(b/29334616): This probably should be non-blocking; part
443 // of the asynchronous request processing.
444 if (buffer->acquire_fence != -1) {
445 res = sync_wait(buffer->acquire_fence, CAMERA_SYNC_TIMEOUT);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700446 if (res == -ETIME) {
447 ALOGE("%s:%d: Timeout waiting on buffer acquire fence",
448 __func__, mId);
449 return res;
450 } else if (res) {
451 ALOGE("%s:%d: Error waiting on buffer acquire fence: %s(%d)",
452 __func__, mId, strerror(-res), res);
453 return res;
454 }
Eric Jeongeadafff2017-08-01 19:40:03 +0900455 ::close(buffer->acquire_fence);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700456 }
457
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700458 // Acquire fence has been waited upon.
459 buffer->acquire_fence = -1;
460 // No release fence waiting unless the device sets it.
461 buffer->release_fence = -1;
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700462
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700463 buffer->status = CAMERA3_BUFFER_STATUS_OK;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700464 return 0;
465}
466
467void Camera::notifyShutter(uint32_t frame_number, uint64_t timestamp)
468{
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700469 camera3_notify_msg_t message;
470 memset(&message, 0, sizeof(message));
471 message.type = CAMERA3_MSG_SHUTTER;
472 message.message.shutter.frame_number = frame_number;
473 message.message.shutter.timestamp = timestamp;
474 mCallbackOps->notify(mCallbackOps, &message);
475}
476
477void Camera::completeRequestWithError(std::shared_ptr<CaptureRequest> request)
478{
479 // Send an error notification.
480 camera3_notify_msg_t message;
481 memset(&message, 0, sizeof(message));
482 message.type = CAMERA3_MSG_ERROR;
483 message.message.error.frame_number = request->frame_number;
484 message.message.error.error_stream = nullptr;
485 message.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
486 mCallbackOps->notify(mCallbackOps, &message);
487
488 // TODO(b/31856611): Ensure all the buffers indicate their error status.
489
490 // Send the errored out result.
491 sendResult(request);
492}
493
494void Camera::sendResult(std::shared_ptr<CaptureRequest> request) {
495 // Fill in the result struct
496 // (it only needs to live until the end of the framework callback).
497 camera3_capture_result_t result {
498 request->frame_number,
499 request->settings.getAndLock(),
Dmitry Shmidt9df082e2018-01-23 16:30:08 -0800500 static_cast<uint32_t>(request->output_buffers.size()),
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700501 request->output_buffers.data(),
502 request->input_buffer.get(),
Sergii Piatakov6a4d4d52018-08-06 10:30:58 +0300503 1, // Total result; only 1 part.
504 0, // Number of physical camera metadata.
505 nullptr,
506 nullptr
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700507 };
508 // Make the framework callback.
509 mCallbackOps->process_capture_result(mCallbackOps, &result);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700510}
511
512void Camera::dump(int fd)
513{
514 ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd);
515 ATRACE_CALL();
516 android::Mutex::Autolock al(mDeviceLock);
517
518 dprintf(fd, "Camera ID: %d (Busy: %d)\n", mId, mBusy);
519
520 // TODO: dump all settings
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700521}
522
523const char* Camera::templateToString(int type)
524{
525 switch (type) {
526 case CAMERA3_TEMPLATE_PREVIEW:
527 return "CAMERA3_TEMPLATE_PREVIEW";
528 case CAMERA3_TEMPLATE_STILL_CAPTURE:
529 return "CAMERA3_TEMPLATE_STILL_CAPTURE";
530 case CAMERA3_TEMPLATE_VIDEO_RECORD:
531 return "CAMERA3_TEMPLATE_VIDEO_RECORD";
532 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
533 return "CAMERA3_TEMPLATE_VIDEO_SNAPSHOT";
534 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
535 return "CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG";
536 }
537 // TODO: support vendor templates
538 return "Invalid template type!";
539}
540
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700541extern "C" {
542// Get handle to camera from device priv data
543static Camera *camdev_to_camera(const camera3_device_t *dev)
544{
545 return reinterpret_cast<Camera*>(dev->priv);
546}
547
548static int initialize(const camera3_device_t *dev,
549 const camera3_callback_ops_t *callback_ops)
550{
551 return camdev_to_camera(dev)->initialize(callback_ops);
552}
553
554static int configure_streams(const camera3_device_t *dev,
555 camera3_stream_configuration_t *stream_list)
556{
557 return camdev_to_camera(dev)->configureStreams(stream_list);
558}
559
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700560static const camera_metadata_t *construct_default_request_settings(
561 const camera3_device_t *dev, int type)
562{
563 return camdev_to_camera(dev)->constructDefaultRequestSettings(type);
564}
565
566static int process_capture_request(const camera3_device_t *dev,
567 camera3_capture_request_t *request)
568{
569 return camdev_to_camera(dev)->processCaptureRequest(request);
570}
571
572static void dump(const camera3_device_t *dev, int fd)
573{
574 camdev_to_camera(dev)->dump(fd);
575}
576
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800577static int flush(const camera3_device_t *dev)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700578{
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800579 return camdev_to_camera(dev)->flush();
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700580}
581
582} // extern "C"
583
584const camera3_device_ops_t Camera::sOps = {
585 .initialize = default_camera_hal::initialize,
586 .configure_streams = default_camera_hal::configure_streams,
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700587 .register_stream_buffers = nullptr,
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700588 .construct_default_request_settings
589 = default_camera_hal::construct_default_request_settings,
590 .process_capture_request = default_camera_hal::process_capture_request,
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700591 .get_metadata_vendor_tag_ops = nullptr,
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700592 .dump = default_camera_hal::dump,
593 .flush = default_camera_hal::flush,
594 .reserved = {0},
595};
596
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700597} // namespace default_camera_hal