blob: 552259f8cce796190989f6746f769237b9001fb5 [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
203 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800204 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700205 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700206 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700207 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800208
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800209 // Measure the clock domain offset between camera and video/hw_composer
210 camera_metadata_entry timestampSource =
211 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
212 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
213 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
214 mTimestampOffset = getMonoToBoottimeOffset();
215 }
216
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700217 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700218 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
219 camera_metadata_entry partialResultsCount =
220 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
221 if (partialResultsCount.count > 0) {
222 mNumPartialResults = partialResultsCount.data.i32[0];
223 mUsePartialResult = (mNumPartialResults > 1);
224 }
225 } else {
226 camera_metadata_entry partialResultsQuirk =
227 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
228 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
229 mUsePartialResult = true;
230 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700231 }
232
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700233 camera_metadata_entry configs =
234 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
235 for (uint32_t i = 0; i < configs.count; i += 4) {
236 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
237 configs.data.i32[i + 3] ==
238 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
239 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
240 configs.data.i32[i + 2]));
241 }
242 }
243
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800244 return OK;
245}
246
247status_t Camera3Device::disconnect() {
248 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700249 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800250
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800251 ALOGV("%s: E", __FUNCTION__);
252
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700253 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800254
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700255 {
256 Mutex::Autolock l(mLock);
257 if (mStatus == STATUS_UNINITIALIZED) return res;
258
259 if (mStatus == STATUS_ACTIVE ||
260 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
261 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700262 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700263 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700264 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700265 } else {
266 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
267 if (res != OK) {
268 SET_ERR_L("Timeout waiting for HAL to drain");
269 // Continue to close device even in case of error
270 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700271 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800272 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800273
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700274 if (mStatus == STATUS_ERROR) {
275 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700276 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277
278 if (mStatusTracker != NULL) {
279 mStatusTracker->requestExit();
280 }
281
282 if (mRequestThread != NULL) {
283 mRequestThread->requestExit();
284 }
285
286 mOutputStreams.clear();
287 mInputStream.clear();
288 }
289
290 // Joining done without holding mLock, otherwise deadlocks may ensue
291 // as the threads try to access parent state
292 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
293 // HAL may be in a bad state, so waiting for request thread
294 // (which may be stuck in the HAL processCaptureRequest call)
295 // could be dangerous.
296 mRequestThread->join();
297 }
298
299 if (mStatusTracker != NULL) {
300 mStatusTracker->join();
301 }
302
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700303 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700304 {
305 Mutex::Autolock l(mLock);
306
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800307 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700308 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800309 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800310
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700311 hal3Device = mHal3Device;
312 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800313
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700314 // Call close without internal mutex held, as the HAL close may need to
315 // wait on assorted callbacks,etc, to complete before it can return.
316 if (hal3Device != NULL) {
317 ATRACE_BEGIN("camera3->close");
318 hal3Device->common.close(&hal3Device->common);
319 ATRACE_END();
320 }
321
322 {
323 Mutex::Autolock l(mLock);
324 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700325 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700326 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800327
328 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700329 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800330}
331
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700332// For dumping/debugging only -
333// try to acquire a lock a few times, eventually give up to proceed with
334// debug/dump operations
335bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
336 bool gotLock = false;
337 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
338 if (lock.tryLock() == NO_ERROR) {
339 gotLock = true;
340 break;
341 } else {
342 usleep(kDumpSleepDuration);
343 }
344 }
345 return gotLock;
346}
347
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700348Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
349 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
350 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
351 const int STREAM_CONFIGURATION_SIZE = 4;
352 const int STREAM_FORMAT_OFFSET = 0;
353 const int STREAM_WIDTH_OFFSET = 1;
354 const int STREAM_HEIGHT_OFFSET = 2;
355 const int STREAM_IS_INPUT_OFFSET = 3;
356 camera_metadata_ro_entry_t availableStreamConfigs =
357 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
358 if (availableStreamConfigs.count == 0 ||
359 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
360 return Size(0, 0);
361 }
362
363 // Get max jpeg size (area-wise).
364 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
365 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
366 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
367 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
368 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
369 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
370 && format == HAL_PIXEL_FORMAT_BLOB &&
371 (width * height > maxJpegWidth * maxJpegHeight)) {
372 maxJpegWidth = width;
373 maxJpegHeight = height;
374 }
375 }
376 } else {
377 camera_metadata_ro_entry availableJpegSizes =
378 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
379 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
380 return Size(0, 0);
381 }
382
383 // Get max jpeg size (area-wise).
384 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
385 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
386 > (maxJpegWidth * maxJpegHeight)) {
387 maxJpegWidth = availableJpegSizes.data.i32[i];
388 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
389 }
390 }
391 }
392 return Size(maxJpegWidth, maxJpegHeight);
393}
394
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800395nsecs_t Camera3Device::getMonoToBoottimeOffset() {
396 // try three times to get the clock offset, choose the one
397 // with the minimum gap in measurements.
398 const int tries = 3;
399 nsecs_t bestGap, measured;
400 for (int i = 0; i < tries; ++i) {
401 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
402 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
403 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
404 const nsecs_t gap = tmono2 - tmono;
405 if (i == 0 || gap < bestGap) {
406 bestGap = gap;
407 measured = tbase - ((tmono + tmono2) >> 1);
408 }
409 }
410 return measured;
411}
412
Zhijun Hef7da0962014-04-24 13:27:56 -0700413ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700414 // Get max jpeg size (area-wise).
415 Size maxJpegResolution = getMaxJpegResolution();
416 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700417 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700418 __FUNCTION__, mId);
419 return BAD_VALUE;
420 }
421
Zhijun Hef7da0962014-04-24 13:27:56 -0700422 // Get max jpeg buffer size
423 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700424 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
425 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700426 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
427 return BAD_VALUE;
428 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700429 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800430 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700431
432 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700433 float scaleFactor = ((float) (width * height)) /
434 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800435 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
436 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700437 if (jpegBufferSize > maxJpegBufferSize) {
438 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700439 }
440
441 return jpegBufferSize;
442}
443
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700444ssize_t Camera3Device::getPointCloudBufferSize() const {
445 const int FLOATS_PER_POINT=4;
446 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
447 if (maxPointCount.count == 0) {
448 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
449 __FUNCTION__, mId);
450 return BAD_VALUE;
451 }
452 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
453 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
454 return maxBytesForPointCloud;
455}
456
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800457ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800458 const int PER_CONFIGURATION_SIZE = 3;
459 const int WIDTH_OFFSET = 0;
460 const int HEIGHT_OFFSET = 1;
461 const int SIZE_OFFSET = 2;
462 camera_metadata_ro_entry rawOpaqueSizes =
463 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800464 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800465 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800466 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800467 __FUNCTION__, mId, count);
468 return BAD_VALUE;
469 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700470
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800471 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
472 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
473 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
474 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
475 }
476 }
477
478 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
479 __FUNCTION__, mId, width, height);
480 return BAD_VALUE;
481}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700482
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800483status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
484 ATRACE_CALL();
485 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700486
487 // Try to lock, but continue in case of failure (to avoid blocking in
488 // deadlocks)
489 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
490 bool gotLock = tryLockSpinRightRound(mLock);
491
492 ALOGW_IF(!gotInterfaceLock,
493 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
494 mId, __FUNCTION__);
495 ALOGW_IF(!gotLock,
496 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
497 mId, __FUNCTION__);
498
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800499 bool dumpTemplates = false;
500 String16 templatesOption("-t");
501 int n = args.size();
502 for (int i = 0; i < n; i++) {
503 if (args[i] == templatesOption) {
504 dumpTemplates = true;
505 }
506 }
507
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800508 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800509
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800510 const char *status =
511 mStatus == STATUS_ERROR ? "ERROR" :
512 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700513 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
514 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800515 mStatus == STATUS_ACTIVE ? "ACTIVE" :
516 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700517
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800518 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700519 if (mStatus == STATUS_ERROR) {
520 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
521 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800522 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700523 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700524 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800525
526 if (mInputStream != NULL) {
527 write(fd, lines.string(), lines.size());
528 mInputStream->dump(fd, args);
529 } else {
530 lines.appendFormat(" No input stream.\n");
531 write(fd, lines.string(), lines.size());
532 }
533 for (size_t i = 0; i < mOutputStreams.size(); i++) {
534 mOutputStreams[i]->dump(fd,args);
535 }
536
Zhijun He125684a2015-12-26 15:07:30 -0800537 lines = String8(" Camera3 Buffer Manager:\n");
538 write(fd, lines.string(), lines.size());
539 mBufferManager->dump(fd, args);
540
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700541 lines = String8(" In-flight requests:\n");
542 if (mInFlightMap.size() == 0) {
543 lines.append(" None\n");
544 } else {
545 for (size_t i = 0; i < mInFlightMap.size(); i++) {
546 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700547 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700548 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800549 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700550 r.numBuffersLeft);
551 }
552 }
553 write(fd, lines.string(), lines.size());
554
Igor Murashkin1e479c02013-09-06 16:55:14 -0700555 {
556 lines = String8(" Last request sent:\n");
557 write(fd, lines.string(), lines.size());
558
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700559 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700560 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
561 }
562
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800563 if (dumpTemplates) {
564 const char *templateNames[] = {
565 "TEMPLATE_PREVIEW",
566 "TEMPLATE_STILL_CAPTURE",
567 "TEMPLATE_VIDEO_RECORD",
568 "TEMPLATE_VIDEO_SNAPSHOT",
569 "TEMPLATE_ZERO_SHUTTER_LAG",
570 "TEMPLATE_MANUAL"
571 };
572
573 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
574 const camera_metadata_t *templateRequest;
575 templateRequest =
576 mHal3Device->ops->construct_default_request_settings(
577 mHal3Device, i);
578 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
579 if (templateRequest == NULL) {
580 lines.append(" Not supported\n");
581 write(fd, lines.string(), lines.size());
582 } else {
583 write(fd, lines.string(), lines.size());
584 dump_indented_camera_metadata(templateRequest,
585 fd, /*verbosity*/2, /*indentation*/8);
586 }
587 }
588 }
589
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800590 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700591 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800592 write(fd, lines.string(), lines.size());
593 mHal3Device->ops->dump(mHal3Device, fd);
594 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800595
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700596 if (gotLock) mLock.unlock();
597 if (gotInterfaceLock) mInterfaceLock.unlock();
598
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800599 return OK;
600}
601
602const CameraMetadata& Camera3Device::info() const {
603 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800604 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
605 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700606 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800607 mStatus == STATUS_ERROR ?
608 "when in error state" : "before init");
609 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800610 return mDeviceInfo;
611}
612
Jianing Wei90e59c92014-03-12 18:29:36 -0700613status_t Camera3Device::checkStatusOkToCaptureLocked() {
614 switch (mStatus) {
615 case STATUS_ERROR:
616 CLOGE("Device has encountered a serious error");
617 return INVALID_OPERATION;
618 case STATUS_UNINITIALIZED:
619 CLOGE("Device not initialized");
620 return INVALID_OPERATION;
621 case STATUS_UNCONFIGURED:
622 case STATUS_CONFIGURED:
623 case STATUS_ACTIVE:
624 // OK
625 break;
626 default:
627 SET_ERR_L("Unexpected status: %d", mStatus);
628 return INVALID_OPERATION;
629 }
630 return OK;
631}
632
633status_t Camera3Device::convertMetadataListToRequestListLocked(
634 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
635 if (requestList == NULL) {
636 CLOGE("requestList cannot be NULL.");
637 return BAD_VALUE;
638 }
639
Jianing Weicb0652e2014-03-12 18:29:36 -0700640 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700641 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
642 it != metadataList.end(); ++it) {
643 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
644 if (newRequest == 0) {
645 CLOGE("Can't create capture request");
646 return BAD_VALUE;
647 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700648
649 // Setup burst Id and request Id
650 newRequest->mResultExtras.burstId = burstId++;
651 if (it->exists(ANDROID_REQUEST_ID)) {
652 if (it->find(ANDROID_REQUEST_ID).count == 0) {
653 CLOGE("RequestID entry exists; but must not be empty in metadata");
654 return BAD_VALUE;
655 }
656 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
657 } else {
658 CLOGE("RequestID does not exist in metadata");
659 return BAD_VALUE;
660 }
661
Jianing Wei90e59c92014-03-12 18:29:36 -0700662 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700663
664 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700665 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700666
667 // Setup batch size if this is a high speed video recording request.
668 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
669 auto firstRequest = requestList->begin();
670 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
671 if (outputStream->isVideoStream()) {
672 (*firstRequest)->mBatchSize = requestList->size();
673 break;
674 }
675 }
676 }
677
Jianing Wei90e59c92014-03-12 18:29:36 -0700678 return OK;
679}
680
Jianing Weicb0652e2014-03-12 18:29:36 -0700681status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800682 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800683
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700684 List<const CameraMetadata> requests;
685 requests.push_back(request);
686 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800687}
688
Jianing Wei90e59c92014-03-12 18:29:36 -0700689status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700690 const List<const CameraMetadata> &requests, bool repeating,
691 /*out*/
692 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700693 ATRACE_CALL();
694 Mutex::Autolock il(mInterfaceLock);
695 Mutex::Autolock l(mLock);
696
697 status_t res = checkStatusOkToCaptureLocked();
698 if (res != OK) {
699 // error logged by previous call
700 return res;
701 }
702
703 RequestList requestList;
704
705 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
706 if (res != OK) {
707 // error logged by previous call
708 return res;
709 }
710
711 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700712 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700713 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700714 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700715 }
716
717 if (res == OK) {
718 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
719 if (res != OK) {
720 SET_ERR_L("Can't transition to active in %f seconds!",
721 kActiveTimeout/1e9);
722 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700723 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
724 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700725 } else {
726 CLOGE("Cannot queue request. Impossible.");
727 return BAD_VALUE;
728 }
729
730 return res;
731}
732
Jianing Weicb0652e2014-03-12 18:29:36 -0700733status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
734 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700735 ATRACE_CALL();
736
Jianing Weicb0652e2014-03-12 18:29:36 -0700737 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700738}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800739
Jianing Weicb0652e2014-03-12 18:29:36 -0700740status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
741 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800742 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800743
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700744 List<const CameraMetadata> requests;
745 requests.push_back(request);
746 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800747}
748
Jianing Weicb0652e2014-03-12 18:29:36 -0700749status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
750 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700751 ATRACE_CALL();
752
Jianing Weicb0652e2014-03-12 18:29:36 -0700753 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700754}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800755
756sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
757 const CameraMetadata &request) {
758 status_t res;
759
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700760 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800761 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700762 // Stream configuration failed due to unsupported configuration.
763 // Device back to unconfigured state. Client might try other configuraitons
764 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
765 CLOGE("No streams configured");
766 return NULL;
767 }
768 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800769 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700770 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800771 return NULL;
772 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700773 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700774 if (mStatus == STATUS_UNCONFIGURED) {
775 CLOGE("No streams configured");
776 return NULL;
777 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800778 }
779
780 sp<CaptureRequest> newRequest = createCaptureRequest(request);
781 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800782}
783
Jianing Weicb0652e2014-03-12 18:29:36 -0700784status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800785 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700786 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800787 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800788
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800789 switch (mStatus) {
790 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700791 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800792 return INVALID_OPERATION;
793 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700794 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800795 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700796 case STATUS_UNCONFIGURED:
797 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800798 case STATUS_ACTIVE:
799 // OK
800 break;
801 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700802 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800803 return INVALID_OPERATION;
804 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700805 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700806
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700807 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800808}
809
810status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
811 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700812 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800813
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700814 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800815}
816
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700817status_t Camera3Device::createInputStream(
818 uint32_t width, uint32_t height, int format, int *id) {
819 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700820 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700821 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700822 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
823 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700824
825 status_t res;
826 bool wasActive = false;
827
828 switch (mStatus) {
829 case STATUS_ERROR:
830 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
831 return INVALID_OPERATION;
832 case STATUS_UNINITIALIZED:
833 ALOGE("%s: Device not initialized", __FUNCTION__);
834 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700835 case STATUS_UNCONFIGURED:
836 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700837 // OK
838 break;
839 case STATUS_ACTIVE:
840 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700841 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700842 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700843 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700844 return res;
845 }
846 wasActive = true;
847 break;
848 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700849 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700850 return INVALID_OPERATION;
851 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700852 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700853
854 if (mInputStream != 0) {
855 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
856 return INVALID_OPERATION;
857 }
858
859 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
860 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700861 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700862
863 mInputStream = newStream;
864
865 *id = mNextStreamId++;
866
867 // Continue captures if active at start
868 if (wasActive) {
869 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
870 res = configureStreamsLocked();
871 if (res != OK) {
872 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
873 __FUNCTION__, mNextStreamId, strerror(-res), res);
874 return res;
875 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700876 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700877 }
878
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700879 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700880 return OK;
881}
882
Igor Murashkin2fba5842013-04-22 14:03:54 -0700883
884status_t Camera3Device::createZslStream(
885 uint32_t width, uint32_t height,
886 int depth,
887 /*out*/
888 int *id,
889 sp<Camera3ZslStream>* zslStream) {
890 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700891 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700892 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700893 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
894 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700895
896 status_t res;
897 bool wasActive = false;
898
899 switch (mStatus) {
900 case STATUS_ERROR:
901 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
902 return INVALID_OPERATION;
903 case STATUS_UNINITIALIZED:
904 ALOGE("%s: Device not initialized", __FUNCTION__);
905 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700906 case STATUS_UNCONFIGURED:
907 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700908 // OK
909 break;
910 case STATUS_ACTIVE:
911 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700912 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700913 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700914 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700915 return res;
916 }
917 wasActive = true;
918 break;
919 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700920 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700921 return INVALID_OPERATION;
922 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700923 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700924
925 if (mInputStream != 0) {
926 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
927 return INVALID_OPERATION;
928 }
929
930 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
931 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700932 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700933
934 res = mOutputStreams.add(mNextStreamId, newStream);
935 if (res < 0) {
936 ALOGE("%s: Can't add new stream to set: %s (%d)",
937 __FUNCTION__, strerror(-res), res);
938 return res;
939 }
940 mInputStream = newStream;
941
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530942 mNeedConfig = true;
943
Igor Murashkin2fba5842013-04-22 14:03:54 -0700944 *id = mNextStreamId++;
945 *zslStream = newStream;
946
947 // Continue captures if active at start
948 if (wasActive) {
949 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
950 res = configureStreamsLocked();
951 if (res != OK) {
952 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
953 __FUNCTION__, mNextStreamId, strerror(-res), res);
954 return res;
955 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700956 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700957 }
958
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700959 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700960 return OK;
961}
962
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700963status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800964 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -0800965 camera3_stream_rotation_t rotation, int *id, int streamSetId) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800966 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700967 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800968 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700969 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
970 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800971
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800972 status_t res;
973 bool wasActive = false;
974
975 switch (mStatus) {
976 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700977 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800978 return INVALID_OPERATION;
979 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700980 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800981 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700982 case STATUS_UNCONFIGURED:
983 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800984 // OK
985 break;
986 case STATUS_ACTIVE:
987 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700988 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800989 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700990 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800991 return res;
992 }
993 wasActive = true;
994 break;
995 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700996 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800997 return INVALID_OPERATION;
998 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700999 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001000
1001 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001002 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001003 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001004 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001005 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1006 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001007 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001008 ssize_t blobBufferSize;
1009 if (dataSpace != HAL_DATASPACE_DEPTH) {
1010 blobBufferSize = getJpegBufferSize(width, height);
1011 if (blobBufferSize <= 0) {
1012 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1013 return BAD_VALUE;
1014 }
1015 } else {
1016 blobBufferSize = getPointCloudBufferSize();
1017 if (blobBufferSize <= 0) {
1018 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1019 return BAD_VALUE;
1020 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001021 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001022 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001023 width, height, blobBufferSize, format, dataSpace, rotation,
1024 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001025 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1026 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1027 if (rawOpaqueBufferSize <= 0) {
1028 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1029 return BAD_VALUE;
1030 }
1031 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001032 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1033 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001034 } else {
1035 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001036 width, height, format, dataSpace, rotation,
1037 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001038 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001039 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001040
Zhijun He125684a2015-12-26 15:07:30 -08001041 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001042 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1043 * requires buffers to be statically allocated for internal static buffer registration, while
1044 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1045 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001046 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001047 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001048 newStream->setBufferManager(mBufferManager);
1049 }
1050
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001051 res = mOutputStreams.add(mNextStreamId, newStream);
1052 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001053 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001054 return res;
1055 }
1056
1057 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001058 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001059
1060 // Continue captures if active at start
1061 if (wasActive) {
1062 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1063 res = configureStreamsLocked();
1064 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001065 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1066 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001067 return res;
1068 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001069 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001070 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001071 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001072 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001073}
1074
1075status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1076 ATRACE_CALL();
1077 (void)outputId; (void)id;
1078
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001079 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001080 return INVALID_OPERATION;
1081}
1082
1083
1084status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001085 uint32_t *width, uint32_t *height,
1086 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001087 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001088 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001089 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001090
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001091 switch (mStatus) {
1092 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001093 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001094 return INVALID_OPERATION;
1095 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001096 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001097 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001098 case STATUS_UNCONFIGURED:
1099 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001100 case STATUS_ACTIVE:
1101 // OK
1102 break;
1103 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001104 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001105 return INVALID_OPERATION;
1106 }
1107
1108 ssize_t idx = mOutputStreams.indexOfKey(id);
1109 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001110 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001111 return idx;
1112 }
1113
1114 if (width) *width = mOutputStreams[idx]->getWidth();
1115 if (height) *height = mOutputStreams[idx]->getHeight();
1116 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001117 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001118 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001119}
1120
1121status_t Camera3Device::setStreamTransform(int id,
1122 int transform) {
1123 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001124 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001125 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001126
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001127 switch (mStatus) {
1128 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001129 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001130 return INVALID_OPERATION;
1131 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001132 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001133 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001134 case STATUS_UNCONFIGURED:
1135 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001136 case STATUS_ACTIVE:
1137 // OK
1138 break;
1139 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001140 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141 return INVALID_OPERATION;
1142 }
1143
1144 ssize_t idx = mOutputStreams.indexOfKey(id);
1145 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001146 CLOGE("Stream %d does not exist",
1147 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001148 return BAD_VALUE;
1149 }
1150
1151 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001152}
1153
1154status_t Camera3Device::deleteStream(int id) {
1155 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001156 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001157 Mutex::Autolock l(mLock);
1158 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001159
Igor Murashkine2172be2013-05-28 15:31:39 -07001160 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1161
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001162 // CameraDevice semantics require device to already be idle before
1163 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001164 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001165 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1166 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001167 }
1168
Igor Murashkin2fba5842013-04-22 14:03:54 -07001169 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001170 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001171 if (mInputStream != NULL && id == mInputStream->getId()) {
1172 deletedStream = mInputStream;
1173 mInputStream.clear();
1174 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001175 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001176 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177 return BAD_VALUE;
1178 }
Zhijun He5f446352014-01-22 09:49:33 -08001179 }
1180
1181 // Delete output stream or the output part of a bi-directional stream.
1182 if (outputStreamIdx != NAME_NOT_FOUND) {
1183 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001184 mOutputStreams.removeItem(id);
1185 }
1186
1187 // Free up the stream endpoint so that it can be used by some other stream
1188 res = deletedStream->disconnect();
1189 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001190 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001191 // fall through since we want to still list the stream as deleted.
1192 }
1193 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001194 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001195
1196 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001197}
1198
1199status_t Camera3Device::deleteReprocessStream(int id) {
1200 ATRACE_CALL();
1201 (void)id;
1202
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001203 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001204 return INVALID_OPERATION;
1205}
1206
Zhijun He1fa89992015-06-01 15:44:31 -07001207status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001208 ATRACE_CALL();
1209 ALOGV("%s: E", __FUNCTION__);
1210
1211 Mutex::Autolock il(mInterfaceLock);
1212 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001213
1214 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1215 mNeedConfig = true;
1216 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1217 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001218
1219 return configureStreamsLocked();
1220}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001221
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001222status_t Camera3Device::getInputBufferProducer(
1223 sp<IGraphicBufferProducer> *producer) {
1224 Mutex::Autolock il(mInterfaceLock);
1225 Mutex::Autolock l(mLock);
1226
1227 if (producer == NULL) {
1228 return BAD_VALUE;
1229 } else if (mInputStream == NULL) {
1230 return INVALID_OPERATION;
1231 }
1232
1233 return mInputStream->getInputBufferProducer(producer);
1234}
1235
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001236status_t Camera3Device::createDefaultRequest(int templateId,
1237 CameraMetadata *request) {
1238 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001239 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001240
1241 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1242 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1243 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1244 return BAD_VALUE;
1245 }
1246
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001247 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001248 Mutex::Autolock l(mLock);
1249
1250 switch (mStatus) {
1251 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001252 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001253 return INVALID_OPERATION;
1254 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001255 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001256 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001257 case STATUS_UNCONFIGURED:
1258 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001259 case STATUS_ACTIVE:
1260 // OK
1261 break;
1262 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001263 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001264 return INVALID_OPERATION;
1265 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001266
Zhijun Hea1530f12014-09-14 12:44:20 -07001267 if (!mRequestTemplateCache[templateId].isEmpty()) {
1268 *request = mRequestTemplateCache[templateId];
1269 return OK;
1270 }
1271
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001272 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001273 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001274 rawRequest = mHal3Device->ops->construct_default_request_settings(
1275 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001276 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001277 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001278 ALOGI("%s: template %d is not supported on this camera device",
1279 __FUNCTION__, templateId);
1280 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001281 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001282 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001283 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001284
1285 return OK;
1286}
1287
1288status_t Camera3Device::waitUntilDrained() {
1289 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001290 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001291 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001292
Zhijun He69a37482014-03-23 18:44:49 -07001293 return waitUntilDrainedLocked();
1294}
1295
1296status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001297 switch (mStatus) {
1298 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001299 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001300 ALOGV("%s: Already idle", __FUNCTION__);
1301 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001302 case STATUS_CONFIGURED:
1303 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001304 case STATUS_ERROR:
1305 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001306 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001307 break;
1308 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001309 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001310 return INVALID_OPERATION;
1311 }
1312
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001313 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1314 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001315 if (res != OK) {
1316 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1317 res);
1318 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001319 return res;
1320}
1321
Ruben Brunk183f0562015-08-12 12:55:02 -07001322
1323void Camera3Device::internalUpdateStatusLocked(Status status) {
1324 mStatus = status;
1325 mRecentStatusUpdates.add(mStatus);
1326 mStatusChanged.broadcast();
1327}
1328
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001329// Pause to reconfigure
1330status_t Camera3Device::internalPauseAndWaitLocked() {
1331 mRequestThread->setPaused(true);
1332 mPauseStateNotify = true;
1333
1334 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1335 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1336 if (res != OK) {
1337 SET_ERR_L("Can't idle device in %f seconds!",
1338 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001339 }
1340
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001341 return res;
1342}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001343
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001344// Resume after internalPauseAndWaitLocked
1345status_t Camera3Device::internalResumeLocked() {
1346 status_t res;
1347
1348 mRequestThread->setPaused(false);
1349
1350 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1351 if (res != OK) {
1352 SET_ERR_L("Can't transition to active in %f seconds!",
1353 kActiveTimeout/1e9);
1354 }
1355 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001356 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001357}
1358
Ruben Brunk183f0562015-08-12 12:55:02 -07001359status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001360 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001361
1362 size_t startIndex = 0;
1363 if (mStatusWaiters == 0) {
1364 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1365 // this status list
1366 mRecentStatusUpdates.clear();
1367 } else {
1368 // If other threads are waiting on updates to this status list, set the position of the
1369 // first element that this list will check rather than clearing the list.
1370 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001371 }
1372
Ruben Brunk183f0562015-08-12 12:55:02 -07001373 mStatusWaiters++;
1374
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001375 bool stateSeen = false;
1376 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001377 if (active == (mStatus == STATUS_ACTIVE)) {
1378 // Desired state is current
1379 break;
1380 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001381
1382 res = mStatusChanged.waitRelative(mLock, timeout);
1383 if (res != OK) break;
1384
Ruben Brunk183f0562015-08-12 12:55:02 -07001385 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1386 // transitions.
1387 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1388 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1389 __FUNCTION__);
1390
1391 // Encountered desired state since we began waiting
1392 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001393 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1394 stateSeen = true;
1395 break;
1396 }
1397 }
1398 } while (!stateSeen);
1399
Ruben Brunk183f0562015-08-12 12:55:02 -07001400 mStatusWaiters--;
1401
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001402 return res;
1403}
1404
1405
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001406status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1407 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001408 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001409
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001410 if (listener != NULL && mListener != NULL) {
1411 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1412 }
1413 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001414 mRequestThread->setNotificationListener(listener);
1415 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001416
1417 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001418}
1419
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001420bool Camera3Device::willNotify3A() {
1421 return false;
1422}
1423
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001424status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001425 status_t res;
1426 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001427
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001428 while (mResultQueue.empty()) {
1429 res = mResultSignal.waitRelative(mOutputLock, timeout);
1430 if (res == TIMED_OUT) {
1431 return res;
1432 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001433 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001434 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001435 return res;
1436 }
1437 }
1438 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001439}
1440
Jianing Weicb0652e2014-03-12 18:29:36 -07001441status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001442 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001443 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001444
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001445 if (mResultQueue.empty()) {
1446 return NOT_ENOUGH_DATA;
1447 }
1448
Jianing Weicb0652e2014-03-12 18:29:36 -07001449 if (frame == NULL) {
1450 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1451 return BAD_VALUE;
1452 }
1453
1454 CaptureResult &result = *(mResultQueue.begin());
1455 frame->mResultExtras = result.mResultExtras;
1456 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001457 mResultQueue.erase(mResultQueue.begin());
1458
1459 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001460}
1461
1462status_t Camera3Device::triggerAutofocus(uint32_t id) {
1463 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001464 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001465
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001466 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1467 // Mix-in this trigger into the next request and only the next request.
1468 RequestTrigger trigger[] = {
1469 {
1470 ANDROID_CONTROL_AF_TRIGGER,
1471 ANDROID_CONTROL_AF_TRIGGER_START
1472 },
1473 {
1474 ANDROID_CONTROL_AF_TRIGGER_ID,
1475 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001476 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001477 };
1478
1479 return mRequestThread->queueTrigger(trigger,
1480 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001481}
1482
1483status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1484 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001485 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001486
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001487 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1488 // Mix-in this trigger into the next request and only the next request.
1489 RequestTrigger trigger[] = {
1490 {
1491 ANDROID_CONTROL_AF_TRIGGER,
1492 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1493 },
1494 {
1495 ANDROID_CONTROL_AF_TRIGGER_ID,
1496 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001497 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001498 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001499
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001500 return mRequestThread->queueTrigger(trigger,
1501 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001502}
1503
1504status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1505 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001506 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001507
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001508 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1509 // Mix-in this trigger into the next request and only the next request.
1510 RequestTrigger trigger[] = {
1511 {
1512 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1513 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1514 },
1515 {
1516 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1517 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001518 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001519 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001520
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001521 return mRequestThread->queueTrigger(trigger,
1522 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001523}
1524
1525status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1526 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1527 ATRACE_CALL();
1528 (void)reprocessStreamId; (void)buffer; (void)listener;
1529
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001530 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001531 return INVALID_OPERATION;
1532}
1533
Jianing Weicb0652e2014-03-12 18:29:36 -07001534status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001535 ATRACE_CALL();
1536 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001537 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001538
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001539 NotificationListener* listener;
1540 {
1541 Mutex::Autolock l(mOutputLock);
1542 listener = mListener;
1543 }
1544
Zhijun He7ef20392014-04-21 16:04:17 -07001545 {
1546 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001547 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001548 }
1549
Zhijun He491e3412013-12-27 10:57:44 -08001550 status_t res;
1551 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001552 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001553 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001554 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001555 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001556 }
1557
1558 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001559}
1560
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001561status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001562 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1563}
1564
1565status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001566 ATRACE_CALL();
1567 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001568 Mutex::Autolock il(mInterfaceLock);
1569 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001570
1571 sp<Camera3StreamInterface> stream;
1572 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1573 if (outputStreamIdx == NAME_NOT_FOUND) {
1574 CLOGE("Stream %d does not exist", streamId);
1575 return BAD_VALUE;
1576 }
1577
1578 stream = mOutputStreams.editValueAt(outputStreamIdx);
1579
1580 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001581 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001582 return BAD_VALUE;
1583 }
1584
1585 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001586 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001587 return BAD_VALUE;
1588 }
1589
Ruben Brunkc78ac262015-08-13 17:58:46 -07001590 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001591}
1592
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001593status_t Camera3Device::tearDown(int streamId) {
1594 ATRACE_CALL();
1595 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1596 Mutex::Autolock il(mInterfaceLock);
1597 Mutex::Autolock l(mLock);
1598
1599 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1600 // since we cannot call register_stream_buffers except right after configure_streams.
1601 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1602 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1603 __FUNCTION__, mHal3Device->common.version);
1604 return NO_INIT;
1605 }
1606
1607 sp<Camera3StreamInterface> stream;
1608 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1609 if (outputStreamIdx == NAME_NOT_FOUND) {
1610 CLOGE("Stream %d does not exist", streamId);
1611 return BAD_VALUE;
1612 }
1613
1614 stream = mOutputStreams.editValueAt(outputStreamIdx);
1615
1616 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1617 CLOGE("Stream %d is a target of a in-progress request", streamId);
1618 return BAD_VALUE;
1619 }
1620
1621 return stream->tearDown();
1622}
1623
Zhijun He204e3292014-07-14 17:09:23 -07001624uint32_t Camera3Device::getDeviceVersion() {
1625 ATRACE_CALL();
1626 Mutex::Autolock il(mInterfaceLock);
1627 return mDeviceVersion;
1628}
1629
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001630/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001631 * Methods called by subclasses
1632 */
1633
1634void Camera3Device::notifyStatus(bool idle) {
1635 {
1636 // Need mLock to safely update state and synchronize to current
1637 // state of methods in flight.
1638 Mutex::Autolock l(mLock);
1639 // We can get various system-idle notices from the status tracker
1640 // while starting up. Only care about them if we've actually sent
1641 // in some requests recently.
1642 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1643 return;
1644 }
1645 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1646 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001647 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001648
1649 // Skip notifying listener if we're doing some user-transparent
1650 // state changes
1651 if (mPauseStateNotify) return;
1652 }
1653 NotificationListener *listener;
1654 {
1655 Mutex::Autolock l(mOutputLock);
1656 listener = mListener;
1657 }
1658 if (idle && listener != NULL) {
1659 listener->notifyIdle();
1660 }
1661}
1662
1663/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001664 * Camera3Device private methods
1665 */
1666
1667sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1668 const CameraMetadata &request) {
1669 ATRACE_CALL();
1670 status_t res;
1671
1672 sp<CaptureRequest> newRequest = new CaptureRequest;
1673 newRequest->mSettings = request;
1674
1675 camera_metadata_entry_t inputStreams =
1676 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1677 if (inputStreams.count > 0) {
1678 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001679 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001680 CLOGE("Request references unknown input stream %d",
1681 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001682 return NULL;
1683 }
1684 // Lazy completion of stream configuration (allocation/registration)
1685 // on first use
1686 if (mInputStream->isConfiguring()) {
1687 res = mInputStream->finishConfiguration(mHal3Device);
1688 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001689 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001690 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001691 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001692 return NULL;
1693 }
1694 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001695 // Check if stream is being prepared
1696 if (mInputStream->isPreparing()) {
1697 CLOGE("Request references an input stream that's being prepared!");
1698 return NULL;
1699 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001700
1701 newRequest->mInputStream = mInputStream;
1702 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1703 }
1704
1705 camera_metadata_entry_t streams =
1706 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1707 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001708 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001709 return NULL;
1710 }
1711
1712 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001713 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001714 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001715 CLOGE("Request references unknown stream %d",
1716 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001717 return NULL;
1718 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001719 sp<Camera3OutputStreamInterface> stream =
1720 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001721
1722 // Lazy completion of stream configuration (allocation/registration)
1723 // on first use
1724 if (stream->isConfiguring()) {
1725 res = stream->finishConfiguration(mHal3Device);
1726 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001727 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1728 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001729 return NULL;
1730 }
1731 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001732 // Check if stream is being prepared
1733 if (stream->isPreparing()) {
1734 CLOGE("Request references an output stream that's being prepared!");
1735 return NULL;
1736 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001737
1738 newRequest->mOutputStreams.push(stream);
1739 }
1740 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001741 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001742
1743 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001744}
1745
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001746bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1747 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1748 Size size = mSupportedOpaqueInputSizes[i];
1749 if (size.width == width && size.height == height) {
1750 return true;
1751 }
1752 }
1753
1754 return false;
1755}
1756
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001757status_t Camera3Device::configureStreamsLocked() {
1758 ATRACE_CALL();
1759 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001760
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001761 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001762 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001763 return INVALID_OPERATION;
1764 }
1765
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001766 if (!mNeedConfig) {
1767 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1768 return OK;
1769 }
1770
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001771 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1772 // adding a dummy stream instead.
1773 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1774 if (mOutputStreams.size() == 0) {
1775 addDummyStreamLocked();
1776 } else {
1777 tryRemoveDummyStreamLocked();
1778 }
1779
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001780 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001781 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001782
1783 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001784 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1785 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1786 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001787 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1788
1789 Vector<camera3_stream_t*> streams;
1790 streams.setCapacity(config.num_streams);
1791
1792 if (mInputStream != NULL) {
1793 camera3_stream_t *inputStream;
1794 inputStream = mInputStream->startConfiguration();
1795 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001796 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001797 return INVALID_OPERATION;
1798 }
1799 streams.add(inputStream);
1800 }
1801
1802 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001803
1804 // Don't configure bidi streams twice, nor add them twice to the list
1805 if (mOutputStreams[i].get() ==
1806 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1807
1808 config.num_streams--;
1809 continue;
1810 }
1811
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001812 camera3_stream_t *outputStream;
1813 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1814 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001815 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001816 return INVALID_OPERATION;
1817 }
1818 streams.add(outputStream);
1819 }
1820
1821 config.streams = streams.editArray();
1822
1823 // Do the HAL configuration; will potentially touch stream
1824 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001825 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001826 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001827 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001828
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001829 if (res == BAD_VALUE) {
1830 // HAL rejected this set of streams as unsupported, clean up config
1831 // attempt and return to unconfigured state
1832 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1833 res = mInputStream->cancelConfiguration();
1834 if (res != OK) {
1835 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1836 mInputStream->getId(), strerror(-res), res);
1837 return res;
1838 }
1839 }
1840
1841 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1842 sp<Camera3OutputStreamInterface> outputStream =
1843 mOutputStreams.editValueAt(i);
1844 if (outputStream->isConfiguring()) {
1845 res = outputStream->cancelConfiguration();
1846 if (res != OK) {
1847 SET_ERR_L(
1848 "Can't cancel configuring output stream %d: %s (%d)",
1849 outputStream->getId(), strerror(-res), res);
1850 return res;
1851 }
1852 }
1853 }
1854
1855 // Return state to that at start of call, so that future configures
1856 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001857 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001858 mNeedConfig = true;
1859
1860 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1861 return BAD_VALUE;
1862 } else if (res != OK) {
1863 // Some other kind of error from configure_streams - this is not
1864 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001865 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1866 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001867 return res;
1868 }
1869
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001870 // Finish all stream configuration immediately.
1871 // TODO: Try to relax this later back to lazy completion, which should be
1872 // faster
1873
Igor Murashkin073f8572013-05-02 14:59:28 -07001874 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001875 res = mInputStream->finishConfiguration(mHal3Device);
1876 if (res != OK) {
1877 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1878 mInputStream->getId(), strerror(-res), res);
1879 return res;
1880 }
1881 }
1882
1883 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001884 sp<Camera3OutputStreamInterface> outputStream =
1885 mOutputStreams.editValueAt(i);
1886 if (outputStream->isConfiguring()) {
1887 res = outputStream->finishConfiguration(mHal3Device);
1888 if (res != OK) {
1889 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1890 outputStream->getId(), strerror(-res), res);
1891 return res;
1892 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001893 }
1894 }
1895
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001896 // Request thread needs to know to avoid using repeat-last-settings protocol
1897 // across configure_streams() calls
1898 mRequestThread->configurationComplete();
1899
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07001900 // Boost priority of request thread for high speed recording to SCHED_FIFO
1901 if (mIsConstrainedHighSpeedConfiguration) {
1902 pid_t requestThreadTid = mRequestThread->getTid();
1903 res = requestPriority(getpid(), requestThreadTid,
1904 kConstrainedHighSpeedThreadPriority, true);
1905 if (res != OK) {
1906 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
1907 strerror(-res), res);
1908 } else {
1909 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
1910 }
1911 } else {
1912 // TODO: Set/restore normal priority for normal use cases
1913 }
1914
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001915 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001916
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001917 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001918
Ruben Brunk183f0562015-08-12 12:55:02 -07001919 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1920 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001921
1922 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1923
Zhijun He0a210512014-07-24 13:45:15 -07001924 // tear down the deleted streams after configure streams.
1925 mDeletedStreams.clear();
1926
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001927 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001928}
1929
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001930status_t Camera3Device::addDummyStreamLocked() {
1931 ATRACE_CALL();
1932 status_t res;
1933
1934 if (mDummyStreamId != NO_STREAM) {
1935 // Should never be adding a second dummy stream when one is already
1936 // active
1937 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1938 __FUNCTION__, mId);
1939 return INVALID_OPERATION;
1940 }
1941
1942 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1943
1944 sp<Camera3OutputStreamInterface> dummyStream =
1945 new Camera3DummyStream(mNextStreamId);
1946
1947 res = mOutputStreams.add(mNextStreamId, dummyStream);
1948 if (res < 0) {
1949 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1950 return res;
1951 }
1952
1953 mDummyStreamId = mNextStreamId;
1954 mNextStreamId++;
1955
1956 return OK;
1957}
1958
1959status_t Camera3Device::tryRemoveDummyStreamLocked() {
1960 ATRACE_CALL();
1961 status_t res;
1962
1963 if (mDummyStreamId == NO_STREAM) return OK;
1964 if (mOutputStreams.size() == 1) return OK;
1965
1966 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1967
1968 // Ok, have a dummy stream and there's at least one other output stream,
1969 // so remove the dummy
1970
1971 sp<Camera3StreamInterface> deletedStream;
1972 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1973 if (outputStreamIdx == NAME_NOT_FOUND) {
1974 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1975 return INVALID_OPERATION;
1976 }
1977
1978 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1979 mOutputStreams.removeItemsAt(outputStreamIdx);
1980
1981 // Free up the stream endpoint so that it can be used by some other stream
1982 res = deletedStream->disconnect();
1983 if (res != OK) {
1984 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1985 // fall through since we want to still list the stream as deleted.
1986 }
1987 mDeletedStreams.add(deletedStream);
1988 mDummyStreamId = NO_STREAM;
1989
1990 return res;
1991}
1992
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001993void Camera3Device::setErrorState(const char *fmt, ...) {
1994 Mutex::Autolock l(mLock);
1995 va_list args;
1996 va_start(args, fmt);
1997
1998 setErrorStateLockedV(fmt, args);
1999
2000 va_end(args);
2001}
2002
2003void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2004 Mutex::Autolock l(mLock);
2005 setErrorStateLockedV(fmt, args);
2006}
2007
2008void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2009 va_list args;
2010 va_start(args, fmt);
2011
2012 setErrorStateLockedV(fmt, args);
2013
2014 va_end(args);
2015}
2016
2017void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002018 // Print out all error messages to log
2019 String8 errorCause = String8::formatV(fmt, args);
2020 ALOGE("Camera %d: %s", mId, errorCause.string());
2021
2022 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002023 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002024
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002025 mErrorCause = errorCause;
2026
2027 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002028 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002029
2030 // Notify upstream about a device error
2031 if (mListener != NULL) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002032 mListener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002033 CaptureResultExtras());
2034 }
2035
2036 // Save stack trace. View by dumping it later.
2037 CameraTraces::saveTrace();
2038 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002039}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002040
2041/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002042 * In-flight request management
2043 */
2044
Jianing Weicb0652e2014-03-12 18:29:36 -07002045status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002046 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2047 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002048 ATRACE_CALL();
2049 Mutex::Autolock l(mInFlightLock);
2050
2051 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002052 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2053 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002054 if (res < 0) return res;
2055
2056 return OK;
2057}
2058
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002059void Camera3Device::returnOutputBuffers(
2060 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2061 nsecs_t timestamp) {
2062 for (size_t i = 0; i < numBuffers; i++)
2063 {
2064 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2065 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2066 // Note: stream may be deallocated at this point, if this buffer was
2067 // the last reference to it.
2068 if (res != OK) {
2069 ALOGE("Can't return buffer to its stream: %s (%d)",
2070 strerror(-res), res);
2071 }
2072 }
2073}
2074
2075
2076void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2077
2078 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2079 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2080
2081 nsecs_t sensorTimestamp = request.sensorTimestamp;
2082 nsecs_t shutterTimestamp = request.shutterTimestamp;
2083
2084 // Check if it's okay to remove the request from InFlightMap:
2085 // In the case of a successful request:
2086 // all input and output buffers, all result metadata, shutter callback
2087 // arrived.
2088 // In the case of a unsuccessful request:
2089 // all input and output buffers arrived.
2090 if (request.numBuffersLeft == 0 &&
2091 (request.requestStatus != OK ||
2092 (request.haveResultMetadata && shutterTimestamp != 0))) {
2093 ATRACE_ASYNC_END("frame capture", frameNumber);
2094
2095 // Sanity check - if sensor timestamp matches shutter timestamp
2096 if (request.requestStatus == OK &&
2097 sensorTimestamp != shutterTimestamp) {
2098 SET_ERR("sensor timestamp (%" PRId64
2099 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2100 sensorTimestamp, frameNumber, shutterTimestamp);
2101 }
2102
2103 // for an unsuccessful request, it may have pending output buffers to
2104 // return.
2105 assert(request.requestStatus != OK ||
2106 request.pendingOutputBuffers.size() == 0);
2107 returnOutputBuffers(request.pendingOutputBuffers.array(),
2108 request.pendingOutputBuffers.size(), 0);
2109
2110 mInFlightMap.removeItemsAt(idx, 1);
2111
2112 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2113 }
2114
2115 // Sanity check - if we have too many in-flight frames, something has
2116 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002117 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002118 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002119 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2120 kInFlightWarnLimitHighSpeed) {
2121 CLOGE("In-flight list too large for high speed configuration: %zu",
2122 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002123 }
2124}
2125
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002126void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2127 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2128 if (result == nullptr) return;
2129
2130 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2131 (int32_t*)&frameNumber, 1) != OK) {
2132 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2133 return;
2134 }
2135
2136 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2137 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2138 return;
2139 }
2140
2141 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2142
2143 // Valid result, insert into queue
2144 List<CaptureResult>::iterator queuedResult =
2145 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2146 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2147 ", burstId = %" PRId32, __FUNCTION__,
2148 queuedResult->mResultExtras.requestId,
2149 queuedResult->mResultExtras.frameNumber,
2150 queuedResult->mResultExtras.burstId);
2151
2152 mResultSignal.signal();
2153}
2154
2155
2156void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2157 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2158 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2159 Mutex::Autolock l(mOutputLock);
2160
2161 CaptureResult captureResult;
2162 captureResult.mResultExtras = resultExtras;
2163 captureResult.mMetadata = partialResult;
2164
2165 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2166}
2167
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002168
2169void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2170 CaptureResultExtras &resultExtras,
2171 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002172 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002173 bool reprocess,
2174 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002175 if (pendingMetadata.isEmpty())
2176 return;
2177
2178 Mutex::Autolock l(mOutputLock);
2179
2180 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002181 if (reprocess) {
2182 if (frameNumber < mNextReprocessResultFrameNumber) {
2183 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002184 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002185 frameNumber, mNextReprocessResultFrameNumber);
2186 return;
2187 }
2188 mNextReprocessResultFrameNumber = frameNumber + 1;
2189 } else {
2190 if (frameNumber < mNextResultFrameNumber) {
2191 SET_ERR("Out-of-order capture result metadata submitted! "
2192 "(got frame number %d, expecting %d)",
2193 frameNumber, mNextResultFrameNumber);
2194 return;
2195 }
2196 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002197 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002198
2199 CaptureResult captureResult;
2200 captureResult.mResultExtras = resultExtras;
2201 captureResult.mMetadata = pendingMetadata;
2202
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002203 // Append any previous partials to form a complete result
2204 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2205 captureResult.mMetadata.append(collectedPartialResult);
2206 }
2207
2208 captureResult.mMetadata.sort();
2209
2210 // Check that there's a timestamp in the result metadata
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002211 camera_metadata_entry entry = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002212 if (entry.count == 0) {
2213 SET_ERR("No timestamp provided by HAL for frame %d!",
2214 frameNumber);
2215 return;
2216 }
2217
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002218 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002219}
2220
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002221/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002222 * Camera HAL device callback methods
2223 */
2224
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002225void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002226 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002227
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002228 status_t res;
2229
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002230 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002231 if (result->result == NULL && result->num_output_buffers == 0 &&
2232 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002233 SET_ERR("No result data provided by HAL for frame %d",
2234 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002235 return;
2236 }
Zhijun He204e3292014-07-14 17:09:23 -07002237
2238 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2239 // partial_result to 1 when metadata is included in this result.
2240 if (!mUsePartialResult &&
2241 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2242 result->result != NULL &&
2243 result->partial_result != 1) {
2244 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2245 " if partial result is not supported",
2246 frameNumber, result->partial_result);
2247 return;
2248 }
2249
2250 bool isPartialResult = false;
2251 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002252 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002253 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002254
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002255 // Get shutter timestamp and resultExtras from list of in-flight requests,
2256 // where it was added by the shutter notification for this frame. If the
2257 // shutter timestamp isn't received yet, append the output buffers to the
2258 // in-flight request and they will be returned when the shutter timestamp
2259 // arrives. Update the in-flight status and remove the in-flight entry if
2260 // all result data and shutter timestamp have been received.
2261 nsecs_t shutterTimestamp = 0;
2262
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002263 {
2264 Mutex::Autolock l(mInFlightLock);
2265 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2266 if (idx == NAME_NOT_FOUND) {
2267 SET_ERR("Unknown frame number for capture result: %d",
2268 frameNumber);
2269 return;
2270 }
2271 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002272 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2273 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2274 ", partialResultCount = %d",
2275 __FUNCTION__, request.resultExtras.requestId,
2276 request.resultExtras.frameNumber, request.resultExtras.burstId,
2277 result->partial_result);
2278 // Always update the partial count to the latest one if it's not 0
2279 // (buffers only). When framework aggregates adjacent partial results
2280 // into one, the latest partial count will be used.
2281 if (result->partial_result != 0)
2282 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002283
2284 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002285 if (mUsePartialResult && result->result != NULL) {
2286 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2287 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2288 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2289 " the range of [1, %d] when metadata is included in the result",
2290 frameNumber, result->partial_result, mNumPartialResults);
2291 return;
2292 }
2293 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002294 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002295 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002296 }
Zhijun He204e3292014-07-14 17:09:23 -07002297 } else {
2298 camera_metadata_ro_entry_t partialResultEntry;
2299 res = find_camera_metadata_ro_entry(result->result,
2300 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2301 if (res != NAME_NOT_FOUND &&
2302 partialResultEntry.count > 0 &&
2303 partialResultEntry.data.u8[0] ==
2304 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2305 // A partial result. Flag this as such, and collect this
2306 // set of metadata into the in-flight entry.
2307 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002308 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002309 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002310 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002311 ANDROID_QUIRKS_PARTIAL_RESULT);
2312 }
2313 }
2314
2315 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002316 // Send partial capture result
2317 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2318 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002319 }
2320 }
2321
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002322 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002323 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002324
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002325 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002326 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002327 if (request.haveResultMetadata) {
2328 SET_ERR("Called multiple times with metadata for frame %d",
2329 frameNumber);
2330 return;
2331 }
Zhijun He204e3292014-07-14 17:09:23 -07002332 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002333 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002334 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002335 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002336 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002337 request.haveResultMetadata = true;
2338 }
2339
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002340 uint32_t numBuffersReturned = result->num_output_buffers;
2341 if (result->input_buffer != NULL) {
2342 if (hasInputBufferInRequest) {
2343 numBuffersReturned += 1;
2344 } else {
2345 ALOGW("%s: Input buffer should be NULL if there is no input"
2346 " buffer sent in the request",
2347 __FUNCTION__);
2348 }
2349 }
2350 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002351 if (request.numBuffersLeft < 0) {
2352 SET_ERR("Too many buffers returned for frame %d",
2353 frameNumber);
2354 return;
2355 }
2356
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002357 camera_metadata_ro_entry_t entry;
2358 res = find_camera_metadata_ro_entry(result->result,
2359 ANDROID_SENSOR_TIMESTAMP, &entry);
2360 if (res == OK && entry.count == 1) {
2361 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002362 }
2363
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002364 // If shutter event isn't received yet, append the output buffers to
2365 // the in-flight request. Otherwise, return the output buffers to
2366 // streams.
2367 if (shutterTimestamp == 0) {
2368 request.pendingOutputBuffers.appendArray(result->output_buffers,
2369 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002370 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002371 returnOutputBuffers(result->output_buffers,
2372 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002373 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002374
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002375 if (result->result != NULL && !isPartialResult) {
2376 if (shutterTimestamp == 0) {
2377 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002378 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002379 } else {
2380 CameraMetadata metadata;
2381 metadata = result->result;
2382 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002383 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2384 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002385 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002386 }
2387
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002388 removeInFlightRequestIfReadyLocked(idx);
2389 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002390
Zhijun Hef0d962a2014-06-30 10:24:11 -07002391 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002392 if (hasInputBufferInRequest) {
2393 Camera3Stream *stream =
2394 Camera3Stream::cast(result->input_buffer->stream);
2395 res = stream->returnInputBuffer(*(result->input_buffer));
2396 // Note: stream may be deallocated at this point, if this buffer was the
2397 // last reference to it.
2398 if (res != OK) {
2399 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2400 " its stream:%s (%d)", __FUNCTION__,
2401 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002402 }
2403 } else {
2404 ALOGW("%s: Input buffer should be NULL if there is no input"
2405 " buffer sent in the request, skipping input buffer return.",
2406 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002407 }
2408 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002409}
2410
2411void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002412 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002413 NotificationListener *listener;
2414 {
2415 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002416 listener = mListener;
2417 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002418
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002419 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002420 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002421 return;
2422 }
2423
2424 switch (msg->type) {
2425 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002426 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002427 break;
2428 }
2429 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002430 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002431 break;
2432 }
2433 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002434 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002435 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002436 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002437}
2438
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002439void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2440 NotificationListener *listener) {
2441
2442 // Map camera HAL error codes to ICameraDeviceCallback error codes
2443 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002444 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002445 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002446 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002447 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002448 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002449 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002450 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002451 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002452 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002453 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002454 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002455 };
2456
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002457 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002458 ((msg.error_code >= 0) &&
2459 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2460 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002461 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002462
2463 int streamId = 0;
2464 if (msg.error_stream != NULL) {
2465 Camera3Stream *stream =
2466 Camera3Stream::cast(msg.error_stream);
2467 streamId = stream->getId();
2468 }
2469 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2470 mId, __FUNCTION__, msg.frame_number,
2471 streamId, msg.error_code);
2472
2473 CaptureResultExtras resultExtras;
2474 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002475 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002476 // SET_ERR calls notifyError
2477 SET_ERR("Camera HAL reported serious device error");
2478 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002479 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2480 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2481 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002482 {
2483 Mutex::Autolock l(mInFlightLock);
2484 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2485 if (idx >= 0) {
2486 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2487 r.requestStatus = msg.error_code;
2488 resultExtras = r.resultExtras;
2489 } else {
2490 resultExtras.frameNumber = msg.frame_number;
2491 ALOGE("Camera %d: %s: cannot find in-flight request on "
2492 "frame %" PRId64 " error", mId, __FUNCTION__,
2493 resultExtras.frameNumber);
2494 }
2495 }
2496 if (listener != NULL) {
2497 listener->notifyError(errorCode, resultExtras);
2498 } else {
2499 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2500 }
2501 break;
2502 default:
2503 // SET_ERR calls notifyError
2504 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2505 break;
2506 }
2507}
2508
2509void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2510 NotificationListener *listener) {
2511 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002512
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002513 // Set timestamp for the request in the in-flight tracking
2514 // and get the request ID to send upstream
2515 {
2516 Mutex::Autolock l(mInFlightLock);
2517 idx = mInFlightMap.indexOfKey(msg.frame_number);
2518 if (idx >= 0) {
2519 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002520
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002521 // Verify ordering of shutter notifications
2522 {
2523 Mutex::Autolock l(mOutputLock);
2524 // TODO: need to track errors for tighter bounds on expected frame number.
2525 if (r.hasInputBuffer) {
2526 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2527 SET_ERR("Shutter notification out-of-order. Expected "
2528 "notification for frame %d, got frame %d",
2529 mNextReprocessShutterFrameNumber, msg.frame_number);
2530 return;
2531 }
2532 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2533 } else {
2534 if (msg.frame_number < mNextShutterFrameNumber) {
2535 SET_ERR("Shutter notification out-of-order. Expected "
2536 "notification for frame %d, got frame %d",
2537 mNextShutterFrameNumber, msg.frame_number);
2538 return;
2539 }
2540 mNextShutterFrameNumber = msg.frame_number + 1;
2541 }
2542 }
2543
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002544 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2545 mId, __FUNCTION__,
2546 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2547 // Call listener, if any
2548 if (listener != NULL) {
2549 listener->notifyShutter(r.resultExtras, msg.timestamp);
2550 }
2551
2552 r.shutterTimestamp = msg.timestamp;
2553
2554 // send pending result and buffers
2555 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002556 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002557 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002558 returnOutputBuffers(r.pendingOutputBuffers.array(),
2559 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2560 r.pendingOutputBuffers.clear();
2561
2562 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002563 }
2564 }
2565 if (idx < 0) {
2566 SET_ERR("Shutter notification for non-existent frame number %d",
2567 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002568 }
2569}
2570
2571
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002572CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002573 ALOGV("%s", __FUNCTION__);
2574
Igor Murashkin1e479c02013-09-06 16:55:14 -07002575 CameraMetadata retVal;
2576
2577 if (mRequestThread != NULL) {
2578 retVal = mRequestThread->getLatestRequest();
2579 }
2580
Igor Murashkin1e479c02013-09-06 16:55:14 -07002581 return retVal;
2582}
2583
Jianing Weicb0652e2014-03-12 18:29:36 -07002584
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002585/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002586 * RequestThread inner class methods
2587 */
2588
2589Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002590 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002591 camera3_device_t *hal3Device,
2592 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002593 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002594 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002595 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002596 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002597 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002598 mReconfigured(false),
2599 mDoPause(false),
2600 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002601 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002602 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002603 mCurrentAfTriggerId(0),
2604 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002605 mRepeatingLastFrameNumber(
2606 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002607 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002608 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002609}
2610
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002611void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002612 NotificationListener *listener) {
2613 Mutex::Autolock l(mRequestLock);
2614 mListener = listener;
2615}
2616
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002617void Camera3Device::RequestThread::configurationComplete() {
2618 Mutex::Autolock l(mRequestLock);
2619 mReconfigured = true;
2620}
2621
Jianing Wei90e59c92014-03-12 18:29:36 -07002622status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002623 List<sp<CaptureRequest> > &requests,
2624 /*out*/
2625 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002626 Mutex::Autolock l(mRequestLock);
2627 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2628 ++it) {
2629 mRequestQueue.push_back(*it);
2630 }
2631
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002632 if (lastFrameNumber != NULL) {
2633 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2634 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2635 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2636 *lastFrameNumber);
2637 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002638
Jianing Wei90e59c92014-03-12 18:29:36 -07002639 unpauseForNewRequests();
2640
2641 return OK;
2642}
2643
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002644
2645status_t Camera3Device::RequestThread::queueTrigger(
2646 RequestTrigger trigger[],
2647 size_t count) {
2648
2649 Mutex::Autolock l(mTriggerMutex);
2650 status_t ret;
2651
2652 for (size_t i = 0; i < count; ++i) {
2653 ret = queueTriggerLocked(trigger[i]);
2654
2655 if (ret != OK) {
2656 return ret;
2657 }
2658 }
2659
2660 return OK;
2661}
2662
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002663int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2664 sp<Camera3Device> d = device.promote();
2665 if (d != NULL) return d->mId;
2666 return 0;
2667}
2668
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002669status_t Camera3Device::RequestThread::queueTriggerLocked(
2670 RequestTrigger trigger) {
2671
2672 uint32_t tag = trigger.metadataTag;
2673 ssize_t index = mTriggerMap.indexOfKey(tag);
2674
2675 switch (trigger.getTagType()) {
2676 case TYPE_BYTE:
2677 // fall-through
2678 case TYPE_INT32:
2679 break;
2680 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002681 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2682 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002683 return INVALID_OPERATION;
2684 }
2685
2686 /**
2687 * Collect only the latest trigger, since we only have 1 field
2688 * in the request settings per trigger tag, and can't send more than 1
2689 * trigger per request.
2690 */
2691 if (index != NAME_NOT_FOUND) {
2692 mTriggerMap.editValueAt(index) = trigger;
2693 } else {
2694 mTriggerMap.add(tag, trigger);
2695 }
2696
2697 return OK;
2698}
2699
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002700status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002701 const RequestList &requests,
2702 /*out*/
2703 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002704 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002705 if (lastFrameNumber != NULL) {
2706 *lastFrameNumber = mRepeatingLastFrameNumber;
2707 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002708 mRepeatingRequests.clear();
2709 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2710 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002711
2712 unpauseForNewRequests();
2713
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002714 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002715 return OK;
2716}
2717
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002718bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2719 if (mRepeatingRequests.empty()) {
2720 return false;
2721 }
2722 int32_t requestId = requestIn->mResultExtras.requestId;
2723 const RequestList &repeatRequests = mRepeatingRequests;
2724 // All repeating requests are guaranteed to have same id so only check first quest
2725 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2726 return (firstRequest->mResultExtras.requestId == requestId);
2727}
2728
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002729status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002730 Mutex::Autolock l(mRequestLock);
2731 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002732 if (lastFrameNumber != NULL) {
2733 *lastFrameNumber = mRepeatingLastFrameNumber;
2734 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002735 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002736 return OK;
2737}
2738
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002739status_t Camera3Device::RequestThread::clear(
2740 NotificationListener *listener,
2741 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002742 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002743 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002744
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002745 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002746
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002747 // Send errors for all requests pending in the request queue, including
2748 // pending repeating requests
2749 if (listener != NULL) {
2750 for (RequestList::iterator it = mRequestQueue.begin();
2751 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002752 // Abort the input buffers for reprocess requests.
2753 if ((*it)->mInputStream != NULL) {
2754 camera3_stream_buffer_t inputBuffer;
2755 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2756 if (res != OK) {
2757 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2758 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2759 } else {
2760 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2761 if (res != OK) {
2762 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2763 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2764 }
2765 }
2766 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002767 // Set the frame number this request would have had, if it
2768 // had been submitted; this frame number will not be reused.
2769 // The requestId and burstId fields were set when the request was
2770 // submitted originally (in convertMetadataListToRequestListLocked)
2771 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002772 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002773 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002774 }
2775 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002776 mRequestQueue.clear();
2777 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002778 if (lastFrameNumber != NULL) {
2779 *lastFrameNumber = mRepeatingLastFrameNumber;
2780 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002781 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002782 return OK;
2783}
2784
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002785status_t Camera3Device::RequestThread::flush() {
2786 ATRACE_CALL();
2787 Mutex::Autolock l(mFlushLock);
2788
2789 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2790 return mHal3Device->ops->flush(mHal3Device);
2791 }
2792
2793 return -ENOTSUP;
2794}
2795
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002796void Camera3Device::RequestThread::setPaused(bool paused) {
2797 Mutex::Autolock l(mPauseLock);
2798 mDoPause = paused;
2799 mDoPauseSignal.signal();
2800}
2801
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002802status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2803 int32_t requestId, nsecs_t timeout) {
2804 Mutex::Autolock l(mLatestRequestMutex);
2805 status_t res;
2806 while (mLatestRequestId != requestId) {
2807 nsecs_t startTime = systemTime();
2808
2809 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2810 if (res != OK) return res;
2811
2812 timeout -= (systemTime() - startTime);
2813 }
2814
2815 return OK;
2816}
2817
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002818void Camera3Device::RequestThread::requestExit() {
2819 // Call parent to set up shutdown
2820 Thread::requestExit();
2821 // The exit from any possible waits
2822 mDoPauseSignal.signal();
2823 mRequestSignal.signal();
2824}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002825
Chien-Yu Chend196d612015-06-22 19:49:01 -07002826
2827/**
2828 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2829 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2830 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2831 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2832 * request.
2833 */
2834void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2835 request->mAeTriggerCancelOverride.applyAeLock = false;
2836 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2837
2838 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2839 return;
2840 }
2841
2842 camera_metadata_entry_t aePrecaptureTrigger =
2843 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2844 if (aePrecaptureTrigger.count > 0 &&
2845 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2846 // Always override CANCEL to IDLE
2847 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2848 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2849 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2850 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2851 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2852
2853 if (mAeLockAvailable == true) {
2854 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2855 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2856 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2857 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2858 request->mAeTriggerCancelOverride.applyAeLock = true;
2859 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2860 }
2861 }
2862 }
2863}
2864
2865/**
2866 * Override result metadata for cancelling AE precapture trigger applied in
2867 * handleAePrecaptureCancelRequest().
2868 */
2869void Camera3Device::overrideResultForPrecaptureCancel(
2870 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2871 if (aeTriggerCancelOverride.applyAeLock) {
2872 // Only devices <= v3.2 should have this override
2873 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2874 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2875 }
2876
2877 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2878 // Only devices <= v3.2 should have this override
2879 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2880 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2881 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2882 }
2883}
2884
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002885bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002886 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002887 status_t res;
2888
2889 // Handle paused state.
2890 if (waitIfPaused()) {
2891 return true;
2892 }
2893
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002894 // Wait for the next batch of requests.
2895 waitForNextRequestBatch();
2896 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002897 return true;
2898 }
2899
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002900 // Get the latest request ID, if any
2901 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002902 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002903 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002904 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002905 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002906 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002907 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
2908 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002909 }
2910
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002911 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002912 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002913 if (res == TIMED_OUT) {
2914 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002915 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002916 return true;
2917 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002918 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002919 return false;
2920 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002921
Zhijun Hecc27e112013-10-03 16:12:43 -07002922 // Inform waitUntilRequestProcessed thread of a new request ID
2923 {
2924 Mutex::Autolock al(mLatestRequestMutex);
2925
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002926 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07002927 mLatestRequestSignal.signal();
2928 }
2929
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002930 // Submit a batch of requests to HAL.
2931 // Use flush lock only when submitting multilple requests in a batch.
2932 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
2933 // which may take a long time to finish so synchronizing flush() and
2934 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
2935 // For now, only synchronize for high speed recording and we should figure something out for
2936 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002937 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002938
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002939 if (useFlushLock) {
2940 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002941 }
2942
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002943 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002944 mNextRequests.size());
2945 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002946 // Submit request and block until ready for next one
2947 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
2948 ATRACE_BEGIN("camera3->process_capture_request");
2949 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
2950 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07002951
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002952 if (res != OK) {
2953 // Should only get a failure here for malformed requests or device-level
2954 // errors, so consider all errors fatal. Bad metadata failures should
2955 // come through notify.
2956 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
2957 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
2958 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002959 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002960 if (useFlushLock) {
2961 mFlushLock.unlock();
2962 }
2963 return false;
2964 }
2965
2966 // Mark that the request has be submitted successfully.
2967 nextRequest.submitted = true;
2968
2969 // Update the latest request sent to HAL
2970 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
2971 Mutex::Autolock al(mLatestRequestMutex);
2972
2973 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
2974 mLatestRequest.acquire(cloned);
2975 }
2976
2977 if (nextRequest.halRequest.settings != NULL) {
2978 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
2979 }
2980
2981 // Remove any previously queued triggers (after unlock)
2982 res = removeTriggers(mPrevRequest);
2983 if (res != OK) {
2984 SET_ERR("RequestThread: Unable to remove triggers "
2985 "(capture request %d, HAL device: %s (%d)",
2986 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002987 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002988 if (useFlushLock) {
2989 mFlushLock.unlock();
2990 }
2991 return false;
2992 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07002993 }
2994
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002995 if (useFlushLock) {
2996 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002997 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002998
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002999 // Unset as current request
3000 {
3001 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003002 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003003 }
3004
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003005 return true;
3006}
3007
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003008status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003009 ATRACE_CALL();
3010
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003011 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003012 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3013 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3014 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3015
3016 // Prepare a request to HAL
3017 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3018
3019 // Insert any queued triggers (before metadata is locked)
3020 status_t res = insertTriggers(captureRequest);
3021
3022 if (res < 0) {
3023 SET_ERR("RequestThread: Unable to insert triggers "
3024 "(capture request %d, HAL device: %s (%d)",
3025 halRequest->frame_number, strerror(-res), res);
3026 return INVALID_OPERATION;
3027 }
3028 int triggerCount = res;
3029 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3030 mPrevTriggers = triggerCount;
3031
3032 // If the request is the same as last, or we had triggers last time
3033 if (mPrevRequest != captureRequest || triggersMixedIn) {
3034 /**
3035 * HAL workaround:
3036 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3037 */
3038 res = addDummyTriggerIds(captureRequest);
3039 if (res != OK) {
3040 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3041 "(capture request %d, HAL device: %s (%d)",
3042 halRequest->frame_number, strerror(-res), res);
3043 return INVALID_OPERATION;
3044 }
3045
3046 /**
3047 * The request should be presorted so accesses in HAL
3048 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3049 */
3050 captureRequest->mSettings.sort();
3051 halRequest->settings = captureRequest->mSettings.getAndLock();
3052 mPrevRequest = captureRequest;
3053 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3054
3055 IF_ALOGV() {
3056 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3057 find_camera_metadata_ro_entry(
3058 halRequest->settings,
3059 ANDROID_CONTROL_AF_TRIGGER,
3060 &e
3061 );
3062 if (e.count > 0) {
3063 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3064 __FUNCTION__,
3065 halRequest->frame_number,
3066 e.data.u8[0]);
3067 }
3068 }
3069 } else {
3070 // leave request.settings NULL to indicate 'reuse latest given'
3071 ALOGVV("%s: Request settings are REUSED",
3072 __FUNCTION__);
3073 }
3074
3075 uint32_t totalNumBuffers = 0;
3076
3077 // Fill in buffers
3078 if (captureRequest->mInputStream != NULL) {
3079 halRequest->input_buffer = &captureRequest->mInputBuffer;
3080 totalNumBuffers += 1;
3081 } else {
3082 halRequest->input_buffer = NULL;
3083 }
3084
3085 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3086 captureRequest->mOutputStreams.size());
3087 halRequest->output_buffers = outputBuffers->array();
3088 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3089 res = captureRequest->mOutputStreams.editItemAt(i)->
3090 getBuffer(&outputBuffers->editItemAt(i));
3091 if (res != OK) {
3092 // Can't get output buffer from gralloc queue - this could be due to
3093 // abandoned queue or other consumer misbehavior, so not a fatal
3094 // error
3095 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3096 " %s (%d)", strerror(-res), res);
3097
3098 return TIMED_OUT;
3099 }
3100 halRequest->num_output_buffers++;
3101 }
3102 totalNumBuffers += halRequest->num_output_buffers;
3103
3104 // Log request in the in-flight queue
3105 sp<Camera3Device> parent = mParent.promote();
3106 if (parent == NULL) {
3107 // Should not happen, and nowhere to send errors to, so just log it
3108 CLOGE("RequestThread: Parent is gone");
3109 return INVALID_OPERATION;
3110 }
3111 res = parent->registerInFlight(halRequest->frame_number,
3112 totalNumBuffers, captureRequest->mResultExtras,
3113 /*hasInput*/halRequest->input_buffer != NULL,
3114 captureRequest->mAeTriggerCancelOverride);
3115 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3116 ", burstId = %" PRId32 ".",
3117 __FUNCTION__,
3118 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3119 captureRequest->mResultExtras.burstId);
3120 if (res != OK) {
3121 SET_ERR("RequestThread: Unable to register new in-flight request:"
3122 " %s (%d)", strerror(-res), res);
3123 return INVALID_OPERATION;
3124 }
3125 }
3126
3127 return OK;
3128}
3129
Igor Murashkin1e479c02013-09-06 16:55:14 -07003130CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3131 Mutex::Autolock al(mLatestRequestMutex);
3132
3133 ALOGV("RequestThread::%s", __FUNCTION__);
3134
3135 return mLatestRequest;
3136}
3137
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003138bool Camera3Device::RequestThread::isStreamPending(
3139 sp<Camera3StreamInterface>& stream) {
3140 Mutex::Autolock l(mRequestLock);
3141
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003142 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003143 if (!nextRequest.submitted) {
3144 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3145 if (stream == s) return true;
3146 }
3147 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003148 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003149 }
3150
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003151 for (const auto& request : mRequestQueue) {
3152 for (const auto& s : request->mOutputStreams) {
3153 if (stream == s) return true;
3154 }
3155 if (stream == request->mInputStream) return true;
3156 }
3157
3158 for (const auto& request : mRepeatingRequests) {
3159 for (const auto& s : request->mOutputStreams) {
3160 if (stream == s) return true;
3161 }
3162 if (stream == request->mInputStream) return true;
3163 }
3164
3165 return false;
3166}
Jianing Weicb0652e2014-03-12 18:29:36 -07003167
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003168void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3169 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003170 return;
3171 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003172
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003173 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003174 // Skip the ones that have been submitted successfully.
3175 if (nextRequest.submitted) {
3176 continue;
3177 }
3178
3179 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3180 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3181 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3182
3183 if (halRequest->settings != NULL) {
3184 captureRequest->mSettings.unlock(halRequest->settings);
3185 }
3186
3187 if (captureRequest->mInputStream != NULL) {
3188 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3189 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3190 }
3191
3192 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3193 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3194 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3195 }
3196
3197 if (sendRequestError) {
3198 Mutex::Autolock l(mRequestLock);
3199 if (mListener != NULL) {
3200 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003201 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003202 captureRequest->mResultExtras);
3203 }
3204 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003205 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003206
3207 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003208 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003209}
3210
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003211void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003212 // Optimized a bit for the simple steady-state case (single repeating
3213 // request), to avoid putting that request in the queue temporarily.
3214 Mutex::Autolock l(mRequestLock);
3215
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003216 assert(mNextRequests.empty());
3217
3218 NextRequest nextRequest;
3219 nextRequest.captureRequest = waitForNextRequestLocked();
3220 if (nextRequest.captureRequest == nullptr) {
3221 return;
3222 }
3223
3224 nextRequest.halRequest = camera3_capture_request_t();
3225 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003226 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003227
3228 // Wait for additional requests
3229 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3230
3231 for (size_t i = 1; i < batchSize; i++) {
3232 NextRequest additionalRequest;
3233 additionalRequest.captureRequest = waitForNextRequestLocked();
3234 if (additionalRequest.captureRequest == nullptr) {
3235 break;
3236 }
3237
3238 additionalRequest.halRequest = camera3_capture_request_t();
3239 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003240 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003241 }
3242
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003243 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003244 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003245 mNextRequests.size(), batchSize);
3246 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003247 }
3248
3249 return;
3250}
3251
3252sp<Camera3Device::CaptureRequest>
3253 Camera3Device::RequestThread::waitForNextRequestLocked() {
3254 status_t res;
3255 sp<CaptureRequest> nextRequest;
3256
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003257 while (mRequestQueue.empty()) {
3258 if (!mRepeatingRequests.empty()) {
3259 // Always atomically enqueue all requests in a repeating request
3260 // list. Guarantees a complete in-sequence set of captures to
3261 // application.
3262 const RequestList &requests = mRepeatingRequests;
3263 RequestList::const_iterator firstRequest =
3264 requests.begin();
3265 nextRequest = *firstRequest;
3266 mRequestQueue.insert(mRequestQueue.end(),
3267 ++firstRequest,
3268 requests.end());
3269 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003270
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003271 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003272
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003273 break;
3274 }
3275
3276 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3277
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003278 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3279 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003280 Mutex::Autolock pl(mPauseLock);
3281 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003282 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003283 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003284 // Let the tracker know
3285 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3286 if (statusTracker != 0) {
3287 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3288 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003289 }
3290 // Stop waiting for now and let thread management happen
3291 return NULL;
3292 }
3293 }
3294
3295 if (nextRequest == NULL) {
3296 // Don't have a repeating request already in hand, so queue
3297 // must have an entry now.
3298 RequestList::iterator firstRequest =
3299 mRequestQueue.begin();
3300 nextRequest = *firstRequest;
3301 mRequestQueue.erase(firstRequest);
3302 }
3303
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003304 // In case we've been unpaused by setPaused clearing mDoPause, need to
3305 // update internal pause state (capture/setRepeatingRequest unpause
3306 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003307 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003308 if (mPaused) {
3309 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3310 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3311 if (statusTracker != 0) {
3312 statusTracker->markComponentActive(mStatusId);
3313 }
3314 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003315 mPaused = false;
3316
3317 // Check if we've reconfigured since last time, and reset the preview
3318 // request if so. Can't use 'NULL request == repeat' across configure calls.
3319 if (mReconfigured) {
3320 mPrevRequest.clear();
3321 mReconfigured = false;
3322 }
3323
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003324 if (nextRequest != NULL) {
3325 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003326 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3327 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003328
3329 // Since RequestThread::clear() removes buffers from the input stream,
3330 // get the right buffer here before unlocking mRequestLock
3331 if (nextRequest->mInputStream != NULL) {
3332 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3333 if (res != OK) {
3334 // Can't get input buffer from gralloc queue - this could be due to
3335 // disconnected queue or other producer misbehavior, so not a fatal
3336 // error
3337 ALOGE("%s: Can't get input buffer, skipping request:"
3338 " %s (%d)", __FUNCTION__, strerror(-res), res);
3339 if (mListener != NULL) {
3340 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003341 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003342 nextRequest->mResultExtras);
3343 }
3344 return NULL;
3345 }
3346 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003347 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003348
3349 handleAePrecaptureCancelRequest(nextRequest);
3350
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003351 return nextRequest;
3352}
3353
3354bool Camera3Device::RequestThread::waitIfPaused() {
3355 status_t res;
3356 Mutex::Autolock l(mPauseLock);
3357 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003358 if (mPaused == false) {
3359 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003360 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3361 // Let the tracker know
3362 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3363 if (statusTracker != 0) {
3364 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3365 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003366 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003367
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003368 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003369 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003370 return true;
3371 }
3372 }
3373 // We don't set mPaused to false here, because waitForNextRequest needs
3374 // to further manage the paused state in case of starvation.
3375 return false;
3376}
3377
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003378void Camera3Device::RequestThread::unpauseForNewRequests() {
3379 // With work to do, mark thread as unpaused.
3380 // If paused by request (setPaused), don't resume, to avoid
3381 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003382 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003383 Mutex::Autolock p(mPauseLock);
3384 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003385 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3386 if (mPaused) {
3387 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3388 if (statusTracker != 0) {
3389 statusTracker->markComponentActive(mStatusId);
3390 }
3391 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003392 mPaused = false;
3393 }
3394}
3395
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003396void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3397 sp<Camera3Device> parent = mParent.promote();
3398 if (parent != NULL) {
3399 va_list args;
3400 va_start(args, fmt);
3401
3402 parent->setErrorStateV(fmt, args);
3403
3404 va_end(args);
3405 }
3406}
3407
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003408status_t Camera3Device::RequestThread::insertTriggers(
3409 const sp<CaptureRequest> &request) {
3410
3411 Mutex::Autolock al(mTriggerMutex);
3412
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003413 sp<Camera3Device> parent = mParent.promote();
3414 if (parent == NULL) {
3415 CLOGE("RequestThread: Parent is gone");
3416 return DEAD_OBJECT;
3417 }
3418
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003419 CameraMetadata &metadata = request->mSettings;
3420 size_t count = mTriggerMap.size();
3421
3422 for (size_t i = 0; i < count; ++i) {
3423 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003424 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003425
3426 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3427 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3428 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003429 if (isAeTrigger) {
3430 request->mResultExtras.precaptureTriggerId = triggerId;
3431 mCurrentPreCaptureTriggerId = triggerId;
3432 } else {
3433 request->mResultExtras.afTriggerId = triggerId;
3434 mCurrentAfTriggerId = triggerId;
3435 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003436 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3437 continue; // Trigger ID tag is deprecated since device HAL 3.2
3438 }
3439 }
3440
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003441 camera_metadata_entry entry = metadata.find(tag);
3442
3443 if (entry.count > 0) {
3444 /**
3445 * Already has an entry for this trigger in the request.
3446 * Rewrite it with our requested trigger value.
3447 */
3448 RequestTrigger oldTrigger = trigger;
3449
3450 oldTrigger.entryValue = entry.data.u8[0];
3451
3452 mTriggerReplacedMap.add(tag, oldTrigger);
3453 } else {
3454 /**
3455 * More typical, no trigger entry, so we just add it
3456 */
3457 mTriggerRemovedMap.add(tag, trigger);
3458 }
3459
3460 status_t res;
3461
3462 switch (trigger.getTagType()) {
3463 case TYPE_BYTE: {
3464 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3465 res = metadata.update(tag,
3466 &entryValue,
3467 /*count*/1);
3468 break;
3469 }
3470 case TYPE_INT32:
3471 res = metadata.update(tag,
3472 &trigger.entryValue,
3473 /*count*/1);
3474 break;
3475 default:
3476 ALOGE("%s: Type not supported: 0x%x",
3477 __FUNCTION__,
3478 trigger.getTagType());
3479 return INVALID_OPERATION;
3480 }
3481
3482 if (res != OK) {
3483 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3484 ", value %d", __FUNCTION__, trigger.getTagName(),
3485 trigger.entryValue);
3486 return res;
3487 }
3488
3489 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3490 trigger.getTagName(),
3491 trigger.entryValue);
3492 }
3493
3494 mTriggerMap.clear();
3495
3496 return count;
3497}
3498
3499status_t Camera3Device::RequestThread::removeTriggers(
3500 const sp<CaptureRequest> &request) {
3501 Mutex::Autolock al(mTriggerMutex);
3502
3503 CameraMetadata &metadata = request->mSettings;
3504
3505 /**
3506 * Replace all old entries with their old values.
3507 */
3508 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3509 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3510
3511 status_t res;
3512
3513 uint32_t tag = trigger.metadataTag;
3514 switch (trigger.getTagType()) {
3515 case TYPE_BYTE: {
3516 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3517 res = metadata.update(tag,
3518 &entryValue,
3519 /*count*/1);
3520 break;
3521 }
3522 case TYPE_INT32:
3523 res = metadata.update(tag,
3524 &trigger.entryValue,
3525 /*count*/1);
3526 break;
3527 default:
3528 ALOGE("%s: Type not supported: 0x%x",
3529 __FUNCTION__,
3530 trigger.getTagType());
3531 return INVALID_OPERATION;
3532 }
3533
3534 if (res != OK) {
3535 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3536 ", trigger value %d", __FUNCTION__,
3537 trigger.getTagName(), trigger.entryValue);
3538 return res;
3539 }
3540 }
3541 mTriggerReplacedMap.clear();
3542
3543 /**
3544 * Remove all new entries.
3545 */
3546 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3547 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3548 status_t res = metadata.erase(trigger.metadataTag);
3549
3550 if (res != OK) {
3551 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3552 ", trigger value %d", __FUNCTION__,
3553 trigger.getTagName(), trigger.entryValue);
3554 return res;
3555 }
3556 }
3557 mTriggerRemovedMap.clear();
3558
3559 return OK;
3560}
3561
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003562status_t Camera3Device::RequestThread::addDummyTriggerIds(
3563 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003564 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003565 static const int32_t dummyTriggerId = 1;
3566 status_t res;
3567
3568 CameraMetadata &metadata = request->mSettings;
3569
3570 // If AF trigger is active, insert a dummy AF trigger ID if none already
3571 // exists
3572 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3573 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3574 if (afTrigger.count > 0 &&
3575 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3576 afId.count == 0) {
3577 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3578 if (res != OK) return res;
3579 }
3580
3581 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3582 // if none already exists
3583 camera_metadata_entry pcTrigger =
3584 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3585 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3586 if (pcTrigger.count > 0 &&
3587 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3588 pcId.count == 0) {
3589 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3590 &dummyTriggerId, 1);
3591 if (res != OK) return res;
3592 }
3593
3594 return OK;
3595}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003596
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003597/**
3598 * PreparerThread inner class methods
3599 */
3600
3601Camera3Device::PreparerThread::PreparerThread() :
3602 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3603}
3604
3605Camera3Device::PreparerThread::~PreparerThread() {
3606 Thread::requestExitAndWait();
3607 if (mCurrentStream != nullptr) {
3608 mCurrentStream->cancelPrepare();
3609 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3610 mCurrentStream.clear();
3611 }
3612 clear();
3613}
3614
Ruben Brunkc78ac262015-08-13 17:58:46 -07003615status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003616 status_t res;
3617
3618 Mutex::Autolock l(mLock);
3619
Ruben Brunkc78ac262015-08-13 17:58:46 -07003620 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003621 if (res == OK) {
3622 // No preparation needed, fire listener right off
3623 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3624 if (mListener) {
3625 mListener->notifyPrepared(stream->getId());
3626 }
3627 return OK;
3628 } else if (res != NOT_ENOUGH_DATA) {
3629 return res;
3630 }
3631
3632 // Need to prepare, start up thread if necessary
3633 if (!mActive) {
3634 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3635 // isn't running
3636 Thread::requestExitAndWait();
3637 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3638 if (res != OK) {
3639 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3640 if (mListener) {
3641 mListener->notifyPrepared(stream->getId());
3642 }
3643 return res;
3644 }
3645 mCancelNow = false;
3646 mActive = true;
3647 ALOGV("%s: Preparer stream started", __FUNCTION__);
3648 }
3649
3650 // queue up the work
3651 mPendingStreams.push_back(stream);
3652 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3653
3654 return OK;
3655}
3656
3657status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003658 Mutex::Autolock l(mLock);
3659
3660 for (const auto& stream : mPendingStreams) {
3661 stream->cancelPrepare();
3662 }
3663 mPendingStreams.clear();
3664 mCancelNow = true;
3665
3666 return OK;
3667}
3668
3669void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3670 Mutex::Autolock l(mLock);
3671 mListener = listener;
3672}
3673
3674bool Camera3Device::PreparerThread::threadLoop() {
3675 status_t res;
3676 {
3677 Mutex::Autolock l(mLock);
3678 if (mCurrentStream == nullptr) {
3679 // End thread if done with work
3680 if (mPendingStreams.empty()) {
3681 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3682 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3683 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3684 mActive = false;
3685 return false;
3686 }
3687
3688 // Get next stream to prepare
3689 auto it = mPendingStreams.begin();
3690 mCurrentStream = *it;
3691 mPendingStreams.erase(it);
3692 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3693 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3694 } else if (mCancelNow) {
3695 mCurrentStream->cancelPrepare();
3696 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3697 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3698 mCurrentStream.clear();
3699 mCancelNow = false;
3700 return true;
3701 }
3702 }
3703
3704 res = mCurrentStream->prepareNextBuffer();
3705 if (res == NOT_ENOUGH_DATA) return true;
3706 if (res != OK) {
3707 // Something bad happened; try to recover by cancelling prepare and
3708 // signalling listener anyway
3709 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3710 mCurrentStream->getId(), res, strerror(-res));
3711 mCurrentStream->cancelPrepare();
3712 }
3713
3714 // This stream has finished, notify listener
3715 Mutex::Autolock l(mLock);
3716 if (mListener) {
3717 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3718 mCurrentStream->getId());
3719 mListener->notifyPrepared(mCurrentStream->getId());
3720 }
3721
3722 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3723 mCurrentStream.clear();
3724
3725 return true;
3726}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003727
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003728/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003729 * Static callback forwarding methods from HAL to instance
3730 */
3731
3732void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3733 const camera3_capture_result *result) {
3734 Camera3Device *d =
3735 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003736
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003737 d->processCaptureResult(result);
3738}
3739
3740void Camera3Device::sNotify(const camera3_callback_ops *cb,
3741 const camera3_notify_msg *msg) {
3742 Camera3Device *d =
3743 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3744 d->notify(msg);
3745}
3746
3747}; // namespace android