blob: 331f10d75a4583ba4ad595e0b4176e89d0e96936 [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
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -0700413/**
414 * Map Android N dataspace definitions back to Android M definitions, for
415 * use with HALv3.3 or older.
416 *
417 * Only map where correspondences exist, and otherwise preserve the value.
418 */
419android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) {
420 switch (dataSpace) {
421 case HAL_DATASPACE_V0_SRGB_LINEAR:
422 return HAL_DATASPACE_SRGB_LINEAR;
423 case HAL_DATASPACE_V0_SRGB:
424 return HAL_DATASPACE_SRGB;
425 case HAL_DATASPACE_V0_JFIF:
426 return HAL_DATASPACE_JFIF;
427 case HAL_DATASPACE_V0_BT601_625:
428 return HAL_DATASPACE_BT601_625;
429 case HAL_DATASPACE_V0_BT601_525:
430 return HAL_DATASPACE_BT601_525;
431 case HAL_DATASPACE_V0_BT709:
432 return HAL_DATASPACE_BT709;
433 default:
434 return dataSpace;
435 }
436}
437
Zhijun Hef7da0962014-04-24 13:27:56 -0700438ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700439 // Get max jpeg size (area-wise).
440 Size maxJpegResolution = getMaxJpegResolution();
441 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700442 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700443 __FUNCTION__, mId);
444 return BAD_VALUE;
445 }
446
Zhijun Hef7da0962014-04-24 13:27:56 -0700447 // Get max jpeg buffer size
448 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700449 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
450 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700451 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
452 return BAD_VALUE;
453 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700454 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800455 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700456
457 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700458 float scaleFactor = ((float) (width * height)) /
459 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800460 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
461 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700462 if (jpegBufferSize > maxJpegBufferSize) {
463 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700464 }
465
466 return jpegBufferSize;
467}
468
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700469ssize_t Camera3Device::getPointCloudBufferSize() const {
470 const int FLOATS_PER_POINT=4;
471 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
472 if (maxPointCount.count == 0) {
473 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
474 __FUNCTION__, mId);
475 return BAD_VALUE;
476 }
477 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
478 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
479 return maxBytesForPointCloud;
480}
481
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800482ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800483 const int PER_CONFIGURATION_SIZE = 3;
484 const int WIDTH_OFFSET = 0;
485 const int HEIGHT_OFFSET = 1;
486 const int SIZE_OFFSET = 2;
487 camera_metadata_ro_entry rawOpaqueSizes =
488 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800489 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800490 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800491 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800492 __FUNCTION__, mId, count);
493 return BAD_VALUE;
494 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700495
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800496 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
497 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
498 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
499 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
500 }
501 }
502
503 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
504 __FUNCTION__, mId, width, height);
505 return BAD_VALUE;
506}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700507
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800508status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
509 ATRACE_CALL();
510 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700511
512 // Try to lock, but continue in case of failure (to avoid blocking in
513 // deadlocks)
514 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
515 bool gotLock = tryLockSpinRightRound(mLock);
516
517 ALOGW_IF(!gotInterfaceLock,
518 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
519 mId, __FUNCTION__);
520 ALOGW_IF(!gotLock,
521 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
522 mId, __FUNCTION__);
523
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800524 bool dumpTemplates = false;
525 String16 templatesOption("-t");
526 int n = args.size();
527 for (int i = 0; i < n; i++) {
528 if (args[i] == templatesOption) {
529 dumpTemplates = true;
530 }
531 }
532
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800533 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800534
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800535 const char *status =
536 mStatus == STATUS_ERROR ? "ERROR" :
537 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700538 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
539 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800540 mStatus == STATUS_ACTIVE ? "ACTIVE" :
541 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700542
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800543 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700544 if (mStatus == STATUS_ERROR) {
545 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
546 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800547 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700548 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700549 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800550
551 if (mInputStream != NULL) {
552 write(fd, lines.string(), lines.size());
553 mInputStream->dump(fd, args);
554 } else {
555 lines.appendFormat(" No input stream.\n");
556 write(fd, lines.string(), lines.size());
557 }
558 for (size_t i = 0; i < mOutputStreams.size(); i++) {
559 mOutputStreams[i]->dump(fd,args);
560 }
561
Zhijun He431503c2016-03-07 17:30:16 -0800562 if (mBufferManager != NULL) {
563 lines = String8(" Camera3 Buffer Manager:\n");
564 write(fd, lines.string(), lines.size());
565 mBufferManager->dump(fd, args);
566 }
Zhijun He125684a2015-12-26 15:07:30 -0800567
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700568 lines = String8(" In-flight requests:\n");
569 if (mInFlightMap.size() == 0) {
570 lines.append(" None\n");
571 } else {
572 for (size_t i = 0; i < mInFlightMap.size(); i++) {
573 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700574 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700575 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800576 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700577 r.numBuffersLeft);
578 }
579 }
580 write(fd, lines.string(), lines.size());
581
Igor Murashkin1e479c02013-09-06 16:55:14 -0700582 {
583 lines = String8(" Last request sent:\n");
584 write(fd, lines.string(), lines.size());
585
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700586 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700587 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
588 }
589
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800590 if (dumpTemplates) {
591 const char *templateNames[] = {
592 "TEMPLATE_PREVIEW",
593 "TEMPLATE_STILL_CAPTURE",
594 "TEMPLATE_VIDEO_RECORD",
595 "TEMPLATE_VIDEO_SNAPSHOT",
596 "TEMPLATE_ZERO_SHUTTER_LAG",
597 "TEMPLATE_MANUAL"
598 };
599
600 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
601 const camera_metadata_t *templateRequest;
602 templateRequest =
603 mHal3Device->ops->construct_default_request_settings(
604 mHal3Device, i);
605 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
606 if (templateRequest == NULL) {
607 lines.append(" Not supported\n");
608 write(fd, lines.string(), lines.size());
609 } else {
610 write(fd, lines.string(), lines.size());
611 dump_indented_camera_metadata(templateRequest,
612 fd, /*verbosity*/2, /*indentation*/8);
613 }
614 }
615 }
616
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800617 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700618 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800619 write(fd, lines.string(), lines.size());
620 mHal3Device->ops->dump(mHal3Device, fd);
621 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800622
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700623 if (gotLock) mLock.unlock();
624 if (gotInterfaceLock) mInterfaceLock.unlock();
625
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800626 return OK;
627}
628
629const CameraMetadata& Camera3Device::info() const {
630 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800631 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
632 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700633 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800634 mStatus == STATUS_ERROR ?
635 "when in error state" : "before init");
636 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800637 return mDeviceInfo;
638}
639
Jianing Wei90e59c92014-03-12 18:29:36 -0700640status_t Camera3Device::checkStatusOkToCaptureLocked() {
641 switch (mStatus) {
642 case STATUS_ERROR:
643 CLOGE("Device has encountered a serious error");
644 return INVALID_OPERATION;
645 case STATUS_UNINITIALIZED:
646 CLOGE("Device not initialized");
647 return INVALID_OPERATION;
648 case STATUS_UNCONFIGURED:
649 case STATUS_CONFIGURED:
650 case STATUS_ACTIVE:
651 // OK
652 break;
653 default:
654 SET_ERR_L("Unexpected status: %d", mStatus);
655 return INVALID_OPERATION;
656 }
657 return OK;
658}
659
660status_t Camera3Device::convertMetadataListToRequestListLocked(
661 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
662 if (requestList == NULL) {
663 CLOGE("requestList cannot be NULL.");
664 return BAD_VALUE;
665 }
666
Jianing Weicb0652e2014-03-12 18:29:36 -0700667 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700668 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
669 it != metadataList.end(); ++it) {
670 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
671 if (newRequest == 0) {
672 CLOGE("Can't create capture request");
673 return BAD_VALUE;
674 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700675
676 // Setup burst Id and request Id
677 newRequest->mResultExtras.burstId = burstId++;
678 if (it->exists(ANDROID_REQUEST_ID)) {
679 if (it->find(ANDROID_REQUEST_ID).count == 0) {
680 CLOGE("RequestID entry exists; but must not be empty in metadata");
681 return BAD_VALUE;
682 }
683 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
684 } else {
685 CLOGE("RequestID does not exist in metadata");
686 return BAD_VALUE;
687 }
688
Jianing Wei90e59c92014-03-12 18:29:36 -0700689 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700690
691 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700692 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700693
694 // Setup batch size if this is a high speed video recording request.
695 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
696 auto firstRequest = requestList->begin();
697 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
698 if (outputStream->isVideoStream()) {
699 (*firstRequest)->mBatchSize = requestList->size();
700 break;
701 }
702 }
703 }
704
Jianing Wei90e59c92014-03-12 18:29:36 -0700705 return OK;
706}
707
Jianing Weicb0652e2014-03-12 18:29:36 -0700708status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800709 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800710
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700711 List<const CameraMetadata> requests;
712 requests.push_back(request);
713 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800714}
715
Jianing Wei90e59c92014-03-12 18:29:36 -0700716status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700717 const List<const CameraMetadata> &requests, bool repeating,
718 /*out*/
719 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700720 ATRACE_CALL();
721 Mutex::Autolock il(mInterfaceLock);
722 Mutex::Autolock l(mLock);
723
724 status_t res = checkStatusOkToCaptureLocked();
725 if (res != OK) {
726 // error logged by previous call
727 return res;
728 }
729
730 RequestList requestList;
731
732 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
733 if (res != OK) {
734 // error logged by previous call
735 return res;
736 }
737
738 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700739 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700740 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700741 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700742 }
743
744 if (res == OK) {
745 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
746 if (res != OK) {
747 SET_ERR_L("Can't transition to active in %f seconds!",
748 kActiveTimeout/1e9);
749 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700750 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
751 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700752 } else {
753 CLOGE("Cannot queue request. Impossible.");
754 return BAD_VALUE;
755 }
756
757 return res;
758}
759
Jianing Weicb0652e2014-03-12 18:29:36 -0700760status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
761 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700762 ATRACE_CALL();
763
Jianing Weicb0652e2014-03-12 18:29:36 -0700764 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700765}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800766
Jianing Weicb0652e2014-03-12 18:29:36 -0700767status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
768 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800769 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800770
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700771 List<const CameraMetadata> requests;
772 requests.push_back(request);
773 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800774}
775
Jianing Weicb0652e2014-03-12 18:29:36 -0700776status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
777 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700778 ATRACE_CALL();
779
Jianing Weicb0652e2014-03-12 18:29:36 -0700780 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700781}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800782
783sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
784 const CameraMetadata &request) {
785 status_t res;
786
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700787 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800788 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700789 // Stream configuration failed due to unsupported configuration.
790 // Device back to unconfigured state. Client might try other configuraitons
791 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
792 CLOGE("No streams configured");
793 return NULL;
794 }
795 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800796 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700797 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800798 return NULL;
799 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700800 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700801 if (mStatus == STATUS_UNCONFIGURED) {
802 CLOGE("No streams configured");
803 return NULL;
804 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800805 }
806
807 sp<CaptureRequest> newRequest = createCaptureRequest(request);
808 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800809}
810
Jianing Weicb0652e2014-03-12 18:29:36 -0700811status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800812 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700813 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800814 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800815
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800816 switch (mStatus) {
817 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700818 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800819 return INVALID_OPERATION;
820 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700821 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800822 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700823 case STATUS_UNCONFIGURED:
824 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800825 case STATUS_ACTIVE:
826 // OK
827 break;
828 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700829 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800830 return INVALID_OPERATION;
831 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700832 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700833
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700834 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800835}
836
837status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
838 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700839 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800840
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700841 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800842}
843
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700844status_t Camera3Device::createInputStream(
845 uint32_t width, uint32_t height, int format, int *id) {
846 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700847 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700848 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700849 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
850 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700851
852 status_t res;
853 bool wasActive = false;
854
855 switch (mStatus) {
856 case STATUS_ERROR:
857 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
858 return INVALID_OPERATION;
859 case STATUS_UNINITIALIZED:
860 ALOGE("%s: Device not initialized", __FUNCTION__);
861 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700862 case STATUS_UNCONFIGURED:
863 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700864 // OK
865 break;
866 case STATUS_ACTIVE:
867 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700868 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700869 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700870 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700871 return res;
872 }
873 wasActive = true;
874 break;
875 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700876 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700877 return INVALID_OPERATION;
878 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700879 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700880
881 if (mInputStream != 0) {
882 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
883 return INVALID_OPERATION;
884 }
885
886 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
887 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700888 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700889
890 mInputStream = newStream;
891
892 *id = mNextStreamId++;
893
894 // Continue captures if active at start
895 if (wasActive) {
896 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
897 res = configureStreamsLocked();
898 if (res != OK) {
899 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
900 __FUNCTION__, mNextStreamId, strerror(-res), res);
901 return res;
902 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700903 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700904 }
905
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700906 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700907 return OK;
908}
909
Igor Murashkin2fba5842013-04-22 14:03:54 -0700910
911status_t Camera3Device::createZslStream(
912 uint32_t width, uint32_t height,
913 int depth,
914 /*out*/
915 int *id,
916 sp<Camera3ZslStream>* zslStream) {
917 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700918 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700919 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700920 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
921 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700922
923 status_t res;
924 bool wasActive = false;
925
926 switch (mStatus) {
927 case STATUS_ERROR:
928 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
929 return INVALID_OPERATION;
930 case STATUS_UNINITIALIZED:
931 ALOGE("%s: Device not initialized", __FUNCTION__);
932 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700933 case STATUS_UNCONFIGURED:
934 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700935 // OK
936 break;
937 case STATUS_ACTIVE:
938 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700939 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700940 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700941 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700942 return res;
943 }
944 wasActive = true;
945 break;
946 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700947 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700948 return INVALID_OPERATION;
949 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700950 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700951
952 if (mInputStream != 0) {
953 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
954 return INVALID_OPERATION;
955 }
956
957 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
958 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700959 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700960
961 res = mOutputStreams.add(mNextStreamId, newStream);
962 if (res < 0) {
963 ALOGE("%s: Can't add new stream to set: %s (%d)",
964 __FUNCTION__, strerror(-res), res);
965 return res;
966 }
967 mInputStream = newStream;
968
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530969 mNeedConfig = true;
970
Igor Murashkin2fba5842013-04-22 14:03:54 -0700971 *id = mNextStreamId++;
972 *zslStream = newStream;
973
974 // Continue captures if active at start
975 if (wasActive) {
976 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
977 res = configureStreamsLocked();
978 if (res != OK) {
979 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
980 __FUNCTION__, mNextStreamId, strerror(-res), res);
981 return res;
982 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700983 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700984 }
985
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700986 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700987 return OK;
988}
989
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700990status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800991 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -0800992 camera3_stream_rotation_t rotation, int *id, int streamSetId) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800993 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700994 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800995 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700996 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
997 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800998
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800999 status_t res;
1000 bool wasActive = false;
1001
1002 switch (mStatus) {
1003 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001004 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001005 return INVALID_OPERATION;
1006 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001007 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001008 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001009 case STATUS_UNCONFIGURED:
1010 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001011 // OK
1012 break;
1013 case STATUS_ACTIVE:
1014 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001015 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001016 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001017 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001018 return res;
1019 }
1020 wasActive = true;
1021 break;
1022 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001023 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001024 return INVALID_OPERATION;
1025 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001026 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001027
1028 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001029 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001030 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001031 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001032 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1033 }
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001034 // Use legacy dataspace values for older HALs
1035 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1036 dataSpace = mapToLegacyDataspace(dataSpace);
1037 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001038 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001039 ssize_t blobBufferSize;
1040 if (dataSpace != HAL_DATASPACE_DEPTH) {
1041 blobBufferSize = getJpegBufferSize(width, height);
1042 if (blobBufferSize <= 0) {
1043 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1044 return BAD_VALUE;
1045 }
1046 } else {
1047 blobBufferSize = getPointCloudBufferSize();
1048 if (blobBufferSize <= 0) {
1049 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1050 return BAD_VALUE;
1051 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001052 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001054 width, height, blobBufferSize, format, dataSpace, rotation,
1055 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001056 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1057 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1058 if (rawOpaqueBufferSize <= 0) {
1059 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1060 return BAD_VALUE;
1061 }
1062 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001063 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1064 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001065 } else {
1066 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001067 width, height, format, dataSpace, rotation,
1068 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001070 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071
Zhijun He125684a2015-12-26 15:07:30 -08001072 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001073 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1074 * requires buffers to be statically allocated for internal static buffer registration, while
1075 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1076 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001077 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001078 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001079 newStream->setBufferManager(mBufferManager);
1080 }
1081
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001082 res = mOutputStreams.add(mNextStreamId, newStream);
1083 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001084 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001085 return res;
1086 }
1087
1088 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001089 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001090
1091 // Continue captures if active at start
1092 if (wasActive) {
1093 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1094 res = configureStreamsLocked();
1095 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001096 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1097 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001098 return res;
1099 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001100 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001101 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001102 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001103 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001104}
1105
1106status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1107 ATRACE_CALL();
1108 (void)outputId; (void)id;
1109
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001110 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001111 return INVALID_OPERATION;
1112}
1113
1114
1115status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001116 uint32_t *width, uint32_t *height,
1117 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001118 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001119 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001120 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001121
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001122 switch (mStatus) {
1123 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001124 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001125 return INVALID_OPERATION;
1126 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001127 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001128 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001129 case STATUS_UNCONFIGURED:
1130 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001131 case STATUS_ACTIVE:
1132 // OK
1133 break;
1134 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001135 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001136 return INVALID_OPERATION;
1137 }
1138
1139 ssize_t idx = mOutputStreams.indexOfKey(id);
1140 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001141 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001142 return idx;
1143 }
1144
1145 if (width) *width = mOutputStreams[idx]->getWidth();
1146 if (height) *height = mOutputStreams[idx]->getHeight();
1147 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001148 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001149 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001150}
1151
1152status_t Camera3Device::setStreamTransform(int id,
1153 int transform) {
1154 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001155 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001156 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001157
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001158 switch (mStatus) {
1159 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001160 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001161 return INVALID_OPERATION;
1162 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001163 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001164 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001165 case STATUS_UNCONFIGURED:
1166 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001167 case STATUS_ACTIVE:
1168 // OK
1169 break;
1170 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001171 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001172 return INVALID_OPERATION;
1173 }
1174
1175 ssize_t idx = mOutputStreams.indexOfKey(id);
1176 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001177 CLOGE("Stream %d does not exist",
1178 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001179 return BAD_VALUE;
1180 }
1181
1182 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001183}
1184
1185status_t Camera3Device::deleteStream(int id) {
1186 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001187 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001188 Mutex::Autolock l(mLock);
1189 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001190
Igor Murashkine2172be2013-05-28 15:31:39 -07001191 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1192
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001193 // CameraDevice semantics require device to already be idle before
1194 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001195 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001196 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1197 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001198 }
1199
Igor Murashkin2fba5842013-04-22 14:03:54 -07001200 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001201 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001202 if (mInputStream != NULL && id == mInputStream->getId()) {
1203 deletedStream = mInputStream;
1204 mInputStream.clear();
1205 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001206 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001207 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001208 return BAD_VALUE;
1209 }
Zhijun He5f446352014-01-22 09:49:33 -08001210 }
1211
1212 // Delete output stream or the output part of a bi-directional stream.
1213 if (outputStreamIdx != NAME_NOT_FOUND) {
1214 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001215 mOutputStreams.removeItem(id);
1216 }
1217
1218 // Free up the stream endpoint so that it can be used by some other stream
1219 res = deletedStream->disconnect();
1220 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001221 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001222 // fall through since we want to still list the stream as deleted.
1223 }
1224 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001225 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001226
1227 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001228}
1229
1230status_t Camera3Device::deleteReprocessStream(int id) {
1231 ATRACE_CALL();
1232 (void)id;
1233
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001234 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001235 return INVALID_OPERATION;
1236}
1237
Zhijun He1fa89992015-06-01 15:44:31 -07001238status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001239 ATRACE_CALL();
1240 ALOGV("%s: E", __FUNCTION__);
1241
1242 Mutex::Autolock il(mInterfaceLock);
1243 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001244
1245 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1246 mNeedConfig = true;
1247 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1248 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001249
1250 return configureStreamsLocked();
1251}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001252
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001253status_t Camera3Device::getInputBufferProducer(
1254 sp<IGraphicBufferProducer> *producer) {
1255 Mutex::Autolock il(mInterfaceLock);
1256 Mutex::Autolock l(mLock);
1257
1258 if (producer == NULL) {
1259 return BAD_VALUE;
1260 } else if (mInputStream == NULL) {
1261 return INVALID_OPERATION;
1262 }
1263
1264 return mInputStream->getInputBufferProducer(producer);
1265}
1266
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001267status_t Camera3Device::createDefaultRequest(int templateId,
1268 CameraMetadata *request) {
1269 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001270 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001271
1272 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1273 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1274 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1275 return BAD_VALUE;
1276 }
1277
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001278 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001279 Mutex::Autolock l(mLock);
1280
1281 switch (mStatus) {
1282 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001283 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001284 return INVALID_OPERATION;
1285 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001286 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001287 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001288 case STATUS_UNCONFIGURED:
1289 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001290 case STATUS_ACTIVE:
1291 // OK
1292 break;
1293 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001294 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001295 return INVALID_OPERATION;
1296 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001297
Zhijun Hea1530f12014-09-14 12:44:20 -07001298 if (!mRequestTemplateCache[templateId].isEmpty()) {
1299 *request = mRequestTemplateCache[templateId];
1300 return OK;
1301 }
1302
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001303 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001304 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001305 rawRequest = mHal3Device->ops->construct_default_request_settings(
1306 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001307 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001308 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001309 ALOGI("%s: template %d is not supported on this camera device",
1310 __FUNCTION__, templateId);
1311 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001312 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001313 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001314 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001315
1316 return OK;
1317}
1318
1319status_t Camera3Device::waitUntilDrained() {
1320 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001321 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001322 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001323
Zhijun He69a37482014-03-23 18:44:49 -07001324 return waitUntilDrainedLocked();
1325}
1326
1327status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001328 switch (mStatus) {
1329 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001330 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001331 ALOGV("%s: Already idle", __FUNCTION__);
1332 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001333 case STATUS_CONFIGURED:
1334 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001335 case STATUS_ERROR:
1336 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001337 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001338 break;
1339 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001340 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001341 return INVALID_OPERATION;
1342 }
1343
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001344 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1345 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001346 if (res != OK) {
1347 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1348 res);
1349 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001350 return res;
1351}
1352
Ruben Brunk183f0562015-08-12 12:55:02 -07001353
1354void Camera3Device::internalUpdateStatusLocked(Status status) {
1355 mStatus = status;
1356 mRecentStatusUpdates.add(mStatus);
1357 mStatusChanged.broadcast();
1358}
1359
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001360// Pause to reconfigure
1361status_t Camera3Device::internalPauseAndWaitLocked() {
1362 mRequestThread->setPaused(true);
1363 mPauseStateNotify = true;
1364
1365 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1366 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1367 if (res != OK) {
1368 SET_ERR_L("Can't idle device in %f seconds!",
1369 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001370 }
1371
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001372 return res;
1373}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001374
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001375// Resume after internalPauseAndWaitLocked
1376status_t Camera3Device::internalResumeLocked() {
1377 status_t res;
1378
1379 mRequestThread->setPaused(false);
1380
1381 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1382 if (res != OK) {
1383 SET_ERR_L("Can't transition to active in %f seconds!",
1384 kActiveTimeout/1e9);
1385 }
1386 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001387 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001388}
1389
Ruben Brunk183f0562015-08-12 12:55:02 -07001390status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001391 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001392
1393 size_t startIndex = 0;
1394 if (mStatusWaiters == 0) {
1395 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1396 // this status list
1397 mRecentStatusUpdates.clear();
1398 } else {
1399 // If other threads are waiting on updates to this status list, set the position of the
1400 // first element that this list will check rather than clearing the list.
1401 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001402 }
1403
Ruben Brunk183f0562015-08-12 12:55:02 -07001404 mStatusWaiters++;
1405
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001406 bool stateSeen = false;
1407 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001408 if (active == (mStatus == STATUS_ACTIVE)) {
1409 // Desired state is current
1410 break;
1411 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001412
1413 res = mStatusChanged.waitRelative(mLock, timeout);
1414 if (res != OK) break;
1415
Ruben Brunk183f0562015-08-12 12:55:02 -07001416 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1417 // transitions.
1418 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1419 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1420 __FUNCTION__);
1421
1422 // Encountered desired state since we began waiting
1423 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001424 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1425 stateSeen = true;
1426 break;
1427 }
1428 }
1429 } while (!stateSeen);
1430
Ruben Brunk183f0562015-08-12 12:55:02 -07001431 mStatusWaiters--;
1432
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001433 return res;
1434}
1435
1436
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001437status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1438 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001439 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001440
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001441 if (listener != NULL && mListener != NULL) {
1442 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1443 }
1444 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001445 mRequestThread->setNotificationListener(listener);
1446 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001447
1448 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001449}
1450
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001451bool Camera3Device::willNotify3A() {
1452 return false;
1453}
1454
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001455status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001456 status_t res;
1457 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001458
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001459 while (mResultQueue.empty()) {
1460 res = mResultSignal.waitRelative(mOutputLock, timeout);
1461 if (res == TIMED_OUT) {
1462 return res;
1463 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001464 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001465 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001466 return res;
1467 }
1468 }
1469 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001470}
1471
Jianing Weicb0652e2014-03-12 18:29:36 -07001472status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001473 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001474 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001475
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001476 if (mResultQueue.empty()) {
1477 return NOT_ENOUGH_DATA;
1478 }
1479
Jianing Weicb0652e2014-03-12 18:29:36 -07001480 if (frame == NULL) {
1481 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1482 return BAD_VALUE;
1483 }
1484
1485 CaptureResult &result = *(mResultQueue.begin());
1486 frame->mResultExtras = result.mResultExtras;
1487 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001488 mResultQueue.erase(mResultQueue.begin());
1489
1490 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001491}
1492
1493status_t Camera3Device::triggerAutofocus(uint32_t id) {
1494 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001495 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001496
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001497 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1498 // Mix-in this trigger into the next request and only the next request.
1499 RequestTrigger trigger[] = {
1500 {
1501 ANDROID_CONTROL_AF_TRIGGER,
1502 ANDROID_CONTROL_AF_TRIGGER_START
1503 },
1504 {
1505 ANDROID_CONTROL_AF_TRIGGER_ID,
1506 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001507 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001508 };
1509
1510 return mRequestThread->queueTrigger(trigger,
1511 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001512}
1513
1514status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1515 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001516 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001517
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001518 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1519 // Mix-in this trigger into the next request and only the next request.
1520 RequestTrigger trigger[] = {
1521 {
1522 ANDROID_CONTROL_AF_TRIGGER,
1523 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1524 },
1525 {
1526 ANDROID_CONTROL_AF_TRIGGER_ID,
1527 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001528 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001529 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001530
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001531 return mRequestThread->queueTrigger(trigger,
1532 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001533}
1534
1535status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1536 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001537 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001538
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001539 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1540 // Mix-in this trigger into the next request and only the next request.
1541 RequestTrigger trigger[] = {
1542 {
1543 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1544 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1545 },
1546 {
1547 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1548 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001549 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001550 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001551
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001552 return mRequestThread->queueTrigger(trigger,
1553 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001554}
1555
1556status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1557 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1558 ATRACE_CALL();
1559 (void)reprocessStreamId; (void)buffer; (void)listener;
1560
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001561 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001562 return INVALID_OPERATION;
1563}
1564
Jianing Weicb0652e2014-03-12 18:29:36 -07001565status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001566 ATRACE_CALL();
1567 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001568 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001569
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001570 NotificationListener* listener;
1571 {
1572 Mutex::Autolock l(mOutputLock);
1573 listener = mListener;
1574 }
1575
Zhijun He7ef20392014-04-21 16:04:17 -07001576 {
1577 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001578 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001579 }
1580
Zhijun He491e3412013-12-27 10:57:44 -08001581 status_t res;
1582 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001583 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001584 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001585 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001586 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001587 }
1588
1589 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001590}
1591
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001592status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001593 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1594}
1595
1596status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001597 ATRACE_CALL();
1598 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001599 Mutex::Autolock il(mInterfaceLock);
1600 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001601
1602 sp<Camera3StreamInterface> stream;
1603 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1604 if (outputStreamIdx == NAME_NOT_FOUND) {
1605 CLOGE("Stream %d does not exist", streamId);
1606 return BAD_VALUE;
1607 }
1608
1609 stream = mOutputStreams.editValueAt(outputStreamIdx);
1610
1611 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001612 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001613 return BAD_VALUE;
1614 }
1615
1616 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001617 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001618 return BAD_VALUE;
1619 }
1620
Ruben Brunkc78ac262015-08-13 17:58:46 -07001621 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001622}
1623
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001624status_t Camera3Device::tearDown(int streamId) {
1625 ATRACE_CALL();
1626 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1627 Mutex::Autolock il(mInterfaceLock);
1628 Mutex::Autolock l(mLock);
1629
1630 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1631 // since we cannot call register_stream_buffers except right after configure_streams.
1632 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1633 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1634 __FUNCTION__, mHal3Device->common.version);
1635 return NO_INIT;
1636 }
1637
1638 sp<Camera3StreamInterface> stream;
1639 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1640 if (outputStreamIdx == NAME_NOT_FOUND) {
1641 CLOGE("Stream %d does not exist", streamId);
1642 return BAD_VALUE;
1643 }
1644
1645 stream = mOutputStreams.editValueAt(outputStreamIdx);
1646
1647 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1648 CLOGE("Stream %d is a target of a in-progress request", streamId);
1649 return BAD_VALUE;
1650 }
1651
1652 return stream->tearDown();
1653}
1654
Zhijun He204e3292014-07-14 17:09:23 -07001655uint32_t Camera3Device::getDeviceVersion() {
1656 ATRACE_CALL();
1657 Mutex::Autolock il(mInterfaceLock);
1658 return mDeviceVersion;
1659}
1660
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001661/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001662 * Methods called by subclasses
1663 */
1664
1665void Camera3Device::notifyStatus(bool idle) {
1666 {
1667 // Need mLock to safely update state and synchronize to current
1668 // state of methods in flight.
1669 Mutex::Autolock l(mLock);
1670 // We can get various system-idle notices from the status tracker
1671 // while starting up. Only care about them if we've actually sent
1672 // in some requests recently.
1673 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1674 return;
1675 }
1676 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1677 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001678 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001679
1680 // Skip notifying listener if we're doing some user-transparent
1681 // state changes
1682 if (mPauseStateNotify) return;
1683 }
1684 NotificationListener *listener;
1685 {
1686 Mutex::Autolock l(mOutputLock);
1687 listener = mListener;
1688 }
1689 if (idle && listener != NULL) {
1690 listener->notifyIdle();
1691 }
1692}
1693
1694/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001695 * Camera3Device private methods
1696 */
1697
1698sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1699 const CameraMetadata &request) {
1700 ATRACE_CALL();
1701 status_t res;
1702
1703 sp<CaptureRequest> newRequest = new CaptureRequest;
1704 newRequest->mSettings = request;
1705
1706 camera_metadata_entry_t inputStreams =
1707 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1708 if (inputStreams.count > 0) {
1709 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001710 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001711 CLOGE("Request references unknown input stream %d",
1712 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001713 return NULL;
1714 }
1715 // Lazy completion of stream configuration (allocation/registration)
1716 // on first use
1717 if (mInputStream->isConfiguring()) {
1718 res = mInputStream->finishConfiguration(mHal3Device);
1719 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001720 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001721 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001722 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001723 return NULL;
1724 }
1725 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001726 // Check if stream is being prepared
1727 if (mInputStream->isPreparing()) {
1728 CLOGE("Request references an input stream that's being prepared!");
1729 return NULL;
1730 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001731
1732 newRequest->mInputStream = mInputStream;
1733 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1734 }
1735
1736 camera_metadata_entry_t streams =
1737 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1738 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001739 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001740 return NULL;
1741 }
1742
1743 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001744 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001745 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001746 CLOGE("Request references unknown stream %d",
1747 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001748 return NULL;
1749 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001750 sp<Camera3OutputStreamInterface> stream =
1751 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001752
1753 // Lazy completion of stream configuration (allocation/registration)
1754 // on first use
1755 if (stream->isConfiguring()) {
1756 res = stream->finishConfiguration(mHal3Device);
1757 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001758 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1759 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001760 return NULL;
1761 }
1762 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001763 // Check if stream is being prepared
1764 if (stream->isPreparing()) {
1765 CLOGE("Request references an output stream that's being prepared!");
1766 return NULL;
1767 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001768
1769 newRequest->mOutputStreams.push(stream);
1770 }
1771 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001772 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001773
1774 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001775}
1776
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001777bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1778 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1779 Size size = mSupportedOpaqueInputSizes[i];
1780 if (size.width == width && size.height == height) {
1781 return true;
1782 }
1783 }
1784
1785 return false;
1786}
1787
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001788status_t Camera3Device::configureStreamsLocked() {
1789 ATRACE_CALL();
1790 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001791
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001792 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001793 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001794 return INVALID_OPERATION;
1795 }
1796
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001797 if (!mNeedConfig) {
1798 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1799 return OK;
1800 }
1801
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001802 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1803 // adding a dummy stream instead.
1804 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1805 if (mOutputStreams.size() == 0) {
1806 addDummyStreamLocked();
1807 } else {
1808 tryRemoveDummyStreamLocked();
1809 }
1810
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001811 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001812 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001813
1814 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001815 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1816 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1817 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001818 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1819
1820 Vector<camera3_stream_t*> streams;
1821 streams.setCapacity(config.num_streams);
1822
1823 if (mInputStream != NULL) {
1824 camera3_stream_t *inputStream;
1825 inputStream = mInputStream->startConfiguration();
1826 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001827 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001828 return INVALID_OPERATION;
1829 }
1830 streams.add(inputStream);
1831 }
1832
1833 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001834
1835 // Don't configure bidi streams twice, nor add them twice to the list
1836 if (mOutputStreams[i].get() ==
1837 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1838
1839 config.num_streams--;
1840 continue;
1841 }
1842
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001843 camera3_stream_t *outputStream;
1844 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1845 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001846 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001847 return INVALID_OPERATION;
1848 }
1849 streams.add(outputStream);
1850 }
1851
1852 config.streams = streams.editArray();
1853
1854 // Do the HAL configuration; will potentially touch stream
1855 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001856 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001857 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001858 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001859
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001860 if (res == BAD_VALUE) {
1861 // HAL rejected this set of streams as unsupported, clean up config
1862 // attempt and return to unconfigured state
1863 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1864 res = mInputStream->cancelConfiguration();
1865 if (res != OK) {
1866 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1867 mInputStream->getId(), strerror(-res), res);
1868 return res;
1869 }
1870 }
1871
1872 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1873 sp<Camera3OutputStreamInterface> outputStream =
1874 mOutputStreams.editValueAt(i);
1875 if (outputStream->isConfiguring()) {
1876 res = outputStream->cancelConfiguration();
1877 if (res != OK) {
1878 SET_ERR_L(
1879 "Can't cancel configuring output stream %d: %s (%d)",
1880 outputStream->getId(), strerror(-res), res);
1881 return res;
1882 }
1883 }
1884 }
1885
1886 // Return state to that at start of call, so that future configures
1887 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001888 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001889 mNeedConfig = true;
1890
1891 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1892 return BAD_VALUE;
1893 } else if (res != OK) {
1894 // Some other kind of error from configure_streams - this is not
1895 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001896 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1897 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001898 return res;
1899 }
1900
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001901 // Finish all stream configuration immediately.
1902 // TODO: Try to relax this later back to lazy completion, which should be
1903 // faster
1904
Igor Murashkin073f8572013-05-02 14:59:28 -07001905 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001906 res = mInputStream->finishConfiguration(mHal3Device);
1907 if (res != OK) {
1908 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1909 mInputStream->getId(), strerror(-res), res);
1910 return res;
1911 }
1912 }
1913
1914 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001915 sp<Camera3OutputStreamInterface> outputStream =
1916 mOutputStreams.editValueAt(i);
1917 if (outputStream->isConfiguring()) {
1918 res = outputStream->finishConfiguration(mHal3Device);
1919 if (res != OK) {
1920 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1921 outputStream->getId(), strerror(-res), res);
1922 return res;
1923 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001924 }
1925 }
1926
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001927 // Request thread needs to know to avoid using repeat-last-settings protocol
1928 // across configure_streams() calls
1929 mRequestThread->configurationComplete();
1930
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07001931 // Boost priority of request thread for high speed recording to SCHED_FIFO
1932 if (mIsConstrainedHighSpeedConfiguration) {
1933 pid_t requestThreadTid = mRequestThread->getTid();
1934 res = requestPriority(getpid(), requestThreadTid,
1935 kConstrainedHighSpeedThreadPriority, true);
1936 if (res != OK) {
1937 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
1938 strerror(-res), res);
1939 } else {
1940 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
1941 }
1942 } else {
1943 // TODO: Set/restore normal priority for normal use cases
1944 }
1945
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001946 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001947
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001948 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001949
Ruben Brunk183f0562015-08-12 12:55:02 -07001950 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1951 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001952
1953 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1954
Zhijun He0a210512014-07-24 13:45:15 -07001955 // tear down the deleted streams after configure streams.
1956 mDeletedStreams.clear();
1957
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001958 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001959}
1960
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001961status_t Camera3Device::addDummyStreamLocked() {
1962 ATRACE_CALL();
1963 status_t res;
1964
1965 if (mDummyStreamId != NO_STREAM) {
1966 // Should never be adding a second dummy stream when one is already
1967 // active
1968 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1969 __FUNCTION__, mId);
1970 return INVALID_OPERATION;
1971 }
1972
1973 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1974
1975 sp<Camera3OutputStreamInterface> dummyStream =
1976 new Camera3DummyStream(mNextStreamId);
1977
1978 res = mOutputStreams.add(mNextStreamId, dummyStream);
1979 if (res < 0) {
1980 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1981 return res;
1982 }
1983
1984 mDummyStreamId = mNextStreamId;
1985 mNextStreamId++;
1986
1987 return OK;
1988}
1989
1990status_t Camera3Device::tryRemoveDummyStreamLocked() {
1991 ATRACE_CALL();
1992 status_t res;
1993
1994 if (mDummyStreamId == NO_STREAM) return OK;
1995 if (mOutputStreams.size() == 1) return OK;
1996
1997 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1998
1999 // Ok, have a dummy stream and there's at least one other output stream,
2000 // so remove the dummy
2001
2002 sp<Camera3StreamInterface> deletedStream;
2003 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2004 if (outputStreamIdx == NAME_NOT_FOUND) {
2005 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2006 return INVALID_OPERATION;
2007 }
2008
2009 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2010 mOutputStreams.removeItemsAt(outputStreamIdx);
2011
2012 // Free up the stream endpoint so that it can be used by some other stream
2013 res = deletedStream->disconnect();
2014 if (res != OK) {
2015 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2016 // fall through since we want to still list the stream as deleted.
2017 }
2018 mDeletedStreams.add(deletedStream);
2019 mDummyStreamId = NO_STREAM;
2020
2021 return res;
2022}
2023
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002024void Camera3Device::setErrorState(const char *fmt, ...) {
2025 Mutex::Autolock l(mLock);
2026 va_list args;
2027 va_start(args, fmt);
2028
2029 setErrorStateLockedV(fmt, args);
2030
2031 va_end(args);
2032}
2033
2034void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2035 Mutex::Autolock l(mLock);
2036 setErrorStateLockedV(fmt, args);
2037}
2038
2039void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2040 va_list args;
2041 va_start(args, fmt);
2042
2043 setErrorStateLockedV(fmt, args);
2044
2045 va_end(args);
2046}
2047
2048void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002049 // Print out all error messages to log
2050 String8 errorCause = String8::formatV(fmt, args);
2051 ALOGE("Camera %d: %s", mId, errorCause.string());
2052
2053 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002054 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002055
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002056 mErrorCause = errorCause;
2057
2058 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002059 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002060
2061 // Notify upstream about a device error
2062 if (mListener != NULL) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002063 mListener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002064 CaptureResultExtras());
2065 }
2066
2067 // Save stack trace. View by dumping it later.
2068 CameraTraces::saveTrace();
2069 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002070}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002071
2072/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002073 * In-flight request management
2074 */
2075
Jianing Weicb0652e2014-03-12 18:29:36 -07002076status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002077 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2078 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002079 ATRACE_CALL();
2080 Mutex::Autolock l(mInFlightLock);
2081
2082 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002083 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2084 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002085 if (res < 0) return res;
2086
2087 return OK;
2088}
2089
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002090void Camera3Device::returnOutputBuffers(
2091 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2092 nsecs_t timestamp) {
2093 for (size_t i = 0; i < numBuffers; i++)
2094 {
2095 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2096 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2097 // Note: stream may be deallocated at this point, if this buffer was
2098 // the last reference to it.
2099 if (res != OK) {
2100 ALOGE("Can't return buffer to its stream: %s (%d)",
2101 strerror(-res), res);
2102 }
2103 }
2104}
2105
2106
2107void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2108
2109 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2110 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2111
2112 nsecs_t sensorTimestamp = request.sensorTimestamp;
2113 nsecs_t shutterTimestamp = request.shutterTimestamp;
2114
2115 // Check if it's okay to remove the request from InFlightMap:
2116 // In the case of a successful request:
2117 // all input and output buffers, all result metadata, shutter callback
2118 // arrived.
2119 // In the case of a unsuccessful request:
2120 // all input and output buffers arrived.
2121 if (request.numBuffersLeft == 0 &&
2122 (request.requestStatus != OK ||
2123 (request.haveResultMetadata && shutterTimestamp != 0))) {
2124 ATRACE_ASYNC_END("frame capture", frameNumber);
2125
2126 // Sanity check - if sensor timestamp matches shutter timestamp
2127 if (request.requestStatus == OK &&
2128 sensorTimestamp != shutterTimestamp) {
2129 SET_ERR("sensor timestamp (%" PRId64
2130 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2131 sensorTimestamp, frameNumber, shutterTimestamp);
2132 }
2133
2134 // for an unsuccessful request, it may have pending output buffers to
2135 // return.
2136 assert(request.requestStatus != OK ||
2137 request.pendingOutputBuffers.size() == 0);
2138 returnOutputBuffers(request.pendingOutputBuffers.array(),
2139 request.pendingOutputBuffers.size(), 0);
2140
2141 mInFlightMap.removeItemsAt(idx, 1);
2142
2143 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2144 }
2145
2146 // Sanity check - if we have too many in-flight frames, something has
2147 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002148 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002149 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002150 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2151 kInFlightWarnLimitHighSpeed) {
2152 CLOGE("In-flight list too large for high speed configuration: %zu",
2153 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002154 }
2155}
2156
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002157void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2158 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2159 if (result == nullptr) return;
2160
2161 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2162 (int32_t*)&frameNumber, 1) != OK) {
2163 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2164 return;
2165 }
2166
2167 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2168 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2169 return;
2170 }
2171
2172 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2173
2174 // Valid result, insert into queue
2175 List<CaptureResult>::iterator queuedResult =
2176 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2177 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2178 ", burstId = %" PRId32, __FUNCTION__,
2179 queuedResult->mResultExtras.requestId,
2180 queuedResult->mResultExtras.frameNumber,
2181 queuedResult->mResultExtras.burstId);
2182
2183 mResultSignal.signal();
2184}
2185
2186
2187void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2188 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2189 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2190 Mutex::Autolock l(mOutputLock);
2191
2192 CaptureResult captureResult;
2193 captureResult.mResultExtras = resultExtras;
2194 captureResult.mMetadata = partialResult;
2195
2196 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2197}
2198
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002199
2200void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2201 CaptureResultExtras &resultExtras,
2202 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002203 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002204 bool reprocess,
2205 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002206 if (pendingMetadata.isEmpty())
2207 return;
2208
2209 Mutex::Autolock l(mOutputLock);
2210
2211 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002212 if (reprocess) {
2213 if (frameNumber < mNextReprocessResultFrameNumber) {
2214 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002215 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002216 frameNumber, mNextReprocessResultFrameNumber);
2217 return;
2218 }
2219 mNextReprocessResultFrameNumber = frameNumber + 1;
2220 } else {
2221 if (frameNumber < mNextResultFrameNumber) {
2222 SET_ERR("Out-of-order capture result metadata submitted! "
2223 "(got frame number %d, expecting %d)",
2224 frameNumber, mNextResultFrameNumber);
2225 return;
2226 }
2227 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002228 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002229
2230 CaptureResult captureResult;
2231 captureResult.mResultExtras = resultExtras;
2232 captureResult.mMetadata = pendingMetadata;
2233
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002234 // Append any previous partials to form a complete result
2235 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2236 captureResult.mMetadata.append(collectedPartialResult);
2237 }
2238
2239 captureResult.mMetadata.sort();
2240
2241 // Check that there's a timestamp in the result metadata
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002242 camera_metadata_entry entry = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002243 if (entry.count == 0) {
2244 SET_ERR("No timestamp provided by HAL for frame %d!",
2245 frameNumber);
2246 return;
2247 }
2248
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002249 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002250}
2251
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002252/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002253 * Camera HAL device callback methods
2254 */
2255
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002256void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002257 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002258
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002259 status_t res;
2260
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002261 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002262 if (result->result == NULL && result->num_output_buffers == 0 &&
2263 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002264 SET_ERR("No result data provided by HAL for frame %d",
2265 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002266 return;
2267 }
Zhijun He204e3292014-07-14 17:09:23 -07002268
2269 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2270 // partial_result to 1 when metadata is included in this result.
2271 if (!mUsePartialResult &&
2272 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2273 result->result != NULL &&
2274 result->partial_result != 1) {
2275 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2276 " if partial result is not supported",
2277 frameNumber, result->partial_result);
2278 return;
2279 }
2280
2281 bool isPartialResult = false;
2282 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002283 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002284 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002285
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002286 // Get shutter timestamp and resultExtras from list of in-flight requests,
2287 // where it was added by the shutter notification for this frame. If the
2288 // shutter timestamp isn't received yet, append the output buffers to the
2289 // in-flight request and they will be returned when the shutter timestamp
2290 // arrives. Update the in-flight status and remove the in-flight entry if
2291 // all result data and shutter timestamp have been received.
2292 nsecs_t shutterTimestamp = 0;
2293
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002294 {
2295 Mutex::Autolock l(mInFlightLock);
2296 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2297 if (idx == NAME_NOT_FOUND) {
2298 SET_ERR("Unknown frame number for capture result: %d",
2299 frameNumber);
2300 return;
2301 }
2302 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002303 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2304 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2305 ", partialResultCount = %d",
2306 __FUNCTION__, request.resultExtras.requestId,
2307 request.resultExtras.frameNumber, request.resultExtras.burstId,
2308 result->partial_result);
2309 // Always update the partial count to the latest one if it's not 0
2310 // (buffers only). When framework aggregates adjacent partial results
2311 // into one, the latest partial count will be used.
2312 if (result->partial_result != 0)
2313 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002314
2315 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002316 if (mUsePartialResult && result->result != NULL) {
2317 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2318 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2319 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2320 " the range of [1, %d] when metadata is included in the result",
2321 frameNumber, result->partial_result, mNumPartialResults);
2322 return;
2323 }
2324 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002325 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002326 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002327 }
Zhijun He204e3292014-07-14 17:09:23 -07002328 } else {
2329 camera_metadata_ro_entry_t partialResultEntry;
2330 res = find_camera_metadata_ro_entry(result->result,
2331 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2332 if (res != NAME_NOT_FOUND &&
2333 partialResultEntry.count > 0 &&
2334 partialResultEntry.data.u8[0] ==
2335 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2336 // A partial result. Flag this as such, and collect this
2337 // set of metadata into the in-flight entry.
2338 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002339 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002340 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002341 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002342 ANDROID_QUIRKS_PARTIAL_RESULT);
2343 }
2344 }
2345
2346 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002347 // Send partial capture result
2348 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2349 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002350 }
2351 }
2352
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002353 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002354 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002355
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002356 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002357 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002358 if (request.haveResultMetadata) {
2359 SET_ERR("Called multiple times with metadata for frame %d",
2360 frameNumber);
2361 return;
2362 }
Zhijun He204e3292014-07-14 17:09:23 -07002363 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002364 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002365 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002366 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002367 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002368 request.haveResultMetadata = true;
2369 }
2370
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002371 uint32_t numBuffersReturned = result->num_output_buffers;
2372 if (result->input_buffer != NULL) {
2373 if (hasInputBufferInRequest) {
2374 numBuffersReturned += 1;
2375 } else {
2376 ALOGW("%s: Input buffer should be NULL if there is no input"
2377 " buffer sent in the request",
2378 __FUNCTION__);
2379 }
2380 }
2381 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002382 if (request.numBuffersLeft < 0) {
2383 SET_ERR("Too many buffers returned for frame %d",
2384 frameNumber);
2385 return;
2386 }
2387
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002388 camera_metadata_ro_entry_t entry;
2389 res = find_camera_metadata_ro_entry(result->result,
2390 ANDROID_SENSOR_TIMESTAMP, &entry);
2391 if (res == OK && entry.count == 1) {
2392 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002393 }
2394
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002395 // If shutter event isn't received yet, append the output buffers to
2396 // the in-flight request. Otherwise, return the output buffers to
2397 // streams.
2398 if (shutterTimestamp == 0) {
2399 request.pendingOutputBuffers.appendArray(result->output_buffers,
2400 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002401 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002402 returnOutputBuffers(result->output_buffers,
2403 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002404 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002405
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002406 if (result->result != NULL && !isPartialResult) {
2407 if (shutterTimestamp == 0) {
2408 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002409 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002410 } else {
2411 CameraMetadata metadata;
2412 metadata = result->result;
2413 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002414 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2415 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002416 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002417 }
2418
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002419 removeInFlightRequestIfReadyLocked(idx);
2420 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002421
Zhijun Hef0d962a2014-06-30 10:24:11 -07002422 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002423 if (hasInputBufferInRequest) {
2424 Camera3Stream *stream =
2425 Camera3Stream::cast(result->input_buffer->stream);
2426 res = stream->returnInputBuffer(*(result->input_buffer));
2427 // Note: stream may be deallocated at this point, if this buffer was the
2428 // last reference to it.
2429 if (res != OK) {
2430 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2431 " its stream:%s (%d)", __FUNCTION__,
2432 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002433 }
2434 } else {
2435 ALOGW("%s: Input buffer should be NULL if there is no input"
2436 " buffer sent in the request, skipping input buffer return.",
2437 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002438 }
2439 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002440}
2441
2442void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002443 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002444 NotificationListener *listener;
2445 {
2446 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002447 listener = mListener;
2448 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002449
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002450 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002451 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002452 return;
2453 }
2454
2455 switch (msg->type) {
2456 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002457 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002458 break;
2459 }
2460 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002461 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002462 break;
2463 }
2464 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002465 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002466 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002467 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002468}
2469
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002470void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2471 NotificationListener *listener) {
2472
2473 // Map camera HAL error codes to ICameraDeviceCallback error codes
2474 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002475 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002476 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002477 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002478 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002479 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002480 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002481 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002482 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002483 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002484 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002485 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002486 };
2487
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002488 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002489 ((msg.error_code >= 0) &&
2490 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2491 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002492 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002493
2494 int streamId = 0;
2495 if (msg.error_stream != NULL) {
2496 Camera3Stream *stream =
2497 Camera3Stream::cast(msg.error_stream);
2498 streamId = stream->getId();
2499 }
2500 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2501 mId, __FUNCTION__, msg.frame_number,
2502 streamId, msg.error_code);
2503
2504 CaptureResultExtras resultExtras;
2505 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002506 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002507 // SET_ERR calls notifyError
2508 SET_ERR("Camera HAL reported serious device error");
2509 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002510 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2511 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2512 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002513 {
2514 Mutex::Autolock l(mInFlightLock);
2515 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2516 if (idx >= 0) {
2517 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2518 r.requestStatus = msg.error_code;
2519 resultExtras = r.resultExtras;
2520 } else {
2521 resultExtras.frameNumber = msg.frame_number;
2522 ALOGE("Camera %d: %s: cannot find in-flight request on "
2523 "frame %" PRId64 " error", mId, __FUNCTION__,
2524 resultExtras.frameNumber);
2525 }
2526 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002527 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002528 if (listener != NULL) {
2529 listener->notifyError(errorCode, resultExtras);
2530 } else {
2531 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2532 }
2533 break;
2534 default:
2535 // SET_ERR calls notifyError
2536 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2537 break;
2538 }
2539}
2540
2541void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2542 NotificationListener *listener) {
2543 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002544
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002545 // Set timestamp for the request in the in-flight tracking
2546 // and get the request ID to send upstream
2547 {
2548 Mutex::Autolock l(mInFlightLock);
2549 idx = mInFlightMap.indexOfKey(msg.frame_number);
2550 if (idx >= 0) {
2551 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002552
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002553 // Verify ordering of shutter notifications
2554 {
2555 Mutex::Autolock l(mOutputLock);
2556 // TODO: need to track errors for tighter bounds on expected frame number.
2557 if (r.hasInputBuffer) {
2558 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2559 SET_ERR("Shutter notification out-of-order. Expected "
2560 "notification for frame %d, got frame %d",
2561 mNextReprocessShutterFrameNumber, msg.frame_number);
2562 return;
2563 }
2564 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2565 } else {
2566 if (msg.frame_number < mNextShutterFrameNumber) {
2567 SET_ERR("Shutter notification out-of-order. Expected "
2568 "notification for frame %d, got frame %d",
2569 mNextShutterFrameNumber, msg.frame_number);
2570 return;
2571 }
2572 mNextShutterFrameNumber = msg.frame_number + 1;
2573 }
2574 }
2575
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002576 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2577 mId, __FUNCTION__,
2578 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2579 // Call listener, if any
2580 if (listener != NULL) {
2581 listener->notifyShutter(r.resultExtras, msg.timestamp);
2582 }
2583
2584 r.shutterTimestamp = msg.timestamp;
2585
2586 // send pending result and buffers
2587 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002588 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002589 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002590 returnOutputBuffers(r.pendingOutputBuffers.array(),
2591 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2592 r.pendingOutputBuffers.clear();
2593
2594 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002595 }
2596 }
2597 if (idx < 0) {
2598 SET_ERR("Shutter notification for non-existent frame number %d",
2599 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002600 }
2601}
2602
2603
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002604CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002605 ALOGV("%s", __FUNCTION__);
2606
Igor Murashkin1e479c02013-09-06 16:55:14 -07002607 CameraMetadata retVal;
2608
2609 if (mRequestThread != NULL) {
2610 retVal = mRequestThread->getLatestRequest();
2611 }
2612
Igor Murashkin1e479c02013-09-06 16:55:14 -07002613 return retVal;
2614}
2615
Jianing Weicb0652e2014-03-12 18:29:36 -07002616
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002617/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002618 * RequestThread inner class methods
2619 */
2620
2621Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002622 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002623 camera3_device_t *hal3Device,
2624 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002625 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002626 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002627 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002628 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002629 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002630 mReconfigured(false),
2631 mDoPause(false),
2632 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002633 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002634 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002635 mCurrentAfTriggerId(0),
2636 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002637 mRepeatingLastFrameNumber(
2638 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002639 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002640 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002641}
2642
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002643void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002644 NotificationListener *listener) {
2645 Mutex::Autolock l(mRequestLock);
2646 mListener = listener;
2647}
2648
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002649void Camera3Device::RequestThread::configurationComplete() {
2650 Mutex::Autolock l(mRequestLock);
2651 mReconfigured = true;
2652}
2653
Jianing Wei90e59c92014-03-12 18:29:36 -07002654status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002655 List<sp<CaptureRequest> > &requests,
2656 /*out*/
2657 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002658 Mutex::Autolock l(mRequestLock);
2659 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2660 ++it) {
2661 mRequestQueue.push_back(*it);
2662 }
2663
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002664 if (lastFrameNumber != NULL) {
2665 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2666 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2667 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2668 *lastFrameNumber);
2669 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002670
Jianing Wei90e59c92014-03-12 18:29:36 -07002671 unpauseForNewRequests();
2672
2673 return OK;
2674}
2675
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002676
2677status_t Camera3Device::RequestThread::queueTrigger(
2678 RequestTrigger trigger[],
2679 size_t count) {
2680
2681 Mutex::Autolock l(mTriggerMutex);
2682 status_t ret;
2683
2684 for (size_t i = 0; i < count; ++i) {
2685 ret = queueTriggerLocked(trigger[i]);
2686
2687 if (ret != OK) {
2688 return ret;
2689 }
2690 }
2691
2692 return OK;
2693}
2694
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002695int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2696 sp<Camera3Device> d = device.promote();
2697 if (d != NULL) return d->mId;
2698 return 0;
2699}
2700
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002701status_t Camera3Device::RequestThread::queueTriggerLocked(
2702 RequestTrigger trigger) {
2703
2704 uint32_t tag = trigger.metadataTag;
2705 ssize_t index = mTriggerMap.indexOfKey(tag);
2706
2707 switch (trigger.getTagType()) {
2708 case TYPE_BYTE:
2709 // fall-through
2710 case TYPE_INT32:
2711 break;
2712 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002713 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2714 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002715 return INVALID_OPERATION;
2716 }
2717
2718 /**
2719 * Collect only the latest trigger, since we only have 1 field
2720 * in the request settings per trigger tag, and can't send more than 1
2721 * trigger per request.
2722 */
2723 if (index != NAME_NOT_FOUND) {
2724 mTriggerMap.editValueAt(index) = trigger;
2725 } else {
2726 mTriggerMap.add(tag, trigger);
2727 }
2728
2729 return OK;
2730}
2731
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002732status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002733 const RequestList &requests,
2734 /*out*/
2735 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002736 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002737 if (lastFrameNumber != NULL) {
2738 *lastFrameNumber = mRepeatingLastFrameNumber;
2739 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002740 mRepeatingRequests.clear();
2741 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2742 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002743
2744 unpauseForNewRequests();
2745
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002746 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002747 return OK;
2748}
2749
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002750bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2751 if (mRepeatingRequests.empty()) {
2752 return false;
2753 }
2754 int32_t requestId = requestIn->mResultExtras.requestId;
2755 const RequestList &repeatRequests = mRepeatingRequests;
2756 // All repeating requests are guaranteed to have same id so only check first quest
2757 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2758 return (firstRequest->mResultExtras.requestId == requestId);
2759}
2760
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002761status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002762 Mutex::Autolock l(mRequestLock);
2763 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002764 if (lastFrameNumber != NULL) {
2765 *lastFrameNumber = mRepeatingLastFrameNumber;
2766 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002767 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002768 return OK;
2769}
2770
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002771status_t Camera3Device::RequestThread::clear(
2772 NotificationListener *listener,
2773 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002774 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002775 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002776
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002777 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002778
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002779 // Send errors for all requests pending in the request queue, including
2780 // pending repeating requests
2781 if (listener != NULL) {
2782 for (RequestList::iterator it = mRequestQueue.begin();
2783 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002784 // Abort the input buffers for reprocess requests.
2785 if ((*it)->mInputStream != NULL) {
2786 camera3_stream_buffer_t inputBuffer;
2787 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2788 if (res != OK) {
2789 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2790 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2791 } else {
2792 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2793 if (res != OK) {
2794 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2795 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2796 }
2797 }
2798 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002799 // Set the frame number this request would have had, if it
2800 // had been submitted; this frame number will not be reused.
2801 // The requestId and burstId fields were set when the request was
2802 // submitted originally (in convertMetadataListToRequestListLocked)
2803 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002804 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002805 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002806 }
2807 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002808 mRequestQueue.clear();
2809 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002810 if (lastFrameNumber != NULL) {
2811 *lastFrameNumber = mRepeatingLastFrameNumber;
2812 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002813 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002814 return OK;
2815}
2816
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002817status_t Camera3Device::RequestThread::flush() {
2818 ATRACE_CALL();
2819 Mutex::Autolock l(mFlushLock);
2820
2821 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2822 return mHal3Device->ops->flush(mHal3Device);
2823 }
2824
2825 return -ENOTSUP;
2826}
2827
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002828void Camera3Device::RequestThread::setPaused(bool paused) {
2829 Mutex::Autolock l(mPauseLock);
2830 mDoPause = paused;
2831 mDoPauseSignal.signal();
2832}
2833
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002834status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2835 int32_t requestId, nsecs_t timeout) {
2836 Mutex::Autolock l(mLatestRequestMutex);
2837 status_t res;
2838 while (mLatestRequestId != requestId) {
2839 nsecs_t startTime = systemTime();
2840
2841 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2842 if (res != OK) return res;
2843
2844 timeout -= (systemTime() - startTime);
2845 }
2846
2847 return OK;
2848}
2849
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002850void Camera3Device::RequestThread::requestExit() {
2851 // Call parent to set up shutdown
2852 Thread::requestExit();
2853 // The exit from any possible waits
2854 mDoPauseSignal.signal();
2855 mRequestSignal.signal();
2856}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002857
Chien-Yu Chend196d612015-06-22 19:49:01 -07002858
2859/**
2860 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2861 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2862 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2863 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2864 * request.
2865 */
2866void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2867 request->mAeTriggerCancelOverride.applyAeLock = false;
2868 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2869
2870 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2871 return;
2872 }
2873
2874 camera_metadata_entry_t aePrecaptureTrigger =
2875 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2876 if (aePrecaptureTrigger.count > 0 &&
2877 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2878 // Always override CANCEL to IDLE
2879 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2880 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2881 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2882 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2883 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2884
2885 if (mAeLockAvailable == true) {
2886 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2887 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2888 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2889 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2890 request->mAeTriggerCancelOverride.applyAeLock = true;
2891 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2892 }
2893 }
2894 }
2895}
2896
2897/**
2898 * Override result metadata for cancelling AE precapture trigger applied in
2899 * handleAePrecaptureCancelRequest().
2900 */
2901void Camera3Device::overrideResultForPrecaptureCancel(
2902 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2903 if (aeTriggerCancelOverride.applyAeLock) {
2904 // Only devices <= v3.2 should have this override
2905 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2906 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2907 }
2908
2909 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2910 // Only devices <= v3.2 should have this override
2911 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2912 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2913 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2914 }
2915}
2916
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002917bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002918 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002919 status_t res;
2920
2921 // Handle paused state.
2922 if (waitIfPaused()) {
2923 return true;
2924 }
2925
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002926 // Wait for the next batch of requests.
2927 waitForNextRequestBatch();
2928 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002929 return true;
2930 }
2931
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002932 // Get the latest request ID, if any
2933 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002934 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002935 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002936 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002937 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002938 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002939 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
2940 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002941 }
2942
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002943 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002944 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002945 if (res == TIMED_OUT) {
2946 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002947 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002948 return true;
2949 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002950 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002951 return false;
2952 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002953
Zhijun Hecc27e112013-10-03 16:12:43 -07002954 // Inform waitUntilRequestProcessed thread of a new request ID
2955 {
2956 Mutex::Autolock al(mLatestRequestMutex);
2957
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002958 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07002959 mLatestRequestSignal.signal();
2960 }
2961
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002962 // Submit a batch of requests to HAL.
2963 // Use flush lock only when submitting multilple requests in a batch.
2964 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
2965 // which may take a long time to finish so synchronizing flush() and
2966 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
2967 // For now, only synchronize for high speed recording and we should figure something out for
2968 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002969 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002970
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002971 if (useFlushLock) {
2972 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002973 }
2974
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002975 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002976 mNextRequests.size());
2977 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002978 // Submit request and block until ready for next one
2979 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
2980 ATRACE_BEGIN("camera3->process_capture_request");
2981 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
2982 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07002983
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002984 if (res != OK) {
2985 // Should only get a failure here for malformed requests or device-level
2986 // errors, so consider all errors fatal. Bad metadata failures should
2987 // come through notify.
2988 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
2989 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
2990 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002991 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002992 if (useFlushLock) {
2993 mFlushLock.unlock();
2994 }
2995 return false;
2996 }
2997
2998 // Mark that the request has be submitted successfully.
2999 nextRequest.submitted = true;
3000
3001 // Update the latest request sent to HAL
3002 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3003 Mutex::Autolock al(mLatestRequestMutex);
3004
3005 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3006 mLatestRequest.acquire(cloned);
3007 }
3008
3009 if (nextRequest.halRequest.settings != NULL) {
3010 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3011 }
3012
3013 // Remove any previously queued triggers (after unlock)
3014 res = removeTriggers(mPrevRequest);
3015 if (res != OK) {
3016 SET_ERR("RequestThread: Unable to remove triggers "
3017 "(capture request %d, HAL device: %s (%d)",
3018 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003019 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003020 if (useFlushLock) {
3021 mFlushLock.unlock();
3022 }
3023 return false;
3024 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003025 }
3026
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003027 if (useFlushLock) {
3028 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003029 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003030
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003031 // Unset as current request
3032 {
3033 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003034 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003035 }
3036
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003037 return true;
3038}
3039
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003040status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003041 ATRACE_CALL();
3042
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003043 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003044 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3045 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3046 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3047
3048 // Prepare a request to HAL
3049 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3050
3051 // Insert any queued triggers (before metadata is locked)
3052 status_t res = insertTriggers(captureRequest);
3053
3054 if (res < 0) {
3055 SET_ERR("RequestThread: Unable to insert triggers "
3056 "(capture request %d, HAL device: %s (%d)",
3057 halRequest->frame_number, strerror(-res), res);
3058 return INVALID_OPERATION;
3059 }
3060 int triggerCount = res;
3061 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3062 mPrevTriggers = triggerCount;
3063
3064 // If the request is the same as last, or we had triggers last time
3065 if (mPrevRequest != captureRequest || triggersMixedIn) {
3066 /**
3067 * HAL workaround:
3068 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3069 */
3070 res = addDummyTriggerIds(captureRequest);
3071 if (res != OK) {
3072 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3073 "(capture request %d, HAL device: %s (%d)",
3074 halRequest->frame_number, strerror(-res), res);
3075 return INVALID_OPERATION;
3076 }
3077
3078 /**
3079 * The request should be presorted so accesses in HAL
3080 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3081 */
3082 captureRequest->mSettings.sort();
3083 halRequest->settings = captureRequest->mSettings.getAndLock();
3084 mPrevRequest = captureRequest;
3085 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3086
3087 IF_ALOGV() {
3088 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3089 find_camera_metadata_ro_entry(
3090 halRequest->settings,
3091 ANDROID_CONTROL_AF_TRIGGER,
3092 &e
3093 );
3094 if (e.count > 0) {
3095 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3096 __FUNCTION__,
3097 halRequest->frame_number,
3098 e.data.u8[0]);
3099 }
3100 }
3101 } else {
3102 // leave request.settings NULL to indicate 'reuse latest given'
3103 ALOGVV("%s: Request settings are REUSED",
3104 __FUNCTION__);
3105 }
3106
3107 uint32_t totalNumBuffers = 0;
3108
3109 // Fill in buffers
3110 if (captureRequest->mInputStream != NULL) {
3111 halRequest->input_buffer = &captureRequest->mInputBuffer;
3112 totalNumBuffers += 1;
3113 } else {
3114 halRequest->input_buffer = NULL;
3115 }
3116
3117 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3118 captureRequest->mOutputStreams.size());
3119 halRequest->output_buffers = outputBuffers->array();
3120 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3121 res = captureRequest->mOutputStreams.editItemAt(i)->
3122 getBuffer(&outputBuffers->editItemAt(i));
3123 if (res != OK) {
3124 // Can't get output buffer from gralloc queue - this could be due to
3125 // abandoned queue or other consumer misbehavior, so not a fatal
3126 // error
3127 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3128 " %s (%d)", strerror(-res), res);
3129
3130 return TIMED_OUT;
3131 }
3132 halRequest->num_output_buffers++;
3133 }
3134 totalNumBuffers += halRequest->num_output_buffers;
3135
3136 // Log request in the in-flight queue
3137 sp<Camera3Device> parent = mParent.promote();
3138 if (parent == NULL) {
3139 // Should not happen, and nowhere to send errors to, so just log it
3140 CLOGE("RequestThread: Parent is gone");
3141 return INVALID_OPERATION;
3142 }
3143 res = parent->registerInFlight(halRequest->frame_number,
3144 totalNumBuffers, captureRequest->mResultExtras,
3145 /*hasInput*/halRequest->input_buffer != NULL,
3146 captureRequest->mAeTriggerCancelOverride);
3147 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3148 ", burstId = %" PRId32 ".",
3149 __FUNCTION__,
3150 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3151 captureRequest->mResultExtras.burstId);
3152 if (res != OK) {
3153 SET_ERR("RequestThread: Unable to register new in-flight request:"
3154 " %s (%d)", strerror(-res), res);
3155 return INVALID_OPERATION;
3156 }
3157 }
3158
3159 return OK;
3160}
3161
Igor Murashkin1e479c02013-09-06 16:55:14 -07003162CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3163 Mutex::Autolock al(mLatestRequestMutex);
3164
3165 ALOGV("RequestThread::%s", __FUNCTION__);
3166
3167 return mLatestRequest;
3168}
3169
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003170bool Camera3Device::RequestThread::isStreamPending(
3171 sp<Camera3StreamInterface>& stream) {
3172 Mutex::Autolock l(mRequestLock);
3173
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003174 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003175 if (!nextRequest.submitted) {
3176 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3177 if (stream == s) return true;
3178 }
3179 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003180 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003181 }
3182
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003183 for (const auto& request : mRequestQueue) {
3184 for (const auto& s : request->mOutputStreams) {
3185 if (stream == s) return true;
3186 }
3187 if (stream == request->mInputStream) return true;
3188 }
3189
3190 for (const auto& request : mRepeatingRequests) {
3191 for (const auto& s : request->mOutputStreams) {
3192 if (stream == s) return true;
3193 }
3194 if (stream == request->mInputStream) return true;
3195 }
3196
3197 return false;
3198}
Jianing Weicb0652e2014-03-12 18:29:36 -07003199
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003200void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3201 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003202 return;
3203 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003204
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003205 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003206 // Skip the ones that have been submitted successfully.
3207 if (nextRequest.submitted) {
3208 continue;
3209 }
3210
3211 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3212 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3213 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3214
3215 if (halRequest->settings != NULL) {
3216 captureRequest->mSettings.unlock(halRequest->settings);
3217 }
3218
3219 if (captureRequest->mInputStream != NULL) {
3220 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3221 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3222 }
3223
3224 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3225 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3226 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3227 }
3228
3229 if (sendRequestError) {
3230 Mutex::Autolock l(mRequestLock);
3231 if (mListener != NULL) {
3232 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003233 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003234 captureRequest->mResultExtras);
3235 }
3236 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003237 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003238
3239 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003240 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003241}
3242
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003243void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003244 // Optimized a bit for the simple steady-state case (single repeating
3245 // request), to avoid putting that request in the queue temporarily.
3246 Mutex::Autolock l(mRequestLock);
3247
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003248 assert(mNextRequests.empty());
3249
3250 NextRequest nextRequest;
3251 nextRequest.captureRequest = waitForNextRequestLocked();
3252 if (nextRequest.captureRequest == nullptr) {
3253 return;
3254 }
3255
3256 nextRequest.halRequest = camera3_capture_request_t();
3257 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003258 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003259
3260 // Wait for additional requests
3261 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3262
3263 for (size_t i = 1; i < batchSize; i++) {
3264 NextRequest additionalRequest;
3265 additionalRequest.captureRequest = waitForNextRequestLocked();
3266 if (additionalRequest.captureRequest == nullptr) {
3267 break;
3268 }
3269
3270 additionalRequest.halRequest = camera3_capture_request_t();
3271 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003272 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003273 }
3274
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003275 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003276 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003277 mNextRequests.size(), batchSize);
3278 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003279 }
3280
3281 return;
3282}
3283
3284sp<Camera3Device::CaptureRequest>
3285 Camera3Device::RequestThread::waitForNextRequestLocked() {
3286 status_t res;
3287 sp<CaptureRequest> nextRequest;
3288
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003289 while (mRequestQueue.empty()) {
3290 if (!mRepeatingRequests.empty()) {
3291 // Always atomically enqueue all requests in a repeating request
3292 // list. Guarantees a complete in-sequence set of captures to
3293 // application.
3294 const RequestList &requests = mRepeatingRequests;
3295 RequestList::const_iterator firstRequest =
3296 requests.begin();
3297 nextRequest = *firstRequest;
3298 mRequestQueue.insert(mRequestQueue.end(),
3299 ++firstRequest,
3300 requests.end());
3301 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003302
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003303 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003304
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003305 break;
3306 }
3307
3308 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3309
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003310 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3311 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003312 Mutex::Autolock pl(mPauseLock);
3313 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003314 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003315 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003316 // Let the tracker know
3317 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3318 if (statusTracker != 0) {
3319 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3320 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003321 }
3322 // Stop waiting for now and let thread management happen
3323 return NULL;
3324 }
3325 }
3326
3327 if (nextRequest == NULL) {
3328 // Don't have a repeating request already in hand, so queue
3329 // must have an entry now.
3330 RequestList::iterator firstRequest =
3331 mRequestQueue.begin();
3332 nextRequest = *firstRequest;
3333 mRequestQueue.erase(firstRequest);
3334 }
3335
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003336 // In case we've been unpaused by setPaused clearing mDoPause, need to
3337 // update internal pause state (capture/setRepeatingRequest unpause
3338 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003339 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003340 if (mPaused) {
3341 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3342 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3343 if (statusTracker != 0) {
3344 statusTracker->markComponentActive(mStatusId);
3345 }
3346 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003347 mPaused = false;
3348
3349 // Check if we've reconfigured since last time, and reset the preview
3350 // request if so. Can't use 'NULL request == repeat' across configure calls.
3351 if (mReconfigured) {
3352 mPrevRequest.clear();
3353 mReconfigured = false;
3354 }
3355
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003356 if (nextRequest != NULL) {
3357 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003358 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3359 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003360
3361 // Since RequestThread::clear() removes buffers from the input stream,
3362 // get the right buffer here before unlocking mRequestLock
3363 if (nextRequest->mInputStream != NULL) {
3364 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3365 if (res != OK) {
3366 // Can't get input buffer from gralloc queue - this could be due to
3367 // disconnected queue or other producer misbehavior, so not a fatal
3368 // error
3369 ALOGE("%s: Can't get input buffer, skipping request:"
3370 " %s (%d)", __FUNCTION__, strerror(-res), res);
3371 if (mListener != NULL) {
3372 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003373 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003374 nextRequest->mResultExtras);
3375 }
3376 return NULL;
3377 }
3378 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003379 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003380
3381 handleAePrecaptureCancelRequest(nextRequest);
3382
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003383 return nextRequest;
3384}
3385
3386bool Camera3Device::RequestThread::waitIfPaused() {
3387 status_t res;
3388 Mutex::Autolock l(mPauseLock);
3389 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003390 if (mPaused == false) {
3391 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003392 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3393 // Let the tracker know
3394 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3395 if (statusTracker != 0) {
3396 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3397 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003398 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003399
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003400 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003401 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003402 return true;
3403 }
3404 }
3405 // We don't set mPaused to false here, because waitForNextRequest needs
3406 // to further manage the paused state in case of starvation.
3407 return false;
3408}
3409
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003410void Camera3Device::RequestThread::unpauseForNewRequests() {
3411 // With work to do, mark thread as unpaused.
3412 // If paused by request (setPaused), don't resume, to avoid
3413 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003414 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003415 Mutex::Autolock p(mPauseLock);
3416 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003417 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3418 if (mPaused) {
3419 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3420 if (statusTracker != 0) {
3421 statusTracker->markComponentActive(mStatusId);
3422 }
3423 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003424 mPaused = false;
3425 }
3426}
3427
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003428void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3429 sp<Camera3Device> parent = mParent.promote();
3430 if (parent != NULL) {
3431 va_list args;
3432 va_start(args, fmt);
3433
3434 parent->setErrorStateV(fmt, args);
3435
3436 va_end(args);
3437 }
3438}
3439
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003440status_t Camera3Device::RequestThread::insertTriggers(
3441 const sp<CaptureRequest> &request) {
3442
3443 Mutex::Autolock al(mTriggerMutex);
3444
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003445 sp<Camera3Device> parent = mParent.promote();
3446 if (parent == NULL) {
3447 CLOGE("RequestThread: Parent is gone");
3448 return DEAD_OBJECT;
3449 }
3450
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003451 CameraMetadata &metadata = request->mSettings;
3452 size_t count = mTriggerMap.size();
3453
3454 for (size_t i = 0; i < count; ++i) {
3455 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003456 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003457
3458 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3459 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3460 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003461 if (isAeTrigger) {
3462 request->mResultExtras.precaptureTriggerId = triggerId;
3463 mCurrentPreCaptureTriggerId = triggerId;
3464 } else {
3465 request->mResultExtras.afTriggerId = triggerId;
3466 mCurrentAfTriggerId = triggerId;
3467 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003468 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3469 continue; // Trigger ID tag is deprecated since device HAL 3.2
3470 }
3471 }
3472
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003473 camera_metadata_entry entry = metadata.find(tag);
3474
3475 if (entry.count > 0) {
3476 /**
3477 * Already has an entry for this trigger in the request.
3478 * Rewrite it with our requested trigger value.
3479 */
3480 RequestTrigger oldTrigger = trigger;
3481
3482 oldTrigger.entryValue = entry.data.u8[0];
3483
3484 mTriggerReplacedMap.add(tag, oldTrigger);
3485 } else {
3486 /**
3487 * More typical, no trigger entry, so we just add it
3488 */
3489 mTriggerRemovedMap.add(tag, trigger);
3490 }
3491
3492 status_t res;
3493
3494 switch (trigger.getTagType()) {
3495 case TYPE_BYTE: {
3496 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3497 res = metadata.update(tag,
3498 &entryValue,
3499 /*count*/1);
3500 break;
3501 }
3502 case TYPE_INT32:
3503 res = metadata.update(tag,
3504 &trigger.entryValue,
3505 /*count*/1);
3506 break;
3507 default:
3508 ALOGE("%s: Type not supported: 0x%x",
3509 __FUNCTION__,
3510 trigger.getTagType());
3511 return INVALID_OPERATION;
3512 }
3513
3514 if (res != OK) {
3515 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3516 ", value %d", __FUNCTION__, trigger.getTagName(),
3517 trigger.entryValue);
3518 return res;
3519 }
3520
3521 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3522 trigger.getTagName(),
3523 trigger.entryValue);
3524 }
3525
3526 mTriggerMap.clear();
3527
3528 return count;
3529}
3530
3531status_t Camera3Device::RequestThread::removeTriggers(
3532 const sp<CaptureRequest> &request) {
3533 Mutex::Autolock al(mTriggerMutex);
3534
3535 CameraMetadata &metadata = request->mSettings;
3536
3537 /**
3538 * Replace all old entries with their old values.
3539 */
3540 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3541 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3542
3543 status_t res;
3544
3545 uint32_t tag = trigger.metadataTag;
3546 switch (trigger.getTagType()) {
3547 case TYPE_BYTE: {
3548 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3549 res = metadata.update(tag,
3550 &entryValue,
3551 /*count*/1);
3552 break;
3553 }
3554 case TYPE_INT32:
3555 res = metadata.update(tag,
3556 &trigger.entryValue,
3557 /*count*/1);
3558 break;
3559 default:
3560 ALOGE("%s: Type not supported: 0x%x",
3561 __FUNCTION__,
3562 trigger.getTagType());
3563 return INVALID_OPERATION;
3564 }
3565
3566 if (res != OK) {
3567 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3568 ", trigger value %d", __FUNCTION__,
3569 trigger.getTagName(), trigger.entryValue);
3570 return res;
3571 }
3572 }
3573 mTriggerReplacedMap.clear();
3574
3575 /**
3576 * Remove all new entries.
3577 */
3578 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3579 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3580 status_t res = metadata.erase(trigger.metadataTag);
3581
3582 if (res != OK) {
3583 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3584 ", trigger value %d", __FUNCTION__,
3585 trigger.getTagName(), trigger.entryValue);
3586 return res;
3587 }
3588 }
3589 mTriggerRemovedMap.clear();
3590
3591 return OK;
3592}
3593
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003594status_t Camera3Device::RequestThread::addDummyTriggerIds(
3595 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003596 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003597 static const int32_t dummyTriggerId = 1;
3598 status_t res;
3599
3600 CameraMetadata &metadata = request->mSettings;
3601
3602 // If AF trigger is active, insert a dummy AF trigger ID if none already
3603 // exists
3604 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3605 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3606 if (afTrigger.count > 0 &&
3607 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3608 afId.count == 0) {
3609 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3610 if (res != OK) return res;
3611 }
3612
3613 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3614 // if none already exists
3615 camera_metadata_entry pcTrigger =
3616 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3617 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3618 if (pcTrigger.count > 0 &&
3619 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3620 pcId.count == 0) {
3621 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3622 &dummyTriggerId, 1);
3623 if (res != OK) return res;
3624 }
3625
3626 return OK;
3627}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003628
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003629/**
3630 * PreparerThread inner class methods
3631 */
3632
3633Camera3Device::PreparerThread::PreparerThread() :
3634 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3635}
3636
3637Camera3Device::PreparerThread::~PreparerThread() {
3638 Thread::requestExitAndWait();
3639 if (mCurrentStream != nullptr) {
3640 mCurrentStream->cancelPrepare();
3641 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3642 mCurrentStream.clear();
3643 }
3644 clear();
3645}
3646
Ruben Brunkc78ac262015-08-13 17:58:46 -07003647status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003648 status_t res;
3649
3650 Mutex::Autolock l(mLock);
3651
Ruben Brunkc78ac262015-08-13 17:58:46 -07003652 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003653 if (res == OK) {
3654 // No preparation needed, fire listener right off
3655 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3656 if (mListener) {
3657 mListener->notifyPrepared(stream->getId());
3658 }
3659 return OK;
3660 } else if (res != NOT_ENOUGH_DATA) {
3661 return res;
3662 }
3663
3664 // Need to prepare, start up thread if necessary
3665 if (!mActive) {
3666 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3667 // isn't running
3668 Thread::requestExitAndWait();
3669 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3670 if (res != OK) {
3671 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3672 if (mListener) {
3673 mListener->notifyPrepared(stream->getId());
3674 }
3675 return res;
3676 }
3677 mCancelNow = false;
3678 mActive = true;
3679 ALOGV("%s: Preparer stream started", __FUNCTION__);
3680 }
3681
3682 // queue up the work
3683 mPendingStreams.push_back(stream);
3684 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3685
3686 return OK;
3687}
3688
3689status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003690 Mutex::Autolock l(mLock);
3691
3692 for (const auto& stream : mPendingStreams) {
3693 stream->cancelPrepare();
3694 }
3695 mPendingStreams.clear();
3696 mCancelNow = true;
3697
3698 return OK;
3699}
3700
3701void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3702 Mutex::Autolock l(mLock);
3703 mListener = listener;
3704}
3705
3706bool Camera3Device::PreparerThread::threadLoop() {
3707 status_t res;
3708 {
3709 Mutex::Autolock l(mLock);
3710 if (mCurrentStream == nullptr) {
3711 // End thread if done with work
3712 if (mPendingStreams.empty()) {
3713 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3714 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3715 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3716 mActive = false;
3717 return false;
3718 }
3719
3720 // Get next stream to prepare
3721 auto it = mPendingStreams.begin();
3722 mCurrentStream = *it;
3723 mPendingStreams.erase(it);
3724 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3725 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3726 } else if (mCancelNow) {
3727 mCurrentStream->cancelPrepare();
3728 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3729 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3730 mCurrentStream.clear();
3731 mCancelNow = false;
3732 return true;
3733 }
3734 }
3735
3736 res = mCurrentStream->prepareNextBuffer();
3737 if (res == NOT_ENOUGH_DATA) return true;
3738 if (res != OK) {
3739 // Something bad happened; try to recover by cancelling prepare and
3740 // signalling listener anyway
3741 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3742 mCurrentStream->getId(), res, strerror(-res));
3743 mCurrentStream->cancelPrepare();
3744 }
3745
3746 // This stream has finished, notify listener
3747 Mutex::Autolock l(mLock);
3748 if (mListener) {
3749 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3750 mCurrentStream->getId());
3751 mListener->notifyPrepared(mCurrentStream->getId());
3752 }
3753
3754 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3755 mCurrentStream.clear();
3756
3757 return true;
3758}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003759
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003760/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003761 * Static callback forwarding methods from HAL to instance
3762 */
3763
3764void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3765 const camera3_capture_result *result) {
3766 Camera3Device *d =
3767 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003768
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003769 d->processCaptureResult(result);
3770}
3771
3772void Camera3Device::sNotify(const camera3_callback_ops *cb,
3773 const camera3_notify_msg *msg) {
3774 Camera3Device *d =
3775 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3776 d->notify(msg);
3777}
3778
3779}; // namespace android