blob: eece01b46a215711736a11d0078a1e51a0cbded6 [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 Talvalae4df8ab2016-07-21 12:53:07 -0700173 /** Register in-flight map to the status tracker */
174 mInFlightStatusId = mStatusTracker->addComponent();
175
Zhijun He125684a2015-12-26 15:07:30 -0800176 /** Create buffer manager */
177 mBufferManager = new Camera3BufferManager();
178
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700179 bool aeLockAvailable = false;
180 camera_metadata_ro_entry aeLockAvailableEntry;
181 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
182 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
183 if (res == OK && aeLockAvailableEntry.count > 0) {
184 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
185 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
186 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700188 /** Start up request queue thread */
189 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800190 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800191 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700192 SET_ERR_L("Unable to start request queue thread: %s (%d)",
193 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800194 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800195 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800196 return res;
197 }
198
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700199 mPreparerThread = new PreparerThread();
200
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800201 /** Everything is good to go */
202
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700203 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800204 mDeviceInfo = info.static_camera_characteristics;
205 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700206
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700207 // Determine whether we need to derive sensitivity boost values for older devices.
208 // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
209 // be listed (as the default value 100)
210 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_4 &&
211 mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
212 mDerivePostRawSensKey = true;
213 }
214
Ruben Brunk183f0562015-08-12 12:55:02 -0700215 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800216 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700217 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700218 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700219 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800220
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800221 // Measure the clock domain offset between camera and video/hw_composer
222 camera_metadata_entry timestampSource =
223 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
224 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
225 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
226 mTimestampOffset = getMonoToBoottimeOffset();
227 }
228
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700229 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700230 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
231 camera_metadata_entry partialResultsCount =
232 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
233 if (partialResultsCount.count > 0) {
234 mNumPartialResults = partialResultsCount.data.i32[0];
235 mUsePartialResult = (mNumPartialResults > 1);
236 }
237 } else {
238 camera_metadata_entry partialResultsQuirk =
239 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
240 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
241 mUsePartialResult = true;
242 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700243 }
244
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700245 camera_metadata_entry configs =
246 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
247 for (uint32_t i = 0; i < configs.count; i += 4) {
248 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
249 configs.data.i32[i + 3] ==
250 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
251 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
252 configs.data.i32[i + 2]));
253 }
254 }
255
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800256 return OK;
257}
258
259status_t Camera3Device::disconnect() {
260 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700261 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800262
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700263 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800264
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700265 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800266
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700267 {
268 Mutex::Autolock l(mLock);
269 if (mStatus == STATUS_UNINITIALIZED) return res;
270
271 if (mStatus == STATUS_ACTIVE ||
272 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
273 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700274 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700275 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700276 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277 } else {
278 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
279 if (res != OK) {
280 SET_ERR_L("Timeout waiting for HAL to drain");
281 // Continue to close device even in case of error
282 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700283 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800284 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800285
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700286 if (mStatus == STATUS_ERROR) {
287 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700288 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700289
290 if (mStatusTracker != NULL) {
291 mStatusTracker->requestExit();
292 }
293
294 if (mRequestThread != NULL) {
295 mRequestThread->requestExit();
296 }
297
298 mOutputStreams.clear();
299 mInputStream.clear();
300 }
301
302 // Joining done without holding mLock, otherwise deadlocks may ensue
303 // as the threads try to access parent state
304 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
305 // HAL may be in a bad state, so waiting for request thread
306 // (which may be stuck in the HAL processCaptureRequest call)
307 // could be dangerous.
308 mRequestThread->join();
309 }
310
311 if (mStatusTracker != NULL) {
312 mStatusTracker->join();
313 }
314
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700315 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700316 {
317 Mutex::Autolock l(mLock);
318
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800319 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700320 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800321 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800322
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700323 hal3Device = mHal3Device;
324 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800325
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700326 // Call close without internal mutex held, as the HAL close may need to
327 // wait on assorted callbacks,etc, to complete before it can return.
328 if (hal3Device != NULL) {
329 ATRACE_BEGIN("camera3->close");
330 hal3Device->common.close(&hal3Device->common);
331 ATRACE_END();
332 }
333
334 {
335 Mutex::Autolock l(mLock);
336 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700337 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700338 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800339
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700340 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700341 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800342}
343
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700344// For dumping/debugging only -
345// try to acquire a lock a few times, eventually give up to proceed with
346// debug/dump operations
347bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
348 bool gotLock = false;
349 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
350 if (lock.tryLock() == NO_ERROR) {
351 gotLock = true;
352 break;
353 } else {
354 usleep(kDumpSleepDuration);
355 }
356 }
357 return gotLock;
358}
359
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700360Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
361 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
362 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
363 const int STREAM_CONFIGURATION_SIZE = 4;
364 const int STREAM_FORMAT_OFFSET = 0;
365 const int STREAM_WIDTH_OFFSET = 1;
366 const int STREAM_HEIGHT_OFFSET = 2;
367 const int STREAM_IS_INPUT_OFFSET = 3;
368 camera_metadata_ro_entry_t availableStreamConfigs =
369 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
370 if (availableStreamConfigs.count == 0 ||
371 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
372 return Size(0, 0);
373 }
374
375 // Get max jpeg size (area-wise).
376 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
377 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
378 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
379 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
380 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
381 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
382 && format == HAL_PIXEL_FORMAT_BLOB &&
383 (width * height > maxJpegWidth * maxJpegHeight)) {
384 maxJpegWidth = width;
385 maxJpegHeight = height;
386 }
387 }
388 } else {
389 camera_metadata_ro_entry availableJpegSizes =
390 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
391 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
392 return Size(0, 0);
393 }
394
395 // Get max jpeg size (area-wise).
396 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
397 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
398 > (maxJpegWidth * maxJpegHeight)) {
399 maxJpegWidth = availableJpegSizes.data.i32[i];
400 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
401 }
402 }
403 }
404 return Size(maxJpegWidth, maxJpegHeight);
405}
406
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800407nsecs_t Camera3Device::getMonoToBoottimeOffset() {
408 // try three times to get the clock offset, choose the one
409 // with the minimum gap in measurements.
410 const int tries = 3;
411 nsecs_t bestGap, measured;
412 for (int i = 0; i < tries; ++i) {
413 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
414 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
415 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
416 const nsecs_t gap = tmono2 - tmono;
417 if (i == 0 || gap < bestGap) {
418 bestGap = gap;
419 measured = tbase - ((tmono + tmono2) >> 1);
420 }
421 }
422 return measured;
423}
424
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -0700425/**
426 * Map Android N dataspace definitions back to Android M definitions, for
427 * use with HALv3.3 or older.
428 *
429 * Only map where correspondences exist, and otherwise preserve the value.
430 */
431android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) {
432 switch (dataSpace) {
433 case HAL_DATASPACE_V0_SRGB_LINEAR:
434 return HAL_DATASPACE_SRGB_LINEAR;
435 case HAL_DATASPACE_V0_SRGB:
436 return HAL_DATASPACE_SRGB;
437 case HAL_DATASPACE_V0_JFIF:
438 return HAL_DATASPACE_JFIF;
439 case HAL_DATASPACE_V0_BT601_625:
440 return HAL_DATASPACE_BT601_625;
441 case HAL_DATASPACE_V0_BT601_525:
442 return HAL_DATASPACE_BT601_525;
443 case HAL_DATASPACE_V0_BT709:
444 return HAL_DATASPACE_BT709;
445 default:
446 return dataSpace;
447 }
448}
449
Zhijun Hef7da0962014-04-24 13:27:56 -0700450ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700451 // Get max jpeg size (area-wise).
452 Size maxJpegResolution = getMaxJpegResolution();
453 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700454 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700455 __FUNCTION__, mId);
456 return BAD_VALUE;
457 }
458
Zhijun Hef7da0962014-04-24 13:27:56 -0700459 // Get max jpeg buffer size
460 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700461 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
462 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700463 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
464 return BAD_VALUE;
465 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700466 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800467 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700468
469 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700470 float scaleFactor = ((float) (width * height)) /
471 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800472 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
473 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700474 if (jpegBufferSize > maxJpegBufferSize) {
475 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700476 }
477
478 return jpegBufferSize;
479}
480
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700481ssize_t Camera3Device::getPointCloudBufferSize() const {
482 const int FLOATS_PER_POINT=4;
483 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
484 if (maxPointCount.count == 0) {
485 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
486 __FUNCTION__, mId);
487 return BAD_VALUE;
488 }
489 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
490 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
491 return maxBytesForPointCloud;
492}
493
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800494ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800495 const int PER_CONFIGURATION_SIZE = 3;
496 const int WIDTH_OFFSET = 0;
497 const int HEIGHT_OFFSET = 1;
498 const int SIZE_OFFSET = 2;
499 camera_metadata_ro_entry rawOpaqueSizes =
500 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800501 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800502 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800503 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800504 __FUNCTION__, mId, count);
505 return BAD_VALUE;
506 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700507
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800508 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
509 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
510 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
511 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
512 }
513 }
514
515 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
516 __FUNCTION__, mId, width, height);
517 return BAD_VALUE;
518}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700519
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800520status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
521 ATRACE_CALL();
522 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700523
524 // Try to lock, but continue in case of failure (to avoid blocking in
525 // deadlocks)
526 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
527 bool gotLock = tryLockSpinRightRound(mLock);
528
529 ALOGW_IF(!gotInterfaceLock,
530 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
531 mId, __FUNCTION__);
532 ALOGW_IF(!gotLock,
533 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
534 mId, __FUNCTION__);
535
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800536 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700537
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800538 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700539 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800540 int n = args.size();
541 for (int i = 0; i < n; i++) {
542 if (args[i] == templatesOption) {
543 dumpTemplates = true;
544 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700545 if (args[i] == monitorOption) {
546 if (i + 1 < n) {
547 String8 monitorTags = String8(args[i + 1]);
548 if (monitorTags == "off") {
549 mTagMonitor.disableMonitoring();
550 } else {
551 mTagMonitor.parseTagsToMonitor(monitorTags);
552 }
553 } else {
554 mTagMonitor.disableMonitoring();
555 }
556 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800557 }
558
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800559 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800560
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800561 const char *status =
562 mStatus == STATUS_ERROR ? "ERROR" :
563 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700564 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
565 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800566 mStatus == STATUS_ACTIVE ? "ACTIVE" :
567 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700568
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800569 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700570 if (mStatus == STATUS_ERROR) {
571 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
572 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800573 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700574 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700575 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800576
577 if (mInputStream != NULL) {
578 write(fd, lines.string(), lines.size());
579 mInputStream->dump(fd, args);
580 } else {
581 lines.appendFormat(" No input stream.\n");
582 write(fd, lines.string(), lines.size());
583 }
584 for (size_t i = 0; i < mOutputStreams.size(); i++) {
585 mOutputStreams[i]->dump(fd,args);
586 }
587
Zhijun He431503c2016-03-07 17:30:16 -0800588 if (mBufferManager != NULL) {
589 lines = String8(" Camera3 Buffer Manager:\n");
590 write(fd, lines.string(), lines.size());
591 mBufferManager->dump(fd, args);
592 }
Zhijun He125684a2015-12-26 15:07:30 -0800593
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700594 lines = String8(" In-flight requests:\n");
595 if (mInFlightMap.size() == 0) {
596 lines.append(" None\n");
597 } else {
598 for (size_t i = 0; i < mInFlightMap.size(); i++) {
599 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700600 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700601 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800602 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700603 r.numBuffersLeft);
604 }
605 }
606 write(fd, lines.string(), lines.size());
607
Igor Murashkin1e479c02013-09-06 16:55:14 -0700608 {
609 lines = String8(" Last request sent:\n");
610 write(fd, lines.string(), lines.size());
611
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700612 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700613 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
614 }
615
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800616 if (dumpTemplates) {
617 const char *templateNames[] = {
618 "TEMPLATE_PREVIEW",
619 "TEMPLATE_STILL_CAPTURE",
620 "TEMPLATE_VIDEO_RECORD",
621 "TEMPLATE_VIDEO_SNAPSHOT",
622 "TEMPLATE_ZERO_SHUTTER_LAG",
623 "TEMPLATE_MANUAL"
624 };
625
626 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
627 const camera_metadata_t *templateRequest;
628 templateRequest =
629 mHal3Device->ops->construct_default_request_settings(
630 mHal3Device, i);
631 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
632 if (templateRequest == NULL) {
633 lines.append(" Not supported\n");
634 write(fd, lines.string(), lines.size());
635 } else {
636 write(fd, lines.string(), lines.size());
637 dump_indented_camera_metadata(templateRequest,
638 fd, /*verbosity*/2, /*indentation*/8);
639 }
640 }
641 }
642
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700643 mTagMonitor.dumpMonitoredMetadata(fd);
644
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800645 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700646 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800647 write(fd, lines.string(), lines.size());
648 mHal3Device->ops->dump(mHal3Device, fd);
649 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800650
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700651 if (gotLock) mLock.unlock();
652 if (gotInterfaceLock) mInterfaceLock.unlock();
653
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800654 return OK;
655}
656
657const CameraMetadata& Camera3Device::info() const {
658 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800659 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
660 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700661 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800662 mStatus == STATUS_ERROR ?
663 "when in error state" : "before init");
664 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800665 return mDeviceInfo;
666}
667
Jianing Wei90e59c92014-03-12 18:29:36 -0700668status_t Camera3Device::checkStatusOkToCaptureLocked() {
669 switch (mStatus) {
670 case STATUS_ERROR:
671 CLOGE("Device has encountered a serious error");
672 return INVALID_OPERATION;
673 case STATUS_UNINITIALIZED:
674 CLOGE("Device not initialized");
675 return INVALID_OPERATION;
676 case STATUS_UNCONFIGURED:
677 case STATUS_CONFIGURED:
678 case STATUS_ACTIVE:
679 // OK
680 break;
681 default:
682 SET_ERR_L("Unexpected status: %d", mStatus);
683 return INVALID_OPERATION;
684 }
685 return OK;
686}
687
688status_t Camera3Device::convertMetadataListToRequestListLocked(
689 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
690 if (requestList == NULL) {
691 CLOGE("requestList cannot be NULL.");
692 return BAD_VALUE;
693 }
694
Jianing Weicb0652e2014-03-12 18:29:36 -0700695 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700696 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
697 it != metadataList.end(); ++it) {
698 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
699 if (newRequest == 0) {
700 CLOGE("Can't create capture request");
701 return BAD_VALUE;
702 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700703
704 // Setup burst Id and request Id
705 newRequest->mResultExtras.burstId = burstId++;
706 if (it->exists(ANDROID_REQUEST_ID)) {
707 if (it->find(ANDROID_REQUEST_ID).count == 0) {
708 CLOGE("RequestID entry exists; but must not be empty in metadata");
709 return BAD_VALUE;
710 }
711 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
712 } else {
713 CLOGE("RequestID does not exist in metadata");
714 return BAD_VALUE;
715 }
716
Jianing Wei90e59c92014-03-12 18:29:36 -0700717 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700718
719 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700720 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700721
722 // Setup batch size if this is a high speed video recording request.
723 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
724 auto firstRequest = requestList->begin();
725 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
726 if (outputStream->isVideoStream()) {
727 (*firstRequest)->mBatchSize = requestList->size();
728 break;
729 }
730 }
731 }
732
Jianing Wei90e59c92014-03-12 18:29:36 -0700733 return OK;
734}
735
Jianing Weicb0652e2014-03-12 18:29:36 -0700736status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800737 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800738
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700739 List<const CameraMetadata> requests;
740 requests.push_back(request);
741 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800742}
743
Jianing Wei90e59c92014-03-12 18:29:36 -0700744status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700745 const List<const CameraMetadata> &requests, bool repeating,
746 /*out*/
747 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700748 ATRACE_CALL();
749 Mutex::Autolock il(mInterfaceLock);
750 Mutex::Autolock l(mLock);
751
752 status_t res = checkStatusOkToCaptureLocked();
753 if (res != OK) {
754 // error logged by previous call
755 return res;
756 }
757
758 RequestList requestList;
759
760 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
761 if (res != OK) {
762 // error logged by previous call
763 return res;
764 }
765
766 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700767 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700768 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700769 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700770 }
771
772 if (res == OK) {
773 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
774 if (res != OK) {
775 SET_ERR_L("Can't transition to active in %f seconds!",
776 kActiveTimeout/1e9);
777 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700778 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
779 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700780 } else {
781 CLOGE("Cannot queue request. Impossible.");
782 return BAD_VALUE;
783 }
784
785 return res;
786}
787
Jianing Weicb0652e2014-03-12 18:29:36 -0700788status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
789 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700790 ATRACE_CALL();
791
Jianing Weicb0652e2014-03-12 18:29:36 -0700792 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700793}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800794
Jianing Weicb0652e2014-03-12 18:29:36 -0700795status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
796 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800797 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800798
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700799 List<const CameraMetadata> requests;
800 requests.push_back(request);
801 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800802}
803
Jianing Weicb0652e2014-03-12 18:29:36 -0700804status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
805 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700806 ATRACE_CALL();
807
Jianing Weicb0652e2014-03-12 18:29:36 -0700808 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700809}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800810
811sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
812 const CameraMetadata &request) {
813 status_t res;
814
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700815 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800816 res = configureStreamsLocked();
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700817 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800818 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700819 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800820 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700821 } else if (mStatus == STATUS_UNCONFIGURED) {
822 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700823 CLOGE("No streams configured");
824 return NULL;
825 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800826 }
827
828 sp<CaptureRequest> newRequest = createCaptureRequest(request);
829 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800830}
831
Jianing Weicb0652e2014-03-12 18:29:36 -0700832status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800833 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700834 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800835 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800836
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800837 switch (mStatus) {
838 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700839 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800840 return INVALID_OPERATION;
841 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700842 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800843 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700844 case STATUS_UNCONFIGURED:
845 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800846 case STATUS_ACTIVE:
847 // OK
848 break;
849 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700850 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800851 return INVALID_OPERATION;
852 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700853 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700854
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700855 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800856}
857
858status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
859 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700860 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800861
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700862 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800863}
864
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700865status_t Camera3Device::createInputStream(
866 uint32_t width, uint32_t height, int format, int *id) {
867 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700868 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700869 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700870 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
871 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700872
873 status_t res;
874 bool wasActive = false;
875
876 switch (mStatus) {
877 case STATUS_ERROR:
878 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
879 return INVALID_OPERATION;
880 case STATUS_UNINITIALIZED:
881 ALOGE("%s: Device not initialized", __FUNCTION__);
882 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700883 case STATUS_UNCONFIGURED:
884 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700885 // OK
886 break;
887 case STATUS_ACTIVE:
888 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700889 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700890 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700891 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700892 return res;
893 }
894 wasActive = true;
895 break;
896 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700897 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700898 return INVALID_OPERATION;
899 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700900 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700901
902 if (mInputStream != 0) {
903 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
904 return INVALID_OPERATION;
905 }
906
907 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
908 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700909 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700910
911 mInputStream = newStream;
912
913 *id = mNextStreamId++;
914
915 // Continue captures if active at start
916 if (wasActive) {
917 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
918 res = configureStreamsLocked();
919 if (res != OK) {
920 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
921 __FUNCTION__, mNextStreamId, strerror(-res), res);
922 return res;
923 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700924 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700925 }
926
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700927 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700928 return OK;
929}
930
Igor Murashkin2fba5842013-04-22 14:03:54 -0700931
932status_t Camera3Device::createZslStream(
933 uint32_t width, uint32_t height,
934 int depth,
935 /*out*/
936 int *id,
937 sp<Camera3ZslStream>* zslStream) {
938 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700939 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700940 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700941 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
942 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700943
944 status_t res;
945 bool wasActive = false;
946
947 switch (mStatus) {
948 case STATUS_ERROR:
949 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
950 return INVALID_OPERATION;
951 case STATUS_UNINITIALIZED:
952 ALOGE("%s: Device not initialized", __FUNCTION__);
953 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700954 case STATUS_UNCONFIGURED:
955 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700956 // OK
957 break;
958 case STATUS_ACTIVE:
959 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700960 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700961 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700962 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700963 return res;
964 }
965 wasActive = true;
966 break;
967 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700968 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700969 return INVALID_OPERATION;
970 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700971 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700972
973 if (mInputStream != 0) {
974 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
975 return INVALID_OPERATION;
976 }
977
978 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
979 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700980 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700981
982 res = mOutputStreams.add(mNextStreamId, newStream);
983 if (res < 0) {
984 ALOGE("%s: Can't add new stream to set: %s (%d)",
985 __FUNCTION__, strerror(-res), res);
986 return res;
987 }
988 mInputStream = newStream;
989
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530990 mNeedConfig = true;
991
Igor Murashkin2fba5842013-04-22 14:03:54 -0700992 *id = mNextStreamId++;
993 *zslStream = newStream;
994
995 // Continue captures if active at start
996 if (wasActive) {
997 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
998 res = configureStreamsLocked();
999 if (res != OK) {
1000 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1001 __FUNCTION__, mNextStreamId, strerror(-res), res);
1002 return res;
1003 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001004 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -07001005 }
1006
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001007 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -07001008 return OK;
1009}
1010
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001011status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001012 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -07001013 camera3_stream_rotation_t rotation, int *id, int streamSetId, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001014 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001015 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001016 Mutex::Autolock l(mLock);
Zhijun He5d677d12016-05-29 16:52:39 -07001017 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
1018 " consumer usage 0x%x", mId, mNextStreamId, width, height, format, dataSpace, rotation,
1019 consumerUsage);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001020
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001021 status_t res;
1022 bool wasActive = false;
1023
1024 switch (mStatus) {
1025 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001026 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001027 return INVALID_OPERATION;
1028 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001029 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001030 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001031 case STATUS_UNCONFIGURED:
1032 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001033 // OK
1034 break;
1035 case STATUS_ACTIVE:
1036 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001037 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001038 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001039 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001040 return res;
1041 }
1042 wasActive = true;
1043 break;
1044 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001045 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001046 return INVALID_OPERATION;
1047 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001048 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001049
1050 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001051 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001052 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001053 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001054 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1055 }
Zhijun He5d677d12016-05-29 16:52:39 -07001056
1057 // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
1058 // which requires a consumer surface to be available.
1059 if (consumer == nullptr && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1060 ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
1061 return BAD_VALUE;
1062 }
1063
1064 if (consumer == nullptr && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1065 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1066 return BAD_VALUE;
1067 }
1068
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001069 // Use legacy dataspace values for older HALs
1070 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1071 dataSpace = mapToLegacyDataspace(dataSpace);
1072 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001073 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001074 ssize_t blobBufferSize;
1075 if (dataSpace != HAL_DATASPACE_DEPTH) {
1076 blobBufferSize = getJpegBufferSize(width, height);
1077 if (blobBufferSize <= 0) {
1078 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1079 return BAD_VALUE;
1080 }
1081 } else {
1082 blobBufferSize = getPointCloudBufferSize();
1083 if (blobBufferSize <= 0) {
1084 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1085 return BAD_VALUE;
1086 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001087 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001088 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001089 width, height, blobBufferSize, format, dataSpace, rotation,
1090 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001091 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1092 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1093 if (rawOpaqueBufferSize <= 0) {
1094 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1095 return BAD_VALUE;
1096 }
1097 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001098 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1099 mTimestampOffset, streamSetId);
Zhijun He5d677d12016-05-29 16:52:39 -07001100 } else if (consumer == nullptr) {
1101 newStream = new Camera3OutputStream(mNextStreamId,
1102 width, height, format, consumerUsage, dataSpace, rotation,
1103 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001104 } else {
1105 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001106 width, height, format, dataSpace, rotation,
1107 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001108 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001109 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001110
Zhijun He125684a2015-12-26 15:07:30 -08001111 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001112 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1113 * requires buffers to be statically allocated for internal static buffer registration, while
1114 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1115 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001116 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001117 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001118 newStream->setBufferManager(mBufferManager);
1119 }
1120
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001121 res = mOutputStreams.add(mNextStreamId, newStream);
1122 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001123 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001124 return res;
1125 }
1126
1127 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001128 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001129
1130 // Continue captures if active at start
1131 if (wasActive) {
1132 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1133 res = configureStreamsLocked();
1134 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001135 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1136 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001137 return res;
1138 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001139 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001140 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001141 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001142 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001143}
1144
1145status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1146 ATRACE_CALL();
1147 (void)outputId; (void)id;
1148
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001149 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001150 return INVALID_OPERATION;
1151}
1152
1153
1154status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001155 uint32_t *width, uint32_t *height,
1156 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001157 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001158 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001159 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001160
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001161 switch (mStatus) {
1162 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001163 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001164 return INVALID_OPERATION;
1165 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001166 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001167 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001168 case STATUS_UNCONFIGURED:
1169 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001170 case STATUS_ACTIVE:
1171 // OK
1172 break;
1173 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001174 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001175 return INVALID_OPERATION;
1176 }
1177
1178 ssize_t idx = mOutputStreams.indexOfKey(id);
1179 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001180 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001181 return idx;
1182 }
1183
1184 if (width) *width = mOutputStreams[idx]->getWidth();
1185 if (height) *height = mOutputStreams[idx]->getHeight();
1186 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001187 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001188 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001189}
1190
1191status_t Camera3Device::setStreamTransform(int id,
1192 int transform) {
1193 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001194 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001195 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001196
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001197 switch (mStatus) {
1198 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001199 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001200 return INVALID_OPERATION;
1201 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001202 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001203 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001204 case STATUS_UNCONFIGURED:
1205 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001206 case STATUS_ACTIVE:
1207 // OK
1208 break;
1209 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001210 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001211 return INVALID_OPERATION;
1212 }
1213
1214 ssize_t idx = mOutputStreams.indexOfKey(id);
1215 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001216 CLOGE("Stream %d does not exist",
1217 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001218 return BAD_VALUE;
1219 }
1220
1221 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001222}
1223
1224status_t Camera3Device::deleteStream(int id) {
1225 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001226 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001227 Mutex::Autolock l(mLock);
1228 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001229
Igor Murashkine2172be2013-05-28 15:31:39 -07001230 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1231
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001232 // CameraDevice semantics require device to already be idle before
1233 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001234 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001235 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1236 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001237 }
1238
Igor Murashkin2fba5842013-04-22 14:03:54 -07001239 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001240 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001241 if (mInputStream != NULL && id == mInputStream->getId()) {
1242 deletedStream = mInputStream;
1243 mInputStream.clear();
1244 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001245 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001246 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001247 return BAD_VALUE;
1248 }
Zhijun He5f446352014-01-22 09:49:33 -08001249 }
1250
1251 // Delete output stream or the output part of a bi-directional stream.
1252 if (outputStreamIdx != NAME_NOT_FOUND) {
1253 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001254 mOutputStreams.removeItem(id);
1255 }
1256
1257 // Free up the stream endpoint so that it can be used by some other stream
1258 res = deletedStream->disconnect();
1259 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001260 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001261 // fall through since we want to still list the stream as deleted.
1262 }
1263 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001264 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001265
1266 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001267}
1268
1269status_t Camera3Device::deleteReprocessStream(int id) {
1270 ATRACE_CALL();
1271 (void)id;
1272
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001273 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001274 return INVALID_OPERATION;
1275}
1276
Zhijun He1fa89992015-06-01 15:44:31 -07001277status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001278 ATRACE_CALL();
1279 ALOGV("%s: E", __FUNCTION__);
1280
1281 Mutex::Autolock il(mInterfaceLock);
1282 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001283
1284 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1285 mNeedConfig = true;
1286 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1287 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001288
1289 return configureStreamsLocked();
1290}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001291
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001292status_t Camera3Device::getInputBufferProducer(
1293 sp<IGraphicBufferProducer> *producer) {
1294 Mutex::Autolock il(mInterfaceLock);
1295 Mutex::Autolock l(mLock);
1296
1297 if (producer == NULL) {
1298 return BAD_VALUE;
1299 } else if (mInputStream == NULL) {
1300 return INVALID_OPERATION;
1301 }
1302
1303 return mInputStream->getInputBufferProducer(producer);
1304}
1305
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001306status_t Camera3Device::createDefaultRequest(int templateId,
1307 CameraMetadata *request) {
1308 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001309 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001310
1311 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1312 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1313 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1314 return BAD_VALUE;
1315 }
1316
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001317 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001318 Mutex::Autolock l(mLock);
1319
1320 switch (mStatus) {
1321 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001322 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001323 return INVALID_OPERATION;
1324 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001325 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001326 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001327 case STATUS_UNCONFIGURED:
1328 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001329 case STATUS_ACTIVE:
1330 // OK
1331 break;
1332 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001333 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001334 return INVALID_OPERATION;
1335 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001336
Zhijun Hea1530f12014-09-14 12:44:20 -07001337 if (!mRequestTemplateCache[templateId].isEmpty()) {
1338 *request = mRequestTemplateCache[templateId];
1339 return OK;
1340 }
1341
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001342 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001343 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001344 rawRequest = mHal3Device->ops->construct_default_request_settings(
1345 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001346 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001347 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001348 ALOGI("%s: template %d is not supported on this camera device",
1349 __FUNCTION__, templateId);
1350 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001351 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001352
Zhijun Hea1530f12014-09-14 12:44:20 -07001353 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001354
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001355 // Derive some new keys for backward compatibility
1356 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
1357 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
1358 int32_t defaultBoost[1] = {100};
1359 mRequestTemplateCache[templateId].update(
1360 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1361 defaultBoost, 1);
1362 }
1363
1364 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001365 return OK;
1366}
1367
1368status_t Camera3Device::waitUntilDrained() {
1369 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001370 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001371 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001372
Zhijun He69a37482014-03-23 18:44:49 -07001373 return waitUntilDrainedLocked();
1374}
1375
1376status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001377 switch (mStatus) {
1378 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001379 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001380 ALOGV("%s: Already idle", __FUNCTION__);
1381 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001382 case STATUS_CONFIGURED:
1383 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001384 case STATUS_ERROR:
1385 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001386 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001387 break;
1388 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001389 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001390 return INVALID_OPERATION;
1391 }
1392
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001393 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1394 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001395 if (res != OK) {
1396 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1397 res);
1398 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001399 return res;
1400}
1401
Ruben Brunk183f0562015-08-12 12:55:02 -07001402
1403void Camera3Device::internalUpdateStatusLocked(Status status) {
1404 mStatus = status;
1405 mRecentStatusUpdates.add(mStatus);
1406 mStatusChanged.broadcast();
1407}
1408
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001409// Pause to reconfigure
1410status_t Camera3Device::internalPauseAndWaitLocked() {
1411 mRequestThread->setPaused(true);
1412 mPauseStateNotify = true;
1413
1414 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1415 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1416 if (res != OK) {
1417 SET_ERR_L("Can't idle device in %f seconds!",
1418 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001419 }
1420
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001421 return res;
1422}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001423
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001424// Resume after internalPauseAndWaitLocked
1425status_t Camera3Device::internalResumeLocked() {
1426 status_t res;
1427
1428 mRequestThread->setPaused(false);
1429
1430 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1431 if (res != OK) {
1432 SET_ERR_L("Can't transition to active in %f seconds!",
1433 kActiveTimeout/1e9);
1434 }
1435 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001436 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001437}
1438
Ruben Brunk183f0562015-08-12 12:55:02 -07001439status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001440 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001441
1442 size_t startIndex = 0;
1443 if (mStatusWaiters == 0) {
1444 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1445 // this status list
1446 mRecentStatusUpdates.clear();
1447 } else {
1448 // If other threads are waiting on updates to this status list, set the position of the
1449 // first element that this list will check rather than clearing the list.
1450 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001451 }
1452
Ruben Brunk183f0562015-08-12 12:55:02 -07001453 mStatusWaiters++;
1454
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001455 bool stateSeen = false;
1456 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001457 if (active == (mStatus == STATUS_ACTIVE)) {
1458 // Desired state is current
1459 break;
1460 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001461
1462 res = mStatusChanged.waitRelative(mLock, timeout);
1463 if (res != OK) break;
1464
Ruben Brunk183f0562015-08-12 12:55:02 -07001465 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1466 // transitions.
1467 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1468 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1469 __FUNCTION__);
1470
1471 // Encountered desired state since we began waiting
1472 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001473 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1474 stateSeen = true;
1475 break;
1476 }
1477 }
1478 } while (!stateSeen);
1479
Ruben Brunk183f0562015-08-12 12:55:02 -07001480 mStatusWaiters--;
1481
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001482 return res;
1483}
1484
1485
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001486status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001487 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001488 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001489
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001490 if (listener != NULL && mListener != NULL) {
1491 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1492 }
1493 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001494 mRequestThread->setNotificationListener(listener);
1495 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001496
1497 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001498}
1499
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001500bool Camera3Device::willNotify3A() {
1501 return false;
1502}
1503
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001504status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001505 status_t res;
1506 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001507
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001508 while (mResultQueue.empty()) {
1509 res = mResultSignal.waitRelative(mOutputLock, timeout);
1510 if (res == TIMED_OUT) {
1511 return res;
1512 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001513 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001514 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001515 return res;
1516 }
1517 }
1518 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001519}
1520
Jianing Weicb0652e2014-03-12 18:29:36 -07001521status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001522 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001523 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001524
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001525 if (mResultQueue.empty()) {
1526 return NOT_ENOUGH_DATA;
1527 }
1528
Jianing Weicb0652e2014-03-12 18:29:36 -07001529 if (frame == NULL) {
1530 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1531 return BAD_VALUE;
1532 }
1533
1534 CaptureResult &result = *(mResultQueue.begin());
1535 frame->mResultExtras = result.mResultExtras;
1536 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001537 mResultQueue.erase(mResultQueue.begin());
1538
1539 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001540}
1541
1542status_t Camera3Device::triggerAutofocus(uint32_t id) {
1543 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001544 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001545
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001546 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1547 // Mix-in this trigger into the next request and only the next request.
1548 RequestTrigger trigger[] = {
1549 {
1550 ANDROID_CONTROL_AF_TRIGGER,
1551 ANDROID_CONTROL_AF_TRIGGER_START
1552 },
1553 {
1554 ANDROID_CONTROL_AF_TRIGGER_ID,
1555 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001556 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001557 };
1558
1559 return mRequestThread->queueTrigger(trigger,
1560 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001561}
1562
1563status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1564 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001565 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001566
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001567 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1568 // Mix-in this trigger into the next request and only the next request.
1569 RequestTrigger trigger[] = {
1570 {
1571 ANDROID_CONTROL_AF_TRIGGER,
1572 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1573 },
1574 {
1575 ANDROID_CONTROL_AF_TRIGGER_ID,
1576 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001577 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001578 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001579
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001580 return mRequestThread->queueTrigger(trigger,
1581 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001582}
1583
1584status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1585 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001586 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001587
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001588 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1589 // Mix-in this trigger into the next request and only the next request.
1590 RequestTrigger trigger[] = {
1591 {
1592 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1593 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1594 },
1595 {
1596 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1597 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001598 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001599 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001600
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001601 return mRequestThread->queueTrigger(trigger,
1602 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001603}
1604
1605status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1606 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1607 ATRACE_CALL();
1608 (void)reprocessStreamId; (void)buffer; (void)listener;
1609
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001610 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001611 return INVALID_OPERATION;
1612}
1613
Jianing Weicb0652e2014-03-12 18:29:36 -07001614status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001615 ATRACE_CALL();
1616 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001617 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001618
Zhijun He7ef20392014-04-21 16:04:17 -07001619 {
1620 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001621 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001622 }
1623
Zhijun He491e3412013-12-27 10:57:44 -08001624 status_t res;
1625 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001626 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001627 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001628 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001629 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001630 }
1631
1632 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001633}
1634
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001635status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001636 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1637}
1638
1639status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001640 ATRACE_CALL();
1641 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001642 Mutex::Autolock il(mInterfaceLock);
1643 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001644
1645 sp<Camera3StreamInterface> stream;
1646 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1647 if (outputStreamIdx == NAME_NOT_FOUND) {
1648 CLOGE("Stream %d does not exist", streamId);
1649 return BAD_VALUE;
1650 }
1651
1652 stream = mOutputStreams.editValueAt(outputStreamIdx);
1653
1654 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001655 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001656 return BAD_VALUE;
1657 }
1658
1659 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001660 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001661 return BAD_VALUE;
1662 }
1663
Ruben Brunkc78ac262015-08-13 17:58:46 -07001664 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001665}
1666
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001667status_t Camera3Device::tearDown(int streamId) {
1668 ATRACE_CALL();
1669 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1670 Mutex::Autolock il(mInterfaceLock);
1671 Mutex::Autolock l(mLock);
1672
1673 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1674 // since we cannot call register_stream_buffers except right after configure_streams.
1675 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1676 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1677 __FUNCTION__, mHal3Device->common.version);
1678 return NO_INIT;
1679 }
1680
1681 sp<Camera3StreamInterface> stream;
1682 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1683 if (outputStreamIdx == NAME_NOT_FOUND) {
1684 CLOGE("Stream %d does not exist", streamId);
1685 return BAD_VALUE;
1686 }
1687
1688 stream = mOutputStreams.editValueAt(outputStreamIdx);
1689
1690 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1691 CLOGE("Stream %d is a target of a in-progress request", streamId);
1692 return BAD_VALUE;
1693 }
1694
1695 return stream->tearDown();
1696}
1697
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001698status_t Camera3Device::addBufferListenerForStream(int streamId,
1699 wp<Camera3StreamBufferListener> listener) {
1700 ATRACE_CALL();
1701 ALOGV("%s: Camera %d: Adding buffer listener for stream %d", __FUNCTION__, mId, streamId);
1702 Mutex::Autolock il(mInterfaceLock);
1703 Mutex::Autolock l(mLock);
1704
1705 sp<Camera3StreamInterface> stream;
1706 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1707 if (outputStreamIdx == NAME_NOT_FOUND) {
1708 CLOGE("Stream %d does not exist", streamId);
1709 return BAD_VALUE;
1710 }
1711
1712 stream = mOutputStreams.editValueAt(outputStreamIdx);
1713 stream->addBufferListener(listener);
1714
1715 return OK;
1716}
1717
Zhijun He204e3292014-07-14 17:09:23 -07001718uint32_t Camera3Device::getDeviceVersion() {
1719 ATRACE_CALL();
1720 Mutex::Autolock il(mInterfaceLock);
1721 return mDeviceVersion;
1722}
1723
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001724/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001725 * Methods called by subclasses
1726 */
1727
1728void Camera3Device::notifyStatus(bool idle) {
1729 {
1730 // Need mLock to safely update state and synchronize to current
1731 // state of methods in flight.
1732 Mutex::Autolock l(mLock);
1733 // We can get various system-idle notices from the status tracker
1734 // while starting up. Only care about them if we've actually sent
1735 // in some requests recently.
1736 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1737 return;
1738 }
1739 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1740 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001741 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001742
1743 // Skip notifying listener if we're doing some user-transparent
1744 // state changes
1745 if (mPauseStateNotify) return;
1746 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001747
1748 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001749 {
1750 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001751 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001752 }
1753 if (idle && listener != NULL) {
1754 listener->notifyIdle();
1755 }
1756}
1757
Zhijun He5d677d12016-05-29 16:52:39 -07001758status_t Camera3Device::setConsumerSurface(int streamId, sp<Surface> consumer) {
1759 ATRACE_CALL();
1760 ALOGV("%s: Camera %d: set consumer surface for stream %d", __FUNCTION__, mId, streamId);
1761 Mutex::Autolock il(mInterfaceLock);
1762 Mutex::Autolock l(mLock);
1763
1764 if (consumer == nullptr) {
1765 CLOGE("Null consumer is passed!");
1766 return BAD_VALUE;
1767 }
1768
1769 ssize_t idx = mOutputStreams.indexOfKey(streamId);
1770 if (idx == NAME_NOT_FOUND) {
1771 CLOGE("Stream %d is unknown", streamId);
1772 return idx;
1773 }
1774 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
1775 status_t res = stream->setConsumer(consumer);
1776 if (res != OK) {
1777 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1778 return res;
1779 }
1780
1781 if (!stream->isConfiguring()) {
1782 CLOGE("Stream %d was already fully configured.", streamId);
1783 return INVALID_OPERATION;
1784 }
1785
1786 res = stream->finishConfiguration(mHal3Device);
1787 if (res != OK) {
1788 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1789 stream->getId(), strerror(-res), res);
1790 return res;
1791 }
1792
1793 return OK;
1794}
1795
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001796/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001797 * Camera3Device private methods
1798 */
1799
1800sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1801 const CameraMetadata &request) {
1802 ATRACE_CALL();
1803 status_t res;
1804
1805 sp<CaptureRequest> newRequest = new CaptureRequest;
1806 newRequest->mSettings = request;
1807
1808 camera_metadata_entry_t inputStreams =
1809 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1810 if (inputStreams.count > 0) {
1811 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001812 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001813 CLOGE("Request references unknown input stream %d",
1814 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001815 return NULL;
1816 }
1817 // Lazy completion of stream configuration (allocation/registration)
1818 // on first use
1819 if (mInputStream->isConfiguring()) {
1820 res = mInputStream->finishConfiguration(mHal3Device);
1821 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001822 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001823 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001824 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001825 return NULL;
1826 }
1827 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001828 // Check if stream is being prepared
1829 if (mInputStream->isPreparing()) {
1830 CLOGE("Request references an input stream that's being prepared!");
1831 return NULL;
1832 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001833
1834 newRequest->mInputStream = mInputStream;
1835 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1836 }
1837
1838 camera_metadata_entry_t streams =
1839 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1840 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001841 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001842 return NULL;
1843 }
1844
1845 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001846 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001847 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001848 CLOGE("Request references unknown stream %d",
1849 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001850 return NULL;
1851 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001852 sp<Camera3OutputStreamInterface> stream =
1853 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001854
Zhijun He5d677d12016-05-29 16:52:39 -07001855 // It is illegal to include a deferred consumer output stream into a request
1856 if (stream->isConsumerConfigurationDeferred()) {
1857 CLOGE("Stream %d hasn't finished configuration yet due to deferred consumer",
1858 stream->getId());
1859 return NULL;
1860 }
1861
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001862 // Lazy completion of stream configuration (allocation/registration)
1863 // on first use
1864 if (stream->isConfiguring()) {
1865 res = stream->finishConfiguration(mHal3Device);
1866 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001867 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1868 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001869 return NULL;
1870 }
1871 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001872 // Check if stream is being prepared
1873 if (stream->isPreparing()) {
1874 CLOGE("Request references an output stream that's being prepared!");
1875 return NULL;
1876 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001877
1878 newRequest->mOutputStreams.push(stream);
1879 }
1880 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001881 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001882
1883 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001884}
1885
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001886bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1887 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1888 Size size = mSupportedOpaqueInputSizes[i];
1889 if (size.width == width && size.height == height) {
1890 return true;
1891 }
1892 }
1893
1894 return false;
1895}
1896
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001897void Camera3Device::cancelStreamsConfigurationLocked() {
1898 int res = OK;
1899 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1900 res = mInputStream->cancelConfiguration();
1901 if (res != OK) {
1902 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
1903 mInputStream->getId(), strerror(-res), res);
1904 }
1905 }
1906
1907 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1908 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
1909 if (outputStream->isConfiguring()) {
1910 res = outputStream->cancelConfiguration();
1911 if (res != OK) {
1912 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
1913 outputStream->getId(), strerror(-res), res);
1914 }
1915 }
1916 }
1917
1918 // Return state to that at start of call, so that future configures
1919 // properly clean things up
1920 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
1921 mNeedConfig = true;
1922}
1923
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001924status_t Camera3Device::configureStreamsLocked() {
1925 ATRACE_CALL();
1926 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001927
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001928 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001929 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001930 return INVALID_OPERATION;
1931 }
1932
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001933 if (!mNeedConfig) {
1934 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1935 return OK;
1936 }
1937
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001938 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1939 // adding a dummy stream instead.
1940 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1941 if (mOutputStreams.size() == 0) {
1942 addDummyStreamLocked();
1943 } else {
1944 tryRemoveDummyStreamLocked();
1945 }
1946
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001947 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001948 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001949
1950 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001951 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1952 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1953 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001954 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1955
1956 Vector<camera3_stream_t*> streams;
1957 streams.setCapacity(config.num_streams);
1958
1959 if (mInputStream != NULL) {
1960 camera3_stream_t *inputStream;
1961 inputStream = mInputStream->startConfiguration();
1962 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001963 CLOGE("Can't start input stream configuration");
1964 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001965 return INVALID_OPERATION;
1966 }
1967 streams.add(inputStream);
1968 }
1969
1970 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001971
1972 // Don't configure bidi streams twice, nor add them twice to the list
1973 if (mOutputStreams[i].get() ==
1974 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1975
1976 config.num_streams--;
1977 continue;
1978 }
1979
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001980 camera3_stream_t *outputStream;
1981 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1982 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001983 CLOGE("Can't start output stream configuration");
1984 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001985 return INVALID_OPERATION;
1986 }
1987 streams.add(outputStream);
1988 }
1989
1990 config.streams = streams.editArray();
1991
1992 // Do the HAL configuration; will potentially touch stream
1993 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001994 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001995 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001996 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001997
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001998 if (res == BAD_VALUE) {
1999 // HAL rejected this set of streams as unsupported, clean up config
2000 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002001 CLOGE("Set of requested inputs/outputs not supported by HAL");
2002 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002003 return BAD_VALUE;
2004 } else if (res != OK) {
2005 // Some other kind of error from configure_streams - this is not
2006 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002007 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2008 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002009 return res;
2010 }
2011
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002012 // Finish all stream configuration immediately.
2013 // TODO: Try to relax this later back to lazy completion, which should be
2014 // faster
2015
Igor Murashkin073f8572013-05-02 14:59:28 -07002016 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002017 res = mInputStream->finishConfiguration(mHal3Device);
2018 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002019 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002020 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002021 cancelStreamsConfigurationLocked();
2022 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002023 }
2024 }
2025
2026 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002027 sp<Camera3OutputStreamInterface> outputStream =
2028 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002029 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002030 res = outputStream->finishConfiguration(mHal3Device);
2031 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002032 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002033 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002034 cancelStreamsConfigurationLocked();
2035 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002036 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002037 }
2038 }
2039
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002040 // Request thread needs to know to avoid using repeat-last-settings protocol
2041 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002042 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002043
Zhijun He90f7c372016-08-16 16:19:43 -07002044 char value[PROPERTY_VALUE_MAX];
2045 property_get("camera.fifo.disable", value, "0");
2046 int32_t disableFifo = atoi(value);
2047 if (disableFifo != 1) {
2048 // Boost priority of request thread to SCHED_FIFO.
2049 pid_t requestThreadTid = mRequestThread->getTid();
2050 res = requestPriority(getpid(), requestThreadTid,
2051 kRequestThreadPriority, /*asynchronous*/ false);
2052 if (res != OK) {
2053 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2054 strerror(-res), res);
2055 } else {
2056 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2057 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002058 }
2059
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002060 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002061
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002062 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002063
Ruben Brunk183f0562015-08-12 12:55:02 -07002064 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2065 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002066
2067 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
2068
Zhijun He0a210512014-07-24 13:45:15 -07002069 // tear down the deleted streams after configure streams.
2070 mDeletedStreams.clear();
2071
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002072 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002073}
2074
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002075status_t Camera3Device::addDummyStreamLocked() {
2076 ATRACE_CALL();
2077 status_t res;
2078
2079 if (mDummyStreamId != NO_STREAM) {
2080 // Should never be adding a second dummy stream when one is already
2081 // active
2082 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
2083 __FUNCTION__, mId);
2084 return INVALID_OPERATION;
2085 }
2086
2087 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
2088
2089 sp<Camera3OutputStreamInterface> dummyStream =
2090 new Camera3DummyStream(mNextStreamId);
2091
2092 res = mOutputStreams.add(mNextStreamId, dummyStream);
2093 if (res < 0) {
2094 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2095 return res;
2096 }
2097
2098 mDummyStreamId = mNextStreamId;
2099 mNextStreamId++;
2100
2101 return OK;
2102}
2103
2104status_t Camera3Device::tryRemoveDummyStreamLocked() {
2105 ATRACE_CALL();
2106 status_t res;
2107
2108 if (mDummyStreamId == NO_STREAM) return OK;
2109 if (mOutputStreams.size() == 1) return OK;
2110
2111 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
2112
2113 // Ok, have a dummy stream and there's at least one other output stream,
2114 // so remove the dummy
2115
2116 sp<Camera3StreamInterface> deletedStream;
2117 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2118 if (outputStreamIdx == NAME_NOT_FOUND) {
2119 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2120 return INVALID_OPERATION;
2121 }
2122
2123 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2124 mOutputStreams.removeItemsAt(outputStreamIdx);
2125
2126 // Free up the stream endpoint so that it can be used by some other stream
2127 res = deletedStream->disconnect();
2128 if (res != OK) {
2129 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2130 // fall through since we want to still list the stream as deleted.
2131 }
2132 mDeletedStreams.add(deletedStream);
2133 mDummyStreamId = NO_STREAM;
2134
2135 return res;
2136}
2137
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002138void Camera3Device::setErrorState(const char *fmt, ...) {
2139 Mutex::Autolock l(mLock);
2140 va_list args;
2141 va_start(args, fmt);
2142
2143 setErrorStateLockedV(fmt, args);
2144
2145 va_end(args);
2146}
2147
2148void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2149 Mutex::Autolock l(mLock);
2150 setErrorStateLockedV(fmt, args);
2151}
2152
2153void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2154 va_list args;
2155 va_start(args, fmt);
2156
2157 setErrorStateLockedV(fmt, args);
2158
2159 va_end(args);
2160}
2161
2162void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002163 // Print out all error messages to log
2164 String8 errorCause = String8::formatV(fmt, args);
2165 ALOGE("Camera %d: %s", mId, errorCause.string());
2166
2167 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002168 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002169
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002170 mErrorCause = errorCause;
2171
2172 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002173 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002174
2175 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002176 sp<NotificationListener> listener = mListener.promote();
2177 if (listener != NULL) {
2178 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002179 CaptureResultExtras());
2180 }
2181
2182 // Save stack trace. View by dumping it later.
2183 CameraTraces::saveTrace();
2184 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002185}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002186
2187/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002188 * In-flight request management
2189 */
2190
Jianing Weicb0652e2014-03-12 18:29:36 -07002191status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002192 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2193 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002194 ATRACE_CALL();
2195 Mutex::Autolock l(mInFlightLock);
2196
2197 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002198 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2199 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002200 if (res < 0) return res;
2201
Eino-Ville Talvalae4df8ab2016-07-21 12:53:07 -07002202 if (mInFlightMap.size() == 1) {
2203 mStatusTracker->markComponentActive(mInFlightStatusId);
2204 }
2205
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002206 return OK;
2207}
2208
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002209void Camera3Device::returnOutputBuffers(
2210 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2211 nsecs_t timestamp) {
2212 for (size_t i = 0; i < numBuffers; i++)
2213 {
2214 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2215 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2216 // Note: stream may be deallocated at this point, if this buffer was
2217 // the last reference to it.
2218 if (res != OK) {
2219 ALOGE("Can't return buffer to its stream: %s (%d)",
2220 strerror(-res), res);
2221 }
2222 }
2223}
2224
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002225void Camera3Device::removeInFlightMapEntryLocked(int idx) {
2226 mInFlightMap.removeItemsAt(idx, 1);
2227
2228 // Indicate idle inFlightMap to the status tracker
2229 if (mInFlightMap.size() == 0) {
2230 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2231 }
2232}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002233
2234void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2235
2236 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2237 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2238
2239 nsecs_t sensorTimestamp = request.sensorTimestamp;
2240 nsecs_t shutterTimestamp = request.shutterTimestamp;
2241
2242 // Check if it's okay to remove the request from InFlightMap:
2243 // In the case of a successful request:
2244 // all input and output buffers, all result metadata, shutter callback
2245 // arrived.
2246 // In the case of a unsuccessful request:
2247 // all input and output buffers arrived.
2248 if (request.numBuffersLeft == 0 &&
2249 (request.requestStatus != OK ||
2250 (request.haveResultMetadata && shutterTimestamp != 0))) {
2251 ATRACE_ASYNC_END("frame capture", frameNumber);
2252
2253 // Sanity check - if sensor timestamp matches shutter timestamp
2254 if (request.requestStatus == OK &&
2255 sensorTimestamp != shutterTimestamp) {
2256 SET_ERR("sensor timestamp (%" PRId64
2257 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2258 sensorTimestamp, frameNumber, shutterTimestamp);
2259 }
2260
2261 // for an unsuccessful request, it may have pending output buffers to
2262 // return.
2263 assert(request.requestStatus != OK ||
2264 request.pendingOutputBuffers.size() == 0);
2265 returnOutputBuffers(request.pendingOutputBuffers.array(),
2266 request.pendingOutputBuffers.size(), 0);
2267
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002268 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002269 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2270 }
2271
2272 // Sanity check - if we have too many in-flight frames, something has
2273 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002274 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002275 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002276 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2277 kInFlightWarnLimitHighSpeed) {
2278 CLOGE("In-flight list too large for high speed configuration: %zu",
2279 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002280 }
2281}
2282
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002283void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2284 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2285 if (result == nullptr) return;
2286
2287 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2288 (int32_t*)&frameNumber, 1) != OK) {
2289 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2290 return;
2291 }
2292
2293 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2294 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2295 return;
2296 }
2297
2298 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2299
2300 // Valid result, insert into queue
2301 List<CaptureResult>::iterator queuedResult =
2302 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2303 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2304 ", burstId = %" PRId32, __FUNCTION__,
2305 queuedResult->mResultExtras.requestId,
2306 queuedResult->mResultExtras.frameNumber,
2307 queuedResult->mResultExtras.burstId);
2308
2309 mResultSignal.signal();
2310}
2311
2312
2313void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2314 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2315 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2316 Mutex::Autolock l(mOutputLock);
2317
2318 CaptureResult captureResult;
2319 captureResult.mResultExtras = resultExtras;
2320 captureResult.mMetadata = partialResult;
2321
2322 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2323}
2324
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002325
2326void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2327 CaptureResultExtras &resultExtras,
2328 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002329 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002330 bool reprocess,
2331 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002332 if (pendingMetadata.isEmpty())
2333 return;
2334
2335 Mutex::Autolock l(mOutputLock);
2336
2337 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002338 if (reprocess) {
2339 if (frameNumber < mNextReprocessResultFrameNumber) {
2340 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002341 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002342 frameNumber, mNextReprocessResultFrameNumber);
2343 return;
2344 }
2345 mNextReprocessResultFrameNumber = frameNumber + 1;
2346 } else {
2347 if (frameNumber < mNextResultFrameNumber) {
2348 SET_ERR("Out-of-order capture result metadata submitted! "
2349 "(got frame number %d, expecting %d)",
2350 frameNumber, mNextResultFrameNumber);
2351 return;
2352 }
2353 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002354 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002355
2356 CaptureResult captureResult;
2357 captureResult.mResultExtras = resultExtras;
2358 captureResult.mMetadata = pendingMetadata;
2359
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002360 // Append any previous partials to form a complete result
2361 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2362 captureResult.mMetadata.append(collectedPartialResult);
2363 }
2364
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07002365 // Derive some new keys for backward compaibility
2366 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
2367 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
2368 int32_t defaultBoost[1] = {100};
2369 captureResult.mMetadata.update(
2370 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
2371 defaultBoost, 1);
2372 }
2373
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002374 captureResult.mMetadata.sort();
2375
2376 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002377 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2378 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002379 SET_ERR("No timestamp provided by HAL for frame %d!",
2380 frameNumber);
2381 return;
2382 }
2383
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002384 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2385 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2386
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002387 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002388}
2389
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002390/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002391 * Camera HAL device callback methods
2392 */
2393
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002394void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002395 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002396
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002397 status_t res;
2398
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002399 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002400 if (result->result == NULL && result->num_output_buffers == 0 &&
2401 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002402 SET_ERR("No result data provided by HAL for frame %d",
2403 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002404 return;
2405 }
Zhijun He204e3292014-07-14 17:09:23 -07002406
2407 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2408 // partial_result to 1 when metadata is included in this result.
2409 if (!mUsePartialResult &&
2410 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2411 result->result != NULL &&
2412 result->partial_result != 1) {
2413 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2414 " if partial result is not supported",
2415 frameNumber, result->partial_result);
2416 return;
2417 }
2418
2419 bool isPartialResult = false;
2420 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002421 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002422 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002423
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002424 // Get shutter timestamp and resultExtras from list of in-flight requests,
2425 // where it was added by the shutter notification for this frame. If the
2426 // shutter timestamp isn't received yet, append the output buffers to the
2427 // in-flight request and they will be returned when the shutter timestamp
2428 // arrives. Update the in-flight status and remove the in-flight entry if
2429 // all result data and shutter timestamp have been received.
2430 nsecs_t shutterTimestamp = 0;
2431
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002432 {
2433 Mutex::Autolock l(mInFlightLock);
2434 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2435 if (idx == NAME_NOT_FOUND) {
2436 SET_ERR("Unknown frame number for capture result: %d",
2437 frameNumber);
2438 return;
2439 }
2440 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002441 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2442 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2443 ", partialResultCount = %d",
2444 __FUNCTION__, request.resultExtras.requestId,
2445 request.resultExtras.frameNumber, request.resultExtras.burstId,
2446 result->partial_result);
2447 // Always update the partial count to the latest one if it's not 0
2448 // (buffers only). When framework aggregates adjacent partial results
2449 // into one, the latest partial count will be used.
2450 if (result->partial_result != 0)
2451 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002452
2453 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002454 if (mUsePartialResult && result->result != NULL) {
2455 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2456 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2457 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2458 " the range of [1, %d] when metadata is included in the result",
2459 frameNumber, result->partial_result, mNumPartialResults);
2460 return;
2461 }
2462 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002463 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002464 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002465 }
Zhijun He204e3292014-07-14 17:09:23 -07002466 } else {
2467 camera_metadata_ro_entry_t partialResultEntry;
2468 res = find_camera_metadata_ro_entry(result->result,
2469 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2470 if (res != NAME_NOT_FOUND &&
2471 partialResultEntry.count > 0 &&
2472 partialResultEntry.data.u8[0] ==
2473 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2474 // A partial result. Flag this as such, and collect this
2475 // set of metadata into the in-flight entry.
2476 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002477 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002478 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002479 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002480 ANDROID_QUIRKS_PARTIAL_RESULT);
2481 }
2482 }
2483
2484 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002485 // Send partial capture result
2486 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2487 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002488 }
2489 }
2490
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002491 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002492 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002493
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002494 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002495 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002496 if (request.haveResultMetadata) {
2497 SET_ERR("Called multiple times with metadata for frame %d",
2498 frameNumber);
2499 return;
2500 }
Zhijun He204e3292014-07-14 17:09:23 -07002501 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002502 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002503 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002504 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002505 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002506 request.haveResultMetadata = true;
2507 }
2508
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002509 uint32_t numBuffersReturned = result->num_output_buffers;
2510 if (result->input_buffer != NULL) {
2511 if (hasInputBufferInRequest) {
2512 numBuffersReturned += 1;
2513 } else {
2514 ALOGW("%s: Input buffer should be NULL if there is no input"
2515 " buffer sent in the request",
2516 __FUNCTION__);
2517 }
2518 }
2519 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002520 if (request.numBuffersLeft < 0) {
2521 SET_ERR("Too many buffers returned for frame %d",
2522 frameNumber);
2523 return;
2524 }
2525
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002526 camera_metadata_ro_entry_t entry;
2527 res = find_camera_metadata_ro_entry(result->result,
2528 ANDROID_SENSOR_TIMESTAMP, &entry);
2529 if (res == OK && entry.count == 1) {
2530 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002531 }
2532
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002533 // If shutter event isn't received yet, append the output buffers to
2534 // the in-flight request. Otherwise, return the output buffers to
2535 // streams.
2536 if (shutterTimestamp == 0) {
2537 request.pendingOutputBuffers.appendArray(result->output_buffers,
2538 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002539 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002540 returnOutputBuffers(result->output_buffers,
2541 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002542 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002543
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002544 if (result->result != NULL && !isPartialResult) {
2545 if (shutterTimestamp == 0) {
2546 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002547 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002548 } else {
2549 CameraMetadata metadata;
2550 metadata = result->result;
2551 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002552 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2553 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002554 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002555 }
2556
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002557 removeInFlightRequestIfReadyLocked(idx);
2558 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002559
Zhijun Hef0d962a2014-06-30 10:24:11 -07002560 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002561 if (hasInputBufferInRequest) {
2562 Camera3Stream *stream =
2563 Camera3Stream::cast(result->input_buffer->stream);
2564 res = stream->returnInputBuffer(*(result->input_buffer));
2565 // Note: stream may be deallocated at this point, if this buffer was the
2566 // last reference to it.
2567 if (res != OK) {
2568 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2569 " its stream:%s (%d)", __FUNCTION__,
2570 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002571 }
2572 } else {
2573 ALOGW("%s: Input buffer should be NULL if there is no input"
2574 " buffer sent in the request, skipping input buffer return.",
2575 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002576 }
2577 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002578}
2579
2580void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002581 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002582 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002583 {
2584 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002585 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002586 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002587
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002588 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002589 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002590 return;
2591 }
2592
2593 switch (msg->type) {
2594 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002595 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002596 break;
2597 }
2598 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002599 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002600 break;
2601 }
2602 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002603 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002604 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002605 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002606}
2607
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002608void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002609 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002610
2611 // Map camera HAL error codes to ICameraDeviceCallback error codes
2612 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002613 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002614 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002615 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002616 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002617 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002618 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002619 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002620 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002621 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002622 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002623 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002624 };
2625
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002626 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002627 ((msg.error_code >= 0) &&
2628 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2629 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002630 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002631
2632 int streamId = 0;
2633 if (msg.error_stream != NULL) {
2634 Camera3Stream *stream =
2635 Camera3Stream::cast(msg.error_stream);
2636 streamId = stream->getId();
2637 }
2638 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2639 mId, __FUNCTION__, msg.frame_number,
2640 streamId, msg.error_code);
2641
2642 CaptureResultExtras resultExtras;
2643 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002644 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002645 // SET_ERR calls notifyError
2646 SET_ERR("Camera HAL reported serious device error");
2647 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002648 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2649 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2650 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002651 {
2652 Mutex::Autolock l(mInFlightLock);
2653 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2654 if (idx >= 0) {
2655 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2656 r.requestStatus = msg.error_code;
2657 resultExtras = r.resultExtras;
2658 } else {
2659 resultExtras.frameNumber = msg.frame_number;
2660 ALOGE("Camera %d: %s: cannot find in-flight request on "
2661 "frame %" PRId64 " error", mId, __FUNCTION__,
2662 resultExtras.frameNumber);
2663 }
2664 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002665 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002666 if (listener != NULL) {
2667 listener->notifyError(errorCode, resultExtras);
2668 } else {
2669 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2670 }
2671 break;
2672 default:
2673 // SET_ERR calls notifyError
2674 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2675 break;
2676 }
2677}
2678
2679void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002680 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002681 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002682
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002683 // Set timestamp for the request in the in-flight tracking
2684 // and get the request ID to send upstream
2685 {
2686 Mutex::Autolock l(mInFlightLock);
2687 idx = mInFlightMap.indexOfKey(msg.frame_number);
2688 if (idx >= 0) {
2689 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002690
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002691 // Verify ordering of shutter notifications
2692 {
2693 Mutex::Autolock l(mOutputLock);
2694 // TODO: need to track errors for tighter bounds on expected frame number.
2695 if (r.hasInputBuffer) {
2696 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2697 SET_ERR("Shutter notification out-of-order. Expected "
2698 "notification for frame %d, got frame %d",
2699 mNextReprocessShutterFrameNumber, msg.frame_number);
2700 return;
2701 }
2702 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2703 } else {
2704 if (msg.frame_number < mNextShutterFrameNumber) {
2705 SET_ERR("Shutter notification out-of-order. Expected "
2706 "notification for frame %d, got frame %d",
2707 mNextShutterFrameNumber, msg.frame_number);
2708 return;
2709 }
2710 mNextShutterFrameNumber = msg.frame_number + 1;
2711 }
2712 }
2713
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002714 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2715 mId, __FUNCTION__,
2716 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2717 // Call listener, if any
2718 if (listener != NULL) {
2719 listener->notifyShutter(r.resultExtras, msg.timestamp);
2720 }
2721
2722 r.shutterTimestamp = msg.timestamp;
2723
2724 // send pending result and buffers
2725 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002726 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002727 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002728 returnOutputBuffers(r.pendingOutputBuffers.array(),
2729 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2730 r.pendingOutputBuffers.clear();
2731
2732 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002733 }
2734 }
2735 if (idx < 0) {
2736 SET_ERR("Shutter notification for non-existent frame number %d",
2737 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002738 }
2739}
2740
2741
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002742CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002743 ALOGV("%s", __FUNCTION__);
2744
Igor Murashkin1e479c02013-09-06 16:55:14 -07002745 CameraMetadata retVal;
2746
2747 if (mRequestThread != NULL) {
2748 retVal = mRequestThread->getLatestRequest();
2749 }
2750
Igor Murashkin1e479c02013-09-06 16:55:14 -07002751 return retVal;
2752}
2753
Jianing Weicb0652e2014-03-12 18:29:36 -07002754
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002755void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
2756 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
2757 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
2758}
2759
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002760/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002761 * RequestThread inner class methods
2762 */
2763
2764Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002765 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002766 camera3_device_t *hal3Device,
2767 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002768 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002769 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002770 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002771 mHal3Device(hal3Device),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002772 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002773 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002774 mReconfigured(false),
2775 mDoPause(false),
2776 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002777 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002778 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002779 mCurrentAfTriggerId(0),
2780 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002781 mRepeatingLastFrameNumber(
2782 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002783 mAeLockAvailable(aeLockAvailable),
2784 mPrepareVideoStream(false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002785 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002786}
2787
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002788void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002789 wp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002790 Mutex::Autolock l(mRequestLock);
2791 mListener = listener;
2792}
2793
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002794void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002795 Mutex::Autolock l(mRequestLock);
2796 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002797 // Prepare video stream for high speed recording.
2798 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002799}
2800
Jianing Wei90e59c92014-03-12 18:29:36 -07002801status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002802 List<sp<CaptureRequest> > &requests,
2803 /*out*/
2804 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002805 Mutex::Autolock l(mRequestLock);
2806 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2807 ++it) {
2808 mRequestQueue.push_back(*it);
2809 }
2810
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002811 if (lastFrameNumber != NULL) {
2812 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2813 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2814 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2815 *lastFrameNumber);
2816 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002817
Jianing Wei90e59c92014-03-12 18:29:36 -07002818 unpauseForNewRequests();
2819
2820 return OK;
2821}
2822
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002823
2824status_t Camera3Device::RequestThread::queueTrigger(
2825 RequestTrigger trigger[],
2826 size_t count) {
2827
2828 Mutex::Autolock l(mTriggerMutex);
2829 status_t ret;
2830
2831 for (size_t i = 0; i < count; ++i) {
2832 ret = queueTriggerLocked(trigger[i]);
2833
2834 if (ret != OK) {
2835 return ret;
2836 }
2837 }
2838
2839 return OK;
2840}
2841
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002842int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2843 sp<Camera3Device> d = device.promote();
2844 if (d != NULL) return d->mId;
2845 return 0;
2846}
2847
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002848status_t Camera3Device::RequestThread::queueTriggerLocked(
2849 RequestTrigger trigger) {
2850
2851 uint32_t tag = trigger.metadataTag;
2852 ssize_t index = mTriggerMap.indexOfKey(tag);
2853
2854 switch (trigger.getTagType()) {
2855 case TYPE_BYTE:
2856 // fall-through
2857 case TYPE_INT32:
2858 break;
2859 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002860 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2861 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002862 return INVALID_OPERATION;
2863 }
2864
2865 /**
2866 * Collect only the latest trigger, since we only have 1 field
2867 * in the request settings per trigger tag, and can't send more than 1
2868 * trigger per request.
2869 */
2870 if (index != NAME_NOT_FOUND) {
2871 mTriggerMap.editValueAt(index) = trigger;
2872 } else {
2873 mTriggerMap.add(tag, trigger);
2874 }
2875
2876 return OK;
2877}
2878
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002879status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002880 const RequestList &requests,
2881 /*out*/
2882 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002883 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002884 if (lastFrameNumber != NULL) {
2885 *lastFrameNumber = mRepeatingLastFrameNumber;
2886 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002887 mRepeatingRequests.clear();
2888 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2889 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002890
2891 unpauseForNewRequests();
2892
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002893 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002894 return OK;
2895}
2896
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002897bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2898 if (mRepeatingRequests.empty()) {
2899 return false;
2900 }
2901 int32_t requestId = requestIn->mResultExtras.requestId;
2902 const RequestList &repeatRequests = mRepeatingRequests;
2903 // All repeating requests are guaranteed to have same id so only check first quest
2904 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2905 return (firstRequest->mResultExtras.requestId == requestId);
2906}
2907
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002908status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002909 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002910 return clearRepeatingRequestsLocked(lastFrameNumber);
2911
2912}
2913
2914status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002915 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002916 if (lastFrameNumber != NULL) {
2917 *lastFrameNumber = mRepeatingLastFrameNumber;
2918 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002919 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002920 return OK;
2921}
2922
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002923status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002924 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002925 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002926 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002927
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002928 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002929
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002930 // Send errors for all requests pending in the request queue, including
2931 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002932 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002933 if (listener != NULL) {
2934 for (RequestList::iterator it = mRequestQueue.begin();
2935 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002936 // Abort the input buffers for reprocess requests.
2937 if ((*it)->mInputStream != NULL) {
2938 camera3_stream_buffer_t inputBuffer;
2939 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2940 if (res != OK) {
2941 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2942 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2943 } else {
2944 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2945 if (res != OK) {
2946 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2947 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2948 }
2949 }
2950 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002951 // Set the frame number this request would have had, if it
2952 // had been submitted; this frame number will not be reused.
2953 // The requestId and burstId fields were set when the request was
2954 // submitted originally (in convertMetadataListToRequestListLocked)
2955 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002956 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002957 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002958 }
2959 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002960 mRequestQueue.clear();
2961 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002962 if (lastFrameNumber != NULL) {
2963 *lastFrameNumber = mRepeatingLastFrameNumber;
2964 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002965 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002966 return OK;
2967}
2968
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002969status_t Camera3Device::RequestThread::flush() {
2970 ATRACE_CALL();
2971 Mutex::Autolock l(mFlushLock);
2972
2973 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2974 return mHal3Device->ops->flush(mHal3Device);
2975 }
2976
2977 return -ENOTSUP;
2978}
2979
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002980void Camera3Device::RequestThread::setPaused(bool paused) {
2981 Mutex::Autolock l(mPauseLock);
2982 mDoPause = paused;
2983 mDoPauseSignal.signal();
2984}
2985
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002986status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2987 int32_t requestId, nsecs_t timeout) {
2988 Mutex::Autolock l(mLatestRequestMutex);
2989 status_t res;
2990 while (mLatestRequestId != requestId) {
2991 nsecs_t startTime = systemTime();
2992
2993 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2994 if (res != OK) return res;
2995
2996 timeout -= (systemTime() - startTime);
2997 }
2998
2999 return OK;
3000}
3001
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003002void Camera3Device::RequestThread::requestExit() {
3003 // Call parent to set up shutdown
3004 Thread::requestExit();
3005 // The exit from any possible waits
3006 mDoPauseSignal.signal();
3007 mRequestSignal.signal();
3008}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003009
Chien-Yu Chend196d612015-06-22 19:49:01 -07003010
3011/**
3012 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
3013 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
3014 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
3015 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
3016 * request.
3017 */
3018void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
3019 request->mAeTriggerCancelOverride.applyAeLock = false;
3020 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
3021
3022 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
3023 return;
3024 }
3025
3026 camera_metadata_entry_t aePrecaptureTrigger =
3027 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3028 if (aePrecaptureTrigger.count > 0 &&
3029 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
3030 // Always override CANCEL to IDLE
3031 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
3032 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
3033 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
3034 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
3035 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3036
3037 if (mAeLockAvailable == true) {
3038 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3039 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3040 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3041 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3042 request->mAeTriggerCancelOverride.applyAeLock = true;
3043 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3044 }
3045 }
3046 }
3047}
3048
3049/**
3050 * Override result metadata for cancelling AE precapture trigger applied in
3051 * handleAePrecaptureCancelRequest().
3052 */
3053void Camera3Device::overrideResultForPrecaptureCancel(
3054 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3055 if (aeTriggerCancelOverride.applyAeLock) {
3056 // Only devices <= v3.2 should have this override
3057 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3058 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3059 }
3060
3061 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3062 // Only devices <= v3.2 should have this override
3063 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3064 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3065 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3066 }
3067}
3068
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003069void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003070 bool surfaceAbandoned = false;
3071 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003072 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003073 {
3074 Mutex::Autolock l(mRequestLock);
3075 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3076 // repeating requests.
3077 for (const auto& request : mRepeatingRequests) {
3078 for (const auto& s : request->mOutputStreams) {
3079 if (s->isAbandoned()) {
3080 surfaceAbandoned = true;
3081 clearRepeatingRequestsLocked(&lastFrameNumber);
3082 break;
3083 }
3084 }
3085 if (surfaceAbandoned) {
3086 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003087 }
3088 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003089 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003090 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003091
3092 if (listener != NULL && surfaceAbandoned) {
3093 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003094 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003095}
3096
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003097bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003098 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003099 status_t res;
3100
3101 // Handle paused state.
3102 if (waitIfPaused()) {
3103 return true;
3104 }
3105
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003106 // Wait for the next batch of requests.
3107 waitForNextRequestBatch();
3108 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003109 return true;
3110 }
3111
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003112 // Get the latest request ID, if any
3113 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003114 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003115 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003116 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003117 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003118 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003119 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3120 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003121 }
3122
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003123 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003124 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003125 if (res == TIMED_OUT) {
3126 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003127 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003128 // Check if any stream is abandoned.
3129 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003130 return true;
3131 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003132 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003133 return false;
3134 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003135
Zhijun Hecc27e112013-10-03 16:12:43 -07003136 // Inform waitUntilRequestProcessed thread of a new request ID
3137 {
3138 Mutex::Autolock al(mLatestRequestMutex);
3139
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003140 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003141 mLatestRequestSignal.signal();
3142 }
3143
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003144 // Submit a batch of requests to HAL.
3145 // Use flush lock only when submitting multilple requests in a batch.
3146 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3147 // which may take a long time to finish so synchronizing flush() and
3148 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3149 // For now, only synchronize for high speed recording and we should figure something out for
3150 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003151 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003152
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003153 if (useFlushLock) {
3154 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003155 }
3156
Zhijun Hef0645c12016-08-02 00:58:11 -07003157 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003158 mNextRequests.size());
3159 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003160 // Submit request and block until ready for next one
3161 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3162 ATRACE_BEGIN("camera3->process_capture_request");
3163 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3164 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003165
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003166 if (res != OK) {
3167 // Should only get a failure here for malformed requests or device-level
3168 // errors, so consider all errors fatal. Bad metadata failures should
3169 // come through notify.
3170 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3171 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3172 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003173 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003174 if (useFlushLock) {
3175 mFlushLock.unlock();
3176 }
3177 return false;
3178 }
3179
3180 // Mark that the request has be submitted successfully.
3181 nextRequest.submitted = true;
3182
3183 // Update the latest request sent to HAL
3184 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3185 Mutex::Autolock al(mLatestRequestMutex);
3186
3187 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3188 mLatestRequest.acquire(cloned);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003189
3190 sp<Camera3Device> parent = mParent.promote();
3191 if (parent != NULL) {
3192 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
3193 0, mLatestRequest);
3194 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003195 }
3196
3197 if (nextRequest.halRequest.settings != NULL) {
3198 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3199 }
3200
3201 // Remove any previously queued triggers (after unlock)
3202 res = removeTriggers(mPrevRequest);
3203 if (res != OK) {
3204 SET_ERR("RequestThread: Unable to remove triggers "
3205 "(capture request %d, HAL device: %s (%d)",
3206 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003207 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003208 if (useFlushLock) {
3209 mFlushLock.unlock();
3210 }
3211 return false;
3212 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003213 }
3214
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003215 if (useFlushLock) {
3216 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003217 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003218
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003219 // Unset as current request
3220 {
3221 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003222 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003223 }
3224
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003225 return true;
3226}
3227
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003228status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003229 ATRACE_CALL();
3230
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003231 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003232 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3233 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3234 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3235
3236 // Prepare a request to HAL
3237 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3238
3239 // Insert any queued triggers (before metadata is locked)
3240 status_t res = insertTriggers(captureRequest);
3241
3242 if (res < 0) {
3243 SET_ERR("RequestThread: Unable to insert triggers "
3244 "(capture request %d, HAL device: %s (%d)",
3245 halRequest->frame_number, strerror(-res), res);
3246 return INVALID_OPERATION;
3247 }
3248 int triggerCount = res;
3249 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3250 mPrevTriggers = triggerCount;
3251
3252 // If the request is the same as last, or we had triggers last time
3253 if (mPrevRequest != captureRequest || triggersMixedIn) {
3254 /**
3255 * HAL workaround:
3256 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3257 */
3258 res = addDummyTriggerIds(captureRequest);
3259 if (res != OK) {
3260 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3261 "(capture request %d, HAL device: %s (%d)",
3262 halRequest->frame_number, strerror(-res), res);
3263 return INVALID_OPERATION;
3264 }
3265
3266 /**
3267 * The request should be presorted so accesses in HAL
3268 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3269 */
3270 captureRequest->mSettings.sort();
3271 halRequest->settings = captureRequest->mSettings.getAndLock();
3272 mPrevRequest = captureRequest;
3273 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3274
3275 IF_ALOGV() {
3276 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3277 find_camera_metadata_ro_entry(
3278 halRequest->settings,
3279 ANDROID_CONTROL_AF_TRIGGER,
3280 &e
3281 );
3282 if (e.count > 0) {
3283 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3284 __FUNCTION__,
3285 halRequest->frame_number,
3286 e.data.u8[0]);
3287 }
3288 }
3289 } else {
3290 // leave request.settings NULL to indicate 'reuse latest given'
3291 ALOGVV("%s: Request settings are REUSED",
3292 __FUNCTION__);
3293 }
3294
3295 uint32_t totalNumBuffers = 0;
3296
3297 // Fill in buffers
3298 if (captureRequest->mInputStream != NULL) {
3299 halRequest->input_buffer = &captureRequest->mInputBuffer;
3300 totalNumBuffers += 1;
3301 } else {
3302 halRequest->input_buffer = NULL;
3303 }
3304
3305 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3306 captureRequest->mOutputStreams.size());
3307 halRequest->output_buffers = outputBuffers->array();
3308 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003309 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(i);
3310
3311 // Prepare video buffers for high speed recording on the first video request.
3312 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3313 // Only try to prepare video stream on the first video request.
3314 mPrepareVideoStream = false;
3315
3316 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
3317 while (res == NOT_ENOUGH_DATA) {
3318 res = outputStream->prepareNextBuffer();
3319 }
3320 if (res != OK) {
3321 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3322 __FUNCTION__, strerror(-res), res);
3323 outputStream->cancelPrepare();
3324 }
3325 }
3326
3327 res = outputStream->getBuffer(&outputBuffers->editItemAt(i));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003328 if (res != OK) {
3329 // Can't get output buffer from gralloc queue - this could be due to
3330 // abandoned queue or other consumer misbehavior, so not a fatal
3331 // error
3332 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3333 " %s (%d)", strerror(-res), res);
3334
3335 return TIMED_OUT;
3336 }
3337 halRequest->num_output_buffers++;
3338 }
3339 totalNumBuffers += halRequest->num_output_buffers;
3340
3341 // Log request in the in-flight queue
3342 sp<Camera3Device> parent = mParent.promote();
3343 if (parent == NULL) {
3344 // Should not happen, and nowhere to send errors to, so just log it
3345 CLOGE("RequestThread: Parent is gone");
3346 return INVALID_OPERATION;
3347 }
3348 res = parent->registerInFlight(halRequest->frame_number,
3349 totalNumBuffers, captureRequest->mResultExtras,
3350 /*hasInput*/halRequest->input_buffer != NULL,
3351 captureRequest->mAeTriggerCancelOverride);
3352 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3353 ", burstId = %" PRId32 ".",
3354 __FUNCTION__,
3355 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3356 captureRequest->mResultExtras.burstId);
3357 if (res != OK) {
3358 SET_ERR("RequestThread: Unable to register new in-flight request:"
3359 " %s (%d)", strerror(-res), res);
3360 return INVALID_OPERATION;
3361 }
3362 }
3363
3364 return OK;
3365}
3366
Igor Murashkin1e479c02013-09-06 16:55:14 -07003367CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3368 Mutex::Autolock al(mLatestRequestMutex);
3369
3370 ALOGV("RequestThread::%s", __FUNCTION__);
3371
3372 return mLatestRequest;
3373}
3374
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003375bool Camera3Device::RequestThread::isStreamPending(
3376 sp<Camera3StreamInterface>& stream) {
3377 Mutex::Autolock l(mRequestLock);
3378
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003379 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003380 if (!nextRequest.submitted) {
3381 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3382 if (stream == s) return true;
3383 }
3384 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003385 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003386 }
3387
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003388 for (const auto& request : mRequestQueue) {
3389 for (const auto& s : request->mOutputStreams) {
3390 if (stream == s) return true;
3391 }
3392 if (stream == request->mInputStream) return true;
3393 }
3394
3395 for (const auto& request : mRepeatingRequests) {
3396 for (const auto& s : request->mOutputStreams) {
3397 if (stream == s) return true;
3398 }
3399 if (stream == request->mInputStream) return true;
3400 }
3401
3402 return false;
3403}
Jianing Weicb0652e2014-03-12 18:29:36 -07003404
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003405void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3406 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003407 return;
3408 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003409
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003410 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003411 // Skip the ones that have been submitted successfully.
3412 if (nextRequest.submitted) {
3413 continue;
3414 }
3415
3416 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3417 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3418 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3419
3420 if (halRequest->settings != NULL) {
3421 captureRequest->mSettings.unlock(halRequest->settings);
3422 }
3423
3424 if (captureRequest->mInputStream != NULL) {
3425 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3426 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3427 }
3428
3429 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3430 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3431 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3432 }
3433
3434 if (sendRequestError) {
3435 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003436 sp<NotificationListener> listener = mListener.promote();
3437 if (listener != NULL) {
3438 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003439 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003440 captureRequest->mResultExtras);
3441 }
3442 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003443
3444 // Remove yet-to-be submitted inflight request from inflightMap
3445 {
3446 sp<Camera3Device> parent = mParent.promote();
3447 if (parent != NULL) {
3448 Mutex::Autolock l(parent->mInFlightLock);
3449 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
3450 if (idx >= 0) {
3451 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
3452 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
3453 parent->removeInFlightMapEntryLocked(idx);
3454 }
3455 }
3456 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003457 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003458
3459 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003460 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003461}
3462
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003463void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003464 // Optimized a bit for the simple steady-state case (single repeating
3465 // request), to avoid putting that request in the queue temporarily.
3466 Mutex::Autolock l(mRequestLock);
3467
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003468 assert(mNextRequests.empty());
3469
3470 NextRequest nextRequest;
3471 nextRequest.captureRequest = waitForNextRequestLocked();
3472 if (nextRequest.captureRequest == nullptr) {
3473 return;
3474 }
3475
3476 nextRequest.halRequest = camera3_capture_request_t();
3477 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003478 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003479
3480 // Wait for additional requests
3481 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3482
3483 for (size_t i = 1; i < batchSize; i++) {
3484 NextRequest additionalRequest;
3485 additionalRequest.captureRequest = waitForNextRequestLocked();
3486 if (additionalRequest.captureRequest == nullptr) {
3487 break;
3488 }
3489
3490 additionalRequest.halRequest = camera3_capture_request_t();
3491 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003492 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003493 }
3494
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003495 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003496 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003497 mNextRequests.size(), batchSize);
3498 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003499 }
3500
3501 return;
3502}
3503
3504sp<Camera3Device::CaptureRequest>
3505 Camera3Device::RequestThread::waitForNextRequestLocked() {
3506 status_t res;
3507 sp<CaptureRequest> nextRequest;
3508
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003509 while (mRequestQueue.empty()) {
3510 if (!mRepeatingRequests.empty()) {
3511 // Always atomically enqueue all requests in a repeating request
3512 // list. Guarantees a complete in-sequence set of captures to
3513 // application.
3514 const RequestList &requests = mRepeatingRequests;
3515 RequestList::const_iterator firstRequest =
3516 requests.begin();
3517 nextRequest = *firstRequest;
3518 mRequestQueue.insert(mRequestQueue.end(),
3519 ++firstRequest,
3520 requests.end());
3521 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003522
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003523 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003524
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003525 break;
3526 }
3527
3528 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3529
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003530 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3531 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003532 Mutex::Autolock pl(mPauseLock);
3533 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003534 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003535 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003536 // Let the tracker know
3537 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3538 if (statusTracker != 0) {
3539 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3540 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003541 }
3542 // Stop waiting for now and let thread management happen
3543 return NULL;
3544 }
3545 }
3546
3547 if (nextRequest == NULL) {
3548 // Don't have a repeating request already in hand, so queue
3549 // must have an entry now.
3550 RequestList::iterator firstRequest =
3551 mRequestQueue.begin();
3552 nextRequest = *firstRequest;
3553 mRequestQueue.erase(firstRequest);
3554 }
3555
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003556 // In case we've been unpaused by setPaused clearing mDoPause, need to
3557 // update internal pause state (capture/setRepeatingRequest unpause
3558 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003559 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003560 if (mPaused) {
3561 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3562 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3563 if (statusTracker != 0) {
3564 statusTracker->markComponentActive(mStatusId);
3565 }
3566 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003567 mPaused = false;
3568
3569 // Check if we've reconfigured since last time, and reset the preview
3570 // request if so. Can't use 'NULL request == repeat' across configure calls.
3571 if (mReconfigured) {
3572 mPrevRequest.clear();
3573 mReconfigured = false;
3574 }
3575
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003576 if (nextRequest != NULL) {
3577 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003578 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3579 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003580
3581 // Since RequestThread::clear() removes buffers from the input stream,
3582 // get the right buffer here before unlocking mRequestLock
3583 if (nextRequest->mInputStream != NULL) {
3584 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3585 if (res != OK) {
3586 // Can't get input buffer from gralloc queue - this could be due to
3587 // disconnected queue or other producer misbehavior, so not a fatal
3588 // error
3589 ALOGE("%s: Can't get input buffer, skipping request:"
3590 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003591
3592 sp<NotificationListener> listener = mListener.promote();
3593 if (listener != NULL) {
3594 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003595 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003596 nextRequest->mResultExtras);
3597 }
3598 return NULL;
3599 }
3600 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003601 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003602
3603 handleAePrecaptureCancelRequest(nextRequest);
3604
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003605 return nextRequest;
3606}
3607
3608bool Camera3Device::RequestThread::waitIfPaused() {
3609 status_t res;
3610 Mutex::Autolock l(mPauseLock);
3611 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003612 if (mPaused == false) {
3613 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003614 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3615 // Let the tracker know
3616 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3617 if (statusTracker != 0) {
3618 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3619 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003620 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003621
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003622 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003623 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003624 return true;
3625 }
3626 }
3627 // We don't set mPaused to false here, because waitForNextRequest needs
3628 // to further manage the paused state in case of starvation.
3629 return false;
3630}
3631
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003632void Camera3Device::RequestThread::unpauseForNewRequests() {
3633 // With work to do, mark thread as unpaused.
3634 // If paused by request (setPaused), don't resume, to avoid
3635 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003636 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003637 Mutex::Autolock p(mPauseLock);
3638 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003639 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3640 if (mPaused) {
3641 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3642 if (statusTracker != 0) {
3643 statusTracker->markComponentActive(mStatusId);
3644 }
3645 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003646 mPaused = false;
3647 }
3648}
3649
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003650void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3651 sp<Camera3Device> parent = mParent.promote();
3652 if (parent != NULL) {
3653 va_list args;
3654 va_start(args, fmt);
3655
3656 parent->setErrorStateV(fmt, args);
3657
3658 va_end(args);
3659 }
3660}
3661
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003662status_t Camera3Device::RequestThread::insertTriggers(
3663 const sp<CaptureRequest> &request) {
3664
3665 Mutex::Autolock al(mTriggerMutex);
3666
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003667 sp<Camera3Device> parent = mParent.promote();
3668 if (parent == NULL) {
3669 CLOGE("RequestThread: Parent is gone");
3670 return DEAD_OBJECT;
3671 }
3672
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003673 CameraMetadata &metadata = request->mSettings;
3674 size_t count = mTriggerMap.size();
3675
3676 for (size_t i = 0; i < count; ++i) {
3677 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003678 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003679
3680 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3681 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3682 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003683 if (isAeTrigger) {
3684 request->mResultExtras.precaptureTriggerId = triggerId;
3685 mCurrentPreCaptureTriggerId = triggerId;
3686 } else {
3687 request->mResultExtras.afTriggerId = triggerId;
3688 mCurrentAfTriggerId = triggerId;
3689 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003690 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3691 continue; // Trigger ID tag is deprecated since device HAL 3.2
3692 }
3693 }
3694
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003695 camera_metadata_entry entry = metadata.find(tag);
3696
3697 if (entry.count > 0) {
3698 /**
3699 * Already has an entry for this trigger in the request.
3700 * Rewrite it with our requested trigger value.
3701 */
3702 RequestTrigger oldTrigger = trigger;
3703
3704 oldTrigger.entryValue = entry.data.u8[0];
3705
3706 mTriggerReplacedMap.add(tag, oldTrigger);
3707 } else {
3708 /**
3709 * More typical, no trigger entry, so we just add it
3710 */
3711 mTriggerRemovedMap.add(tag, trigger);
3712 }
3713
3714 status_t res;
3715
3716 switch (trigger.getTagType()) {
3717 case TYPE_BYTE: {
3718 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3719 res = metadata.update(tag,
3720 &entryValue,
3721 /*count*/1);
3722 break;
3723 }
3724 case TYPE_INT32:
3725 res = metadata.update(tag,
3726 &trigger.entryValue,
3727 /*count*/1);
3728 break;
3729 default:
3730 ALOGE("%s: Type not supported: 0x%x",
3731 __FUNCTION__,
3732 trigger.getTagType());
3733 return INVALID_OPERATION;
3734 }
3735
3736 if (res != OK) {
3737 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3738 ", value %d", __FUNCTION__, trigger.getTagName(),
3739 trigger.entryValue);
3740 return res;
3741 }
3742
3743 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3744 trigger.getTagName(),
3745 trigger.entryValue);
3746 }
3747
3748 mTriggerMap.clear();
3749
3750 return count;
3751}
3752
3753status_t Camera3Device::RequestThread::removeTriggers(
3754 const sp<CaptureRequest> &request) {
3755 Mutex::Autolock al(mTriggerMutex);
3756
3757 CameraMetadata &metadata = request->mSettings;
3758
3759 /**
3760 * Replace all old entries with their old values.
3761 */
3762 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3763 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3764
3765 status_t res;
3766
3767 uint32_t tag = trigger.metadataTag;
3768 switch (trigger.getTagType()) {
3769 case TYPE_BYTE: {
3770 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3771 res = metadata.update(tag,
3772 &entryValue,
3773 /*count*/1);
3774 break;
3775 }
3776 case TYPE_INT32:
3777 res = metadata.update(tag,
3778 &trigger.entryValue,
3779 /*count*/1);
3780 break;
3781 default:
3782 ALOGE("%s: Type not supported: 0x%x",
3783 __FUNCTION__,
3784 trigger.getTagType());
3785 return INVALID_OPERATION;
3786 }
3787
3788 if (res != OK) {
3789 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3790 ", trigger value %d", __FUNCTION__,
3791 trigger.getTagName(), trigger.entryValue);
3792 return res;
3793 }
3794 }
3795 mTriggerReplacedMap.clear();
3796
3797 /**
3798 * Remove all new entries.
3799 */
3800 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3801 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3802 status_t res = metadata.erase(trigger.metadataTag);
3803
3804 if (res != OK) {
3805 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3806 ", trigger value %d", __FUNCTION__,
3807 trigger.getTagName(), trigger.entryValue);
3808 return res;
3809 }
3810 }
3811 mTriggerRemovedMap.clear();
3812
3813 return OK;
3814}
3815
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003816status_t Camera3Device::RequestThread::addDummyTriggerIds(
3817 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003818 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003819 static const int32_t dummyTriggerId = 1;
3820 status_t res;
3821
3822 CameraMetadata &metadata = request->mSettings;
3823
3824 // If AF trigger is active, insert a dummy AF trigger ID if none already
3825 // exists
3826 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3827 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3828 if (afTrigger.count > 0 &&
3829 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3830 afId.count == 0) {
3831 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3832 if (res != OK) return res;
3833 }
3834
3835 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3836 // if none already exists
3837 camera_metadata_entry pcTrigger =
3838 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3839 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3840 if (pcTrigger.count > 0 &&
3841 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3842 pcId.count == 0) {
3843 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3844 &dummyTriggerId, 1);
3845 if (res != OK) return res;
3846 }
3847
3848 return OK;
3849}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003850
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003851/**
3852 * PreparerThread inner class methods
3853 */
3854
3855Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003856 Thread(/*canCallJava*/false), mListener(nullptr),
3857 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003858}
3859
3860Camera3Device::PreparerThread::~PreparerThread() {
3861 Thread::requestExitAndWait();
3862 if (mCurrentStream != nullptr) {
3863 mCurrentStream->cancelPrepare();
3864 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3865 mCurrentStream.clear();
3866 }
3867 clear();
3868}
3869
Ruben Brunkc78ac262015-08-13 17:58:46 -07003870status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003871 status_t res;
3872
3873 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003874 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003875
Ruben Brunkc78ac262015-08-13 17:58:46 -07003876 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003877 if (res == OK) {
3878 // No preparation needed, fire listener right off
3879 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003880 if (listener != NULL) {
3881 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003882 }
3883 return OK;
3884 } else if (res != NOT_ENOUGH_DATA) {
3885 return res;
3886 }
3887
3888 // Need to prepare, start up thread if necessary
3889 if (!mActive) {
3890 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3891 // isn't running
3892 Thread::requestExitAndWait();
3893 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3894 if (res != OK) {
3895 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003896 if (listener != NULL) {
3897 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003898 }
3899 return res;
3900 }
3901 mCancelNow = false;
3902 mActive = true;
3903 ALOGV("%s: Preparer stream started", __FUNCTION__);
3904 }
3905
3906 // queue up the work
3907 mPendingStreams.push_back(stream);
3908 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3909
3910 return OK;
3911}
3912
3913status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003914 Mutex::Autolock l(mLock);
3915
3916 for (const auto& stream : mPendingStreams) {
3917 stream->cancelPrepare();
3918 }
3919 mPendingStreams.clear();
3920 mCancelNow = true;
3921
3922 return OK;
3923}
3924
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003925void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003926 Mutex::Autolock l(mLock);
3927 mListener = listener;
3928}
3929
3930bool Camera3Device::PreparerThread::threadLoop() {
3931 status_t res;
3932 {
3933 Mutex::Autolock l(mLock);
3934 if (mCurrentStream == nullptr) {
3935 // End thread if done with work
3936 if (mPendingStreams.empty()) {
3937 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3938 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3939 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3940 mActive = false;
3941 return false;
3942 }
3943
3944 // Get next stream to prepare
3945 auto it = mPendingStreams.begin();
3946 mCurrentStream = *it;
3947 mPendingStreams.erase(it);
3948 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3949 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3950 } else if (mCancelNow) {
3951 mCurrentStream->cancelPrepare();
3952 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3953 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3954 mCurrentStream.clear();
3955 mCancelNow = false;
3956 return true;
3957 }
3958 }
3959
3960 res = mCurrentStream->prepareNextBuffer();
3961 if (res == NOT_ENOUGH_DATA) return true;
3962 if (res != OK) {
3963 // Something bad happened; try to recover by cancelling prepare and
3964 // signalling listener anyway
3965 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3966 mCurrentStream->getId(), res, strerror(-res));
3967 mCurrentStream->cancelPrepare();
3968 }
3969
3970 // This stream has finished, notify listener
3971 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003972 sp<NotificationListener> listener = mListener.promote();
3973 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003974 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3975 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003976 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003977 }
3978
3979 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3980 mCurrentStream.clear();
3981
3982 return true;
3983}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003984
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003985/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003986 * Static callback forwarding methods from HAL to instance
3987 */
3988
3989void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3990 const camera3_capture_result *result) {
3991 Camera3Device *d =
3992 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003993
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003994 d->processCaptureResult(result);
3995}
3996
3997void Camera3Device::sNotify(const camera3_callback_ops *cb,
3998 const camera3_notify_msg *msg) {
3999 Camera3Device *d =
4000 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
4001 d->notify(msg);
4002}
4003
4004}; // namespace android