blob: 5d7423c05fd92175e2a825a0e3fc1c527fe47ffd [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
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Igor Murashkinff3e31d2013-10-23 16:40:06 -070046#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070047#include "device3/Camera3Device.h"
48#include "device3/Camera3OutputStream.h"
49#include "device3/Camera3InputStream.h"
50#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070051#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070052#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080053
54using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080055
56namespace android {
57
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058Camera3Device::Camera3Device(int id):
59 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070060 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080061 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070062 mStatus(STATUS_UNINITIALIZED),
Zhijun He204e3292014-07-14 17:09:23 -070063 mUsePartialResult(false),
64 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070065 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070066 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070067 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070068 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080069{
70 ATRACE_CALL();
71 camera3_callback_ops::notify = &sNotify;
72 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
73 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
74}
75
76Camera3Device::~Camera3Device()
77{
78 ATRACE_CALL();
79 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
80 disconnect();
81}
82
Igor Murashkin71381052013-03-04 14:53:08 -080083int Camera3Device::getId() const {
84 return mId;
85}
86
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080087/**
88 * CameraDeviceBase interface
89 */
90
Yin-Chia Yehe074a932015-01-30 10:29:02 -080091status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080092{
93 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070094 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080095 Mutex::Autolock l(mLock);
96
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080097 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080098 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070099 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800100 return INVALID_OPERATION;
101 }
102
103 /** Open HAL device */
104
105 status_t res;
106 String8 deviceName = String8::format("%d", mId);
107
108 camera3_device_t *device;
109
Zhijun He213ce792013-11-19 08:45:15 -0800110 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800111 res = module->open(deviceName.string(),
112 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800113 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800114
115 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700116 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117 return res;
118 }
119
120 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700121 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700123 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700124 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800125 device->common.version);
126 device->common.close(&device->common);
127 return BAD_VALUE;
128 }
129
130 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800131 res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700132 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800133 if (res != OK) return res;
134
135 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700136 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
137 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700138 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 device->common.close(&device->common);
140 return BAD_VALUE;
141 }
142
143 /** Initialize device with callback functions */
144
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700145 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800146 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700147 ATRACE_END();
148
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800149 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700150 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
151 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800152 device->common.close(&device->common);
153 return BAD_VALUE;
154 }
155
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700156 /** Start up status tracker thread */
157 mStatusTracker = new StatusTracker(this);
158 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
159 if (res != OK) {
160 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
161 strerror(-res), res);
162 device->common.close(&device->common);
163 mStatusTracker.clear();
164 return res;
165 }
166
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700167 bool aeLockAvailable = false;
168 camera_metadata_ro_entry aeLockAvailableEntry;
169 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
170 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
171 if (res == OK && aeLockAvailableEntry.count > 0) {
172 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
173 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
174 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800175
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700176 /** Start up request queue thread */
177 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800178 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800179 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700180 SET_ERR_L("Unable to start request queue thread: %s (%d)",
181 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800182 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800183 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800184 return res;
185 }
186
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700187 mPreparerThread = new PreparerThread();
188
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800189 /** Everything is good to go */
190
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700191 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 mDeviceInfo = info.static_camera_characteristics;
193 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700194 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800195 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700196 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700197 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700198 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800199
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700200 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700201 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
202 camera_metadata_entry partialResultsCount =
203 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
204 if (partialResultsCount.count > 0) {
205 mNumPartialResults = partialResultsCount.data.i32[0];
206 mUsePartialResult = (mNumPartialResults > 1);
207 }
208 } else {
209 camera_metadata_entry partialResultsQuirk =
210 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
211 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
212 mUsePartialResult = true;
213 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700214 }
215
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700216 camera_metadata_entry configs =
217 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
218 for (uint32_t i = 0; i < configs.count; i += 4) {
219 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
220 configs.data.i32[i + 3] ==
221 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
222 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
223 configs.data.i32[i + 2]));
224 }
225 }
226
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800227 return OK;
228}
229
230status_t Camera3Device::disconnect() {
231 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700232 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800233
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800234 ALOGV("%s: E", __FUNCTION__);
235
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700236 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800237
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700238 {
239 Mutex::Autolock l(mLock);
240 if (mStatus == STATUS_UNINITIALIZED) return res;
241
242 if (mStatus == STATUS_ACTIVE ||
243 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
244 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700245 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700246 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700247 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700248 } else {
249 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
250 if (res != OK) {
251 SET_ERR_L("Timeout waiting for HAL to drain");
252 // Continue to close device even in case of error
253 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700254 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800255 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800256
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700257 if (mStatus == STATUS_ERROR) {
258 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700259 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700260
261 if (mStatusTracker != NULL) {
262 mStatusTracker->requestExit();
263 }
264
265 if (mRequestThread != NULL) {
266 mRequestThread->requestExit();
267 }
268
269 mOutputStreams.clear();
270 mInputStream.clear();
271 }
272
273 // Joining done without holding mLock, otherwise deadlocks may ensue
274 // as the threads try to access parent state
275 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
276 // HAL may be in a bad state, so waiting for request thread
277 // (which may be stuck in the HAL processCaptureRequest call)
278 // could be dangerous.
279 mRequestThread->join();
280 }
281
282 if (mStatusTracker != NULL) {
283 mStatusTracker->join();
284 }
285
286 {
287 Mutex::Autolock l(mLock);
288
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800289 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700290 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800291
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700292 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800293 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700294 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800295 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700296 mHal3Device = NULL;
297 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800298
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700299 mStatus = STATUS_UNINITIALIZED;
300 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800301
302 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700303 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800304}
305
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700306// For dumping/debugging only -
307// try to acquire a lock a few times, eventually give up to proceed with
308// debug/dump operations
309bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
310 bool gotLock = false;
311 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
312 if (lock.tryLock() == NO_ERROR) {
313 gotLock = true;
314 break;
315 } else {
316 usleep(kDumpSleepDuration);
317 }
318 }
319 return gotLock;
320}
321
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700322Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
323 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
324 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
325 const int STREAM_CONFIGURATION_SIZE = 4;
326 const int STREAM_FORMAT_OFFSET = 0;
327 const int STREAM_WIDTH_OFFSET = 1;
328 const int STREAM_HEIGHT_OFFSET = 2;
329 const int STREAM_IS_INPUT_OFFSET = 3;
330 camera_metadata_ro_entry_t availableStreamConfigs =
331 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
332 if (availableStreamConfigs.count == 0 ||
333 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
334 return Size(0, 0);
335 }
336
337 // Get max jpeg size (area-wise).
338 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
339 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
340 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
341 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
342 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
343 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
344 && format == HAL_PIXEL_FORMAT_BLOB &&
345 (width * height > maxJpegWidth * maxJpegHeight)) {
346 maxJpegWidth = width;
347 maxJpegHeight = height;
348 }
349 }
350 } else {
351 camera_metadata_ro_entry availableJpegSizes =
352 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
353 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
354 return Size(0, 0);
355 }
356
357 // Get max jpeg size (area-wise).
358 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
359 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
360 > (maxJpegWidth * maxJpegHeight)) {
361 maxJpegWidth = availableJpegSizes.data.i32[i];
362 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
363 }
364 }
365 }
366 return Size(maxJpegWidth, maxJpegHeight);
367}
368
Zhijun Hef7da0962014-04-24 13:27:56 -0700369ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700370 // Get max jpeg size (area-wise).
371 Size maxJpegResolution = getMaxJpegResolution();
372 if (maxJpegResolution.width == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700373 ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
374 __FUNCTION__, mId);
375 return BAD_VALUE;
376 }
377
Zhijun Hef7da0962014-04-24 13:27:56 -0700378 // Get max jpeg buffer size
379 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700380 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
381 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700382 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
383 return BAD_VALUE;
384 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700385 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800386 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700387
388 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700389 float scaleFactor = ((float) (width * height)) /
390 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800391 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
392 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700393 if (jpegBufferSize > maxJpegBufferSize) {
394 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700395 }
396
397 return jpegBufferSize;
398}
399
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800400status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
401 ATRACE_CALL();
402 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700403
404 // Try to lock, but continue in case of failure (to avoid blocking in
405 // deadlocks)
406 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
407 bool gotLock = tryLockSpinRightRound(mLock);
408
409 ALOGW_IF(!gotInterfaceLock,
410 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
411 mId, __FUNCTION__);
412 ALOGW_IF(!gotLock,
413 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
414 mId, __FUNCTION__);
415
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800416 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800417
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800418 const char *status =
419 mStatus == STATUS_ERROR ? "ERROR" :
420 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700421 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
422 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800423 mStatus == STATUS_ACTIVE ? "ACTIVE" :
424 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700425
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800426 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700427 if (mStatus == STATUS_ERROR) {
428 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
429 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800430 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700431 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700432 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800433
434 if (mInputStream != NULL) {
435 write(fd, lines.string(), lines.size());
436 mInputStream->dump(fd, args);
437 } else {
438 lines.appendFormat(" No input stream.\n");
439 write(fd, lines.string(), lines.size());
440 }
441 for (size_t i = 0; i < mOutputStreams.size(); i++) {
442 mOutputStreams[i]->dump(fd,args);
443 }
444
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700445 lines = String8(" In-flight requests:\n");
446 if (mInFlightMap.size() == 0) {
447 lines.append(" None\n");
448 } else {
449 for (size_t i = 0; i < mInFlightMap.size(); i++) {
450 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700451 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700452 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800453 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700454 r.numBuffersLeft);
455 }
456 }
457 write(fd, lines.string(), lines.size());
458
Igor Murashkin1e479c02013-09-06 16:55:14 -0700459 {
460 lines = String8(" Last request sent:\n");
461 write(fd, lines.string(), lines.size());
462
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700463 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700464 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
465 }
466
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800467 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700468 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800469 write(fd, lines.string(), lines.size());
470 mHal3Device->ops->dump(mHal3Device, fd);
471 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800472
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700473 if (gotLock) mLock.unlock();
474 if (gotInterfaceLock) mInterfaceLock.unlock();
475
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800476 return OK;
477}
478
479const CameraMetadata& Camera3Device::info() const {
480 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800481 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
482 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700483 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800484 mStatus == STATUS_ERROR ?
485 "when in error state" : "before init");
486 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800487 return mDeviceInfo;
488}
489
Jianing Wei90e59c92014-03-12 18:29:36 -0700490status_t Camera3Device::checkStatusOkToCaptureLocked() {
491 switch (mStatus) {
492 case STATUS_ERROR:
493 CLOGE("Device has encountered a serious error");
494 return INVALID_OPERATION;
495 case STATUS_UNINITIALIZED:
496 CLOGE("Device not initialized");
497 return INVALID_OPERATION;
498 case STATUS_UNCONFIGURED:
499 case STATUS_CONFIGURED:
500 case STATUS_ACTIVE:
501 // OK
502 break;
503 default:
504 SET_ERR_L("Unexpected status: %d", mStatus);
505 return INVALID_OPERATION;
506 }
507 return OK;
508}
509
510status_t Camera3Device::convertMetadataListToRequestListLocked(
511 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
512 if (requestList == NULL) {
513 CLOGE("requestList cannot be NULL.");
514 return BAD_VALUE;
515 }
516
Jianing Weicb0652e2014-03-12 18:29:36 -0700517 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700518 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
519 it != metadataList.end(); ++it) {
520 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
521 if (newRequest == 0) {
522 CLOGE("Can't create capture request");
523 return BAD_VALUE;
524 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700525
526 // Setup burst Id and request Id
527 newRequest->mResultExtras.burstId = burstId++;
528 if (it->exists(ANDROID_REQUEST_ID)) {
529 if (it->find(ANDROID_REQUEST_ID).count == 0) {
530 CLOGE("RequestID entry exists; but must not be empty in metadata");
531 return BAD_VALUE;
532 }
533 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
534 } else {
535 CLOGE("RequestID does not exist in metadata");
536 return BAD_VALUE;
537 }
538
Jianing Wei90e59c92014-03-12 18:29:36 -0700539 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700540
541 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700542 }
543 return OK;
544}
545
Jianing Weicb0652e2014-03-12 18:29:36 -0700546status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800547 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800548
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700549 List<const CameraMetadata> requests;
550 requests.push_back(request);
551 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800552}
553
Jianing Wei90e59c92014-03-12 18:29:36 -0700554status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700555 const List<const CameraMetadata> &requests, bool repeating,
556 /*out*/
557 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700558 ATRACE_CALL();
559 Mutex::Autolock il(mInterfaceLock);
560 Mutex::Autolock l(mLock);
561
562 status_t res = checkStatusOkToCaptureLocked();
563 if (res != OK) {
564 // error logged by previous call
565 return res;
566 }
567
568 RequestList requestList;
569
570 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
571 if (res != OK) {
572 // error logged by previous call
573 return res;
574 }
575
576 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700577 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700578 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700579 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700580 }
581
582 if (res == OK) {
583 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
584 if (res != OK) {
585 SET_ERR_L("Can't transition to active in %f seconds!",
586 kActiveTimeout/1e9);
587 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700588 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
589 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700590 } else {
591 CLOGE("Cannot queue request. Impossible.");
592 return BAD_VALUE;
593 }
594
595 return res;
596}
597
Jianing Weicb0652e2014-03-12 18:29:36 -0700598status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
599 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700600 ATRACE_CALL();
601
Jianing Weicb0652e2014-03-12 18:29:36 -0700602 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700603}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800604
Jianing Weicb0652e2014-03-12 18:29:36 -0700605status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
606 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800607 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800608
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700609 List<const CameraMetadata> requests;
610 requests.push_back(request);
611 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800612}
613
Jianing Weicb0652e2014-03-12 18:29:36 -0700614status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
615 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700616 ATRACE_CALL();
617
Jianing Weicb0652e2014-03-12 18:29:36 -0700618 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700619}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800620
621sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
622 const CameraMetadata &request) {
623 status_t res;
624
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700625 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800626 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700627 // Stream configuration failed due to unsupported configuration.
628 // Device back to unconfigured state. Client might try other configuraitons
629 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
630 CLOGE("No streams configured");
631 return NULL;
632 }
633 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800634 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700635 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800636 return NULL;
637 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700638 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700639 if (mStatus == STATUS_UNCONFIGURED) {
640 CLOGE("No streams configured");
641 return NULL;
642 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800643 }
644
645 sp<CaptureRequest> newRequest = createCaptureRequest(request);
646 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800647}
648
Jianing Weicb0652e2014-03-12 18:29:36 -0700649status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800650 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700651 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800652 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800653
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800654 switch (mStatus) {
655 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700656 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800657 return INVALID_OPERATION;
658 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700659 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800660 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700661 case STATUS_UNCONFIGURED:
662 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800663 case STATUS_ACTIVE:
664 // OK
665 break;
666 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700667 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800668 return INVALID_OPERATION;
669 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700670 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700671
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700672 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800673}
674
675status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
676 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700677 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800678
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700679 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800680}
681
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700682status_t Camera3Device::createInputStream(
683 uint32_t width, uint32_t height, int format, int *id) {
684 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700685 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700686 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700687 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
688 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700689
690 status_t res;
691 bool wasActive = false;
692
693 switch (mStatus) {
694 case STATUS_ERROR:
695 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
696 return INVALID_OPERATION;
697 case STATUS_UNINITIALIZED:
698 ALOGE("%s: Device not initialized", __FUNCTION__);
699 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700700 case STATUS_UNCONFIGURED:
701 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700702 // OK
703 break;
704 case STATUS_ACTIVE:
705 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700706 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700707 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700708 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700709 return res;
710 }
711 wasActive = true;
712 break;
713 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700714 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700715 return INVALID_OPERATION;
716 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700717 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700718
719 if (mInputStream != 0) {
720 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
721 return INVALID_OPERATION;
722 }
723
724 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
725 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700726 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700727
728 mInputStream = newStream;
729
730 *id = mNextStreamId++;
731
732 // Continue captures if active at start
733 if (wasActive) {
734 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
735 res = configureStreamsLocked();
736 if (res != OK) {
737 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
738 __FUNCTION__, mNextStreamId, strerror(-res), res);
739 return res;
740 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700741 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700742 }
743
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700744 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700745 return OK;
746}
747
Igor Murashkin2fba5842013-04-22 14:03:54 -0700748
749status_t Camera3Device::createZslStream(
750 uint32_t width, uint32_t height,
751 int depth,
752 /*out*/
753 int *id,
754 sp<Camera3ZslStream>* zslStream) {
755 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700756 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700757 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700758 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
759 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700760
761 status_t res;
762 bool wasActive = false;
763
764 switch (mStatus) {
765 case STATUS_ERROR:
766 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
767 return INVALID_OPERATION;
768 case STATUS_UNINITIALIZED:
769 ALOGE("%s: Device not initialized", __FUNCTION__);
770 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700771 case STATUS_UNCONFIGURED:
772 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700773 // OK
774 break;
775 case STATUS_ACTIVE:
776 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700777 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700778 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700779 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700780 return res;
781 }
782 wasActive = true;
783 break;
784 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700785 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700786 return INVALID_OPERATION;
787 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700788 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700789
790 if (mInputStream != 0) {
791 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
792 return INVALID_OPERATION;
793 }
794
795 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
796 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700797 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700798
799 res = mOutputStreams.add(mNextStreamId, newStream);
800 if (res < 0) {
801 ALOGE("%s: Can't add new stream to set: %s (%d)",
802 __FUNCTION__, strerror(-res), res);
803 return res;
804 }
805 mInputStream = newStream;
806
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530807 mNeedConfig = true;
808
Igor Murashkin2fba5842013-04-22 14:03:54 -0700809 *id = mNextStreamId++;
810 *zslStream = newStream;
811
812 // Continue captures if active at start
813 if (wasActive) {
814 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
815 res = configureStreamsLocked();
816 if (res != OK) {
817 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
818 __FUNCTION__, mNextStreamId, strerror(-res), res);
819 return res;
820 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700821 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700822 }
823
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700824 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700825 return OK;
826}
827
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700828status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800829 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700830 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800831 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700832 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800833 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700834 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
835 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800836
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800837 status_t res;
838 bool wasActive = false;
839
840 switch (mStatus) {
841 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700842 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800843 return INVALID_OPERATION;
844 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700845 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800846 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700847 case STATUS_UNCONFIGURED:
848 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800849 // OK
850 break;
851 case STATUS_ACTIVE:
852 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700853 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800854 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700855 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800856 return res;
857 }
858 wasActive = true;
859 break;
860 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700861 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800862 return INVALID_OPERATION;
863 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700864 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800865
866 sp<Camera3OutputStream> newStream;
867 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700868 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700869 if (jpegBufferSize <= 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700870 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
871 return BAD_VALUE;
872 }
873
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800874 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700875 width, height, jpegBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800876 } else {
877 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700878 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800879 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700880 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800881
882 res = mOutputStreams.add(mNextStreamId, newStream);
883 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700884 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800885 return res;
886 }
887
888 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700889 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800890
891 // Continue captures if active at start
892 if (wasActive) {
893 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
894 res = configureStreamsLocked();
895 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700896 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
897 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800898 return res;
899 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700900 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800901 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700902 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800903 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800904}
905
906status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
907 ATRACE_CALL();
908 (void)outputId; (void)id;
909
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700910 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800911 return INVALID_OPERATION;
912}
913
914
915status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700916 uint32_t *width, uint32_t *height,
917 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800918 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700919 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800920 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800921
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800922 switch (mStatus) {
923 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700924 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800925 return INVALID_OPERATION;
926 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700927 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800928 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700929 case STATUS_UNCONFIGURED:
930 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800931 case STATUS_ACTIVE:
932 // OK
933 break;
934 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700935 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800936 return INVALID_OPERATION;
937 }
938
939 ssize_t idx = mOutputStreams.indexOfKey(id);
940 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700941 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800942 return idx;
943 }
944
945 if (width) *width = mOutputStreams[idx]->getWidth();
946 if (height) *height = mOutputStreams[idx]->getHeight();
947 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700948 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800949 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800950}
951
952status_t Camera3Device::setStreamTransform(int id,
953 int transform) {
954 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700955 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800956 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800957
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800958 switch (mStatus) {
959 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700960 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800961 return INVALID_OPERATION;
962 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700963 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800964 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700965 case STATUS_UNCONFIGURED:
966 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800967 case STATUS_ACTIVE:
968 // OK
969 break;
970 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700971 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800972 return INVALID_OPERATION;
973 }
974
975 ssize_t idx = mOutputStreams.indexOfKey(id);
976 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700977 CLOGE("Stream %d does not exist",
978 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800979 return BAD_VALUE;
980 }
981
982 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800983}
984
985status_t Camera3Device::deleteStream(int id) {
986 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700987 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800988 Mutex::Autolock l(mLock);
989 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800990
Igor Murashkine2172be2013-05-28 15:31:39 -0700991 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
992
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800993 // CameraDevice semantics require device to already be idle before
994 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700995 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700996 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
997 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800998 }
999
Igor Murashkin2fba5842013-04-22 14:03:54 -07001000 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001001 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001002 if (mInputStream != NULL && id == mInputStream->getId()) {
1003 deletedStream = mInputStream;
1004 mInputStream.clear();
1005 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001006 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001007 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001008 return BAD_VALUE;
1009 }
Zhijun He5f446352014-01-22 09:49:33 -08001010 }
1011
1012 // Delete output stream or the output part of a bi-directional stream.
1013 if (outputStreamIdx != NAME_NOT_FOUND) {
1014 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001015 mOutputStreams.removeItem(id);
1016 }
1017
1018 // Free up the stream endpoint so that it can be used by some other stream
1019 res = deletedStream->disconnect();
1020 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001021 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001022 // fall through since we want to still list the stream as deleted.
1023 }
1024 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001025 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001026
1027 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001028}
1029
1030status_t Camera3Device::deleteReprocessStream(int id) {
1031 ATRACE_CALL();
1032 (void)id;
1033
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001034 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001035 return INVALID_OPERATION;
1036}
1037
Zhijun He1fa89992015-06-01 15:44:31 -07001038status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001039 ATRACE_CALL();
1040 ALOGV("%s: E", __FUNCTION__);
1041
1042 Mutex::Autolock il(mInterfaceLock);
1043 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001044
1045 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1046 mNeedConfig = true;
1047 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1048 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001049
1050 return configureStreamsLocked();
1051}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001052
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001053status_t Camera3Device::getInputBufferProducer(
1054 sp<IGraphicBufferProducer> *producer) {
1055 Mutex::Autolock il(mInterfaceLock);
1056 Mutex::Autolock l(mLock);
1057
1058 if (producer == NULL) {
1059 return BAD_VALUE;
1060 } else if (mInputStream == NULL) {
1061 return INVALID_OPERATION;
1062 }
1063
1064 return mInputStream->getInputBufferProducer(producer);
1065}
1066
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001067status_t Camera3Device::createDefaultRequest(int templateId,
1068 CameraMetadata *request) {
1069 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001070 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001071 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001072 Mutex::Autolock l(mLock);
1073
1074 switch (mStatus) {
1075 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001076 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001077 return INVALID_OPERATION;
1078 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001079 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001080 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001081 case STATUS_UNCONFIGURED:
1082 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001083 case STATUS_ACTIVE:
1084 // OK
1085 break;
1086 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001087 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001088 return INVALID_OPERATION;
1089 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001090
Zhijun Hea1530f12014-09-14 12:44:20 -07001091 if (!mRequestTemplateCache[templateId].isEmpty()) {
1092 *request = mRequestTemplateCache[templateId];
1093 return OK;
1094 }
1095
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001096 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001097 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001098 rawRequest = mHal3Device->ops->construct_default_request_settings(
1099 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001100 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001101 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001102 ALOGI("%s: template %d is not supported on this camera device",
1103 __FUNCTION__, templateId);
1104 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001105 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001106 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001107 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001108
1109 return OK;
1110}
1111
1112status_t Camera3Device::waitUntilDrained() {
1113 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001114 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001115 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001116
Zhijun He69a37482014-03-23 18:44:49 -07001117 return waitUntilDrainedLocked();
1118}
1119
1120status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001121 switch (mStatus) {
1122 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001123 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001124 ALOGV("%s: Already idle", __FUNCTION__);
1125 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001126 case STATUS_CONFIGURED:
1127 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001128 case STATUS_ERROR:
1129 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001130 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001131 break;
1132 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001133 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001134 return INVALID_OPERATION;
1135 }
1136
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001137 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1138 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001139 if (res != OK) {
1140 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1141 res);
1142 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001143 return res;
1144}
1145
1146// Pause to reconfigure
1147status_t Camera3Device::internalPauseAndWaitLocked() {
1148 mRequestThread->setPaused(true);
1149 mPauseStateNotify = true;
1150
1151 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1152 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1153 if (res != OK) {
1154 SET_ERR_L("Can't idle device in %f seconds!",
1155 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001156 }
1157
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001158 return res;
1159}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001160
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001161// Resume after internalPauseAndWaitLocked
1162status_t Camera3Device::internalResumeLocked() {
1163 status_t res;
1164
1165 mRequestThread->setPaused(false);
1166
1167 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1168 if (res != OK) {
1169 SET_ERR_L("Can't transition to active in %f seconds!",
1170 kActiveTimeout/1e9);
1171 }
1172 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001173 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001174}
1175
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001176status_t Camera3Device::waitUntilStateThenRelock(bool active,
1177 nsecs_t timeout) {
1178 status_t res = OK;
1179 if (active == (mStatus == STATUS_ACTIVE)) {
1180 // Desired state already reached
1181 return res;
1182 }
1183
1184 bool stateSeen = false;
1185 do {
1186 mRecentStatusUpdates.clear();
1187
1188 res = mStatusChanged.waitRelative(mLock, timeout);
1189 if (res != OK) break;
1190
1191 // Check state change history during wait
1192 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1193 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1194 stateSeen = true;
1195 break;
1196 }
1197 }
1198 } while (!stateSeen);
1199
1200 return res;
1201}
1202
1203
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001204status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1205 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001206 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001207
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001208 if (listener != NULL && mListener != NULL) {
1209 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1210 }
1211 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001212 mRequestThread->setNotificationListener(listener);
1213 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001214
1215 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001216}
1217
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001218bool Camera3Device::willNotify3A() {
1219 return false;
1220}
1221
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001222status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001223 status_t res;
1224 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001225
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001226 while (mResultQueue.empty()) {
1227 res = mResultSignal.waitRelative(mOutputLock, timeout);
1228 if (res == TIMED_OUT) {
1229 return res;
1230 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001231 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001232 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001233 return res;
1234 }
1235 }
1236 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001237}
1238
Jianing Weicb0652e2014-03-12 18:29:36 -07001239status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001240 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001241 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001242
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001243 if (mResultQueue.empty()) {
1244 return NOT_ENOUGH_DATA;
1245 }
1246
Jianing Weicb0652e2014-03-12 18:29:36 -07001247 if (frame == NULL) {
1248 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1249 return BAD_VALUE;
1250 }
1251
1252 CaptureResult &result = *(mResultQueue.begin());
1253 frame->mResultExtras = result.mResultExtras;
1254 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001255 mResultQueue.erase(mResultQueue.begin());
1256
1257 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001258}
1259
1260status_t Camera3Device::triggerAutofocus(uint32_t id) {
1261 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001262 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001263
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001264 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1265 // Mix-in this trigger into the next request and only the next request.
1266 RequestTrigger trigger[] = {
1267 {
1268 ANDROID_CONTROL_AF_TRIGGER,
1269 ANDROID_CONTROL_AF_TRIGGER_START
1270 },
1271 {
1272 ANDROID_CONTROL_AF_TRIGGER_ID,
1273 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001274 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001275 };
1276
1277 return mRequestThread->queueTrigger(trigger,
1278 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001279}
1280
1281status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1282 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001283 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001284
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001285 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1286 // Mix-in this trigger into the next request and only the next request.
1287 RequestTrigger trigger[] = {
1288 {
1289 ANDROID_CONTROL_AF_TRIGGER,
1290 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1291 },
1292 {
1293 ANDROID_CONTROL_AF_TRIGGER_ID,
1294 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001295 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001296 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001297
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001298 return mRequestThread->queueTrigger(trigger,
1299 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001300}
1301
1302status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1303 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001304 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001305
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001306 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1307 // Mix-in this trigger into the next request and only the next request.
1308 RequestTrigger trigger[] = {
1309 {
1310 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1311 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1312 },
1313 {
1314 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1315 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001316 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001317 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001318
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001319 return mRequestThread->queueTrigger(trigger,
1320 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001321}
1322
1323status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1324 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1325 ATRACE_CALL();
1326 (void)reprocessStreamId; (void)buffer; (void)listener;
1327
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001328 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001329 return INVALID_OPERATION;
1330}
1331
Jianing Weicb0652e2014-03-12 18:29:36 -07001332status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001333 ATRACE_CALL();
1334 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001335 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001336
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001337 NotificationListener* listener;
1338 {
1339 Mutex::Autolock l(mOutputLock);
1340 listener = mListener;
1341 }
1342
Zhijun He7ef20392014-04-21 16:04:17 -07001343 {
1344 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001345 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001346 }
1347
Zhijun He491e3412013-12-27 10:57:44 -08001348 status_t res;
1349 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1350 res = mHal3Device->ops->flush(mHal3Device);
1351 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001352 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001353 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001354 }
1355
1356 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001357}
1358
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001359status_t Camera3Device::prepare(int streamId) {
1360 ATRACE_CALL();
1361 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001362 Mutex::Autolock il(mInterfaceLock);
1363 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001364
1365 sp<Camera3StreamInterface> stream;
1366 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1367 if (outputStreamIdx == NAME_NOT_FOUND) {
1368 CLOGE("Stream %d does not exist", streamId);
1369 return BAD_VALUE;
1370 }
1371
1372 stream = mOutputStreams.editValueAt(outputStreamIdx);
1373
1374 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001375 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001376 return BAD_VALUE;
1377 }
1378
1379 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001380 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001381 return BAD_VALUE;
1382 }
1383
1384 return mPreparerThread->prepare(stream);
1385}
1386
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001387status_t Camera3Device::tearDown(int streamId) {
1388 ATRACE_CALL();
1389 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1390 Mutex::Autolock il(mInterfaceLock);
1391 Mutex::Autolock l(mLock);
1392
1393 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1394 // since we cannot call register_stream_buffers except right after configure_streams.
1395 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1396 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1397 __FUNCTION__, mHal3Device->common.version);
1398 return NO_INIT;
1399 }
1400
1401 sp<Camera3StreamInterface> stream;
1402 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1403 if (outputStreamIdx == NAME_NOT_FOUND) {
1404 CLOGE("Stream %d does not exist", streamId);
1405 return BAD_VALUE;
1406 }
1407
1408 stream = mOutputStreams.editValueAt(outputStreamIdx);
1409
1410 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1411 CLOGE("Stream %d is a target of a in-progress request", streamId);
1412 return BAD_VALUE;
1413 }
1414
1415 return stream->tearDown();
1416}
1417
Zhijun He204e3292014-07-14 17:09:23 -07001418uint32_t Camera3Device::getDeviceVersion() {
1419 ATRACE_CALL();
1420 Mutex::Autolock il(mInterfaceLock);
1421 return mDeviceVersion;
1422}
1423
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001424/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001425 * Methods called by subclasses
1426 */
1427
1428void Camera3Device::notifyStatus(bool idle) {
1429 {
1430 // Need mLock to safely update state and synchronize to current
1431 // state of methods in flight.
1432 Mutex::Autolock l(mLock);
1433 // We can get various system-idle notices from the status tracker
1434 // while starting up. Only care about them if we've actually sent
1435 // in some requests recently.
1436 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1437 return;
1438 }
1439 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1440 idle ? "idle" : "active");
1441 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1442 mRecentStatusUpdates.add(mStatus);
1443 mStatusChanged.signal();
1444
1445 // Skip notifying listener if we're doing some user-transparent
1446 // state changes
1447 if (mPauseStateNotify) return;
1448 }
1449 NotificationListener *listener;
1450 {
1451 Mutex::Autolock l(mOutputLock);
1452 listener = mListener;
1453 }
1454 if (idle && listener != NULL) {
1455 listener->notifyIdle();
1456 }
1457}
1458
1459/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001460 * Camera3Device private methods
1461 */
1462
1463sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1464 const CameraMetadata &request) {
1465 ATRACE_CALL();
1466 status_t res;
1467
1468 sp<CaptureRequest> newRequest = new CaptureRequest;
1469 newRequest->mSettings = request;
1470
1471 camera_metadata_entry_t inputStreams =
1472 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1473 if (inputStreams.count > 0) {
1474 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001475 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001476 CLOGE("Request references unknown input stream %d",
1477 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001478 return NULL;
1479 }
1480 // Lazy completion of stream configuration (allocation/registration)
1481 // on first use
1482 if (mInputStream->isConfiguring()) {
1483 res = mInputStream->finishConfiguration(mHal3Device);
1484 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001485 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001486 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001487 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001488 return NULL;
1489 }
1490 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001491 // Check if stream is being prepared
1492 if (mInputStream->isPreparing()) {
1493 CLOGE("Request references an input stream that's being prepared!");
1494 return NULL;
1495 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001496
1497 newRequest->mInputStream = mInputStream;
1498 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1499 }
1500
1501 camera_metadata_entry_t streams =
1502 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1503 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001504 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001505 return NULL;
1506 }
1507
1508 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001509 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001510 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001511 CLOGE("Request references unknown stream %d",
1512 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001513 return NULL;
1514 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001515 sp<Camera3OutputStreamInterface> stream =
1516 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001517
1518 // Lazy completion of stream configuration (allocation/registration)
1519 // on first use
1520 if (stream->isConfiguring()) {
1521 res = stream->finishConfiguration(mHal3Device);
1522 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001523 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1524 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001525 return NULL;
1526 }
1527 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001528 // Check if stream is being prepared
1529 if (stream->isPreparing()) {
1530 CLOGE("Request references an output stream that's being prepared!");
1531 return NULL;
1532 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001533
1534 newRequest->mOutputStreams.push(stream);
1535 }
1536 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1537
1538 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001539}
1540
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001541bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1542 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1543 Size size = mSupportedOpaqueInputSizes[i];
1544 if (size.width == width && size.height == height) {
1545 return true;
1546 }
1547 }
1548
1549 return false;
1550}
1551
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001552status_t Camera3Device::configureStreamsLocked() {
1553 ATRACE_CALL();
1554 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001555
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001556 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001557 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001558 return INVALID_OPERATION;
1559 }
1560
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001561 if (!mNeedConfig) {
1562 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1563 return OK;
1564 }
1565
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001566 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1567 // adding a dummy stream instead.
1568 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1569 if (mOutputStreams.size() == 0) {
1570 addDummyStreamLocked();
1571 } else {
1572 tryRemoveDummyStreamLocked();
1573 }
1574
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001575 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001576 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001577
1578 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001579 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1580 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1581 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001582 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1583
1584 Vector<camera3_stream_t*> streams;
1585 streams.setCapacity(config.num_streams);
1586
1587 if (mInputStream != NULL) {
1588 camera3_stream_t *inputStream;
1589 inputStream = mInputStream->startConfiguration();
1590 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001591 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001592 return INVALID_OPERATION;
1593 }
1594 streams.add(inputStream);
1595 }
1596
1597 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001598
1599 // Don't configure bidi streams twice, nor add them twice to the list
1600 if (mOutputStreams[i].get() ==
1601 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1602
1603 config.num_streams--;
1604 continue;
1605 }
1606
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001607 camera3_stream_t *outputStream;
1608 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1609 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001610 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001611 return INVALID_OPERATION;
1612 }
1613 streams.add(outputStream);
1614 }
1615
1616 config.streams = streams.editArray();
1617
1618 // Do the HAL configuration; will potentially touch stream
1619 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001620 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001621 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001622 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001623
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001624 if (res == BAD_VALUE) {
1625 // HAL rejected this set of streams as unsupported, clean up config
1626 // attempt and return to unconfigured state
1627 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1628 res = mInputStream->cancelConfiguration();
1629 if (res != OK) {
1630 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1631 mInputStream->getId(), strerror(-res), res);
1632 return res;
1633 }
1634 }
1635
1636 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1637 sp<Camera3OutputStreamInterface> outputStream =
1638 mOutputStreams.editValueAt(i);
1639 if (outputStream->isConfiguring()) {
1640 res = outputStream->cancelConfiguration();
1641 if (res != OK) {
1642 SET_ERR_L(
1643 "Can't cancel configuring output stream %d: %s (%d)",
1644 outputStream->getId(), strerror(-res), res);
1645 return res;
1646 }
1647 }
1648 }
1649
1650 // Return state to that at start of call, so that future configures
1651 // properly clean things up
1652 mStatus = STATUS_UNCONFIGURED;
1653 mNeedConfig = true;
1654
1655 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1656 return BAD_VALUE;
1657 } else if (res != OK) {
1658 // Some other kind of error from configure_streams - this is not
1659 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001660 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1661 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001662 return res;
1663 }
1664
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001665 // Finish all stream configuration immediately.
1666 // TODO: Try to relax this later back to lazy completion, which should be
1667 // faster
1668
Igor Murashkin073f8572013-05-02 14:59:28 -07001669 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001670 res = mInputStream->finishConfiguration(mHal3Device);
1671 if (res != OK) {
1672 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1673 mInputStream->getId(), strerror(-res), res);
1674 return res;
1675 }
1676 }
1677
1678 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001679 sp<Camera3OutputStreamInterface> outputStream =
1680 mOutputStreams.editValueAt(i);
1681 if (outputStream->isConfiguring()) {
1682 res = outputStream->finishConfiguration(mHal3Device);
1683 if (res != OK) {
1684 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1685 outputStream->getId(), strerror(-res), res);
1686 return res;
1687 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001688 }
1689 }
1690
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001691 // Request thread needs to know to avoid using repeat-last-settings protocol
1692 // across configure_streams() calls
1693 mRequestThread->configurationComplete();
1694
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001695 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001696
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001697 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001698
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001699 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001700 mStatus = STATUS_CONFIGURED;
1701 } else {
1702 mStatus = STATUS_UNCONFIGURED;
1703 }
1704
1705 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1706
Zhijun He0a210512014-07-24 13:45:15 -07001707 // tear down the deleted streams after configure streams.
1708 mDeletedStreams.clear();
1709
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001710 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001711}
1712
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001713status_t Camera3Device::addDummyStreamLocked() {
1714 ATRACE_CALL();
1715 status_t res;
1716
1717 if (mDummyStreamId != NO_STREAM) {
1718 // Should never be adding a second dummy stream when one is already
1719 // active
1720 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1721 __FUNCTION__, mId);
1722 return INVALID_OPERATION;
1723 }
1724
1725 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1726
1727 sp<Camera3OutputStreamInterface> dummyStream =
1728 new Camera3DummyStream(mNextStreamId);
1729
1730 res = mOutputStreams.add(mNextStreamId, dummyStream);
1731 if (res < 0) {
1732 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1733 return res;
1734 }
1735
1736 mDummyStreamId = mNextStreamId;
1737 mNextStreamId++;
1738
1739 return OK;
1740}
1741
1742status_t Camera3Device::tryRemoveDummyStreamLocked() {
1743 ATRACE_CALL();
1744 status_t res;
1745
1746 if (mDummyStreamId == NO_STREAM) return OK;
1747 if (mOutputStreams.size() == 1) return OK;
1748
1749 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1750
1751 // Ok, have a dummy stream and there's at least one other output stream,
1752 // so remove the dummy
1753
1754 sp<Camera3StreamInterface> deletedStream;
1755 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1756 if (outputStreamIdx == NAME_NOT_FOUND) {
1757 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1758 return INVALID_OPERATION;
1759 }
1760
1761 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1762 mOutputStreams.removeItemsAt(outputStreamIdx);
1763
1764 // Free up the stream endpoint so that it can be used by some other stream
1765 res = deletedStream->disconnect();
1766 if (res != OK) {
1767 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1768 // fall through since we want to still list the stream as deleted.
1769 }
1770 mDeletedStreams.add(deletedStream);
1771 mDummyStreamId = NO_STREAM;
1772
1773 return res;
1774}
1775
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001776void Camera3Device::setErrorState(const char *fmt, ...) {
1777 Mutex::Autolock l(mLock);
1778 va_list args;
1779 va_start(args, fmt);
1780
1781 setErrorStateLockedV(fmt, args);
1782
1783 va_end(args);
1784}
1785
1786void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1787 Mutex::Autolock l(mLock);
1788 setErrorStateLockedV(fmt, args);
1789}
1790
1791void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1792 va_list args;
1793 va_start(args, fmt);
1794
1795 setErrorStateLockedV(fmt, args);
1796
1797 va_end(args);
1798}
1799
1800void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001801 // Print out all error messages to log
1802 String8 errorCause = String8::formatV(fmt, args);
1803 ALOGE("Camera %d: %s", mId, errorCause.string());
1804
1805 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001806 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001807
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001808 mErrorCause = errorCause;
1809
1810 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001811 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001812
1813 // Notify upstream about a device error
1814 if (mListener != NULL) {
1815 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1816 CaptureResultExtras());
1817 }
1818
1819 // Save stack trace. View by dumping it later.
1820 CameraTraces::saveTrace();
1821 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001822}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001823
1824/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001825 * In-flight request management
1826 */
1827
Jianing Weicb0652e2014-03-12 18:29:36 -07001828status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07001829 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1830 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001831 ATRACE_CALL();
1832 Mutex::Autolock l(mInFlightLock);
1833
1834 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07001835 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
1836 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001837 if (res < 0) return res;
1838
1839 return OK;
1840}
1841
1842/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001843 * Check if all 3A fields are ready, and send off a partial 3A-only result
1844 * to the output frame queue
1845 */
Zhijun He204e3292014-07-14 17:09:23 -07001846bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001847 uint32_t frameNumber,
1848 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001849
1850 // Check if all 3A states are present
1851 // The full list of fields is
1852 // android.control.afMode
1853 // android.control.awbMode
1854 // android.control.aeState
1855 // android.control.awbState
1856 // android.control.afState
1857 // android.control.afTriggerID
1858 // android.control.aePrecaptureID
1859 // TODO: Add android.control.aeMode
1860
1861 bool gotAllStates = true;
1862
1863 uint8_t afMode;
1864 uint8_t awbMode;
1865 uint8_t aeState;
1866 uint8_t afState;
1867 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001868
1869 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1870 &afMode, frameNumber);
1871
1872 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1873 &awbMode, frameNumber);
1874
1875 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1876 &aeState, frameNumber);
1877
1878 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1879 &afState, frameNumber);
1880
1881 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1882 &awbState, frameNumber);
1883
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001884 if (!gotAllStates) return false;
1885
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001886 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001887 "AF state %d, AE state %d, AWB state %d, "
1888 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001889 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001890 afMode, awbMode,
1891 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001892 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001893
1894 // Got all states, so construct a minimal result to send
1895 // In addition to the above fields, this means adding in
1896 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001897 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001898 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001899
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001900 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001901
1902 Mutex::Autolock l(mOutputLock);
1903
Jianing Weicb0652e2014-03-12 18:29:36 -07001904 CaptureResult captureResult;
1905 captureResult.mResultExtras = resultExtras;
1906 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1907 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1908 // but not limited to CameraDeviceBase::getNextResult
1909 CaptureResult& min3AResult =
1910 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001911
Jianing Weicb0652e2014-03-12 18:29:36 -07001912 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1913 // TODO: This is problematic casting. Need to fix CameraMetadata.
1914 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001915 return false;
1916 }
1917
Jianing Weicb0652e2014-03-12 18:29:36 -07001918 int32_t requestId = resultExtras.requestId;
1919 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001920 &requestId, frameNumber)) {
1921 return false;
1922 }
1923
Zhijun He204e3292014-07-14 17:09:23 -07001924 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1925 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1926 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1927 &partialResult, frameNumber)) {
1928 return false;
1929 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001930 }
1931
Jianing Weicb0652e2014-03-12 18:29:36 -07001932 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001933 &afMode, frameNumber)) {
1934 return false;
1935 }
1936
Jianing Weicb0652e2014-03-12 18:29:36 -07001937 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001938 &awbMode, frameNumber)) {
1939 return false;
1940 }
1941
Jianing Weicb0652e2014-03-12 18:29:36 -07001942 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001943 &aeState, frameNumber)) {
1944 return false;
1945 }
1946
Jianing Weicb0652e2014-03-12 18:29:36 -07001947 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001948 &afState, frameNumber)) {
1949 return false;
1950 }
1951
Jianing Weicb0652e2014-03-12 18:29:36 -07001952 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001953 &awbState, frameNumber)) {
1954 return false;
1955 }
1956
Jianing Weicb0652e2014-03-12 18:29:36 -07001957 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001958 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001959 return false;
1960 }
1961
Jianing Weicb0652e2014-03-12 18:29:36 -07001962 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001963 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001964 return false;
1965 }
1966
Zhijun He204e3292014-07-14 17:09:23 -07001967 // We only send the aggregated partial when all 3A related metadata are available
1968 // For both API1 and API2.
1969 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001970 mResultSignal.signal();
1971
1972 return true;
1973}
1974
1975template<typename T>
1976bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001977 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001978 (void) frameNumber;
1979
1980 camera_metadata_ro_entry_t entry;
1981
1982 entry = result.find(tag);
1983 if (entry.count == 0) {
1984 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1985 mId, frameNumber, get_camera_metadata_tag_name(tag));
1986 return false;
1987 }
1988
1989 if (sizeof(T) == sizeof(uint8_t)) {
1990 *value = entry.data.u8[0];
1991 } else if (sizeof(T) == sizeof(int32_t)) {
1992 *value = entry.data.i32[0];
1993 } else {
1994 ALOGE("%s: Unexpected type", __FUNCTION__);
1995 return false;
1996 }
1997 return true;
1998}
1999
2000template<typename T>
2001bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002002 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002003 if (result.update(tag, value, 1) != NO_ERROR) {
2004 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2005 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2006 frameNumber, get_camera_metadata_tag_name(tag));
2007 return false;
2008 }
2009 return true;
2010}
2011
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002012void Camera3Device::returnOutputBuffers(
2013 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2014 nsecs_t timestamp) {
2015 for (size_t i = 0; i < numBuffers; i++)
2016 {
2017 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2018 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2019 // Note: stream may be deallocated at this point, if this buffer was
2020 // the last reference to it.
2021 if (res != OK) {
2022 ALOGE("Can't return buffer to its stream: %s (%d)",
2023 strerror(-res), res);
2024 }
2025 }
2026}
2027
2028
2029void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2030
2031 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2032 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2033
2034 nsecs_t sensorTimestamp = request.sensorTimestamp;
2035 nsecs_t shutterTimestamp = request.shutterTimestamp;
2036
2037 // Check if it's okay to remove the request from InFlightMap:
2038 // In the case of a successful request:
2039 // all input and output buffers, all result metadata, shutter callback
2040 // arrived.
2041 // In the case of a unsuccessful request:
2042 // all input and output buffers arrived.
2043 if (request.numBuffersLeft == 0 &&
2044 (request.requestStatus != OK ||
2045 (request.haveResultMetadata && shutterTimestamp != 0))) {
2046 ATRACE_ASYNC_END("frame capture", frameNumber);
2047
2048 // Sanity check - if sensor timestamp matches shutter timestamp
2049 if (request.requestStatus == OK &&
2050 sensorTimestamp != shutterTimestamp) {
2051 SET_ERR("sensor timestamp (%" PRId64
2052 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2053 sensorTimestamp, frameNumber, shutterTimestamp);
2054 }
2055
2056 // for an unsuccessful request, it may have pending output buffers to
2057 // return.
2058 assert(request.requestStatus != OK ||
2059 request.pendingOutputBuffers.size() == 0);
2060 returnOutputBuffers(request.pendingOutputBuffers.array(),
2061 request.pendingOutputBuffers.size(), 0);
2062
2063 mInFlightMap.removeItemsAt(idx, 1);
2064
2065 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2066 }
2067
2068 // Sanity check - if we have too many in-flight frames, something has
2069 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002070 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002071 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002072 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2073 kInFlightWarnLimitHighSpeed) {
2074 CLOGE("In-flight list too large for high speed configuration: %zu",
2075 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002076 }
2077}
2078
2079
2080void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2081 CaptureResultExtras &resultExtras,
2082 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002083 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002084 bool reprocess,
2085 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002086 if (pendingMetadata.isEmpty())
2087 return;
2088
2089 Mutex::Autolock l(mOutputLock);
2090
2091 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002092 if (reprocess) {
2093 if (frameNumber < mNextReprocessResultFrameNumber) {
2094 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002095 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002096 frameNumber, mNextReprocessResultFrameNumber);
2097 return;
2098 }
2099 mNextReprocessResultFrameNumber = frameNumber + 1;
2100 } else {
2101 if (frameNumber < mNextResultFrameNumber) {
2102 SET_ERR("Out-of-order capture result metadata submitted! "
2103 "(got frame number %d, expecting %d)",
2104 frameNumber, mNextResultFrameNumber);
2105 return;
2106 }
2107 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002108 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002109
2110 CaptureResult captureResult;
2111 captureResult.mResultExtras = resultExtras;
2112 captureResult.mMetadata = pendingMetadata;
2113
2114 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2115 (int32_t*)&frameNumber, 1) != OK) {
2116 SET_ERR("Failed to set frame# in metadata (%d)",
2117 frameNumber);
2118 return;
2119 } else {
2120 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2121 __FUNCTION__, mId, frameNumber);
2122 }
2123
2124 // Append any previous partials to form a complete result
2125 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2126 captureResult.mMetadata.append(collectedPartialResult);
2127 }
2128
2129 captureResult.mMetadata.sort();
2130
2131 // Check that there's a timestamp in the result metadata
2132 camera_metadata_entry entry =
2133 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2134 if (entry.count == 0) {
2135 SET_ERR("No timestamp provided by HAL for frame %d!",
2136 frameNumber);
2137 return;
2138 }
2139
Chien-Yu Chend196d612015-06-22 19:49:01 -07002140 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2141
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002142 // Valid result, insert into queue
2143 List<CaptureResult>::iterator queuedResult =
2144 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2145 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2146 ", burstId = %" PRId32, __FUNCTION__,
2147 queuedResult->mResultExtras.requestId,
2148 queuedResult->mResultExtras.frameNumber,
2149 queuedResult->mResultExtras.burstId);
2150
2151 mResultSignal.signal();
2152}
2153
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002154/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002155 * Camera HAL device callback methods
2156 */
2157
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002158void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002159 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002160
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002161 status_t res;
2162
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002163 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002164 if (result->result == NULL && result->num_output_buffers == 0 &&
2165 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002166 SET_ERR("No result data provided by HAL for frame %d",
2167 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002168 return;
2169 }
Zhijun He204e3292014-07-14 17:09:23 -07002170
2171 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2172 // partial_result to 1 when metadata is included in this result.
2173 if (!mUsePartialResult &&
2174 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2175 result->result != NULL &&
2176 result->partial_result != 1) {
2177 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2178 " if partial result is not supported",
2179 frameNumber, result->partial_result);
2180 return;
2181 }
2182
2183 bool isPartialResult = false;
2184 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002185 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002186 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002187
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002188 // Get shutter timestamp and resultExtras from list of in-flight requests,
2189 // where it was added by the shutter notification for this frame. If the
2190 // shutter timestamp isn't received yet, append the output buffers to the
2191 // in-flight request and they will be returned when the shutter timestamp
2192 // arrives. Update the in-flight status and remove the in-flight entry if
2193 // all result data and shutter timestamp have been received.
2194 nsecs_t shutterTimestamp = 0;
2195
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002196 {
2197 Mutex::Autolock l(mInFlightLock);
2198 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2199 if (idx == NAME_NOT_FOUND) {
2200 SET_ERR("Unknown frame number for capture result: %d",
2201 frameNumber);
2202 return;
2203 }
2204 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002205 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2206 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2207 ", partialResultCount = %d",
2208 __FUNCTION__, request.resultExtras.requestId,
2209 request.resultExtras.frameNumber, request.resultExtras.burstId,
2210 result->partial_result);
2211 // Always update the partial count to the latest one if it's not 0
2212 // (buffers only). When framework aggregates adjacent partial results
2213 // into one, the latest partial count will be used.
2214 if (result->partial_result != 0)
2215 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002216
2217 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002218 if (mUsePartialResult && result->result != NULL) {
2219 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2220 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2221 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2222 " the range of [1, %d] when metadata is included in the result",
2223 frameNumber, result->partial_result, mNumPartialResults);
2224 return;
2225 }
2226 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002227 if (isPartialResult) {
2228 request.partialResult.collectedResult.append(result->result);
2229 }
Zhijun He204e3292014-07-14 17:09:23 -07002230 } else {
2231 camera_metadata_ro_entry_t partialResultEntry;
2232 res = find_camera_metadata_ro_entry(result->result,
2233 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2234 if (res != NAME_NOT_FOUND &&
2235 partialResultEntry.count > 0 &&
2236 partialResultEntry.data.u8[0] ==
2237 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2238 // A partial result. Flag this as such, and collect this
2239 // set of metadata into the in-flight entry.
2240 isPartialResult = true;
2241 request.partialResult.collectedResult.append(
2242 result->result);
2243 request.partialResult.collectedResult.erase(
2244 ANDROID_QUIRKS_PARTIAL_RESULT);
2245 }
2246 }
2247
2248 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002249 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002250 if (!request.partialResult.haveSent3A) {
2251 request.partialResult.haveSent3A =
2252 processPartial3AResult(frameNumber,
2253 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002254 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002255 }
2256 }
2257 }
2258
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002259 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002260 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002261
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002262 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002263 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002264 if (request.haveResultMetadata) {
2265 SET_ERR("Called multiple times with metadata for frame %d",
2266 frameNumber);
2267 return;
2268 }
Zhijun He204e3292014-07-14 17:09:23 -07002269 if (mUsePartialResult &&
2270 !request.partialResult.collectedResult.isEmpty()) {
2271 collectedPartialResult.acquire(
2272 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002273 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002274 request.haveResultMetadata = true;
2275 }
2276
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002277 uint32_t numBuffersReturned = result->num_output_buffers;
2278 if (result->input_buffer != NULL) {
2279 if (hasInputBufferInRequest) {
2280 numBuffersReturned += 1;
2281 } else {
2282 ALOGW("%s: Input buffer should be NULL if there is no input"
2283 " buffer sent in the request",
2284 __FUNCTION__);
2285 }
2286 }
2287 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002288 if (request.numBuffersLeft < 0) {
2289 SET_ERR("Too many buffers returned for frame %d",
2290 frameNumber);
2291 return;
2292 }
2293
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002294 camera_metadata_ro_entry_t entry;
2295 res = find_camera_metadata_ro_entry(result->result,
2296 ANDROID_SENSOR_TIMESTAMP, &entry);
2297 if (res == OK && entry.count == 1) {
2298 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002299 }
2300
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002301 // If shutter event isn't received yet, append the output buffers to
2302 // the in-flight request. Otherwise, return the output buffers to
2303 // streams.
2304 if (shutterTimestamp == 0) {
2305 request.pendingOutputBuffers.appendArray(result->output_buffers,
2306 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002307 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002308 returnOutputBuffers(result->output_buffers,
2309 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002310 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002311
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002312 if (result->result != NULL && !isPartialResult) {
2313 if (shutterTimestamp == 0) {
2314 request.pendingMetadata = result->result;
2315 request.partialResult.collectedResult = collectedPartialResult;
2316 } else {
2317 CameraMetadata metadata;
2318 metadata = result->result;
2319 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002320 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2321 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002322 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002323 }
2324
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002325 removeInFlightRequestIfReadyLocked(idx);
2326 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002327
Zhijun Hef0d962a2014-06-30 10:24:11 -07002328 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002329 if (hasInputBufferInRequest) {
2330 Camera3Stream *stream =
2331 Camera3Stream::cast(result->input_buffer->stream);
2332 res = stream->returnInputBuffer(*(result->input_buffer));
2333 // Note: stream may be deallocated at this point, if this buffer was the
2334 // last reference to it.
2335 if (res != OK) {
2336 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2337 " its stream:%s (%d)", __FUNCTION__,
2338 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002339 }
2340 } else {
2341 ALOGW("%s: Input buffer should be NULL if there is no input"
2342 " buffer sent in the request, skipping input buffer return.",
2343 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002344 }
2345 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002346}
2347
2348void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002349 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002350 NotificationListener *listener;
2351 {
2352 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002353 listener = mListener;
2354 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002355
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002356 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002357 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002358 return;
2359 }
2360
2361 switch (msg->type) {
2362 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002363 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002364 break;
2365 }
2366 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002367 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002368 break;
2369 }
2370 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002371 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002372 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002373 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002374}
2375
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002376void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2377 NotificationListener *listener) {
2378
2379 // Map camera HAL error codes to ICameraDeviceCallback error codes
2380 // Index into this with the HAL error code
2381 static const ICameraDeviceCallbacks::CameraErrorCode
2382 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2383 // 0 = Unused error code
2384 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2385 // 1 = CAMERA3_MSG_ERROR_DEVICE
2386 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2387 // 2 = CAMERA3_MSG_ERROR_REQUEST
2388 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2389 // 3 = CAMERA3_MSG_ERROR_RESULT
2390 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2391 // 4 = CAMERA3_MSG_ERROR_BUFFER
2392 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2393 };
2394
2395 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2396 ((msg.error_code >= 0) &&
2397 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2398 halErrorMap[msg.error_code] :
2399 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2400
2401 int streamId = 0;
2402 if (msg.error_stream != NULL) {
2403 Camera3Stream *stream =
2404 Camera3Stream::cast(msg.error_stream);
2405 streamId = stream->getId();
2406 }
2407 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2408 mId, __FUNCTION__, msg.frame_number,
2409 streamId, msg.error_code);
2410
2411 CaptureResultExtras resultExtras;
2412 switch (errorCode) {
2413 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2414 // SET_ERR calls notifyError
2415 SET_ERR("Camera HAL reported serious device error");
2416 break;
2417 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2418 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2419 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2420 {
2421 Mutex::Autolock l(mInFlightLock);
2422 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2423 if (idx >= 0) {
2424 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2425 r.requestStatus = msg.error_code;
2426 resultExtras = r.resultExtras;
2427 } else {
2428 resultExtras.frameNumber = msg.frame_number;
2429 ALOGE("Camera %d: %s: cannot find in-flight request on "
2430 "frame %" PRId64 " error", mId, __FUNCTION__,
2431 resultExtras.frameNumber);
2432 }
2433 }
2434 if (listener != NULL) {
2435 listener->notifyError(errorCode, resultExtras);
2436 } else {
2437 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2438 }
2439 break;
2440 default:
2441 // SET_ERR calls notifyError
2442 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2443 break;
2444 }
2445}
2446
2447void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2448 NotificationListener *listener) {
2449 ssize_t idx;
2450 // Verify ordering of shutter notifications
2451 {
2452 Mutex::Autolock l(mOutputLock);
2453 // TODO: need to track errors for tighter bounds on expected frame number.
2454 if (msg.frame_number < mNextShutterFrameNumber) {
2455 SET_ERR("Shutter notification out-of-order. Expected "
2456 "notification for frame %d, got frame %d",
2457 mNextShutterFrameNumber, msg.frame_number);
2458 return;
2459 }
2460 mNextShutterFrameNumber = msg.frame_number + 1;
2461 }
2462
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002463 // Set timestamp for the request in the in-flight tracking
2464 // and get the request ID to send upstream
2465 {
2466 Mutex::Autolock l(mInFlightLock);
2467 idx = mInFlightMap.indexOfKey(msg.frame_number);
2468 if (idx >= 0) {
2469 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002470
2471 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2472 mId, __FUNCTION__,
2473 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2474 // Call listener, if any
2475 if (listener != NULL) {
2476 listener->notifyShutter(r.resultExtras, msg.timestamp);
2477 }
2478
2479 r.shutterTimestamp = msg.timestamp;
2480
2481 // send pending result and buffers
2482 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002483 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002484 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002485 returnOutputBuffers(r.pendingOutputBuffers.array(),
2486 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2487 r.pendingOutputBuffers.clear();
2488
2489 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002490 }
2491 }
2492 if (idx < 0) {
2493 SET_ERR("Shutter notification for non-existent frame number %d",
2494 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002495 }
2496}
2497
2498
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002499CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002500 ALOGV("%s", __FUNCTION__);
2501
Igor Murashkin1e479c02013-09-06 16:55:14 -07002502 CameraMetadata retVal;
2503
2504 if (mRequestThread != NULL) {
2505 retVal = mRequestThread->getLatestRequest();
2506 }
2507
Igor Murashkin1e479c02013-09-06 16:55:14 -07002508 return retVal;
2509}
2510
Jianing Weicb0652e2014-03-12 18:29:36 -07002511
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002512/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002513 * RequestThread inner class methods
2514 */
2515
2516Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002517 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002518 camera3_device_t *hal3Device,
2519 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002520 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002521 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002522 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002523 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002524 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002525 mReconfigured(false),
2526 mDoPause(false),
2527 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002528 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002529 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002530 mCurrentAfTriggerId(0),
2531 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002532 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2533 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002534 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002535}
2536
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002537void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002538 NotificationListener *listener) {
2539 Mutex::Autolock l(mRequestLock);
2540 mListener = listener;
2541}
2542
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002543void Camera3Device::RequestThread::configurationComplete() {
2544 Mutex::Autolock l(mRequestLock);
2545 mReconfigured = true;
2546}
2547
Jianing Wei90e59c92014-03-12 18:29:36 -07002548status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002549 List<sp<CaptureRequest> > &requests,
2550 /*out*/
2551 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002552 Mutex::Autolock l(mRequestLock);
2553 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2554 ++it) {
2555 mRequestQueue.push_back(*it);
2556 }
2557
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002558 if (lastFrameNumber != NULL) {
2559 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2560 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2561 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2562 *lastFrameNumber);
2563 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002564
Jianing Wei90e59c92014-03-12 18:29:36 -07002565 unpauseForNewRequests();
2566
2567 return OK;
2568}
2569
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002570
2571status_t Camera3Device::RequestThread::queueTrigger(
2572 RequestTrigger trigger[],
2573 size_t count) {
2574
2575 Mutex::Autolock l(mTriggerMutex);
2576 status_t ret;
2577
2578 for (size_t i = 0; i < count; ++i) {
2579 ret = queueTriggerLocked(trigger[i]);
2580
2581 if (ret != OK) {
2582 return ret;
2583 }
2584 }
2585
2586 return OK;
2587}
2588
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002589int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2590 sp<Camera3Device> d = device.promote();
2591 if (d != NULL) return d->mId;
2592 return 0;
2593}
2594
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002595status_t Camera3Device::RequestThread::queueTriggerLocked(
2596 RequestTrigger trigger) {
2597
2598 uint32_t tag = trigger.metadataTag;
2599 ssize_t index = mTriggerMap.indexOfKey(tag);
2600
2601 switch (trigger.getTagType()) {
2602 case TYPE_BYTE:
2603 // fall-through
2604 case TYPE_INT32:
2605 break;
2606 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002607 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2608 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002609 return INVALID_OPERATION;
2610 }
2611
2612 /**
2613 * Collect only the latest trigger, since we only have 1 field
2614 * in the request settings per trigger tag, and can't send more than 1
2615 * trigger per request.
2616 */
2617 if (index != NAME_NOT_FOUND) {
2618 mTriggerMap.editValueAt(index) = trigger;
2619 } else {
2620 mTriggerMap.add(tag, trigger);
2621 }
2622
2623 return OK;
2624}
2625
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002626status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002627 const RequestList &requests,
2628 /*out*/
2629 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002630 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002631 if (lastFrameNumber != NULL) {
2632 *lastFrameNumber = mRepeatingLastFrameNumber;
2633 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002634 mRepeatingRequests.clear();
2635 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2636 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002637
2638 unpauseForNewRequests();
2639
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002640 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002641 return OK;
2642}
2643
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002644bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2645 if (mRepeatingRequests.empty()) {
2646 return false;
2647 }
2648 int32_t requestId = requestIn->mResultExtras.requestId;
2649 const RequestList &repeatRequests = mRepeatingRequests;
2650 // All repeating requests are guaranteed to have same id so only check first quest
2651 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2652 return (firstRequest->mResultExtras.requestId == requestId);
2653}
2654
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002655status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002656 Mutex::Autolock l(mRequestLock);
2657 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002658 if (lastFrameNumber != NULL) {
2659 *lastFrameNumber = mRepeatingLastFrameNumber;
2660 }
2661 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002662 return OK;
2663}
2664
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002665status_t Camera3Device::RequestThread::clear(
2666 NotificationListener *listener,
2667 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002668 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002669 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002670
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002671 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002672
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002673 // Send errors for all requests pending in the request queue, including
2674 // pending repeating requests
2675 if (listener != NULL) {
2676 for (RequestList::iterator it = mRequestQueue.begin();
2677 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002678 // Abort the input buffers for reprocess requests.
2679 if ((*it)->mInputStream != NULL) {
2680 camera3_stream_buffer_t inputBuffer;
2681 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2682 if (res != OK) {
2683 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2684 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2685 } else {
2686 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2687 if (res != OK) {
2688 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2689 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2690 }
2691 }
2692 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002693 // Set the frame number this request would have had, if it
2694 // had been submitted; this frame number will not be reused.
2695 // The requestId and burstId fields were set when the request was
2696 // submitted originally (in convertMetadataListToRequestListLocked)
2697 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2698 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2699 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002700 }
2701 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002702 mRequestQueue.clear();
2703 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002704 if (lastFrameNumber != NULL) {
2705 *lastFrameNumber = mRepeatingLastFrameNumber;
2706 }
2707 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002708 return OK;
2709}
2710
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002711void Camera3Device::RequestThread::setPaused(bool paused) {
2712 Mutex::Autolock l(mPauseLock);
2713 mDoPause = paused;
2714 mDoPauseSignal.signal();
2715}
2716
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002717status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2718 int32_t requestId, nsecs_t timeout) {
2719 Mutex::Autolock l(mLatestRequestMutex);
2720 status_t res;
2721 while (mLatestRequestId != requestId) {
2722 nsecs_t startTime = systemTime();
2723
2724 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2725 if (res != OK) return res;
2726
2727 timeout -= (systemTime() - startTime);
2728 }
2729
2730 return OK;
2731}
2732
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002733void Camera3Device::RequestThread::requestExit() {
2734 // Call parent to set up shutdown
2735 Thread::requestExit();
2736 // The exit from any possible waits
2737 mDoPauseSignal.signal();
2738 mRequestSignal.signal();
2739}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002740
Chien-Yu Chend196d612015-06-22 19:49:01 -07002741
2742/**
2743 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2744 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2745 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2746 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2747 * request.
2748 */
2749void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2750 request->mAeTriggerCancelOverride.applyAeLock = false;
2751 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2752
2753 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2754 return;
2755 }
2756
2757 camera_metadata_entry_t aePrecaptureTrigger =
2758 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2759 if (aePrecaptureTrigger.count > 0 &&
2760 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2761 // Always override CANCEL to IDLE
2762 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2763 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2764 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2765 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2766 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2767
2768 if (mAeLockAvailable == true) {
2769 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2770 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2771 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2772 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2773 request->mAeTriggerCancelOverride.applyAeLock = true;
2774 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2775 }
2776 }
2777 }
2778}
2779
2780/**
2781 * Override result metadata for cancelling AE precapture trigger applied in
2782 * handleAePrecaptureCancelRequest().
2783 */
2784void Camera3Device::overrideResultForPrecaptureCancel(
2785 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2786 if (aeTriggerCancelOverride.applyAeLock) {
2787 // Only devices <= v3.2 should have this override
2788 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2789 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2790 }
2791
2792 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2793 // Only devices <= v3.2 should have this override
2794 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2795 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2796 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2797 }
2798}
2799
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002800bool Camera3Device::RequestThread::threadLoop() {
2801
2802 status_t res;
2803
2804 // Handle paused state.
2805 if (waitIfPaused()) {
2806 return true;
2807 }
2808
2809 // Get work to do
2810
2811 sp<CaptureRequest> nextRequest = waitForNextRequest();
2812 if (nextRequest == NULL) {
2813 return true;
2814 }
2815
2816 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002817 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002818 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002819 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002820
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002821 // Get the request ID, if any
2822 int requestId;
2823 camera_metadata_entry_t requestIdEntry =
2824 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2825 if (requestIdEntry.count > 0) {
2826 requestId = requestIdEntry.data.i32[0];
2827 } else {
2828 ALOGW("%s: Did not have android.request.id set in the request",
2829 __FUNCTION__);
2830 requestId = NAME_NOT_FOUND;
2831 }
2832
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002833 // Insert any queued triggers (before metadata is locked)
2834 int32_t triggerCount;
2835 res = insertTriggers(nextRequest);
2836 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002837 SET_ERR("RequestThread: Unable to insert triggers "
2838 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002839 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002840 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2841 return false;
2842 }
2843 triggerCount = res;
2844
2845 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2846
2847 // If the request is the same as last, or we had triggers last time
2848 if (mPrevRequest != nextRequest || triggersMixedIn) {
2849 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002850 * HAL workaround:
2851 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2852 */
2853 res = addDummyTriggerIds(nextRequest);
2854 if (res != OK) {
2855 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2856 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002857 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002858 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2859 return false;
2860 }
2861
2862 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002863 * The request should be presorted so accesses in HAL
2864 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2865 */
2866 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002867 request.settings = nextRequest->mSettings.getAndLock();
2868 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002869 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2870
2871 IF_ALOGV() {
2872 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2873 find_camera_metadata_ro_entry(
2874 request.settings,
2875 ANDROID_CONTROL_AF_TRIGGER,
2876 &e
2877 );
2878 if (e.count > 0) {
2879 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2880 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002881 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002882 e.data.u8[0]);
2883 }
2884 }
2885 } else {
2886 // leave request.settings NULL to indicate 'reuse latest given'
2887 ALOGVV("%s: Request settings are REUSED",
2888 __FUNCTION__);
2889 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002890
Zhijun Hef0d962a2014-06-30 10:24:11 -07002891 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002892
2893 // Fill in buffers
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002894 if (nextRequest->mInputStream != NULL) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002895 request.input_buffer = &nextRequest->mInputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002896 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002897 } else {
2898 request.input_buffer = NULL;
2899 }
2900
2901 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2902 nextRequest->mOutputStreams.size());
2903 request.output_buffers = outputBuffers.array();
2904 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2905 res = nextRequest->mOutputStreams.editItemAt(i)->
2906 getBuffer(&outputBuffers.editItemAt(i));
2907 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002908 // Can't get output buffer from gralloc queue - this could be due to
2909 // abandoned queue or other consumer misbehavior, so not a fatal
2910 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002911 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2912 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala0ec23d32015-05-28 15:15:54 -07002913 {
2914 Mutex::Autolock l(mRequestLock);
2915 if (mListener != NULL) {
2916 mListener->notifyError(
2917 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2918 nextRequest->mResultExtras);
2919 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002920 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002921 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2922 return true;
2923 }
2924 request.num_output_buffers++;
2925 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002926 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002927
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002928 // Log request in the in-flight queue
2929 sp<Camera3Device> parent = mParent.promote();
2930 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002931 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002932 CLOGE("RequestThread: Parent is gone");
2933 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2934 return false;
2935 }
2936
Jianing Weicb0652e2014-03-12 18:29:36 -07002937 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002938 totalNumBuffers, nextRequest->mResultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002939 /*hasInput*/request.input_buffer != NULL,
2940 nextRequest->mAeTriggerCancelOverride);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002941 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2942 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002943 __FUNCTION__,
2944 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2945 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002946 if (res != OK) {
2947 SET_ERR("RequestThread: Unable to register new in-flight request:"
2948 " %s (%d)", strerror(-res), res);
2949 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2950 return false;
2951 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002952
Zhijun Hecc27e112013-10-03 16:12:43 -07002953 // Inform waitUntilRequestProcessed thread of a new request ID
2954 {
2955 Mutex::Autolock al(mLatestRequestMutex);
2956
2957 mLatestRequestId = requestId;
2958 mLatestRequestSignal.signal();
2959 }
2960
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002961 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002962 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2963 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002964 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002965 ATRACE_END();
2966
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002967 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002968 // Should only get a failure here for malformed requests or device-level
2969 // errors, so consider all errors fatal. Bad metadata failures should
2970 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002971 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002972 " device: %s (%d)", request.frame_number, strerror(-res), res);
2973 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2974 return false;
2975 }
2976
Igor Murashkin1e479c02013-09-06 16:55:14 -07002977 // Update the latest request sent to HAL
2978 if (request.settings != NULL) { // Don't update them if they were unchanged
2979 Mutex::Autolock al(mLatestRequestMutex);
2980
2981 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2982 mLatestRequest.acquire(cloned);
2983 }
2984
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002985 if (request.settings != NULL) {
2986 nextRequest->mSettings.unlock(request.settings);
2987 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002988
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002989 // Unset as current request
2990 {
2991 Mutex::Autolock l(mRequestLock);
2992 mNextRequest.clear();
2993 }
2994
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002995 // Remove any previously queued triggers (after unlock)
2996 res = removeTriggers(mPrevRequest);
2997 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002998 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002999 "(capture request %d, HAL device: %s (%d)",
3000 request.frame_number, strerror(-res), res);
3001 return false;
3002 }
3003 mPrevTriggers = triggerCount;
3004
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003005 return true;
3006}
3007
Igor Murashkin1e479c02013-09-06 16:55:14 -07003008CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3009 Mutex::Autolock al(mLatestRequestMutex);
3010
3011 ALOGV("RequestThread::%s", __FUNCTION__);
3012
3013 return mLatestRequest;
3014}
3015
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003016bool Camera3Device::RequestThread::isStreamPending(
3017 sp<Camera3StreamInterface>& stream) {
3018 Mutex::Autolock l(mRequestLock);
3019
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003020 if (mNextRequest != nullptr) {
3021 for (const auto& s : mNextRequest->mOutputStreams) {
3022 if (stream == s) return true;
3023 }
3024 if (stream == mNextRequest->mInputStream) return true;
3025 }
3026
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003027 for (const auto& request : mRequestQueue) {
3028 for (const auto& s : request->mOutputStreams) {
3029 if (stream == s) return true;
3030 }
3031 if (stream == request->mInputStream) return true;
3032 }
3033
3034 for (const auto& request : mRepeatingRequests) {
3035 for (const auto& s : request->mOutputStreams) {
3036 if (stream == s) return true;
3037 }
3038 if (stream == request->mInputStream) return true;
3039 }
3040
3041 return false;
3042}
Jianing Weicb0652e2014-03-12 18:29:36 -07003043
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003044void Camera3Device::RequestThread::cleanUpFailedRequest(
3045 camera3_capture_request_t &request,
3046 sp<CaptureRequest> &nextRequest,
3047 Vector<camera3_stream_buffer_t> &outputBuffers) {
3048
3049 if (request.settings != NULL) {
3050 nextRequest->mSettings.unlock(request.settings);
3051 }
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003052 if (nextRequest->mInputStream != NULL) {
3053 nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3054 nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003055 }
3056 for (size_t i = 0; i < request.num_output_buffers; i++) {
3057 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3058 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
3059 outputBuffers[i], 0);
3060 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003061
3062 Mutex::Autolock l(mRequestLock);
3063 mNextRequest.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003064}
3065
3066sp<Camera3Device::CaptureRequest>
3067 Camera3Device::RequestThread::waitForNextRequest() {
3068 status_t res;
3069 sp<CaptureRequest> nextRequest;
3070
3071 // Optimized a bit for the simple steady-state case (single repeating
3072 // request), to avoid putting that request in the queue temporarily.
3073 Mutex::Autolock l(mRequestLock);
3074
3075 while (mRequestQueue.empty()) {
3076 if (!mRepeatingRequests.empty()) {
3077 // Always atomically enqueue all requests in a repeating request
3078 // list. Guarantees a complete in-sequence set of captures to
3079 // application.
3080 const RequestList &requests = mRepeatingRequests;
3081 RequestList::const_iterator firstRequest =
3082 requests.begin();
3083 nextRequest = *firstRequest;
3084 mRequestQueue.insert(mRequestQueue.end(),
3085 ++firstRequest,
3086 requests.end());
3087 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003088
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003089 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003090
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003091 break;
3092 }
3093
3094 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3095
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003096 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3097 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003098 Mutex::Autolock pl(mPauseLock);
3099 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003100 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003101 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003102 // Let the tracker know
3103 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3104 if (statusTracker != 0) {
3105 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3106 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003107 }
3108 // Stop waiting for now and let thread management happen
3109 return NULL;
3110 }
3111 }
3112
3113 if (nextRequest == NULL) {
3114 // Don't have a repeating request already in hand, so queue
3115 // must have an entry now.
3116 RequestList::iterator firstRequest =
3117 mRequestQueue.begin();
3118 nextRequest = *firstRequest;
3119 mRequestQueue.erase(firstRequest);
3120 }
3121
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003122 // In case we've been unpaused by setPaused clearing mDoPause, need to
3123 // update internal pause state (capture/setRepeatingRequest unpause
3124 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003125 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003126 if (mPaused) {
3127 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3128 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3129 if (statusTracker != 0) {
3130 statusTracker->markComponentActive(mStatusId);
3131 }
3132 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003133 mPaused = false;
3134
3135 // Check if we've reconfigured since last time, and reset the preview
3136 // request if so. Can't use 'NULL request == repeat' across configure calls.
3137 if (mReconfigured) {
3138 mPrevRequest.clear();
3139 mReconfigured = false;
3140 }
3141
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003142 if (nextRequest != NULL) {
3143 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003144 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3145 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003146
3147 // Since RequestThread::clear() removes buffers from the input stream,
3148 // get the right buffer here before unlocking mRequestLock
3149 if (nextRequest->mInputStream != NULL) {
3150 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3151 if (res != OK) {
3152 // Can't get input buffer from gralloc queue - this could be due to
3153 // disconnected queue or other producer misbehavior, so not a fatal
3154 // error
3155 ALOGE("%s: Can't get input buffer, skipping request:"
3156 " %s (%d)", __FUNCTION__, strerror(-res), res);
3157 if (mListener != NULL) {
3158 mListener->notifyError(
3159 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3160 nextRequest->mResultExtras);
3161 }
3162 return NULL;
3163 }
3164 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003165 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003166
3167 handleAePrecaptureCancelRequest(nextRequest);
3168
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003169 mNextRequest = nextRequest;
3170
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003171 return nextRequest;
3172}
3173
3174bool Camera3Device::RequestThread::waitIfPaused() {
3175 status_t res;
3176 Mutex::Autolock l(mPauseLock);
3177 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003178 if (mPaused == false) {
3179 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003180 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3181 // Let the tracker know
3182 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3183 if (statusTracker != 0) {
3184 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3185 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003186 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003187
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003188 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003189 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003190 return true;
3191 }
3192 }
3193 // We don't set mPaused to false here, because waitForNextRequest needs
3194 // to further manage the paused state in case of starvation.
3195 return false;
3196}
3197
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003198void Camera3Device::RequestThread::unpauseForNewRequests() {
3199 // With work to do, mark thread as unpaused.
3200 // If paused by request (setPaused), don't resume, to avoid
3201 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003202 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003203 Mutex::Autolock p(mPauseLock);
3204 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003205 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3206 if (mPaused) {
3207 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3208 if (statusTracker != 0) {
3209 statusTracker->markComponentActive(mStatusId);
3210 }
3211 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003212 mPaused = false;
3213 }
3214}
3215
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003216void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3217 sp<Camera3Device> parent = mParent.promote();
3218 if (parent != NULL) {
3219 va_list args;
3220 va_start(args, fmt);
3221
3222 parent->setErrorStateV(fmt, args);
3223
3224 va_end(args);
3225 }
3226}
3227
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003228status_t Camera3Device::RequestThread::insertTriggers(
3229 const sp<CaptureRequest> &request) {
3230
3231 Mutex::Autolock al(mTriggerMutex);
3232
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003233 sp<Camera3Device> parent = mParent.promote();
3234 if (parent == NULL) {
3235 CLOGE("RequestThread: Parent is gone");
3236 return DEAD_OBJECT;
3237 }
3238
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003239 CameraMetadata &metadata = request->mSettings;
3240 size_t count = mTriggerMap.size();
3241
3242 for (size_t i = 0; i < count; ++i) {
3243 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003244 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003245
3246 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3247 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3248 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003249 if (isAeTrigger) {
3250 request->mResultExtras.precaptureTriggerId = triggerId;
3251 mCurrentPreCaptureTriggerId = triggerId;
3252 } else {
3253 request->mResultExtras.afTriggerId = triggerId;
3254 mCurrentAfTriggerId = triggerId;
3255 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003256 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3257 continue; // Trigger ID tag is deprecated since device HAL 3.2
3258 }
3259 }
3260
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003261 camera_metadata_entry entry = metadata.find(tag);
3262
3263 if (entry.count > 0) {
3264 /**
3265 * Already has an entry for this trigger in the request.
3266 * Rewrite it with our requested trigger value.
3267 */
3268 RequestTrigger oldTrigger = trigger;
3269
3270 oldTrigger.entryValue = entry.data.u8[0];
3271
3272 mTriggerReplacedMap.add(tag, oldTrigger);
3273 } else {
3274 /**
3275 * More typical, no trigger entry, so we just add it
3276 */
3277 mTriggerRemovedMap.add(tag, trigger);
3278 }
3279
3280 status_t res;
3281
3282 switch (trigger.getTagType()) {
3283 case TYPE_BYTE: {
3284 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3285 res = metadata.update(tag,
3286 &entryValue,
3287 /*count*/1);
3288 break;
3289 }
3290 case TYPE_INT32:
3291 res = metadata.update(tag,
3292 &trigger.entryValue,
3293 /*count*/1);
3294 break;
3295 default:
3296 ALOGE("%s: Type not supported: 0x%x",
3297 __FUNCTION__,
3298 trigger.getTagType());
3299 return INVALID_OPERATION;
3300 }
3301
3302 if (res != OK) {
3303 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3304 ", value %d", __FUNCTION__, trigger.getTagName(),
3305 trigger.entryValue);
3306 return res;
3307 }
3308
3309 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3310 trigger.getTagName(),
3311 trigger.entryValue);
3312 }
3313
3314 mTriggerMap.clear();
3315
3316 return count;
3317}
3318
3319status_t Camera3Device::RequestThread::removeTriggers(
3320 const sp<CaptureRequest> &request) {
3321 Mutex::Autolock al(mTriggerMutex);
3322
3323 CameraMetadata &metadata = request->mSettings;
3324
3325 /**
3326 * Replace all old entries with their old values.
3327 */
3328 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3329 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3330
3331 status_t res;
3332
3333 uint32_t tag = trigger.metadataTag;
3334 switch (trigger.getTagType()) {
3335 case TYPE_BYTE: {
3336 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3337 res = metadata.update(tag,
3338 &entryValue,
3339 /*count*/1);
3340 break;
3341 }
3342 case TYPE_INT32:
3343 res = metadata.update(tag,
3344 &trigger.entryValue,
3345 /*count*/1);
3346 break;
3347 default:
3348 ALOGE("%s: Type not supported: 0x%x",
3349 __FUNCTION__,
3350 trigger.getTagType());
3351 return INVALID_OPERATION;
3352 }
3353
3354 if (res != OK) {
3355 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3356 ", trigger value %d", __FUNCTION__,
3357 trigger.getTagName(), trigger.entryValue);
3358 return res;
3359 }
3360 }
3361 mTriggerReplacedMap.clear();
3362
3363 /**
3364 * Remove all new entries.
3365 */
3366 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3367 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3368 status_t res = metadata.erase(trigger.metadataTag);
3369
3370 if (res != OK) {
3371 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3372 ", trigger value %d", __FUNCTION__,
3373 trigger.getTagName(), trigger.entryValue);
3374 return res;
3375 }
3376 }
3377 mTriggerRemovedMap.clear();
3378
3379 return OK;
3380}
3381
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003382status_t Camera3Device::RequestThread::addDummyTriggerIds(
3383 const sp<CaptureRequest> &request) {
3384 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3385 static const int32_t dummyTriggerId = 1;
3386 status_t res;
3387
3388 CameraMetadata &metadata = request->mSettings;
3389
3390 // If AF trigger is active, insert a dummy AF trigger ID if none already
3391 // exists
3392 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3393 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3394 if (afTrigger.count > 0 &&
3395 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3396 afId.count == 0) {
3397 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3398 if (res != OK) return res;
3399 }
3400
3401 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3402 // if none already exists
3403 camera_metadata_entry pcTrigger =
3404 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3405 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3406 if (pcTrigger.count > 0 &&
3407 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3408 pcId.count == 0) {
3409 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3410 &dummyTriggerId, 1);
3411 if (res != OK) return res;
3412 }
3413
3414 return OK;
3415}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003416
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003417/**
3418 * PreparerThread inner class methods
3419 */
3420
3421Camera3Device::PreparerThread::PreparerThread() :
3422 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3423}
3424
3425Camera3Device::PreparerThread::~PreparerThread() {
3426 Thread::requestExitAndWait();
3427 if (mCurrentStream != nullptr) {
3428 mCurrentStream->cancelPrepare();
3429 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3430 mCurrentStream.clear();
3431 }
3432 clear();
3433}
3434
3435status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) {
3436 status_t res;
3437
3438 Mutex::Autolock l(mLock);
3439
3440 res = stream->startPrepare();
3441 if (res == OK) {
3442 // No preparation needed, fire listener right off
3443 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3444 if (mListener) {
3445 mListener->notifyPrepared(stream->getId());
3446 }
3447 return OK;
3448 } else if (res != NOT_ENOUGH_DATA) {
3449 return res;
3450 }
3451
3452 // Need to prepare, start up thread if necessary
3453 if (!mActive) {
3454 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3455 // isn't running
3456 Thread::requestExitAndWait();
3457 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3458 if (res != OK) {
3459 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3460 if (mListener) {
3461 mListener->notifyPrepared(stream->getId());
3462 }
3463 return res;
3464 }
3465 mCancelNow = false;
3466 mActive = true;
3467 ALOGV("%s: Preparer stream started", __FUNCTION__);
3468 }
3469
3470 // queue up the work
3471 mPendingStreams.push_back(stream);
3472 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3473
3474 return OK;
3475}
3476
3477status_t Camera3Device::PreparerThread::clear() {
3478 status_t res;
3479
3480 Mutex::Autolock l(mLock);
3481
3482 for (const auto& stream : mPendingStreams) {
3483 stream->cancelPrepare();
3484 }
3485 mPendingStreams.clear();
3486 mCancelNow = true;
3487
3488 return OK;
3489}
3490
3491void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3492 Mutex::Autolock l(mLock);
3493 mListener = listener;
3494}
3495
3496bool Camera3Device::PreparerThread::threadLoop() {
3497 status_t res;
3498 {
3499 Mutex::Autolock l(mLock);
3500 if (mCurrentStream == nullptr) {
3501 // End thread if done with work
3502 if (mPendingStreams.empty()) {
3503 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3504 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3505 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3506 mActive = false;
3507 return false;
3508 }
3509
3510 // Get next stream to prepare
3511 auto it = mPendingStreams.begin();
3512 mCurrentStream = *it;
3513 mPendingStreams.erase(it);
3514 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3515 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3516 } else if (mCancelNow) {
3517 mCurrentStream->cancelPrepare();
3518 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3519 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3520 mCurrentStream.clear();
3521 mCancelNow = false;
3522 return true;
3523 }
3524 }
3525
3526 res = mCurrentStream->prepareNextBuffer();
3527 if (res == NOT_ENOUGH_DATA) return true;
3528 if (res != OK) {
3529 // Something bad happened; try to recover by cancelling prepare and
3530 // signalling listener anyway
3531 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3532 mCurrentStream->getId(), res, strerror(-res));
3533 mCurrentStream->cancelPrepare();
3534 }
3535
3536 // This stream has finished, notify listener
3537 Mutex::Autolock l(mLock);
3538 if (mListener) {
3539 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3540 mCurrentStream->getId());
3541 mListener->notifyPrepared(mCurrentStream->getId());
3542 }
3543
3544 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3545 mCurrentStream.clear();
3546
3547 return true;
3548}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003549
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003550/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003551 * Static callback forwarding methods from HAL to instance
3552 */
3553
3554void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3555 const camera3_capture_result *result) {
3556 Camera3Device *d =
3557 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003558
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003559 d->processCaptureResult(result);
3560}
3561
3562void Camera3Device::sNotify(const camera3_callback_ops *cb,
3563 const camera3_notify_msg *msg) {
3564 Camera3Device *d =
3565 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3566 d->notify(msg);
3567}
3568
3569}; // namespace android