blob: bbe7317e0ccb139547a1eda720b106b2166d316d [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
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046#include <android/hardware/camera2/ICameraDeviceUser.h>
47
Igor Murashkinff3e31d2013-10-23 16:40:06 -070048#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070049#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070050#include "device3/Camera3Device.h"
51#include "device3/Camera3OutputStream.h"
52#include "device3/Camera3InputStream.h"
53#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070054#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070055#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080056
57using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058
59namespace android {
60
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061Camera3Device::Camera3Device(int id):
62 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070063 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080064 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070065 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070066 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070067 mUsePartialResult(false),
68 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080069 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070070 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070071 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070072 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070073 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070074 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080075{
76 ATRACE_CALL();
77 camera3_callback_ops::notify = &sNotify;
78 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
79 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
80}
81
82Camera3Device::~Camera3Device()
83{
84 ATRACE_CALL();
85 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
86 disconnect();
87}
88
Igor Murashkin71381052013-03-04 14:53:08 -080089int Camera3Device::getId() const {
90 return mId;
91}
92
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080093/**
94 * CameraDeviceBase interface
95 */
96
Yin-Chia Yehe074a932015-01-30 10:29:02 -080097status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080098{
99 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700100 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800101 Mutex::Autolock l(mLock);
102
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800104 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700105 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800106 return INVALID_OPERATION;
107 }
108
109 /** Open HAL device */
110
111 status_t res;
112 String8 deviceName = String8::format("%d", mId);
113
114 camera3_device_t *device;
115
Zhijun He213ce792013-11-19 08:45:15 -0800116 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800117 res = module->open(deviceName.string(),
118 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800119 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800120
121 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800123 return res;
124 }
125
126 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700127 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700128 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700129 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700130 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800131 device->common.version);
132 device->common.close(&device->common);
133 return BAD_VALUE;
134 }
135
136 camera_info info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800137 res = module->getCameraInfo(mId, &info);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 if (res != OK) return res;
139
140 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700141 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
142 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700143 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800144 device->common.close(&device->common);
145 return BAD_VALUE;
146 }
147
148 /** Initialize device with callback functions */
149
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700150 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700152 ATRACE_END();
153
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800154 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700155 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
156 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800157 device->common.close(&device->common);
158 return BAD_VALUE;
159 }
160
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700161 /** Start up status tracker thread */
162 mStatusTracker = new StatusTracker(this);
163 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
164 if (res != OK) {
165 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
166 strerror(-res), res);
167 device->common.close(&device->common);
168 mStatusTracker.clear();
169 return res;
170 }
171
Zhijun He125684a2015-12-26 15:07:30 -0800172 /** Create buffer manager */
173 mBufferManager = new Camera3BufferManager();
174
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700175 bool aeLockAvailable = false;
176 camera_metadata_ro_entry aeLockAvailableEntry;
177 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
178 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
179 if (res == OK && aeLockAvailableEntry.count > 0) {
180 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
181 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
182 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800183
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700184 /** Start up request queue thread */
185 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800186 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700188 SET_ERR_L("Unable to start request queue thread: %s (%d)",
189 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800191 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 return res;
193 }
194
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700195 mPreparerThread = new PreparerThread();
196
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800197 /** Everything is good to go */
198
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700199 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800200 mDeviceInfo = info.static_camera_characteristics;
201 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700202
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700203 // Determine whether we need to derive sensitivity boost values for older devices.
204 // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
205 // be listed (as the default value 100)
206 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_4 &&
207 mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
208 mDerivePostRawSensKey = true;
209 }
210
Ruben Brunk183f0562015-08-12 12:55:02 -0700211 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800212 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700213 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700214 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700215 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800216
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800217 // Measure the clock domain offset between camera and video/hw_composer
218 camera_metadata_entry timestampSource =
219 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
220 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
221 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
222 mTimestampOffset = getMonoToBoottimeOffset();
223 }
224
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700225 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700226 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
227 camera_metadata_entry partialResultsCount =
228 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
229 if (partialResultsCount.count > 0) {
230 mNumPartialResults = partialResultsCount.data.i32[0];
231 mUsePartialResult = (mNumPartialResults > 1);
232 }
233 } else {
234 camera_metadata_entry partialResultsQuirk =
235 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
236 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
237 mUsePartialResult = true;
238 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700239 }
240
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700241 camera_metadata_entry configs =
242 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
243 for (uint32_t i = 0; i < configs.count; i += 4) {
244 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
245 configs.data.i32[i + 3] ==
246 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
247 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
248 configs.data.i32[i + 2]));
249 }
250 }
251
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800252 return OK;
253}
254
255status_t Camera3Device::disconnect() {
256 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700257 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800258
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800259 ALOGV("%s: E", __FUNCTION__);
260
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700261 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800262
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700263 {
264 Mutex::Autolock l(mLock);
265 if (mStatus == STATUS_UNINITIALIZED) return res;
266
267 if (mStatus == STATUS_ACTIVE ||
268 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
269 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700270 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700271 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700272 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700273 } else {
274 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
275 if (res != OK) {
276 SET_ERR_L("Timeout waiting for HAL to drain");
277 // Continue to close device even in case of error
278 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700279 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800280 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800281
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700282 if (mStatus == STATUS_ERROR) {
283 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700284 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700285
286 if (mStatusTracker != NULL) {
287 mStatusTracker->requestExit();
288 }
289
290 if (mRequestThread != NULL) {
291 mRequestThread->requestExit();
292 }
293
294 mOutputStreams.clear();
295 mInputStream.clear();
296 }
297
298 // Joining done without holding mLock, otherwise deadlocks may ensue
299 // as the threads try to access parent state
300 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
301 // HAL may be in a bad state, so waiting for request thread
302 // (which may be stuck in the HAL processCaptureRequest call)
303 // could be dangerous.
304 mRequestThread->join();
305 }
306
307 if (mStatusTracker != NULL) {
308 mStatusTracker->join();
309 }
310
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700311 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700312 {
313 Mutex::Autolock l(mLock);
314
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800315 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700316 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800317 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800318
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700319 hal3Device = mHal3Device;
320 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800321
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700322 // Call close without internal mutex held, as the HAL close may need to
323 // wait on assorted callbacks,etc, to complete before it can return.
324 if (hal3Device != NULL) {
325 ATRACE_BEGIN("camera3->close");
326 hal3Device->common.close(&hal3Device->common);
327 ATRACE_END();
328 }
329
330 {
331 Mutex::Autolock l(mLock);
332 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700333 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700334 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800335
336 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700337 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800338}
339
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700340// For dumping/debugging only -
341// try to acquire a lock a few times, eventually give up to proceed with
342// debug/dump operations
343bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
344 bool gotLock = false;
345 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
346 if (lock.tryLock() == NO_ERROR) {
347 gotLock = true;
348 break;
349 } else {
350 usleep(kDumpSleepDuration);
351 }
352 }
353 return gotLock;
354}
355
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700356Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
357 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
358 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
359 const int STREAM_CONFIGURATION_SIZE = 4;
360 const int STREAM_FORMAT_OFFSET = 0;
361 const int STREAM_WIDTH_OFFSET = 1;
362 const int STREAM_HEIGHT_OFFSET = 2;
363 const int STREAM_IS_INPUT_OFFSET = 3;
364 camera_metadata_ro_entry_t availableStreamConfigs =
365 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
366 if (availableStreamConfigs.count == 0 ||
367 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
368 return Size(0, 0);
369 }
370
371 // Get max jpeg size (area-wise).
372 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
373 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
374 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
375 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
376 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
377 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
378 && format == HAL_PIXEL_FORMAT_BLOB &&
379 (width * height > maxJpegWidth * maxJpegHeight)) {
380 maxJpegWidth = width;
381 maxJpegHeight = height;
382 }
383 }
384 } else {
385 camera_metadata_ro_entry availableJpegSizes =
386 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
387 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
388 return Size(0, 0);
389 }
390
391 // Get max jpeg size (area-wise).
392 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
393 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
394 > (maxJpegWidth * maxJpegHeight)) {
395 maxJpegWidth = availableJpegSizes.data.i32[i];
396 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
397 }
398 }
399 }
400 return Size(maxJpegWidth, maxJpegHeight);
401}
402
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800403nsecs_t Camera3Device::getMonoToBoottimeOffset() {
404 // try three times to get the clock offset, choose the one
405 // with the minimum gap in measurements.
406 const int tries = 3;
407 nsecs_t bestGap, measured;
408 for (int i = 0; i < tries; ++i) {
409 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
410 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
411 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
412 const nsecs_t gap = tmono2 - tmono;
413 if (i == 0 || gap < bestGap) {
414 bestGap = gap;
415 measured = tbase - ((tmono + tmono2) >> 1);
416 }
417 }
418 return measured;
419}
420
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -0700421/**
422 * Map Android N dataspace definitions back to Android M definitions, for
423 * use with HALv3.3 or older.
424 *
425 * Only map where correspondences exist, and otherwise preserve the value.
426 */
427android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) {
428 switch (dataSpace) {
429 case HAL_DATASPACE_V0_SRGB_LINEAR:
430 return HAL_DATASPACE_SRGB_LINEAR;
431 case HAL_DATASPACE_V0_SRGB:
432 return HAL_DATASPACE_SRGB;
433 case HAL_DATASPACE_V0_JFIF:
434 return HAL_DATASPACE_JFIF;
435 case HAL_DATASPACE_V0_BT601_625:
436 return HAL_DATASPACE_BT601_625;
437 case HAL_DATASPACE_V0_BT601_525:
438 return HAL_DATASPACE_BT601_525;
439 case HAL_DATASPACE_V0_BT709:
440 return HAL_DATASPACE_BT709;
441 default:
442 return dataSpace;
443 }
444}
445
Zhijun Hef7da0962014-04-24 13:27:56 -0700446ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700447 // Get max jpeg size (area-wise).
448 Size maxJpegResolution = getMaxJpegResolution();
449 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700450 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700451 __FUNCTION__, mId);
452 return BAD_VALUE;
453 }
454
Zhijun Hef7da0962014-04-24 13:27:56 -0700455 // Get max jpeg buffer size
456 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700457 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
458 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700459 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
460 return BAD_VALUE;
461 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700462 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800463 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700464
465 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700466 float scaleFactor = ((float) (width * height)) /
467 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800468 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
469 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700470 if (jpegBufferSize > maxJpegBufferSize) {
471 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700472 }
473
474 return jpegBufferSize;
475}
476
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700477ssize_t Camera3Device::getPointCloudBufferSize() const {
478 const int FLOATS_PER_POINT=4;
479 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
480 if (maxPointCount.count == 0) {
481 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
482 __FUNCTION__, mId);
483 return BAD_VALUE;
484 }
485 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
486 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
487 return maxBytesForPointCloud;
488}
489
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800490ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800491 const int PER_CONFIGURATION_SIZE = 3;
492 const int WIDTH_OFFSET = 0;
493 const int HEIGHT_OFFSET = 1;
494 const int SIZE_OFFSET = 2;
495 camera_metadata_ro_entry rawOpaqueSizes =
496 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800497 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800498 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800499 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800500 __FUNCTION__, mId, count);
501 return BAD_VALUE;
502 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700503
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800504 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
505 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
506 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
507 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
508 }
509 }
510
511 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
512 __FUNCTION__, mId, width, height);
513 return BAD_VALUE;
514}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700515
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800516status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
517 ATRACE_CALL();
518 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700519
520 // Try to lock, but continue in case of failure (to avoid blocking in
521 // deadlocks)
522 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
523 bool gotLock = tryLockSpinRightRound(mLock);
524
525 ALOGW_IF(!gotInterfaceLock,
526 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
527 mId, __FUNCTION__);
528 ALOGW_IF(!gotLock,
529 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
530 mId, __FUNCTION__);
531
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800532 bool dumpTemplates = false;
533 String16 templatesOption("-t");
534 int n = args.size();
535 for (int i = 0; i < n; i++) {
536 if (args[i] == templatesOption) {
537 dumpTemplates = true;
538 }
539 }
540
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800541 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800542
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800543 const char *status =
544 mStatus == STATUS_ERROR ? "ERROR" :
545 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700546 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
547 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800548 mStatus == STATUS_ACTIVE ? "ACTIVE" :
549 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700550
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800551 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700552 if (mStatus == STATUS_ERROR) {
553 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
554 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800555 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700556 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700557 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800558
559 if (mInputStream != NULL) {
560 write(fd, lines.string(), lines.size());
561 mInputStream->dump(fd, args);
562 } else {
563 lines.appendFormat(" No input stream.\n");
564 write(fd, lines.string(), lines.size());
565 }
566 for (size_t i = 0; i < mOutputStreams.size(); i++) {
567 mOutputStreams[i]->dump(fd,args);
568 }
569
Zhijun He431503c2016-03-07 17:30:16 -0800570 if (mBufferManager != NULL) {
571 lines = String8(" Camera3 Buffer Manager:\n");
572 write(fd, lines.string(), lines.size());
573 mBufferManager->dump(fd, args);
574 }
Zhijun He125684a2015-12-26 15:07:30 -0800575
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700576 lines = String8(" In-flight requests:\n");
577 if (mInFlightMap.size() == 0) {
578 lines.append(" None\n");
579 } else {
580 for (size_t i = 0; i < mInFlightMap.size(); i++) {
581 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700582 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700583 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800584 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700585 r.numBuffersLeft);
586 }
587 }
588 write(fd, lines.string(), lines.size());
589
Igor Murashkin1e479c02013-09-06 16:55:14 -0700590 {
591 lines = String8(" Last request sent:\n");
592 write(fd, lines.string(), lines.size());
593
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700594 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700595 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
596 }
597
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800598 if (dumpTemplates) {
599 const char *templateNames[] = {
600 "TEMPLATE_PREVIEW",
601 "TEMPLATE_STILL_CAPTURE",
602 "TEMPLATE_VIDEO_RECORD",
603 "TEMPLATE_VIDEO_SNAPSHOT",
604 "TEMPLATE_ZERO_SHUTTER_LAG",
605 "TEMPLATE_MANUAL"
606 };
607
608 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
609 const camera_metadata_t *templateRequest;
610 templateRequest =
611 mHal3Device->ops->construct_default_request_settings(
612 mHal3Device, i);
613 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
614 if (templateRequest == NULL) {
615 lines.append(" Not supported\n");
616 write(fd, lines.string(), lines.size());
617 } else {
618 write(fd, lines.string(), lines.size());
619 dump_indented_camera_metadata(templateRequest,
620 fd, /*verbosity*/2, /*indentation*/8);
621 }
622 }
623 }
624
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800625 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700626 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800627 write(fd, lines.string(), lines.size());
628 mHal3Device->ops->dump(mHal3Device, fd);
629 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800630
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700631 if (gotLock) mLock.unlock();
632 if (gotInterfaceLock) mInterfaceLock.unlock();
633
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800634 return OK;
635}
636
637const CameraMetadata& Camera3Device::info() const {
638 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800639 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
640 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700641 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800642 mStatus == STATUS_ERROR ?
643 "when in error state" : "before init");
644 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800645 return mDeviceInfo;
646}
647
Jianing Wei90e59c92014-03-12 18:29:36 -0700648status_t Camera3Device::checkStatusOkToCaptureLocked() {
649 switch (mStatus) {
650 case STATUS_ERROR:
651 CLOGE("Device has encountered a serious error");
652 return INVALID_OPERATION;
653 case STATUS_UNINITIALIZED:
654 CLOGE("Device not initialized");
655 return INVALID_OPERATION;
656 case STATUS_UNCONFIGURED:
657 case STATUS_CONFIGURED:
658 case STATUS_ACTIVE:
659 // OK
660 break;
661 default:
662 SET_ERR_L("Unexpected status: %d", mStatus);
663 return INVALID_OPERATION;
664 }
665 return OK;
666}
667
668status_t Camera3Device::convertMetadataListToRequestListLocked(
669 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
670 if (requestList == NULL) {
671 CLOGE("requestList cannot be NULL.");
672 return BAD_VALUE;
673 }
674
Jianing Weicb0652e2014-03-12 18:29:36 -0700675 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700676 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
677 it != metadataList.end(); ++it) {
678 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
679 if (newRequest == 0) {
680 CLOGE("Can't create capture request");
681 return BAD_VALUE;
682 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700683
684 // Setup burst Id and request Id
685 newRequest->mResultExtras.burstId = burstId++;
686 if (it->exists(ANDROID_REQUEST_ID)) {
687 if (it->find(ANDROID_REQUEST_ID).count == 0) {
688 CLOGE("RequestID entry exists; but must not be empty in metadata");
689 return BAD_VALUE;
690 }
691 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
692 } else {
693 CLOGE("RequestID does not exist in metadata");
694 return BAD_VALUE;
695 }
696
Jianing Wei90e59c92014-03-12 18:29:36 -0700697 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700698
699 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700700 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700701
702 // Setup batch size if this is a high speed video recording request.
703 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
704 auto firstRequest = requestList->begin();
705 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
706 if (outputStream->isVideoStream()) {
707 (*firstRequest)->mBatchSize = requestList->size();
708 break;
709 }
710 }
711 }
712
Jianing Wei90e59c92014-03-12 18:29:36 -0700713 return OK;
714}
715
Jianing Weicb0652e2014-03-12 18:29:36 -0700716status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800717 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800718
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700719 List<const CameraMetadata> requests;
720 requests.push_back(request);
721 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800722}
723
Jianing Wei90e59c92014-03-12 18:29:36 -0700724status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700725 const List<const CameraMetadata> &requests, bool repeating,
726 /*out*/
727 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700728 ATRACE_CALL();
729 Mutex::Autolock il(mInterfaceLock);
730 Mutex::Autolock l(mLock);
731
732 status_t res = checkStatusOkToCaptureLocked();
733 if (res != OK) {
734 // error logged by previous call
735 return res;
736 }
737
738 RequestList requestList;
739
740 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
741 if (res != OK) {
742 // error logged by previous call
743 return res;
744 }
745
746 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700747 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700748 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700749 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700750 }
751
752 if (res == OK) {
753 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
754 if (res != OK) {
755 SET_ERR_L("Can't transition to active in %f seconds!",
756 kActiveTimeout/1e9);
757 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700758 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
759 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700760 } else {
761 CLOGE("Cannot queue request. Impossible.");
762 return BAD_VALUE;
763 }
764
765 return res;
766}
767
Jianing Weicb0652e2014-03-12 18:29:36 -0700768status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
769 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700770 ATRACE_CALL();
771
Jianing Weicb0652e2014-03-12 18:29:36 -0700772 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700773}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800774
Jianing Weicb0652e2014-03-12 18:29:36 -0700775status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
776 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800777 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800778
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700779 List<const CameraMetadata> requests;
780 requests.push_back(request);
781 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800782}
783
Jianing Weicb0652e2014-03-12 18:29:36 -0700784status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
785 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700786 ATRACE_CALL();
787
Jianing Weicb0652e2014-03-12 18:29:36 -0700788 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700789}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800790
791sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
792 const CameraMetadata &request) {
793 status_t res;
794
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700795 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800796 res = configureStreamsLocked();
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700797 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800798 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700799 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800800 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700801 } else if (mStatus == STATUS_UNCONFIGURED) {
802 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700803 CLOGE("No streams configured");
804 return NULL;
805 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800806 }
807
808 sp<CaptureRequest> newRequest = createCaptureRequest(request);
809 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800810}
811
Jianing Weicb0652e2014-03-12 18:29:36 -0700812status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800813 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700814 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800815 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800816
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800817 switch (mStatus) {
818 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700819 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800820 return INVALID_OPERATION;
821 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700822 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800823 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700824 case STATUS_UNCONFIGURED:
825 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800826 case STATUS_ACTIVE:
827 // OK
828 break;
829 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700830 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800831 return INVALID_OPERATION;
832 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700833 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700834
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700835 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800836}
837
838status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
839 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700840 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800841
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700842 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800843}
844
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700845status_t Camera3Device::createInputStream(
846 uint32_t width, uint32_t height, int format, int *id) {
847 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700848 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700849 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700850 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
851 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700852
853 status_t res;
854 bool wasActive = false;
855
856 switch (mStatus) {
857 case STATUS_ERROR:
858 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
859 return INVALID_OPERATION;
860 case STATUS_UNINITIALIZED:
861 ALOGE("%s: Device not initialized", __FUNCTION__);
862 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700863 case STATUS_UNCONFIGURED:
864 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700865 // OK
866 break;
867 case STATUS_ACTIVE:
868 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700869 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700870 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700871 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700872 return res;
873 }
874 wasActive = true;
875 break;
876 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700877 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700878 return INVALID_OPERATION;
879 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700880 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700881
882 if (mInputStream != 0) {
883 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
884 return INVALID_OPERATION;
885 }
886
887 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
888 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700889 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700890
891 mInputStream = newStream;
892
893 *id = mNextStreamId++;
894
895 // Continue captures if active at start
896 if (wasActive) {
897 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
898 res = configureStreamsLocked();
899 if (res != OK) {
900 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
901 __FUNCTION__, mNextStreamId, strerror(-res), res);
902 return res;
903 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700904 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700905 }
906
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700907 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700908 return OK;
909}
910
Igor Murashkin2fba5842013-04-22 14:03:54 -0700911
912status_t Camera3Device::createZslStream(
913 uint32_t width, uint32_t height,
914 int depth,
915 /*out*/
916 int *id,
917 sp<Camera3ZslStream>* zslStream) {
918 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700919 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700920 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700921 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
922 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700923
924 status_t res;
925 bool wasActive = false;
926
927 switch (mStatus) {
928 case STATUS_ERROR:
929 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
930 return INVALID_OPERATION;
931 case STATUS_UNINITIALIZED:
932 ALOGE("%s: Device not initialized", __FUNCTION__);
933 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700934 case STATUS_UNCONFIGURED:
935 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700936 // OK
937 break;
938 case STATUS_ACTIVE:
939 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700940 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700941 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700942 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700943 return res;
944 }
945 wasActive = true;
946 break;
947 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700948 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700949 return INVALID_OPERATION;
950 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700951 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700952
953 if (mInputStream != 0) {
954 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
955 return INVALID_OPERATION;
956 }
957
958 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
959 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700960 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700961
962 res = mOutputStreams.add(mNextStreamId, newStream);
963 if (res < 0) {
964 ALOGE("%s: Can't add new stream to set: %s (%d)",
965 __FUNCTION__, strerror(-res), res);
966 return res;
967 }
968 mInputStream = newStream;
969
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530970 mNeedConfig = true;
971
Igor Murashkin2fba5842013-04-22 14:03:54 -0700972 *id = mNextStreamId++;
973 *zslStream = newStream;
974
975 // Continue captures if active at start
976 if (wasActive) {
977 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
978 res = configureStreamsLocked();
979 if (res != OK) {
980 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
981 __FUNCTION__, mNextStreamId, strerror(-res), res);
982 return res;
983 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700984 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700985 }
986
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700987 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700988 return OK;
989}
990
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700991status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800992 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -0700993 camera3_stream_rotation_t rotation, int *id, int streamSetId, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800994 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700995 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800996 Mutex::Autolock l(mLock);
Zhijun He5d677d12016-05-29 16:52:39 -0700997 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
998 " consumer usage 0x%x", mId, mNextStreamId, width, height, format, dataSpace, rotation,
999 consumerUsage);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001000
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001001 status_t res;
1002 bool wasActive = false;
1003
1004 switch (mStatus) {
1005 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001006 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001007 return INVALID_OPERATION;
1008 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001009 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001010 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001011 case STATUS_UNCONFIGURED:
1012 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001013 // OK
1014 break;
1015 case STATUS_ACTIVE:
1016 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001017 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001018 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001019 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001020 return res;
1021 }
1022 wasActive = true;
1023 break;
1024 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001025 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001026 return INVALID_OPERATION;
1027 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001028 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001029
1030 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001031 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001032 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001033 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001034 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1035 }
Zhijun He5d677d12016-05-29 16:52:39 -07001036
1037 // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
1038 // which requires a consumer surface to be available.
1039 if (consumer == nullptr && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1040 ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
1041 return BAD_VALUE;
1042 }
1043
1044 if (consumer == nullptr && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1045 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1046 return BAD_VALUE;
1047 }
1048
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001049 // Use legacy dataspace values for older HALs
1050 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1051 dataSpace = mapToLegacyDataspace(dataSpace);
1052 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001054 ssize_t blobBufferSize;
1055 if (dataSpace != HAL_DATASPACE_DEPTH) {
1056 blobBufferSize = getJpegBufferSize(width, height);
1057 if (blobBufferSize <= 0) {
1058 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1059 return BAD_VALUE;
1060 }
1061 } else {
1062 blobBufferSize = getPointCloudBufferSize();
1063 if (blobBufferSize <= 0) {
1064 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1065 return BAD_VALUE;
1066 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001067 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001068 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001069 width, height, blobBufferSize, format, dataSpace, rotation,
1070 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001071 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1072 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1073 if (rawOpaqueBufferSize <= 0) {
1074 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1075 return BAD_VALUE;
1076 }
1077 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001078 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1079 mTimestampOffset, streamSetId);
Zhijun He5d677d12016-05-29 16:52:39 -07001080 } else if (consumer == nullptr) {
1081 newStream = new Camera3OutputStream(mNextStreamId,
1082 width, height, format, consumerUsage, dataSpace, rotation,
1083 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001084 } else {
1085 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001086 width, height, format, dataSpace, rotation,
1087 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001088 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001089 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001090
Zhijun He125684a2015-12-26 15:07:30 -08001091 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001092 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1093 * requires buffers to be statically allocated for internal static buffer registration, while
1094 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1095 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001096 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001097 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001098 newStream->setBufferManager(mBufferManager);
1099 }
1100
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001101 res = mOutputStreams.add(mNextStreamId, newStream);
1102 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001103 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001104 return res;
1105 }
1106
1107 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001108 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001109
1110 // Continue captures if active at start
1111 if (wasActive) {
1112 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1113 res = configureStreamsLocked();
1114 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001115 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1116 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001117 return res;
1118 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001119 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001120 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001121 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001122 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001123}
1124
1125status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1126 ATRACE_CALL();
1127 (void)outputId; (void)id;
1128
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001129 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001130 return INVALID_OPERATION;
1131}
1132
1133
1134status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001135 uint32_t *width, uint32_t *height,
1136 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001137 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001138 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001139 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001140
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141 switch (mStatus) {
1142 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001143 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001144 return INVALID_OPERATION;
1145 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001146 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001147 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001148 case STATUS_UNCONFIGURED:
1149 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001150 case STATUS_ACTIVE:
1151 // OK
1152 break;
1153 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001154 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001155 return INVALID_OPERATION;
1156 }
1157
1158 ssize_t idx = mOutputStreams.indexOfKey(id);
1159 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001160 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001161 return idx;
1162 }
1163
1164 if (width) *width = mOutputStreams[idx]->getWidth();
1165 if (height) *height = mOutputStreams[idx]->getHeight();
1166 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001167 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001168 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001169}
1170
1171status_t Camera3Device::setStreamTransform(int id,
1172 int transform) {
1173 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001174 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001175 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001176
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177 switch (mStatus) {
1178 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001179 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001180 return INVALID_OPERATION;
1181 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001182 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001183 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001184 case STATUS_UNCONFIGURED:
1185 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001186 case STATUS_ACTIVE:
1187 // OK
1188 break;
1189 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001190 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001191 return INVALID_OPERATION;
1192 }
1193
1194 ssize_t idx = mOutputStreams.indexOfKey(id);
1195 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001196 CLOGE("Stream %d does not exist",
1197 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001198 return BAD_VALUE;
1199 }
1200
1201 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001202}
1203
1204status_t Camera3Device::deleteStream(int id) {
1205 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001206 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001207 Mutex::Autolock l(mLock);
1208 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001209
Igor Murashkine2172be2013-05-28 15:31:39 -07001210 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1211
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001212 // CameraDevice semantics require device to already be idle before
1213 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001214 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001215 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1216 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001217 }
1218
Igor Murashkin2fba5842013-04-22 14:03:54 -07001219 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001220 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001221 if (mInputStream != NULL && id == mInputStream->getId()) {
1222 deletedStream = mInputStream;
1223 mInputStream.clear();
1224 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001225 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001226 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001227 return BAD_VALUE;
1228 }
Zhijun He5f446352014-01-22 09:49:33 -08001229 }
1230
1231 // Delete output stream or the output part of a bi-directional stream.
1232 if (outputStreamIdx != NAME_NOT_FOUND) {
1233 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001234 mOutputStreams.removeItem(id);
1235 }
1236
1237 // Free up the stream endpoint so that it can be used by some other stream
1238 res = deletedStream->disconnect();
1239 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001240 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001241 // fall through since we want to still list the stream as deleted.
1242 }
1243 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001244 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001245
1246 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001247}
1248
1249status_t Camera3Device::deleteReprocessStream(int id) {
1250 ATRACE_CALL();
1251 (void)id;
1252
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001253 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001254 return INVALID_OPERATION;
1255}
1256
Zhijun He1fa89992015-06-01 15:44:31 -07001257status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001258 ATRACE_CALL();
1259 ALOGV("%s: E", __FUNCTION__);
1260
1261 Mutex::Autolock il(mInterfaceLock);
1262 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001263
1264 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1265 mNeedConfig = true;
1266 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1267 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001268
1269 return configureStreamsLocked();
1270}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001271
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001272status_t Camera3Device::getInputBufferProducer(
1273 sp<IGraphicBufferProducer> *producer) {
1274 Mutex::Autolock il(mInterfaceLock);
1275 Mutex::Autolock l(mLock);
1276
1277 if (producer == NULL) {
1278 return BAD_VALUE;
1279 } else if (mInputStream == NULL) {
1280 return INVALID_OPERATION;
1281 }
1282
1283 return mInputStream->getInputBufferProducer(producer);
1284}
1285
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001286status_t Camera3Device::createDefaultRequest(int templateId,
1287 CameraMetadata *request) {
1288 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001289 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001290
1291 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1292 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1293 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1294 return BAD_VALUE;
1295 }
1296
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001297 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001298 Mutex::Autolock l(mLock);
1299
1300 switch (mStatus) {
1301 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001302 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001303 return INVALID_OPERATION;
1304 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001305 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001306 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001307 case STATUS_UNCONFIGURED:
1308 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001309 case STATUS_ACTIVE:
1310 // OK
1311 break;
1312 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001313 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001314 return INVALID_OPERATION;
1315 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001316
Zhijun Hea1530f12014-09-14 12:44:20 -07001317 if (!mRequestTemplateCache[templateId].isEmpty()) {
1318 *request = mRequestTemplateCache[templateId];
1319 return OK;
1320 }
1321
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001322 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001323 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001324 rawRequest = mHal3Device->ops->construct_default_request_settings(
1325 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001326 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001327 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001328 ALOGI("%s: template %d is not supported on this camera device",
1329 __FUNCTION__, templateId);
1330 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001331 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001332
Zhijun Hea1530f12014-09-14 12:44:20 -07001333 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001334
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001335 // Derive some new keys for backward compatibility
1336 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
1337 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
1338 int32_t defaultBoost[1] = {100};
1339 mRequestTemplateCache[templateId].update(
1340 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1341 defaultBoost, 1);
1342 }
1343
1344 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001345 return OK;
1346}
1347
1348status_t Camera3Device::waitUntilDrained() {
1349 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001350 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001351 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001352
Zhijun He69a37482014-03-23 18:44:49 -07001353 return waitUntilDrainedLocked();
1354}
1355
1356status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001357 switch (mStatus) {
1358 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001359 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001360 ALOGV("%s: Already idle", __FUNCTION__);
1361 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001362 case STATUS_CONFIGURED:
1363 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001364 case STATUS_ERROR:
1365 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001366 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001367 break;
1368 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001369 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001370 return INVALID_OPERATION;
1371 }
1372
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001373 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1374 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001375 if (res != OK) {
1376 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1377 res);
1378 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001379 return res;
1380}
1381
Ruben Brunk183f0562015-08-12 12:55:02 -07001382
1383void Camera3Device::internalUpdateStatusLocked(Status status) {
1384 mStatus = status;
1385 mRecentStatusUpdates.add(mStatus);
1386 mStatusChanged.broadcast();
1387}
1388
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001389// Pause to reconfigure
1390status_t Camera3Device::internalPauseAndWaitLocked() {
1391 mRequestThread->setPaused(true);
1392 mPauseStateNotify = true;
1393
1394 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1395 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1396 if (res != OK) {
1397 SET_ERR_L("Can't idle device in %f seconds!",
1398 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001399 }
1400
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001401 return res;
1402}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001403
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001404// Resume after internalPauseAndWaitLocked
1405status_t Camera3Device::internalResumeLocked() {
1406 status_t res;
1407
1408 mRequestThread->setPaused(false);
1409
1410 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1411 if (res != OK) {
1412 SET_ERR_L("Can't transition to active in %f seconds!",
1413 kActiveTimeout/1e9);
1414 }
1415 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001416 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001417}
1418
Ruben Brunk183f0562015-08-12 12:55:02 -07001419status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001420 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001421
1422 size_t startIndex = 0;
1423 if (mStatusWaiters == 0) {
1424 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1425 // this status list
1426 mRecentStatusUpdates.clear();
1427 } else {
1428 // If other threads are waiting on updates to this status list, set the position of the
1429 // first element that this list will check rather than clearing the list.
1430 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001431 }
1432
Ruben Brunk183f0562015-08-12 12:55:02 -07001433 mStatusWaiters++;
1434
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001435 bool stateSeen = false;
1436 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001437 if (active == (mStatus == STATUS_ACTIVE)) {
1438 // Desired state is current
1439 break;
1440 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001441
1442 res = mStatusChanged.waitRelative(mLock, timeout);
1443 if (res != OK) break;
1444
Ruben Brunk183f0562015-08-12 12:55:02 -07001445 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1446 // transitions.
1447 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1448 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1449 __FUNCTION__);
1450
1451 // Encountered desired state since we began waiting
1452 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001453 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1454 stateSeen = true;
1455 break;
1456 }
1457 }
1458 } while (!stateSeen);
1459
Ruben Brunk183f0562015-08-12 12:55:02 -07001460 mStatusWaiters--;
1461
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001462 return res;
1463}
1464
1465
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001466status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1467 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001468 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001469
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001470 if (listener != NULL && mListener != NULL) {
1471 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1472 }
1473 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001474 mRequestThread->setNotificationListener(listener);
1475 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001476
1477 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001478}
1479
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001480bool Camera3Device::willNotify3A() {
1481 return false;
1482}
1483
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001484status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001485 status_t res;
1486 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001487
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001488 while (mResultQueue.empty()) {
1489 res = mResultSignal.waitRelative(mOutputLock, timeout);
1490 if (res == TIMED_OUT) {
1491 return res;
1492 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001493 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001494 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001495 return res;
1496 }
1497 }
1498 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001499}
1500
Jianing Weicb0652e2014-03-12 18:29:36 -07001501status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001502 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001503 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001504
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001505 if (mResultQueue.empty()) {
1506 return NOT_ENOUGH_DATA;
1507 }
1508
Jianing Weicb0652e2014-03-12 18:29:36 -07001509 if (frame == NULL) {
1510 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1511 return BAD_VALUE;
1512 }
1513
1514 CaptureResult &result = *(mResultQueue.begin());
1515 frame->mResultExtras = result.mResultExtras;
1516 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001517 mResultQueue.erase(mResultQueue.begin());
1518
1519 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001520}
1521
1522status_t Camera3Device::triggerAutofocus(uint32_t id) {
1523 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001524 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001525
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001526 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1527 // Mix-in this trigger into the next request and only the next request.
1528 RequestTrigger trigger[] = {
1529 {
1530 ANDROID_CONTROL_AF_TRIGGER,
1531 ANDROID_CONTROL_AF_TRIGGER_START
1532 },
1533 {
1534 ANDROID_CONTROL_AF_TRIGGER_ID,
1535 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001536 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001537 };
1538
1539 return mRequestThread->queueTrigger(trigger,
1540 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001541}
1542
1543status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1544 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001545 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001546
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001547 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1548 // Mix-in this trigger into the next request and only the next request.
1549 RequestTrigger trigger[] = {
1550 {
1551 ANDROID_CONTROL_AF_TRIGGER,
1552 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1553 },
1554 {
1555 ANDROID_CONTROL_AF_TRIGGER_ID,
1556 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001557 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001558 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001559
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001560 return mRequestThread->queueTrigger(trigger,
1561 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001562}
1563
1564status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1565 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001566 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001567
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001568 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1569 // Mix-in this trigger into the next request and only the next request.
1570 RequestTrigger trigger[] = {
1571 {
1572 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1573 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1574 },
1575 {
1576 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1577 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001578 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001579 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001580
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001581 return mRequestThread->queueTrigger(trigger,
1582 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001583}
1584
1585status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1586 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1587 ATRACE_CALL();
1588 (void)reprocessStreamId; (void)buffer; (void)listener;
1589
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001590 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001591 return INVALID_OPERATION;
1592}
1593
Jianing Weicb0652e2014-03-12 18:29:36 -07001594status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001595 ATRACE_CALL();
1596 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001597 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001598
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001599 NotificationListener* listener;
1600 {
1601 Mutex::Autolock l(mOutputLock);
1602 listener = mListener;
1603 }
1604
Zhijun He7ef20392014-04-21 16:04:17 -07001605 {
1606 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001607 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001608 }
1609
Zhijun He491e3412013-12-27 10:57:44 -08001610 status_t res;
1611 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001612 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001613 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001614 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001615 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001616 }
1617
1618 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001619}
1620
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001621status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001622 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1623}
1624
1625status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001626 ATRACE_CALL();
1627 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001628 Mutex::Autolock il(mInterfaceLock);
1629 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001630
1631 sp<Camera3StreamInterface> stream;
1632 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1633 if (outputStreamIdx == NAME_NOT_FOUND) {
1634 CLOGE("Stream %d does not exist", streamId);
1635 return BAD_VALUE;
1636 }
1637
1638 stream = mOutputStreams.editValueAt(outputStreamIdx);
1639
1640 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001641 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001642 return BAD_VALUE;
1643 }
1644
1645 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001646 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001647 return BAD_VALUE;
1648 }
1649
Ruben Brunkc78ac262015-08-13 17:58:46 -07001650 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001651}
1652
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001653status_t Camera3Device::tearDown(int streamId) {
1654 ATRACE_CALL();
1655 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1656 Mutex::Autolock il(mInterfaceLock);
1657 Mutex::Autolock l(mLock);
1658
1659 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1660 // since we cannot call register_stream_buffers except right after configure_streams.
1661 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1662 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1663 __FUNCTION__, mHal3Device->common.version);
1664 return NO_INIT;
1665 }
1666
1667 sp<Camera3StreamInterface> stream;
1668 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1669 if (outputStreamIdx == NAME_NOT_FOUND) {
1670 CLOGE("Stream %d does not exist", streamId);
1671 return BAD_VALUE;
1672 }
1673
1674 stream = mOutputStreams.editValueAt(outputStreamIdx);
1675
1676 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1677 CLOGE("Stream %d is a target of a in-progress request", streamId);
1678 return BAD_VALUE;
1679 }
1680
1681 return stream->tearDown();
1682}
1683
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001684status_t Camera3Device::addBufferListenerForStream(int streamId,
1685 wp<Camera3StreamBufferListener> listener) {
1686 ATRACE_CALL();
1687 ALOGV("%s: Camera %d: Adding buffer listener for stream %d", __FUNCTION__, mId, streamId);
1688 Mutex::Autolock il(mInterfaceLock);
1689 Mutex::Autolock l(mLock);
1690
1691 sp<Camera3StreamInterface> stream;
1692 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1693 if (outputStreamIdx == NAME_NOT_FOUND) {
1694 CLOGE("Stream %d does not exist", streamId);
1695 return BAD_VALUE;
1696 }
1697
1698 stream = mOutputStreams.editValueAt(outputStreamIdx);
1699 stream->addBufferListener(listener);
1700
1701 return OK;
1702}
1703
Zhijun He204e3292014-07-14 17:09:23 -07001704uint32_t Camera3Device::getDeviceVersion() {
1705 ATRACE_CALL();
1706 Mutex::Autolock il(mInterfaceLock);
1707 return mDeviceVersion;
1708}
1709
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001710/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001711 * Methods called by subclasses
1712 */
1713
1714void Camera3Device::notifyStatus(bool idle) {
1715 {
1716 // Need mLock to safely update state and synchronize to current
1717 // state of methods in flight.
1718 Mutex::Autolock l(mLock);
1719 // We can get various system-idle notices from the status tracker
1720 // while starting up. Only care about them if we've actually sent
1721 // in some requests recently.
1722 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1723 return;
1724 }
1725 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1726 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001727 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001728
1729 // Skip notifying listener if we're doing some user-transparent
1730 // state changes
1731 if (mPauseStateNotify) return;
1732 }
1733 NotificationListener *listener;
1734 {
1735 Mutex::Autolock l(mOutputLock);
1736 listener = mListener;
1737 }
1738 if (idle && listener != NULL) {
1739 listener->notifyIdle();
1740 }
1741}
1742
Zhijun He5d677d12016-05-29 16:52:39 -07001743status_t Camera3Device::setConsumerSurface(int streamId, sp<Surface> consumer) {
1744 ATRACE_CALL();
1745 ALOGV("%s: Camera %d: set consumer surface for stream %d", __FUNCTION__, mId, streamId);
1746 Mutex::Autolock il(mInterfaceLock);
1747 Mutex::Autolock l(mLock);
1748
1749 if (consumer == nullptr) {
1750 CLOGE("Null consumer is passed!");
1751 return BAD_VALUE;
1752 }
1753
1754 ssize_t idx = mOutputStreams.indexOfKey(streamId);
1755 if (idx == NAME_NOT_FOUND) {
1756 CLOGE("Stream %d is unknown", streamId);
1757 return idx;
1758 }
1759 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
1760 status_t res = stream->setConsumer(consumer);
1761 if (res != OK) {
1762 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1763 return res;
1764 }
1765
1766 if (!stream->isConfiguring()) {
1767 CLOGE("Stream %d was already fully configured.", streamId);
1768 return INVALID_OPERATION;
1769 }
1770
1771 res = stream->finishConfiguration(mHal3Device);
1772 if (res != OK) {
1773 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1774 stream->getId(), strerror(-res), res);
1775 return res;
1776 }
1777
1778 return OK;
1779}
1780
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001781/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001782 * Camera3Device private methods
1783 */
1784
1785sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1786 const CameraMetadata &request) {
1787 ATRACE_CALL();
1788 status_t res;
1789
1790 sp<CaptureRequest> newRequest = new CaptureRequest;
1791 newRequest->mSettings = request;
1792
1793 camera_metadata_entry_t inputStreams =
1794 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1795 if (inputStreams.count > 0) {
1796 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001797 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001798 CLOGE("Request references unknown input stream %d",
1799 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001800 return NULL;
1801 }
1802 // Lazy completion of stream configuration (allocation/registration)
1803 // on first use
1804 if (mInputStream->isConfiguring()) {
1805 res = mInputStream->finishConfiguration(mHal3Device);
1806 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001807 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001808 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001809 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001810 return NULL;
1811 }
1812 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001813 // Check if stream is being prepared
1814 if (mInputStream->isPreparing()) {
1815 CLOGE("Request references an input stream that's being prepared!");
1816 return NULL;
1817 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001818
1819 newRequest->mInputStream = mInputStream;
1820 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1821 }
1822
1823 camera_metadata_entry_t streams =
1824 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1825 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001826 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001827 return NULL;
1828 }
1829
1830 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001831 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001832 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001833 CLOGE("Request references unknown stream %d",
1834 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001835 return NULL;
1836 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001837 sp<Camera3OutputStreamInterface> stream =
1838 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001839
Zhijun He5d677d12016-05-29 16:52:39 -07001840 // It is illegal to include a deferred consumer output stream into a request
1841 if (stream->isConsumerConfigurationDeferred()) {
1842 CLOGE("Stream %d hasn't finished configuration yet due to deferred consumer",
1843 stream->getId());
1844 return NULL;
1845 }
1846
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001847 // Lazy completion of stream configuration (allocation/registration)
1848 // on first use
1849 if (stream->isConfiguring()) {
1850 res = stream->finishConfiguration(mHal3Device);
1851 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001852 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1853 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001854 return NULL;
1855 }
1856 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001857 // Check if stream is being prepared
1858 if (stream->isPreparing()) {
1859 CLOGE("Request references an output stream that's being prepared!");
1860 return NULL;
1861 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001862
1863 newRequest->mOutputStreams.push(stream);
1864 }
1865 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001866 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001867
1868 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001869}
1870
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001871bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1872 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1873 Size size = mSupportedOpaqueInputSizes[i];
1874 if (size.width == width && size.height == height) {
1875 return true;
1876 }
1877 }
1878
1879 return false;
1880}
1881
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001882void Camera3Device::cancelStreamsConfigurationLocked() {
1883 int res = OK;
1884 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1885 res = mInputStream->cancelConfiguration();
1886 if (res != OK) {
1887 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
1888 mInputStream->getId(), strerror(-res), res);
1889 }
1890 }
1891
1892 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1893 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
1894 if (outputStream->isConfiguring()) {
1895 res = outputStream->cancelConfiguration();
1896 if (res != OK) {
1897 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
1898 outputStream->getId(), strerror(-res), res);
1899 }
1900 }
1901 }
1902
1903 // Return state to that at start of call, so that future configures
1904 // properly clean things up
1905 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
1906 mNeedConfig = true;
1907}
1908
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001909status_t Camera3Device::configureStreamsLocked() {
1910 ATRACE_CALL();
1911 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001912
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001913 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001914 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001915 return INVALID_OPERATION;
1916 }
1917
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001918 if (!mNeedConfig) {
1919 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1920 return OK;
1921 }
1922
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001923 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1924 // adding a dummy stream instead.
1925 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1926 if (mOutputStreams.size() == 0) {
1927 addDummyStreamLocked();
1928 } else {
1929 tryRemoveDummyStreamLocked();
1930 }
1931
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001932 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001933 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001934
1935 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001936 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1937 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1938 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001939 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1940
1941 Vector<camera3_stream_t*> streams;
1942 streams.setCapacity(config.num_streams);
1943
1944 if (mInputStream != NULL) {
1945 camera3_stream_t *inputStream;
1946 inputStream = mInputStream->startConfiguration();
1947 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001948 CLOGE("Can't start input stream configuration");
1949 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001950 return INVALID_OPERATION;
1951 }
1952 streams.add(inputStream);
1953 }
1954
1955 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001956
1957 // Don't configure bidi streams twice, nor add them twice to the list
1958 if (mOutputStreams[i].get() ==
1959 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1960
1961 config.num_streams--;
1962 continue;
1963 }
1964
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001965 camera3_stream_t *outputStream;
1966 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1967 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001968 CLOGE("Can't start output stream configuration");
1969 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001970 return INVALID_OPERATION;
1971 }
1972 streams.add(outputStream);
1973 }
1974
1975 config.streams = streams.editArray();
1976
1977 // Do the HAL configuration; will potentially touch stream
1978 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001979 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001980 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001981 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001982
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001983 if (res == BAD_VALUE) {
1984 // HAL rejected this set of streams as unsupported, clean up config
1985 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001986 CLOGE("Set of requested inputs/outputs not supported by HAL");
1987 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001988 return BAD_VALUE;
1989 } else if (res != OK) {
1990 // Some other kind of error from configure_streams - this is not
1991 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001992 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1993 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001994 return res;
1995 }
1996
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001997 // Finish all stream configuration immediately.
1998 // TODO: Try to relax this later back to lazy completion, which should be
1999 // faster
2000
Igor Murashkin073f8572013-05-02 14:59:28 -07002001 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002002 res = mInputStream->finishConfiguration(mHal3Device);
2003 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002004 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002005 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002006 cancelStreamsConfigurationLocked();
2007 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002008 }
2009 }
2010
2011 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002012 sp<Camera3OutputStreamInterface> outputStream =
2013 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002014 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002015 res = outputStream->finishConfiguration(mHal3Device);
2016 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002017 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002018 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002019 cancelStreamsConfigurationLocked();
2020 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002021 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002022 }
2023 }
2024
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002025 // Request thread needs to know to avoid using repeat-last-settings protocol
2026 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002027 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002028
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002029 // Boost priority of request thread for high speed recording to SCHED_FIFO
2030 if (mIsConstrainedHighSpeedConfiguration) {
2031 pid_t requestThreadTid = mRequestThread->getTid();
2032 res = requestPriority(getpid(), requestThreadTid,
Eino-Ville Talvala0f6778e2016-04-25 16:57:04 -07002033 kConstrainedHighSpeedThreadPriority, /*asynchronous*/ false);
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002034 if (res != OK) {
2035 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2036 strerror(-res), res);
2037 } else {
2038 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2039 }
2040 } else {
2041 // TODO: Set/restore normal priority for normal use cases
2042 }
2043
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002044 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002045
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002046 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002047
Ruben Brunk183f0562015-08-12 12:55:02 -07002048 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2049 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002050
2051 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
2052
Zhijun He0a210512014-07-24 13:45:15 -07002053 // tear down the deleted streams after configure streams.
2054 mDeletedStreams.clear();
2055
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002056 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002057}
2058
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002059status_t Camera3Device::addDummyStreamLocked() {
2060 ATRACE_CALL();
2061 status_t res;
2062
2063 if (mDummyStreamId != NO_STREAM) {
2064 // Should never be adding a second dummy stream when one is already
2065 // active
2066 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
2067 __FUNCTION__, mId);
2068 return INVALID_OPERATION;
2069 }
2070
2071 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
2072
2073 sp<Camera3OutputStreamInterface> dummyStream =
2074 new Camera3DummyStream(mNextStreamId);
2075
2076 res = mOutputStreams.add(mNextStreamId, dummyStream);
2077 if (res < 0) {
2078 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2079 return res;
2080 }
2081
2082 mDummyStreamId = mNextStreamId;
2083 mNextStreamId++;
2084
2085 return OK;
2086}
2087
2088status_t Camera3Device::tryRemoveDummyStreamLocked() {
2089 ATRACE_CALL();
2090 status_t res;
2091
2092 if (mDummyStreamId == NO_STREAM) return OK;
2093 if (mOutputStreams.size() == 1) return OK;
2094
2095 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
2096
2097 // Ok, have a dummy stream and there's at least one other output stream,
2098 // so remove the dummy
2099
2100 sp<Camera3StreamInterface> deletedStream;
2101 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2102 if (outputStreamIdx == NAME_NOT_FOUND) {
2103 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2104 return INVALID_OPERATION;
2105 }
2106
2107 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2108 mOutputStreams.removeItemsAt(outputStreamIdx);
2109
2110 // Free up the stream endpoint so that it can be used by some other stream
2111 res = deletedStream->disconnect();
2112 if (res != OK) {
2113 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2114 // fall through since we want to still list the stream as deleted.
2115 }
2116 mDeletedStreams.add(deletedStream);
2117 mDummyStreamId = NO_STREAM;
2118
2119 return res;
2120}
2121
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002122void Camera3Device::setErrorState(const char *fmt, ...) {
2123 Mutex::Autolock l(mLock);
2124 va_list args;
2125 va_start(args, fmt);
2126
2127 setErrorStateLockedV(fmt, args);
2128
2129 va_end(args);
2130}
2131
2132void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2133 Mutex::Autolock l(mLock);
2134 setErrorStateLockedV(fmt, args);
2135}
2136
2137void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2138 va_list args;
2139 va_start(args, fmt);
2140
2141 setErrorStateLockedV(fmt, args);
2142
2143 va_end(args);
2144}
2145
2146void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002147 // Print out all error messages to log
2148 String8 errorCause = String8::formatV(fmt, args);
2149 ALOGE("Camera %d: %s", mId, errorCause.string());
2150
2151 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002152 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002153
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002154 mErrorCause = errorCause;
2155
2156 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002157 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002158
2159 // Notify upstream about a device error
2160 if (mListener != NULL) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002161 mListener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002162 CaptureResultExtras());
2163 }
2164
2165 // Save stack trace. View by dumping it later.
2166 CameraTraces::saveTrace();
2167 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002168}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002169
2170/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002171 * In-flight request management
2172 */
2173
Jianing Weicb0652e2014-03-12 18:29:36 -07002174status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002175 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2176 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002177 ATRACE_CALL();
2178 Mutex::Autolock l(mInFlightLock);
2179
2180 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002181 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2182 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002183 if (res < 0) return res;
2184
2185 return OK;
2186}
2187
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002188void Camera3Device::returnOutputBuffers(
2189 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2190 nsecs_t timestamp) {
2191 for (size_t i = 0; i < numBuffers; i++)
2192 {
2193 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2194 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2195 // Note: stream may be deallocated at this point, if this buffer was
2196 // the last reference to it.
2197 if (res != OK) {
2198 ALOGE("Can't return buffer to its stream: %s (%d)",
2199 strerror(-res), res);
2200 }
2201 }
2202}
2203
2204
2205void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2206
2207 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2208 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2209
2210 nsecs_t sensorTimestamp = request.sensorTimestamp;
2211 nsecs_t shutterTimestamp = request.shutterTimestamp;
2212
2213 // Check if it's okay to remove the request from InFlightMap:
2214 // In the case of a successful request:
2215 // all input and output buffers, all result metadata, shutter callback
2216 // arrived.
2217 // In the case of a unsuccessful request:
2218 // all input and output buffers arrived.
2219 if (request.numBuffersLeft == 0 &&
2220 (request.requestStatus != OK ||
2221 (request.haveResultMetadata && shutterTimestamp != 0))) {
2222 ATRACE_ASYNC_END("frame capture", frameNumber);
2223
2224 // Sanity check - if sensor timestamp matches shutter timestamp
2225 if (request.requestStatus == OK &&
2226 sensorTimestamp != shutterTimestamp) {
2227 SET_ERR("sensor timestamp (%" PRId64
2228 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2229 sensorTimestamp, frameNumber, shutterTimestamp);
2230 }
2231
2232 // for an unsuccessful request, it may have pending output buffers to
2233 // return.
2234 assert(request.requestStatus != OK ||
2235 request.pendingOutputBuffers.size() == 0);
2236 returnOutputBuffers(request.pendingOutputBuffers.array(),
2237 request.pendingOutputBuffers.size(), 0);
2238
2239 mInFlightMap.removeItemsAt(idx, 1);
2240
2241 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2242 }
2243
2244 // Sanity check - if we have too many in-flight frames, something has
2245 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002246 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002247 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002248 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2249 kInFlightWarnLimitHighSpeed) {
2250 CLOGE("In-flight list too large for high speed configuration: %zu",
2251 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002252 }
2253}
2254
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002255void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2256 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2257 if (result == nullptr) return;
2258
2259 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2260 (int32_t*)&frameNumber, 1) != OK) {
2261 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2262 return;
2263 }
2264
2265 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2266 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2267 return;
2268 }
2269
2270 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2271
2272 // Valid result, insert into queue
2273 List<CaptureResult>::iterator queuedResult =
2274 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2275 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2276 ", burstId = %" PRId32, __FUNCTION__,
2277 queuedResult->mResultExtras.requestId,
2278 queuedResult->mResultExtras.frameNumber,
2279 queuedResult->mResultExtras.burstId);
2280
2281 mResultSignal.signal();
2282}
2283
2284
2285void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2286 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2287 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2288 Mutex::Autolock l(mOutputLock);
2289
2290 CaptureResult captureResult;
2291 captureResult.mResultExtras = resultExtras;
2292 captureResult.mMetadata = partialResult;
2293
2294 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2295}
2296
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002297
2298void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2299 CaptureResultExtras &resultExtras,
2300 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002301 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002302 bool reprocess,
2303 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002304 if (pendingMetadata.isEmpty())
2305 return;
2306
2307 Mutex::Autolock l(mOutputLock);
2308
2309 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002310 if (reprocess) {
2311 if (frameNumber < mNextReprocessResultFrameNumber) {
2312 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002313 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002314 frameNumber, mNextReprocessResultFrameNumber);
2315 return;
2316 }
2317 mNextReprocessResultFrameNumber = frameNumber + 1;
2318 } else {
2319 if (frameNumber < mNextResultFrameNumber) {
2320 SET_ERR("Out-of-order capture result metadata submitted! "
2321 "(got frame number %d, expecting %d)",
2322 frameNumber, mNextResultFrameNumber);
2323 return;
2324 }
2325 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002326 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002327
2328 CaptureResult captureResult;
2329 captureResult.mResultExtras = resultExtras;
2330 captureResult.mMetadata = pendingMetadata;
2331
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002332 // Append any previous partials to form a complete result
2333 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2334 captureResult.mMetadata.append(collectedPartialResult);
2335 }
2336
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07002337 // Derive some new keys for backward compaibility
2338 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
2339 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
2340 int32_t defaultBoost[1] = {100};
2341 captureResult.mMetadata.update(
2342 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
2343 defaultBoost, 1);
2344 }
2345
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002346 captureResult.mMetadata.sort();
2347
2348 // Check that there's a timestamp in the result metadata
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002349 camera_metadata_entry entry = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002350 if (entry.count == 0) {
2351 SET_ERR("No timestamp provided by HAL for frame %d!",
2352 frameNumber);
2353 return;
2354 }
2355
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002356 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002357}
2358
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002359/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002360 * Camera HAL device callback methods
2361 */
2362
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002363void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002364 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002365
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002366 status_t res;
2367
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002368 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002369 if (result->result == NULL && result->num_output_buffers == 0 &&
2370 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002371 SET_ERR("No result data provided by HAL for frame %d",
2372 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002373 return;
2374 }
Zhijun He204e3292014-07-14 17:09:23 -07002375
2376 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2377 // partial_result to 1 when metadata is included in this result.
2378 if (!mUsePartialResult &&
2379 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2380 result->result != NULL &&
2381 result->partial_result != 1) {
2382 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2383 " if partial result is not supported",
2384 frameNumber, result->partial_result);
2385 return;
2386 }
2387
2388 bool isPartialResult = false;
2389 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002390 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002391 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002392
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002393 // Get shutter timestamp and resultExtras from list of in-flight requests,
2394 // where it was added by the shutter notification for this frame. If the
2395 // shutter timestamp isn't received yet, append the output buffers to the
2396 // in-flight request and they will be returned when the shutter timestamp
2397 // arrives. Update the in-flight status and remove the in-flight entry if
2398 // all result data and shutter timestamp have been received.
2399 nsecs_t shutterTimestamp = 0;
2400
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002401 {
2402 Mutex::Autolock l(mInFlightLock);
2403 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2404 if (idx == NAME_NOT_FOUND) {
2405 SET_ERR("Unknown frame number for capture result: %d",
2406 frameNumber);
2407 return;
2408 }
2409 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002410 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2411 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2412 ", partialResultCount = %d",
2413 __FUNCTION__, request.resultExtras.requestId,
2414 request.resultExtras.frameNumber, request.resultExtras.burstId,
2415 result->partial_result);
2416 // Always update the partial count to the latest one if it's not 0
2417 // (buffers only). When framework aggregates adjacent partial results
2418 // into one, the latest partial count will be used.
2419 if (result->partial_result != 0)
2420 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002421
2422 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002423 if (mUsePartialResult && result->result != NULL) {
2424 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2425 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2426 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2427 " the range of [1, %d] when metadata is included in the result",
2428 frameNumber, result->partial_result, mNumPartialResults);
2429 return;
2430 }
2431 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002432 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002433 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002434 }
Zhijun He204e3292014-07-14 17:09:23 -07002435 } else {
2436 camera_metadata_ro_entry_t partialResultEntry;
2437 res = find_camera_metadata_ro_entry(result->result,
2438 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2439 if (res != NAME_NOT_FOUND &&
2440 partialResultEntry.count > 0 &&
2441 partialResultEntry.data.u8[0] ==
2442 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2443 // A partial result. Flag this as such, and collect this
2444 // set of metadata into the in-flight entry.
2445 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002446 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002447 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002448 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002449 ANDROID_QUIRKS_PARTIAL_RESULT);
2450 }
2451 }
2452
2453 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002454 // Send partial capture result
2455 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2456 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002457 }
2458 }
2459
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002460 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002461 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002462
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002463 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002464 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002465 if (request.haveResultMetadata) {
2466 SET_ERR("Called multiple times with metadata for frame %d",
2467 frameNumber);
2468 return;
2469 }
Zhijun He204e3292014-07-14 17:09:23 -07002470 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002471 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002472 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002473 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002474 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002475 request.haveResultMetadata = true;
2476 }
2477
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002478 uint32_t numBuffersReturned = result->num_output_buffers;
2479 if (result->input_buffer != NULL) {
2480 if (hasInputBufferInRequest) {
2481 numBuffersReturned += 1;
2482 } else {
2483 ALOGW("%s: Input buffer should be NULL if there is no input"
2484 " buffer sent in the request",
2485 __FUNCTION__);
2486 }
2487 }
2488 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002489 if (request.numBuffersLeft < 0) {
2490 SET_ERR("Too many buffers returned for frame %d",
2491 frameNumber);
2492 return;
2493 }
2494
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002495 camera_metadata_ro_entry_t entry;
2496 res = find_camera_metadata_ro_entry(result->result,
2497 ANDROID_SENSOR_TIMESTAMP, &entry);
2498 if (res == OK && entry.count == 1) {
2499 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002500 }
2501
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002502 // If shutter event isn't received yet, append the output buffers to
2503 // the in-flight request. Otherwise, return the output buffers to
2504 // streams.
2505 if (shutterTimestamp == 0) {
2506 request.pendingOutputBuffers.appendArray(result->output_buffers,
2507 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002508 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002509 returnOutputBuffers(result->output_buffers,
2510 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002511 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002512
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002513 if (result->result != NULL && !isPartialResult) {
2514 if (shutterTimestamp == 0) {
2515 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002516 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002517 } else {
2518 CameraMetadata metadata;
2519 metadata = result->result;
2520 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002521 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2522 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002523 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002524 }
2525
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002526 removeInFlightRequestIfReadyLocked(idx);
2527 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002528
Zhijun Hef0d962a2014-06-30 10:24:11 -07002529 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002530 if (hasInputBufferInRequest) {
2531 Camera3Stream *stream =
2532 Camera3Stream::cast(result->input_buffer->stream);
2533 res = stream->returnInputBuffer(*(result->input_buffer));
2534 // Note: stream may be deallocated at this point, if this buffer was the
2535 // last reference to it.
2536 if (res != OK) {
2537 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2538 " its stream:%s (%d)", __FUNCTION__,
2539 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002540 }
2541 } else {
2542 ALOGW("%s: Input buffer should be NULL if there is no input"
2543 " buffer sent in the request, skipping input buffer return.",
2544 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002545 }
2546 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002547}
2548
2549void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002550 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002551 NotificationListener *listener;
2552 {
2553 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002554 listener = mListener;
2555 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002556
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002557 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002558 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002559 return;
2560 }
2561
2562 switch (msg->type) {
2563 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002564 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002565 break;
2566 }
2567 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002568 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002569 break;
2570 }
2571 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002572 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002573 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002574 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002575}
2576
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002577void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2578 NotificationListener *listener) {
2579
2580 // Map camera HAL error codes to ICameraDeviceCallback error codes
2581 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002582 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002583 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002584 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002585 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002586 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002587 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002588 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002589 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002590 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002591 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002592 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002593 };
2594
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002595 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002596 ((msg.error_code >= 0) &&
2597 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2598 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002599 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002600
2601 int streamId = 0;
2602 if (msg.error_stream != NULL) {
2603 Camera3Stream *stream =
2604 Camera3Stream::cast(msg.error_stream);
2605 streamId = stream->getId();
2606 }
2607 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2608 mId, __FUNCTION__, msg.frame_number,
2609 streamId, msg.error_code);
2610
2611 CaptureResultExtras resultExtras;
2612 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002613 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002614 // SET_ERR calls notifyError
2615 SET_ERR("Camera HAL reported serious device error");
2616 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002617 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2618 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2619 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002620 {
2621 Mutex::Autolock l(mInFlightLock);
2622 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2623 if (idx >= 0) {
2624 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2625 r.requestStatus = msg.error_code;
2626 resultExtras = r.resultExtras;
2627 } else {
2628 resultExtras.frameNumber = msg.frame_number;
2629 ALOGE("Camera %d: %s: cannot find in-flight request on "
2630 "frame %" PRId64 " error", mId, __FUNCTION__,
2631 resultExtras.frameNumber);
2632 }
2633 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002634 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002635 if (listener != NULL) {
2636 listener->notifyError(errorCode, resultExtras);
2637 } else {
2638 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2639 }
2640 break;
2641 default:
2642 // SET_ERR calls notifyError
2643 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2644 break;
2645 }
2646}
2647
2648void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2649 NotificationListener *listener) {
2650 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002651
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002652 // Set timestamp for the request in the in-flight tracking
2653 // and get the request ID to send upstream
2654 {
2655 Mutex::Autolock l(mInFlightLock);
2656 idx = mInFlightMap.indexOfKey(msg.frame_number);
2657 if (idx >= 0) {
2658 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002659
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002660 // Verify ordering of shutter notifications
2661 {
2662 Mutex::Autolock l(mOutputLock);
2663 // TODO: need to track errors for tighter bounds on expected frame number.
2664 if (r.hasInputBuffer) {
2665 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2666 SET_ERR("Shutter notification out-of-order. Expected "
2667 "notification for frame %d, got frame %d",
2668 mNextReprocessShutterFrameNumber, msg.frame_number);
2669 return;
2670 }
2671 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2672 } else {
2673 if (msg.frame_number < mNextShutterFrameNumber) {
2674 SET_ERR("Shutter notification out-of-order. Expected "
2675 "notification for frame %d, got frame %d",
2676 mNextShutterFrameNumber, msg.frame_number);
2677 return;
2678 }
2679 mNextShutterFrameNumber = msg.frame_number + 1;
2680 }
2681 }
2682
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002683 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2684 mId, __FUNCTION__,
2685 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2686 // Call listener, if any
2687 if (listener != NULL) {
2688 listener->notifyShutter(r.resultExtras, msg.timestamp);
2689 }
2690
2691 r.shutterTimestamp = msg.timestamp;
2692
2693 // send pending result and buffers
2694 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002695 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002696 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002697 returnOutputBuffers(r.pendingOutputBuffers.array(),
2698 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2699 r.pendingOutputBuffers.clear();
2700
2701 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002702 }
2703 }
2704 if (idx < 0) {
2705 SET_ERR("Shutter notification for non-existent frame number %d",
2706 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002707 }
2708}
2709
2710
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002711CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002712 ALOGV("%s", __FUNCTION__);
2713
Igor Murashkin1e479c02013-09-06 16:55:14 -07002714 CameraMetadata retVal;
2715
2716 if (mRequestThread != NULL) {
2717 retVal = mRequestThread->getLatestRequest();
2718 }
2719
Igor Murashkin1e479c02013-09-06 16:55:14 -07002720 return retVal;
2721}
2722
Jianing Weicb0652e2014-03-12 18:29:36 -07002723
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002724/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002725 * RequestThread inner class methods
2726 */
2727
2728Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002729 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002730 camera3_device_t *hal3Device,
2731 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002732 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002733 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002734 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002735 mHal3Device(hal3Device),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002736 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002737 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002738 mReconfigured(false),
2739 mDoPause(false),
2740 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002741 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002742 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002743 mCurrentAfTriggerId(0),
2744 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002745 mRepeatingLastFrameNumber(
2746 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002747 mAeLockAvailable(aeLockAvailable),
2748 mPrepareVideoStream(false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002749 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002750}
2751
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002752void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002753 NotificationListener *listener) {
2754 Mutex::Autolock l(mRequestLock);
2755 mListener = listener;
2756}
2757
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002758void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002759 Mutex::Autolock l(mRequestLock);
2760 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002761 // Prepare video stream for high speed recording.
2762 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002763}
2764
Jianing Wei90e59c92014-03-12 18:29:36 -07002765status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002766 List<sp<CaptureRequest> > &requests,
2767 /*out*/
2768 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002769 Mutex::Autolock l(mRequestLock);
2770 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2771 ++it) {
2772 mRequestQueue.push_back(*it);
2773 }
2774
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002775 if (lastFrameNumber != NULL) {
2776 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2777 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2778 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2779 *lastFrameNumber);
2780 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002781
Jianing Wei90e59c92014-03-12 18:29:36 -07002782 unpauseForNewRequests();
2783
2784 return OK;
2785}
2786
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002787
2788status_t Camera3Device::RequestThread::queueTrigger(
2789 RequestTrigger trigger[],
2790 size_t count) {
2791
2792 Mutex::Autolock l(mTriggerMutex);
2793 status_t ret;
2794
2795 for (size_t i = 0; i < count; ++i) {
2796 ret = queueTriggerLocked(trigger[i]);
2797
2798 if (ret != OK) {
2799 return ret;
2800 }
2801 }
2802
2803 return OK;
2804}
2805
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002806int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2807 sp<Camera3Device> d = device.promote();
2808 if (d != NULL) return d->mId;
2809 return 0;
2810}
2811
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002812status_t Camera3Device::RequestThread::queueTriggerLocked(
2813 RequestTrigger trigger) {
2814
2815 uint32_t tag = trigger.metadataTag;
2816 ssize_t index = mTriggerMap.indexOfKey(tag);
2817
2818 switch (trigger.getTagType()) {
2819 case TYPE_BYTE:
2820 // fall-through
2821 case TYPE_INT32:
2822 break;
2823 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002824 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2825 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002826 return INVALID_OPERATION;
2827 }
2828
2829 /**
2830 * Collect only the latest trigger, since we only have 1 field
2831 * in the request settings per trigger tag, and can't send more than 1
2832 * trigger per request.
2833 */
2834 if (index != NAME_NOT_FOUND) {
2835 mTriggerMap.editValueAt(index) = trigger;
2836 } else {
2837 mTriggerMap.add(tag, trigger);
2838 }
2839
2840 return OK;
2841}
2842
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002843status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002844 const RequestList &requests,
2845 /*out*/
2846 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002847 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002848 if (lastFrameNumber != NULL) {
2849 *lastFrameNumber = mRepeatingLastFrameNumber;
2850 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002851 mRepeatingRequests.clear();
2852 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2853 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002854
2855 unpauseForNewRequests();
2856
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002857 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002858 return OK;
2859}
2860
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002861bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2862 if (mRepeatingRequests.empty()) {
2863 return false;
2864 }
2865 int32_t requestId = requestIn->mResultExtras.requestId;
2866 const RequestList &repeatRequests = mRepeatingRequests;
2867 // All repeating requests are guaranteed to have same id so only check first quest
2868 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2869 return (firstRequest->mResultExtras.requestId == requestId);
2870}
2871
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002872status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002873 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002874 return clearRepeatingRequestsLocked(lastFrameNumber);
2875
2876}
2877
2878status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002879 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002880 if (lastFrameNumber != NULL) {
2881 *lastFrameNumber = mRepeatingLastFrameNumber;
2882 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002883 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002884 return OK;
2885}
2886
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002887status_t Camera3Device::RequestThread::clear(
2888 NotificationListener *listener,
2889 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002890 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002891 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002892
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002893 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002894
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002895 // Send errors for all requests pending in the request queue, including
2896 // pending repeating requests
2897 if (listener != NULL) {
2898 for (RequestList::iterator it = mRequestQueue.begin();
2899 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002900 // Abort the input buffers for reprocess requests.
2901 if ((*it)->mInputStream != NULL) {
2902 camera3_stream_buffer_t inputBuffer;
2903 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2904 if (res != OK) {
2905 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2906 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2907 } else {
2908 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2909 if (res != OK) {
2910 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2911 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2912 }
2913 }
2914 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002915 // Set the frame number this request would have had, if it
2916 // had been submitted; this frame number will not be reused.
2917 // The requestId and burstId fields were set when the request was
2918 // submitted originally (in convertMetadataListToRequestListLocked)
2919 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002920 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002921 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002922 }
2923 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002924 mRequestQueue.clear();
2925 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002926 if (lastFrameNumber != NULL) {
2927 *lastFrameNumber = mRepeatingLastFrameNumber;
2928 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002929 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002930 return OK;
2931}
2932
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002933status_t Camera3Device::RequestThread::flush() {
2934 ATRACE_CALL();
2935 Mutex::Autolock l(mFlushLock);
2936
2937 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2938 return mHal3Device->ops->flush(mHal3Device);
2939 }
2940
2941 return -ENOTSUP;
2942}
2943
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002944void Camera3Device::RequestThread::setPaused(bool paused) {
2945 Mutex::Autolock l(mPauseLock);
2946 mDoPause = paused;
2947 mDoPauseSignal.signal();
2948}
2949
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002950status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2951 int32_t requestId, nsecs_t timeout) {
2952 Mutex::Autolock l(mLatestRequestMutex);
2953 status_t res;
2954 while (mLatestRequestId != requestId) {
2955 nsecs_t startTime = systemTime();
2956
2957 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2958 if (res != OK) return res;
2959
2960 timeout -= (systemTime() - startTime);
2961 }
2962
2963 return OK;
2964}
2965
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002966void Camera3Device::RequestThread::requestExit() {
2967 // Call parent to set up shutdown
2968 Thread::requestExit();
2969 // The exit from any possible waits
2970 mDoPauseSignal.signal();
2971 mRequestSignal.signal();
2972}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002973
Chien-Yu Chend196d612015-06-22 19:49:01 -07002974
2975/**
2976 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2977 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2978 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2979 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2980 * request.
2981 */
2982void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2983 request->mAeTriggerCancelOverride.applyAeLock = false;
2984 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2985
2986 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2987 return;
2988 }
2989
2990 camera_metadata_entry_t aePrecaptureTrigger =
2991 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2992 if (aePrecaptureTrigger.count > 0 &&
2993 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2994 // Always override CANCEL to IDLE
2995 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2996 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2997 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2998 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2999 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3000
3001 if (mAeLockAvailable == true) {
3002 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3003 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3004 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3005 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3006 request->mAeTriggerCancelOverride.applyAeLock = true;
3007 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3008 }
3009 }
3010 }
3011}
3012
3013/**
3014 * Override result metadata for cancelling AE precapture trigger applied in
3015 * handleAePrecaptureCancelRequest().
3016 */
3017void Camera3Device::overrideResultForPrecaptureCancel(
3018 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3019 if (aeTriggerCancelOverride.applyAeLock) {
3020 // Only devices <= v3.2 should have this override
3021 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3022 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3023 }
3024
3025 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3026 // Only devices <= v3.2 should have this override
3027 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3028 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3029 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3030 }
3031}
3032
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003033void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003034 bool surfaceAbandoned = false;
3035 int64_t lastFrameNumber = 0;
3036 {
3037 Mutex::Autolock l(mRequestLock);
3038 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3039 // repeating requests.
3040 for (const auto& request : mRepeatingRequests) {
3041 for (const auto& s : request->mOutputStreams) {
3042 if (s->isAbandoned()) {
3043 surfaceAbandoned = true;
3044 clearRepeatingRequestsLocked(&lastFrameNumber);
3045 break;
3046 }
3047 }
3048 if (surfaceAbandoned) {
3049 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003050 }
3051 }
3052 }
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003053 if (surfaceAbandoned) {
3054 mListener->notifyRepeatingRequestError(lastFrameNumber);
3055 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003056}
3057
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003058bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003059 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003060 status_t res;
3061
3062 // Handle paused state.
3063 if (waitIfPaused()) {
3064 return true;
3065 }
3066
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003067 // Wait for the next batch of requests.
3068 waitForNextRequestBatch();
3069 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003070 return true;
3071 }
3072
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003073 // Get the latest request ID, if any
3074 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003075 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003076 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003077 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003078 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003079 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003080 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3081 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003082 }
3083
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003084 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003085 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003086 if (res == TIMED_OUT) {
3087 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003088 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003089 // Check if any stream is abandoned.
3090 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003091 return true;
3092 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003093 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003094 return false;
3095 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003096
Zhijun Hecc27e112013-10-03 16:12:43 -07003097 // Inform waitUntilRequestProcessed thread of a new request ID
3098 {
3099 Mutex::Autolock al(mLatestRequestMutex);
3100
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003101 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003102 mLatestRequestSignal.signal();
3103 }
3104
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003105 // Submit a batch of requests to HAL.
3106 // Use flush lock only when submitting multilple requests in a batch.
3107 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3108 // which may take a long time to finish so synchronizing flush() and
3109 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3110 // For now, only synchronize for high speed recording and we should figure something out for
3111 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003112 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003113
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003114 if (useFlushLock) {
3115 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003116 }
3117
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003118 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003119 mNextRequests.size());
3120 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003121 // Submit request and block until ready for next one
3122 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3123 ATRACE_BEGIN("camera3->process_capture_request");
3124 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3125 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003126
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003127 if (res != OK) {
3128 // Should only get a failure here for malformed requests or device-level
3129 // errors, so consider all errors fatal. Bad metadata failures should
3130 // come through notify.
3131 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3132 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3133 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003134 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003135 if (useFlushLock) {
3136 mFlushLock.unlock();
3137 }
3138 return false;
3139 }
3140
3141 // Mark that the request has be submitted successfully.
3142 nextRequest.submitted = true;
3143
3144 // Update the latest request sent to HAL
3145 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3146 Mutex::Autolock al(mLatestRequestMutex);
3147
3148 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3149 mLatestRequest.acquire(cloned);
3150 }
3151
3152 if (nextRequest.halRequest.settings != NULL) {
3153 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3154 }
3155
3156 // Remove any previously queued triggers (after unlock)
3157 res = removeTriggers(mPrevRequest);
3158 if (res != OK) {
3159 SET_ERR("RequestThread: Unable to remove triggers "
3160 "(capture request %d, HAL device: %s (%d)",
3161 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003162 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003163 if (useFlushLock) {
3164 mFlushLock.unlock();
3165 }
3166 return false;
3167 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003168 }
3169
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003170 if (useFlushLock) {
3171 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003172 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003173
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003174 // Unset as current request
3175 {
3176 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003177 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003178 }
3179
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003180 return true;
3181}
3182
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003183status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003184 ATRACE_CALL();
3185
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003186 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003187 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3188 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3189 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3190
3191 // Prepare a request to HAL
3192 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3193
3194 // Insert any queued triggers (before metadata is locked)
3195 status_t res = insertTriggers(captureRequest);
3196
3197 if (res < 0) {
3198 SET_ERR("RequestThread: Unable to insert triggers "
3199 "(capture request %d, HAL device: %s (%d)",
3200 halRequest->frame_number, strerror(-res), res);
3201 return INVALID_OPERATION;
3202 }
3203 int triggerCount = res;
3204 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3205 mPrevTriggers = triggerCount;
3206
3207 // If the request is the same as last, or we had triggers last time
3208 if (mPrevRequest != captureRequest || triggersMixedIn) {
3209 /**
3210 * HAL workaround:
3211 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3212 */
3213 res = addDummyTriggerIds(captureRequest);
3214 if (res != OK) {
3215 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3216 "(capture request %d, HAL device: %s (%d)",
3217 halRequest->frame_number, strerror(-res), res);
3218 return INVALID_OPERATION;
3219 }
3220
3221 /**
3222 * The request should be presorted so accesses in HAL
3223 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3224 */
3225 captureRequest->mSettings.sort();
3226 halRequest->settings = captureRequest->mSettings.getAndLock();
3227 mPrevRequest = captureRequest;
3228 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3229
3230 IF_ALOGV() {
3231 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3232 find_camera_metadata_ro_entry(
3233 halRequest->settings,
3234 ANDROID_CONTROL_AF_TRIGGER,
3235 &e
3236 );
3237 if (e.count > 0) {
3238 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3239 __FUNCTION__,
3240 halRequest->frame_number,
3241 e.data.u8[0]);
3242 }
3243 }
3244 } else {
3245 // leave request.settings NULL to indicate 'reuse latest given'
3246 ALOGVV("%s: Request settings are REUSED",
3247 __FUNCTION__);
3248 }
3249
3250 uint32_t totalNumBuffers = 0;
3251
3252 // Fill in buffers
3253 if (captureRequest->mInputStream != NULL) {
3254 halRequest->input_buffer = &captureRequest->mInputBuffer;
3255 totalNumBuffers += 1;
3256 } else {
3257 halRequest->input_buffer = NULL;
3258 }
3259
3260 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3261 captureRequest->mOutputStreams.size());
3262 halRequest->output_buffers = outputBuffers->array();
3263 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003264 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(i);
3265
3266 // Prepare video buffers for high speed recording on the first video request.
3267 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3268 // Only try to prepare video stream on the first video request.
3269 mPrepareVideoStream = false;
3270
3271 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
3272 while (res == NOT_ENOUGH_DATA) {
3273 res = outputStream->prepareNextBuffer();
3274 }
3275 if (res != OK) {
3276 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3277 __FUNCTION__, strerror(-res), res);
3278 outputStream->cancelPrepare();
3279 }
3280 }
3281
3282 res = outputStream->getBuffer(&outputBuffers->editItemAt(i));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003283 if (res != OK) {
3284 // Can't get output buffer from gralloc queue - this could be due to
3285 // abandoned queue or other consumer misbehavior, so not a fatal
3286 // error
3287 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3288 " %s (%d)", strerror(-res), res);
3289
3290 return TIMED_OUT;
3291 }
3292 halRequest->num_output_buffers++;
3293 }
3294 totalNumBuffers += halRequest->num_output_buffers;
3295
3296 // Log request in the in-flight queue
3297 sp<Camera3Device> parent = mParent.promote();
3298 if (parent == NULL) {
3299 // Should not happen, and nowhere to send errors to, so just log it
3300 CLOGE("RequestThread: Parent is gone");
3301 return INVALID_OPERATION;
3302 }
3303 res = parent->registerInFlight(halRequest->frame_number,
3304 totalNumBuffers, captureRequest->mResultExtras,
3305 /*hasInput*/halRequest->input_buffer != NULL,
3306 captureRequest->mAeTriggerCancelOverride);
3307 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3308 ", burstId = %" PRId32 ".",
3309 __FUNCTION__,
3310 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3311 captureRequest->mResultExtras.burstId);
3312 if (res != OK) {
3313 SET_ERR("RequestThread: Unable to register new in-flight request:"
3314 " %s (%d)", strerror(-res), res);
3315 return INVALID_OPERATION;
3316 }
3317 }
3318
3319 return OK;
3320}
3321
Igor Murashkin1e479c02013-09-06 16:55:14 -07003322CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3323 Mutex::Autolock al(mLatestRequestMutex);
3324
3325 ALOGV("RequestThread::%s", __FUNCTION__);
3326
3327 return mLatestRequest;
3328}
3329
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003330bool Camera3Device::RequestThread::isStreamPending(
3331 sp<Camera3StreamInterface>& stream) {
3332 Mutex::Autolock l(mRequestLock);
3333
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003334 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003335 if (!nextRequest.submitted) {
3336 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3337 if (stream == s) return true;
3338 }
3339 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003340 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003341 }
3342
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003343 for (const auto& request : mRequestQueue) {
3344 for (const auto& s : request->mOutputStreams) {
3345 if (stream == s) return true;
3346 }
3347 if (stream == request->mInputStream) return true;
3348 }
3349
3350 for (const auto& request : mRepeatingRequests) {
3351 for (const auto& s : request->mOutputStreams) {
3352 if (stream == s) return true;
3353 }
3354 if (stream == request->mInputStream) return true;
3355 }
3356
3357 return false;
3358}
Jianing Weicb0652e2014-03-12 18:29:36 -07003359
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003360void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3361 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003362 return;
3363 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003364
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003365 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003366 // Skip the ones that have been submitted successfully.
3367 if (nextRequest.submitted) {
3368 continue;
3369 }
3370
3371 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3372 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3373 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3374
3375 if (halRequest->settings != NULL) {
3376 captureRequest->mSettings.unlock(halRequest->settings);
3377 }
3378
3379 if (captureRequest->mInputStream != NULL) {
3380 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3381 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3382 }
3383
3384 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3385 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3386 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3387 }
3388
3389 if (sendRequestError) {
3390 Mutex::Autolock l(mRequestLock);
3391 if (mListener != NULL) {
3392 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003393 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003394 captureRequest->mResultExtras);
3395 }
3396 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003397 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003398
3399 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003400 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003401}
3402
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003403void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003404 // Optimized a bit for the simple steady-state case (single repeating
3405 // request), to avoid putting that request in the queue temporarily.
3406 Mutex::Autolock l(mRequestLock);
3407
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003408 assert(mNextRequests.empty());
3409
3410 NextRequest nextRequest;
3411 nextRequest.captureRequest = waitForNextRequestLocked();
3412 if (nextRequest.captureRequest == nullptr) {
3413 return;
3414 }
3415
3416 nextRequest.halRequest = camera3_capture_request_t();
3417 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003418 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003419
3420 // Wait for additional requests
3421 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3422
3423 for (size_t i = 1; i < batchSize; i++) {
3424 NextRequest additionalRequest;
3425 additionalRequest.captureRequest = waitForNextRequestLocked();
3426 if (additionalRequest.captureRequest == nullptr) {
3427 break;
3428 }
3429
3430 additionalRequest.halRequest = camera3_capture_request_t();
3431 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003432 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003433 }
3434
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003435 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003436 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003437 mNextRequests.size(), batchSize);
3438 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003439 }
3440
3441 return;
3442}
3443
3444sp<Camera3Device::CaptureRequest>
3445 Camera3Device::RequestThread::waitForNextRequestLocked() {
3446 status_t res;
3447 sp<CaptureRequest> nextRequest;
3448
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003449 while (mRequestQueue.empty()) {
3450 if (!mRepeatingRequests.empty()) {
3451 // Always atomically enqueue all requests in a repeating request
3452 // list. Guarantees a complete in-sequence set of captures to
3453 // application.
3454 const RequestList &requests = mRepeatingRequests;
3455 RequestList::const_iterator firstRequest =
3456 requests.begin();
3457 nextRequest = *firstRequest;
3458 mRequestQueue.insert(mRequestQueue.end(),
3459 ++firstRequest,
3460 requests.end());
3461 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003462
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003463 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003464
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003465 break;
3466 }
3467
3468 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3469
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003470 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3471 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003472 Mutex::Autolock pl(mPauseLock);
3473 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003474 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003475 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003476 // Let the tracker know
3477 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3478 if (statusTracker != 0) {
3479 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3480 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003481 }
3482 // Stop waiting for now and let thread management happen
3483 return NULL;
3484 }
3485 }
3486
3487 if (nextRequest == NULL) {
3488 // Don't have a repeating request already in hand, so queue
3489 // must have an entry now.
3490 RequestList::iterator firstRequest =
3491 mRequestQueue.begin();
3492 nextRequest = *firstRequest;
3493 mRequestQueue.erase(firstRequest);
3494 }
3495
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003496 // In case we've been unpaused by setPaused clearing mDoPause, need to
3497 // update internal pause state (capture/setRepeatingRequest unpause
3498 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003499 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003500 if (mPaused) {
3501 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3502 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3503 if (statusTracker != 0) {
3504 statusTracker->markComponentActive(mStatusId);
3505 }
3506 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003507 mPaused = false;
3508
3509 // Check if we've reconfigured since last time, and reset the preview
3510 // request if so. Can't use 'NULL request == repeat' across configure calls.
3511 if (mReconfigured) {
3512 mPrevRequest.clear();
3513 mReconfigured = false;
3514 }
3515
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003516 if (nextRequest != NULL) {
3517 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003518 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3519 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003520
3521 // Since RequestThread::clear() removes buffers from the input stream,
3522 // get the right buffer here before unlocking mRequestLock
3523 if (nextRequest->mInputStream != NULL) {
3524 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3525 if (res != OK) {
3526 // Can't get input buffer from gralloc queue - this could be due to
3527 // disconnected queue or other producer misbehavior, so not a fatal
3528 // error
3529 ALOGE("%s: Can't get input buffer, skipping request:"
3530 " %s (%d)", __FUNCTION__, strerror(-res), res);
3531 if (mListener != NULL) {
3532 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003533 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003534 nextRequest->mResultExtras);
3535 }
3536 return NULL;
3537 }
3538 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003539 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003540
3541 handleAePrecaptureCancelRequest(nextRequest);
3542
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003543 return nextRequest;
3544}
3545
3546bool Camera3Device::RequestThread::waitIfPaused() {
3547 status_t res;
3548 Mutex::Autolock l(mPauseLock);
3549 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003550 if (mPaused == false) {
3551 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003552 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3553 // Let the tracker know
3554 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3555 if (statusTracker != 0) {
3556 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3557 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003558 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003559
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003560 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003561 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003562 return true;
3563 }
3564 }
3565 // We don't set mPaused to false here, because waitForNextRequest needs
3566 // to further manage the paused state in case of starvation.
3567 return false;
3568}
3569
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003570void Camera3Device::RequestThread::unpauseForNewRequests() {
3571 // With work to do, mark thread as unpaused.
3572 // If paused by request (setPaused), don't resume, to avoid
3573 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003574 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003575 Mutex::Autolock p(mPauseLock);
3576 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003577 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3578 if (mPaused) {
3579 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3580 if (statusTracker != 0) {
3581 statusTracker->markComponentActive(mStatusId);
3582 }
3583 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003584 mPaused = false;
3585 }
3586}
3587
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003588void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3589 sp<Camera3Device> parent = mParent.promote();
3590 if (parent != NULL) {
3591 va_list args;
3592 va_start(args, fmt);
3593
3594 parent->setErrorStateV(fmt, args);
3595
3596 va_end(args);
3597 }
3598}
3599
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003600status_t Camera3Device::RequestThread::insertTriggers(
3601 const sp<CaptureRequest> &request) {
3602
3603 Mutex::Autolock al(mTriggerMutex);
3604
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003605 sp<Camera3Device> parent = mParent.promote();
3606 if (parent == NULL) {
3607 CLOGE("RequestThread: Parent is gone");
3608 return DEAD_OBJECT;
3609 }
3610
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003611 CameraMetadata &metadata = request->mSettings;
3612 size_t count = mTriggerMap.size();
3613
3614 for (size_t i = 0; i < count; ++i) {
3615 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003616 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003617
3618 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3619 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3620 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003621 if (isAeTrigger) {
3622 request->mResultExtras.precaptureTriggerId = triggerId;
3623 mCurrentPreCaptureTriggerId = triggerId;
3624 } else {
3625 request->mResultExtras.afTriggerId = triggerId;
3626 mCurrentAfTriggerId = triggerId;
3627 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003628 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3629 continue; // Trigger ID tag is deprecated since device HAL 3.2
3630 }
3631 }
3632
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003633 camera_metadata_entry entry = metadata.find(tag);
3634
3635 if (entry.count > 0) {
3636 /**
3637 * Already has an entry for this trigger in the request.
3638 * Rewrite it with our requested trigger value.
3639 */
3640 RequestTrigger oldTrigger = trigger;
3641
3642 oldTrigger.entryValue = entry.data.u8[0];
3643
3644 mTriggerReplacedMap.add(tag, oldTrigger);
3645 } else {
3646 /**
3647 * More typical, no trigger entry, so we just add it
3648 */
3649 mTriggerRemovedMap.add(tag, trigger);
3650 }
3651
3652 status_t res;
3653
3654 switch (trigger.getTagType()) {
3655 case TYPE_BYTE: {
3656 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3657 res = metadata.update(tag,
3658 &entryValue,
3659 /*count*/1);
3660 break;
3661 }
3662 case TYPE_INT32:
3663 res = metadata.update(tag,
3664 &trigger.entryValue,
3665 /*count*/1);
3666 break;
3667 default:
3668 ALOGE("%s: Type not supported: 0x%x",
3669 __FUNCTION__,
3670 trigger.getTagType());
3671 return INVALID_OPERATION;
3672 }
3673
3674 if (res != OK) {
3675 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3676 ", value %d", __FUNCTION__, trigger.getTagName(),
3677 trigger.entryValue);
3678 return res;
3679 }
3680
3681 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3682 trigger.getTagName(),
3683 trigger.entryValue);
3684 }
3685
3686 mTriggerMap.clear();
3687
3688 return count;
3689}
3690
3691status_t Camera3Device::RequestThread::removeTriggers(
3692 const sp<CaptureRequest> &request) {
3693 Mutex::Autolock al(mTriggerMutex);
3694
3695 CameraMetadata &metadata = request->mSettings;
3696
3697 /**
3698 * Replace all old entries with their old values.
3699 */
3700 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3701 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3702
3703 status_t res;
3704
3705 uint32_t tag = trigger.metadataTag;
3706 switch (trigger.getTagType()) {
3707 case TYPE_BYTE: {
3708 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3709 res = metadata.update(tag,
3710 &entryValue,
3711 /*count*/1);
3712 break;
3713 }
3714 case TYPE_INT32:
3715 res = metadata.update(tag,
3716 &trigger.entryValue,
3717 /*count*/1);
3718 break;
3719 default:
3720 ALOGE("%s: Type not supported: 0x%x",
3721 __FUNCTION__,
3722 trigger.getTagType());
3723 return INVALID_OPERATION;
3724 }
3725
3726 if (res != OK) {
3727 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3728 ", trigger value %d", __FUNCTION__,
3729 trigger.getTagName(), trigger.entryValue);
3730 return res;
3731 }
3732 }
3733 mTriggerReplacedMap.clear();
3734
3735 /**
3736 * Remove all new entries.
3737 */
3738 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3739 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3740 status_t res = metadata.erase(trigger.metadataTag);
3741
3742 if (res != OK) {
3743 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3744 ", trigger value %d", __FUNCTION__,
3745 trigger.getTagName(), trigger.entryValue);
3746 return res;
3747 }
3748 }
3749 mTriggerRemovedMap.clear();
3750
3751 return OK;
3752}
3753
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003754status_t Camera3Device::RequestThread::addDummyTriggerIds(
3755 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003756 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003757 static const int32_t dummyTriggerId = 1;
3758 status_t res;
3759
3760 CameraMetadata &metadata = request->mSettings;
3761
3762 // If AF trigger is active, insert a dummy AF trigger ID if none already
3763 // exists
3764 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3765 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3766 if (afTrigger.count > 0 &&
3767 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3768 afId.count == 0) {
3769 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3770 if (res != OK) return res;
3771 }
3772
3773 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3774 // if none already exists
3775 camera_metadata_entry pcTrigger =
3776 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3777 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3778 if (pcTrigger.count > 0 &&
3779 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3780 pcId.count == 0) {
3781 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3782 &dummyTriggerId, 1);
3783 if (res != OK) return res;
3784 }
3785
3786 return OK;
3787}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003788
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003789/**
3790 * PreparerThread inner class methods
3791 */
3792
3793Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003794 Thread(/*canCallJava*/false), mListener(nullptr),
3795 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003796}
3797
3798Camera3Device::PreparerThread::~PreparerThread() {
3799 Thread::requestExitAndWait();
3800 if (mCurrentStream != nullptr) {
3801 mCurrentStream->cancelPrepare();
3802 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3803 mCurrentStream.clear();
3804 }
3805 clear();
3806}
3807
Ruben Brunkc78ac262015-08-13 17:58:46 -07003808status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003809 status_t res;
3810
3811 Mutex::Autolock l(mLock);
3812
Ruben Brunkc78ac262015-08-13 17:58:46 -07003813 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003814 if (res == OK) {
3815 // No preparation needed, fire listener right off
3816 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3817 if (mListener) {
3818 mListener->notifyPrepared(stream->getId());
3819 }
3820 return OK;
3821 } else if (res != NOT_ENOUGH_DATA) {
3822 return res;
3823 }
3824
3825 // Need to prepare, start up thread if necessary
3826 if (!mActive) {
3827 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3828 // isn't running
3829 Thread::requestExitAndWait();
3830 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3831 if (res != OK) {
3832 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3833 if (mListener) {
3834 mListener->notifyPrepared(stream->getId());
3835 }
3836 return res;
3837 }
3838 mCancelNow = false;
3839 mActive = true;
3840 ALOGV("%s: Preparer stream started", __FUNCTION__);
3841 }
3842
3843 // queue up the work
3844 mPendingStreams.push_back(stream);
3845 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3846
3847 return OK;
3848}
3849
3850status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003851 Mutex::Autolock l(mLock);
3852
3853 for (const auto& stream : mPendingStreams) {
3854 stream->cancelPrepare();
3855 }
3856 mPendingStreams.clear();
3857 mCancelNow = true;
3858
3859 return OK;
3860}
3861
3862void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3863 Mutex::Autolock l(mLock);
3864 mListener = listener;
3865}
3866
3867bool Camera3Device::PreparerThread::threadLoop() {
3868 status_t res;
3869 {
3870 Mutex::Autolock l(mLock);
3871 if (mCurrentStream == nullptr) {
3872 // End thread if done with work
3873 if (mPendingStreams.empty()) {
3874 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3875 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3876 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3877 mActive = false;
3878 return false;
3879 }
3880
3881 // Get next stream to prepare
3882 auto it = mPendingStreams.begin();
3883 mCurrentStream = *it;
3884 mPendingStreams.erase(it);
3885 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3886 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3887 } else if (mCancelNow) {
3888 mCurrentStream->cancelPrepare();
3889 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3890 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3891 mCurrentStream.clear();
3892 mCancelNow = false;
3893 return true;
3894 }
3895 }
3896
3897 res = mCurrentStream->prepareNextBuffer();
3898 if (res == NOT_ENOUGH_DATA) return true;
3899 if (res != OK) {
3900 // Something bad happened; try to recover by cancelling prepare and
3901 // signalling listener anyway
3902 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3903 mCurrentStream->getId(), res, strerror(-res));
3904 mCurrentStream->cancelPrepare();
3905 }
3906
3907 // This stream has finished, notify listener
3908 Mutex::Autolock l(mLock);
3909 if (mListener) {
3910 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3911 mCurrentStream->getId());
3912 mListener->notifyPrepared(mCurrentStream->getId());
3913 }
3914
3915 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3916 mCurrentStream.clear();
3917
3918 return true;
3919}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003920
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003921/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003922 * Static callback forwarding methods from HAL to instance
3923 */
3924
3925void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3926 const camera3_capture_result *result) {
3927 Camera3Device *d =
3928 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003929
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003930 d->processCaptureResult(result);
3931}
3932
3933void Camera3Device::sNotify(const camera3_callback_ops *cb,
3934 const camera3_notify_msg *msg) {
3935 Camera3Device *d =
3936 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3937 d->notify(msg);
3938}
3939
3940}; // namespace android