blob: 0a4440f3dc24bc5db7db0f8ff7214cf1e508ce38 [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) {
1383 ATRACE_CALL();
1384 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001385 Mutex::Autolock il(mInterfaceLock);
1386 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001387
1388 sp<Camera3StreamInterface> stream;
1389 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1390 if (outputStreamIdx == NAME_NOT_FOUND) {
1391 CLOGE("Stream %d does not exist", streamId);
1392 return BAD_VALUE;
1393 }
1394
1395 stream = mOutputStreams.editValueAt(outputStreamIdx);
1396
1397 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001398 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001399 return BAD_VALUE;
1400 }
1401
1402 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001403 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001404 return BAD_VALUE;
1405 }
1406
1407 return mPreparerThread->prepare(stream);
1408}
1409
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001410status_t Camera3Device::tearDown(int streamId) {
1411 ATRACE_CALL();
1412 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1413 Mutex::Autolock il(mInterfaceLock);
1414 Mutex::Autolock l(mLock);
1415
1416 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1417 // since we cannot call register_stream_buffers except right after configure_streams.
1418 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1419 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1420 __FUNCTION__, mHal3Device->common.version);
1421 return NO_INIT;
1422 }
1423
1424 sp<Camera3StreamInterface> stream;
1425 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1426 if (outputStreamIdx == NAME_NOT_FOUND) {
1427 CLOGE("Stream %d does not exist", streamId);
1428 return BAD_VALUE;
1429 }
1430
1431 stream = mOutputStreams.editValueAt(outputStreamIdx);
1432
1433 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1434 CLOGE("Stream %d is a target of a in-progress request", streamId);
1435 return BAD_VALUE;
1436 }
1437
1438 return stream->tearDown();
1439}
1440
Zhijun He204e3292014-07-14 17:09:23 -07001441uint32_t Camera3Device::getDeviceVersion() {
1442 ATRACE_CALL();
1443 Mutex::Autolock il(mInterfaceLock);
1444 return mDeviceVersion;
1445}
1446
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001447/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001448 * Methods called by subclasses
1449 */
1450
1451void Camera3Device::notifyStatus(bool idle) {
1452 {
1453 // Need mLock to safely update state and synchronize to current
1454 // state of methods in flight.
1455 Mutex::Autolock l(mLock);
1456 // We can get various system-idle notices from the status tracker
1457 // while starting up. Only care about them if we've actually sent
1458 // in some requests recently.
1459 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1460 return;
1461 }
1462 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1463 idle ? "idle" : "active");
1464 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1465 mRecentStatusUpdates.add(mStatus);
1466 mStatusChanged.signal();
1467
1468 // Skip notifying listener if we're doing some user-transparent
1469 // state changes
1470 if (mPauseStateNotify) return;
1471 }
1472 NotificationListener *listener;
1473 {
1474 Mutex::Autolock l(mOutputLock);
1475 listener = mListener;
1476 }
1477 if (idle && listener != NULL) {
1478 listener->notifyIdle();
1479 }
1480}
1481
1482/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001483 * Camera3Device private methods
1484 */
1485
1486sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1487 const CameraMetadata &request) {
1488 ATRACE_CALL();
1489 status_t res;
1490
1491 sp<CaptureRequest> newRequest = new CaptureRequest;
1492 newRequest->mSettings = request;
1493
1494 camera_metadata_entry_t inputStreams =
1495 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1496 if (inputStreams.count > 0) {
1497 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001498 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001499 CLOGE("Request references unknown input stream %d",
1500 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001501 return NULL;
1502 }
1503 // Lazy completion of stream configuration (allocation/registration)
1504 // on first use
1505 if (mInputStream->isConfiguring()) {
1506 res = mInputStream->finishConfiguration(mHal3Device);
1507 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001508 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001509 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001510 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001511 return NULL;
1512 }
1513 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001514 // Check if stream is being prepared
1515 if (mInputStream->isPreparing()) {
1516 CLOGE("Request references an input stream that's being prepared!");
1517 return NULL;
1518 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001519
1520 newRequest->mInputStream = mInputStream;
1521 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1522 }
1523
1524 camera_metadata_entry_t streams =
1525 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1526 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001527 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001528 return NULL;
1529 }
1530
1531 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001532 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001533 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001534 CLOGE("Request references unknown stream %d",
1535 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001536 return NULL;
1537 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001538 sp<Camera3OutputStreamInterface> stream =
1539 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001540
1541 // Lazy completion of stream configuration (allocation/registration)
1542 // on first use
1543 if (stream->isConfiguring()) {
1544 res = stream->finishConfiguration(mHal3Device);
1545 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001546 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1547 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001548 return NULL;
1549 }
1550 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001551 // Check if stream is being prepared
1552 if (stream->isPreparing()) {
1553 CLOGE("Request references an output stream that's being prepared!");
1554 return NULL;
1555 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001556
1557 newRequest->mOutputStreams.push(stream);
1558 }
1559 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1560
1561 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001562}
1563
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001564bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1565 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1566 Size size = mSupportedOpaqueInputSizes[i];
1567 if (size.width == width && size.height == height) {
1568 return true;
1569 }
1570 }
1571
1572 return false;
1573}
1574
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001575status_t Camera3Device::configureStreamsLocked() {
1576 ATRACE_CALL();
1577 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001578
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001579 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001580 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001581 return INVALID_OPERATION;
1582 }
1583
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001584 if (!mNeedConfig) {
1585 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1586 return OK;
1587 }
1588
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001589 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1590 // adding a dummy stream instead.
1591 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1592 if (mOutputStreams.size() == 0) {
1593 addDummyStreamLocked();
1594 } else {
1595 tryRemoveDummyStreamLocked();
1596 }
1597
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001598 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001599 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001600
1601 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001602 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1603 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1604 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001605 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1606
1607 Vector<camera3_stream_t*> streams;
1608 streams.setCapacity(config.num_streams);
1609
1610 if (mInputStream != NULL) {
1611 camera3_stream_t *inputStream;
1612 inputStream = mInputStream->startConfiguration();
1613 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001614 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001615 return INVALID_OPERATION;
1616 }
1617 streams.add(inputStream);
1618 }
1619
1620 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001621
1622 // Don't configure bidi streams twice, nor add them twice to the list
1623 if (mOutputStreams[i].get() ==
1624 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1625
1626 config.num_streams--;
1627 continue;
1628 }
1629
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001630 camera3_stream_t *outputStream;
1631 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1632 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001633 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001634 return INVALID_OPERATION;
1635 }
1636 streams.add(outputStream);
1637 }
1638
1639 config.streams = streams.editArray();
1640
1641 // Do the HAL configuration; will potentially touch stream
1642 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001643 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001644 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001645 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001646
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001647 if (res == BAD_VALUE) {
1648 // HAL rejected this set of streams as unsupported, clean up config
1649 // attempt and return to unconfigured state
1650 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1651 res = mInputStream->cancelConfiguration();
1652 if (res != OK) {
1653 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1654 mInputStream->getId(), strerror(-res), res);
1655 return res;
1656 }
1657 }
1658
1659 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1660 sp<Camera3OutputStreamInterface> outputStream =
1661 mOutputStreams.editValueAt(i);
1662 if (outputStream->isConfiguring()) {
1663 res = outputStream->cancelConfiguration();
1664 if (res != OK) {
1665 SET_ERR_L(
1666 "Can't cancel configuring output stream %d: %s (%d)",
1667 outputStream->getId(), strerror(-res), res);
1668 return res;
1669 }
1670 }
1671 }
1672
1673 // Return state to that at start of call, so that future configures
1674 // properly clean things up
1675 mStatus = STATUS_UNCONFIGURED;
1676 mNeedConfig = true;
1677
1678 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1679 return BAD_VALUE;
1680 } else if (res != OK) {
1681 // Some other kind of error from configure_streams - this is not
1682 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001683 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1684 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001685 return res;
1686 }
1687
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001688 // Finish all stream configuration immediately.
1689 // TODO: Try to relax this later back to lazy completion, which should be
1690 // faster
1691
Igor Murashkin073f8572013-05-02 14:59:28 -07001692 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001693 res = mInputStream->finishConfiguration(mHal3Device);
1694 if (res != OK) {
1695 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1696 mInputStream->getId(), strerror(-res), res);
1697 return res;
1698 }
1699 }
1700
1701 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001702 sp<Camera3OutputStreamInterface> outputStream =
1703 mOutputStreams.editValueAt(i);
1704 if (outputStream->isConfiguring()) {
1705 res = outputStream->finishConfiguration(mHal3Device);
1706 if (res != OK) {
1707 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1708 outputStream->getId(), strerror(-res), res);
1709 return res;
1710 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001711 }
1712 }
1713
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001714 // Request thread needs to know to avoid using repeat-last-settings protocol
1715 // across configure_streams() calls
1716 mRequestThread->configurationComplete();
1717
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001718 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001719
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001720 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001721
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001722 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001723 mStatus = STATUS_CONFIGURED;
1724 } else {
1725 mStatus = STATUS_UNCONFIGURED;
1726 }
1727
1728 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1729
Zhijun He0a210512014-07-24 13:45:15 -07001730 // tear down the deleted streams after configure streams.
1731 mDeletedStreams.clear();
1732
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001733 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001734}
1735
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001736status_t Camera3Device::addDummyStreamLocked() {
1737 ATRACE_CALL();
1738 status_t res;
1739
1740 if (mDummyStreamId != NO_STREAM) {
1741 // Should never be adding a second dummy stream when one is already
1742 // active
1743 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1744 __FUNCTION__, mId);
1745 return INVALID_OPERATION;
1746 }
1747
1748 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1749
1750 sp<Camera3OutputStreamInterface> dummyStream =
1751 new Camera3DummyStream(mNextStreamId);
1752
1753 res = mOutputStreams.add(mNextStreamId, dummyStream);
1754 if (res < 0) {
1755 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1756 return res;
1757 }
1758
1759 mDummyStreamId = mNextStreamId;
1760 mNextStreamId++;
1761
1762 return OK;
1763}
1764
1765status_t Camera3Device::tryRemoveDummyStreamLocked() {
1766 ATRACE_CALL();
1767 status_t res;
1768
1769 if (mDummyStreamId == NO_STREAM) return OK;
1770 if (mOutputStreams.size() == 1) return OK;
1771
1772 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1773
1774 // Ok, have a dummy stream and there's at least one other output stream,
1775 // so remove the dummy
1776
1777 sp<Camera3StreamInterface> deletedStream;
1778 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1779 if (outputStreamIdx == NAME_NOT_FOUND) {
1780 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1781 return INVALID_OPERATION;
1782 }
1783
1784 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1785 mOutputStreams.removeItemsAt(outputStreamIdx);
1786
1787 // Free up the stream endpoint so that it can be used by some other stream
1788 res = deletedStream->disconnect();
1789 if (res != OK) {
1790 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1791 // fall through since we want to still list the stream as deleted.
1792 }
1793 mDeletedStreams.add(deletedStream);
1794 mDummyStreamId = NO_STREAM;
1795
1796 return res;
1797}
1798
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001799void Camera3Device::setErrorState(const char *fmt, ...) {
1800 Mutex::Autolock l(mLock);
1801 va_list args;
1802 va_start(args, fmt);
1803
1804 setErrorStateLockedV(fmt, args);
1805
1806 va_end(args);
1807}
1808
1809void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1810 Mutex::Autolock l(mLock);
1811 setErrorStateLockedV(fmt, args);
1812}
1813
1814void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1815 va_list args;
1816 va_start(args, fmt);
1817
1818 setErrorStateLockedV(fmt, args);
1819
1820 va_end(args);
1821}
1822
1823void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001824 // Print out all error messages to log
1825 String8 errorCause = String8::formatV(fmt, args);
1826 ALOGE("Camera %d: %s", mId, errorCause.string());
1827
1828 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001829 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001830
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001831 mErrorCause = errorCause;
1832
1833 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001834 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001835
1836 // Notify upstream about a device error
1837 if (mListener != NULL) {
1838 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1839 CaptureResultExtras());
1840 }
1841
1842 // Save stack trace. View by dumping it later.
1843 CameraTraces::saveTrace();
1844 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001845}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001846
1847/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001848 * In-flight request management
1849 */
1850
Jianing Weicb0652e2014-03-12 18:29:36 -07001851status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07001852 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1853 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001854 ATRACE_CALL();
1855 Mutex::Autolock l(mInFlightLock);
1856
1857 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07001858 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
1859 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001860 if (res < 0) return res;
1861
1862 return OK;
1863}
1864
1865/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001866 * Check if all 3A fields are ready, and send off a partial 3A-only result
1867 * to the output frame queue
1868 */
Zhijun He204e3292014-07-14 17:09:23 -07001869bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001870 uint32_t frameNumber,
1871 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001872
1873 // Check if all 3A states are present
1874 // The full list of fields is
1875 // android.control.afMode
1876 // android.control.awbMode
1877 // android.control.aeState
1878 // android.control.awbState
1879 // android.control.afState
1880 // android.control.afTriggerID
1881 // android.control.aePrecaptureID
1882 // TODO: Add android.control.aeMode
1883
1884 bool gotAllStates = true;
1885
1886 uint8_t afMode;
1887 uint8_t awbMode;
1888 uint8_t aeState;
1889 uint8_t afState;
1890 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001891
1892 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1893 &afMode, frameNumber);
1894
1895 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1896 &awbMode, frameNumber);
1897
1898 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1899 &aeState, frameNumber);
1900
1901 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1902 &afState, frameNumber);
1903
1904 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1905 &awbState, frameNumber);
1906
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001907 if (!gotAllStates) return false;
1908
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001909 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001910 "AF state %d, AE state %d, AWB state %d, "
1911 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001912 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001913 afMode, awbMode,
1914 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001915 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001916
1917 // Got all states, so construct a minimal result to send
1918 // In addition to the above fields, this means adding in
1919 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001920 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001921 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001922
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001923 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001924
1925 Mutex::Autolock l(mOutputLock);
1926
Jianing Weicb0652e2014-03-12 18:29:36 -07001927 CaptureResult captureResult;
1928 captureResult.mResultExtras = resultExtras;
1929 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1930 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1931 // but not limited to CameraDeviceBase::getNextResult
1932 CaptureResult& min3AResult =
1933 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001934
Jianing Weicb0652e2014-03-12 18:29:36 -07001935 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1936 // TODO: This is problematic casting. Need to fix CameraMetadata.
1937 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001938 return false;
1939 }
1940
Jianing Weicb0652e2014-03-12 18:29:36 -07001941 int32_t requestId = resultExtras.requestId;
1942 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001943 &requestId, frameNumber)) {
1944 return false;
1945 }
1946
Zhijun He204e3292014-07-14 17:09:23 -07001947 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1948 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1949 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1950 &partialResult, frameNumber)) {
1951 return false;
1952 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001953 }
1954
Jianing Weicb0652e2014-03-12 18:29:36 -07001955 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001956 &afMode, frameNumber)) {
1957 return false;
1958 }
1959
Jianing Weicb0652e2014-03-12 18:29:36 -07001960 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001961 &awbMode, frameNumber)) {
1962 return false;
1963 }
1964
Jianing Weicb0652e2014-03-12 18:29:36 -07001965 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001966 &aeState, frameNumber)) {
1967 return false;
1968 }
1969
Jianing Weicb0652e2014-03-12 18:29:36 -07001970 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001971 &afState, frameNumber)) {
1972 return false;
1973 }
1974
Jianing Weicb0652e2014-03-12 18:29:36 -07001975 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001976 &awbState, frameNumber)) {
1977 return false;
1978 }
1979
Jianing Weicb0652e2014-03-12 18:29:36 -07001980 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001981 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001982 return false;
1983 }
1984
Jianing Weicb0652e2014-03-12 18:29:36 -07001985 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001986 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001987 return false;
1988 }
1989
Zhijun He204e3292014-07-14 17:09:23 -07001990 // We only send the aggregated partial when all 3A related metadata are available
1991 // For both API1 and API2.
1992 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001993 mResultSignal.signal();
1994
1995 return true;
1996}
1997
1998template<typename T>
1999bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002000 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002001 (void) frameNumber;
2002
2003 camera_metadata_ro_entry_t entry;
2004
2005 entry = result.find(tag);
2006 if (entry.count == 0) {
2007 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2008 mId, frameNumber, get_camera_metadata_tag_name(tag));
2009 return false;
2010 }
2011
2012 if (sizeof(T) == sizeof(uint8_t)) {
2013 *value = entry.data.u8[0];
2014 } else if (sizeof(T) == sizeof(int32_t)) {
2015 *value = entry.data.i32[0];
2016 } else {
2017 ALOGE("%s: Unexpected type", __FUNCTION__);
2018 return false;
2019 }
2020 return true;
2021}
2022
2023template<typename T>
2024bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002025 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002026 if (result.update(tag, value, 1) != NO_ERROR) {
2027 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2028 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2029 frameNumber, get_camera_metadata_tag_name(tag));
2030 return false;
2031 }
2032 return true;
2033}
2034
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002035void Camera3Device::returnOutputBuffers(
2036 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2037 nsecs_t timestamp) {
2038 for (size_t i = 0; i < numBuffers; i++)
2039 {
2040 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2041 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2042 // Note: stream may be deallocated at this point, if this buffer was
2043 // the last reference to it.
2044 if (res != OK) {
2045 ALOGE("Can't return buffer to its stream: %s (%d)",
2046 strerror(-res), res);
2047 }
2048 }
2049}
2050
2051
2052void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2053
2054 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2055 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2056
2057 nsecs_t sensorTimestamp = request.sensorTimestamp;
2058 nsecs_t shutterTimestamp = request.shutterTimestamp;
2059
2060 // Check if it's okay to remove the request from InFlightMap:
2061 // In the case of a successful request:
2062 // all input and output buffers, all result metadata, shutter callback
2063 // arrived.
2064 // In the case of a unsuccessful request:
2065 // all input and output buffers arrived.
2066 if (request.numBuffersLeft == 0 &&
2067 (request.requestStatus != OK ||
2068 (request.haveResultMetadata && shutterTimestamp != 0))) {
2069 ATRACE_ASYNC_END("frame capture", frameNumber);
2070
2071 // Sanity check - if sensor timestamp matches shutter timestamp
2072 if (request.requestStatus == OK &&
2073 sensorTimestamp != shutterTimestamp) {
2074 SET_ERR("sensor timestamp (%" PRId64
2075 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2076 sensorTimestamp, frameNumber, shutterTimestamp);
2077 }
2078
2079 // for an unsuccessful request, it may have pending output buffers to
2080 // return.
2081 assert(request.requestStatus != OK ||
2082 request.pendingOutputBuffers.size() == 0);
2083 returnOutputBuffers(request.pendingOutputBuffers.array(),
2084 request.pendingOutputBuffers.size(), 0);
2085
2086 mInFlightMap.removeItemsAt(idx, 1);
2087
2088 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2089 }
2090
2091 // Sanity check - if we have too many in-flight frames, something has
2092 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002093 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002094 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002095 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2096 kInFlightWarnLimitHighSpeed) {
2097 CLOGE("In-flight list too large for high speed configuration: %zu",
2098 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002099 }
2100}
2101
2102
2103void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2104 CaptureResultExtras &resultExtras,
2105 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002106 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002107 bool reprocess,
2108 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002109 if (pendingMetadata.isEmpty())
2110 return;
2111
2112 Mutex::Autolock l(mOutputLock);
2113
2114 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002115 if (reprocess) {
2116 if (frameNumber < mNextReprocessResultFrameNumber) {
2117 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002118 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002119 frameNumber, mNextReprocessResultFrameNumber);
2120 return;
2121 }
2122 mNextReprocessResultFrameNumber = frameNumber + 1;
2123 } else {
2124 if (frameNumber < mNextResultFrameNumber) {
2125 SET_ERR("Out-of-order capture result metadata submitted! "
2126 "(got frame number %d, expecting %d)",
2127 frameNumber, mNextResultFrameNumber);
2128 return;
2129 }
2130 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002131 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002132
2133 CaptureResult captureResult;
2134 captureResult.mResultExtras = resultExtras;
2135 captureResult.mMetadata = pendingMetadata;
2136
2137 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2138 (int32_t*)&frameNumber, 1) != OK) {
2139 SET_ERR("Failed to set frame# in metadata (%d)",
2140 frameNumber);
2141 return;
2142 } else {
2143 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2144 __FUNCTION__, mId, frameNumber);
2145 }
2146
2147 // Append any previous partials to form a complete result
2148 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2149 captureResult.mMetadata.append(collectedPartialResult);
2150 }
2151
2152 captureResult.mMetadata.sort();
2153
2154 // Check that there's a timestamp in the result metadata
2155 camera_metadata_entry entry =
2156 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2157 if (entry.count == 0) {
2158 SET_ERR("No timestamp provided by HAL for frame %d!",
2159 frameNumber);
2160 return;
2161 }
2162
Chien-Yu Chend196d612015-06-22 19:49:01 -07002163 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2164
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002165 // Valid result, insert into queue
2166 List<CaptureResult>::iterator queuedResult =
2167 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2168 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2169 ", burstId = %" PRId32, __FUNCTION__,
2170 queuedResult->mResultExtras.requestId,
2171 queuedResult->mResultExtras.frameNumber,
2172 queuedResult->mResultExtras.burstId);
2173
2174 mResultSignal.signal();
2175}
2176
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002177/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002178 * Camera HAL device callback methods
2179 */
2180
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002181void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002182 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002183
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002184 status_t res;
2185
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002186 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002187 if (result->result == NULL && result->num_output_buffers == 0 &&
2188 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002189 SET_ERR("No result data provided by HAL for frame %d",
2190 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002191 return;
2192 }
Zhijun He204e3292014-07-14 17:09:23 -07002193
2194 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2195 // partial_result to 1 when metadata is included in this result.
2196 if (!mUsePartialResult &&
2197 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2198 result->result != NULL &&
2199 result->partial_result != 1) {
2200 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2201 " if partial result is not supported",
2202 frameNumber, result->partial_result);
2203 return;
2204 }
2205
2206 bool isPartialResult = false;
2207 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002208 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002209 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002210
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002211 // Get shutter timestamp and resultExtras from list of in-flight requests,
2212 // where it was added by the shutter notification for this frame. If the
2213 // shutter timestamp isn't received yet, append the output buffers to the
2214 // in-flight request and they will be returned when the shutter timestamp
2215 // arrives. Update the in-flight status and remove the in-flight entry if
2216 // all result data and shutter timestamp have been received.
2217 nsecs_t shutterTimestamp = 0;
2218
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002219 {
2220 Mutex::Autolock l(mInFlightLock);
2221 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2222 if (idx == NAME_NOT_FOUND) {
2223 SET_ERR("Unknown frame number for capture result: %d",
2224 frameNumber);
2225 return;
2226 }
2227 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002228 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2229 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2230 ", partialResultCount = %d",
2231 __FUNCTION__, request.resultExtras.requestId,
2232 request.resultExtras.frameNumber, request.resultExtras.burstId,
2233 result->partial_result);
2234 // Always update the partial count to the latest one if it's not 0
2235 // (buffers only). When framework aggregates adjacent partial results
2236 // into one, the latest partial count will be used.
2237 if (result->partial_result != 0)
2238 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002239
2240 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002241 if (mUsePartialResult && result->result != NULL) {
2242 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2243 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2244 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2245 " the range of [1, %d] when metadata is included in the result",
2246 frameNumber, result->partial_result, mNumPartialResults);
2247 return;
2248 }
2249 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002250 if (isPartialResult) {
2251 request.partialResult.collectedResult.append(result->result);
2252 }
Zhijun He204e3292014-07-14 17:09:23 -07002253 } else {
2254 camera_metadata_ro_entry_t partialResultEntry;
2255 res = find_camera_metadata_ro_entry(result->result,
2256 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2257 if (res != NAME_NOT_FOUND &&
2258 partialResultEntry.count > 0 &&
2259 partialResultEntry.data.u8[0] ==
2260 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2261 // A partial result. Flag this as such, and collect this
2262 // set of metadata into the in-flight entry.
2263 isPartialResult = true;
2264 request.partialResult.collectedResult.append(
2265 result->result);
2266 request.partialResult.collectedResult.erase(
2267 ANDROID_QUIRKS_PARTIAL_RESULT);
2268 }
2269 }
2270
2271 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002272 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002273 if (!request.partialResult.haveSent3A) {
2274 request.partialResult.haveSent3A =
2275 processPartial3AResult(frameNumber,
2276 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002277 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002278 }
2279 }
2280 }
2281
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002282 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002283 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002284
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002285 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002286 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002287 if (request.haveResultMetadata) {
2288 SET_ERR("Called multiple times with metadata for frame %d",
2289 frameNumber);
2290 return;
2291 }
Zhijun He204e3292014-07-14 17:09:23 -07002292 if (mUsePartialResult &&
2293 !request.partialResult.collectedResult.isEmpty()) {
2294 collectedPartialResult.acquire(
2295 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002296 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002297 request.haveResultMetadata = true;
2298 }
2299
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002300 uint32_t numBuffersReturned = result->num_output_buffers;
2301 if (result->input_buffer != NULL) {
2302 if (hasInputBufferInRequest) {
2303 numBuffersReturned += 1;
2304 } else {
2305 ALOGW("%s: Input buffer should be NULL if there is no input"
2306 " buffer sent in the request",
2307 __FUNCTION__);
2308 }
2309 }
2310 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002311 if (request.numBuffersLeft < 0) {
2312 SET_ERR("Too many buffers returned for frame %d",
2313 frameNumber);
2314 return;
2315 }
2316
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002317 camera_metadata_ro_entry_t entry;
2318 res = find_camera_metadata_ro_entry(result->result,
2319 ANDROID_SENSOR_TIMESTAMP, &entry);
2320 if (res == OK && entry.count == 1) {
2321 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002322 }
2323
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002324 // If shutter event isn't received yet, append the output buffers to
2325 // the in-flight request. Otherwise, return the output buffers to
2326 // streams.
2327 if (shutterTimestamp == 0) {
2328 request.pendingOutputBuffers.appendArray(result->output_buffers,
2329 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002330 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002331 returnOutputBuffers(result->output_buffers,
2332 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002333 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002334
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002335 if (result->result != NULL && !isPartialResult) {
2336 if (shutterTimestamp == 0) {
2337 request.pendingMetadata = result->result;
2338 request.partialResult.collectedResult = collectedPartialResult;
2339 } else {
2340 CameraMetadata metadata;
2341 metadata = result->result;
2342 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002343 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2344 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002345 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002346 }
2347
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002348 removeInFlightRequestIfReadyLocked(idx);
2349 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002350
Zhijun Hef0d962a2014-06-30 10:24:11 -07002351 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002352 if (hasInputBufferInRequest) {
2353 Camera3Stream *stream =
2354 Camera3Stream::cast(result->input_buffer->stream);
2355 res = stream->returnInputBuffer(*(result->input_buffer));
2356 // Note: stream may be deallocated at this point, if this buffer was the
2357 // last reference to it.
2358 if (res != OK) {
2359 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2360 " its stream:%s (%d)", __FUNCTION__,
2361 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002362 }
2363 } else {
2364 ALOGW("%s: Input buffer should be NULL if there is no input"
2365 " buffer sent in the request, skipping input buffer return.",
2366 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002367 }
2368 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002369}
2370
2371void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002372 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002373 NotificationListener *listener;
2374 {
2375 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002376 listener = mListener;
2377 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002378
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002379 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002380 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002381 return;
2382 }
2383
2384 switch (msg->type) {
2385 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002386 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002387 break;
2388 }
2389 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002390 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002391 break;
2392 }
2393 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002394 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002395 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002396 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002397}
2398
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002399void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2400 NotificationListener *listener) {
2401
2402 // Map camera HAL error codes to ICameraDeviceCallback error codes
2403 // Index into this with the HAL error code
2404 static const ICameraDeviceCallbacks::CameraErrorCode
2405 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2406 // 0 = Unused error code
2407 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2408 // 1 = CAMERA3_MSG_ERROR_DEVICE
2409 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2410 // 2 = CAMERA3_MSG_ERROR_REQUEST
2411 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2412 // 3 = CAMERA3_MSG_ERROR_RESULT
2413 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2414 // 4 = CAMERA3_MSG_ERROR_BUFFER
2415 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2416 };
2417
2418 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2419 ((msg.error_code >= 0) &&
2420 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2421 halErrorMap[msg.error_code] :
2422 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2423
2424 int streamId = 0;
2425 if (msg.error_stream != NULL) {
2426 Camera3Stream *stream =
2427 Camera3Stream::cast(msg.error_stream);
2428 streamId = stream->getId();
2429 }
2430 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2431 mId, __FUNCTION__, msg.frame_number,
2432 streamId, msg.error_code);
2433
2434 CaptureResultExtras resultExtras;
2435 switch (errorCode) {
2436 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2437 // SET_ERR calls notifyError
2438 SET_ERR("Camera HAL reported serious device error");
2439 break;
2440 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2441 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2442 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2443 {
2444 Mutex::Autolock l(mInFlightLock);
2445 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2446 if (idx >= 0) {
2447 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2448 r.requestStatus = msg.error_code;
2449 resultExtras = r.resultExtras;
2450 } else {
2451 resultExtras.frameNumber = msg.frame_number;
2452 ALOGE("Camera %d: %s: cannot find in-flight request on "
2453 "frame %" PRId64 " error", mId, __FUNCTION__,
2454 resultExtras.frameNumber);
2455 }
2456 }
2457 if (listener != NULL) {
2458 listener->notifyError(errorCode, resultExtras);
2459 } else {
2460 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2461 }
2462 break;
2463 default:
2464 // SET_ERR calls notifyError
2465 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2466 break;
2467 }
2468}
2469
2470void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2471 NotificationListener *listener) {
2472 ssize_t idx;
2473 // Verify ordering of shutter notifications
2474 {
2475 Mutex::Autolock l(mOutputLock);
2476 // TODO: need to track errors for tighter bounds on expected frame number.
2477 if (msg.frame_number < mNextShutterFrameNumber) {
2478 SET_ERR("Shutter notification out-of-order. Expected "
2479 "notification for frame %d, got frame %d",
2480 mNextShutterFrameNumber, msg.frame_number);
2481 return;
2482 }
2483 mNextShutterFrameNumber = msg.frame_number + 1;
2484 }
2485
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002486 // Set timestamp for the request in the in-flight tracking
2487 // and get the request ID to send upstream
2488 {
2489 Mutex::Autolock l(mInFlightLock);
2490 idx = mInFlightMap.indexOfKey(msg.frame_number);
2491 if (idx >= 0) {
2492 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002493
2494 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2495 mId, __FUNCTION__,
2496 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2497 // Call listener, if any
2498 if (listener != NULL) {
2499 listener->notifyShutter(r.resultExtras, msg.timestamp);
2500 }
2501
2502 r.shutterTimestamp = msg.timestamp;
2503
2504 // send pending result and buffers
2505 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002506 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002507 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002508 returnOutputBuffers(r.pendingOutputBuffers.array(),
2509 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2510 r.pendingOutputBuffers.clear();
2511
2512 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002513 }
2514 }
2515 if (idx < 0) {
2516 SET_ERR("Shutter notification for non-existent frame number %d",
2517 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002518 }
2519}
2520
2521
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002522CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002523 ALOGV("%s", __FUNCTION__);
2524
Igor Murashkin1e479c02013-09-06 16:55:14 -07002525 CameraMetadata retVal;
2526
2527 if (mRequestThread != NULL) {
2528 retVal = mRequestThread->getLatestRequest();
2529 }
2530
Igor Murashkin1e479c02013-09-06 16:55:14 -07002531 return retVal;
2532}
2533
Jianing Weicb0652e2014-03-12 18:29:36 -07002534
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002535/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002536 * RequestThread inner class methods
2537 */
2538
2539Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002540 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002541 camera3_device_t *hal3Device,
2542 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002543 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002544 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002545 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002546 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002547 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002548 mReconfigured(false),
2549 mDoPause(false),
2550 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002551 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002552 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002553 mCurrentAfTriggerId(0),
2554 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002555 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2556 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002557 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002558}
2559
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002560void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002561 NotificationListener *listener) {
2562 Mutex::Autolock l(mRequestLock);
2563 mListener = listener;
2564}
2565
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002566void Camera3Device::RequestThread::configurationComplete() {
2567 Mutex::Autolock l(mRequestLock);
2568 mReconfigured = true;
2569}
2570
Jianing Wei90e59c92014-03-12 18:29:36 -07002571status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002572 List<sp<CaptureRequest> > &requests,
2573 /*out*/
2574 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002575 Mutex::Autolock l(mRequestLock);
2576 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2577 ++it) {
2578 mRequestQueue.push_back(*it);
2579 }
2580
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002581 if (lastFrameNumber != NULL) {
2582 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2583 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2584 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2585 *lastFrameNumber);
2586 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002587
Jianing Wei90e59c92014-03-12 18:29:36 -07002588 unpauseForNewRequests();
2589
2590 return OK;
2591}
2592
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002593
2594status_t Camera3Device::RequestThread::queueTrigger(
2595 RequestTrigger trigger[],
2596 size_t count) {
2597
2598 Mutex::Autolock l(mTriggerMutex);
2599 status_t ret;
2600
2601 for (size_t i = 0; i < count; ++i) {
2602 ret = queueTriggerLocked(trigger[i]);
2603
2604 if (ret != OK) {
2605 return ret;
2606 }
2607 }
2608
2609 return OK;
2610}
2611
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002612int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2613 sp<Camera3Device> d = device.promote();
2614 if (d != NULL) return d->mId;
2615 return 0;
2616}
2617
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002618status_t Camera3Device::RequestThread::queueTriggerLocked(
2619 RequestTrigger trigger) {
2620
2621 uint32_t tag = trigger.metadataTag;
2622 ssize_t index = mTriggerMap.indexOfKey(tag);
2623
2624 switch (trigger.getTagType()) {
2625 case TYPE_BYTE:
2626 // fall-through
2627 case TYPE_INT32:
2628 break;
2629 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002630 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2631 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002632 return INVALID_OPERATION;
2633 }
2634
2635 /**
2636 * Collect only the latest trigger, since we only have 1 field
2637 * in the request settings per trigger tag, and can't send more than 1
2638 * trigger per request.
2639 */
2640 if (index != NAME_NOT_FOUND) {
2641 mTriggerMap.editValueAt(index) = trigger;
2642 } else {
2643 mTriggerMap.add(tag, trigger);
2644 }
2645
2646 return OK;
2647}
2648
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002649status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002650 const RequestList &requests,
2651 /*out*/
2652 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002653 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002654 if (lastFrameNumber != NULL) {
2655 *lastFrameNumber = mRepeatingLastFrameNumber;
2656 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002657 mRepeatingRequests.clear();
2658 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2659 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002660
2661 unpauseForNewRequests();
2662
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002663 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002664 return OK;
2665}
2666
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002667bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2668 if (mRepeatingRequests.empty()) {
2669 return false;
2670 }
2671 int32_t requestId = requestIn->mResultExtras.requestId;
2672 const RequestList &repeatRequests = mRepeatingRequests;
2673 // All repeating requests are guaranteed to have same id so only check first quest
2674 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2675 return (firstRequest->mResultExtras.requestId == requestId);
2676}
2677
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002678status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002679 Mutex::Autolock l(mRequestLock);
2680 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002681 if (lastFrameNumber != NULL) {
2682 *lastFrameNumber = mRepeatingLastFrameNumber;
2683 }
2684 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002685 return OK;
2686}
2687
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002688status_t Camera3Device::RequestThread::clear(
2689 NotificationListener *listener,
2690 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002691 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002692 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002693
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002694 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002695
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002696 // Send errors for all requests pending in the request queue, including
2697 // pending repeating requests
2698 if (listener != NULL) {
2699 for (RequestList::iterator it = mRequestQueue.begin();
2700 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002701 // Abort the input buffers for reprocess requests.
2702 if ((*it)->mInputStream != NULL) {
2703 camera3_stream_buffer_t inputBuffer;
2704 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2705 if (res != OK) {
2706 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2707 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2708 } else {
2709 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2710 if (res != OK) {
2711 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2712 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2713 }
2714 }
2715 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002716 // Set the frame number this request would have had, if it
2717 // had been submitted; this frame number will not be reused.
2718 // The requestId and burstId fields were set when the request was
2719 // submitted originally (in convertMetadataListToRequestListLocked)
2720 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2721 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2722 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002723 }
2724 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002725 mRequestQueue.clear();
2726 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002727 if (lastFrameNumber != NULL) {
2728 *lastFrameNumber = mRepeatingLastFrameNumber;
2729 }
2730 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002731 return OK;
2732}
2733
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002734void Camera3Device::RequestThread::setPaused(bool paused) {
2735 Mutex::Autolock l(mPauseLock);
2736 mDoPause = paused;
2737 mDoPauseSignal.signal();
2738}
2739
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002740status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2741 int32_t requestId, nsecs_t timeout) {
2742 Mutex::Autolock l(mLatestRequestMutex);
2743 status_t res;
2744 while (mLatestRequestId != requestId) {
2745 nsecs_t startTime = systemTime();
2746
2747 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2748 if (res != OK) return res;
2749
2750 timeout -= (systemTime() - startTime);
2751 }
2752
2753 return OK;
2754}
2755
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002756void Camera3Device::RequestThread::requestExit() {
2757 // Call parent to set up shutdown
2758 Thread::requestExit();
2759 // The exit from any possible waits
2760 mDoPauseSignal.signal();
2761 mRequestSignal.signal();
2762}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002763
Chien-Yu Chend196d612015-06-22 19:49:01 -07002764
2765/**
2766 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2767 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2768 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2769 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2770 * request.
2771 */
2772void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2773 request->mAeTriggerCancelOverride.applyAeLock = false;
2774 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2775
2776 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2777 return;
2778 }
2779
2780 camera_metadata_entry_t aePrecaptureTrigger =
2781 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2782 if (aePrecaptureTrigger.count > 0 &&
2783 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2784 // Always override CANCEL to IDLE
2785 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2786 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2787 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2788 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2789 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2790
2791 if (mAeLockAvailable == true) {
2792 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2793 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2794 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2795 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2796 request->mAeTriggerCancelOverride.applyAeLock = true;
2797 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2798 }
2799 }
2800 }
2801}
2802
2803/**
2804 * Override result metadata for cancelling AE precapture trigger applied in
2805 * handleAePrecaptureCancelRequest().
2806 */
2807void Camera3Device::overrideResultForPrecaptureCancel(
2808 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2809 if (aeTriggerCancelOverride.applyAeLock) {
2810 // Only devices <= v3.2 should have this override
2811 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2812 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2813 }
2814
2815 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2816 // Only devices <= v3.2 should have this override
2817 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2818 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2819 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2820 }
2821}
2822
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002823bool Camera3Device::RequestThread::threadLoop() {
2824
2825 status_t res;
2826
2827 // Handle paused state.
2828 if (waitIfPaused()) {
2829 return true;
2830 }
2831
2832 // Get work to do
2833
2834 sp<CaptureRequest> nextRequest = waitForNextRequest();
2835 if (nextRequest == NULL) {
2836 return true;
2837 }
2838
2839 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002840 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002841 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002842 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002843
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002844 // Get the request ID, if any
2845 int requestId;
2846 camera_metadata_entry_t requestIdEntry =
2847 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2848 if (requestIdEntry.count > 0) {
2849 requestId = requestIdEntry.data.i32[0];
2850 } else {
2851 ALOGW("%s: Did not have android.request.id set in the request",
2852 __FUNCTION__);
2853 requestId = NAME_NOT_FOUND;
2854 }
2855
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002856 // Insert any queued triggers (before metadata is locked)
2857 int32_t triggerCount;
2858 res = insertTriggers(nextRequest);
2859 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002860 SET_ERR("RequestThread: Unable to insert triggers "
2861 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002862 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002863 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2864 return false;
2865 }
2866 triggerCount = res;
2867
2868 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2869
2870 // If the request is the same as last, or we had triggers last time
2871 if (mPrevRequest != nextRequest || triggersMixedIn) {
2872 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002873 * HAL workaround:
2874 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2875 */
2876 res = addDummyTriggerIds(nextRequest);
2877 if (res != OK) {
2878 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2879 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002880 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002881 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2882 return false;
2883 }
2884
2885 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002886 * The request should be presorted so accesses in HAL
2887 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2888 */
2889 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002890 request.settings = nextRequest->mSettings.getAndLock();
2891 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002892 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2893
2894 IF_ALOGV() {
2895 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2896 find_camera_metadata_ro_entry(
2897 request.settings,
2898 ANDROID_CONTROL_AF_TRIGGER,
2899 &e
2900 );
2901 if (e.count > 0) {
2902 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2903 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002904 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002905 e.data.u8[0]);
2906 }
2907 }
2908 } else {
2909 // leave request.settings NULL to indicate 'reuse latest given'
2910 ALOGVV("%s: Request settings are REUSED",
2911 __FUNCTION__);
2912 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002913
Zhijun Hef0d962a2014-06-30 10:24:11 -07002914 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002915
2916 // Fill in buffers
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002917 if (nextRequest->mInputStream != NULL) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002918 request.input_buffer = &nextRequest->mInputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002919 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002920 } else {
2921 request.input_buffer = NULL;
2922 }
2923
2924 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2925 nextRequest->mOutputStreams.size());
2926 request.output_buffers = outputBuffers.array();
2927 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2928 res = nextRequest->mOutputStreams.editItemAt(i)->
2929 getBuffer(&outputBuffers.editItemAt(i));
2930 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002931 // Can't get output buffer from gralloc queue - this could be due to
2932 // abandoned queue or other consumer misbehavior, so not a fatal
2933 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002934 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2935 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala0ec23d32015-05-28 15:15:54 -07002936 {
2937 Mutex::Autolock l(mRequestLock);
2938 if (mListener != NULL) {
2939 mListener->notifyError(
2940 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2941 nextRequest->mResultExtras);
2942 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002943 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002944 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2945 return true;
2946 }
2947 request.num_output_buffers++;
2948 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002949 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002950
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002951 // Log request in the in-flight queue
2952 sp<Camera3Device> parent = mParent.promote();
2953 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002954 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002955 CLOGE("RequestThread: Parent is gone");
2956 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2957 return false;
2958 }
2959
Jianing Weicb0652e2014-03-12 18:29:36 -07002960 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002961 totalNumBuffers, nextRequest->mResultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002962 /*hasInput*/request.input_buffer != NULL,
2963 nextRequest->mAeTriggerCancelOverride);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002964 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2965 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002966 __FUNCTION__,
2967 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2968 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002969 if (res != OK) {
2970 SET_ERR("RequestThread: Unable to register new in-flight request:"
2971 " %s (%d)", strerror(-res), res);
2972 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2973 return false;
2974 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002975
Zhijun Hecc27e112013-10-03 16:12:43 -07002976 // Inform waitUntilRequestProcessed thread of a new request ID
2977 {
2978 Mutex::Autolock al(mLatestRequestMutex);
2979
2980 mLatestRequestId = requestId;
2981 mLatestRequestSignal.signal();
2982 }
2983
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002984 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002985 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2986 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002987 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002988 ATRACE_END();
2989
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002990 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002991 // Should only get a failure here for malformed requests or device-level
2992 // errors, so consider all errors fatal. Bad metadata failures should
2993 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002994 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002995 " device: %s (%d)", request.frame_number, strerror(-res), res);
2996 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2997 return false;
2998 }
2999
Igor Murashkin1e479c02013-09-06 16:55:14 -07003000 // Update the latest request sent to HAL
3001 if (request.settings != NULL) { // Don't update them if they were unchanged
3002 Mutex::Autolock al(mLatestRequestMutex);
3003
3004 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
3005 mLatestRequest.acquire(cloned);
3006 }
3007
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003008 if (request.settings != NULL) {
3009 nextRequest->mSettings.unlock(request.settings);
3010 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003011
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003012 // Unset as current request
3013 {
3014 Mutex::Autolock l(mRequestLock);
3015 mNextRequest.clear();
3016 }
3017
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003018 // Remove any previously queued triggers (after unlock)
3019 res = removeTriggers(mPrevRequest);
3020 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003021 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003022 "(capture request %d, HAL device: %s (%d)",
3023 request.frame_number, strerror(-res), res);
3024 return false;
3025 }
3026 mPrevTriggers = triggerCount;
3027
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003028 return true;
3029}
3030
Igor Murashkin1e479c02013-09-06 16:55:14 -07003031CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3032 Mutex::Autolock al(mLatestRequestMutex);
3033
3034 ALOGV("RequestThread::%s", __FUNCTION__);
3035
3036 return mLatestRequest;
3037}
3038
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003039bool Camera3Device::RequestThread::isStreamPending(
3040 sp<Camera3StreamInterface>& stream) {
3041 Mutex::Autolock l(mRequestLock);
3042
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003043 if (mNextRequest != nullptr) {
3044 for (const auto& s : mNextRequest->mOutputStreams) {
3045 if (stream == s) return true;
3046 }
3047 if (stream == mNextRequest->mInputStream) return true;
3048 }
3049
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003050 for (const auto& request : mRequestQueue) {
3051 for (const auto& s : request->mOutputStreams) {
3052 if (stream == s) return true;
3053 }
3054 if (stream == request->mInputStream) return true;
3055 }
3056
3057 for (const auto& request : mRepeatingRequests) {
3058 for (const auto& s : request->mOutputStreams) {
3059 if (stream == s) return true;
3060 }
3061 if (stream == request->mInputStream) return true;
3062 }
3063
3064 return false;
3065}
Jianing Weicb0652e2014-03-12 18:29:36 -07003066
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003067void Camera3Device::RequestThread::cleanUpFailedRequest(
3068 camera3_capture_request_t &request,
3069 sp<CaptureRequest> &nextRequest,
3070 Vector<camera3_stream_buffer_t> &outputBuffers) {
3071
3072 if (request.settings != NULL) {
3073 nextRequest->mSettings.unlock(request.settings);
3074 }
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003075 if (nextRequest->mInputStream != NULL) {
3076 nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3077 nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003078 }
3079 for (size_t i = 0; i < request.num_output_buffers; i++) {
3080 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3081 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
3082 outputBuffers[i], 0);
3083 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003084
3085 Mutex::Autolock l(mRequestLock);
3086 mNextRequest.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003087}
3088
3089sp<Camera3Device::CaptureRequest>
3090 Camera3Device::RequestThread::waitForNextRequest() {
3091 status_t res;
3092 sp<CaptureRequest> nextRequest;
3093
3094 // Optimized a bit for the simple steady-state case (single repeating
3095 // request), to avoid putting that request in the queue temporarily.
3096 Mutex::Autolock l(mRequestLock);
3097
3098 while (mRequestQueue.empty()) {
3099 if (!mRepeatingRequests.empty()) {
3100 // Always atomically enqueue all requests in a repeating request
3101 // list. Guarantees a complete in-sequence set of captures to
3102 // application.
3103 const RequestList &requests = mRepeatingRequests;
3104 RequestList::const_iterator firstRequest =
3105 requests.begin();
3106 nextRequest = *firstRequest;
3107 mRequestQueue.insert(mRequestQueue.end(),
3108 ++firstRequest,
3109 requests.end());
3110 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003111
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003112 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003113
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003114 break;
3115 }
3116
3117 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3118
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003119 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3120 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003121 Mutex::Autolock pl(mPauseLock);
3122 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003123 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003124 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003125 // Let the tracker know
3126 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3127 if (statusTracker != 0) {
3128 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3129 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003130 }
3131 // Stop waiting for now and let thread management happen
3132 return NULL;
3133 }
3134 }
3135
3136 if (nextRequest == NULL) {
3137 // Don't have a repeating request already in hand, so queue
3138 // must have an entry now.
3139 RequestList::iterator firstRequest =
3140 mRequestQueue.begin();
3141 nextRequest = *firstRequest;
3142 mRequestQueue.erase(firstRequest);
3143 }
3144
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003145 // In case we've been unpaused by setPaused clearing mDoPause, need to
3146 // update internal pause state (capture/setRepeatingRequest unpause
3147 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003148 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003149 if (mPaused) {
3150 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3151 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3152 if (statusTracker != 0) {
3153 statusTracker->markComponentActive(mStatusId);
3154 }
3155 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003156 mPaused = false;
3157
3158 // Check if we've reconfigured since last time, and reset the preview
3159 // request if so. Can't use 'NULL request == repeat' across configure calls.
3160 if (mReconfigured) {
3161 mPrevRequest.clear();
3162 mReconfigured = false;
3163 }
3164
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003165 if (nextRequest != NULL) {
3166 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003167 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3168 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003169
3170 // Since RequestThread::clear() removes buffers from the input stream,
3171 // get the right buffer here before unlocking mRequestLock
3172 if (nextRequest->mInputStream != NULL) {
3173 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3174 if (res != OK) {
3175 // Can't get input buffer from gralloc queue - this could be due to
3176 // disconnected queue or other producer misbehavior, so not a fatal
3177 // error
3178 ALOGE("%s: Can't get input buffer, skipping request:"
3179 " %s (%d)", __FUNCTION__, strerror(-res), res);
3180 if (mListener != NULL) {
3181 mListener->notifyError(
3182 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3183 nextRequest->mResultExtras);
3184 }
3185 return NULL;
3186 }
3187 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003188 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003189
3190 handleAePrecaptureCancelRequest(nextRequest);
3191
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003192 mNextRequest = nextRequest;
3193
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003194 return nextRequest;
3195}
3196
3197bool Camera3Device::RequestThread::waitIfPaused() {
3198 status_t res;
3199 Mutex::Autolock l(mPauseLock);
3200 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003201 if (mPaused == false) {
3202 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003203 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3204 // Let the tracker know
3205 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3206 if (statusTracker != 0) {
3207 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3208 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003209 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003210
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003211 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003212 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003213 return true;
3214 }
3215 }
3216 // We don't set mPaused to false here, because waitForNextRequest needs
3217 // to further manage the paused state in case of starvation.
3218 return false;
3219}
3220
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003221void Camera3Device::RequestThread::unpauseForNewRequests() {
3222 // With work to do, mark thread as unpaused.
3223 // If paused by request (setPaused), don't resume, to avoid
3224 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003225 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003226 Mutex::Autolock p(mPauseLock);
3227 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003228 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3229 if (mPaused) {
3230 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3231 if (statusTracker != 0) {
3232 statusTracker->markComponentActive(mStatusId);
3233 }
3234 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003235 mPaused = false;
3236 }
3237}
3238
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003239void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3240 sp<Camera3Device> parent = mParent.promote();
3241 if (parent != NULL) {
3242 va_list args;
3243 va_start(args, fmt);
3244
3245 parent->setErrorStateV(fmt, args);
3246
3247 va_end(args);
3248 }
3249}
3250
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003251status_t Camera3Device::RequestThread::insertTriggers(
3252 const sp<CaptureRequest> &request) {
3253
3254 Mutex::Autolock al(mTriggerMutex);
3255
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003256 sp<Camera3Device> parent = mParent.promote();
3257 if (parent == NULL) {
3258 CLOGE("RequestThread: Parent is gone");
3259 return DEAD_OBJECT;
3260 }
3261
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003262 CameraMetadata &metadata = request->mSettings;
3263 size_t count = mTriggerMap.size();
3264
3265 for (size_t i = 0; i < count; ++i) {
3266 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003267 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003268
3269 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3270 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3271 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003272 if (isAeTrigger) {
3273 request->mResultExtras.precaptureTriggerId = triggerId;
3274 mCurrentPreCaptureTriggerId = triggerId;
3275 } else {
3276 request->mResultExtras.afTriggerId = triggerId;
3277 mCurrentAfTriggerId = triggerId;
3278 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003279 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3280 continue; // Trigger ID tag is deprecated since device HAL 3.2
3281 }
3282 }
3283
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003284 camera_metadata_entry entry = metadata.find(tag);
3285
3286 if (entry.count > 0) {
3287 /**
3288 * Already has an entry for this trigger in the request.
3289 * Rewrite it with our requested trigger value.
3290 */
3291 RequestTrigger oldTrigger = trigger;
3292
3293 oldTrigger.entryValue = entry.data.u8[0];
3294
3295 mTriggerReplacedMap.add(tag, oldTrigger);
3296 } else {
3297 /**
3298 * More typical, no trigger entry, so we just add it
3299 */
3300 mTriggerRemovedMap.add(tag, trigger);
3301 }
3302
3303 status_t res;
3304
3305 switch (trigger.getTagType()) {
3306 case TYPE_BYTE: {
3307 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3308 res = metadata.update(tag,
3309 &entryValue,
3310 /*count*/1);
3311 break;
3312 }
3313 case TYPE_INT32:
3314 res = metadata.update(tag,
3315 &trigger.entryValue,
3316 /*count*/1);
3317 break;
3318 default:
3319 ALOGE("%s: Type not supported: 0x%x",
3320 __FUNCTION__,
3321 trigger.getTagType());
3322 return INVALID_OPERATION;
3323 }
3324
3325 if (res != OK) {
3326 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3327 ", value %d", __FUNCTION__, trigger.getTagName(),
3328 trigger.entryValue);
3329 return res;
3330 }
3331
3332 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3333 trigger.getTagName(),
3334 trigger.entryValue);
3335 }
3336
3337 mTriggerMap.clear();
3338
3339 return count;
3340}
3341
3342status_t Camera3Device::RequestThread::removeTriggers(
3343 const sp<CaptureRequest> &request) {
3344 Mutex::Autolock al(mTriggerMutex);
3345
3346 CameraMetadata &metadata = request->mSettings;
3347
3348 /**
3349 * Replace all old entries with their old values.
3350 */
3351 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3352 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3353
3354 status_t res;
3355
3356 uint32_t tag = trigger.metadataTag;
3357 switch (trigger.getTagType()) {
3358 case TYPE_BYTE: {
3359 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3360 res = metadata.update(tag,
3361 &entryValue,
3362 /*count*/1);
3363 break;
3364 }
3365 case TYPE_INT32:
3366 res = metadata.update(tag,
3367 &trigger.entryValue,
3368 /*count*/1);
3369 break;
3370 default:
3371 ALOGE("%s: Type not supported: 0x%x",
3372 __FUNCTION__,
3373 trigger.getTagType());
3374 return INVALID_OPERATION;
3375 }
3376
3377 if (res != OK) {
3378 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3379 ", trigger value %d", __FUNCTION__,
3380 trigger.getTagName(), trigger.entryValue);
3381 return res;
3382 }
3383 }
3384 mTriggerReplacedMap.clear();
3385
3386 /**
3387 * Remove all new entries.
3388 */
3389 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3390 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3391 status_t res = metadata.erase(trigger.metadataTag);
3392
3393 if (res != OK) {
3394 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3395 ", trigger value %d", __FUNCTION__,
3396 trigger.getTagName(), trigger.entryValue);
3397 return res;
3398 }
3399 }
3400 mTriggerRemovedMap.clear();
3401
3402 return OK;
3403}
3404
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003405status_t Camera3Device::RequestThread::addDummyTriggerIds(
3406 const sp<CaptureRequest> &request) {
3407 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3408 static const int32_t dummyTriggerId = 1;
3409 status_t res;
3410
3411 CameraMetadata &metadata = request->mSettings;
3412
3413 // If AF trigger is active, insert a dummy AF trigger ID if none already
3414 // exists
3415 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3416 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3417 if (afTrigger.count > 0 &&
3418 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3419 afId.count == 0) {
3420 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3421 if (res != OK) return res;
3422 }
3423
3424 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3425 // if none already exists
3426 camera_metadata_entry pcTrigger =
3427 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3428 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3429 if (pcTrigger.count > 0 &&
3430 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3431 pcId.count == 0) {
3432 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3433 &dummyTriggerId, 1);
3434 if (res != OK) return res;
3435 }
3436
3437 return OK;
3438}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003439
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003440/**
3441 * PreparerThread inner class methods
3442 */
3443
3444Camera3Device::PreparerThread::PreparerThread() :
3445 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3446}
3447
3448Camera3Device::PreparerThread::~PreparerThread() {
3449 Thread::requestExitAndWait();
3450 if (mCurrentStream != nullptr) {
3451 mCurrentStream->cancelPrepare();
3452 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3453 mCurrentStream.clear();
3454 }
3455 clear();
3456}
3457
3458status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) {
3459 status_t res;
3460
3461 Mutex::Autolock l(mLock);
3462
3463 res = stream->startPrepare();
3464 if (res == OK) {
3465 // No preparation needed, fire listener right off
3466 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3467 if (mListener) {
3468 mListener->notifyPrepared(stream->getId());
3469 }
3470 return OK;
3471 } else if (res != NOT_ENOUGH_DATA) {
3472 return res;
3473 }
3474
3475 // Need to prepare, start up thread if necessary
3476 if (!mActive) {
3477 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3478 // isn't running
3479 Thread::requestExitAndWait();
3480 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3481 if (res != OK) {
3482 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3483 if (mListener) {
3484 mListener->notifyPrepared(stream->getId());
3485 }
3486 return res;
3487 }
3488 mCancelNow = false;
3489 mActive = true;
3490 ALOGV("%s: Preparer stream started", __FUNCTION__);
3491 }
3492
3493 // queue up the work
3494 mPendingStreams.push_back(stream);
3495 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3496
3497 return OK;
3498}
3499
3500status_t Camera3Device::PreparerThread::clear() {
3501 status_t res;
3502
3503 Mutex::Autolock l(mLock);
3504
3505 for (const auto& stream : mPendingStreams) {
3506 stream->cancelPrepare();
3507 }
3508 mPendingStreams.clear();
3509 mCancelNow = true;
3510
3511 return OK;
3512}
3513
3514void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3515 Mutex::Autolock l(mLock);
3516 mListener = listener;
3517}
3518
3519bool Camera3Device::PreparerThread::threadLoop() {
3520 status_t res;
3521 {
3522 Mutex::Autolock l(mLock);
3523 if (mCurrentStream == nullptr) {
3524 // End thread if done with work
3525 if (mPendingStreams.empty()) {
3526 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3527 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3528 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3529 mActive = false;
3530 return false;
3531 }
3532
3533 // Get next stream to prepare
3534 auto it = mPendingStreams.begin();
3535 mCurrentStream = *it;
3536 mPendingStreams.erase(it);
3537 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3538 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3539 } else if (mCancelNow) {
3540 mCurrentStream->cancelPrepare();
3541 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3542 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3543 mCurrentStream.clear();
3544 mCancelNow = false;
3545 return true;
3546 }
3547 }
3548
3549 res = mCurrentStream->prepareNextBuffer();
3550 if (res == NOT_ENOUGH_DATA) return true;
3551 if (res != OK) {
3552 // Something bad happened; try to recover by cancelling prepare and
3553 // signalling listener anyway
3554 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3555 mCurrentStream->getId(), res, strerror(-res));
3556 mCurrentStream->cancelPrepare();
3557 }
3558
3559 // This stream has finished, notify listener
3560 Mutex::Autolock l(mLock);
3561 if (mListener) {
3562 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3563 mCurrentStream->getId());
3564 mListener->notifyPrepared(mCurrentStream->getId());
3565 }
3566
3567 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3568 mCurrentStream.clear();
3569
3570 return true;
3571}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003572
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003573/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003574 * Static callback forwarding methods from HAL to instance
3575 */
3576
3577void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3578 const camera3_capture_result *result) {
3579 Camera3Device *d =
3580 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003581
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003582 d->processCaptureResult(result);
3583}
3584
3585void Camera3Device::sNotify(const camera3_callback_ops *cb,
3586 const camera3_notify_msg *msg) {
3587 Camera3Device *d =
3588 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3589 d->notify(msg);
3590}
3591
3592}; // namespace android