blob: cf47bc85efedb441b69101d091c3ca463afc0e9d [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) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700373 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700374 __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 Talvala95a1d0f2015-08-11 15:08:53 -0700400ssize_t Camera3Device::getPointCloudBufferSize() const {
401 const int FLOATS_PER_POINT=4;
402 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
403 if (maxPointCount.count == 0) {
404 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
405 __FUNCTION__, mId);
406 return BAD_VALUE;
407 }
408 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
409 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
410 return maxBytesForPointCloud;
411}
412
413
414
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800415status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
416 ATRACE_CALL();
417 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700418
419 // Try to lock, but continue in case of failure (to avoid blocking in
420 // deadlocks)
421 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
422 bool gotLock = tryLockSpinRightRound(mLock);
423
424 ALOGW_IF(!gotInterfaceLock,
425 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
426 mId, __FUNCTION__);
427 ALOGW_IF(!gotLock,
428 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
429 mId, __FUNCTION__);
430
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800431 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800432
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800433 const char *status =
434 mStatus == STATUS_ERROR ? "ERROR" :
435 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700436 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
437 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800438 mStatus == STATUS_ACTIVE ? "ACTIVE" :
439 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700440
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800441 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700442 if (mStatus == STATUS_ERROR) {
443 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
444 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800445 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700446 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700447 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800448
449 if (mInputStream != NULL) {
450 write(fd, lines.string(), lines.size());
451 mInputStream->dump(fd, args);
452 } else {
453 lines.appendFormat(" No input stream.\n");
454 write(fd, lines.string(), lines.size());
455 }
456 for (size_t i = 0; i < mOutputStreams.size(); i++) {
457 mOutputStreams[i]->dump(fd,args);
458 }
459
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700460 lines = String8(" In-flight requests:\n");
461 if (mInFlightMap.size() == 0) {
462 lines.append(" None\n");
463 } else {
464 for (size_t i = 0; i < mInFlightMap.size(); i++) {
465 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700466 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700467 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800468 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700469 r.numBuffersLeft);
470 }
471 }
472 write(fd, lines.string(), lines.size());
473
Igor Murashkin1e479c02013-09-06 16:55:14 -0700474 {
475 lines = String8(" Last request sent:\n");
476 write(fd, lines.string(), lines.size());
477
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700478 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700479 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
480 }
481
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800482 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700483 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800484 write(fd, lines.string(), lines.size());
485 mHal3Device->ops->dump(mHal3Device, fd);
486 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800487
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700488 if (gotLock) mLock.unlock();
489 if (gotInterfaceLock) mInterfaceLock.unlock();
490
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800491 return OK;
492}
493
494const CameraMetadata& Camera3Device::info() const {
495 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800496 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
497 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700498 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800499 mStatus == STATUS_ERROR ?
500 "when in error state" : "before init");
501 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800502 return mDeviceInfo;
503}
504
Jianing Wei90e59c92014-03-12 18:29:36 -0700505status_t Camera3Device::checkStatusOkToCaptureLocked() {
506 switch (mStatus) {
507 case STATUS_ERROR:
508 CLOGE("Device has encountered a serious error");
509 return INVALID_OPERATION;
510 case STATUS_UNINITIALIZED:
511 CLOGE("Device not initialized");
512 return INVALID_OPERATION;
513 case STATUS_UNCONFIGURED:
514 case STATUS_CONFIGURED:
515 case STATUS_ACTIVE:
516 // OK
517 break;
518 default:
519 SET_ERR_L("Unexpected status: %d", mStatus);
520 return INVALID_OPERATION;
521 }
522 return OK;
523}
524
525status_t Camera3Device::convertMetadataListToRequestListLocked(
526 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
527 if (requestList == NULL) {
528 CLOGE("requestList cannot be NULL.");
529 return BAD_VALUE;
530 }
531
Jianing Weicb0652e2014-03-12 18:29:36 -0700532 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700533 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
534 it != metadataList.end(); ++it) {
535 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
536 if (newRequest == 0) {
537 CLOGE("Can't create capture request");
538 return BAD_VALUE;
539 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700540
541 // Setup burst Id and request Id
542 newRequest->mResultExtras.burstId = burstId++;
543 if (it->exists(ANDROID_REQUEST_ID)) {
544 if (it->find(ANDROID_REQUEST_ID).count == 0) {
545 CLOGE("RequestID entry exists; but must not be empty in metadata");
546 return BAD_VALUE;
547 }
548 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
549 } else {
550 CLOGE("RequestID does not exist in metadata");
551 return BAD_VALUE;
552 }
553
Jianing Wei90e59c92014-03-12 18:29:36 -0700554 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700555
556 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700557 }
558 return OK;
559}
560
Jianing Weicb0652e2014-03-12 18:29:36 -0700561status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800562 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800563
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700564 List<const CameraMetadata> requests;
565 requests.push_back(request);
566 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800567}
568
Jianing Wei90e59c92014-03-12 18:29:36 -0700569status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700570 const List<const CameraMetadata> &requests, bool repeating,
571 /*out*/
572 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700573 ATRACE_CALL();
574 Mutex::Autolock il(mInterfaceLock);
575 Mutex::Autolock l(mLock);
576
577 status_t res = checkStatusOkToCaptureLocked();
578 if (res != OK) {
579 // error logged by previous call
580 return res;
581 }
582
583 RequestList requestList;
584
585 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
586 if (res != OK) {
587 // error logged by previous call
588 return res;
589 }
590
591 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700592 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700593 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700594 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700595 }
596
597 if (res == OK) {
598 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
599 if (res != OK) {
600 SET_ERR_L("Can't transition to active in %f seconds!",
601 kActiveTimeout/1e9);
602 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700603 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
604 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700605 } else {
606 CLOGE("Cannot queue request. Impossible.");
607 return BAD_VALUE;
608 }
609
610 return res;
611}
612
Jianing Weicb0652e2014-03-12 18:29:36 -0700613status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
614 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700615 ATRACE_CALL();
616
Jianing Weicb0652e2014-03-12 18:29:36 -0700617 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700618}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800619
Jianing Weicb0652e2014-03-12 18:29:36 -0700620status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
621 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800622 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800623
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700624 List<const CameraMetadata> requests;
625 requests.push_back(request);
626 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800627}
628
Jianing Weicb0652e2014-03-12 18:29:36 -0700629status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
630 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700631 ATRACE_CALL();
632
Jianing Weicb0652e2014-03-12 18:29:36 -0700633 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700634}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800635
636sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
637 const CameraMetadata &request) {
638 status_t res;
639
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700640 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800641 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700642 // Stream configuration failed due to unsupported configuration.
643 // Device back to unconfigured state. Client might try other configuraitons
644 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
645 CLOGE("No streams configured");
646 return NULL;
647 }
648 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800649 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700650 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800651 return NULL;
652 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700653 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700654 if (mStatus == STATUS_UNCONFIGURED) {
655 CLOGE("No streams configured");
656 return NULL;
657 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800658 }
659
660 sp<CaptureRequest> newRequest = createCaptureRequest(request);
661 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800662}
663
Jianing Weicb0652e2014-03-12 18:29:36 -0700664status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800665 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700666 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800667 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800668
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800669 switch (mStatus) {
670 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700671 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800672 return INVALID_OPERATION;
673 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700674 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800675 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700676 case STATUS_UNCONFIGURED:
677 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800678 case STATUS_ACTIVE:
679 // OK
680 break;
681 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700682 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800683 return INVALID_OPERATION;
684 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700685 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700686
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700687 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800688}
689
690status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
691 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700692 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800693
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700694 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800695}
696
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700697status_t Camera3Device::createInputStream(
698 uint32_t width, uint32_t height, int format, int *id) {
699 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700700 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700701 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700702 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
703 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700704
705 status_t res;
706 bool wasActive = false;
707
708 switch (mStatus) {
709 case STATUS_ERROR:
710 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
711 return INVALID_OPERATION;
712 case STATUS_UNINITIALIZED:
713 ALOGE("%s: Device not initialized", __FUNCTION__);
714 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700715 case STATUS_UNCONFIGURED:
716 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700717 // OK
718 break;
719 case STATUS_ACTIVE:
720 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700721 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700722 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700723 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700724 return res;
725 }
726 wasActive = true;
727 break;
728 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700729 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700730 return INVALID_OPERATION;
731 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700732 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700733
734 if (mInputStream != 0) {
735 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
736 return INVALID_OPERATION;
737 }
738
739 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
740 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700741 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700742
743 mInputStream = newStream;
744
745 *id = mNextStreamId++;
746
747 // Continue captures if active at start
748 if (wasActive) {
749 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
750 res = configureStreamsLocked();
751 if (res != OK) {
752 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
753 __FUNCTION__, mNextStreamId, strerror(-res), res);
754 return res;
755 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700756 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700757 }
758
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700759 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700760 return OK;
761}
762
Igor Murashkin2fba5842013-04-22 14:03:54 -0700763
764status_t Camera3Device::createZslStream(
765 uint32_t width, uint32_t height,
766 int depth,
767 /*out*/
768 int *id,
769 sp<Camera3ZslStream>* zslStream) {
770 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700771 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700772 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700773 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
774 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700775
776 status_t res;
777 bool wasActive = false;
778
779 switch (mStatus) {
780 case STATUS_ERROR:
781 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
782 return INVALID_OPERATION;
783 case STATUS_UNINITIALIZED:
784 ALOGE("%s: Device not initialized", __FUNCTION__);
785 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700786 case STATUS_UNCONFIGURED:
787 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700788 // OK
789 break;
790 case STATUS_ACTIVE:
791 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700792 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700793 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700794 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700795 return res;
796 }
797 wasActive = true;
798 break;
799 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700800 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700801 return INVALID_OPERATION;
802 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700803 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700804
805 if (mInputStream != 0) {
806 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
807 return INVALID_OPERATION;
808 }
809
810 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
811 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700812 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700813
814 res = mOutputStreams.add(mNextStreamId, newStream);
815 if (res < 0) {
816 ALOGE("%s: Can't add new stream to set: %s (%d)",
817 __FUNCTION__, strerror(-res), res);
818 return res;
819 }
820 mInputStream = newStream;
821
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530822 mNeedConfig = true;
823
Igor Murashkin2fba5842013-04-22 14:03:54 -0700824 *id = mNextStreamId++;
825 *zslStream = newStream;
826
827 // Continue captures if active at start
828 if (wasActive) {
829 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
830 res = configureStreamsLocked();
831 if (res != OK) {
832 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
833 __FUNCTION__, mNextStreamId, strerror(-res), res);
834 return res;
835 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700836 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700837 }
838
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700839 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700840 return OK;
841}
842
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700843status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800844 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700845 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800846 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700847 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800848 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700849 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
850 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800851
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800852 status_t res;
853 bool wasActive = false;
854
855 switch (mStatus) {
856 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700857 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800858 return INVALID_OPERATION;
859 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700860 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800861 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700862 case STATUS_UNCONFIGURED:
863 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800864 // OK
865 break;
866 case STATUS_ACTIVE:
867 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700868 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800869 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700870 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800871 return res;
872 }
873 wasActive = true;
874 break;
875 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700876 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800877 return INVALID_OPERATION;
878 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700879 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800880
881 sp<Camera3OutputStream> newStream;
882 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700883 ssize_t blobBufferSize;
884 if (dataSpace != HAL_DATASPACE_DEPTH) {
885 blobBufferSize = getJpegBufferSize(width, height);
886 if (blobBufferSize <= 0) {
887 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
888 return BAD_VALUE;
889 }
890 } else {
891 blobBufferSize = getPointCloudBufferSize();
892 if (blobBufferSize <= 0) {
893 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
894 return BAD_VALUE;
895 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700896 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800897 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700898 width, height, blobBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800899 } else {
900 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700901 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800902 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700903 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800904
905 res = mOutputStreams.add(mNextStreamId, newStream);
906 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700907 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800908 return res;
909 }
910
911 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700912 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800913
914 // Continue captures if active at start
915 if (wasActive) {
916 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
917 res = configureStreamsLocked();
918 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700919 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
920 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800921 return res;
922 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700923 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800924 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700925 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800926 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800927}
928
929status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
930 ATRACE_CALL();
931 (void)outputId; (void)id;
932
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700933 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800934 return INVALID_OPERATION;
935}
936
937
938status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700939 uint32_t *width, uint32_t *height,
940 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800941 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700942 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800943 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800944
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800945 switch (mStatus) {
946 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700947 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800948 return INVALID_OPERATION;
949 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700950 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800951 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700952 case STATUS_UNCONFIGURED:
953 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800954 case STATUS_ACTIVE:
955 // OK
956 break;
957 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700958 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800959 return INVALID_OPERATION;
960 }
961
962 ssize_t idx = mOutputStreams.indexOfKey(id);
963 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700964 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800965 return idx;
966 }
967
968 if (width) *width = mOutputStreams[idx]->getWidth();
969 if (height) *height = mOutputStreams[idx]->getHeight();
970 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700971 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800972 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800973}
974
975status_t Camera3Device::setStreamTransform(int id,
976 int transform) {
977 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700978 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800979 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800980
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800981 switch (mStatus) {
982 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700983 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800984 return INVALID_OPERATION;
985 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700986 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800987 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700988 case STATUS_UNCONFIGURED:
989 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800990 case STATUS_ACTIVE:
991 // OK
992 break;
993 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700994 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800995 return INVALID_OPERATION;
996 }
997
998 ssize_t idx = mOutputStreams.indexOfKey(id);
999 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001000 CLOGE("Stream %d does not exist",
1001 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001002 return BAD_VALUE;
1003 }
1004
1005 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001006}
1007
1008status_t Camera3Device::deleteStream(int id) {
1009 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001010 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001011 Mutex::Autolock l(mLock);
1012 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001013
Igor Murashkine2172be2013-05-28 15:31:39 -07001014 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1015
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001016 // CameraDevice semantics require device to already be idle before
1017 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001018 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001019 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1020 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001021 }
1022
Igor Murashkin2fba5842013-04-22 14:03:54 -07001023 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001024 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001025 if (mInputStream != NULL && id == mInputStream->getId()) {
1026 deletedStream = mInputStream;
1027 mInputStream.clear();
1028 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001029 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001030 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001031 return BAD_VALUE;
1032 }
Zhijun He5f446352014-01-22 09:49:33 -08001033 }
1034
1035 // Delete output stream or the output part of a bi-directional stream.
1036 if (outputStreamIdx != NAME_NOT_FOUND) {
1037 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001038 mOutputStreams.removeItem(id);
1039 }
1040
1041 // Free up the stream endpoint so that it can be used by some other stream
1042 res = deletedStream->disconnect();
1043 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001044 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001045 // fall through since we want to still list the stream as deleted.
1046 }
1047 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001048 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001049
1050 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001051}
1052
1053status_t Camera3Device::deleteReprocessStream(int id) {
1054 ATRACE_CALL();
1055 (void)id;
1056
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001057 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001058 return INVALID_OPERATION;
1059}
1060
Zhijun He1fa89992015-06-01 15:44:31 -07001061status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001062 ATRACE_CALL();
1063 ALOGV("%s: E", __FUNCTION__);
1064
1065 Mutex::Autolock il(mInterfaceLock);
1066 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001067
1068 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1069 mNeedConfig = true;
1070 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1071 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001072
1073 return configureStreamsLocked();
1074}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001075
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001076status_t Camera3Device::getInputBufferProducer(
1077 sp<IGraphicBufferProducer> *producer) {
1078 Mutex::Autolock il(mInterfaceLock);
1079 Mutex::Autolock l(mLock);
1080
1081 if (producer == NULL) {
1082 return BAD_VALUE;
1083 } else if (mInputStream == NULL) {
1084 return INVALID_OPERATION;
1085 }
1086
1087 return mInputStream->getInputBufferProducer(producer);
1088}
1089
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001090status_t Camera3Device::createDefaultRequest(int templateId,
1091 CameraMetadata *request) {
1092 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001093 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001094 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001095 Mutex::Autolock l(mLock);
1096
1097 switch (mStatus) {
1098 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001099 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001100 return INVALID_OPERATION;
1101 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001102 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001103 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001104 case STATUS_UNCONFIGURED:
1105 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001106 case STATUS_ACTIVE:
1107 // OK
1108 break;
1109 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001110 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001111 return INVALID_OPERATION;
1112 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001113
Zhijun Hea1530f12014-09-14 12:44:20 -07001114 if (!mRequestTemplateCache[templateId].isEmpty()) {
1115 *request = mRequestTemplateCache[templateId];
1116 return OK;
1117 }
1118
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001119 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001120 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001121 rawRequest = mHal3Device->ops->construct_default_request_settings(
1122 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001123 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001124 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001125 ALOGI("%s: template %d is not supported on this camera device",
1126 __FUNCTION__, templateId);
1127 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001128 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001129 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001130 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001131
1132 return OK;
1133}
1134
1135status_t Camera3Device::waitUntilDrained() {
1136 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001137 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001138 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001139
Zhijun He69a37482014-03-23 18:44:49 -07001140 return waitUntilDrainedLocked();
1141}
1142
1143status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001144 switch (mStatus) {
1145 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001146 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001147 ALOGV("%s: Already idle", __FUNCTION__);
1148 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001149 case STATUS_CONFIGURED:
1150 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001151 case STATUS_ERROR:
1152 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001153 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001154 break;
1155 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001156 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001157 return INVALID_OPERATION;
1158 }
1159
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001160 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1161 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001162 if (res != OK) {
1163 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1164 res);
1165 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001166 return res;
1167}
1168
1169// Pause to reconfigure
1170status_t Camera3Device::internalPauseAndWaitLocked() {
1171 mRequestThread->setPaused(true);
1172 mPauseStateNotify = true;
1173
1174 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1175 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1176 if (res != OK) {
1177 SET_ERR_L("Can't idle device in %f seconds!",
1178 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001179 }
1180
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001181 return res;
1182}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001183
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001184// Resume after internalPauseAndWaitLocked
1185status_t Camera3Device::internalResumeLocked() {
1186 status_t res;
1187
1188 mRequestThread->setPaused(false);
1189
1190 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1191 if (res != OK) {
1192 SET_ERR_L("Can't transition to active in %f seconds!",
1193 kActiveTimeout/1e9);
1194 }
1195 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001196 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001197}
1198
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001199status_t Camera3Device::waitUntilStateThenRelock(bool active,
1200 nsecs_t timeout) {
1201 status_t res = OK;
1202 if (active == (mStatus == STATUS_ACTIVE)) {
1203 // Desired state already reached
1204 return res;
1205 }
1206
1207 bool stateSeen = false;
1208 do {
1209 mRecentStatusUpdates.clear();
1210
1211 res = mStatusChanged.waitRelative(mLock, timeout);
1212 if (res != OK) break;
1213
1214 // Check state change history during wait
1215 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1216 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1217 stateSeen = true;
1218 break;
1219 }
1220 }
1221 } while (!stateSeen);
1222
1223 return res;
1224}
1225
1226
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001227status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1228 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001229 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001230
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001231 if (listener != NULL && mListener != NULL) {
1232 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1233 }
1234 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001235 mRequestThread->setNotificationListener(listener);
1236 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001237
1238 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001239}
1240
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001241bool Camera3Device::willNotify3A() {
1242 return false;
1243}
1244
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001245status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001246 status_t res;
1247 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001248
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001249 while (mResultQueue.empty()) {
1250 res = mResultSignal.waitRelative(mOutputLock, timeout);
1251 if (res == TIMED_OUT) {
1252 return res;
1253 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001254 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001255 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001256 return res;
1257 }
1258 }
1259 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001260}
1261
Jianing Weicb0652e2014-03-12 18:29:36 -07001262status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001263 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001264 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001265
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001266 if (mResultQueue.empty()) {
1267 return NOT_ENOUGH_DATA;
1268 }
1269
Jianing Weicb0652e2014-03-12 18:29:36 -07001270 if (frame == NULL) {
1271 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1272 return BAD_VALUE;
1273 }
1274
1275 CaptureResult &result = *(mResultQueue.begin());
1276 frame->mResultExtras = result.mResultExtras;
1277 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001278 mResultQueue.erase(mResultQueue.begin());
1279
1280 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001281}
1282
1283status_t Camera3Device::triggerAutofocus(uint32_t id) {
1284 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001285 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001286
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001287 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1288 // Mix-in this trigger into the next request and only the next request.
1289 RequestTrigger trigger[] = {
1290 {
1291 ANDROID_CONTROL_AF_TRIGGER,
1292 ANDROID_CONTROL_AF_TRIGGER_START
1293 },
1294 {
1295 ANDROID_CONTROL_AF_TRIGGER_ID,
1296 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001297 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001298 };
1299
1300 return mRequestThread->queueTrigger(trigger,
1301 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001302}
1303
1304status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1305 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001306 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001307
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001308 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1309 // Mix-in this trigger into the next request and only the next request.
1310 RequestTrigger trigger[] = {
1311 {
1312 ANDROID_CONTROL_AF_TRIGGER,
1313 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1314 },
1315 {
1316 ANDROID_CONTROL_AF_TRIGGER_ID,
1317 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001318 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001319 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001320
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001321 return mRequestThread->queueTrigger(trigger,
1322 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001323}
1324
1325status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1326 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001327 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001328
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001329 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1330 // Mix-in this trigger into the next request and only the next request.
1331 RequestTrigger trigger[] = {
1332 {
1333 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1334 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1335 },
1336 {
1337 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1338 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001339 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001340 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001341
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001342 return mRequestThread->queueTrigger(trigger,
1343 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001344}
1345
1346status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1347 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1348 ATRACE_CALL();
1349 (void)reprocessStreamId; (void)buffer; (void)listener;
1350
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001351 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001352 return INVALID_OPERATION;
1353}
1354
Jianing Weicb0652e2014-03-12 18:29:36 -07001355status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001356 ATRACE_CALL();
1357 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001358 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001359
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001360 NotificationListener* listener;
1361 {
1362 Mutex::Autolock l(mOutputLock);
1363 listener = mListener;
1364 }
1365
Zhijun He7ef20392014-04-21 16:04:17 -07001366 {
1367 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001368 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001369 }
1370
Zhijun He491e3412013-12-27 10:57:44 -08001371 status_t res;
1372 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1373 res = mHal3Device->ops->flush(mHal3Device);
1374 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001375 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001376 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001377 }
1378
1379 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001380}
1381
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001382status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001383 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1384}
1385
1386status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001387 ATRACE_CALL();
1388 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001389 Mutex::Autolock il(mInterfaceLock);
1390 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001391
1392 sp<Camera3StreamInterface> stream;
1393 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1394 if (outputStreamIdx == NAME_NOT_FOUND) {
1395 CLOGE("Stream %d does not exist", streamId);
1396 return BAD_VALUE;
1397 }
1398
1399 stream = mOutputStreams.editValueAt(outputStreamIdx);
1400
1401 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001402 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001403 return BAD_VALUE;
1404 }
1405
1406 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001407 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001408 return BAD_VALUE;
1409 }
1410
Ruben Brunkc78ac262015-08-13 17:58:46 -07001411 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001412}
1413
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001414status_t Camera3Device::tearDown(int streamId) {
1415 ATRACE_CALL();
1416 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1417 Mutex::Autolock il(mInterfaceLock);
1418 Mutex::Autolock l(mLock);
1419
1420 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1421 // since we cannot call register_stream_buffers except right after configure_streams.
1422 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1423 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1424 __FUNCTION__, mHal3Device->common.version);
1425 return NO_INIT;
1426 }
1427
1428 sp<Camera3StreamInterface> stream;
1429 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1430 if (outputStreamIdx == NAME_NOT_FOUND) {
1431 CLOGE("Stream %d does not exist", streamId);
1432 return BAD_VALUE;
1433 }
1434
1435 stream = mOutputStreams.editValueAt(outputStreamIdx);
1436
1437 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1438 CLOGE("Stream %d is a target of a in-progress request", streamId);
1439 return BAD_VALUE;
1440 }
1441
1442 return stream->tearDown();
1443}
1444
Zhijun He204e3292014-07-14 17:09:23 -07001445uint32_t Camera3Device::getDeviceVersion() {
1446 ATRACE_CALL();
1447 Mutex::Autolock il(mInterfaceLock);
1448 return mDeviceVersion;
1449}
1450
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001451/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001452 * Methods called by subclasses
1453 */
1454
1455void Camera3Device::notifyStatus(bool idle) {
1456 {
1457 // Need mLock to safely update state and synchronize to current
1458 // state of methods in flight.
1459 Mutex::Autolock l(mLock);
1460 // We can get various system-idle notices from the status tracker
1461 // while starting up. Only care about them if we've actually sent
1462 // in some requests recently.
1463 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1464 return;
1465 }
1466 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1467 idle ? "idle" : "active");
1468 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1469 mRecentStatusUpdates.add(mStatus);
1470 mStatusChanged.signal();
1471
1472 // Skip notifying listener if we're doing some user-transparent
1473 // state changes
1474 if (mPauseStateNotify) return;
1475 }
1476 NotificationListener *listener;
1477 {
1478 Mutex::Autolock l(mOutputLock);
1479 listener = mListener;
1480 }
1481 if (idle && listener != NULL) {
1482 listener->notifyIdle();
1483 }
1484}
1485
1486/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001487 * Camera3Device private methods
1488 */
1489
1490sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1491 const CameraMetadata &request) {
1492 ATRACE_CALL();
1493 status_t res;
1494
1495 sp<CaptureRequest> newRequest = new CaptureRequest;
1496 newRequest->mSettings = request;
1497
1498 camera_metadata_entry_t inputStreams =
1499 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1500 if (inputStreams.count > 0) {
1501 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001502 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001503 CLOGE("Request references unknown input stream %d",
1504 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001505 return NULL;
1506 }
1507 // Lazy completion of stream configuration (allocation/registration)
1508 // on first use
1509 if (mInputStream->isConfiguring()) {
1510 res = mInputStream->finishConfiguration(mHal3Device);
1511 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001512 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001513 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001514 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001515 return NULL;
1516 }
1517 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001518 // Check if stream is being prepared
1519 if (mInputStream->isPreparing()) {
1520 CLOGE("Request references an input stream that's being prepared!");
1521 return NULL;
1522 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001523
1524 newRequest->mInputStream = mInputStream;
1525 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1526 }
1527
1528 camera_metadata_entry_t streams =
1529 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1530 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001531 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001532 return NULL;
1533 }
1534
1535 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001536 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001537 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001538 CLOGE("Request references unknown stream %d",
1539 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001540 return NULL;
1541 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001542 sp<Camera3OutputStreamInterface> stream =
1543 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001544
1545 // Lazy completion of stream configuration (allocation/registration)
1546 // on first use
1547 if (stream->isConfiguring()) {
1548 res = stream->finishConfiguration(mHal3Device);
1549 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001550 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1551 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001552 return NULL;
1553 }
1554 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001555 // Check if stream is being prepared
1556 if (stream->isPreparing()) {
1557 CLOGE("Request references an output stream that's being prepared!");
1558 return NULL;
1559 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001560
1561 newRequest->mOutputStreams.push(stream);
1562 }
1563 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1564
1565 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001566}
1567
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001568bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1569 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1570 Size size = mSupportedOpaqueInputSizes[i];
1571 if (size.width == width && size.height == height) {
1572 return true;
1573 }
1574 }
1575
1576 return false;
1577}
1578
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001579status_t Camera3Device::configureStreamsLocked() {
1580 ATRACE_CALL();
1581 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001582
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001583 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001584 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001585 return INVALID_OPERATION;
1586 }
1587
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001588 if (!mNeedConfig) {
1589 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1590 return OK;
1591 }
1592
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001593 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1594 // adding a dummy stream instead.
1595 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1596 if (mOutputStreams.size() == 0) {
1597 addDummyStreamLocked();
1598 } else {
1599 tryRemoveDummyStreamLocked();
1600 }
1601
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001602 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001603 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001604
1605 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001606 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1607 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1608 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001609 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1610
1611 Vector<camera3_stream_t*> streams;
1612 streams.setCapacity(config.num_streams);
1613
1614 if (mInputStream != NULL) {
1615 camera3_stream_t *inputStream;
1616 inputStream = mInputStream->startConfiguration();
1617 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001618 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001619 return INVALID_OPERATION;
1620 }
1621 streams.add(inputStream);
1622 }
1623
1624 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001625
1626 // Don't configure bidi streams twice, nor add them twice to the list
1627 if (mOutputStreams[i].get() ==
1628 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1629
1630 config.num_streams--;
1631 continue;
1632 }
1633
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001634 camera3_stream_t *outputStream;
1635 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1636 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001637 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001638 return INVALID_OPERATION;
1639 }
1640 streams.add(outputStream);
1641 }
1642
1643 config.streams = streams.editArray();
1644
1645 // Do the HAL configuration; will potentially touch stream
1646 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001647 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001648 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001649 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001650
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001651 if (res == BAD_VALUE) {
1652 // HAL rejected this set of streams as unsupported, clean up config
1653 // attempt and return to unconfigured state
1654 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1655 res = mInputStream->cancelConfiguration();
1656 if (res != OK) {
1657 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1658 mInputStream->getId(), strerror(-res), res);
1659 return res;
1660 }
1661 }
1662
1663 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1664 sp<Camera3OutputStreamInterface> outputStream =
1665 mOutputStreams.editValueAt(i);
1666 if (outputStream->isConfiguring()) {
1667 res = outputStream->cancelConfiguration();
1668 if (res != OK) {
1669 SET_ERR_L(
1670 "Can't cancel configuring output stream %d: %s (%d)",
1671 outputStream->getId(), strerror(-res), res);
1672 return res;
1673 }
1674 }
1675 }
1676
1677 // Return state to that at start of call, so that future configures
1678 // properly clean things up
1679 mStatus = STATUS_UNCONFIGURED;
1680 mNeedConfig = true;
1681
1682 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1683 return BAD_VALUE;
1684 } else if (res != OK) {
1685 // Some other kind of error from configure_streams - this is not
1686 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001687 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1688 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001689 return res;
1690 }
1691
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001692 // Finish all stream configuration immediately.
1693 // TODO: Try to relax this later back to lazy completion, which should be
1694 // faster
1695
Igor Murashkin073f8572013-05-02 14:59:28 -07001696 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001697 res = mInputStream->finishConfiguration(mHal3Device);
1698 if (res != OK) {
1699 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1700 mInputStream->getId(), strerror(-res), res);
1701 return res;
1702 }
1703 }
1704
1705 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001706 sp<Camera3OutputStreamInterface> outputStream =
1707 mOutputStreams.editValueAt(i);
1708 if (outputStream->isConfiguring()) {
1709 res = outputStream->finishConfiguration(mHal3Device);
1710 if (res != OK) {
1711 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1712 outputStream->getId(), strerror(-res), res);
1713 return res;
1714 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001715 }
1716 }
1717
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001718 // Request thread needs to know to avoid using repeat-last-settings protocol
1719 // across configure_streams() calls
1720 mRequestThread->configurationComplete();
1721
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001722 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001723
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001724 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001725
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001726 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001727 mStatus = STATUS_CONFIGURED;
1728 } else {
1729 mStatus = STATUS_UNCONFIGURED;
1730 }
1731
1732 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1733
Zhijun He0a210512014-07-24 13:45:15 -07001734 // tear down the deleted streams after configure streams.
1735 mDeletedStreams.clear();
1736
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001737 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001738}
1739
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001740status_t Camera3Device::addDummyStreamLocked() {
1741 ATRACE_CALL();
1742 status_t res;
1743
1744 if (mDummyStreamId != NO_STREAM) {
1745 // Should never be adding a second dummy stream when one is already
1746 // active
1747 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1748 __FUNCTION__, mId);
1749 return INVALID_OPERATION;
1750 }
1751
1752 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1753
1754 sp<Camera3OutputStreamInterface> dummyStream =
1755 new Camera3DummyStream(mNextStreamId);
1756
1757 res = mOutputStreams.add(mNextStreamId, dummyStream);
1758 if (res < 0) {
1759 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1760 return res;
1761 }
1762
1763 mDummyStreamId = mNextStreamId;
1764 mNextStreamId++;
1765
1766 return OK;
1767}
1768
1769status_t Camera3Device::tryRemoveDummyStreamLocked() {
1770 ATRACE_CALL();
1771 status_t res;
1772
1773 if (mDummyStreamId == NO_STREAM) return OK;
1774 if (mOutputStreams.size() == 1) return OK;
1775
1776 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1777
1778 // Ok, have a dummy stream and there's at least one other output stream,
1779 // so remove the dummy
1780
1781 sp<Camera3StreamInterface> deletedStream;
1782 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1783 if (outputStreamIdx == NAME_NOT_FOUND) {
1784 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1785 return INVALID_OPERATION;
1786 }
1787
1788 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1789 mOutputStreams.removeItemsAt(outputStreamIdx);
1790
1791 // Free up the stream endpoint so that it can be used by some other stream
1792 res = deletedStream->disconnect();
1793 if (res != OK) {
1794 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1795 // fall through since we want to still list the stream as deleted.
1796 }
1797 mDeletedStreams.add(deletedStream);
1798 mDummyStreamId = NO_STREAM;
1799
1800 return res;
1801}
1802
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001803void Camera3Device::setErrorState(const char *fmt, ...) {
1804 Mutex::Autolock l(mLock);
1805 va_list args;
1806 va_start(args, fmt);
1807
1808 setErrorStateLockedV(fmt, args);
1809
1810 va_end(args);
1811}
1812
1813void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1814 Mutex::Autolock l(mLock);
1815 setErrorStateLockedV(fmt, args);
1816}
1817
1818void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1819 va_list args;
1820 va_start(args, fmt);
1821
1822 setErrorStateLockedV(fmt, args);
1823
1824 va_end(args);
1825}
1826
1827void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001828 // Print out all error messages to log
1829 String8 errorCause = String8::formatV(fmt, args);
1830 ALOGE("Camera %d: %s", mId, errorCause.string());
1831
1832 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001833 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001834
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001835 mErrorCause = errorCause;
1836
1837 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001838 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001839
1840 // Notify upstream about a device error
1841 if (mListener != NULL) {
1842 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1843 CaptureResultExtras());
1844 }
1845
1846 // Save stack trace. View by dumping it later.
1847 CameraTraces::saveTrace();
1848 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001849}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001850
1851/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001852 * In-flight request management
1853 */
1854
Jianing Weicb0652e2014-03-12 18:29:36 -07001855status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07001856 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1857 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001858 ATRACE_CALL();
1859 Mutex::Autolock l(mInFlightLock);
1860
1861 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07001862 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
1863 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001864 if (res < 0) return res;
1865
1866 return OK;
1867}
1868
1869/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001870 * Check if all 3A fields are ready, and send off a partial 3A-only result
1871 * to the output frame queue
1872 */
Zhijun He204e3292014-07-14 17:09:23 -07001873bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001874 uint32_t frameNumber,
1875 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001876
1877 // Check if all 3A states are present
1878 // The full list of fields is
1879 // android.control.afMode
1880 // android.control.awbMode
1881 // android.control.aeState
1882 // android.control.awbState
1883 // android.control.afState
1884 // android.control.afTriggerID
1885 // android.control.aePrecaptureID
1886 // TODO: Add android.control.aeMode
1887
1888 bool gotAllStates = true;
1889
1890 uint8_t afMode;
1891 uint8_t awbMode;
1892 uint8_t aeState;
1893 uint8_t afState;
1894 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001895
1896 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1897 &afMode, frameNumber);
1898
1899 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1900 &awbMode, frameNumber);
1901
1902 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1903 &aeState, frameNumber);
1904
1905 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1906 &afState, frameNumber);
1907
1908 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1909 &awbState, frameNumber);
1910
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001911 if (!gotAllStates) return false;
1912
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001913 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001914 "AF state %d, AE state %d, AWB state %d, "
1915 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001916 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001917 afMode, awbMode,
1918 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001919 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001920
1921 // Got all states, so construct a minimal result to send
1922 // In addition to the above fields, this means adding in
1923 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001924 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001925 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001926
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001927 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001928
1929 Mutex::Autolock l(mOutputLock);
1930
Jianing Weicb0652e2014-03-12 18:29:36 -07001931 CaptureResult captureResult;
1932 captureResult.mResultExtras = resultExtras;
1933 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1934 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1935 // but not limited to CameraDeviceBase::getNextResult
1936 CaptureResult& min3AResult =
1937 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001938
Jianing Weicb0652e2014-03-12 18:29:36 -07001939 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1940 // TODO: This is problematic casting. Need to fix CameraMetadata.
1941 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001942 return false;
1943 }
1944
Jianing Weicb0652e2014-03-12 18:29:36 -07001945 int32_t requestId = resultExtras.requestId;
1946 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001947 &requestId, frameNumber)) {
1948 return false;
1949 }
1950
Zhijun He204e3292014-07-14 17:09:23 -07001951 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1952 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1953 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1954 &partialResult, frameNumber)) {
1955 return false;
1956 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001957 }
1958
Jianing Weicb0652e2014-03-12 18:29:36 -07001959 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001960 &afMode, frameNumber)) {
1961 return false;
1962 }
1963
Jianing Weicb0652e2014-03-12 18:29:36 -07001964 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001965 &awbMode, frameNumber)) {
1966 return false;
1967 }
1968
Jianing Weicb0652e2014-03-12 18:29:36 -07001969 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001970 &aeState, frameNumber)) {
1971 return false;
1972 }
1973
Jianing Weicb0652e2014-03-12 18:29:36 -07001974 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001975 &afState, frameNumber)) {
1976 return false;
1977 }
1978
Jianing Weicb0652e2014-03-12 18:29:36 -07001979 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001980 &awbState, frameNumber)) {
1981 return false;
1982 }
1983
Jianing Weicb0652e2014-03-12 18:29:36 -07001984 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001985 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001986 return false;
1987 }
1988
Jianing Weicb0652e2014-03-12 18:29:36 -07001989 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001990 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001991 return false;
1992 }
1993
Zhijun He204e3292014-07-14 17:09:23 -07001994 // We only send the aggregated partial when all 3A related metadata are available
1995 // For both API1 and API2.
1996 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001997 mResultSignal.signal();
1998
1999 return true;
2000}
2001
2002template<typename T>
2003bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002004 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002005 (void) frameNumber;
2006
2007 camera_metadata_ro_entry_t entry;
2008
2009 entry = result.find(tag);
2010 if (entry.count == 0) {
2011 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2012 mId, frameNumber, get_camera_metadata_tag_name(tag));
2013 return false;
2014 }
2015
2016 if (sizeof(T) == sizeof(uint8_t)) {
2017 *value = entry.data.u8[0];
2018 } else if (sizeof(T) == sizeof(int32_t)) {
2019 *value = entry.data.i32[0];
2020 } else {
2021 ALOGE("%s: Unexpected type", __FUNCTION__);
2022 return false;
2023 }
2024 return true;
2025}
2026
2027template<typename T>
2028bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002029 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002030 if (result.update(tag, value, 1) != NO_ERROR) {
2031 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2032 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2033 frameNumber, get_camera_metadata_tag_name(tag));
2034 return false;
2035 }
2036 return true;
2037}
2038
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002039void Camera3Device::returnOutputBuffers(
2040 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2041 nsecs_t timestamp) {
2042 for (size_t i = 0; i < numBuffers; i++)
2043 {
2044 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2045 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2046 // Note: stream may be deallocated at this point, if this buffer was
2047 // the last reference to it.
2048 if (res != OK) {
2049 ALOGE("Can't return buffer to its stream: %s (%d)",
2050 strerror(-res), res);
2051 }
2052 }
2053}
2054
2055
2056void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2057
2058 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2059 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2060
2061 nsecs_t sensorTimestamp = request.sensorTimestamp;
2062 nsecs_t shutterTimestamp = request.shutterTimestamp;
2063
2064 // Check if it's okay to remove the request from InFlightMap:
2065 // In the case of a successful request:
2066 // all input and output buffers, all result metadata, shutter callback
2067 // arrived.
2068 // In the case of a unsuccessful request:
2069 // all input and output buffers arrived.
2070 if (request.numBuffersLeft == 0 &&
2071 (request.requestStatus != OK ||
2072 (request.haveResultMetadata && shutterTimestamp != 0))) {
2073 ATRACE_ASYNC_END("frame capture", frameNumber);
2074
2075 // Sanity check - if sensor timestamp matches shutter timestamp
2076 if (request.requestStatus == OK &&
2077 sensorTimestamp != shutterTimestamp) {
2078 SET_ERR("sensor timestamp (%" PRId64
2079 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2080 sensorTimestamp, frameNumber, shutterTimestamp);
2081 }
2082
2083 // for an unsuccessful request, it may have pending output buffers to
2084 // return.
2085 assert(request.requestStatus != OK ||
2086 request.pendingOutputBuffers.size() == 0);
2087 returnOutputBuffers(request.pendingOutputBuffers.array(),
2088 request.pendingOutputBuffers.size(), 0);
2089
2090 mInFlightMap.removeItemsAt(idx, 1);
2091
2092 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2093 }
2094
2095 // Sanity check - if we have too many in-flight frames, something has
2096 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002097 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002098 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002099 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2100 kInFlightWarnLimitHighSpeed) {
2101 CLOGE("In-flight list too large for high speed configuration: %zu",
2102 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002103 }
2104}
2105
2106
2107void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2108 CaptureResultExtras &resultExtras,
2109 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002110 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002111 bool reprocess,
2112 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002113 if (pendingMetadata.isEmpty())
2114 return;
2115
2116 Mutex::Autolock l(mOutputLock);
2117
2118 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002119 if (reprocess) {
2120 if (frameNumber < mNextReprocessResultFrameNumber) {
2121 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002122 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002123 frameNumber, mNextReprocessResultFrameNumber);
2124 return;
2125 }
2126 mNextReprocessResultFrameNumber = frameNumber + 1;
2127 } else {
2128 if (frameNumber < mNextResultFrameNumber) {
2129 SET_ERR("Out-of-order capture result metadata submitted! "
2130 "(got frame number %d, expecting %d)",
2131 frameNumber, mNextResultFrameNumber);
2132 return;
2133 }
2134 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002135 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002136
2137 CaptureResult captureResult;
2138 captureResult.mResultExtras = resultExtras;
2139 captureResult.mMetadata = pendingMetadata;
2140
2141 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2142 (int32_t*)&frameNumber, 1) != OK) {
2143 SET_ERR("Failed to set frame# in metadata (%d)",
2144 frameNumber);
2145 return;
2146 } else {
2147 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2148 __FUNCTION__, mId, frameNumber);
2149 }
2150
2151 // Append any previous partials to form a complete result
2152 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2153 captureResult.mMetadata.append(collectedPartialResult);
2154 }
2155
2156 captureResult.mMetadata.sort();
2157
2158 // Check that there's a timestamp in the result metadata
2159 camera_metadata_entry entry =
2160 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2161 if (entry.count == 0) {
2162 SET_ERR("No timestamp provided by HAL for frame %d!",
2163 frameNumber);
2164 return;
2165 }
2166
Chien-Yu Chend196d612015-06-22 19:49:01 -07002167 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2168
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002169 // Valid result, insert into queue
2170 List<CaptureResult>::iterator queuedResult =
2171 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2172 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2173 ", burstId = %" PRId32, __FUNCTION__,
2174 queuedResult->mResultExtras.requestId,
2175 queuedResult->mResultExtras.frameNumber,
2176 queuedResult->mResultExtras.burstId);
2177
2178 mResultSignal.signal();
2179}
2180
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002181/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002182 * Camera HAL device callback methods
2183 */
2184
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002185void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002186 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002187
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002188 status_t res;
2189
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002190 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002191 if (result->result == NULL && result->num_output_buffers == 0 &&
2192 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002193 SET_ERR("No result data provided by HAL for frame %d",
2194 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002195 return;
2196 }
Zhijun He204e3292014-07-14 17:09:23 -07002197
2198 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2199 // partial_result to 1 when metadata is included in this result.
2200 if (!mUsePartialResult &&
2201 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2202 result->result != NULL &&
2203 result->partial_result != 1) {
2204 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2205 " if partial result is not supported",
2206 frameNumber, result->partial_result);
2207 return;
2208 }
2209
2210 bool isPartialResult = false;
2211 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002212 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002213 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002214
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002215 // Get shutter timestamp and resultExtras from list of in-flight requests,
2216 // where it was added by the shutter notification for this frame. If the
2217 // shutter timestamp isn't received yet, append the output buffers to the
2218 // in-flight request and they will be returned when the shutter timestamp
2219 // arrives. Update the in-flight status and remove the in-flight entry if
2220 // all result data and shutter timestamp have been received.
2221 nsecs_t shutterTimestamp = 0;
2222
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002223 {
2224 Mutex::Autolock l(mInFlightLock);
2225 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2226 if (idx == NAME_NOT_FOUND) {
2227 SET_ERR("Unknown frame number for capture result: %d",
2228 frameNumber);
2229 return;
2230 }
2231 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002232 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2233 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2234 ", partialResultCount = %d",
2235 __FUNCTION__, request.resultExtras.requestId,
2236 request.resultExtras.frameNumber, request.resultExtras.burstId,
2237 result->partial_result);
2238 // Always update the partial count to the latest one if it's not 0
2239 // (buffers only). When framework aggregates adjacent partial results
2240 // into one, the latest partial count will be used.
2241 if (result->partial_result != 0)
2242 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002243
2244 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002245 if (mUsePartialResult && result->result != NULL) {
2246 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2247 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2248 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2249 " the range of [1, %d] when metadata is included in the result",
2250 frameNumber, result->partial_result, mNumPartialResults);
2251 return;
2252 }
2253 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002254 if (isPartialResult) {
2255 request.partialResult.collectedResult.append(result->result);
2256 }
Zhijun He204e3292014-07-14 17:09:23 -07002257 } else {
2258 camera_metadata_ro_entry_t partialResultEntry;
2259 res = find_camera_metadata_ro_entry(result->result,
2260 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2261 if (res != NAME_NOT_FOUND &&
2262 partialResultEntry.count > 0 &&
2263 partialResultEntry.data.u8[0] ==
2264 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2265 // A partial result. Flag this as such, and collect this
2266 // set of metadata into the in-flight entry.
2267 isPartialResult = true;
2268 request.partialResult.collectedResult.append(
2269 result->result);
2270 request.partialResult.collectedResult.erase(
2271 ANDROID_QUIRKS_PARTIAL_RESULT);
2272 }
2273 }
2274
2275 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002276 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002277 if (!request.partialResult.haveSent3A) {
2278 request.partialResult.haveSent3A =
2279 processPartial3AResult(frameNumber,
2280 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002281 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002282 }
2283 }
2284 }
2285
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002286 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002287 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002288
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002289 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002290 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002291 if (request.haveResultMetadata) {
2292 SET_ERR("Called multiple times with metadata for frame %d",
2293 frameNumber);
2294 return;
2295 }
Zhijun He204e3292014-07-14 17:09:23 -07002296 if (mUsePartialResult &&
2297 !request.partialResult.collectedResult.isEmpty()) {
2298 collectedPartialResult.acquire(
2299 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002300 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002301 request.haveResultMetadata = true;
2302 }
2303
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002304 uint32_t numBuffersReturned = result->num_output_buffers;
2305 if (result->input_buffer != NULL) {
2306 if (hasInputBufferInRequest) {
2307 numBuffersReturned += 1;
2308 } else {
2309 ALOGW("%s: Input buffer should be NULL if there is no input"
2310 " buffer sent in the request",
2311 __FUNCTION__);
2312 }
2313 }
2314 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002315 if (request.numBuffersLeft < 0) {
2316 SET_ERR("Too many buffers returned for frame %d",
2317 frameNumber);
2318 return;
2319 }
2320
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002321 camera_metadata_ro_entry_t entry;
2322 res = find_camera_metadata_ro_entry(result->result,
2323 ANDROID_SENSOR_TIMESTAMP, &entry);
2324 if (res == OK && entry.count == 1) {
2325 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002326 }
2327
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002328 // If shutter event isn't received yet, append the output buffers to
2329 // the in-flight request. Otherwise, return the output buffers to
2330 // streams.
2331 if (shutterTimestamp == 0) {
2332 request.pendingOutputBuffers.appendArray(result->output_buffers,
2333 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002334 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002335 returnOutputBuffers(result->output_buffers,
2336 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002337 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002338
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002339 if (result->result != NULL && !isPartialResult) {
2340 if (shutterTimestamp == 0) {
2341 request.pendingMetadata = result->result;
2342 request.partialResult.collectedResult = collectedPartialResult;
2343 } else {
2344 CameraMetadata metadata;
2345 metadata = result->result;
2346 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002347 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2348 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002349 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002350 }
2351
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002352 removeInFlightRequestIfReadyLocked(idx);
2353 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002354
Zhijun Hef0d962a2014-06-30 10:24:11 -07002355 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002356 if (hasInputBufferInRequest) {
2357 Camera3Stream *stream =
2358 Camera3Stream::cast(result->input_buffer->stream);
2359 res = stream->returnInputBuffer(*(result->input_buffer));
2360 // Note: stream may be deallocated at this point, if this buffer was the
2361 // last reference to it.
2362 if (res != OK) {
2363 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2364 " its stream:%s (%d)", __FUNCTION__,
2365 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002366 }
2367 } else {
2368 ALOGW("%s: Input buffer should be NULL if there is no input"
2369 " buffer sent in the request, skipping input buffer return.",
2370 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002371 }
2372 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002373}
2374
2375void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002376 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002377 NotificationListener *listener;
2378 {
2379 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002380 listener = mListener;
2381 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002382
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002383 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002384 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002385 return;
2386 }
2387
2388 switch (msg->type) {
2389 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002390 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002391 break;
2392 }
2393 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002394 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002395 break;
2396 }
2397 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002398 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002399 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002400 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002401}
2402
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002403void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2404 NotificationListener *listener) {
2405
2406 // Map camera HAL error codes to ICameraDeviceCallback error codes
2407 // Index into this with the HAL error code
2408 static const ICameraDeviceCallbacks::CameraErrorCode
2409 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2410 // 0 = Unused error code
2411 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2412 // 1 = CAMERA3_MSG_ERROR_DEVICE
2413 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2414 // 2 = CAMERA3_MSG_ERROR_REQUEST
2415 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2416 // 3 = CAMERA3_MSG_ERROR_RESULT
2417 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2418 // 4 = CAMERA3_MSG_ERROR_BUFFER
2419 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2420 };
2421
2422 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2423 ((msg.error_code >= 0) &&
2424 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2425 halErrorMap[msg.error_code] :
2426 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2427
2428 int streamId = 0;
2429 if (msg.error_stream != NULL) {
2430 Camera3Stream *stream =
2431 Camera3Stream::cast(msg.error_stream);
2432 streamId = stream->getId();
2433 }
2434 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2435 mId, __FUNCTION__, msg.frame_number,
2436 streamId, msg.error_code);
2437
2438 CaptureResultExtras resultExtras;
2439 switch (errorCode) {
2440 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2441 // SET_ERR calls notifyError
2442 SET_ERR("Camera HAL reported serious device error");
2443 break;
2444 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2445 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2446 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2447 {
2448 Mutex::Autolock l(mInFlightLock);
2449 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2450 if (idx >= 0) {
2451 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2452 r.requestStatus = msg.error_code;
2453 resultExtras = r.resultExtras;
2454 } else {
2455 resultExtras.frameNumber = msg.frame_number;
2456 ALOGE("Camera %d: %s: cannot find in-flight request on "
2457 "frame %" PRId64 " error", mId, __FUNCTION__,
2458 resultExtras.frameNumber);
2459 }
2460 }
2461 if (listener != NULL) {
2462 listener->notifyError(errorCode, resultExtras);
2463 } else {
2464 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2465 }
2466 break;
2467 default:
2468 // SET_ERR calls notifyError
2469 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2470 break;
2471 }
2472}
2473
2474void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2475 NotificationListener *listener) {
2476 ssize_t idx;
2477 // Verify ordering of shutter notifications
2478 {
2479 Mutex::Autolock l(mOutputLock);
2480 // TODO: need to track errors for tighter bounds on expected frame number.
2481 if (msg.frame_number < mNextShutterFrameNumber) {
2482 SET_ERR("Shutter notification out-of-order. Expected "
2483 "notification for frame %d, got frame %d",
2484 mNextShutterFrameNumber, msg.frame_number);
2485 return;
2486 }
2487 mNextShutterFrameNumber = msg.frame_number + 1;
2488 }
2489
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002490 // Set timestamp for the request in the in-flight tracking
2491 // and get the request ID to send upstream
2492 {
2493 Mutex::Autolock l(mInFlightLock);
2494 idx = mInFlightMap.indexOfKey(msg.frame_number);
2495 if (idx >= 0) {
2496 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002497
2498 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2499 mId, __FUNCTION__,
2500 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2501 // Call listener, if any
2502 if (listener != NULL) {
2503 listener->notifyShutter(r.resultExtras, msg.timestamp);
2504 }
2505
2506 r.shutterTimestamp = msg.timestamp;
2507
2508 // send pending result and buffers
2509 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002510 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002511 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002512 returnOutputBuffers(r.pendingOutputBuffers.array(),
2513 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2514 r.pendingOutputBuffers.clear();
2515
2516 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002517 }
2518 }
2519 if (idx < 0) {
2520 SET_ERR("Shutter notification for non-existent frame number %d",
2521 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002522 }
2523}
2524
2525
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002526CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002527 ALOGV("%s", __FUNCTION__);
2528
Igor Murashkin1e479c02013-09-06 16:55:14 -07002529 CameraMetadata retVal;
2530
2531 if (mRequestThread != NULL) {
2532 retVal = mRequestThread->getLatestRequest();
2533 }
2534
Igor Murashkin1e479c02013-09-06 16:55:14 -07002535 return retVal;
2536}
2537
Jianing Weicb0652e2014-03-12 18:29:36 -07002538
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002539/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002540 * RequestThread inner class methods
2541 */
2542
2543Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002544 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002545 camera3_device_t *hal3Device,
2546 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002547 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002548 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002549 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002550 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002551 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002552 mReconfigured(false),
2553 mDoPause(false),
2554 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002555 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002556 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002557 mCurrentAfTriggerId(0),
2558 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002559 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2560 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002561 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002562}
2563
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002564void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002565 NotificationListener *listener) {
2566 Mutex::Autolock l(mRequestLock);
2567 mListener = listener;
2568}
2569
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002570void Camera3Device::RequestThread::configurationComplete() {
2571 Mutex::Autolock l(mRequestLock);
2572 mReconfigured = true;
2573}
2574
Jianing Wei90e59c92014-03-12 18:29:36 -07002575status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002576 List<sp<CaptureRequest> > &requests,
2577 /*out*/
2578 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002579 Mutex::Autolock l(mRequestLock);
2580 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2581 ++it) {
2582 mRequestQueue.push_back(*it);
2583 }
2584
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002585 if (lastFrameNumber != NULL) {
2586 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2587 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2588 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2589 *lastFrameNumber);
2590 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002591
Jianing Wei90e59c92014-03-12 18:29:36 -07002592 unpauseForNewRequests();
2593
2594 return OK;
2595}
2596
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002597
2598status_t Camera3Device::RequestThread::queueTrigger(
2599 RequestTrigger trigger[],
2600 size_t count) {
2601
2602 Mutex::Autolock l(mTriggerMutex);
2603 status_t ret;
2604
2605 for (size_t i = 0; i < count; ++i) {
2606 ret = queueTriggerLocked(trigger[i]);
2607
2608 if (ret != OK) {
2609 return ret;
2610 }
2611 }
2612
2613 return OK;
2614}
2615
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002616int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2617 sp<Camera3Device> d = device.promote();
2618 if (d != NULL) return d->mId;
2619 return 0;
2620}
2621
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002622status_t Camera3Device::RequestThread::queueTriggerLocked(
2623 RequestTrigger trigger) {
2624
2625 uint32_t tag = trigger.metadataTag;
2626 ssize_t index = mTriggerMap.indexOfKey(tag);
2627
2628 switch (trigger.getTagType()) {
2629 case TYPE_BYTE:
2630 // fall-through
2631 case TYPE_INT32:
2632 break;
2633 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002634 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2635 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002636 return INVALID_OPERATION;
2637 }
2638
2639 /**
2640 * Collect only the latest trigger, since we only have 1 field
2641 * in the request settings per trigger tag, and can't send more than 1
2642 * trigger per request.
2643 */
2644 if (index != NAME_NOT_FOUND) {
2645 mTriggerMap.editValueAt(index) = trigger;
2646 } else {
2647 mTriggerMap.add(tag, trigger);
2648 }
2649
2650 return OK;
2651}
2652
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002653status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002654 const RequestList &requests,
2655 /*out*/
2656 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002657 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002658 if (lastFrameNumber != NULL) {
2659 *lastFrameNumber = mRepeatingLastFrameNumber;
2660 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002661 mRepeatingRequests.clear();
2662 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2663 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002664
2665 unpauseForNewRequests();
2666
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002667 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002668 return OK;
2669}
2670
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002671bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2672 if (mRepeatingRequests.empty()) {
2673 return false;
2674 }
2675 int32_t requestId = requestIn->mResultExtras.requestId;
2676 const RequestList &repeatRequests = mRepeatingRequests;
2677 // All repeating requests are guaranteed to have same id so only check first quest
2678 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2679 return (firstRequest->mResultExtras.requestId == requestId);
2680}
2681
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002682status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002683 Mutex::Autolock l(mRequestLock);
2684 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002685 if (lastFrameNumber != NULL) {
2686 *lastFrameNumber = mRepeatingLastFrameNumber;
2687 }
2688 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002689 return OK;
2690}
2691
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002692status_t Camera3Device::RequestThread::clear(
2693 NotificationListener *listener,
2694 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002695 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002696 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002697
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002698 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002699
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002700 // Send errors for all requests pending in the request queue, including
2701 // pending repeating requests
2702 if (listener != NULL) {
2703 for (RequestList::iterator it = mRequestQueue.begin();
2704 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002705 // Abort the input buffers for reprocess requests.
2706 if ((*it)->mInputStream != NULL) {
2707 camera3_stream_buffer_t inputBuffer;
2708 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2709 if (res != OK) {
2710 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2711 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2712 } else {
2713 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2714 if (res != OK) {
2715 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2716 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2717 }
2718 }
2719 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002720 // Set the frame number this request would have had, if it
2721 // had been submitted; this frame number will not be reused.
2722 // The requestId and burstId fields were set when the request was
2723 // submitted originally (in convertMetadataListToRequestListLocked)
2724 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2725 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2726 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002727 }
2728 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002729 mRequestQueue.clear();
2730 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002731 if (lastFrameNumber != NULL) {
2732 *lastFrameNumber = mRepeatingLastFrameNumber;
2733 }
2734 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002735 return OK;
2736}
2737
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002738void Camera3Device::RequestThread::setPaused(bool paused) {
2739 Mutex::Autolock l(mPauseLock);
2740 mDoPause = paused;
2741 mDoPauseSignal.signal();
2742}
2743
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002744status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2745 int32_t requestId, nsecs_t timeout) {
2746 Mutex::Autolock l(mLatestRequestMutex);
2747 status_t res;
2748 while (mLatestRequestId != requestId) {
2749 nsecs_t startTime = systemTime();
2750
2751 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2752 if (res != OK) return res;
2753
2754 timeout -= (systemTime() - startTime);
2755 }
2756
2757 return OK;
2758}
2759
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002760void Camera3Device::RequestThread::requestExit() {
2761 // Call parent to set up shutdown
2762 Thread::requestExit();
2763 // The exit from any possible waits
2764 mDoPauseSignal.signal();
2765 mRequestSignal.signal();
2766}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002767
Chien-Yu Chend196d612015-06-22 19:49:01 -07002768
2769/**
2770 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2771 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2772 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2773 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2774 * request.
2775 */
2776void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2777 request->mAeTriggerCancelOverride.applyAeLock = false;
2778 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2779
2780 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2781 return;
2782 }
2783
2784 camera_metadata_entry_t aePrecaptureTrigger =
2785 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2786 if (aePrecaptureTrigger.count > 0 &&
2787 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2788 // Always override CANCEL to IDLE
2789 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2790 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2791 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2792 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2793 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2794
2795 if (mAeLockAvailable == true) {
2796 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2797 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2798 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2799 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2800 request->mAeTriggerCancelOverride.applyAeLock = true;
2801 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2802 }
2803 }
2804 }
2805}
2806
2807/**
2808 * Override result metadata for cancelling AE precapture trigger applied in
2809 * handleAePrecaptureCancelRequest().
2810 */
2811void Camera3Device::overrideResultForPrecaptureCancel(
2812 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2813 if (aeTriggerCancelOverride.applyAeLock) {
2814 // Only devices <= v3.2 should have this override
2815 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2816 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2817 }
2818
2819 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2820 // Only devices <= v3.2 should have this override
2821 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2822 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2823 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2824 }
2825}
2826
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002827bool Camera3Device::RequestThread::threadLoop() {
2828
2829 status_t res;
2830
2831 // Handle paused state.
2832 if (waitIfPaused()) {
2833 return true;
2834 }
2835
2836 // Get work to do
2837
2838 sp<CaptureRequest> nextRequest = waitForNextRequest();
2839 if (nextRequest == NULL) {
2840 return true;
2841 }
2842
2843 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002844 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002845 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002846 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002847
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002848 // Get the request ID, if any
2849 int requestId;
2850 camera_metadata_entry_t requestIdEntry =
2851 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2852 if (requestIdEntry.count > 0) {
2853 requestId = requestIdEntry.data.i32[0];
2854 } else {
2855 ALOGW("%s: Did not have android.request.id set in the request",
2856 __FUNCTION__);
2857 requestId = NAME_NOT_FOUND;
2858 }
2859
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002860 // Insert any queued triggers (before metadata is locked)
2861 int32_t triggerCount;
2862 res = insertTriggers(nextRequest);
2863 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002864 SET_ERR("RequestThread: Unable to insert triggers "
2865 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002866 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002867 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2868 return false;
2869 }
2870 triggerCount = res;
2871
2872 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2873
2874 // If the request is the same as last, or we had triggers last time
2875 if (mPrevRequest != nextRequest || triggersMixedIn) {
2876 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002877 * HAL workaround:
2878 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2879 */
2880 res = addDummyTriggerIds(nextRequest);
2881 if (res != OK) {
2882 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2883 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002884 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002885 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2886 return false;
2887 }
2888
2889 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002890 * The request should be presorted so accesses in HAL
2891 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2892 */
2893 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002894 request.settings = nextRequest->mSettings.getAndLock();
2895 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002896 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2897
2898 IF_ALOGV() {
2899 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2900 find_camera_metadata_ro_entry(
2901 request.settings,
2902 ANDROID_CONTROL_AF_TRIGGER,
2903 &e
2904 );
2905 if (e.count > 0) {
2906 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2907 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002908 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002909 e.data.u8[0]);
2910 }
2911 }
2912 } else {
2913 // leave request.settings NULL to indicate 'reuse latest given'
2914 ALOGVV("%s: Request settings are REUSED",
2915 __FUNCTION__);
2916 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002917
Zhijun Hef0d962a2014-06-30 10:24:11 -07002918 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002919
2920 // Fill in buffers
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002921 if (nextRequest->mInputStream != NULL) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002922 request.input_buffer = &nextRequest->mInputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002923 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002924 } else {
2925 request.input_buffer = NULL;
2926 }
2927
2928 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2929 nextRequest->mOutputStreams.size());
2930 request.output_buffers = outputBuffers.array();
2931 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2932 res = nextRequest->mOutputStreams.editItemAt(i)->
2933 getBuffer(&outputBuffers.editItemAt(i));
2934 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002935 // Can't get output buffer from gralloc queue - this could be due to
2936 // abandoned queue or other consumer misbehavior, so not a fatal
2937 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002938 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2939 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala0ec23d32015-05-28 15:15:54 -07002940 {
2941 Mutex::Autolock l(mRequestLock);
2942 if (mListener != NULL) {
2943 mListener->notifyError(
2944 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2945 nextRequest->mResultExtras);
2946 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002947 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002948 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2949 return true;
2950 }
2951 request.num_output_buffers++;
2952 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002953 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002954
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002955 // Log request in the in-flight queue
2956 sp<Camera3Device> parent = mParent.promote();
2957 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002958 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002959 CLOGE("RequestThread: Parent is gone");
2960 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2961 return false;
2962 }
2963
Jianing Weicb0652e2014-03-12 18:29:36 -07002964 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002965 totalNumBuffers, nextRequest->mResultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002966 /*hasInput*/request.input_buffer != NULL,
2967 nextRequest->mAeTriggerCancelOverride);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002968 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2969 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002970 __FUNCTION__,
2971 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2972 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002973 if (res != OK) {
2974 SET_ERR("RequestThread: Unable to register new in-flight request:"
2975 " %s (%d)", strerror(-res), res);
2976 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2977 return false;
2978 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002979
Zhijun Hecc27e112013-10-03 16:12:43 -07002980 // Inform waitUntilRequestProcessed thread of a new request ID
2981 {
2982 Mutex::Autolock al(mLatestRequestMutex);
2983
2984 mLatestRequestId = requestId;
2985 mLatestRequestSignal.signal();
2986 }
2987
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002988 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002989 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2990 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002991 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002992 ATRACE_END();
2993
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002994 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002995 // Should only get a failure here for malformed requests or device-level
2996 // errors, so consider all errors fatal. Bad metadata failures should
2997 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002998 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002999 " device: %s (%d)", request.frame_number, strerror(-res), res);
3000 cleanUpFailedRequest(request, nextRequest, outputBuffers);
3001 return false;
3002 }
3003
Igor Murashkin1e479c02013-09-06 16:55:14 -07003004 // Update the latest request sent to HAL
3005 if (request.settings != NULL) { // Don't update them if they were unchanged
3006 Mutex::Autolock al(mLatestRequestMutex);
3007
3008 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
3009 mLatestRequest.acquire(cloned);
3010 }
3011
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003012 if (request.settings != NULL) {
3013 nextRequest->mSettings.unlock(request.settings);
3014 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003015
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003016 // Unset as current request
3017 {
3018 Mutex::Autolock l(mRequestLock);
3019 mNextRequest.clear();
3020 }
3021
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003022 // Remove any previously queued triggers (after unlock)
3023 res = removeTriggers(mPrevRequest);
3024 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003025 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003026 "(capture request %d, HAL device: %s (%d)",
3027 request.frame_number, strerror(-res), res);
3028 return false;
3029 }
3030 mPrevTriggers = triggerCount;
3031
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003032 return true;
3033}
3034
Igor Murashkin1e479c02013-09-06 16:55:14 -07003035CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3036 Mutex::Autolock al(mLatestRequestMutex);
3037
3038 ALOGV("RequestThread::%s", __FUNCTION__);
3039
3040 return mLatestRequest;
3041}
3042
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003043bool Camera3Device::RequestThread::isStreamPending(
3044 sp<Camera3StreamInterface>& stream) {
3045 Mutex::Autolock l(mRequestLock);
3046
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003047 if (mNextRequest != nullptr) {
3048 for (const auto& s : mNextRequest->mOutputStreams) {
3049 if (stream == s) return true;
3050 }
3051 if (stream == mNextRequest->mInputStream) return true;
3052 }
3053
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003054 for (const auto& request : mRequestQueue) {
3055 for (const auto& s : request->mOutputStreams) {
3056 if (stream == s) return true;
3057 }
3058 if (stream == request->mInputStream) return true;
3059 }
3060
3061 for (const auto& request : mRepeatingRequests) {
3062 for (const auto& s : request->mOutputStreams) {
3063 if (stream == s) return true;
3064 }
3065 if (stream == request->mInputStream) return true;
3066 }
3067
3068 return false;
3069}
Jianing Weicb0652e2014-03-12 18:29:36 -07003070
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003071void Camera3Device::RequestThread::cleanUpFailedRequest(
3072 camera3_capture_request_t &request,
3073 sp<CaptureRequest> &nextRequest,
3074 Vector<camera3_stream_buffer_t> &outputBuffers) {
3075
3076 if (request.settings != NULL) {
3077 nextRequest->mSettings.unlock(request.settings);
3078 }
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003079 if (nextRequest->mInputStream != NULL) {
3080 nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3081 nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003082 }
3083 for (size_t i = 0; i < request.num_output_buffers; i++) {
3084 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3085 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
3086 outputBuffers[i], 0);
3087 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003088
3089 Mutex::Autolock l(mRequestLock);
3090 mNextRequest.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003091}
3092
3093sp<Camera3Device::CaptureRequest>
3094 Camera3Device::RequestThread::waitForNextRequest() {
3095 status_t res;
3096 sp<CaptureRequest> nextRequest;
3097
3098 // Optimized a bit for the simple steady-state case (single repeating
3099 // request), to avoid putting that request in the queue temporarily.
3100 Mutex::Autolock l(mRequestLock);
3101
3102 while (mRequestQueue.empty()) {
3103 if (!mRepeatingRequests.empty()) {
3104 // Always atomically enqueue all requests in a repeating request
3105 // list. Guarantees a complete in-sequence set of captures to
3106 // application.
3107 const RequestList &requests = mRepeatingRequests;
3108 RequestList::const_iterator firstRequest =
3109 requests.begin();
3110 nextRequest = *firstRequest;
3111 mRequestQueue.insert(mRequestQueue.end(),
3112 ++firstRequest,
3113 requests.end());
3114 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003115
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003116 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003117
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003118 break;
3119 }
3120
3121 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3122
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003123 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3124 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003125 Mutex::Autolock pl(mPauseLock);
3126 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003127 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003128 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003129 // Let the tracker know
3130 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3131 if (statusTracker != 0) {
3132 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3133 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003134 }
3135 // Stop waiting for now and let thread management happen
3136 return NULL;
3137 }
3138 }
3139
3140 if (nextRequest == NULL) {
3141 // Don't have a repeating request already in hand, so queue
3142 // must have an entry now.
3143 RequestList::iterator firstRequest =
3144 mRequestQueue.begin();
3145 nextRequest = *firstRequest;
3146 mRequestQueue.erase(firstRequest);
3147 }
3148
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003149 // In case we've been unpaused by setPaused clearing mDoPause, need to
3150 // update internal pause state (capture/setRepeatingRequest unpause
3151 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003152 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003153 if (mPaused) {
3154 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3155 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3156 if (statusTracker != 0) {
3157 statusTracker->markComponentActive(mStatusId);
3158 }
3159 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003160 mPaused = false;
3161
3162 // Check if we've reconfigured since last time, and reset the preview
3163 // request if so. Can't use 'NULL request == repeat' across configure calls.
3164 if (mReconfigured) {
3165 mPrevRequest.clear();
3166 mReconfigured = false;
3167 }
3168
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003169 if (nextRequest != NULL) {
3170 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003171 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3172 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003173
3174 // Since RequestThread::clear() removes buffers from the input stream,
3175 // get the right buffer here before unlocking mRequestLock
3176 if (nextRequest->mInputStream != NULL) {
3177 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3178 if (res != OK) {
3179 // Can't get input buffer from gralloc queue - this could be due to
3180 // disconnected queue or other producer misbehavior, so not a fatal
3181 // error
3182 ALOGE("%s: Can't get input buffer, skipping request:"
3183 " %s (%d)", __FUNCTION__, strerror(-res), res);
3184 if (mListener != NULL) {
3185 mListener->notifyError(
3186 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3187 nextRequest->mResultExtras);
3188 }
3189 return NULL;
3190 }
3191 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003192 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003193
3194 handleAePrecaptureCancelRequest(nextRequest);
3195
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003196 mNextRequest = nextRequest;
3197
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003198 return nextRequest;
3199}
3200
3201bool Camera3Device::RequestThread::waitIfPaused() {
3202 status_t res;
3203 Mutex::Autolock l(mPauseLock);
3204 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003205 if (mPaused == false) {
3206 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003207 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3208 // Let the tracker know
3209 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3210 if (statusTracker != 0) {
3211 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3212 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003213 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003214
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003215 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003216 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003217 return true;
3218 }
3219 }
3220 // We don't set mPaused to false here, because waitForNextRequest needs
3221 // to further manage the paused state in case of starvation.
3222 return false;
3223}
3224
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003225void Camera3Device::RequestThread::unpauseForNewRequests() {
3226 // With work to do, mark thread as unpaused.
3227 // If paused by request (setPaused), don't resume, to avoid
3228 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003229 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003230 Mutex::Autolock p(mPauseLock);
3231 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003232 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3233 if (mPaused) {
3234 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3235 if (statusTracker != 0) {
3236 statusTracker->markComponentActive(mStatusId);
3237 }
3238 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003239 mPaused = false;
3240 }
3241}
3242
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003243void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3244 sp<Camera3Device> parent = mParent.promote();
3245 if (parent != NULL) {
3246 va_list args;
3247 va_start(args, fmt);
3248
3249 parent->setErrorStateV(fmt, args);
3250
3251 va_end(args);
3252 }
3253}
3254
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003255status_t Camera3Device::RequestThread::insertTriggers(
3256 const sp<CaptureRequest> &request) {
3257
3258 Mutex::Autolock al(mTriggerMutex);
3259
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003260 sp<Camera3Device> parent = mParent.promote();
3261 if (parent == NULL) {
3262 CLOGE("RequestThread: Parent is gone");
3263 return DEAD_OBJECT;
3264 }
3265
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003266 CameraMetadata &metadata = request->mSettings;
3267 size_t count = mTriggerMap.size();
3268
3269 for (size_t i = 0; i < count; ++i) {
3270 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003271 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003272
3273 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3274 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3275 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003276 if (isAeTrigger) {
3277 request->mResultExtras.precaptureTriggerId = triggerId;
3278 mCurrentPreCaptureTriggerId = triggerId;
3279 } else {
3280 request->mResultExtras.afTriggerId = triggerId;
3281 mCurrentAfTriggerId = triggerId;
3282 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003283 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3284 continue; // Trigger ID tag is deprecated since device HAL 3.2
3285 }
3286 }
3287
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003288 camera_metadata_entry entry = metadata.find(tag);
3289
3290 if (entry.count > 0) {
3291 /**
3292 * Already has an entry for this trigger in the request.
3293 * Rewrite it with our requested trigger value.
3294 */
3295 RequestTrigger oldTrigger = trigger;
3296
3297 oldTrigger.entryValue = entry.data.u8[0];
3298
3299 mTriggerReplacedMap.add(tag, oldTrigger);
3300 } else {
3301 /**
3302 * More typical, no trigger entry, so we just add it
3303 */
3304 mTriggerRemovedMap.add(tag, trigger);
3305 }
3306
3307 status_t res;
3308
3309 switch (trigger.getTagType()) {
3310 case TYPE_BYTE: {
3311 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3312 res = metadata.update(tag,
3313 &entryValue,
3314 /*count*/1);
3315 break;
3316 }
3317 case TYPE_INT32:
3318 res = metadata.update(tag,
3319 &trigger.entryValue,
3320 /*count*/1);
3321 break;
3322 default:
3323 ALOGE("%s: Type not supported: 0x%x",
3324 __FUNCTION__,
3325 trigger.getTagType());
3326 return INVALID_OPERATION;
3327 }
3328
3329 if (res != OK) {
3330 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3331 ", value %d", __FUNCTION__, trigger.getTagName(),
3332 trigger.entryValue);
3333 return res;
3334 }
3335
3336 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3337 trigger.getTagName(),
3338 trigger.entryValue);
3339 }
3340
3341 mTriggerMap.clear();
3342
3343 return count;
3344}
3345
3346status_t Camera3Device::RequestThread::removeTriggers(
3347 const sp<CaptureRequest> &request) {
3348 Mutex::Autolock al(mTriggerMutex);
3349
3350 CameraMetadata &metadata = request->mSettings;
3351
3352 /**
3353 * Replace all old entries with their old values.
3354 */
3355 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3356 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3357
3358 status_t res;
3359
3360 uint32_t tag = trigger.metadataTag;
3361 switch (trigger.getTagType()) {
3362 case TYPE_BYTE: {
3363 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3364 res = metadata.update(tag,
3365 &entryValue,
3366 /*count*/1);
3367 break;
3368 }
3369 case TYPE_INT32:
3370 res = metadata.update(tag,
3371 &trigger.entryValue,
3372 /*count*/1);
3373 break;
3374 default:
3375 ALOGE("%s: Type not supported: 0x%x",
3376 __FUNCTION__,
3377 trigger.getTagType());
3378 return INVALID_OPERATION;
3379 }
3380
3381 if (res != OK) {
3382 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3383 ", trigger value %d", __FUNCTION__,
3384 trigger.getTagName(), trigger.entryValue);
3385 return res;
3386 }
3387 }
3388 mTriggerReplacedMap.clear();
3389
3390 /**
3391 * Remove all new entries.
3392 */
3393 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3394 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3395 status_t res = metadata.erase(trigger.metadataTag);
3396
3397 if (res != OK) {
3398 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3399 ", trigger value %d", __FUNCTION__,
3400 trigger.getTagName(), trigger.entryValue);
3401 return res;
3402 }
3403 }
3404 mTriggerRemovedMap.clear();
3405
3406 return OK;
3407}
3408
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003409status_t Camera3Device::RequestThread::addDummyTriggerIds(
3410 const sp<CaptureRequest> &request) {
3411 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3412 static const int32_t dummyTriggerId = 1;
3413 status_t res;
3414
3415 CameraMetadata &metadata = request->mSettings;
3416
3417 // If AF trigger is active, insert a dummy AF trigger ID if none already
3418 // exists
3419 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3420 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3421 if (afTrigger.count > 0 &&
3422 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3423 afId.count == 0) {
3424 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3425 if (res != OK) return res;
3426 }
3427
3428 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3429 // if none already exists
3430 camera_metadata_entry pcTrigger =
3431 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3432 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3433 if (pcTrigger.count > 0 &&
3434 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3435 pcId.count == 0) {
3436 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3437 &dummyTriggerId, 1);
3438 if (res != OK) return res;
3439 }
3440
3441 return OK;
3442}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003443
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003444/**
3445 * PreparerThread inner class methods
3446 */
3447
3448Camera3Device::PreparerThread::PreparerThread() :
3449 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3450}
3451
3452Camera3Device::PreparerThread::~PreparerThread() {
3453 Thread::requestExitAndWait();
3454 if (mCurrentStream != nullptr) {
3455 mCurrentStream->cancelPrepare();
3456 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3457 mCurrentStream.clear();
3458 }
3459 clear();
3460}
3461
Ruben Brunkc78ac262015-08-13 17:58:46 -07003462status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003463 status_t res;
3464
3465 Mutex::Autolock l(mLock);
3466
Ruben Brunkc78ac262015-08-13 17:58:46 -07003467 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003468 if (res == OK) {
3469 // No preparation needed, fire listener right off
3470 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3471 if (mListener) {
3472 mListener->notifyPrepared(stream->getId());
3473 }
3474 return OK;
3475 } else if (res != NOT_ENOUGH_DATA) {
3476 return res;
3477 }
3478
3479 // Need to prepare, start up thread if necessary
3480 if (!mActive) {
3481 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3482 // isn't running
3483 Thread::requestExitAndWait();
3484 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3485 if (res != OK) {
3486 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3487 if (mListener) {
3488 mListener->notifyPrepared(stream->getId());
3489 }
3490 return res;
3491 }
3492 mCancelNow = false;
3493 mActive = true;
3494 ALOGV("%s: Preparer stream started", __FUNCTION__);
3495 }
3496
3497 // queue up the work
3498 mPendingStreams.push_back(stream);
3499 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3500
3501 return OK;
3502}
3503
3504status_t Camera3Device::PreparerThread::clear() {
3505 status_t res;
3506
3507 Mutex::Autolock l(mLock);
3508
3509 for (const auto& stream : mPendingStreams) {
3510 stream->cancelPrepare();
3511 }
3512 mPendingStreams.clear();
3513 mCancelNow = true;
3514
3515 return OK;
3516}
3517
3518void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3519 Mutex::Autolock l(mLock);
3520 mListener = listener;
3521}
3522
3523bool Camera3Device::PreparerThread::threadLoop() {
3524 status_t res;
3525 {
3526 Mutex::Autolock l(mLock);
3527 if (mCurrentStream == nullptr) {
3528 // End thread if done with work
3529 if (mPendingStreams.empty()) {
3530 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3531 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3532 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3533 mActive = false;
3534 return false;
3535 }
3536
3537 // Get next stream to prepare
3538 auto it = mPendingStreams.begin();
3539 mCurrentStream = *it;
3540 mPendingStreams.erase(it);
3541 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3542 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3543 } else if (mCancelNow) {
3544 mCurrentStream->cancelPrepare();
3545 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3546 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3547 mCurrentStream.clear();
3548 mCancelNow = false;
3549 return true;
3550 }
3551 }
3552
3553 res = mCurrentStream->prepareNextBuffer();
3554 if (res == NOT_ENOUGH_DATA) return true;
3555 if (res != OK) {
3556 // Something bad happened; try to recover by cancelling prepare and
3557 // signalling listener anyway
3558 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3559 mCurrentStream->getId(), res, strerror(-res));
3560 mCurrentStream->cancelPrepare();
3561 }
3562
3563 // This stream has finished, notify listener
3564 Mutex::Autolock l(mLock);
3565 if (mListener) {
3566 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3567 mCurrentStream->getId());
3568 mListener->notifyPrepared(mCurrentStream->getId());
3569 }
3570
3571 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3572 mCurrentStream.clear();
3573
3574 return true;
3575}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003576
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003577/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003578 * Static callback forwarding methods from HAL to instance
3579 */
3580
3581void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3582 const camera3_capture_result *result) {
3583 Camera3Device *d =
3584 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003585
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003586 d->processCaptureResult(result);
3587}
3588
3589void Camera3Device::sNotify(const camera3_callback_ops *cb,
3590 const camera3_notify_msg *msg) {
3591 Camera3Device *d =
3592 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3593 d->notify(msg);
3594}
3595
3596}; // namespace android