blob: aeab451a9abdaa6e59fcfd28d43368552b4ffc9a [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>
Zhijun He90f7c372016-08-16 16:19:43 -070045#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070046
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080047#include <android/hardware/camera2/ICameraDeviceUser.h>
48
Igor Murashkinff3e31d2013-10-23 16:40:06 -070049#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070050#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070051#include "device3/Camera3Device.h"
52#include "device3/Camera3OutputStream.h"
53#include "device3/Camera3InputStream.h"
54#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070055#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070056#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080057
58using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080059
60namespace android {
61
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080062Camera3Device::Camera3Device(int id):
63 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070064 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080065 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070066 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070067 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070068 mUsePartialResult(false),
69 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080070 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070071 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070072 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070073 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070074 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070075 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080076{
77 ATRACE_CALL();
78 camera3_callback_ops::notify = &sNotify;
79 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
80 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
81}
82
83Camera3Device::~Camera3Device()
84{
85 ATRACE_CALL();
86 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
87 disconnect();
88}
89
Igor Murashkin71381052013-03-04 14:53:08 -080090int Camera3Device::getId() const {
91 return mId;
92}
93
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080094/**
95 * CameraDeviceBase interface
96 */
97
Yin-Chia Yehe074a932015-01-30 10:29:02 -080098status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080099{
100 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700101 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800102 Mutex::Autolock l(mLock);
103
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800104 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800105 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700106 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800107 return INVALID_OPERATION;
108 }
109
110 /** Open HAL device */
111
112 status_t res;
113 String8 deviceName = String8::format("%d", mId);
114
115 camera3_device_t *device;
116
Zhijun He213ce792013-11-19 08:45:15 -0800117 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800118 res = module->open(deviceName.string(),
119 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800120 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800121
122 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700123 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800124 return res;
125 }
126
127 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700128 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700129 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700130 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700131 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800132 device->common.version);
133 device->common.close(&device->common);
134 return BAD_VALUE;
135 }
136
137 camera_info info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800138 res = module->getCameraInfo(mId, &info);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 if (res != OK) return res;
140
141 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700142 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
143 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700144 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800145 device->common.close(&device->common);
146 return BAD_VALUE;
147 }
148
149 /** Initialize device with callback functions */
150
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700151 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800152 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700153 ATRACE_END();
154
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800155 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700156 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
157 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800158 device->common.close(&device->common);
159 return BAD_VALUE;
160 }
161
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700162 /** Start up status tracker thread */
163 mStatusTracker = new StatusTracker(this);
164 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
165 if (res != OK) {
166 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
167 strerror(-res), res);
168 device->common.close(&device->common);
169 mStatusTracker.clear();
170 return res;
171 }
172
Zhijun He125684a2015-12-26 15:07:30 -0800173 /** Create buffer manager */
174 mBufferManager = new Camera3BufferManager();
175
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700176 bool aeLockAvailable = false;
177 camera_metadata_ro_entry aeLockAvailableEntry;
178 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
179 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
180 if (res == OK && aeLockAvailableEntry.count > 0) {
181 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
182 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
183 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800184
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700185 /** Start up request queue thread */
186 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800187 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800188 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700189 SET_ERR_L("Unable to start request queue thread: %s (%d)",
190 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800191 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800192 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800193 return res;
194 }
195
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700196 mPreparerThread = new PreparerThread();
197
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800198 /** Everything is good to go */
199
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700200 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800201 mDeviceInfo = info.static_camera_characteristics;
202 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700203
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700204 // Determine whether we need to derive sensitivity boost values for older devices.
205 // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
206 // be listed (as the default value 100)
207 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_4 &&
208 mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
209 mDerivePostRawSensKey = true;
210 }
211
Ruben Brunk183f0562015-08-12 12:55:02 -0700212 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800213 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700214 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700215 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700216 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800217
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800218 // Measure the clock domain offset between camera and video/hw_composer
219 camera_metadata_entry timestampSource =
220 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
221 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
222 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
223 mTimestampOffset = getMonoToBoottimeOffset();
224 }
225
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700226 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700227 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
228 camera_metadata_entry partialResultsCount =
229 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
230 if (partialResultsCount.count > 0) {
231 mNumPartialResults = partialResultsCount.data.i32[0];
232 mUsePartialResult = (mNumPartialResults > 1);
233 }
234 } else {
235 camera_metadata_entry partialResultsQuirk =
236 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
237 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
238 mUsePartialResult = true;
239 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700240 }
241
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700242 camera_metadata_entry configs =
243 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
244 for (uint32_t i = 0; i < configs.count; i += 4) {
245 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
246 configs.data.i32[i + 3] ==
247 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
248 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
249 configs.data.i32[i + 2]));
250 }
251 }
252
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800253 return OK;
254}
255
256status_t Camera3Device::disconnect() {
257 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700258 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800259
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700260 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800261
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700262 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800263
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700264 {
265 Mutex::Autolock l(mLock);
266 if (mStatus == STATUS_UNINITIALIZED) return res;
267
268 if (mStatus == STATUS_ACTIVE ||
269 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
270 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700271 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700272 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700273 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700274 } else {
275 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
276 if (res != OK) {
277 SET_ERR_L("Timeout waiting for HAL to drain");
278 // Continue to close device even in case of error
279 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700280 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800281 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800282
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700283 if (mStatus == STATUS_ERROR) {
284 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700285 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700286
287 if (mStatusTracker != NULL) {
288 mStatusTracker->requestExit();
289 }
290
291 if (mRequestThread != NULL) {
292 mRequestThread->requestExit();
293 }
294
295 mOutputStreams.clear();
296 mInputStream.clear();
297 }
298
299 // Joining done without holding mLock, otherwise deadlocks may ensue
300 // as the threads try to access parent state
301 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
302 // HAL may be in a bad state, so waiting for request thread
303 // (which may be stuck in the HAL processCaptureRequest call)
304 // could be dangerous.
305 mRequestThread->join();
306 }
307
308 if (mStatusTracker != NULL) {
309 mStatusTracker->join();
310 }
311
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700312 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700313 {
314 Mutex::Autolock l(mLock);
315
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800316 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700317 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800318 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800319
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700320 hal3Device = mHal3Device;
321 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800322
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700323 // Call close without internal mutex held, as the HAL close may need to
324 // wait on assorted callbacks,etc, to complete before it can return.
325 if (hal3Device != NULL) {
326 ATRACE_BEGIN("camera3->close");
327 hal3Device->common.close(&hal3Device->common);
328 ATRACE_END();
329 }
330
331 {
332 Mutex::Autolock l(mLock);
333 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700334 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700335 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800336
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700337 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700338 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800339}
340
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700341// For dumping/debugging only -
342// try to acquire a lock a few times, eventually give up to proceed with
343// debug/dump operations
344bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
345 bool gotLock = false;
346 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
347 if (lock.tryLock() == NO_ERROR) {
348 gotLock = true;
349 break;
350 } else {
351 usleep(kDumpSleepDuration);
352 }
353 }
354 return gotLock;
355}
356
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700357Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
358 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
359 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
360 const int STREAM_CONFIGURATION_SIZE = 4;
361 const int STREAM_FORMAT_OFFSET = 0;
362 const int STREAM_WIDTH_OFFSET = 1;
363 const int STREAM_HEIGHT_OFFSET = 2;
364 const int STREAM_IS_INPUT_OFFSET = 3;
365 camera_metadata_ro_entry_t availableStreamConfigs =
366 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
367 if (availableStreamConfigs.count == 0 ||
368 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
369 return Size(0, 0);
370 }
371
372 // Get max jpeg size (area-wise).
373 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
374 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
375 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
376 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
377 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
378 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
379 && format == HAL_PIXEL_FORMAT_BLOB &&
380 (width * height > maxJpegWidth * maxJpegHeight)) {
381 maxJpegWidth = width;
382 maxJpegHeight = height;
383 }
384 }
385 } else {
386 camera_metadata_ro_entry availableJpegSizes =
387 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
388 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
389 return Size(0, 0);
390 }
391
392 // Get max jpeg size (area-wise).
393 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
394 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
395 > (maxJpegWidth * maxJpegHeight)) {
396 maxJpegWidth = availableJpegSizes.data.i32[i];
397 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
398 }
399 }
400 }
401 return Size(maxJpegWidth, maxJpegHeight);
402}
403
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800404nsecs_t Camera3Device::getMonoToBoottimeOffset() {
405 // try three times to get the clock offset, choose the one
406 // with the minimum gap in measurements.
407 const int tries = 3;
408 nsecs_t bestGap, measured;
409 for (int i = 0; i < tries; ++i) {
410 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
411 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
412 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
413 const nsecs_t gap = tmono2 - tmono;
414 if (i == 0 || gap < bestGap) {
415 bestGap = gap;
416 measured = tbase - ((tmono + tmono2) >> 1);
417 }
418 }
419 return measured;
420}
421
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -0700422/**
423 * Map Android N dataspace definitions back to Android M definitions, for
424 * use with HALv3.3 or older.
425 *
426 * Only map where correspondences exist, and otherwise preserve the value.
427 */
428android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) {
429 switch (dataSpace) {
430 case HAL_DATASPACE_V0_SRGB_LINEAR:
431 return HAL_DATASPACE_SRGB_LINEAR;
432 case HAL_DATASPACE_V0_SRGB:
433 return HAL_DATASPACE_SRGB;
434 case HAL_DATASPACE_V0_JFIF:
435 return HAL_DATASPACE_JFIF;
436 case HAL_DATASPACE_V0_BT601_625:
437 return HAL_DATASPACE_BT601_625;
438 case HAL_DATASPACE_V0_BT601_525:
439 return HAL_DATASPACE_BT601_525;
440 case HAL_DATASPACE_V0_BT709:
441 return HAL_DATASPACE_BT709;
442 default:
443 return dataSpace;
444 }
445}
446
Zhijun Hef7da0962014-04-24 13:27:56 -0700447ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700448 // Get max jpeg size (area-wise).
449 Size maxJpegResolution = getMaxJpegResolution();
450 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700451 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700452 __FUNCTION__, mId);
453 return BAD_VALUE;
454 }
455
Zhijun Hef7da0962014-04-24 13:27:56 -0700456 // Get max jpeg buffer size
457 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700458 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
459 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700460 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
461 return BAD_VALUE;
462 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700463 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800464 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700465
466 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700467 float scaleFactor = ((float) (width * height)) /
468 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800469 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
470 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700471 if (jpegBufferSize > maxJpegBufferSize) {
472 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700473 }
474
475 return jpegBufferSize;
476}
477
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700478ssize_t Camera3Device::getPointCloudBufferSize() const {
479 const int FLOATS_PER_POINT=4;
480 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
481 if (maxPointCount.count == 0) {
482 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
483 __FUNCTION__, mId);
484 return BAD_VALUE;
485 }
486 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
487 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
488 return maxBytesForPointCloud;
489}
490
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800491ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800492 const int PER_CONFIGURATION_SIZE = 3;
493 const int WIDTH_OFFSET = 0;
494 const int HEIGHT_OFFSET = 1;
495 const int SIZE_OFFSET = 2;
496 camera_metadata_ro_entry rawOpaqueSizes =
497 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800498 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800499 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800500 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800501 __FUNCTION__, mId, count);
502 return BAD_VALUE;
503 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700504
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800505 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
506 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
507 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
508 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
509 }
510 }
511
512 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
513 __FUNCTION__, mId, width, height);
514 return BAD_VALUE;
515}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700516
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800517status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
518 ATRACE_CALL();
519 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700520
521 // Try to lock, but continue in case of failure (to avoid blocking in
522 // deadlocks)
523 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
524 bool gotLock = tryLockSpinRightRound(mLock);
525
526 ALOGW_IF(!gotInterfaceLock,
527 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
528 mId, __FUNCTION__);
529 ALOGW_IF(!gotLock,
530 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
531 mId, __FUNCTION__);
532
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800533 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700534
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800535 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700536 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800537 int n = args.size();
538 for (int i = 0; i < n; i++) {
539 if (args[i] == templatesOption) {
540 dumpTemplates = true;
541 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700542 if (args[i] == monitorOption) {
543 if (i + 1 < n) {
544 String8 monitorTags = String8(args[i + 1]);
545 if (monitorTags == "off") {
546 mTagMonitor.disableMonitoring();
547 } else {
548 mTagMonitor.parseTagsToMonitor(monitorTags);
549 }
550 } else {
551 mTagMonitor.disableMonitoring();
552 }
553 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800554 }
555
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800556 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800557
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800558 const char *status =
559 mStatus == STATUS_ERROR ? "ERROR" :
560 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700561 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
562 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800563 mStatus == STATUS_ACTIVE ? "ACTIVE" :
564 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700565
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800566 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700567 if (mStatus == STATUS_ERROR) {
568 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
569 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800570 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700571 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700572 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800573
574 if (mInputStream != NULL) {
575 write(fd, lines.string(), lines.size());
576 mInputStream->dump(fd, args);
577 } else {
578 lines.appendFormat(" No input stream.\n");
579 write(fd, lines.string(), lines.size());
580 }
581 for (size_t i = 0; i < mOutputStreams.size(); i++) {
582 mOutputStreams[i]->dump(fd,args);
583 }
584
Zhijun He431503c2016-03-07 17:30:16 -0800585 if (mBufferManager != NULL) {
586 lines = String8(" Camera3 Buffer Manager:\n");
587 write(fd, lines.string(), lines.size());
588 mBufferManager->dump(fd, args);
589 }
Zhijun He125684a2015-12-26 15:07:30 -0800590
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700591 lines = String8(" In-flight requests:\n");
592 if (mInFlightMap.size() == 0) {
593 lines.append(" None\n");
594 } else {
595 for (size_t i = 0; i < mInFlightMap.size(); i++) {
596 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700597 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700598 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800599 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700600 r.numBuffersLeft);
601 }
602 }
603 write(fd, lines.string(), lines.size());
604
Igor Murashkin1e479c02013-09-06 16:55:14 -0700605 {
606 lines = String8(" Last request sent:\n");
607 write(fd, lines.string(), lines.size());
608
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700609 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700610 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
611 }
612
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800613 if (dumpTemplates) {
614 const char *templateNames[] = {
615 "TEMPLATE_PREVIEW",
616 "TEMPLATE_STILL_CAPTURE",
617 "TEMPLATE_VIDEO_RECORD",
618 "TEMPLATE_VIDEO_SNAPSHOT",
619 "TEMPLATE_ZERO_SHUTTER_LAG",
620 "TEMPLATE_MANUAL"
621 };
622
623 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
624 const camera_metadata_t *templateRequest;
625 templateRequest =
626 mHal3Device->ops->construct_default_request_settings(
627 mHal3Device, i);
628 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
629 if (templateRequest == NULL) {
630 lines.append(" Not supported\n");
631 write(fd, lines.string(), lines.size());
632 } else {
633 write(fd, lines.string(), lines.size());
634 dump_indented_camera_metadata(templateRequest,
635 fd, /*verbosity*/2, /*indentation*/8);
636 }
637 }
638 }
639
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700640 mTagMonitor.dumpMonitoredMetadata(fd);
641
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800642 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700643 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800644 write(fd, lines.string(), lines.size());
645 mHal3Device->ops->dump(mHal3Device, fd);
646 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800647
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700648 if (gotLock) mLock.unlock();
649 if (gotInterfaceLock) mInterfaceLock.unlock();
650
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800651 return OK;
652}
653
654const CameraMetadata& Camera3Device::info() const {
655 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800656 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
657 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700658 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800659 mStatus == STATUS_ERROR ?
660 "when in error state" : "before init");
661 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800662 return mDeviceInfo;
663}
664
Jianing Wei90e59c92014-03-12 18:29:36 -0700665status_t Camera3Device::checkStatusOkToCaptureLocked() {
666 switch (mStatus) {
667 case STATUS_ERROR:
668 CLOGE("Device has encountered a serious error");
669 return INVALID_OPERATION;
670 case STATUS_UNINITIALIZED:
671 CLOGE("Device not initialized");
672 return INVALID_OPERATION;
673 case STATUS_UNCONFIGURED:
674 case STATUS_CONFIGURED:
675 case STATUS_ACTIVE:
676 // OK
677 break;
678 default:
679 SET_ERR_L("Unexpected status: %d", mStatus);
680 return INVALID_OPERATION;
681 }
682 return OK;
683}
684
685status_t Camera3Device::convertMetadataListToRequestListLocked(
686 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
687 if (requestList == NULL) {
688 CLOGE("requestList cannot be NULL.");
689 return BAD_VALUE;
690 }
691
Jianing Weicb0652e2014-03-12 18:29:36 -0700692 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700693 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
694 it != metadataList.end(); ++it) {
695 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
696 if (newRequest == 0) {
697 CLOGE("Can't create capture request");
698 return BAD_VALUE;
699 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700700
701 // Setup burst Id and request Id
702 newRequest->mResultExtras.burstId = burstId++;
703 if (it->exists(ANDROID_REQUEST_ID)) {
704 if (it->find(ANDROID_REQUEST_ID).count == 0) {
705 CLOGE("RequestID entry exists; but must not be empty in metadata");
706 return BAD_VALUE;
707 }
708 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
709 } else {
710 CLOGE("RequestID does not exist in metadata");
711 return BAD_VALUE;
712 }
713
Jianing Wei90e59c92014-03-12 18:29:36 -0700714 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700715
716 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700717 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700718
719 // Setup batch size if this is a high speed video recording request.
720 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
721 auto firstRequest = requestList->begin();
722 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
723 if (outputStream->isVideoStream()) {
724 (*firstRequest)->mBatchSize = requestList->size();
725 break;
726 }
727 }
728 }
729
Jianing Wei90e59c92014-03-12 18:29:36 -0700730 return OK;
731}
732
Jianing Weicb0652e2014-03-12 18:29:36 -0700733status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800734 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800735
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700736 List<const CameraMetadata> requests;
737 requests.push_back(request);
738 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800739}
740
Jianing Wei90e59c92014-03-12 18:29:36 -0700741status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700742 const List<const CameraMetadata> &requests, bool repeating,
743 /*out*/
744 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700745 ATRACE_CALL();
746 Mutex::Autolock il(mInterfaceLock);
747 Mutex::Autolock l(mLock);
748
749 status_t res = checkStatusOkToCaptureLocked();
750 if (res != OK) {
751 // error logged by previous call
752 return res;
753 }
754
755 RequestList requestList;
756
757 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
758 if (res != OK) {
759 // error logged by previous call
760 return res;
761 }
762
763 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700764 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700765 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700766 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700767 }
768
769 if (res == OK) {
770 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
771 if (res != OK) {
772 SET_ERR_L("Can't transition to active in %f seconds!",
773 kActiveTimeout/1e9);
774 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700775 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
776 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700777 } else {
778 CLOGE("Cannot queue request. Impossible.");
779 return BAD_VALUE;
780 }
781
782 return res;
783}
784
Jianing Weicb0652e2014-03-12 18:29:36 -0700785status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
786 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700787 ATRACE_CALL();
788
Jianing Weicb0652e2014-03-12 18:29:36 -0700789 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700790}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800791
Jianing Weicb0652e2014-03-12 18:29:36 -0700792status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
793 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800794 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800795
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700796 List<const CameraMetadata> requests;
797 requests.push_back(request);
798 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800799}
800
Jianing Weicb0652e2014-03-12 18:29:36 -0700801status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
802 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700803 ATRACE_CALL();
804
Jianing Weicb0652e2014-03-12 18:29:36 -0700805 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700806}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800807
808sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
809 const CameraMetadata &request) {
810 status_t res;
811
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700812 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800813 res = configureStreamsLocked();
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700814 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800815 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700816 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800817 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700818 } else if (mStatus == STATUS_UNCONFIGURED) {
819 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700820 CLOGE("No streams configured");
821 return NULL;
822 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800823 }
824
825 sp<CaptureRequest> newRequest = createCaptureRequest(request);
826 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800827}
828
Jianing Weicb0652e2014-03-12 18:29:36 -0700829status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800830 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700831 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800832 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800833
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800834 switch (mStatus) {
835 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700836 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800837 return INVALID_OPERATION;
838 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700839 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800840 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700841 case STATUS_UNCONFIGURED:
842 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800843 case STATUS_ACTIVE:
844 // OK
845 break;
846 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700847 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800848 return INVALID_OPERATION;
849 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700850 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700851
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700852 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800853}
854
855status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
856 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700857 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800858
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700859 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800860}
861
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700862status_t Camera3Device::createInputStream(
863 uint32_t width, uint32_t height, int format, int *id) {
864 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700865 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700866 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700867 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
868 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700869
870 status_t res;
871 bool wasActive = false;
872
873 switch (mStatus) {
874 case STATUS_ERROR:
875 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
876 return INVALID_OPERATION;
877 case STATUS_UNINITIALIZED:
878 ALOGE("%s: Device not initialized", __FUNCTION__);
879 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700880 case STATUS_UNCONFIGURED:
881 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700882 // OK
883 break;
884 case STATUS_ACTIVE:
885 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700886 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700887 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700888 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700889 return res;
890 }
891 wasActive = true;
892 break;
893 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700894 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700895 return INVALID_OPERATION;
896 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700897 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700898
899 if (mInputStream != 0) {
900 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
901 return INVALID_OPERATION;
902 }
903
904 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
905 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700906 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700907
908 mInputStream = newStream;
909
910 *id = mNextStreamId++;
911
912 // Continue captures if active at start
913 if (wasActive) {
914 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
915 res = configureStreamsLocked();
916 if (res != OK) {
917 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
918 __FUNCTION__, mNextStreamId, strerror(-res), res);
919 return res;
920 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700921 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700922 }
923
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700924 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700925 return OK;
926}
927
Igor Murashkin2fba5842013-04-22 14:03:54 -0700928
929status_t Camera3Device::createZslStream(
930 uint32_t width, uint32_t height,
931 int depth,
932 /*out*/
933 int *id,
934 sp<Camera3ZslStream>* zslStream) {
935 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700936 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700937 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700938 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
939 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700940
941 status_t res;
942 bool wasActive = false;
943
944 switch (mStatus) {
945 case STATUS_ERROR:
946 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
947 return INVALID_OPERATION;
948 case STATUS_UNINITIALIZED:
949 ALOGE("%s: Device not initialized", __FUNCTION__);
950 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700951 case STATUS_UNCONFIGURED:
952 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700953 // OK
954 break;
955 case STATUS_ACTIVE:
956 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700957 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700958 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700959 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700960 return res;
961 }
962 wasActive = true;
963 break;
964 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700965 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700966 return INVALID_OPERATION;
967 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700968 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700969
970 if (mInputStream != 0) {
971 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
972 return INVALID_OPERATION;
973 }
974
975 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
976 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700977 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700978
979 res = mOutputStreams.add(mNextStreamId, newStream);
980 if (res < 0) {
981 ALOGE("%s: Can't add new stream to set: %s (%d)",
982 __FUNCTION__, strerror(-res), res);
983 return res;
984 }
985 mInputStream = newStream;
986
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530987 mNeedConfig = true;
988
Igor Murashkin2fba5842013-04-22 14:03:54 -0700989 *id = mNextStreamId++;
990 *zslStream = newStream;
991
992 // Continue captures if active at start
993 if (wasActive) {
994 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
995 res = configureStreamsLocked();
996 if (res != OK) {
997 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
998 __FUNCTION__, mNextStreamId, strerror(-res), res);
999 return res;
1000 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001001 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -07001002 }
1003
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001004 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -07001005 return OK;
1006}
1007
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001008status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001009 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -07001010 camera3_stream_rotation_t rotation, int *id, int streamSetId, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001011 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001012 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001013 Mutex::Autolock l(mLock);
Zhijun He5d677d12016-05-29 16:52:39 -07001014 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
1015 " consumer usage 0x%x", mId, mNextStreamId, width, height, format, dataSpace, rotation,
1016 consumerUsage);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001017
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001018 status_t res;
1019 bool wasActive = false;
1020
1021 switch (mStatus) {
1022 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001023 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001024 return INVALID_OPERATION;
1025 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001026 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001027 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001028 case STATUS_UNCONFIGURED:
1029 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001030 // OK
1031 break;
1032 case STATUS_ACTIVE:
1033 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001034 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001035 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001036 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001037 return res;
1038 }
1039 wasActive = true;
1040 break;
1041 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001042 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001043 return INVALID_OPERATION;
1044 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001045 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001046
1047 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001048 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001049 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001050 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001051 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1052 }
Zhijun He5d677d12016-05-29 16:52:39 -07001053
1054 // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
1055 // which requires a consumer surface to be available.
1056 if (consumer == nullptr && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1057 ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
1058 return BAD_VALUE;
1059 }
1060
1061 if (consumer == nullptr && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1062 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1063 return BAD_VALUE;
1064 }
1065
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001066 // Use legacy dataspace values for older HALs
1067 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1068 dataSpace = mapToLegacyDataspace(dataSpace);
1069 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001070 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001071 ssize_t blobBufferSize;
1072 if (dataSpace != HAL_DATASPACE_DEPTH) {
1073 blobBufferSize = getJpegBufferSize(width, height);
1074 if (blobBufferSize <= 0) {
1075 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1076 return BAD_VALUE;
1077 }
1078 } else {
1079 blobBufferSize = getPointCloudBufferSize();
1080 if (blobBufferSize <= 0) {
1081 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1082 return BAD_VALUE;
1083 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001084 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001085 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001086 width, height, blobBufferSize, format, dataSpace, rotation,
1087 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001088 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1089 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1090 if (rawOpaqueBufferSize <= 0) {
1091 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1092 return BAD_VALUE;
1093 }
1094 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001095 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1096 mTimestampOffset, streamSetId);
Zhijun He5d677d12016-05-29 16:52:39 -07001097 } else if (consumer == nullptr) {
1098 newStream = new Camera3OutputStream(mNextStreamId,
1099 width, height, format, consumerUsage, dataSpace, rotation,
1100 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001101 } else {
1102 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001103 width, height, format, dataSpace, rotation,
1104 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001105 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001106 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001107
Zhijun He125684a2015-12-26 15:07:30 -08001108 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001109 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1110 * requires buffers to be statically allocated for internal static buffer registration, while
1111 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1112 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001113 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001114 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001115 newStream->setBufferManager(mBufferManager);
1116 }
1117
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001118 res = mOutputStreams.add(mNextStreamId, newStream);
1119 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001120 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001121 return res;
1122 }
1123
1124 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001125 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001126
1127 // Continue captures if active at start
1128 if (wasActive) {
1129 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1130 res = configureStreamsLocked();
1131 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001132 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1133 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001134 return res;
1135 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001136 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001137 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001138 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001139 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001140}
1141
1142status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1143 ATRACE_CALL();
1144 (void)outputId; (void)id;
1145
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001146 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001147 return INVALID_OPERATION;
1148}
1149
1150
1151status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001152 uint32_t *width, uint32_t *height,
1153 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001154 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001155 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001156 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001157
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001158 switch (mStatus) {
1159 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001160 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001161 return INVALID_OPERATION;
1162 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001163 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001164 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001165 case STATUS_UNCONFIGURED:
1166 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001167 case STATUS_ACTIVE:
1168 // OK
1169 break;
1170 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001171 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001172 return INVALID_OPERATION;
1173 }
1174
1175 ssize_t idx = mOutputStreams.indexOfKey(id);
1176 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001177 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001178 return idx;
1179 }
1180
1181 if (width) *width = mOutputStreams[idx]->getWidth();
1182 if (height) *height = mOutputStreams[idx]->getHeight();
1183 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001184 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001185 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001186}
1187
1188status_t Camera3Device::setStreamTransform(int id,
1189 int transform) {
1190 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001191 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001192 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001193
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001194 switch (mStatus) {
1195 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001196 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001197 return INVALID_OPERATION;
1198 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001199 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001200 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001201 case STATUS_UNCONFIGURED:
1202 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001203 case STATUS_ACTIVE:
1204 // OK
1205 break;
1206 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001207 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001208 return INVALID_OPERATION;
1209 }
1210
1211 ssize_t idx = mOutputStreams.indexOfKey(id);
1212 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001213 CLOGE("Stream %d does not exist",
1214 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001215 return BAD_VALUE;
1216 }
1217
1218 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001219}
1220
1221status_t Camera3Device::deleteStream(int id) {
1222 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001223 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001224 Mutex::Autolock l(mLock);
1225 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001226
Igor Murashkine2172be2013-05-28 15:31:39 -07001227 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1228
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001229 // CameraDevice semantics require device to already be idle before
1230 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001231 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001232 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1233 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001234 }
1235
Igor Murashkin2fba5842013-04-22 14:03:54 -07001236 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001237 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001238 if (mInputStream != NULL && id == mInputStream->getId()) {
1239 deletedStream = mInputStream;
1240 mInputStream.clear();
1241 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001242 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001243 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001244 return BAD_VALUE;
1245 }
Zhijun He5f446352014-01-22 09:49:33 -08001246 }
1247
1248 // Delete output stream or the output part of a bi-directional stream.
1249 if (outputStreamIdx != NAME_NOT_FOUND) {
1250 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001251 mOutputStreams.removeItem(id);
1252 }
1253
1254 // Free up the stream endpoint so that it can be used by some other stream
1255 res = deletedStream->disconnect();
1256 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001257 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001258 // fall through since we want to still list the stream as deleted.
1259 }
1260 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001261 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001262
1263 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001264}
1265
1266status_t Camera3Device::deleteReprocessStream(int id) {
1267 ATRACE_CALL();
1268 (void)id;
1269
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001270 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001271 return INVALID_OPERATION;
1272}
1273
Zhijun He1fa89992015-06-01 15:44:31 -07001274status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001275 ATRACE_CALL();
1276 ALOGV("%s: E", __FUNCTION__);
1277
1278 Mutex::Autolock il(mInterfaceLock);
1279 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001280
1281 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1282 mNeedConfig = true;
1283 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1284 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001285
1286 return configureStreamsLocked();
1287}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001288
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001289status_t Camera3Device::getInputBufferProducer(
1290 sp<IGraphicBufferProducer> *producer) {
1291 Mutex::Autolock il(mInterfaceLock);
1292 Mutex::Autolock l(mLock);
1293
1294 if (producer == NULL) {
1295 return BAD_VALUE;
1296 } else if (mInputStream == NULL) {
1297 return INVALID_OPERATION;
1298 }
1299
1300 return mInputStream->getInputBufferProducer(producer);
1301}
1302
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001303status_t Camera3Device::createDefaultRequest(int templateId,
1304 CameraMetadata *request) {
1305 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001306 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001307
1308 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1309 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1310 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1311 return BAD_VALUE;
1312 }
1313
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001314 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001315 Mutex::Autolock l(mLock);
1316
1317 switch (mStatus) {
1318 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001319 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001320 return INVALID_OPERATION;
1321 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001322 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001323 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001324 case STATUS_UNCONFIGURED:
1325 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001326 case STATUS_ACTIVE:
1327 // OK
1328 break;
1329 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001330 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001331 return INVALID_OPERATION;
1332 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001333
Zhijun Hea1530f12014-09-14 12:44:20 -07001334 if (!mRequestTemplateCache[templateId].isEmpty()) {
1335 *request = mRequestTemplateCache[templateId];
1336 return OK;
1337 }
1338
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001339 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001340 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001341 rawRequest = mHal3Device->ops->construct_default_request_settings(
1342 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001343 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001344 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001345 ALOGI("%s: template %d is not supported on this camera device",
1346 __FUNCTION__, templateId);
1347 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001348 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001349
Zhijun Hea1530f12014-09-14 12:44:20 -07001350 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001351
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001352 // Derive some new keys for backward compatibility
1353 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
1354 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
1355 int32_t defaultBoost[1] = {100};
1356 mRequestTemplateCache[templateId].update(
1357 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1358 defaultBoost, 1);
1359 }
1360
1361 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001362 return OK;
1363}
1364
1365status_t Camera3Device::waitUntilDrained() {
1366 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001367 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001368 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001369
Zhijun He69a37482014-03-23 18:44:49 -07001370 return waitUntilDrainedLocked();
1371}
1372
1373status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001374 switch (mStatus) {
1375 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001376 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001377 ALOGV("%s: Already idle", __FUNCTION__);
1378 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001379 case STATUS_CONFIGURED:
1380 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381 case STATUS_ERROR:
1382 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001383 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001384 break;
1385 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001386 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001387 return INVALID_OPERATION;
1388 }
1389
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001390 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1391 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001392 if (res != OK) {
1393 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1394 res);
1395 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001396 return res;
1397}
1398
Ruben Brunk183f0562015-08-12 12:55:02 -07001399
1400void Camera3Device::internalUpdateStatusLocked(Status status) {
1401 mStatus = status;
1402 mRecentStatusUpdates.add(mStatus);
1403 mStatusChanged.broadcast();
1404}
1405
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001406// Pause to reconfigure
1407status_t Camera3Device::internalPauseAndWaitLocked() {
1408 mRequestThread->setPaused(true);
1409 mPauseStateNotify = true;
1410
1411 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1412 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1413 if (res != OK) {
1414 SET_ERR_L("Can't idle device in %f seconds!",
1415 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001416 }
1417
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001418 return res;
1419}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001420
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001421// Resume after internalPauseAndWaitLocked
1422status_t Camera3Device::internalResumeLocked() {
1423 status_t res;
1424
1425 mRequestThread->setPaused(false);
1426
1427 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1428 if (res != OK) {
1429 SET_ERR_L("Can't transition to active in %f seconds!",
1430 kActiveTimeout/1e9);
1431 }
1432 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001433 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001434}
1435
Ruben Brunk183f0562015-08-12 12:55:02 -07001436status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001437 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001438
1439 size_t startIndex = 0;
1440 if (mStatusWaiters == 0) {
1441 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1442 // this status list
1443 mRecentStatusUpdates.clear();
1444 } else {
1445 // If other threads are waiting on updates to this status list, set the position of the
1446 // first element that this list will check rather than clearing the list.
1447 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001448 }
1449
Ruben Brunk183f0562015-08-12 12:55:02 -07001450 mStatusWaiters++;
1451
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001452 bool stateSeen = false;
1453 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001454 if (active == (mStatus == STATUS_ACTIVE)) {
1455 // Desired state is current
1456 break;
1457 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001458
1459 res = mStatusChanged.waitRelative(mLock, timeout);
1460 if (res != OK) break;
1461
Ruben Brunk183f0562015-08-12 12:55:02 -07001462 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1463 // transitions.
1464 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1465 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1466 __FUNCTION__);
1467
1468 // Encountered desired state since we began waiting
1469 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001470 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1471 stateSeen = true;
1472 break;
1473 }
1474 }
1475 } while (!stateSeen);
1476
Ruben Brunk183f0562015-08-12 12:55:02 -07001477 mStatusWaiters--;
1478
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001479 return res;
1480}
1481
1482
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001483status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001484 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001485 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001486
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001487 if (listener != NULL && mListener != NULL) {
1488 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1489 }
1490 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001491 mRequestThread->setNotificationListener(listener);
1492 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001493
1494 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001495}
1496
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001497bool Camera3Device::willNotify3A() {
1498 return false;
1499}
1500
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001501status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001502 status_t res;
1503 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001504
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001505 while (mResultQueue.empty()) {
1506 res = mResultSignal.waitRelative(mOutputLock, timeout);
1507 if (res == TIMED_OUT) {
1508 return res;
1509 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001510 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001511 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001512 return res;
1513 }
1514 }
1515 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001516}
1517
Jianing Weicb0652e2014-03-12 18:29:36 -07001518status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001519 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001520 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001521
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001522 if (mResultQueue.empty()) {
1523 return NOT_ENOUGH_DATA;
1524 }
1525
Jianing Weicb0652e2014-03-12 18:29:36 -07001526 if (frame == NULL) {
1527 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1528 return BAD_VALUE;
1529 }
1530
1531 CaptureResult &result = *(mResultQueue.begin());
1532 frame->mResultExtras = result.mResultExtras;
1533 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001534 mResultQueue.erase(mResultQueue.begin());
1535
1536 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001537}
1538
1539status_t Camera3Device::triggerAutofocus(uint32_t id) {
1540 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001541 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001542
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001543 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1544 // Mix-in this trigger into the next request and only the next request.
1545 RequestTrigger trigger[] = {
1546 {
1547 ANDROID_CONTROL_AF_TRIGGER,
1548 ANDROID_CONTROL_AF_TRIGGER_START
1549 },
1550 {
1551 ANDROID_CONTROL_AF_TRIGGER_ID,
1552 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001553 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001554 };
1555
1556 return mRequestThread->queueTrigger(trigger,
1557 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001558}
1559
1560status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1561 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001562 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001563
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001564 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1565 // Mix-in this trigger into the next request and only the next request.
1566 RequestTrigger trigger[] = {
1567 {
1568 ANDROID_CONTROL_AF_TRIGGER,
1569 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1570 },
1571 {
1572 ANDROID_CONTROL_AF_TRIGGER_ID,
1573 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001574 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001575 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001576
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001577 return mRequestThread->queueTrigger(trigger,
1578 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001579}
1580
1581status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1582 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001583 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001584
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001585 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1586 // Mix-in this trigger into the next request and only the next request.
1587 RequestTrigger trigger[] = {
1588 {
1589 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1590 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1591 },
1592 {
1593 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1594 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001595 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001596 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001597
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001598 return mRequestThread->queueTrigger(trigger,
1599 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001600}
1601
1602status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1603 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1604 ATRACE_CALL();
1605 (void)reprocessStreamId; (void)buffer; (void)listener;
1606
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001607 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001608 return INVALID_OPERATION;
1609}
1610
Jianing Weicb0652e2014-03-12 18:29:36 -07001611status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001612 ATRACE_CALL();
1613 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001614 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001615
Zhijun He7ef20392014-04-21 16:04:17 -07001616 {
1617 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001618 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001619 }
1620
Zhijun He491e3412013-12-27 10:57:44 -08001621 status_t res;
1622 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001623 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001624 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001625 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001626 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001627 }
1628
1629 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001630}
1631
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001632status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001633 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1634}
1635
1636status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001637 ATRACE_CALL();
1638 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001639 Mutex::Autolock il(mInterfaceLock);
1640 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001641
1642 sp<Camera3StreamInterface> stream;
1643 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1644 if (outputStreamIdx == NAME_NOT_FOUND) {
1645 CLOGE("Stream %d does not exist", streamId);
1646 return BAD_VALUE;
1647 }
1648
1649 stream = mOutputStreams.editValueAt(outputStreamIdx);
1650
1651 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001652 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001653 return BAD_VALUE;
1654 }
1655
1656 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001657 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001658 return BAD_VALUE;
1659 }
1660
Ruben Brunkc78ac262015-08-13 17:58:46 -07001661 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001662}
1663
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001664status_t Camera3Device::tearDown(int streamId) {
1665 ATRACE_CALL();
1666 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1667 Mutex::Autolock il(mInterfaceLock);
1668 Mutex::Autolock l(mLock);
1669
1670 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1671 // since we cannot call register_stream_buffers except right after configure_streams.
1672 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1673 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1674 __FUNCTION__, mHal3Device->common.version);
1675 return NO_INIT;
1676 }
1677
1678 sp<Camera3StreamInterface> stream;
1679 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1680 if (outputStreamIdx == NAME_NOT_FOUND) {
1681 CLOGE("Stream %d does not exist", streamId);
1682 return BAD_VALUE;
1683 }
1684
1685 stream = mOutputStreams.editValueAt(outputStreamIdx);
1686
1687 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1688 CLOGE("Stream %d is a target of a in-progress request", streamId);
1689 return BAD_VALUE;
1690 }
1691
1692 return stream->tearDown();
1693}
1694
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001695status_t Camera3Device::addBufferListenerForStream(int streamId,
1696 wp<Camera3StreamBufferListener> listener) {
1697 ATRACE_CALL();
1698 ALOGV("%s: Camera %d: Adding buffer listener for stream %d", __FUNCTION__, mId, streamId);
1699 Mutex::Autolock il(mInterfaceLock);
1700 Mutex::Autolock l(mLock);
1701
1702 sp<Camera3StreamInterface> stream;
1703 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1704 if (outputStreamIdx == NAME_NOT_FOUND) {
1705 CLOGE("Stream %d does not exist", streamId);
1706 return BAD_VALUE;
1707 }
1708
1709 stream = mOutputStreams.editValueAt(outputStreamIdx);
1710 stream->addBufferListener(listener);
1711
1712 return OK;
1713}
1714
Zhijun He204e3292014-07-14 17:09:23 -07001715uint32_t Camera3Device::getDeviceVersion() {
1716 ATRACE_CALL();
1717 Mutex::Autolock il(mInterfaceLock);
1718 return mDeviceVersion;
1719}
1720
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001721/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001722 * Methods called by subclasses
1723 */
1724
1725void Camera3Device::notifyStatus(bool idle) {
1726 {
1727 // Need mLock to safely update state and synchronize to current
1728 // state of methods in flight.
1729 Mutex::Autolock l(mLock);
1730 // We can get various system-idle notices from the status tracker
1731 // while starting up. Only care about them if we've actually sent
1732 // in some requests recently.
1733 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1734 return;
1735 }
1736 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1737 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001738 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001739
1740 // Skip notifying listener if we're doing some user-transparent
1741 // state changes
1742 if (mPauseStateNotify) return;
1743 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001744
1745 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001746 {
1747 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001748 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001749 }
1750 if (idle && listener != NULL) {
1751 listener->notifyIdle();
1752 }
1753}
1754
Zhijun He5d677d12016-05-29 16:52:39 -07001755status_t Camera3Device::setConsumerSurface(int streamId, sp<Surface> consumer) {
1756 ATRACE_CALL();
1757 ALOGV("%s: Camera %d: set consumer surface for stream %d", __FUNCTION__, mId, streamId);
1758 Mutex::Autolock il(mInterfaceLock);
1759 Mutex::Autolock l(mLock);
1760
1761 if (consumer == nullptr) {
1762 CLOGE("Null consumer is passed!");
1763 return BAD_VALUE;
1764 }
1765
1766 ssize_t idx = mOutputStreams.indexOfKey(streamId);
1767 if (idx == NAME_NOT_FOUND) {
1768 CLOGE("Stream %d is unknown", streamId);
1769 return idx;
1770 }
1771 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
1772 status_t res = stream->setConsumer(consumer);
1773 if (res != OK) {
1774 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1775 return res;
1776 }
1777
1778 if (!stream->isConfiguring()) {
1779 CLOGE("Stream %d was already fully configured.", streamId);
1780 return INVALID_OPERATION;
1781 }
1782
1783 res = stream->finishConfiguration(mHal3Device);
1784 if (res != OK) {
1785 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1786 stream->getId(), strerror(-res), res);
1787 return res;
1788 }
1789
1790 return OK;
1791}
1792
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001793/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001794 * Camera3Device private methods
1795 */
1796
1797sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1798 const CameraMetadata &request) {
1799 ATRACE_CALL();
1800 status_t res;
1801
1802 sp<CaptureRequest> newRequest = new CaptureRequest;
1803 newRequest->mSettings = request;
1804
1805 camera_metadata_entry_t inputStreams =
1806 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1807 if (inputStreams.count > 0) {
1808 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001809 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001810 CLOGE("Request references unknown input stream %d",
1811 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001812 return NULL;
1813 }
1814 // Lazy completion of stream configuration (allocation/registration)
1815 // on first use
1816 if (mInputStream->isConfiguring()) {
1817 res = mInputStream->finishConfiguration(mHal3Device);
1818 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001819 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001820 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001821 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001822 return NULL;
1823 }
1824 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001825 // Check if stream is being prepared
1826 if (mInputStream->isPreparing()) {
1827 CLOGE("Request references an input stream that's being prepared!");
1828 return NULL;
1829 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001830
1831 newRequest->mInputStream = mInputStream;
1832 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1833 }
1834
1835 camera_metadata_entry_t streams =
1836 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1837 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001838 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001839 return NULL;
1840 }
1841
1842 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001843 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001844 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001845 CLOGE("Request references unknown stream %d",
1846 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001847 return NULL;
1848 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001849 sp<Camera3OutputStreamInterface> stream =
1850 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001851
Zhijun He5d677d12016-05-29 16:52:39 -07001852 // It is illegal to include a deferred consumer output stream into a request
1853 if (stream->isConsumerConfigurationDeferred()) {
1854 CLOGE("Stream %d hasn't finished configuration yet due to deferred consumer",
1855 stream->getId());
1856 return NULL;
1857 }
1858
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001859 // Lazy completion of stream configuration (allocation/registration)
1860 // on first use
1861 if (stream->isConfiguring()) {
1862 res = stream->finishConfiguration(mHal3Device);
1863 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001864 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1865 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001866 return NULL;
1867 }
1868 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001869 // Check if stream is being prepared
1870 if (stream->isPreparing()) {
1871 CLOGE("Request references an output stream that's being prepared!");
1872 return NULL;
1873 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001874
1875 newRequest->mOutputStreams.push(stream);
1876 }
1877 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001878 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001879
1880 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001881}
1882
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001883bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1884 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1885 Size size = mSupportedOpaqueInputSizes[i];
1886 if (size.width == width && size.height == height) {
1887 return true;
1888 }
1889 }
1890
1891 return false;
1892}
1893
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001894void Camera3Device::cancelStreamsConfigurationLocked() {
1895 int res = OK;
1896 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1897 res = mInputStream->cancelConfiguration();
1898 if (res != OK) {
1899 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
1900 mInputStream->getId(), strerror(-res), res);
1901 }
1902 }
1903
1904 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1905 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
1906 if (outputStream->isConfiguring()) {
1907 res = outputStream->cancelConfiguration();
1908 if (res != OK) {
1909 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
1910 outputStream->getId(), strerror(-res), res);
1911 }
1912 }
1913 }
1914
1915 // Return state to that at start of call, so that future configures
1916 // properly clean things up
1917 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
1918 mNeedConfig = true;
1919}
1920
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001921status_t Camera3Device::configureStreamsLocked() {
1922 ATRACE_CALL();
1923 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001924
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001925 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001926 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001927 return INVALID_OPERATION;
1928 }
1929
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001930 if (!mNeedConfig) {
1931 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1932 return OK;
1933 }
1934
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001935 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1936 // adding a dummy stream instead.
1937 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1938 if (mOutputStreams.size() == 0) {
1939 addDummyStreamLocked();
1940 } else {
1941 tryRemoveDummyStreamLocked();
1942 }
1943
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001944 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001945 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001946
1947 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001948 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1949 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1950 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001951 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1952
1953 Vector<camera3_stream_t*> streams;
1954 streams.setCapacity(config.num_streams);
1955
1956 if (mInputStream != NULL) {
1957 camera3_stream_t *inputStream;
1958 inputStream = mInputStream->startConfiguration();
1959 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001960 CLOGE("Can't start input stream configuration");
1961 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001962 return INVALID_OPERATION;
1963 }
1964 streams.add(inputStream);
1965 }
1966
1967 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001968
1969 // Don't configure bidi streams twice, nor add them twice to the list
1970 if (mOutputStreams[i].get() ==
1971 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1972
1973 config.num_streams--;
1974 continue;
1975 }
1976
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001977 camera3_stream_t *outputStream;
1978 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1979 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001980 CLOGE("Can't start output stream configuration");
1981 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001982 return INVALID_OPERATION;
1983 }
1984 streams.add(outputStream);
1985 }
1986
1987 config.streams = streams.editArray();
1988
1989 // Do the HAL configuration; will potentially touch stream
1990 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001991 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001992 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001993 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001994
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001995 if (res == BAD_VALUE) {
1996 // HAL rejected this set of streams as unsupported, clean up config
1997 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001998 CLOGE("Set of requested inputs/outputs not supported by HAL");
1999 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002000 return BAD_VALUE;
2001 } else if (res != OK) {
2002 // Some other kind of error from configure_streams - this is not
2003 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002004 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2005 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002006 return res;
2007 }
2008
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002009 // Finish all stream configuration immediately.
2010 // TODO: Try to relax this later back to lazy completion, which should be
2011 // faster
2012
Igor Murashkin073f8572013-05-02 14:59:28 -07002013 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002014 res = mInputStream->finishConfiguration(mHal3Device);
2015 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002016 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002017 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002018 cancelStreamsConfigurationLocked();
2019 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002020 }
2021 }
2022
2023 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002024 sp<Camera3OutputStreamInterface> outputStream =
2025 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002026 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002027 res = outputStream->finishConfiguration(mHal3Device);
2028 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002029 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002030 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002031 cancelStreamsConfigurationLocked();
2032 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002033 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002034 }
2035 }
2036
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002037 // Request thread needs to know to avoid using repeat-last-settings protocol
2038 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002039 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002040
Zhijun He90f7c372016-08-16 16:19:43 -07002041 char value[PROPERTY_VALUE_MAX];
2042 property_get("camera.fifo.disable", value, "0");
2043 int32_t disableFifo = atoi(value);
2044 if (disableFifo != 1) {
2045 // Boost priority of request thread to SCHED_FIFO.
2046 pid_t requestThreadTid = mRequestThread->getTid();
2047 res = requestPriority(getpid(), requestThreadTid,
2048 kRequestThreadPriority, /*asynchronous*/ false);
2049 if (res != OK) {
2050 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2051 strerror(-res), res);
2052 } else {
2053 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2054 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002055 }
2056
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002057 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002058
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002059 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002060
Ruben Brunk183f0562015-08-12 12:55:02 -07002061 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2062 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002063
2064 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
2065
Zhijun He0a210512014-07-24 13:45:15 -07002066 // tear down the deleted streams after configure streams.
2067 mDeletedStreams.clear();
2068
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002069 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002070}
2071
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002072status_t Camera3Device::addDummyStreamLocked() {
2073 ATRACE_CALL();
2074 status_t res;
2075
2076 if (mDummyStreamId != NO_STREAM) {
2077 // Should never be adding a second dummy stream when one is already
2078 // active
2079 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
2080 __FUNCTION__, mId);
2081 return INVALID_OPERATION;
2082 }
2083
2084 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
2085
2086 sp<Camera3OutputStreamInterface> dummyStream =
2087 new Camera3DummyStream(mNextStreamId);
2088
2089 res = mOutputStreams.add(mNextStreamId, dummyStream);
2090 if (res < 0) {
2091 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2092 return res;
2093 }
2094
2095 mDummyStreamId = mNextStreamId;
2096 mNextStreamId++;
2097
2098 return OK;
2099}
2100
2101status_t Camera3Device::tryRemoveDummyStreamLocked() {
2102 ATRACE_CALL();
2103 status_t res;
2104
2105 if (mDummyStreamId == NO_STREAM) return OK;
2106 if (mOutputStreams.size() == 1) return OK;
2107
2108 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
2109
2110 // Ok, have a dummy stream and there's at least one other output stream,
2111 // so remove the dummy
2112
2113 sp<Camera3StreamInterface> deletedStream;
2114 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2115 if (outputStreamIdx == NAME_NOT_FOUND) {
2116 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2117 return INVALID_OPERATION;
2118 }
2119
2120 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2121 mOutputStreams.removeItemsAt(outputStreamIdx);
2122
2123 // Free up the stream endpoint so that it can be used by some other stream
2124 res = deletedStream->disconnect();
2125 if (res != OK) {
2126 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2127 // fall through since we want to still list the stream as deleted.
2128 }
2129 mDeletedStreams.add(deletedStream);
2130 mDummyStreamId = NO_STREAM;
2131
2132 return res;
2133}
2134
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002135void Camera3Device::setErrorState(const char *fmt, ...) {
2136 Mutex::Autolock l(mLock);
2137 va_list args;
2138 va_start(args, fmt);
2139
2140 setErrorStateLockedV(fmt, args);
2141
2142 va_end(args);
2143}
2144
2145void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2146 Mutex::Autolock l(mLock);
2147 setErrorStateLockedV(fmt, args);
2148}
2149
2150void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2151 va_list args;
2152 va_start(args, fmt);
2153
2154 setErrorStateLockedV(fmt, args);
2155
2156 va_end(args);
2157}
2158
2159void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002160 // Print out all error messages to log
2161 String8 errorCause = String8::formatV(fmt, args);
2162 ALOGE("Camera %d: %s", mId, errorCause.string());
2163
2164 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002165 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002166
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002167 mErrorCause = errorCause;
2168
2169 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002170 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002171
2172 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002173 sp<NotificationListener> listener = mListener.promote();
2174 if (listener != NULL) {
2175 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002176 CaptureResultExtras());
2177 }
2178
2179 // Save stack trace. View by dumping it later.
2180 CameraTraces::saveTrace();
2181 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002182}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002183
2184/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002185 * In-flight request management
2186 */
2187
Jianing Weicb0652e2014-03-12 18:29:36 -07002188status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002189 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2190 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002191 ATRACE_CALL();
2192 Mutex::Autolock l(mInFlightLock);
2193
2194 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002195 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2196 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002197 if (res < 0) return res;
2198
2199 return OK;
2200}
2201
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002202void Camera3Device::returnOutputBuffers(
2203 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2204 nsecs_t timestamp) {
2205 for (size_t i = 0; i < numBuffers; i++)
2206 {
2207 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2208 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2209 // Note: stream may be deallocated at this point, if this buffer was
2210 // the last reference to it.
2211 if (res != OK) {
2212 ALOGE("Can't return buffer to its stream: %s (%d)",
2213 strerror(-res), res);
2214 }
2215 }
2216}
2217
2218
2219void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2220
2221 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2222 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2223
2224 nsecs_t sensorTimestamp = request.sensorTimestamp;
2225 nsecs_t shutterTimestamp = request.shutterTimestamp;
2226
2227 // Check if it's okay to remove the request from InFlightMap:
2228 // In the case of a successful request:
2229 // all input and output buffers, all result metadata, shutter callback
2230 // arrived.
2231 // In the case of a unsuccessful request:
2232 // all input and output buffers arrived.
2233 if (request.numBuffersLeft == 0 &&
2234 (request.requestStatus != OK ||
2235 (request.haveResultMetadata && shutterTimestamp != 0))) {
2236 ATRACE_ASYNC_END("frame capture", frameNumber);
2237
2238 // Sanity check - if sensor timestamp matches shutter timestamp
2239 if (request.requestStatus == OK &&
2240 sensorTimestamp != shutterTimestamp) {
2241 SET_ERR("sensor timestamp (%" PRId64
2242 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2243 sensorTimestamp, frameNumber, shutterTimestamp);
2244 }
2245
2246 // for an unsuccessful request, it may have pending output buffers to
2247 // return.
2248 assert(request.requestStatus != OK ||
2249 request.pendingOutputBuffers.size() == 0);
2250 returnOutputBuffers(request.pendingOutputBuffers.array(),
2251 request.pendingOutputBuffers.size(), 0);
2252
2253 mInFlightMap.removeItemsAt(idx, 1);
2254
2255 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2256 }
2257
2258 // Sanity check - if we have too many in-flight frames, something has
2259 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002260 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002261 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002262 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2263 kInFlightWarnLimitHighSpeed) {
2264 CLOGE("In-flight list too large for high speed configuration: %zu",
2265 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002266 }
2267}
2268
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002269void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2270 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2271 if (result == nullptr) return;
2272
2273 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2274 (int32_t*)&frameNumber, 1) != OK) {
2275 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2276 return;
2277 }
2278
2279 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2280 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2281 return;
2282 }
2283
2284 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2285
2286 // Valid result, insert into queue
2287 List<CaptureResult>::iterator queuedResult =
2288 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2289 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2290 ", burstId = %" PRId32, __FUNCTION__,
2291 queuedResult->mResultExtras.requestId,
2292 queuedResult->mResultExtras.frameNumber,
2293 queuedResult->mResultExtras.burstId);
2294
2295 mResultSignal.signal();
2296}
2297
2298
2299void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2300 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2301 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2302 Mutex::Autolock l(mOutputLock);
2303
2304 CaptureResult captureResult;
2305 captureResult.mResultExtras = resultExtras;
2306 captureResult.mMetadata = partialResult;
2307
2308 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2309}
2310
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002311
2312void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2313 CaptureResultExtras &resultExtras,
2314 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002315 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002316 bool reprocess,
2317 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002318 if (pendingMetadata.isEmpty())
2319 return;
2320
2321 Mutex::Autolock l(mOutputLock);
2322
2323 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002324 if (reprocess) {
2325 if (frameNumber < mNextReprocessResultFrameNumber) {
2326 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002327 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002328 frameNumber, mNextReprocessResultFrameNumber);
2329 return;
2330 }
2331 mNextReprocessResultFrameNumber = frameNumber + 1;
2332 } else {
2333 if (frameNumber < mNextResultFrameNumber) {
2334 SET_ERR("Out-of-order capture result metadata submitted! "
2335 "(got frame number %d, expecting %d)",
2336 frameNumber, mNextResultFrameNumber);
2337 return;
2338 }
2339 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002340 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002341
2342 CaptureResult captureResult;
2343 captureResult.mResultExtras = resultExtras;
2344 captureResult.mMetadata = pendingMetadata;
2345
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002346 // Append any previous partials to form a complete result
2347 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2348 captureResult.mMetadata.append(collectedPartialResult);
2349 }
2350
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07002351 // Derive some new keys for backward compaibility
2352 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
2353 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
2354 int32_t defaultBoost[1] = {100};
2355 captureResult.mMetadata.update(
2356 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
2357 defaultBoost, 1);
2358 }
2359
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002360 captureResult.mMetadata.sort();
2361
2362 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002363 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2364 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002365 SET_ERR("No timestamp provided by HAL for frame %d!",
2366 frameNumber);
2367 return;
2368 }
2369
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002370 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2371 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2372
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002373 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002374}
2375
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002376/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002377 * Camera HAL device callback methods
2378 */
2379
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002380void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002381 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002382
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002383 status_t res;
2384
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002385 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002386 if (result->result == NULL && result->num_output_buffers == 0 &&
2387 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002388 SET_ERR("No result data provided by HAL for frame %d",
2389 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002390 return;
2391 }
Zhijun He204e3292014-07-14 17:09:23 -07002392
2393 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2394 // partial_result to 1 when metadata is included in this result.
2395 if (!mUsePartialResult &&
2396 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2397 result->result != NULL &&
2398 result->partial_result != 1) {
2399 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2400 " if partial result is not supported",
2401 frameNumber, result->partial_result);
2402 return;
2403 }
2404
2405 bool isPartialResult = false;
2406 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002407 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002408 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002409
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002410 // Get shutter timestamp and resultExtras from list of in-flight requests,
2411 // where it was added by the shutter notification for this frame. If the
2412 // shutter timestamp isn't received yet, append the output buffers to the
2413 // in-flight request and they will be returned when the shutter timestamp
2414 // arrives. Update the in-flight status and remove the in-flight entry if
2415 // all result data and shutter timestamp have been received.
2416 nsecs_t shutterTimestamp = 0;
2417
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002418 {
2419 Mutex::Autolock l(mInFlightLock);
2420 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2421 if (idx == NAME_NOT_FOUND) {
2422 SET_ERR("Unknown frame number for capture result: %d",
2423 frameNumber);
2424 return;
2425 }
2426 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002427 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2428 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2429 ", partialResultCount = %d",
2430 __FUNCTION__, request.resultExtras.requestId,
2431 request.resultExtras.frameNumber, request.resultExtras.burstId,
2432 result->partial_result);
2433 // Always update the partial count to the latest one if it's not 0
2434 // (buffers only). When framework aggregates adjacent partial results
2435 // into one, the latest partial count will be used.
2436 if (result->partial_result != 0)
2437 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002438
2439 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002440 if (mUsePartialResult && result->result != NULL) {
2441 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2442 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2443 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2444 " the range of [1, %d] when metadata is included in the result",
2445 frameNumber, result->partial_result, mNumPartialResults);
2446 return;
2447 }
2448 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002449 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002450 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002451 }
Zhijun He204e3292014-07-14 17:09:23 -07002452 } else {
2453 camera_metadata_ro_entry_t partialResultEntry;
2454 res = find_camera_metadata_ro_entry(result->result,
2455 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2456 if (res != NAME_NOT_FOUND &&
2457 partialResultEntry.count > 0 &&
2458 partialResultEntry.data.u8[0] ==
2459 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2460 // A partial result. Flag this as such, and collect this
2461 // set of metadata into the in-flight entry.
2462 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002463 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002464 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002465 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002466 ANDROID_QUIRKS_PARTIAL_RESULT);
2467 }
2468 }
2469
2470 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002471 // Send partial capture result
2472 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2473 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002474 }
2475 }
2476
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002477 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002478 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002479
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002480 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002481 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002482 if (request.haveResultMetadata) {
2483 SET_ERR("Called multiple times with metadata for frame %d",
2484 frameNumber);
2485 return;
2486 }
Zhijun He204e3292014-07-14 17:09:23 -07002487 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002488 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002489 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002490 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002491 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002492 request.haveResultMetadata = true;
2493 }
2494
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002495 uint32_t numBuffersReturned = result->num_output_buffers;
2496 if (result->input_buffer != NULL) {
2497 if (hasInputBufferInRequest) {
2498 numBuffersReturned += 1;
2499 } else {
2500 ALOGW("%s: Input buffer should be NULL if there is no input"
2501 " buffer sent in the request",
2502 __FUNCTION__);
2503 }
2504 }
2505 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002506 if (request.numBuffersLeft < 0) {
2507 SET_ERR("Too many buffers returned for frame %d",
2508 frameNumber);
2509 return;
2510 }
2511
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002512 camera_metadata_ro_entry_t entry;
2513 res = find_camera_metadata_ro_entry(result->result,
2514 ANDROID_SENSOR_TIMESTAMP, &entry);
2515 if (res == OK && entry.count == 1) {
2516 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002517 }
2518
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002519 // If shutter event isn't received yet, append the output buffers to
2520 // the in-flight request. Otherwise, return the output buffers to
2521 // streams.
2522 if (shutterTimestamp == 0) {
2523 request.pendingOutputBuffers.appendArray(result->output_buffers,
2524 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002525 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002526 returnOutputBuffers(result->output_buffers,
2527 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002528 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002529
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002530 if (result->result != NULL && !isPartialResult) {
2531 if (shutterTimestamp == 0) {
2532 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002533 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002534 } else {
2535 CameraMetadata metadata;
2536 metadata = result->result;
2537 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002538 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2539 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002540 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002541 }
2542
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002543 removeInFlightRequestIfReadyLocked(idx);
2544 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002545
Zhijun Hef0d962a2014-06-30 10:24:11 -07002546 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002547 if (hasInputBufferInRequest) {
2548 Camera3Stream *stream =
2549 Camera3Stream::cast(result->input_buffer->stream);
2550 res = stream->returnInputBuffer(*(result->input_buffer));
2551 // Note: stream may be deallocated at this point, if this buffer was the
2552 // last reference to it.
2553 if (res != OK) {
2554 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2555 " its stream:%s (%d)", __FUNCTION__,
2556 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002557 }
2558 } else {
2559 ALOGW("%s: Input buffer should be NULL if there is no input"
2560 " buffer sent in the request, skipping input buffer return.",
2561 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002562 }
2563 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002564}
2565
2566void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002567 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002568 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002569 {
2570 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002571 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002572 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002573
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002574 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002575 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002576 return;
2577 }
2578
2579 switch (msg->type) {
2580 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002581 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002582 break;
2583 }
2584 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002585 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002586 break;
2587 }
2588 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002589 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002590 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002591 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002592}
2593
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002594void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002595 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002596
2597 // Map camera HAL error codes to ICameraDeviceCallback error codes
2598 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002599 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002600 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002601 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002602 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002603 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002604 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002605 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002606 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002607 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002608 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002609 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002610 };
2611
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002612 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002613 ((msg.error_code >= 0) &&
2614 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2615 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002616 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002617
2618 int streamId = 0;
2619 if (msg.error_stream != NULL) {
2620 Camera3Stream *stream =
2621 Camera3Stream::cast(msg.error_stream);
2622 streamId = stream->getId();
2623 }
2624 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2625 mId, __FUNCTION__, msg.frame_number,
2626 streamId, msg.error_code);
2627
2628 CaptureResultExtras resultExtras;
2629 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002630 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002631 // SET_ERR calls notifyError
2632 SET_ERR("Camera HAL reported serious device error");
2633 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002634 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2635 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2636 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002637 {
2638 Mutex::Autolock l(mInFlightLock);
2639 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2640 if (idx >= 0) {
2641 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2642 r.requestStatus = msg.error_code;
2643 resultExtras = r.resultExtras;
2644 } else {
2645 resultExtras.frameNumber = msg.frame_number;
2646 ALOGE("Camera %d: %s: cannot find in-flight request on "
2647 "frame %" PRId64 " error", mId, __FUNCTION__,
2648 resultExtras.frameNumber);
2649 }
2650 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002651 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002652 if (listener != NULL) {
2653 listener->notifyError(errorCode, resultExtras);
2654 } else {
2655 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2656 }
2657 break;
2658 default:
2659 // SET_ERR calls notifyError
2660 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2661 break;
2662 }
2663}
2664
2665void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002666 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002667 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002668
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002669 // Set timestamp for the request in the in-flight tracking
2670 // and get the request ID to send upstream
2671 {
2672 Mutex::Autolock l(mInFlightLock);
2673 idx = mInFlightMap.indexOfKey(msg.frame_number);
2674 if (idx >= 0) {
2675 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002676
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002677 // Verify ordering of shutter notifications
2678 {
2679 Mutex::Autolock l(mOutputLock);
2680 // TODO: need to track errors for tighter bounds on expected frame number.
2681 if (r.hasInputBuffer) {
2682 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2683 SET_ERR("Shutter notification out-of-order. Expected "
2684 "notification for frame %d, got frame %d",
2685 mNextReprocessShutterFrameNumber, msg.frame_number);
2686 return;
2687 }
2688 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2689 } else {
2690 if (msg.frame_number < mNextShutterFrameNumber) {
2691 SET_ERR("Shutter notification out-of-order. Expected "
2692 "notification for frame %d, got frame %d",
2693 mNextShutterFrameNumber, msg.frame_number);
2694 return;
2695 }
2696 mNextShutterFrameNumber = msg.frame_number + 1;
2697 }
2698 }
2699
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002700 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2701 mId, __FUNCTION__,
2702 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2703 // Call listener, if any
2704 if (listener != NULL) {
2705 listener->notifyShutter(r.resultExtras, msg.timestamp);
2706 }
2707
2708 r.shutterTimestamp = msg.timestamp;
2709
2710 // send pending result and buffers
2711 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002712 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002713 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002714 returnOutputBuffers(r.pendingOutputBuffers.array(),
2715 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2716 r.pendingOutputBuffers.clear();
2717
2718 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002719 }
2720 }
2721 if (idx < 0) {
2722 SET_ERR("Shutter notification for non-existent frame number %d",
2723 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002724 }
2725}
2726
2727
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002728CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002729 ALOGV("%s", __FUNCTION__);
2730
Igor Murashkin1e479c02013-09-06 16:55:14 -07002731 CameraMetadata retVal;
2732
2733 if (mRequestThread != NULL) {
2734 retVal = mRequestThread->getLatestRequest();
2735 }
2736
Igor Murashkin1e479c02013-09-06 16:55:14 -07002737 return retVal;
2738}
2739
Jianing Weicb0652e2014-03-12 18:29:36 -07002740
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002741void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
2742 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
2743 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
2744}
2745
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002746/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002747 * RequestThread inner class methods
2748 */
2749
2750Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002751 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002752 camera3_device_t *hal3Device,
2753 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002754 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002755 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002756 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002757 mHal3Device(hal3Device),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002758 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002759 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002760 mReconfigured(false),
2761 mDoPause(false),
2762 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002763 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002764 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002765 mCurrentAfTriggerId(0),
2766 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002767 mRepeatingLastFrameNumber(
2768 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002769 mAeLockAvailable(aeLockAvailable),
2770 mPrepareVideoStream(false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002771 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002772}
2773
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002774void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002775 wp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002776 Mutex::Autolock l(mRequestLock);
2777 mListener = listener;
2778}
2779
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002780void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002781 Mutex::Autolock l(mRequestLock);
2782 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002783 // Prepare video stream for high speed recording.
2784 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002785}
2786
Jianing Wei90e59c92014-03-12 18:29:36 -07002787status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002788 List<sp<CaptureRequest> > &requests,
2789 /*out*/
2790 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002791 Mutex::Autolock l(mRequestLock);
2792 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2793 ++it) {
2794 mRequestQueue.push_back(*it);
2795 }
2796
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002797 if (lastFrameNumber != NULL) {
2798 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2799 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2800 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2801 *lastFrameNumber);
2802 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002803
Jianing Wei90e59c92014-03-12 18:29:36 -07002804 unpauseForNewRequests();
2805
2806 return OK;
2807}
2808
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002809
2810status_t Camera3Device::RequestThread::queueTrigger(
2811 RequestTrigger trigger[],
2812 size_t count) {
2813
2814 Mutex::Autolock l(mTriggerMutex);
2815 status_t ret;
2816
2817 for (size_t i = 0; i < count; ++i) {
2818 ret = queueTriggerLocked(trigger[i]);
2819
2820 if (ret != OK) {
2821 return ret;
2822 }
2823 }
2824
2825 return OK;
2826}
2827
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002828int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2829 sp<Camera3Device> d = device.promote();
2830 if (d != NULL) return d->mId;
2831 return 0;
2832}
2833
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002834status_t Camera3Device::RequestThread::queueTriggerLocked(
2835 RequestTrigger trigger) {
2836
2837 uint32_t tag = trigger.metadataTag;
2838 ssize_t index = mTriggerMap.indexOfKey(tag);
2839
2840 switch (trigger.getTagType()) {
2841 case TYPE_BYTE:
2842 // fall-through
2843 case TYPE_INT32:
2844 break;
2845 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002846 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2847 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002848 return INVALID_OPERATION;
2849 }
2850
2851 /**
2852 * Collect only the latest trigger, since we only have 1 field
2853 * in the request settings per trigger tag, and can't send more than 1
2854 * trigger per request.
2855 */
2856 if (index != NAME_NOT_FOUND) {
2857 mTriggerMap.editValueAt(index) = trigger;
2858 } else {
2859 mTriggerMap.add(tag, trigger);
2860 }
2861
2862 return OK;
2863}
2864
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002865status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002866 const RequestList &requests,
2867 /*out*/
2868 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002869 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002870 if (lastFrameNumber != NULL) {
2871 *lastFrameNumber = mRepeatingLastFrameNumber;
2872 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002873 mRepeatingRequests.clear();
2874 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2875 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002876
2877 unpauseForNewRequests();
2878
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002879 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002880 return OK;
2881}
2882
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002883bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2884 if (mRepeatingRequests.empty()) {
2885 return false;
2886 }
2887 int32_t requestId = requestIn->mResultExtras.requestId;
2888 const RequestList &repeatRequests = mRepeatingRequests;
2889 // All repeating requests are guaranteed to have same id so only check first quest
2890 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2891 return (firstRequest->mResultExtras.requestId == requestId);
2892}
2893
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002894status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002895 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002896 return clearRepeatingRequestsLocked(lastFrameNumber);
2897
2898}
2899
2900status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002901 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002902 if (lastFrameNumber != NULL) {
2903 *lastFrameNumber = mRepeatingLastFrameNumber;
2904 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002905 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002906 return OK;
2907}
2908
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002909status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002910 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002911 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002912 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002913
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002914 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002915
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002916 // Send errors for all requests pending in the request queue, including
2917 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002918 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002919 if (listener != NULL) {
2920 for (RequestList::iterator it = mRequestQueue.begin();
2921 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002922 // Abort the input buffers for reprocess requests.
2923 if ((*it)->mInputStream != NULL) {
2924 camera3_stream_buffer_t inputBuffer;
2925 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2926 if (res != OK) {
2927 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2928 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2929 } else {
2930 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2931 if (res != OK) {
2932 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2933 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2934 }
2935 }
2936 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002937 // Set the frame number this request would have had, if it
2938 // had been submitted; this frame number will not be reused.
2939 // The requestId and burstId fields were set when the request was
2940 // submitted originally (in convertMetadataListToRequestListLocked)
2941 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002942 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002943 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002944 }
2945 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002946 mRequestQueue.clear();
2947 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002948 if (lastFrameNumber != NULL) {
2949 *lastFrameNumber = mRepeatingLastFrameNumber;
2950 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002951 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002952 return OK;
2953}
2954
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002955status_t Camera3Device::RequestThread::flush() {
2956 ATRACE_CALL();
2957 Mutex::Autolock l(mFlushLock);
2958
2959 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2960 return mHal3Device->ops->flush(mHal3Device);
2961 }
2962
2963 return -ENOTSUP;
2964}
2965
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002966void Camera3Device::RequestThread::setPaused(bool paused) {
2967 Mutex::Autolock l(mPauseLock);
2968 mDoPause = paused;
2969 mDoPauseSignal.signal();
2970}
2971
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002972status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2973 int32_t requestId, nsecs_t timeout) {
2974 Mutex::Autolock l(mLatestRequestMutex);
2975 status_t res;
2976 while (mLatestRequestId != requestId) {
2977 nsecs_t startTime = systemTime();
2978
2979 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2980 if (res != OK) return res;
2981
2982 timeout -= (systemTime() - startTime);
2983 }
2984
2985 return OK;
2986}
2987
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002988void Camera3Device::RequestThread::requestExit() {
2989 // Call parent to set up shutdown
2990 Thread::requestExit();
2991 // The exit from any possible waits
2992 mDoPauseSignal.signal();
2993 mRequestSignal.signal();
2994}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002995
Chien-Yu Chend196d612015-06-22 19:49:01 -07002996
2997/**
2998 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2999 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
3000 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
3001 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
3002 * request.
3003 */
3004void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
3005 request->mAeTriggerCancelOverride.applyAeLock = false;
3006 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
3007
3008 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
3009 return;
3010 }
3011
3012 camera_metadata_entry_t aePrecaptureTrigger =
3013 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3014 if (aePrecaptureTrigger.count > 0 &&
3015 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
3016 // Always override CANCEL to IDLE
3017 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
3018 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
3019 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
3020 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
3021 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3022
3023 if (mAeLockAvailable == true) {
3024 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3025 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3026 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3027 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3028 request->mAeTriggerCancelOverride.applyAeLock = true;
3029 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3030 }
3031 }
3032 }
3033}
3034
3035/**
3036 * Override result metadata for cancelling AE precapture trigger applied in
3037 * handleAePrecaptureCancelRequest().
3038 */
3039void Camera3Device::overrideResultForPrecaptureCancel(
3040 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3041 if (aeTriggerCancelOverride.applyAeLock) {
3042 // Only devices <= v3.2 should have this override
3043 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3044 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3045 }
3046
3047 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3048 // Only devices <= v3.2 should have this override
3049 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3050 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3051 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3052 }
3053}
3054
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003055void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003056 bool surfaceAbandoned = false;
3057 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003058 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003059 {
3060 Mutex::Autolock l(mRequestLock);
3061 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3062 // repeating requests.
3063 for (const auto& request : mRepeatingRequests) {
3064 for (const auto& s : request->mOutputStreams) {
3065 if (s->isAbandoned()) {
3066 surfaceAbandoned = true;
3067 clearRepeatingRequestsLocked(&lastFrameNumber);
3068 break;
3069 }
3070 }
3071 if (surfaceAbandoned) {
3072 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003073 }
3074 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003075 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003076 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003077
3078 if (listener != NULL && surfaceAbandoned) {
3079 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003080 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003081}
3082
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003083bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003084 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003085 status_t res;
3086
3087 // Handle paused state.
3088 if (waitIfPaused()) {
3089 return true;
3090 }
3091
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003092 // Wait for the next batch of requests.
3093 waitForNextRequestBatch();
3094 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003095 return true;
3096 }
3097
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003098 // Get the latest request ID, if any
3099 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003100 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003101 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003102 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003103 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003104 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003105 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3106 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003107 }
3108
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003109 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003110 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003111 if (res == TIMED_OUT) {
3112 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003113 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003114 // Check if any stream is abandoned.
3115 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003116 return true;
3117 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003118 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003119 return false;
3120 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003121
Zhijun Hecc27e112013-10-03 16:12:43 -07003122 // Inform waitUntilRequestProcessed thread of a new request ID
3123 {
3124 Mutex::Autolock al(mLatestRequestMutex);
3125
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003126 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003127 mLatestRequestSignal.signal();
3128 }
3129
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003130 // Submit a batch of requests to HAL.
3131 // Use flush lock only when submitting multilple requests in a batch.
3132 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3133 // which may take a long time to finish so synchronizing flush() and
3134 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3135 // For now, only synchronize for high speed recording and we should figure something out for
3136 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003137 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003138
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003139 if (useFlushLock) {
3140 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003141 }
3142
Zhijun Hef0645c12016-08-02 00:58:11 -07003143 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003144 mNextRequests.size());
3145 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003146 // Submit request and block until ready for next one
3147 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3148 ATRACE_BEGIN("camera3->process_capture_request");
3149 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3150 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003151
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003152 if (res != OK) {
3153 // Should only get a failure here for malformed requests or device-level
3154 // errors, so consider all errors fatal. Bad metadata failures should
3155 // come through notify.
3156 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3157 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3158 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003159 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003160 if (useFlushLock) {
3161 mFlushLock.unlock();
3162 }
3163 return false;
3164 }
3165
3166 // Mark that the request has be submitted successfully.
3167 nextRequest.submitted = true;
3168
3169 // Update the latest request sent to HAL
3170 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3171 Mutex::Autolock al(mLatestRequestMutex);
3172
3173 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3174 mLatestRequest.acquire(cloned);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003175
3176 sp<Camera3Device> parent = mParent.promote();
3177 if (parent != NULL) {
3178 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
3179 0, mLatestRequest);
3180 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003181 }
3182
3183 if (nextRequest.halRequest.settings != NULL) {
3184 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3185 }
3186
3187 // Remove any previously queued triggers (after unlock)
3188 res = removeTriggers(mPrevRequest);
3189 if (res != OK) {
3190 SET_ERR("RequestThread: Unable to remove triggers "
3191 "(capture request %d, HAL device: %s (%d)",
3192 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003193 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003194 if (useFlushLock) {
3195 mFlushLock.unlock();
3196 }
3197 return false;
3198 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003199 }
3200
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003201 if (useFlushLock) {
3202 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003203 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003204
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003205 // Unset as current request
3206 {
3207 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003208 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003209 }
3210
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003211 return true;
3212}
3213
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003214status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003215 ATRACE_CALL();
3216
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003217 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003218 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3219 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3220 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3221
3222 // Prepare a request to HAL
3223 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3224
3225 // Insert any queued triggers (before metadata is locked)
3226 status_t res = insertTriggers(captureRequest);
3227
3228 if (res < 0) {
3229 SET_ERR("RequestThread: Unable to insert triggers "
3230 "(capture request %d, HAL device: %s (%d)",
3231 halRequest->frame_number, strerror(-res), res);
3232 return INVALID_OPERATION;
3233 }
3234 int triggerCount = res;
3235 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3236 mPrevTriggers = triggerCount;
3237
3238 // If the request is the same as last, or we had triggers last time
3239 if (mPrevRequest != captureRequest || triggersMixedIn) {
3240 /**
3241 * HAL workaround:
3242 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3243 */
3244 res = addDummyTriggerIds(captureRequest);
3245 if (res != OK) {
3246 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3247 "(capture request %d, HAL device: %s (%d)",
3248 halRequest->frame_number, strerror(-res), res);
3249 return INVALID_OPERATION;
3250 }
3251
3252 /**
3253 * The request should be presorted so accesses in HAL
3254 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3255 */
3256 captureRequest->mSettings.sort();
3257 halRequest->settings = captureRequest->mSettings.getAndLock();
3258 mPrevRequest = captureRequest;
3259 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3260
3261 IF_ALOGV() {
3262 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3263 find_camera_metadata_ro_entry(
3264 halRequest->settings,
3265 ANDROID_CONTROL_AF_TRIGGER,
3266 &e
3267 );
3268 if (e.count > 0) {
3269 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3270 __FUNCTION__,
3271 halRequest->frame_number,
3272 e.data.u8[0]);
3273 }
3274 }
3275 } else {
3276 // leave request.settings NULL to indicate 'reuse latest given'
3277 ALOGVV("%s: Request settings are REUSED",
3278 __FUNCTION__);
3279 }
3280
3281 uint32_t totalNumBuffers = 0;
3282
3283 // Fill in buffers
3284 if (captureRequest->mInputStream != NULL) {
3285 halRequest->input_buffer = &captureRequest->mInputBuffer;
3286 totalNumBuffers += 1;
3287 } else {
3288 halRequest->input_buffer = NULL;
3289 }
3290
3291 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3292 captureRequest->mOutputStreams.size());
3293 halRequest->output_buffers = outputBuffers->array();
3294 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003295 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(i);
3296
3297 // Prepare video buffers for high speed recording on the first video request.
3298 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3299 // Only try to prepare video stream on the first video request.
3300 mPrepareVideoStream = false;
3301
3302 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
3303 while (res == NOT_ENOUGH_DATA) {
3304 res = outputStream->prepareNextBuffer();
3305 }
3306 if (res != OK) {
3307 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3308 __FUNCTION__, strerror(-res), res);
3309 outputStream->cancelPrepare();
3310 }
3311 }
3312
3313 res = outputStream->getBuffer(&outputBuffers->editItemAt(i));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003314 if (res != OK) {
3315 // Can't get output buffer from gralloc queue - this could be due to
3316 // abandoned queue or other consumer misbehavior, so not a fatal
3317 // error
3318 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3319 " %s (%d)", strerror(-res), res);
3320
3321 return TIMED_OUT;
3322 }
3323 halRequest->num_output_buffers++;
3324 }
3325 totalNumBuffers += halRequest->num_output_buffers;
3326
3327 // Log request in the in-flight queue
3328 sp<Camera3Device> parent = mParent.promote();
3329 if (parent == NULL) {
3330 // Should not happen, and nowhere to send errors to, so just log it
3331 CLOGE("RequestThread: Parent is gone");
3332 return INVALID_OPERATION;
3333 }
3334 res = parent->registerInFlight(halRequest->frame_number,
3335 totalNumBuffers, captureRequest->mResultExtras,
3336 /*hasInput*/halRequest->input_buffer != NULL,
3337 captureRequest->mAeTriggerCancelOverride);
3338 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3339 ", burstId = %" PRId32 ".",
3340 __FUNCTION__,
3341 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3342 captureRequest->mResultExtras.burstId);
3343 if (res != OK) {
3344 SET_ERR("RequestThread: Unable to register new in-flight request:"
3345 " %s (%d)", strerror(-res), res);
3346 return INVALID_OPERATION;
3347 }
3348 }
3349
3350 return OK;
3351}
3352
Igor Murashkin1e479c02013-09-06 16:55:14 -07003353CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3354 Mutex::Autolock al(mLatestRequestMutex);
3355
3356 ALOGV("RequestThread::%s", __FUNCTION__);
3357
3358 return mLatestRequest;
3359}
3360
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003361bool Camera3Device::RequestThread::isStreamPending(
3362 sp<Camera3StreamInterface>& stream) {
3363 Mutex::Autolock l(mRequestLock);
3364
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003365 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003366 if (!nextRequest.submitted) {
3367 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3368 if (stream == s) return true;
3369 }
3370 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003371 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003372 }
3373
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003374 for (const auto& request : mRequestQueue) {
3375 for (const auto& s : request->mOutputStreams) {
3376 if (stream == s) return true;
3377 }
3378 if (stream == request->mInputStream) return true;
3379 }
3380
3381 for (const auto& request : mRepeatingRequests) {
3382 for (const auto& s : request->mOutputStreams) {
3383 if (stream == s) return true;
3384 }
3385 if (stream == request->mInputStream) return true;
3386 }
3387
3388 return false;
3389}
Jianing Weicb0652e2014-03-12 18:29:36 -07003390
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003391void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3392 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003393 return;
3394 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003395
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003396 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003397 // Skip the ones that have been submitted successfully.
3398 if (nextRequest.submitted) {
3399 continue;
3400 }
3401
3402 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3403 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3404 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3405
3406 if (halRequest->settings != NULL) {
3407 captureRequest->mSettings.unlock(halRequest->settings);
3408 }
3409
3410 if (captureRequest->mInputStream != NULL) {
3411 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3412 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3413 }
3414
3415 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3416 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3417 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3418 }
3419
3420 if (sendRequestError) {
3421 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003422 sp<NotificationListener> listener = mListener.promote();
3423 if (listener != NULL) {
3424 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003425 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003426 captureRequest->mResultExtras);
3427 }
3428 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003429 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003430
3431 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003432 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003433}
3434
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003435void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003436 // Optimized a bit for the simple steady-state case (single repeating
3437 // request), to avoid putting that request in the queue temporarily.
3438 Mutex::Autolock l(mRequestLock);
3439
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003440 assert(mNextRequests.empty());
3441
3442 NextRequest nextRequest;
3443 nextRequest.captureRequest = waitForNextRequestLocked();
3444 if (nextRequest.captureRequest == nullptr) {
3445 return;
3446 }
3447
3448 nextRequest.halRequest = camera3_capture_request_t();
3449 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003450 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003451
3452 // Wait for additional requests
3453 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3454
3455 for (size_t i = 1; i < batchSize; i++) {
3456 NextRequest additionalRequest;
3457 additionalRequest.captureRequest = waitForNextRequestLocked();
3458 if (additionalRequest.captureRequest == nullptr) {
3459 break;
3460 }
3461
3462 additionalRequest.halRequest = camera3_capture_request_t();
3463 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003464 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003465 }
3466
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003467 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003468 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003469 mNextRequests.size(), batchSize);
3470 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003471 }
3472
3473 return;
3474}
3475
3476sp<Camera3Device::CaptureRequest>
3477 Camera3Device::RequestThread::waitForNextRequestLocked() {
3478 status_t res;
3479 sp<CaptureRequest> nextRequest;
3480
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003481 while (mRequestQueue.empty()) {
3482 if (!mRepeatingRequests.empty()) {
3483 // Always atomically enqueue all requests in a repeating request
3484 // list. Guarantees a complete in-sequence set of captures to
3485 // application.
3486 const RequestList &requests = mRepeatingRequests;
3487 RequestList::const_iterator firstRequest =
3488 requests.begin();
3489 nextRequest = *firstRequest;
3490 mRequestQueue.insert(mRequestQueue.end(),
3491 ++firstRequest,
3492 requests.end());
3493 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003494
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003495 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003496
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003497 break;
3498 }
3499
3500 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3501
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003502 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3503 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003504 Mutex::Autolock pl(mPauseLock);
3505 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003506 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003507 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003508 // Let the tracker know
3509 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3510 if (statusTracker != 0) {
3511 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3512 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003513 }
3514 // Stop waiting for now and let thread management happen
3515 return NULL;
3516 }
3517 }
3518
3519 if (nextRequest == NULL) {
3520 // Don't have a repeating request already in hand, so queue
3521 // must have an entry now.
3522 RequestList::iterator firstRequest =
3523 mRequestQueue.begin();
3524 nextRequest = *firstRequest;
3525 mRequestQueue.erase(firstRequest);
3526 }
3527
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003528 // In case we've been unpaused by setPaused clearing mDoPause, need to
3529 // update internal pause state (capture/setRepeatingRequest unpause
3530 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003531 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003532 if (mPaused) {
3533 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3534 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3535 if (statusTracker != 0) {
3536 statusTracker->markComponentActive(mStatusId);
3537 }
3538 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003539 mPaused = false;
3540
3541 // Check if we've reconfigured since last time, and reset the preview
3542 // request if so. Can't use 'NULL request == repeat' across configure calls.
3543 if (mReconfigured) {
3544 mPrevRequest.clear();
3545 mReconfigured = false;
3546 }
3547
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003548 if (nextRequest != NULL) {
3549 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003550 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3551 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003552
3553 // Since RequestThread::clear() removes buffers from the input stream,
3554 // get the right buffer here before unlocking mRequestLock
3555 if (nextRequest->mInputStream != NULL) {
3556 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3557 if (res != OK) {
3558 // Can't get input buffer from gralloc queue - this could be due to
3559 // disconnected queue or other producer misbehavior, so not a fatal
3560 // error
3561 ALOGE("%s: Can't get input buffer, skipping request:"
3562 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003563
3564 sp<NotificationListener> listener = mListener.promote();
3565 if (listener != NULL) {
3566 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003567 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003568 nextRequest->mResultExtras);
3569 }
3570 return NULL;
3571 }
3572 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003573 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003574
3575 handleAePrecaptureCancelRequest(nextRequest);
3576
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003577 return nextRequest;
3578}
3579
3580bool Camera3Device::RequestThread::waitIfPaused() {
3581 status_t res;
3582 Mutex::Autolock l(mPauseLock);
3583 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003584 if (mPaused == false) {
3585 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003586 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3587 // Let the tracker know
3588 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3589 if (statusTracker != 0) {
3590 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3591 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003592 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003593
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003594 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003595 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003596 return true;
3597 }
3598 }
3599 // We don't set mPaused to false here, because waitForNextRequest needs
3600 // to further manage the paused state in case of starvation.
3601 return false;
3602}
3603
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003604void Camera3Device::RequestThread::unpauseForNewRequests() {
3605 // With work to do, mark thread as unpaused.
3606 // If paused by request (setPaused), don't resume, to avoid
3607 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003608 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003609 Mutex::Autolock p(mPauseLock);
3610 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003611 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3612 if (mPaused) {
3613 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3614 if (statusTracker != 0) {
3615 statusTracker->markComponentActive(mStatusId);
3616 }
3617 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003618 mPaused = false;
3619 }
3620}
3621
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003622void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3623 sp<Camera3Device> parent = mParent.promote();
3624 if (parent != NULL) {
3625 va_list args;
3626 va_start(args, fmt);
3627
3628 parent->setErrorStateV(fmt, args);
3629
3630 va_end(args);
3631 }
3632}
3633
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003634status_t Camera3Device::RequestThread::insertTriggers(
3635 const sp<CaptureRequest> &request) {
3636
3637 Mutex::Autolock al(mTriggerMutex);
3638
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003639 sp<Camera3Device> parent = mParent.promote();
3640 if (parent == NULL) {
3641 CLOGE("RequestThread: Parent is gone");
3642 return DEAD_OBJECT;
3643 }
3644
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003645 CameraMetadata &metadata = request->mSettings;
3646 size_t count = mTriggerMap.size();
3647
3648 for (size_t i = 0; i < count; ++i) {
3649 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003650 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003651
3652 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3653 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3654 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003655 if (isAeTrigger) {
3656 request->mResultExtras.precaptureTriggerId = triggerId;
3657 mCurrentPreCaptureTriggerId = triggerId;
3658 } else {
3659 request->mResultExtras.afTriggerId = triggerId;
3660 mCurrentAfTriggerId = triggerId;
3661 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003662 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3663 continue; // Trigger ID tag is deprecated since device HAL 3.2
3664 }
3665 }
3666
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003667 camera_metadata_entry entry = metadata.find(tag);
3668
3669 if (entry.count > 0) {
3670 /**
3671 * Already has an entry for this trigger in the request.
3672 * Rewrite it with our requested trigger value.
3673 */
3674 RequestTrigger oldTrigger = trigger;
3675
3676 oldTrigger.entryValue = entry.data.u8[0];
3677
3678 mTriggerReplacedMap.add(tag, oldTrigger);
3679 } else {
3680 /**
3681 * More typical, no trigger entry, so we just add it
3682 */
3683 mTriggerRemovedMap.add(tag, trigger);
3684 }
3685
3686 status_t res;
3687
3688 switch (trigger.getTagType()) {
3689 case TYPE_BYTE: {
3690 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3691 res = metadata.update(tag,
3692 &entryValue,
3693 /*count*/1);
3694 break;
3695 }
3696 case TYPE_INT32:
3697 res = metadata.update(tag,
3698 &trigger.entryValue,
3699 /*count*/1);
3700 break;
3701 default:
3702 ALOGE("%s: Type not supported: 0x%x",
3703 __FUNCTION__,
3704 trigger.getTagType());
3705 return INVALID_OPERATION;
3706 }
3707
3708 if (res != OK) {
3709 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3710 ", value %d", __FUNCTION__, trigger.getTagName(),
3711 trigger.entryValue);
3712 return res;
3713 }
3714
3715 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3716 trigger.getTagName(),
3717 trigger.entryValue);
3718 }
3719
3720 mTriggerMap.clear();
3721
3722 return count;
3723}
3724
3725status_t Camera3Device::RequestThread::removeTriggers(
3726 const sp<CaptureRequest> &request) {
3727 Mutex::Autolock al(mTriggerMutex);
3728
3729 CameraMetadata &metadata = request->mSettings;
3730
3731 /**
3732 * Replace all old entries with their old values.
3733 */
3734 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3735 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3736
3737 status_t res;
3738
3739 uint32_t tag = trigger.metadataTag;
3740 switch (trigger.getTagType()) {
3741 case TYPE_BYTE: {
3742 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3743 res = metadata.update(tag,
3744 &entryValue,
3745 /*count*/1);
3746 break;
3747 }
3748 case TYPE_INT32:
3749 res = metadata.update(tag,
3750 &trigger.entryValue,
3751 /*count*/1);
3752 break;
3753 default:
3754 ALOGE("%s: Type not supported: 0x%x",
3755 __FUNCTION__,
3756 trigger.getTagType());
3757 return INVALID_OPERATION;
3758 }
3759
3760 if (res != OK) {
3761 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3762 ", trigger value %d", __FUNCTION__,
3763 trigger.getTagName(), trigger.entryValue);
3764 return res;
3765 }
3766 }
3767 mTriggerReplacedMap.clear();
3768
3769 /**
3770 * Remove all new entries.
3771 */
3772 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3773 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3774 status_t res = metadata.erase(trigger.metadataTag);
3775
3776 if (res != OK) {
3777 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3778 ", trigger value %d", __FUNCTION__,
3779 trigger.getTagName(), trigger.entryValue);
3780 return res;
3781 }
3782 }
3783 mTriggerRemovedMap.clear();
3784
3785 return OK;
3786}
3787
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003788status_t Camera3Device::RequestThread::addDummyTriggerIds(
3789 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003790 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003791 static const int32_t dummyTriggerId = 1;
3792 status_t res;
3793
3794 CameraMetadata &metadata = request->mSettings;
3795
3796 // If AF trigger is active, insert a dummy AF trigger ID if none already
3797 // exists
3798 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3799 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3800 if (afTrigger.count > 0 &&
3801 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3802 afId.count == 0) {
3803 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3804 if (res != OK) return res;
3805 }
3806
3807 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3808 // if none already exists
3809 camera_metadata_entry pcTrigger =
3810 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3811 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3812 if (pcTrigger.count > 0 &&
3813 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3814 pcId.count == 0) {
3815 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3816 &dummyTriggerId, 1);
3817 if (res != OK) return res;
3818 }
3819
3820 return OK;
3821}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003822
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003823/**
3824 * PreparerThread inner class methods
3825 */
3826
3827Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003828 Thread(/*canCallJava*/false), mListener(nullptr),
3829 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003830}
3831
3832Camera3Device::PreparerThread::~PreparerThread() {
3833 Thread::requestExitAndWait();
3834 if (mCurrentStream != nullptr) {
3835 mCurrentStream->cancelPrepare();
3836 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3837 mCurrentStream.clear();
3838 }
3839 clear();
3840}
3841
Ruben Brunkc78ac262015-08-13 17:58:46 -07003842status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003843 status_t res;
3844
3845 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003846 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003847
Ruben Brunkc78ac262015-08-13 17:58:46 -07003848 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003849 if (res == OK) {
3850 // No preparation needed, fire listener right off
3851 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003852 if (listener != NULL) {
3853 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003854 }
3855 return OK;
3856 } else if (res != NOT_ENOUGH_DATA) {
3857 return res;
3858 }
3859
3860 // Need to prepare, start up thread if necessary
3861 if (!mActive) {
3862 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3863 // isn't running
3864 Thread::requestExitAndWait();
3865 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3866 if (res != OK) {
3867 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003868 if (listener != NULL) {
3869 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003870 }
3871 return res;
3872 }
3873 mCancelNow = false;
3874 mActive = true;
3875 ALOGV("%s: Preparer stream started", __FUNCTION__);
3876 }
3877
3878 // queue up the work
3879 mPendingStreams.push_back(stream);
3880 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3881
3882 return OK;
3883}
3884
3885status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003886 Mutex::Autolock l(mLock);
3887
3888 for (const auto& stream : mPendingStreams) {
3889 stream->cancelPrepare();
3890 }
3891 mPendingStreams.clear();
3892 mCancelNow = true;
3893
3894 return OK;
3895}
3896
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003897void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003898 Mutex::Autolock l(mLock);
3899 mListener = listener;
3900}
3901
3902bool Camera3Device::PreparerThread::threadLoop() {
3903 status_t res;
3904 {
3905 Mutex::Autolock l(mLock);
3906 if (mCurrentStream == nullptr) {
3907 // End thread if done with work
3908 if (mPendingStreams.empty()) {
3909 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3910 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3911 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3912 mActive = false;
3913 return false;
3914 }
3915
3916 // Get next stream to prepare
3917 auto it = mPendingStreams.begin();
3918 mCurrentStream = *it;
3919 mPendingStreams.erase(it);
3920 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3921 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3922 } else if (mCancelNow) {
3923 mCurrentStream->cancelPrepare();
3924 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3925 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3926 mCurrentStream.clear();
3927 mCancelNow = false;
3928 return true;
3929 }
3930 }
3931
3932 res = mCurrentStream->prepareNextBuffer();
3933 if (res == NOT_ENOUGH_DATA) return true;
3934 if (res != OK) {
3935 // Something bad happened; try to recover by cancelling prepare and
3936 // signalling listener anyway
3937 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3938 mCurrentStream->getId(), res, strerror(-res));
3939 mCurrentStream->cancelPrepare();
3940 }
3941
3942 // This stream has finished, notify listener
3943 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003944 sp<NotificationListener> listener = mListener.promote();
3945 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003946 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3947 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003948 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003949 }
3950
3951 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3952 mCurrentStream.clear();
3953
3954 return true;
3955}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003956
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003957/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003958 * Static callback forwarding methods from HAL to instance
3959 */
3960
3961void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3962 const camera3_capture_result *result) {
3963 Camera3Device *d =
3964 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003965
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003966 d->processCaptureResult(result);
3967}
3968
3969void Camera3Device::sNotify(const camera3_callback_ops *cb,
3970 const camera3_notify_msg *msg) {
3971 Camera3Device *d =
3972 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3973 d->notify(msg);
3974}
3975
3976}; // namespace android