blob: 3705e8ff012be09842aac2e0d1ae0b9191d68475 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Zhijun He90f7c372016-08-16 16:19:43 -070045#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070046
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080047#include <android/hardware/camera2/ICameraDeviceUser.h>
48
Igor Murashkinff3e31d2013-10-23 16:40:06 -070049#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070050#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070051#include "device3/Camera3Device.h"
52#include "device3/Camera3OutputStream.h"
53#include "device3/Camera3InputStream.h"
54#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070055#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070056#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080057
58using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080059
60namespace android {
61
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080062Camera3Device::Camera3Device(int id):
63 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070064 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080065 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070066 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070067 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070068 mUsePartialResult(false),
69 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080070 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070071 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070072 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070073 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070074 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070075 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080076{
77 ATRACE_CALL();
78 camera3_callback_ops::notify = &sNotify;
79 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
80 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
81}
82
83Camera3Device::~Camera3Device()
84{
85 ATRACE_CALL();
86 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
87 disconnect();
88}
89
Igor Murashkin71381052013-03-04 14:53:08 -080090int Camera3Device::getId() const {
91 return mId;
92}
93
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080094/**
95 * CameraDeviceBase interface
96 */
97
Yin-Chia Yehe074a932015-01-30 10:29:02 -080098status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080099{
100 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700101 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800102 Mutex::Autolock l(mLock);
103
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800104 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800105 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700106 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800107 return INVALID_OPERATION;
108 }
109
110 /** Open HAL device */
111
112 status_t res;
113 String8 deviceName = String8::format("%d", mId);
114
115 camera3_device_t *device;
116
Zhijun He213ce792013-11-19 08:45:15 -0800117 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800118 res = module->open(deviceName.string(),
119 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800120 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800121
122 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700123 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800124 return res;
125 }
126
127 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700128 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700129 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700130 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700131 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800132 device->common.version);
133 device->common.close(&device->common);
134 return BAD_VALUE;
135 }
136
137 camera_info info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800138 res = module->getCameraInfo(mId, &info);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 if (res != OK) return res;
140
141 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700142 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
143 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700144 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800145 device->common.close(&device->common);
146 return BAD_VALUE;
147 }
148
149 /** Initialize device with callback functions */
150
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700151 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800152 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700153 ATRACE_END();
154
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800155 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700156 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
157 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800158 device->common.close(&device->common);
159 return BAD_VALUE;
160 }
161
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700162 /** Start up status tracker thread */
163 mStatusTracker = new StatusTracker(this);
164 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
165 if (res != OK) {
166 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
167 strerror(-res), res);
168 device->common.close(&device->common);
169 mStatusTracker.clear();
170 return res;
171 }
172
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700173 /** Register in-flight map to the status tracker */
174 mInFlightStatusId = mStatusTracker->addComponent();
175
Zhijun He125684a2015-12-26 15:07:30 -0800176 /** Create buffer manager */
177 mBufferManager = new Camera3BufferManager();
178
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700179 bool aeLockAvailable = false;
180 camera_metadata_ro_entry aeLockAvailableEntry;
181 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
182 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
183 if (res == OK && aeLockAvailableEntry.count > 0) {
184 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
185 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
186 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700188 /** Start up request queue thread */
189 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800190 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800191 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700192 SET_ERR_L("Unable to start request queue thread: %s (%d)",
193 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800194 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800195 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800196 return res;
197 }
198
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700199 mPreparerThread = new PreparerThread();
200
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800201 /** Everything is good to go */
202
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700203 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800204 mDeviceInfo = info.static_camera_characteristics;
205 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700206
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700207 // Determine whether we need to derive sensitivity boost values for older devices.
208 // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
209 // be listed (as the default value 100)
210 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_4 &&
211 mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
212 mDerivePostRawSensKey = true;
213 }
214
Ruben Brunk183f0562015-08-12 12:55:02 -0700215 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800216 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700217 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700218 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700219 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800220
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800221 // Measure the clock domain offset between camera and video/hw_composer
222 camera_metadata_entry timestampSource =
223 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
224 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
225 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
226 mTimestampOffset = getMonoToBoottimeOffset();
227 }
228
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700229 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700230 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
231 camera_metadata_entry partialResultsCount =
232 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
233 if (partialResultsCount.count > 0) {
234 mNumPartialResults = partialResultsCount.data.i32[0];
235 mUsePartialResult = (mNumPartialResults > 1);
236 }
237 } else {
238 camera_metadata_entry partialResultsQuirk =
239 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
240 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
241 mUsePartialResult = true;
242 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700243 }
244
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700245 camera_metadata_entry configs =
246 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
247 for (uint32_t i = 0; i < configs.count; i += 4) {
248 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
249 configs.data.i32[i + 3] ==
250 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
251 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
252 configs.data.i32[i + 2]));
253 }
254 }
255
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800256 return OK;
257}
258
259status_t Camera3Device::disconnect() {
260 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700261 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800262
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700263 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800264
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700265 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800266
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700267 {
268 Mutex::Autolock l(mLock);
269 if (mStatus == STATUS_UNINITIALIZED) return res;
270
271 if (mStatus == STATUS_ACTIVE ||
272 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
273 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700274 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700275 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700276 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277 } else {
278 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
279 if (res != OK) {
280 SET_ERR_L("Timeout waiting for HAL to drain");
281 // Continue to close device even in case of error
282 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700283 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800284 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800285
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700286 if (mStatus == STATUS_ERROR) {
287 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700288 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700289
290 if (mStatusTracker != NULL) {
291 mStatusTracker->requestExit();
292 }
293
294 if (mRequestThread != NULL) {
295 mRequestThread->requestExit();
296 }
297
298 mOutputStreams.clear();
299 mInputStream.clear();
300 }
301
302 // Joining done without holding mLock, otherwise deadlocks may ensue
303 // as the threads try to access parent state
304 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
305 // HAL may be in a bad state, so waiting for request thread
306 // (which may be stuck in the HAL processCaptureRequest call)
307 // could be dangerous.
308 mRequestThread->join();
309 }
310
311 if (mStatusTracker != NULL) {
312 mStatusTracker->join();
313 }
314
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700315 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700316 {
317 Mutex::Autolock l(mLock);
318
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800319 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700320 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800321 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800322
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700323 hal3Device = mHal3Device;
324 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800325
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700326 // Call close without internal mutex held, as the HAL close may need to
327 // wait on assorted callbacks,etc, to complete before it can return.
328 if (hal3Device != NULL) {
329 ATRACE_BEGIN("camera3->close");
330 hal3Device->common.close(&hal3Device->common);
331 ATRACE_END();
332 }
333
334 {
335 Mutex::Autolock l(mLock);
336 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700337 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700338 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800339
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700340 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700341 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800342}
343
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700344// For dumping/debugging only -
345// try to acquire a lock a few times, eventually give up to proceed with
346// debug/dump operations
347bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
348 bool gotLock = false;
349 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
350 if (lock.tryLock() == NO_ERROR) {
351 gotLock = true;
352 break;
353 } else {
354 usleep(kDumpSleepDuration);
355 }
356 }
357 return gotLock;
358}
359
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700360Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
361 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
362 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
363 const int STREAM_CONFIGURATION_SIZE = 4;
364 const int STREAM_FORMAT_OFFSET = 0;
365 const int STREAM_WIDTH_OFFSET = 1;
366 const int STREAM_HEIGHT_OFFSET = 2;
367 const int STREAM_IS_INPUT_OFFSET = 3;
368 camera_metadata_ro_entry_t availableStreamConfigs =
369 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
370 if (availableStreamConfigs.count == 0 ||
371 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
372 return Size(0, 0);
373 }
374
375 // Get max jpeg size (area-wise).
376 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
377 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
378 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
379 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
380 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
381 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
382 && format == HAL_PIXEL_FORMAT_BLOB &&
383 (width * height > maxJpegWidth * maxJpegHeight)) {
384 maxJpegWidth = width;
385 maxJpegHeight = height;
386 }
387 }
388 } else {
389 camera_metadata_ro_entry availableJpegSizes =
390 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
391 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
392 return Size(0, 0);
393 }
394
395 // Get max jpeg size (area-wise).
396 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
397 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
398 > (maxJpegWidth * maxJpegHeight)) {
399 maxJpegWidth = availableJpegSizes.data.i32[i];
400 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
401 }
402 }
403 }
404 return Size(maxJpegWidth, maxJpegHeight);
405}
406
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800407nsecs_t Camera3Device::getMonoToBoottimeOffset() {
408 // try three times to get the clock offset, choose the one
409 // with the minimum gap in measurements.
410 const int tries = 3;
411 nsecs_t bestGap, measured;
412 for (int i = 0; i < tries; ++i) {
413 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
414 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
415 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
416 const nsecs_t gap = tmono2 - tmono;
417 if (i == 0 || gap < bestGap) {
418 bestGap = gap;
419 measured = tbase - ((tmono + tmono2) >> 1);
420 }
421 }
422 return measured;
423}
424
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -0700425/**
426 * Map Android N dataspace definitions back to Android M definitions, for
427 * use with HALv3.3 or older.
428 *
429 * Only map where correspondences exist, and otherwise preserve the value.
430 */
431android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) {
432 switch (dataSpace) {
433 case HAL_DATASPACE_V0_SRGB_LINEAR:
434 return HAL_DATASPACE_SRGB_LINEAR;
435 case HAL_DATASPACE_V0_SRGB:
436 return HAL_DATASPACE_SRGB;
437 case HAL_DATASPACE_V0_JFIF:
438 return HAL_DATASPACE_JFIF;
439 case HAL_DATASPACE_V0_BT601_625:
440 return HAL_DATASPACE_BT601_625;
441 case HAL_DATASPACE_V0_BT601_525:
442 return HAL_DATASPACE_BT601_525;
443 case HAL_DATASPACE_V0_BT709:
444 return HAL_DATASPACE_BT709;
445 default:
446 return dataSpace;
447 }
448}
449
Zhijun Hef7da0962014-04-24 13:27:56 -0700450ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700451 // Get max jpeg size (area-wise).
452 Size maxJpegResolution = getMaxJpegResolution();
453 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700454 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700455 __FUNCTION__, mId);
456 return BAD_VALUE;
457 }
458
Zhijun Hef7da0962014-04-24 13:27:56 -0700459 // Get max jpeg buffer size
460 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700461 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
462 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700463 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
464 return BAD_VALUE;
465 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700466 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800467 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700468
469 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700470 float scaleFactor = ((float) (width * height)) /
471 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800472 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
473 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700474 if (jpegBufferSize > maxJpegBufferSize) {
475 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700476 }
477
478 return jpegBufferSize;
479}
480
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700481ssize_t Camera3Device::getPointCloudBufferSize() const {
482 const int FLOATS_PER_POINT=4;
483 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
484 if (maxPointCount.count == 0) {
485 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
486 __FUNCTION__, mId);
487 return BAD_VALUE;
488 }
489 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
490 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
491 return maxBytesForPointCloud;
492}
493
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800494ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800495 const int PER_CONFIGURATION_SIZE = 3;
496 const int WIDTH_OFFSET = 0;
497 const int HEIGHT_OFFSET = 1;
498 const int SIZE_OFFSET = 2;
499 camera_metadata_ro_entry rawOpaqueSizes =
500 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800501 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800502 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800503 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800504 __FUNCTION__, mId, count);
505 return BAD_VALUE;
506 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700507
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800508 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
509 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
510 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
511 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
512 }
513 }
514
515 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
516 __FUNCTION__, mId, width, height);
517 return BAD_VALUE;
518}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700519
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800520status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
521 ATRACE_CALL();
522 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700523
524 // Try to lock, but continue in case of failure (to avoid blocking in
525 // deadlocks)
526 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
527 bool gotLock = tryLockSpinRightRound(mLock);
528
529 ALOGW_IF(!gotInterfaceLock,
530 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
531 mId, __FUNCTION__);
532 ALOGW_IF(!gotLock,
533 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
534 mId, __FUNCTION__);
535
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800536 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700537
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800538 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700539 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800540 int n = args.size();
541 for (int i = 0; i < n; i++) {
542 if (args[i] == templatesOption) {
543 dumpTemplates = true;
544 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700545 if (args[i] == monitorOption) {
546 if (i + 1 < n) {
547 String8 monitorTags = String8(args[i + 1]);
548 if (monitorTags == "off") {
549 mTagMonitor.disableMonitoring();
550 } else {
551 mTagMonitor.parseTagsToMonitor(monitorTags);
552 }
553 } else {
554 mTagMonitor.disableMonitoring();
555 }
556 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800557 }
558
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800559 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800560
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800561 const char *status =
562 mStatus == STATUS_ERROR ? "ERROR" :
563 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700564 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
565 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800566 mStatus == STATUS_ACTIVE ? "ACTIVE" :
567 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700568
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800569 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700570 if (mStatus == STATUS_ERROR) {
571 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
572 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800573 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700574 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700575 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800576
577 if (mInputStream != NULL) {
578 write(fd, lines.string(), lines.size());
579 mInputStream->dump(fd, args);
580 } else {
581 lines.appendFormat(" No input stream.\n");
582 write(fd, lines.string(), lines.size());
583 }
584 for (size_t i = 0; i < mOutputStreams.size(); i++) {
585 mOutputStreams[i]->dump(fd,args);
586 }
587
Zhijun He431503c2016-03-07 17:30:16 -0800588 if (mBufferManager != NULL) {
589 lines = String8(" Camera3 Buffer Manager:\n");
590 write(fd, lines.string(), lines.size());
591 mBufferManager->dump(fd, args);
592 }
Zhijun He125684a2015-12-26 15:07:30 -0800593
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700594 lines = String8(" In-flight requests:\n");
595 if (mInFlightMap.size() == 0) {
596 lines.append(" None\n");
597 } else {
598 for (size_t i = 0; i < mInFlightMap.size(); i++) {
599 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700600 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700601 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800602 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700603 r.numBuffersLeft);
604 }
605 }
606 write(fd, lines.string(), lines.size());
607
Igor Murashkin1e479c02013-09-06 16:55:14 -0700608 {
609 lines = String8(" Last request sent:\n");
610 write(fd, lines.string(), lines.size());
611
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700612 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700613 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
614 }
615
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800616 if (dumpTemplates) {
617 const char *templateNames[] = {
618 "TEMPLATE_PREVIEW",
619 "TEMPLATE_STILL_CAPTURE",
620 "TEMPLATE_VIDEO_RECORD",
621 "TEMPLATE_VIDEO_SNAPSHOT",
622 "TEMPLATE_ZERO_SHUTTER_LAG",
623 "TEMPLATE_MANUAL"
624 };
625
626 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
627 const camera_metadata_t *templateRequest;
628 templateRequest =
629 mHal3Device->ops->construct_default_request_settings(
630 mHal3Device, i);
631 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
632 if (templateRequest == NULL) {
633 lines.append(" Not supported\n");
634 write(fd, lines.string(), lines.size());
635 } else {
636 write(fd, lines.string(), lines.size());
637 dump_indented_camera_metadata(templateRequest,
638 fd, /*verbosity*/2, /*indentation*/8);
639 }
640 }
641 }
642
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700643 mTagMonitor.dumpMonitoredMetadata(fd);
644
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800645 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700646 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800647 write(fd, lines.string(), lines.size());
648 mHal3Device->ops->dump(mHal3Device, fd);
649 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800650
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700651 if (gotLock) mLock.unlock();
652 if (gotInterfaceLock) mInterfaceLock.unlock();
653
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800654 return OK;
655}
656
657const CameraMetadata& Camera3Device::info() const {
658 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800659 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
660 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700661 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800662 mStatus == STATUS_ERROR ?
663 "when in error state" : "before init");
664 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800665 return mDeviceInfo;
666}
667
Jianing Wei90e59c92014-03-12 18:29:36 -0700668status_t Camera3Device::checkStatusOkToCaptureLocked() {
669 switch (mStatus) {
670 case STATUS_ERROR:
671 CLOGE("Device has encountered a serious error");
672 return INVALID_OPERATION;
673 case STATUS_UNINITIALIZED:
674 CLOGE("Device not initialized");
675 return INVALID_OPERATION;
676 case STATUS_UNCONFIGURED:
677 case STATUS_CONFIGURED:
678 case STATUS_ACTIVE:
679 // OK
680 break;
681 default:
682 SET_ERR_L("Unexpected status: %d", mStatus);
683 return INVALID_OPERATION;
684 }
685 return OK;
686}
687
688status_t Camera3Device::convertMetadataListToRequestListLocked(
Shuzhen Wang9d066012016-09-30 11:30:20 -0700689 const List<const CameraMetadata> &metadataList, bool repeating,
690 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700691 if (requestList == NULL) {
692 CLOGE("requestList cannot be NULL.");
693 return BAD_VALUE;
694 }
695
Jianing Weicb0652e2014-03-12 18:29:36 -0700696 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700697 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
698 it != metadataList.end(); ++it) {
699 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
700 if (newRequest == 0) {
701 CLOGE("Can't create capture request");
702 return BAD_VALUE;
703 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700704
Shuzhen Wang9d066012016-09-30 11:30:20 -0700705 newRequest->mRepeating = repeating;
706
Jianing Weicb0652e2014-03-12 18:29:36 -0700707 // Setup burst Id and request Id
708 newRequest->mResultExtras.burstId = burstId++;
709 if (it->exists(ANDROID_REQUEST_ID)) {
710 if (it->find(ANDROID_REQUEST_ID).count == 0) {
711 CLOGE("RequestID entry exists; but must not be empty in metadata");
712 return BAD_VALUE;
713 }
714 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
715 } else {
716 CLOGE("RequestID does not exist in metadata");
717 return BAD_VALUE;
718 }
719
Jianing Wei90e59c92014-03-12 18:29:36 -0700720 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700721
722 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700723 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700724
725 // Setup batch size if this is a high speed video recording request.
726 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
727 auto firstRequest = requestList->begin();
728 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
729 if (outputStream->isVideoStream()) {
730 (*firstRequest)->mBatchSize = requestList->size();
731 break;
732 }
733 }
734 }
735
Jianing Wei90e59c92014-03-12 18:29:36 -0700736 return OK;
737}
738
Jianing Weicb0652e2014-03-12 18:29:36 -0700739status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800740 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800741
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700742 List<const CameraMetadata> requests;
743 requests.push_back(request);
744 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800745}
746
Jianing Wei90e59c92014-03-12 18:29:36 -0700747status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700748 const List<const CameraMetadata> &requests, bool repeating,
749 /*out*/
750 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700751 ATRACE_CALL();
752 Mutex::Autolock il(mInterfaceLock);
753 Mutex::Autolock l(mLock);
754
755 status_t res = checkStatusOkToCaptureLocked();
756 if (res != OK) {
757 // error logged by previous call
758 return res;
759 }
760
761 RequestList requestList;
762
Shuzhen Wang9d066012016-09-30 11:30:20 -0700763 res = convertMetadataListToRequestListLocked(requests, repeating,
764 /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700765 if (res != OK) {
766 // error logged by previous call
767 return res;
768 }
769
770 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700771 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700772 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700773 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700774 }
775
776 if (res == OK) {
777 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
778 if (res != OK) {
779 SET_ERR_L("Can't transition to active in %f seconds!",
780 kActiveTimeout/1e9);
781 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700782 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
783 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700784 } else {
785 CLOGE("Cannot queue request. Impossible.");
786 return BAD_VALUE;
787 }
788
789 return res;
790}
791
Jianing Weicb0652e2014-03-12 18:29:36 -0700792status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
793 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700794 ATRACE_CALL();
795
Jianing Weicb0652e2014-03-12 18:29:36 -0700796 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700797}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800798
Jianing Weicb0652e2014-03-12 18:29:36 -0700799status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
800 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800801 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800802
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700803 List<const CameraMetadata> requests;
804 requests.push_back(request);
805 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800806}
807
Jianing Weicb0652e2014-03-12 18:29:36 -0700808status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
809 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700810 ATRACE_CALL();
811
Jianing Weicb0652e2014-03-12 18:29:36 -0700812 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700813}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800814
815sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
816 const CameraMetadata &request) {
817 status_t res;
818
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700819 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800820 res = configureStreamsLocked();
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700821 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800822 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700823 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800824 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700825 } else if (mStatus == STATUS_UNCONFIGURED) {
826 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700827 CLOGE("No streams configured");
828 return NULL;
829 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800830 }
831
832 sp<CaptureRequest> newRequest = createCaptureRequest(request);
833 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800834}
835
Jianing Weicb0652e2014-03-12 18:29:36 -0700836status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800837 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700838 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800839 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800840
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800841 switch (mStatus) {
842 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700843 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800844 return INVALID_OPERATION;
845 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700846 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800847 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700848 case STATUS_UNCONFIGURED:
849 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800850 case STATUS_ACTIVE:
851 // OK
852 break;
853 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700854 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800855 return INVALID_OPERATION;
856 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700857 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700858
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700859 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800860}
861
862status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
863 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700864 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800865
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700866 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800867}
868
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700869status_t Camera3Device::createInputStream(
870 uint32_t width, uint32_t height, int format, int *id) {
871 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700872 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700873 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700874 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
875 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700876
877 status_t res;
878 bool wasActive = false;
879
880 switch (mStatus) {
881 case STATUS_ERROR:
882 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
883 return INVALID_OPERATION;
884 case STATUS_UNINITIALIZED:
885 ALOGE("%s: Device not initialized", __FUNCTION__);
886 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700887 case STATUS_UNCONFIGURED:
888 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700889 // OK
890 break;
891 case STATUS_ACTIVE:
892 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700893 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700894 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700895 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700896 return res;
897 }
898 wasActive = true;
899 break;
900 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700901 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700902 return INVALID_OPERATION;
903 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700904 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700905
906 if (mInputStream != 0) {
907 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
908 return INVALID_OPERATION;
909 }
910
911 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
912 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700913 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700914
915 mInputStream = newStream;
916
917 *id = mNextStreamId++;
918
919 // Continue captures if active at start
920 if (wasActive) {
921 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
922 res = configureStreamsLocked();
923 if (res != OK) {
924 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
925 __FUNCTION__, mNextStreamId, strerror(-res), res);
926 return res;
927 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700928 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700929 }
930
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700931 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700932 return OK;
933}
934
Igor Murashkin2fba5842013-04-22 14:03:54 -0700935
936status_t Camera3Device::createZslStream(
937 uint32_t width, uint32_t height,
938 int depth,
939 /*out*/
940 int *id,
941 sp<Camera3ZslStream>* zslStream) {
942 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700943 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700944 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700945 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
946 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700947
948 status_t res;
949 bool wasActive = false;
950
951 switch (mStatus) {
952 case STATUS_ERROR:
953 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
954 return INVALID_OPERATION;
955 case STATUS_UNINITIALIZED:
956 ALOGE("%s: Device not initialized", __FUNCTION__);
957 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700958 case STATUS_UNCONFIGURED:
959 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700960 // OK
961 break;
962 case STATUS_ACTIVE:
963 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700964 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700965 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700966 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700967 return res;
968 }
969 wasActive = true;
970 break;
971 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700972 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700973 return INVALID_OPERATION;
974 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700975 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700976
977 if (mInputStream != 0) {
978 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
979 return INVALID_OPERATION;
980 }
981
982 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
983 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700984 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700985
986 res = mOutputStreams.add(mNextStreamId, newStream);
987 if (res < 0) {
988 ALOGE("%s: Can't add new stream to set: %s (%d)",
989 __FUNCTION__, strerror(-res), res);
990 return res;
991 }
992 mInputStream = newStream;
993
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530994 mNeedConfig = true;
995
Igor Murashkin2fba5842013-04-22 14:03:54 -0700996 *id = mNextStreamId++;
997 *zslStream = newStream;
998
999 // Continue captures if active at start
1000 if (wasActive) {
1001 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1002 res = configureStreamsLocked();
1003 if (res != OK) {
1004 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1005 __FUNCTION__, mNextStreamId, strerror(-res), res);
1006 return res;
1007 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001008 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -07001009 }
1010
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001011 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -07001012 return OK;
1013}
1014
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001015status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001016 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -07001017 camera3_stream_rotation_t rotation, int *id, int streamSetId, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001018 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001019 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001020 Mutex::Autolock l(mLock);
Zhijun He5d677d12016-05-29 16:52:39 -07001021 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
1022 " consumer usage 0x%x", mId, mNextStreamId, width, height, format, dataSpace, rotation,
1023 consumerUsage);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001024
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001025 status_t res;
1026 bool wasActive = false;
1027
1028 switch (mStatus) {
1029 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001030 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001031 return INVALID_OPERATION;
1032 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001033 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001034 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001035 case STATUS_UNCONFIGURED:
1036 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001037 // OK
1038 break;
1039 case STATUS_ACTIVE:
1040 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001041 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001042 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001043 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001044 return res;
1045 }
1046 wasActive = true;
1047 break;
1048 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001049 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001050 return INVALID_OPERATION;
1051 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001052 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053
1054 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001055 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001056 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001057 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001058 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1059 }
Zhijun He5d677d12016-05-29 16:52:39 -07001060
1061 // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
1062 // which requires a consumer surface to be available.
1063 if (consumer == nullptr && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1064 ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
1065 return BAD_VALUE;
1066 }
1067
1068 if (consumer == nullptr && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1069 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1070 return BAD_VALUE;
1071 }
1072
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001073 // Use legacy dataspace values for older HALs
1074 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1075 dataSpace = mapToLegacyDataspace(dataSpace);
1076 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001077 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001078 ssize_t blobBufferSize;
1079 if (dataSpace != HAL_DATASPACE_DEPTH) {
1080 blobBufferSize = getJpegBufferSize(width, height);
1081 if (blobBufferSize <= 0) {
1082 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1083 return BAD_VALUE;
1084 }
1085 } else {
1086 blobBufferSize = getPointCloudBufferSize();
1087 if (blobBufferSize <= 0) {
1088 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1089 return BAD_VALUE;
1090 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001091 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001092 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001093 width, height, blobBufferSize, format, dataSpace, rotation,
1094 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001095 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1096 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1097 if (rawOpaqueBufferSize <= 0) {
1098 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1099 return BAD_VALUE;
1100 }
1101 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001102 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1103 mTimestampOffset, streamSetId);
Zhijun He5d677d12016-05-29 16:52:39 -07001104 } else if (consumer == nullptr) {
1105 newStream = new Camera3OutputStream(mNextStreamId,
1106 width, height, format, consumerUsage, dataSpace, rotation,
1107 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001108 } else {
1109 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001110 width, height, format, dataSpace, rotation,
1111 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001112 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001113 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001114
Zhijun He125684a2015-12-26 15:07:30 -08001115 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001116 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1117 * requires buffers to be statically allocated for internal static buffer registration, while
1118 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1119 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001120 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001121 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001122 newStream->setBufferManager(mBufferManager);
1123 }
1124
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001125 res = mOutputStreams.add(mNextStreamId, newStream);
1126 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001127 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001128 return res;
1129 }
1130
1131 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001132 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001133
1134 // Continue captures if active at start
1135 if (wasActive) {
1136 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1137 res = configureStreamsLocked();
1138 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001139 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1140 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141 return res;
1142 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001143 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001144 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001145 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001146 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001147}
1148
1149status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1150 ATRACE_CALL();
1151 (void)outputId; (void)id;
1152
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001153 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001154 return INVALID_OPERATION;
1155}
1156
1157
1158status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001159 uint32_t *width, uint32_t *height,
1160 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001161 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001162 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001163 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001164
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001165 switch (mStatus) {
1166 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001167 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001168 return INVALID_OPERATION;
1169 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001170 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001171 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001172 case STATUS_UNCONFIGURED:
1173 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001174 case STATUS_ACTIVE:
1175 // OK
1176 break;
1177 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001178 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001179 return INVALID_OPERATION;
1180 }
1181
1182 ssize_t idx = mOutputStreams.indexOfKey(id);
1183 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001184 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001185 return idx;
1186 }
1187
1188 if (width) *width = mOutputStreams[idx]->getWidth();
1189 if (height) *height = mOutputStreams[idx]->getHeight();
1190 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001191 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001192 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001193}
1194
1195status_t Camera3Device::setStreamTransform(int id,
1196 int transform) {
1197 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001198 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001199 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001200
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001201 switch (mStatus) {
1202 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001203 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001204 return INVALID_OPERATION;
1205 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001206 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001207 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001208 case STATUS_UNCONFIGURED:
1209 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001210 case STATUS_ACTIVE:
1211 // OK
1212 break;
1213 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001214 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001215 return INVALID_OPERATION;
1216 }
1217
1218 ssize_t idx = mOutputStreams.indexOfKey(id);
1219 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001220 CLOGE("Stream %d does not exist",
1221 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001222 return BAD_VALUE;
1223 }
1224
1225 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001226}
1227
1228status_t Camera3Device::deleteStream(int id) {
1229 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001230 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001231 Mutex::Autolock l(mLock);
1232 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001233
Igor Murashkine2172be2013-05-28 15:31:39 -07001234 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1235
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001236 // CameraDevice semantics require device to already be idle before
1237 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001238 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001239 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1240 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001241 }
1242
Igor Murashkin2fba5842013-04-22 14:03:54 -07001243 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001244 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001245 if (mInputStream != NULL && id == mInputStream->getId()) {
1246 deletedStream = mInputStream;
1247 mInputStream.clear();
1248 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001249 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001250 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001251 return BAD_VALUE;
1252 }
Zhijun He5f446352014-01-22 09:49:33 -08001253 }
1254
1255 // Delete output stream or the output part of a bi-directional stream.
1256 if (outputStreamIdx != NAME_NOT_FOUND) {
1257 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001258 mOutputStreams.removeItem(id);
1259 }
1260
1261 // Free up the stream endpoint so that it can be used by some other stream
1262 res = deletedStream->disconnect();
1263 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001264 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001265 // fall through since we want to still list the stream as deleted.
1266 }
1267 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001268 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001269
1270 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001271}
1272
1273status_t Camera3Device::deleteReprocessStream(int id) {
1274 ATRACE_CALL();
1275 (void)id;
1276
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001277 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001278 return INVALID_OPERATION;
1279}
1280
Zhijun He1fa89992015-06-01 15:44:31 -07001281status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001282 ATRACE_CALL();
1283 ALOGV("%s: E", __FUNCTION__);
1284
1285 Mutex::Autolock il(mInterfaceLock);
1286 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001287
1288 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1289 mNeedConfig = true;
1290 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1291 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001292
1293 return configureStreamsLocked();
1294}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001295
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001296status_t Camera3Device::getInputBufferProducer(
1297 sp<IGraphicBufferProducer> *producer) {
1298 Mutex::Autolock il(mInterfaceLock);
1299 Mutex::Autolock l(mLock);
1300
1301 if (producer == NULL) {
1302 return BAD_VALUE;
1303 } else if (mInputStream == NULL) {
1304 return INVALID_OPERATION;
1305 }
1306
1307 return mInputStream->getInputBufferProducer(producer);
1308}
1309
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001310status_t Camera3Device::createDefaultRequest(int templateId,
1311 CameraMetadata *request) {
1312 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001313 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001314
1315 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1316 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1317 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1318 return BAD_VALUE;
1319 }
1320
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001321 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001322 Mutex::Autolock l(mLock);
1323
1324 switch (mStatus) {
1325 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001326 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001327 return INVALID_OPERATION;
1328 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001329 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001330 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001331 case STATUS_UNCONFIGURED:
1332 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001333 case STATUS_ACTIVE:
1334 // OK
1335 break;
1336 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001337 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001338 return INVALID_OPERATION;
1339 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001340
Zhijun Hea1530f12014-09-14 12:44:20 -07001341 if (!mRequestTemplateCache[templateId].isEmpty()) {
1342 *request = mRequestTemplateCache[templateId];
1343 return OK;
1344 }
1345
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001346 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001347 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001348 rawRequest = mHal3Device->ops->construct_default_request_settings(
1349 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001350 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001351 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001352 ALOGI("%s: template %d is not supported on this camera device",
1353 __FUNCTION__, templateId);
1354 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001355 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001356
Zhijun Hea1530f12014-09-14 12:44:20 -07001357 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001358
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001359 // Derive some new keys for backward compatibility
1360 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
1361 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
1362 int32_t defaultBoost[1] = {100};
1363 mRequestTemplateCache[templateId].update(
1364 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1365 defaultBoost, 1);
1366 }
1367
1368 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001369 return OK;
1370}
1371
1372status_t Camera3Device::waitUntilDrained() {
1373 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001374 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001375 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001376
Zhijun He69a37482014-03-23 18:44:49 -07001377 return waitUntilDrainedLocked();
1378}
1379
1380status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381 switch (mStatus) {
1382 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001383 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001384 ALOGV("%s: Already idle", __FUNCTION__);
1385 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001386 case STATUS_CONFIGURED:
1387 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001388 case STATUS_ERROR:
1389 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001390 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001391 break;
1392 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001393 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001394 return INVALID_OPERATION;
1395 }
1396
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001397 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1398 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001399 if (res != OK) {
1400 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1401 res);
1402 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001403 return res;
1404}
1405
Ruben Brunk183f0562015-08-12 12:55:02 -07001406
1407void Camera3Device::internalUpdateStatusLocked(Status status) {
1408 mStatus = status;
1409 mRecentStatusUpdates.add(mStatus);
1410 mStatusChanged.broadcast();
1411}
1412
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001413// Pause to reconfigure
1414status_t Camera3Device::internalPauseAndWaitLocked() {
1415 mRequestThread->setPaused(true);
1416 mPauseStateNotify = true;
1417
1418 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1419 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1420 if (res != OK) {
1421 SET_ERR_L("Can't idle device in %f seconds!",
1422 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001423 }
1424
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001425 return res;
1426}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001427
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001428// Resume after internalPauseAndWaitLocked
1429status_t Camera3Device::internalResumeLocked() {
1430 status_t res;
1431
1432 mRequestThread->setPaused(false);
1433
1434 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1435 if (res != OK) {
1436 SET_ERR_L("Can't transition to active in %f seconds!",
1437 kActiveTimeout/1e9);
1438 }
1439 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001440 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001441}
1442
Ruben Brunk183f0562015-08-12 12:55:02 -07001443status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001444 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001445
1446 size_t startIndex = 0;
1447 if (mStatusWaiters == 0) {
1448 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1449 // this status list
1450 mRecentStatusUpdates.clear();
1451 } else {
1452 // If other threads are waiting on updates to this status list, set the position of the
1453 // first element that this list will check rather than clearing the list.
1454 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001455 }
1456
Ruben Brunk183f0562015-08-12 12:55:02 -07001457 mStatusWaiters++;
1458
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001459 bool stateSeen = false;
1460 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001461 if (active == (mStatus == STATUS_ACTIVE)) {
1462 // Desired state is current
1463 break;
1464 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001465
1466 res = mStatusChanged.waitRelative(mLock, timeout);
1467 if (res != OK) break;
1468
Ruben Brunk183f0562015-08-12 12:55:02 -07001469 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1470 // transitions.
1471 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1472 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1473 __FUNCTION__);
1474
1475 // Encountered desired state since we began waiting
1476 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001477 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1478 stateSeen = true;
1479 break;
1480 }
1481 }
1482 } while (!stateSeen);
1483
Ruben Brunk183f0562015-08-12 12:55:02 -07001484 mStatusWaiters--;
1485
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001486 return res;
1487}
1488
1489
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001490status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001491 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001492 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001493
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001494 if (listener != NULL && mListener != NULL) {
1495 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1496 }
1497 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001498 mRequestThread->setNotificationListener(listener);
1499 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001500
1501 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001502}
1503
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001504bool Camera3Device::willNotify3A() {
1505 return false;
1506}
1507
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001508status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001509 status_t res;
1510 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001511
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001512 while (mResultQueue.empty()) {
1513 res = mResultSignal.waitRelative(mOutputLock, timeout);
1514 if (res == TIMED_OUT) {
1515 return res;
1516 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001517 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001518 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001519 return res;
1520 }
1521 }
1522 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001523}
1524
Jianing Weicb0652e2014-03-12 18:29:36 -07001525status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001526 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001527 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001528
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001529 if (mResultQueue.empty()) {
1530 return NOT_ENOUGH_DATA;
1531 }
1532
Jianing Weicb0652e2014-03-12 18:29:36 -07001533 if (frame == NULL) {
1534 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1535 return BAD_VALUE;
1536 }
1537
1538 CaptureResult &result = *(mResultQueue.begin());
1539 frame->mResultExtras = result.mResultExtras;
1540 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001541 mResultQueue.erase(mResultQueue.begin());
1542
1543 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001544}
1545
1546status_t Camera3Device::triggerAutofocus(uint32_t id) {
1547 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001548 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001549
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001550 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1551 // Mix-in this trigger into the next request and only the next request.
1552 RequestTrigger trigger[] = {
1553 {
1554 ANDROID_CONTROL_AF_TRIGGER,
1555 ANDROID_CONTROL_AF_TRIGGER_START
1556 },
1557 {
1558 ANDROID_CONTROL_AF_TRIGGER_ID,
1559 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001560 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001561 };
1562
1563 return mRequestThread->queueTrigger(trigger,
1564 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001565}
1566
1567status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1568 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001569 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001570
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001571 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1572 // Mix-in this trigger into the next request and only the next request.
1573 RequestTrigger trigger[] = {
1574 {
1575 ANDROID_CONTROL_AF_TRIGGER,
1576 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1577 },
1578 {
1579 ANDROID_CONTROL_AF_TRIGGER_ID,
1580 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001581 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001582 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001583
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001584 return mRequestThread->queueTrigger(trigger,
1585 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001586}
1587
1588status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1589 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001590 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001591
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001592 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1593 // Mix-in this trigger into the next request and only the next request.
1594 RequestTrigger trigger[] = {
1595 {
1596 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1597 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1598 },
1599 {
1600 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1601 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001602 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001603 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001604
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001605 return mRequestThread->queueTrigger(trigger,
1606 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001607}
1608
1609status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1610 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1611 ATRACE_CALL();
1612 (void)reprocessStreamId; (void)buffer; (void)listener;
1613
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001614 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001615 return INVALID_OPERATION;
1616}
1617
Jianing Weicb0652e2014-03-12 18:29:36 -07001618status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001619 ATRACE_CALL();
1620 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001621 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001622
Zhijun He7ef20392014-04-21 16:04:17 -07001623 {
1624 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001625 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001626 }
1627
Zhijun He491e3412013-12-27 10:57:44 -08001628 status_t res;
1629 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001630 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001631 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001632 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001633 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001634 }
1635
1636 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001637}
1638
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001639status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001640 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1641}
1642
1643status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001644 ATRACE_CALL();
1645 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001646 Mutex::Autolock il(mInterfaceLock);
1647 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001648
1649 sp<Camera3StreamInterface> stream;
1650 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1651 if (outputStreamIdx == NAME_NOT_FOUND) {
1652 CLOGE("Stream %d does not exist", streamId);
1653 return BAD_VALUE;
1654 }
1655
1656 stream = mOutputStreams.editValueAt(outputStreamIdx);
1657
1658 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001659 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001660 return BAD_VALUE;
1661 }
1662
1663 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001664 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001665 return BAD_VALUE;
1666 }
1667
Ruben Brunkc78ac262015-08-13 17:58:46 -07001668 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001669}
1670
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001671status_t Camera3Device::tearDown(int streamId) {
1672 ATRACE_CALL();
1673 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1674 Mutex::Autolock il(mInterfaceLock);
1675 Mutex::Autolock l(mLock);
1676
1677 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1678 // since we cannot call register_stream_buffers except right after configure_streams.
1679 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1680 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1681 __FUNCTION__, mHal3Device->common.version);
1682 return NO_INIT;
1683 }
1684
1685 sp<Camera3StreamInterface> stream;
1686 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1687 if (outputStreamIdx == NAME_NOT_FOUND) {
1688 CLOGE("Stream %d does not exist", streamId);
1689 return BAD_VALUE;
1690 }
1691
1692 stream = mOutputStreams.editValueAt(outputStreamIdx);
1693
1694 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1695 CLOGE("Stream %d is a target of a in-progress request", streamId);
1696 return BAD_VALUE;
1697 }
1698
1699 return stream->tearDown();
1700}
1701
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001702status_t Camera3Device::addBufferListenerForStream(int streamId,
1703 wp<Camera3StreamBufferListener> listener) {
1704 ATRACE_CALL();
1705 ALOGV("%s: Camera %d: Adding buffer listener for stream %d", __FUNCTION__, mId, streamId);
1706 Mutex::Autolock il(mInterfaceLock);
1707 Mutex::Autolock l(mLock);
1708
1709 sp<Camera3StreamInterface> stream;
1710 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1711 if (outputStreamIdx == NAME_NOT_FOUND) {
1712 CLOGE("Stream %d does not exist", streamId);
1713 return BAD_VALUE;
1714 }
1715
1716 stream = mOutputStreams.editValueAt(outputStreamIdx);
1717 stream->addBufferListener(listener);
1718
1719 return OK;
1720}
1721
Zhijun He204e3292014-07-14 17:09:23 -07001722uint32_t Camera3Device::getDeviceVersion() {
1723 ATRACE_CALL();
1724 Mutex::Autolock il(mInterfaceLock);
1725 return mDeviceVersion;
1726}
1727
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001728/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001729 * Methods called by subclasses
1730 */
1731
1732void Camera3Device::notifyStatus(bool idle) {
1733 {
1734 // Need mLock to safely update state and synchronize to current
1735 // state of methods in flight.
1736 Mutex::Autolock l(mLock);
1737 // We can get various system-idle notices from the status tracker
1738 // while starting up. Only care about them if we've actually sent
1739 // in some requests recently.
1740 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1741 return;
1742 }
1743 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1744 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001745 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001746
1747 // Skip notifying listener if we're doing some user-transparent
1748 // state changes
1749 if (mPauseStateNotify) return;
1750 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001751
1752 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001753 {
1754 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001755 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001756 }
1757 if (idle && listener != NULL) {
1758 listener->notifyIdle();
1759 }
1760}
1761
Zhijun He5d677d12016-05-29 16:52:39 -07001762status_t Camera3Device::setConsumerSurface(int streamId, sp<Surface> consumer) {
1763 ATRACE_CALL();
1764 ALOGV("%s: Camera %d: set consumer surface for stream %d", __FUNCTION__, mId, streamId);
1765 Mutex::Autolock il(mInterfaceLock);
1766 Mutex::Autolock l(mLock);
1767
1768 if (consumer == nullptr) {
1769 CLOGE("Null consumer is passed!");
1770 return BAD_VALUE;
1771 }
1772
1773 ssize_t idx = mOutputStreams.indexOfKey(streamId);
1774 if (idx == NAME_NOT_FOUND) {
1775 CLOGE("Stream %d is unknown", streamId);
1776 return idx;
1777 }
1778 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
1779 status_t res = stream->setConsumer(consumer);
1780 if (res != OK) {
1781 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1782 return res;
1783 }
1784
1785 if (!stream->isConfiguring()) {
1786 CLOGE("Stream %d was already fully configured.", streamId);
1787 return INVALID_OPERATION;
1788 }
1789
1790 res = stream->finishConfiguration(mHal3Device);
1791 if (res != OK) {
1792 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1793 stream->getId(), strerror(-res), res);
1794 return res;
1795 }
1796
1797 return OK;
1798}
1799
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001800/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001801 * Camera3Device private methods
1802 */
1803
1804sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1805 const CameraMetadata &request) {
1806 ATRACE_CALL();
1807 status_t res;
1808
1809 sp<CaptureRequest> newRequest = new CaptureRequest;
1810 newRequest->mSettings = request;
1811
1812 camera_metadata_entry_t inputStreams =
1813 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1814 if (inputStreams.count > 0) {
1815 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001816 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001817 CLOGE("Request references unknown input stream %d",
1818 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001819 return NULL;
1820 }
1821 // Lazy completion of stream configuration (allocation/registration)
1822 // on first use
1823 if (mInputStream->isConfiguring()) {
1824 res = mInputStream->finishConfiguration(mHal3Device);
1825 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001826 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001827 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001828 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001829 return NULL;
1830 }
1831 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001832 // Check if stream is being prepared
1833 if (mInputStream->isPreparing()) {
1834 CLOGE("Request references an input stream that's being prepared!");
1835 return NULL;
1836 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001837
1838 newRequest->mInputStream = mInputStream;
1839 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1840 }
1841
1842 camera_metadata_entry_t streams =
1843 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1844 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001845 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001846 return NULL;
1847 }
1848
1849 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001850 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001851 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001852 CLOGE("Request references unknown stream %d",
1853 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001854 return NULL;
1855 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001856 sp<Camera3OutputStreamInterface> stream =
1857 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001858
Zhijun He5d677d12016-05-29 16:52:39 -07001859 // It is illegal to include a deferred consumer output stream into a request
1860 if (stream->isConsumerConfigurationDeferred()) {
1861 CLOGE("Stream %d hasn't finished configuration yet due to deferred consumer",
1862 stream->getId());
1863 return NULL;
1864 }
1865
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001866 // Lazy completion of stream configuration (allocation/registration)
1867 // on first use
1868 if (stream->isConfiguring()) {
1869 res = stream->finishConfiguration(mHal3Device);
1870 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001871 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1872 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001873 return NULL;
1874 }
1875 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001876 // Check if stream is being prepared
1877 if (stream->isPreparing()) {
1878 CLOGE("Request references an output stream that's being prepared!");
1879 return NULL;
1880 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001881
1882 newRequest->mOutputStreams.push(stream);
1883 }
1884 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001885 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001886
1887 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001888}
1889
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001890bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1891 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1892 Size size = mSupportedOpaqueInputSizes[i];
1893 if (size.width == width && size.height == height) {
1894 return true;
1895 }
1896 }
1897
1898 return false;
1899}
1900
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001901void Camera3Device::cancelStreamsConfigurationLocked() {
1902 int res = OK;
1903 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1904 res = mInputStream->cancelConfiguration();
1905 if (res != OK) {
1906 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
1907 mInputStream->getId(), strerror(-res), res);
1908 }
1909 }
1910
1911 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1912 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
1913 if (outputStream->isConfiguring()) {
1914 res = outputStream->cancelConfiguration();
1915 if (res != OK) {
1916 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
1917 outputStream->getId(), strerror(-res), res);
1918 }
1919 }
1920 }
1921
1922 // Return state to that at start of call, so that future configures
1923 // properly clean things up
1924 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
1925 mNeedConfig = true;
1926}
1927
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001928status_t Camera3Device::configureStreamsLocked() {
1929 ATRACE_CALL();
1930 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001931
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001932 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001933 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001934 return INVALID_OPERATION;
1935 }
1936
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001937 if (!mNeedConfig) {
1938 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1939 return OK;
1940 }
1941
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001942 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1943 // adding a dummy stream instead.
1944 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1945 if (mOutputStreams.size() == 0) {
1946 addDummyStreamLocked();
1947 } else {
1948 tryRemoveDummyStreamLocked();
1949 }
1950
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001951 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001952 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001953
1954 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001955 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1956 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1957 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001958 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1959
1960 Vector<camera3_stream_t*> streams;
1961 streams.setCapacity(config.num_streams);
1962
1963 if (mInputStream != NULL) {
1964 camera3_stream_t *inputStream;
1965 inputStream = mInputStream->startConfiguration();
1966 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001967 CLOGE("Can't start input stream configuration");
1968 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001969 return INVALID_OPERATION;
1970 }
1971 streams.add(inputStream);
1972 }
1973
1974 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001975
1976 // Don't configure bidi streams twice, nor add them twice to the list
1977 if (mOutputStreams[i].get() ==
1978 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1979
1980 config.num_streams--;
1981 continue;
1982 }
1983
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001984 camera3_stream_t *outputStream;
1985 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1986 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001987 CLOGE("Can't start output stream configuration");
1988 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001989 return INVALID_OPERATION;
1990 }
1991 streams.add(outputStream);
1992 }
1993
1994 config.streams = streams.editArray();
1995
1996 // Do the HAL configuration; will potentially touch stream
1997 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001998 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001999 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002000 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002001
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002002 if (res == BAD_VALUE) {
2003 // HAL rejected this set of streams as unsupported, clean up config
2004 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002005 CLOGE("Set of requested inputs/outputs not supported by HAL");
2006 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002007 return BAD_VALUE;
2008 } else if (res != OK) {
2009 // Some other kind of error from configure_streams - this is not
2010 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002011 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2012 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002013 return res;
2014 }
2015
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002016 // Finish all stream configuration immediately.
2017 // TODO: Try to relax this later back to lazy completion, which should be
2018 // faster
2019
Igor Murashkin073f8572013-05-02 14:59:28 -07002020 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002021 res = mInputStream->finishConfiguration(mHal3Device);
2022 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002023 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002024 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002025 cancelStreamsConfigurationLocked();
2026 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002027 }
2028 }
2029
2030 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002031 sp<Camera3OutputStreamInterface> outputStream =
2032 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002033 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002034 res = outputStream->finishConfiguration(mHal3Device);
2035 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002036 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002037 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002038 cancelStreamsConfigurationLocked();
2039 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002040 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002041 }
2042 }
2043
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002044 // Request thread needs to know to avoid using repeat-last-settings protocol
2045 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002046 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002047
Zhijun He90f7c372016-08-16 16:19:43 -07002048 char value[PROPERTY_VALUE_MAX];
2049 property_get("camera.fifo.disable", value, "0");
2050 int32_t disableFifo = atoi(value);
2051 if (disableFifo != 1) {
2052 // Boost priority of request thread to SCHED_FIFO.
2053 pid_t requestThreadTid = mRequestThread->getTid();
2054 res = requestPriority(getpid(), requestThreadTid,
2055 kRequestThreadPriority, /*asynchronous*/ false);
2056 if (res != OK) {
2057 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2058 strerror(-res), res);
2059 } else {
2060 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2061 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002062 }
2063
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002064 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002065
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002066 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002067
Ruben Brunk183f0562015-08-12 12:55:02 -07002068 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2069 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002070
2071 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
2072
Zhijun He0a210512014-07-24 13:45:15 -07002073 // tear down the deleted streams after configure streams.
2074 mDeletedStreams.clear();
2075
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002076 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002077}
2078
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002079status_t Camera3Device::addDummyStreamLocked() {
2080 ATRACE_CALL();
2081 status_t res;
2082
2083 if (mDummyStreamId != NO_STREAM) {
2084 // Should never be adding a second dummy stream when one is already
2085 // active
2086 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
2087 __FUNCTION__, mId);
2088 return INVALID_OPERATION;
2089 }
2090
2091 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
2092
2093 sp<Camera3OutputStreamInterface> dummyStream =
2094 new Camera3DummyStream(mNextStreamId);
2095
2096 res = mOutputStreams.add(mNextStreamId, dummyStream);
2097 if (res < 0) {
2098 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2099 return res;
2100 }
2101
2102 mDummyStreamId = mNextStreamId;
2103 mNextStreamId++;
2104
2105 return OK;
2106}
2107
2108status_t Camera3Device::tryRemoveDummyStreamLocked() {
2109 ATRACE_CALL();
2110 status_t res;
2111
2112 if (mDummyStreamId == NO_STREAM) return OK;
2113 if (mOutputStreams.size() == 1) return OK;
2114
2115 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
2116
2117 // Ok, have a dummy stream and there's at least one other output stream,
2118 // so remove the dummy
2119
2120 sp<Camera3StreamInterface> deletedStream;
2121 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2122 if (outputStreamIdx == NAME_NOT_FOUND) {
2123 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2124 return INVALID_OPERATION;
2125 }
2126
2127 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2128 mOutputStreams.removeItemsAt(outputStreamIdx);
2129
2130 // Free up the stream endpoint so that it can be used by some other stream
2131 res = deletedStream->disconnect();
2132 if (res != OK) {
2133 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2134 // fall through since we want to still list the stream as deleted.
2135 }
2136 mDeletedStreams.add(deletedStream);
2137 mDummyStreamId = NO_STREAM;
2138
2139 return res;
2140}
2141
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002142void Camera3Device::setErrorState(const char *fmt, ...) {
2143 Mutex::Autolock l(mLock);
2144 va_list args;
2145 va_start(args, fmt);
2146
2147 setErrorStateLockedV(fmt, args);
2148
2149 va_end(args);
2150}
2151
2152void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2153 Mutex::Autolock l(mLock);
2154 setErrorStateLockedV(fmt, args);
2155}
2156
2157void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2158 va_list args;
2159 va_start(args, fmt);
2160
2161 setErrorStateLockedV(fmt, args);
2162
2163 va_end(args);
2164}
2165
2166void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002167 // Print out all error messages to log
2168 String8 errorCause = String8::formatV(fmt, args);
2169 ALOGE("Camera %d: %s", mId, errorCause.string());
2170
2171 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002172 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002173
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002174 mErrorCause = errorCause;
2175
2176 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002177 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002178
2179 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002180 sp<NotificationListener> listener = mListener.promote();
2181 if (listener != NULL) {
2182 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002183 CaptureResultExtras());
2184 }
2185
2186 // Save stack trace. View by dumping it later.
2187 CameraTraces::saveTrace();
2188 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002189}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002190
2191/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002192 * In-flight request management
2193 */
2194
Jianing Weicb0652e2014-03-12 18:29:36 -07002195status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002196 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2197 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002198 ATRACE_CALL();
2199 Mutex::Autolock l(mInFlightLock);
2200
2201 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002202 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2203 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002204 if (res < 0) return res;
2205
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002206 if (mInFlightMap.size() == 1) {
2207 mStatusTracker->markComponentActive(mInFlightStatusId);
2208 }
2209
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002210 return OK;
2211}
2212
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002213void Camera3Device::returnOutputBuffers(
2214 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2215 nsecs_t timestamp) {
2216 for (size_t i = 0; i < numBuffers; i++)
2217 {
2218 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2219 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2220 // Note: stream may be deallocated at this point, if this buffer was
2221 // the last reference to it.
2222 if (res != OK) {
2223 ALOGE("Can't return buffer to its stream: %s (%d)",
2224 strerror(-res), res);
2225 }
2226 }
2227}
2228
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002229void Camera3Device::removeInFlightMapEntryLocked(int idx) {
2230 mInFlightMap.removeItemsAt(idx, 1);
2231
2232 // Indicate idle inFlightMap to the status tracker
2233 if (mInFlightMap.size() == 0) {
2234 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2235 }
2236}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002237
2238void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2239
2240 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2241 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2242
2243 nsecs_t sensorTimestamp = request.sensorTimestamp;
2244 nsecs_t shutterTimestamp = request.shutterTimestamp;
2245
2246 // Check if it's okay to remove the request from InFlightMap:
2247 // In the case of a successful request:
2248 // all input and output buffers, all result metadata, shutter callback
2249 // arrived.
2250 // In the case of a unsuccessful request:
2251 // all input and output buffers arrived.
2252 if (request.numBuffersLeft == 0 &&
2253 (request.requestStatus != OK ||
2254 (request.haveResultMetadata && shutterTimestamp != 0))) {
2255 ATRACE_ASYNC_END("frame capture", frameNumber);
2256
2257 // Sanity check - if sensor timestamp matches shutter timestamp
2258 if (request.requestStatus == OK &&
2259 sensorTimestamp != shutterTimestamp) {
2260 SET_ERR("sensor timestamp (%" PRId64
2261 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2262 sensorTimestamp, frameNumber, shutterTimestamp);
2263 }
2264
2265 // for an unsuccessful request, it may have pending output buffers to
2266 // return.
2267 assert(request.requestStatus != OK ||
2268 request.pendingOutputBuffers.size() == 0);
2269 returnOutputBuffers(request.pendingOutputBuffers.array(),
2270 request.pendingOutputBuffers.size(), 0);
2271
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002272 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002273 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2274 }
2275
2276 // Sanity check - if we have too many in-flight frames, something has
2277 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002278 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002279 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002280 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2281 kInFlightWarnLimitHighSpeed) {
2282 CLOGE("In-flight list too large for high speed configuration: %zu",
2283 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002284 }
2285}
2286
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002287void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2288 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2289 if (result == nullptr) return;
2290
2291 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2292 (int32_t*)&frameNumber, 1) != OK) {
2293 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2294 return;
2295 }
2296
2297 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2298 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2299 return;
2300 }
2301
2302 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2303
2304 // Valid result, insert into queue
2305 List<CaptureResult>::iterator queuedResult =
2306 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2307 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2308 ", burstId = %" PRId32, __FUNCTION__,
2309 queuedResult->mResultExtras.requestId,
2310 queuedResult->mResultExtras.frameNumber,
2311 queuedResult->mResultExtras.burstId);
2312
2313 mResultSignal.signal();
2314}
2315
2316
2317void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2318 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2319 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2320 Mutex::Autolock l(mOutputLock);
2321
2322 CaptureResult captureResult;
2323 captureResult.mResultExtras = resultExtras;
2324 captureResult.mMetadata = partialResult;
2325
2326 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2327}
2328
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002329
2330void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2331 CaptureResultExtras &resultExtras,
2332 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002333 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002334 bool reprocess,
2335 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002336 if (pendingMetadata.isEmpty())
2337 return;
2338
2339 Mutex::Autolock l(mOutputLock);
2340
2341 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002342 if (reprocess) {
2343 if (frameNumber < mNextReprocessResultFrameNumber) {
2344 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002345 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002346 frameNumber, mNextReprocessResultFrameNumber);
2347 return;
2348 }
2349 mNextReprocessResultFrameNumber = frameNumber + 1;
2350 } else {
2351 if (frameNumber < mNextResultFrameNumber) {
2352 SET_ERR("Out-of-order capture result metadata submitted! "
2353 "(got frame number %d, expecting %d)",
2354 frameNumber, mNextResultFrameNumber);
2355 return;
2356 }
2357 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002358 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002359
2360 CaptureResult captureResult;
2361 captureResult.mResultExtras = resultExtras;
2362 captureResult.mMetadata = pendingMetadata;
2363
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002364 // Append any previous partials to form a complete result
2365 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2366 captureResult.mMetadata.append(collectedPartialResult);
2367 }
2368
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07002369 // Derive some new keys for backward compaibility
2370 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
2371 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
2372 int32_t defaultBoost[1] = {100};
2373 captureResult.mMetadata.update(
2374 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
2375 defaultBoost, 1);
2376 }
2377
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002378 captureResult.mMetadata.sort();
2379
2380 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002381 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2382 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002383 SET_ERR("No timestamp provided by HAL for frame %d!",
2384 frameNumber);
2385 return;
2386 }
2387
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002388 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2389 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2390
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002391 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002392}
2393
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002394/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002395 * Camera HAL device callback methods
2396 */
2397
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002398void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002399 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002400
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002401 status_t res;
2402
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002403 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002404 if (result->result == NULL && result->num_output_buffers == 0 &&
2405 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002406 SET_ERR("No result data provided by HAL for frame %d",
2407 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002408 return;
2409 }
Zhijun He204e3292014-07-14 17:09:23 -07002410
2411 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2412 // partial_result to 1 when metadata is included in this result.
2413 if (!mUsePartialResult &&
2414 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2415 result->result != NULL &&
2416 result->partial_result != 1) {
2417 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2418 " if partial result is not supported",
2419 frameNumber, result->partial_result);
2420 return;
2421 }
2422
2423 bool isPartialResult = false;
2424 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002425 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002426 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002427
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002428 // Get shutter timestamp and resultExtras from list of in-flight requests,
2429 // where it was added by the shutter notification for this frame. If the
2430 // shutter timestamp isn't received yet, append the output buffers to the
2431 // in-flight request and they will be returned when the shutter timestamp
2432 // arrives. Update the in-flight status and remove the in-flight entry if
2433 // all result data and shutter timestamp have been received.
2434 nsecs_t shutterTimestamp = 0;
2435
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002436 {
2437 Mutex::Autolock l(mInFlightLock);
2438 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2439 if (idx == NAME_NOT_FOUND) {
2440 SET_ERR("Unknown frame number for capture result: %d",
2441 frameNumber);
2442 return;
2443 }
2444 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002445 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2446 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2447 ", partialResultCount = %d",
2448 __FUNCTION__, request.resultExtras.requestId,
2449 request.resultExtras.frameNumber, request.resultExtras.burstId,
2450 result->partial_result);
2451 // Always update the partial count to the latest one if it's not 0
2452 // (buffers only). When framework aggregates adjacent partial results
2453 // into one, the latest partial count will be used.
2454 if (result->partial_result != 0)
2455 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002456
2457 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002458 if (mUsePartialResult && result->result != NULL) {
2459 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2460 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2461 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2462 " the range of [1, %d] when metadata is included in the result",
2463 frameNumber, result->partial_result, mNumPartialResults);
2464 return;
2465 }
2466 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002467 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002468 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002469 }
Zhijun He204e3292014-07-14 17:09:23 -07002470 } else {
2471 camera_metadata_ro_entry_t partialResultEntry;
2472 res = find_camera_metadata_ro_entry(result->result,
2473 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2474 if (res != NAME_NOT_FOUND &&
2475 partialResultEntry.count > 0 &&
2476 partialResultEntry.data.u8[0] ==
2477 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2478 // A partial result. Flag this as such, and collect this
2479 // set of metadata into the in-flight entry.
2480 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002481 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002482 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002483 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002484 ANDROID_QUIRKS_PARTIAL_RESULT);
2485 }
2486 }
2487
2488 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002489 // Send partial capture result
2490 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2491 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002492 }
2493 }
2494
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002495 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002496 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002497
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002498 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002499 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002500 if (request.haveResultMetadata) {
2501 SET_ERR("Called multiple times with metadata for frame %d",
2502 frameNumber);
2503 return;
2504 }
Zhijun He204e3292014-07-14 17:09:23 -07002505 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002506 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002507 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002508 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002509 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002510 request.haveResultMetadata = true;
2511 }
2512
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002513 uint32_t numBuffersReturned = result->num_output_buffers;
2514 if (result->input_buffer != NULL) {
2515 if (hasInputBufferInRequest) {
2516 numBuffersReturned += 1;
2517 } else {
2518 ALOGW("%s: Input buffer should be NULL if there is no input"
2519 " buffer sent in the request",
2520 __FUNCTION__);
2521 }
2522 }
2523 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002524 if (request.numBuffersLeft < 0) {
2525 SET_ERR("Too many buffers returned for frame %d",
2526 frameNumber);
2527 return;
2528 }
2529
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002530 camera_metadata_ro_entry_t entry;
2531 res = find_camera_metadata_ro_entry(result->result,
2532 ANDROID_SENSOR_TIMESTAMP, &entry);
2533 if (res == OK && entry.count == 1) {
2534 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002535 }
2536
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002537 // If shutter event isn't received yet, append the output buffers to
2538 // the in-flight request. Otherwise, return the output buffers to
2539 // streams.
2540 if (shutterTimestamp == 0) {
2541 request.pendingOutputBuffers.appendArray(result->output_buffers,
2542 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002543 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002544 returnOutputBuffers(result->output_buffers,
2545 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002546 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002547
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002548 if (result->result != NULL && !isPartialResult) {
2549 if (shutterTimestamp == 0) {
2550 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002551 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002552 } else {
2553 CameraMetadata metadata;
2554 metadata = result->result;
2555 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002556 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2557 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002558 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002559 }
2560
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002561 removeInFlightRequestIfReadyLocked(idx);
2562 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002563
Zhijun Hef0d962a2014-06-30 10:24:11 -07002564 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002565 if (hasInputBufferInRequest) {
2566 Camera3Stream *stream =
2567 Camera3Stream::cast(result->input_buffer->stream);
2568 res = stream->returnInputBuffer(*(result->input_buffer));
2569 // Note: stream may be deallocated at this point, if this buffer was the
2570 // last reference to it.
2571 if (res != OK) {
2572 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2573 " its stream:%s (%d)", __FUNCTION__,
2574 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002575 }
2576 } else {
2577 ALOGW("%s: Input buffer should be NULL if there is no input"
2578 " buffer sent in the request, skipping input buffer return.",
2579 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002580 }
2581 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002582}
2583
2584void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002585 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002586 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002587 {
2588 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002589 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002590 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002591
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002592 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002593 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002594 return;
2595 }
2596
2597 switch (msg->type) {
2598 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002599 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002600 break;
2601 }
2602 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002603 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002604 break;
2605 }
2606 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002607 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002608 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002609 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002610}
2611
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002612void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002613 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002614
2615 // Map camera HAL error codes to ICameraDeviceCallback error codes
2616 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002617 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002618 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002619 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002620 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002621 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002622 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002623 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002624 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002625 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002626 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002627 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002628 };
2629
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002630 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002631 ((msg.error_code >= 0) &&
2632 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2633 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002634 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002635
2636 int streamId = 0;
2637 if (msg.error_stream != NULL) {
2638 Camera3Stream *stream =
2639 Camera3Stream::cast(msg.error_stream);
2640 streamId = stream->getId();
2641 }
2642 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2643 mId, __FUNCTION__, msg.frame_number,
2644 streamId, msg.error_code);
2645
2646 CaptureResultExtras resultExtras;
2647 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002648 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002649 // SET_ERR calls notifyError
2650 SET_ERR("Camera HAL reported serious device error");
2651 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002652 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2653 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2654 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002655 {
2656 Mutex::Autolock l(mInFlightLock);
2657 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2658 if (idx >= 0) {
2659 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2660 r.requestStatus = msg.error_code;
2661 resultExtras = r.resultExtras;
2662 } else {
2663 resultExtras.frameNumber = msg.frame_number;
2664 ALOGE("Camera %d: %s: cannot find in-flight request on "
2665 "frame %" PRId64 " error", mId, __FUNCTION__,
2666 resultExtras.frameNumber);
2667 }
2668 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002669 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002670 if (listener != NULL) {
2671 listener->notifyError(errorCode, resultExtras);
2672 } else {
2673 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2674 }
2675 break;
2676 default:
2677 // SET_ERR calls notifyError
2678 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2679 break;
2680 }
2681}
2682
2683void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002684 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002685 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002686
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002687 // Set timestamp for the request in the in-flight tracking
2688 // and get the request ID to send upstream
2689 {
2690 Mutex::Autolock l(mInFlightLock);
2691 idx = mInFlightMap.indexOfKey(msg.frame_number);
2692 if (idx >= 0) {
2693 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002694
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002695 // Verify ordering of shutter notifications
2696 {
2697 Mutex::Autolock l(mOutputLock);
2698 // TODO: need to track errors for tighter bounds on expected frame number.
2699 if (r.hasInputBuffer) {
2700 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2701 SET_ERR("Shutter notification out-of-order. Expected "
2702 "notification for frame %d, got frame %d",
2703 mNextReprocessShutterFrameNumber, msg.frame_number);
2704 return;
2705 }
2706 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2707 } else {
2708 if (msg.frame_number < mNextShutterFrameNumber) {
2709 SET_ERR("Shutter notification out-of-order. Expected "
2710 "notification for frame %d, got frame %d",
2711 mNextShutterFrameNumber, msg.frame_number);
2712 return;
2713 }
2714 mNextShutterFrameNumber = msg.frame_number + 1;
2715 }
2716 }
2717
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002718 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2719 mId, __FUNCTION__,
2720 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2721 // Call listener, if any
2722 if (listener != NULL) {
2723 listener->notifyShutter(r.resultExtras, msg.timestamp);
2724 }
2725
2726 r.shutterTimestamp = msg.timestamp;
2727
2728 // send pending result and buffers
2729 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002730 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002731 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002732 returnOutputBuffers(r.pendingOutputBuffers.array(),
2733 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2734 r.pendingOutputBuffers.clear();
2735
2736 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002737 }
2738 }
2739 if (idx < 0) {
2740 SET_ERR("Shutter notification for non-existent frame number %d",
2741 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002742 }
2743}
2744
2745
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002746CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002747 ALOGV("%s", __FUNCTION__);
2748
Igor Murashkin1e479c02013-09-06 16:55:14 -07002749 CameraMetadata retVal;
2750
2751 if (mRequestThread != NULL) {
2752 retVal = mRequestThread->getLatestRequest();
2753 }
2754
Igor Murashkin1e479c02013-09-06 16:55:14 -07002755 return retVal;
2756}
2757
Jianing Weicb0652e2014-03-12 18:29:36 -07002758
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002759void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
2760 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
2761 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
2762}
2763
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002764/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002765 * RequestThread inner class methods
2766 */
2767
2768Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002769 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002770 camera3_device_t *hal3Device,
2771 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002772 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002773 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002774 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002775 mHal3Device(hal3Device),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002776 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002777 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002778 mReconfigured(false),
2779 mDoPause(false),
2780 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002781 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002782 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002783 mCurrentAfTriggerId(0),
2784 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002785 mRepeatingLastFrameNumber(
2786 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002787 mAeLockAvailable(aeLockAvailable),
2788 mPrepareVideoStream(false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002789 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002790}
2791
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002792void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002793 wp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002794 Mutex::Autolock l(mRequestLock);
2795 mListener = listener;
2796}
2797
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002798void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002799 Mutex::Autolock l(mRequestLock);
2800 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002801 // Prepare video stream for high speed recording.
2802 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002803}
2804
Jianing Wei90e59c92014-03-12 18:29:36 -07002805status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002806 List<sp<CaptureRequest> > &requests,
2807 /*out*/
2808 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002809 Mutex::Autolock l(mRequestLock);
2810 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2811 ++it) {
2812 mRequestQueue.push_back(*it);
2813 }
2814
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002815 if (lastFrameNumber != NULL) {
2816 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2817 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2818 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2819 *lastFrameNumber);
2820 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002821
Jianing Wei90e59c92014-03-12 18:29:36 -07002822 unpauseForNewRequests();
2823
2824 return OK;
2825}
2826
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002827
2828status_t Camera3Device::RequestThread::queueTrigger(
2829 RequestTrigger trigger[],
2830 size_t count) {
2831
2832 Mutex::Autolock l(mTriggerMutex);
2833 status_t ret;
2834
2835 for (size_t i = 0; i < count; ++i) {
2836 ret = queueTriggerLocked(trigger[i]);
2837
2838 if (ret != OK) {
2839 return ret;
2840 }
2841 }
2842
2843 return OK;
2844}
2845
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002846int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2847 sp<Camera3Device> d = device.promote();
2848 if (d != NULL) return d->mId;
2849 return 0;
2850}
2851
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002852status_t Camera3Device::RequestThread::queueTriggerLocked(
2853 RequestTrigger trigger) {
2854
2855 uint32_t tag = trigger.metadataTag;
2856 ssize_t index = mTriggerMap.indexOfKey(tag);
2857
2858 switch (trigger.getTagType()) {
2859 case TYPE_BYTE:
2860 // fall-through
2861 case TYPE_INT32:
2862 break;
2863 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002864 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2865 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002866 return INVALID_OPERATION;
2867 }
2868
2869 /**
2870 * Collect only the latest trigger, since we only have 1 field
2871 * in the request settings per trigger tag, and can't send more than 1
2872 * trigger per request.
2873 */
2874 if (index != NAME_NOT_FOUND) {
2875 mTriggerMap.editValueAt(index) = trigger;
2876 } else {
2877 mTriggerMap.add(tag, trigger);
2878 }
2879
2880 return OK;
2881}
2882
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002883status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002884 const RequestList &requests,
2885 /*out*/
2886 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002887 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002888 if (lastFrameNumber != NULL) {
2889 *lastFrameNumber = mRepeatingLastFrameNumber;
2890 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002891 mRepeatingRequests.clear();
2892 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2893 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002894
2895 unpauseForNewRequests();
2896
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002897 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002898 return OK;
2899}
2900
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07002901bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002902 if (mRepeatingRequests.empty()) {
2903 return false;
2904 }
2905 int32_t requestId = requestIn->mResultExtras.requestId;
2906 const RequestList &repeatRequests = mRepeatingRequests;
2907 // All repeating requests are guaranteed to have same id so only check first quest
2908 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2909 return (firstRequest->mResultExtras.requestId == requestId);
2910}
2911
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002912status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002913 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002914 return clearRepeatingRequestsLocked(lastFrameNumber);
2915
2916}
2917
2918status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002919 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002920 if (lastFrameNumber != NULL) {
2921 *lastFrameNumber = mRepeatingLastFrameNumber;
2922 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002923 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002924 return OK;
2925}
2926
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002927status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002928 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002929 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002930 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002931
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002932 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002933
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002934 // Send errors for all requests pending in the request queue, including
2935 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002936 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002937 if (listener != NULL) {
2938 for (RequestList::iterator it = mRequestQueue.begin();
2939 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002940 // Abort the input buffers for reprocess requests.
2941 if ((*it)->mInputStream != NULL) {
2942 camera3_stream_buffer_t inputBuffer;
2943 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2944 if (res != OK) {
2945 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2946 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2947 } else {
2948 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2949 if (res != OK) {
2950 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2951 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2952 }
2953 }
2954 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002955 // Set the frame number this request would have had, if it
2956 // had been submitted; this frame number will not be reused.
2957 // The requestId and burstId fields were set when the request was
2958 // submitted originally (in convertMetadataListToRequestListLocked)
2959 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002960 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002961 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002962 }
2963 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002964 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08002965
2966 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002967 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002968 if (lastFrameNumber != NULL) {
2969 *lastFrameNumber = mRepeatingLastFrameNumber;
2970 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002971 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002972 return OK;
2973}
2974
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002975status_t Camera3Device::RequestThread::flush() {
2976 ATRACE_CALL();
2977 Mutex::Autolock l(mFlushLock);
2978
2979 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2980 return mHal3Device->ops->flush(mHal3Device);
2981 }
2982
2983 return -ENOTSUP;
2984}
2985
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002986void Camera3Device::RequestThread::setPaused(bool paused) {
2987 Mutex::Autolock l(mPauseLock);
2988 mDoPause = paused;
2989 mDoPauseSignal.signal();
2990}
2991
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002992status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2993 int32_t requestId, nsecs_t timeout) {
2994 Mutex::Autolock l(mLatestRequestMutex);
2995 status_t res;
2996 while (mLatestRequestId != requestId) {
2997 nsecs_t startTime = systemTime();
2998
2999 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3000 if (res != OK) return res;
3001
3002 timeout -= (systemTime() - startTime);
3003 }
3004
3005 return OK;
3006}
3007
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003008void Camera3Device::RequestThread::requestExit() {
3009 // Call parent to set up shutdown
3010 Thread::requestExit();
3011 // The exit from any possible waits
3012 mDoPauseSignal.signal();
3013 mRequestSignal.signal();
3014}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003015
Chien-Yu Chend196d612015-06-22 19:49:01 -07003016
3017/**
3018 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
3019 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
3020 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
3021 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
3022 * request.
3023 */
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003024void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(const sp<CaptureRequest>& request) {
Chien-Yu Chend196d612015-06-22 19:49:01 -07003025 request->mAeTriggerCancelOverride.applyAeLock = false;
3026 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
3027
3028 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
3029 return;
3030 }
3031
3032 camera_metadata_entry_t aePrecaptureTrigger =
3033 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3034 if (aePrecaptureTrigger.count > 0 &&
3035 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
3036 // Always override CANCEL to IDLE
3037 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
3038 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
3039 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
3040 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
3041 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3042
3043 if (mAeLockAvailable == true) {
3044 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3045 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3046 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3047 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3048 request->mAeTriggerCancelOverride.applyAeLock = true;
3049 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3050 }
3051 }
3052 }
3053}
3054
3055/**
3056 * Override result metadata for cancelling AE precapture trigger applied in
3057 * handleAePrecaptureCancelRequest().
3058 */
3059void Camera3Device::overrideResultForPrecaptureCancel(
3060 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3061 if (aeTriggerCancelOverride.applyAeLock) {
3062 // Only devices <= v3.2 should have this override
3063 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3064 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3065 }
3066
3067 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3068 // Only devices <= v3.2 should have this override
3069 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3070 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3071 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3072 }
3073}
3074
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003075void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003076 bool surfaceAbandoned = false;
3077 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003078 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003079 {
3080 Mutex::Autolock l(mRequestLock);
3081 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3082 // repeating requests.
3083 for (const auto& request : mRepeatingRequests) {
3084 for (const auto& s : request->mOutputStreams) {
3085 if (s->isAbandoned()) {
3086 surfaceAbandoned = true;
3087 clearRepeatingRequestsLocked(&lastFrameNumber);
3088 break;
3089 }
3090 }
3091 if (surfaceAbandoned) {
3092 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003093 }
3094 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003095 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003096 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003097
3098 if (listener != NULL && surfaceAbandoned) {
3099 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003100 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003101}
3102
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003103bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003104 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003105 status_t res;
3106
3107 // Handle paused state.
3108 if (waitIfPaused()) {
3109 return true;
3110 }
3111
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003112 // Wait for the next batch of requests.
3113 waitForNextRequestBatch();
3114 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003115 return true;
3116 }
3117
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003118 // Get the latest request ID, if any
3119 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003120 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003121 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003122 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003123 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003124 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003125 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3126 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003127 }
3128
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003129 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003130 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003131 if (res == TIMED_OUT) {
3132 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003133 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003134 // Check if any stream is abandoned.
3135 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003136 return true;
3137 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003138 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003139 return false;
3140 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003141
Zhijun Hecc27e112013-10-03 16:12:43 -07003142 // Inform waitUntilRequestProcessed thread of a new request ID
3143 {
3144 Mutex::Autolock al(mLatestRequestMutex);
3145
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003146 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003147 mLatestRequestSignal.signal();
3148 }
3149
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003150 // Submit a batch of requests to HAL.
3151 // Use flush lock only when submitting multilple requests in a batch.
3152 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3153 // which may take a long time to finish so synchronizing flush() and
3154 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3155 // For now, only synchronize for high speed recording and we should figure something out for
3156 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003157 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003158
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003159 if (useFlushLock) {
3160 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003161 }
3162
Zhijun Hef0645c12016-08-02 00:58:11 -07003163 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003164 mNextRequests.size());
3165 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003166 // Submit request and block until ready for next one
3167 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3168 ATRACE_BEGIN("camera3->process_capture_request");
3169 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3170 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003171
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003172 if (res != OK) {
3173 // Should only get a failure here for malformed requests or device-level
3174 // errors, so consider all errors fatal. Bad metadata failures should
3175 // come through notify.
3176 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3177 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3178 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003179 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003180 if (useFlushLock) {
3181 mFlushLock.unlock();
3182 }
3183 return false;
3184 }
3185
3186 // Mark that the request has be submitted successfully.
3187 nextRequest.submitted = true;
3188
3189 // Update the latest request sent to HAL
3190 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3191 Mutex::Autolock al(mLatestRequestMutex);
3192
3193 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3194 mLatestRequest.acquire(cloned);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003195
3196 sp<Camera3Device> parent = mParent.promote();
3197 if (parent != NULL) {
3198 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
3199 0, mLatestRequest);
3200 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003201 }
3202
3203 if (nextRequest.halRequest.settings != NULL) {
3204 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3205 }
3206
3207 // Remove any previously queued triggers (after unlock)
3208 res = removeTriggers(mPrevRequest);
3209 if (res != OK) {
3210 SET_ERR("RequestThread: Unable to remove triggers "
3211 "(capture request %d, HAL device: %s (%d)",
3212 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003213 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003214 if (useFlushLock) {
3215 mFlushLock.unlock();
3216 }
3217 return false;
3218 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003219 }
3220
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003221 if (useFlushLock) {
3222 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003223 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003224
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003225 // Unset as current request
3226 {
3227 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003228 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003229 }
3230
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003231 return true;
3232}
3233
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003234status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003235 ATRACE_CALL();
3236
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003237 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003238 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3239 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3240 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3241
3242 // Prepare a request to HAL
3243 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3244
3245 // Insert any queued triggers (before metadata is locked)
3246 status_t res = insertTriggers(captureRequest);
3247
3248 if (res < 0) {
3249 SET_ERR("RequestThread: Unable to insert triggers "
3250 "(capture request %d, HAL device: %s (%d)",
3251 halRequest->frame_number, strerror(-res), res);
3252 return INVALID_OPERATION;
3253 }
3254 int triggerCount = res;
3255 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3256 mPrevTriggers = triggerCount;
3257
3258 // If the request is the same as last, or we had triggers last time
3259 if (mPrevRequest != captureRequest || triggersMixedIn) {
3260 /**
3261 * HAL workaround:
3262 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3263 */
3264 res = addDummyTriggerIds(captureRequest);
3265 if (res != OK) {
3266 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3267 "(capture request %d, HAL device: %s (%d)",
3268 halRequest->frame_number, strerror(-res), res);
3269 return INVALID_OPERATION;
3270 }
3271
3272 /**
3273 * The request should be presorted so accesses in HAL
3274 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3275 */
3276 captureRequest->mSettings.sort();
3277 halRequest->settings = captureRequest->mSettings.getAndLock();
3278 mPrevRequest = captureRequest;
3279 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3280
3281 IF_ALOGV() {
3282 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3283 find_camera_metadata_ro_entry(
3284 halRequest->settings,
3285 ANDROID_CONTROL_AF_TRIGGER,
3286 &e
3287 );
3288 if (e.count > 0) {
3289 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3290 __FUNCTION__,
3291 halRequest->frame_number,
3292 e.data.u8[0]);
3293 }
3294 }
3295 } else {
3296 // leave request.settings NULL to indicate 'reuse latest given'
3297 ALOGVV("%s: Request settings are REUSED",
3298 __FUNCTION__);
3299 }
3300
3301 uint32_t totalNumBuffers = 0;
3302
3303 // Fill in buffers
3304 if (captureRequest->mInputStream != NULL) {
3305 halRequest->input_buffer = &captureRequest->mInputBuffer;
3306 totalNumBuffers += 1;
3307 } else {
3308 halRequest->input_buffer = NULL;
3309 }
3310
3311 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3312 captureRequest->mOutputStreams.size());
3313 halRequest->output_buffers = outputBuffers->array();
3314 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003315 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(i);
3316
3317 // Prepare video buffers for high speed recording on the first video request.
3318 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3319 // Only try to prepare video stream on the first video request.
3320 mPrepareVideoStream = false;
3321
3322 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
3323 while (res == NOT_ENOUGH_DATA) {
3324 res = outputStream->prepareNextBuffer();
3325 }
3326 if (res != OK) {
3327 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3328 __FUNCTION__, strerror(-res), res);
3329 outputStream->cancelPrepare();
3330 }
3331 }
3332
3333 res = outputStream->getBuffer(&outputBuffers->editItemAt(i));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003334 if (res != OK) {
3335 // Can't get output buffer from gralloc queue - this could be due to
3336 // abandoned queue or other consumer misbehavior, so not a fatal
3337 // error
3338 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3339 " %s (%d)", strerror(-res), res);
3340
3341 return TIMED_OUT;
3342 }
3343 halRequest->num_output_buffers++;
3344 }
3345 totalNumBuffers += halRequest->num_output_buffers;
3346
3347 // Log request in the in-flight queue
3348 sp<Camera3Device> parent = mParent.promote();
3349 if (parent == NULL) {
3350 // Should not happen, and nowhere to send errors to, so just log it
3351 CLOGE("RequestThread: Parent is gone");
3352 return INVALID_OPERATION;
3353 }
3354 res = parent->registerInFlight(halRequest->frame_number,
3355 totalNumBuffers, captureRequest->mResultExtras,
3356 /*hasInput*/halRequest->input_buffer != NULL,
3357 captureRequest->mAeTriggerCancelOverride);
3358 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3359 ", burstId = %" PRId32 ".",
3360 __FUNCTION__,
3361 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3362 captureRequest->mResultExtras.burstId);
3363 if (res != OK) {
3364 SET_ERR("RequestThread: Unable to register new in-flight request:"
3365 " %s (%d)", strerror(-res), res);
3366 return INVALID_OPERATION;
3367 }
3368 }
3369
3370 return OK;
3371}
3372
Igor Murashkin1e479c02013-09-06 16:55:14 -07003373CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3374 Mutex::Autolock al(mLatestRequestMutex);
3375
3376 ALOGV("RequestThread::%s", __FUNCTION__);
3377
3378 return mLatestRequest;
3379}
3380
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003381bool Camera3Device::RequestThread::isStreamPending(
3382 sp<Camera3StreamInterface>& stream) {
3383 Mutex::Autolock l(mRequestLock);
3384
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003385 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003386 if (!nextRequest.submitted) {
3387 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3388 if (stream == s) return true;
3389 }
3390 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003391 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003392 }
3393
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003394 for (const auto& request : mRequestQueue) {
3395 for (const auto& s : request->mOutputStreams) {
3396 if (stream == s) return true;
3397 }
3398 if (stream == request->mInputStream) return true;
3399 }
3400
3401 for (const auto& request : mRepeatingRequests) {
3402 for (const auto& s : request->mOutputStreams) {
3403 if (stream == s) return true;
3404 }
3405 if (stream == request->mInputStream) return true;
3406 }
3407
3408 return false;
3409}
Jianing Weicb0652e2014-03-12 18:29:36 -07003410
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003411void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3412 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003413 return;
3414 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003415
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003416 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003417 // Skip the ones that have been submitted successfully.
3418 if (nextRequest.submitted) {
3419 continue;
3420 }
3421
3422 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3423 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3424 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3425
3426 if (halRequest->settings != NULL) {
3427 captureRequest->mSettings.unlock(halRequest->settings);
3428 }
3429
3430 if (captureRequest->mInputStream != NULL) {
3431 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3432 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3433 }
3434
3435 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3436 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3437 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3438 }
3439
3440 if (sendRequestError) {
3441 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003442 sp<NotificationListener> listener = mListener.promote();
3443 if (listener != NULL) {
3444 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003445 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003446 captureRequest->mResultExtras);
3447 }
3448 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003449
3450 // Remove yet-to-be submitted inflight request from inflightMap
3451 {
3452 sp<Camera3Device> parent = mParent.promote();
3453 if (parent != NULL) {
3454 Mutex::Autolock l(parent->mInFlightLock);
3455 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
3456 if (idx >= 0) {
3457 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
3458 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
3459 parent->removeInFlightMapEntryLocked(idx);
3460 }
3461 }
3462 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003463 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003464
3465 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003466 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003467}
3468
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003469void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003470 // Optimized a bit for the simple steady-state case (single repeating
3471 // request), to avoid putting that request in the queue temporarily.
3472 Mutex::Autolock l(mRequestLock);
3473
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003474 assert(mNextRequests.empty());
3475
3476 NextRequest nextRequest;
3477 nextRequest.captureRequest = waitForNextRequestLocked();
3478 if (nextRequest.captureRequest == nullptr) {
3479 return;
3480 }
3481
3482 nextRequest.halRequest = camera3_capture_request_t();
3483 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003484 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003485
3486 // Wait for additional requests
3487 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3488
3489 for (size_t i = 1; i < batchSize; i++) {
3490 NextRequest additionalRequest;
3491 additionalRequest.captureRequest = waitForNextRequestLocked();
3492 if (additionalRequest.captureRequest == nullptr) {
3493 break;
3494 }
3495
3496 additionalRequest.halRequest = camera3_capture_request_t();
3497 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003498 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003499 }
3500
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003501 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003502 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003503 mNextRequests.size(), batchSize);
3504 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003505 }
3506
3507 return;
3508}
3509
3510sp<Camera3Device::CaptureRequest>
3511 Camera3Device::RequestThread::waitForNextRequestLocked() {
3512 status_t res;
3513 sp<CaptureRequest> nextRequest;
3514
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003515 while (mRequestQueue.empty()) {
3516 if (!mRepeatingRequests.empty()) {
3517 // Always atomically enqueue all requests in a repeating request
3518 // list. Guarantees a complete in-sequence set of captures to
3519 // application.
3520 const RequestList &requests = mRepeatingRequests;
3521 RequestList::const_iterator firstRequest =
3522 requests.begin();
3523 nextRequest = *firstRequest;
3524 mRequestQueue.insert(mRequestQueue.end(),
3525 ++firstRequest,
3526 requests.end());
3527 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003528
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003529 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003530
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003531 break;
3532 }
3533
3534 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3535
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003536 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3537 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003538 Mutex::Autolock pl(mPauseLock);
3539 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003540 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003541 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003542 // Let the tracker know
3543 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3544 if (statusTracker != 0) {
3545 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3546 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003547 }
3548 // Stop waiting for now and let thread management happen
3549 return NULL;
3550 }
3551 }
3552
3553 if (nextRequest == NULL) {
3554 // Don't have a repeating request already in hand, so queue
3555 // must have an entry now.
3556 RequestList::iterator firstRequest =
3557 mRequestQueue.begin();
3558 nextRequest = *firstRequest;
3559 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07003560 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
3561 sp<NotificationListener> listener = mListener.promote();
3562 if (listener != NULL) {
3563 listener->notifyRequestQueueEmpty();
3564 }
3565 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003566 }
3567
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003568 // In case we've been unpaused by setPaused clearing mDoPause, need to
3569 // update internal pause state (capture/setRepeatingRequest unpause
3570 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003571 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003572 if (mPaused) {
3573 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3574 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3575 if (statusTracker != 0) {
3576 statusTracker->markComponentActive(mStatusId);
3577 }
3578 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003579 mPaused = false;
3580
3581 // Check if we've reconfigured since last time, and reset the preview
3582 // request if so. Can't use 'NULL request == repeat' across configure calls.
3583 if (mReconfigured) {
3584 mPrevRequest.clear();
3585 mReconfigured = false;
3586 }
3587
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003588 if (nextRequest != NULL) {
3589 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003590 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3591 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003592
3593 // Since RequestThread::clear() removes buffers from the input stream,
3594 // get the right buffer here before unlocking mRequestLock
3595 if (nextRequest->mInputStream != NULL) {
3596 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3597 if (res != OK) {
3598 // Can't get input buffer from gralloc queue - this could be due to
3599 // disconnected queue or other producer misbehavior, so not a fatal
3600 // error
3601 ALOGE("%s: Can't get input buffer, skipping request:"
3602 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003603
3604 sp<NotificationListener> listener = mListener.promote();
3605 if (listener != NULL) {
3606 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003607 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003608 nextRequest->mResultExtras);
3609 }
3610 return NULL;
3611 }
3612 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003613 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003614
3615 handleAePrecaptureCancelRequest(nextRequest);
3616
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003617 return nextRequest;
3618}
3619
3620bool Camera3Device::RequestThread::waitIfPaused() {
3621 status_t res;
3622 Mutex::Autolock l(mPauseLock);
3623 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003624 if (mPaused == false) {
3625 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003626 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3627 // Let the tracker know
3628 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3629 if (statusTracker != 0) {
3630 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3631 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003632 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003633
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003634 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003635 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003636 return true;
3637 }
3638 }
3639 // We don't set mPaused to false here, because waitForNextRequest needs
3640 // to further manage the paused state in case of starvation.
3641 return false;
3642}
3643
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003644void Camera3Device::RequestThread::unpauseForNewRequests() {
3645 // With work to do, mark thread as unpaused.
3646 // If paused by request (setPaused), don't resume, to avoid
3647 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003648 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003649 Mutex::Autolock p(mPauseLock);
3650 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003651 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3652 if (mPaused) {
3653 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3654 if (statusTracker != 0) {
3655 statusTracker->markComponentActive(mStatusId);
3656 }
3657 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003658 mPaused = false;
3659 }
3660}
3661
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003662void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3663 sp<Camera3Device> parent = mParent.promote();
3664 if (parent != NULL) {
3665 va_list args;
3666 va_start(args, fmt);
3667
3668 parent->setErrorStateV(fmt, args);
3669
3670 va_end(args);
3671 }
3672}
3673
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003674status_t Camera3Device::RequestThread::insertTriggers(
3675 const sp<CaptureRequest> &request) {
3676
3677 Mutex::Autolock al(mTriggerMutex);
3678
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003679 sp<Camera3Device> parent = mParent.promote();
3680 if (parent == NULL) {
3681 CLOGE("RequestThread: Parent is gone");
3682 return DEAD_OBJECT;
3683 }
3684
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003685 CameraMetadata &metadata = request->mSettings;
3686 size_t count = mTriggerMap.size();
3687
3688 for (size_t i = 0; i < count; ++i) {
3689 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003690 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003691
3692 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3693 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3694 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003695 if (isAeTrigger) {
3696 request->mResultExtras.precaptureTriggerId = triggerId;
3697 mCurrentPreCaptureTriggerId = triggerId;
3698 } else {
3699 request->mResultExtras.afTriggerId = triggerId;
3700 mCurrentAfTriggerId = triggerId;
3701 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003702 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3703 continue; // Trigger ID tag is deprecated since device HAL 3.2
3704 }
3705 }
3706
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003707 camera_metadata_entry entry = metadata.find(tag);
3708
3709 if (entry.count > 0) {
3710 /**
3711 * Already has an entry for this trigger in the request.
3712 * Rewrite it with our requested trigger value.
3713 */
3714 RequestTrigger oldTrigger = trigger;
3715
3716 oldTrigger.entryValue = entry.data.u8[0];
3717
3718 mTriggerReplacedMap.add(tag, oldTrigger);
3719 } else {
3720 /**
3721 * More typical, no trigger entry, so we just add it
3722 */
3723 mTriggerRemovedMap.add(tag, trigger);
3724 }
3725
3726 status_t res;
3727
3728 switch (trigger.getTagType()) {
3729 case TYPE_BYTE: {
3730 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3731 res = metadata.update(tag,
3732 &entryValue,
3733 /*count*/1);
3734 break;
3735 }
3736 case TYPE_INT32:
3737 res = metadata.update(tag,
3738 &trigger.entryValue,
3739 /*count*/1);
3740 break;
3741 default:
3742 ALOGE("%s: Type not supported: 0x%x",
3743 __FUNCTION__,
3744 trigger.getTagType());
3745 return INVALID_OPERATION;
3746 }
3747
3748 if (res != OK) {
3749 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3750 ", value %d", __FUNCTION__, trigger.getTagName(),
3751 trigger.entryValue);
3752 return res;
3753 }
3754
3755 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3756 trigger.getTagName(),
3757 trigger.entryValue);
3758 }
3759
3760 mTriggerMap.clear();
3761
3762 return count;
3763}
3764
3765status_t Camera3Device::RequestThread::removeTriggers(
3766 const sp<CaptureRequest> &request) {
3767 Mutex::Autolock al(mTriggerMutex);
3768
3769 CameraMetadata &metadata = request->mSettings;
3770
3771 /**
3772 * Replace all old entries with their old values.
3773 */
3774 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3775 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3776
3777 status_t res;
3778
3779 uint32_t tag = trigger.metadataTag;
3780 switch (trigger.getTagType()) {
3781 case TYPE_BYTE: {
3782 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3783 res = metadata.update(tag,
3784 &entryValue,
3785 /*count*/1);
3786 break;
3787 }
3788 case TYPE_INT32:
3789 res = metadata.update(tag,
3790 &trigger.entryValue,
3791 /*count*/1);
3792 break;
3793 default:
3794 ALOGE("%s: Type not supported: 0x%x",
3795 __FUNCTION__,
3796 trigger.getTagType());
3797 return INVALID_OPERATION;
3798 }
3799
3800 if (res != OK) {
3801 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3802 ", trigger value %d", __FUNCTION__,
3803 trigger.getTagName(), trigger.entryValue);
3804 return res;
3805 }
3806 }
3807 mTriggerReplacedMap.clear();
3808
3809 /**
3810 * Remove all new entries.
3811 */
3812 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3813 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3814 status_t res = metadata.erase(trigger.metadataTag);
3815
3816 if (res != OK) {
3817 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3818 ", trigger value %d", __FUNCTION__,
3819 trigger.getTagName(), trigger.entryValue);
3820 return res;
3821 }
3822 }
3823 mTriggerRemovedMap.clear();
3824
3825 return OK;
3826}
3827
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003828status_t Camera3Device::RequestThread::addDummyTriggerIds(
3829 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003830 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003831 static const int32_t dummyTriggerId = 1;
3832 status_t res;
3833
3834 CameraMetadata &metadata = request->mSettings;
3835
3836 // If AF trigger is active, insert a dummy AF trigger ID if none already
3837 // exists
3838 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3839 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3840 if (afTrigger.count > 0 &&
3841 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3842 afId.count == 0) {
3843 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3844 if (res != OK) return res;
3845 }
3846
3847 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3848 // if none already exists
3849 camera_metadata_entry pcTrigger =
3850 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3851 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3852 if (pcTrigger.count > 0 &&
3853 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3854 pcId.count == 0) {
3855 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3856 &dummyTriggerId, 1);
3857 if (res != OK) return res;
3858 }
3859
3860 return OK;
3861}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003862
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003863/**
3864 * PreparerThread inner class methods
3865 */
3866
3867Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003868 Thread(/*canCallJava*/false), mListener(nullptr),
3869 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003870}
3871
3872Camera3Device::PreparerThread::~PreparerThread() {
3873 Thread::requestExitAndWait();
3874 if (mCurrentStream != nullptr) {
3875 mCurrentStream->cancelPrepare();
3876 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3877 mCurrentStream.clear();
3878 }
3879 clear();
3880}
3881
Ruben Brunkc78ac262015-08-13 17:58:46 -07003882status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003883 status_t res;
3884
3885 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003886 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003887
Ruben Brunkc78ac262015-08-13 17:58:46 -07003888 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003889 if (res == OK) {
3890 // No preparation needed, fire listener right off
3891 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003892 if (listener != NULL) {
3893 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003894 }
3895 return OK;
3896 } else if (res != NOT_ENOUGH_DATA) {
3897 return res;
3898 }
3899
3900 // Need to prepare, start up thread if necessary
3901 if (!mActive) {
3902 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3903 // isn't running
3904 Thread::requestExitAndWait();
3905 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3906 if (res != OK) {
3907 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003908 if (listener != NULL) {
3909 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003910 }
3911 return res;
3912 }
3913 mCancelNow = false;
3914 mActive = true;
3915 ALOGV("%s: Preparer stream started", __FUNCTION__);
3916 }
3917
3918 // queue up the work
3919 mPendingStreams.push_back(stream);
3920 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3921
3922 return OK;
3923}
3924
3925status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003926 Mutex::Autolock l(mLock);
3927
3928 for (const auto& stream : mPendingStreams) {
3929 stream->cancelPrepare();
3930 }
3931 mPendingStreams.clear();
3932 mCancelNow = true;
3933
3934 return OK;
3935}
3936
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003937void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003938 Mutex::Autolock l(mLock);
3939 mListener = listener;
3940}
3941
3942bool Camera3Device::PreparerThread::threadLoop() {
3943 status_t res;
3944 {
3945 Mutex::Autolock l(mLock);
3946 if (mCurrentStream == nullptr) {
3947 // End thread if done with work
3948 if (mPendingStreams.empty()) {
3949 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3950 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3951 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3952 mActive = false;
3953 return false;
3954 }
3955
3956 // Get next stream to prepare
3957 auto it = mPendingStreams.begin();
3958 mCurrentStream = *it;
3959 mPendingStreams.erase(it);
3960 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3961 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3962 } else if (mCancelNow) {
3963 mCurrentStream->cancelPrepare();
3964 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3965 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3966 mCurrentStream.clear();
3967 mCancelNow = false;
3968 return true;
3969 }
3970 }
3971
3972 res = mCurrentStream->prepareNextBuffer();
3973 if (res == NOT_ENOUGH_DATA) return true;
3974 if (res != OK) {
3975 // Something bad happened; try to recover by cancelling prepare and
3976 // signalling listener anyway
3977 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3978 mCurrentStream->getId(), res, strerror(-res));
3979 mCurrentStream->cancelPrepare();
3980 }
3981
3982 // This stream has finished, notify listener
3983 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003984 sp<NotificationListener> listener = mListener.promote();
3985 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003986 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3987 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003988 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003989 }
3990
3991 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3992 mCurrentStream.clear();
3993
3994 return true;
3995}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003996
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003997/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003998 * Static callback forwarding methods from HAL to instance
3999 */
4000
4001void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
4002 const camera3_capture_result *result) {
4003 Camera3Device *d =
4004 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07004005
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004006 d->processCaptureResult(result);
4007}
4008
4009void Camera3Device::sNotify(const camera3_callback_ops *cb,
4010 const camera3_notify_msg *msg) {
4011 Camera3Device *d =
4012 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
4013 d->notify(msg);
4014}
4015
4016}; // namespace android