blob: 73bf30c2037fb42e2aea9b99d89f7ea10051796e [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
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
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080040#include <utils/Log.h>
41#include <utils/Trace.h>
42#include <utils/Timers.h>
43#include "Camera3Device.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080044#include "camera3/Camera3OutputStream.h"
Igor Murashkin5a269fa2013-04-15 14:59:22 -070045#include "camera3/Camera3InputStream.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080046
47using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080048
49namespace android {
50
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080051Camera3Device::Camera3Device(int id):
52 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080053 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070054 mStatus(STATUS_UNINITIALIZED),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070055 mNextResultFrameNumber(0),
56 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070057 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058{
59 ATRACE_CALL();
60 camera3_callback_ops::notify = &sNotify;
61 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
62 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
63}
64
65Camera3Device::~Camera3Device()
66{
67 ATRACE_CALL();
68 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
69 disconnect();
70}
71
Igor Murashkin71381052013-03-04 14:53:08 -080072int Camera3Device::getId() const {
73 return mId;
74}
75
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080076/**
77 * CameraDeviceBase interface
78 */
79
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080080status_t Camera3Device::initialize(camera_module_t *module)
81{
82 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080083 Mutex::Autolock l(mLock);
84
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080085 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080086 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070087 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080088 return INVALID_OPERATION;
89 }
90
91 /** Open HAL device */
92
93 status_t res;
94 String8 deviceName = String8::format("%d", mId);
95
96 camera3_device_t *device;
97
98 res = module->common.methods->open(&module->common, deviceName.string(),
99 reinterpret_cast<hw_device_t**>(&device));
100
101 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700102 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 return res;
104 }
105
106 /** Cross-check device version */
107
108 if (device->common.version != CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700109 SET_ERR_L("Could not open camera: "
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800110 "Camera device is not version %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700111 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800112 device->common.version);
113 device->common.close(&device->common);
114 return BAD_VALUE;
115 }
116
117 camera_info info;
118 res = module->get_camera_info(mId, &info);
119 if (res != OK) return res;
120
121 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
123 " and device version (%x).",
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800124 device->common.version, info.device_version);
125 device->common.close(&device->common);
126 return BAD_VALUE;
127 }
128
129 /** Initialize device with callback functions */
130
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700131 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800132 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700133 ATRACE_END();
134
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800135 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700136 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
137 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 device->common.close(&device->common);
139 return BAD_VALUE;
140 }
141
142 /** Get vendor metadata tags */
143
144 mVendorTagOps.get_camera_vendor_section_name = NULL;
145
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700146 ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800147 device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700148 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800149
150 if (mVendorTagOps.get_camera_vendor_section_name != NULL) {
151 res = set_camera_metadata_vendor_tag_ops(&mVendorTagOps);
152 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700153 SET_ERR_L("Unable to set tag ops: %s (%d)",
154 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800155 device->common.close(&device->common);
156 return res;
157 }
158 }
159
160 /** Start up request queue thread */
161
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800162 mRequestThread = new RequestThread(this, device);
163 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800164 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700165 SET_ERR_L("Unable to start request queue thread: %s (%d)",
166 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800167 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800168 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800169 return res;
170 }
171
172 /** Everything is good to go */
173
174 mDeviceInfo = info.static_camera_characteristics;
175 mHal3Device = device;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800176 mStatus = STATUS_IDLE;
177 mNextStreamId = 0;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700178 mNeedConfig = true;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800179
180 return OK;
181}
182
183status_t Camera3Device::disconnect() {
184 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800185 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800186
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800187 ALOGV("%s: E", __FUNCTION__);
188
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700189 status_t res = OK;
190 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800191
192 if (mStatus == STATUS_ACTIVE ||
193 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
194 res = mRequestThread->clearRepeatingRequests();
195 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700196 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700197 // Continue to close device even in case of error
198 } else {
199 res = waitUntilDrainedLocked();
200 if (res != OK) {
201 SET_ERR_L("Timeout waiting for HAL to drain");
202 // Continue to close device even in case of error
203 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800204 }
205 }
206 assert(mStatus == STATUS_IDLE || mStatus == STATUS_ERROR);
207
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700208 if (mStatus == STATUS_ERROR) {
209 CLOGE("Shutting down in an error state");
210 }
211
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800212 if (mRequestThread != NULL) {
213 mRequestThread->requestExit();
214 }
215
216 mOutputStreams.clear();
217 mInputStream.clear();
218
219 if (mRequestThread != NULL) {
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700220 if (mStatus != STATUS_ERROR) {
221 // HAL may be in a bad state, so waiting for request thread
222 // (which may be stuck in the HAL processCaptureRequest call)
223 // could be dangerous.
224 mRequestThread->join();
225 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800226 mRequestThread.clear();
227 }
228
229 if (mHal3Device != NULL) {
230 mHal3Device->common.close(&mHal3Device->common);
231 mHal3Device = NULL;
232 }
233
234 mStatus = STATUS_UNINITIALIZED;
235
236 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700237 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800238}
239
240status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
241 ATRACE_CALL();
242 (void)args;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800243 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800244
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800245 const char *status =
246 mStatus == STATUS_ERROR ? "ERROR" :
247 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
248 mStatus == STATUS_IDLE ? "IDLE" :
249 mStatus == STATUS_ACTIVE ? "ACTIVE" :
250 "Unknown";
251 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700252 if (mStatus == STATUS_ERROR) {
253 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
254 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800255 lines.appendFormat(" Stream configuration:\n");
256
257 if (mInputStream != NULL) {
258 write(fd, lines.string(), lines.size());
259 mInputStream->dump(fd, args);
260 } else {
261 lines.appendFormat(" No input stream.\n");
262 write(fd, lines.string(), lines.size());
263 }
264 for (size_t i = 0; i < mOutputStreams.size(); i++) {
265 mOutputStreams[i]->dump(fd,args);
266 }
267
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700268 lines = String8(" In-flight requests:\n");
269 if (mInFlightMap.size() == 0) {
270 lines.append(" None\n");
271 } else {
272 for (size_t i = 0; i < mInFlightMap.size(); i++) {
273 InFlightRequest r = mInFlightMap.valueAt(i);
274 lines.appendFormat(" Frame %d | Timestamp: %lld, metadata"
275 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
276 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
277 r.numBuffersLeft);
278 }
279 }
280 write(fd, lines.string(), lines.size());
281
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800282 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700283 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800284 write(fd, lines.string(), lines.size());
285 mHal3Device->ops->dump(mHal3Device, fd);
286 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800287
288 return OK;
289}
290
291const CameraMetadata& Camera3Device::info() const {
292 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800293 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
294 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700295 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800296 mStatus == STATUS_ERROR ?
297 "when in error state" : "before init");
298 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800299 return mDeviceInfo;
300}
301
302status_t Camera3Device::capture(CameraMetadata &request) {
303 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800304 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800305
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700306 // TODO: take ownership of the request
307
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800308 switch (mStatus) {
309 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700310 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800311 return INVALID_OPERATION;
312 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700313 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800314 return INVALID_OPERATION;
315 case STATUS_IDLE:
316 case STATUS_ACTIVE:
317 // OK
318 break;
319 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700320 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800321 return INVALID_OPERATION;
322 }
323
324 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
325 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700326 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800327 return BAD_VALUE;
328 }
329
330 return mRequestThread->queueRequest(newRequest);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800331}
332
333
334status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) {
335 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800336 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800337
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800338 switch (mStatus) {
339 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700340 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800341 return INVALID_OPERATION;
342 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700343 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800344 return INVALID_OPERATION;
345 case STATUS_IDLE:
346 case STATUS_ACTIVE:
347 // OK
348 break;
349 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700350 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800351 return INVALID_OPERATION;
352 }
353
354 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
355 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700356 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800357 return BAD_VALUE;
358 }
359
360 RequestList newRepeatingRequests;
361 newRepeatingRequests.push_back(newRepeatingRequest);
362
363 return mRequestThread->setRepeatingRequests(newRepeatingRequests);
364}
365
366
367sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
368 const CameraMetadata &request) {
369 status_t res;
370
371 if (mStatus == STATUS_IDLE) {
372 res = configureStreamsLocked();
373 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700374 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800375 return NULL;
376 }
377 }
378
379 sp<CaptureRequest> newRequest = createCaptureRequest(request);
380 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800381}
382
383status_t Camera3Device::clearStreamingRequest() {
384 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800385 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800386
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800387 switch (mStatus) {
388 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700389 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800390 return INVALID_OPERATION;
391 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700392 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800393 return INVALID_OPERATION;
394 case STATUS_IDLE:
395 case STATUS_ACTIVE:
396 // OK
397 break;
398 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700399 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800400 return INVALID_OPERATION;
401 }
402
403 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800404}
405
406status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
407 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800408
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700409 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800410}
411
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700412status_t Camera3Device::createInputStream(
413 uint32_t width, uint32_t height, int format, int *id) {
414 ATRACE_CALL();
415 Mutex::Autolock l(mLock);
416
417 status_t res;
418 bool wasActive = false;
419
420 switch (mStatus) {
421 case STATUS_ERROR:
422 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
423 return INVALID_OPERATION;
424 case STATUS_UNINITIALIZED:
425 ALOGE("%s: Device not initialized", __FUNCTION__);
426 return INVALID_OPERATION;
427 case STATUS_IDLE:
428 // OK
429 break;
430 case STATUS_ACTIVE:
431 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
432 mRequestThread->setPaused(true);
433 res = waitUntilDrainedLocked();
434 if (res != OK) {
435 ALOGE("%s: Can't pause captures to reconfigure streams!",
436 __FUNCTION__);
437 mStatus = STATUS_ERROR;
438 return res;
439 }
440 wasActive = true;
441 break;
442 default:
443 ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus);
444 return INVALID_OPERATION;
445 }
446 assert(mStatus == STATUS_IDLE);
447
448 if (mInputStream != 0) {
449 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
450 return INVALID_OPERATION;
451 }
452
453 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
454 width, height, format);
455
456 mInputStream = newStream;
457
458 *id = mNextStreamId++;
459
460 // Continue captures if active at start
461 if (wasActive) {
462 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
463 res = configureStreamsLocked();
464 if (res != OK) {
465 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
466 __FUNCTION__, mNextStreamId, strerror(-res), res);
467 return res;
468 }
469 mRequestThread->setPaused(false);
470 }
471
472 return OK;
473}
474
Igor Murashkin2fba5842013-04-22 14:03:54 -0700475
476status_t Camera3Device::createZslStream(
477 uint32_t width, uint32_t height,
478 int depth,
479 /*out*/
480 int *id,
481 sp<Camera3ZslStream>* zslStream) {
482 ATRACE_CALL();
483 Mutex::Autolock l(mLock);
484
485 status_t res;
486 bool wasActive = false;
487
488 switch (mStatus) {
489 case STATUS_ERROR:
490 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
491 return INVALID_OPERATION;
492 case STATUS_UNINITIALIZED:
493 ALOGE("%s: Device not initialized", __FUNCTION__);
494 return INVALID_OPERATION;
495 case STATUS_IDLE:
496 // OK
497 break;
498 case STATUS_ACTIVE:
499 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
500 mRequestThread->setPaused(true);
501 res = waitUntilDrainedLocked();
502 if (res != OK) {
503 ALOGE("%s: Can't pause captures to reconfigure streams!",
504 __FUNCTION__);
505 mStatus = STATUS_ERROR;
506 return res;
507 }
508 wasActive = true;
509 break;
510 default:
511 ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus);
512 return INVALID_OPERATION;
513 }
514 assert(mStatus == STATUS_IDLE);
515
516 if (mInputStream != 0) {
517 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
518 return INVALID_OPERATION;
519 }
520
521 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
522 width, height, depth);
523
524 res = mOutputStreams.add(mNextStreamId, newStream);
525 if (res < 0) {
526 ALOGE("%s: Can't add new stream to set: %s (%d)",
527 __FUNCTION__, strerror(-res), res);
528 return res;
529 }
530 mInputStream = newStream;
531
532 *id = mNextStreamId++;
533 *zslStream = newStream;
534
535 // Continue captures if active at start
536 if (wasActive) {
537 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
538 res = configureStreamsLocked();
539 if (res != OK) {
540 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
541 __FUNCTION__, mNextStreamId, strerror(-res), res);
542 return res;
543 }
544 mRequestThread->setPaused(false);
545 }
546
547 return OK;
548}
549
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800550status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
551 uint32_t width, uint32_t height, int format, size_t size, int *id) {
552 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800553 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800554
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800555 status_t res;
556 bool wasActive = false;
557
558 switch (mStatus) {
559 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700560 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800561 return INVALID_OPERATION;
562 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700563 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800564 return INVALID_OPERATION;
565 case STATUS_IDLE:
566 // OK
567 break;
568 case STATUS_ACTIVE:
569 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
570 mRequestThread->setPaused(true);
571 res = waitUntilDrainedLocked();
572 if (res != OK) {
573 ALOGE("%s: Can't pause captures to reconfigure streams!",
574 __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800575 return res;
576 }
577 wasActive = true;
578 break;
579 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700580 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800581 return INVALID_OPERATION;
582 }
583 assert(mStatus == STATUS_IDLE);
584
585 sp<Camera3OutputStream> newStream;
586 if (format == HAL_PIXEL_FORMAT_BLOB) {
587 newStream = new Camera3OutputStream(mNextStreamId, consumer,
588 width, height, size, format);
589 } else {
590 newStream = new Camera3OutputStream(mNextStreamId, consumer,
591 width, height, format);
592 }
593
594 res = mOutputStreams.add(mNextStreamId, newStream);
595 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700596 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800597 return res;
598 }
599
600 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700601 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800602
603 // Continue captures if active at start
604 if (wasActive) {
605 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
606 res = configureStreamsLocked();
607 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700608 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
609 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800610 return res;
611 }
612 mRequestThread->setPaused(false);
613 }
614
615 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800616}
617
618status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
619 ATRACE_CALL();
620 (void)outputId; (void)id;
621
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700622 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800623 return INVALID_OPERATION;
624}
625
626
627status_t Camera3Device::getStreamInfo(int id,
628 uint32_t *width, uint32_t *height, uint32_t *format) {
629 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800630 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800631
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800632 switch (mStatus) {
633 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700634 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800635 return INVALID_OPERATION;
636 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700637 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800638 return INVALID_OPERATION;
639 case STATUS_IDLE:
640 case STATUS_ACTIVE:
641 // OK
642 break;
643 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700644 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800645 return INVALID_OPERATION;
646 }
647
648 ssize_t idx = mOutputStreams.indexOfKey(id);
649 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700650 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800651 return idx;
652 }
653
654 if (width) *width = mOutputStreams[idx]->getWidth();
655 if (height) *height = mOutputStreams[idx]->getHeight();
656 if (format) *format = mOutputStreams[idx]->getFormat();
657
658 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800659}
660
661status_t Camera3Device::setStreamTransform(int id,
662 int transform) {
663 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800664 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800665
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800666 switch (mStatus) {
667 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700668 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800669 return INVALID_OPERATION;
670 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700671 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800672 return INVALID_OPERATION;
673 case STATUS_IDLE:
674 case STATUS_ACTIVE:
675 // OK
676 break;
677 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700678 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800679 return INVALID_OPERATION;
680 }
681
682 ssize_t idx = mOutputStreams.indexOfKey(id);
683 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700684 CLOGE("Stream %d does not exist",
685 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800686 return BAD_VALUE;
687 }
688
689 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800690}
691
692status_t Camera3Device::deleteStream(int id) {
693 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800694 Mutex::Autolock l(mLock);
695 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800696
Igor Murashkine2172be2013-05-28 15:31:39 -0700697 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
698
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800699 // CameraDevice semantics require device to already be idle before
700 // deleteStream is called, unlike for createStream.
701 if (mStatus != STATUS_IDLE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700702 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
703 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800704 }
705
Igor Murashkin2fba5842013-04-22 14:03:54 -0700706 sp<Camera3StreamInterface> deletedStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800707 if (mInputStream != NULL && id == mInputStream->getId()) {
708 deletedStream = mInputStream;
709 mInputStream.clear();
710 } else {
711 ssize_t idx = mOutputStreams.indexOfKey(id);
712 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700713 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800714 return BAD_VALUE;
715 }
716 deletedStream = mOutputStreams.editValueAt(idx);
717 mOutputStreams.removeItem(id);
718 }
719
720 // Free up the stream endpoint so that it can be used by some other stream
721 res = deletedStream->disconnect();
722 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700723 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800724 // fall through since we want to still list the stream as deleted.
725 }
726 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700727 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800728
729 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800730}
731
732status_t Camera3Device::deleteReprocessStream(int id) {
733 ATRACE_CALL();
734 (void)id;
735
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700736 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800737 return INVALID_OPERATION;
738}
739
740
741status_t Camera3Device::createDefaultRequest(int templateId,
742 CameraMetadata *request) {
743 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700744 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800745 Mutex::Autolock l(mLock);
746
747 switch (mStatus) {
748 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700749 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800750 return INVALID_OPERATION;
751 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700752 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800753 return INVALID_OPERATION;
754 case STATUS_IDLE:
755 case STATUS_ACTIVE:
756 // OK
757 break;
758 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700759 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800760 return INVALID_OPERATION;
761 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800762
763 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700764 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800765 rawRequest = mHal3Device->ops->construct_default_request_settings(
766 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700767 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700768 if (rawRequest == NULL) {
769 SET_ERR_L("HAL is unable to construct default settings for template %d",
770 templateId);
771 return DEAD_OBJECT;
772 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800773 *request = rawRequest;
774
775 return OK;
776}
777
778status_t Camera3Device::waitUntilDrained() {
779 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800780 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800781
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800782 return waitUntilDrainedLocked();
783}
784
785status_t Camera3Device::waitUntilDrainedLocked() {
786 ATRACE_CALL();
787 status_t res;
788
789 switch (mStatus) {
790 case STATUS_UNINITIALIZED:
791 case STATUS_IDLE:
792 ALOGV("%s: Already idle", __FUNCTION__);
793 return OK;
794 case STATUS_ERROR:
795 case STATUS_ACTIVE:
796 // Need to shut down
797 break;
798 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700799 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800800 return INVALID_OPERATION;
801 }
802
803 if (mRequestThread != NULL) {
804 res = mRequestThread->waitUntilPaused(kShutdownTimeout);
805 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700806 SET_ERR_L("Can't stop request thread in %f seconds!",
807 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800808 return res;
809 }
810 }
811 if (mInputStream != NULL) {
812 res = mInputStream->waitUntilIdle(kShutdownTimeout);
813 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700814 SET_ERR_L("Can't idle input stream %d in %f seconds!",
815 mInputStream->getId(), kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800816 return res;
817 }
818 }
819 for (size_t i = 0; i < mOutputStreams.size(); i++) {
820 res = mOutputStreams.editValueAt(i)->waitUntilIdle(kShutdownTimeout);
821 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700822 SET_ERR_L("Can't idle output stream %d in %f seconds!",
823 mOutputStreams.keyAt(i), kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800824 return res;
825 }
826 }
827
828 if (mStatus != STATUS_ERROR) {
829 mStatus = STATUS_IDLE;
830 }
831
832 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800833}
834
835status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
836 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700837 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800838
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700839 if (listener != NULL && mListener != NULL) {
840 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
841 }
842 mListener = listener;
843
844 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800845}
846
847status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700848 ATRACE_CALL();
849 status_t res;
850 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800851
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700852 while (mResultQueue.empty()) {
853 res = mResultSignal.waitRelative(mOutputLock, timeout);
854 if (res == TIMED_OUT) {
855 return res;
856 } else if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700857 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)",
858 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700859 return res;
860 }
861 }
862 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800863}
864
865status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
866 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700867 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800868
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700869 if (mResultQueue.empty()) {
870 return NOT_ENOUGH_DATA;
871 }
872
873 CameraMetadata &result = *(mResultQueue.begin());
874 frame->acquire(result);
875 mResultQueue.erase(mResultQueue.begin());
876
877 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800878}
879
880status_t Camera3Device::triggerAutofocus(uint32_t id) {
881 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800882
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700883 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
884 // Mix-in this trigger into the next request and only the next request.
885 RequestTrigger trigger[] = {
886 {
887 ANDROID_CONTROL_AF_TRIGGER,
888 ANDROID_CONTROL_AF_TRIGGER_START
889 },
890 {
891 ANDROID_CONTROL_AF_TRIGGER_ID,
892 static_cast<int32_t>(id)
893 },
894 };
895
896 return mRequestThread->queueTrigger(trigger,
897 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800898}
899
900status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
901 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800902
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700903 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
904 // Mix-in this trigger into the next request and only the next request.
905 RequestTrigger trigger[] = {
906 {
907 ANDROID_CONTROL_AF_TRIGGER,
908 ANDROID_CONTROL_AF_TRIGGER_CANCEL
909 },
910 {
911 ANDROID_CONTROL_AF_TRIGGER_ID,
912 static_cast<int32_t>(id)
913 },
914 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800915
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700916 return mRequestThread->queueTrigger(trigger,
917 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800918}
919
920status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
921 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800922
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700923 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
924 // Mix-in this trigger into the next request and only the next request.
925 RequestTrigger trigger[] = {
926 {
927 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
928 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
929 },
930 {
931 ANDROID_CONTROL_AE_PRECAPTURE_ID,
932 static_cast<int32_t>(id)
933 },
934 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800935
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700936 return mRequestThread->queueTrigger(trigger,
937 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800938}
939
940status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
941 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
942 ATRACE_CALL();
943 (void)reprocessStreamId; (void)buffer; (void)listener;
944
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700945 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800946 return INVALID_OPERATION;
947}
948
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800949/**
950 * Camera3Device private methods
951 */
952
953sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
954 const CameraMetadata &request) {
955 ATRACE_CALL();
956 status_t res;
957
958 sp<CaptureRequest> newRequest = new CaptureRequest;
959 newRequest->mSettings = request;
960
961 camera_metadata_entry_t inputStreams =
962 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
963 if (inputStreams.count > 0) {
964 if (mInputStream == NULL ||
965 mInputStream->getId() != inputStreams.data.u8[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700966 CLOGE("Request references unknown input stream %d",
967 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800968 return NULL;
969 }
970 // Lazy completion of stream configuration (allocation/registration)
971 // on first use
972 if (mInputStream->isConfiguring()) {
973 res = mInputStream->finishConfiguration(mHal3Device);
974 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700975 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800976 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700977 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800978 return NULL;
979 }
980 }
981
982 newRequest->mInputStream = mInputStream;
983 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
984 }
985
986 camera_metadata_entry_t streams =
987 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
988 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700989 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800990 return NULL;
991 }
992
993 for (size_t i = 0; i < streams.count; i++) {
994 int idx = mOutputStreams.indexOfKey(streams.data.u8[i]);
995 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700996 CLOGE("Request references unknown stream %d",
997 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800998 return NULL;
999 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001000 sp<Camera3OutputStreamInterface> stream =
1001 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001002
1003 // Lazy completion of stream configuration (allocation/registration)
1004 // on first use
1005 if (stream->isConfiguring()) {
1006 res = stream->finishConfiguration(mHal3Device);
1007 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001008 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1009 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001010 return NULL;
1011 }
1012 }
1013
1014 newRequest->mOutputStreams.push(stream);
1015 }
1016 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1017
1018 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001019}
1020
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001021status_t Camera3Device::configureStreamsLocked() {
1022 ATRACE_CALL();
1023 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001024
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001025 if (mStatus != STATUS_IDLE) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001026 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001027 return INVALID_OPERATION;
1028 }
1029
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001030 if (!mNeedConfig) {
1031 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
Eino-Ville Talvala31fdb292013-06-12 17:06:41 -07001032 mStatus = STATUS_ACTIVE;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001033 return OK;
1034 }
1035
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001036 // Start configuring the streams
1037
1038 camera3_stream_configuration config;
1039
1040 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1041
1042 Vector<camera3_stream_t*> streams;
1043 streams.setCapacity(config.num_streams);
1044
1045 if (mInputStream != NULL) {
1046 camera3_stream_t *inputStream;
1047 inputStream = mInputStream->startConfiguration();
1048 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001049 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001050 return INVALID_OPERATION;
1051 }
1052 streams.add(inputStream);
1053 }
1054
1055 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001056
1057 // Don't configure bidi streams twice, nor add them twice to the list
1058 if (mOutputStreams[i].get() ==
1059 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1060
1061 config.num_streams--;
1062 continue;
1063 }
1064
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001065 camera3_stream_t *outputStream;
1066 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1067 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001068 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 return INVALID_OPERATION;
1070 }
1071 streams.add(outputStream);
1072 }
1073
1074 config.streams = streams.editArray();
1075
1076 // Do the HAL configuration; will potentially touch stream
1077 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001078 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001079 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001080 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001081
1082 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001083 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1084 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001085 return res;
1086 }
1087
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001088 // Finish all stream configuration immediately.
1089 // TODO: Try to relax this later back to lazy completion, which should be
1090 // faster
1091
Igor Murashkin073f8572013-05-02 14:59:28 -07001092 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001093 res = mInputStream->finishConfiguration(mHal3Device);
1094 if (res != OK) {
1095 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1096 mInputStream->getId(), strerror(-res), res);
1097 return res;
1098 }
1099 }
1100
1101 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001102 sp<Camera3OutputStreamInterface> outputStream =
1103 mOutputStreams.editValueAt(i);
1104 if (outputStream->isConfiguring()) {
1105 res = outputStream->finishConfiguration(mHal3Device);
1106 if (res != OK) {
1107 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1108 outputStream->getId(), strerror(-res), res);
1109 return res;
1110 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001111 }
1112 }
1113
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001114 // Request thread needs to know to avoid using repeat-last-settings protocol
1115 // across configure_streams() calls
1116 mRequestThread->configurationComplete();
1117
1118 // Finish configuring the streams lazily on first reference
1119
1120 mStatus = STATUS_ACTIVE;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001121 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001122
1123 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001124}
1125
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001126void Camera3Device::setErrorState(const char *fmt, ...) {
1127 Mutex::Autolock l(mLock);
1128 va_list args;
1129 va_start(args, fmt);
1130
1131 setErrorStateLockedV(fmt, args);
1132
1133 va_end(args);
1134}
1135
1136void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1137 Mutex::Autolock l(mLock);
1138 setErrorStateLockedV(fmt, args);
1139}
1140
1141void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1142 va_list args;
1143 va_start(args, fmt);
1144
1145 setErrorStateLockedV(fmt, args);
1146
1147 va_end(args);
1148}
1149
1150void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001151 // Print out all error messages to log
1152 String8 errorCause = String8::formatV(fmt, args);
1153 ALOGE("Camera %d: %s", mId, errorCause.string());
1154
1155 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001156 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001157
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001158 mErrorCause = errorCause;
1159
1160 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001161 mStatus = STATUS_ERROR;
1162}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001163
1164/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001165 * In-flight request management
1166 */
1167
1168status_t Camera3Device::registerInFlight(int32_t frameNumber,
1169 int32_t numBuffers) {
1170 ATRACE_CALL();
1171 Mutex::Autolock l(mInFlightLock);
1172
1173 ssize_t res;
1174 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers));
1175 if (res < 0) return res;
1176
1177 return OK;
1178}
1179
1180/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001181 * Camera HAL device callback methods
1182 */
1183
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001184void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001185 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001186
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001187 status_t res;
1188
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001189 uint32_t frameNumber = result->frame_number;
1190 if (result->result == NULL && result->num_output_buffers == 0) {
1191 SET_ERR("No result data provided by HAL for frame %d",
1192 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001193 return;
1194 }
1195
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001196 // Get capture timestamp from list of in-flight requests, where it was added
1197 // by the shutter notification for this frame. Then update the in-flight
1198 // status and remove the in-flight entry if all result data has been
1199 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001200 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001201 {
1202 Mutex::Autolock l(mInFlightLock);
1203 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1204 if (idx == NAME_NOT_FOUND) {
1205 SET_ERR("Unknown frame number for capture result: %d",
1206 frameNumber);
1207 return;
1208 }
1209 InFlightRequest &request = mInFlightMap.editValueAt(idx);
1210 timestamp = request.captureTimestamp;
1211 if (timestamp == 0) {
1212 SET_ERR("Called before shutter notify for frame %d",
1213 frameNumber);
1214 return;
1215 }
1216
1217 if (result->result != NULL) {
1218 if (request.haveResultMetadata) {
1219 SET_ERR("Called multiple times with metadata for frame %d",
1220 frameNumber);
1221 return;
1222 }
1223 request.haveResultMetadata = true;
1224 }
1225
1226 request.numBuffersLeft -= result->num_output_buffers;
1227
1228 if (request.numBuffersLeft < 0) {
1229 SET_ERR("Too many buffers returned for frame %d",
1230 frameNumber);
1231 return;
1232 }
1233
1234 if (request.haveResultMetadata && request.numBuffersLeft == 0) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001235 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001236 mInFlightMap.removeItemsAt(idx, 1);
1237 }
1238
1239 // Sanity check - if we have too many in-flight frames, something has
1240 // likely gone wrong
1241 if (mInFlightMap.size() > kInFlightWarnLimit) {
1242 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1243 }
1244
1245 }
1246
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001247 AlgState cur3aState;
1248 AlgState new3aState;
1249 int32_t aeTriggerId = 0;
1250 int32_t afTriggerId = 0;
1251
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001252 NotificationListener *listener = NULL;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001253
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001254 // Process the result metadata, if provided
1255 if (result->result != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001256 Mutex::Autolock l(mOutputLock);
1257
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001258 if (frameNumber != mNextResultFrameNumber) {
1259 SET_ERR("Out-of-order capture result metadata submitted! "
1260 "(got frame number %d, expecting %d)",
1261 frameNumber, mNextResultFrameNumber);
1262 return;
1263 }
1264 mNextResultFrameNumber++;
1265
1266 CameraMetadata &captureResult =
1267 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001268
1269 captureResult = result->result;
Igor Murashkind2c90692013-04-02 12:32:32 -07001270 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001271 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001272 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001273 frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001274 } else {
1275 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001276 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001277 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001278
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001279 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001280
1281 camera_metadata_entry entry =
1282 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1283 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001284 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001285 frameNumber);
Alex Rayfe7e0c62013-05-30 00:12:13 -07001286 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001287 SET_ERR("Timestamp mismatch between shutter notify and result"
1288 " metadata for frame %d (%lld vs %lld respectively)",
1289 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001290 }
1291
1292 // Get 3A states from result metadata
1293
1294 entry = captureResult.find(ANDROID_CONTROL_AE_STATE);
1295 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001296 CLOGE("No AE state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001297 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001298 } else {
1299 new3aState.aeState =
1300 static_cast<camera_metadata_enum_android_control_ae_state>(
1301 entry.data.u8[0]);
1302 }
1303
1304 entry = captureResult.find(ANDROID_CONTROL_AF_STATE);
1305 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001306 CLOGE("No AF state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001307 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001308 } else {
1309 new3aState.afState =
1310 static_cast<camera_metadata_enum_android_control_af_state>(
1311 entry.data.u8[0]);
1312 }
1313
1314 entry = captureResult.find(ANDROID_CONTROL_AWB_STATE);
1315 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001316 CLOGE("No AWB state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001317 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001318 } else {
1319 new3aState.awbState =
1320 static_cast<camera_metadata_enum_android_control_awb_state>(
1321 entry.data.u8[0]);
1322 }
1323
1324 entry = captureResult.find(ANDROID_CONTROL_AF_TRIGGER_ID);
1325 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001326 CLOGE("No AF trigger ID provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001327 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001328 } else {
1329 afTriggerId = entry.data.i32[0];
1330 }
1331
1332 entry = captureResult.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
1333 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001334 CLOGE("No AE precapture trigger ID provided by HAL"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001335 " for frame %d!", frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001336 } else {
1337 aeTriggerId = entry.data.i32[0];
1338 }
1339
1340 listener = mListener;
1341 cur3aState = m3AState;
1342
1343 m3AState = new3aState;
1344 } // scope for mOutputLock
1345
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001346 // Return completed buffers to their streams with the timestamp
1347
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001348 for (size_t i = 0; i < result->num_output_buffers; i++) {
1349 Camera3Stream *stream =
1350 Camera3Stream::cast(result->output_buffers[i].stream);
1351 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1352 // Note: stream may be deallocated at this point, if this buffer was the
1353 // last reference to it.
1354 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001355 SET_ERR("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001356 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001357 }
1358 }
1359
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001360 // Finally, dispatch any 3A change events to listeners if we got metadata
1361
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001362 if (result->result != NULL) {
1363 mResultSignal.signal();
1364 }
1365
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001366 if (result->result != NULL && listener != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001367 if (new3aState.aeState != cur3aState.aeState) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001368 ALOGVV("%s: AE state changed from 0x%x to 0x%x",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001369 __FUNCTION__, cur3aState.aeState, new3aState.aeState);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001370 listener->notifyAutoExposure(new3aState.aeState, aeTriggerId);
1371 }
1372 if (new3aState.afState != cur3aState.afState) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001373 ALOGVV("%s: AF state changed from 0x%x to 0x%x",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001374 __FUNCTION__, cur3aState.afState, new3aState.afState);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001375 listener->notifyAutoFocus(new3aState.afState, afTriggerId);
1376 }
1377 if (new3aState.awbState != cur3aState.awbState) {
1378 listener->notifyAutoWhitebalance(new3aState.awbState, aeTriggerId);
1379 }
1380 }
1381
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001382}
1383
1384void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001385 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001386 NotificationListener *listener;
1387 {
1388 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001389 listener = mListener;
1390 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001391
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001392 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001393 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001394 return;
1395 }
1396
1397 switch (msg->type) {
1398 case CAMERA3_MSG_ERROR: {
1399 int streamId = 0;
1400 if (msg->message.error.error_stream != NULL) {
1401 Camera3Stream *stream =
1402 Camera3Stream::cast(
1403 msg->message.error.error_stream);
1404 streamId = stream->getId();
1405 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001406 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1407 mId, __FUNCTION__, msg->message.error.frame_number,
1408 streamId, msg->message.error.error_code);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001409 if (listener != NULL) {
1410 listener->notifyError(msg->message.error.error_code,
1411 msg->message.error.frame_number, streamId);
1412 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001413 break;
1414 }
1415 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001416 ssize_t idx;
1417 uint32_t frameNumber = msg->message.shutter.frame_number;
1418 nsecs_t timestamp = msg->message.shutter.timestamp;
1419 // Verify ordering of shutter notifications
1420 {
1421 Mutex::Autolock l(mOutputLock);
1422 if (frameNumber != mNextShutterFrameNumber) {
1423 SET_ERR("Shutter notification out-of-order. Expected "
1424 "notification for frame %d, got frame %d",
1425 mNextShutterFrameNumber, frameNumber);
1426 break;
1427 }
1428 mNextShutterFrameNumber++;
1429 }
1430
1431 // Set timestamp for the request in the in-flight tracking
1432 {
1433 Mutex::Autolock l(mInFlightLock);
1434 idx = mInFlightMap.indexOfKey(frameNumber);
1435 if (idx >= 0) {
1436 mInFlightMap.editValueAt(idx).captureTimestamp = timestamp;
1437 }
1438 }
1439 if (idx < 0) {
1440 SET_ERR("Shutter notification for non-existent frame number %d",
1441 frameNumber);
1442 break;
1443 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001444 ALOGVV("Camera %d: %s: Shutter fired for frame %d at %lld",
1445 mId, __FUNCTION__, frameNumber, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001446 // Call listener, if any
1447 if (listener != NULL) {
1448 listener->notifyShutter(frameNumber, timestamp);
1449 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001450 break;
1451 }
1452 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001453 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001454 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001455 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001456}
1457
1458/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001459 * RequestThread inner class methods
1460 */
1461
1462Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
1463 camera3_device_t *hal3Device) :
1464 Thread(false),
1465 mParent(parent),
1466 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001467 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001468 mReconfigured(false),
1469 mDoPause(false),
1470 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001471 mFrameNumber(0),
1472 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001473}
1474
1475void Camera3Device::RequestThread::configurationComplete() {
1476 Mutex::Autolock l(mRequestLock);
1477 mReconfigured = true;
1478}
1479
1480status_t Camera3Device::RequestThread::queueRequest(
1481 sp<CaptureRequest> request) {
1482 Mutex::Autolock l(mRequestLock);
1483 mRequestQueue.push_back(request);
1484
1485 return OK;
1486}
1487
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001488
1489status_t Camera3Device::RequestThread::queueTrigger(
1490 RequestTrigger trigger[],
1491 size_t count) {
1492
1493 Mutex::Autolock l(mTriggerMutex);
1494 status_t ret;
1495
1496 for (size_t i = 0; i < count; ++i) {
1497 ret = queueTriggerLocked(trigger[i]);
1498
1499 if (ret != OK) {
1500 return ret;
1501 }
1502 }
1503
1504 return OK;
1505}
1506
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001507int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1508 sp<Camera3Device> d = device.promote();
1509 if (d != NULL) return d->mId;
1510 return 0;
1511}
1512
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001513status_t Camera3Device::RequestThread::queueTriggerLocked(
1514 RequestTrigger trigger) {
1515
1516 uint32_t tag = trigger.metadataTag;
1517 ssize_t index = mTriggerMap.indexOfKey(tag);
1518
1519 switch (trigger.getTagType()) {
1520 case TYPE_BYTE:
1521 // fall-through
1522 case TYPE_INT32:
1523 break;
1524 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001525 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1526 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001527 return INVALID_OPERATION;
1528 }
1529
1530 /**
1531 * Collect only the latest trigger, since we only have 1 field
1532 * in the request settings per trigger tag, and can't send more than 1
1533 * trigger per request.
1534 */
1535 if (index != NAME_NOT_FOUND) {
1536 mTriggerMap.editValueAt(index) = trigger;
1537 } else {
1538 mTriggerMap.add(tag, trigger);
1539 }
1540
1541 return OK;
1542}
1543
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001544status_t Camera3Device::RequestThread::setRepeatingRequests(
1545 const RequestList &requests) {
1546 Mutex::Autolock l(mRequestLock);
1547 mRepeatingRequests.clear();
1548 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1549 requests.begin(), requests.end());
1550 return OK;
1551}
1552
1553status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1554 Mutex::Autolock l(mRequestLock);
1555 mRepeatingRequests.clear();
1556 return OK;
1557}
1558
1559void Camera3Device::RequestThread::setPaused(bool paused) {
1560 Mutex::Autolock l(mPauseLock);
1561 mDoPause = paused;
1562 mDoPauseSignal.signal();
1563}
1564
1565status_t Camera3Device::RequestThread::waitUntilPaused(nsecs_t timeout) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001566 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001567 status_t res;
1568 Mutex::Autolock l(mPauseLock);
1569 while (!mPaused) {
1570 res = mPausedSignal.waitRelative(mPauseLock, timeout);
1571 if (res == TIMED_OUT) {
1572 return res;
1573 }
1574 }
1575 return OK;
1576}
1577
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001578status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1579 int32_t requestId, nsecs_t timeout) {
1580 Mutex::Autolock l(mLatestRequestMutex);
1581 status_t res;
1582 while (mLatestRequestId != requestId) {
1583 nsecs_t startTime = systemTime();
1584
1585 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
1586 if (res != OK) return res;
1587
1588 timeout -= (systemTime() - startTime);
1589 }
1590
1591 return OK;
1592}
1593
1594
1595
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001596bool Camera3Device::RequestThread::threadLoop() {
1597
1598 status_t res;
1599
1600 // Handle paused state.
1601 if (waitIfPaused()) {
1602 return true;
1603 }
1604
1605 // Get work to do
1606
1607 sp<CaptureRequest> nextRequest = waitForNextRequest();
1608 if (nextRequest == NULL) {
1609 return true;
1610 }
1611
1612 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001613 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001614 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001615
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001616 // Insert any queued triggers (before metadata is locked)
1617 int32_t triggerCount;
1618 res = insertTriggers(nextRequest);
1619 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001620 SET_ERR("RequestThread: Unable to insert triggers "
1621 "(capture request %d, HAL device: %s (%d)",
1622 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001623 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1624 return false;
1625 }
1626 triggerCount = res;
1627
1628 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
1629
1630 // If the request is the same as last, or we had triggers last time
1631 if (mPrevRequest != nextRequest || triggersMixedIn) {
1632 /**
1633 * The request should be presorted so accesses in HAL
1634 * are O(logn). Sidenote, sorting a sorted metadata is nop.
1635 */
1636 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001637 request.settings = nextRequest->mSettings.getAndLock();
1638 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001639 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
1640
1641 IF_ALOGV() {
1642 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
1643 find_camera_metadata_ro_entry(
1644 request.settings,
1645 ANDROID_CONTROL_AF_TRIGGER,
1646 &e
1647 );
1648 if (e.count > 0) {
1649 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
1650 __FUNCTION__,
1651 mFrameNumber+1,
1652 e.data.u8[0]);
1653 }
1654 }
1655 } else {
1656 // leave request.settings NULL to indicate 'reuse latest given'
1657 ALOGVV("%s: Request settings are REUSED",
1658 __FUNCTION__);
1659 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001660
1661 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001662
1663 // Fill in buffers
1664
1665 if (nextRequest->mInputStream != NULL) {
1666 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001667 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001668 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001669 SET_ERR("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001670 " %s (%d)", strerror(-res), res);
1671 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1672 return true;
1673 }
1674 } else {
1675 request.input_buffer = NULL;
1676 }
1677
1678 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
1679 nextRequest->mOutputStreams.size());
1680 request.output_buffers = outputBuffers.array();
1681 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
1682 res = nextRequest->mOutputStreams.editItemAt(i)->
1683 getBuffer(&outputBuffers.editItemAt(i));
1684 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001685 SET_ERR("RequestThread: Can't get output buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001686 "%s (%d)", strerror(-res), res);
1687 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1688 return true;
1689 }
1690 request.num_output_buffers++;
1691 }
1692
1693 request.frame_number = mFrameNumber++;
1694
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001695 // Log request in the in-flight queue
1696 sp<Camera3Device> parent = mParent.promote();
1697 if (parent == NULL) {
1698 CLOGE("RequestThread: Parent is gone");
1699 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1700 return false;
1701 }
1702
1703 res = parent->registerInFlight(request.frame_number,
1704 request.num_output_buffers);
1705 if (res != OK) {
1706 SET_ERR("RequestThread: Unable to register new in-flight request:"
1707 " %s (%d)", strerror(-res), res);
1708 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1709 return false;
1710 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001711
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001712 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001713 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
1714 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001715 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001716 ATRACE_END();
1717
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001718 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001719 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001720 " device: %s (%d)", request.frame_number, strerror(-res), res);
1721 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1722 return false;
1723 }
1724
1725 if (request.settings != NULL) {
1726 nextRequest->mSettings.unlock(request.settings);
1727 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001728
1729 // Remove any previously queued triggers (after unlock)
1730 res = removeTriggers(mPrevRequest);
1731 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001732 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001733 "(capture request %d, HAL device: %s (%d)",
1734 request.frame_number, strerror(-res), res);
1735 return false;
1736 }
1737 mPrevTriggers = triggerCount;
1738
1739 // Read android.request.id from the request settings metadata
1740 // - inform waitUntilRequestProcessed thread of a new request ID
1741 {
1742 Mutex::Autolock al(mLatestRequestMutex);
1743
1744 camera_metadata_entry_t requestIdEntry =
1745 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
1746 if (requestIdEntry.count > 0) {
1747 mLatestRequestId = requestIdEntry.data.i32[0];
1748 } else {
1749 ALOGW("%s: Did not have android.request.id set in the request",
1750 __FUNCTION__);
1751 mLatestRequestId = NAME_NOT_FOUND;
1752 }
1753
1754 mLatestRequestSignal.signal();
1755 }
1756
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001757 // Return input buffer back to framework
1758 if (request.input_buffer != NULL) {
1759 Camera3Stream *stream =
1760 Camera3Stream::cast(request.input_buffer->stream);
1761 res = stream->returnInputBuffer(*(request.input_buffer));
1762 // Note: stream may be deallocated at this point, if this buffer was the
1763 // last reference to it.
1764 if (res != OK) {
1765 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1766 " its stream:%s (%d)", __FUNCTION__,
1767 request.frame_number, strerror(-res), res);
1768 // TODO: Report error upstream
1769 }
1770 }
1771
1772
1773
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001774 return true;
1775}
1776
1777void Camera3Device::RequestThread::cleanUpFailedRequest(
1778 camera3_capture_request_t &request,
1779 sp<CaptureRequest> &nextRequest,
1780 Vector<camera3_stream_buffer_t> &outputBuffers) {
1781
1782 if (request.settings != NULL) {
1783 nextRequest->mSettings.unlock(request.settings);
1784 }
1785 if (request.input_buffer != NULL) {
1786 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001787 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001788 }
1789 for (size_t i = 0; i < request.num_output_buffers; i++) {
1790 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
1791 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
1792 outputBuffers[i], 0);
1793 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001794}
1795
1796sp<Camera3Device::CaptureRequest>
1797 Camera3Device::RequestThread::waitForNextRequest() {
1798 status_t res;
1799 sp<CaptureRequest> nextRequest;
1800
1801 // Optimized a bit for the simple steady-state case (single repeating
1802 // request), to avoid putting that request in the queue temporarily.
1803 Mutex::Autolock l(mRequestLock);
1804
1805 while (mRequestQueue.empty()) {
1806 if (!mRepeatingRequests.empty()) {
1807 // Always atomically enqueue all requests in a repeating request
1808 // list. Guarantees a complete in-sequence set of captures to
1809 // application.
1810 const RequestList &requests = mRepeatingRequests;
1811 RequestList::const_iterator firstRequest =
1812 requests.begin();
1813 nextRequest = *firstRequest;
1814 mRequestQueue.insert(mRequestQueue.end(),
1815 ++firstRequest,
1816 requests.end());
1817 // No need to wait any longer
1818 break;
1819 }
1820
1821 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
1822
1823 if (res == TIMED_OUT) {
1824 // Signal that we're paused by starvation
1825 Mutex::Autolock pl(mPauseLock);
1826 if (mPaused == false) {
1827 mPaused = true;
1828 mPausedSignal.signal();
1829 }
1830 // Stop waiting for now and let thread management happen
1831 return NULL;
1832 }
1833 }
1834
1835 if (nextRequest == NULL) {
1836 // Don't have a repeating request already in hand, so queue
1837 // must have an entry now.
1838 RequestList::iterator firstRequest =
1839 mRequestQueue.begin();
1840 nextRequest = *firstRequest;
1841 mRequestQueue.erase(firstRequest);
1842 }
1843
1844 // Not paused
1845 Mutex::Autolock pl(mPauseLock);
1846 mPaused = false;
1847
1848 // Check if we've reconfigured since last time, and reset the preview
1849 // request if so. Can't use 'NULL request == repeat' across configure calls.
1850 if (mReconfigured) {
1851 mPrevRequest.clear();
1852 mReconfigured = false;
1853 }
1854
1855 return nextRequest;
1856}
1857
1858bool Camera3Device::RequestThread::waitIfPaused() {
1859 status_t res;
1860 Mutex::Autolock l(mPauseLock);
1861 while (mDoPause) {
1862 // Signal that we're paused by request
1863 if (mPaused == false) {
1864 mPaused = true;
1865 mPausedSignal.signal();
1866 }
1867 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
1868 if (res == TIMED_OUT) {
1869 return true;
1870 }
1871 }
1872 // We don't set mPaused to false here, because waitForNextRequest needs
1873 // to further manage the paused state in case of starvation.
1874 return false;
1875}
1876
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001877void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
1878 sp<Camera3Device> parent = mParent.promote();
1879 if (parent != NULL) {
1880 va_list args;
1881 va_start(args, fmt);
1882
1883 parent->setErrorStateV(fmt, args);
1884
1885 va_end(args);
1886 }
1887}
1888
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001889status_t Camera3Device::RequestThread::insertTriggers(
1890 const sp<CaptureRequest> &request) {
1891
1892 Mutex::Autolock al(mTriggerMutex);
1893
1894 CameraMetadata &metadata = request->mSettings;
1895 size_t count = mTriggerMap.size();
1896
1897 for (size_t i = 0; i < count; ++i) {
1898 RequestTrigger trigger = mTriggerMap.valueAt(i);
1899
1900 uint32_t tag = trigger.metadataTag;
1901 camera_metadata_entry entry = metadata.find(tag);
1902
1903 if (entry.count > 0) {
1904 /**
1905 * Already has an entry for this trigger in the request.
1906 * Rewrite it with our requested trigger value.
1907 */
1908 RequestTrigger oldTrigger = trigger;
1909
1910 oldTrigger.entryValue = entry.data.u8[0];
1911
1912 mTriggerReplacedMap.add(tag, oldTrigger);
1913 } else {
1914 /**
1915 * More typical, no trigger entry, so we just add it
1916 */
1917 mTriggerRemovedMap.add(tag, trigger);
1918 }
1919
1920 status_t res;
1921
1922 switch (trigger.getTagType()) {
1923 case TYPE_BYTE: {
1924 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1925 res = metadata.update(tag,
1926 &entryValue,
1927 /*count*/1);
1928 break;
1929 }
1930 case TYPE_INT32:
1931 res = metadata.update(tag,
1932 &trigger.entryValue,
1933 /*count*/1);
1934 break;
1935 default:
1936 ALOGE("%s: Type not supported: 0x%x",
1937 __FUNCTION__,
1938 trigger.getTagType());
1939 return INVALID_OPERATION;
1940 }
1941
1942 if (res != OK) {
1943 ALOGE("%s: Failed to update request metadata with trigger tag %s"
1944 ", value %d", __FUNCTION__, trigger.getTagName(),
1945 trigger.entryValue);
1946 return res;
1947 }
1948
1949 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
1950 trigger.getTagName(),
1951 trigger.entryValue);
1952 }
1953
1954 mTriggerMap.clear();
1955
1956 return count;
1957}
1958
1959status_t Camera3Device::RequestThread::removeTriggers(
1960 const sp<CaptureRequest> &request) {
1961 Mutex::Autolock al(mTriggerMutex);
1962
1963 CameraMetadata &metadata = request->mSettings;
1964
1965 /**
1966 * Replace all old entries with their old values.
1967 */
1968 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
1969 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
1970
1971 status_t res;
1972
1973 uint32_t tag = trigger.metadataTag;
1974 switch (trigger.getTagType()) {
1975 case TYPE_BYTE: {
1976 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1977 res = metadata.update(tag,
1978 &entryValue,
1979 /*count*/1);
1980 break;
1981 }
1982 case TYPE_INT32:
1983 res = metadata.update(tag,
1984 &trigger.entryValue,
1985 /*count*/1);
1986 break;
1987 default:
1988 ALOGE("%s: Type not supported: 0x%x",
1989 __FUNCTION__,
1990 trigger.getTagType());
1991 return INVALID_OPERATION;
1992 }
1993
1994 if (res != OK) {
1995 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
1996 ", trigger value %d", __FUNCTION__,
1997 trigger.getTagName(), trigger.entryValue);
1998 return res;
1999 }
2000 }
2001 mTriggerReplacedMap.clear();
2002
2003 /**
2004 * Remove all new entries.
2005 */
2006 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2007 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2008 status_t res = metadata.erase(trigger.metadataTag);
2009
2010 if (res != OK) {
2011 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2012 ", trigger value %d", __FUNCTION__,
2013 trigger.getTagName(), trigger.entryValue);
2014 return res;
2015 }
2016 }
2017 mTriggerRemovedMap.clear();
2018
2019 return OK;
2020}
2021
2022
2023
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002024/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002025 * Static callback forwarding methods from HAL to instance
2026 */
2027
2028void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2029 const camera3_capture_result *result) {
2030 Camera3Device *d =
2031 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2032 d->processCaptureResult(result);
2033}
2034
2035void Camera3Device::sNotify(const camera3_callback_ops *cb,
2036 const camera3_notify_msg *msg) {
2037 Camera3Device *d =
2038 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2039 d->notify(msg);
2040}
2041
2042}; // namespace android