blob: 8b43154b71063d9e29748df9e0e3165f727b61ab [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
Chien-Yu Chen116a1892016-03-09 12:21:01 -080046#include "CameraService.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070047#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070048#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070049#include "device3/Camera3Device.h"
50#include "device3/Camera3OutputStream.h"
51#include "device3/Camera3InputStream.h"
52#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070053#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070054#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080055
56using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080057
58namespace android {
59
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080060Camera3Device::Camera3Device(int id):
61 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070062 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080063 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070064 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070065 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070066 mUsePartialResult(false),
67 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070068 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070069 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070070 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070071 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070072 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080073{
74 ATRACE_CALL();
75 camera3_callback_ops::notify = &sNotify;
76 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
77 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
78}
79
80Camera3Device::~Camera3Device()
81{
82 ATRACE_CALL();
83 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
84 disconnect();
85}
86
Igor Murashkin71381052013-03-04 14:53:08 -080087int Camera3Device::getId() const {
88 return mId;
89}
90
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080091/**
92 * CameraDeviceBase interface
93 */
94
Yin-Chia Yehe074a932015-01-30 10:29:02 -080095status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080096{
97 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070098 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080099 Mutex::Autolock l(mLock);
100
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800101 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800102 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700103 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800104 return INVALID_OPERATION;
105 }
106
107 /** Open HAL device */
108
109 status_t res;
110 String8 deviceName = String8::format("%d", mId);
111
112 camera3_device_t *device;
113
Zhijun He213ce792013-11-19 08:45:15 -0800114 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800115 res = module->open(deviceName.string(),
116 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800117 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800118
119 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700120 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800121 return res;
122 }
123
124 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700125 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700126 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700127 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700128 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800129 device->common.version);
130 device->common.close(&device->common);
131 return BAD_VALUE;
132 }
133
134 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800135 res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700136 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800137 if (res != OK) return res;
138
139 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700140 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
141 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700142 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800143 device->common.close(&device->common);
144 return BAD_VALUE;
145 }
146
147 /** Initialize device with callback functions */
148
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700149 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800150 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700151 ATRACE_END();
152
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800153 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700154 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
155 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800156 device->common.close(&device->common);
157 return BAD_VALUE;
158 }
159
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700160 /** Start up status tracker thread */
161 mStatusTracker = new StatusTracker(this);
162 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
163 if (res != OK) {
164 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
165 strerror(-res), res);
166 device->common.close(&device->common);
167 mStatusTracker.clear();
168 return res;
169 }
170
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700171 bool aeLockAvailable = false;
172 camera_metadata_ro_entry aeLockAvailableEntry;
173 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
174 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
175 if (res == OK && aeLockAvailableEntry.count > 0) {
176 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
177 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
178 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800179
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700180 /** Start up request queue thread */
181 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800182 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800183 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700184 SET_ERR_L("Unable to start request queue thread: %s (%d)",
185 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800186 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800187 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800188 return res;
189 }
190
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700191 mPreparerThread = new PreparerThread();
192
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800193 /** Everything is good to go */
194
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700195 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800196 mDeviceInfo = info.static_camera_characteristics;
197 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700198
199 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800200 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700201 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700202 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700203 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800204
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700205 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700206 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
207 camera_metadata_entry partialResultsCount =
208 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
209 if (partialResultsCount.count > 0) {
210 mNumPartialResults = partialResultsCount.data.i32[0];
211 mUsePartialResult = (mNumPartialResults > 1);
212 }
213 } else {
214 camera_metadata_entry partialResultsQuirk =
215 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
216 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
217 mUsePartialResult = true;
218 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700219 }
220
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700221 camera_metadata_entry configs =
222 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
223 for (uint32_t i = 0; i < configs.count; i += 4) {
224 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
225 configs.data.i32[i + 3] ==
226 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
227 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
228 configs.data.i32[i + 2]));
229 }
230 }
231
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800232 return OK;
233}
234
235status_t Camera3Device::disconnect() {
236 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700237 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800238
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800239 ALOGV("%s: E", __FUNCTION__);
240
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700241 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800242
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700243 {
244 Mutex::Autolock l(mLock);
245 if (mStatus == STATUS_UNINITIALIZED) return res;
246
247 if (mStatus == STATUS_ACTIVE ||
248 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
249 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700250 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700251 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700252 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700253 } else {
254 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
255 if (res != OK) {
256 SET_ERR_L("Timeout waiting for HAL to drain");
257 // Continue to close device even in case of error
258 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700259 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800260 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800261
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700262 if (mStatus == STATUS_ERROR) {
263 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700264 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700265
266 if (mStatusTracker != NULL) {
267 mStatusTracker->requestExit();
268 }
269
270 if (mRequestThread != NULL) {
271 mRequestThread->requestExit();
272 }
273
274 mOutputStreams.clear();
275 mInputStream.clear();
276 }
277
278 // Joining done without holding mLock, otherwise deadlocks may ensue
279 // as the threads try to access parent state
280 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
281 // HAL may be in a bad state, so waiting for request thread
282 // (which may be stuck in the HAL processCaptureRequest call)
283 // could be dangerous.
284 mRequestThread->join();
285 }
286
287 if (mStatusTracker != NULL) {
288 mStatusTracker->join();
289 }
290
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700291 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700292 {
293 Mutex::Autolock l(mLock);
294
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800295 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700296 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800297
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700298 hal3Device = mHal3Device;
299 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800300
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700301 // Call close without internal mutex held, as the HAL close may need to
302 // wait on assorted callbacks,etc, to complete before it can return.
303 if (hal3Device != NULL) {
304 ATRACE_BEGIN("camera3->close");
305 hal3Device->common.close(&hal3Device->common);
306 ATRACE_END();
307 }
308
309 {
310 Mutex::Autolock l(mLock);
311 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700312 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700313 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800314
315 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700316 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800317}
318
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700319// For dumping/debugging only -
320// try to acquire a lock a few times, eventually give up to proceed with
321// debug/dump operations
322bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
323 bool gotLock = false;
324 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
325 if (lock.tryLock() == NO_ERROR) {
326 gotLock = true;
327 break;
328 } else {
329 usleep(kDumpSleepDuration);
330 }
331 }
332 return gotLock;
333}
334
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700335Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
336 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
337 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
338 const int STREAM_CONFIGURATION_SIZE = 4;
339 const int STREAM_FORMAT_OFFSET = 0;
340 const int STREAM_WIDTH_OFFSET = 1;
341 const int STREAM_HEIGHT_OFFSET = 2;
342 const int STREAM_IS_INPUT_OFFSET = 3;
343 camera_metadata_ro_entry_t availableStreamConfigs =
344 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
345 if (availableStreamConfigs.count == 0 ||
346 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
347 return Size(0, 0);
348 }
349
350 // Get max jpeg size (area-wise).
351 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
352 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
353 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
354 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
355 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
356 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
357 && format == HAL_PIXEL_FORMAT_BLOB &&
358 (width * height > maxJpegWidth * maxJpegHeight)) {
359 maxJpegWidth = width;
360 maxJpegHeight = height;
361 }
362 }
363 } else {
364 camera_metadata_ro_entry availableJpegSizes =
365 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
366 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
367 return Size(0, 0);
368 }
369
370 // Get max jpeg size (area-wise).
371 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
372 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
373 > (maxJpegWidth * maxJpegHeight)) {
374 maxJpegWidth = availableJpegSizes.data.i32[i];
375 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
376 }
377 }
378 }
379 return Size(maxJpegWidth, maxJpegHeight);
380}
381
Zhijun Hef7da0962014-04-24 13:27:56 -0700382ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700383 // Get max jpeg size (area-wise).
384 Size maxJpegResolution = getMaxJpegResolution();
385 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700386 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700387 __FUNCTION__, mId);
388 return BAD_VALUE;
389 }
390
Zhijun Hef7da0962014-04-24 13:27:56 -0700391 // Get max jpeg buffer size
392 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700393 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
394 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700395 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
396 return BAD_VALUE;
397 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700398 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800399 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700400
401 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700402 float scaleFactor = ((float) (width * height)) /
403 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800404 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
405 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700406 if (jpegBufferSize > maxJpegBufferSize) {
407 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700408 }
409
410 return jpegBufferSize;
411}
412
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700413ssize_t Camera3Device::getPointCloudBufferSize() const {
414 const int FLOATS_PER_POINT=4;
415 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
416 if (maxPointCount.count == 0) {
417 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
418 __FUNCTION__, mId);
419 return BAD_VALUE;
420 }
421 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
422 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
423 return maxBytesForPointCloud;
424}
425
426
427
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800428status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
429 ATRACE_CALL();
430 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700431
432 // Try to lock, but continue in case of failure (to avoid blocking in
433 // deadlocks)
434 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
435 bool gotLock = tryLockSpinRightRound(mLock);
436
437 ALOGW_IF(!gotInterfaceLock,
438 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
439 mId, __FUNCTION__);
440 ALOGW_IF(!gotLock,
441 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
442 mId, __FUNCTION__);
443
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800444 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800445
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800446 const char *status =
447 mStatus == STATUS_ERROR ? "ERROR" :
448 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700449 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
450 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800451 mStatus == STATUS_ACTIVE ? "ACTIVE" :
452 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700453
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800454 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700455 if (mStatus == STATUS_ERROR) {
456 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
457 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800458 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700459 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700460 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800461
462 if (mInputStream != NULL) {
463 write(fd, lines.string(), lines.size());
464 mInputStream->dump(fd, args);
465 } else {
466 lines.appendFormat(" No input stream.\n");
467 write(fd, lines.string(), lines.size());
468 }
469 for (size_t i = 0; i < mOutputStreams.size(); i++) {
470 mOutputStreams[i]->dump(fd,args);
471 }
472
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700473 lines = String8(" In-flight requests:\n");
474 if (mInFlightMap.size() == 0) {
475 lines.append(" None\n");
476 } else {
477 for (size_t i = 0; i < mInFlightMap.size(); i++) {
478 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700479 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700480 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800481 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700482 r.numBuffersLeft);
483 }
484 }
485 write(fd, lines.string(), lines.size());
486
Igor Murashkin1e479c02013-09-06 16:55:14 -0700487 {
488 lines = String8(" Last request sent:\n");
489 write(fd, lines.string(), lines.size());
490
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700491 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700492 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
493 }
494
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800495 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700496 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800497 write(fd, lines.string(), lines.size());
498 mHal3Device->ops->dump(mHal3Device, fd);
499 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800500
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700501 if (gotLock) mLock.unlock();
502 if (gotInterfaceLock) mInterfaceLock.unlock();
503
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800504 return OK;
505}
506
507const CameraMetadata& Camera3Device::info() const {
508 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800509 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
510 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700511 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800512 mStatus == STATUS_ERROR ?
513 "when in error state" : "before init");
514 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800515 return mDeviceInfo;
516}
517
Jianing Wei90e59c92014-03-12 18:29:36 -0700518status_t Camera3Device::checkStatusOkToCaptureLocked() {
519 switch (mStatus) {
520 case STATUS_ERROR:
521 CLOGE("Device has encountered a serious error");
522 return INVALID_OPERATION;
523 case STATUS_UNINITIALIZED:
524 CLOGE("Device not initialized");
525 return INVALID_OPERATION;
526 case STATUS_UNCONFIGURED:
527 case STATUS_CONFIGURED:
528 case STATUS_ACTIVE:
529 // OK
530 break;
531 default:
532 SET_ERR_L("Unexpected status: %d", mStatus);
533 return INVALID_OPERATION;
534 }
535 return OK;
536}
537
538status_t Camera3Device::convertMetadataListToRequestListLocked(
539 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
540 if (requestList == NULL) {
541 CLOGE("requestList cannot be NULL.");
542 return BAD_VALUE;
543 }
544
Jianing Weicb0652e2014-03-12 18:29:36 -0700545 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700546 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
547 it != metadataList.end(); ++it) {
548 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
549 if (newRequest == 0) {
550 CLOGE("Can't create capture request");
551 return BAD_VALUE;
552 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700553
554 // Setup burst Id and request Id
555 newRequest->mResultExtras.burstId = burstId++;
556 if (it->exists(ANDROID_REQUEST_ID)) {
557 if (it->find(ANDROID_REQUEST_ID).count == 0) {
558 CLOGE("RequestID entry exists; but must not be empty in metadata");
559 return BAD_VALUE;
560 }
561 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
562 } else {
563 CLOGE("RequestID does not exist in metadata");
564 return BAD_VALUE;
565 }
566
Jianing Wei90e59c92014-03-12 18:29:36 -0700567 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700568
569 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700570 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700571
572 // Setup batch size if this is a high speed video recording request.
573 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
574 auto firstRequest = requestList->begin();
575 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
576 if (outputStream->isVideoStream()) {
577 (*firstRequest)->mBatchSize = requestList->size();
578 break;
579 }
580 }
581 }
582
Jianing Wei90e59c92014-03-12 18:29:36 -0700583 return OK;
584}
585
Jianing Weicb0652e2014-03-12 18:29:36 -0700586status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800587 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800588
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700589 List<const CameraMetadata> requests;
590 requests.push_back(request);
591 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800592}
593
Jianing Wei90e59c92014-03-12 18:29:36 -0700594status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700595 const List<const CameraMetadata> &requests, bool repeating,
596 /*out*/
597 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700598 ATRACE_CALL();
599 Mutex::Autolock il(mInterfaceLock);
600 Mutex::Autolock l(mLock);
601
602 status_t res = checkStatusOkToCaptureLocked();
603 if (res != OK) {
604 // error logged by previous call
605 return res;
606 }
607
608 RequestList requestList;
609
610 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
611 if (res != OK) {
612 // error logged by previous call
613 return res;
614 }
615
616 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700617 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700618 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700619 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700620 }
621
622 if (res == OK) {
623 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
624 if (res != OK) {
625 SET_ERR_L("Can't transition to active in %f seconds!",
626 kActiveTimeout/1e9);
627 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700628 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
629 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700630 } else {
631 CLOGE("Cannot queue request. Impossible.");
632 return BAD_VALUE;
633 }
634
635 return res;
636}
637
Jianing Weicb0652e2014-03-12 18:29:36 -0700638status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
639 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700640 ATRACE_CALL();
641
Jianing Weicb0652e2014-03-12 18:29:36 -0700642 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700643}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800644
Jianing Weicb0652e2014-03-12 18:29:36 -0700645status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
646 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800647 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800648
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700649 List<const CameraMetadata> requests;
650 requests.push_back(request);
651 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800652}
653
Jianing Weicb0652e2014-03-12 18:29:36 -0700654status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
655 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700656 ATRACE_CALL();
657
Jianing Weicb0652e2014-03-12 18:29:36 -0700658 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700659}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800660
661sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
662 const CameraMetadata &request) {
663 status_t res;
664
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700665 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800666 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700667 // Stream configuration failed due to unsupported configuration.
668 // Device back to unconfigured state. Client might try other configuraitons
669 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
670 CLOGE("No streams configured");
671 return NULL;
672 }
673 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800674 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700675 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800676 return NULL;
677 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700678 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700679 if (mStatus == STATUS_UNCONFIGURED) {
680 CLOGE("No streams configured");
681 return NULL;
682 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800683 }
684
685 sp<CaptureRequest> newRequest = createCaptureRequest(request);
686 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800687}
688
Jianing Weicb0652e2014-03-12 18:29:36 -0700689status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800690 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700691 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800692 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800693
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800694 switch (mStatus) {
695 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700696 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800697 return INVALID_OPERATION;
698 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700699 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800700 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700701 case STATUS_UNCONFIGURED:
702 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800703 case STATUS_ACTIVE:
704 // OK
705 break;
706 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700707 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800708 return INVALID_OPERATION;
709 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700710 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700711
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700712 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800713}
714
715status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
716 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700717 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800718
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700719 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800720}
721
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700722status_t Camera3Device::createInputStream(
723 uint32_t width, uint32_t height, int format, int *id) {
724 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700725 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700726 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700727 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
728 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700729
730 status_t res;
731 bool wasActive = false;
732
733 switch (mStatus) {
734 case STATUS_ERROR:
735 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
736 return INVALID_OPERATION;
737 case STATUS_UNINITIALIZED:
738 ALOGE("%s: Device not initialized", __FUNCTION__);
739 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700740 case STATUS_UNCONFIGURED:
741 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700742 // OK
743 break;
744 case STATUS_ACTIVE:
745 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700746 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700747 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700748 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700749 return res;
750 }
751 wasActive = true;
752 break;
753 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700754 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700755 return INVALID_OPERATION;
756 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700757 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700758
759 if (mInputStream != 0) {
760 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
761 return INVALID_OPERATION;
762 }
763
764 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
765 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700766 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700767
768 mInputStream = newStream;
769
770 *id = mNextStreamId++;
771
772 // Continue captures if active at start
773 if (wasActive) {
774 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
775 res = configureStreamsLocked();
776 if (res != OK) {
777 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
778 __FUNCTION__, mNextStreamId, strerror(-res), res);
779 return res;
780 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700781 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700782 }
783
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700784 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700785 return OK;
786}
787
Igor Murashkin2fba5842013-04-22 14:03:54 -0700788
789status_t Camera3Device::createZslStream(
790 uint32_t width, uint32_t height,
791 int depth,
792 /*out*/
793 int *id,
794 sp<Camera3ZslStream>* zslStream) {
795 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700796 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700797 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700798 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
799 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700800
801 status_t res;
802 bool wasActive = false;
803
804 switch (mStatus) {
805 case STATUS_ERROR:
806 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
807 return INVALID_OPERATION;
808 case STATUS_UNINITIALIZED:
809 ALOGE("%s: Device not initialized", __FUNCTION__);
810 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700811 case STATUS_UNCONFIGURED:
812 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700813 // OK
814 break;
815 case STATUS_ACTIVE:
816 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700817 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700818 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700819 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700820 return res;
821 }
822 wasActive = true;
823 break;
824 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700825 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700826 return INVALID_OPERATION;
827 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700828 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700829
830 if (mInputStream != 0) {
831 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
832 return INVALID_OPERATION;
833 }
834
835 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
836 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700837 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700838
839 res = mOutputStreams.add(mNextStreamId, newStream);
840 if (res < 0) {
841 ALOGE("%s: Can't add new stream to set: %s (%d)",
842 __FUNCTION__, strerror(-res), res);
843 return res;
844 }
845 mInputStream = newStream;
846
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530847 mNeedConfig = true;
848
Igor Murashkin2fba5842013-04-22 14:03:54 -0700849 *id = mNextStreamId++;
850 *zslStream = newStream;
851
852 // Continue captures if active at start
853 if (wasActive) {
854 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
855 res = configureStreamsLocked();
856 if (res != OK) {
857 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
858 __FUNCTION__, mNextStreamId, strerror(-res), res);
859 return res;
860 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700861 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700862 }
863
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700864 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700865 return OK;
866}
867
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700868status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800869 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700870 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800871 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700872 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800873 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700874 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
875 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800876
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800877 status_t res;
878 bool wasActive = false;
879
880 switch (mStatus) {
881 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700882 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800883 return INVALID_OPERATION;
884 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700885 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800886 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700887 case STATUS_UNCONFIGURED:
888 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800889 // OK
890 break;
891 case STATUS_ACTIVE:
892 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700893 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800894 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700895 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800896 return res;
897 }
898 wasActive = true;
899 break;
900 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700901 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800902 return INVALID_OPERATION;
903 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700904 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800905
906 sp<Camera3OutputStream> newStream;
907 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700908 ssize_t blobBufferSize;
909 if (dataSpace != HAL_DATASPACE_DEPTH) {
910 blobBufferSize = getJpegBufferSize(width, height);
911 if (blobBufferSize <= 0) {
912 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
913 return BAD_VALUE;
914 }
915 } else {
916 blobBufferSize = getPointCloudBufferSize();
917 if (blobBufferSize <= 0) {
918 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
919 return BAD_VALUE;
920 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700921 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800922 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700923 width, height, blobBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800924 } else {
925 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700926 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800927 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700928 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800929
930 res = mOutputStreams.add(mNextStreamId, newStream);
931 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700932 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800933 return res;
934 }
935
936 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700937 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800938
939 // Continue captures if active at start
940 if (wasActive) {
941 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
942 res = configureStreamsLocked();
943 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700944 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
945 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800946 return res;
947 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700948 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800949 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700950 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800951 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800952}
953
954status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
955 ATRACE_CALL();
956 (void)outputId; (void)id;
957
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700958 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800959 return INVALID_OPERATION;
960}
961
962
963status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700964 uint32_t *width, uint32_t *height,
965 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800966 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700967 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800968 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800969
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800970 switch (mStatus) {
971 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700972 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800973 return INVALID_OPERATION;
974 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700975 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800976 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700977 case STATUS_UNCONFIGURED:
978 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800979 case STATUS_ACTIVE:
980 // OK
981 break;
982 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700983 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800984 return INVALID_OPERATION;
985 }
986
987 ssize_t idx = mOutputStreams.indexOfKey(id);
988 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700989 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800990 return idx;
991 }
992
993 if (width) *width = mOutputStreams[idx]->getWidth();
994 if (height) *height = mOutputStreams[idx]->getHeight();
995 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700996 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800997 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800998}
999
1000status_t Camera3Device::setStreamTransform(int id,
1001 int transform) {
1002 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001003 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001004 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001005
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001006 switch (mStatus) {
1007 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001008 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001009 return INVALID_OPERATION;
1010 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001011 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001012 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001013 case STATUS_UNCONFIGURED:
1014 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001015 case STATUS_ACTIVE:
1016 // OK
1017 break;
1018 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001019 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001020 return INVALID_OPERATION;
1021 }
1022
1023 ssize_t idx = mOutputStreams.indexOfKey(id);
1024 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001025 CLOGE("Stream %d does not exist",
1026 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001027 return BAD_VALUE;
1028 }
1029
1030 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001031}
1032
1033status_t Camera3Device::deleteStream(int id) {
1034 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001035 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001036 Mutex::Autolock l(mLock);
1037 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001038
Igor Murashkine2172be2013-05-28 15:31:39 -07001039 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1040
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001041 // CameraDevice semantics require device to already be idle before
1042 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001043 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001044 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1045 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001046 }
1047
Igor Murashkin2fba5842013-04-22 14:03:54 -07001048 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001049 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001050 if (mInputStream != NULL && id == mInputStream->getId()) {
1051 deletedStream = mInputStream;
1052 mInputStream.clear();
1053 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001054 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001055 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001056 return BAD_VALUE;
1057 }
Zhijun He5f446352014-01-22 09:49:33 -08001058 }
1059
1060 // Delete output stream or the output part of a bi-directional stream.
1061 if (outputStreamIdx != NAME_NOT_FOUND) {
1062 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001063 mOutputStreams.removeItem(id);
1064 }
1065
1066 // Free up the stream endpoint so that it can be used by some other stream
1067 res = deletedStream->disconnect();
1068 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001069 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001070 // fall through since we want to still list the stream as deleted.
1071 }
1072 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001073 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001074
1075 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001076}
1077
1078status_t Camera3Device::deleteReprocessStream(int id) {
1079 ATRACE_CALL();
1080 (void)id;
1081
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001082 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001083 return INVALID_OPERATION;
1084}
1085
Zhijun He1fa89992015-06-01 15:44:31 -07001086status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001087 ATRACE_CALL();
1088 ALOGV("%s: E", __FUNCTION__);
1089
1090 Mutex::Autolock il(mInterfaceLock);
1091 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001092
1093 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1094 mNeedConfig = true;
1095 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1096 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001097
1098 return configureStreamsLocked();
1099}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001100
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001101status_t Camera3Device::getInputBufferProducer(
1102 sp<IGraphicBufferProducer> *producer) {
1103 Mutex::Autolock il(mInterfaceLock);
1104 Mutex::Autolock l(mLock);
1105
1106 if (producer == NULL) {
1107 return BAD_VALUE;
1108 } else if (mInputStream == NULL) {
1109 return INVALID_OPERATION;
1110 }
1111
1112 return mInputStream->getInputBufferProducer(producer);
1113}
1114
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001115status_t Camera3Device::createDefaultRequest(int templateId,
1116 CameraMetadata *request) {
1117 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001118 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen116a1892016-03-09 12:21:01 -08001119
1120 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1121 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1122 IPCThreadState::self()->getCallingUid(), NULL, 0);
1123 return BAD_VALUE;
1124 }
1125
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001126 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001127 Mutex::Autolock l(mLock);
1128
1129 switch (mStatus) {
1130 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001131 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001132 return INVALID_OPERATION;
1133 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001134 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001135 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001136 case STATUS_UNCONFIGURED:
1137 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001138 case STATUS_ACTIVE:
1139 // OK
1140 break;
1141 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001142 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001143 return INVALID_OPERATION;
1144 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001145
Zhijun Hea1530f12014-09-14 12:44:20 -07001146 if (!mRequestTemplateCache[templateId].isEmpty()) {
1147 *request = mRequestTemplateCache[templateId];
1148 return OK;
1149 }
1150
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001151 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001152 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001153 rawRequest = mHal3Device->ops->construct_default_request_settings(
1154 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001155 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001156 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001157 ALOGI("%s: template %d is not supported on this camera device",
1158 __FUNCTION__, templateId);
1159 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001160 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001161 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001162 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001163
1164 return OK;
1165}
1166
1167status_t Camera3Device::waitUntilDrained() {
1168 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001169 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001170 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001171
Zhijun He69a37482014-03-23 18:44:49 -07001172 return waitUntilDrainedLocked();
1173}
1174
1175status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001176 switch (mStatus) {
1177 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001178 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001179 ALOGV("%s: Already idle", __FUNCTION__);
1180 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001181 case STATUS_CONFIGURED:
1182 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001183 case STATUS_ERROR:
1184 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001185 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001186 break;
1187 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001188 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001189 return INVALID_OPERATION;
1190 }
1191
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001192 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1193 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001194 if (res != OK) {
1195 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1196 res);
1197 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001198 return res;
1199}
1200
Ruben Brunk183f0562015-08-12 12:55:02 -07001201
1202void Camera3Device::internalUpdateStatusLocked(Status status) {
1203 mStatus = status;
1204 mRecentStatusUpdates.add(mStatus);
1205 mStatusChanged.broadcast();
1206}
1207
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001208// Pause to reconfigure
1209status_t Camera3Device::internalPauseAndWaitLocked() {
1210 mRequestThread->setPaused(true);
1211 mPauseStateNotify = true;
1212
1213 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1214 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1215 if (res != OK) {
1216 SET_ERR_L("Can't idle device in %f seconds!",
1217 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001218 }
1219
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001220 return res;
1221}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001222
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001223// Resume after internalPauseAndWaitLocked
1224status_t Camera3Device::internalResumeLocked() {
1225 status_t res;
1226
1227 mRequestThread->setPaused(false);
1228
1229 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1230 if (res != OK) {
1231 SET_ERR_L("Can't transition to active in %f seconds!",
1232 kActiveTimeout/1e9);
1233 }
1234 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001235 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001236}
1237
Ruben Brunk183f0562015-08-12 12:55:02 -07001238status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001239 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001240
1241 size_t startIndex = 0;
1242 if (mStatusWaiters == 0) {
1243 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1244 // this status list
1245 mRecentStatusUpdates.clear();
1246 } else {
1247 // If other threads are waiting on updates to this status list, set the position of the
1248 // first element that this list will check rather than clearing the list.
1249 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001250 }
1251
Ruben Brunk183f0562015-08-12 12:55:02 -07001252 mStatusWaiters++;
1253
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001254 bool stateSeen = false;
1255 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001256 if (active == (mStatus == STATUS_ACTIVE)) {
1257 // Desired state is current
1258 break;
1259 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001260
1261 res = mStatusChanged.waitRelative(mLock, timeout);
1262 if (res != OK) break;
1263
Ruben Brunk183f0562015-08-12 12:55:02 -07001264 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1265 // transitions.
1266 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1267 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1268 __FUNCTION__);
1269
1270 // Encountered desired state since we began waiting
1271 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001272 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1273 stateSeen = true;
1274 break;
1275 }
1276 }
1277 } while (!stateSeen);
1278
Ruben Brunk183f0562015-08-12 12:55:02 -07001279 mStatusWaiters--;
1280
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001281 return res;
1282}
1283
1284
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001285status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1286 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001287 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001288
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001289 if (listener != NULL && mListener != NULL) {
1290 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1291 }
1292 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001293 mRequestThread->setNotificationListener(listener);
1294 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001295
1296 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001297}
1298
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001299bool Camera3Device::willNotify3A() {
1300 return false;
1301}
1302
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001303status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001304 status_t res;
1305 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001306
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001307 while (mResultQueue.empty()) {
1308 res = mResultSignal.waitRelative(mOutputLock, timeout);
1309 if (res == TIMED_OUT) {
1310 return res;
1311 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001312 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001313 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001314 return res;
1315 }
1316 }
1317 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001318}
1319
Jianing Weicb0652e2014-03-12 18:29:36 -07001320status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001321 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001322 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001323
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001324 if (mResultQueue.empty()) {
1325 return NOT_ENOUGH_DATA;
1326 }
1327
Jianing Weicb0652e2014-03-12 18:29:36 -07001328 if (frame == NULL) {
1329 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1330 return BAD_VALUE;
1331 }
1332
1333 CaptureResult &result = *(mResultQueue.begin());
1334 frame->mResultExtras = result.mResultExtras;
1335 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001336 mResultQueue.erase(mResultQueue.begin());
1337
1338 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001339}
1340
1341status_t Camera3Device::triggerAutofocus(uint32_t id) {
1342 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001343 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001344
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001345 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1346 // Mix-in this trigger into the next request and only the next request.
1347 RequestTrigger trigger[] = {
1348 {
1349 ANDROID_CONTROL_AF_TRIGGER,
1350 ANDROID_CONTROL_AF_TRIGGER_START
1351 },
1352 {
1353 ANDROID_CONTROL_AF_TRIGGER_ID,
1354 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001355 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001356 };
1357
1358 return mRequestThread->queueTrigger(trigger,
1359 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001360}
1361
1362status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1363 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001364 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001365
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001366 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1367 // Mix-in this trigger into the next request and only the next request.
1368 RequestTrigger trigger[] = {
1369 {
1370 ANDROID_CONTROL_AF_TRIGGER,
1371 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1372 },
1373 {
1374 ANDROID_CONTROL_AF_TRIGGER_ID,
1375 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001376 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001377 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001378
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001379 return mRequestThread->queueTrigger(trigger,
1380 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001381}
1382
1383status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1384 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001385 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001386
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001387 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1388 // Mix-in this trigger into the next request and only the next request.
1389 RequestTrigger trigger[] = {
1390 {
1391 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1392 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1393 },
1394 {
1395 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1396 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001397 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001398 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001399
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001400 return mRequestThread->queueTrigger(trigger,
1401 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001402}
1403
1404status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1405 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1406 ATRACE_CALL();
1407 (void)reprocessStreamId; (void)buffer; (void)listener;
1408
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001409 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001410 return INVALID_OPERATION;
1411}
1412
Jianing Weicb0652e2014-03-12 18:29:36 -07001413status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001414 ATRACE_CALL();
1415 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001416 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001417
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001418 NotificationListener* listener;
1419 {
1420 Mutex::Autolock l(mOutputLock);
1421 listener = mListener;
1422 }
1423
Zhijun He7ef20392014-04-21 16:04:17 -07001424 {
1425 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001426 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001427 }
1428
Zhijun He491e3412013-12-27 10:57:44 -08001429 status_t res;
1430 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001431 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001432 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001433 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001434 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001435 }
1436
1437 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001438}
1439
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001440status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001441 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1442}
1443
1444status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001445 ATRACE_CALL();
1446 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001447 Mutex::Autolock il(mInterfaceLock);
1448 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001449
1450 sp<Camera3StreamInterface> stream;
1451 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1452 if (outputStreamIdx == NAME_NOT_FOUND) {
1453 CLOGE("Stream %d does not exist", streamId);
1454 return BAD_VALUE;
1455 }
1456
1457 stream = mOutputStreams.editValueAt(outputStreamIdx);
1458
1459 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001460 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001461 return BAD_VALUE;
1462 }
1463
1464 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001465 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001466 return BAD_VALUE;
1467 }
1468
Ruben Brunkc78ac262015-08-13 17:58:46 -07001469 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001470}
1471
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001472status_t Camera3Device::tearDown(int streamId) {
1473 ATRACE_CALL();
1474 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1475 Mutex::Autolock il(mInterfaceLock);
1476 Mutex::Autolock l(mLock);
1477
1478 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1479 // since we cannot call register_stream_buffers except right after configure_streams.
1480 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1481 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1482 __FUNCTION__, mHal3Device->common.version);
1483 return NO_INIT;
1484 }
1485
1486 sp<Camera3StreamInterface> stream;
1487 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1488 if (outputStreamIdx == NAME_NOT_FOUND) {
1489 CLOGE("Stream %d does not exist", streamId);
1490 return BAD_VALUE;
1491 }
1492
1493 stream = mOutputStreams.editValueAt(outputStreamIdx);
1494
1495 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1496 CLOGE("Stream %d is a target of a in-progress request", streamId);
1497 return BAD_VALUE;
1498 }
1499
1500 return stream->tearDown();
1501}
1502
Zhijun He204e3292014-07-14 17:09:23 -07001503uint32_t Camera3Device::getDeviceVersion() {
1504 ATRACE_CALL();
1505 Mutex::Autolock il(mInterfaceLock);
1506 return mDeviceVersion;
1507}
1508
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001509/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001510 * Methods called by subclasses
1511 */
1512
1513void Camera3Device::notifyStatus(bool idle) {
1514 {
1515 // Need mLock to safely update state and synchronize to current
1516 // state of methods in flight.
1517 Mutex::Autolock l(mLock);
1518 // We can get various system-idle notices from the status tracker
1519 // while starting up. Only care about them if we've actually sent
1520 // in some requests recently.
1521 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1522 return;
1523 }
1524 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1525 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001526 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001527
1528 // Skip notifying listener if we're doing some user-transparent
1529 // state changes
1530 if (mPauseStateNotify) return;
1531 }
1532 NotificationListener *listener;
1533 {
1534 Mutex::Autolock l(mOutputLock);
1535 listener = mListener;
1536 }
1537 if (idle && listener != NULL) {
1538 listener->notifyIdle();
1539 }
1540}
1541
1542/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001543 * Camera3Device private methods
1544 */
1545
1546sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1547 const CameraMetadata &request) {
1548 ATRACE_CALL();
1549 status_t res;
1550
1551 sp<CaptureRequest> newRequest = new CaptureRequest;
1552 newRequest->mSettings = request;
1553
1554 camera_metadata_entry_t inputStreams =
1555 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1556 if (inputStreams.count > 0) {
1557 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001558 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001559 CLOGE("Request references unknown input stream %d",
1560 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001561 return NULL;
1562 }
1563 // Lazy completion of stream configuration (allocation/registration)
1564 // on first use
1565 if (mInputStream->isConfiguring()) {
1566 res = mInputStream->finishConfiguration(mHal3Device);
1567 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001568 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001569 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001570 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001571 return NULL;
1572 }
1573 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001574 // Check if stream is being prepared
1575 if (mInputStream->isPreparing()) {
1576 CLOGE("Request references an input stream that's being prepared!");
1577 return NULL;
1578 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001579
1580 newRequest->mInputStream = mInputStream;
1581 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1582 }
1583
1584 camera_metadata_entry_t streams =
1585 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1586 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001587 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001588 return NULL;
1589 }
1590
1591 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001592 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001593 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001594 CLOGE("Request references unknown stream %d",
1595 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001596 return NULL;
1597 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001598 sp<Camera3OutputStreamInterface> stream =
1599 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001600
1601 // Lazy completion of stream configuration (allocation/registration)
1602 // on first use
1603 if (stream->isConfiguring()) {
1604 res = stream->finishConfiguration(mHal3Device);
1605 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001606 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1607 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001608 return NULL;
1609 }
1610 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001611 // Check if stream is being prepared
1612 if (stream->isPreparing()) {
1613 CLOGE("Request references an output stream that's being prepared!");
1614 return NULL;
1615 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001616
1617 newRequest->mOutputStreams.push(stream);
1618 }
1619 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001620 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001621
1622 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001623}
1624
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001625bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1626 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1627 Size size = mSupportedOpaqueInputSizes[i];
1628 if (size.width == width && size.height == height) {
1629 return true;
1630 }
1631 }
1632
1633 return false;
1634}
1635
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001636status_t Camera3Device::configureStreamsLocked() {
1637 ATRACE_CALL();
1638 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001639
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001640 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001641 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001642 return INVALID_OPERATION;
1643 }
1644
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001645 if (!mNeedConfig) {
1646 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1647 return OK;
1648 }
1649
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001650 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1651 // adding a dummy stream instead.
1652 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1653 if (mOutputStreams.size() == 0) {
1654 addDummyStreamLocked();
1655 } else {
1656 tryRemoveDummyStreamLocked();
1657 }
1658
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001659 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001660 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001661
1662 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001663 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1664 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1665 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001666 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1667
1668 Vector<camera3_stream_t*> streams;
1669 streams.setCapacity(config.num_streams);
1670
1671 if (mInputStream != NULL) {
1672 camera3_stream_t *inputStream;
1673 inputStream = mInputStream->startConfiguration();
1674 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001675 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001676 return INVALID_OPERATION;
1677 }
1678 streams.add(inputStream);
1679 }
1680
1681 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001682
1683 // Don't configure bidi streams twice, nor add them twice to the list
1684 if (mOutputStreams[i].get() ==
1685 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1686
1687 config.num_streams--;
1688 continue;
1689 }
1690
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001691 camera3_stream_t *outputStream;
1692 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1693 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001694 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001695 return INVALID_OPERATION;
1696 }
1697 streams.add(outputStream);
1698 }
1699
1700 config.streams = streams.editArray();
1701
1702 // Do the HAL configuration; will potentially touch stream
1703 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001704 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001705 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001706 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001707
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001708 if (res == BAD_VALUE) {
1709 // HAL rejected this set of streams as unsupported, clean up config
1710 // attempt and return to unconfigured state
1711 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1712 res = mInputStream->cancelConfiguration();
1713 if (res != OK) {
1714 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1715 mInputStream->getId(), strerror(-res), res);
1716 return res;
1717 }
1718 }
1719
1720 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1721 sp<Camera3OutputStreamInterface> outputStream =
1722 mOutputStreams.editValueAt(i);
1723 if (outputStream->isConfiguring()) {
1724 res = outputStream->cancelConfiguration();
1725 if (res != OK) {
1726 SET_ERR_L(
1727 "Can't cancel configuring output stream %d: %s (%d)",
1728 outputStream->getId(), strerror(-res), res);
1729 return res;
1730 }
1731 }
1732 }
1733
1734 // Return state to that at start of call, so that future configures
1735 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001736 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001737 mNeedConfig = true;
1738
1739 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1740 return BAD_VALUE;
1741 } else if (res != OK) {
1742 // Some other kind of error from configure_streams - this is not
1743 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001744 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1745 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001746 return res;
1747 }
1748
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001749 // Finish all stream configuration immediately.
1750 // TODO: Try to relax this later back to lazy completion, which should be
1751 // faster
1752
Igor Murashkin073f8572013-05-02 14:59:28 -07001753 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001754 res = mInputStream->finishConfiguration(mHal3Device);
1755 if (res != OK) {
1756 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1757 mInputStream->getId(), strerror(-res), res);
1758 return res;
1759 }
1760 }
1761
1762 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001763 sp<Camera3OutputStreamInterface> outputStream =
1764 mOutputStreams.editValueAt(i);
1765 if (outputStream->isConfiguring()) {
1766 res = outputStream->finishConfiguration(mHal3Device);
1767 if (res != OK) {
1768 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1769 outputStream->getId(), strerror(-res), res);
1770 return res;
1771 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001772 }
1773 }
1774
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001775 // Request thread needs to know to avoid using repeat-last-settings protocol
1776 // across configure_streams() calls
1777 mRequestThread->configurationComplete();
1778
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07001779 // Boost priority of request thread for high speed recording to SCHED_FIFO
1780 if (mIsConstrainedHighSpeedConfiguration) {
1781 pid_t requestThreadTid = mRequestThread->getTid();
1782 res = requestPriority(getpid(), requestThreadTid,
1783 kConstrainedHighSpeedThreadPriority, true);
1784 if (res != OK) {
1785 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
1786 strerror(-res), res);
1787 } else {
1788 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
1789 }
1790 } else {
1791 // TODO: Set/restore normal priority for normal use cases
1792 }
1793
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001794 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001795
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001796 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001797
Ruben Brunk183f0562015-08-12 12:55:02 -07001798 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1799 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001800
1801 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1802
Zhijun He0a210512014-07-24 13:45:15 -07001803 // tear down the deleted streams after configure streams.
1804 mDeletedStreams.clear();
1805
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001806 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001807}
1808
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001809status_t Camera3Device::addDummyStreamLocked() {
1810 ATRACE_CALL();
1811 status_t res;
1812
1813 if (mDummyStreamId != NO_STREAM) {
1814 // Should never be adding a second dummy stream when one is already
1815 // active
1816 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1817 __FUNCTION__, mId);
1818 return INVALID_OPERATION;
1819 }
1820
1821 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1822
1823 sp<Camera3OutputStreamInterface> dummyStream =
1824 new Camera3DummyStream(mNextStreamId);
1825
1826 res = mOutputStreams.add(mNextStreamId, dummyStream);
1827 if (res < 0) {
1828 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1829 return res;
1830 }
1831
1832 mDummyStreamId = mNextStreamId;
1833 mNextStreamId++;
1834
1835 return OK;
1836}
1837
1838status_t Camera3Device::tryRemoveDummyStreamLocked() {
1839 ATRACE_CALL();
1840 status_t res;
1841
1842 if (mDummyStreamId == NO_STREAM) return OK;
1843 if (mOutputStreams.size() == 1) return OK;
1844
1845 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1846
1847 // Ok, have a dummy stream and there's at least one other output stream,
1848 // so remove the dummy
1849
1850 sp<Camera3StreamInterface> deletedStream;
1851 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1852 if (outputStreamIdx == NAME_NOT_FOUND) {
1853 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1854 return INVALID_OPERATION;
1855 }
1856
1857 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1858 mOutputStreams.removeItemsAt(outputStreamIdx);
1859
1860 // Free up the stream endpoint so that it can be used by some other stream
1861 res = deletedStream->disconnect();
1862 if (res != OK) {
1863 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1864 // fall through since we want to still list the stream as deleted.
1865 }
1866 mDeletedStreams.add(deletedStream);
1867 mDummyStreamId = NO_STREAM;
1868
1869 return res;
1870}
1871
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001872void Camera3Device::setErrorState(const char *fmt, ...) {
1873 Mutex::Autolock l(mLock);
1874 va_list args;
1875 va_start(args, fmt);
1876
1877 setErrorStateLockedV(fmt, args);
1878
1879 va_end(args);
1880}
1881
1882void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1883 Mutex::Autolock l(mLock);
1884 setErrorStateLockedV(fmt, args);
1885}
1886
1887void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1888 va_list args;
1889 va_start(args, fmt);
1890
1891 setErrorStateLockedV(fmt, args);
1892
1893 va_end(args);
1894}
1895
1896void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001897 // Print out all error messages to log
1898 String8 errorCause = String8::formatV(fmt, args);
1899 ALOGE("Camera %d: %s", mId, errorCause.string());
1900
1901 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001902 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001903
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001904 mErrorCause = errorCause;
1905
1906 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07001907 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001908
1909 // Notify upstream about a device error
1910 if (mListener != NULL) {
1911 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1912 CaptureResultExtras());
1913 }
1914
1915 // Save stack trace. View by dumping it later.
1916 CameraTraces::saveTrace();
1917 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001918}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001919
1920/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001921 * In-flight request management
1922 */
1923
Jianing Weicb0652e2014-03-12 18:29:36 -07001924status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07001925 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1926 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001927 ATRACE_CALL();
1928 Mutex::Autolock l(mInFlightLock);
1929
1930 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07001931 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
1932 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001933 if (res < 0) return res;
1934
1935 return OK;
1936}
1937
1938/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001939 * Check if all 3A fields are ready, and send off a partial 3A-only result
1940 * to the output frame queue
1941 */
Zhijun He204e3292014-07-14 17:09:23 -07001942bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001943 uint32_t frameNumber,
1944 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001945
1946 // Check if all 3A states are present
1947 // The full list of fields is
1948 // android.control.afMode
1949 // android.control.awbMode
1950 // android.control.aeState
1951 // android.control.awbState
1952 // android.control.afState
1953 // android.control.afTriggerID
1954 // android.control.aePrecaptureID
1955 // TODO: Add android.control.aeMode
1956
1957 bool gotAllStates = true;
1958
1959 uint8_t afMode;
1960 uint8_t awbMode;
1961 uint8_t aeState;
1962 uint8_t afState;
1963 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001964
1965 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1966 &afMode, frameNumber);
1967
1968 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1969 &awbMode, frameNumber);
1970
1971 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1972 &aeState, frameNumber);
1973
1974 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1975 &afState, frameNumber);
1976
1977 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1978 &awbState, frameNumber);
1979
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001980 if (!gotAllStates) return false;
1981
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001982 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001983 "AF state %d, AE state %d, AWB state %d, "
1984 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001985 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001986 afMode, awbMode,
1987 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001988 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001989
1990 // Got all states, so construct a minimal result to send
1991 // In addition to the above fields, this means adding in
1992 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001993 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001994 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001995
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001996 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001997
1998 Mutex::Autolock l(mOutputLock);
1999
Jianing Weicb0652e2014-03-12 18:29:36 -07002000 CaptureResult captureResult;
2001 captureResult.mResultExtras = resultExtras;
2002 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
2003 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
2004 // but not limited to CameraDeviceBase::getNextResult
2005 CaptureResult& min3AResult =
2006 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002007
Jianing Weicb0652e2014-03-12 18:29:36 -07002008 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
2009 // TODO: This is problematic casting. Need to fix CameraMetadata.
2010 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002011 return false;
2012 }
2013
Jianing Weicb0652e2014-03-12 18:29:36 -07002014 int32_t requestId = resultExtras.requestId;
2015 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002016 &requestId, frameNumber)) {
2017 return false;
2018 }
2019
Zhijun He204e3292014-07-14 17:09:23 -07002020 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
2021 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
2022 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
2023 &partialResult, frameNumber)) {
2024 return false;
2025 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002026 }
2027
Jianing Weicb0652e2014-03-12 18:29:36 -07002028 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002029 &afMode, frameNumber)) {
2030 return false;
2031 }
2032
Jianing Weicb0652e2014-03-12 18:29:36 -07002033 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002034 &awbMode, frameNumber)) {
2035 return false;
2036 }
2037
Jianing Weicb0652e2014-03-12 18:29:36 -07002038 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002039 &aeState, frameNumber)) {
2040 return false;
2041 }
2042
Jianing Weicb0652e2014-03-12 18:29:36 -07002043 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002044 &afState, frameNumber)) {
2045 return false;
2046 }
2047
Jianing Weicb0652e2014-03-12 18:29:36 -07002048 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002049 &awbState, frameNumber)) {
2050 return false;
2051 }
2052
Jianing Weicb0652e2014-03-12 18:29:36 -07002053 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002054 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002055 return false;
2056 }
2057
Jianing Weicb0652e2014-03-12 18:29:36 -07002058 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002059 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002060 return false;
2061 }
2062
Zhijun He204e3292014-07-14 17:09:23 -07002063 // We only send the aggregated partial when all 3A related metadata are available
2064 // For both API1 and API2.
2065 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002066 mResultSignal.signal();
2067
2068 return true;
2069}
2070
2071template<typename T>
2072bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002073 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002074 (void) frameNumber;
2075
2076 camera_metadata_ro_entry_t entry;
2077
2078 entry = result.find(tag);
2079 if (entry.count == 0) {
2080 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2081 mId, frameNumber, get_camera_metadata_tag_name(tag));
2082 return false;
2083 }
2084
2085 if (sizeof(T) == sizeof(uint8_t)) {
2086 *value = entry.data.u8[0];
2087 } else if (sizeof(T) == sizeof(int32_t)) {
2088 *value = entry.data.i32[0];
2089 } else {
2090 ALOGE("%s: Unexpected type", __FUNCTION__);
2091 return false;
2092 }
2093 return true;
2094}
2095
2096template<typename T>
2097bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002098 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002099 if (result.update(tag, value, 1) != NO_ERROR) {
2100 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2101 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2102 frameNumber, get_camera_metadata_tag_name(tag));
2103 return false;
2104 }
2105 return true;
2106}
2107
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002108void Camera3Device::returnOutputBuffers(
2109 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2110 nsecs_t timestamp) {
2111 for (size_t i = 0; i < numBuffers; i++)
2112 {
2113 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2114 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2115 // Note: stream may be deallocated at this point, if this buffer was
2116 // the last reference to it.
2117 if (res != OK) {
2118 ALOGE("Can't return buffer to its stream: %s (%d)",
2119 strerror(-res), res);
2120 }
2121 }
2122}
2123
2124
2125void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2126
2127 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2128 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2129
2130 nsecs_t sensorTimestamp = request.sensorTimestamp;
2131 nsecs_t shutterTimestamp = request.shutterTimestamp;
2132
2133 // Check if it's okay to remove the request from InFlightMap:
2134 // In the case of a successful request:
2135 // all input and output buffers, all result metadata, shutter callback
2136 // arrived.
2137 // In the case of a unsuccessful request:
2138 // all input and output buffers arrived.
2139 if (request.numBuffersLeft == 0 &&
2140 (request.requestStatus != OK ||
2141 (request.haveResultMetadata && shutterTimestamp != 0))) {
2142 ATRACE_ASYNC_END("frame capture", frameNumber);
2143
2144 // Sanity check - if sensor timestamp matches shutter timestamp
2145 if (request.requestStatus == OK &&
2146 sensorTimestamp != shutterTimestamp) {
2147 SET_ERR("sensor timestamp (%" PRId64
2148 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2149 sensorTimestamp, frameNumber, shutterTimestamp);
2150 }
2151
2152 // for an unsuccessful request, it may have pending output buffers to
2153 // return.
2154 assert(request.requestStatus != OK ||
2155 request.pendingOutputBuffers.size() == 0);
2156 returnOutputBuffers(request.pendingOutputBuffers.array(),
2157 request.pendingOutputBuffers.size(), 0);
2158
2159 mInFlightMap.removeItemsAt(idx, 1);
2160
2161 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2162 }
2163
2164 // Sanity check - if we have too many in-flight frames, something has
2165 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002166 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002167 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002168 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2169 kInFlightWarnLimitHighSpeed) {
2170 CLOGE("In-flight list too large for high speed configuration: %zu",
2171 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002172 }
2173}
2174
2175
2176void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2177 CaptureResultExtras &resultExtras,
2178 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002179 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002180 bool reprocess,
2181 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002182 if (pendingMetadata.isEmpty())
2183 return;
2184
2185 Mutex::Autolock l(mOutputLock);
2186
2187 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002188 if (reprocess) {
2189 if (frameNumber < mNextReprocessResultFrameNumber) {
2190 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002191 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002192 frameNumber, mNextReprocessResultFrameNumber);
2193 return;
2194 }
2195 mNextReprocessResultFrameNumber = frameNumber + 1;
2196 } else {
2197 if (frameNumber < mNextResultFrameNumber) {
2198 SET_ERR("Out-of-order capture result metadata submitted! "
2199 "(got frame number %d, expecting %d)",
2200 frameNumber, mNextResultFrameNumber);
2201 return;
2202 }
2203 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002204 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002205
2206 CaptureResult captureResult;
2207 captureResult.mResultExtras = resultExtras;
2208 captureResult.mMetadata = pendingMetadata;
2209
2210 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2211 (int32_t*)&frameNumber, 1) != OK) {
2212 SET_ERR("Failed to set frame# in metadata (%d)",
2213 frameNumber);
2214 return;
2215 } else {
2216 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2217 __FUNCTION__, mId, frameNumber);
2218 }
2219
2220 // Append any previous partials to form a complete result
2221 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2222 captureResult.mMetadata.append(collectedPartialResult);
2223 }
2224
2225 captureResult.mMetadata.sort();
2226
2227 // Check that there's a timestamp in the result metadata
2228 camera_metadata_entry entry =
2229 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2230 if (entry.count == 0) {
2231 SET_ERR("No timestamp provided by HAL for frame %d!",
2232 frameNumber);
2233 return;
2234 }
2235
Chien-Yu Chend196d612015-06-22 19:49:01 -07002236 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2237
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002238 // Valid result, insert into queue
2239 List<CaptureResult>::iterator queuedResult =
2240 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2241 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2242 ", burstId = %" PRId32, __FUNCTION__,
2243 queuedResult->mResultExtras.requestId,
2244 queuedResult->mResultExtras.frameNumber,
2245 queuedResult->mResultExtras.burstId);
2246
2247 mResultSignal.signal();
2248}
2249
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002250/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002251 * Camera HAL device callback methods
2252 */
2253
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002254void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002255 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002256
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002257 status_t res;
2258
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002259 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002260 if (result->result == NULL && result->num_output_buffers == 0 &&
2261 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002262 SET_ERR("No result data provided by HAL for frame %d",
2263 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002264 return;
2265 }
Zhijun He204e3292014-07-14 17:09:23 -07002266
2267 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2268 // partial_result to 1 when metadata is included in this result.
2269 if (!mUsePartialResult &&
2270 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2271 result->result != NULL &&
2272 result->partial_result != 1) {
2273 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2274 " if partial result is not supported",
2275 frameNumber, result->partial_result);
2276 return;
2277 }
2278
2279 bool isPartialResult = false;
2280 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002281 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002282 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002283
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002284 // Get shutter timestamp and resultExtras from list of in-flight requests,
2285 // where it was added by the shutter notification for this frame. If the
2286 // shutter timestamp isn't received yet, append the output buffers to the
2287 // in-flight request and they will be returned when the shutter timestamp
2288 // arrives. Update the in-flight status and remove the in-flight entry if
2289 // all result data and shutter timestamp have been received.
2290 nsecs_t shutterTimestamp = 0;
2291
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002292 {
2293 Mutex::Autolock l(mInFlightLock);
2294 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2295 if (idx == NAME_NOT_FOUND) {
2296 SET_ERR("Unknown frame number for capture result: %d",
2297 frameNumber);
2298 return;
2299 }
2300 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002301 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2302 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2303 ", partialResultCount = %d",
2304 __FUNCTION__, request.resultExtras.requestId,
2305 request.resultExtras.frameNumber, request.resultExtras.burstId,
2306 result->partial_result);
2307 // Always update the partial count to the latest one if it's not 0
2308 // (buffers only). When framework aggregates adjacent partial results
2309 // into one, the latest partial count will be used.
2310 if (result->partial_result != 0)
2311 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002312
2313 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002314 if (mUsePartialResult && result->result != NULL) {
2315 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2316 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2317 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2318 " the range of [1, %d] when metadata is included in the result",
2319 frameNumber, result->partial_result, mNumPartialResults);
2320 return;
2321 }
2322 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002323 if (isPartialResult) {
2324 request.partialResult.collectedResult.append(result->result);
2325 }
Zhijun He204e3292014-07-14 17:09:23 -07002326 } else {
2327 camera_metadata_ro_entry_t partialResultEntry;
2328 res = find_camera_metadata_ro_entry(result->result,
2329 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2330 if (res != NAME_NOT_FOUND &&
2331 partialResultEntry.count > 0 &&
2332 partialResultEntry.data.u8[0] ==
2333 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2334 // A partial result. Flag this as such, and collect this
2335 // set of metadata into the in-flight entry.
2336 isPartialResult = true;
2337 request.partialResult.collectedResult.append(
2338 result->result);
2339 request.partialResult.collectedResult.erase(
2340 ANDROID_QUIRKS_PARTIAL_RESULT);
2341 }
2342 }
2343
2344 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002345 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002346 if (!request.partialResult.haveSent3A) {
2347 request.partialResult.haveSent3A =
2348 processPartial3AResult(frameNumber,
2349 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002350 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002351 }
2352 }
2353 }
2354
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002355 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002356 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002357
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002358 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002359 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002360 if (request.haveResultMetadata) {
2361 SET_ERR("Called multiple times with metadata for frame %d",
2362 frameNumber);
2363 return;
2364 }
Zhijun He204e3292014-07-14 17:09:23 -07002365 if (mUsePartialResult &&
2366 !request.partialResult.collectedResult.isEmpty()) {
2367 collectedPartialResult.acquire(
2368 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002369 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002370 request.haveResultMetadata = true;
2371 }
2372
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002373 uint32_t numBuffersReturned = result->num_output_buffers;
2374 if (result->input_buffer != NULL) {
2375 if (hasInputBufferInRequest) {
2376 numBuffersReturned += 1;
2377 } else {
2378 ALOGW("%s: Input buffer should be NULL if there is no input"
2379 " buffer sent in the request",
2380 __FUNCTION__);
2381 }
2382 }
2383 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002384 if (request.numBuffersLeft < 0) {
2385 SET_ERR("Too many buffers returned for frame %d",
2386 frameNumber);
2387 return;
2388 }
2389
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002390 camera_metadata_ro_entry_t entry;
2391 res = find_camera_metadata_ro_entry(result->result,
2392 ANDROID_SENSOR_TIMESTAMP, &entry);
2393 if (res == OK && entry.count == 1) {
2394 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002395 }
2396
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002397 // If shutter event isn't received yet, append the output buffers to
2398 // the in-flight request. Otherwise, return the output buffers to
2399 // streams.
2400 if (shutterTimestamp == 0) {
2401 request.pendingOutputBuffers.appendArray(result->output_buffers,
2402 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002403 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002404 returnOutputBuffers(result->output_buffers,
2405 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002406 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002407
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002408 if (result->result != NULL && !isPartialResult) {
2409 if (shutterTimestamp == 0) {
2410 request.pendingMetadata = result->result;
2411 request.partialResult.collectedResult = collectedPartialResult;
2412 } else {
2413 CameraMetadata metadata;
2414 metadata = result->result;
2415 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002416 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2417 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002418 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002419 }
2420
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002421 removeInFlightRequestIfReadyLocked(idx);
2422 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002423
Zhijun Hef0d962a2014-06-30 10:24:11 -07002424 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002425 if (hasInputBufferInRequest) {
2426 Camera3Stream *stream =
2427 Camera3Stream::cast(result->input_buffer->stream);
2428 res = stream->returnInputBuffer(*(result->input_buffer));
2429 // Note: stream may be deallocated at this point, if this buffer was the
2430 // last reference to it.
2431 if (res != OK) {
2432 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2433 " its stream:%s (%d)", __FUNCTION__,
2434 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002435 }
2436 } else {
2437 ALOGW("%s: Input buffer should be NULL if there is no input"
2438 " buffer sent in the request, skipping input buffer return.",
2439 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002440 }
2441 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002442}
2443
2444void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002445 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002446 NotificationListener *listener;
2447 {
2448 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002449 listener = mListener;
2450 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002451
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002452 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002453 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002454 return;
2455 }
2456
2457 switch (msg->type) {
2458 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002459 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002460 break;
2461 }
2462 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002463 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002464 break;
2465 }
2466 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002467 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002468 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002469 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002470}
2471
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002472void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2473 NotificationListener *listener) {
2474
2475 // Map camera HAL error codes to ICameraDeviceCallback error codes
2476 // Index into this with the HAL error code
2477 static const ICameraDeviceCallbacks::CameraErrorCode
2478 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2479 // 0 = Unused error code
2480 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2481 // 1 = CAMERA3_MSG_ERROR_DEVICE
2482 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2483 // 2 = CAMERA3_MSG_ERROR_REQUEST
2484 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2485 // 3 = CAMERA3_MSG_ERROR_RESULT
2486 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2487 // 4 = CAMERA3_MSG_ERROR_BUFFER
2488 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2489 };
2490
2491 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2492 ((msg.error_code >= 0) &&
2493 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2494 halErrorMap[msg.error_code] :
2495 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2496
2497 int streamId = 0;
2498 if (msg.error_stream != NULL) {
2499 Camera3Stream *stream =
2500 Camera3Stream::cast(msg.error_stream);
2501 streamId = stream->getId();
2502 }
2503 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2504 mId, __FUNCTION__, msg.frame_number,
2505 streamId, msg.error_code);
2506
2507 CaptureResultExtras resultExtras;
2508 switch (errorCode) {
2509 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2510 // SET_ERR calls notifyError
2511 SET_ERR("Camera HAL reported serious device error");
2512 break;
2513 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2514 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2515 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2516 {
2517 Mutex::Autolock l(mInFlightLock);
2518 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2519 if (idx >= 0) {
2520 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2521 r.requestStatus = msg.error_code;
2522 resultExtras = r.resultExtras;
2523 } else {
2524 resultExtras.frameNumber = msg.frame_number;
2525 ALOGE("Camera %d: %s: cannot find in-flight request on "
2526 "frame %" PRId64 " error", mId, __FUNCTION__,
2527 resultExtras.frameNumber);
2528 }
2529 }
2530 if (listener != NULL) {
2531 listener->notifyError(errorCode, resultExtras);
2532 } else {
2533 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2534 }
2535 break;
2536 default:
2537 // SET_ERR calls notifyError
2538 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2539 break;
2540 }
2541}
2542
2543void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2544 NotificationListener *listener) {
2545 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002546
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002547 // Set timestamp for the request in the in-flight tracking
2548 // and get the request ID to send upstream
2549 {
2550 Mutex::Autolock l(mInFlightLock);
2551 idx = mInFlightMap.indexOfKey(msg.frame_number);
2552 if (idx >= 0) {
2553 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002554
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002555 // Verify ordering of shutter notifications
2556 {
2557 Mutex::Autolock l(mOutputLock);
2558 // TODO: need to track errors for tighter bounds on expected frame number.
2559 if (r.hasInputBuffer) {
2560 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2561 SET_ERR("Shutter notification out-of-order. Expected "
2562 "notification for frame %d, got frame %d",
2563 mNextReprocessShutterFrameNumber, msg.frame_number);
2564 return;
2565 }
2566 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2567 } else {
2568 if (msg.frame_number < mNextShutterFrameNumber) {
2569 SET_ERR("Shutter notification out-of-order. Expected "
2570 "notification for frame %d, got frame %d",
2571 mNextShutterFrameNumber, msg.frame_number);
2572 return;
2573 }
2574 mNextShutterFrameNumber = msg.frame_number + 1;
2575 }
2576 }
2577
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002578 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2579 mId, __FUNCTION__,
2580 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2581 // Call listener, if any
2582 if (listener != NULL) {
2583 listener->notifyShutter(r.resultExtras, msg.timestamp);
2584 }
2585
2586 r.shutterTimestamp = msg.timestamp;
2587
2588 // send pending result and buffers
2589 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002590 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002591 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002592 returnOutputBuffers(r.pendingOutputBuffers.array(),
2593 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2594 r.pendingOutputBuffers.clear();
2595
2596 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002597 }
2598 }
2599 if (idx < 0) {
2600 SET_ERR("Shutter notification for non-existent frame number %d",
2601 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002602 }
2603}
2604
2605
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002606CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002607 ALOGV("%s", __FUNCTION__);
2608
Igor Murashkin1e479c02013-09-06 16:55:14 -07002609 CameraMetadata retVal;
2610
2611 if (mRequestThread != NULL) {
2612 retVal = mRequestThread->getLatestRequest();
2613 }
2614
Igor Murashkin1e479c02013-09-06 16:55:14 -07002615 return retVal;
2616}
2617
Jianing Weicb0652e2014-03-12 18:29:36 -07002618
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002619/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002620 * RequestThread inner class methods
2621 */
2622
2623Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002624 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002625 camera3_device_t *hal3Device,
2626 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002627 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002628 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002629 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002630 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002631 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002632 mReconfigured(false),
2633 mDoPause(false),
2634 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002635 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002636 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002637 mCurrentAfTriggerId(0),
2638 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002639 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2640 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002641 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002642}
2643
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002644void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002645 NotificationListener *listener) {
2646 Mutex::Autolock l(mRequestLock);
2647 mListener = listener;
2648}
2649
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002650void Camera3Device::RequestThread::configurationComplete() {
2651 Mutex::Autolock l(mRequestLock);
2652 mReconfigured = true;
2653}
2654
Jianing Wei90e59c92014-03-12 18:29:36 -07002655status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002656 List<sp<CaptureRequest> > &requests,
2657 /*out*/
2658 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002659 Mutex::Autolock l(mRequestLock);
2660 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2661 ++it) {
2662 mRequestQueue.push_back(*it);
2663 }
2664
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002665 if (lastFrameNumber != NULL) {
2666 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2667 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2668 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2669 *lastFrameNumber);
2670 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002671
Jianing Wei90e59c92014-03-12 18:29:36 -07002672 unpauseForNewRequests();
2673
2674 return OK;
2675}
2676
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002677
2678status_t Camera3Device::RequestThread::queueTrigger(
2679 RequestTrigger trigger[],
2680 size_t count) {
2681
2682 Mutex::Autolock l(mTriggerMutex);
2683 status_t ret;
2684
2685 for (size_t i = 0; i < count; ++i) {
2686 ret = queueTriggerLocked(trigger[i]);
2687
2688 if (ret != OK) {
2689 return ret;
2690 }
2691 }
2692
2693 return OK;
2694}
2695
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002696int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2697 sp<Camera3Device> d = device.promote();
2698 if (d != NULL) return d->mId;
2699 return 0;
2700}
2701
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002702status_t Camera3Device::RequestThread::queueTriggerLocked(
2703 RequestTrigger trigger) {
2704
2705 uint32_t tag = trigger.metadataTag;
2706 ssize_t index = mTriggerMap.indexOfKey(tag);
2707
2708 switch (trigger.getTagType()) {
2709 case TYPE_BYTE:
2710 // fall-through
2711 case TYPE_INT32:
2712 break;
2713 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002714 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2715 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002716 return INVALID_OPERATION;
2717 }
2718
2719 /**
2720 * Collect only the latest trigger, since we only have 1 field
2721 * in the request settings per trigger tag, and can't send more than 1
2722 * trigger per request.
2723 */
2724 if (index != NAME_NOT_FOUND) {
2725 mTriggerMap.editValueAt(index) = trigger;
2726 } else {
2727 mTriggerMap.add(tag, trigger);
2728 }
2729
2730 return OK;
2731}
2732
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002733status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002734 const RequestList &requests,
2735 /*out*/
2736 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002737 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002738 if (lastFrameNumber != NULL) {
2739 *lastFrameNumber = mRepeatingLastFrameNumber;
2740 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002741 mRepeatingRequests.clear();
2742 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2743 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002744
2745 unpauseForNewRequests();
2746
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002747 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002748 return OK;
2749}
2750
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002751bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2752 if (mRepeatingRequests.empty()) {
2753 return false;
2754 }
2755 int32_t requestId = requestIn->mResultExtras.requestId;
2756 const RequestList &repeatRequests = mRepeatingRequests;
2757 // All repeating requests are guaranteed to have same id so only check first quest
2758 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2759 return (firstRequest->mResultExtras.requestId == requestId);
2760}
2761
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002762status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002763 Mutex::Autolock l(mRequestLock);
2764 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002765 if (lastFrameNumber != NULL) {
2766 *lastFrameNumber = mRepeatingLastFrameNumber;
2767 }
2768 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002769 return OK;
2770}
2771
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002772status_t Camera3Device::RequestThread::clear(
2773 NotificationListener *listener,
2774 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002775 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002776 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002777
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002778 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002779
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002780 // Send errors for all requests pending in the request queue, including
2781 // pending repeating requests
2782 if (listener != NULL) {
2783 for (RequestList::iterator it = mRequestQueue.begin();
2784 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002785 // Abort the input buffers for reprocess requests.
2786 if ((*it)->mInputStream != NULL) {
2787 camera3_stream_buffer_t inputBuffer;
2788 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2789 if (res != OK) {
2790 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2791 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2792 } else {
2793 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2794 if (res != OK) {
2795 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2796 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2797 }
2798 }
2799 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002800 // Set the frame number this request would have had, if it
2801 // had been submitted; this frame number will not be reused.
2802 // The requestId and burstId fields were set when the request was
2803 // submitted originally (in convertMetadataListToRequestListLocked)
2804 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2805 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2806 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002807 }
2808 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002809 mRequestQueue.clear();
2810 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002811 if (lastFrameNumber != NULL) {
2812 *lastFrameNumber = mRepeatingLastFrameNumber;
2813 }
2814 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002815 return OK;
2816}
2817
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002818status_t Camera3Device::RequestThread::flush() {
2819 ATRACE_CALL();
2820 Mutex::Autolock l(mFlushLock);
2821
2822 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2823 return mHal3Device->ops->flush(mHal3Device);
2824 }
2825
2826 return -ENOTSUP;
2827}
2828
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002829void Camera3Device::RequestThread::setPaused(bool paused) {
2830 Mutex::Autolock l(mPauseLock);
2831 mDoPause = paused;
2832 mDoPauseSignal.signal();
2833}
2834
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002835status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2836 int32_t requestId, nsecs_t timeout) {
2837 Mutex::Autolock l(mLatestRequestMutex);
2838 status_t res;
2839 while (mLatestRequestId != requestId) {
2840 nsecs_t startTime = systemTime();
2841
2842 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2843 if (res != OK) return res;
2844
2845 timeout -= (systemTime() - startTime);
2846 }
2847
2848 return OK;
2849}
2850
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002851void Camera3Device::RequestThread::requestExit() {
2852 // Call parent to set up shutdown
2853 Thread::requestExit();
2854 // The exit from any possible waits
2855 mDoPauseSignal.signal();
2856 mRequestSignal.signal();
2857}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002858
Chien-Yu Chend196d612015-06-22 19:49:01 -07002859
2860/**
2861 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2862 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2863 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2864 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2865 * request.
2866 */
2867void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2868 request->mAeTriggerCancelOverride.applyAeLock = false;
2869 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2870
2871 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2872 return;
2873 }
2874
2875 camera_metadata_entry_t aePrecaptureTrigger =
2876 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2877 if (aePrecaptureTrigger.count > 0 &&
2878 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2879 // Always override CANCEL to IDLE
2880 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2881 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2882 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2883 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2884 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2885
2886 if (mAeLockAvailable == true) {
2887 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2888 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2889 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2890 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2891 request->mAeTriggerCancelOverride.applyAeLock = true;
2892 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2893 }
2894 }
2895 }
2896}
2897
2898/**
2899 * Override result metadata for cancelling AE precapture trigger applied in
2900 * handleAePrecaptureCancelRequest().
2901 */
2902void Camera3Device::overrideResultForPrecaptureCancel(
2903 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2904 if (aeTriggerCancelOverride.applyAeLock) {
2905 // Only devices <= v3.2 should have this override
2906 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2907 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2908 }
2909
2910 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2911 // Only devices <= v3.2 should have this override
2912 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2913 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2914 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2915 }
2916}
2917
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002918bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002919 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002920 status_t res;
2921
2922 // Handle paused state.
2923 if (waitIfPaused()) {
2924 return true;
2925 }
2926
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002927 // Wait for the next batch of requests.
2928 waitForNextRequestBatch();
2929 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002930 return true;
2931 }
2932
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002933 // Get the latest request ID, if any
2934 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002935 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002936 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002937 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002938 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002939 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002940 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
2941 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002942 }
2943
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002944 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002945 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002946 if (res == TIMED_OUT) {
2947 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002948 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002949 return true;
2950 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002951 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002952 return false;
2953 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002954
Zhijun Hecc27e112013-10-03 16:12:43 -07002955 // Inform waitUntilRequestProcessed thread of a new request ID
2956 {
2957 Mutex::Autolock al(mLatestRequestMutex);
2958
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002959 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07002960 mLatestRequestSignal.signal();
2961 }
2962
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002963 // Submit a batch of requests to HAL.
2964 // Use flush lock only when submitting multilple requests in a batch.
2965 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
2966 // which may take a long time to finish so synchronizing flush() and
2967 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
2968 // For now, only synchronize for high speed recording and we should figure something out for
2969 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002970 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002971
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002972 if (useFlushLock) {
2973 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002974 }
2975
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002976 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002977 mNextRequests.size());
2978 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002979 // Submit request and block until ready for next one
2980 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
2981 ATRACE_BEGIN("camera3->process_capture_request");
2982 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
2983 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07002984
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002985 if (res != OK) {
2986 // Should only get a failure here for malformed requests or device-level
2987 // errors, so consider all errors fatal. Bad metadata failures should
2988 // come through notify.
2989 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
2990 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
2991 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002992 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002993 if (useFlushLock) {
2994 mFlushLock.unlock();
2995 }
2996 return false;
2997 }
2998
2999 // Mark that the request has be submitted successfully.
3000 nextRequest.submitted = true;
3001
3002 // Update the latest request sent to HAL
3003 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3004 Mutex::Autolock al(mLatestRequestMutex);
3005
3006 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3007 mLatestRequest.acquire(cloned);
3008 }
3009
3010 if (nextRequest.halRequest.settings != NULL) {
3011 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3012 }
3013
3014 // Remove any previously queued triggers (after unlock)
3015 res = removeTriggers(mPrevRequest);
3016 if (res != OK) {
3017 SET_ERR("RequestThread: Unable to remove triggers "
3018 "(capture request %d, HAL device: %s (%d)",
3019 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003020 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003021 if (useFlushLock) {
3022 mFlushLock.unlock();
3023 }
3024 return false;
3025 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003026 }
3027
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003028 if (useFlushLock) {
3029 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003030 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003031
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003032 // Unset as current request
3033 {
3034 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003035 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003036 }
3037
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003038 return true;
3039}
3040
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003041status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003042 ATRACE_CALL();
3043
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003044 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003045 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3046 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3047 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3048
3049 // Prepare a request to HAL
3050 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3051
3052 // Insert any queued triggers (before metadata is locked)
3053 status_t res = insertTriggers(captureRequest);
3054
3055 if (res < 0) {
3056 SET_ERR("RequestThread: Unable to insert triggers "
3057 "(capture request %d, HAL device: %s (%d)",
3058 halRequest->frame_number, strerror(-res), res);
3059 return INVALID_OPERATION;
3060 }
3061 int triggerCount = res;
3062 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3063 mPrevTriggers = triggerCount;
3064
3065 // If the request is the same as last, or we had triggers last time
3066 if (mPrevRequest != captureRequest || triggersMixedIn) {
3067 /**
3068 * HAL workaround:
3069 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3070 */
3071 res = addDummyTriggerIds(captureRequest);
3072 if (res != OK) {
3073 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3074 "(capture request %d, HAL device: %s (%d)",
3075 halRequest->frame_number, strerror(-res), res);
3076 return INVALID_OPERATION;
3077 }
3078
3079 /**
3080 * The request should be presorted so accesses in HAL
3081 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3082 */
3083 captureRequest->mSettings.sort();
3084 halRequest->settings = captureRequest->mSettings.getAndLock();
3085 mPrevRequest = captureRequest;
3086 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3087
3088 IF_ALOGV() {
3089 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3090 find_camera_metadata_ro_entry(
3091 halRequest->settings,
3092 ANDROID_CONTROL_AF_TRIGGER,
3093 &e
3094 );
3095 if (e.count > 0) {
3096 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3097 __FUNCTION__,
3098 halRequest->frame_number,
3099 e.data.u8[0]);
3100 }
3101 }
3102 } else {
3103 // leave request.settings NULL to indicate 'reuse latest given'
3104 ALOGVV("%s: Request settings are REUSED",
3105 __FUNCTION__);
3106 }
3107
3108 uint32_t totalNumBuffers = 0;
3109
3110 // Fill in buffers
3111 if (captureRequest->mInputStream != NULL) {
3112 halRequest->input_buffer = &captureRequest->mInputBuffer;
3113 totalNumBuffers += 1;
3114 } else {
3115 halRequest->input_buffer = NULL;
3116 }
3117
3118 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3119 captureRequest->mOutputStreams.size());
3120 halRequest->output_buffers = outputBuffers->array();
3121 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3122 res = captureRequest->mOutputStreams.editItemAt(i)->
3123 getBuffer(&outputBuffers->editItemAt(i));
3124 if (res != OK) {
3125 // Can't get output buffer from gralloc queue - this could be due to
3126 // abandoned queue or other consumer misbehavior, so not a fatal
3127 // error
3128 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3129 " %s (%d)", strerror(-res), res);
3130
3131 return TIMED_OUT;
3132 }
3133 halRequest->num_output_buffers++;
3134 }
3135 totalNumBuffers += halRequest->num_output_buffers;
3136
3137 // Log request in the in-flight queue
3138 sp<Camera3Device> parent = mParent.promote();
3139 if (parent == NULL) {
3140 // Should not happen, and nowhere to send errors to, so just log it
3141 CLOGE("RequestThread: Parent is gone");
3142 return INVALID_OPERATION;
3143 }
3144 res = parent->registerInFlight(halRequest->frame_number,
3145 totalNumBuffers, captureRequest->mResultExtras,
3146 /*hasInput*/halRequest->input_buffer != NULL,
3147 captureRequest->mAeTriggerCancelOverride);
3148 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3149 ", burstId = %" PRId32 ".",
3150 __FUNCTION__,
3151 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3152 captureRequest->mResultExtras.burstId);
3153 if (res != OK) {
3154 SET_ERR("RequestThread: Unable to register new in-flight request:"
3155 " %s (%d)", strerror(-res), res);
3156 return INVALID_OPERATION;
3157 }
3158 }
3159
3160 return OK;
3161}
3162
Igor Murashkin1e479c02013-09-06 16:55:14 -07003163CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3164 Mutex::Autolock al(mLatestRequestMutex);
3165
3166 ALOGV("RequestThread::%s", __FUNCTION__);
3167
3168 return mLatestRequest;
3169}
3170
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003171bool Camera3Device::RequestThread::isStreamPending(
3172 sp<Camera3StreamInterface>& stream) {
3173 Mutex::Autolock l(mRequestLock);
3174
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003175 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003176 if (!nextRequest.submitted) {
3177 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3178 if (stream == s) return true;
3179 }
3180 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003181 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003182 }
3183
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003184 for (const auto& request : mRequestQueue) {
3185 for (const auto& s : request->mOutputStreams) {
3186 if (stream == s) return true;
3187 }
3188 if (stream == request->mInputStream) return true;
3189 }
3190
3191 for (const auto& request : mRepeatingRequests) {
3192 for (const auto& s : request->mOutputStreams) {
3193 if (stream == s) return true;
3194 }
3195 if (stream == request->mInputStream) return true;
3196 }
3197
3198 return false;
3199}
Jianing Weicb0652e2014-03-12 18:29:36 -07003200
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003201void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3202 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003203 return;
3204 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003205
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003206 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003207 // Skip the ones that have been submitted successfully.
3208 if (nextRequest.submitted) {
3209 continue;
3210 }
3211
3212 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3213 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3214 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3215
3216 if (halRequest->settings != NULL) {
3217 captureRequest->mSettings.unlock(halRequest->settings);
3218 }
3219
3220 if (captureRequest->mInputStream != NULL) {
3221 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3222 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3223 }
3224
3225 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3226 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3227 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3228 }
3229
3230 if (sendRequestError) {
3231 Mutex::Autolock l(mRequestLock);
3232 if (mListener != NULL) {
3233 mListener->notifyError(
3234 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3235 captureRequest->mResultExtras);
3236 }
3237 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003238 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003239
3240 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003241 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003242}
3243
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003244void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003245 // Optimized a bit for the simple steady-state case (single repeating
3246 // request), to avoid putting that request in the queue temporarily.
3247 Mutex::Autolock l(mRequestLock);
3248
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003249 assert(mNextRequests.empty());
3250
3251 NextRequest nextRequest;
3252 nextRequest.captureRequest = waitForNextRequestLocked();
3253 if (nextRequest.captureRequest == nullptr) {
3254 return;
3255 }
3256
3257 nextRequest.halRequest = camera3_capture_request_t();
3258 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003259 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003260
3261 // Wait for additional requests
3262 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3263
3264 for (size_t i = 1; i < batchSize; i++) {
3265 NextRequest additionalRequest;
3266 additionalRequest.captureRequest = waitForNextRequestLocked();
3267 if (additionalRequest.captureRequest == nullptr) {
3268 break;
3269 }
3270
3271 additionalRequest.halRequest = camera3_capture_request_t();
3272 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003273 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003274 }
3275
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003276 if (mNextRequests.size() < batchSize) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003277 ALOGE("RequestThread: only get %d out of %d requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003278 mNextRequests.size(), batchSize);
3279 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003280 }
3281
3282 return;
3283}
3284
3285sp<Camera3Device::CaptureRequest>
3286 Camera3Device::RequestThread::waitForNextRequestLocked() {
3287 status_t res;
3288 sp<CaptureRequest> nextRequest;
3289
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003290 while (mRequestQueue.empty()) {
3291 if (!mRepeatingRequests.empty()) {
3292 // Always atomically enqueue all requests in a repeating request
3293 // list. Guarantees a complete in-sequence set of captures to
3294 // application.
3295 const RequestList &requests = mRepeatingRequests;
3296 RequestList::const_iterator firstRequest =
3297 requests.begin();
3298 nextRequest = *firstRequest;
3299 mRequestQueue.insert(mRequestQueue.end(),
3300 ++firstRequest,
3301 requests.end());
3302 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003303
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003304 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003305
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003306 break;
3307 }
3308
3309 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3310
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003311 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3312 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003313 Mutex::Autolock pl(mPauseLock);
3314 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003315 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003316 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003317 // Let the tracker know
3318 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3319 if (statusTracker != 0) {
3320 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3321 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003322 }
3323 // Stop waiting for now and let thread management happen
3324 return NULL;
3325 }
3326 }
3327
3328 if (nextRequest == NULL) {
3329 // Don't have a repeating request already in hand, so queue
3330 // must have an entry now.
3331 RequestList::iterator firstRequest =
3332 mRequestQueue.begin();
3333 nextRequest = *firstRequest;
3334 mRequestQueue.erase(firstRequest);
3335 }
3336
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003337 // In case we've been unpaused by setPaused clearing mDoPause, need to
3338 // update internal pause state (capture/setRepeatingRequest unpause
3339 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003340 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003341 if (mPaused) {
3342 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3343 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3344 if (statusTracker != 0) {
3345 statusTracker->markComponentActive(mStatusId);
3346 }
3347 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003348 mPaused = false;
3349
3350 // Check if we've reconfigured since last time, and reset the preview
3351 // request if so. Can't use 'NULL request == repeat' across configure calls.
3352 if (mReconfigured) {
3353 mPrevRequest.clear();
3354 mReconfigured = false;
3355 }
3356
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003357 if (nextRequest != NULL) {
3358 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003359 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3360 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003361
3362 // Since RequestThread::clear() removes buffers from the input stream,
3363 // get the right buffer here before unlocking mRequestLock
3364 if (nextRequest->mInputStream != NULL) {
3365 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3366 if (res != OK) {
3367 // Can't get input buffer from gralloc queue - this could be due to
3368 // disconnected queue or other producer misbehavior, so not a fatal
3369 // error
3370 ALOGE("%s: Can't get input buffer, skipping request:"
3371 " %s (%d)", __FUNCTION__, strerror(-res), res);
3372 if (mListener != NULL) {
3373 mListener->notifyError(
3374 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3375 nextRequest->mResultExtras);
3376 }
3377 return NULL;
3378 }
3379 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003380 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003381
3382 handleAePrecaptureCancelRequest(nextRequest);
3383
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003384 return nextRequest;
3385}
3386
3387bool Camera3Device::RequestThread::waitIfPaused() {
3388 status_t res;
3389 Mutex::Autolock l(mPauseLock);
3390 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003391 if (mPaused == false) {
3392 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003393 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3394 // Let the tracker know
3395 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3396 if (statusTracker != 0) {
3397 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3398 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003399 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003400
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003401 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003402 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003403 return true;
3404 }
3405 }
3406 // We don't set mPaused to false here, because waitForNextRequest needs
3407 // to further manage the paused state in case of starvation.
3408 return false;
3409}
3410
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003411void Camera3Device::RequestThread::unpauseForNewRequests() {
3412 // With work to do, mark thread as unpaused.
3413 // If paused by request (setPaused), don't resume, to avoid
3414 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003415 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003416 Mutex::Autolock p(mPauseLock);
3417 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003418 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3419 if (mPaused) {
3420 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3421 if (statusTracker != 0) {
3422 statusTracker->markComponentActive(mStatusId);
3423 }
3424 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003425 mPaused = false;
3426 }
3427}
3428
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003429void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3430 sp<Camera3Device> parent = mParent.promote();
3431 if (parent != NULL) {
3432 va_list args;
3433 va_start(args, fmt);
3434
3435 parent->setErrorStateV(fmt, args);
3436
3437 va_end(args);
3438 }
3439}
3440
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003441status_t Camera3Device::RequestThread::insertTriggers(
3442 const sp<CaptureRequest> &request) {
3443
3444 Mutex::Autolock al(mTriggerMutex);
3445
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003446 sp<Camera3Device> parent = mParent.promote();
3447 if (parent == NULL) {
3448 CLOGE("RequestThread: Parent is gone");
3449 return DEAD_OBJECT;
3450 }
3451
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003452 CameraMetadata &metadata = request->mSettings;
3453 size_t count = mTriggerMap.size();
3454
3455 for (size_t i = 0; i < count; ++i) {
3456 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003457 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003458
3459 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3460 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3461 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003462 if (isAeTrigger) {
3463 request->mResultExtras.precaptureTriggerId = triggerId;
3464 mCurrentPreCaptureTriggerId = triggerId;
3465 } else {
3466 request->mResultExtras.afTriggerId = triggerId;
3467 mCurrentAfTriggerId = triggerId;
3468 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003469 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3470 continue; // Trigger ID tag is deprecated since device HAL 3.2
3471 }
3472 }
3473
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003474 camera_metadata_entry entry = metadata.find(tag);
3475
3476 if (entry.count > 0) {
3477 /**
3478 * Already has an entry for this trigger in the request.
3479 * Rewrite it with our requested trigger value.
3480 */
3481 RequestTrigger oldTrigger = trigger;
3482
3483 oldTrigger.entryValue = entry.data.u8[0];
3484
3485 mTriggerReplacedMap.add(tag, oldTrigger);
3486 } else {
3487 /**
3488 * More typical, no trigger entry, so we just add it
3489 */
3490 mTriggerRemovedMap.add(tag, trigger);
3491 }
3492
3493 status_t res;
3494
3495 switch (trigger.getTagType()) {
3496 case TYPE_BYTE: {
3497 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3498 res = metadata.update(tag,
3499 &entryValue,
3500 /*count*/1);
3501 break;
3502 }
3503 case TYPE_INT32:
3504 res = metadata.update(tag,
3505 &trigger.entryValue,
3506 /*count*/1);
3507 break;
3508 default:
3509 ALOGE("%s: Type not supported: 0x%x",
3510 __FUNCTION__,
3511 trigger.getTagType());
3512 return INVALID_OPERATION;
3513 }
3514
3515 if (res != OK) {
3516 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3517 ", value %d", __FUNCTION__, trigger.getTagName(),
3518 trigger.entryValue);
3519 return res;
3520 }
3521
3522 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3523 trigger.getTagName(),
3524 trigger.entryValue);
3525 }
3526
3527 mTriggerMap.clear();
3528
3529 return count;
3530}
3531
3532status_t Camera3Device::RequestThread::removeTriggers(
3533 const sp<CaptureRequest> &request) {
3534 Mutex::Autolock al(mTriggerMutex);
3535
3536 CameraMetadata &metadata = request->mSettings;
3537
3538 /**
3539 * Replace all old entries with their old values.
3540 */
3541 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3542 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3543
3544 status_t res;
3545
3546 uint32_t tag = trigger.metadataTag;
3547 switch (trigger.getTagType()) {
3548 case TYPE_BYTE: {
3549 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3550 res = metadata.update(tag,
3551 &entryValue,
3552 /*count*/1);
3553 break;
3554 }
3555 case TYPE_INT32:
3556 res = metadata.update(tag,
3557 &trigger.entryValue,
3558 /*count*/1);
3559 break;
3560 default:
3561 ALOGE("%s: Type not supported: 0x%x",
3562 __FUNCTION__,
3563 trigger.getTagType());
3564 return INVALID_OPERATION;
3565 }
3566
3567 if (res != OK) {
3568 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3569 ", trigger value %d", __FUNCTION__,
3570 trigger.getTagName(), trigger.entryValue);
3571 return res;
3572 }
3573 }
3574 mTriggerReplacedMap.clear();
3575
3576 /**
3577 * Remove all new entries.
3578 */
3579 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3580 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3581 status_t res = metadata.erase(trigger.metadataTag);
3582
3583 if (res != OK) {
3584 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3585 ", trigger value %d", __FUNCTION__,
3586 trigger.getTagName(), trigger.entryValue);
3587 return res;
3588 }
3589 }
3590 mTriggerRemovedMap.clear();
3591
3592 return OK;
3593}
3594
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003595status_t Camera3Device::RequestThread::addDummyTriggerIds(
3596 const sp<CaptureRequest> &request) {
3597 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3598 static const int32_t dummyTriggerId = 1;
3599 status_t res;
3600
3601 CameraMetadata &metadata = request->mSettings;
3602
3603 // If AF trigger is active, insert a dummy AF trigger ID if none already
3604 // exists
3605 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3606 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3607 if (afTrigger.count > 0 &&
3608 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3609 afId.count == 0) {
3610 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3611 if (res != OK) return res;
3612 }
3613
3614 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3615 // if none already exists
3616 camera_metadata_entry pcTrigger =
3617 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3618 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3619 if (pcTrigger.count > 0 &&
3620 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3621 pcId.count == 0) {
3622 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3623 &dummyTriggerId, 1);
3624 if (res != OK) return res;
3625 }
3626
3627 return OK;
3628}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003629
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003630/**
3631 * PreparerThread inner class methods
3632 */
3633
3634Camera3Device::PreparerThread::PreparerThread() :
3635 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3636}
3637
3638Camera3Device::PreparerThread::~PreparerThread() {
3639 Thread::requestExitAndWait();
3640 if (mCurrentStream != nullptr) {
3641 mCurrentStream->cancelPrepare();
3642 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3643 mCurrentStream.clear();
3644 }
3645 clear();
3646}
3647
Ruben Brunkc78ac262015-08-13 17:58:46 -07003648status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003649 status_t res;
3650
3651 Mutex::Autolock l(mLock);
3652
Ruben Brunkc78ac262015-08-13 17:58:46 -07003653 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003654 if (res == OK) {
3655 // No preparation needed, fire listener right off
3656 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3657 if (mListener) {
3658 mListener->notifyPrepared(stream->getId());
3659 }
3660 return OK;
3661 } else if (res != NOT_ENOUGH_DATA) {
3662 return res;
3663 }
3664
3665 // Need to prepare, start up thread if necessary
3666 if (!mActive) {
3667 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3668 // isn't running
3669 Thread::requestExitAndWait();
3670 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3671 if (res != OK) {
3672 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3673 if (mListener) {
3674 mListener->notifyPrepared(stream->getId());
3675 }
3676 return res;
3677 }
3678 mCancelNow = false;
3679 mActive = true;
3680 ALOGV("%s: Preparer stream started", __FUNCTION__);
3681 }
3682
3683 // queue up the work
3684 mPendingStreams.push_back(stream);
3685 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3686
3687 return OK;
3688}
3689
3690status_t Camera3Device::PreparerThread::clear() {
3691 status_t res;
3692
3693 Mutex::Autolock l(mLock);
3694
3695 for (const auto& stream : mPendingStreams) {
3696 stream->cancelPrepare();
3697 }
3698 mPendingStreams.clear();
3699 mCancelNow = true;
3700
3701 return OK;
3702}
3703
3704void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3705 Mutex::Autolock l(mLock);
3706 mListener = listener;
3707}
3708
3709bool Camera3Device::PreparerThread::threadLoop() {
3710 status_t res;
3711 {
3712 Mutex::Autolock l(mLock);
3713 if (mCurrentStream == nullptr) {
3714 // End thread if done with work
3715 if (mPendingStreams.empty()) {
3716 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3717 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3718 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3719 mActive = false;
3720 return false;
3721 }
3722
3723 // Get next stream to prepare
3724 auto it = mPendingStreams.begin();
3725 mCurrentStream = *it;
3726 mPendingStreams.erase(it);
3727 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3728 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3729 } else if (mCancelNow) {
3730 mCurrentStream->cancelPrepare();
3731 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3732 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3733 mCurrentStream.clear();
3734 mCancelNow = false;
3735 return true;
3736 }
3737 }
3738
3739 res = mCurrentStream->prepareNextBuffer();
3740 if (res == NOT_ENOUGH_DATA) return true;
3741 if (res != OK) {
3742 // Something bad happened; try to recover by cancelling prepare and
3743 // signalling listener anyway
3744 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3745 mCurrentStream->getId(), res, strerror(-res));
3746 mCurrentStream->cancelPrepare();
3747 }
3748
3749 // This stream has finished, notify listener
3750 Mutex::Autolock l(mLock);
3751 if (mListener) {
3752 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3753 mCurrentStream->getId());
3754 mListener->notifyPrepared(mCurrentStream->getId());
3755 }
3756
3757 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3758 mCurrentStream.clear();
3759
3760 return true;
3761}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003762
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003763/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003764 * Static callback forwarding methods from HAL to instance
3765 */
3766
3767void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3768 const camera3_capture_result *result) {
3769 Camera3Device *d =
3770 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003771
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003772 d->processCaptureResult(result);
3773}
3774
3775void Camera3Device::sNotify(const camera3_callback_ops *cb,
3776 const camera3_notify_msg *msg) {
3777 Camera3Device *d =
3778 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3779 d->notify(msg);
3780}
3781
3782}; // namespace android