blob: 3d2e2257d85776678775426f0dfbe1af95867f33 [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();
Sergii Piatakov48ac84b2018-08-02 12:41:11 +030079 android::Mutex::Autolock dl(mDeviceLock);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -070080
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.
Sergii Piatakov48ac84b2018-08-02 12:41:11 +0300116 android::Mutex::Autolock sl(mStaticInfoLock);
Ari Hausman-Cohene31e1f32016-11-16 15:02:07 -0800117
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();
Sergii Piatakov48ac84b2018-08-02 12:41:11 +0300146 android::Mutex::Autolock dl(mDeviceLock);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700147
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{
Sergii Piatakov48ac84b2018-08-02 12:41:11 +0300176 android::Mutex::Autolock dl(mDeviceLock);
177 android::Mutex::Autolock tl(mInFlightTrackerLock);
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700178
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700179 ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config);
180 ATRACE_CALL();
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700181
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800182 // Check that there are no in-flight requests.
183 if (!mInFlightTracker->Empty()) {
184 ALOGE("%s:%d: Can't configure streams while frames are in flight.",
185 __func__, mId);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700186 return -EINVAL;
187 }
188
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800189 // Verify the set of streams in aggregate, and perform configuration if valid.
190 int res = validateStreamConfiguration(stream_config);
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700191 if (res) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800192 ALOGE("%s:%d: Failed to validate stream set", __func__, mId);
193 } else {
194 // Set up all streams. Since they've been validated,
195 // this should only result in fatal (-ENODEV) errors.
196 // This occurs after validation to ensure that if there
197 // is a non-fatal error, the stream configuration doesn't change states.
198 res = setupStreams(stream_config);
199 if (res) {
200 ALOGE("%s:%d: Failed to setup stream set", __func__, mId);
201 }
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700202 }
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700203
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800204 // Set trackers based on result.
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700205 if (!res) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800206 // Success, set up the in-flight trackers for the new streams.
207 mInFlightTracker->SetStreamConfiguration(*stream_config);
208 // Must provide new settings for the new configuration.
209 mSettingsSet = false;
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700210 } else if (res != -EINVAL) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800211 // Fatal error, the old configuration is invalid.
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700212 mInFlightTracker->ClearStreamConfiguration();
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700213 }
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800214 // On a non-fatal error the old configuration, if any, remains valid.
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700215 return res;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700216}
217
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800218int Camera::validateStreamConfiguration(
219 const camera3_stream_configuration_t* stream_config)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700220{
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800221 // Check that the configuration is well-formed.
222 if (stream_config == nullptr) {
223 ALOGE("%s:%d: NULL stream configuration array", __func__, mId);
224 return -EINVAL;
225 } else if (stream_config->num_streams == 0) {
226 ALOGE("%s:%d: Empty stream configuration array", __func__, mId);
227 return -EINVAL;
228 } else if (stream_config->streams == nullptr) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700229 ALOGE("%s:%d: NULL stream configuration streams", __func__, mId);
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800230 return -EINVAL;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700231 }
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700232
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800233 // Check that the configuration is supported.
234 // Make sure static info has been initialized before trying to use it.
235 if (!mStaticInfo) {
236 int res = loadStaticInfo();
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700237 if (res) {
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800238 return res;
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700239 }
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700240 }
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800241 if (!mStaticInfo->StreamConfigurationSupported(stream_config)) {
242 ALOGE("%s:%d: Stream configuration does not match static "
243 "metadata restrictions.", __func__, mId);
244 return -EINVAL;
245 }
246
247 // Dataspace support is poorly documented - unclear if the expectation
248 // is that a device supports ALL dataspaces that could match a given
249 // format. For now, defer to child class implementation.
250 // Rotation support isn't described by metadata, so must defer to device.
251 if (!validateDataspacesAndRotations(stream_config)) {
252 ALOGE("%s:%d: Device can not handle configuration "
253 "dataspaces or rotations.", __func__, mId);
254 return -EINVAL;
255 }
256
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700257 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700258}
259
260bool Camera::isValidTemplateType(int type)
261{
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700262 return type > 0 && type < CAMERA3_TEMPLATE_COUNT;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700263}
264
265const camera_metadata_t* Camera::constructDefaultRequestSettings(int type)
266{
267 ALOGV("%s:%d: type=%d", __func__, mId, type);
268
269 if (!isValidTemplateType(type)) {
270 ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type);
271 return NULL;
272 }
Ari Hausman-Cohen49925842016-06-21 14:07:58 -0700273
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700274 if (!mTemplates[type]) {
Ari Hausman-Cohene31e1f32016-11-16 15:02:07 -0800275 // Check if the device has the necessary features
276 // for the requested template. If not, don't bother.
277 if (!mStaticInfo->TemplateSupported(type)) {
278 ALOGW("%s:%d: Camera does not support template type %d",
279 __func__, mId, type);
280 return NULL;
281 }
282
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700283 // Initialize this template if it hasn't been initialized yet.
284 std::unique_ptr<android::CameraMetadata> new_template =
285 std::make_unique<android::CameraMetadata>();
286 int res = initTemplate(type, new_template.get());
287 if (res || !new_template) {
288 ALOGE("%s:%d: Failed to generate template of type: %d",
289 __func__, mId, type);
290 return NULL;
291 }
292 mTemplates[type] = std::move(new_template);
293 }
294
295 // The "locking" here only causes non-const methods to fail,
296 // which is not a problem since the CameraMetadata being locked
297 // is already const. Destructing automatically "unlocks".
298 return mTemplates[type]->getAndLock();
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700299}
300
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700301int Camera::processCaptureRequest(camera3_capture_request_t *temp_request)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700302{
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700303 int res;
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800304 // TODO(b/32917568): A capture request submitted or ongoing during a flush
305 // should be returned with an error; for now they are mutually exclusive.
Sergii Piatakov48ac84b2018-08-02 12:41:11 +0300306 android::Mutex::Autolock tl(mInFlightTrackerLock);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700307
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700308 ATRACE_CALL();
309
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700310 if (temp_request == NULL) {
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700311 ALOGE("%s:%d: NULL request recieved", __func__, mId);
312 return -EINVAL;
313 }
314
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700315 // Make a persistent copy of request, since otherwise it won't live
316 // past the end of this method.
317 std::shared_ptr<CaptureRequest> request = std::make_shared<CaptureRequest>(temp_request);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700318
Ari Hausman-Cohenad6fe2b2016-11-16 10:48:07 -0800319 ALOGV("%s:%d: frame: %d", __func__, mId, request->frame_number);
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700320
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800321 if (!mInFlightTracker->CanAddRequest(*request)) {
322 // Streams are full or frame number is not unique.
323 ALOGE("%s:%d: Can not add request.", __func__, mId);
324 return -EINVAL;
325 }
326
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700327 // Null/Empty indicates use last settings
328 if (request->settings.isEmpty() && !mSettingsSet) {
329 ALOGE("%s:%d: NULL settings without previous set Frame:%d",
330 __func__, mId, request->frame_number);
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700331 return -EINVAL;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700332 }
333
334 if (request->input_buffer != NULL) {
335 ALOGV("%s:%d: Reprocessing input buffer %p", __func__, mId,
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700336 request->input_buffer.get());
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700337 } else {
338 ALOGV("%s:%d: Capturing new frame.", __func__, mId);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700339 }
340
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800341 if (!isValidRequestSettings(request->settings)) {
342 ALOGE("%s:%d: Invalid request settings.", __func__, mId);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700343 return -EINVAL;
344 }
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700345
346 // Pre-process output buffers.
347 if (request->output_buffers.size() <= 0) {
Sergii Piatakova01d1f82018-08-03 17:37:05 +0300348 ALOGE("%s:%d: Invalid number of output buffers: %zu", __func__, mId,
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700349 request->output_buffers.size());
350 return -EINVAL;
351 }
352 for (auto& output_buffer : request->output_buffers) {
353 res = preprocessCaptureBuffer(&output_buffer);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700354 if (res)
355 return -ENODEV;
356 }
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700357
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700358 // Add the request to tracking.
359 if (!mInFlightTracker->Add(request)) {
360 ALOGE("%s:%d: Failed to track request for frame %d.",
361 __func__, mId, request->frame_number);
362 return -ENODEV;
363 }
364
Ari Hausman-Cohenef523102016-11-21 17:02:01 -0800365 // Valid settings have been provided (mSettingsSet is a misnomer;
366 // all that matters is that a previous request with valid settings
367 // has been passed to the device, not that they've been set).
368 mSettingsSet = true;
369
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700370 // Send the request off to the device for completion.
371 enqueueRequest(request);
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700372
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700373 // Request is now in flight. The device will call completeRequest
374 // asynchronously when it is done filling buffers and metadata.
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700375 return 0;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700376}
377
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700378void Camera::completeRequest(std::shared_ptr<CaptureRequest> request, int err)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700379{
Sergii Piatakov48ac84b2018-08-02 12:41:11 +0300380 android::Mutex::Autolock tl(mInFlightTrackerLock);
381
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700382 if (!mInFlightTracker->Remove(request)) {
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800383 ALOGE("%s:%d: Completed request %p is not being tracked. "
384 "It may have been cleared out during a flush.",
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700385 __func__, mId, request.get());
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700386 return;
387 }
388
Ari Hausman-Cohen0b2113c2016-10-03 13:43:13 -0700389 // Since |request| has been removed from the tracking, this method
390 // MUST call sendResult (can still return a result in an error state, e.g.
391 // through completeRequestWithError) so the frame doesn't get lost.
392
393 if (err) {
394 ALOGE("%s:%d: Error completing request for frame %d.",
395 __func__, mId, request->frame_number);
396 completeRequestWithError(request);
397 return;
398 }
399
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700400 // Notify the framework with the shutter time (extracted from the result).
401 int64_t timestamp = 0;
402 // TODO(b/31360070): The general metadata methods should be part of the
403 // default_camera_hal namespace, not the v4l2_camera_hal namespace.
404 int res = v4l2_camera_hal::SingleTagValue(
405 request->settings, ANDROID_SENSOR_TIMESTAMP, &timestamp);
406 if (res) {
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700407 ALOGE("%s:%d: Request for frame %d is missing required metadata.",
408 __func__, mId, request->frame_number);
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700409 // TODO(b/31653322): Send RESULT error.
410 // For now sending REQUEST error instead.
411 completeRequestWithError(request);
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700412 return;
413 }
414 notifyShutter(request->frame_number, timestamp);
415
416 // TODO(b/31653322): Check all returned buffers for errors
417 // (if any, send BUFFER error).
418
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700419 sendResult(request);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700420}
421
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800422int Camera::flush()
423{
424 ALOGV("%s:%d: Flushing.", __func__, mId);
425 // TODO(b/32917568): Synchronization. Behave "appropriately"
426 // (i.e. according to camera3.h) if process_capture_request()
427 // is called concurrently with this (in either order).
428 // Since the callback to completeRequest also may happen on a separate
429 // thread, this function should behave nicely concurrently with that too.
Sergii Piatakov48ac84b2018-08-02 12:41:11 +0300430 android::Mutex::Autolock tl(mInFlightTrackerLock);
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800431
432 std::set<std::shared_ptr<CaptureRequest>> requests;
433 mInFlightTracker->Clear(&requests);
434 for (auto& request : requests) {
435 // TODO(b/31653322): See camera3.h. Should return different error
436 // depending on status of the request.
437 completeRequestWithError(request);
438 }
439
Sergii Piatakova01d1f82018-08-03 17:37:05 +0300440 ALOGV("%s:%d: Flushed %zu requests.", __func__, mId, requests.size());
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800441
442 // Call down into the device flushing.
443 return flushBuffers();
444}
445
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700446int Camera::preprocessCaptureBuffer(camera3_stream_buffer_t *buffer)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700447{
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700448 int res;
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700449 // TODO(b/29334616): This probably should be non-blocking; part
450 // of the asynchronous request processing.
451 if (buffer->acquire_fence != -1) {
452 res = sync_wait(buffer->acquire_fence, CAMERA_SYNC_TIMEOUT);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700453 if (res == -ETIME) {
454 ALOGE("%s:%d: Timeout waiting on buffer acquire fence",
455 __func__, mId);
456 return res;
457 } else if (res) {
458 ALOGE("%s:%d: Error waiting on buffer acquire fence: %s(%d)",
459 __func__, mId, strerror(-res), res);
460 return res;
461 }
Eric Jeongeadafff2017-08-01 19:40:03 +0900462 ::close(buffer->acquire_fence);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700463 }
464
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700465 // Acquire fence has been waited upon.
466 buffer->acquire_fence = -1;
467 // No release fence waiting unless the device sets it.
468 buffer->release_fence = -1;
Ari Hausman-Cohen24e541c2016-07-21 11:20:30 -0700469
Ari Hausman-Cohen2738a9c2016-09-21 15:03:49 -0700470 buffer->status = CAMERA3_BUFFER_STATUS_OK;
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700471 return 0;
472}
473
474void Camera::notifyShutter(uint32_t frame_number, uint64_t timestamp)
475{
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700476 camera3_notify_msg_t message;
477 memset(&message, 0, sizeof(message));
478 message.type = CAMERA3_MSG_SHUTTER;
479 message.message.shutter.frame_number = frame_number;
480 message.message.shutter.timestamp = timestamp;
481 mCallbackOps->notify(mCallbackOps, &message);
482}
483
484void Camera::completeRequestWithError(std::shared_ptr<CaptureRequest> request)
485{
486 // Send an error notification.
487 camera3_notify_msg_t message;
488 memset(&message, 0, sizeof(message));
489 message.type = CAMERA3_MSG_ERROR;
490 message.message.error.frame_number = request->frame_number;
491 message.message.error.error_stream = nullptr;
492 message.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
493 mCallbackOps->notify(mCallbackOps, &message);
494
495 // TODO(b/31856611): Ensure all the buffers indicate their error status.
496
497 // Send the errored out result.
498 sendResult(request);
499}
500
501void Camera::sendResult(std::shared_ptr<CaptureRequest> request) {
502 // Fill in the result struct
503 // (it only needs to live until the end of the framework callback).
504 camera3_capture_result_t result {
505 request->frame_number,
506 request->settings.getAndLock(),
Dmitry Shmidt9df082e2018-01-23 16:30:08 -0800507 static_cast<uint32_t>(request->output_buffers.size()),
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700508 request->output_buffers.data(),
509 request->input_buffer.get(),
Sergii Piatakov6a4d4d52018-08-06 10:30:58 +0300510 1, // Total result; only 1 part.
511 0, // Number of physical camera metadata.
512 nullptr,
513 nullptr
Ari Hausman-Cohenfb161112016-09-29 14:35:00 -0700514 };
515 // Make the framework callback.
516 mCallbackOps->process_capture_result(mCallbackOps, &result);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700517}
518
519void Camera::dump(int fd)
520{
521 ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd);
522 ATRACE_CALL();
Sergii Piatakov48ac84b2018-08-02 12:41:11 +0300523 android::Mutex::Autolock dl(mDeviceLock);
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700524
525 dprintf(fd, "Camera ID: %d (Busy: %d)\n", mId, mBusy);
526
527 // TODO: dump all settings
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700528}
529
530const char* Camera::templateToString(int type)
531{
532 switch (type) {
533 case CAMERA3_TEMPLATE_PREVIEW:
534 return "CAMERA3_TEMPLATE_PREVIEW";
535 case CAMERA3_TEMPLATE_STILL_CAPTURE:
536 return "CAMERA3_TEMPLATE_STILL_CAPTURE";
537 case CAMERA3_TEMPLATE_VIDEO_RECORD:
538 return "CAMERA3_TEMPLATE_VIDEO_RECORD";
539 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
540 return "CAMERA3_TEMPLATE_VIDEO_SNAPSHOT";
541 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
542 return "CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG";
543 }
544 // TODO: support vendor templates
545 return "Invalid template type!";
546}
547
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700548extern "C" {
549// Get handle to camera from device priv data
550static Camera *camdev_to_camera(const camera3_device_t *dev)
551{
552 return reinterpret_cast<Camera*>(dev->priv);
553}
554
555static int initialize(const camera3_device_t *dev,
556 const camera3_callback_ops_t *callback_ops)
557{
558 return camdev_to_camera(dev)->initialize(callback_ops);
559}
560
561static int configure_streams(const camera3_device_t *dev,
562 camera3_stream_configuration_t *stream_list)
563{
564 return camdev_to_camera(dev)->configureStreams(stream_list);
565}
566
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700567static const camera_metadata_t *construct_default_request_settings(
568 const camera3_device_t *dev, int type)
569{
570 return camdev_to_camera(dev)->constructDefaultRequestSettings(type);
571}
572
573static int process_capture_request(const camera3_device_t *dev,
574 camera3_capture_request_t *request)
575{
576 return camdev_to_camera(dev)->processCaptureRequest(request);
577}
578
579static void dump(const camera3_device_t *dev, int fd)
580{
581 camdev_to_camera(dev)->dump(fd);
582}
583
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800584static int flush(const camera3_device_t *dev)
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700585{
Ari Hausman-Cohenc5a48522016-11-16 10:53:52 -0800586 return camdev_to_camera(dev)->flush();
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700587}
588
589} // extern "C"
590
591const camera3_device_ops_t Camera::sOps = {
592 .initialize = default_camera_hal::initialize,
593 .configure_streams = default_camera_hal::configure_streams,
Ari Hausman-Cohen900c1e32016-06-20 16:52:41 -0700594 .register_stream_buffers = nullptr,
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700595 .construct_default_request_settings
596 = default_camera_hal::construct_default_request_settings,
597 .process_capture_request = default_camera_hal::process_capture_request,
Ari Hausman-Cohen72fddb32016-06-30 16:53:31 -0700598 .get_metadata_vendor_tag_ops = nullptr,
Ari Hausman-Cohen73442152016-06-08 15:50:49 -0700599 .dump = default_camera_hal::dump,
600 .flush = default_camera_hal::flush,
601 .reserved = {0},
602};
603
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -0700604} // namespace default_camera_hal