blob: 73a412417140763d0c1e7d246f68ce88261ffa0d [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(
689 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
690 if (requestList == NULL) {
691 CLOGE("requestList cannot be NULL.");
692 return BAD_VALUE;
693 }
694
Jianing Weicb0652e2014-03-12 18:29:36 -0700695 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700696 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
697 it != metadataList.end(); ++it) {
698 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
699 if (newRequest == 0) {
700 CLOGE("Can't create capture request");
701 return BAD_VALUE;
702 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700703
704 // Setup burst Id and request Id
705 newRequest->mResultExtras.burstId = burstId++;
706 if (it->exists(ANDROID_REQUEST_ID)) {
707 if (it->find(ANDROID_REQUEST_ID).count == 0) {
708 CLOGE("RequestID entry exists; but must not be empty in metadata");
709 return BAD_VALUE;
710 }
711 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
712 } else {
713 CLOGE("RequestID does not exist in metadata");
714 return BAD_VALUE;
715 }
716
Jianing Wei90e59c92014-03-12 18:29:36 -0700717 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700718
719 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700720 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700721
722 // Setup batch size if this is a high speed video recording request.
723 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
724 auto firstRequest = requestList->begin();
725 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
726 if (outputStream->isVideoStream()) {
727 (*firstRequest)->mBatchSize = requestList->size();
728 break;
729 }
730 }
731 }
732
Jianing Wei90e59c92014-03-12 18:29:36 -0700733 return OK;
734}
735
Jianing Weicb0652e2014-03-12 18:29:36 -0700736status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800737 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800738
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700739 List<const CameraMetadata> requests;
740 requests.push_back(request);
741 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800742}
743
Jianing Wei90e59c92014-03-12 18:29:36 -0700744status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700745 const List<const CameraMetadata> &requests, bool repeating,
746 /*out*/
747 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700748 ATRACE_CALL();
749 Mutex::Autolock il(mInterfaceLock);
750 Mutex::Autolock l(mLock);
751
752 status_t res = checkStatusOkToCaptureLocked();
753 if (res != OK) {
754 // error logged by previous call
755 return res;
756 }
757
758 RequestList requestList;
759
760 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
761 if (res != OK) {
762 // error logged by previous call
763 return res;
764 }
765
766 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700767 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700768 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700769 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700770 }
771
772 if (res == OK) {
773 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
774 if (res != OK) {
775 SET_ERR_L("Can't transition to active in %f seconds!",
776 kActiveTimeout/1e9);
777 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700778 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
779 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700780 } else {
781 CLOGE("Cannot queue request. Impossible.");
782 return BAD_VALUE;
783 }
784
785 return res;
786}
787
Jianing Weicb0652e2014-03-12 18:29:36 -0700788status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
789 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700790 ATRACE_CALL();
791
Jianing Weicb0652e2014-03-12 18:29:36 -0700792 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700793}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800794
Jianing Weicb0652e2014-03-12 18:29:36 -0700795status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
796 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800797 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800798
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700799 List<const CameraMetadata> requests;
800 requests.push_back(request);
801 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800802}
803
Jianing Weicb0652e2014-03-12 18:29:36 -0700804status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
805 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700806 ATRACE_CALL();
807
Jianing Weicb0652e2014-03-12 18:29:36 -0700808 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700809}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800810
811sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
812 const CameraMetadata &request) {
813 status_t res;
814
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700815 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800816 res = configureStreamsLocked();
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700817 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800818 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700819 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800820 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700821 } else if (mStatus == STATUS_UNCONFIGURED) {
822 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700823 CLOGE("No streams configured");
824 return NULL;
825 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800826 }
827
828 sp<CaptureRequest> newRequest = createCaptureRequest(request);
829 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800830}
831
Jianing Weicb0652e2014-03-12 18:29:36 -0700832status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800833 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700834 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800835 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800836
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800837 switch (mStatus) {
838 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700839 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800840 return INVALID_OPERATION;
841 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700842 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800843 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700844 case STATUS_UNCONFIGURED:
845 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800846 case STATUS_ACTIVE:
847 // OK
848 break;
849 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700850 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800851 return INVALID_OPERATION;
852 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700853 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700854
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700855 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800856}
857
858status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
859 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700860 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800861
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700862 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800863}
864
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700865status_t Camera3Device::createInputStream(
866 uint32_t width, uint32_t height, int format, int *id) {
867 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700868 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700869 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700870 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
871 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700872
873 status_t res;
874 bool wasActive = false;
875
876 switch (mStatus) {
877 case STATUS_ERROR:
878 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
879 return INVALID_OPERATION;
880 case STATUS_UNINITIALIZED:
881 ALOGE("%s: Device not initialized", __FUNCTION__);
882 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700883 case STATUS_UNCONFIGURED:
884 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700885 // OK
886 break;
887 case STATUS_ACTIVE:
888 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700889 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700890 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700891 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700892 return res;
893 }
894 wasActive = true;
895 break;
896 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700897 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700898 return INVALID_OPERATION;
899 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700900 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700901
902 if (mInputStream != 0) {
903 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
904 return INVALID_OPERATION;
905 }
906
907 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
908 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700909 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700910
911 mInputStream = newStream;
912
913 *id = mNextStreamId++;
914
915 // Continue captures if active at start
916 if (wasActive) {
917 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
918 res = configureStreamsLocked();
919 if (res != OK) {
920 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
921 __FUNCTION__, mNextStreamId, strerror(-res), res);
922 return res;
923 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700924 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700925 }
926
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700927 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700928 return OK;
929}
930
Igor Murashkin2fba5842013-04-22 14:03:54 -0700931
932status_t Camera3Device::createZslStream(
933 uint32_t width, uint32_t height,
934 int depth,
935 /*out*/
936 int *id,
937 sp<Camera3ZslStream>* zslStream) {
938 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700939 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700940 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700941 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
942 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700943
944 status_t res;
945 bool wasActive = false;
946
947 switch (mStatus) {
948 case STATUS_ERROR:
949 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
950 return INVALID_OPERATION;
951 case STATUS_UNINITIALIZED:
952 ALOGE("%s: Device not initialized", __FUNCTION__);
953 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700954 case STATUS_UNCONFIGURED:
955 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700956 // OK
957 break;
958 case STATUS_ACTIVE:
959 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700960 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700961 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700962 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700963 return res;
964 }
965 wasActive = true;
966 break;
967 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700968 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700969 return INVALID_OPERATION;
970 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700971 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700972
973 if (mInputStream != 0) {
974 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
975 return INVALID_OPERATION;
976 }
977
978 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
979 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700980 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700981
982 res = mOutputStreams.add(mNextStreamId, newStream);
983 if (res < 0) {
984 ALOGE("%s: Can't add new stream to set: %s (%d)",
985 __FUNCTION__, strerror(-res), res);
986 return res;
987 }
988 mInputStream = newStream;
989
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530990 mNeedConfig = true;
991
Igor Murashkin2fba5842013-04-22 14:03:54 -0700992 *id = mNextStreamId++;
993 *zslStream = newStream;
994
995 // Continue captures if active at start
996 if (wasActive) {
997 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
998 res = configureStreamsLocked();
999 if (res != OK) {
1000 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1001 __FUNCTION__, mNextStreamId, strerror(-res), res);
1002 return res;
1003 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001004 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -07001005 }
1006
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001007 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -07001008 return OK;
1009}
1010
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001011status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001012 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -07001013 camera3_stream_rotation_t rotation, int *id, int streamSetId, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001014 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001015 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001016 Mutex::Autolock l(mLock);
Zhijun He5d677d12016-05-29 16:52:39 -07001017 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
1018 " consumer usage 0x%x", mId, mNextStreamId, width, height, format, dataSpace, rotation,
1019 consumerUsage);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001020
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001021 status_t res;
1022 bool wasActive = false;
1023
1024 switch (mStatus) {
1025 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001026 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001027 return INVALID_OPERATION;
1028 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001029 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001030 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001031 case STATUS_UNCONFIGURED:
1032 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001033 // OK
1034 break;
1035 case STATUS_ACTIVE:
1036 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001037 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001038 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001039 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001040 return res;
1041 }
1042 wasActive = true;
1043 break;
1044 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001045 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001046 return INVALID_OPERATION;
1047 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001048 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001049
1050 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001051 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001052 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001053 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001054 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1055 }
Zhijun He5d677d12016-05-29 16:52:39 -07001056
1057 // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
1058 // which requires a consumer surface to be available.
1059 if (consumer == nullptr && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1060 ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
1061 return BAD_VALUE;
1062 }
1063
1064 if (consumer == nullptr && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1065 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1066 return BAD_VALUE;
1067 }
1068
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001069 // Use legacy dataspace values for older HALs
1070 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1071 dataSpace = mapToLegacyDataspace(dataSpace);
1072 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001073 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001074 ssize_t blobBufferSize;
1075 if (dataSpace != HAL_DATASPACE_DEPTH) {
1076 blobBufferSize = getJpegBufferSize(width, height);
1077 if (blobBufferSize <= 0) {
1078 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1079 return BAD_VALUE;
1080 }
1081 } else {
1082 blobBufferSize = getPointCloudBufferSize();
1083 if (blobBufferSize <= 0) {
1084 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1085 return BAD_VALUE;
1086 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001087 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001088 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001089 width, height, blobBufferSize, format, dataSpace, rotation,
1090 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001091 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1092 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1093 if (rawOpaqueBufferSize <= 0) {
1094 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1095 return BAD_VALUE;
1096 }
1097 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001098 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1099 mTimestampOffset, streamSetId);
Zhijun He5d677d12016-05-29 16:52:39 -07001100 } else if (consumer == nullptr) {
1101 newStream = new Camera3OutputStream(mNextStreamId,
1102 width, height, format, consumerUsage, dataSpace, rotation,
1103 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001104 } else {
1105 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001106 width, height, format, dataSpace, rotation,
1107 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001108 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001109 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001110
Zhijun He125684a2015-12-26 15:07:30 -08001111 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001112 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1113 * requires buffers to be statically allocated for internal static buffer registration, while
1114 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1115 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001116 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001117 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001118 newStream->setBufferManager(mBufferManager);
1119 }
1120
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001121 res = mOutputStreams.add(mNextStreamId, newStream);
1122 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001123 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001124 return res;
1125 }
1126
1127 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001128 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001129
1130 // Continue captures if active at start
1131 if (wasActive) {
1132 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1133 res = configureStreamsLocked();
1134 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001135 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1136 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001137 return res;
1138 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001139 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001140 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001141 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001142 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001143}
1144
1145status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1146 ATRACE_CALL();
1147 (void)outputId; (void)id;
1148
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001149 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001150 return INVALID_OPERATION;
1151}
1152
1153
1154status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001155 uint32_t *width, uint32_t *height,
1156 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001157 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001158 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001159 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001160
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001161 switch (mStatus) {
1162 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001163 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001164 return INVALID_OPERATION;
1165 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001166 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001167 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001168 case STATUS_UNCONFIGURED:
1169 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001170 case STATUS_ACTIVE:
1171 // OK
1172 break;
1173 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001174 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001175 return INVALID_OPERATION;
1176 }
1177
1178 ssize_t idx = mOutputStreams.indexOfKey(id);
1179 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001180 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001181 return idx;
1182 }
1183
1184 if (width) *width = mOutputStreams[idx]->getWidth();
1185 if (height) *height = mOutputStreams[idx]->getHeight();
1186 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001187 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001188 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001189}
1190
1191status_t Camera3Device::setStreamTransform(int id,
1192 int transform) {
1193 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001194 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001195 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001196
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001197 switch (mStatus) {
1198 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001199 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001200 return INVALID_OPERATION;
1201 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001202 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001203 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001204 case STATUS_UNCONFIGURED:
1205 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001206 case STATUS_ACTIVE:
1207 // OK
1208 break;
1209 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001210 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001211 return INVALID_OPERATION;
1212 }
1213
1214 ssize_t idx = mOutputStreams.indexOfKey(id);
1215 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001216 CLOGE("Stream %d does not exist",
1217 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001218 return BAD_VALUE;
1219 }
1220
1221 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001222}
1223
1224status_t Camera3Device::deleteStream(int id) {
1225 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001226 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001227 Mutex::Autolock l(mLock);
1228 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001229
Igor Murashkine2172be2013-05-28 15:31:39 -07001230 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1231
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001232 // CameraDevice semantics require device to already be idle before
1233 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001234 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001235 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1236 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001237 }
1238
Igor Murashkin2fba5842013-04-22 14:03:54 -07001239 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001240 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001241 if (mInputStream != NULL && id == mInputStream->getId()) {
1242 deletedStream = mInputStream;
1243 mInputStream.clear();
1244 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001245 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001246 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001247 return BAD_VALUE;
1248 }
Zhijun He5f446352014-01-22 09:49:33 -08001249 }
1250
1251 // Delete output stream or the output part of a bi-directional stream.
1252 if (outputStreamIdx != NAME_NOT_FOUND) {
1253 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001254 mOutputStreams.removeItem(id);
1255 }
1256
1257 // Free up the stream endpoint so that it can be used by some other stream
1258 res = deletedStream->disconnect();
1259 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001260 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001261 // fall through since we want to still list the stream as deleted.
1262 }
1263 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001264 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001265
1266 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001267}
1268
1269status_t Camera3Device::deleteReprocessStream(int id) {
1270 ATRACE_CALL();
1271 (void)id;
1272
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001273 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001274 return INVALID_OPERATION;
1275}
1276
Zhijun He1fa89992015-06-01 15:44:31 -07001277status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001278 ATRACE_CALL();
1279 ALOGV("%s: E", __FUNCTION__);
1280
1281 Mutex::Autolock il(mInterfaceLock);
1282 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001283
1284 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1285 mNeedConfig = true;
1286 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1287 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001288
1289 return configureStreamsLocked();
1290}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001291
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001292status_t Camera3Device::getInputBufferProducer(
1293 sp<IGraphicBufferProducer> *producer) {
1294 Mutex::Autolock il(mInterfaceLock);
1295 Mutex::Autolock l(mLock);
1296
1297 if (producer == NULL) {
1298 return BAD_VALUE;
1299 } else if (mInputStream == NULL) {
1300 return INVALID_OPERATION;
1301 }
1302
1303 return mInputStream->getInputBufferProducer(producer);
1304}
1305
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001306status_t Camera3Device::createDefaultRequest(int templateId,
1307 CameraMetadata *request) {
1308 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001309 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001310
1311 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1312 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1313 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1314 return BAD_VALUE;
1315 }
1316
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001317 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001318 Mutex::Autolock l(mLock);
1319
1320 switch (mStatus) {
1321 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001322 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001323 return INVALID_OPERATION;
1324 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001325 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001326 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001327 case STATUS_UNCONFIGURED:
1328 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001329 case STATUS_ACTIVE:
1330 // OK
1331 break;
1332 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001333 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001334 return INVALID_OPERATION;
1335 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001336
Zhijun Hea1530f12014-09-14 12:44:20 -07001337 if (!mRequestTemplateCache[templateId].isEmpty()) {
1338 *request = mRequestTemplateCache[templateId];
1339 return OK;
1340 }
1341
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001342 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001343 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001344 rawRequest = mHal3Device->ops->construct_default_request_settings(
1345 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001346 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001347 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001348 ALOGI("%s: template %d is not supported on this camera device",
1349 __FUNCTION__, templateId);
1350 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001351 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001352
Zhijun Hea1530f12014-09-14 12:44:20 -07001353 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001354
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001355 // Derive some new keys for backward compatibility
1356 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
1357 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
1358 int32_t defaultBoost[1] = {100};
1359 mRequestTemplateCache[templateId].update(
1360 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1361 defaultBoost, 1);
1362 }
1363
1364 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001365 return OK;
1366}
1367
1368status_t Camera3Device::waitUntilDrained() {
1369 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001370 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001371 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001372
Zhijun He69a37482014-03-23 18:44:49 -07001373 return waitUntilDrainedLocked();
1374}
1375
1376status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001377 switch (mStatus) {
1378 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001379 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001380 ALOGV("%s: Already idle", __FUNCTION__);
1381 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001382 case STATUS_CONFIGURED:
1383 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001384 case STATUS_ERROR:
1385 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001386 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001387 break;
1388 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001389 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001390 return INVALID_OPERATION;
1391 }
1392
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001393 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1394 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001395 if (res != OK) {
1396 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1397 res);
1398 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001399 return res;
1400}
1401
Ruben Brunk183f0562015-08-12 12:55:02 -07001402
1403void Camera3Device::internalUpdateStatusLocked(Status status) {
1404 mStatus = status;
1405 mRecentStatusUpdates.add(mStatus);
1406 mStatusChanged.broadcast();
1407}
1408
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001409// Pause to reconfigure
1410status_t Camera3Device::internalPauseAndWaitLocked() {
1411 mRequestThread->setPaused(true);
1412 mPauseStateNotify = true;
1413
1414 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1415 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1416 if (res != OK) {
1417 SET_ERR_L("Can't idle device in %f seconds!",
1418 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001419 }
1420
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001421 return res;
1422}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001423
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001424// Resume after internalPauseAndWaitLocked
1425status_t Camera3Device::internalResumeLocked() {
1426 status_t res;
1427
1428 mRequestThread->setPaused(false);
1429
1430 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1431 if (res != OK) {
1432 SET_ERR_L("Can't transition to active in %f seconds!",
1433 kActiveTimeout/1e9);
1434 }
1435 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001436 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001437}
1438
Ruben Brunk183f0562015-08-12 12:55:02 -07001439status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001440 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001441
1442 size_t startIndex = 0;
1443 if (mStatusWaiters == 0) {
1444 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1445 // this status list
1446 mRecentStatusUpdates.clear();
1447 } else {
1448 // If other threads are waiting on updates to this status list, set the position of the
1449 // first element that this list will check rather than clearing the list.
1450 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001451 }
1452
Ruben Brunk183f0562015-08-12 12:55:02 -07001453 mStatusWaiters++;
1454
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001455 bool stateSeen = false;
1456 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001457 if (active == (mStatus == STATUS_ACTIVE)) {
1458 // Desired state is current
1459 break;
1460 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001461
1462 res = mStatusChanged.waitRelative(mLock, timeout);
1463 if (res != OK) break;
1464
Ruben Brunk183f0562015-08-12 12:55:02 -07001465 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1466 // transitions.
1467 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1468 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1469 __FUNCTION__);
1470
1471 // Encountered desired state since we began waiting
1472 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001473 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1474 stateSeen = true;
1475 break;
1476 }
1477 }
1478 } while (!stateSeen);
1479
Ruben Brunk183f0562015-08-12 12:55:02 -07001480 mStatusWaiters--;
1481
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001482 return res;
1483}
1484
1485
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001486status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001487 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001488 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001489
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001490 if (listener != NULL && mListener != NULL) {
1491 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1492 }
1493 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001494 mRequestThread->setNotificationListener(listener);
1495 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001496
1497 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001498}
1499
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001500bool Camera3Device::willNotify3A() {
1501 return false;
1502}
1503
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001504status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001505 status_t res;
1506 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001507
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001508 while (mResultQueue.empty()) {
1509 res = mResultSignal.waitRelative(mOutputLock, timeout);
1510 if (res == TIMED_OUT) {
1511 return res;
1512 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001513 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001514 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001515 return res;
1516 }
1517 }
1518 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001519}
1520
Jianing Weicb0652e2014-03-12 18:29:36 -07001521status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001522 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001523 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001524
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001525 if (mResultQueue.empty()) {
1526 return NOT_ENOUGH_DATA;
1527 }
1528
Jianing Weicb0652e2014-03-12 18:29:36 -07001529 if (frame == NULL) {
1530 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1531 return BAD_VALUE;
1532 }
1533
1534 CaptureResult &result = *(mResultQueue.begin());
1535 frame->mResultExtras = result.mResultExtras;
1536 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001537 mResultQueue.erase(mResultQueue.begin());
1538
1539 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001540}
1541
1542status_t Camera3Device::triggerAutofocus(uint32_t id) {
1543 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001544 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001545
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001546 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1547 // Mix-in this trigger into the next request and only the next request.
1548 RequestTrigger trigger[] = {
1549 {
1550 ANDROID_CONTROL_AF_TRIGGER,
1551 ANDROID_CONTROL_AF_TRIGGER_START
1552 },
1553 {
1554 ANDROID_CONTROL_AF_TRIGGER_ID,
1555 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001556 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001557 };
1558
1559 return mRequestThread->queueTrigger(trigger,
1560 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001561}
1562
1563status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1564 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001565 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001566
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001567 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1568 // Mix-in this trigger into the next request and only the next request.
1569 RequestTrigger trigger[] = {
1570 {
1571 ANDROID_CONTROL_AF_TRIGGER,
1572 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1573 },
1574 {
1575 ANDROID_CONTROL_AF_TRIGGER_ID,
1576 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001577 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001578 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001579
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001580 return mRequestThread->queueTrigger(trigger,
1581 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001582}
1583
1584status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1585 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001586 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001587
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001588 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1589 // Mix-in this trigger into the next request and only the next request.
1590 RequestTrigger trigger[] = {
1591 {
1592 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1593 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1594 },
1595 {
1596 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1597 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001598 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001599 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001600
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001601 return mRequestThread->queueTrigger(trigger,
1602 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001603}
1604
1605status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1606 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1607 ATRACE_CALL();
1608 (void)reprocessStreamId; (void)buffer; (void)listener;
1609
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001610 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001611 return INVALID_OPERATION;
1612}
1613
Jianing Weicb0652e2014-03-12 18:29:36 -07001614status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001615 ATRACE_CALL();
1616 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001617 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001618
Zhijun He7ef20392014-04-21 16:04:17 -07001619 {
1620 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001621 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001622 }
1623
Zhijun He491e3412013-12-27 10:57:44 -08001624 status_t res;
1625 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001626 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001627 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001628 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001629 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001630 }
1631
1632 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001633}
1634
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001635status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001636 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1637}
1638
1639status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001640 ATRACE_CALL();
1641 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001642 Mutex::Autolock il(mInterfaceLock);
1643 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001644
1645 sp<Camera3StreamInterface> stream;
1646 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1647 if (outputStreamIdx == NAME_NOT_FOUND) {
1648 CLOGE("Stream %d does not exist", streamId);
1649 return BAD_VALUE;
1650 }
1651
1652 stream = mOutputStreams.editValueAt(outputStreamIdx);
1653
1654 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001655 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001656 return BAD_VALUE;
1657 }
1658
1659 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001660 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001661 return BAD_VALUE;
1662 }
1663
Ruben Brunkc78ac262015-08-13 17:58:46 -07001664 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001665}
1666
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001667status_t Camera3Device::tearDown(int streamId) {
1668 ATRACE_CALL();
1669 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1670 Mutex::Autolock il(mInterfaceLock);
1671 Mutex::Autolock l(mLock);
1672
1673 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1674 // since we cannot call register_stream_buffers except right after configure_streams.
1675 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1676 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1677 __FUNCTION__, mHal3Device->common.version);
1678 return NO_INIT;
1679 }
1680
1681 sp<Camera3StreamInterface> stream;
1682 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1683 if (outputStreamIdx == NAME_NOT_FOUND) {
1684 CLOGE("Stream %d does not exist", streamId);
1685 return BAD_VALUE;
1686 }
1687
1688 stream = mOutputStreams.editValueAt(outputStreamIdx);
1689
1690 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1691 CLOGE("Stream %d is a target of a in-progress request", streamId);
1692 return BAD_VALUE;
1693 }
1694
1695 return stream->tearDown();
1696}
1697
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001698status_t Camera3Device::addBufferListenerForStream(int streamId,
1699 wp<Camera3StreamBufferListener> listener) {
1700 ATRACE_CALL();
1701 ALOGV("%s: Camera %d: Adding buffer listener for stream %d", __FUNCTION__, mId, streamId);
1702 Mutex::Autolock il(mInterfaceLock);
1703 Mutex::Autolock l(mLock);
1704
1705 sp<Camera3StreamInterface> stream;
1706 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1707 if (outputStreamIdx == NAME_NOT_FOUND) {
1708 CLOGE("Stream %d does not exist", streamId);
1709 return BAD_VALUE;
1710 }
1711
1712 stream = mOutputStreams.editValueAt(outputStreamIdx);
1713 stream->addBufferListener(listener);
1714
1715 return OK;
1716}
1717
Zhijun He204e3292014-07-14 17:09:23 -07001718uint32_t Camera3Device::getDeviceVersion() {
1719 ATRACE_CALL();
1720 Mutex::Autolock il(mInterfaceLock);
1721 return mDeviceVersion;
1722}
1723
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001724/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001725 * Methods called by subclasses
1726 */
1727
1728void Camera3Device::notifyStatus(bool idle) {
1729 {
1730 // Need mLock to safely update state and synchronize to current
1731 // state of methods in flight.
1732 Mutex::Autolock l(mLock);
1733 // We can get various system-idle notices from the status tracker
1734 // while starting up. Only care about them if we've actually sent
1735 // in some requests recently.
1736 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1737 return;
1738 }
1739 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1740 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001741 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001742
1743 // Skip notifying listener if we're doing some user-transparent
1744 // state changes
1745 if (mPauseStateNotify) return;
1746 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001747
1748 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001749 {
1750 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001751 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001752 }
1753 if (idle && listener != NULL) {
1754 listener->notifyIdle();
1755 }
1756}
1757
Zhijun He5d677d12016-05-29 16:52:39 -07001758status_t Camera3Device::setConsumerSurface(int streamId, sp<Surface> consumer) {
1759 ATRACE_CALL();
1760 ALOGV("%s: Camera %d: set consumer surface for stream %d", __FUNCTION__, mId, streamId);
1761 Mutex::Autolock il(mInterfaceLock);
1762 Mutex::Autolock l(mLock);
1763
1764 if (consumer == nullptr) {
1765 CLOGE("Null consumer is passed!");
1766 return BAD_VALUE;
1767 }
1768
1769 ssize_t idx = mOutputStreams.indexOfKey(streamId);
1770 if (idx == NAME_NOT_FOUND) {
1771 CLOGE("Stream %d is unknown", streamId);
1772 return idx;
1773 }
1774 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
1775 status_t res = stream->setConsumer(consumer);
1776 if (res != OK) {
1777 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1778 return res;
1779 }
1780
1781 if (!stream->isConfiguring()) {
1782 CLOGE("Stream %d was already fully configured.", streamId);
1783 return INVALID_OPERATION;
1784 }
1785
1786 res = stream->finishConfiguration(mHal3Device);
1787 if (res != OK) {
1788 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1789 stream->getId(), strerror(-res), res);
1790 return res;
1791 }
1792
1793 return OK;
1794}
1795
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001796/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001797 * Camera3Device private methods
1798 */
1799
1800sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1801 const CameraMetadata &request) {
1802 ATRACE_CALL();
1803 status_t res;
1804
1805 sp<CaptureRequest> newRequest = new CaptureRequest;
1806 newRequest->mSettings = request;
1807
1808 camera_metadata_entry_t inputStreams =
1809 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1810 if (inputStreams.count > 0) {
1811 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001812 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001813 CLOGE("Request references unknown input stream %d",
1814 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001815 return NULL;
1816 }
1817 // Lazy completion of stream configuration (allocation/registration)
1818 // on first use
1819 if (mInputStream->isConfiguring()) {
1820 res = mInputStream->finishConfiguration(mHal3Device);
1821 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001822 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001823 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001824 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001825 return NULL;
1826 }
1827 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001828 // Check if stream is being prepared
1829 if (mInputStream->isPreparing()) {
1830 CLOGE("Request references an input stream that's being prepared!");
1831 return NULL;
1832 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001833
1834 newRequest->mInputStream = mInputStream;
1835 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1836 }
1837
1838 camera_metadata_entry_t streams =
1839 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1840 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001841 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001842 return NULL;
1843 }
1844
1845 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001846 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001847 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001848 CLOGE("Request references unknown stream %d",
1849 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001850 return NULL;
1851 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001852 sp<Camera3OutputStreamInterface> stream =
1853 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001854
Zhijun He5d677d12016-05-29 16:52:39 -07001855 // It is illegal to include a deferred consumer output stream into a request
1856 if (stream->isConsumerConfigurationDeferred()) {
1857 CLOGE("Stream %d hasn't finished configuration yet due to deferred consumer",
1858 stream->getId());
1859 return NULL;
1860 }
1861
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001862 // Lazy completion of stream configuration (allocation/registration)
1863 // on first use
1864 if (stream->isConfiguring()) {
1865 res = stream->finishConfiguration(mHal3Device);
1866 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001867 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1868 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001869 return NULL;
1870 }
1871 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001872 // Check if stream is being prepared
1873 if (stream->isPreparing()) {
1874 CLOGE("Request references an output stream that's being prepared!");
1875 return NULL;
1876 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001877
1878 newRequest->mOutputStreams.push(stream);
1879 }
1880 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001881 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001882
1883 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001884}
1885
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001886bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1887 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1888 Size size = mSupportedOpaqueInputSizes[i];
1889 if (size.width == width && size.height == height) {
1890 return true;
1891 }
1892 }
1893
1894 return false;
1895}
1896
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001897void Camera3Device::cancelStreamsConfigurationLocked() {
1898 int res = OK;
1899 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1900 res = mInputStream->cancelConfiguration();
1901 if (res != OK) {
1902 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
1903 mInputStream->getId(), strerror(-res), res);
1904 }
1905 }
1906
1907 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1908 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
1909 if (outputStream->isConfiguring()) {
1910 res = outputStream->cancelConfiguration();
1911 if (res != OK) {
1912 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
1913 outputStream->getId(), strerror(-res), res);
1914 }
1915 }
1916 }
1917
1918 // Return state to that at start of call, so that future configures
1919 // properly clean things up
1920 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
1921 mNeedConfig = true;
1922}
1923
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001924status_t Camera3Device::configureStreamsLocked() {
1925 ATRACE_CALL();
1926 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001927
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001928 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001929 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001930 return INVALID_OPERATION;
1931 }
1932
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001933 if (!mNeedConfig) {
1934 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1935 return OK;
1936 }
1937
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001938 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1939 // adding a dummy stream instead.
1940 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1941 if (mOutputStreams.size() == 0) {
1942 addDummyStreamLocked();
1943 } else {
1944 tryRemoveDummyStreamLocked();
1945 }
1946
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001947 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001948 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001949
1950 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001951 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1952 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1953 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001954 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1955
1956 Vector<camera3_stream_t*> streams;
1957 streams.setCapacity(config.num_streams);
1958
1959 if (mInputStream != NULL) {
1960 camera3_stream_t *inputStream;
1961 inputStream = mInputStream->startConfiguration();
1962 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001963 CLOGE("Can't start input stream configuration");
1964 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001965 return INVALID_OPERATION;
1966 }
1967 streams.add(inputStream);
1968 }
1969
1970 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001971
1972 // Don't configure bidi streams twice, nor add them twice to the list
1973 if (mOutputStreams[i].get() ==
1974 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1975
1976 config.num_streams--;
1977 continue;
1978 }
1979
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001980 camera3_stream_t *outputStream;
1981 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1982 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001983 CLOGE("Can't start output stream configuration");
1984 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001985 return INVALID_OPERATION;
1986 }
1987 streams.add(outputStream);
1988 }
1989
1990 config.streams = streams.editArray();
1991
1992 // Do the HAL configuration; will potentially touch stream
1993 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001994 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001995 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001996 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001997
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001998 if (res == BAD_VALUE) {
1999 // HAL rejected this set of streams as unsupported, clean up config
2000 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002001 CLOGE("Set of requested inputs/outputs not supported by HAL");
2002 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002003 return BAD_VALUE;
2004 } else if (res != OK) {
2005 // Some other kind of error from configure_streams - this is not
2006 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002007 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2008 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002009 return res;
2010 }
2011
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002012 // Finish all stream configuration immediately.
2013 // TODO: Try to relax this later back to lazy completion, which should be
2014 // faster
2015
Igor Murashkin073f8572013-05-02 14:59:28 -07002016 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002017 res = mInputStream->finishConfiguration(mHal3Device);
2018 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002019 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002020 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002021 cancelStreamsConfigurationLocked();
2022 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002023 }
2024 }
2025
2026 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002027 sp<Camera3OutputStreamInterface> outputStream =
2028 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002029 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002030 res = outputStream->finishConfiguration(mHal3Device);
2031 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002032 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002033 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002034 cancelStreamsConfigurationLocked();
2035 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002036 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002037 }
2038 }
2039
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002040 // Request thread needs to know to avoid using repeat-last-settings protocol
2041 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002042 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002043
Zhijun He90f7c372016-08-16 16:19:43 -07002044 char value[PROPERTY_VALUE_MAX];
2045 property_get("camera.fifo.disable", value, "0");
2046 int32_t disableFifo = atoi(value);
2047 if (disableFifo != 1) {
2048 // Boost priority of request thread to SCHED_FIFO.
2049 pid_t requestThreadTid = mRequestThread->getTid();
2050 res = requestPriority(getpid(), requestThreadTid,
2051 kRequestThreadPriority, /*asynchronous*/ false);
2052 if (res != OK) {
2053 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2054 strerror(-res), res);
2055 } else {
2056 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2057 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002058 }
2059
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002060 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002061
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002062 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002063
Ruben Brunk183f0562015-08-12 12:55:02 -07002064 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2065 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002066
2067 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
2068
Zhijun He0a210512014-07-24 13:45:15 -07002069 // tear down the deleted streams after configure streams.
2070 mDeletedStreams.clear();
2071
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002072 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002073}
2074
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002075status_t Camera3Device::addDummyStreamLocked() {
2076 ATRACE_CALL();
2077 status_t res;
2078
2079 if (mDummyStreamId != NO_STREAM) {
2080 // Should never be adding a second dummy stream when one is already
2081 // active
2082 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
2083 __FUNCTION__, mId);
2084 return INVALID_OPERATION;
2085 }
2086
2087 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
2088
2089 sp<Camera3OutputStreamInterface> dummyStream =
2090 new Camera3DummyStream(mNextStreamId);
2091
2092 res = mOutputStreams.add(mNextStreamId, dummyStream);
2093 if (res < 0) {
2094 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2095 return res;
2096 }
2097
2098 mDummyStreamId = mNextStreamId;
2099 mNextStreamId++;
2100
2101 return OK;
2102}
2103
2104status_t Camera3Device::tryRemoveDummyStreamLocked() {
2105 ATRACE_CALL();
2106 status_t res;
2107
2108 if (mDummyStreamId == NO_STREAM) return OK;
2109 if (mOutputStreams.size() == 1) return OK;
2110
2111 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
2112
2113 // Ok, have a dummy stream and there's at least one other output stream,
2114 // so remove the dummy
2115
2116 sp<Camera3StreamInterface> deletedStream;
2117 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2118 if (outputStreamIdx == NAME_NOT_FOUND) {
2119 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2120 return INVALID_OPERATION;
2121 }
2122
2123 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2124 mOutputStreams.removeItemsAt(outputStreamIdx);
2125
2126 // Free up the stream endpoint so that it can be used by some other stream
2127 res = deletedStream->disconnect();
2128 if (res != OK) {
2129 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2130 // fall through since we want to still list the stream as deleted.
2131 }
2132 mDeletedStreams.add(deletedStream);
2133 mDummyStreamId = NO_STREAM;
2134
2135 return res;
2136}
2137
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002138void Camera3Device::setErrorState(const char *fmt, ...) {
2139 Mutex::Autolock l(mLock);
2140 va_list args;
2141 va_start(args, fmt);
2142
2143 setErrorStateLockedV(fmt, args);
2144
2145 va_end(args);
2146}
2147
2148void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2149 Mutex::Autolock l(mLock);
2150 setErrorStateLockedV(fmt, args);
2151}
2152
2153void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2154 va_list args;
2155 va_start(args, fmt);
2156
2157 setErrorStateLockedV(fmt, args);
2158
2159 va_end(args);
2160}
2161
2162void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002163 // Print out all error messages to log
2164 String8 errorCause = String8::formatV(fmt, args);
2165 ALOGE("Camera %d: %s", mId, errorCause.string());
2166
2167 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002168 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002169
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002170 mErrorCause = errorCause;
2171
2172 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002173 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002174
2175 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002176 sp<NotificationListener> listener = mListener.promote();
2177 if (listener != NULL) {
2178 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002179 CaptureResultExtras());
2180 }
2181
2182 // Save stack trace. View by dumping it later.
2183 CameraTraces::saveTrace();
2184 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002185}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002186
2187/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002188 * In-flight request management
2189 */
2190
Jianing Weicb0652e2014-03-12 18:29:36 -07002191status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002192 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2193 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002194 ATRACE_CALL();
2195 Mutex::Autolock l(mInFlightLock);
2196
2197 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002198 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2199 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002200 if (res < 0) return res;
2201
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002202 if (mInFlightMap.size() == 1) {
2203 mStatusTracker->markComponentActive(mInFlightStatusId);
2204 }
2205
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002206 return OK;
2207}
2208
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002209void Camera3Device::returnOutputBuffers(
2210 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2211 nsecs_t timestamp) {
2212 for (size_t i = 0; i < numBuffers; i++)
2213 {
2214 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2215 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2216 // Note: stream may be deallocated at this point, if this buffer was
2217 // the last reference to it.
2218 if (res != OK) {
2219 ALOGE("Can't return buffer to its stream: %s (%d)",
2220 strerror(-res), res);
2221 }
2222 }
2223}
2224
2225
2226void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2227
2228 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2229 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2230
2231 nsecs_t sensorTimestamp = request.sensorTimestamp;
2232 nsecs_t shutterTimestamp = request.shutterTimestamp;
2233
2234 // Check if it's okay to remove the request from InFlightMap:
2235 // In the case of a successful request:
2236 // all input and output buffers, all result metadata, shutter callback
2237 // arrived.
2238 // In the case of a unsuccessful request:
2239 // all input and output buffers arrived.
2240 if (request.numBuffersLeft == 0 &&
2241 (request.requestStatus != OK ||
2242 (request.haveResultMetadata && shutterTimestamp != 0))) {
2243 ATRACE_ASYNC_END("frame capture", frameNumber);
2244
2245 // Sanity check - if sensor timestamp matches shutter timestamp
2246 if (request.requestStatus == OK &&
2247 sensorTimestamp != shutterTimestamp) {
2248 SET_ERR("sensor timestamp (%" PRId64
2249 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2250 sensorTimestamp, frameNumber, shutterTimestamp);
2251 }
2252
2253 // for an unsuccessful request, it may have pending output buffers to
2254 // return.
2255 assert(request.requestStatus != OK ||
2256 request.pendingOutputBuffers.size() == 0);
2257 returnOutputBuffers(request.pendingOutputBuffers.array(),
2258 request.pendingOutputBuffers.size(), 0);
2259
2260 mInFlightMap.removeItemsAt(idx, 1);
2261
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002262 // Indicate idle inFlightMap to the status tracker
2263 if (mInFlightMap.size() == 0) {
2264 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2265 }
2266
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002267 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2268 }
2269
2270 // Sanity check - if we have too many in-flight frames, something has
2271 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002272 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002273 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002274 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2275 kInFlightWarnLimitHighSpeed) {
2276 CLOGE("In-flight list too large for high speed configuration: %zu",
2277 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002278 }
2279}
2280
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002281void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2282 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2283 if (result == nullptr) return;
2284
2285 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2286 (int32_t*)&frameNumber, 1) != OK) {
2287 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2288 return;
2289 }
2290
2291 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2292 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2293 return;
2294 }
2295
2296 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2297
2298 // Valid result, insert into queue
2299 List<CaptureResult>::iterator queuedResult =
2300 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2301 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2302 ", burstId = %" PRId32, __FUNCTION__,
2303 queuedResult->mResultExtras.requestId,
2304 queuedResult->mResultExtras.frameNumber,
2305 queuedResult->mResultExtras.burstId);
2306
2307 mResultSignal.signal();
2308}
2309
2310
2311void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2312 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2313 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2314 Mutex::Autolock l(mOutputLock);
2315
2316 CaptureResult captureResult;
2317 captureResult.mResultExtras = resultExtras;
2318 captureResult.mMetadata = partialResult;
2319
2320 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2321}
2322
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002323
2324void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2325 CaptureResultExtras &resultExtras,
2326 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002327 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002328 bool reprocess,
2329 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002330 if (pendingMetadata.isEmpty())
2331 return;
2332
2333 Mutex::Autolock l(mOutputLock);
2334
2335 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002336 if (reprocess) {
2337 if (frameNumber < mNextReprocessResultFrameNumber) {
2338 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002339 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002340 frameNumber, mNextReprocessResultFrameNumber);
2341 return;
2342 }
2343 mNextReprocessResultFrameNumber = frameNumber + 1;
2344 } else {
2345 if (frameNumber < mNextResultFrameNumber) {
2346 SET_ERR("Out-of-order capture result metadata submitted! "
2347 "(got frame number %d, expecting %d)",
2348 frameNumber, mNextResultFrameNumber);
2349 return;
2350 }
2351 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002352 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002353
2354 CaptureResult captureResult;
2355 captureResult.mResultExtras = resultExtras;
2356 captureResult.mMetadata = pendingMetadata;
2357
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002358 // Append any previous partials to form a complete result
2359 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2360 captureResult.mMetadata.append(collectedPartialResult);
2361 }
2362
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07002363 // Derive some new keys for backward compaibility
2364 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
2365 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
2366 int32_t defaultBoost[1] = {100};
2367 captureResult.mMetadata.update(
2368 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
2369 defaultBoost, 1);
2370 }
2371
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002372 captureResult.mMetadata.sort();
2373
2374 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002375 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2376 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002377 SET_ERR("No timestamp provided by HAL for frame %d!",
2378 frameNumber);
2379 return;
2380 }
2381
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002382 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2383 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2384
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002385 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002386}
2387
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002388/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002389 * Camera HAL device callback methods
2390 */
2391
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002392void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002393 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002394
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002395 status_t res;
2396
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002397 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002398 if (result->result == NULL && result->num_output_buffers == 0 &&
2399 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002400 SET_ERR("No result data provided by HAL for frame %d",
2401 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002402 return;
2403 }
Zhijun He204e3292014-07-14 17:09:23 -07002404
2405 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2406 // partial_result to 1 when metadata is included in this result.
2407 if (!mUsePartialResult &&
2408 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2409 result->result != NULL &&
2410 result->partial_result != 1) {
2411 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2412 " if partial result is not supported",
2413 frameNumber, result->partial_result);
2414 return;
2415 }
2416
2417 bool isPartialResult = false;
2418 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002419 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002420 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002421
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002422 // Get shutter timestamp and resultExtras from list of in-flight requests,
2423 // where it was added by the shutter notification for this frame. If the
2424 // shutter timestamp isn't received yet, append the output buffers to the
2425 // in-flight request and they will be returned when the shutter timestamp
2426 // arrives. Update the in-flight status and remove the in-flight entry if
2427 // all result data and shutter timestamp have been received.
2428 nsecs_t shutterTimestamp = 0;
2429
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002430 {
2431 Mutex::Autolock l(mInFlightLock);
2432 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2433 if (idx == NAME_NOT_FOUND) {
2434 SET_ERR("Unknown frame number for capture result: %d",
2435 frameNumber);
2436 return;
2437 }
2438 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002439 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2440 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2441 ", partialResultCount = %d",
2442 __FUNCTION__, request.resultExtras.requestId,
2443 request.resultExtras.frameNumber, request.resultExtras.burstId,
2444 result->partial_result);
2445 // Always update the partial count to the latest one if it's not 0
2446 // (buffers only). When framework aggregates adjacent partial results
2447 // into one, the latest partial count will be used.
2448 if (result->partial_result != 0)
2449 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002450
2451 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002452 if (mUsePartialResult && result->result != NULL) {
2453 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2454 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2455 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2456 " the range of [1, %d] when metadata is included in the result",
2457 frameNumber, result->partial_result, mNumPartialResults);
2458 return;
2459 }
2460 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002461 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002462 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002463 }
Zhijun He204e3292014-07-14 17:09:23 -07002464 } else {
2465 camera_metadata_ro_entry_t partialResultEntry;
2466 res = find_camera_metadata_ro_entry(result->result,
2467 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2468 if (res != NAME_NOT_FOUND &&
2469 partialResultEntry.count > 0 &&
2470 partialResultEntry.data.u8[0] ==
2471 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2472 // A partial result. Flag this as such, and collect this
2473 // set of metadata into the in-flight entry.
2474 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002475 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002476 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002477 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002478 ANDROID_QUIRKS_PARTIAL_RESULT);
2479 }
2480 }
2481
2482 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002483 // Send partial capture result
2484 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2485 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002486 }
2487 }
2488
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002489 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002490 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002491
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002492 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002493 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002494 if (request.haveResultMetadata) {
2495 SET_ERR("Called multiple times with metadata for frame %d",
2496 frameNumber);
2497 return;
2498 }
Zhijun He204e3292014-07-14 17:09:23 -07002499 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002500 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002501 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002502 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002503 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002504 request.haveResultMetadata = true;
2505 }
2506
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002507 uint32_t numBuffersReturned = result->num_output_buffers;
2508 if (result->input_buffer != NULL) {
2509 if (hasInputBufferInRequest) {
2510 numBuffersReturned += 1;
2511 } else {
2512 ALOGW("%s: Input buffer should be NULL if there is no input"
2513 " buffer sent in the request",
2514 __FUNCTION__);
2515 }
2516 }
2517 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002518 if (request.numBuffersLeft < 0) {
2519 SET_ERR("Too many buffers returned for frame %d",
2520 frameNumber);
2521 return;
2522 }
2523
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002524 camera_metadata_ro_entry_t entry;
2525 res = find_camera_metadata_ro_entry(result->result,
2526 ANDROID_SENSOR_TIMESTAMP, &entry);
2527 if (res == OK && entry.count == 1) {
2528 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002529 }
2530
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002531 // If shutter event isn't received yet, append the output buffers to
2532 // the in-flight request. Otherwise, return the output buffers to
2533 // streams.
2534 if (shutterTimestamp == 0) {
2535 request.pendingOutputBuffers.appendArray(result->output_buffers,
2536 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002537 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002538 returnOutputBuffers(result->output_buffers,
2539 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002540 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002541
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002542 if (result->result != NULL && !isPartialResult) {
2543 if (shutterTimestamp == 0) {
2544 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002545 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002546 } else {
2547 CameraMetadata metadata;
2548 metadata = result->result;
2549 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002550 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2551 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002552 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002553 }
2554
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002555 removeInFlightRequestIfReadyLocked(idx);
2556 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002557
Zhijun Hef0d962a2014-06-30 10:24:11 -07002558 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002559 if (hasInputBufferInRequest) {
2560 Camera3Stream *stream =
2561 Camera3Stream::cast(result->input_buffer->stream);
2562 res = stream->returnInputBuffer(*(result->input_buffer));
2563 // Note: stream may be deallocated at this point, if this buffer was the
2564 // last reference to it.
2565 if (res != OK) {
2566 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2567 " its stream:%s (%d)", __FUNCTION__,
2568 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002569 }
2570 } else {
2571 ALOGW("%s: Input buffer should be NULL if there is no input"
2572 " buffer sent in the request, skipping input buffer return.",
2573 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002574 }
2575 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002576}
2577
2578void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002579 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002580 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002581 {
2582 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002583 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002584 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002585
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002586 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002587 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002588 return;
2589 }
2590
2591 switch (msg->type) {
2592 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002593 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002594 break;
2595 }
2596 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002597 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002598 break;
2599 }
2600 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002601 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002602 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002603 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002604}
2605
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002606void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002607 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002608
2609 // Map camera HAL error codes to ICameraDeviceCallback error codes
2610 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002611 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002612 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002613 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002614 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002615 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002616 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002617 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002618 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002619 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002620 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002621 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002622 };
2623
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002624 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002625 ((msg.error_code >= 0) &&
2626 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2627 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002628 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002629
2630 int streamId = 0;
2631 if (msg.error_stream != NULL) {
2632 Camera3Stream *stream =
2633 Camera3Stream::cast(msg.error_stream);
2634 streamId = stream->getId();
2635 }
2636 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2637 mId, __FUNCTION__, msg.frame_number,
2638 streamId, msg.error_code);
2639
2640 CaptureResultExtras resultExtras;
2641 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002642 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002643 // SET_ERR calls notifyError
2644 SET_ERR("Camera HAL reported serious device error");
2645 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002646 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2647 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2648 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002649 {
2650 Mutex::Autolock l(mInFlightLock);
2651 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2652 if (idx >= 0) {
2653 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2654 r.requestStatus = msg.error_code;
2655 resultExtras = r.resultExtras;
2656 } else {
2657 resultExtras.frameNumber = msg.frame_number;
2658 ALOGE("Camera %d: %s: cannot find in-flight request on "
2659 "frame %" PRId64 " error", mId, __FUNCTION__,
2660 resultExtras.frameNumber);
2661 }
2662 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002663 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002664 if (listener != NULL) {
2665 listener->notifyError(errorCode, resultExtras);
2666 } else {
2667 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2668 }
2669 break;
2670 default:
2671 // SET_ERR calls notifyError
2672 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2673 break;
2674 }
2675}
2676
2677void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002678 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002679 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002680
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002681 // Set timestamp for the request in the in-flight tracking
2682 // and get the request ID to send upstream
2683 {
2684 Mutex::Autolock l(mInFlightLock);
2685 idx = mInFlightMap.indexOfKey(msg.frame_number);
2686 if (idx >= 0) {
2687 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002688
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002689 // Verify ordering of shutter notifications
2690 {
2691 Mutex::Autolock l(mOutputLock);
2692 // TODO: need to track errors for tighter bounds on expected frame number.
2693 if (r.hasInputBuffer) {
2694 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2695 SET_ERR("Shutter notification out-of-order. Expected "
2696 "notification for frame %d, got frame %d",
2697 mNextReprocessShutterFrameNumber, msg.frame_number);
2698 return;
2699 }
2700 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2701 } else {
2702 if (msg.frame_number < mNextShutterFrameNumber) {
2703 SET_ERR("Shutter notification out-of-order. Expected "
2704 "notification for frame %d, got frame %d",
2705 mNextShutterFrameNumber, msg.frame_number);
2706 return;
2707 }
2708 mNextShutterFrameNumber = msg.frame_number + 1;
2709 }
2710 }
2711
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002712 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2713 mId, __FUNCTION__,
2714 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2715 // Call listener, if any
2716 if (listener != NULL) {
2717 listener->notifyShutter(r.resultExtras, msg.timestamp);
2718 }
2719
2720 r.shutterTimestamp = msg.timestamp;
2721
2722 // send pending result and buffers
2723 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002724 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002725 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002726 returnOutputBuffers(r.pendingOutputBuffers.array(),
2727 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2728 r.pendingOutputBuffers.clear();
2729
2730 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002731 }
2732 }
2733 if (idx < 0) {
2734 SET_ERR("Shutter notification for non-existent frame number %d",
2735 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002736 }
2737}
2738
2739
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002740CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002741 ALOGV("%s", __FUNCTION__);
2742
Igor Murashkin1e479c02013-09-06 16:55:14 -07002743 CameraMetadata retVal;
2744
2745 if (mRequestThread != NULL) {
2746 retVal = mRequestThread->getLatestRequest();
2747 }
2748
Igor Murashkin1e479c02013-09-06 16:55:14 -07002749 return retVal;
2750}
2751
Jianing Weicb0652e2014-03-12 18:29:36 -07002752
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002753void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
2754 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
2755 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
2756}
2757
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002758/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002759 * RequestThread inner class methods
2760 */
2761
2762Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002763 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002764 camera3_device_t *hal3Device,
2765 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002766 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002767 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002768 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002769 mHal3Device(hal3Device),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002770 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002771 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002772 mReconfigured(false),
2773 mDoPause(false),
2774 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002775 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002776 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002777 mCurrentAfTriggerId(0),
2778 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002779 mRepeatingLastFrameNumber(
2780 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002781 mAeLockAvailable(aeLockAvailable),
2782 mPrepareVideoStream(false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002783 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002784}
2785
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002786void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002787 wp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002788 Mutex::Autolock l(mRequestLock);
2789 mListener = listener;
2790}
2791
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002792void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002793 Mutex::Autolock l(mRequestLock);
2794 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002795 // Prepare video stream for high speed recording.
2796 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002797}
2798
Jianing Wei90e59c92014-03-12 18:29:36 -07002799status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002800 List<sp<CaptureRequest> > &requests,
2801 /*out*/
2802 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002803 Mutex::Autolock l(mRequestLock);
2804 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2805 ++it) {
2806 mRequestQueue.push_back(*it);
2807 }
2808
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002809 if (lastFrameNumber != NULL) {
2810 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2811 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2812 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2813 *lastFrameNumber);
2814 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002815
Jianing Wei90e59c92014-03-12 18:29:36 -07002816 unpauseForNewRequests();
2817
2818 return OK;
2819}
2820
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002821
2822status_t Camera3Device::RequestThread::queueTrigger(
2823 RequestTrigger trigger[],
2824 size_t count) {
2825
2826 Mutex::Autolock l(mTriggerMutex);
2827 status_t ret;
2828
2829 for (size_t i = 0; i < count; ++i) {
2830 ret = queueTriggerLocked(trigger[i]);
2831
2832 if (ret != OK) {
2833 return ret;
2834 }
2835 }
2836
2837 return OK;
2838}
2839
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002840int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2841 sp<Camera3Device> d = device.promote();
2842 if (d != NULL) return d->mId;
2843 return 0;
2844}
2845
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002846status_t Camera3Device::RequestThread::queueTriggerLocked(
2847 RequestTrigger trigger) {
2848
2849 uint32_t tag = trigger.metadataTag;
2850 ssize_t index = mTriggerMap.indexOfKey(tag);
2851
2852 switch (trigger.getTagType()) {
2853 case TYPE_BYTE:
2854 // fall-through
2855 case TYPE_INT32:
2856 break;
2857 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002858 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2859 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002860 return INVALID_OPERATION;
2861 }
2862
2863 /**
2864 * Collect only the latest trigger, since we only have 1 field
2865 * in the request settings per trigger tag, and can't send more than 1
2866 * trigger per request.
2867 */
2868 if (index != NAME_NOT_FOUND) {
2869 mTriggerMap.editValueAt(index) = trigger;
2870 } else {
2871 mTriggerMap.add(tag, trigger);
2872 }
2873
2874 return OK;
2875}
2876
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002877status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002878 const RequestList &requests,
2879 /*out*/
2880 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002881 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002882 if (lastFrameNumber != NULL) {
2883 *lastFrameNumber = mRepeatingLastFrameNumber;
2884 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002885 mRepeatingRequests.clear();
2886 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2887 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002888
2889 unpauseForNewRequests();
2890
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002891 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002892 return OK;
2893}
2894
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07002895bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002896 if (mRepeatingRequests.empty()) {
2897 return false;
2898 }
2899 int32_t requestId = requestIn->mResultExtras.requestId;
2900 const RequestList &repeatRequests = mRepeatingRequests;
2901 // All repeating requests are guaranteed to have same id so only check first quest
2902 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2903 return (firstRequest->mResultExtras.requestId == requestId);
2904}
2905
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002906status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002907 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002908 return clearRepeatingRequestsLocked(lastFrameNumber);
2909
2910}
2911
2912status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002913 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002914 if (lastFrameNumber != NULL) {
2915 *lastFrameNumber = mRepeatingLastFrameNumber;
2916 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002917 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002918 return OK;
2919}
2920
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002921status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002922 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002923 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002924 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002925
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002926 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002927
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002928 // Send errors for all requests pending in the request queue, including
2929 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002930 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002931 if (listener != NULL) {
2932 for (RequestList::iterator it = mRequestQueue.begin();
2933 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002934 // Abort the input buffers for reprocess requests.
2935 if ((*it)->mInputStream != NULL) {
2936 camera3_stream_buffer_t inputBuffer;
2937 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2938 if (res != OK) {
2939 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2940 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2941 } else {
2942 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2943 if (res != OK) {
2944 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2945 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2946 }
2947 }
2948 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002949 // Set the frame number this request would have had, if it
2950 // had been submitted; this frame number will not be reused.
2951 // The requestId and burstId fields were set when the request was
2952 // submitted originally (in convertMetadataListToRequestListLocked)
2953 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002954 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002955 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002956 }
2957 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002958 mRequestQueue.clear();
2959 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002960 if (lastFrameNumber != NULL) {
2961 *lastFrameNumber = mRepeatingLastFrameNumber;
2962 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002963 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002964 return OK;
2965}
2966
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002967status_t Camera3Device::RequestThread::flush() {
2968 ATRACE_CALL();
2969 Mutex::Autolock l(mFlushLock);
2970
2971 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2972 return mHal3Device->ops->flush(mHal3Device);
2973 }
2974
2975 return -ENOTSUP;
2976}
2977
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002978void Camera3Device::RequestThread::setPaused(bool paused) {
2979 Mutex::Autolock l(mPauseLock);
2980 mDoPause = paused;
2981 mDoPauseSignal.signal();
2982}
2983
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002984status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2985 int32_t requestId, nsecs_t timeout) {
2986 Mutex::Autolock l(mLatestRequestMutex);
2987 status_t res;
2988 while (mLatestRequestId != requestId) {
2989 nsecs_t startTime = systemTime();
2990
2991 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2992 if (res != OK) return res;
2993
2994 timeout -= (systemTime() - startTime);
2995 }
2996
2997 return OK;
2998}
2999
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003000void Camera3Device::RequestThread::requestExit() {
3001 // Call parent to set up shutdown
3002 Thread::requestExit();
3003 // The exit from any possible waits
3004 mDoPauseSignal.signal();
3005 mRequestSignal.signal();
3006}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003007
Chien-Yu Chend196d612015-06-22 19:49:01 -07003008
3009/**
3010 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
3011 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
3012 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
3013 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
3014 * request.
3015 */
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003016void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(const sp<CaptureRequest>& request) {
Chien-Yu Chend196d612015-06-22 19:49:01 -07003017 request->mAeTriggerCancelOverride.applyAeLock = false;
3018 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
3019
3020 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
3021 return;
3022 }
3023
3024 camera_metadata_entry_t aePrecaptureTrigger =
3025 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3026 if (aePrecaptureTrigger.count > 0 &&
3027 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
3028 // Always override CANCEL to IDLE
3029 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
3030 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
3031 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
3032 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
3033 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3034
3035 if (mAeLockAvailable == true) {
3036 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3037 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3038 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3039 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3040 request->mAeTriggerCancelOverride.applyAeLock = true;
3041 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3042 }
3043 }
3044 }
3045}
3046
3047/**
3048 * Override result metadata for cancelling AE precapture trigger applied in
3049 * handleAePrecaptureCancelRequest().
3050 */
3051void Camera3Device::overrideResultForPrecaptureCancel(
3052 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3053 if (aeTriggerCancelOverride.applyAeLock) {
3054 // Only devices <= v3.2 should have this override
3055 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3056 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3057 }
3058
3059 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3060 // Only devices <= v3.2 should have this override
3061 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3062 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3063 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3064 }
3065}
3066
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003067void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003068 bool surfaceAbandoned = false;
3069 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003070 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003071 {
3072 Mutex::Autolock l(mRequestLock);
3073 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3074 // repeating requests.
3075 for (const auto& request : mRepeatingRequests) {
3076 for (const auto& s : request->mOutputStreams) {
3077 if (s->isAbandoned()) {
3078 surfaceAbandoned = true;
3079 clearRepeatingRequestsLocked(&lastFrameNumber);
3080 break;
3081 }
3082 }
3083 if (surfaceAbandoned) {
3084 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003085 }
3086 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003087 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003088 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003089
3090 if (listener != NULL && surfaceAbandoned) {
3091 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003092 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003093}
3094
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003095bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003096 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003097 status_t res;
3098
3099 // Handle paused state.
3100 if (waitIfPaused()) {
3101 return true;
3102 }
3103
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003104 // Wait for the next batch of requests.
3105 waitForNextRequestBatch();
3106 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003107 return true;
3108 }
3109
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003110 // Get the latest request ID, if any
3111 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003112 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003113 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003114 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003115 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003116 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003117 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3118 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003119 }
3120
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003121 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003122 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003123 if (res == TIMED_OUT) {
3124 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003125 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003126 // Check if any stream is abandoned.
3127 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003128 return true;
3129 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003130 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003131 return false;
3132 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003133
Zhijun Hecc27e112013-10-03 16:12:43 -07003134 // Inform waitUntilRequestProcessed thread of a new request ID
3135 {
3136 Mutex::Autolock al(mLatestRequestMutex);
3137
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003138 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003139 mLatestRequestSignal.signal();
3140 }
3141
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003142 // Submit a batch of requests to HAL.
3143 // Use flush lock only when submitting multilple requests in a batch.
3144 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3145 // which may take a long time to finish so synchronizing flush() and
3146 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3147 // For now, only synchronize for high speed recording and we should figure something out for
3148 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003149 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003150
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003151 if (useFlushLock) {
3152 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003153 }
3154
Zhijun Hef0645c12016-08-02 00:58:11 -07003155 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003156 mNextRequests.size());
3157 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003158 // Submit request and block until ready for next one
3159 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3160 ATRACE_BEGIN("camera3->process_capture_request");
3161 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3162 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003163
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003164 if (res != OK) {
3165 // Should only get a failure here for malformed requests or device-level
3166 // errors, so consider all errors fatal. Bad metadata failures should
3167 // come through notify.
3168 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3169 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3170 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003171 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003172 if (useFlushLock) {
3173 mFlushLock.unlock();
3174 }
3175 return false;
3176 }
3177
3178 // Mark that the request has be submitted successfully.
3179 nextRequest.submitted = true;
3180
3181 // Update the latest request sent to HAL
3182 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3183 Mutex::Autolock al(mLatestRequestMutex);
3184
3185 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3186 mLatestRequest.acquire(cloned);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003187
3188 sp<Camera3Device> parent = mParent.promote();
3189 if (parent != NULL) {
3190 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
3191 0, mLatestRequest);
3192 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003193 }
3194
3195 if (nextRequest.halRequest.settings != NULL) {
3196 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3197 }
3198
3199 // Remove any previously queued triggers (after unlock)
3200 res = removeTriggers(mPrevRequest);
3201 if (res != OK) {
3202 SET_ERR("RequestThread: Unable to remove triggers "
3203 "(capture request %d, HAL device: %s (%d)",
3204 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003205 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003206 if (useFlushLock) {
3207 mFlushLock.unlock();
3208 }
3209 return false;
3210 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003211 }
3212
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003213 if (useFlushLock) {
3214 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003215 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003216
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003217 // Unset as current request
3218 {
3219 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003220 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003221 }
3222
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003223 return true;
3224}
3225
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003226status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003227 ATRACE_CALL();
3228
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003229 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003230 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3231 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3232 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3233
3234 // Prepare a request to HAL
3235 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3236
3237 // Insert any queued triggers (before metadata is locked)
3238 status_t res = insertTriggers(captureRequest);
3239
3240 if (res < 0) {
3241 SET_ERR("RequestThread: Unable to insert triggers "
3242 "(capture request %d, HAL device: %s (%d)",
3243 halRequest->frame_number, strerror(-res), res);
3244 return INVALID_OPERATION;
3245 }
3246 int triggerCount = res;
3247 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3248 mPrevTriggers = triggerCount;
3249
3250 // If the request is the same as last, or we had triggers last time
3251 if (mPrevRequest != captureRequest || triggersMixedIn) {
3252 /**
3253 * HAL workaround:
3254 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3255 */
3256 res = addDummyTriggerIds(captureRequest);
3257 if (res != OK) {
3258 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3259 "(capture request %d, HAL device: %s (%d)",
3260 halRequest->frame_number, strerror(-res), res);
3261 return INVALID_OPERATION;
3262 }
3263
3264 /**
3265 * The request should be presorted so accesses in HAL
3266 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3267 */
3268 captureRequest->mSettings.sort();
3269 halRequest->settings = captureRequest->mSettings.getAndLock();
3270 mPrevRequest = captureRequest;
3271 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3272
3273 IF_ALOGV() {
3274 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3275 find_camera_metadata_ro_entry(
3276 halRequest->settings,
3277 ANDROID_CONTROL_AF_TRIGGER,
3278 &e
3279 );
3280 if (e.count > 0) {
3281 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3282 __FUNCTION__,
3283 halRequest->frame_number,
3284 e.data.u8[0]);
3285 }
3286 }
3287 } else {
3288 // leave request.settings NULL to indicate 'reuse latest given'
3289 ALOGVV("%s: Request settings are REUSED",
3290 __FUNCTION__);
3291 }
3292
3293 uint32_t totalNumBuffers = 0;
3294
3295 // Fill in buffers
3296 if (captureRequest->mInputStream != NULL) {
3297 halRequest->input_buffer = &captureRequest->mInputBuffer;
3298 totalNumBuffers += 1;
3299 } else {
3300 halRequest->input_buffer = NULL;
3301 }
3302
3303 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3304 captureRequest->mOutputStreams.size());
3305 halRequest->output_buffers = outputBuffers->array();
3306 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003307 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(i);
3308
3309 // Prepare video buffers for high speed recording on the first video request.
3310 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3311 // Only try to prepare video stream on the first video request.
3312 mPrepareVideoStream = false;
3313
3314 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
3315 while (res == NOT_ENOUGH_DATA) {
3316 res = outputStream->prepareNextBuffer();
3317 }
3318 if (res != OK) {
3319 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3320 __FUNCTION__, strerror(-res), res);
3321 outputStream->cancelPrepare();
3322 }
3323 }
3324
3325 res = outputStream->getBuffer(&outputBuffers->editItemAt(i));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003326 if (res != OK) {
3327 // Can't get output buffer from gralloc queue - this could be due to
3328 // abandoned queue or other consumer misbehavior, so not a fatal
3329 // error
3330 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3331 " %s (%d)", strerror(-res), res);
3332
3333 return TIMED_OUT;
3334 }
3335 halRequest->num_output_buffers++;
3336 }
3337 totalNumBuffers += halRequest->num_output_buffers;
3338
3339 // Log request in the in-flight queue
3340 sp<Camera3Device> parent = mParent.promote();
3341 if (parent == NULL) {
3342 // Should not happen, and nowhere to send errors to, so just log it
3343 CLOGE("RequestThread: Parent is gone");
3344 return INVALID_OPERATION;
3345 }
3346 res = parent->registerInFlight(halRequest->frame_number,
3347 totalNumBuffers, captureRequest->mResultExtras,
3348 /*hasInput*/halRequest->input_buffer != NULL,
3349 captureRequest->mAeTriggerCancelOverride);
3350 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3351 ", burstId = %" PRId32 ".",
3352 __FUNCTION__,
3353 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3354 captureRequest->mResultExtras.burstId);
3355 if (res != OK) {
3356 SET_ERR("RequestThread: Unable to register new in-flight request:"
3357 " %s (%d)", strerror(-res), res);
3358 return INVALID_OPERATION;
3359 }
3360 }
3361
3362 return OK;
3363}
3364
Igor Murashkin1e479c02013-09-06 16:55:14 -07003365CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3366 Mutex::Autolock al(mLatestRequestMutex);
3367
3368 ALOGV("RequestThread::%s", __FUNCTION__);
3369
3370 return mLatestRequest;
3371}
3372
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003373bool Camera3Device::RequestThread::isStreamPending(
3374 sp<Camera3StreamInterface>& stream) {
3375 Mutex::Autolock l(mRequestLock);
3376
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003377 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003378 if (!nextRequest.submitted) {
3379 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3380 if (stream == s) return true;
3381 }
3382 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003383 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003384 }
3385
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003386 for (const auto& request : mRequestQueue) {
3387 for (const auto& s : request->mOutputStreams) {
3388 if (stream == s) return true;
3389 }
3390 if (stream == request->mInputStream) return true;
3391 }
3392
3393 for (const auto& request : mRepeatingRequests) {
3394 for (const auto& s : request->mOutputStreams) {
3395 if (stream == s) return true;
3396 }
3397 if (stream == request->mInputStream) return true;
3398 }
3399
3400 return false;
3401}
Jianing Weicb0652e2014-03-12 18:29:36 -07003402
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003403void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3404 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003405 return;
3406 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003407
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003408 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003409 // Skip the ones that have been submitted successfully.
3410 if (nextRequest.submitted) {
3411 continue;
3412 }
3413
3414 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3415 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3416 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3417
3418 if (halRequest->settings != NULL) {
3419 captureRequest->mSettings.unlock(halRequest->settings);
3420 }
3421
3422 if (captureRequest->mInputStream != NULL) {
3423 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3424 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3425 }
3426
3427 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3428 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3429 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3430 }
3431
3432 if (sendRequestError) {
3433 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003434 sp<NotificationListener> listener = mListener.promote();
3435 if (listener != NULL) {
3436 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003437 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003438 captureRequest->mResultExtras);
3439 }
3440 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003441 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003442
3443 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003444 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003445}
3446
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003447void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003448 // Optimized a bit for the simple steady-state case (single repeating
3449 // request), to avoid putting that request in the queue temporarily.
3450 Mutex::Autolock l(mRequestLock);
3451
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003452 assert(mNextRequests.empty());
3453
3454 NextRequest nextRequest;
3455 nextRequest.captureRequest = waitForNextRequestLocked();
3456 if (nextRequest.captureRequest == nullptr) {
3457 return;
3458 }
3459
3460 nextRequest.halRequest = camera3_capture_request_t();
3461 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003462 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003463
3464 // Wait for additional requests
3465 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3466
3467 for (size_t i = 1; i < batchSize; i++) {
3468 NextRequest additionalRequest;
3469 additionalRequest.captureRequest = waitForNextRequestLocked();
3470 if (additionalRequest.captureRequest == nullptr) {
3471 break;
3472 }
3473
3474 additionalRequest.halRequest = camera3_capture_request_t();
3475 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003476 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003477 }
3478
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003479 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003480 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003481 mNextRequests.size(), batchSize);
3482 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003483 }
3484
3485 return;
3486}
3487
3488sp<Camera3Device::CaptureRequest>
3489 Camera3Device::RequestThread::waitForNextRequestLocked() {
3490 status_t res;
3491 sp<CaptureRequest> nextRequest;
3492
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003493 while (mRequestQueue.empty()) {
3494 if (!mRepeatingRequests.empty()) {
3495 // Always atomically enqueue all requests in a repeating request
3496 // list. Guarantees a complete in-sequence set of captures to
3497 // application.
3498 const RequestList &requests = mRepeatingRequests;
3499 RequestList::const_iterator firstRequest =
3500 requests.begin();
3501 nextRequest = *firstRequest;
3502 mRequestQueue.insert(mRequestQueue.end(),
3503 ++firstRequest,
3504 requests.end());
3505 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003506
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003507 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003508
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003509 break;
3510 }
3511
3512 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3513
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003514 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3515 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003516 Mutex::Autolock pl(mPauseLock);
3517 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003518 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003519 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003520 // Let the tracker know
3521 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3522 if (statusTracker != 0) {
3523 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3524 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003525 }
3526 // Stop waiting for now and let thread management happen
3527 return NULL;
3528 }
3529 }
3530
3531 if (nextRequest == NULL) {
3532 // Don't have a repeating request already in hand, so queue
3533 // must have an entry now.
3534 RequestList::iterator firstRequest =
3535 mRequestQueue.begin();
3536 nextRequest = *firstRequest;
3537 mRequestQueue.erase(firstRequest);
3538 }
3539
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003540 // In case we've been unpaused by setPaused clearing mDoPause, need to
3541 // update internal pause state (capture/setRepeatingRequest unpause
3542 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003543 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003544 if (mPaused) {
3545 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3546 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3547 if (statusTracker != 0) {
3548 statusTracker->markComponentActive(mStatusId);
3549 }
3550 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003551 mPaused = false;
3552
3553 // Check if we've reconfigured since last time, and reset the preview
3554 // request if so. Can't use 'NULL request == repeat' across configure calls.
3555 if (mReconfigured) {
3556 mPrevRequest.clear();
3557 mReconfigured = false;
3558 }
3559
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003560 if (nextRequest != NULL) {
3561 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003562 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3563 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003564
3565 // Since RequestThread::clear() removes buffers from the input stream,
3566 // get the right buffer here before unlocking mRequestLock
3567 if (nextRequest->mInputStream != NULL) {
3568 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3569 if (res != OK) {
3570 // Can't get input buffer from gralloc queue - this could be due to
3571 // disconnected queue or other producer misbehavior, so not a fatal
3572 // error
3573 ALOGE("%s: Can't get input buffer, skipping request:"
3574 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003575
3576 sp<NotificationListener> listener = mListener.promote();
3577 if (listener != NULL) {
3578 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003579 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003580 nextRequest->mResultExtras);
3581 }
3582 return NULL;
3583 }
3584 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003585 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003586
3587 handleAePrecaptureCancelRequest(nextRequest);
3588
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003589 return nextRequest;
3590}
3591
3592bool Camera3Device::RequestThread::waitIfPaused() {
3593 status_t res;
3594 Mutex::Autolock l(mPauseLock);
3595 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003596 if (mPaused == false) {
3597 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003598 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3599 // Let the tracker know
3600 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3601 if (statusTracker != 0) {
3602 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3603 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003604 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003605
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003606 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003607 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003608 return true;
3609 }
3610 }
3611 // We don't set mPaused to false here, because waitForNextRequest needs
3612 // to further manage the paused state in case of starvation.
3613 return false;
3614}
3615
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003616void Camera3Device::RequestThread::unpauseForNewRequests() {
3617 // With work to do, mark thread as unpaused.
3618 // If paused by request (setPaused), don't resume, to avoid
3619 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003620 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003621 Mutex::Autolock p(mPauseLock);
3622 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003623 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3624 if (mPaused) {
3625 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3626 if (statusTracker != 0) {
3627 statusTracker->markComponentActive(mStatusId);
3628 }
3629 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003630 mPaused = false;
3631 }
3632}
3633
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003634void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3635 sp<Camera3Device> parent = mParent.promote();
3636 if (parent != NULL) {
3637 va_list args;
3638 va_start(args, fmt);
3639
3640 parent->setErrorStateV(fmt, args);
3641
3642 va_end(args);
3643 }
3644}
3645
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003646status_t Camera3Device::RequestThread::insertTriggers(
3647 const sp<CaptureRequest> &request) {
3648
3649 Mutex::Autolock al(mTriggerMutex);
3650
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003651 sp<Camera3Device> parent = mParent.promote();
3652 if (parent == NULL) {
3653 CLOGE("RequestThread: Parent is gone");
3654 return DEAD_OBJECT;
3655 }
3656
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003657 CameraMetadata &metadata = request->mSettings;
3658 size_t count = mTriggerMap.size();
3659
3660 for (size_t i = 0; i < count; ++i) {
3661 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003662 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003663
3664 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3665 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3666 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003667 if (isAeTrigger) {
3668 request->mResultExtras.precaptureTriggerId = triggerId;
3669 mCurrentPreCaptureTriggerId = triggerId;
3670 } else {
3671 request->mResultExtras.afTriggerId = triggerId;
3672 mCurrentAfTriggerId = triggerId;
3673 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003674 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3675 continue; // Trigger ID tag is deprecated since device HAL 3.2
3676 }
3677 }
3678
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003679 camera_metadata_entry entry = metadata.find(tag);
3680
3681 if (entry.count > 0) {
3682 /**
3683 * Already has an entry for this trigger in the request.
3684 * Rewrite it with our requested trigger value.
3685 */
3686 RequestTrigger oldTrigger = trigger;
3687
3688 oldTrigger.entryValue = entry.data.u8[0];
3689
3690 mTriggerReplacedMap.add(tag, oldTrigger);
3691 } else {
3692 /**
3693 * More typical, no trigger entry, so we just add it
3694 */
3695 mTriggerRemovedMap.add(tag, trigger);
3696 }
3697
3698 status_t res;
3699
3700 switch (trigger.getTagType()) {
3701 case TYPE_BYTE: {
3702 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3703 res = metadata.update(tag,
3704 &entryValue,
3705 /*count*/1);
3706 break;
3707 }
3708 case TYPE_INT32:
3709 res = metadata.update(tag,
3710 &trigger.entryValue,
3711 /*count*/1);
3712 break;
3713 default:
3714 ALOGE("%s: Type not supported: 0x%x",
3715 __FUNCTION__,
3716 trigger.getTagType());
3717 return INVALID_OPERATION;
3718 }
3719
3720 if (res != OK) {
3721 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3722 ", value %d", __FUNCTION__, trigger.getTagName(),
3723 trigger.entryValue);
3724 return res;
3725 }
3726
3727 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3728 trigger.getTagName(),
3729 trigger.entryValue);
3730 }
3731
3732 mTriggerMap.clear();
3733
3734 return count;
3735}
3736
3737status_t Camera3Device::RequestThread::removeTriggers(
3738 const sp<CaptureRequest> &request) {
3739 Mutex::Autolock al(mTriggerMutex);
3740
3741 CameraMetadata &metadata = request->mSettings;
3742
3743 /**
3744 * Replace all old entries with their old values.
3745 */
3746 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3747 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3748
3749 status_t res;
3750
3751 uint32_t tag = trigger.metadataTag;
3752 switch (trigger.getTagType()) {
3753 case TYPE_BYTE: {
3754 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3755 res = metadata.update(tag,
3756 &entryValue,
3757 /*count*/1);
3758 break;
3759 }
3760 case TYPE_INT32:
3761 res = metadata.update(tag,
3762 &trigger.entryValue,
3763 /*count*/1);
3764 break;
3765 default:
3766 ALOGE("%s: Type not supported: 0x%x",
3767 __FUNCTION__,
3768 trigger.getTagType());
3769 return INVALID_OPERATION;
3770 }
3771
3772 if (res != OK) {
3773 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3774 ", trigger value %d", __FUNCTION__,
3775 trigger.getTagName(), trigger.entryValue);
3776 return res;
3777 }
3778 }
3779 mTriggerReplacedMap.clear();
3780
3781 /**
3782 * Remove all new entries.
3783 */
3784 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3785 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3786 status_t res = metadata.erase(trigger.metadataTag);
3787
3788 if (res != OK) {
3789 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3790 ", trigger value %d", __FUNCTION__,
3791 trigger.getTagName(), trigger.entryValue);
3792 return res;
3793 }
3794 }
3795 mTriggerRemovedMap.clear();
3796
3797 return OK;
3798}
3799
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003800status_t Camera3Device::RequestThread::addDummyTriggerIds(
3801 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003802 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003803 static const int32_t dummyTriggerId = 1;
3804 status_t res;
3805
3806 CameraMetadata &metadata = request->mSettings;
3807
3808 // If AF trigger is active, insert a dummy AF trigger ID if none already
3809 // exists
3810 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3811 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3812 if (afTrigger.count > 0 &&
3813 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3814 afId.count == 0) {
3815 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3816 if (res != OK) return res;
3817 }
3818
3819 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3820 // if none already exists
3821 camera_metadata_entry pcTrigger =
3822 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3823 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3824 if (pcTrigger.count > 0 &&
3825 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3826 pcId.count == 0) {
3827 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3828 &dummyTriggerId, 1);
3829 if (res != OK) return res;
3830 }
3831
3832 return OK;
3833}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003834
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003835/**
3836 * PreparerThread inner class methods
3837 */
3838
3839Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003840 Thread(/*canCallJava*/false), mListener(nullptr),
3841 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003842}
3843
3844Camera3Device::PreparerThread::~PreparerThread() {
3845 Thread::requestExitAndWait();
3846 if (mCurrentStream != nullptr) {
3847 mCurrentStream->cancelPrepare();
3848 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3849 mCurrentStream.clear();
3850 }
3851 clear();
3852}
3853
Ruben Brunkc78ac262015-08-13 17:58:46 -07003854status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003855 status_t res;
3856
3857 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003858 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003859
Ruben Brunkc78ac262015-08-13 17:58:46 -07003860 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003861 if (res == OK) {
3862 // No preparation needed, fire listener right off
3863 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003864 if (listener != NULL) {
3865 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003866 }
3867 return OK;
3868 } else if (res != NOT_ENOUGH_DATA) {
3869 return res;
3870 }
3871
3872 // Need to prepare, start up thread if necessary
3873 if (!mActive) {
3874 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3875 // isn't running
3876 Thread::requestExitAndWait();
3877 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3878 if (res != OK) {
3879 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003880 if (listener != NULL) {
3881 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003882 }
3883 return res;
3884 }
3885 mCancelNow = false;
3886 mActive = true;
3887 ALOGV("%s: Preparer stream started", __FUNCTION__);
3888 }
3889
3890 // queue up the work
3891 mPendingStreams.push_back(stream);
3892 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3893
3894 return OK;
3895}
3896
3897status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003898 Mutex::Autolock l(mLock);
3899
3900 for (const auto& stream : mPendingStreams) {
3901 stream->cancelPrepare();
3902 }
3903 mPendingStreams.clear();
3904 mCancelNow = true;
3905
3906 return OK;
3907}
3908
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003909void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003910 Mutex::Autolock l(mLock);
3911 mListener = listener;
3912}
3913
3914bool Camera3Device::PreparerThread::threadLoop() {
3915 status_t res;
3916 {
3917 Mutex::Autolock l(mLock);
3918 if (mCurrentStream == nullptr) {
3919 // End thread if done with work
3920 if (mPendingStreams.empty()) {
3921 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3922 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3923 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3924 mActive = false;
3925 return false;
3926 }
3927
3928 // Get next stream to prepare
3929 auto it = mPendingStreams.begin();
3930 mCurrentStream = *it;
3931 mPendingStreams.erase(it);
3932 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3933 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3934 } else if (mCancelNow) {
3935 mCurrentStream->cancelPrepare();
3936 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3937 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3938 mCurrentStream.clear();
3939 mCancelNow = false;
3940 return true;
3941 }
3942 }
3943
3944 res = mCurrentStream->prepareNextBuffer();
3945 if (res == NOT_ENOUGH_DATA) return true;
3946 if (res != OK) {
3947 // Something bad happened; try to recover by cancelling prepare and
3948 // signalling listener anyway
3949 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3950 mCurrentStream->getId(), res, strerror(-res));
3951 mCurrentStream->cancelPrepare();
3952 }
3953
3954 // This stream has finished, notify listener
3955 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003956 sp<NotificationListener> listener = mListener.promote();
3957 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003958 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3959 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003960 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003961 }
3962
3963 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3964 mCurrentStream.clear();
3965
3966 return true;
3967}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003968
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003969/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003970 * Static callback forwarding methods from HAL to instance
3971 */
3972
3973void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3974 const camera3_capture_result *result) {
3975 Camera3Device *d =
3976 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003977
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003978 d->processCaptureResult(result);
3979}
3980
3981void Camera3Device::sNotify(const camera3_callback_ops *cb,
3982 const camera3_notify_msg *msg) {
3983 Camera3Device *d =
3984 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3985 d->notify(msg);
3986}
3987
3988}; // namespace android