blob: 9e73b5c4cff0a079125578ebf5e1ddde447d740e [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
Zhijun He204e3292014-07-14 17:09:23 -07001387uint32_t Camera3Device::getDeviceVersion() {
1388 ATRACE_CALL();
1389 Mutex::Autolock il(mInterfaceLock);
1390 return mDeviceVersion;
1391}
1392
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001393/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001394 * Methods called by subclasses
1395 */
1396
1397void Camera3Device::notifyStatus(bool idle) {
1398 {
1399 // Need mLock to safely update state and synchronize to current
1400 // state of methods in flight.
1401 Mutex::Autolock l(mLock);
1402 // We can get various system-idle notices from the status tracker
1403 // while starting up. Only care about them if we've actually sent
1404 // in some requests recently.
1405 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1406 return;
1407 }
1408 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1409 idle ? "idle" : "active");
1410 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1411 mRecentStatusUpdates.add(mStatus);
1412 mStatusChanged.signal();
1413
1414 // Skip notifying listener if we're doing some user-transparent
1415 // state changes
1416 if (mPauseStateNotify) return;
1417 }
1418 NotificationListener *listener;
1419 {
1420 Mutex::Autolock l(mOutputLock);
1421 listener = mListener;
1422 }
1423 if (idle && listener != NULL) {
1424 listener->notifyIdle();
1425 }
1426}
1427
1428/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001429 * Camera3Device private methods
1430 */
1431
1432sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1433 const CameraMetadata &request) {
1434 ATRACE_CALL();
1435 status_t res;
1436
1437 sp<CaptureRequest> newRequest = new CaptureRequest;
1438 newRequest->mSettings = request;
1439
1440 camera_metadata_entry_t inputStreams =
1441 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1442 if (inputStreams.count > 0) {
1443 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001444 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001445 CLOGE("Request references unknown input stream %d",
1446 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001447 return NULL;
1448 }
1449 // Lazy completion of stream configuration (allocation/registration)
1450 // on first use
1451 if (mInputStream->isConfiguring()) {
1452 res = mInputStream->finishConfiguration(mHal3Device);
1453 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001454 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001455 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001456 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001457 return NULL;
1458 }
1459 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001460 // Check if stream is being prepared
1461 if (mInputStream->isPreparing()) {
1462 CLOGE("Request references an input stream that's being prepared!");
1463 return NULL;
1464 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001465
1466 newRequest->mInputStream = mInputStream;
1467 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1468 }
1469
1470 camera_metadata_entry_t streams =
1471 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1472 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001473 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001474 return NULL;
1475 }
1476
1477 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001478 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001479 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001480 CLOGE("Request references unknown stream %d",
1481 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001482 return NULL;
1483 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001484 sp<Camera3OutputStreamInterface> stream =
1485 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001486
1487 // Lazy completion of stream configuration (allocation/registration)
1488 // on first use
1489 if (stream->isConfiguring()) {
1490 res = stream->finishConfiguration(mHal3Device);
1491 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001492 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1493 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001494 return NULL;
1495 }
1496 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001497 // Check if stream is being prepared
1498 if (stream->isPreparing()) {
1499 CLOGE("Request references an output stream that's being prepared!");
1500 return NULL;
1501 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001502
1503 newRequest->mOutputStreams.push(stream);
1504 }
1505 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1506
1507 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001508}
1509
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001510bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1511 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1512 Size size = mSupportedOpaqueInputSizes[i];
1513 if (size.width == width && size.height == height) {
1514 return true;
1515 }
1516 }
1517
1518 return false;
1519}
1520
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001521status_t Camera3Device::configureStreamsLocked() {
1522 ATRACE_CALL();
1523 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001524
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001525 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001526 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001527 return INVALID_OPERATION;
1528 }
1529
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001530 if (!mNeedConfig) {
1531 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1532 return OK;
1533 }
1534
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001535 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1536 // adding a dummy stream instead.
1537 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1538 if (mOutputStreams.size() == 0) {
1539 addDummyStreamLocked();
1540 } else {
1541 tryRemoveDummyStreamLocked();
1542 }
1543
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001544 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001545 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001546
1547 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001548 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1549 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1550 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001551 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1552
1553 Vector<camera3_stream_t*> streams;
1554 streams.setCapacity(config.num_streams);
1555
1556 if (mInputStream != NULL) {
1557 camera3_stream_t *inputStream;
1558 inputStream = mInputStream->startConfiguration();
1559 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001560 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001561 return INVALID_OPERATION;
1562 }
1563 streams.add(inputStream);
1564 }
1565
1566 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001567
1568 // Don't configure bidi streams twice, nor add them twice to the list
1569 if (mOutputStreams[i].get() ==
1570 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1571
1572 config.num_streams--;
1573 continue;
1574 }
1575
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001576 camera3_stream_t *outputStream;
1577 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1578 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001579 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001580 return INVALID_OPERATION;
1581 }
1582 streams.add(outputStream);
1583 }
1584
1585 config.streams = streams.editArray();
1586
1587 // Do the HAL configuration; will potentially touch stream
1588 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001589 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001590 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001591 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001592
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001593 if (res == BAD_VALUE) {
1594 // HAL rejected this set of streams as unsupported, clean up config
1595 // attempt and return to unconfigured state
1596 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1597 res = mInputStream->cancelConfiguration();
1598 if (res != OK) {
1599 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1600 mInputStream->getId(), strerror(-res), res);
1601 return res;
1602 }
1603 }
1604
1605 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1606 sp<Camera3OutputStreamInterface> outputStream =
1607 mOutputStreams.editValueAt(i);
1608 if (outputStream->isConfiguring()) {
1609 res = outputStream->cancelConfiguration();
1610 if (res != OK) {
1611 SET_ERR_L(
1612 "Can't cancel configuring output stream %d: %s (%d)",
1613 outputStream->getId(), strerror(-res), res);
1614 return res;
1615 }
1616 }
1617 }
1618
1619 // Return state to that at start of call, so that future configures
1620 // properly clean things up
1621 mStatus = STATUS_UNCONFIGURED;
1622 mNeedConfig = true;
1623
1624 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1625 return BAD_VALUE;
1626 } else if (res != OK) {
1627 // Some other kind of error from configure_streams - this is not
1628 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001629 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1630 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001631 return res;
1632 }
1633
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001634 // Finish all stream configuration immediately.
1635 // TODO: Try to relax this later back to lazy completion, which should be
1636 // faster
1637
Igor Murashkin073f8572013-05-02 14:59:28 -07001638 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001639 res = mInputStream->finishConfiguration(mHal3Device);
1640 if (res != OK) {
1641 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1642 mInputStream->getId(), strerror(-res), res);
1643 return res;
1644 }
1645 }
1646
1647 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001648 sp<Camera3OutputStreamInterface> outputStream =
1649 mOutputStreams.editValueAt(i);
1650 if (outputStream->isConfiguring()) {
1651 res = outputStream->finishConfiguration(mHal3Device);
1652 if (res != OK) {
1653 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1654 outputStream->getId(), strerror(-res), res);
1655 return res;
1656 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001657 }
1658 }
1659
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001660 // Request thread needs to know to avoid using repeat-last-settings protocol
1661 // across configure_streams() calls
1662 mRequestThread->configurationComplete();
1663
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001664 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001665
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001666 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001667
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001668 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001669 mStatus = STATUS_CONFIGURED;
1670 } else {
1671 mStatus = STATUS_UNCONFIGURED;
1672 }
1673
1674 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1675
Zhijun He0a210512014-07-24 13:45:15 -07001676 // tear down the deleted streams after configure streams.
1677 mDeletedStreams.clear();
1678
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001679 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001680}
1681
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001682status_t Camera3Device::addDummyStreamLocked() {
1683 ATRACE_CALL();
1684 status_t res;
1685
1686 if (mDummyStreamId != NO_STREAM) {
1687 // Should never be adding a second dummy stream when one is already
1688 // active
1689 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1690 __FUNCTION__, mId);
1691 return INVALID_OPERATION;
1692 }
1693
1694 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1695
1696 sp<Camera3OutputStreamInterface> dummyStream =
1697 new Camera3DummyStream(mNextStreamId);
1698
1699 res = mOutputStreams.add(mNextStreamId, dummyStream);
1700 if (res < 0) {
1701 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1702 return res;
1703 }
1704
1705 mDummyStreamId = mNextStreamId;
1706 mNextStreamId++;
1707
1708 return OK;
1709}
1710
1711status_t Camera3Device::tryRemoveDummyStreamLocked() {
1712 ATRACE_CALL();
1713 status_t res;
1714
1715 if (mDummyStreamId == NO_STREAM) return OK;
1716 if (mOutputStreams.size() == 1) return OK;
1717
1718 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1719
1720 // Ok, have a dummy stream and there's at least one other output stream,
1721 // so remove the dummy
1722
1723 sp<Camera3StreamInterface> deletedStream;
1724 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1725 if (outputStreamIdx == NAME_NOT_FOUND) {
1726 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1727 return INVALID_OPERATION;
1728 }
1729
1730 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1731 mOutputStreams.removeItemsAt(outputStreamIdx);
1732
1733 // Free up the stream endpoint so that it can be used by some other stream
1734 res = deletedStream->disconnect();
1735 if (res != OK) {
1736 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1737 // fall through since we want to still list the stream as deleted.
1738 }
1739 mDeletedStreams.add(deletedStream);
1740 mDummyStreamId = NO_STREAM;
1741
1742 return res;
1743}
1744
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001745void Camera3Device::setErrorState(const char *fmt, ...) {
1746 Mutex::Autolock l(mLock);
1747 va_list args;
1748 va_start(args, fmt);
1749
1750 setErrorStateLockedV(fmt, args);
1751
1752 va_end(args);
1753}
1754
1755void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1756 Mutex::Autolock l(mLock);
1757 setErrorStateLockedV(fmt, args);
1758}
1759
1760void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1761 va_list args;
1762 va_start(args, fmt);
1763
1764 setErrorStateLockedV(fmt, args);
1765
1766 va_end(args);
1767}
1768
1769void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001770 // Print out all error messages to log
1771 String8 errorCause = String8::formatV(fmt, args);
1772 ALOGE("Camera %d: %s", mId, errorCause.string());
1773
1774 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001775 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001776
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001777 mErrorCause = errorCause;
1778
1779 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001780 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001781
1782 // Notify upstream about a device error
1783 if (mListener != NULL) {
1784 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1785 CaptureResultExtras());
1786 }
1787
1788 // Save stack trace. View by dumping it later.
1789 CameraTraces::saveTrace();
1790 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001791}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001792
1793/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001794 * In-flight request management
1795 */
1796
Jianing Weicb0652e2014-03-12 18:29:36 -07001797status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07001798 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1799 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001800 ATRACE_CALL();
1801 Mutex::Autolock l(mInFlightLock);
1802
1803 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07001804 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
1805 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001806 if (res < 0) return res;
1807
1808 return OK;
1809}
1810
1811/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001812 * Check if all 3A fields are ready, and send off a partial 3A-only result
1813 * to the output frame queue
1814 */
Zhijun He204e3292014-07-14 17:09:23 -07001815bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001816 uint32_t frameNumber,
1817 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001818
1819 // Check if all 3A states are present
1820 // The full list of fields is
1821 // android.control.afMode
1822 // android.control.awbMode
1823 // android.control.aeState
1824 // android.control.awbState
1825 // android.control.afState
1826 // android.control.afTriggerID
1827 // android.control.aePrecaptureID
1828 // TODO: Add android.control.aeMode
1829
1830 bool gotAllStates = true;
1831
1832 uint8_t afMode;
1833 uint8_t awbMode;
1834 uint8_t aeState;
1835 uint8_t afState;
1836 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001837
1838 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1839 &afMode, frameNumber);
1840
1841 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1842 &awbMode, frameNumber);
1843
1844 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1845 &aeState, frameNumber);
1846
1847 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1848 &afState, frameNumber);
1849
1850 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1851 &awbState, frameNumber);
1852
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001853 if (!gotAllStates) return false;
1854
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001855 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001856 "AF state %d, AE state %d, AWB state %d, "
1857 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001858 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001859 afMode, awbMode,
1860 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001861 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001862
1863 // Got all states, so construct a minimal result to send
1864 // In addition to the above fields, this means adding in
1865 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001866 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001867 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001868
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001869 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001870
1871 Mutex::Autolock l(mOutputLock);
1872
Jianing Weicb0652e2014-03-12 18:29:36 -07001873 CaptureResult captureResult;
1874 captureResult.mResultExtras = resultExtras;
1875 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1876 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1877 // but not limited to CameraDeviceBase::getNextResult
1878 CaptureResult& min3AResult =
1879 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001880
Jianing Weicb0652e2014-03-12 18:29:36 -07001881 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1882 // TODO: This is problematic casting. Need to fix CameraMetadata.
1883 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001884 return false;
1885 }
1886
Jianing Weicb0652e2014-03-12 18:29:36 -07001887 int32_t requestId = resultExtras.requestId;
1888 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001889 &requestId, frameNumber)) {
1890 return false;
1891 }
1892
Zhijun He204e3292014-07-14 17:09:23 -07001893 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1894 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1895 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1896 &partialResult, frameNumber)) {
1897 return false;
1898 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001899 }
1900
Jianing Weicb0652e2014-03-12 18:29:36 -07001901 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001902 &afMode, frameNumber)) {
1903 return false;
1904 }
1905
Jianing Weicb0652e2014-03-12 18:29:36 -07001906 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001907 &awbMode, frameNumber)) {
1908 return false;
1909 }
1910
Jianing Weicb0652e2014-03-12 18:29:36 -07001911 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001912 &aeState, frameNumber)) {
1913 return false;
1914 }
1915
Jianing Weicb0652e2014-03-12 18:29:36 -07001916 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001917 &afState, frameNumber)) {
1918 return false;
1919 }
1920
Jianing Weicb0652e2014-03-12 18:29:36 -07001921 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001922 &awbState, frameNumber)) {
1923 return false;
1924 }
1925
Jianing Weicb0652e2014-03-12 18:29:36 -07001926 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001927 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001928 return false;
1929 }
1930
Jianing Weicb0652e2014-03-12 18:29:36 -07001931 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001932 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001933 return false;
1934 }
1935
Zhijun He204e3292014-07-14 17:09:23 -07001936 // We only send the aggregated partial when all 3A related metadata are available
1937 // For both API1 and API2.
1938 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001939 mResultSignal.signal();
1940
1941 return true;
1942}
1943
1944template<typename T>
1945bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001946 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001947 (void) frameNumber;
1948
1949 camera_metadata_ro_entry_t entry;
1950
1951 entry = result.find(tag);
1952 if (entry.count == 0) {
1953 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1954 mId, frameNumber, get_camera_metadata_tag_name(tag));
1955 return false;
1956 }
1957
1958 if (sizeof(T) == sizeof(uint8_t)) {
1959 *value = entry.data.u8[0];
1960 } else if (sizeof(T) == sizeof(int32_t)) {
1961 *value = entry.data.i32[0];
1962 } else {
1963 ALOGE("%s: Unexpected type", __FUNCTION__);
1964 return false;
1965 }
1966 return true;
1967}
1968
1969template<typename T>
1970bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001971 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001972 if (result.update(tag, value, 1) != NO_ERROR) {
1973 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1974 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1975 frameNumber, get_camera_metadata_tag_name(tag));
1976 return false;
1977 }
1978 return true;
1979}
1980
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08001981void Camera3Device::returnOutputBuffers(
1982 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
1983 nsecs_t timestamp) {
1984 for (size_t i = 0; i < numBuffers; i++)
1985 {
1986 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
1987 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
1988 // Note: stream may be deallocated at this point, if this buffer was
1989 // the last reference to it.
1990 if (res != OK) {
1991 ALOGE("Can't return buffer to its stream: %s (%d)",
1992 strerror(-res), res);
1993 }
1994 }
1995}
1996
1997
1998void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
1999
2000 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2001 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2002
2003 nsecs_t sensorTimestamp = request.sensorTimestamp;
2004 nsecs_t shutterTimestamp = request.shutterTimestamp;
2005
2006 // Check if it's okay to remove the request from InFlightMap:
2007 // In the case of a successful request:
2008 // all input and output buffers, all result metadata, shutter callback
2009 // arrived.
2010 // In the case of a unsuccessful request:
2011 // all input and output buffers arrived.
2012 if (request.numBuffersLeft == 0 &&
2013 (request.requestStatus != OK ||
2014 (request.haveResultMetadata && shutterTimestamp != 0))) {
2015 ATRACE_ASYNC_END("frame capture", frameNumber);
2016
2017 // Sanity check - if sensor timestamp matches shutter timestamp
2018 if (request.requestStatus == OK &&
2019 sensorTimestamp != shutterTimestamp) {
2020 SET_ERR("sensor timestamp (%" PRId64
2021 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2022 sensorTimestamp, frameNumber, shutterTimestamp);
2023 }
2024
2025 // for an unsuccessful request, it may have pending output buffers to
2026 // return.
2027 assert(request.requestStatus != OK ||
2028 request.pendingOutputBuffers.size() == 0);
2029 returnOutputBuffers(request.pendingOutputBuffers.array(),
2030 request.pendingOutputBuffers.size(), 0);
2031
2032 mInFlightMap.removeItemsAt(idx, 1);
2033
2034 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2035 }
2036
2037 // Sanity check - if we have too many in-flight frames, something has
2038 // likely gone wrong
2039 if (mInFlightMap.size() > kInFlightWarnLimit) {
2040 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
2041 }
2042}
2043
2044
2045void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2046 CaptureResultExtras &resultExtras,
2047 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002048 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002049 bool reprocess,
2050 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002051 if (pendingMetadata.isEmpty())
2052 return;
2053
2054 Mutex::Autolock l(mOutputLock);
2055
2056 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002057 if (reprocess) {
2058 if (frameNumber < mNextReprocessResultFrameNumber) {
2059 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002060 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002061 frameNumber, mNextReprocessResultFrameNumber);
2062 return;
2063 }
2064 mNextReprocessResultFrameNumber = frameNumber + 1;
2065 } else {
2066 if (frameNumber < mNextResultFrameNumber) {
2067 SET_ERR("Out-of-order capture result metadata submitted! "
2068 "(got frame number %d, expecting %d)",
2069 frameNumber, mNextResultFrameNumber);
2070 return;
2071 }
2072 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002073 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002074
2075 CaptureResult captureResult;
2076 captureResult.mResultExtras = resultExtras;
2077 captureResult.mMetadata = pendingMetadata;
2078
2079 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2080 (int32_t*)&frameNumber, 1) != OK) {
2081 SET_ERR("Failed to set frame# in metadata (%d)",
2082 frameNumber);
2083 return;
2084 } else {
2085 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2086 __FUNCTION__, mId, frameNumber);
2087 }
2088
2089 // Append any previous partials to form a complete result
2090 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2091 captureResult.mMetadata.append(collectedPartialResult);
2092 }
2093
2094 captureResult.mMetadata.sort();
2095
2096 // Check that there's a timestamp in the result metadata
2097 camera_metadata_entry entry =
2098 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2099 if (entry.count == 0) {
2100 SET_ERR("No timestamp provided by HAL for frame %d!",
2101 frameNumber);
2102 return;
2103 }
2104
Chien-Yu Chend196d612015-06-22 19:49:01 -07002105 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2106
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002107 // Valid result, insert into queue
2108 List<CaptureResult>::iterator queuedResult =
2109 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2110 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2111 ", burstId = %" PRId32, __FUNCTION__,
2112 queuedResult->mResultExtras.requestId,
2113 queuedResult->mResultExtras.frameNumber,
2114 queuedResult->mResultExtras.burstId);
2115
2116 mResultSignal.signal();
2117}
2118
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002119/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002120 * Camera HAL device callback methods
2121 */
2122
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002123void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002124 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002125
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002126 status_t res;
2127
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002128 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002129 if (result->result == NULL && result->num_output_buffers == 0 &&
2130 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002131 SET_ERR("No result data provided by HAL for frame %d",
2132 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002133 return;
2134 }
Zhijun He204e3292014-07-14 17:09:23 -07002135
2136 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2137 // partial_result to 1 when metadata is included in this result.
2138 if (!mUsePartialResult &&
2139 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2140 result->result != NULL &&
2141 result->partial_result != 1) {
2142 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2143 " if partial result is not supported",
2144 frameNumber, result->partial_result);
2145 return;
2146 }
2147
2148 bool isPartialResult = false;
2149 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002150 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002151 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002152
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002153 // Get shutter timestamp and resultExtras from list of in-flight requests,
2154 // where it was added by the shutter notification for this frame. If the
2155 // shutter timestamp isn't received yet, append the output buffers to the
2156 // in-flight request and they will be returned when the shutter timestamp
2157 // arrives. Update the in-flight status and remove the in-flight entry if
2158 // all result data and shutter timestamp have been received.
2159 nsecs_t shutterTimestamp = 0;
2160
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002161 {
2162 Mutex::Autolock l(mInFlightLock);
2163 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2164 if (idx == NAME_NOT_FOUND) {
2165 SET_ERR("Unknown frame number for capture result: %d",
2166 frameNumber);
2167 return;
2168 }
2169 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002170 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2171 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2172 ", partialResultCount = %d",
2173 __FUNCTION__, request.resultExtras.requestId,
2174 request.resultExtras.frameNumber, request.resultExtras.burstId,
2175 result->partial_result);
2176 // Always update the partial count to the latest one if it's not 0
2177 // (buffers only). When framework aggregates adjacent partial results
2178 // into one, the latest partial count will be used.
2179 if (result->partial_result != 0)
2180 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002181
2182 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002183 if (mUsePartialResult && result->result != NULL) {
2184 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2185 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2186 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2187 " the range of [1, %d] when metadata is included in the result",
2188 frameNumber, result->partial_result, mNumPartialResults);
2189 return;
2190 }
2191 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002192 if (isPartialResult) {
2193 request.partialResult.collectedResult.append(result->result);
2194 }
Zhijun He204e3292014-07-14 17:09:23 -07002195 } else {
2196 camera_metadata_ro_entry_t partialResultEntry;
2197 res = find_camera_metadata_ro_entry(result->result,
2198 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2199 if (res != NAME_NOT_FOUND &&
2200 partialResultEntry.count > 0 &&
2201 partialResultEntry.data.u8[0] ==
2202 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2203 // A partial result. Flag this as such, and collect this
2204 // set of metadata into the in-flight entry.
2205 isPartialResult = true;
2206 request.partialResult.collectedResult.append(
2207 result->result);
2208 request.partialResult.collectedResult.erase(
2209 ANDROID_QUIRKS_PARTIAL_RESULT);
2210 }
2211 }
2212
2213 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002214 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002215 if (!request.partialResult.haveSent3A) {
2216 request.partialResult.haveSent3A =
2217 processPartial3AResult(frameNumber,
2218 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002219 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002220 }
2221 }
2222 }
2223
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002224 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002225 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002226
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002227 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002228 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002229 if (request.haveResultMetadata) {
2230 SET_ERR("Called multiple times with metadata for frame %d",
2231 frameNumber);
2232 return;
2233 }
Zhijun He204e3292014-07-14 17:09:23 -07002234 if (mUsePartialResult &&
2235 !request.partialResult.collectedResult.isEmpty()) {
2236 collectedPartialResult.acquire(
2237 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002238 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002239 request.haveResultMetadata = true;
2240 }
2241
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002242 uint32_t numBuffersReturned = result->num_output_buffers;
2243 if (result->input_buffer != NULL) {
2244 if (hasInputBufferInRequest) {
2245 numBuffersReturned += 1;
2246 } else {
2247 ALOGW("%s: Input buffer should be NULL if there is no input"
2248 " buffer sent in the request",
2249 __FUNCTION__);
2250 }
2251 }
2252 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002253 if (request.numBuffersLeft < 0) {
2254 SET_ERR("Too many buffers returned for frame %d",
2255 frameNumber);
2256 return;
2257 }
2258
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002259 camera_metadata_ro_entry_t entry;
2260 res = find_camera_metadata_ro_entry(result->result,
2261 ANDROID_SENSOR_TIMESTAMP, &entry);
2262 if (res == OK && entry.count == 1) {
2263 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002264 }
2265
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002266 // If shutter event isn't received yet, append the output buffers to
2267 // the in-flight request. Otherwise, return the output buffers to
2268 // streams.
2269 if (shutterTimestamp == 0) {
2270 request.pendingOutputBuffers.appendArray(result->output_buffers,
2271 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002272 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002273 returnOutputBuffers(result->output_buffers,
2274 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002275 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002276
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002277 if (result->result != NULL && !isPartialResult) {
2278 if (shutterTimestamp == 0) {
2279 request.pendingMetadata = result->result;
2280 request.partialResult.collectedResult = collectedPartialResult;
2281 } else {
2282 CameraMetadata metadata;
2283 metadata = result->result;
2284 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002285 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2286 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002287 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002288 }
2289
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002290 removeInFlightRequestIfReadyLocked(idx);
2291 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002292
Zhijun Hef0d962a2014-06-30 10:24:11 -07002293 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002294 if (hasInputBufferInRequest) {
2295 Camera3Stream *stream =
2296 Camera3Stream::cast(result->input_buffer->stream);
2297 res = stream->returnInputBuffer(*(result->input_buffer));
2298 // Note: stream may be deallocated at this point, if this buffer was the
2299 // last reference to it.
2300 if (res != OK) {
2301 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2302 " its stream:%s (%d)", __FUNCTION__,
2303 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002304 }
2305 } else {
2306 ALOGW("%s: Input buffer should be NULL if there is no input"
2307 " buffer sent in the request, skipping input buffer return.",
2308 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002309 }
2310 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002311}
2312
2313void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002314 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002315 NotificationListener *listener;
2316 {
2317 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002318 listener = mListener;
2319 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002320
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002321 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002322 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002323 return;
2324 }
2325
2326 switch (msg->type) {
2327 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002328 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002329 break;
2330 }
2331 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002332 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002333 break;
2334 }
2335 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002336 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002337 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002338 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002339}
2340
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002341void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2342 NotificationListener *listener) {
2343
2344 // Map camera HAL error codes to ICameraDeviceCallback error codes
2345 // Index into this with the HAL error code
2346 static const ICameraDeviceCallbacks::CameraErrorCode
2347 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2348 // 0 = Unused error code
2349 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2350 // 1 = CAMERA3_MSG_ERROR_DEVICE
2351 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2352 // 2 = CAMERA3_MSG_ERROR_REQUEST
2353 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2354 // 3 = CAMERA3_MSG_ERROR_RESULT
2355 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2356 // 4 = CAMERA3_MSG_ERROR_BUFFER
2357 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2358 };
2359
2360 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2361 ((msg.error_code >= 0) &&
2362 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2363 halErrorMap[msg.error_code] :
2364 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2365
2366 int streamId = 0;
2367 if (msg.error_stream != NULL) {
2368 Camera3Stream *stream =
2369 Camera3Stream::cast(msg.error_stream);
2370 streamId = stream->getId();
2371 }
2372 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2373 mId, __FUNCTION__, msg.frame_number,
2374 streamId, msg.error_code);
2375
2376 CaptureResultExtras resultExtras;
2377 switch (errorCode) {
2378 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2379 // SET_ERR calls notifyError
2380 SET_ERR("Camera HAL reported serious device error");
2381 break;
2382 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2383 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2384 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2385 {
2386 Mutex::Autolock l(mInFlightLock);
2387 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2388 if (idx >= 0) {
2389 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2390 r.requestStatus = msg.error_code;
2391 resultExtras = r.resultExtras;
2392 } else {
2393 resultExtras.frameNumber = msg.frame_number;
2394 ALOGE("Camera %d: %s: cannot find in-flight request on "
2395 "frame %" PRId64 " error", mId, __FUNCTION__,
2396 resultExtras.frameNumber);
2397 }
2398 }
2399 if (listener != NULL) {
2400 listener->notifyError(errorCode, resultExtras);
2401 } else {
2402 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2403 }
2404 break;
2405 default:
2406 // SET_ERR calls notifyError
2407 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2408 break;
2409 }
2410}
2411
2412void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2413 NotificationListener *listener) {
2414 ssize_t idx;
2415 // Verify ordering of shutter notifications
2416 {
2417 Mutex::Autolock l(mOutputLock);
2418 // TODO: need to track errors for tighter bounds on expected frame number.
2419 if (msg.frame_number < mNextShutterFrameNumber) {
2420 SET_ERR("Shutter notification out-of-order. Expected "
2421 "notification for frame %d, got frame %d",
2422 mNextShutterFrameNumber, msg.frame_number);
2423 return;
2424 }
2425 mNextShutterFrameNumber = msg.frame_number + 1;
2426 }
2427
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002428 // Set timestamp for the request in the in-flight tracking
2429 // and get the request ID to send upstream
2430 {
2431 Mutex::Autolock l(mInFlightLock);
2432 idx = mInFlightMap.indexOfKey(msg.frame_number);
2433 if (idx >= 0) {
2434 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002435
2436 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2437 mId, __FUNCTION__,
2438 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2439 // Call listener, if any
2440 if (listener != NULL) {
2441 listener->notifyShutter(r.resultExtras, msg.timestamp);
2442 }
2443
2444 r.shutterTimestamp = msg.timestamp;
2445
2446 // send pending result and buffers
2447 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002448 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002449 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002450 returnOutputBuffers(r.pendingOutputBuffers.array(),
2451 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2452 r.pendingOutputBuffers.clear();
2453
2454 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002455 }
2456 }
2457 if (idx < 0) {
2458 SET_ERR("Shutter notification for non-existent frame number %d",
2459 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002460 }
2461}
2462
2463
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002464CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002465 ALOGV("%s", __FUNCTION__);
2466
Igor Murashkin1e479c02013-09-06 16:55:14 -07002467 CameraMetadata retVal;
2468
2469 if (mRequestThread != NULL) {
2470 retVal = mRequestThread->getLatestRequest();
2471 }
2472
Igor Murashkin1e479c02013-09-06 16:55:14 -07002473 return retVal;
2474}
2475
Jianing Weicb0652e2014-03-12 18:29:36 -07002476
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002477/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002478 * RequestThread inner class methods
2479 */
2480
2481Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002482 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002483 camera3_device_t *hal3Device,
2484 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002485 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002486 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002487 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002488 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002489 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002490 mReconfigured(false),
2491 mDoPause(false),
2492 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002493 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002494 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002495 mCurrentAfTriggerId(0),
2496 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002497 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2498 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002499 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002500}
2501
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002502void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002503 NotificationListener *listener) {
2504 Mutex::Autolock l(mRequestLock);
2505 mListener = listener;
2506}
2507
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002508void Camera3Device::RequestThread::configurationComplete() {
2509 Mutex::Autolock l(mRequestLock);
2510 mReconfigured = true;
2511}
2512
Jianing Wei90e59c92014-03-12 18:29:36 -07002513status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002514 List<sp<CaptureRequest> > &requests,
2515 /*out*/
2516 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002517 Mutex::Autolock l(mRequestLock);
2518 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2519 ++it) {
2520 mRequestQueue.push_back(*it);
2521 }
2522
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002523 if (lastFrameNumber != NULL) {
2524 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2525 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2526 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2527 *lastFrameNumber);
2528 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002529
Jianing Wei90e59c92014-03-12 18:29:36 -07002530 unpauseForNewRequests();
2531
2532 return OK;
2533}
2534
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002535
2536status_t Camera3Device::RequestThread::queueTrigger(
2537 RequestTrigger trigger[],
2538 size_t count) {
2539
2540 Mutex::Autolock l(mTriggerMutex);
2541 status_t ret;
2542
2543 for (size_t i = 0; i < count; ++i) {
2544 ret = queueTriggerLocked(trigger[i]);
2545
2546 if (ret != OK) {
2547 return ret;
2548 }
2549 }
2550
2551 return OK;
2552}
2553
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002554int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2555 sp<Camera3Device> d = device.promote();
2556 if (d != NULL) return d->mId;
2557 return 0;
2558}
2559
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002560status_t Camera3Device::RequestThread::queueTriggerLocked(
2561 RequestTrigger trigger) {
2562
2563 uint32_t tag = trigger.metadataTag;
2564 ssize_t index = mTriggerMap.indexOfKey(tag);
2565
2566 switch (trigger.getTagType()) {
2567 case TYPE_BYTE:
2568 // fall-through
2569 case TYPE_INT32:
2570 break;
2571 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002572 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2573 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002574 return INVALID_OPERATION;
2575 }
2576
2577 /**
2578 * Collect only the latest trigger, since we only have 1 field
2579 * in the request settings per trigger tag, and can't send more than 1
2580 * trigger per request.
2581 */
2582 if (index != NAME_NOT_FOUND) {
2583 mTriggerMap.editValueAt(index) = trigger;
2584 } else {
2585 mTriggerMap.add(tag, trigger);
2586 }
2587
2588 return OK;
2589}
2590
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002591status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002592 const RequestList &requests,
2593 /*out*/
2594 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002595 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002596 if (lastFrameNumber != NULL) {
2597 *lastFrameNumber = mRepeatingLastFrameNumber;
2598 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002599 mRepeatingRequests.clear();
2600 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2601 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002602
2603 unpauseForNewRequests();
2604
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002605 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002606 return OK;
2607}
2608
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002609bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2610 if (mRepeatingRequests.empty()) {
2611 return false;
2612 }
2613 int32_t requestId = requestIn->mResultExtras.requestId;
2614 const RequestList &repeatRequests = mRepeatingRequests;
2615 // All repeating requests are guaranteed to have same id so only check first quest
2616 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2617 return (firstRequest->mResultExtras.requestId == requestId);
2618}
2619
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002620status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002621 Mutex::Autolock l(mRequestLock);
2622 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002623 if (lastFrameNumber != NULL) {
2624 *lastFrameNumber = mRepeatingLastFrameNumber;
2625 }
2626 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002627 return OK;
2628}
2629
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002630status_t Camera3Device::RequestThread::clear(
2631 NotificationListener *listener,
2632 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002633 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002634 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002635
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002636 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002637
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002638 // Send errors for all requests pending in the request queue, including
2639 // pending repeating requests
2640 if (listener != NULL) {
2641 for (RequestList::iterator it = mRequestQueue.begin();
2642 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002643 // Abort the input buffers for reprocess requests.
2644 if ((*it)->mInputStream != NULL) {
2645 camera3_stream_buffer_t inputBuffer;
2646 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2647 if (res != OK) {
2648 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2649 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2650 } else {
2651 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2652 if (res != OK) {
2653 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2654 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2655 }
2656 }
2657 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002658 // Set the frame number this request would have had, if it
2659 // had been submitted; this frame number will not be reused.
2660 // The requestId and burstId fields were set when the request was
2661 // submitted originally (in convertMetadataListToRequestListLocked)
2662 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2663 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2664 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002665 }
2666 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002667 mRequestQueue.clear();
2668 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002669 if (lastFrameNumber != NULL) {
2670 *lastFrameNumber = mRepeatingLastFrameNumber;
2671 }
2672 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002673 return OK;
2674}
2675
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002676void Camera3Device::RequestThread::setPaused(bool paused) {
2677 Mutex::Autolock l(mPauseLock);
2678 mDoPause = paused;
2679 mDoPauseSignal.signal();
2680}
2681
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002682status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2683 int32_t requestId, nsecs_t timeout) {
2684 Mutex::Autolock l(mLatestRequestMutex);
2685 status_t res;
2686 while (mLatestRequestId != requestId) {
2687 nsecs_t startTime = systemTime();
2688
2689 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2690 if (res != OK) return res;
2691
2692 timeout -= (systemTime() - startTime);
2693 }
2694
2695 return OK;
2696}
2697
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002698void Camera3Device::RequestThread::requestExit() {
2699 // Call parent to set up shutdown
2700 Thread::requestExit();
2701 // The exit from any possible waits
2702 mDoPauseSignal.signal();
2703 mRequestSignal.signal();
2704}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002705
Chien-Yu Chend196d612015-06-22 19:49:01 -07002706
2707/**
2708 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2709 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2710 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2711 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2712 * request.
2713 */
2714void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2715 request->mAeTriggerCancelOverride.applyAeLock = false;
2716 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2717
2718 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2719 return;
2720 }
2721
2722 camera_metadata_entry_t aePrecaptureTrigger =
2723 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2724 if (aePrecaptureTrigger.count > 0 &&
2725 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2726 // Always override CANCEL to IDLE
2727 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2728 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2729 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2730 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2731 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2732
2733 if (mAeLockAvailable == true) {
2734 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2735 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2736 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2737 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2738 request->mAeTriggerCancelOverride.applyAeLock = true;
2739 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2740 }
2741 }
2742 }
2743}
2744
2745/**
2746 * Override result metadata for cancelling AE precapture trigger applied in
2747 * handleAePrecaptureCancelRequest().
2748 */
2749void Camera3Device::overrideResultForPrecaptureCancel(
2750 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2751 if (aeTriggerCancelOverride.applyAeLock) {
2752 // Only devices <= v3.2 should have this override
2753 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2754 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2755 }
2756
2757 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2758 // Only devices <= v3.2 should have this override
2759 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2760 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2761 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2762 }
2763}
2764
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002765bool Camera3Device::RequestThread::threadLoop() {
2766
2767 status_t res;
2768
2769 // Handle paused state.
2770 if (waitIfPaused()) {
2771 return true;
2772 }
2773
2774 // Get work to do
2775
2776 sp<CaptureRequest> nextRequest = waitForNextRequest();
2777 if (nextRequest == NULL) {
2778 return true;
2779 }
2780
2781 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002782 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002783 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002784 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002785
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002786 // Get the request ID, if any
2787 int requestId;
2788 camera_metadata_entry_t requestIdEntry =
2789 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2790 if (requestIdEntry.count > 0) {
2791 requestId = requestIdEntry.data.i32[0];
2792 } else {
2793 ALOGW("%s: Did not have android.request.id set in the request",
2794 __FUNCTION__);
2795 requestId = NAME_NOT_FOUND;
2796 }
2797
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002798 // Insert any queued triggers (before metadata is locked)
2799 int32_t triggerCount;
2800 res = insertTriggers(nextRequest);
2801 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002802 SET_ERR("RequestThread: Unable to insert triggers "
2803 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002804 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002805 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2806 return false;
2807 }
2808 triggerCount = res;
2809
2810 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2811
2812 // If the request is the same as last, or we had triggers last time
2813 if (mPrevRequest != nextRequest || triggersMixedIn) {
2814 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002815 * HAL workaround:
2816 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2817 */
2818 res = addDummyTriggerIds(nextRequest);
2819 if (res != OK) {
2820 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2821 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002822 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002823 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2824 return false;
2825 }
2826
2827 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002828 * The request should be presorted so accesses in HAL
2829 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2830 */
2831 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002832 request.settings = nextRequest->mSettings.getAndLock();
2833 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002834 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2835
2836 IF_ALOGV() {
2837 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2838 find_camera_metadata_ro_entry(
2839 request.settings,
2840 ANDROID_CONTROL_AF_TRIGGER,
2841 &e
2842 );
2843 if (e.count > 0) {
2844 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2845 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002846 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002847 e.data.u8[0]);
2848 }
2849 }
2850 } else {
2851 // leave request.settings NULL to indicate 'reuse latest given'
2852 ALOGVV("%s: Request settings are REUSED",
2853 __FUNCTION__);
2854 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002855
Zhijun Hef0d962a2014-06-30 10:24:11 -07002856 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002857
2858 // Fill in buffers
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002859 if (nextRequest->mInputStream != NULL) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002860 request.input_buffer = &nextRequest->mInputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002861 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002862 } else {
2863 request.input_buffer = NULL;
2864 }
2865
2866 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2867 nextRequest->mOutputStreams.size());
2868 request.output_buffers = outputBuffers.array();
2869 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2870 res = nextRequest->mOutputStreams.editItemAt(i)->
2871 getBuffer(&outputBuffers.editItemAt(i));
2872 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002873 // Can't get output buffer from gralloc queue - this could be due to
2874 // abandoned queue or other consumer misbehavior, so not a fatal
2875 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002876 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2877 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala0ec23d32015-05-28 15:15:54 -07002878 {
2879 Mutex::Autolock l(mRequestLock);
2880 if (mListener != NULL) {
2881 mListener->notifyError(
2882 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2883 nextRequest->mResultExtras);
2884 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002885 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002886 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2887 return true;
2888 }
2889 request.num_output_buffers++;
2890 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002891 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002892
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002893 // Log request in the in-flight queue
2894 sp<Camera3Device> parent = mParent.promote();
2895 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002896 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002897 CLOGE("RequestThread: Parent is gone");
2898 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2899 return false;
2900 }
2901
Jianing Weicb0652e2014-03-12 18:29:36 -07002902 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002903 totalNumBuffers, nextRequest->mResultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002904 /*hasInput*/request.input_buffer != NULL,
2905 nextRequest->mAeTriggerCancelOverride);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002906 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2907 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002908 __FUNCTION__,
2909 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2910 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002911 if (res != OK) {
2912 SET_ERR("RequestThread: Unable to register new in-flight request:"
2913 " %s (%d)", strerror(-res), res);
2914 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2915 return false;
2916 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002917
Zhijun Hecc27e112013-10-03 16:12:43 -07002918 // Inform waitUntilRequestProcessed thread of a new request ID
2919 {
2920 Mutex::Autolock al(mLatestRequestMutex);
2921
2922 mLatestRequestId = requestId;
2923 mLatestRequestSignal.signal();
2924 }
2925
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002926 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002927 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2928 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002929 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002930 ATRACE_END();
2931
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002932 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002933 // Should only get a failure here for malformed requests or device-level
2934 // errors, so consider all errors fatal. Bad metadata failures should
2935 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002936 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002937 " device: %s (%d)", request.frame_number, strerror(-res), res);
2938 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2939 return false;
2940 }
2941
Igor Murashkin1e479c02013-09-06 16:55:14 -07002942 // Update the latest request sent to HAL
2943 if (request.settings != NULL) { // Don't update them if they were unchanged
2944 Mutex::Autolock al(mLatestRequestMutex);
2945
2946 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2947 mLatestRequest.acquire(cloned);
2948 }
2949
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002950 if (request.settings != NULL) {
2951 nextRequest->mSettings.unlock(request.settings);
2952 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002953
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002954 // Unset as current request
2955 {
2956 Mutex::Autolock l(mRequestLock);
2957 mNextRequest.clear();
2958 }
2959
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002960 // Remove any previously queued triggers (after unlock)
2961 res = removeTriggers(mPrevRequest);
2962 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002963 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002964 "(capture request %d, HAL device: %s (%d)",
2965 request.frame_number, strerror(-res), res);
2966 return false;
2967 }
2968 mPrevTriggers = triggerCount;
2969
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002970 return true;
2971}
2972
Igor Murashkin1e479c02013-09-06 16:55:14 -07002973CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2974 Mutex::Autolock al(mLatestRequestMutex);
2975
2976 ALOGV("RequestThread::%s", __FUNCTION__);
2977
2978 return mLatestRequest;
2979}
2980
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002981bool Camera3Device::RequestThread::isStreamPending(
2982 sp<Camera3StreamInterface>& stream) {
2983 Mutex::Autolock l(mRequestLock);
2984
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002985 if (mNextRequest != nullptr) {
2986 for (const auto& s : mNextRequest->mOutputStreams) {
2987 if (stream == s) return true;
2988 }
2989 if (stream == mNextRequest->mInputStream) return true;
2990 }
2991
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002992 for (const auto& request : mRequestQueue) {
2993 for (const auto& s : request->mOutputStreams) {
2994 if (stream == s) return true;
2995 }
2996 if (stream == request->mInputStream) return true;
2997 }
2998
2999 for (const auto& request : mRepeatingRequests) {
3000 for (const auto& s : request->mOutputStreams) {
3001 if (stream == s) return true;
3002 }
3003 if (stream == request->mInputStream) return true;
3004 }
3005
3006 return false;
3007}
Jianing Weicb0652e2014-03-12 18:29:36 -07003008
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003009void Camera3Device::RequestThread::cleanUpFailedRequest(
3010 camera3_capture_request_t &request,
3011 sp<CaptureRequest> &nextRequest,
3012 Vector<camera3_stream_buffer_t> &outputBuffers) {
3013
3014 if (request.settings != NULL) {
3015 nextRequest->mSettings.unlock(request.settings);
3016 }
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003017 if (nextRequest->mInputStream != NULL) {
3018 nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3019 nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003020 }
3021 for (size_t i = 0; i < request.num_output_buffers; i++) {
3022 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3023 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
3024 outputBuffers[i], 0);
3025 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003026
3027 Mutex::Autolock l(mRequestLock);
3028 mNextRequest.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003029}
3030
3031sp<Camera3Device::CaptureRequest>
3032 Camera3Device::RequestThread::waitForNextRequest() {
3033 status_t res;
3034 sp<CaptureRequest> nextRequest;
3035
3036 // Optimized a bit for the simple steady-state case (single repeating
3037 // request), to avoid putting that request in the queue temporarily.
3038 Mutex::Autolock l(mRequestLock);
3039
3040 while (mRequestQueue.empty()) {
3041 if (!mRepeatingRequests.empty()) {
3042 // Always atomically enqueue all requests in a repeating request
3043 // list. Guarantees a complete in-sequence set of captures to
3044 // application.
3045 const RequestList &requests = mRepeatingRequests;
3046 RequestList::const_iterator firstRequest =
3047 requests.begin();
3048 nextRequest = *firstRequest;
3049 mRequestQueue.insert(mRequestQueue.end(),
3050 ++firstRequest,
3051 requests.end());
3052 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003053
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003054 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003055
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003056 break;
3057 }
3058
3059 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3060
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003061 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3062 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003063 Mutex::Autolock pl(mPauseLock);
3064 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003065 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003066 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003067 // Let the tracker know
3068 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3069 if (statusTracker != 0) {
3070 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3071 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003072 }
3073 // Stop waiting for now and let thread management happen
3074 return NULL;
3075 }
3076 }
3077
3078 if (nextRequest == NULL) {
3079 // Don't have a repeating request already in hand, so queue
3080 // must have an entry now.
3081 RequestList::iterator firstRequest =
3082 mRequestQueue.begin();
3083 nextRequest = *firstRequest;
3084 mRequestQueue.erase(firstRequest);
3085 }
3086
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003087 // In case we've been unpaused by setPaused clearing mDoPause, need to
3088 // update internal pause state (capture/setRepeatingRequest unpause
3089 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003090 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003091 if (mPaused) {
3092 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3093 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3094 if (statusTracker != 0) {
3095 statusTracker->markComponentActive(mStatusId);
3096 }
3097 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003098 mPaused = false;
3099
3100 // Check if we've reconfigured since last time, and reset the preview
3101 // request if so. Can't use 'NULL request == repeat' across configure calls.
3102 if (mReconfigured) {
3103 mPrevRequest.clear();
3104 mReconfigured = false;
3105 }
3106
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003107 if (nextRequest != NULL) {
3108 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003109 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3110 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003111
3112 // Since RequestThread::clear() removes buffers from the input stream,
3113 // get the right buffer here before unlocking mRequestLock
3114 if (nextRequest->mInputStream != NULL) {
3115 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3116 if (res != OK) {
3117 // Can't get input buffer from gralloc queue - this could be due to
3118 // disconnected queue or other producer misbehavior, so not a fatal
3119 // error
3120 ALOGE("%s: Can't get input buffer, skipping request:"
3121 " %s (%d)", __FUNCTION__, strerror(-res), res);
3122 if (mListener != NULL) {
3123 mListener->notifyError(
3124 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3125 nextRequest->mResultExtras);
3126 }
3127 return NULL;
3128 }
3129 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003130 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003131
3132 handleAePrecaptureCancelRequest(nextRequest);
3133
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003134 mNextRequest = nextRequest;
3135
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003136 return nextRequest;
3137}
3138
3139bool Camera3Device::RequestThread::waitIfPaused() {
3140 status_t res;
3141 Mutex::Autolock l(mPauseLock);
3142 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003143 if (mPaused == false) {
3144 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003145 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3146 // Let the tracker know
3147 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3148 if (statusTracker != 0) {
3149 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3150 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003151 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003152
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003153 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003154 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003155 return true;
3156 }
3157 }
3158 // We don't set mPaused to false here, because waitForNextRequest needs
3159 // to further manage the paused state in case of starvation.
3160 return false;
3161}
3162
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003163void Camera3Device::RequestThread::unpauseForNewRequests() {
3164 // With work to do, mark thread as unpaused.
3165 // If paused by request (setPaused), don't resume, to avoid
3166 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003167 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003168 Mutex::Autolock p(mPauseLock);
3169 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003170 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3171 if (mPaused) {
3172 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3173 if (statusTracker != 0) {
3174 statusTracker->markComponentActive(mStatusId);
3175 }
3176 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003177 mPaused = false;
3178 }
3179}
3180
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003181void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3182 sp<Camera3Device> parent = mParent.promote();
3183 if (parent != NULL) {
3184 va_list args;
3185 va_start(args, fmt);
3186
3187 parent->setErrorStateV(fmt, args);
3188
3189 va_end(args);
3190 }
3191}
3192
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003193status_t Camera3Device::RequestThread::insertTriggers(
3194 const sp<CaptureRequest> &request) {
3195
3196 Mutex::Autolock al(mTriggerMutex);
3197
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003198 sp<Camera3Device> parent = mParent.promote();
3199 if (parent == NULL) {
3200 CLOGE("RequestThread: Parent is gone");
3201 return DEAD_OBJECT;
3202 }
3203
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003204 CameraMetadata &metadata = request->mSettings;
3205 size_t count = mTriggerMap.size();
3206
3207 for (size_t i = 0; i < count; ++i) {
3208 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003209 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003210
3211 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3212 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3213 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003214 if (isAeTrigger) {
3215 request->mResultExtras.precaptureTriggerId = triggerId;
3216 mCurrentPreCaptureTriggerId = triggerId;
3217 } else {
3218 request->mResultExtras.afTriggerId = triggerId;
3219 mCurrentAfTriggerId = triggerId;
3220 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003221 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3222 continue; // Trigger ID tag is deprecated since device HAL 3.2
3223 }
3224 }
3225
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003226 camera_metadata_entry entry = metadata.find(tag);
3227
3228 if (entry.count > 0) {
3229 /**
3230 * Already has an entry for this trigger in the request.
3231 * Rewrite it with our requested trigger value.
3232 */
3233 RequestTrigger oldTrigger = trigger;
3234
3235 oldTrigger.entryValue = entry.data.u8[0];
3236
3237 mTriggerReplacedMap.add(tag, oldTrigger);
3238 } else {
3239 /**
3240 * More typical, no trigger entry, so we just add it
3241 */
3242 mTriggerRemovedMap.add(tag, trigger);
3243 }
3244
3245 status_t res;
3246
3247 switch (trigger.getTagType()) {
3248 case TYPE_BYTE: {
3249 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3250 res = metadata.update(tag,
3251 &entryValue,
3252 /*count*/1);
3253 break;
3254 }
3255 case TYPE_INT32:
3256 res = metadata.update(tag,
3257 &trigger.entryValue,
3258 /*count*/1);
3259 break;
3260 default:
3261 ALOGE("%s: Type not supported: 0x%x",
3262 __FUNCTION__,
3263 trigger.getTagType());
3264 return INVALID_OPERATION;
3265 }
3266
3267 if (res != OK) {
3268 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3269 ", value %d", __FUNCTION__, trigger.getTagName(),
3270 trigger.entryValue);
3271 return res;
3272 }
3273
3274 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3275 trigger.getTagName(),
3276 trigger.entryValue);
3277 }
3278
3279 mTriggerMap.clear();
3280
3281 return count;
3282}
3283
3284status_t Camera3Device::RequestThread::removeTriggers(
3285 const sp<CaptureRequest> &request) {
3286 Mutex::Autolock al(mTriggerMutex);
3287
3288 CameraMetadata &metadata = request->mSettings;
3289
3290 /**
3291 * Replace all old entries with their old values.
3292 */
3293 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3294 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3295
3296 status_t res;
3297
3298 uint32_t tag = trigger.metadataTag;
3299 switch (trigger.getTagType()) {
3300 case TYPE_BYTE: {
3301 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3302 res = metadata.update(tag,
3303 &entryValue,
3304 /*count*/1);
3305 break;
3306 }
3307 case TYPE_INT32:
3308 res = metadata.update(tag,
3309 &trigger.entryValue,
3310 /*count*/1);
3311 break;
3312 default:
3313 ALOGE("%s: Type not supported: 0x%x",
3314 __FUNCTION__,
3315 trigger.getTagType());
3316 return INVALID_OPERATION;
3317 }
3318
3319 if (res != OK) {
3320 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3321 ", trigger value %d", __FUNCTION__,
3322 trigger.getTagName(), trigger.entryValue);
3323 return res;
3324 }
3325 }
3326 mTriggerReplacedMap.clear();
3327
3328 /**
3329 * Remove all new entries.
3330 */
3331 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3332 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3333 status_t res = metadata.erase(trigger.metadataTag);
3334
3335 if (res != OK) {
3336 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3337 ", trigger value %d", __FUNCTION__,
3338 trigger.getTagName(), trigger.entryValue);
3339 return res;
3340 }
3341 }
3342 mTriggerRemovedMap.clear();
3343
3344 return OK;
3345}
3346
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003347status_t Camera3Device::RequestThread::addDummyTriggerIds(
3348 const sp<CaptureRequest> &request) {
3349 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3350 static const int32_t dummyTriggerId = 1;
3351 status_t res;
3352
3353 CameraMetadata &metadata = request->mSettings;
3354
3355 // If AF trigger is active, insert a dummy AF trigger ID if none already
3356 // exists
3357 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3358 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3359 if (afTrigger.count > 0 &&
3360 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3361 afId.count == 0) {
3362 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3363 if (res != OK) return res;
3364 }
3365
3366 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3367 // if none already exists
3368 camera_metadata_entry pcTrigger =
3369 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3370 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3371 if (pcTrigger.count > 0 &&
3372 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3373 pcId.count == 0) {
3374 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3375 &dummyTriggerId, 1);
3376 if (res != OK) return res;
3377 }
3378
3379 return OK;
3380}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003381
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003382/**
3383 * PreparerThread inner class methods
3384 */
3385
3386Camera3Device::PreparerThread::PreparerThread() :
3387 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3388}
3389
3390Camera3Device::PreparerThread::~PreparerThread() {
3391 Thread::requestExitAndWait();
3392 if (mCurrentStream != nullptr) {
3393 mCurrentStream->cancelPrepare();
3394 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3395 mCurrentStream.clear();
3396 }
3397 clear();
3398}
3399
3400status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) {
3401 status_t res;
3402
3403 Mutex::Autolock l(mLock);
3404
3405 res = stream->startPrepare();
3406 if (res == OK) {
3407 // No preparation needed, fire listener right off
3408 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3409 if (mListener) {
3410 mListener->notifyPrepared(stream->getId());
3411 }
3412 return OK;
3413 } else if (res != NOT_ENOUGH_DATA) {
3414 return res;
3415 }
3416
3417 // Need to prepare, start up thread if necessary
3418 if (!mActive) {
3419 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3420 // isn't running
3421 Thread::requestExitAndWait();
3422 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3423 if (res != OK) {
3424 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3425 if (mListener) {
3426 mListener->notifyPrepared(stream->getId());
3427 }
3428 return res;
3429 }
3430 mCancelNow = false;
3431 mActive = true;
3432 ALOGV("%s: Preparer stream started", __FUNCTION__);
3433 }
3434
3435 // queue up the work
3436 mPendingStreams.push_back(stream);
3437 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3438
3439 return OK;
3440}
3441
3442status_t Camera3Device::PreparerThread::clear() {
3443 status_t res;
3444
3445 Mutex::Autolock l(mLock);
3446
3447 for (const auto& stream : mPendingStreams) {
3448 stream->cancelPrepare();
3449 }
3450 mPendingStreams.clear();
3451 mCancelNow = true;
3452
3453 return OK;
3454}
3455
3456void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3457 Mutex::Autolock l(mLock);
3458 mListener = listener;
3459}
3460
3461bool Camera3Device::PreparerThread::threadLoop() {
3462 status_t res;
3463 {
3464 Mutex::Autolock l(mLock);
3465 if (mCurrentStream == nullptr) {
3466 // End thread if done with work
3467 if (mPendingStreams.empty()) {
3468 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3469 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3470 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3471 mActive = false;
3472 return false;
3473 }
3474
3475 // Get next stream to prepare
3476 auto it = mPendingStreams.begin();
3477 mCurrentStream = *it;
3478 mPendingStreams.erase(it);
3479 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3480 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3481 } else if (mCancelNow) {
3482 mCurrentStream->cancelPrepare();
3483 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3484 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3485 mCurrentStream.clear();
3486 mCancelNow = false;
3487 return true;
3488 }
3489 }
3490
3491 res = mCurrentStream->prepareNextBuffer();
3492 if (res == NOT_ENOUGH_DATA) return true;
3493 if (res != OK) {
3494 // Something bad happened; try to recover by cancelling prepare and
3495 // signalling listener anyway
3496 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3497 mCurrentStream->getId(), res, strerror(-res));
3498 mCurrentStream->cancelPrepare();
3499 }
3500
3501 // This stream has finished, notify listener
3502 Mutex::Autolock l(mLock);
3503 if (mListener) {
3504 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3505 mCurrentStream->getId());
3506 mListener->notifyPrepared(mCurrentStream->getId());
3507 }
3508
3509 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3510 mCurrentStream.clear();
3511
3512 return true;
3513}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003514
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003515/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003516 * Static callback forwarding methods from HAL to instance
3517 */
3518
3519void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3520 const camera3_capture_result *result) {
3521 Camera3Device *d =
3522 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003523
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003524 d->processCaptureResult(result);
3525}
3526
3527void Camera3Device::sNotify(const camera3_callback_ops *cb,
3528 const camera3_notify_msg *msg) {
3529 Camera3Device *d =
3530 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3531 d->notify(msg);
3532}
3533
3534}; // namespace android