blob: 5703934e47ec9e0fd781cf908b998125a2b03a7f [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>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046#include <android/hardware/camera2/ICameraDeviceUser.h>
47
Igor Murashkinff3e31d2013-10-23 16:40:06 -070048#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070049#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070050#include "device3/Camera3Device.h"
51#include "device3/Camera3OutputStream.h"
52#include "device3/Camera3InputStream.h"
53#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070054#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070055#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080056
57using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058
59namespace android {
60
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061Camera3Device::Camera3Device(int id):
62 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070063 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080064 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070065 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070066 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070067 mUsePartialResult(false),
68 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080069 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070070 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070071 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070072 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070073 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070074 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080075{
76 ATRACE_CALL();
77 camera3_callback_ops::notify = &sNotify;
78 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
79 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
80}
81
82Camera3Device::~Camera3Device()
83{
84 ATRACE_CALL();
85 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
86 disconnect();
87}
88
Igor Murashkin71381052013-03-04 14:53:08 -080089int Camera3Device::getId() const {
90 return mId;
91}
92
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080093/**
94 * CameraDeviceBase interface
95 */
96
Yin-Chia Yehe074a932015-01-30 10:29:02 -080097status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080098{
99 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700100 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800101 Mutex::Autolock l(mLock);
102
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800104 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700105 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800106 return INVALID_OPERATION;
107 }
108
109 /** Open HAL device */
110
111 status_t res;
112 String8 deviceName = String8::format("%d", mId);
113
114 camera3_device_t *device;
115
Zhijun He213ce792013-11-19 08:45:15 -0800116 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800117 res = module->open(deviceName.string(),
118 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800119 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800120
121 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800123 return res;
124 }
125
126 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700127 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700128 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700129 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700130 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800131 device->common.version);
132 device->common.close(&device->common);
133 return BAD_VALUE;
134 }
135
136 camera_info info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800137 res = module->getCameraInfo(mId, &info);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 if (res != OK) return res;
139
140 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700141 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
142 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700143 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800144 device->common.close(&device->common);
145 return BAD_VALUE;
146 }
147
148 /** Initialize device with callback functions */
149
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700150 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700152 ATRACE_END();
153
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800154 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700155 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
156 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800157 device->common.close(&device->common);
158 return BAD_VALUE;
159 }
160
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700161 /** Start up status tracker thread */
162 mStatusTracker = new StatusTracker(this);
163 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
164 if (res != OK) {
165 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
166 strerror(-res), res);
167 device->common.close(&device->common);
168 mStatusTracker.clear();
169 return res;
170 }
171
Zhijun He125684a2015-12-26 15:07:30 -0800172 /** Create buffer manager */
173 mBufferManager = new Camera3BufferManager();
174
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700175 bool aeLockAvailable = false;
176 camera_metadata_ro_entry aeLockAvailableEntry;
177 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
178 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
179 if (res == OK && aeLockAvailableEntry.count > 0) {
180 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
181 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
182 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800183
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700184 /** Start up request queue thread */
185 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800186 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700188 SET_ERR_L("Unable to start request queue thread: %s (%d)",
189 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800191 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 return res;
193 }
194
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700195 mPreparerThread = new PreparerThread();
196
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800197 /** Everything is good to go */
198
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700199 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800200 mDeviceInfo = info.static_camera_characteristics;
201 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700202
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700203 // Determine whether we need to derive sensitivity boost values for older devices.
204 // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
205 // be listed (as the default value 100)
206 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_4 &&
207 mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
208 mDerivePostRawSensKey = true;
209 }
210
Ruben Brunk183f0562015-08-12 12:55:02 -0700211 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800212 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700213 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700214 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700215 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800216
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800217 // Measure the clock domain offset between camera and video/hw_composer
218 camera_metadata_entry timestampSource =
219 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
220 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
221 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
222 mTimestampOffset = getMonoToBoottimeOffset();
223 }
224
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700225 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700226 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
227 camera_metadata_entry partialResultsCount =
228 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
229 if (partialResultsCount.count > 0) {
230 mNumPartialResults = partialResultsCount.data.i32[0];
231 mUsePartialResult = (mNumPartialResults > 1);
232 }
233 } else {
234 camera_metadata_entry partialResultsQuirk =
235 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
236 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
237 mUsePartialResult = true;
238 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700239 }
240
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700241 camera_metadata_entry configs =
242 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
243 for (uint32_t i = 0; i < configs.count; i += 4) {
244 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
245 configs.data.i32[i + 3] ==
246 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
247 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
248 configs.data.i32[i + 2]));
249 }
250 }
251
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800252 return OK;
253}
254
255status_t Camera3Device::disconnect() {
256 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700257 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800258
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700259 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800260
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700261 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800262
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700263 {
264 Mutex::Autolock l(mLock);
265 if (mStatus == STATUS_UNINITIALIZED) return res;
266
267 if (mStatus == STATUS_ACTIVE ||
268 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
269 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700270 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700271 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700272 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700273 } else {
274 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
275 if (res != OK) {
276 SET_ERR_L("Timeout waiting for HAL to drain");
277 // Continue to close device even in case of error
278 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700279 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800280 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800281
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700282 if (mStatus == STATUS_ERROR) {
283 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700284 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700285
286 if (mStatusTracker != NULL) {
287 mStatusTracker->requestExit();
288 }
289
290 if (mRequestThread != NULL) {
291 mRequestThread->requestExit();
292 }
293
294 mOutputStreams.clear();
295 mInputStream.clear();
296 }
297
298 // Joining done without holding mLock, otherwise deadlocks may ensue
299 // as the threads try to access parent state
300 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
301 // HAL may be in a bad state, so waiting for request thread
302 // (which may be stuck in the HAL processCaptureRequest call)
303 // could be dangerous.
304 mRequestThread->join();
305 }
306
307 if (mStatusTracker != NULL) {
308 mStatusTracker->join();
309 }
310
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700311 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700312 {
313 Mutex::Autolock l(mLock);
314
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800315 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700316 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800317 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800318
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700319 hal3Device = mHal3Device;
320 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800321
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700322 // Call close without internal mutex held, as the HAL close may need to
323 // wait on assorted callbacks,etc, to complete before it can return.
324 if (hal3Device != NULL) {
325 ATRACE_BEGIN("camera3->close");
326 hal3Device->common.close(&hal3Device->common);
327 ATRACE_END();
328 }
329
330 {
331 Mutex::Autolock l(mLock);
332 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700333 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700334 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800335
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700336 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700337 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800338}
339
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700340// For dumping/debugging only -
341// try to acquire a lock a few times, eventually give up to proceed with
342// debug/dump operations
343bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
344 bool gotLock = false;
345 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
346 if (lock.tryLock() == NO_ERROR) {
347 gotLock = true;
348 break;
349 } else {
350 usleep(kDumpSleepDuration);
351 }
352 }
353 return gotLock;
354}
355
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700356Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
357 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
358 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
359 const int STREAM_CONFIGURATION_SIZE = 4;
360 const int STREAM_FORMAT_OFFSET = 0;
361 const int STREAM_WIDTH_OFFSET = 1;
362 const int STREAM_HEIGHT_OFFSET = 2;
363 const int STREAM_IS_INPUT_OFFSET = 3;
364 camera_metadata_ro_entry_t availableStreamConfigs =
365 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
366 if (availableStreamConfigs.count == 0 ||
367 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
368 return Size(0, 0);
369 }
370
371 // Get max jpeg size (area-wise).
372 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
373 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
374 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
375 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
376 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
377 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
378 && format == HAL_PIXEL_FORMAT_BLOB &&
379 (width * height > maxJpegWidth * maxJpegHeight)) {
380 maxJpegWidth = width;
381 maxJpegHeight = height;
382 }
383 }
384 } else {
385 camera_metadata_ro_entry availableJpegSizes =
386 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
387 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
388 return Size(0, 0);
389 }
390
391 // Get max jpeg size (area-wise).
392 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
393 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
394 > (maxJpegWidth * maxJpegHeight)) {
395 maxJpegWidth = availableJpegSizes.data.i32[i];
396 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
397 }
398 }
399 }
400 return Size(maxJpegWidth, maxJpegHeight);
401}
402
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800403nsecs_t Camera3Device::getMonoToBoottimeOffset() {
404 // try three times to get the clock offset, choose the one
405 // with the minimum gap in measurements.
406 const int tries = 3;
407 nsecs_t bestGap, measured;
408 for (int i = 0; i < tries; ++i) {
409 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
410 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
411 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
412 const nsecs_t gap = tmono2 - tmono;
413 if (i == 0 || gap < bestGap) {
414 bestGap = gap;
415 measured = tbase - ((tmono + tmono2) >> 1);
416 }
417 }
418 return measured;
419}
420
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -0700421/**
422 * Map Android N dataspace definitions back to Android M definitions, for
423 * use with HALv3.3 or older.
424 *
425 * Only map where correspondences exist, and otherwise preserve the value.
426 */
427android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) {
428 switch (dataSpace) {
429 case HAL_DATASPACE_V0_SRGB_LINEAR:
430 return HAL_DATASPACE_SRGB_LINEAR;
431 case HAL_DATASPACE_V0_SRGB:
432 return HAL_DATASPACE_SRGB;
433 case HAL_DATASPACE_V0_JFIF:
434 return HAL_DATASPACE_JFIF;
435 case HAL_DATASPACE_V0_BT601_625:
436 return HAL_DATASPACE_BT601_625;
437 case HAL_DATASPACE_V0_BT601_525:
438 return HAL_DATASPACE_BT601_525;
439 case HAL_DATASPACE_V0_BT709:
440 return HAL_DATASPACE_BT709;
441 default:
442 return dataSpace;
443 }
444}
445
Zhijun Hef7da0962014-04-24 13:27:56 -0700446ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700447 // Get max jpeg size (area-wise).
448 Size maxJpegResolution = getMaxJpegResolution();
449 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700450 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700451 __FUNCTION__, mId);
452 return BAD_VALUE;
453 }
454
Zhijun Hef7da0962014-04-24 13:27:56 -0700455 // Get max jpeg buffer size
456 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700457 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
458 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700459 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
460 return BAD_VALUE;
461 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700462 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800463 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700464
465 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700466 float scaleFactor = ((float) (width * height)) /
467 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800468 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
469 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700470 if (jpegBufferSize > maxJpegBufferSize) {
471 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700472 }
473
474 return jpegBufferSize;
475}
476
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700477ssize_t Camera3Device::getPointCloudBufferSize() const {
478 const int FLOATS_PER_POINT=4;
479 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
480 if (maxPointCount.count == 0) {
481 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
482 __FUNCTION__, mId);
483 return BAD_VALUE;
484 }
485 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
486 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
487 return maxBytesForPointCloud;
488}
489
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800490ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800491 const int PER_CONFIGURATION_SIZE = 3;
492 const int WIDTH_OFFSET = 0;
493 const int HEIGHT_OFFSET = 1;
494 const int SIZE_OFFSET = 2;
495 camera_metadata_ro_entry rawOpaqueSizes =
496 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800497 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800498 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800499 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800500 __FUNCTION__, mId, count);
501 return BAD_VALUE;
502 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700503
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800504 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
505 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
506 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
507 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
508 }
509 }
510
511 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
512 __FUNCTION__, mId, width, height);
513 return BAD_VALUE;
514}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700515
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800516status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
517 ATRACE_CALL();
518 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700519
520 // Try to lock, but continue in case of failure (to avoid blocking in
521 // deadlocks)
522 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
523 bool gotLock = tryLockSpinRightRound(mLock);
524
525 ALOGW_IF(!gotInterfaceLock,
526 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
527 mId, __FUNCTION__);
528 ALOGW_IF(!gotLock,
529 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
530 mId, __FUNCTION__);
531
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800532 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700533
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800534 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700535 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800536 int n = args.size();
537 for (int i = 0; i < n; i++) {
538 if (args[i] == templatesOption) {
539 dumpTemplates = true;
540 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700541 if (args[i] == monitorOption) {
542 if (i + 1 < n) {
543 String8 monitorTags = String8(args[i + 1]);
544 if (monitorTags == "off") {
545 mTagMonitor.disableMonitoring();
546 } else {
547 mTagMonitor.parseTagsToMonitor(monitorTags);
548 }
549 } else {
550 mTagMonitor.disableMonitoring();
551 }
552 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800553 }
554
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800555 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800556
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800557 const char *status =
558 mStatus == STATUS_ERROR ? "ERROR" :
559 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700560 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
561 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800562 mStatus == STATUS_ACTIVE ? "ACTIVE" :
563 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700564
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800565 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700566 if (mStatus == STATUS_ERROR) {
567 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
568 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800569 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700570 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700571 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800572
573 if (mInputStream != NULL) {
574 write(fd, lines.string(), lines.size());
575 mInputStream->dump(fd, args);
576 } else {
577 lines.appendFormat(" No input stream.\n");
578 write(fd, lines.string(), lines.size());
579 }
580 for (size_t i = 0; i < mOutputStreams.size(); i++) {
581 mOutputStreams[i]->dump(fd,args);
582 }
583
Zhijun He431503c2016-03-07 17:30:16 -0800584 if (mBufferManager != NULL) {
585 lines = String8(" Camera3 Buffer Manager:\n");
586 write(fd, lines.string(), lines.size());
587 mBufferManager->dump(fd, args);
588 }
Zhijun He125684a2015-12-26 15:07:30 -0800589
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700590 lines = String8(" In-flight requests:\n");
591 if (mInFlightMap.size() == 0) {
592 lines.append(" None\n");
593 } else {
594 for (size_t i = 0; i < mInFlightMap.size(); i++) {
595 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700596 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700597 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800598 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700599 r.numBuffersLeft);
600 }
601 }
602 write(fd, lines.string(), lines.size());
603
Igor Murashkin1e479c02013-09-06 16:55:14 -0700604 {
605 lines = String8(" Last request sent:\n");
606 write(fd, lines.string(), lines.size());
607
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700608 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700609 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
610 }
611
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800612 if (dumpTemplates) {
613 const char *templateNames[] = {
614 "TEMPLATE_PREVIEW",
615 "TEMPLATE_STILL_CAPTURE",
616 "TEMPLATE_VIDEO_RECORD",
617 "TEMPLATE_VIDEO_SNAPSHOT",
618 "TEMPLATE_ZERO_SHUTTER_LAG",
619 "TEMPLATE_MANUAL"
620 };
621
622 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
623 const camera_metadata_t *templateRequest;
624 templateRequest =
625 mHal3Device->ops->construct_default_request_settings(
626 mHal3Device, i);
627 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
628 if (templateRequest == NULL) {
629 lines.append(" Not supported\n");
630 write(fd, lines.string(), lines.size());
631 } else {
632 write(fd, lines.string(), lines.size());
633 dump_indented_camera_metadata(templateRequest,
634 fd, /*verbosity*/2, /*indentation*/8);
635 }
636 }
637 }
638
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700639 mTagMonitor.dumpMonitoredMetadata(fd);
640
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800641 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700642 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800643 write(fd, lines.string(), lines.size());
644 mHal3Device->ops->dump(mHal3Device, fd);
645 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800646
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700647 if (gotLock) mLock.unlock();
648 if (gotInterfaceLock) mInterfaceLock.unlock();
649
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800650 return OK;
651}
652
653const CameraMetadata& Camera3Device::info() const {
654 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800655 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
656 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700657 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800658 mStatus == STATUS_ERROR ?
659 "when in error state" : "before init");
660 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800661 return mDeviceInfo;
662}
663
Jianing Wei90e59c92014-03-12 18:29:36 -0700664status_t Camera3Device::checkStatusOkToCaptureLocked() {
665 switch (mStatus) {
666 case STATUS_ERROR:
667 CLOGE("Device has encountered a serious error");
668 return INVALID_OPERATION;
669 case STATUS_UNINITIALIZED:
670 CLOGE("Device not initialized");
671 return INVALID_OPERATION;
672 case STATUS_UNCONFIGURED:
673 case STATUS_CONFIGURED:
674 case STATUS_ACTIVE:
675 // OK
676 break;
677 default:
678 SET_ERR_L("Unexpected status: %d", mStatus);
679 return INVALID_OPERATION;
680 }
681 return OK;
682}
683
684status_t Camera3Device::convertMetadataListToRequestListLocked(
685 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
686 if (requestList == NULL) {
687 CLOGE("requestList cannot be NULL.");
688 return BAD_VALUE;
689 }
690
Jianing Weicb0652e2014-03-12 18:29:36 -0700691 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700692 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
693 it != metadataList.end(); ++it) {
694 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
695 if (newRequest == 0) {
696 CLOGE("Can't create capture request");
697 return BAD_VALUE;
698 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700699
700 // Setup burst Id and request Id
701 newRequest->mResultExtras.burstId = burstId++;
702 if (it->exists(ANDROID_REQUEST_ID)) {
703 if (it->find(ANDROID_REQUEST_ID).count == 0) {
704 CLOGE("RequestID entry exists; but must not be empty in metadata");
705 return BAD_VALUE;
706 }
707 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
708 } else {
709 CLOGE("RequestID does not exist in metadata");
710 return BAD_VALUE;
711 }
712
Jianing Wei90e59c92014-03-12 18:29:36 -0700713 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700714
715 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700716 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700717
718 // Setup batch size if this is a high speed video recording request.
719 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
720 auto firstRequest = requestList->begin();
721 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
722 if (outputStream->isVideoStream()) {
723 (*firstRequest)->mBatchSize = requestList->size();
724 break;
725 }
726 }
727 }
728
Jianing Wei90e59c92014-03-12 18:29:36 -0700729 return OK;
730}
731
Jianing Weicb0652e2014-03-12 18:29:36 -0700732status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800733 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800734
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700735 List<const CameraMetadata> requests;
736 requests.push_back(request);
737 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800738}
739
Jianing Wei90e59c92014-03-12 18:29:36 -0700740status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700741 const List<const CameraMetadata> &requests, bool repeating,
742 /*out*/
743 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700744 ATRACE_CALL();
745 Mutex::Autolock il(mInterfaceLock);
746 Mutex::Autolock l(mLock);
747
748 status_t res = checkStatusOkToCaptureLocked();
749 if (res != OK) {
750 // error logged by previous call
751 return res;
752 }
753
754 RequestList requestList;
755
756 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
757 if (res != OK) {
758 // error logged by previous call
759 return res;
760 }
761
762 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700763 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700764 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700765 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700766 }
767
768 if (res == OK) {
769 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
770 if (res != OK) {
771 SET_ERR_L("Can't transition to active in %f seconds!",
772 kActiveTimeout/1e9);
773 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700774 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
775 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700776 } else {
777 CLOGE("Cannot queue request. Impossible.");
778 return BAD_VALUE;
779 }
780
781 return res;
782}
783
Jianing Weicb0652e2014-03-12 18:29:36 -0700784status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
785 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700786 ATRACE_CALL();
787
Jianing Weicb0652e2014-03-12 18:29:36 -0700788 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700789}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800790
Jianing Weicb0652e2014-03-12 18:29:36 -0700791status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
792 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800793 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800794
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700795 List<const CameraMetadata> requests;
796 requests.push_back(request);
797 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800798}
799
Jianing Weicb0652e2014-03-12 18:29:36 -0700800status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
801 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700802 ATRACE_CALL();
803
Jianing Weicb0652e2014-03-12 18:29:36 -0700804 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700805}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800806
807sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
808 const CameraMetadata &request) {
809 status_t res;
810
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700811 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800812 res = configureStreamsLocked();
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700813 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800814 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700815 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800816 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700817 } else if (mStatus == STATUS_UNCONFIGURED) {
818 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700819 CLOGE("No streams configured");
820 return NULL;
821 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800822 }
823
824 sp<CaptureRequest> newRequest = createCaptureRequest(request);
825 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800826}
827
Jianing Weicb0652e2014-03-12 18:29:36 -0700828status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800829 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700830 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800831 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800832
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800833 switch (mStatus) {
834 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700835 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800836 return INVALID_OPERATION;
837 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700838 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800839 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700840 case STATUS_UNCONFIGURED:
841 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800842 case STATUS_ACTIVE:
843 // OK
844 break;
845 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700846 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800847 return INVALID_OPERATION;
848 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700849 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700850
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700851 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800852}
853
854status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
855 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700856 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800857
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700858 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800859}
860
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700861status_t Camera3Device::createInputStream(
862 uint32_t width, uint32_t height, int format, int *id) {
863 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700864 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700865 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700866 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
867 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700868
869 status_t res;
870 bool wasActive = false;
871
872 switch (mStatus) {
873 case STATUS_ERROR:
874 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
875 return INVALID_OPERATION;
876 case STATUS_UNINITIALIZED:
877 ALOGE("%s: Device not initialized", __FUNCTION__);
878 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700879 case STATUS_UNCONFIGURED:
880 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700881 // OK
882 break;
883 case STATUS_ACTIVE:
884 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700885 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700886 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700887 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700888 return res;
889 }
890 wasActive = true;
891 break;
892 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700893 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700894 return INVALID_OPERATION;
895 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700896 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700897
898 if (mInputStream != 0) {
899 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
900 return INVALID_OPERATION;
901 }
902
903 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
904 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700905 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700906
907 mInputStream = newStream;
908
909 *id = mNextStreamId++;
910
911 // Continue captures if active at start
912 if (wasActive) {
913 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
914 res = configureStreamsLocked();
915 if (res != OK) {
916 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
917 __FUNCTION__, mNextStreamId, strerror(-res), res);
918 return res;
919 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700920 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700921 }
922
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700923 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700924 return OK;
925}
926
Igor Murashkin2fba5842013-04-22 14:03:54 -0700927
928status_t Camera3Device::createZslStream(
929 uint32_t width, uint32_t height,
930 int depth,
931 /*out*/
932 int *id,
933 sp<Camera3ZslStream>* zslStream) {
934 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700935 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700936 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700937 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
938 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700939
940 status_t res;
941 bool wasActive = false;
942
943 switch (mStatus) {
944 case STATUS_ERROR:
945 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
946 return INVALID_OPERATION;
947 case STATUS_UNINITIALIZED:
948 ALOGE("%s: Device not initialized", __FUNCTION__);
949 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700950 case STATUS_UNCONFIGURED:
951 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700952 // OK
953 break;
954 case STATUS_ACTIVE:
955 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700956 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700957 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700958 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700959 return res;
960 }
961 wasActive = true;
962 break;
963 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700964 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700965 return INVALID_OPERATION;
966 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700967 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700968
969 if (mInputStream != 0) {
970 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
971 return INVALID_OPERATION;
972 }
973
974 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
975 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700976 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700977
978 res = mOutputStreams.add(mNextStreamId, newStream);
979 if (res < 0) {
980 ALOGE("%s: Can't add new stream to set: %s (%d)",
981 __FUNCTION__, strerror(-res), res);
982 return res;
983 }
984 mInputStream = newStream;
985
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530986 mNeedConfig = true;
987
Igor Murashkin2fba5842013-04-22 14:03:54 -0700988 *id = mNextStreamId++;
989 *zslStream = newStream;
990
991 // Continue captures if active at start
992 if (wasActive) {
993 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
994 res = configureStreamsLocked();
995 if (res != OK) {
996 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
997 __FUNCTION__, mNextStreamId, strerror(-res), res);
998 return res;
999 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001000 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -07001001 }
1002
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001003 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -07001004 return OK;
1005}
1006
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001007status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001008 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -07001009 camera3_stream_rotation_t rotation, int *id, int streamSetId, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001010 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001011 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001012 Mutex::Autolock l(mLock);
Zhijun He5d677d12016-05-29 16:52:39 -07001013 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
1014 " consumer usage 0x%x", mId, mNextStreamId, width, height, format, dataSpace, rotation,
1015 consumerUsage);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001016
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001017 status_t res;
1018 bool wasActive = false;
1019
1020 switch (mStatus) {
1021 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001022 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001023 return INVALID_OPERATION;
1024 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001025 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001026 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001027 case STATUS_UNCONFIGURED:
1028 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001029 // OK
1030 break;
1031 case STATUS_ACTIVE:
1032 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001033 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001034 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001035 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001036 return res;
1037 }
1038 wasActive = true;
1039 break;
1040 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001041 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001042 return INVALID_OPERATION;
1043 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001044 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001045
1046 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001047 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001048 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001049 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001050 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1051 }
Zhijun He5d677d12016-05-29 16:52:39 -07001052
1053 // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
1054 // which requires a consumer surface to be available.
1055 if (consumer == nullptr && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1056 ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
1057 return BAD_VALUE;
1058 }
1059
1060 if (consumer == nullptr && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1061 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1062 return BAD_VALUE;
1063 }
1064
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001065 // Use legacy dataspace values for older HALs
1066 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1067 dataSpace = mapToLegacyDataspace(dataSpace);
1068 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001070 ssize_t blobBufferSize;
1071 if (dataSpace != HAL_DATASPACE_DEPTH) {
1072 blobBufferSize = getJpegBufferSize(width, height);
1073 if (blobBufferSize <= 0) {
1074 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1075 return BAD_VALUE;
1076 }
1077 } else {
1078 blobBufferSize = getPointCloudBufferSize();
1079 if (blobBufferSize <= 0) {
1080 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1081 return BAD_VALUE;
1082 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001083 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001084 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001085 width, height, blobBufferSize, format, dataSpace, rotation,
1086 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001087 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1088 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1089 if (rawOpaqueBufferSize <= 0) {
1090 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1091 return BAD_VALUE;
1092 }
1093 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001094 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1095 mTimestampOffset, streamSetId);
Zhijun He5d677d12016-05-29 16:52:39 -07001096 } else if (consumer == nullptr) {
1097 newStream = new Camera3OutputStream(mNextStreamId,
1098 width, height, format, consumerUsage, dataSpace, rotation,
1099 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001100 } else {
1101 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001102 width, height, format, dataSpace, rotation,
1103 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001104 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001105 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001106
Zhijun He125684a2015-12-26 15:07:30 -08001107 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001108 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1109 * requires buffers to be statically allocated for internal static buffer registration, while
1110 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1111 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001112 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001113 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001114 newStream->setBufferManager(mBufferManager);
1115 }
1116
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001117 res = mOutputStreams.add(mNextStreamId, newStream);
1118 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001119 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001120 return res;
1121 }
1122
1123 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001124 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001125
1126 // Continue captures if active at start
1127 if (wasActive) {
1128 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1129 res = configureStreamsLocked();
1130 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001131 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1132 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001133 return res;
1134 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001135 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001136 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001137 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001138 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001139}
1140
1141status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1142 ATRACE_CALL();
1143 (void)outputId; (void)id;
1144
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001145 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001146 return INVALID_OPERATION;
1147}
1148
1149
1150status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001151 uint32_t *width, uint32_t *height,
1152 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001153 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001154 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001155 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001156
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001157 switch (mStatus) {
1158 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001159 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001160 return INVALID_OPERATION;
1161 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001162 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001163 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001164 case STATUS_UNCONFIGURED:
1165 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001166 case STATUS_ACTIVE:
1167 // OK
1168 break;
1169 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001170 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001171 return INVALID_OPERATION;
1172 }
1173
1174 ssize_t idx = mOutputStreams.indexOfKey(id);
1175 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001176 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177 return idx;
1178 }
1179
1180 if (width) *width = mOutputStreams[idx]->getWidth();
1181 if (height) *height = mOutputStreams[idx]->getHeight();
1182 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001183 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001184 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001185}
1186
1187status_t Camera3Device::setStreamTransform(int id,
1188 int transform) {
1189 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001190 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001191 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001192
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001193 switch (mStatus) {
1194 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001195 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001196 return INVALID_OPERATION;
1197 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001198 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001199 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001200 case STATUS_UNCONFIGURED:
1201 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001202 case STATUS_ACTIVE:
1203 // OK
1204 break;
1205 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001206 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001207 return INVALID_OPERATION;
1208 }
1209
1210 ssize_t idx = mOutputStreams.indexOfKey(id);
1211 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001212 CLOGE("Stream %d does not exist",
1213 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001214 return BAD_VALUE;
1215 }
1216
1217 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001218}
1219
1220status_t Camera3Device::deleteStream(int id) {
1221 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001222 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001223 Mutex::Autolock l(mLock);
1224 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001225
Igor Murashkine2172be2013-05-28 15:31:39 -07001226 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1227
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001228 // CameraDevice semantics require device to already be idle before
1229 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001230 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001231 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1232 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001233 }
1234
Igor Murashkin2fba5842013-04-22 14:03:54 -07001235 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001236 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001237 if (mInputStream != NULL && id == mInputStream->getId()) {
1238 deletedStream = mInputStream;
1239 mInputStream.clear();
1240 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001241 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001242 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001243 return BAD_VALUE;
1244 }
Zhijun He5f446352014-01-22 09:49:33 -08001245 }
1246
1247 // Delete output stream or the output part of a bi-directional stream.
1248 if (outputStreamIdx != NAME_NOT_FOUND) {
1249 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001250 mOutputStreams.removeItem(id);
1251 }
1252
1253 // Free up the stream endpoint so that it can be used by some other stream
1254 res = deletedStream->disconnect();
1255 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001256 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001257 // fall through since we want to still list the stream as deleted.
1258 }
1259 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001260 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001261
1262 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001263}
1264
1265status_t Camera3Device::deleteReprocessStream(int id) {
1266 ATRACE_CALL();
1267 (void)id;
1268
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001269 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001270 return INVALID_OPERATION;
1271}
1272
Zhijun He1fa89992015-06-01 15:44:31 -07001273status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001274 ATRACE_CALL();
1275 ALOGV("%s: E", __FUNCTION__);
1276
1277 Mutex::Autolock il(mInterfaceLock);
1278 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001279
1280 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1281 mNeedConfig = true;
1282 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1283 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001284
1285 return configureStreamsLocked();
1286}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001287
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001288status_t Camera3Device::getInputBufferProducer(
1289 sp<IGraphicBufferProducer> *producer) {
1290 Mutex::Autolock il(mInterfaceLock);
1291 Mutex::Autolock l(mLock);
1292
1293 if (producer == NULL) {
1294 return BAD_VALUE;
1295 } else if (mInputStream == NULL) {
1296 return INVALID_OPERATION;
1297 }
1298
1299 return mInputStream->getInputBufferProducer(producer);
1300}
1301
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001302status_t Camera3Device::createDefaultRequest(int templateId,
1303 CameraMetadata *request) {
1304 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001305 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001306
1307 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1308 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1309 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1310 return BAD_VALUE;
1311 }
1312
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001313 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001314 Mutex::Autolock l(mLock);
1315
1316 switch (mStatus) {
1317 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001318 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001319 return INVALID_OPERATION;
1320 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001321 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001322 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001323 case STATUS_UNCONFIGURED:
1324 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001325 case STATUS_ACTIVE:
1326 // OK
1327 break;
1328 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001329 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001330 return INVALID_OPERATION;
1331 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001332
Zhijun Hea1530f12014-09-14 12:44:20 -07001333 if (!mRequestTemplateCache[templateId].isEmpty()) {
1334 *request = mRequestTemplateCache[templateId];
1335 return OK;
1336 }
1337
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001338 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001339 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001340 rawRequest = mHal3Device->ops->construct_default_request_settings(
1341 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001342 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001343 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001344 ALOGI("%s: template %d is not supported on this camera device",
1345 __FUNCTION__, templateId);
1346 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001347 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001348
Zhijun Hea1530f12014-09-14 12:44:20 -07001349 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001350
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001351 // Derive some new keys for backward compatibility
1352 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
1353 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
1354 int32_t defaultBoost[1] = {100};
1355 mRequestTemplateCache[templateId].update(
1356 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1357 defaultBoost, 1);
1358 }
1359
1360 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001361 return OK;
1362}
1363
1364status_t Camera3Device::waitUntilDrained() {
1365 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001366 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001367 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001368
Zhijun He69a37482014-03-23 18:44:49 -07001369 return waitUntilDrainedLocked();
1370}
1371
1372status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001373 switch (mStatus) {
1374 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001375 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001376 ALOGV("%s: Already idle", __FUNCTION__);
1377 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001378 case STATUS_CONFIGURED:
1379 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001380 case STATUS_ERROR:
1381 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001382 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001383 break;
1384 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001385 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001386 return INVALID_OPERATION;
1387 }
1388
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001389 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1390 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001391 if (res != OK) {
1392 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1393 res);
1394 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001395 return res;
1396}
1397
Ruben Brunk183f0562015-08-12 12:55:02 -07001398
1399void Camera3Device::internalUpdateStatusLocked(Status status) {
1400 mStatus = status;
1401 mRecentStatusUpdates.add(mStatus);
1402 mStatusChanged.broadcast();
1403}
1404
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001405// Pause to reconfigure
1406status_t Camera3Device::internalPauseAndWaitLocked() {
1407 mRequestThread->setPaused(true);
1408 mPauseStateNotify = true;
1409
1410 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1411 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1412 if (res != OK) {
1413 SET_ERR_L("Can't idle device in %f seconds!",
1414 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001415 }
1416
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001417 return res;
1418}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001419
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001420// Resume after internalPauseAndWaitLocked
1421status_t Camera3Device::internalResumeLocked() {
1422 status_t res;
1423
1424 mRequestThread->setPaused(false);
1425
1426 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1427 if (res != OK) {
1428 SET_ERR_L("Can't transition to active in %f seconds!",
1429 kActiveTimeout/1e9);
1430 }
1431 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001432 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001433}
1434
Ruben Brunk183f0562015-08-12 12:55:02 -07001435status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001436 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001437
1438 size_t startIndex = 0;
1439 if (mStatusWaiters == 0) {
1440 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1441 // this status list
1442 mRecentStatusUpdates.clear();
1443 } else {
1444 // If other threads are waiting on updates to this status list, set the position of the
1445 // first element that this list will check rather than clearing the list.
1446 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001447 }
1448
Ruben Brunk183f0562015-08-12 12:55:02 -07001449 mStatusWaiters++;
1450
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001451 bool stateSeen = false;
1452 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001453 if (active == (mStatus == STATUS_ACTIVE)) {
1454 // Desired state is current
1455 break;
1456 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001457
1458 res = mStatusChanged.waitRelative(mLock, timeout);
1459 if (res != OK) break;
1460
Ruben Brunk183f0562015-08-12 12:55:02 -07001461 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1462 // transitions.
1463 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1464 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1465 __FUNCTION__);
1466
1467 // Encountered desired state since we began waiting
1468 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001469 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1470 stateSeen = true;
1471 break;
1472 }
1473 }
1474 } while (!stateSeen);
1475
Ruben Brunk183f0562015-08-12 12:55:02 -07001476 mStatusWaiters--;
1477
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001478 return res;
1479}
1480
1481
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001482status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001483 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001484 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001485
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001486 if (listener != NULL && mListener != NULL) {
1487 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1488 }
1489 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001490 mRequestThread->setNotificationListener(listener);
1491 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001492
1493 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001494}
1495
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001496bool Camera3Device::willNotify3A() {
1497 return false;
1498}
1499
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001500status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001501 status_t res;
1502 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001503
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001504 while (mResultQueue.empty()) {
1505 res = mResultSignal.waitRelative(mOutputLock, timeout);
1506 if (res == TIMED_OUT) {
1507 return res;
1508 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001509 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001510 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001511 return res;
1512 }
1513 }
1514 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001515}
1516
Jianing Weicb0652e2014-03-12 18:29:36 -07001517status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001518 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001519 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001520
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001521 if (mResultQueue.empty()) {
1522 return NOT_ENOUGH_DATA;
1523 }
1524
Jianing Weicb0652e2014-03-12 18:29:36 -07001525 if (frame == NULL) {
1526 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1527 return BAD_VALUE;
1528 }
1529
1530 CaptureResult &result = *(mResultQueue.begin());
1531 frame->mResultExtras = result.mResultExtras;
1532 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001533 mResultQueue.erase(mResultQueue.begin());
1534
1535 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001536}
1537
1538status_t Camera3Device::triggerAutofocus(uint32_t id) {
1539 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001540 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001541
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001542 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1543 // Mix-in this trigger into the next request and only the next request.
1544 RequestTrigger trigger[] = {
1545 {
1546 ANDROID_CONTROL_AF_TRIGGER,
1547 ANDROID_CONTROL_AF_TRIGGER_START
1548 },
1549 {
1550 ANDROID_CONTROL_AF_TRIGGER_ID,
1551 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001552 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001553 };
1554
1555 return mRequestThread->queueTrigger(trigger,
1556 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001557}
1558
1559status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1560 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001561 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001562
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001563 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1564 // Mix-in this trigger into the next request and only the next request.
1565 RequestTrigger trigger[] = {
1566 {
1567 ANDROID_CONTROL_AF_TRIGGER,
1568 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1569 },
1570 {
1571 ANDROID_CONTROL_AF_TRIGGER_ID,
1572 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001573 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001574 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001575
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001576 return mRequestThread->queueTrigger(trigger,
1577 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001578}
1579
1580status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1581 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001582 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001583
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001584 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1585 // Mix-in this trigger into the next request and only the next request.
1586 RequestTrigger trigger[] = {
1587 {
1588 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1589 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1590 },
1591 {
1592 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1593 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001594 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001595 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001596
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001597 return mRequestThread->queueTrigger(trigger,
1598 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001599}
1600
1601status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1602 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1603 ATRACE_CALL();
1604 (void)reprocessStreamId; (void)buffer; (void)listener;
1605
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001606 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001607 return INVALID_OPERATION;
1608}
1609
Jianing Weicb0652e2014-03-12 18:29:36 -07001610status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001611 ATRACE_CALL();
1612 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001613 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001614
Zhijun He7ef20392014-04-21 16:04:17 -07001615 {
1616 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001617 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001618 }
1619
Zhijun He491e3412013-12-27 10:57:44 -08001620 status_t res;
1621 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001622 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001623 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001624 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001625 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001626 }
1627
1628 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001629}
1630
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001631status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001632 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1633}
1634
1635status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001636 ATRACE_CALL();
1637 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001638 Mutex::Autolock il(mInterfaceLock);
1639 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001640
1641 sp<Camera3StreamInterface> stream;
1642 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1643 if (outputStreamIdx == NAME_NOT_FOUND) {
1644 CLOGE("Stream %d does not exist", streamId);
1645 return BAD_VALUE;
1646 }
1647
1648 stream = mOutputStreams.editValueAt(outputStreamIdx);
1649
1650 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001651 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001652 return BAD_VALUE;
1653 }
1654
1655 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001656 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001657 return BAD_VALUE;
1658 }
1659
Ruben Brunkc78ac262015-08-13 17:58:46 -07001660 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001661}
1662
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001663status_t Camera3Device::tearDown(int streamId) {
1664 ATRACE_CALL();
1665 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1666 Mutex::Autolock il(mInterfaceLock);
1667 Mutex::Autolock l(mLock);
1668
1669 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1670 // since we cannot call register_stream_buffers except right after configure_streams.
1671 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1672 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1673 __FUNCTION__, mHal3Device->common.version);
1674 return NO_INIT;
1675 }
1676
1677 sp<Camera3StreamInterface> stream;
1678 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1679 if (outputStreamIdx == NAME_NOT_FOUND) {
1680 CLOGE("Stream %d does not exist", streamId);
1681 return BAD_VALUE;
1682 }
1683
1684 stream = mOutputStreams.editValueAt(outputStreamIdx);
1685
1686 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1687 CLOGE("Stream %d is a target of a in-progress request", streamId);
1688 return BAD_VALUE;
1689 }
1690
1691 return stream->tearDown();
1692}
1693
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001694status_t Camera3Device::addBufferListenerForStream(int streamId,
1695 wp<Camera3StreamBufferListener> listener) {
1696 ATRACE_CALL();
1697 ALOGV("%s: Camera %d: Adding buffer listener for stream %d", __FUNCTION__, mId, streamId);
1698 Mutex::Autolock il(mInterfaceLock);
1699 Mutex::Autolock l(mLock);
1700
1701 sp<Camera3StreamInterface> stream;
1702 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1703 if (outputStreamIdx == NAME_NOT_FOUND) {
1704 CLOGE("Stream %d does not exist", streamId);
1705 return BAD_VALUE;
1706 }
1707
1708 stream = mOutputStreams.editValueAt(outputStreamIdx);
1709 stream->addBufferListener(listener);
1710
1711 return OK;
1712}
1713
Zhijun He204e3292014-07-14 17:09:23 -07001714uint32_t Camera3Device::getDeviceVersion() {
1715 ATRACE_CALL();
1716 Mutex::Autolock il(mInterfaceLock);
1717 return mDeviceVersion;
1718}
1719
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001720/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001721 * Methods called by subclasses
1722 */
1723
1724void Camera3Device::notifyStatus(bool idle) {
1725 {
1726 // Need mLock to safely update state and synchronize to current
1727 // state of methods in flight.
1728 Mutex::Autolock l(mLock);
1729 // We can get various system-idle notices from the status tracker
1730 // while starting up. Only care about them if we've actually sent
1731 // in some requests recently.
1732 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1733 return;
1734 }
1735 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1736 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001737 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001738
1739 // Skip notifying listener if we're doing some user-transparent
1740 // state changes
1741 if (mPauseStateNotify) return;
1742 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001743
1744 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001745 {
1746 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001747 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001748 }
1749 if (idle && listener != NULL) {
1750 listener->notifyIdle();
1751 }
1752}
1753
Zhijun He5d677d12016-05-29 16:52:39 -07001754status_t Camera3Device::setConsumerSurface(int streamId, sp<Surface> consumer) {
1755 ATRACE_CALL();
1756 ALOGV("%s: Camera %d: set consumer surface for stream %d", __FUNCTION__, mId, streamId);
1757 Mutex::Autolock il(mInterfaceLock);
1758 Mutex::Autolock l(mLock);
1759
1760 if (consumer == nullptr) {
1761 CLOGE("Null consumer is passed!");
1762 return BAD_VALUE;
1763 }
1764
1765 ssize_t idx = mOutputStreams.indexOfKey(streamId);
1766 if (idx == NAME_NOT_FOUND) {
1767 CLOGE("Stream %d is unknown", streamId);
1768 return idx;
1769 }
1770 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
1771 status_t res = stream->setConsumer(consumer);
1772 if (res != OK) {
1773 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1774 return res;
1775 }
1776
1777 if (!stream->isConfiguring()) {
1778 CLOGE("Stream %d was already fully configured.", streamId);
1779 return INVALID_OPERATION;
1780 }
1781
1782 res = stream->finishConfiguration(mHal3Device);
1783 if (res != OK) {
1784 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1785 stream->getId(), strerror(-res), res);
1786 return res;
1787 }
1788
1789 return OK;
1790}
1791
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001792/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001793 * Camera3Device private methods
1794 */
1795
1796sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1797 const CameraMetadata &request) {
1798 ATRACE_CALL();
1799 status_t res;
1800
1801 sp<CaptureRequest> newRequest = new CaptureRequest;
1802 newRequest->mSettings = request;
1803
1804 camera_metadata_entry_t inputStreams =
1805 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1806 if (inputStreams.count > 0) {
1807 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001808 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001809 CLOGE("Request references unknown input stream %d",
1810 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001811 return NULL;
1812 }
1813 // Lazy completion of stream configuration (allocation/registration)
1814 // on first use
1815 if (mInputStream->isConfiguring()) {
1816 res = mInputStream->finishConfiguration(mHal3Device);
1817 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001818 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001819 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001820 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001821 return NULL;
1822 }
1823 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001824 // Check if stream is being prepared
1825 if (mInputStream->isPreparing()) {
1826 CLOGE("Request references an input stream that's being prepared!");
1827 return NULL;
1828 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001829
1830 newRequest->mInputStream = mInputStream;
1831 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1832 }
1833
1834 camera_metadata_entry_t streams =
1835 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1836 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001837 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001838 return NULL;
1839 }
1840
1841 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001842 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001843 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001844 CLOGE("Request references unknown stream %d",
1845 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001846 return NULL;
1847 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001848 sp<Camera3OutputStreamInterface> stream =
1849 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001850
Zhijun He5d677d12016-05-29 16:52:39 -07001851 // It is illegal to include a deferred consumer output stream into a request
1852 if (stream->isConsumerConfigurationDeferred()) {
1853 CLOGE("Stream %d hasn't finished configuration yet due to deferred consumer",
1854 stream->getId());
1855 return NULL;
1856 }
1857
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001858 // Lazy completion of stream configuration (allocation/registration)
1859 // on first use
1860 if (stream->isConfiguring()) {
1861 res = stream->finishConfiguration(mHal3Device);
1862 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001863 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1864 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001865 return NULL;
1866 }
1867 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001868 // Check if stream is being prepared
1869 if (stream->isPreparing()) {
1870 CLOGE("Request references an output stream that's being prepared!");
1871 return NULL;
1872 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001873
1874 newRequest->mOutputStreams.push(stream);
1875 }
1876 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001877 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001878
1879 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001880}
1881
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001882bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1883 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1884 Size size = mSupportedOpaqueInputSizes[i];
1885 if (size.width == width && size.height == height) {
1886 return true;
1887 }
1888 }
1889
1890 return false;
1891}
1892
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001893void Camera3Device::cancelStreamsConfigurationLocked() {
1894 int res = OK;
1895 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1896 res = mInputStream->cancelConfiguration();
1897 if (res != OK) {
1898 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
1899 mInputStream->getId(), strerror(-res), res);
1900 }
1901 }
1902
1903 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1904 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
1905 if (outputStream->isConfiguring()) {
1906 res = outputStream->cancelConfiguration();
1907 if (res != OK) {
1908 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
1909 outputStream->getId(), strerror(-res), res);
1910 }
1911 }
1912 }
1913
1914 // Return state to that at start of call, so that future configures
1915 // properly clean things up
1916 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
1917 mNeedConfig = true;
1918}
1919
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001920status_t Camera3Device::configureStreamsLocked() {
1921 ATRACE_CALL();
1922 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001923
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001924 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001925 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001926 return INVALID_OPERATION;
1927 }
1928
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001929 if (!mNeedConfig) {
1930 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1931 return OK;
1932 }
1933
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001934 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1935 // adding a dummy stream instead.
1936 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1937 if (mOutputStreams.size() == 0) {
1938 addDummyStreamLocked();
1939 } else {
1940 tryRemoveDummyStreamLocked();
1941 }
1942
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001943 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001944 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001945
1946 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001947 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1948 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1949 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001950 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1951
1952 Vector<camera3_stream_t*> streams;
1953 streams.setCapacity(config.num_streams);
1954
1955 if (mInputStream != NULL) {
1956 camera3_stream_t *inputStream;
1957 inputStream = mInputStream->startConfiguration();
1958 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001959 CLOGE("Can't start input stream configuration");
1960 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001961 return INVALID_OPERATION;
1962 }
1963 streams.add(inputStream);
1964 }
1965
1966 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001967
1968 // Don't configure bidi streams twice, nor add them twice to the list
1969 if (mOutputStreams[i].get() ==
1970 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1971
1972 config.num_streams--;
1973 continue;
1974 }
1975
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001976 camera3_stream_t *outputStream;
1977 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1978 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001979 CLOGE("Can't start output stream configuration");
1980 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001981 return INVALID_OPERATION;
1982 }
1983 streams.add(outputStream);
1984 }
1985
1986 config.streams = streams.editArray();
1987
1988 // Do the HAL configuration; will potentially touch stream
1989 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001990 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001991 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001992 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001993
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001994 if (res == BAD_VALUE) {
1995 // HAL rejected this set of streams as unsupported, clean up config
1996 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001997 CLOGE("Set of requested inputs/outputs not supported by HAL");
1998 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001999 return BAD_VALUE;
2000 } else if (res != OK) {
2001 // Some other kind of error from configure_streams - this is not
2002 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002003 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2004 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002005 return res;
2006 }
2007
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002008 // Finish all stream configuration immediately.
2009 // TODO: Try to relax this later back to lazy completion, which should be
2010 // faster
2011
Igor Murashkin073f8572013-05-02 14:59:28 -07002012 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002013 res = mInputStream->finishConfiguration(mHal3Device);
2014 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002015 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002016 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002017 cancelStreamsConfigurationLocked();
2018 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002019 }
2020 }
2021
2022 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002023 sp<Camera3OutputStreamInterface> outputStream =
2024 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002025 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002026 res = outputStream->finishConfiguration(mHal3Device);
2027 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002028 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002029 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002030 cancelStreamsConfigurationLocked();
2031 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002032 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002033 }
2034 }
2035
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002036 // Request thread needs to know to avoid using repeat-last-settings protocol
2037 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002038 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002039
Zhijun He7ee4c072016-07-25 13:52:28 -07002040 // Boost priority of request thread to SCHED_FIFO.
2041 pid_t requestThreadTid = mRequestThread->getTid();
2042 res = requestPriority(getpid(), requestThreadTid,
2043 kRequestThreadPriority, /*asynchronous*/ false);
2044 if (res != OK) {
2045 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2046 strerror(-res), res);
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002047 } else {
Zhijun He7ee4c072016-07-25 13:52:28 -07002048 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002049 }
2050
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002051 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002052
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002053 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002054
Ruben Brunk183f0562015-08-12 12:55:02 -07002055 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2056 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002057
2058 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
2059
Zhijun He0a210512014-07-24 13:45:15 -07002060 // tear down the deleted streams after configure streams.
2061 mDeletedStreams.clear();
2062
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002063 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002064}
2065
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002066status_t Camera3Device::addDummyStreamLocked() {
2067 ATRACE_CALL();
2068 status_t res;
2069
2070 if (mDummyStreamId != NO_STREAM) {
2071 // Should never be adding a second dummy stream when one is already
2072 // active
2073 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
2074 __FUNCTION__, mId);
2075 return INVALID_OPERATION;
2076 }
2077
2078 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
2079
2080 sp<Camera3OutputStreamInterface> dummyStream =
2081 new Camera3DummyStream(mNextStreamId);
2082
2083 res = mOutputStreams.add(mNextStreamId, dummyStream);
2084 if (res < 0) {
2085 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2086 return res;
2087 }
2088
2089 mDummyStreamId = mNextStreamId;
2090 mNextStreamId++;
2091
2092 return OK;
2093}
2094
2095status_t Camera3Device::tryRemoveDummyStreamLocked() {
2096 ATRACE_CALL();
2097 status_t res;
2098
2099 if (mDummyStreamId == NO_STREAM) return OK;
2100 if (mOutputStreams.size() == 1) return OK;
2101
2102 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
2103
2104 // Ok, have a dummy stream and there's at least one other output stream,
2105 // so remove the dummy
2106
2107 sp<Camera3StreamInterface> deletedStream;
2108 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2109 if (outputStreamIdx == NAME_NOT_FOUND) {
2110 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2111 return INVALID_OPERATION;
2112 }
2113
2114 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2115 mOutputStreams.removeItemsAt(outputStreamIdx);
2116
2117 // Free up the stream endpoint so that it can be used by some other stream
2118 res = deletedStream->disconnect();
2119 if (res != OK) {
2120 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2121 // fall through since we want to still list the stream as deleted.
2122 }
2123 mDeletedStreams.add(deletedStream);
2124 mDummyStreamId = NO_STREAM;
2125
2126 return res;
2127}
2128
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002129void Camera3Device::setErrorState(const char *fmt, ...) {
2130 Mutex::Autolock l(mLock);
2131 va_list args;
2132 va_start(args, fmt);
2133
2134 setErrorStateLockedV(fmt, args);
2135
2136 va_end(args);
2137}
2138
2139void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2140 Mutex::Autolock l(mLock);
2141 setErrorStateLockedV(fmt, args);
2142}
2143
2144void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2145 va_list args;
2146 va_start(args, fmt);
2147
2148 setErrorStateLockedV(fmt, args);
2149
2150 va_end(args);
2151}
2152
2153void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002154 // Print out all error messages to log
2155 String8 errorCause = String8::formatV(fmt, args);
2156 ALOGE("Camera %d: %s", mId, errorCause.string());
2157
2158 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002159 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002160
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002161 mErrorCause = errorCause;
2162
2163 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002164 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002165
2166 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002167 sp<NotificationListener> listener = mListener.promote();
2168 if (listener != NULL) {
2169 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002170 CaptureResultExtras());
2171 }
2172
2173 // Save stack trace. View by dumping it later.
2174 CameraTraces::saveTrace();
2175 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002176}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002177
2178/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002179 * In-flight request management
2180 */
2181
Jianing Weicb0652e2014-03-12 18:29:36 -07002182status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002183 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2184 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002185 ATRACE_CALL();
2186 Mutex::Autolock l(mInFlightLock);
2187
2188 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002189 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2190 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002191 if (res < 0) return res;
2192
2193 return OK;
2194}
2195
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002196void Camera3Device::returnOutputBuffers(
2197 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2198 nsecs_t timestamp) {
2199 for (size_t i = 0; i < numBuffers; i++)
2200 {
2201 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2202 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2203 // Note: stream may be deallocated at this point, if this buffer was
2204 // the last reference to it.
2205 if (res != OK) {
2206 ALOGE("Can't return buffer to its stream: %s (%d)",
2207 strerror(-res), res);
2208 }
2209 }
2210}
2211
2212
2213void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2214
2215 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2216 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2217
2218 nsecs_t sensorTimestamp = request.sensorTimestamp;
2219 nsecs_t shutterTimestamp = request.shutterTimestamp;
2220
2221 // Check if it's okay to remove the request from InFlightMap:
2222 // In the case of a successful request:
2223 // all input and output buffers, all result metadata, shutter callback
2224 // arrived.
2225 // In the case of a unsuccessful request:
2226 // all input and output buffers arrived.
2227 if (request.numBuffersLeft == 0 &&
2228 (request.requestStatus != OK ||
2229 (request.haveResultMetadata && shutterTimestamp != 0))) {
2230 ATRACE_ASYNC_END("frame capture", frameNumber);
2231
2232 // Sanity check - if sensor timestamp matches shutter timestamp
2233 if (request.requestStatus == OK &&
2234 sensorTimestamp != shutterTimestamp) {
2235 SET_ERR("sensor timestamp (%" PRId64
2236 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2237 sensorTimestamp, frameNumber, shutterTimestamp);
2238 }
2239
2240 // for an unsuccessful request, it may have pending output buffers to
2241 // return.
2242 assert(request.requestStatus != OK ||
2243 request.pendingOutputBuffers.size() == 0);
2244 returnOutputBuffers(request.pendingOutputBuffers.array(),
2245 request.pendingOutputBuffers.size(), 0);
2246
2247 mInFlightMap.removeItemsAt(idx, 1);
2248
2249 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2250 }
2251
2252 // Sanity check - if we have too many in-flight frames, something has
2253 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002254 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002255 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002256 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2257 kInFlightWarnLimitHighSpeed) {
2258 CLOGE("In-flight list too large for high speed configuration: %zu",
2259 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002260 }
2261}
2262
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002263void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2264 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2265 if (result == nullptr) return;
2266
2267 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2268 (int32_t*)&frameNumber, 1) != OK) {
2269 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2270 return;
2271 }
2272
2273 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2274 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2275 return;
2276 }
2277
2278 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2279
2280 // Valid result, insert into queue
2281 List<CaptureResult>::iterator queuedResult =
2282 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2283 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2284 ", burstId = %" PRId32, __FUNCTION__,
2285 queuedResult->mResultExtras.requestId,
2286 queuedResult->mResultExtras.frameNumber,
2287 queuedResult->mResultExtras.burstId);
2288
2289 mResultSignal.signal();
2290}
2291
2292
2293void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2294 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2295 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2296 Mutex::Autolock l(mOutputLock);
2297
2298 CaptureResult captureResult;
2299 captureResult.mResultExtras = resultExtras;
2300 captureResult.mMetadata = partialResult;
2301
2302 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2303}
2304
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002305
2306void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2307 CaptureResultExtras &resultExtras,
2308 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002309 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002310 bool reprocess,
2311 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002312 if (pendingMetadata.isEmpty())
2313 return;
2314
2315 Mutex::Autolock l(mOutputLock);
2316
2317 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002318 if (reprocess) {
2319 if (frameNumber < mNextReprocessResultFrameNumber) {
2320 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002321 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002322 frameNumber, mNextReprocessResultFrameNumber);
2323 return;
2324 }
2325 mNextReprocessResultFrameNumber = frameNumber + 1;
2326 } else {
2327 if (frameNumber < mNextResultFrameNumber) {
2328 SET_ERR("Out-of-order capture result metadata submitted! "
2329 "(got frame number %d, expecting %d)",
2330 frameNumber, mNextResultFrameNumber);
2331 return;
2332 }
2333 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002334 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002335
2336 CaptureResult captureResult;
2337 captureResult.mResultExtras = resultExtras;
2338 captureResult.mMetadata = pendingMetadata;
2339
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002340 // Append any previous partials to form a complete result
2341 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2342 captureResult.mMetadata.append(collectedPartialResult);
2343 }
2344
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07002345 // Derive some new keys for backward compaibility
2346 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
2347 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
2348 int32_t defaultBoost[1] = {100};
2349 captureResult.mMetadata.update(
2350 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
2351 defaultBoost, 1);
2352 }
2353
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002354 captureResult.mMetadata.sort();
2355
2356 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002357 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2358 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002359 SET_ERR("No timestamp provided by HAL for frame %d!",
2360 frameNumber);
2361 return;
2362 }
2363
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002364 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2365 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2366
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002367 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002368}
2369
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002370/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002371 * Camera HAL device callback methods
2372 */
2373
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002374void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002375 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002376
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002377 status_t res;
2378
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002379 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002380 if (result->result == NULL && result->num_output_buffers == 0 &&
2381 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002382 SET_ERR("No result data provided by HAL for frame %d",
2383 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002384 return;
2385 }
Zhijun He204e3292014-07-14 17:09:23 -07002386
2387 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2388 // partial_result to 1 when metadata is included in this result.
2389 if (!mUsePartialResult &&
2390 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2391 result->result != NULL &&
2392 result->partial_result != 1) {
2393 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2394 " if partial result is not supported",
2395 frameNumber, result->partial_result);
2396 return;
2397 }
2398
2399 bool isPartialResult = false;
2400 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002401 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002402 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002403
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002404 // Get shutter timestamp and resultExtras from list of in-flight requests,
2405 // where it was added by the shutter notification for this frame. If the
2406 // shutter timestamp isn't received yet, append the output buffers to the
2407 // in-flight request and they will be returned when the shutter timestamp
2408 // arrives. Update the in-flight status and remove the in-flight entry if
2409 // all result data and shutter timestamp have been received.
2410 nsecs_t shutterTimestamp = 0;
2411
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002412 {
2413 Mutex::Autolock l(mInFlightLock);
2414 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2415 if (idx == NAME_NOT_FOUND) {
2416 SET_ERR("Unknown frame number for capture result: %d",
2417 frameNumber);
2418 return;
2419 }
2420 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002421 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2422 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2423 ", partialResultCount = %d",
2424 __FUNCTION__, request.resultExtras.requestId,
2425 request.resultExtras.frameNumber, request.resultExtras.burstId,
2426 result->partial_result);
2427 // Always update the partial count to the latest one if it's not 0
2428 // (buffers only). When framework aggregates adjacent partial results
2429 // into one, the latest partial count will be used.
2430 if (result->partial_result != 0)
2431 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002432
2433 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002434 if (mUsePartialResult && result->result != NULL) {
2435 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2436 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2437 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2438 " the range of [1, %d] when metadata is included in the result",
2439 frameNumber, result->partial_result, mNumPartialResults);
2440 return;
2441 }
2442 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002443 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002444 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002445 }
Zhijun He204e3292014-07-14 17:09:23 -07002446 } else {
2447 camera_metadata_ro_entry_t partialResultEntry;
2448 res = find_camera_metadata_ro_entry(result->result,
2449 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2450 if (res != NAME_NOT_FOUND &&
2451 partialResultEntry.count > 0 &&
2452 partialResultEntry.data.u8[0] ==
2453 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2454 // A partial result. Flag this as such, and collect this
2455 // set of metadata into the in-flight entry.
2456 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002457 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002458 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002459 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002460 ANDROID_QUIRKS_PARTIAL_RESULT);
2461 }
2462 }
2463
2464 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002465 // Send partial capture result
2466 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2467 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002468 }
2469 }
2470
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002471 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002472 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002473
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002474 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002475 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002476 if (request.haveResultMetadata) {
2477 SET_ERR("Called multiple times with metadata for frame %d",
2478 frameNumber);
2479 return;
2480 }
Zhijun He204e3292014-07-14 17:09:23 -07002481 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002482 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002483 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002484 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002485 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002486 request.haveResultMetadata = true;
2487 }
2488
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002489 uint32_t numBuffersReturned = result->num_output_buffers;
2490 if (result->input_buffer != NULL) {
2491 if (hasInputBufferInRequest) {
2492 numBuffersReturned += 1;
2493 } else {
2494 ALOGW("%s: Input buffer should be NULL if there is no input"
2495 " buffer sent in the request",
2496 __FUNCTION__);
2497 }
2498 }
2499 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002500 if (request.numBuffersLeft < 0) {
2501 SET_ERR("Too many buffers returned for frame %d",
2502 frameNumber);
2503 return;
2504 }
2505
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002506 camera_metadata_ro_entry_t entry;
2507 res = find_camera_metadata_ro_entry(result->result,
2508 ANDROID_SENSOR_TIMESTAMP, &entry);
2509 if (res == OK && entry.count == 1) {
2510 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002511 }
2512
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002513 // If shutter event isn't received yet, append the output buffers to
2514 // the in-flight request. Otherwise, return the output buffers to
2515 // streams.
2516 if (shutterTimestamp == 0) {
2517 request.pendingOutputBuffers.appendArray(result->output_buffers,
2518 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002519 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002520 returnOutputBuffers(result->output_buffers,
2521 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002522 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002523
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002524 if (result->result != NULL && !isPartialResult) {
2525 if (shutterTimestamp == 0) {
2526 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002527 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002528 } else {
2529 CameraMetadata metadata;
2530 metadata = result->result;
2531 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002532 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2533 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002534 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002535 }
2536
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002537 removeInFlightRequestIfReadyLocked(idx);
2538 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002539
Zhijun Hef0d962a2014-06-30 10:24:11 -07002540 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002541 if (hasInputBufferInRequest) {
2542 Camera3Stream *stream =
2543 Camera3Stream::cast(result->input_buffer->stream);
2544 res = stream->returnInputBuffer(*(result->input_buffer));
2545 // Note: stream may be deallocated at this point, if this buffer was the
2546 // last reference to it.
2547 if (res != OK) {
2548 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2549 " its stream:%s (%d)", __FUNCTION__,
2550 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002551 }
2552 } else {
2553 ALOGW("%s: Input buffer should be NULL if there is no input"
2554 " buffer sent in the request, skipping input buffer return.",
2555 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002556 }
2557 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002558}
2559
2560void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002561 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002562 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002563 {
2564 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002565 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002566 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002567
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002568 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002569 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002570 return;
2571 }
2572
2573 switch (msg->type) {
2574 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002575 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002576 break;
2577 }
2578 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002579 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002580 break;
2581 }
2582 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002583 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002584 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002585 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002586}
2587
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002588void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002589 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002590
2591 // Map camera HAL error codes to ICameraDeviceCallback error codes
2592 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002593 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002594 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002595 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002596 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002597 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002598 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002599 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002600 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002601 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002602 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002603 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002604 };
2605
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002606 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002607 ((msg.error_code >= 0) &&
2608 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2609 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002610 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002611
2612 int streamId = 0;
2613 if (msg.error_stream != NULL) {
2614 Camera3Stream *stream =
2615 Camera3Stream::cast(msg.error_stream);
2616 streamId = stream->getId();
2617 }
2618 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2619 mId, __FUNCTION__, msg.frame_number,
2620 streamId, msg.error_code);
2621
2622 CaptureResultExtras resultExtras;
2623 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002624 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002625 // SET_ERR calls notifyError
2626 SET_ERR("Camera HAL reported serious device error");
2627 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002628 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2629 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2630 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002631 {
2632 Mutex::Autolock l(mInFlightLock);
2633 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2634 if (idx >= 0) {
2635 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2636 r.requestStatus = msg.error_code;
2637 resultExtras = r.resultExtras;
2638 } else {
2639 resultExtras.frameNumber = msg.frame_number;
2640 ALOGE("Camera %d: %s: cannot find in-flight request on "
2641 "frame %" PRId64 " error", mId, __FUNCTION__,
2642 resultExtras.frameNumber);
2643 }
2644 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002645 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002646 if (listener != NULL) {
2647 listener->notifyError(errorCode, resultExtras);
2648 } else {
2649 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2650 }
2651 break;
2652 default:
2653 // SET_ERR calls notifyError
2654 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2655 break;
2656 }
2657}
2658
2659void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002660 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002661 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002662
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002663 // Set timestamp for the request in the in-flight tracking
2664 // and get the request ID to send upstream
2665 {
2666 Mutex::Autolock l(mInFlightLock);
2667 idx = mInFlightMap.indexOfKey(msg.frame_number);
2668 if (idx >= 0) {
2669 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002670
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002671 // Verify ordering of shutter notifications
2672 {
2673 Mutex::Autolock l(mOutputLock);
2674 // TODO: need to track errors for tighter bounds on expected frame number.
2675 if (r.hasInputBuffer) {
2676 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2677 SET_ERR("Shutter notification out-of-order. Expected "
2678 "notification for frame %d, got frame %d",
2679 mNextReprocessShutterFrameNumber, msg.frame_number);
2680 return;
2681 }
2682 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2683 } else {
2684 if (msg.frame_number < mNextShutterFrameNumber) {
2685 SET_ERR("Shutter notification out-of-order. Expected "
2686 "notification for frame %d, got frame %d",
2687 mNextShutterFrameNumber, msg.frame_number);
2688 return;
2689 }
2690 mNextShutterFrameNumber = msg.frame_number + 1;
2691 }
2692 }
2693
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002694 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2695 mId, __FUNCTION__,
2696 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2697 // Call listener, if any
2698 if (listener != NULL) {
2699 listener->notifyShutter(r.resultExtras, msg.timestamp);
2700 }
2701
2702 r.shutterTimestamp = msg.timestamp;
2703
2704 // send pending result and buffers
2705 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002706 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002707 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002708 returnOutputBuffers(r.pendingOutputBuffers.array(),
2709 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2710 r.pendingOutputBuffers.clear();
2711
2712 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002713 }
2714 }
2715 if (idx < 0) {
2716 SET_ERR("Shutter notification for non-existent frame number %d",
2717 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002718 }
2719}
2720
2721
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002722CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002723 ALOGV("%s", __FUNCTION__);
2724
Igor Murashkin1e479c02013-09-06 16:55:14 -07002725 CameraMetadata retVal;
2726
2727 if (mRequestThread != NULL) {
2728 retVal = mRequestThread->getLatestRequest();
2729 }
2730
Igor Murashkin1e479c02013-09-06 16:55:14 -07002731 return retVal;
2732}
2733
Jianing Weicb0652e2014-03-12 18:29:36 -07002734
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002735void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
2736 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
2737 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
2738}
2739
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002740/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002741 * RequestThread inner class methods
2742 */
2743
2744Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002745 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002746 camera3_device_t *hal3Device,
2747 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002748 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002749 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002750 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002751 mHal3Device(hal3Device),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002752 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002753 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002754 mReconfigured(false),
2755 mDoPause(false),
2756 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002757 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002758 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002759 mCurrentAfTriggerId(0),
2760 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002761 mRepeatingLastFrameNumber(
2762 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002763 mAeLockAvailable(aeLockAvailable),
2764 mPrepareVideoStream(false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002765 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002766}
2767
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002768void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002769 wp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002770 Mutex::Autolock l(mRequestLock);
2771 mListener = listener;
2772}
2773
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002774void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002775 Mutex::Autolock l(mRequestLock);
2776 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002777 // Prepare video stream for high speed recording.
2778 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002779}
2780
Jianing Wei90e59c92014-03-12 18:29:36 -07002781status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002782 List<sp<CaptureRequest> > &requests,
2783 /*out*/
2784 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002785 Mutex::Autolock l(mRequestLock);
2786 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2787 ++it) {
2788 mRequestQueue.push_back(*it);
2789 }
2790
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002791 if (lastFrameNumber != NULL) {
2792 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2793 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2794 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2795 *lastFrameNumber);
2796 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002797
Jianing Wei90e59c92014-03-12 18:29:36 -07002798 unpauseForNewRequests();
2799
2800 return OK;
2801}
2802
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002803
2804status_t Camera3Device::RequestThread::queueTrigger(
2805 RequestTrigger trigger[],
2806 size_t count) {
2807
2808 Mutex::Autolock l(mTriggerMutex);
2809 status_t ret;
2810
2811 for (size_t i = 0; i < count; ++i) {
2812 ret = queueTriggerLocked(trigger[i]);
2813
2814 if (ret != OK) {
2815 return ret;
2816 }
2817 }
2818
2819 return OK;
2820}
2821
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002822int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2823 sp<Camera3Device> d = device.promote();
2824 if (d != NULL) return d->mId;
2825 return 0;
2826}
2827
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002828status_t Camera3Device::RequestThread::queueTriggerLocked(
2829 RequestTrigger trigger) {
2830
2831 uint32_t tag = trigger.metadataTag;
2832 ssize_t index = mTriggerMap.indexOfKey(tag);
2833
2834 switch (trigger.getTagType()) {
2835 case TYPE_BYTE:
2836 // fall-through
2837 case TYPE_INT32:
2838 break;
2839 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002840 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2841 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002842 return INVALID_OPERATION;
2843 }
2844
2845 /**
2846 * Collect only the latest trigger, since we only have 1 field
2847 * in the request settings per trigger tag, and can't send more than 1
2848 * trigger per request.
2849 */
2850 if (index != NAME_NOT_FOUND) {
2851 mTriggerMap.editValueAt(index) = trigger;
2852 } else {
2853 mTriggerMap.add(tag, trigger);
2854 }
2855
2856 return OK;
2857}
2858
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002859status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002860 const RequestList &requests,
2861 /*out*/
2862 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002863 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002864 if (lastFrameNumber != NULL) {
2865 *lastFrameNumber = mRepeatingLastFrameNumber;
2866 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002867 mRepeatingRequests.clear();
2868 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2869 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002870
2871 unpauseForNewRequests();
2872
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002873 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002874 return OK;
2875}
2876
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002877bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2878 if (mRepeatingRequests.empty()) {
2879 return false;
2880 }
2881 int32_t requestId = requestIn->mResultExtras.requestId;
2882 const RequestList &repeatRequests = mRepeatingRequests;
2883 // All repeating requests are guaranteed to have same id so only check first quest
2884 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2885 return (firstRequest->mResultExtras.requestId == requestId);
2886}
2887
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002888status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002889 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002890 return clearRepeatingRequestsLocked(lastFrameNumber);
2891
2892}
2893
2894status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002895 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002896 if (lastFrameNumber != NULL) {
2897 *lastFrameNumber = mRepeatingLastFrameNumber;
2898 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002899 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002900 return OK;
2901}
2902
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002903status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002904 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002905 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002906 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002907
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002908 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002909
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002910 // Send errors for all requests pending in the request queue, including
2911 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002912 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002913 if (listener != NULL) {
2914 for (RequestList::iterator it = mRequestQueue.begin();
2915 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002916 // Abort the input buffers for reprocess requests.
2917 if ((*it)->mInputStream != NULL) {
2918 camera3_stream_buffer_t inputBuffer;
2919 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2920 if (res != OK) {
2921 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2922 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2923 } else {
2924 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2925 if (res != OK) {
2926 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2927 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2928 }
2929 }
2930 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002931 // Set the frame number this request would have had, if it
2932 // had been submitted; this frame number will not be reused.
2933 // The requestId and burstId fields were set when the request was
2934 // submitted originally (in convertMetadataListToRequestListLocked)
2935 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002936 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002937 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002938 }
2939 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002940 mRequestQueue.clear();
2941 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002942 if (lastFrameNumber != NULL) {
2943 *lastFrameNumber = mRepeatingLastFrameNumber;
2944 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002945 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002946 return OK;
2947}
2948
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002949status_t Camera3Device::RequestThread::flush() {
2950 ATRACE_CALL();
2951 Mutex::Autolock l(mFlushLock);
2952
2953 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2954 return mHal3Device->ops->flush(mHal3Device);
2955 }
2956
2957 return -ENOTSUP;
2958}
2959
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002960void Camera3Device::RequestThread::setPaused(bool paused) {
2961 Mutex::Autolock l(mPauseLock);
2962 mDoPause = paused;
2963 mDoPauseSignal.signal();
2964}
2965
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002966status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2967 int32_t requestId, nsecs_t timeout) {
2968 Mutex::Autolock l(mLatestRequestMutex);
2969 status_t res;
2970 while (mLatestRequestId != requestId) {
2971 nsecs_t startTime = systemTime();
2972
2973 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2974 if (res != OK) return res;
2975
2976 timeout -= (systemTime() - startTime);
2977 }
2978
2979 return OK;
2980}
2981
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002982void Camera3Device::RequestThread::requestExit() {
2983 // Call parent to set up shutdown
2984 Thread::requestExit();
2985 // The exit from any possible waits
2986 mDoPauseSignal.signal();
2987 mRequestSignal.signal();
2988}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002989
Chien-Yu Chend196d612015-06-22 19:49:01 -07002990
2991/**
2992 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2993 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2994 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2995 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2996 * request.
2997 */
2998void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2999 request->mAeTriggerCancelOverride.applyAeLock = false;
3000 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
3001
3002 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
3003 return;
3004 }
3005
3006 camera_metadata_entry_t aePrecaptureTrigger =
3007 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3008 if (aePrecaptureTrigger.count > 0 &&
3009 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
3010 // Always override CANCEL to IDLE
3011 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
3012 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
3013 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
3014 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
3015 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3016
3017 if (mAeLockAvailable == true) {
3018 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3019 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3020 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3021 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3022 request->mAeTriggerCancelOverride.applyAeLock = true;
3023 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3024 }
3025 }
3026 }
3027}
3028
3029/**
3030 * Override result metadata for cancelling AE precapture trigger applied in
3031 * handleAePrecaptureCancelRequest().
3032 */
3033void Camera3Device::overrideResultForPrecaptureCancel(
3034 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3035 if (aeTriggerCancelOverride.applyAeLock) {
3036 // Only devices <= v3.2 should have this override
3037 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3038 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3039 }
3040
3041 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3042 // Only devices <= v3.2 should have this override
3043 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3044 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3045 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3046 }
3047}
3048
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003049void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003050 bool surfaceAbandoned = false;
3051 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003052 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003053 {
3054 Mutex::Autolock l(mRequestLock);
3055 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3056 // repeating requests.
3057 for (const auto& request : mRepeatingRequests) {
3058 for (const auto& s : request->mOutputStreams) {
3059 if (s->isAbandoned()) {
3060 surfaceAbandoned = true;
3061 clearRepeatingRequestsLocked(&lastFrameNumber);
3062 break;
3063 }
3064 }
3065 if (surfaceAbandoned) {
3066 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003067 }
3068 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003069 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003070 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003071
3072 if (listener != NULL && surfaceAbandoned) {
3073 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003074 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003075}
3076
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003077bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003078 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003079 status_t res;
3080
3081 // Handle paused state.
3082 if (waitIfPaused()) {
3083 return true;
3084 }
3085
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003086 // Wait for the next batch of requests.
3087 waitForNextRequestBatch();
3088 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003089 return true;
3090 }
3091
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003092 // Get the latest request ID, if any
3093 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003094 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003095 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003096 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003097 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003098 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003099 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3100 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003101 }
3102
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003103 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003104 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003105 if (res == TIMED_OUT) {
3106 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003107 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003108 // Check if any stream is abandoned.
3109 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003110 return true;
3111 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003112 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003113 return false;
3114 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003115
Zhijun Hecc27e112013-10-03 16:12:43 -07003116 // Inform waitUntilRequestProcessed thread of a new request ID
3117 {
3118 Mutex::Autolock al(mLatestRequestMutex);
3119
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003120 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003121 mLatestRequestSignal.signal();
3122 }
3123
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003124 // Submit a batch of requests to HAL.
3125 // Use flush lock only when submitting multilple requests in a batch.
3126 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3127 // which may take a long time to finish so synchronizing flush() and
3128 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3129 // For now, only synchronize for high speed recording and we should figure something out for
3130 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003131 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003132
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003133 if (useFlushLock) {
3134 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003135 }
3136
Zhijun Hef0645c12016-08-02 00:58:11 -07003137 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003138 mNextRequests.size());
3139 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003140 // Submit request and block until ready for next one
3141 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3142 ATRACE_BEGIN("camera3->process_capture_request");
3143 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3144 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003145
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003146 if (res != OK) {
3147 // Should only get a failure here for malformed requests or device-level
3148 // errors, so consider all errors fatal. Bad metadata failures should
3149 // come through notify.
3150 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3151 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3152 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003153 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003154 if (useFlushLock) {
3155 mFlushLock.unlock();
3156 }
3157 return false;
3158 }
3159
3160 // Mark that the request has be submitted successfully.
3161 nextRequest.submitted = true;
3162
3163 // Update the latest request sent to HAL
3164 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3165 Mutex::Autolock al(mLatestRequestMutex);
3166
3167 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3168 mLatestRequest.acquire(cloned);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003169
3170 sp<Camera3Device> parent = mParent.promote();
3171 if (parent != NULL) {
3172 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
3173 0, mLatestRequest);
3174 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003175 }
3176
3177 if (nextRequest.halRequest.settings != NULL) {
3178 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3179 }
3180
3181 // Remove any previously queued triggers (after unlock)
3182 res = removeTriggers(mPrevRequest);
3183 if (res != OK) {
3184 SET_ERR("RequestThread: Unable to remove triggers "
3185 "(capture request %d, HAL device: %s (%d)",
3186 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003187 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003188 if (useFlushLock) {
3189 mFlushLock.unlock();
3190 }
3191 return false;
3192 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003193 }
3194
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003195 if (useFlushLock) {
3196 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003197 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003198
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003199 // Unset as current request
3200 {
3201 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003202 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003203 }
3204
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003205 return true;
3206}
3207
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003208status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003209 ATRACE_CALL();
3210
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003211 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003212 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3213 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3214 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3215
3216 // Prepare a request to HAL
3217 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3218
3219 // Insert any queued triggers (before metadata is locked)
3220 status_t res = insertTriggers(captureRequest);
3221
3222 if (res < 0) {
3223 SET_ERR("RequestThread: Unable to insert triggers "
3224 "(capture request %d, HAL device: %s (%d)",
3225 halRequest->frame_number, strerror(-res), res);
3226 return INVALID_OPERATION;
3227 }
3228 int triggerCount = res;
3229 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3230 mPrevTriggers = triggerCount;
3231
3232 // If the request is the same as last, or we had triggers last time
3233 if (mPrevRequest != captureRequest || triggersMixedIn) {
3234 /**
3235 * HAL workaround:
3236 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3237 */
3238 res = addDummyTriggerIds(captureRequest);
3239 if (res != OK) {
3240 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3241 "(capture request %d, HAL device: %s (%d)",
3242 halRequest->frame_number, strerror(-res), res);
3243 return INVALID_OPERATION;
3244 }
3245
3246 /**
3247 * The request should be presorted so accesses in HAL
3248 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3249 */
3250 captureRequest->mSettings.sort();
3251 halRequest->settings = captureRequest->mSettings.getAndLock();
3252 mPrevRequest = captureRequest;
3253 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3254
3255 IF_ALOGV() {
3256 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3257 find_camera_metadata_ro_entry(
3258 halRequest->settings,
3259 ANDROID_CONTROL_AF_TRIGGER,
3260 &e
3261 );
3262 if (e.count > 0) {
3263 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3264 __FUNCTION__,
3265 halRequest->frame_number,
3266 e.data.u8[0]);
3267 }
3268 }
3269 } else {
3270 // leave request.settings NULL to indicate 'reuse latest given'
3271 ALOGVV("%s: Request settings are REUSED",
3272 __FUNCTION__);
3273 }
3274
3275 uint32_t totalNumBuffers = 0;
3276
3277 // Fill in buffers
3278 if (captureRequest->mInputStream != NULL) {
3279 halRequest->input_buffer = &captureRequest->mInputBuffer;
3280 totalNumBuffers += 1;
3281 } else {
3282 halRequest->input_buffer = NULL;
3283 }
3284
3285 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3286 captureRequest->mOutputStreams.size());
3287 halRequest->output_buffers = outputBuffers->array();
3288 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003289 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(i);
3290
3291 // Prepare video buffers for high speed recording on the first video request.
3292 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3293 // Only try to prepare video stream on the first video request.
3294 mPrepareVideoStream = false;
3295
3296 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
3297 while (res == NOT_ENOUGH_DATA) {
3298 res = outputStream->prepareNextBuffer();
3299 }
3300 if (res != OK) {
3301 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3302 __FUNCTION__, strerror(-res), res);
3303 outputStream->cancelPrepare();
3304 }
3305 }
3306
3307 res = outputStream->getBuffer(&outputBuffers->editItemAt(i));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003308 if (res != OK) {
3309 // Can't get output buffer from gralloc queue - this could be due to
3310 // abandoned queue or other consumer misbehavior, so not a fatal
3311 // error
3312 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3313 " %s (%d)", strerror(-res), res);
3314
3315 return TIMED_OUT;
3316 }
3317 halRequest->num_output_buffers++;
3318 }
3319 totalNumBuffers += halRequest->num_output_buffers;
3320
3321 // Log request in the in-flight queue
3322 sp<Camera3Device> parent = mParent.promote();
3323 if (parent == NULL) {
3324 // Should not happen, and nowhere to send errors to, so just log it
3325 CLOGE("RequestThread: Parent is gone");
3326 return INVALID_OPERATION;
3327 }
3328 res = parent->registerInFlight(halRequest->frame_number,
3329 totalNumBuffers, captureRequest->mResultExtras,
3330 /*hasInput*/halRequest->input_buffer != NULL,
3331 captureRequest->mAeTriggerCancelOverride);
3332 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3333 ", burstId = %" PRId32 ".",
3334 __FUNCTION__,
3335 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3336 captureRequest->mResultExtras.burstId);
3337 if (res != OK) {
3338 SET_ERR("RequestThread: Unable to register new in-flight request:"
3339 " %s (%d)", strerror(-res), res);
3340 return INVALID_OPERATION;
3341 }
3342 }
3343
3344 return OK;
3345}
3346
Igor Murashkin1e479c02013-09-06 16:55:14 -07003347CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3348 Mutex::Autolock al(mLatestRequestMutex);
3349
3350 ALOGV("RequestThread::%s", __FUNCTION__);
3351
3352 return mLatestRequest;
3353}
3354
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003355bool Camera3Device::RequestThread::isStreamPending(
3356 sp<Camera3StreamInterface>& stream) {
3357 Mutex::Autolock l(mRequestLock);
3358
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003359 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003360 if (!nextRequest.submitted) {
3361 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3362 if (stream == s) return true;
3363 }
3364 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003365 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003366 }
3367
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003368 for (const auto& request : mRequestQueue) {
3369 for (const auto& s : request->mOutputStreams) {
3370 if (stream == s) return true;
3371 }
3372 if (stream == request->mInputStream) return true;
3373 }
3374
3375 for (const auto& request : mRepeatingRequests) {
3376 for (const auto& s : request->mOutputStreams) {
3377 if (stream == s) return true;
3378 }
3379 if (stream == request->mInputStream) return true;
3380 }
3381
3382 return false;
3383}
Jianing Weicb0652e2014-03-12 18:29:36 -07003384
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003385void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3386 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003387 return;
3388 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003389
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003390 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003391 // Skip the ones that have been submitted successfully.
3392 if (nextRequest.submitted) {
3393 continue;
3394 }
3395
3396 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3397 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3398 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3399
3400 if (halRequest->settings != NULL) {
3401 captureRequest->mSettings.unlock(halRequest->settings);
3402 }
3403
3404 if (captureRequest->mInputStream != NULL) {
3405 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3406 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3407 }
3408
3409 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3410 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3411 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3412 }
3413
3414 if (sendRequestError) {
3415 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003416 sp<NotificationListener> listener = mListener.promote();
3417 if (listener != NULL) {
3418 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003419 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003420 captureRequest->mResultExtras);
3421 }
3422 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003423 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003424
3425 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003426 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003427}
3428
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003429void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003430 // Optimized a bit for the simple steady-state case (single repeating
3431 // request), to avoid putting that request in the queue temporarily.
3432 Mutex::Autolock l(mRequestLock);
3433
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003434 assert(mNextRequests.empty());
3435
3436 NextRequest nextRequest;
3437 nextRequest.captureRequest = waitForNextRequestLocked();
3438 if (nextRequest.captureRequest == nullptr) {
3439 return;
3440 }
3441
3442 nextRequest.halRequest = camera3_capture_request_t();
3443 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003444 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003445
3446 // Wait for additional requests
3447 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3448
3449 for (size_t i = 1; i < batchSize; i++) {
3450 NextRequest additionalRequest;
3451 additionalRequest.captureRequest = waitForNextRequestLocked();
3452 if (additionalRequest.captureRequest == nullptr) {
3453 break;
3454 }
3455
3456 additionalRequest.halRequest = camera3_capture_request_t();
3457 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003458 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003459 }
3460
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003461 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003462 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003463 mNextRequests.size(), batchSize);
3464 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003465 }
3466
3467 return;
3468}
3469
3470sp<Camera3Device::CaptureRequest>
3471 Camera3Device::RequestThread::waitForNextRequestLocked() {
3472 status_t res;
3473 sp<CaptureRequest> nextRequest;
3474
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003475 while (mRequestQueue.empty()) {
3476 if (!mRepeatingRequests.empty()) {
3477 // Always atomically enqueue all requests in a repeating request
3478 // list. Guarantees a complete in-sequence set of captures to
3479 // application.
3480 const RequestList &requests = mRepeatingRequests;
3481 RequestList::const_iterator firstRequest =
3482 requests.begin();
3483 nextRequest = *firstRequest;
3484 mRequestQueue.insert(mRequestQueue.end(),
3485 ++firstRequest,
3486 requests.end());
3487 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003488
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003489 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003490
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003491 break;
3492 }
3493
3494 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3495
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003496 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3497 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003498 Mutex::Autolock pl(mPauseLock);
3499 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003500 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003501 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003502 // Let the tracker know
3503 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3504 if (statusTracker != 0) {
3505 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3506 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003507 }
3508 // Stop waiting for now and let thread management happen
3509 return NULL;
3510 }
3511 }
3512
3513 if (nextRequest == NULL) {
3514 // Don't have a repeating request already in hand, so queue
3515 // must have an entry now.
3516 RequestList::iterator firstRequest =
3517 mRequestQueue.begin();
3518 nextRequest = *firstRequest;
3519 mRequestQueue.erase(firstRequest);
3520 }
3521
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003522 // In case we've been unpaused by setPaused clearing mDoPause, need to
3523 // update internal pause state (capture/setRepeatingRequest unpause
3524 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003525 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003526 if (mPaused) {
3527 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3528 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3529 if (statusTracker != 0) {
3530 statusTracker->markComponentActive(mStatusId);
3531 }
3532 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003533 mPaused = false;
3534
3535 // Check if we've reconfigured since last time, and reset the preview
3536 // request if so. Can't use 'NULL request == repeat' across configure calls.
3537 if (mReconfigured) {
3538 mPrevRequest.clear();
3539 mReconfigured = false;
3540 }
3541
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003542 if (nextRequest != NULL) {
3543 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003544 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3545 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003546
3547 // Since RequestThread::clear() removes buffers from the input stream,
3548 // get the right buffer here before unlocking mRequestLock
3549 if (nextRequest->mInputStream != NULL) {
3550 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3551 if (res != OK) {
3552 // Can't get input buffer from gralloc queue - this could be due to
3553 // disconnected queue or other producer misbehavior, so not a fatal
3554 // error
3555 ALOGE("%s: Can't get input buffer, skipping request:"
3556 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003557
3558 sp<NotificationListener> listener = mListener.promote();
3559 if (listener != NULL) {
3560 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003561 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003562 nextRequest->mResultExtras);
3563 }
3564 return NULL;
3565 }
3566 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003567 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003568
3569 handleAePrecaptureCancelRequest(nextRequest);
3570
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003571 return nextRequest;
3572}
3573
3574bool Camera3Device::RequestThread::waitIfPaused() {
3575 status_t res;
3576 Mutex::Autolock l(mPauseLock);
3577 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003578 if (mPaused == false) {
3579 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003580 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3581 // Let the tracker know
3582 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3583 if (statusTracker != 0) {
3584 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3585 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003586 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003587
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003588 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003589 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003590 return true;
3591 }
3592 }
3593 // We don't set mPaused to false here, because waitForNextRequest needs
3594 // to further manage the paused state in case of starvation.
3595 return false;
3596}
3597
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003598void Camera3Device::RequestThread::unpauseForNewRequests() {
3599 // With work to do, mark thread as unpaused.
3600 // If paused by request (setPaused), don't resume, to avoid
3601 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003602 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003603 Mutex::Autolock p(mPauseLock);
3604 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003605 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3606 if (mPaused) {
3607 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3608 if (statusTracker != 0) {
3609 statusTracker->markComponentActive(mStatusId);
3610 }
3611 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003612 mPaused = false;
3613 }
3614}
3615
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003616void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3617 sp<Camera3Device> parent = mParent.promote();
3618 if (parent != NULL) {
3619 va_list args;
3620 va_start(args, fmt);
3621
3622 parent->setErrorStateV(fmt, args);
3623
3624 va_end(args);
3625 }
3626}
3627
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003628status_t Camera3Device::RequestThread::insertTriggers(
3629 const sp<CaptureRequest> &request) {
3630
3631 Mutex::Autolock al(mTriggerMutex);
3632
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003633 sp<Camera3Device> parent = mParent.promote();
3634 if (parent == NULL) {
3635 CLOGE("RequestThread: Parent is gone");
3636 return DEAD_OBJECT;
3637 }
3638
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003639 CameraMetadata &metadata = request->mSettings;
3640 size_t count = mTriggerMap.size();
3641
3642 for (size_t i = 0; i < count; ++i) {
3643 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003644 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003645
3646 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3647 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3648 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003649 if (isAeTrigger) {
3650 request->mResultExtras.precaptureTriggerId = triggerId;
3651 mCurrentPreCaptureTriggerId = triggerId;
3652 } else {
3653 request->mResultExtras.afTriggerId = triggerId;
3654 mCurrentAfTriggerId = triggerId;
3655 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003656 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3657 continue; // Trigger ID tag is deprecated since device HAL 3.2
3658 }
3659 }
3660
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003661 camera_metadata_entry entry = metadata.find(tag);
3662
3663 if (entry.count > 0) {
3664 /**
3665 * Already has an entry for this trigger in the request.
3666 * Rewrite it with our requested trigger value.
3667 */
3668 RequestTrigger oldTrigger = trigger;
3669
3670 oldTrigger.entryValue = entry.data.u8[0];
3671
3672 mTriggerReplacedMap.add(tag, oldTrigger);
3673 } else {
3674 /**
3675 * More typical, no trigger entry, so we just add it
3676 */
3677 mTriggerRemovedMap.add(tag, trigger);
3678 }
3679
3680 status_t res;
3681
3682 switch (trigger.getTagType()) {
3683 case TYPE_BYTE: {
3684 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3685 res = metadata.update(tag,
3686 &entryValue,
3687 /*count*/1);
3688 break;
3689 }
3690 case TYPE_INT32:
3691 res = metadata.update(tag,
3692 &trigger.entryValue,
3693 /*count*/1);
3694 break;
3695 default:
3696 ALOGE("%s: Type not supported: 0x%x",
3697 __FUNCTION__,
3698 trigger.getTagType());
3699 return INVALID_OPERATION;
3700 }
3701
3702 if (res != OK) {
3703 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3704 ", value %d", __FUNCTION__, trigger.getTagName(),
3705 trigger.entryValue);
3706 return res;
3707 }
3708
3709 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3710 trigger.getTagName(),
3711 trigger.entryValue);
3712 }
3713
3714 mTriggerMap.clear();
3715
3716 return count;
3717}
3718
3719status_t Camera3Device::RequestThread::removeTriggers(
3720 const sp<CaptureRequest> &request) {
3721 Mutex::Autolock al(mTriggerMutex);
3722
3723 CameraMetadata &metadata = request->mSettings;
3724
3725 /**
3726 * Replace all old entries with their old values.
3727 */
3728 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3729 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3730
3731 status_t res;
3732
3733 uint32_t tag = trigger.metadataTag;
3734 switch (trigger.getTagType()) {
3735 case TYPE_BYTE: {
3736 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3737 res = metadata.update(tag,
3738 &entryValue,
3739 /*count*/1);
3740 break;
3741 }
3742 case TYPE_INT32:
3743 res = metadata.update(tag,
3744 &trigger.entryValue,
3745 /*count*/1);
3746 break;
3747 default:
3748 ALOGE("%s: Type not supported: 0x%x",
3749 __FUNCTION__,
3750 trigger.getTagType());
3751 return INVALID_OPERATION;
3752 }
3753
3754 if (res != OK) {
3755 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3756 ", trigger value %d", __FUNCTION__,
3757 trigger.getTagName(), trigger.entryValue);
3758 return res;
3759 }
3760 }
3761 mTriggerReplacedMap.clear();
3762
3763 /**
3764 * Remove all new entries.
3765 */
3766 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3767 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3768 status_t res = metadata.erase(trigger.metadataTag);
3769
3770 if (res != OK) {
3771 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3772 ", trigger value %d", __FUNCTION__,
3773 trigger.getTagName(), trigger.entryValue);
3774 return res;
3775 }
3776 }
3777 mTriggerRemovedMap.clear();
3778
3779 return OK;
3780}
3781
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003782status_t Camera3Device::RequestThread::addDummyTriggerIds(
3783 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003784 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003785 static const int32_t dummyTriggerId = 1;
3786 status_t res;
3787
3788 CameraMetadata &metadata = request->mSettings;
3789
3790 // If AF trigger is active, insert a dummy AF trigger ID if none already
3791 // exists
3792 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3793 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3794 if (afTrigger.count > 0 &&
3795 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3796 afId.count == 0) {
3797 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3798 if (res != OK) return res;
3799 }
3800
3801 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3802 // if none already exists
3803 camera_metadata_entry pcTrigger =
3804 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3805 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3806 if (pcTrigger.count > 0 &&
3807 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3808 pcId.count == 0) {
3809 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3810 &dummyTriggerId, 1);
3811 if (res != OK) return res;
3812 }
3813
3814 return OK;
3815}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003816
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003817/**
3818 * PreparerThread inner class methods
3819 */
3820
3821Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003822 Thread(/*canCallJava*/false), mListener(nullptr),
3823 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003824}
3825
3826Camera3Device::PreparerThread::~PreparerThread() {
3827 Thread::requestExitAndWait();
3828 if (mCurrentStream != nullptr) {
3829 mCurrentStream->cancelPrepare();
3830 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3831 mCurrentStream.clear();
3832 }
3833 clear();
3834}
3835
Ruben Brunkc78ac262015-08-13 17:58:46 -07003836status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003837 status_t res;
3838
3839 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003840 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003841
Ruben Brunkc78ac262015-08-13 17:58:46 -07003842 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003843 if (res == OK) {
3844 // No preparation needed, fire listener right off
3845 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003846 if (listener != NULL) {
3847 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003848 }
3849 return OK;
3850 } else if (res != NOT_ENOUGH_DATA) {
3851 return res;
3852 }
3853
3854 // Need to prepare, start up thread if necessary
3855 if (!mActive) {
3856 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3857 // isn't running
3858 Thread::requestExitAndWait();
3859 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3860 if (res != OK) {
3861 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003862 if (listener != NULL) {
3863 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003864 }
3865 return res;
3866 }
3867 mCancelNow = false;
3868 mActive = true;
3869 ALOGV("%s: Preparer stream started", __FUNCTION__);
3870 }
3871
3872 // queue up the work
3873 mPendingStreams.push_back(stream);
3874 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3875
3876 return OK;
3877}
3878
3879status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003880 Mutex::Autolock l(mLock);
3881
3882 for (const auto& stream : mPendingStreams) {
3883 stream->cancelPrepare();
3884 }
3885 mPendingStreams.clear();
3886 mCancelNow = true;
3887
3888 return OK;
3889}
3890
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003891void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003892 Mutex::Autolock l(mLock);
3893 mListener = listener;
3894}
3895
3896bool Camera3Device::PreparerThread::threadLoop() {
3897 status_t res;
3898 {
3899 Mutex::Autolock l(mLock);
3900 if (mCurrentStream == nullptr) {
3901 // End thread if done with work
3902 if (mPendingStreams.empty()) {
3903 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3904 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3905 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3906 mActive = false;
3907 return false;
3908 }
3909
3910 // Get next stream to prepare
3911 auto it = mPendingStreams.begin();
3912 mCurrentStream = *it;
3913 mPendingStreams.erase(it);
3914 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3915 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3916 } else if (mCancelNow) {
3917 mCurrentStream->cancelPrepare();
3918 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3919 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3920 mCurrentStream.clear();
3921 mCancelNow = false;
3922 return true;
3923 }
3924 }
3925
3926 res = mCurrentStream->prepareNextBuffer();
3927 if (res == NOT_ENOUGH_DATA) return true;
3928 if (res != OK) {
3929 // Something bad happened; try to recover by cancelling prepare and
3930 // signalling listener anyway
3931 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3932 mCurrentStream->getId(), res, strerror(-res));
3933 mCurrentStream->cancelPrepare();
3934 }
3935
3936 // This stream has finished, notify listener
3937 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003938 sp<NotificationListener> listener = mListener.promote();
3939 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003940 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3941 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003942 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003943 }
3944
3945 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3946 mCurrentStream.clear();
3947
3948 return true;
3949}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003950
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003951/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003952 * Static callback forwarding methods from HAL to instance
3953 */
3954
3955void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3956 const camera3_capture_result *result) {
3957 Camera3Device *d =
3958 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003959
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003960 d->processCaptureResult(result);
3961}
3962
3963void Camera3Device::sNotify(const camera3_callback_ops *cb,
3964 const camera3_notify_msg *msg) {
3965 Camera3Device *d =
3966 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3967 d->notify(msg);
3968}
3969
3970}; // namespace android