blob: e545fb52dc3323774c935c308f204decd6e71051 [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
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800259 ALOGV("%s: E", __FUNCTION__);
260
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
336 ALOGV("%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;
533 String16 templatesOption("-t");
534 int n = args.size();
535 for (int i = 0; i < n; i++) {
536 if (args[i] == templatesOption) {
537 dumpTemplates = true;
538 }
539 }
540
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800541 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800542
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800543 const char *status =
544 mStatus == STATUS_ERROR ? "ERROR" :
545 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700546 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
547 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800548 mStatus == STATUS_ACTIVE ? "ACTIVE" :
549 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700550
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800551 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700552 if (mStatus == STATUS_ERROR) {
553 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
554 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800555 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700556 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700557 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800558
559 if (mInputStream != NULL) {
560 write(fd, lines.string(), lines.size());
561 mInputStream->dump(fd, args);
562 } else {
563 lines.appendFormat(" No input stream.\n");
564 write(fd, lines.string(), lines.size());
565 }
566 for (size_t i = 0; i < mOutputStreams.size(); i++) {
567 mOutputStreams[i]->dump(fd,args);
568 }
569
Zhijun He431503c2016-03-07 17:30:16 -0800570 if (mBufferManager != NULL) {
571 lines = String8(" Camera3 Buffer Manager:\n");
572 write(fd, lines.string(), lines.size());
573 mBufferManager->dump(fd, args);
574 }
Zhijun He125684a2015-12-26 15:07:30 -0800575
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700576 lines = String8(" In-flight requests:\n");
577 if (mInFlightMap.size() == 0) {
578 lines.append(" None\n");
579 } else {
580 for (size_t i = 0; i < mInFlightMap.size(); i++) {
581 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700582 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700583 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800584 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700585 r.numBuffersLeft);
586 }
587 }
588 write(fd, lines.string(), lines.size());
589
Igor Murashkin1e479c02013-09-06 16:55:14 -0700590 {
591 lines = String8(" Last request sent:\n");
592 write(fd, lines.string(), lines.size());
593
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700594 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700595 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
596 }
597
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800598 if (dumpTemplates) {
599 const char *templateNames[] = {
600 "TEMPLATE_PREVIEW",
601 "TEMPLATE_STILL_CAPTURE",
602 "TEMPLATE_VIDEO_RECORD",
603 "TEMPLATE_VIDEO_SNAPSHOT",
604 "TEMPLATE_ZERO_SHUTTER_LAG",
605 "TEMPLATE_MANUAL"
606 };
607
608 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
609 const camera_metadata_t *templateRequest;
610 templateRequest =
611 mHal3Device->ops->construct_default_request_settings(
612 mHal3Device, i);
613 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
614 if (templateRequest == NULL) {
615 lines.append(" Not supported\n");
616 write(fd, lines.string(), lines.size());
617 } else {
618 write(fd, lines.string(), lines.size());
619 dump_indented_camera_metadata(templateRequest,
620 fd, /*verbosity*/2, /*indentation*/8);
621 }
622 }
623 }
624
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800625 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700626 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800627 write(fd, lines.string(), lines.size());
628 mHal3Device->ops->dump(mHal3Device, fd);
629 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800630
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700631 if (gotLock) mLock.unlock();
632 if (gotInterfaceLock) mInterfaceLock.unlock();
633
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800634 return OK;
635}
636
637const CameraMetadata& Camera3Device::info() const {
638 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800639 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
640 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700641 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800642 mStatus == STATUS_ERROR ?
643 "when in error state" : "before init");
644 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800645 return mDeviceInfo;
646}
647
Jianing Wei90e59c92014-03-12 18:29:36 -0700648status_t Camera3Device::checkStatusOkToCaptureLocked() {
649 switch (mStatus) {
650 case STATUS_ERROR:
651 CLOGE("Device has encountered a serious error");
652 return INVALID_OPERATION;
653 case STATUS_UNINITIALIZED:
654 CLOGE("Device not initialized");
655 return INVALID_OPERATION;
656 case STATUS_UNCONFIGURED:
657 case STATUS_CONFIGURED:
658 case STATUS_ACTIVE:
659 // OK
660 break;
661 default:
662 SET_ERR_L("Unexpected status: %d", mStatus);
663 return INVALID_OPERATION;
664 }
665 return OK;
666}
667
668status_t Camera3Device::convertMetadataListToRequestListLocked(
669 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
670 if (requestList == NULL) {
671 CLOGE("requestList cannot be NULL.");
672 return BAD_VALUE;
673 }
674
Jianing Weicb0652e2014-03-12 18:29:36 -0700675 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700676 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
677 it != metadataList.end(); ++it) {
678 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
679 if (newRequest == 0) {
680 CLOGE("Can't create capture request");
681 return BAD_VALUE;
682 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700683
684 // Setup burst Id and request Id
685 newRequest->mResultExtras.burstId = burstId++;
686 if (it->exists(ANDROID_REQUEST_ID)) {
687 if (it->find(ANDROID_REQUEST_ID).count == 0) {
688 CLOGE("RequestID entry exists; but must not be empty in metadata");
689 return BAD_VALUE;
690 }
691 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
692 } else {
693 CLOGE("RequestID does not exist in metadata");
694 return BAD_VALUE;
695 }
696
Jianing Wei90e59c92014-03-12 18:29:36 -0700697 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700698
699 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700700 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700701
702 // Setup batch size if this is a high speed video recording request.
703 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
704 auto firstRequest = requestList->begin();
705 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
706 if (outputStream->isVideoStream()) {
707 (*firstRequest)->mBatchSize = requestList->size();
708 break;
709 }
710 }
711 }
712
Jianing Wei90e59c92014-03-12 18:29:36 -0700713 return OK;
714}
715
Jianing Weicb0652e2014-03-12 18:29:36 -0700716status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800717 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800718
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700719 List<const CameraMetadata> requests;
720 requests.push_back(request);
721 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800722}
723
Jianing Wei90e59c92014-03-12 18:29:36 -0700724status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700725 const List<const CameraMetadata> &requests, bool repeating,
726 /*out*/
727 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700728 ATRACE_CALL();
729 Mutex::Autolock il(mInterfaceLock);
730 Mutex::Autolock l(mLock);
731
732 status_t res = checkStatusOkToCaptureLocked();
733 if (res != OK) {
734 // error logged by previous call
735 return res;
736 }
737
738 RequestList requestList;
739
740 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
741 if (res != OK) {
742 // error logged by previous call
743 return res;
744 }
745
746 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700747 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700748 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700749 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700750 }
751
752 if (res == OK) {
753 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
754 if (res != OK) {
755 SET_ERR_L("Can't transition to active in %f seconds!",
756 kActiveTimeout/1e9);
757 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700758 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
759 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700760 } else {
761 CLOGE("Cannot queue request. Impossible.");
762 return BAD_VALUE;
763 }
764
765 return res;
766}
767
Jianing Weicb0652e2014-03-12 18:29:36 -0700768status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
769 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700770 ATRACE_CALL();
771
Jianing Weicb0652e2014-03-12 18:29:36 -0700772 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700773}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800774
Jianing Weicb0652e2014-03-12 18:29:36 -0700775status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
776 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800777 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800778
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700779 List<const CameraMetadata> requests;
780 requests.push_back(request);
781 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800782}
783
Jianing Weicb0652e2014-03-12 18:29:36 -0700784status_t Camera3Device::setStreamingRequestList(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*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700789}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800790
791sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
792 const CameraMetadata &request) {
793 status_t res;
794
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700795 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800796 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700797 // Stream configuration failed due to unsupported configuration.
798 // Device back to unconfigured state. Client might try other configuraitons
799 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
800 CLOGE("No streams configured");
801 return NULL;
802 }
803 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800804 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700805 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800806 return NULL;
807 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700808 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700809 if (mStatus == STATUS_UNCONFIGURED) {
810 CLOGE("No streams configured");
811 return NULL;
812 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800813 }
814
815 sp<CaptureRequest> newRequest = createCaptureRequest(request);
816 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800817}
818
Jianing Weicb0652e2014-03-12 18:29:36 -0700819status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800820 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700821 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800822 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800823
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800824 switch (mStatus) {
825 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700826 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800827 return INVALID_OPERATION;
828 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700829 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800830 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700831 case STATUS_UNCONFIGURED:
832 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800833 case STATUS_ACTIVE:
834 // OK
835 break;
836 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700837 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800838 return INVALID_OPERATION;
839 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700840 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700841
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700842 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800843}
844
845status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
846 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700847 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800848
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700849 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800850}
851
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700852status_t Camera3Device::createInputStream(
853 uint32_t width, uint32_t height, int format, int *id) {
854 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700855 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700856 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700857 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
858 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700859
860 status_t res;
861 bool wasActive = false;
862
863 switch (mStatus) {
864 case STATUS_ERROR:
865 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
866 return INVALID_OPERATION;
867 case STATUS_UNINITIALIZED:
868 ALOGE("%s: Device not initialized", __FUNCTION__);
869 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700870 case STATUS_UNCONFIGURED:
871 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700872 // OK
873 break;
874 case STATUS_ACTIVE:
875 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700876 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700877 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700878 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700879 return res;
880 }
881 wasActive = true;
882 break;
883 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700884 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700885 return INVALID_OPERATION;
886 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700887 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700888
889 if (mInputStream != 0) {
890 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
891 return INVALID_OPERATION;
892 }
893
894 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
895 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700896 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700897
898 mInputStream = newStream;
899
900 *id = mNextStreamId++;
901
902 // Continue captures if active at start
903 if (wasActive) {
904 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
905 res = configureStreamsLocked();
906 if (res != OK) {
907 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
908 __FUNCTION__, mNextStreamId, strerror(-res), res);
909 return res;
910 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700911 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700912 }
913
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700914 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700915 return OK;
916}
917
Igor Murashkin2fba5842013-04-22 14:03:54 -0700918
919status_t Camera3Device::createZslStream(
920 uint32_t width, uint32_t height,
921 int depth,
922 /*out*/
923 int *id,
924 sp<Camera3ZslStream>* zslStream) {
925 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700926 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700927 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700928 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
929 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700930
931 status_t res;
932 bool wasActive = false;
933
934 switch (mStatus) {
935 case STATUS_ERROR:
936 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
937 return INVALID_OPERATION;
938 case STATUS_UNINITIALIZED:
939 ALOGE("%s: Device not initialized", __FUNCTION__);
940 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700941 case STATUS_UNCONFIGURED:
942 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700943 // OK
944 break;
945 case STATUS_ACTIVE:
946 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700947 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700948 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700949 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700950 return res;
951 }
952 wasActive = true;
953 break;
954 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700955 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700956 return INVALID_OPERATION;
957 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700958 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700959
960 if (mInputStream != 0) {
961 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
962 return INVALID_OPERATION;
963 }
964
965 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
966 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700967 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700968
969 res = mOutputStreams.add(mNextStreamId, newStream);
970 if (res < 0) {
971 ALOGE("%s: Can't add new stream to set: %s (%d)",
972 __FUNCTION__, strerror(-res), res);
973 return res;
974 }
975 mInputStream = newStream;
976
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530977 mNeedConfig = true;
978
Igor Murashkin2fba5842013-04-22 14:03:54 -0700979 *id = mNextStreamId++;
980 *zslStream = newStream;
981
982 // Continue captures if active at start
983 if (wasActive) {
984 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
985 res = configureStreamsLocked();
986 if (res != OK) {
987 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
988 __FUNCTION__, mNextStreamId, strerror(-res), res);
989 return res;
990 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700991 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700992 }
993
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700994 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700995 return OK;
996}
997
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700998status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800999 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -07001000 camera3_stream_rotation_t rotation, int *id, int streamSetId, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001001 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001002 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001003 Mutex::Autolock l(mLock);
Zhijun He5d677d12016-05-29 16:52:39 -07001004 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
1005 " consumer usage 0x%x", mId, mNextStreamId, width, height, format, dataSpace, rotation,
1006 consumerUsage);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001007
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001008 status_t res;
1009 bool wasActive = false;
1010
1011 switch (mStatus) {
1012 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001013 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001014 return INVALID_OPERATION;
1015 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001016 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001017 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001018 case STATUS_UNCONFIGURED:
1019 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001020 // OK
1021 break;
1022 case STATUS_ACTIVE:
1023 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001024 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001025 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001026 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001027 return res;
1028 }
1029 wasActive = true;
1030 break;
1031 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001032 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001033 return INVALID_OPERATION;
1034 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001035 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001036
1037 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001038 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001039 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001040 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001041 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1042 }
Zhijun He5d677d12016-05-29 16:52:39 -07001043
1044 // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
1045 // which requires a consumer surface to be available.
1046 if (consumer == nullptr && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1047 ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
1048 return BAD_VALUE;
1049 }
1050
1051 if (consumer == nullptr && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1052 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1053 return BAD_VALUE;
1054 }
1055
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001056 // Use legacy dataspace values for older HALs
1057 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1058 dataSpace = mapToLegacyDataspace(dataSpace);
1059 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001060 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001061 ssize_t blobBufferSize;
1062 if (dataSpace != HAL_DATASPACE_DEPTH) {
1063 blobBufferSize = getJpegBufferSize(width, height);
1064 if (blobBufferSize <= 0) {
1065 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1066 return BAD_VALUE;
1067 }
1068 } else {
1069 blobBufferSize = getPointCloudBufferSize();
1070 if (blobBufferSize <= 0) {
1071 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1072 return BAD_VALUE;
1073 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001074 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001075 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001076 width, height, blobBufferSize, format, dataSpace, rotation,
1077 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001078 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1079 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1080 if (rawOpaqueBufferSize <= 0) {
1081 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1082 return BAD_VALUE;
1083 }
1084 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001085 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1086 mTimestampOffset, streamSetId);
Zhijun He5d677d12016-05-29 16:52:39 -07001087 } else if (consumer == nullptr) {
1088 newStream = new Camera3OutputStream(mNextStreamId,
1089 width, height, format, consumerUsage, dataSpace, rotation,
1090 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001091 } else {
1092 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001093 width, height, format, dataSpace, rotation,
1094 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001095 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001096 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001097
Zhijun He125684a2015-12-26 15:07:30 -08001098 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001099 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1100 * requires buffers to be statically allocated for internal static buffer registration, while
1101 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1102 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001103 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001104 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001105 newStream->setBufferManager(mBufferManager);
1106 }
1107
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001108 res = mOutputStreams.add(mNextStreamId, newStream);
1109 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001110 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001111 return res;
1112 }
1113
1114 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001115 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001116
1117 // Continue captures if active at start
1118 if (wasActive) {
1119 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1120 res = configureStreamsLocked();
1121 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001122 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1123 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001124 return res;
1125 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001126 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001127 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001128 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001129 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001130}
1131
1132status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1133 ATRACE_CALL();
1134 (void)outputId; (void)id;
1135
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001136 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001137 return INVALID_OPERATION;
1138}
1139
1140
1141status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001142 uint32_t *width, uint32_t *height,
1143 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001144 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001145 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001146 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001147
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001148 switch (mStatus) {
1149 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001150 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001151 return INVALID_OPERATION;
1152 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001153 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001154 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001155 case STATUS_UNCONFIGURED:
1156 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001157 case STATUS_ACTIVE:
1158 // OK
1159 break;
1160 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001161 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001162 return INVALID_OPERATION;
1163 }
1164
1165 ssize_t idx = mOutputStreams.indexOfKey(id);
1166 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001167 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001168 return idx;
1169 }
1170
1171 if (width) *width = mOutputStreams[idx]->getWidth();
1172 if (height) *height = mOutputStreams[idx]->getHeight();
1173 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001174 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001175 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001176}
1177
1178status_t Camera3Device::setStreamTransform(int id,
1179 int transform) {
1180 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001181 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001182 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001183
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001184 switch (mStatus) {
1185 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001186 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001187 return INVALID_OPERATION;
1188 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001189 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001190 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001191 case STATUS_UNCONFIGURED:
1192 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001193 case STATUS_ACTIVE:
1194 // OK
1195 break;
1196 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001197 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001198 return INVALID_OPERATION;
1199 }
1200
1201 ssize_t idx = mOutputStreams.indexOfKey(id);
1202 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001203 CLOGE("Stream %d does not exist",
1204 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001205 return BAD_VALUE;
1206 }
1207
1208 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001209}
1210
1211status_t Camera3Device::deleteStream(int id) {
1212 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001213 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001214 Mutex::Autolock l(mLock);
1215 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001216
Igor Murashkine2172be2013-05-28 15:31:39 -07001217 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1218
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001219 // CameraDevice semantics require device to already be idle before
1220 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001221 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001222 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1223 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001224 }
1225
Igor Murashkin2fba5842013-04-22 14:03:54 -07001226 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001227 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001228 if (mInputStream != NULL && id == mInputStream->getId()) {
1229 deletedStream = mInputStream;
1230 mInputStream.clear();
1231 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001232 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001233 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001234 return BAD_VALUE;
1235 }
Zhijun He5f446352014-01-22 09:49:33 -08001236 }
1237
1238 // Delete output stream or the output part of a bi-directional stream.
1239 if (outputStreamIdx != NAME_NOT_FOUND) {
1240 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001241 mOutputStreams.removeItem(id);
1242 }
1243
1244 // Free up the stream endpoint so that it can be used by some other stream
1245 res = deletedStream->disconnect();
1246 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001247 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001248 // fall through since we want to still list the stream as deleted.
1249 }
1250 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001251 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001252
1253 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001254}
1255
1256status_t Camera3Device::deleteReprocessStream(int id) {
1257 ATRACE_CALL();
1258 (void)id;
1259
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001260 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001261 return INVALID_OPERATION;
1262}
1263
Zhijun He1fa89992015-06-01 15:44:31 -07001264status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001265 ATRACE_CALL();
1266 ALOGV("%s: E", __FUNCTION__);
1267
1268 Mutex::Autolock il(mInterfaceLock);
1269 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001270
1271 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1272 mNeedConfig = true;
1273 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1274 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001275
1276 return configureStreamsLocked();
1277}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001278
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001279status_t Camera3Device::getInputBufferProducer(
1280 sp<IGraphicBufferProducer> *producer) {
1281 Mutex::Autolock il(mInterfaceLock);
1282 Mutex::Autolock l(mLock);
1283
1284 if (producer == NULL) {
1285 return BAD_VALUE;
1286 } else if (mInputStream == NULL) {
1287 return INVALID_OPERATION;
1288 }
1289
1290 return mInputStream->getInputBufferProducer(producer);
1291}
1292
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001293status_t Camera3Device::createDefaultRequest(int templateId,
1294 CameraMetadata *request) {
1295 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001296 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001297
1298 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1299 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1300 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1301 return BAD_VALUE;
1302 }
1303
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001304 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001305 Mutex::Autolock l(mLock);
1306
1307 switch (mStatus) {
1308 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001309 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001310 return INVALID_OPERATION;
1311 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001312 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001313 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001314 case STATUS_UNCONFIGURED:
1315 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001316 case STATUS_ACTIVE:
1317 // OK
1318 break;
1319 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001320 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001321 return INVALID_OPERATION;
1322 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001323
Zhijun Hea1530f12014-09-14 12:44:20 -07001324 if (!mRequestTemplateCache[templateId].isEmpty()) {
1325 *request = mRequestTemplateCache[templateId];
1326 return OK;
1327 }
1328
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001329 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001330 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001331 rawRequest = mHal3Device->ops->construct_default_request_settings(
1332 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001333 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001334 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001335 ALOGI("%s: template %d is not supported on this camera device",
1336 __FUNCTION__, templateId);
1337 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001338 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001339
Zhijun Hea1530f12014-09-14 12:44:20 -07001340 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001341
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001342 // Derive some new keys for backward compatibility
1343 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
1344 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
1345 int32_t defaultBoost[1] = {100};
1346 mRequestTemplateCache[templateId].update(
1347 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1348 defaultBoost, 1);
1349 }
1350
1351 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001352 return OK;
1353}
1354
1355status_t Camera3Device::waitUntilDrained() {
1356 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001357 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001358 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001359
Zhijun He69a37482014-03-23 18:44:49 -07001360 return waitUntilDrainedLocked();
1361}
1362
1363status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001364 switch (mStatus) {
1365 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001366 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001367 ALOGV("%s: Already idle", __FUNCTION__);
1368 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001369 case STATUS_CONFIGURED:
1370 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001371 case STATUS_ERROR:
1372 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001373 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001374 break;
1375 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001376 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001377 return INVALID_OPERATION;
1378 }
1379
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001380 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1381 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001382 if (res != OK) {
1383 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1384 res);
1385 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001386 return res;
1387}
1388
Ruben Brunk183f0562015-08-12 12:55:02 -07001389
1390void Camera3Device::internalUpdateStatusLocked(Status status) {
1391 mStatus = status;
1392 mRecentStatusUpdates.add(mStatus);
1393 mStatusChanged.broadcast();
1394}
1395
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001396// Pause to reconfigure
1397status_t Camera3Device::internalPauseAndWaitLocked() {
1398 mRequestThread->setPaused(true);
1399 mPauseStateNotify = true;
1400
1401 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1402 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1403 if (res != OK) {
1404 SET_ERR_L("Can't idle device in %f seconds!",
1405 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001406 }
1407
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001408 return res;
1409}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001410
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001411// Resume after internalPauseAndWaitLocked
1412status_t Camera3Device::internalResumeLocked() {
1413 status_t res;
1414
1415 mRequestThread->setPaused(false);
1416
1417 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1418 if (res != OK) {
1419 SET_ERR_L("Can't transition to active in %f seconds!",
1420 kActiveTimeout/1e9);
1421 }
1422 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001423 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001424}
1425
Ruben Brunk183f0562015-08-12 12:55:02 -07001426status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001427 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001428
1429 size_t startIndex = 0;
1430 if (mStatusWaiters == 0) {
1431 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1432 // this status list
1433 mRecentStatusUpdates.clear();
1434 } else {
1435 // If other threads are waiting on updates to this status list, set the position of the
1436 // first element that this list will check rather than clearing the list.
1437 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001438 }
1439
Ruben Brunk183f0562015-08-12 12:55:02 -07001440 mStatusWaiters++;
1441
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001442 bool stateSeen = false;
1443 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001444 if (active == (mStatus == STATUS_ACTIVE)) {
1445 // Desired state is current
1446 break;
1447 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001448
1449 res = mStatusChanged.waitRelative(mLock, timeout);
1450 if (res != OK) break;
1451
Ruben Brunk183f0562015-08-12 12:55:02 -07001452 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1453 // transitions.
1454 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1455 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1456 __FUNCTION__);
1457
1458 // Encountered desired state since we began waiting
1459 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001460 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1461 stateSeen = true;
1462 break;
1463 }
1464 }
1465 } while (!stateSeen);
1466
Ruben Brunk183f0562015-08-12 12:55:02 -07001467 mStatusWaiters--;
1468
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001469 return res;
1470}
1471
1472
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001473status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1474 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001475 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001476
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001477 if (listener != NULL && mListener != NULL) {
1478 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1479 }
1480 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001481 mRequestThread->setNotificationListener(listener);
1482 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001483
1484 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001485}
1486
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001487bool Camera3Device::willNotify3A() {
1488 return false;
1489}
1490
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001491status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001492 status_t res;
1493 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001494
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001495 while (mResultQueue.empty()) {
1496 res = mResultSignal.waitRelative(mOutputLock, timeout);
1497 if (res == TIMED_OUT) {
1498 return res;
1499 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001500 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001501 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001502 return res;
1503 }
1504 }
1505 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001506}
1507
Jianing Weicb0652e2014-03-12 18:29:36 -07001508status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001509 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001510 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001511
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001512 if (mResultQueue.empty()) {
1513 return NOT_ENOUGH_DATA;
1514 }
1515
Jianing Weicb0652e2014-03-12 18:29:36 -07001516 if (frame == NULL) {
1517 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1518 return BAD_VALUE;
1519 }
1520
1521 CaptureResult &result = *(mResultQueue.begin());
1522 frame->mResultExtras = result.mResultExtras;
1523 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001524 mResultQueue.erase(mResultQueue.begin());
1525
1526 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001527}
1528
1529status_t Camera3Device::triggerAutofocus(uint32_t id) {
1530 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001531 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001532
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001533 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1534 // Mix-in this trigger into the next request and only the next request.
1535 RequestTrigger trigger[] = {
1536 {
1537 ANDROID_CONTROL_AF_TRIGGER,
1538 ANDROID_CONTROL_AF_TRIGGER_START
1539 },
1540 {
1541 ANDROID_CONTROL_AF_TRIGGER_ID,
1542 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001543 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001544 };
1545
1546 return mRequestThread->queueTrigger(trigger,
1547 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001548}
1549
1550status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1551 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001552 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001553
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001554 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1555 // Mix-in this trigger into the next request and only the next request.
1556 RequestTrigger trigger[] = {
1557 {
1558 ANDROID_CONTROL_AF_TRIGGER,
1559 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1560 },
1561 {
1562 ANDROID_CONTROL_AF_TRIGGER_ID,
1563 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001564 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001565 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001566
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001567 return mRequestThread->queueTrigger(trigger,
1568 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001569}
1570
1571status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1572 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001573 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001574
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001575 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1576 // Mix-in this trigger into the next request and only the next request.
1577 RequestTrigger trigger[] = {
1578 {
1579 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1580 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1581 },
1582 {
1583 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1584 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001585 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001586 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001587
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001588 return mRequestThread->queueTrigger(trigger,
1589 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001590}
1591
1592status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1593 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1594 ATRACE_CALL();
1595 (void)reprocessStreamId; (void)buffer; (void)listener;
1596
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001597 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001598 return INVALID_OPERATION;
1599}
1600
Jianing Weicb0652e2014-03-12 18:29:36 -07001601status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001602 ATRACE_CALL();
1603 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001604 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001605
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001606 NotificationListener* listener;
1607 {
1608 Mutex::Autolock l(mOutputLock);
1609 listener = mListener;
1610 }
1611
Zhijun He7ef20392014-04-21 16:04:17 -07001612 {
1613 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001614 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001615 }
1616
Zhijun He491e3412013-12-27 10:57:44 -08001617 status_t res;
1618 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001619 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001620 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001621 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001622 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001623 }
1624
1625 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001626}
1627
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001628status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001629 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1630}
1631
1632status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001633 ATRACE_CALL();
1634 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001635 Mutex::Autolock il(mInterfaceLock);
1636 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001637
1638 sp<Camera3StreamInterface> stream;
1639 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1640 if (outputStreamIdx == NAME_NOT_FOUND) {
1641 CLOGE("Stream %d does not exist", streamId);
1642 return BAD_VALUE;
1643 }
1644
1645 stream = mOutputStreams.editValueAt(outputStreamIdx);
1646
1647 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001648 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001649 return BAD_VALUE;
1650 }
1651
1652 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001653 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001654 return BAD_VALUE;
1655 }
1656
Ruben Brunkc78ac262015-08-13 17:58:46 -07001657 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001658}
1659
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001660status_t Camera3Device::tearDown(int streamId) {
1661 ATRACE_CALL();
1662 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1663 Mutex::Autolock il(mInterfaceLock);
1664 Mutex::Autolock l(mLock);
1665
1666 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1667 // since we cannot call register_stream_buffers except right after configure_streams.
1668 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1669 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1670 __FUNCTION__, mHal3Device->common.version);
1671 return NO_INIT;
1672 }
1673
1674 sp<Camera3StreamInterface> stream;
1675 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1676 if (outputStreamIdx == NAME_NOT_FOUND) {
1677 CLOGE("Stream %d does not exist", streamId);
1678 return BAD_VALUE;
1679 }
1680
1681 stream = mOutputStreams.editValueAt(outputStreamIdx);
1682
1683 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1684 CLOGE("Stream %d is a target of a in-progress request", streamId);
1685 return BAD_VALUE;
1686 }
1687
1688 return stream->tearDown();
1689}
1690
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001691status_t Camera3Device::addBufferListenerForStream(int streamId,
1692 wp<Camera3StreamBufferListener> listener) {
1693 ATRACE_CALL();
1694 ALOGV("%s: Camera %d: Adding buffer listener for stream %d", __FUNCTION__, mId, streamId);
1695 Mutex::Autolock il(mInterfaceLock);
1696 Mutex::Autolock l(mLock);
1697
1698 sp<Camera3StreamInterface> stream;
1699 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1700 if (outputStreamIdx == NAME_NOT_FOUND) {
1701 CLOGE("Stream %d does not exist", streamId);
1702 return BAD_VALUE;
1703 }
1704
1705 stream = mOutputStreams.editValueAt(outputStreamIdx);
1706 stream->addBufferListener(listener);
1707
1708 return OK;
1709}
1710
Zhijun He204e3292014-07-14 17:09:23 -07001711uint32_t Camera3Device::getDeviceVersion() {
1712 ATRACE_CALL();
1713 Mutex::Autolock il(mInterfaceLock);
1714 return mDeviceVersion;
1715}
1716
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001717/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001718 * Methods called by subclasses
1719 */
1720
1721void Camera3Device::notifyStatus(bool idle) {
1722 {
1723 // Need mLock to safely update state and synchronize to current
1724 // state of methods in flight.
1725 Mutex::Autolock l(mLock);
1726 // We can get various system-idle notices from the status tracker
1727 // while starting up. Only care about them if we've actually sent
1728 // in some requests recently.
1729 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1730 return;
1731 }
1732 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1733 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001734 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001735
1736 // Skip notifying listener if we're doing some user-transparent
1737 // state changes
1738 if (mPauseStateNotify) return;
1739 }
1740 NotificationListener *listener;
1741 {
1742 Mutex::Autolock l(mOutputLock);
1743 listener = mListener;
1744 }
1745 if (idle && listener != NULL) {
1746 listener->notifyIdle();
1747 }
1748}
1749
Zhijun He5d677d12016-05-29 16:52:39 -07001750status_t Camera3Device::setConsumerSurface(int streamId, sp<Surface> consumer) {
1751 ATRACE_CALL();
1752 ALOGV("%s: Camera %d: set consumer surface for stream %d", __FUNCTION__, mId, streamId);
1753 Mutex::Autolock il(mInterfaceLock);
1754 Mutex::Autolock l(mLock);
1755
1756 if (consumer == nullptr) {
1757 CLOGE("Null consumer is passed!");
1758 return BAD_VALUE;
1759 }
1760
1761 ssize_t idx = mOutputStreams.indexOfKey(streamId);
1762 if (idx == NAME_NOT_FOUND) {
1763 CLOGE("Stream %d is unknown", streamId);
1764 return idx;
1765 }
1766 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
1767 status_t res = stream->setConsumer(consumer);
1768 if (res != OK) {
1769 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1770 return res;
1771 }
1772
1773 if (!stream->isConfiguring()) {
1774 CLOGE("Stream %d was already fully configured.", streamId);
1775 return INVALID_OPERATION;
1776 }
1777
1778 res = stream->finishConfiguration(mHal3Device);
1779 if (res != OK) {
1780 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1781 stream->getId(), strerror(-res), res);
1782 return res;
1783 }
1784
1785 return OK;
1786}
1787
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001788/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001789 * Camera3Device private methods
1790 */
1791
1792sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1793 const CameraMetadata &request) {
1794 ATRACE_CALL();
1795 status_t res;
1796
1797 sp<CaptureRequest> newRequest = new CaptureRequest;
1798 newRequest->mSettings = request;
1799
1800 camera_metadata_entry_t inputStreams =
1801 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1802 if (inputStreams.count > 0) {
1803 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001804 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001805 CLOGE("Request references unknown input stream %d",
1806 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001807 return NULL;
1808 }
1809 // Lazy completion of stream configuration (allocation/registration)
1810 // on first use
1811 if (mInputStream->isConfiguring()) {
1812 res = mInputStream->finishConfiguration(mHal3Device);
1813 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001814 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001815 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001816 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001817 return NULL;
1818 }
1819 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001820 // Check if stream is being prepared
1821 if (mInputStream->isPreparing()) {
1822 CLOGE("Request references an input stream that's being prepared!");
1823 return NULL;
1824 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001825
1826 newRequest->mInputStream = mInputStream;
1827 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1828 }
1829
1830 camera_metadata_entry_t streams =
1831 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1832 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001833 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001834 return NULL;
1835 }
1836
1837 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001838 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001839 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001840 CLOGE("Request references unknown stream %d",
1841 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001842 return NULL;
1843 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001844 sp<Camera3OutputStreamInterface> stream =
1845 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001846
Zhijun He5d677d12016-05-29 16:52:39 -07001847 // It is illegal to include a deferred consumer output stream into a request
1848 if (stream->isConsumerConfigurationDeferred()) {
1849 CLOGE("Stream %d hasn't finished configuration yet due to deferred consumer",
1850 stream->getId());
1851 return NULL;
1852 }
1853
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001854 // Lazy completion of stream configuration (allocation/registration)
1855 // on first use
1856 if (stream->isConfiguring()) {
1857 res = stream->finishConfiguration(mHal3Device);
1858 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001859 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1860 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001861 return NULL;
1862 }
1863 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001864 // Check if stream is being prepared
1865 if (stream->isPreparing()) {
1866 CLOGE("Request references an output stream that's being prepared!");
1867 return NULL;
1868 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001869
1870 newRequest->mOutputStreams.push(stream);
1871 }
1872 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001873 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001874
1875 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001876}
1877
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001878bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1879 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1880 Size size = mSupportedOpaqueInputSizes[i];
1881 if (size.width == width && size.height == height) {
1882 return true;
1883 }
1884 }
1885
1886 return false;
1887}
1888
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001889status_t Camera3Device::configureStreamsLocked() {
1890 ATRACE_CALL();
1891 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001892
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001893 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001894 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001895 return INVALID_OPERATION;
1896 }
1897
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001898 if (!mNeedConfig) {
1899 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1900 return OK;
1901 }
1902
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001903 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1904 // adding a dummy stream instead.
1905 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1906 if (mOutputStreams.size() == 0) {
1907 addDummyStreamLocked();
1908 } else {
1909 tryRemoveDummyStreamLocked();
1910 }
1911
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001912 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001913 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001914
1915 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001916 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1917 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1918 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001919 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1920
1921 Vector<camera3_stream_t*> streams;
1922 streams.setCapacity(config.num_streams);
1923
1924 if (mInputStream != NULL) {
1925 camera3_stream_t *inputStream;
1926 inputStream = mInputStream->startConfiguration();
1927 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001928 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001929 return INVALID_OPERATION;
1930 }
1931 streams.add(inputStream);
1932 }
1933
1934 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001935
1936 // Don't configure bidi streams twice, nor add them twice to the list
1937 if (mOutputStreams[i].get() ==
1938 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1939
1940 config.num_streams--;
1941 continue;
1942 }
1943
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001944 camera3_stream_t *outputStream;
1945 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1946 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001947 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001948 return INVALID_OPERATION;
1949 }
1950 streams.add(outputStream);
1951 }
1952
1953 config.streams = streams.editArray();
1954
1955 // Do the HAL configuration; will potentially touch stream
1956 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001957 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001958 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001959 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001960
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001961 if (res == BAD_VALUE) {
1962 // HAL rejected this set of streams as unsupported, clean up config
1963 // attempt and return to unconfigured state
1964 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1965 res = mInputStream->cancelConfiguration();
1966 if (res != OK) {
1967 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1968 mInputStream->getId(), strerror(-res), res);
1969 return res;
1970 }
1971 }
1972
1973 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1974 sp<Camera3OutputStreamInterface> outputStream =
1975 mOutputStreams.editValueAt(i);
1976 if (outputStream->isConfiguring()) {
1977 res = outputStream->cancelConfiguration();
1978 if (res != OK) {
1979 SET_ERR_L(
1980 "Can't cancel configuring output stream %d: %s (%d)",
1981 outputStream->getId(), strerror(-res), res);
1982 return res;
1983 }
1984 }
1985 }
1986
1987 // Return state to that at start of call, so that future configures
1988 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001989 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001990 mNeedConfig = true;
1991
1992 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1993 return BAD_VALUE;
1994 } else if (res != OK) {
1995 // Some other kind of error from configure_streams - this is not
1996 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001997 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1998 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001999 return res;
2000 }
2001
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002002 // Finish all stream configuration immediately.
2003 // TODO: Try to relax this later back to lazy completion, which should be
2004 // faster
2005
Igor Murashkin073f8572013-05-02 14:59:28 -07002006 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002007 res = mInputStream->finishConfiguration(mHal3Device);
2008 if (res != OK) {
2009 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
2010 mInputStream->getId(), strerror(-res), res);
2011 return res;
2012 }
2013 }
2014
2015 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002016 sp<Camera3OutputStreamInterface> outputStream =
2017 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002018 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002019 res = outputStream->finishConfiguration(mHal3Device);
2020 if (res != OK) {
2021 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2022 outputStream->getId(), strerror(-res), res);
2023 return res;
2024 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002025 }
2026 }
2027
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002028 // Request thread needs to know to avoid using repeat-last-settings protocol
2029 // across configure_streams() calls
2030 mRequestThread->configurationComplete();
2031
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002032 // Boost priority of request thread for high speed recording to SCHED_FIFO
2033 if (mIsConstrainedHighSpeedConfiguration) {
2034 pid_t requestThreadTid = mRequestThread->getTid();
2035 res = requestPriority(getpid(), requestThreadTid,
Eino-Ville Talvala0f6778e2016-04-25 16:57:04 -07002036 kConstrainedHighSpeedThreadPriority, /*asynchronous*/ false);
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002037 if (res != OK) {
2038 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2039 strerror(-res), res);
2040 } else {
2041 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2042 }
2043 } else {
2044 // TODO: Set/restore normal priority for normal use cases
2045 }
2046
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002047 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002048
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002049 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002050
Ruben Brunk183f0562015-08-12 12:55:02 -07002051 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2052 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002053
2054 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
2055
Zhijun He0a210512014-07-24 13:45:15 -07002056 // tear down the deleted streams after configure streams.
2057 mDeletedStreams.clear();
2058
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002059 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002060}
2061
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002062status_t Camera3Device::addDummyStreamLocked() {
2063 ATRACE_CALL();
2064 status_t res;
2065
2066 if (mDummyStreamId != NO_STREAM) {
2067 // Should never be adding a second dummy stream when one is already
2068 // active
2069 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
2070 __FUNCTION__, mId);
2071 return INVALID_OPERATION;
2072 }
2073
2074 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
2075
2076 sp<Camera3OutputStreamInterface> dummyStream =
2077 new Camera3DummyStream(mNextStreamId);
2078
2079 res = mOutputStreams.add(mNextStreamId, dummyStream);
2080 if (res < 0) {
2081 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2082 return res;
2083 }
2084
2085 mDummyStreamId = mNextStreamId;
2086 mNextStreamId++;
2087
2088 return OK;
2089}
2090
2091status_t Camera3Device::tryRemoveDummyStreamLocked() {
2092 ATRACE_CALL();
2093 status_t res;
2094
2095 if (mDummyStreamId == NO_STREAM) return OK;
2096 if (mOutputStreams.size() == 1) return OK;
2097
2098 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
2099
2100 // Ok, have a dummy stream and there's at least one other output stream,
2101 // so remove the dummy
2102
2103 sp<Camera3StreamInterface> deletedStream;
2104 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2105 if (outputStreamIdx == NAME_NOT_FOUND) {
2106 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2107 return INVALID_OPERATION;
2108 }
2109
2110 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2111 mOutputStreams.removeItemsAt(outputStreamIdx);
2112
2113 // Free up the stream endpoint so that it can be used by some other stream
2114 res = deletedStream->disconnect();
2115 if (res != OK) {
2116 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2117 // fall through since we want to still list the stream as deleted.
2118 }
2119 mDeletedStreams.add(deletedStream);
2120 mDummyStreamId = NO_STREAM;
2121
2122 return res;
2123}
2124
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002125void Camera3Device::setErrorState(const char *fmt, ...) {
2126 Mutex::Autolock l(mLock);
2127 va_list args;
2128 va_start(args, fmt);
2129
2130 setErrorStateLockedV(fmt, args);
2131
2132 va_end(args);
2133}
2134
2135void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2136 Mutex::Autolock l(mLock);
2137 setErrorStateLockedV(fmt, args);
2138}
2139
2140void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2141 va_list args;
2142 va_start(args, fmt);
2143
2144 setErrorStateLockedV(fmt, args);
2145
2146 va_end(args);
2147}
2148
2149void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002150 // Print out all error messages to log
2151 String8 errorCause = String8::formatV(fmt, args);
2152 ALOGE("Camera %d: %s", mId, errorCause.string());
2153
2154 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002155 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002156
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002157 mErrorCause = errorCause;
2158
2159 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002160 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002161
2162 // Notify upstream about a device error
2163 if (mListener != NULL) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002164 mListener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002165 CaptureResultExtras());
2166 }
2167
2168 // Save stack trace. View by dumping it later.
2169 CameraTraces::saveTrace();
2170 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002171}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002172
2173/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002174 * In-flight request management
2175 */
2176
Jianing Weicb0652e2014-03-12 18:29:36 -07002177status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002178 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2179 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002180 ATRACE_CALL();
2181 Mutex::Autolock l(mInFlightLock);
2182
2183 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002184 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2185 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002186 if (res < 0) return res;
2187
2188 return OK;
2189}
2190
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002191void Camera3Device::returnOutputBuffers(
2192 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2193 nsecs_t timestamp) {
2194 for (size_t i = 0; i < numBuffers; i++)
2195 {
2196 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2197 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2198 // Note: stream may be deallocated at this point, if this buffer was
2199 // the last reference to it.
2200 if (res != OK) {
2201 ALOGE("Can't return buffer to its stream: %s (%d)",
2202 strerror(-res), res);
2203 }
2204 }
2205}
2206
2207
2208void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2209
2210 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2211 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2212
2213 nsecs_t sensorTimestamp = request.sensorTimestamp;
2214 nsecs_t shutterTimestamp = request.shutterTimestamp;
2215
2216 // Check if it's okay to remove the request from InFlightMap:
2217 // In the case of a successful request:
2218 // all input and output buffers, all result metadata, shutter callback
2219 // arrived.
2220 // In the case of a unsuccessful request:
2221 // all input and output buffers arrived.
2222 if (request.numBuffersLeft == 0 &&
2223 (request.requestStatus != OK ||
2224 (request.haveResultMetadata && shutterTimestamp != 0))) {
2225 ATRACE_ASYNC_END("frame capture", frameNumber);
2226
2227 // Sanity check - if sensor timestamp matches shutter timestamp
2228 if (request.requestStatus == OK &&
2229 sensorTimestamp != shutterTimestamp) {
2230 SET_ERR("sensor timestamp (%" PRId64
2231 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2232 sensorTimestamp, frameNumber, shutterTimestamp);
2233 }
2234
2235 // for an unsuccessful request, it may have pending output buffers to
2236 // return.
2237 assert(request.requestStatus != OK ||
2238 request.pendingOutputBuffers.size() == 0);
2239 returnOutputBuffers(request.pendingOutputBuffers.array(),
2240 request.pendingOutputBuffers.size(), 0);
2241
2242 mInFlightMap.removeItemsAt(idx, 1);
2243
2244 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2245 }
2246
2247 // Sanity check - if we have too many in-flight frames, something has
2248 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002249 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002250 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002251 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2252 kInFlightWarnLimitHighSpeed) {
2253 CLOGE("In-flight list too large for high speed configuration: %zu",
2254 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002255 }
2256}
2257
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002258void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2259 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2260 if (result == nullptr) return;
2261
2262 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2263 (int32_t*)&frameNumber, 1) != OK) {
2264 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2265 return;
2266 }
2267
2268 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2269 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2270 return;
2271 }
2272
2273 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2274
2275 // Valid result, insert into queue
2276 List<CaptureResult>::iterator queuedResult =
2277 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2278 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2279 ", burstId = %" PRId32, __FUNCTION__,
2280 queuedResult->mResultExtras.requestId,
2281 queuedResult->mResultExtras.frameNumber,
2282 queuedResult->mResultExtras.burstId);
2283
2284 mResultSignal.signal();
2285}
2286
2287
2288void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2289 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2290 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2291 Mutex::Autolock l(mOutputLock);
2292
2293 CaptureResult captureResult;
2294 captureResult.mResultExtras = resultExtras;
2295 captureResult.mMetadata = partialResult;
2296
2297 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2298}
2299
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002300
2301void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2302 CaptureResultExtras &resultExtras,
2303 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002304 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002305 bool reprocess,
2306 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002307 if (pendingMetadata.isEmpty())
2308 return;
2309
2310 Mutex::Autolock l(mOutputLock);
2311
2312 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002313 if (reprocess) {
2314 if (frameNumber < mNextReprocessResultFrameNumber) {
2315 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002316 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002317 frameNumber, mNextReprocessResultFrameNumber);
2318 return;
2319 }
2320 mNextReprocessResultFrameNumber = frameNumber + 1;
2321 } else {
2322 if (frameNumber < mNextResultFrameNumber) {
2323 SET_ERR("Out-of-order capture result metadata submitted! "
2324 "(got frame number %d, expecting %d)",
2325 frameNumber, mNextResultFrameNumber);
2326 return;
2327 }
2328 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002329 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002330
2331 CaptureResult captureResult;
2332 captureResult.mResultExtras = resultExtras;
2333 captureResult.mMetadata = pendingMetadata;
2334
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002335 // Append any previous partials to form a complete result
2336 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2337 captureResult.mMetadata.append(collectedPartialResult);
2338 }
2339
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07002340 // Derive some new keys for backward compaibility
2341 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
2342 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
2343 int32_t defaultBoost[1] = {100};
2344 captureResult.mMetadata.update(
2345 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
2346 defaultBoost, 1);
2347 }
2348
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002349 captureResult.mMetadata.sort();
2350
2351 // Check that there's a timestamp in the result metadata
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002352 camera_metadata_entry entry = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002353 if (entry.count == 0) {
2354 SET_ERR("No timestamp provided by HAL for frame %d!",
2355 frameNumber);
2356 return;
2357 }
2358
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002359 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002360}
2361
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002362/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002363 * Camera HAL device callback methods
2364 */
2365
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002366void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002367 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002368
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002369 status_t res;
2370
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002371 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002372 if (result->result == NULL && result->num_output_buffers == 0 &&
2373 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002374 SET_ERR("No result data provided by HAL for frame %d",
2375 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002376 return;
2377 }
Zhijun He204e3292014-07-14 17:09:23 -07002378
2379 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2380 // partial_result to 1 when metadata is included in this result.
2381 if (!mUsePartialResult &&
2382 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2383 result->result != NULL &&
2384 result->partial_result != 1) {
2385 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2386 " if partial result is not supported",
2387 frameNumber, result->partial_result);
2388 return;
2389 }
2390
2391 bool isPartialResult = false;
2392 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002393 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002394 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002395
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002396 // Get shutter timestamp and resultExtras from list of in-flight requests,
2397 // where it was added by the shutter notification for this frame. If the
2398 // shutter timestamp isn't received yet, append the output buffers to the
2399 // in-flight request and they will be returned when the shutter timestamp
2400 // arrives. Update the in-flight status and remove the in-flight entry if
2401 // all result data and shutter timestamp have been received.
2402 nsecs_t shutterTimestamp = 0;
2403
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002404 {
2405 Mutex::Autolock l(mInFlightLock);
2406 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2407 if (idx == NAME_NOT_FOUND) {
2408 SET_ERR("Unknown frame number for capture result: %d",
2409 frameNumber);
2410 return;
2411 }
2412 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002413 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2414 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2415 ", partialResultCount = %d",
2416 __FUNCTION__, request.resultExtras.requestId,
2417 request.resultExtras.frameNumber, request.resultExtras.burstId,
2418 result->partial_result);
2419 // Always update the partial count to the latest one if it's not 0
2420 // (buffers only). When framework aggregates adjacent partial results
2421 // into one, the latest partial count will be used.
2422 if (result->partial_result != 0)
2423 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002424
2425 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002426 if (mUsePartialResult && result->result != NULL) {
2427 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2428 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2429 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2430 " the range of [1, %d] when metadata is included in the result",
2431 frameNumber, result->partial_result, mNumPartialResults);
2432 return;
2433 }
2434 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002435 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002436 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002437 }
Zhijun He204e3292014-07-14 17:09:23 -07002438 } else {
2439 camera_metadata_ro_entry_t partialResultEntry;
2440 res = find_camera_metadata_ro_entry(result->result,
2441 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2442 if (res != NAME_NOT_FOUND &&
2443 partialResultEntry.count > 0 &&
2444 partialResultEntry.data.u8[0] ==
2445 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2446 // A partial result. Flag this as such, and collect this
2447 // set of metadata into the in-flight entry.
2448 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002449 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002450 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002451 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002452 ANDROID_QUIRKS_PARTIAL_RESULT);
2453 }
2454 }
2455
2456 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002457 // Send partial capture result
2458 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2459 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002460 }
2461 }
2462
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002463 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002464 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002465
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002466 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002467 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002468 if (request.haveResultMetadata) {
2469 SET_ERR("Called multiple times with metadata for frame %d",
2470 frameNumber);
2471 return;
2472 }
Zhijun He204e3292014-07-14 17:09:23 -07002473 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002474 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002475 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002476 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002477 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002478 request.haveResultMetadata = true;
2479 }
2480
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002481 uint32_t numBuffersReturned = result->num_output_buffers;
2482 if (result->input_buffer != NULL) {
2483 if (hasInputBufferInRequest) {
2484 numBuffersReturned += 1;
2485 } else {
2486 ALOGW("%s: Input buffer should be NULL if there is no input"
2487 " buffer sent in the request",
2488 __FUNCTION__);
2489 }
2490 }
2491 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002492 if (request.numBuffersLeft < 0) {
2493 SET_ERR("Too many buffers returned for frame %d",
2494 frameNumber);
2495 return;
2496 }
2497
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002498 camera_metadata_ro_entry_t entry;
2499 res = find_camera_metadata_ro_entry(result->result,
2500 ANDROID_SENSOR_TIMESTAMP, &entry);
2501 if (res == OK && entry.count == 1) {
2502 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002503 }
2504
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002505 // If shutter event isn't received yet, append the output buffers to
2506 // the in-flight request. Otherwise, return the output buffers to
2507 // streams.
2508 if (shutterTimestamp == 0) {
2509 request.pendingOutputBuffers.appendArray(result->output_buffers,
2510 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002511 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002512 returnOutputBuffers(result->output_buffers,
2513 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002514 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002515
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002516 if (result->result != NULL && !isPartialResult) {
2517 if (shutterTimestamp == 0) {
2518 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002519 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002520 } else {
2521 CameraMetadata metadata;
2522 metadata = result->result;
2523 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002524 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2525 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002526 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002527 }
2528
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002529 removeInFlightRequestIfReadyLocked(idx);
2530 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002531
Zhijun Hef0d962a2014-06-30 10:24:11 -07002532 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002533 if (hasInputBufferInRequest) {
2534 Camera3Stream *stream =
2535 Camera3Stream::cast(result->input_buffer->stream);
2536 res = stream->returnInputBuffer(*(result->input_buffer));
2537 // Note: stream may be deallocated at this point, if this buffer was the
2538 // last reference to it.
2539 if (res != OK) {
2540 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2541 " its stream:%s (%d)", __FUNCTION__,
2542 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002543 }
2544 } else {
2545 ALOGW("%s: Input buffer should be NULL if there is no input"
2546 " buffer sent in the request, skipping input buffer return.",
2547 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002548 }
2549 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002550}
2551
2552void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002553 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002554 NotificationListener *listener;
2555 {
2556 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002557 listener = mListener;
2558 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002559
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002560 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002561 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002562 return;
2563 }
2564
2565 switch (msg->type) {
2566 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002567 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002568 break;
2569 }
2570 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002571 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002572 break;
2573 }
2574 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002575 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002576 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002577 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002578}
2579
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002580void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2581 NotificationListener *listener) {
2582
2583 // Map camera HAL error codes to ICameraDeviceCallback error codes
2584 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002585 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002586 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002587 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002588 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002589 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002590 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002591 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002592 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002593 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002594 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002595 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002596 };
2597
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002598 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002599 ((msg.error_code >= 0) &&
2600 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2601 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002602 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002603
2604 int streamId = 0;
2605 if (msg.error_stream != NULL) {
2606 Camera3Stream *stream =
2607 Camera3Stream::cast(msg.error_stream);
2608 streamId = stream->getId();
2609 }
2610 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2611 mId, __FUNCTION__, msg.frame_number,
2612 streamId, msg.error_code);
2613
2614 CaptureResultExtras resultExtras;
2615 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002616 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002617 // SET_ERR calls notifyError
2618 SET_ERR("Camera HAL reported serious device error");
2619 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002620 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2621 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2622 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002623 {
2624 Mutex::Autolock l(mInFlightLock);
2625 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2626 if (idx >= 0) {
2627 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2628 r.requestStatus = msg.error_code;
2629 resultExtras = r.resultExtras;
2630 } else {
2631 resultExtras.frameNumber = msg.frame_number;
2632 ALOGE("Camera %d: %s: cannot find in-flight request on "
2633 "frame %" PRId64 " error", mId, __FUNCTION__,
2634 resultExtras.frameNumber);
2635 }
2636 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002637 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002638 if (listener != NULL) {
2639 listener->notifyError(errorCode, resultExtras);
2640 } else {
2641 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2642 }
2643 break;
2644 default:
2645 // SET_ERR calls notifyError
2646 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2647 break;
2648 }
2649}
2650
2651void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2652 NotificationListener *listener) {
2653 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002654
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002655 // Set timestamp for the request in the in-flight tracking
2656 // and get the request ID to send upstream
2657 {
2658 Mutex::Autolock l(mInFlightLock);
2659 idx = mInFlightMap.indexOfKey(msg.frame_number);
2660 if (idx >= 0) {
2661 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002662
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002663 // Verify ordering of shutter notifications
2664 {
2665 Mutex::Autolock l(mOutputLock);
2666 // TODO: need to track errors for tighter bounds on expected frame number.
2667 if (r.hasInputBuffer) {
2668 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2669 SET_ERR("Shutter notification out-of-order. Expected "
2670 "notification for frame %d, got frame %d",
2671 mNextReprocessShutterFrameNumber, msg.frame_number);
2672 return;
2673 }
2674 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2675 } else {
2676 if (msg.frame_number < mNextShutterFrameNumber) {
2677 SET_ERR("Shutter notification out-of-order. Expected "
2678 "notification for frame %d, got frame %d",
2679 mNextShutterFrameNumber, msg.frame_number);
2680 return;
2681 }
2682 mNextShutterFrameNumber = msg.frame_number + 1;
2683 }
2684 }
2685
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002686 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2687 mId, __FUNCTION__,
2688 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2689 // Call listener, if any
2690 if (listener != NULL) {
2691 listener->notifyShutter(r.resultExtras, msg.timestamp);
2692 }
2693
2694 r.shutterTimestamp = msg.timestamp;
2695
2696 // send pending result and buffers
2697 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002698 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002699 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002700 returnOutputBuffers(r.pendingOutputBuffers.array(),
2701 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2702 r.pendingOutputBuffers.clear();
2703
2704 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002705 }
2706 }
2707 if (idx < 0) {
2708 SET_ERR("Shutter notification for non-existent frame number %d",
2709 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002710 }
2711}
2712
2713
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002714CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002715 ALOGV("%s", __FUNCTION__);
2716
Igor Murashkin1e479c02013-09-06 16:55:14 -07002717 CameraMetadata retVal;
2718
2719 if (mRequestThread != NULL) {
2720 retVal = mRequestThread->getLatestRequest();
2721 }
2722
Igor Murashkin1e479c02013-09-06 16:55:14 -07002723 return retVal;
2724}
2725
Jianing Weicb0652e2014-03-12 18:29:36 -07002726
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002727/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002728 * RequestThread inner class methods
2729 */
2730
2731Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002732 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002733 camera3_device_t *hal3Device,
2734 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002735 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002736 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002737 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002738 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002739 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002740 mReconfigured(false),
2741 mDoPause(false),
2742 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002743 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002744 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002745 mCurrentAfTriggerId(0),
2746 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002747 mRepeatingLastFrameNumber(
2748 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002749 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002750 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002751}
2752
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002753void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002754 NotificationListener *listener) {
2755 Mutex::Autolock l(mRequestLock);
2756 mListener = listener;
2757}
2758
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002759void Camera3Device::RequestThread::configurationComplete() {
2760 Mutex::Autolock l(mRequestLock);
2761 mReconfigured = true;
2762}
2763
Jianing Wei90e59c92014-03-12 18:29:36 -07002764status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002765 List<sp<CaptureRequest> > &requests,
2766 /*out*/
2767 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002768 Mutex::Autolock l(mRequestLock);
2769 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2770 ++it) {
2771 mRequestQueue.push_back(*it);
2772 }
2773
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002774 if (lastFrameNumber != NULL) {
2775 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2776 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2777 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2778 *lastFrameNumber);
2779 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002780
Jianing Wei90e59c92014-03-12 18:29:36 -07002781 unpauseForNewRequests();
2782
2783 return OK;
2784}
2785
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002786
2787status_t Camera3Device::RequestThread::queueTrigger(
2788 RequestTrigger trigger[],
2789 size_t count) {
2790
2791 Mutex::Autolock l(mTriggerMutex);
2792 status_t ret;
2793
2794 for (size_t i = 0; i < count; ++i) {
2795 ret = queueTriggerLocked(trigger[i]);
2796
2797 if (ret != OK) {
2798 return ret;
2799 }
2800 }
2801
2802 return OK;
2803}
2804
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002805int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2806 sp<Camera3Device> d = device.promote();
2807 if (d != NULL) return d->mId;
2808 return 0;
2809}
2810
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002811status_t Camera3Device::RequestThread::queueTriggerLocked(
2812 RequestTrigger trigger) {
2813
2814 uint32_t tag = trigger.metadataTag;
2815 ssize_t index = mTriggerMap.indexOfKey(tag);
2816
2817 switch (trigger.getTagType()) {
2818 case TYPE_BYTE:
2819 // fall-through
2820 case TYPE_INT32:
2821 break;
2822 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002823 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2824 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002825 return INVALID_OPERATION;
2826 }
2827
2828 /**
2829 * Collect only the latest trigger, since we only have 1 field
2830 * in the request settings per trigger tag, and can't send more than 1
2831 * trigger per request.
2832 */
2833 if (index != NAME_NOT_FOUND) {
2834 mTriggerMap.editValueAt(index) = trigger;
2835 } else {
2836 mTriggerMap.add(tag, trigger);
2837 }
2838
2839 return OK;
2840}
2841
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002842status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002843 const RequestList &requests,
2844 /*out*/
2845 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002846 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002847 if (lastFrameNumber != NULL) {
2848 *lastFrameNumber = mRepeatingLastFrameNumber;
2849 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002850 mRepeatingRequests.clear();
2851 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2852 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002853
2854 unpauseForNewRequests();
2855
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002856 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002857 return OK;
2858}
2859
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002860bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2861 if (mRepeatingRequests.empty()) {
2862 return false;
2863 }
2864 int32_t requestId = requestIn->mResultExtras.requestId;
2865 const RequestList &repeatRequests = mRepeatingRequests;
2866 // All repeating requests are guaranteed to have same id so only check first quest
2867 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2868 return (firstRequest->mResultExtras.requestId == requestId);
2869}
2870
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002871status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002872 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002873 return clearRepeatingRequestsLocked(lastFrameNumber);
2874
2875}
2876
2877status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002878 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002879 if (lastFrameNumber != NULL) {
2880 *lastFrameNumber = mRepeatingLastFrameNumber;
2881 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002882 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002883 return OK;
2884}
2885
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002886status_t Camera3Device::RequestThread::clear(
2887 NotificationListener *listener,
2888 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002889 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002890 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002891
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002892 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002893
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002894 // Send errors for all requests pending in the request queue, including
2895 // pending repeating requests
2896 if (listener != NULL) {
2897 for (RequestList::iterator it = mRequestQueue.begin();
2898 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002899 // Abort the input buffers for reprocess requests.
2900 if ((*it)->mInputStream != NULL) {
2901 camera3_stream_buffer_t inputBuffer;
2902 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2903 if (res != OK) {
2904 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2905 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2906 } else {
2907 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2908 if (res != OK) {
2909 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2910 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2911 }
2912 }
2913 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002914 // Set the frame number this request would have had, if it
2915 // had been submitted; this frame number will not be reused.
2916 // The requestId and burstId fields were set when the request was
2917 // submitted originally (in convertMetadataListToRequestListLocked)
2918 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002919 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002920 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002921 }
2922 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002923 mRequestQueue.clear();
2924 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002925 if (lastFrameNumber != NULL) {
2926 *lastFrameNumber = mRepeatingLastFrameNumber;
2927 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002928 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002929 return OK;
2930}
2931
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002932status_t Camera3Device::RequestThread::flush() {
2933 ATRACE_CALL();
2934 Mutex::Autolock l(mFlushLock);
2935
2936 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2937 return mHal3Device->ops->flush(mHal3Device);
2938 }
2939
2940 return -ENOTSUP;
2941}
2942
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002943void Camera3Device::RequestThread::setPaused(bool paused) {
2944 Mutex::Autolock l(mPauseLock);
2945 mDoPause = paused;
2946 mDoPauseSignal.signal();
2947}
2948
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002949status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2950 int32_t requestId, nsecs_t timeout) {
2951 Mutex::Autolock l(mLatestRequestMutex);
2952 status_t res;
2953 while (mLatestRequestId != requestId) {
2954 nsecs_t startTime = systemTime();
2955
2956 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2957 if (res != OK) return res;
2958
2959 timeout -= (systemTime() - startTime);
2960 }
2961
2962 return OK;
2963}
2964
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002965void Camera3Device::RequestThread::requestExit() {
2966 // Call parent to set up shutdown
2967 Thread::requestExit();
2968 // The exit from any possible waits
2969 mDoPauseSignal.signal();
2970 mRequestSignal.signal();
2971}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002972
Chien-Yu Chend196d612015-06-22 19:49:01 -07002973
2974/**
2975 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2976 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2977 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2978 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2979 * request.
2980 */
2981void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2982 request->mAeTriggerCancelOverride.applyAeLock = false;
2983 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2984
2985 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2986 return;
2987 }
2988
2989 camera_metadata_entry_t aePrecaptureTrigger =
2990 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2991 if (aePrecaptureTrigger.count > 0 &&
2992 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2993 // Always override CANCEL to IDLE
2994 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2995 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2996 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2997 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2998 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2999
3000 if (mAeLockAvailable == true) {
3001 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3002 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3003 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3004 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3005 request->mAeTriggerCancelOverride.applyAeLock = true;
3006 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3007 }
3008 }
3009 }
3010}
3011
3012/**
3013 * Override result metadata for cancelling AE precapture trigger applied in
3014 * handleAePrecaptureCancelRequest().
3015 */
3016void Camera3Device::overrideResultForPrecaptureCancel(
3017 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3018 if (aeTriggerCancelOverride.applyAeLock) {
3019 // Only devices <= v3.2 should have this override
3020 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3021 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3022 }
3023
3024 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3025 // Only devices <= v3.2 should have this override
3026 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3027 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3028 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3029 }
3030}
3031
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003032void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003033 bool surfaceAbandoned = false;
3034 int64_t lastFrameNumber = 0;
3035 {
3036 Mutex::Autolock l(mRequestLock);
3037 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3038 // repeating requests.
3039 for (const auto& request : mRepeatingRequests) {
3040 for (const auto& s : request->mOutputStreams) {
3041 if (s->isAbandoned()) {
3042 surfaceAbandoned = true;
3043 clearRepeatingRequestsLocked(&lastFrameNumber);
3044 break;
3045 }
3046 }
3047 if (surfaceAbandoned) {
3048 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003049 }
3050 }
3051 }
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003052 if (surfaceAbandoned) {
3053 mListener->notifyRepeatingRequestError(lastFrameNumber);
3054 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003055}
3056
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003057bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003058 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003059 status_t res;
3060
3061 // Handle paused state.
3062 if (waitIfPaused()) {
3063 return true;
3064 }
3065
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003066 // Wait for the next batch of requests.
3067 waitForNextRequestBatch();
3068 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003069 return true;
3070 }
3071
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003072 // Get the latest request ID, if any
3073 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003074 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003075 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003076 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003077 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003078 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003079 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3080 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003081 }
3082
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003083 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003084 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003085 if (res == TIMED_OUT) {
3086 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003087 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003088 // Check if any stream is abandoned.
3089 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003090 return true;
3091 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003092 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003093 return false;
3094 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003095
Zhijun Hecc27e112013-10-03 16:12:43 -07003096 // Inform waitUntilRequestProcessed thread of a new request ID
3097 {
3098 Mutex::Autolock al(mLatestRequestMutex);
3099
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003100 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003101 mLatestRequestSignal.signal();
3102 }
3103
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003104 // Submit a batch of requests to HAL.
3105 // Use flush lock only when submitting multilple requests in a batch.
3106 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3107 // which may take a long time to finish so synchronizing flush() and
3108 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3109 // For now, only synchronize for high speed recording and we should figure something out for
3110 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003111 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003112
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003113 if (useFlushLock) {
3114 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003115 }
3116
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003117 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003118 mNextRequests.size());
3119 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003120 // Submit request and block until ready for next one
3121 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3122 ATRACE_BEGIN("camera3->process_capture_request");
3123 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3124 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003125
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003126 if (res != OK) {
3127 // Should only get a failure here for malformed requests or device-level
3128 // errors, so consider all errors fatal. Bad metadata failures should
3129 // come through notify.
3130 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3131 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3132 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003133 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003134 if (useFlushLock) {
3135 mFlushLock.unlock();
3136 }
3137 return false;
3138 }
3139
3140 // Mark that the request has be submitted successfully.
3141 nextRequest.submitted = true;
3142
3143 // Update the latest request sent to HAL
3144 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3145 Mutex::Autolock al(mLatestRequestMutex);
3146
3147 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3148 mLatestRequest.acquire(cloned);
3149 }
3150
3151 if (nextRequest.halRequest.settings != NULL) {
3152 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3153 }
3154
3155 // Remove any previously queued triggers (after unlock)
3156 res = removeTriggers(mPrevRequest);
3157 if (res != OK) {
3158 SET_ERR("RequestThread: Unable to remove triggers "
3159 "(capture request %d, HAL device: %s (%d)",
3160 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003161 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003162 if (useFlushLock) {
3163 mFlushLock.unlock();
3164 }
3165 return false;
3166 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003167 }
3168
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003169 if (useFlushLock) {
3170 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003171 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003172
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003173 // Unset as current request
3174 {
3175 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003176 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003177 }
3178
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003179 return true;
3180}
3181
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003182status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003183 ATRACE_CALL();
3184
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003185 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003186 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3187 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3188 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3189
3190 // Prepare a request to HAL
3191 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3192
3193 // Insert any queued triggers (before metadata is locked)
3194 status_t res = insertTriggers(captureRequest);
3195
3196 if (res < 0) {
3197 SET_ERR("RequestThread: Unable to insert triggers "
3198 "(capture request %d, HAL device: %s (%d)",
3199 halRequest->frame_number, strerror(-res), res);
3200 return INVALID_OPERATION;
3201 }
3202 int triggerCount = res;
3203 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3204 mPrevTriggers = triggerCount;
3205
3206 // If the request is the same as last, or we had triggers last time
3207 if (mPrevRequest != captureRequest || triggersMixedIn) {
3208 /**
3209 * HAL workaround:
3210 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3211 */
3212 res = addDummyTriggerIds(captureRequest);
3213 if (res != OK) {
3214 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3215 "(capture request %d, HAL device: %s (%d)",
3216 halRequest->frame_number, strerror(-res), res);
3217 return INVALID_OPERATION;
3218 }
3219
3220 /**
3221 * The request should be presorted so accesses in HAL
3222 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3223 */
3224 captureRequest->mSettings.sort();
3225 halRequest->settings = captureRequest->mSettings.getAndLock();
3226 mPrevRequest = captureRequest;
3227 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3228
3229 IF_ALOGV() {
3230 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3231 find_camera_metadata_ro_entry(
3232 halRequest->settings,
3233 ANDROID_CONTROL_AF_TRIGGER,
3234 &e
3235 );
3236 if (e.count > 0) {
3237 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3238 __FUNCTION__,
3239 halRequest->frame_number,
3240 e.data.u8[0]);
3241 }
3242 }
3243 } else {
3244 // leave request.settings NULL to indicate 'reuse latest given'
3245 ALOGVV("%s: Request settings are REUSED",
3246 __FUNCTION__);
3247 }
3248
3249 uint32_t totalNumBuffers = 0;
3250
3251 // Fill in buffers
3252 if (captureRequest->mInputStream != NULL) {
3253 halRequest->input_buffer = &captureRequest->mInputBuffer;
3254 totalNumBuffers += 1;
3255 } else {
3256 halRequest->input_buffer = NULL;
3257 }
3258
3259 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3260 captureRequest->mOutputStreams.size());
3261 halRequest->output_buffers = outputBuffers->array();
3262 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3263 res = captureRequest->mOutputStreams.editItemAt(i)->
3264 getBuffer(&outputBuffers->editItemAt(i));
3265 if (res != OK) {
3266 // Can't get output buffer from gralloc queue - this could be due to
3267 // abandoned queue or other consumer misbehavior, so not a fatal
3268 // error
3269 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3270 " %s (%d)", strerror(-res), res);
3271
3272 return TIMED_OUT;
3273 }
3274 halRequest->num_output_buffers++;
3275 }
3276 totalNumBuffers += halRequest->num_output_buffers;
3277
3278 // Log request in the in-flight queue
3279 sp<Camera3Device> parent = mParent.promote();
3280 if (parent == NULL) {
3281 // Should not happen, and nowhere to send errors to, so just log it
3282 CLOGE("RequestThread: Parent is gone");
3283 return INVALID_OPERATION;
3284 }
3285 res = parent->registerInFlight(halRequest->frame_number,
3286 totalNumBuffers, captureRequest->mResultExtras,
3287 /*hasInput*/halRequest->input_buffer != NULL,
3288 captureRequest->mAeTriggerCancelOverride);
3289 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3290 ", burstId = %" PRId32 ".",
3291 __FUNCTION__,
3292 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3293 captureRequest->mResultExtras.burstId);
3294 if (res != OK) {
3295 SET_ERR("RequestThread: Unable to register new in-flight request:"
3296 " %s (%d)", strerror(-res), res);
3297 return INVALID_OPERATION;
3298 }
3299 }
3300
3301 return OK;
3302}
3303
Igor Murashkin1e479c02013-09-06 16:55:14 -07003304CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3305 Mutex::Autolock al(mLatestRequestMutex);
3306
3307 ALOGV("RequestThread::%s", __FUNCTION__);
3308
3309 return mLatestRequest;
3310}
3311
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003312bool Camera3Device::RequestThread::isStreamPending(
3313 sp<Camera3StreamInterface>& stream) {
3314 Mutex::Autolock l(mRequestLock);
3315
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003316 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003317 if (!nextRequest.submitted) {
3318 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3319 if (stream == s) return true;
3320 }
3321 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003322 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003323 }
3324
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003325 for (const auto& request : mRequestQueue) {
3326 for (const auto& s : request->mOutputStreams) {
3327 if (stream == s) return true;
3328 }
3329 if (stream == request->mInputStream) return true;
3330 }
3331
3332 for (const auto& request : mRepeatingRequests) {
3333 for (const auto& s : request->mOutputStreams) {
3334 if (stream == s) return true;
3335 }
3336 if (stream == request->mInputStream) return true;
3337 }
3338
3339 return false;
3340}
Jianing Weicb0652e2014-03-12 18:29:36 -07003341
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003342void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3343 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003344 return;
3345 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003346
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003347 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003348 // Skip the ones that have been submitted successfully.
3349 if (nextRequest.submitted) {
3350 continue;
3351 }
3352
3353 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3354 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3355 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3356
3357 if (halRequest->settings != NULL) {
3358 captureRequest->mSettings.unlock(halRequest->settings);
3359 }
3360
3361 if (captureRequest->mInputStream != NULL) {
3362 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3363 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3364 }
3365
3366 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3367 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3368 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3369 }
3370
3371 if (sendRequestError) {
3372 Mutex::Autolock l(mRequestLock);
3373 if (mListener != NULL) {
3374 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003375 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003376 captureRequest->mResultExtras);
3377 }
3378 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003379 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003380
3381 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003382 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003383}
3384
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003385void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003386 // Optimized a bit for the simple steady-state case (single repeating
3387 // request), to avoid putting that request in the queue temporarily.
3388 Mutex::Autolock l(mRequestLock);
3389
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003390 assert(mNextRequests.empty());
3391
3392 NextRequest nextRequest;
3393 nextRequest.captureRequest = waitForNextRequestLocked();
3394 if (nextRequest.captureRequest == nullptr) {
3395 return;
3396 }
3397
3398 nextRequest.halRequest = camera3_capture_request_t();
3399 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003400 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003401
3402 // Wait for additional requests
3403 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3404
3405 for (size_t i = 1; i < batchSize; i++) {
3406 NextRequest additionalRequest;
3407 additionalRequest.captureRequest = waitForNextRequestLocked();
3408 if (additionalRequest.captureRequest == nullptr) {
3409 break;
3410 }
3411
3412 additionalRequest.halRequest = camera3_capture_request_t();
3413 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003414 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003415 }
3416
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003417 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003418 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003419 mNextRequests.size(), batchSize);
3420 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003421 }
3422
3423 return;
3424}
3425
3426sp<Camera3Device::CaptureRequest>
3427 Camera3Device::RequestThread::waitForNextRequestLocked() {
3428 status_t res;
3429 sp<CaptureRequest> nextRequest;
3430
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003431 while (mRequestQueue.empty()) {
3432 if (!mRepeatingRequests.empty()) {
3433 // Always atomically enqueue all requests in a repeating request
3434 // list. Guarantees a complete in-sequence set of captures to
3435 // application.
3436 const RequestList &requests = mRepeatingRequests;
3437 RequestList::const_iterator firstRequest =
3438 requests.begin();
3439 nextRequest = *firstRequest;
3440 mRequestQueue.insert(mRequestQueue.end(),
3441 ++firstRequest,
3442 requests.end());
3443 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003444
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003445 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003446
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003447 break;
3448 }
3449
3450 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3451
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003452 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3453 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003454 Mutex::Autolock pl(mPauseLock);
3455 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003456 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003457 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003458 // Let the tracker know
3459 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3460 if (statusTracker != 0) {
3461 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3462 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003463 }
3464 // Stop waiting for now and let thread management happen
3465 return NULL;
3466 }
3467 }
3468
3469 if (nextRequest == NULL) {
3470 // Don't have a repeating request already in hand, so queue
3471 // must have an entry now.
3472 RequestList::iterator firstRequest =
3473 mRequestQueue.begin();
3474 nextRequest = *firstRequest;
3475 mRequestQueue.erase(firstRequest);
3476 }
3477
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003478 // In case we've been unpaused by setPaused clearing mDoPause, need to
3479 // update internal pause state (capture/setRepeatingRequest unpause
3480 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003481 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003482 if (mPaused) {
3483 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3484 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3485 if (statusTracker != 0) {
3486 statusTracker->markComponentActive(mStatusId);
3487 }
3488 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003489 mPaused = false;
3490
3491 // Check if we've reconfigured since last time, and reset the preview
3492 // request if so. Can't use 'NULL request == repeat' across configure calls.
3493 if (mReconfigured) {
3494 mPrevRequest.clear();
3495 mReconfigured = false;
3496 }
3497
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003498 if (nextRequest != NULL) {
3499 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003500 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3501 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003502
3503 // Since RequestThread::clear() removes buffers from the input stream,
3504 // get the right buffer here before unlocking mRequestLock
3505 if (nextRequest->mInputStream != NULL) {
3506 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3507 if (res != OK) {
3508 // Can't get input buffer from gralloc queue - this could be due to
3509 // disconnected queue or other producer misbehavior, so not a fatal
3510 // error
3511 ALOGE("%s: Can't get input buffer, skipping request:"
3512 " %s (%d)", __FUNCTION__, strerror(-res), res);
3513 if (mListener != NULL) {
3514 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003515 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003516 nextRequest->mResultExtras);
3517 }
3518 return NULL;
3519 }
3520 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003521 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003522
3523 handleAePrecaptureCancelRequest(nextRequest);
3524
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003525 return nextRequest;
3526}
3527
3528bool Camera3Device::RequestThread::waitIfPaused() {
3529 status_t res;
3530 Mutex::Autolock l(mPauseLock);
3531 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003532 if (mPaused == false) {
3533 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003534 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3535 // Let the tracker know
3536 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3537 if (statusTracker != 0) {
3538 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3539 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003540 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003541
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003542 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003543 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003544 return true;
3545 }
3546 }
3547 // We don't set mPaused to false here, because waitForNextRequest needs
3548 // to further manage the paused state in case of starvation.
3549 return false;
3550}
3551
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003552void Camera3Device::RequestThread::unpauseForNewRequests() {
3553 // With work to do, mark thread as unpaused.
3554 // If paused by request (setPaused), don't resume, to avoid
3555 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003556 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003557 Mutex::Autolock p(mPauseLock);
3558 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003559 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3560 if (mPaused) {
3561 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3562 if (statusTracker != 0) {
3563 statusTracker->markComponentActive(mStatusId);
3564 }
3565 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003566 mPaused = false;
3567 }
3568}
3569
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003570void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3571 sp<Camera3Device> parent = mParent.promote();
3572 if (parent != NULL) {
3573 va_list args;
3574 va_start(args, fmt);
3575
3576 parent->setErrorStateV(fmt, args);
3577
3578 va_end(args);
3579 }
3580}
3581
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003582status_t Camera3Device::RequestThread::insertTriggers(
3583 const sp<CaptureRequest> &request) {
3584
3585 Mutex::Autolock al(mTriggerMutex);
3586
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003587 sp<Camera3Device> parent = mParent.promote();
3588 if (parent == NULL) {
3589 CLOGE("RequestThread: Parent is gone");
3590 return DEAD_OBJECT;
3591 }
3592
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003593 CameraMetadata &metadata = request->mSettings;
3594 size_t count = mTriggerMap.size();
3595
3596 for (size_t i = 0; i < count; ++i) {
3597 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003598 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003599
3600 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3601 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3602 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003603 if (isAeTrigger) {
3604 request->mResultExtras.precaptureTriggerId = triggerId;
3605 mCurrentPreCaptureTriggerId = triggerId;
3606 } else {
3607 request->mResultExtras.afTriggerId = triggerId;
3608 mCurrentAfTriggerId = triggerId;
3609 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003610 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3611 continue; // Trigger ID tag is deprecated since device HAL 3.2
3612 }
3613 }
3614
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003615 camera_metadata_entry entry = metadata.find(tag);
3616
3617 if (entry.count > 0) {
3618 /**
3619 * Already has an entry for this trigger in the request.
3620 * Rewrite it with our requested trigger value.
3621 */
3622 RequestTrigger oldTrigger = trigger;
3623
3624 oldTrigger.entryValue = entry.data.u8[0];
3625
3626 mTriggerReplacedMap.add(tag, oldTrigger);
3627 } else {
3628 /**
3629 * More typical, no trigger entry, so we just add it
3630 */
3631 mTriggerRemovedMap.add(tag, trigger);
3632 }
3633
3634 status_t res;
3635
3636 switch (trigger.getTagType()) {
3637 case TYPE_BYTE: {
3638 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3639 res = metadata.update(tag,
3640 &entryValue,
3641 /*count*/1);
3642 break;
3643 }
3644 case TYPE_INT32:
3645 res = metadata.update(tag,
3646 &trigger.entryValue,
3647 /*count*/1);
3648 break;
3649 default:
3650 ALOGE("%s: Type not supported: 0x%x",
3651 __FUNCTION__,
3652 trigger.getTagType());
3653 return INVALID_OPERATION;
3654 }
3655
3656 if (res != OK) {
3657 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3658 ", value %d", __FUNCTION__, trigger.getTagName(),
3659 trigger.entryValue);
3660 return res;
3661 }
3662
3663 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3664 trigger.getTagName(),
3665 trigger.entryValue);
3666 }
3667
3668 mTriggerMap.clear();
3669
3670 return count;
3671}
3672
3673status_t Camera3Device::RequestThread::removeTriggers(
3674 const sp<CaptureRequest> &request) {
3675 Mutex::Autolock al(mTriggerMutex);
3676
3677 CameraMetadata &metadata = request->mSettings;
3678
3679 /**
3680 * Replace all old entries with their old values.
3681 */
3682 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3683 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3684
3685 status_t res;
3686
3687 uint32_t tag = trigger.metadataTag;
3688 switch (trigger.getTagType()) {
3689 case TYPE_BYTE: {
3690 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3691 res = metadata.update(tag,
3692 &entryValue,
3693 /*count*/1);
3694 break;
3695 }
3696 case TYPE_INT32:
3697 res = metadata.update(tag,
3698 &trigger.entryValue,
3699 /*count*/1);
3700 break;
3701 default:
3702 ALOGE("%s: Type not supported: 0x%x",
3703 __FUNCTION__,
3704 trigger.getTagType());
3705 return INVALID_OPERATION;
3706 }
3707
3708 if (res != OK) {
3709 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3710 ", trigger value %d", __FUNCTION__,
3711 trigger.getTagName(), trigger.entryValue);
3712 return res;
3713 }
3714 }
3715 mTriggerReplacedMap.clear();
3716
3717 /**
3718 * Remove all new entries.
3719 */
3720 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3721 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3722 status_t res = metadata.erase(trigger.metadataTag);
3723
3724 if (res != OK) {
3725 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3726 ", trigger value %d", __FUNCTION__,
3727 trigger.getTagName(), trigger.entryValue);
3728 return res;
3729 }
3730 }
3731 mTriggerRemovedMap.clear();
3732
3733 return OK;
3734}
3735
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003736status_t Camera3Device::RequestThread::addDummyTriggerIds(
3737 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003738 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003739 static const int32_t dummyTriggerId = 1;
3740 status_t res;
3741
3742 CameraMetadata &metadata = request->mSettings;
3743
3744 // If AF trigger is active, insert a dummy AF trigger ID if none already
3745 // exists
3746 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3747 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3748 if (afTrigger.count > 0 &&
3749 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3750 afId.count == 0) {
3751 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3752 if (res != OK) return res;
3753 }
3754
3755 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3756 // if none already exists
3757 camera_metadata_entry pcTrigger =
3758 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3759 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3760 if (pcTrigger.count > 0 &&
3761 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3762 pcId.count == 0) {
3763 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3764 &dummyTriggerId, 1);
3765 if (res != OK) return res;
3766 }
3767
3768 return OK;
3769}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003770
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003771/**
3772 * PreparerThread inner class methods
3773 */
3774
3775Camera3Device::PreparerThread::PreparerThread() :
3776 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3777}
3778
3779Camera3Device::PreparerThread::~PreparerThread() {
3780 Thread::requestExitAndWait();
3781 if (mCurrentStream != nullptr) {
3782 mCurrentStream->cancelPrepare();
3783 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3784 mCurrentStream.clear();
3785 }
3786 clear();
3787}
3788
Ruben Brunkc78ac262015-08-13 17:58:46 -07003789status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003790 status_t res;
3791
3792 Mutex::Autolock l(mLock);
3793
Ruben Brunkc78ac262015-08-13 17:58:46 -07003794 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003795 if (res == OK) {
3796 // No preparation needed, fire listener right off
3797 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3798 if (mListener) {
3799 mListener->notifyPrepared(stream->getId());
3800 }
3801 return OK;
3802 } else if (res != NOT_ENOUGH_DATA) {
3803 return res;
3804 }
3805
3806 // Need to prepare, start up thread if necessary
3807 if (!mActive) {
3808 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3809 // isn't running
3810 Thread::requestExitAndWait();
3811 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3812 if (res != OK) {
3813 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3814 if (mListener) {
3815 mListener->notifyPrepared(stream->getId());
3816 }
3817 return res;
3818 }
3819 mCancelNow = false;
3820 mActive = true;
3821 ALOGV("%s: Preparer stream started", __FUNCTION__);
3822 }
3823
3824 // queue up the work
3825 mPendingStreams.push_back(stream);
3826 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3827
3828 return OK;
3829}
3830
3831status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003832 Mutex::Autolock l(mLock);
3833
3834 for (const auto& stream : mPendingStreams) {
3835 stream->cancelPrepare();
3836 }
3837 mPendingStreams.clear();
3838 mCancelNow = true;
3839
3840 return OK;
3841}
3842
3843void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3844 Mutex::Autolock l(mLock);
3845 mListener = listener;
3846}
3847
3848bool Camera3Device::PreparerThread::threadLoop() {
3849 status_t res;
3850 {
3851 Mutex::Autolock l(mLock);
3852 if (mCurrentStream == nullptr) {
3853 // End thread if done with work
3854 if (mPendingStreams.empty()) {
3855 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3856 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3857 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3858 mActive = false;
3859 return false;
3860 }
3861
3862 // Get next stream to prepare
3863 auto it = mPendingStreams.begin();
3864 mCurrentStream = *it;
3865 mPendingStreams.erase(it);
3866 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3867 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3868 } else if (mCancelNow) {
3869 mCurrentStream->cancelPrepare();
3870 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3871 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3872 mCurrentStream.clear();
3873 mCancelNow = false;
3874 return true;
3875 }
3876 }
3877
3878 res = mCurrentStream->prepareNextBuffer();
3879 if (res == NOT_ENOUGH_DATA) return true;
3880 if (res != OK) {
3881 // Something bad happened; try to recover by cancelling prepare and
3882 // signalling listener anyway
3883 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3884 mCurrentStream->getId(), res, strerror(-res));
3885 mCurrentStream->cancelPrepare();
3886 }
3887
3888 // This stream has finished, notify listener
3889 Mutex::Autolock l(mLock);
3890 if (mListener) {
3891 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3892 mCurrentStream->getId());
3893 mListener->notifyPrepared(mCurrentStream->getId());
3894 }
3895
3896 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3897 mCurrentStream.clear();
3898
3899 return true;
3900}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003901
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003902/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003903 * Static callback forwarding methods from HAL to instance
3904 */
3905
3906void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3907 const camera3_capture_result *result) {
3908 Camera3Device *d =
3909 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003910
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003911 d->processCaptureResult(result);
3912}
3913
3914void Camera3Device::sNotify(const camera3_callback_ops *cb,
3915 const camera3_notify_msg *msg) {
3916 Camera3Device *d =
3917 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3918 d->notify(msg);
3919}
3920
3921}; // namespace android