blob: 01236f3cec0da635403b0a1468db30c6e5714a4e [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
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070022#include <cstdlib>
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -070023#include <memory>
24#include <vector>
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070025#include <stdio.h>
26#include <hardware/camera3.h>
27#include <sync/sync.h>
28#include <system/camera_metadata.h>
29#include <system/graphics.h>
30#include <utils/Mutex.h>
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070031
32#include "metadata/metadata_common.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070033
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070034#include <cutils/log.h>
35
36#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
37#include <utils/Trace.h>
38
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070039#include "camera.h"
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070040
41#define CAMERA_SYNC_TIMEOUT 5000 // in msecs
42
43namespace default_camera_hal {
44
45extern "C" {
46// Shim passed to the framework to close an opened device.
47static int close_device(hw_device_t* dev)
48{
49 camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev);
50 Camera* cam = static_cast<Camera*>(cam_dev->priv);
51 return cam->close();
52}
53} // extern "C"
54
55Camera::Camera(int id)
56 : mId(id),
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070057 mSettingsSet(false),
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070058 mBusy(false),
59 mCallbackOps(NULL),
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -070060 mInFlightTracker(new RequestTracker)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070061{
62 memset(&mTemplates, 0, sizeof(mTemplates));
63 memset(&mDevice, 0, sizeof(mDevice));
64 mDevice.common.tag = HARDWARE_DEVICE_TAG;
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -070065 mDevice.common.version = CAMERA_DEVICE_API_VERSION_3_4;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070066 mDevice.common.close = close_device;
67 mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps);
68 mDevice.priv = this;
69}
70
71Camera::~Camera()
72{
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070073}
74
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070075int Camera::openDevice(const hw_module_t *module, hw_device_t **device)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070076{
77 ALOGI("%s:%d: Opening camera device", __func__, mId);
78 ATRACE_CALL();
79 android::Mutex::Autolock al(mDeviceLock);
80
81 if (mBusy) {
82 ALOGE("%s:%d: Error! Camera device already opened", __func__, mId);
83 return -EBUSY;
84 }
85
Ari Hausman-Cohen345bd3a2016-06-13 15:33:53 -070086 int connectResult = connect();
87 if (connectResult != 0) {
88 return connectResult;
89 }
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070090 mBusy = true;
91 mDevice.common.module = const_cast<hw_module_t*>(module);
92 *device = &mDevice.common;
93 return 0;
94}
95
96int Camera::getInfo(struct camera_info *info)
97{
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070098 info->device_version = mDevice.common.version;
99 initDeviceInfo(info);
Ari Hausman-Cohene31e1f32016-11-16 15:02:07 -0800100 if (!mStaticInfo) {
101 int res = loadStaticInfo();
102 if (res) {
103 return res;
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700104 }
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700105 }
Ari Hausman-Cohene31e1f32016-11-16 15:02:07 -0800106 info->static_camera_characteristics = mStaticInfo->raw_metadata();
107 info->facing = mStaticInfo->facing();
108 info->orientation = mStaticInfo->orientation();
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700109
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700110 return 0;
111}
112
Ari Hausman-Cohene31e1f32016-11-16 15:02:07 -0800113int Camera::loadStaticInfo() {
114 // Using a lock here ensures |mStaticInfo| will only ever be set once,
115 // even in concurrent situations.
116 android::Mutex::Autolock al(mStaticInfoLock);
117
118 if (mStaticInfo) {
119 return 0;
120 }
121
122 std::unique_ptr<android::CameraMetadata> static_metadata =
123 std::make_unique<android::CameraMetadata>();
124 int res = initStaticInfo(static_metadata.get());
125 if (res) {
126 ALOGE("%s:%d: Failed to get static info from device.",
127 __func__, mId);
128 return res;
129 }
130
131 mStaticInfo.reset(StaticProperties::NewStaticProperties(
132 std::move(static_metadata)));
133 if (!mStaticInfo) {
134 ALOGE("%s:%d: Failed to initialize static properties from device metadata.",
135 __func__, mId);
136 return -ENODEV;
137 }
138
139 return 0;
140}
141
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700142int Camera::close()
143{
144 ALOGI("%s:%d: Closing camera device", __func__, mId);
145 ATRACE_CALL();
146 android::Mutex::Autolock al(mDeviceLock);
147
148 if (!mBusy) {
149 ALOGE("%s:%d: Error! Camera device not open", __func__, mId);
150 return -EINVAL;
151 }
152
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800153 flush();
Ari Hausman-Cohen5acb4002016-11-29 18:35:17 -0800154 disconnect();
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700155 mBusy = false;
156 return 0;
157}
158
159int Camera::initialize(const camera3_callback_ops_t *callback_ops)
160{
161 int res;
162
163 ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops);
164 mCallbackOps = callback_ops;
165 // per-device specific initialization
166 res = initDevice();
167 if (res != 0) {
168 ALOGE("%s:%d: Failed to initialize device!", __func__, mId);
169 return res;
170 }
171 return 0;
172}
173
174int Camera::configureStreams(camera3_stream_configuration_t *stream_config)
175{
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800176 android::Mutex::Autolock al(mDeviceLock);
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700177
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700178 ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config);
179 ATRACE_CALL();
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700180
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800181 // Check that there are no in-flight requests.
182 if (!mInFlightTracker->Empty()) {
183 ALOGE("%s:%d: Can't configure streams while frames are in flight.",
184 __func__, mId);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700185 return -EINVAL;
186 }
187
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800188 // Verify the set of streams in aggregate, and perform configuration if valid.
189 int res = validateStreamConfiguration(stream_config);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700190 if (res) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800191 ALOGE("%s:%d: Failed to validate stream set", __func__, mId);
192 } else {
193 // Set up all streams. Since they've been validated,
194 // this should only result in fatal (-ENODEV) errors.
195 // This occurs after validation to ensure that if there
196 // is a non-fatal error, the stream configuration doesn't change states.
197 res = setupStreams(stream_config);
198 if (res) {
199 ALOGE("%s:%d: Failed to setup stream set", __func__, mId);
200 }
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700201 }
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700202
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800203 // Set trackers based on result.
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700204 if (!res) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800205 // Success, set up the in-flight trackers for the new streams.
206 mInFlightTracker->SetStreamConfiguration(*stream_config);
207 // Must provide new settings for the new configuration.
208 mSettingsSet = false;
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700209 } else if (res != -EINVAL) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800210 // Fatal error, the old configuration is invalid.
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700211 mInFlightTracker->ClearStreamConfiguration();
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700212 }
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800213 // On a non-fatal error the old configuration, if any, remains valid.
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700214 return res;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700215}
216
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800217int Camera::validateStreamConfiguration(
218 const camera3_stream_configuration_t* stream_config)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700219{
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800220 // Check that the configuration is well-formed.
221 if (stream_config == nullptr) {
222 ALOGE("%s:%d: NULL stream configuration array", __func__, mId);
223 return -EINVAL;
224 } else if (stream_config->num_streams == 0) {
225 ALOGE("%s:%d: Empty stream configuration array", __func__, mId);
226 return -EINVAL;
227 } else if (stream_config->streams == nullptr) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700228 ALOGE("%s:%d: NULL stream configuration streams", __func__, mId);
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800229 return -EINVAL;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700230 }
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700231
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800232 // Check that the configuration is supported.
233 // Make sure static info has been initialized before trying to use it.
234 if (!mStaticInfo) {
235 int res = loadStaticInfo();
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700236 if (res) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800237 return res;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700238 }
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700239 }
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800240 if (!mStaticInfo->StreamConfigurationSupported(stream_config)) {
241 ALOGE("%s:%d: Stream configuration does not match static "
242 "metadata restrictions.", __func__, mId);
243 return -EINVAL;
244 }
245
246 // Dataspace support is poorly documented - unclear if the expectation
247 // is that a device supports ALL dataspaces that could match a given
248 // format. For now, defer to child class implementation.
249 // Rotation support isn't described by metadata, so must defer to device.
250 if (!validateDataspacesAndRotations(stream_config)) {
251 ALOGE("%s:%d: Device can not handle configuration "
252 "dataspaces or rotations.", __func__, mId);
253 return -EINVAL;
254 }
255
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700256 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700257}
258
259bool Camera::isValidTemplateType(int type)
260{
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700261 return type > 0 && type < CAMERA3_TEMPLATE_COUNT;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700262}
263
264const camera_metadata_t* Camera::constructDefaultRequestSettings(int type)
265{
266 ALOGV("%s:%d: type=%d", __func__, mId, type);
267
268 if (!isValidTemplateType(type)) {
269 ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type);
270 return NULL;
271 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700272
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700273 if (!mTemplates[type]) {
Ari Hausman-Cohene31e1f32016-11-16 15:02:07 -0800274 // Check if the device has the necessary features
275 // for the requested template. If not, don't bother.
276 if (!mStaticInfo->TemplateSupported(type)) {
277 ALOGW("%s:%d: Camera does not support template type %d",
278 __func__, mId, type);
279 return NULL;
280 }
281
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700282 // Initialize this template if it hasn't been initialized yet.
283 std::unique_ptr<android::CameraMetadata> new_template =
284 std::make_unique<android::CameraMetadata>();
285 int res = initTemplate(type, new_template.get());
286 if (res || !new_template) {
287 ALOGE("%s:%d: Failed to generate template of type: %d",
288 __func__, mId, type);
289 return NULL;
290 }
291 mTemplates[type] = std::move(new_template);
292 }
293
294 // The "locking" here only causes non-const methods to fail,
295 // which is not a problem since the CameraMetadata being locked
296 // is already const. Destructing automatically "unlocks".
297 return mTemplates[type]->getAndLock();
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700298}
299
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700300int Camera::processCaptureRequest(camera3_capture_request_t *temp_request)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700301{
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700302 int res;
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800303 // TODO(b/32917568): A capture request submitted or ongoing during a flush
304 // should be returned with an error; for now they are mutually exclusive.
305 android::Mutex::Autolock al(mFlushLock);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700306
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700307 ATRACE_CALL();
308
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700309 if (temp_request == NULL) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700310 ALOGE("%s:%d: NULL request recieved", __func__, mId);
311 return -EINVAL;
312 }
313
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700314 // Make a persistent copy of request, since otherwise it won't live
315 // past the end of this method.
316 std::shared_ptr<CaptureRequest> request = std::make_shared<CaptureRequest>(temp_request);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700317
Ari Hausman-Cohenad6fe2b2016-11-16 10:48:07 -0800318 ALOGV("%s:%d: frame: %d", __func__, mId, request->frame_number);
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700319
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800320 if (!mInFlightTracker->CanAddRequest(*request)) {
321 // Streams are full or frame number is not unique.
322 ALOGE("%s:%d: Can not add request.", __func__, mId);
323 return -EINVAL;
324 }
325
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700326 // Null/Empty indicates use last settings
327 if (request->settings.isEmpty() && !mSettingsSet) {
328 ALOGE("%s:%d: NULL settings without previous set Frame:%d",
329 __func__, mId, request->frame_number);
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700330 return -EINVAL;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700331 }
332
333 if (request->input_buffer != NULL) {
334 ALOGV("%s:%d: Reprocessing input buffer %p", __func__, mId,
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700335 request->input_buffer.get());
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700336 } else {
337 ALOGV("%s:%d: Capturing new frame.", __func__, mId);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700338 }
339
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800340 if (!isValidRequestSettings(request->settings)) {
341 ALOGE("%s:%d: Invalid request settings.", __func__, mId);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700342 return -EINVAL;
343 }
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700344
345 // Pre-process output buffers.
346 if (request->output_buffers.size() <= 0) {
347 ALOGE("%s:%d: Invalid number of output buffers: %d", __func__, mId,
348 request->output_buffers.size());
349 return -EINVAL;
350 }
351 for (auto& output_buffer : request->output_buffers) {
352 res = preprocessCaptureBuffer(&output_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700353 if (res)
354 return -ENODEV;
355 }
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700356
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700357 // Add the request to tracking.
358 if (!mInFlightTracker->Add(request)) {
359 ALOGE("%s:%d: Failed to track request for frame %d.",
360 __func__, mId, request->frame_number);
361 return -ENODEV;
362 }
363
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800364 // Valid settings have been provided (mSettingsSet is a misnomer;
365 // all that matters is that a previous request with valid settings
366 // has been passed to the device, not that they've been set).
367 mSettingsSet = true;
368
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700369 // Send the request off to the device for completion.
370 enqueueRequest(request);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700371
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700372 // Request is now in flight. The device will call completeRequest
373 // asynchronously when it is done filling buffers and metadata.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700374 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700375}
376
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700377void Camera::completeRequest(std::shared_ptr<CaptureRequest> request, int err)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700378{
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700379 if (!mInFlightTracker->Remove(request)) {
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800380 ALOGE("%s:%d: Completed request %p is not being tracked. "
381 "It may have been cleared out during a flush.",
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700382 __func__, mId, request.get());
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700383 return;
384 }
385
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700386 // Since |request| has been removed from the tracking, this method
387 // MUST call sendResult (can still return a result in an error state, e.g.
388 // through completeRequestWithError) so the frame doesn't get lost.
389
390 if (err) {
391 ALOGE("%s:%d: Error completing request for frame %d.",
392 __func__, mId, request->frame_number);
393 completeRequestWithError(request);
394 return;
395 }
396
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700397 // Notify the framework with the shutter time (extracted from the result).
398 int64_t timestamp = 0;
399 // TODO(b/31360070): The general metadata methods should be part of the
400 // default_camera_hal namespace, not the v4l2_camera_hal namespace.
401 int res = v4l2_camera_hal::SingleTagValue(
402 request->settings, ANDROID_SENSOR_TIMESTAMP, &timestamp);
403 if (res) {
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700404 ALOGE("%s:%d: Request for frame %d is missing required metadata.",
405 __func__, mId, request->frame_number);
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700406 // TODO(b/31653322): Send RESULT error.
407 // For now sending REQUEST error instead.
408 completeRequestWithError(request);
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700409 return;
410 }
411 notifyShutter(request->frame_number, timestamp);
412
413 // TODO(b/31653322): Check all returned buffers for errors
414 // (if any, send BUFFER error).
415
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700416 sendResult(request);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700417}
418
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800419int Camera::flush()
420{
421 ALOGV("%s:%d: Flushing.", __func__, mId);
422 // TODO(b/32917568): Synchronization. Behave "appropriately"
423 // (i.e. according to camera3.h) if process_capture_request()
424 // is called concurrently with this (in either order).
425 // Since the callback to completeRequest also may happen on a separate
426 // thread, this function should behave nicely concurrently with that too.
427 android::Mutex::Autolock al(mFlushLock);
428
429 std::set<std::shared_ptr<CaptureRequest>> requests;
430 mInFlightTracker->Clear(&requests);
431 for (auto& request : requests) {
432 // TODO(b/31653322): See camera3.h. Should return different error
433 // depending on status of the request.
434 completeRequestWithError(request);
435 }
436
437 ALOGV("%s:%d: Flushed %u requests.", __func__, mId, requests.size());
438
439 // Call down into the device flushing.
440 return flushBuffers();
441}
442
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700443int Camera::preprocessCaptureBuffer(camera3_stream_buffer_t *buffer)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700444{
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700445 int res;
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700446 // TODO(b/29334616): This probably should be non-blocking; part
447 // of the asynchronous request processing.
448 if (buffer->acquire_fence != -1) {
449 res = sync_wait(buffer->acquire_fence, CAMERA_SYNC_TIMEOUT);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700450 if (res == -ETIME) {
451 ALOGE("%s:%d: Timeout waiting on buffer acquire fence",
452 __func__, mId);
453 return res;
454 } else if (res) {
455 ALOGE("%s:%d: Error waiting on buffer acquire fence: %s(%d)",
456 __func__, mId, strerror(-res), res);
457 return res;
458 }
Eric Jeongeadafff2017-08-01 19:40:03 +0900459 ::close(buffer->acquire_fence);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700460 }
461
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700462 // Acquire fence has been waited upon.
463 buffer->acquire_fence = -1;
464 // No release fence waiting unless the device sets it.
465 buffer->release_fence = -1;
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700466
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700467 buffer->status = CAMERA3_BUFFER_STATUS_OK;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700468 return 0;
469}
470
471void Camera::notifyShutter(uint32_t frame_number, uint64_t timestamp)
472{
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700473 camera3_notify_msg_t message;
474 memset(&message, 0, sizeof(message));
475 message.type = CAMERA3_MSG_SHUTTER;
476 message.message.shutter.frame_number = frame_number;
477 message.message.shutter.timestamp = timestamp;
478 mCallbackOps->notify(mCallbackOps, &message);
479}
480
481void Camera::completeRequestWithError(std::shared_ptr<CaptureRequest> request)
482{
483 // Send an error notification.
484 camera3_notify_msg_t message;
485 memset(&message, 0, sizeof(message));
486 message.type = CAMERA3_MSG_ERROR;
487 message.message.error.frame_number = request->frame_number;
488 message.message.error.error_stream = nullptr;
489 message.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
490 mCallbackOps->notify(mCallbackOps, &message);
491
492 // TODO(b/31856611): Ensure all the buffers indicate their error status.
493
494 // Send the errored out result.
495 sendResult(request);
496}
497
498void Camera::sendResult(std::shared_ptr<CaptureRequest> request) {
499 // Fill in the result struct
500 // (it only needs to live until the end of the framework callback).
501 camera3_capture_result_t result {
502 request->frame_number,
503 request->settings.getAndLock(),
Dmitry Shmidt9df082e2018-01-23 16:30:08 -0800504 static_cast<uint32_t>(request->output_buffers.size()),
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700505 request->output_buffers.data(),
506 request->input_buffer.get(),
507 1 // Total result; only 1 part.
508 };
509 // Make the framework callback.
510 mCallbackOps->process_capture_result(mCallbackOps, &result);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700511}
512
513void Camera::dump(int fd)
514{
515 ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd);
516 ATRACE_CALL();
517 android::Mutex::Autolock al(mDeviceLock);
518
519 dprintf(fd, "Camera ID: %d (Busy: %d)\n", mId, mBusy);
520
521 // TODO: dump all settings
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700522}
523
524const char* Camera::templateToString(int type)
525{
526 switch (type) {
527 case CAMERA3_TEMPLATE_PREVIEW:
528 return "CAMERA3_TEMPLATE_PREVIEW";
529 case CAMERA3_TEMPLATE_STILL_CAPTURE:
530 return "CAMERA3_TEMPLATE_STILL_CAPTURE";
531 case CAMERA3_TEMPLATE_VIDEO_RECORD:
532 return "CAMERA3_TEMPLATE_VIDEO_RECORD";
533 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
534 return "CAMERA3_TEMPLATE_VIDEO_SNAPSHOT";
535 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
536 return "CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG";
537 }
538 // TODO: support vendor templates
539 return "Invalid template type!";
540}
541
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700542extern "C" {
543// Get handle to camera from device priv data
544static Camera *camdev_to_camera(const camera3_device_t *dev)
545{
546 return reinterpret_cast<Camera*>(dev->priv);
547}
548
549static int initialize(const camera3_device_t *dev,
550 const camera3_callback_ops_t *callback_ops)
551{
552 return camdev_to_camera(dev)->initialize(callback_ops);
553}
554
555static int configure_streams(const camera3_device_t *dev,
556 camera3_stream_configuration_t *stream_list)
557{
558 return camdev_to_camera(dev)->configureStreams(stream_list);
559}
560
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700561static const camera_metadata_t *construct_default_request_settings(
562 const camera3_device_t *dev, int type)
563{
564 return camdev_to_camera(dev)->constructDefaultRequestSettings(type);
565}
566
567static int process_capture_request(const camera3_device_t *dev,
568 camera3_capture_request_t *request)
569{
570 return camdev_to_camera(dev)->processCaptureRequest(request);
571}
572
573static void dump(const camera3_device_t *dev, int fd)
574{
575 camdev_to_camera(dev)->dump(fd);
576}
577
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800578static int flush(const camera3_device_t *dev)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700579{
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800580 return camdev_to_camera(dev)->flush();
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700581}
582
583} // extern "C"
584
585const camera3_device_ops_t Camera::sOps = {
586 .initialize = default_camera_hal::initialize,
587 .configure_streams = default_camera_hal::configure_streams,
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700588 .register_stream_buffers = nullptr,
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700589 .construct_default_request_settings
590 = default_camera_hal::construct_default_request_settings,
591 .process_capture_request = default_camera_hal::process_capture_request,
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700592 .get_metadata_vendor_tag_ops = nullptr,
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700593 .dump = default_camera_hal::dump,
594 .flush = default_camera_hal::flush,
595 .reserved = {0},
596};
597
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700598} // namespace default_camera_hal