blob: 2f3251f3157830f36c4825b23dfdfb8251aa12a7 [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
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700173 /** Register in-flight map to the status tracker */
174 mInFlightStatusId = mStatusTracker->addComponent();
175
Zhijun He125684a2015-12-26 15:07:30 -0800176 /** Create buffer manager */
177 mBufferManager = new Camera3BufferManager();
178
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700179 bool aeLockAvailable = false;
180 camera_metadata_ro_entry aeLockAvailableEntry;
181 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
182 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
183 if (res == OK && aeLockAvailableEntry.count > 0) {
184 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
185 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
186 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700188 /** Start up request queue thread */
189 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800190 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800191 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700192 SET_ERR_L("Unable to start request queue thread: %s (%d)",
193 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800194 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800195 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800196 return res;
197 }
198
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700199 mPreparerThread = new PreparerThread();
200
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800201 /** Everything is good to go */
202
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700203 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800204 mDeviceInfo = info.static_camera_characteristics;
205 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700206
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700207 // Determine whether we need to derive sensitivity boost values for older devices.
208 // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
209 // be listed (as the default value 100)
210 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_4 &&
211 mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
212 mDerivePostRawSensKey = true;
213 }
214
Ruben Brunk183f0562015-08-12 12:55:02 -0700215 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800216 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700217 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700218 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700219 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800220
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800221 // Measure the clock domain offset between camera and video/hw_composer
222 camera_metadata_entry timestampSource =
223 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
224 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
225 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
226 mTimestampOffset = getMonoToBoottimeOffset();
227 }
228
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700229 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700230 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
231 camera_metadata_entry partialResultsCount =
232 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
233 if (partialResultsCount.count > 0) {
234 mNumPartialResults = partialResultsCount.data.i32[0];
235 mUsePartialResult = (mNumPartialResults > 1);
236 }
237 } else {
238 camera_metadata_entry partialResultsQuirk =
239 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
240 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
241 mUsePartialResult = true;
242 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700243 }
244
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700245 camera_metadata_entry configs =
246 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
247 for (uint32_t i = 0; i < configs.count; i += 4) {
248 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
249 configs.data.i32[i + 3] ==
250 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
251 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
252 configs.data.i32[i + 2]));
253 }
254 }
255
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800256 return OK;
257}
258
259status_t Camera3Device::disconnect() {
260 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700261 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800262
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700263 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800264
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700265 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800266
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700267 {
268 Mutex::Autolock l(mLock);
269 if (mStatus == STATUS_UNINITIALIZED) return res;
270
271 if (mStatus == STATUS_ACTIVE ||
272 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
273 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700274 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700275 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700276 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277 } else {
278 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
279 if (res != OK) {
280 SET_ERR_L("Timeout waiting for HAL to drain");
281 // Continue to close device even in case of error
282 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700283 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800284 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800285
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700286 if (mStatus == STATUS_ERROR) {
287 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700288 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700289
290 if (mStatusTracker != NULL) {
291 mStatusTracker->requestExit();
292 }
293
294 if (mRequestThread != NULL) {
295 mRequestThread->requestExit();
296 }
297
298 mOutputStreams.clear();
299 mInputStream.clear();
300 }
301
302 // Joining done without holding mLock, otherwise deadlocks may ensue
303 // as the threads try to access parent state
304 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
305 // HAL may be in a bad state, so waiting for request thread
306 // (which may be stuck in the HAL processCaptureRequest call)
307 // could be dangerous.
308 mRequestThread->join();
309 }
310
311 if (mStatusTracker != NULL) {
312 mStatusTracker->join();
313 }
314
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700315 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700316 {
317 Mutex::Autolock l(mLock);
318
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800319 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700320 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800321 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800322
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700323 hal3Device = mHal3Device;
324 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800325
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700326 // Call close without internal mutex held, as the HAL close may need to
327 // wait on assorted callbacks,etc, to complete before it can return.
328 if (hal3Device != NULL) {
329 ATRACE_BEGIN("camera3->close");
330 hal3Device->common.close(&hal3Device->common);
331 ATRACE_END();
332 }
333
334 {
335 Mutex::Autolock l(mLock);
336 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700337 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700338 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800339
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700340 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700341 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800342}
343
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700344// For dumping/debugging only -
345// try to acquire a lock a few times, eventually give up to proceed with
346// debug/dump operations
347bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
348 bool gotLock = false;
349 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
350 if (lock.tryLock() == NO_ERROR) {
351 gotLock = true;
352 break;
353 } else {
354 usleep(kDumpSleepDuration);
355 }
356 }
357 return gotLock;
358}
359
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700360Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
361 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
362 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
363 const int STREAM_CONFIGURATION_SIZE = 4;
364 const int STREAM_FORMAT_OFFSET = 0;
365 const int STREAM_WIDTH_OFFSET = 1;
366 const int STREAM_HEIGHT_OFFSET = 2;
367 const int STREAM_IS_INPUT_OFFSET = 3;
368 camera_metadata_ro_entry_t availableStreamConfigs =
369 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
370 if (availableStreamConfigs.count == 0 ||
371 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
372 return Size(0, 0);
373 }
374
375 // Get max jpeg size (area-wise).
376 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
377 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
378 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
379 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
380 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
381 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
382 && format == HAL_PIXEL_FORMAT_BLOB &&
383 (width * height > maxJpegWidth * maxJpegHeight)) {
384 maxJpegWidth = width;
385 maxJpegHeight = height;
386 }
387 }
388 } else {
389 camera_metadata_ro_entry availableJpegSizes =
390 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
391 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
392 return Size(0, 0);
393 }
394
395 // Get max jpeg size (area-wise).
396 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
397 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
398 > (maxJpegWidth * maxJpegHeight)) {
399 maxJpegWidth = availableJpegSizes.data.i32[i];
400 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
401 }
402 }
403 }
404 return Size(maxJpegWidth, maxJpegHeight);
405}
406
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800407nsecs_t Camera3Device::getMonoToBoottimeOffset() {
408 // try three times to get the clock offset, choose the one
409 // with the minimum gap in measurements.
410 const int tries = 3;
411 nsecs_t bestGap, measured;
412 for (int i = 0; i < tries; ++i) {
413 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
414 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
415 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
416 const nsecs_t gap = tmono2 - tmono;
417 if (i == 0 || gap < bestGap) {
418 bestGap = gap;
419 measured = tbase - ((tmono + tmono2) >> 1);
420 }
421 }
422 return measured;
423}
424
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -0700425/**
426 * Map Android N dataspace definitions back to Android M definitions, for
427 * use with HALv3.3 or older.
428 *
429 * Only map where correspondences exist, and otherwise preserve the value.
430 */
431android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) {
432 switch (dataSpace) {
433 case HAL_DATASPACE_V0_SRGB_LINEAR:
434 return HAL_DATASPACE_SRGB_LINEAR;
435 case HAL_DATASPACE_V0_SRGB:
436 return HAL_DATASPACE_SRGB;
437 case HAL_DATASPACE_V0_JFIF:
438 return HAL_DATASPACE_JFIF;
439 case HAL_DATASPACE_V0_BT601_625:
440 return HAL_DATASPACE_BT601_625;
441 case HAL_DATASPACE_V0_BT601_525:
442 return HAL_DATASPACE_BT601_525;
443 case HAL_DATASPACE_V0_BT709:
444 return HAL_DATASPACE_BT709;
445 default:
446 return dataSpace;
447 }
448}
449
Zhijun Hef7da0962014-04-24 13:27:56 -0700450ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700451 // Get max jpeg size (area-wise).
452 Size maxJpegResolution = getMaxJpegResolution();
453 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700454 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700455 __FUNCTION__, mId);
456 return BAD_VALUE;
457 }
458
Zhijun Hef7da0962014-04-24 13:27:56 -0700459 // Get max jpeg buffer size
460 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700461 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
462 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700463 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
464 return BAD_VALUE;
465 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700466 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800467 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700468
469 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700470 float scaleFactor = ((float) (width * height)) /
471 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800472 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
473 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700474 if (jpegBufferSize > maxJpegBufferSize) {
475 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700476 }
477
478 return jpegBufferSize;
479}
480
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700481ssize_t Camera3Device::getPointCloudBufferSize() const {
482 const int FLOATS_PER_POINT=4;
483 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
484 if (maxPointCount.count == 0) {
485 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
486 __FUNCTION__, mId);
487 return BAD_VALUE;
488 }
489 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
490 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
491 return maxBytesForPointCloud;
492}
493
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800494ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800495 const int PER_CONFIGURATION_SIZE = 3;
496 const int WIDTH_OFFSET = 0;
497 const int HEIGHT_OFFSET = 1;
498 const int SIZE_OFFSET = 2;
499 camera_metadata_ro_entry rawOpaqueSizes =
500 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800501 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800502 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800503 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800504 __FUNCTION__, mId, count);
505 return BAD_VALUE;
506 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700507
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800508 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
509 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
510 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
511 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
512 }
513 }
514
515 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
516 __FUNCTION__, mId, width, height);
517 return BAD_VALUE;
518}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700519
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800520status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
521 ATRACE_CALL();
522 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700523
524 // Try to lock, but continue in case of failure (to avoid blocking in
525 // deadlocks)
526 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
527 bool gotLock = tryLockSpinRightRound(mLock);
528
529 ALOGW_IF(!gotInterfaceLock,
530 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
531 mId, __FUNCTION__);
532 ALOGW_IF(!gotLock,
533 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
534 mId, __FUNCTION__);
535
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800536 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700537
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800538 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700539 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800540 int n = args.size();
541 for (int i = 0; i < n; i++) {
542 if (args[i] == templatesOption) {
543 dumpTemplates = true;
544 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700545 if (args[i] == monitorOption) {
546 if (i + 1 < n) {
547 String8 monitorTags = String8(args[i + 1]);
548 if (monitorTags == "off") {
549 mTagMonitor.disableMonitoring();
550 } else {
551 mTagMonitor.parseTagsToMonitor(monitorTags);
552 }
553 } else {
554 mTagMonitor.disableMonitoring();
555 }
556 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800557 }
558
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800559 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800560
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800561 const char *status =
562 mStatus == STATUS_ERROR ? "ERROR" :
563 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700564 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
565 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800566 mStatus == STATUS_ACTIVE ? "ACTIVE" :
567 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700568
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800569 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700570 if (mStatus == STATUS_ERROR) {
571 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
572 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800573 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700574 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700575 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800576
577 if (mInputStream != NULL) {
578 write(fd, lines.string(), lines.size());
579 mInputStream->dump(fd, args);
580 } else {
581 lines.appendFormat(" No input stream.\n");
582 write(fd, lines.string(), lines.size());
583 }
584 for (size_t i = 0; i < mOutputStreams.size(); i++) {
585 mOutputStreams[i]->dump(fd,args);
586 }
587
Zhijun He431503c2016-03-07 17:30:16 -0800588 if (mBufferManager != NULL) {
589 lines = String8(" Camera3 Buffer Manager:\n");
590 write(fd, lines.string(), lines.size());
591 mBufferManager->dump(fd, args);
592 }
Zhijun He125684a2015-12-26 15:07:30 -0800593
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700594 lines = String8(" In-flight requests:\n");
595 if (mInFlightMap.size() == 0) {
596 lines.append(" None\n");
597 } else {
598 for (size_t i = 0; i < mInFlightMap.size(); i++) {
599 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700600 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700601 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800602 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700603 r.numBuffersLeft);
604 }
605 }
606 write(fd, lines.string(), lines.size());
607
Igor Murashkin1e479c02013-09-06 16:55:14 -0700608 {
609 lines = String8(" Last request sent:\n");
610 write(fd, lines.string(), lines.size());
611
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700612 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700613 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
614 }
615
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800616 if (dumpTemplates) {
617 const char *templateNames[] = {
618 "TEMPLATE_PREVIEW",
619 "TEMPLATE_STILL_CAPTURE",
620 "TEMPLATE_VIDEO_RECORD",
621 "TEMPLATE_VIDEO_SNAPSHOT",
622 "TEMPLATE_ZERO_SHUTTER_LAG",
623 "TEMPLATE_MANUAL"
624 };
625
626 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
627 const camera_metadata_t *templateRequest;
628 templateRequest =
629 mHal3Device->ops->construct_default_request_settings(
630 mHal3Device, i);
631 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
632 if (templateRequest == NULL) {
633 lines.append(" Not supported\n");
634 write(fd, lines.string(), lines.size());
635 } else {
636 write(fd, lines.string(), lines.size());
637 dump_indented_camera_metadata(templateRequest,
638 fd, /*verbosity*/2, /*indentation*/8);
639 }
640 }
641 }
642
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700643 mTagMonitor.dumpMonitoredMetadata(fd);
644
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800645 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700646 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800647 write(fd, lines.string(), lines.size());
648 mHal3Device->ops->dump(mHal3Device, fd);
649 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800650
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700651 if (gotLock) mLock.unlock();
652 if (gotInterfaceLock) mInterfaceLock.unlock();
653
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800654 return OK;
655}
656
657const CameraMetadata& Camera3Device::info() const {
658 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800659 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
660 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700661 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800662 mStatus == STATUS_ERROR ?
663 "when in error state" : "before init");
664 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800665 return mDeviceInfo;
666}
667
Jianing Wei90e59c92014-03-12 18:29:36 -0700668status_t Camera3Device::checkStatusOkToCaptureLocked() {
669 switch (mStatus) {
670 case STATUS_ERROR:
671 CLOGE("Device has encountered a serious error");
672 return INVALID_OPERATION;
673 case STATUS_UNINITIALIZED:
674 CLOGE("Device not initialized");
675 return INVALID_OPERATION;
676 case STATUS_UNCONFIGURED:
677 case STATUS_CONFIGURED:
678 case STATUS_ACTIVE:
679 // OK
680 break;
681 default:
682 SET_ERR_L("Unexpected status: %d", mStatus);
683 return INVALID_OPERATION;
684 }
685 return OK;
686}
687
688status_t Camera3Device::convertMetadataListToRequestListLocked(
Shuzhen Wang9d066012016-09-30 11:30:20 -0700689 const List<const CameraMetadata> &metadataList, bool repeating,
690 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700691 if (requestList == NULL) {
692 CLOGE("requestList cannot be NULL.");
693 return BAD_VALUE;
694 }
695
Jianing Weicb0652e2014-03-12 18:29:36 -0700696 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700697 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
698 it != metadataList.end(); ++it) {
699 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
700 if (newRequest == 0) {
701 CLOGE("Can't create capture request");
702 return BAD_VALUE;
703 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700704
Shuzhen Wang9d066012016-09-30 11:30:20 -0700705 newRequest->mRepeating = repeating;
706
Jianing Weicb0652e2014-03-12 18:29:36 -0700707 // Setup burst Id and request Id
708 newRequest->mResultExtras.burstId = burstId++;
709 if (it->exists(ANDROID_REQUEST_ID)) {
710 if (it->find(ANDROID_REQUEST_ID).count == 0) {
711 CLOGE("RequestID entry exists; but must not be empty in metadata");
712 return BAD_VALUE;
713 }
714 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
715 } else {
716 CLOGE("RequestID does not exist in metadata");
717 return BAD_VALUE;
718 }
719
Jianing Wei90e59c92014-03-12 18:29:36 -0700720 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700721
722 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700723 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700724
725 // Setup batch size if this is a high speed video recording request.
726 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
727 auto firstRequest = requestList->begin();
728 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
729 if (outputStream->isVideoStream()) {
730 (*firstRequest)->mBatchSize = requestList->size();
731 break;
732 }
733 }
734 }
735
Jianing Wei90e59c92014-03-12 18:29:36 -0700736 return OK;
737}
738
Jianing Weicb0652e2014-03-12 18:29:36 -0700739status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800740 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800741
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700742 List<const CameraMetadata> requests;
743 requests.push_back(request);
744 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800745}
746
Jianing Wei90e59c92014-03-12 18:29:36 -0700747status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700748 const List<const CameraMetadata> &requests, bool repeating,
749 /*out*/
750 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700751 ATRACE_CALL();
752 Mutex::Autolock il(mInterfaceLock);
753 Mutex::Autolock l(mLock);
754
755 status_t res = checkStatusOkToCaptureLocked();
756 if (res != OK) {
757 // error logged by previous call
758 return res;
759 }
760
761 RequestList requestList;
762
Shuzhen Wang9d066012016-09-30 11:30:20 -0700763 res = convertMetadataListToRequestListLocked(requests, repeating,
764 /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700765 if (res != OK) {
766 // error logged by previous call
767 return res;
768 }
769
770 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700771 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700772 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700773 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700774 }
775
776 if (res == OK) {
777 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
778 if (res != OK) {
779 SET_ERR_L("Can't transition to active in %f seconds!",
780 kActiveTimeout/1e9);
781 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700782 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
783 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700784 } else {
785 CLOGE("Cannot queue request. Impossible.");
786 return BAD_VALUE;
787 }
788
789 return res;
790}
791
Jianing Weicb0652e2014-03-12 18:29:36 -0700792status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
793 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700794 ATRACE_CALL();
795
Jianing Weicb0652e2014-03-12 18:29:36 -0700796 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700797}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800798
Jianing Weicb0652e2014-03-12 18:29:36 -0700799status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
800 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800801 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800802
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700803 List<const CameraMetadata> requests;
804 requests.push_back(request);
805 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800806}
807
Jianing Weicb0652e2014-03-12 18:29:36 -0700808status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
809 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700810 ATRACE_CALL();
811
Jianing Weicb0652e2014-03-12 18:29:36 -0700812 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700813}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800814
815sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
816 const CameraMetadata &request) {
817 status_t res;
818
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700819 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800820 res = configureStreamsLocked();
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700821 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800822 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700823 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800824 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700825 } else if (mStatus == STATUS_UNCONFIGURED) {
826 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700827 CLOGE("No streams configured");
828 return NULL;
829 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800830 }
831
832 sp<CaptureRequest> newRequest = createCaptureRequest(request);
833 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800834}
835
Jianing Weicb0652e2014-03-12 18:29:36 -0700836status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800837 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700838 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800839 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800840
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800841 switch (mStatus) {
842 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700843 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800844 return INVALID_OPERATION;
845 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700846 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800847 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700848 case STATUS_UNCONFIGURED:
849 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800850 case STATUS_ACTIVE:
851 // OK
852 break;
853 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700854 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800855 return INVALID_OPERATION;
856 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700857 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700858
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700859 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800860}
861
862status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
863 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700864 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800865
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700866 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800867}
868
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700869status_t Camera3Device::createInputStream(
870 uint32_t width, uint32_t height, int format, int *id) {
871 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700872 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700873 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700874 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
875 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700876
877 status_t res;
878 bool wasActive = false;
879
880 switch (mStatus) {
881 case STATUS_ERROR:
882 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
883 return INVALID_OPERATION;
884 case STATUS_UNINITIALIZED:
885 ALOGE("%s: Device not initialized", __FUNCTION__);
886 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700887 case STATUS_UNCONFIGURED:
888 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700889 // OK
890 break;
891 case STATUS_ACTIVE:
892 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700893 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700894 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700895 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700896 return res;
897 }
898 wasActive = true;
899 break;
900 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700901 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700902 return INVALID_OPERATION;
903 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700904 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700905
906 if (mInputStream != 0) {
907 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
908 return INVALID_OPERATION;
909 }
910
911 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
912 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700913 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700914
915 mInputStream = newStream;
916
917 *id = mNextStreamId++;
918
919 // Continue captures if active at start
920 if (wasActive) {
921 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
922 res = configureStreamsLocked();
923 if (res != OK) {
924 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
925 __FUNCTION__, mNextStreamId, strerror(-res), res);
926 return res;
927 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700928 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700929 }
930
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700931 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700932 return OK;
933}
934
Igor Murashkin2fba5842013-04-22 14:03:54 -0700935
936status_t Camera3Device::createZslStream(
937 uint32_t width, uint32_t height,
938 int depth,
939 /*out*/
940 int *id,
941 sp<Camera3ZslStream>* zslStream) {
942 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700943 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700944 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700945 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
946 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700947
948 status_t res;
949 bool wasActive = false;
950
951 switch (mStatus) {
952 case STATUS_ERROR:
953 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
954 return INVALID_OPERATION;
955 case STATUS_UNINITIALIZED:
956 ALOGE("%s: Device not initialized", __FUNCTION__);
957 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700958 case STATUS_UNCONFIGURED:
959 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700960 // OK
961 break;
962 case STATUS_ACTIVE:
963 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700964 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700965 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700966 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700967 return res;
968 }
969 wasActive = true;
970 break;
971 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700972 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700973 return INVALID_OPERATION;
974 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700975 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700976
977 if (mInputStream != 0) {
978 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
979 return INVALID_OPERATION;
980 }
981
982 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
983 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700984 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700985
986 res = mOutputStreams.add(mNextStreamId, newStream);
987 if (res < 0) {
988 ALOGE("%s: Can't add new stream to set: %s (%d)",
989 __FUNCTION__, strerror(-res), res);
990 return res;
991 }
992 mInputStream = newStream;
993
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530994 mNeedConfig = true;
995
Igor Murashkin2fba5842013-04-22 14:03:54 -0700996 *id = mNextStreamId++;
997 *zslStream = newStream;
998
999 // Continue captures if active at start
1000 if (wasActive) {
1001 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1002 res = configureStreamsLocked();
1003 if (res != OK) {
1004 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1005 __FUNCTION__, mNextStreamId, strerror(-res), res);
1006 return res;
1007 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001008 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -07001009 }
1010
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001011 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -07001012 return OK;
1013}
1014
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001015status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001016 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -07001017 camera3_stream_rotation_t rotation, int *id, int streamSetId, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001018 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001019 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001020 Mutex::Autolock l(mLock);
Zhijun He5d677d12016-05-29 16:52:39 -07001021 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
1022 " consumer usage 0x%x", mId, mNextStreamId, width, height, format, dataSpace, rotation,
1023 consumerUsage);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001024
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001025 status_t res;
1026 bool wasActive = false;
1027
1028 switch (mStatus) {
1029 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001030 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001031 return INVALID_OPERATION;
1032 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001033 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001034 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001035 case STATUS_UNCONFIGURED:
1036 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001037 // OK
1038 break;
1039 case STATUS_ACTIVE:
1040 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001041 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001042 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001043 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001044 return res;
1045 }
1046 wasActive = true;
1047 break;
1048 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001049 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001050 return INVALID_OPERATION;
1051 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001052 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053
1054 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001055 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001056 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001057 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001058 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1059 }
Zhijun He5d677d12016-05-29 16:52:39 -07001060
1061 // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
1062 // which requires a consumer surface to be available.
1063 if (consumer == nullptr && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1064 ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
1065 return BAD_VALUE;
1066 }
1067
1068 if (consumer == nullptr && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1069 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1070 return BAD_VALUE;
1071 }
1072
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001073 // Use legacy dataspace values for older HALs
1074 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1075 dataSpace = mapToLegacyDataspace(dataSpace);
1076 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001077 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001078 ssize_t blobBufferSize;
1079 if (dataSpace != HAL_DATASPACE_DEPTH) {
1080 blobBufferSize = getJpegBufferSize(width, height);
1081 if (blobBufferSize <= 0) {
1082 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1083 return BAD_VALUE;
1084 }
1085 } else {
1086 blobBufferSize = getPointCloudBufferSize();
1087 if (blobBufferSize <= 0) {
1088 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1089 return BAD_VALUE;
1090 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001091 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001092 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001093 width, height, blobBufferSize, format, dataSpace, rotation,
1094 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001095 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1096 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1097 if (rawOpaqueBufferSize <= 0) {
1098 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1099 return BAD_VALUE;
1100 }
1101 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001102 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1103 mTimestampOffset, streamSetId);
Zhijun He5d677d12016-05-29 16:52:39 -07001104 } else if (consumer == nullptr) {
1105 newStream = new Camera3OutputStream(mNextStreamId,
1106 width, height, format, consumerUsage, dataSpace, rotation,
1107 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001108 } else {
1109 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001110 width, height, format, dataSpace, rotation,
1111 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001112 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001113 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001114
Zhijun He125684a2015-12-26 15:07:30 -08001115 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001116 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1117 * requires buffers to be statically allocated for internal static buffer registration, while
1118 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1119 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001120 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001121 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001122 newStream->setBufferManager(mBufferManager);
1123 }
1124
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001125 res = mOutputStreams.add(mNextStreamId, newStream);
1126 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001127 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001128 return res;
1129 }
1130
1131 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001132 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001133
1134 // Continue captures if active at start
1135 if (wasActive) {
1136 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1137 res = configureStreamsLocked();
1138 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001139 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1140 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141 return res;
1142 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001143 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001144 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001145 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001146 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001147}
1148
1149status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1150 ATRACE_CALL();
1151 (void)outputId; (void)id;
1152
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001153 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001154 return INVALID_OPERATION;
1155}
1156
1157
1158status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001159 uint32_t *width, uint32_t *height,
1160 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001161 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001162 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001163 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001164
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001165 switch (mStatus) {
1166 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001167 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001168 return INVALID_OPERATION;
1169 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001170 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001171 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001172 case STATUS_UNCONFIGURED:
1173 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001174 case STATUS_ACTIVE:
1175 // OK
1176 break;
1177 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001178 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001179 return INVALID_OPERATION;
1180 }
1181
1182 ssize_t idx = mOutputStreams.indexOfKey(id);
1183 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001184 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001185 return idx;
1186 }
1187
1188 if (width) *width = mOutputStreams[idx]->getWidth();
1189 if (height) *height = mOutputStreams[idx]->getHeight();
1190 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001191 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001192 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001193}
1194
1195status_t Camera3Device::setStreamTransform(int id,
1196 int transform) {
1197 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001198 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001199 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001200
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001201 switch (mStatus) {
1202 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001203 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001204 return INVALID_OPERATION;
1205 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001206 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001207 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001208 case STATUS_UNCONFIGURED:
1209 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001210 case STATUS_ACTIVE:
1211 // OK
1212 break;
1213 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001214 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001215 return INVALID_OPERATION;
1216 }
1217
1218 ssize_t idx = mOutputStreams.indexOfKey(id);
1219 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001220 CLOGE("Stream %d does not exist",
1221 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001222 return BAD_VALUE;
1223 }
1224
1225 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001226}
1227
1228status_t Camera3Device::deleteStream(int id) {
1229 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001230 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001231 Mutex::Autolock l(mLock);
1232 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001233
Igor Murashkine2172be2013-05-28 15:31:39 -07001234 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1235
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001236 // CameraDevice semantics require device to already be idle before
1237 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001238 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001239 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1240 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001241 }
1242
Igor Murashkin2fba5842013-04-22 14:03:54 -07001243 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001244 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001245 if (mInputStream != NULL && id == mInputStream->getId()) {
1246 deletedStream = mInputStream;
1247 mInputStream.clear();
1248 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001249 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001250 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001251 return BAD_VALUE;
1252 }
Zhijun He5f446352014-01-22 09:49:33 -08001253 }
1254
1255 // Delete output stream or the output part of a bi-directional stream.
1256 if (outputStreamIdx != NAME_NOT_FOUND) {
1257 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001258 mOutputStreams.removeItem(id);
1259 }
1260
1261 // Free up the stream endpoint so that it can be used by some other stream
1262 res = deletedStream->disconnect();
1263 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001264 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001265 // fall through since we want to still list the stream as deleted.
1266 }
1267 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001268 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001269
1270 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001271}
1272
1273status_t Camera3Device::deleteReprocessStream(int id) {
1274 ATRACE_CALL();
1275 (void)id;
1276
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001277 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001278 return INVALID_OPERATION;
1279}
1280
Zhijun He1fa89992015-06-01 15:44:31 -07001281status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001282 ATRACE_CALL();
1283 ALOGV("%s: E", __FUNCTION__);
1284
1285 Mutex::Autolock il(mInterfaceLock);
1286 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001287
1288 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1289 mNeedConfig = true;
1290 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1291 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001292
1293 return configureStreamsLocked();
1294}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001295
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001296status_t Camera3Device::getInputBufferProducer(
1297 sp<IGraphicBufferProducer> *producer) {
1298 Mutex::Autolock il(mInterfaceLock);
1299 Mutex::Autolock l(mLock);
1300
1301 if (producer == NULL) {
1302 return BAD_VALUE;
1303 } else if (mInputStream == NULL) {
1304 return INVALID_OPERATION;
1305 }
1306
1307 return mInputStream->getInputBufferProducer(producer);
1308}
1309
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001310status_t Camera3Device::createDefaultRequest(int templateId,
1311 CameraMetadata *request) {
1312 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001313 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001314
1315 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1316 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1317 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1318 return BAD_VALUE;
1319 }
1320
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001321 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001322 Mutex::Autolock l(mLock);
1323
1324 switch (mStatus) {
1325 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001326 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001327 return INVALID_OPERATION;
1328 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001329 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001330 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001331 case STATUS_UNCONFIGURED:
1332 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001333 case STATUS_ACTIVE:
1334 // OK
1335 break;
1336 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001337 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001338 return INVALID_OPERATION;
1339 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001340
Zhijun Hea1530f12014-09-14 12:44:20 -07001341 if (!mRequestTemplateCache[templateId].isEmpty()) {
1342 *request = mRequestTemplateCache[templateId];
1343 return OK;
1344 }
1345
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001346 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001347 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001348 rawRequest = mHal3Device->ops->construct_default_request_settings(
1349 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001350 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001351 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001352 ALOGI("%s: template %d is not supported on this camera device",
1353 __FUNCTION__, templateId);
1354 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001355 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001356
Zhijun Hea1530f12014-09-14 12:44:20 -07001357 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001358
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001359 // Derive some new keys for backward compatibility
1360 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
1361 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
1362 int32_t defaultBoost[1] = {100};
1363 mRequestTemplateCache[templateId].update(
1364 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1365 defaultBoost, 1);
1366 }
1367
1368 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001369 return OK;
1370}
1371
1372status_t Camera3Device::waitUntilDrained() {
1373 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001374 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001375 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001376
Zhijun He69a37482014-03-23 18:44:49 -07001377 return waitUntilDrainedLocked();
1378}
1379
1380status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381 switch (mStatus) {
1382 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001383 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001384 ALOGV("%s: Already idle", __FUNCTION__);
1385 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001386 case STATUS_CONFIGURED:
1387 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001388 case STATUS_ERROR:
1389 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001390 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001391 break;
1392 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001393 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001394 return INVALID_OPERATION;
1395 }
1396
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001397 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1398 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001399 if (res != OK) {
1400 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1401 res);
1402 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001403 return res;
1404}
1405
Ruben Brunk183f0562015-08-12 12:55:02 -07001406
1407void Camera3Device::internalUpdateStatusLocked(Status status) {
1408 mStatus = status;
1409 mRecentStatusUpdates.add(mStatus);
1410 mStatusChanged.broadcast();
1411}
1412
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001413// Pause to reconfigure
1414status_t Camera3Device::internalPauseAndWaitLocked() {
1415 mRequestThread->setPaused(true);
1416 mPauseStateNotify = true;
1417
1418 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1419 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1420 if (res != OK) {
1421 SET_ERR_L("Can't idle device in %f seconds!",
1422 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001423 }
1424
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001425 return res;
1426}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001427
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001428// Resume after internalPauseAndWaitLocked
1429status_t Camera3Device::internalResumeLocked() {
1430 status_t res;
1431
1432 mRequestThread->setPaused(false);
1433
1434 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1435 if (res != OK) {
1436 SET_ERR_L("Can't transition to active in %f seconds!",
1437 kActiveTimeout/1e9);
1438 }
1439 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001440 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001441}
1442
Ruben Brunk183f0562015-08-12 12:55:02 -07001443status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001444 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001445
1446 size_t startIndex = 0;
1447 if (mStatusWaiters == 0) {
1448 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1449 // this status list
1450 mRecentStatusUpdates.clear();
1451 } else {
1452 // If other threads are waiting on updates to this status list, set the position of the
1453 // first element that this list will check rather than clearing the list.
1454 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001455 }
1456
Ruben Brunk183f0562015-08-12 12:55:02 -07001457 mStatusWaiters++;
1458
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001459 bool stateSeen = false;
1460 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001461 if (active == (mStatus == STATUS_ACTIVE)) {
1462 // Desired state is current
1463 break;
1464 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001465
1466 res = mStatusChanged.waitRelative(mLock, timeout);
1467 if (res != OK) break;
1468
Ruben Brunk183f0562015-08-12 12:55:02 -07001469 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1470 // transitions.
1471 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1472 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1473 __FUNCTION__);
1474
1475 // Encountered desired state since we began waiting
1476 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001477 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1478 stateSeen = true;
1479 break;
1480 }
1481 }
1482 } while (!stateSeen);
1483
Ruben Brunk183f0562015-08-12 12:55:02 -07001484 mStatusWaiters--;
1485
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001486 return res;
1487}
1488
1489
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001490status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001491 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001492 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001493
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001494 if (listener != NULL && mListener != NULL) {
1495 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1496 }
1497 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001498 mRequestThread->setNotificationListener(listener);
1499 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001500
1501 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001502}
1503
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001504bool Camera3Device::willNotify3A() {
1505 return false;
1506}
1507
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001508status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001509 status_t res;
1510 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001511
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001512 while (mResultQueue.empty()) {
1513 res = mResultSignal.waitRelative(mOutputLock, timeout);
1514 if (res == TIMED_OUT) {
1515 return res;
1516 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001517 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001518 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001519 return res;
1520 }
1521 }
1522 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001523}
1524
Jianing Weicb0652e2014-03-12 18:29:36 -07001525status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001526 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001527 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001528
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001529 if (mResultQueue.empty()) {
1530 return NOT_ENOUGH_DATA;
1531 }
1532
Jianing Weicb0652e2014-03-12 18:29:36 -07001533 if (frame == NULL) {
1534 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1535 return BAD_VALUE;
1536 }
1537
1538 CaptureResult &result = *(mResultQueue.begin());
1539 frame->mResultExtras = result.mResultExtras;
1540 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001541 mResultQueue.erase(mResultQueue.begin());
1542
1543 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001544}
1545
1546status_t Camera3Device::triggerAutofocus(uint32_t id) {
1547 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001548 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001549
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001550 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1551 // Mix-in this trigger into the next request and only the next request.
1552 RequestTrigger trigger[] = {
1553 {
1554 ANDROID_CONTROL_AF_TRIGGER,
1555 ANDROID_CONTROL_AF_TRIGGER_START
1556 },
1557 {
1558 ANDROID_CONTROL_AF_TRIGGER_ID,
1559 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001560 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001561 };
1562
1563 return mRequestThread->queueTrigger(trigger,
1564 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001565}
1566
1567status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1568 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001569 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001570
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001571 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1572 // Mix-in this trigger into the next request and only the next request.
1573 RequestTrigger trigger[] = {
1574 {
1575 ANDROID_CONTROL_AF_TRIGGER,
1576 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1577 },
1578 {
1579 ANDROID_CONTROL_AF_TRIGGER_ID,
1580 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001581 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001582 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001583
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001584 return mRequestThread->queueTrigger(trigger,
1585 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001586}
1587
1588status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1589 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001590 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001591
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001592 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1593 // Mix-in this trigger into the next request and only the next request.
1594 RequestTrigger trigger[] = {
1595 {
1596 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1597 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1598 },
1599 {
1600 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1601 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001602 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001603 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001604
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001605 return mRequestThread->queueTrigger(trigger,
1606 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001607}
1608
1609status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1610 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1611 ATRACE_CALL();
1612 (void)reprocessStreamId; (void)buffer; (void)listener;
1613
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001614 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001615 return INVALID_OPERATION;
1616}
1617
Jianing Weicb0652e2014-03-12 18:29:36 -07001618status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001619 ATRACE_CALL();
1620 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001621 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001622
Zhijun He7ef20392014-04-21 16:04:17 -07001623 {
1624 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001625 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001626 }
1627
Zhijun He491e3412013-12-27 10:57:44 -08001628 status_t res;
1629 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001630 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001631 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001632 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001633 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001634 }
1635
1636 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001637}
1638
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001639status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001640 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1641}
1642
1643status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001644 ATRACE_CALL();
1645 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001646 Mutex::Autolock il(mInterfaceLock);
1647 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001648
1649 sp<Camera3StreamInterface> stream;
1650 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1651 if (outputStreamIdx == NAME_NOT_FOUND) {
1652 CLOGE("Stream %d does not exist", streamId);
1653 return BAD_VALUE;
1654 }
1655
1656 stream = mOutputStreams.editValueAt(outputStreamIdx);
1657
1658 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001659 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001660 return BAD_VALUE;
1661 }
1662
1663 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001664 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001665 return BAD_VALUE;
1666 }
1667
Ruben Brunkc78ac262015-08-13 17:58:46 -07001668 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001669}
1670
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001671status_t Camera3Device::tearDown(int streamId) {
1672 ATRACE_CALL();
1673 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1674 Mutex::Autolock il(mInterfaceLock);
1675 Mutex::Autolock l(mLock);
1676
1677 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1678 // since we cannot call register_stream_buffers except right after configure_streams.
1679 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1680 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1681 __FUNCTION__, mHal3Device->common.version);
1682 return NO_INIT;
1683 }
1684
1685 sp<Camera3StreamInterface> stream;
1686 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1687 if (outputStreamIdx == NAME_NOT_FOUND) {
1688 CLOGE("Stream %d does not exist", streamId);
1689 return BAD_VALUE;
1690 }
1691
1692 stream = mOutputStreams.editValueAt(outputStreamIdx);
1693
1694 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1695 CLOGE("Stream %d is a target of a in-progress request", streamId);
1696 return BAD_VALUE;
1697 }
1698
1699 return stream->tearDown();
1700}
1701
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001702status_t Camera3Device::addBufferListenerForStream(int streamId,
1703 wp<Camera3StreamBufferListener> listener) {
1704 ATRACE_CALL();
1705 ALOGV("%s: Camera %d: Adding buffer listener for stream %d", __FUNCTION__, mId, streamId);
1706 Mutex::Autolock il(mInterfaceLock);
1707 Mutex::Autolock l(mLock);
1708
1709 sp<Camera3StreamInterface> stream;
1710 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1711 if (outputStreamIdx == NAME_NOT_FOUND) {
1712 CLOGE("Stream %d does not exist", streamId);
1713 return BAD_VALUE;
1714 }
1715
1716 stream = mOutputStreams.editValueAt(outputStreamIdx);
1717 stream->addBufferListener(listener);
1718
1719 return OK;
1720}
1721
Zhijun He204e3292014-07-14 17:09:23 -07001722uint32_t Camera3Device::getDeviceVersion() {
1723 ATRACE_CALL();
1724 Mutex::Autolock il(mInterfaceLock);
1725 return mDeviceVersion;
1726}
1727
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001728/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001729 * Methods called by subclasses
1730 */
1731
1732void Camera3Device::notifyStatus(bool idle) {
1733 {
1734 // Need mLock to safely update state and synchronize to current
1735 // state of methods in flight.
1736 Mutex::Autolock l(mLock);
1737 // We can get various system-idle notices from the status tracker
1738 // while starting up. Only care about them if we've actually sent
1739 // in some requests recently.
1740 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1741 return;
1742 }
1743 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1744 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001745 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001746
1747 // Skip notifying listener if we're doing some user-transparent
1748 // state changes
1749 if (mPauseStateNotify) return;
1750 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001751
1752 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001753 {
1754 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001755 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001756 }
1757 if (idle && listener != NULL) {
1758 listener->notifyIdle();
1759 }
1760}
1761
Zhijun He5d677d12016-05-29 16:52:39 -07001762status_t Camera3Device::setConsumerSurface(int streamId, sp<Surface> consumer) {
1763 ATRACE_CALL();
1764 ALOGV("%s: Camera %d: set consumer surface for stream %d", __FUNCTION__, mId, streamId);
1765 Mutex::Autolock il(mInterfaceLock);
1766 Mutex::Autolock l(mLock);
1767
1768 if (consumer == nullptr) {
1769 CLOGE("Null consumer is passed!");
1770 return BAD_VALUE;
1771 }
1772
1773 ssize_t idx = mOutputStreams.indexOfKey(streamId);
1774 if (idx == NAME_NOT_FOUND) {
1775 CLOGE("Stream %d is unknown", streamId);
1776 return idx;
1777 }
1778 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
1779 status_t res = stream->setConsumer(consumer);
1780 if (res != OK) {
1781 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1782 return res;
1783 }
1784
1785 if (!stream->isConfiguring()) {
1786 CLOGE("Stream %d was already fully configured.", streamId);
1787 return INVALID_OPERATION;
1788 }
1789
1790 res = stream->finishConfiguration(mHal3Device);
1791 if (res != OK) {
1792 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1793 stream->getId(), strerror(-res), res);
1794 return res;
1795 }
1796
1797 return OK;
1798}
1799
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001800/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001801 * Camera3Device private methods
1802 */
1803
1804sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1805 const CameraMetadata &request) {
1806 ATRACE_CALL();
1807 status_t res;
1808
1809 sp<CaptureRequest> newRequest = new CaptureRequest;
1810 newRequest->mSettings = request;
1811
1812 camera_metadata_entry_t inputStreams =
1813 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1814 if (inputStreams.count > 0) {
1815 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001816 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001817 CLOGE("Request references unknown input stream %d",
1818 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001819 return NULL;
1820 }
1821 // Lazy completion of stream configuration (allocation/registration)
1822 // on first use
1823 if (mInputStream->isConfiguring()) {
1824 res = mInputStream->finishConfiguration(mHal3Device);
1825 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001826 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001827 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001828 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001829 return NULL;
1830 }
1831 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001832 // Check if stream is being prepared
1833 if (mInputStream->isPreparing()) {
1834 CLOGE("Request references an input stream that's being prepared!");
1835 return NULL;
1836 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001837
1838 newRequest->mInputStream = mInputStream;
1839 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1840 }
1841
1842 camera_metadata_entry_t streams =
1843 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1844 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001845 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001846 return NULL;
1847 }
1848
1849 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001850 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001851 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001852 CLOGE("Request references unknown stream %d",
1853 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001854 return NULL;
1855 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001856 sp<Camera3OutputStreamInterface> stream =
1857 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001858
Zhijun He5d677d12016-05-29 16:52:39 -07001859 // It is illegal to include a deferred consumer output stream into a request
1860 if (stream->isConsumerConfigurationDeferred()) {
1861 CLOGE("Stream %d hasn't finished configuration yet due to deferred consumer",
1862 stream->getId());
1863 return NULL;
1864 }
1865
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001866 // Lazy completion of stream configuration (allocation/registration)
1867 // on first use
1868 if (stream->isConfiguring()) {
1869 res = stream->finishConfiguration(mHal3Device);
1870 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001871 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1872 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001873 return NULL;
1874 }
1875 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001876 // Check if stream is being prepared
1877 if (stream->isPreparing()) {
1878 CLOGE("Request references an output stream that's being prepared!");
1879 return NULL;
1880 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001881
1882 newRequest->mOutputStreams.push(stream);
1883 }
1884 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001885 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001886
1887 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001888}
1889
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001890bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1891 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1892 Size size = mSupportedOpaqueInputSizes[i];
1893 if (size.width == width && size.height == height) {
1894 return true;
1895 }
1896 }
1897
1898 return false;
1899}
1900
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001901void Camera3Device::cancelStreamsConfigurationLocked() {
1902 int res = OK;
1903 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1904 res = mInputStream->cancelConfiguration();
1905 if (res != OK) {
1906 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
1907 mInputStream->getId(), strerror(-res), res);
1908 }
1909 }
1910
1911 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1912 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
1913 if (outputStream->isConfiguring()) {
1914 res = outputStream->cancelConfiguration();
1915 if (res != OK) {
1916 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
1917 outputStream->getId(), strerror(-res), res);
1918 }
1919 }
1920 }
1921
1922 // Return state to that at start of call, so that future configures
1923 // properly clean things up
1924 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
1925 mNeedConfig = true;
1926}
1927
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001928status_t Camera3Device::configureStreamsLocked() {
1929 ATRACE_CALL();
1930 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001931
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001932 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001933 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001934 return INVALID_OPERATION;
1935 }
1936
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001937 if (!mNeedConfig) {
1938 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1939 return OK;
1940 }
1941
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001942 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1943 // adding a dummy stream instead.
1944 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1945 if (mOutputStreams.size() == 0) {
1946 addDummyStreamLocked();
1947 } else {
1948 tryRemoveDummyStreamLocked();
1949 }
1950
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001951 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001952 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001953
1954 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001955 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1956 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1957 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001958 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1959
1960 Vector<camera3_stream_t*> streams;
1961 streams.setCapacity(config.num_streams);
1962
1963 if (mInputStream != NULL) {
1964 camera3_stream_t *inputStream;
1965 inputStream = mInputStream->startConfiguration();
1966 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001967 CLOGE("Can't start input stream configuration");
1968 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001969 return INVALID_OPERATION;
1970 }
1971 streams.add(inputStream);
1972 }
1973
1974 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001975
1976 // Don't configure bidi streams twice, nor add them twice to the list
1977 if (mOutputStreams[i].get() ==
1978 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1979
1980 config.num_streams--;
1981 continue;
1982 }
1983
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001984 camera3_stream_t *outputStream;
1985 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1986 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001987 CLOGE("Can't start output stream configuration");
1988 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001989 return INVALID_OPERATION;
1990 }
1991 streams.add(outputStream);
1992 }
1993
1994 config.streams = streams.editArray();
1995
1996 // Do the HAL configuration; will potentially touch stream
1997 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001998 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001999 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002000 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002001
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002002 if (res == BAD_VALUE) {
2003 // HAL rejected this set of streams as unsupported, clean up config
2004 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002005 CLOGE("Set of requested inputs/outputs not supported by HAL");
2006 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002007 return BAD_VALUE;
2008 } else if (res != OK) {
2009 // Some other kind of error from configure_streams - this is not
2010 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002011 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2012 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002013 return res;
2014 }
2015
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002016 // Finish all stream configuration immediately.
2017 // TODO: Try to relax this later back to lazy completion, which should be
2018 // faster
2019
Igor Murashkin073f8572013-05-02 14:59:28 -07002020 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002021 res = mInputStream->finishConfiguration(mHal3Device);
2022 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002023 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002024 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002025 cancelStreamsConfigurationLocked();
2026 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002027 }
2028 }
2029
2030 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002031 sp<Camera3OutputStreamInterface> outputStream =
2032 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002033 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002034 res = outputStream->finishConfiguration(mHal3Device);
2035 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002036 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002037 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002038 cancelStreamsConfigurationLocked();
2039 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002040 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002041 }
2042 }
2043
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002044 // Request thread needs to know to avoid using repeat-last-settings protocol
2045 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002046 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002047
Zhijun He90f7c372016-08-16 16:19:43 -07002048 char value[PROPERTY_VALUE_MAX];
2049 property_get("camera.fifo.disable", value, "0");
2050 int32_t disableFifo = atoi(value);
2051 if (disableFifo != 1) {
2052 // Boost priority of request thread to SCHED_FIFO.
2053 pid_t requestThreadTid = mRequestThread->getTid();
2054 res = requestPriority(getpid(), requestThreadTid,
2055 kRequestThreadPriority, /*asynchronous*/ false);
2056 if (res != OK) {
2057 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2058 strerror(-res), res);
2059 } else {
2060 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2061 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002062 }
2063
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002064 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002065
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002066 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002067
Ruben Brunk183f0562015-08-12 12:55:02 -07002068 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2069 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002070
2071 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
2072
Zhijun He0a210512014-07-24 13:45:15 -07002073 // tear down the deleted streams after configure streams.
2074 mDeletedStreams.clear();
2075
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002076 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002077}
2078
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002079status_t Camera3Device::addDummyStreamLocked() {
2080 ATRACE_CALL();
2081 status_t res;
2082
2083 if (mDummyStreamId != NO_STREAM) {
2084 // Should never be adding a second dummy stream when one is already
2085 // active
2086 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
2087 __FUNCTION__, mId);
2088 return INVALID_OPERATION;
2089 }
2090
2091 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
2092
2093 sp<Camera3OutputStreamInterface> dummyStream =
2094 new Camera3DummyStream(mNextStreamId);
2095
2096 res = mOutputStreams.add(mNextStreamId, dummyStream);
2097 if (res < 0) {
2098 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2099 return res;
2100 }
2101
2102 mDummyStreamId = mNextStreamId;
2103 mNextStreamId++;
2104
2105 return OK;
2106}
2107
2108status_t Camera3Device::tryRemoveDummyStreamLocked() {
2109 ATRACE_CALL();
2110 status_t res;
2111
2112 if (mDummyStreamId == NO_STREAM) return OK;
2113 if (mOutputStreams.size() == 1) return OK;
2114
2115 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
2116
2117 // Ok, have a dummy stream and there's at least one other output stream,
2118 // so remove the dummy
2119
2120 sp<Camera3StreamInterface> deletedStream;
2121 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2122 if (outputStreamIdx == NAME_NOT_FOUND) {
2123 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2124 return INVALID_OPERATION;
2125 }
2126
2127 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2128 mOutputStreams.removeItemsAt(outputStreamIdx);
2129
2130 // Free up the stream endpoint so that it can be used by some other stream
2131 res = deletedStream->disconnect();
2132 if (res != OK) {
2133 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2134 // fall through since we want to still list the stream as deleted.
2135 }
2136 mDeletedStreams.add(deletedStream);
2137 mDummyStreamId = NO_STREAM;
2138
2139 return res;
2140}
2141
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002142void Camera3Device::setErrorState(const char *fmt, ...) {
2143 Mutex::Autolock l(mLock);
2144 va_list args;
2145 va_start(args, fmt);
2146
2147 setErrorStateLockedV(fmt, args);
2148
2149 va_end(args);
2150}
2151
2152void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2153 Mutex::Autolock l(mLock);
2154 setErrorStateLockedV(fmt, args);
2155}
2156
2157void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2158 va_list args;
2159 va_start(args, fmt);
2160
2161 setErrorStateLockedV(fmt, args);
2162
2163 va_end(args);
2164}
2165
2166void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002167 // Print out all error messages to log
2168 String8 errorCause = String8::formatV(fmt, args);
2169 ALOGE("Camera %d: %s", mId, errorCause.string());
2170
2171 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002172 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002173
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002174 mErrorCause = errorCause;
2175
2176 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002177 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002178
2179 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002180 sp<NotificationListener> listener = mListener.promote();
2181 if (listener != NULL) {
2182 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002183 CaptureResultExtras());
2184 }
2185
2186 // Save stack trace. View by dumping it later.
2187 CameraTraces::saveTrace();
2188 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002189}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002190
2191/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002192 * In-flight request management
2193 */
2194
Jianing Weicb0652e2014-03-12 18:29:36 -07002195status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002196 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2197 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002198 ATRACE_CALL();
2199 Mutex::Autolock l(mInFlightLock);
2200
2201 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002202 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2203 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002204 if (res < 0) return res;
2205
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002206 if (mInFlightMap.size() == 1) {
2207 mStatusTracker->markComponentActive(mInFlightStatusId);
2208 }
2209
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002210 return OK;
2211}
2212
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002213void Camera3Device::returnOutputBuffers(
2214 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2215 nsecs_t timestamp) {
2216 for (size_t i = 0; i < numBuffers; i++)
2217 {
2218 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2219 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2220 // Note: stream may be deallocated at this point, if this buffer was
2221 // the last reference to it.
2222 if (res != OK) {
2223 ALOGE("Can't return buffer to its stream: %s (%d)",
2224 strerror(-res), res);
2225 }
2226 }
2227}
2228
2229
2230void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2231
2232 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2233 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2234
2235 nsecs_t sensorTimestamp = request.sensorTimestamp;
2236 nsecs_t shutterTimestamp = request.shutterTimestamp;
2237
2238 // Check if it's okay to remove the request from InFlightMap:
2239 // In the case of a successful request:
2240 // all input and output buffers, all result metadata, shutter callback
2241 // arrived.
2242 // In the case of a unsuccessful request:
2243 // all input and output buffers arrived.
2244 if (request.numBuffersLeft == 0 &&
2245 (request.requestStatus != OK ||
2246 (request.haveResultMetadata && shutterTimestamp != 0))) {
2247 ATRACE_ASYNC_END("frame capture", frameNumber);
2248
2249 // Sanity check - if sensor timestamp matches shutter timestamp
2250 if (request.requestStatus == OK &&
2251 sensorTimestamp != shutterTimestamp) {
2252 SET_ERR("sensor timestamp (%" PRId64
2253 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2254 sensorTimestamp, frameNumber, shutterTimestamp);
2255 }
2256
2257 // for an unsuccessful request, it may have pending output buffers to
2258 // return.
2259 assert(request.requestStatus != OK ||
2260 request.pendingOutputBuffers.size() == 0);
2261 returnOutputBuffers(request.pendingOutputBuffers.array(),
2262 request.pendingOutputBuffers.size(), 0);
2263
2264 mInFlightMap.removeItemsAt(idx, 1);
2265
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002266 // Indicate idle inFlightMap to the status tracker
2267 if (mInFlightMap.size() == 0) {
2268 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2269 }
2270
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002271 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2272 }
2273
2274 // Sanity check - if we have too many in-flight frames, something has
2275 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002276 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002277 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002278 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2279 kInFlightWarnLimitHighSpeed) {
2280 CLOGE("In-flight list too large for high speed configuration: %zu",
2281 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002282 }
2283}
2284
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002285void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2286 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2287 if (result == nullptr) return;
2288
2289 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2290 (int32_t*)&frameNumber, 1) != OK) {
2291 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2292 return;
2293 }
2294
2295 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2296 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2297 return;
2298 }
2299
2300 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2301
2302 // Valid result, insert into queue
2303 List<CaptureResult>::iterator queuedResult =
2304 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2305 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2306 ", burstId = %" PRId32, __FUNCTION__,
2307 queuedResult->mResultExtras.requestId,
2308 queuedResult->mResultExtras.frameNumber,
2309 queuedResult->mResultExtras.burstId);
2310
2311 mResultSignal.signal();
2312}
2313
2314
2315void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2316 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2317 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2318 Mutex::Autolock l(mOutputLock);
2319
2320 CaptureResult captureResult;
2321 captureResult.mResultExtras = resultExtras;
2322 captureResult.mMetadata = partialResult;
2323
2324 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2325}
2326
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002327
2328void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2329 CaptureResultExtras &resultExtras,
2330 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002331 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002332 bool reprocess,
2333 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002334 if (pendingMetadata.isEmpty())
2335 return;
2336
2337 Mutex::Autolock l(mOutputLock);
2338
2339 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002340 if (reprocess) {
2341 if (frameNumber < mNextReprocessResultFrameNumber) {
2342 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002343 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002344 frameNumber, mNextReprocessResultFrameNumber);
2345 return;
2346 }
2347 mNextReprocessResultFrameNumber = frameNumber + 1;
2348 } else {
2349 if (frameNumber < mNextResultFrameNumber) {
2350 SET_ERR("Out-of-order capture result metadata submitted! "
2351 "(got frame number %d, expecting %d)",
2352 frameNumber, mNextResultFrameNumber);
2353 return;
2354 }
2355 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002356 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002357
2358 CaptureResult captureResult;
2359 captureResult.mResultExtras = resultExtras;
2360 captureResult.mMetadata = pendingMetadata;
2361
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002362 // Append any previous partials to form a complete result
2363 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2364 captureResult.mMetadata.append(collectedPartialResult);
2365 }
2366
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07002367 // Derive some new keys for backward compaibility
2368 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
2369 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
2370 int32_t defaultBoost[1] = {100};
2371 captureResult.mMetadata.update(
2372 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
2373 defaultBoost, 1);
2374 }
2375
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002376 captureResult.mMetadata.sort();
2377
2378 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002379 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2380 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002381 SET_ERR("No timestamp provided by HAL for frame %d!",
2382 frameNumber);
2383 return;
2384 }
2385
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002386 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2387 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2388
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002389 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002390}
2391
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002392/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002393 * Camera HAL device callback methods
2394 */
2395
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002396void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002397 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002398
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002399 status_t res;
2400
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002401 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002402 if (result->result == NULL && result->num_output_buffers == 0 &&
2403 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002404 SET_ERR("No result data provided by HAL for frame %d",
2405 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002406 return;
2407 }
Zhijun He204e3292014-07-14 17:09:23 -07002408
2409 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2410 // partial_result to 1 when metadata is included in this result.
2411 if (!mUsePartialResult &&
2412 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2413 result->result != NULL &&
2414 result->partial_result != 1) {
2415 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2416 " if partial result is not supported",
2417 frameNumber, result->partial_result);
2418 return;
2419 }
2420
2421 bool isPartialResult = false;
2422 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002423 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002424 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002425
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002426 // Get shutter timestamp and resultExtras from list of in-flight requests,
2427 // where it was added by the shutter notification for this frame. If the
2428 // shutter timestamp isn't received yet, append the output buffers to the
2429 // in-flight request and they will be returned when the shutter timestamp
2430 // arrives. Update the in-flight status and remove the in-flight entry if
2431 // all result data and shutter timestamp have been received.
2432 nsecs_t shutterTimestamp = 0;
2433
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002434 {
2435 Mutex::Autolock l(mInFlightLock);
2436 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2437 if (idx == NAME_NOT_FOUND) {
2438 SET_ERR("Unknown frame number for capture result: %d",
2439 frameNumber);
2440 return;
2441 }
2442 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002443 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2444 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2445 ", partialResultCount = %d",
2446 __FUNCTION__, request.resultExtras.requestId,
2447 request.resultExtras.frameNumber, request.resultExtras.burstId,
2448 result->partial_result);
2449 // Always update the partial count to the latest one if it's not 0
2450 // (buffers only). When framework aggregates adjacent partial results
2451 // into one, the latest partial count will be used.
2452 if (result->partial_result != 0)
2453 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002454
2455 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002456 if (mUsePartialResult && result->result != NULL) {
2457 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2458 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2459 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2460 " the range of [1, %d] when metadata is included in the result",
2461 frameNumber, result->partial_result, mNumPartialResults);
2462 return;
2463 }
2464 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002465 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002466 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002467 }
Zhijun He204e3292014-07-14 17:09:23 -07002468 } else {
2469 camera_metadata_ro_entry_t partialResultEntry;
2470 res = find_camera_metadata_ro_entry(result->result,
2471 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2472 if (res != NAME_NOT_FOUND &&
2473 partialResultEntry.count > 0 &&
2474 partialResultEntry.data.u8[0] ==
2475 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2476 // A partial result. Flag this as such, and collect this
2477 // set of metadata into the in-flight entry.
2478 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002479 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002480 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002481 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002482 ANDROID_QUIRKS_PARTIAL_RESULT);
2483 }
2484 }
2485
2486 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002487 // Send partial capture result
2488 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2489 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002490 }
2491 }
2492
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002493 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002494 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002495
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002496 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002497 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002498 if (request.haveResultMetadata) {
2499 SET_ERR("Called multiple times with metadata for frame %d",
2500 frameNumber);
2501 return;
2502 }
Zhijun He204e3292014-07-14 17:09:23 -07002503 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002504 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002505 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002506 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002507 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002508 request.haveResultMetadata = true;
2509 }
2510
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002511 uint32_t numBuffersReturned = result->num_output_buffers;
2512 if (result->input_buffer != NULL) {
2513 if (hasInputBufferInRequest) {
2514 numBuffersReturned += 1;
2515 } else {
2516 ALOGW("%s: Input buffer should be NULL if there is no input"
2517 " buffer sent in the request",
2518 __FUNCTION__);
2519 }
2520 }
2521 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002522 if (request.numBuffersLeft < 0) {
2523 SET_ERR("Too many buffers returned for frame %d",
2524 frameNumber);
2525 return;
2526 }
2527
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002528 camera_metadata_ro_entry_t entry;
2529 res = find_camera_metadata_ro_entry(result->result,
2530 ANDROID_SENSOR_TIMESTAMP, &entry);
2531 if (res == OK && entry.count == 1) {
2532 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002533 }
2534
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002535 // If shutter event isn't received yet, append the output buffers to
2536 // the in-flight request. Otherwise, return the output buffers to
2537 // streams.
2538 if (shutterTimestamp == 0) {
2539 request.pendingOutputBuffers.appendArray(result->output_buffers,
2540 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002541 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002542 returnOutputBuffers(result->output_buffers,
2543 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002544 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002545
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002546 if (result->result != NULL && !isPartialResult) {
2547 if (shutterTimestamp == 0) {
2548 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002549 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002550 } else {
2551 CameraMetadata metadata;
2552 metadata = result->result;
2553 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002554 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2555 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002556 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002557 }
2558
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002559 removeInFlightRequestIfReadyLocked(idx);
2560 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002561
Zhijun Hef0d962a2014-06-30 10:24:11 -07002562 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002563 if (hasInputBufferInRequest) {
2564 Camera3Stream *stream =
2565 Camera3Stream::cast(result->input_buffer->stream);
2566 res = stream->returnInputBuffer(*(result->input_buffer));
2567 // Note: stream may be deallocated at this point, if this buffer was the
2568 // last reference to it.
2569 if (res != OK) {
2570 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2571 " its stream:%s (%d)", __FUNCTION__,
2572 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002573 }
2574 } else {
2575 ALOGW("%s: Input buffer should be NULL if there is no input"
2576 " buffer sent in the request, skipping input buffer return.",
2577 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002578 }
2579 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002580}
2581
2582void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002583 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002584 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002585 {
2586 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002587 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002588 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002589
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002590 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002591 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002592 return;
2593 }
2594
2595 switch (msg->type) {
2596 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002597 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002598 break;
2599 }
2600 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002601 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002602 break;
2603 }
2604 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002605 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002606 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002607 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002608}
2609
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002610void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002611 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002612
2613 // Map camera HAL error codes to ICameraDeviceCallback error codes
2614 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002615 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002616 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002617 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002618 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002619 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002620 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002621 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002622 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002623 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002624 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002625 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002626 };
2627
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002628 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002629 ((msg.error_code >= 0) &&
2630 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2631 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002632 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002633
2634 int streamId = 0;
2635 if (msg.error_stream != NULL) {
2636 Camera3Stream *stream =
2637 Camera3Stream::cast(msg.error_stream);
2638 streamId = stream->getId();
2639 }
2640 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2641 mId, __FUNCTION__, msg.frame_number,
2642 streamId, msg.error_code);
2643
2644 CaptureResultExtras resultExtras;
2645 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002646 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002647 // SET_ERR calls notifyError
2648 SET_ERR("Camera HAL reported serious device error");
2649 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002650 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2651 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2652 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002653 {
2654 Mutex::Autolock l(mInFlightLock);
2655 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2656 if (idx >= 0) {
2657 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2658 r.requestStatus = msg.error_code;
2659 resultExtras = r.resultExtras;
2660 } else {
2661 resultExtras.frameNumber = msg.frame_number;
2662 ALOGE("Camera %d: %s: cannot find in-flight request on "
2663 "frame %" PRId64 " error", mId, __FUNCTION__,
2664 resultExtras.frameNumber);
2665 }
2666 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002667 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002668 if (listener != NULL) {
2669 listener->notifyError(errorCode, resultExtras);
2670 } else {
2671 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2672 }
2673 break;
2674 default:
2675 // SET_ERR calls notifyError
2676 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2677 break;
2678 }
2679}
2680
2681void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002682 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002683 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002684
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002685 // Set timestamp for the request in the in-flight tracking
2686 // and get the request ID to send upstream
2687 {
2688 Mutex::Autolock l(mInFlightLock);
2689 idx = mInFlightMap.indexOfKey(msg.frame_number);
2690 if (idx >= 0) {
2691 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002692
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002693 // Verify ordering of shutter notifications
2694 {
2695 Mutex::Autolock l(mOutputLock);
2696 // TODO: need to track errors for tighter bounds on expected frame number.
2697 if (r.hasInputBuffer) {
2698 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2699 SET_ERR("Shutter notification out-of-order. Expected "
2700 "notification for frame %d, got frame %d",
2701 mNextReprocessShutterFrameNumber, msg.frame_number);
2702 return;
2703 }
2704 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2705 } else {
2706 if (msg.frame_number < mNextShutterFrameNumber) {
2707 SET_ERR("Shutter notification out-of-order. Expected "
2708 "notification for frame %d, got frame %d",
2709 mNextShutterFrameNumber, msg.frame_number);
2710 return;
2711 }
2712 mNextShutterFrameNumber = msg.frame_number + 1;
2713 }
2714 }
2715
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002716 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2717 mId, __FUNCTION__,
2718 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2719 // Call listener, if any
2720 if (listener != NULL) {
2721 listener->notifyShutter(r.resultExtras, msg.timestamp);
2722 }
2723
2724 r.shutterTimestamp = msg.timestamp;
2725
2726 // send pending result and buffers
2727 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002728 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002729 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002730 returnOutputBuffers(r.pendingOutputBuffers.array(),
2731 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2732 r.pendingOutputBuffers.clear();
2733
2734 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002735 }
2736 }
2737 if (idx < 0) {
2738 SET_ERR("Shutter notification for non-existent frame number %d",
2739 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002740 }
2741}
2742
2743
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002744CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002745 ALOGV("%s", __FUNCTION__);
2746
Igor Murashkin1e479c02013-09-06 16:55:14 -07002747 CameraMetadata retVal;
2748
2749 if (mRequestThread != NULL) {
2750 retVal = mRequestThread->getLatestRequest();
2751 }
2752
Igor Murashkin1e479c02013-09-06 16:55:14 -07002753 return retVal;
2754}
2755
Jianing Weicb0652e2014-03-12 18:29:36 -07002756
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002757void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
2758 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
2759 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
2760}
2761
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002762/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002763 * RequestThread inner class methods
2764 */
2765
2766Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002767 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002768 camera3_device_t *hal3Device,
2769 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002770 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002771 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002772 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002773 mHal3Device(hal3Device),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002774 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002775 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002776 mReconfigured(false),
2777 mDoPause(false),
2778 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002779 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002780 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002781 mCurrentAfTriggerId(0),
2782 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002783 mRepeatingLastFrameNumber(
2784 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002785 mAeLockAvailable(aeLockAvailable),
2786 mPrepareVideoStream(false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002787 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002788}
2789
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002790void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002791 wp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002792 Mutex::Autolock l(mRequestLock);
2793 mListener = listener;
2794}
2795
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002796void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002797 Mutex::Autolock l(mRequestLock);
2798 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002799 // Prepare video stream for high speed recording.
2800 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002801}
2802
Jianing Wei90e59c92014-03-12 18:29:36 -07002803status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002804 List<sp<CaptureRequest> > &requests,
2805 /*out*/
2806 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002807 Mutex::Autolock l(mRequestLock);
2808 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2809 ++it) {
2810 mRequestQueue.push_back(*it);
2811 }
2812
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002813 if (lastFrameNumber != NULL) {
2814 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2815 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2816 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2817 *lastFrameNumber);
2818 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002819
Jianing Wei90e59c92014-03-12 18:29:36 -07002820 unpauseForNewRequests();
2821
2822 return OK;
2823}
2824
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002825
2826status_t Camera3Device::RequestThread::queueTrigger(
2827 RequestTrigger trigger[],
2828 size_t count) {
2829
2830 Mutex::Autolock l(mTriggerMutex);
2831 status_t ret;
2832
2833 for (size_t i = 0; i < count; ++i) {
2834 ret = queueTriggerLocked(trigger[i]);
2835
2836 if (ret != OK) {
2837 return ret;
2838 }
2839 }
2840
2841 return OK;
2842}
2843
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002844int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2845 sp<Camera3Device> d = device.promote();
2846 if (d != NULL) return d->mId;
2847 return 0;
2848}
2849
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002850status_t Camera3Device::RequestThread::queueTriggerLocked(
2851 RequestTrigger trigger) {
2852
2853 uint32_t tag = trigger.metadataTag;
2854 ssize_t index = mTriggerMap.indexOfKey(tag);
2855
2856 switch (trigger.getTagType()) {
2857 case TYPE_BYTE:
2858 // fall-through
2859 case TYPE_INT32:
2860 break;
2861 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002862 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2863 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002864 return INVALID_OPERATION;
2865 }
2866
2867 /**
2868 * Collect only the latest trigger, since we only have 1 field
2869 * in the request settings per trigger tag, and can't send more than 1
2870 * trigger per request.
2871 */
2872 if (index != NAME_NOT_FOUND) {
2873 mTriggerMap.editValueAt(index) = trigger;
2874 } else {
2875 mTriggerMap.add(tag, trigger);
2876 }
2877
2878 return OK;
2879}
2880
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002881status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002882 const RequestList &requests,
2883 /*out*/
2884 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002885 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002886 if (lastFrameNumber != NULL) {
2887 *lastFrameNumber = mRepeatingLastFrameNumber;
2888 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002889 mRepeatingRequests.clear();
2890 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2891 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002892
2893 unpauseForNewRequests();
2894
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002895 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002896 return OK;
2897}
2898
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07002899bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002900 if (mRepeatingRequests.empty()) {
2901 return false;
2902 }
2903 int32_t requestId = requestIn->mResultExtras.requestId;
2904 const RequestList &repeatRequests = mRepeatingRequests;
2905 // All repeating requests are guaranteed to have same id so only check first quest
2906 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2907 return (firstRequest->mResultExtras.requestId == requestId);
2908}
2909
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002910status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002911 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002912 return clearRepeatingRequestsLocked(lastFrameNumber);
2913
2914}
2915
2916status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002917 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002918 if (lastFrameNumber != NULL) {
2919 *lastFrameNumber = mRepeatingLastFrameNumber;
2920 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002921 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002922 return OK;
2923}
2924
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002925status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002926 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002927 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002928 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002929
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002930 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002931
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002932 // Send errors for all requests pending in the request queue, including
2933 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002934 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002935 if (listener != NULL) {
2936 for (RequestList::iterator it = mRequestQueue.begin();
2937 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002938 // Abort the input buffers for reprocess requests.
2939 if ((*it)->mInputStream != NULL) {
2940 camera3_stream_buffer_t inputBuffer;
2941 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2942 if (res != OK) {
2943 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2944 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2945 } else {
2946 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2947 if (res != OK) {
2948 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2949 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2950 }
2951 }
2952 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002953 // Set the frame number this request would have had, if it
2954 // had been submitted; this frame number will not be reused.
2955 // The requestId and burstId fields were set when the request was
2956 // submitted originally (in convertMetadataListToRequestListLocked)
2957 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002958 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002959 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002960 }
2961 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002962 mRequestQueue.clear();
2963 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002964 if (lastFrameNumber != NULL) {
2965 *lastFrameNumber = mRepeatingLastFrameNumber;
2966 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002967 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002968 return OK;
2969}
2970
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002971status_t Camera3Device::RequestThread::flush() {
2972 ATRACE_CALL();
2973 Mutex::Autolock l(mFlushLock);
2974
2975 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2976 return mHal3Device->ops->flush(mHal3Device);
2977 }
2978
2979 return -ENOTSUP;
2980}
2981
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002982void Camera3Device::RequestThread::setPaused(bool paused) {
2983 Mutex::Autolock l(mPauseLock);
2984 mDoPause = paused;
2985 mDoPauseSignal.signal();
2986}
2987
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002988status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2989 int32_t requestId, nsecs_t timeout) {
2990 Mutex::Autolock l(mLatestRequestMutex);
2991 status_t res;
2992 while (mLatestRequestId != requestId) {
2993 nsecs_t startTime = systemTime();
2994
2995 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2996 if (res != OK) return res;
2997
2998 timeout -= (systemTime() - startTime);
2999 }
3000
3001 return OK;
3002}
3003
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003004void Camera3Device::RequestThread::requestExit() {
3005 // Call parent to set up shutdown
3006 Thread::requestExit();
3007 // The exit from any possible waits
3008 mDoPauseSignal.signal();
3009 mRequestSignal.signal();
3010}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003011
Chien-Yu Chend196d612015-06-22 19:49:01 -07003012
3013/**
3014 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
3015 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
3016 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
3017 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
3018 * request.
3019 */
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003020void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(const sp<CaptureRequest>& request) {
Chien-Yu Chend196d612015-06-22 19:49:01 -07003021 request->mAeTriggerCancelOverride.applyAeLock = false;
3022 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
3023
3024 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
3025 return;
3026 }
3027
3028 camera_metadata_entry_t aePrecaptureTrigger =
3029 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3030 if (aePrecaptureTrigger.count > 0 &&
3031 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
3032 // Always override CANCEL to IDLE
3033 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
3034 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
3035 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
3036 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
3037 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3038
3039 if (mAeLockAvailable == true) {
3040 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3041 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3042 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3043 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3044 request->mAeTriggerCancelOverride.applyAeLock = true;
3045 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3046 }
3047 }
3048 }
3049}
3050
3051/**
3052 * Override result metadata for cancelling AE precapture trigger applied in
3053 * handleAePrecaptureCancelRequest().
3054 */
3055void Camera3Device::overrideResultForPrecaptureCancel(
3056 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3057 if (aeTriggerCancelOverride.applyAeLock) {
3058 // Only devices <= v3.2 should have this override
3059 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3060 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3061 }
3062
3063 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3064 // Only devices <= v3.2 should have this override
3065 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3066 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3067 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3068 }
3069}
3070
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003071void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003072 bool surfaceAbandoned = false;
3073 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003074 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003075 {
3076 Mutex::Autolock l(mRequestLock);
3077 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3078 // repeating requests.
3079 for (const auto& request : mRepeatingRequests) {
3080 for (const auto& s : request->mOutputStreams) {
3081 if (s->isAbandoned()) {
3082 surfaceAbandoned = true;
3083 clearRepeatingRequestsLocked(&lastFrameNumber);
3084 break;
3085 }
3086 }
3087 if (surfaceAbandoned) {
3088 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003089 }
3090 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003091 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003092 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003093
3094 if (listener != NULL && surfaceAbandoned) {
3095 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003096 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003097}
3098
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003099bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003100 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003101 status_t res;
3102
3103 // Handle paused state.
3104 if (waitIfPaused()) {
3105 return true;
3106 }
3107
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003108 // Wait for the next batch of requests.
3109 waitForNextRequestBatch();
3110 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003111 return true;
3112 }
3113
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003114 // Get the latest request ID, if any
3115 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003116 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003117 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003118 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003119 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003120 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003121 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3122 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003123 }
3124
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003125 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003126 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003127 if (res == TIMED_OUT) {
3128 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003129 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003130 // Check if any stream is abandoned.
3131 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003132 return true;
3133 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003134 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003135 return false;
3136 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003137
Zhijun Hecc27e112013-10-03 16:12:43 -07003138 // Inform waitUntilRequestProcessed thread of a new request ID
3139 {
3140 Mutex::Autolock al(mLatestRequestMutex);
3141
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003142 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003143 mLatestRequestSignal.signal();
3144 }
3145
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003146 // Submit a batch of requests to HAL.
3147 // Use flush lock only when submitting multilple requests in a batch.
3148 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3149 // which may take a long time to finish so synchronizing flush() and
3150 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3151 // For now, only synchronize for high speed recording and we should figure something out for
3152 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003153 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003154
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003155 if (useFlushLock) {
3156 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003157 }
3158
Zhijun Hef0645c12016-08-02 00:58:11 -07003159 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003160 mNextRequests.size());
3161 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003162 // Submit request and block until ready for next one
3163 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3164 ATRACE_BEGIN("camera3->process_capture_request");
3165 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3166 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003167
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003168 if (res != OK) {
3169 // Should only get a failure here for malformed requests or device-level
3170 // errors, so consider all errors fatal. Bad metadata failures should
3171 // come through notify.
3172 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3173 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3174 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003175 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003176 if (useFlushLock) {
3177 mFlushLock.unlock();
3178 }
3179 return false;
3180 }
3181
3182 // Mark that the request has be submitted successfully.
3183 nextRequest.submitted = true;
3184
3185 // Update the latest request sent to HAL
3186 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3187 Mutex::Autolock al(mLatestRequestMutex);
3188
3189 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3190 mLatestRequest.acquire(cloned);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003191
3192 sp<Camera3Device> parent = mParent.promote();
3193 if (parent != NULL) {
3194 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
3195 0, mLatestRequest);
3196 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003197 }
3198
3199 if (nextRequest.halRequest.settings != NULL) {
3200 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3201 }
3202
3203 // Remove any previously queued triggers (after unlock)
3204 res = removeTriggers(mPrevRequest);
3205 if (res != OK) {
3206 SET_ERR("RequestThread: Unable to remove triggers "
3207 "(capture request %d, HAL device: %s (%d)",
3208 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003209 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003210 if (useFlushLock) {
3211 mFlushLock.unlock();
3212 }
3213 return false;
3214 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003215 }
3216
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003217 if (useFlushLock) {
3218 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003219 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003220
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003221 // Unset as current request
3222 {
3223 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003224 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003225 }
3226
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003227 return true;
3228}
3229
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003230status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003231 ATRACE_CALL();
3232
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003233 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003234 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3235 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3236 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3237
3238 // Prepare a request to HAL
3239 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3240
3241 // Insert any queued triggers (before metadata is locked)
3242 status_t res = insertTriggers(captureRequest);
3243
3244 if (res < 0) {
3245 SET_ERR("RequestThread: Unable to insert triggers "
3246 "(capture request %d, HAL device: %s (%d)",
3247 halRequest->frame_number, strerror(-res), res);
3248 return INVALID_OPERATION;
3249 }
3250 int triggerCount = res;
3251 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3252 mPrevTriggers = triggerCount;
3253
3254 // If the request is the same as last, or we had triggers last time
3255 if (mPrevRequest != captureRequest || triggersMixedIn) {
3256 /**
3257 * HAL workaround:
3258 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3259 */
3260 res = addDummyTriggerIds(captureRequest);
3261 if (res != OK) {
3262 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3263 "(capture request %d, HAL device: %s (%d)",
3264 halRequest->frame_number, strerror(-res), res);
3265 return INVALID_OPERATION;
3266 }
3267
3268 /**
3269 * The request should be presorted so accesses in HAL
3270 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3271 */
3272 captureRequest->mSettings.sort();
3273 halRequest->settings = captureRequest->mSettings.getAndLock();
3274 mPrevRequest = captureRequest;
3275 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3276
3277 IF_ALOGV() {
3278 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3279 find_camera_metadata_ro_entry(
3280 halRequest->settings,
3281 ANDROID_CONTROL_AF_TRIGGER,
3282 &e
3283 );
3284 if (e.count > 0) {
3285 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3286 __FUNCTION__,
3287 halRequest->frame_number,
3288 e.data.u8[0]);
3289 }
3290 }
3291 } else {
3292 // leave request.settings NULL to indicate 'reuse latest given'
3293 ALOGVV("%s: Request settings are REUSED",
3294 __FUNCTION__);
3295 }
3296
3297 uint32_t totalNumBuffers = 0;
3298
3299 // Fill in buffers
3300 if (captureRequest->mInputStream != NULL) {
3301 halRequest->input_buffer = &captureRequest->mInputBuffer;
3302 totalNumBuffers += 1;
3303 } else {
3304 halRequest->input_buffer = NULL;
3305 }
3306
3307 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3308 captureRequest->mOutputStreams.size());
3309 halRequest->output_buffers = outputBuffers->array();
3310 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003311 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(i);
3312
3313 // Prepare video buffers for high speed recording on the first video request.
3314 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3315 // Only try to prepare video stream on the first video request.
3316 mPrepareVideoStream = false;
3317
3318 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
3319 while (res == NOT_ENOUGH_DATA) {
3320 res = outputStream->prepareNextBuffer();
3321 }
3322 if (res != OK) {
3323 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3324 __FUNCTION__, strerror(-res), res);
3325 outputStream->cancelPrepare();
3326 }
3327 }
3328
3329 res = outputStream->getBuffer(&outputBuffers->editItemAt(i));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003330 if (res != OK) {
3331 // Can't get output buffer from gralloc queue - this could be due to
3332 // abandoned queue or other consumer misbehavior, so not a fatal
3333 // error
3334 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3335 " %s (%d)", strerror(-res), res);
3336
3337 return TIMED_OUT;
3338 }
3339 halRequest->num_output_buffers++;
3340 }
3341 totalNumBuffers += halRequest->num_output_buffers;
3342
3343 // Log request in the in-flight queue
3344 sp<Camera3Device> parent = mParent.promote();
3345 if (parent == NULL) {
3346 // Should not happen, and nowhere to send errors to, so just log it
3347 CLOGE("RequestThread: Parent is gone");
3348 return INVALID_OPERATION;
3349 }
3350 res = parent->registerInFlight(halRequest->frame_number,
3351 totalNumBuffers, captureRequest->mResultExtras,
3352 /*hasInput*/halRequest->input_buffer != NULL,
3353 captureRequest->mAeTriggerCancelOverride);
3354 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3355 ", burstId = %" PRId32 ".",
3356 __FUNCTION__,
3357 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3358 captureRequest->mResultExtras.burstId);
3359 if (res != OK) {
3360 SET_ERR("RequestThread: Unable to register new in-flight request:"
3361 " %s (%d)", strerror(-res), res);
3362 return INVALID_OPERATION;
3363 }
3364 }
3365
3366 return OK;
3367}
3368
Igor Murashkin1e479c02013-09-06 16:55:14 -07003369CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3370 Mutex::Autolock al(mLatestRequestMutex);
3371
3372 ALOGV("RequestThread::%s", __FUNCTION__);
3373
3374 return mLatestRequest;
3375}
3376
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003377bool Camera3Device::RequestThread::isStreamPending(
3378 sp<Camera3StreamInterface>& stream) {
3379 Mutex::Autolock l(mRequestLock);
3380
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003381 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003382 if (!nextRequest.submitted) {
3383 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3384 if (stream == s) return true;
3385 }
3386 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003387 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003388 }
3389
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003390 for (const auto& request : mRequestQueue) {
3391 for (const auto& s : request->mOutputStreams) {
3392 if (stream == s) return true;
3393 }
3394 if (stream == request->mInputStream) return true;
3395 }
3396
3397 for (const auto& request : mRepeatingRequests) {
3398 for (const auto& s : request->mOutputStreams) {
3399 if (stream == s) return true;
3400 }
3401 if (stream == request->mInputStream) return true;
3402 }
3403
3404 return false;
3405}
Jianing Weicb0652e2014-03-12 18:29:36 -07003406
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003407void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3408 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003409 return;
3410 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003411
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003412 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003413 // Skip the ones that have been submitted successfully.
3414 if (nextRequest.submitted) {
3415 continue;
3416 }
3417
3418 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3419 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3420 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3421
3422 if (halRequest->settings != NULL) {
3423 captureRequest->mSettings.unlock(halRequest->settings);
3424 }
3425
3426 if (captureRequest->mInputStream != NULL) {
3427 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3428 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3429 }
3430
3431 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3432 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3433 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3434 }
3435
3436 if (sendRequestError) {
3437 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003438 sp<NotificationListener> listener = mListener.promote();
3439 if (listener != NULL) {
3440 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003441 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003442 captureRequest->mResultExtras);
3443 }
3444 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003445 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003446
3447 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003448 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003449}
3450
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003451void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003452 // Optimized a bit for the simple steady-state case (single repeating
3453 // request), to avoid putting that request in the queue temporarily.
3454 Mutex::Autolock l(mRequestLock);
3455
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003456 assert(mNextRequests.empty());
3457
3458 NextRequest nextRequest;
3459 nextRequest.captureRequest = waitForNextRequestLocked();
3460 if (nextRequest.captureRequest == nullptr) {
3461 return;
3462 }
3463
3464 nextRequest.halRequest = camera3_capture_request_t();
3465 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003466 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003467
3468 // Wait for additional requests
3469 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3470
3471 for (size_t i = 1; i < batchSize; i++) {
3472 NextRequest additionalRequest;
3473 additionalRequest.captureRequest = waitForNextRequestLocked();
3474 if (additionalRequest.captureRequest == nullptr) {
3475 break;
3476 }
3477
3478 additionalRequest.halRequest = camera3_capture_request_t();
3479 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003480 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003481 }
3482
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003483 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003484 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003485 mNextRequests.size(), batchSize);
3486 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003487 }
3488
3489 return;
3490}
3491
3492sp<Camera3Device::CaptureRequest>
3493 Camera3Device::RequestThread::waitForNextRequestLocked() {
3494 status_t res;
3495 sp<CaptureRequest> nextRequest;
3496
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003497 while (mRequestQueue.empty()) {
3498 if (!mRepeatingRequests.empty()) {
3499 // Always atomically enqueue all requests in a repeating request
3500 // list. Guarantees a complete in-sequence set of captures to
3501 // application.
3502 const RequestList &requests = mRepeatingRequests;
3503 RequestList::const_iterator firstRequest =
3504 requests.begin();
3505 nextRequest = *firstRequest;
3506 mRequestQueue.insert(mRequestQueue.end(),
3507 ++firstRequest,
3508 requests.end());
3509 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003510
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003511 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003512
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003513 break;
3514 }
3515
3516 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3517
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003518 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3519 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003520 Mutex::Autolock pl(mPauseLock);
3521 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003522 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003523 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003524 // Let the tracker know
3525 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3526 if (statusTracker != 0) {
3527 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3528 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003529 }
3530 // Stop waiting for now and let thread management happen
3531 return NULL;
3532 }
3533 }
3534
3535 if (nextRequest == NULL) {
3536 // Don't have a repeating request already in hand, so queue
3537 // must have an entry now.
3538 RequestList::iterator firstRequest =
3539 mRequestQueue.begin();
3540 nextRequest = *firstRequest;
3541 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07003542 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
3543 sp<NotificationListener> listener = mListener.promote();
3544 if (listener != NULL) {
3545 listener->notifyRequestQueueEmpty();
3546 }
3547 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003548 }
3549
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003550 // In case we've been unpaused by setPaused clearing mDoPause, need to
3551 // update internal pause state (capture/setRepeatingRequest unpause
3552 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003553 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003554 if (mPaused) {
3555 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3556 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3557 if (statusTracker != 0) {
3558 statusTracker->markComponentActive(mStatusId);
3559 }
3560 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003561 mPaused = false;
3562
3563 // Check if we've reconfigured since last time, and reset the preview
3564 // request if so. Can't use 'NULL request == repeat' across configure calls.
3565 if (mReconfigured) {
3566 mPrevRequest.clear();
3567 mReconfigured = false;
3568 }
3569
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003570 if (nextRequest != NULL) {
3571 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003572 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3573 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003574
3575 // Since RequestThread::clear() removes buffers from the input stream,
3576 // get the right buffer here before unlocking mRequestLock
3577 if (nextRequest->mInputStream != NULL) {
3578 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3579 if (res != OK) {
3580 // Can't get input buffer from gralloc queue - this could be due to
3581 // disconnected queue or other producer misbehavior, so not a fatal
3582 // error
3583 ALOGE("%s: Can't get input buffer, skipping request:"
3584 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003585
3586 sp<NotificationListener> listener = mListener.promote();
3587 if (listener != NULL) {
3588 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003589 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003590 nextRequest->mResultExtras);
3591 }
3592 return NULL;
3593 }
3594 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003595 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003596
3597 handleAePrecaptureCancelRequest(nextRequest);
3598
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003599 return nextRequest;
3600}
3601
3602bool Camera3Device::RequestThread::waitIfPaused() {
3603 status_t res;
3604 Mutex::Autolock l(mPauseLock);
3605 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003606 if (mPaused == false) {
3607 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003608 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3609 // Let the tracker know
3610 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3611 if (statusTracker != 0) {
3612 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3613 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003614 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003615
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003616 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003617 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003618 return true;
3619 }
3620 }
3621 // We don't set mPaused to false here, because waitForNextRequest needs
3622 // to further manage the paused state in case of starvation.
3623 return false;
3624}
3625
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003626void Camera3Device::RequestThread::unpauseForNewRequests() {
3627 // With work to do, mark thread as unpaused.
3628 // If paused by request (setPaused), don't resume, to avoid
3629 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003630 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003631 Mutex::Autolock p(mPauseLock);
3632 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003633 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3634 if (mPaused) {
3635 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3636 if (statusTracker != 0) {
3637 statusTracker->markComponentActive(mStatusId);
3638 }
3639 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003640 mPaused = false;
3641 }
3642}
3643
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003644void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3645 sp<Camera3Device> parent = mParent.promote();
3646 if (parent != NULL) {
3647 va_list args;
3648 va_start(args, fmt);
3649
3650 parent->setErrorStateV(fmt, args);
3651
3652 va_end(args);
3653 }
3654}
3655
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003656status_t Camera3Device::RequestThread::insertTriggers(
3657 const sp<CaptureRequest> &request) {
3658
3659 Mutex::Autolock al(mTriggerMutex);
3660
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003661 sp<Camera3Device> parent = mParent.promote();
3662 if (parent == NULL) {
3663 CLOGE("RequestThread: Parent is gone");
3664 return DEAD_OBJECT;
3665 }
3666
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003667 CameraMetadata &metadata = request->mSettings;
3668 size_t count = mTriggerMap.size();
3669
3670 for (size_t i = 0; i < count; ++i) {
3671 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003672 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003673
3674 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3675 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3676 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003677 if (isAeTrigger) {
3678 request->mResultExtras.precaptureTriggerId = triggerId;
3679 mCurrentPreCaptureTriggerId = triggerId;
3680 } else {
3681 request->mResultExtras.afTriggerId = triggerId;
3682 mCurrentAfTriggerId = triggerId;
3683 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003684 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3685 continue; // Trigger ID tag is deprecated since device HAL 3.2
3686 }
3687 }
3688
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003689 camera_metadata_entry entry = metadata.find(tag);
3690
3691 if (entry.count > 0) {
3692 /**
3693 * Already has an entry for this trigger in the request.
3694 * Rewrite it with our requested trigger value.
3695 */
3696 RequestTrigger oldTrigger = trigger;
3697
3698 oldTrigger.entryValue = entry.data.u8[0];
3699
3700 mTriggerReplacedMap.add(tag, oldTrigger);
3701 } else {
3702 /**
3703 * More typical, no trigger entry, so we just add it
3704 */
3705 mTriggerRemovedMap.add(tag, trigger);
3706 }
3707
3708 status_t res;
3709
3710 switch (trigger.getTagType()) {
3711 case TYPE_BYTE: {
3712 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3713 res = metadata.update(tag,
3714 &entryValue,
3715 /*count*/1);
3716 break;
3717 }
3718 case TYPE_INT32:
3719 res = metadata.update(tag,
3720 &trigger.entryValue,
3721 /*count*/1);
3722 break;
3723 default:
3724 ALOGE("%s: Type not supported: 0x%x",
3725 __FUNCTION__,
3726 trigger.getTagType());
3727 return INVALID_OPERATION;
3728 }
3729
3730 if (res != OK) {
3731 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3732 ", value %d", __FUNCTION__, trigger.getTagName(),
3733 trigger.entryValue);
3734 return res;
3735 }
3736
3737 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3738 trigger.getTagName(),
3739 trigger.entryValue);
3740 }
3741
3742 mTriggerMap.clear();
3743
3744 return count;
3745}
3746
3747status_t Camera3Device::RequestThread::removeTriggers(
3748 const sp<CaptureRequest> &request) {
3749 Mutex::Autolock al(mTriggerMutex);
3750
3751 CameraMetadata &metadata = request->mSettings;
3752
3753 /**
3754 * Replace all old entries with their old values.
3755 */
3756 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3757 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3758
3759 status_t res;
3760
3761 uint32_t tag = trigger.metadataTag;
3762 switch (trigger.getTagType()) {
3763 case TYPE_BYTE: {
3764 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3765 res = metadata.update(tag,
3766 &entryValue,
3767 /*count*/1);
3768 break;
3769 }
3770 case TYPE_INT32:
3771 res = metadata.update(tag,
3772 &trigger.entryValue,
3773 /*count*/1);
3774 break;
3775 default:
3776 ALOGE("%s: Type not supported: 0x%x",
3777 __FUNCTION__,
3778 trigger.getTagType());
3779 return INVALID_OPERATION;
3780 }
3781
3782 if (res != OK) {
3783 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3784 ", trigger value %d", __FUNCTION__,
3785 trigger.getTagName(), trigger.entryValue);
3786 return res;
3787 }
3788 }
3789 mTriggerReplacedMap.clear();
3790
3791 /**
3792 * Remove all new entries.
3793 */
3794 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3795 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3796 status_t res = metadata.erase(trigger.metadataTag);
3797
3798 if (res != OK) {
3799 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3800 ", trigger value %d", __FUNCTION__,
3801 trigger.getTagName(), trigger.entryValue);
3802 return res;
3803 }
3804 }
3805 mTriggerRemovedMap.clear();
3806
3807 return OK;
3808}
3809
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003810status_t Camera3Device::RequestThread::addDummyTriggerIds(
3811 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003812 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003813 static const int32_t dummyTriggerId = 1;
3814 status_t res;
3815
3816 CameraMetadata &metadata = request->mSettings;
3817
3818 // If AF trigger is active, insert a dummy AF trigger ID if none already
3819 // exists
3820 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3821 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3822 if (afTrigger.count > 0 &&
3823 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3824 afId.count == 0) {
3825 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3826 if (res != OK) return res;
3827 }
3828
3829 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3830 // if none already exists
3831 camera_metadata_entry pcTrigger =
3832 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3833 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3834 if (pcTrigger.count > 0 &&
3835 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3836 pcId.count == 0) {
3837 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3838 &dummyTriggerId, 1);
3839 if (res != OK) return res;
3840 }
3841
3842 return OK;
3843}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003844
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003845/**
3846 * PreparerThread inner class methods
3847 */
3848
3849Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003850 Thread(/*canCallJava*/false), mListener(nullptr),
3851 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003852}
3853
3854Camera3Device::PreparerThread::~PreparerThread() {
3855 Thread::requestExitAndWait();
3856 if (mCurrentStream != nullptr) {
3857 mCurrentStream->cancelPrepare();
3858 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3859 mCurrentStream.clear();
3860 }
3861 clear();
3862}
3863
Ruben Brunkc78ac262015-08-13 17:58:46 -07003864status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003865 status_t res;
3866
3867 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003868 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003869
Ruben Brunkc78ac262015-08-13 17:58:46 -07003870 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003871 if (res == OK) {
3872 // No preparation needed, fire listener right off
3873 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003874 if (listener != NULL) {
3875 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003876 }
3877 return OK;
3878 } else if (res != NOT_ENOUGH_DATA) {
3879 return res;
3880 }
3881
3882 // Need to prepare, start up thread if necessary
3883 if (!mActive) {
3884 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3885 // isn't running
3886 Thread::requestExitAndWait();
3887 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3888 if (res != OK) {
3889 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003890 if (listener != NULL) {
3891 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003892 }
3893 return res;
3894 }
3895 mCancelNow = false;
3896 mActive = true;
3897 ALOGV("%s: Preparer stream started", __FUNCTION__);
3898 }
3899
3900 // queue up the work
3901 mPendingStreams.push_back(stream);
3902 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3903
3904 return OK;
3905}
3906
3907status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003908 Mutex::Autolock l(mLock);
3909
3910 for (const auto& stream : mPendingStreams) {
3911 stream->cancelPrepare();
3912 }
3913 mPendingStreams.clear();
3914 mCancelNow = true;
3915
3916 return OK;
3917}
3918
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003919void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003920 Mutex::Autolock l(mLock);
3921 mListener = listener;
3922}
3923
3924bool Camera3Device::PreparerThread::threadLoop() {
3925 status_t res;
3926 {
3927 Mutex::Autolock l(mLock);
3928 if (mCurrentStream == nullptr) {
3929 // End thread if done with work
3930 if (mPendingStreams.empty()) {
3931 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3932 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3933 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3934 mActive = false;
3935 return false;
3936 }
3937
3938 // Get next stream to prepare
3939 auto it = mPendingStreams.begin();
3940 mCurrentStream = *it;
3941 mPendingStreams.erase(it);
3942 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3943 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3944 } else if (mCancelNow) {
3945 mCurrentStream->cancelPrepare();
3946 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3947 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3948 mCurrentStream.clear();
3949 mCancelNow = false;
3950 return true;
3951 }
3952 }
3953
3954 res = mCurrentStream->prepareNextBuffer();
3955 if (res == NOT_ENOUGH_DATA) return true;
3956 if (res != OK) {
3957 // Something bad happened; try to recover by cancelling prepare and
3958 // signalling listener anyway
3959 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3960 mCurrentStream->getId(), res, strerror(-res));
3961 mCurrentStream->cancelPrepare();
3962 }
3963
3964 // This stream has finished, notify listener
3965 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003966 sp<NotificationListener> listener = mListener.promote();
3967 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003968 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3969 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003970 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003971 }
3972
3973 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3974 mCurrentStream.clear();
3975
3976 return true;
3977}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003978
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003979/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003980 * Static callback forwarding methods from HAL to instance
3981 */
3982
3983void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3984 const camera3_capture_result *result) {
3985 Camera3Device *d =
3986 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003987
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003988 d->processCaptureResult(result);
3989}
3990
3991void Camera3Device::sNotify(const camera3_callback_ops *cb,
3992 const camera3_notify_msg *msg) {
3993 Camera3Device *d =
3994 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3995 d->notify(msg);
3996}
3997
3998}; // namespace android