blob: 05c53235ef9964520f0fbc777c7b47f7fbb60c43 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046#include <android/hardware/camera2/ICameraDeviceUser.h>
47
Igor Murashkinff3e31d2013-10-23 16:40:06 -070048#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070049#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070050#include "device3/Camera3Device.h"
51#include "device3/Camera3OutputStream.h"
52#include "device3/Camera3InputStream.h"
53#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070054#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070055#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080056
57using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058
59namespace android {
60
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061Camera3Device::Camera3Device(int id):
62 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070063 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080064 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070065 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070066 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070067 mUsePartialResult(false),
68 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080069 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070070 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070071 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070072 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070073 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070074 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080075{
76 ATRACE_CALL();
77 camera3_callback_ops::notify = &sNotify;
78 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
79 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
80}
81
82Camera3Device::~Camera3Device()
83{
84 ATRACE_CALL();
85 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
86 disconnect();
87}
88
Igor Murashkin71381052013-03-04 14:53:08 -080089int Camera3Device::getId() const {
90 return mId;
91}
92
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080093/**
94 * CameraDeviceBase interface
95 */
96
Yin-Chia Yehe074a932015-01-30 10:29:02 -080097status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080098{
99 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700100 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800101 Mutex::Autolock l(mLock);
102
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800104 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700105 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800106 return INVALID_OPERATION;
107 }
108
109 /** Open HAL device */
110
111 status_t res;
112 String8 deviceName = String8::format("%d", mId);
113
114 camera3_device_t *device;
115
Zhijun He213ce792013-11-19 08:45:15 -0800116 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800117 res = module->open(deviceName.string(),
118 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800119 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800120
121 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800123 return res;
124 }
125
126 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700127 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700128 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700129 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700130 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800131 device->common.version);
132 device->common.close(&device->common);
133 return BAD_VALUE;
134 }
135
136 camera_info info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800137 res = module->getCameraInfo(mId, &info);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 if (res != OK) return res;
139
140 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700141 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
142 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700143 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800144 device->common.close(&device->common);
145 return BAD_VALUE;
146 }
147
148 /** Initialize device with callback functions */
149
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700150 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700152 ATRACE_END();
153
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800154 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700155 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
156 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800157 device->common.close(&device->common);
158 return BAD_VALUE;
159 }
160
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700161 /** Start up status tracker thread */
162 mStatusTracker = new StatusTracker(this);
163 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
164 if (res != OK) {
165 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
166 strerror(-res), res);
167 device->common.close(&device->common);
168 mStatusTracker.clear();
169 return res;
170 }
171
Zhijun He125684a2015-12-26 15:07:30 -0800172 /** Create buffer manager */
173 mBufferManager = new Camera3BufferManager();
174
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700175 bool aeLockAvailable = false;
176 camera_metadata_ro_entry aeLockAvailableEntry;
177 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
178 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
179 if (res == OK && aeLockAvailableEntry.count > 0) {
180 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
181 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
182 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800183
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700184 /** Start up request queue thread */
185 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800186 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700188 SET_ERR_L("Unable to start request queue thread: %s (%d)",
189 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800191 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 return res;
193 }
194
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700195 mPreparerThread = new PreparerThread();
196
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800197 /** Everything is good to go */
198
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700199 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800200 mDeviceInfo = info.static_camera_characteristics;
201 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700202
203 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800204 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700205 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700206 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700207 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800208
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800209 // Measure the clock domain offset between camera and video/hw_composer
210 camera_metadata_entry timestampSource =
211 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
212 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
213 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
214 mTimestampOffset = getMonoToBoottimeOffset();
215 }
216
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700217 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700218 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
219 camera_metadata_entry partialResultsCount =
220 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
221 if (partialResultsCount.count > 0) {
222 mNumPartialResults = partialResultsCount.data.i32[0];
223 mUsePartialResult = (mNumPartialResults > 1);
224 }
225 } else {
226 camera_metadata_entry partialResultsQuirk =
227 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
228 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
229 mUsePartialResult = true;
230 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700231 }
232
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700233 camera_metadata_entry configs =
234 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
235 for (uint32_t i = 0; i < configs.count; i += 4) {
236 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
237 configs.data.i32[i + 3] ==
238 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
239 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
240 configs.data.i32[i + 2]));
241 }
242 }
243
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800244 return OK;
245}
246
247status_t Camera3Device::disconnect() {
248 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700249 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800250
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800251 ALOGV("%s: E", __FUNCTION__);
252
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700253 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800254
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700255 {
256 Mutex::Autolock l(mLock);
257 if (mStatus == STATUS_UNINITIALIZED) return res;
258
259 if (mStatus == STATUS_ACTIVE ||
260 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
261 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700262 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700263 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700264 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700265 } else {
266 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
267 if (res != OK) {
268 SET_ERR_L("Timeout waiting for HAL to drain");
269 // Continue to close device even in case of error
270 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700271 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800272 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800273
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700274 if (mStatus == STATUS_ERROR) {
275 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700276 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277
278 if (mStatusTracker != NULL) {
279 mStatusTracker->requestExit();
280 }
281
282 if (mRequestThread != NULL) {
283 mRequestThread->requestExit();
284 }
285
286 mOutputStreams.clear();
287 mInputStream.clear();
288 }
289
290 // Joining done without holding mLock, otherwise deadlocks may ensue
291 // as the threads try to access parent state
292 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
293 // HAL may be in a bad state, so waiting for request thread
294 // (which may be stuck in the HAL processCaptureRequest call)
295 // could be dangerous.
296 mRequestThread->join();
297 }
298
299 if (mStatusTracker != NULL) {
300 mStatusTracker->join();
301 }
302
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700303 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700304 {
305 Mutex::Autolock l(mLock);
306
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800307 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700308 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800309 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800310
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700311 hal3Device = mHal3Device;
312 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800313
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700314 // Call close without internal mutex held, as the HAL close may need to
315 // wait on assorted callbacks,etc, to complete before it can return.
316 if (hal3Device != NULL) {
317 ATRACE_BEGIN("camera3->close");
318 hal3Device->common.close(&hal3Device->common);
319 ATRACE_END();
320 }
321
322 {
323 Mutex::Autolock l(mLock);
324 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700325 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700326 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800327
328 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700329 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800330}
331
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700332// For dumping/debugging only -
333// try to acquire a lock a few times, eventually give up to proceed with
334// debug/dump operations
335bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
336 bool gotLock = false;
337 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
338 if (lock.tryLock() == NO_ERROR) {
339 gotLock = true;
340 break;
341 } else {
342 usleep(kDumpSleepDuration);
343 }
344 }
345 return gotLock;
346}
347
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700348Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
349 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
350 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
351 const int STREAM_CONFIGURATION_SIZE = 4;
352 const int STREAM_FORMAT_OFFSET = 0;
353 const int STREAM_WIDTH_OFFSET = 1;
354 const int STREAM_HEIGHT_OFFSET = 2;
355 const int STREAM_IS_INPUT_OFFSET = 3;
356 camera_metadata_ro_entry_t availableStreamConfigs =
357 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
358 if (availableStreamConfigs.count == 0 ||
359 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
360 return Size(0, 0);
361 }
362
363 // Get max jpeg size (area-wise).
364 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
365 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
366 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
367 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
368 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
369 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
370 && format == HAL_PIXEL_FORMAT_BLOB &&
371 (width * height > maxJpegWidth * maxJpegHeight)) {
372 maxJpegWidth = width;
373 maxJpegHeight = height;
374 }
375 }
376 } else {
377 camera_metadata_ro_entry availableJpegSizes =
378 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
379 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
380 return Size(0, 0);
381 }
382
383 // Get max jpeg size (area-wise).
384 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
385 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
386 > (maxJpegWidth * maxJpegHeight)) {
387 maxJpegWidth = availableJpegSizes.data.i32[i];
388 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
389 }
390 }
391 }
392 return Size(maxJpegWidth, maxJpegHeight);
393}
394
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800395nsecs_t Camera3Device::getMonoToBoottimeOffset() {
396 // try three times to get the clock offset, choose the one
397 // with the minimum gap in measurements.
398 const int tries = 3;
399 nsecs_t bestGap, measured;
400 for (int i = 0; i < tries; ++i) {
401 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
402 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
403 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
404 const nsecs_t gap = tmono2 - tmono;
405 if (i == 0 || gap < bestGap) {
406 bestGap = gap;
407 measured = tbase - ((tmono + tmono2) >> 1);
408 }
409 }
410 return measured;
411}
412
Zhijun Hef7da0962014-04-24 13:27:56 -0700413ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700414 // Get max jpeg size (area-wise).
415 Size maxJpegResolution = getMaxJpegResolution();
416 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700417 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700418 __FUNCTION__, mId);
419 return BAD_VALUE;
420 }
421
Zhijun Hef7da0962014-04-24 13:27:56 -0700422 // Get max jpeg buffer size
423 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700424 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
425 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700426 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
427 return BAD_VALUE;
428 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700429 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800430 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700431
432 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700433 float scaleFactor = ((float) (width * height)) /
434 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800435 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
436 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700437 if (jpegBufferSize > maxJpegBufferSize) {
438 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700439 }
440
441 return jpegBufferSize;
442}
443
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700444ssize_t Camera3Device::getPointCloudBufferSize() const {
445 const int FLOATS_PER_POINT=4;
446 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
447 if (maxPointCount.count == 0) {
448 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
449 __FUNCTION__, mId);
450 return BAD_VALUE;
451 }
452 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
453 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
454 return maxBytesForPointCloud;
455}
456
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800457ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800458 const int PER_CONFIGURATION_SIZE = 3;
459 const int WIDTH_OFFSET = 0;
460 const int HEIGHT_OFFSET = 1;
461 const int SIZE_OFFSET = 2;
462 camera_metadata_ro_entry rawOpaqueSizes =
463 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800464 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800465 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800466 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800467 __FUNCTION__, mId, count);
468 return BAD_VALUE;
469 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700470
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800471 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
472 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
473 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
474 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
475 }
476 }
477
478 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
479 __FUNCTION__, mId, width, height);
480 return BAD_VALUE;
481}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700482
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800483status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
484 ATRACE_CALL();
485 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700486
487 // Try to lock, but continue in case of failure (to avoid blocking in
488 // deadlocks)
489 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
490 bool gotLock = tryLockSpinRightRound(mLock);
491
492 ALOGW_IF(!gotInterfaceLock,
493 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
494 mId, __FUNCTION__);
495 ALOGW_IF(!gotLock,
496 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
497 mId, __FUNCTION__);
498
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800499 bool dumpTemplates = false;
500 String16 templatesOption("-t");
501 int n = args.size();
502 for (int i = 0; i < n; i++) {
503 if (args[i] == templatesOption) {
504 dumpTemplates = true;
505 }
506 }
507
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800508 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800509
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800510 const char *status =
511 mStatus == STATUS_ERROR ? "ERROR" :
512 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700513 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
514 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800515 mStatus == STATUS_ACTIVE ? "ACTIVE" :
516 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700517
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800518 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700519 if (mStatus == STATUS_ERROR) {
520 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
521 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800522 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700523 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700524 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800525
526 if (mInputStream != NULL) {
527 write(fd, lines.string(), lines.size());
528 mInputStream->dump(fd, args);
529 } else {
530 lines.appendFormat(" No input stream.\n");
531 write(fd, lines.string(), lines.size());
532 }
533 for (size_t i = 0; i < mOutputStreams.size(); i++) {
534 mOutputStreams[i]->dump(fd,args);
535 }
536
Zhijun He431503c2016-03-07 17:30:16 -0800537 if (mBufferManager != NULL) {
538 lines = String8(" Camera3 Buffer Manager:\n");
539 write(fd, lines.string(), lines.size());
540 mBufferManager->dump(fd, args);
541 }
Zhijun He125684a2015-12-26 15:07:30 -0800542
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700543 lines = String8(" In-flight requests:\n");
544 if (mInFlightMap.size() == 0) {
545 lines.append(" None\n");
546 } else {
547 for (size_t i = 0; i < mInFlightMap.size(); i++) {
548 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700549 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700550 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800551 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700552 r.numBuffersLeft);
553 }
554 }
555 write(fd, lines.string(), lines.size());
556
Igor Murashkin1e479c02013-09-06 16:55:14 -0700557 {
558 lines = String8(" Last request sent:\n");
559 write(fd, lines.string(), lines.size());
560
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700561 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700562 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
563 }
564
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800565 if (dumpTemplates) {
566 const char *templateNames[] = {
567 "TEMPLATE_PREVIEW",
568 "TEMPLATE_STILL_CAPTURE",
569 "TEMPLATE_VIDEO_RECORD",
570 "TEMPLATE_VIDEO_SNAPSHOT",
571 "TEMPLATE_ZERO_SHUTTER_LAG",
572 "TEMPLATE_MANUAL"
573 };
574
575 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
576 const camera_metadata_t *templateRequest;
577 templateRequest =
578 mHal3Device->ops->construct_default_request_settings(
579 mHal3Device, i);
580 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
581 if (templateRequest == NULL) {
582 lines.append(" Not supported\n");
583 write(fd, lines.string(), lines.size());
584 } else {
585 write(fd, lines.string(), lines.size());
586 dump_indented_camera_metadata(templateRequest,
587 fd, /*verbosity*/2, /*indentation*/8);
588 }
589 }
590 }
591
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800592 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700593 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800594 write(fd, lines.string(), lines.size());
595 mHal3Device->ops->dump(mHal3Device, fd);
596 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800597
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700598 if (gotLock) mLock.unlock();
599 if (gotInterfaceLock) mInterfaceLock.unlock();
600
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800601 return OK;
602}
603
604const CameraMetadata& Camera3Device::info() const {
605 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800606 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
607 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700608 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800609 mStatus == STATUS_ERROR ?
610 "when in error state" : "before init");
611 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800612 return mDeviceInfo;
613}
614
Jianing Wei90e59c92014-03-12 18:29:36 -0700615status_t Camera3Device::checkStatusOkToCaptureLocked() {
616 switch (mStatus) {
617 case STATUS_ERROR:
618 CLOGE("Device has encountered a serious error");
619 return INVALID_OPERATION;
620 case STATUS_UNINITIALIZED:
621 CLOGE("Device not initialized");
622 return INVALID_OPERATION;
623 case STATUS_UNCONFIGURED:
624 case STATUS_CONFIGURED:
625 case STATUS_ACTIVE:
626 // OK
627 break;
628 default:
629 SET_ERR_L("Unexpected status: %d", mStatus);
630 return INVALID_OPERATION;
631 }
632 return OK;
633}
634
635status_t Camera3Device::convertMetadataListToRequestListLocked(
636 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
637 if (requestList == NULL) {
638 CLOGE("requestList cannot be NULL.");
639 return BAD_VALUE;
640 }
641
Jianing Weicb0652e2014-03-12 18:29:36 -0700642 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700643 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
644 it != metadataList.end(); ++it) {
645 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
646 if (newRequest == 0) {
647 CLOGE("Can't create capture request");
648 return BAD_VALUE;
649 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700650
651 // Setup burst Id and request Id
652 newRequest->mResultExtras.burstId = burstId++;
653 if (it->exists(ANDROID_REQUEST_ID)) {
654 if (it->find(ANDROID_REQUEST_ID).count == 0) {
655 CLOGE("RequestID entry exists; but must not be empty in metadata");
656 return BAD_VALUE;
657 }
658 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
659 } else {
660 CLOGE("RequestID does not exist in metadata");
661 return BAD_VALUE;
662 }
663
Jianing Wei90e59c92014-03-12 18:29:36 -0700664 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700665
666 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700667 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700668
669 // Setup batch size if this is a high speed video recording request.
670 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
671 auto firstRequest = requestList->begin();
672 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
673 if (outputStream->isVideoStream()) {
674 (*firstRequest)->mBatchSize = requestList->size();
675 break;
676 }
677 }
678 }
679
Jianing Wei90e59c92014-03-12 18:29:36 -0700680 return OK;
681}
682
Jianing Weicb0652e2014-03-12 18:29:36 -0700683status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800684 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800685
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700686 List<const CameraMetadata> requests;
687 requests.push_back(request);
688 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800689}
690
Jianing Wei90e59c92014-03-12 18:29:36 -0700691status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700692 const List<const CameraMetadata> &requests, bool repeating,
693 /*out*/
694 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700695 ATRACE_CALL();
696 Mutex::Autolock il(mInterfaceLock);
697 Mutex::Autolock l(mLock);
698
699 status_t res = checkStatusOkToCaptureLocked();
700 if (res != OK) {
701 // error logged by previous call
702 return res;
703 }
704
705 RequestList requestList;
706
707 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
708 if (res != OK) {
709 // error logged by previous call
710 return res;
711 }
712
713 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700714 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700715 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700716 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700717 }
718
719 if (res == OK) {
720 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
721 if (res != OK) {
722 SET_ERR_L("Can't transition to active in %f seconds!",
723 kActiveTimeout/1e9);
724 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700725 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
726 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700727 } else {
728 CLOGE("Cannot queue request. Impossible.");
729 return BAD_VALUE;
730 }
731
732 return res;
733}
734
Jianing Weicb0652e2014-03-12 18:29:36 -0700735status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
736 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700737 ATRACE_CALL();
738
Jianing Weicb0652e2014-03-12 18:29:36 -0700739 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700740}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800741
Jianing Weicb0652e2014-03-12 18:29:36 -0700742status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
743 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800744 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800745
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700746 List<const CameraMetadata> requests;
747 requests.push_back(request);
748 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800749}
750
Jianing Weicb0652e2014-03-12 18:29:36 -0700751status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
752 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700753 ATRACE_CALL();
754
Jianing Weicb0652e2014-03-12 18:29:36 -0700755 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700756}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800757
758sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
759 const CameraMetadata &request) {
760 status_t res;
761
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700762 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800763 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700764 // Stream configuration failed due to unsupported configuration.
765 // Device back to unconfigured state. Client might try other configuraitons
766 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
767 CLOGE("No streams configured");
768 return NULL;
769 }
770 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800771 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700772 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800773 return NULL;
774 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700775 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700776 if (mStatus == STATUS_UNCONFIGURED) {
777 CLOGE("No streams configured");
778 return NULL;
779 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800780 }
781
782 sp<CaptureRequest> newRequest = createCaptureRequest(request);
783 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800784}
785
Jianing Weicb0652e2014-03-12 18:29:36 -0700786status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800787 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700788 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800789 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800790
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800791 switch (mStatus) {
792 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700793 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800794 return INVALID_OPERATION;
795 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700796 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800797 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700798 case STATUS_UNCONFIGURED:
799 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800800 case STATUS_ACTIVE:
801 // OK
802 break;
803 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700804 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800805 return INVALID_OPERATION;
806 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700807 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700808
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700809 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800810}
811
812status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
813 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700814 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800815
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700816 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800817}
818
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700819status_t Camera3Device::createInputStream(
820 uint32_t width, uint32_t height, int format, int *id) {
821 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700822 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700823 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700824 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
825 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700826
827 status_t res;
828 bool wasActive = false;
829
830 switch (mStatus) {
831 case STATUS_ERROR:
832 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
833 return INVALID_OPERATION;
834 case STATUS_UNINITIALIZED:
835 ALOGE("%s: Device not initialized", __FUNCTION__);
836 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700837 case STATUS_UNCONFIGURED:
838 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700839 // OK
840 break;
841 case STATUS_ACTIVE:
842 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700843 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700844 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700845 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700846 return res;
847 }
848 wasActive = true;
849 break;
850 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700851 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700852 return INVALID_OPERATION;
853 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700854 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700855
856 if (mInputStream != 0) {
857 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
858 return INVALID_OPERATION;
859 }
860
861 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
862 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700863 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700864
865 mInputStream = newStream;
866
867 *id = mNextStreamId++;
868
869 // Continue captures if active at start
870 if (wasActive) {
871 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
872 res = configureStreamsLocked();
873 if (res != OK) {
874 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
875 __FUNCTION__, mNextStreamId, strerror(-res), res);
876 return res;
877 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700878 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700879 }
880
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700881 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700882 return OK;
883}
884
Igor Murashkin2fba5842013-04-22 14:03:54 -0700885
886status_t Camera3Device::createZslStream(
887 uint32_t width, uint32_t height,
888 int depth,
889 /*out*/
890 int *id,
891 sp<Camera3ZslStream>* zslStream) {
892 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700893 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700894 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700895 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
896 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700897
898 status_t res;
899 bool wasActive = false;
900
901 switch (mStatus) {
902 case STATUS_ERROR:
903 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
904 return INVALID_OPERATION;
905 case STATUS_UNINITIALIZED:
906 ALOGE("%s: Device not initialized", __FUNCTION__);
907 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700908 case STATUS_UNCONFIGURED:
909 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700910 // OK
911 break;
912 case STATUS_ACTIVE:
913 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700914 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700915 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700916 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700917 return res;
918 }
919 wasActive = true;
920 break;
921 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700922 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700923 return INVALID_OPERATION;
924 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700925 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700926
927 if (mInputStream != 0) {
928 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
929 return INVALID_OPERATION;
930 }
931
932 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
933 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700934 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700935
936 res = mOutputStreams.add(mNextStreamId, newStream);
937 if (res < 0) {
938 ALOGE("%s: Can't add new stream to set: %s (%d)",
939 __FUNCTION__, strerror(-res), res);
940 return res;
941 }
942 mInputStream = newStream;
943
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530944 mNeedConfig = true;
945
Igor Murashkin2fba5842013-04-22 14:03:54 -0700946 *id = mNextStreamId++;
947 *zslStream = newStream;
948
949 // Continue captures if active at start
950 if (wasActive) {
951 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
952 res = configureStreamsLocked();
953 if (res != OK) {
954 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
955 __FUNCTION__, mNextStreamId, strerror(-res), res);
956 return res;
957 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700958 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700959 }
960
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700961 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700962 return OK;
963}
964
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700965status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800966 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -0800967 camera3_stream_rotation_t rotation, int *id, int streamSetId) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800968 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700969 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800970 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700971 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
972 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800973
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800974 status_t res;
975 bool wasActive = false;
976
977 switch (mStatus) {
978 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700979 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800980 return INVALID_OPERATION;
981 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700982 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800983 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700984 case STATUS_UNCONFIGURED:
985 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800986 // OK
987 break;
988 case STATUS_ACTIVE:
989 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700990 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800991 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700992 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800993 return res;
994 }
995 wasActive = true;
996 break;
997 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700998 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800999 return INVALID_OPERATION;
1000 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001001 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001002
1003 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001004 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001005 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001006 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001007 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1008 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001009 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001010 ssize_t blobBufferSize;
1011 if (dataSpace != HAL_DATASPACE_DEPTH) {
1012 blobBufferSize = getJpegBufferSize(width, height);
1013 if (blobBufferSize <= 0) {
1014 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1015 return BAD_VALUE;
1016 }
1017 } else {
1018 blobBufferSize = getPointCloudBufferSize();
1019 if (blobBufferSize <= 0) {
1020 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1021 return BAD_VALUE;
1022 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001023 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001024 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001025 width, height, blobBufferSize, format, dataSpace, rotation,
1026 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001027 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1028 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1029 if (rawOpaqueBufferSize <= 0) {
1030 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1031 return BAD_VALUE;
1032 }
1033 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001034 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1035 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001036 } else {
1037 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001038 width, height, format, dataSpace, rotation,
1039 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001040 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001041 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001042
Zhijun He125684a2015-12-26 15:07:30 -08001043 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001044 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1045 * requires buffers to be statically allocated for internal static buffer registration, while
1046 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1047 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001048 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001049 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001050 newStream->setBufferManager(mBufferManager);
1051 }
1052
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053 res = mOutputStreams.add(mNextStreamId, newStream);
1054 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001055 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001056 return res;
1057 }
1058
1059 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001060 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001061
1062 // Continue captures if active at start
1063 if (wasActive) {
1064 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1065 res = configureStreamsLocked();
1066 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001067 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1068 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 return res;
1070 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001071 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001072 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001073 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001074 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001075}
1076
1077status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1078 ATRACE_CALL();
1079 (void)outputId; (void)id;
1080
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001081 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001082 return INVALID_OPERATION;
1083}
1084
1085
1086status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001087 uint32_t *width, uint32_t *height,
1088 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001089 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001090 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001091 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001092
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001093 switch (mStatus) {
1094 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001095 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001096 return INVALID_OPERATION;
1097 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001098 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001099 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001100 case STATUS_UNCONFIGURED:
1101 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001102 case STATUS_ACTIVE:
1103 // OK
1104 break;
1105 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001106 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001107 return INVALID_OPERATION;
1108 }
1109
1110 ssize_t idx = mOutputStreams.indexOfKey(id);
1111 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001112 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001113 return idx;
1114 }
1115
1116 if (width) *width = mOutputStreams[idx]->getWidth();
1117 if (height) *height = mOutputStreams[idx]->getHeight();
1118 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001119 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001120 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001121}
1122
1123status_t Camera3Device::setStreamTransform(int id,
1124 int transform) {
1125 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001126 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001127 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001128
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001129 switch (mStatus) {
1130 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001131 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001132 return INVALID_OPERATION;
1133 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001134 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001135 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001136 case STATUS_UNCONFIGURED:
1137 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001138 case STATUS_ACTIVE:
1139 // OK
1140 break;
1141 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001142 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001143 return INVALID_OPERATION;
1144 }
1145
1146 ssize_t idx = mOutputStreams.indexOfKey(id);
1147 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001148 CLOGE("Stream %d does not exist",
1149 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001150 return BAD_VALUE;
1151 }
1152
1153 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001154}
1155
1156status_t Camera3Device::deleteStream(int id) {
1157 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001158 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001159 Mutex::Autolock l(mLock);
1160 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001161
Igor Murashkine2172be2013-05-28 15:31:39 -07001162 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1163
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001164 // CameraDevice semantics require device to already be idle before
1165 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001166 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001167 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1168 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001169 }
1170
Igor Murashkin2fba5842013-04-22 14:03:54 -07001171 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001172 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001173 if (mInputStream != NULL && id == mInputStream->getId()) {
1174 deletedStream = mInputStream;
1175 mInputStream.clear();
1176 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001177 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001178 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001179 return BAD_VALUE;
1180 }
Zhijun He5f446352014-01-22 09:49:33 -08001181 }
1182
1183 // Delete output stream or the output part of a bi-directional stream.
1184 if (outputStreamIdx != NAME_NOT_FOUND) {
1185 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001186 mOutputStreams.removeItem(id);
1187 }
1188
1189 // Free up the stream endpoint so that it can be used by some other stream
1190 res = deletedStream->disconnect();
1191 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001192 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001193 // fall through since we want to still list the stream as deleted.
1194 }
1195 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001196 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001197
1198 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001199}
1200
1201status_t Camera3Device::deleteReprocessStream(int id) {
1202 ATRACE_CALL();
1203 (void)id;
1204
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001205 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001206 return INVALID_OPERATION;
1207}
1208
Zhijun He1fa89992015-06-01 15:44:31 -07001209status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001210 ATRACE_CALL();
1211 ALOGV("%s: E", __FUNCTION__);
1212
1213 Mutex::Autolock il(mInterfaceLock);
1214 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001215
1216 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1217 mNeedConfig = true;
1218 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1219 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001220
1221 return configureStreamsLocked();
1222}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001223
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001224status_t Camera3Device::getInputBufferProducer(
1225 sp<IGraphicBufferProducer> *producer) {
1226 Mutex::Autolock il(mInterfaceLock);
1227 Mutex::Autolock l(mLock);
1228
1229 if (producer == NULL) {
1230 return BAD_VALUE;
1231 } else if (mInputStream == NULL) {
1232 return INVALID_OPERATION;
1233 }
1234
1235 return mInputStream->getInputBufferProducer(producer);
1236}
1237
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001238status_t Camera3Device::createDefaultRequest(int templateId,
1239 CameraMetadata *request) {
1240 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001241 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001242
1243 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1244 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1245 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1246 return BAD_VALUE;
1247 }
1248
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001249 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001250 Mutex::Autolock l(mLock);
1251
1252 switch (mStatus) {
1253 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001254 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001255 return INVALID_OPERATION;
1256 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001257 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001258 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001259 case STATUS_UNCONFIGURED:
1260 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001261 case STATUS_ACTIVE:
1262 // OK
1263 break;
1264 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001265 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001266 return INVALID_OPERATION;
1267 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001268
Zhijun Hea1530f12014-09-14 12:44:20 -07001269 if (!mRequestTemplateCache[templateId].isEmpty()) {
1270 *request = mRequestTemplateCache[templateId];
1271 return OK;
1272 }
1273
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001274 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001275 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001276 rawRequest = mHal3Device->ops->construct_default_request_settings(
1277 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001278 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001279 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001280 ALOGI("%s: template %d is not supported on this camera device",
1281 __FUNCTION__, templateId);
1282 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001283 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001284 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001285 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001286
1287 return OK;
1288}
1289
1290status_t Camera3Device::waitUntilDrained() {
1291 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001292 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001293 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001294
Zhijun He69a37482014-03-23 18:44:49 -07001295 return waitUntilDrainedLocked();
1296}
1297
1298status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001299 switch (mStatus) {
1300 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001301 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001302 ALOGV("%s: Already idle", __FUNCTION__);
1303 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001304 case STATUS_CONFIGURED:
1305 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001306 case STATUS_ERROR:
1307 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001308 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001309 break;
1310 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001311 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001312 return INVALID_OPERATION;
1313 }
1314
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001315 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1316 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001317 if (res != OK) {
1318 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1319 res);
1320 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001321 return res;
1322}
1323
Ruben Brunk183f0562015-08-12 12:55:02 -07001324
1325void Camera3Device::internalUpdateStatusLocked(Status status) {
1326 mStatus = status;
1327 mRecentStatusUpdates.add(mStatus);
1328 mStatusChanged.broadcast();
1329}
1330
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001331// Pause to reconfigure
1332status_t Camera3Device::internalPauseAndWaitLocked() {
1333 mRequestThread->setPaused(true);
1334 mPauseStateNotify = true;
1335
1336 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1337 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1338 if (res != OK) {
1339 SET_ERR_L("Can't idle device in %f seconds!",
1340 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001341 }
1342
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001343 return res;
1344}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001345
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001346// Resume after internalPauseAndWaitLocked
1347status_t Camera3Device::internalResumeLocked() {
1348 status_t res;
1349
1350 mRequestThread->setPaused(false);
1351
1352 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1353 if (res != OK) {
1354 SET_ERR_L("Can't transition to active in %f seconds!",
1355 kActiveTimeout/1e9);
1356 }
1357 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001358 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001359}
1360
Ruben Brunk183f0562015-08-12 12:55:02 -07001361status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001362 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001363
1364 size_t startIndex = 0;
1365 if (mStatusWaiters == 0) {
1366 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1367 // this status list
1368 mRecentStatusUpdates.clear();
1369 } else {
1370 // If other threads are waiting on updates to this status list, set the position of the
1371 // first element that this list will check rather than clearing the list.
1372 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001373 }
1374
Ruben Brunk183f0562015-08-12 12:55:02 -07001375 mStatusWaiters++;
1376
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001377 bool stateSeen = false;
1378 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001379 if (active == (mStatus == STATUS_ACTIVE)) {
1380 // Desired state is current
1381 break;
1382 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001383
1384 res = mStatusChanged.waitRelative(mLock, timeout);
1385 if (res != OK) break;
1386
Ruben Brunk183f0562015-08-12 12:55:02 -07001387 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1388 // transitions.
1389 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1390 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1391 __FUNCTION__);
1392
1393 // Encountered desired state since we began waiting
1394 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001395 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1396 stateSeen = true;
1397 break;
1398 }
1399 }
1400 } while (!stateSeen);
1401
Ruben Brunk183f0562015-08-12 12:55:02 -07001402 mStatusWaiters--;
1403
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001404 return res;
1405}
1406
1407
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001408status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1409 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001410 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001411
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001412 if (listener != NULL && mListener != NULL) {
1413 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1414 }
1415 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001416 mRequestThread->setNotificationListener(listener);
1417 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001418
1419 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001420}
1421
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001422bool Camera3Device::willNotify3A() {
1423 return false;
1424}
1425
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001426status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001427 status_t res;
1428 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001429
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001430 while (mResultQueue.empty()) {
1431 res = mResultSignal.waitRelative(mOutputLock, timeout);
1432 if (res == TIMED_OUT) {
1433 return res;
1434 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001435 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001436 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001437 return res;
1438 }
1439 }
1440 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001441}
1442
Jianing Weicb0652e2014-03-12 18:29:36 -07001443status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001444 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001445 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001446
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001447 if (mResultQueue.empty()) {
1448 return NOT_ENOUGH_DATA;
1449 }
1450
Jianing Weicb0652e2014-03-12 18:29:36 -07001451 if (frame == NULL) {
1452 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1453 return BAD_VALUE;
1454 }
1455
1456 CaptureResult &result = *(mResultQueue.begin());
1457 frame->mResultExtras = result.mResultExtras;
1458 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001459 mResultQueue.erase(mResultQueue.begin());
1460
1461 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001462}
1463
1464status_t Camera3Device::triggerAutofocus(uint32_t id) {
1465 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001466 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001467
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001468 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1469 // Mix-in this trigger into the next request and only the next request.
1470 RequestTrigger trigger[] = {
1471 {
1472 ANDROID_CONTROL_AF_TRIGGER,
1473 ANDROID_CONTROL_AF_TRIGGER_START
1474 },
1475 {
1476 ANDROID_CONTROL_AF_TRIGGER_ID,
1477 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001478 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001479 };
1480
1481 return mRequestThread->queueTrigger(trigger,
1482 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001483}
1484
1485status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1486 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001487 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001488
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001489 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1490 // Mix-in this trigger into the next request and only the next request.
1491 RequestTrigger trigger[] = {
1492 {
1493 ANDROID_CONTROL_AF_TRIGGER,
1494 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1495 },
1496 {
1497 ANDROID_CONTROL_AF_TRIGGER_ID,
1498 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001499 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001500 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001501
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001502 return mRequestThread->queueTrigger(trigger,
1503 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001504}
1505
1506status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1507 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001508 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001509
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001510 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1511 // Mix-in this trigger into the next request and only the next request.
1512 RequestTrigger trigger[] = {
1513 {
1514 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1515 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1516 },
1517 {
1518 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1519 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001520 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001521 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001522
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001523 return mRequestThread->queueTrigger(trigger,
1524 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001525}
1526
1527status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1528 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1529 ATRACE_CALL();
1530 (void)reprocessStreamId; (void)buffer; (void)listener;
1531
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001532 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001533 return INVALID_OPERATION;
1534}
1535
Jianing Weicb0652e2014-03-12 18:29:36 -07001536status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001537 ATRACE_CALL();
1538 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001539 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001540
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001541 NotificationListener* listener;
1542 {
1543 Mutex::Autolock l(mOutputLock);
1544 listener = mListener;
1545 }
1546
Zhijun He7ef20392014-04-21 16:04:17 -07001547 {
1548 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001549 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001550 }
1551
Zhijun He491e3412013-12-27 10:57:44 -08001552 status_t res;
1553 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001554 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001555 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001556 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001557 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001558 }
1559
1560 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001561}
1562
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001563status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001564 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1565}
1566
1567status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001568 ATRACE_CALL();
1569 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001570 Mutex::Autolock il(mInterfaceLock);
1571 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001572
1573 sp<Camera3StreamInterface> stream;
1574 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1575 if (outputStreamIdx == NAME_NOT_FOUND) {
1576 CLOGE("Stream %d does not exist", streamId);
1577 return BAD_VALUE;
1578 }
1579
1580 stream = mOutputStreams.editValueAt(outputStreamIdx);
1581
1582 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001583 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001584 return BAD_VALUE;
1585 }
1586
1587 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001588 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001589 return BAD_VALUE;
1590 }
1591
Ruben Brunkc78ac262015-08-13 17:58:46 -07001592 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001593}
1594
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001595status_t Camera3Device::tearDown(int streamId) {
1596 ATRACE_CALL();
1597 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1598 Mutex::Autolock il(mInterfaceLock);
1599 Mutex::Autolock l(mLock);
1600
1601 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1602 // since we cannot call register_stream_buffers except right after configure_streams.
1603 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1604 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1605 __FUNCTION__, mHal3Device->common.version);
1606 return NO_INIT;
1607 }
1608
1609 sp<Camera3StreamInterface> stream;
1610 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1611 if (outputStreamIdx == NAME_NOT_FOUND) {
1612 CLOGE("Stream %d does not exist", streamId);
1613 return BAD_VALUE;
1614 }
1615
1616 stream = mOutputStreams.editValueAt(outputStreamIdx);
1617
1618 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1619 CLOGE("Stream %d is a target of a in-progress request", streamId);
1620 return BAD_VALUE;
1621 }
1622
1623 return stream->tearDown();
1624}
1625
Zhijun He204e3292014-07-14 17:09:23 -07001626uint32_t Camera3Device::getDeviceVersion() {
1627 ATRACE_CALL();
1628 Mutex::Autolock il(mInterfaceLock);
1629 return mDeviceVersion;
1630}
1631
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001632/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001633 * Methods called by subclasses
1634 */
1635
1636void Camera3Device::notifyStatus(bool idle) {
1637 {
1638 // Need mLock to safely update state and synchronize to current
1639 // state of methods in flight.
1640 Mutex::Autolock l(mLock);
1641 // We can get various system-idle notices from the status tracker
1642 // while starting up. Only care about them if we've actually sent
1643 // in some requests recently.
1644 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1645 return;
1646 }
1647 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1648 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001649 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001650
1651 // Skip notifying listener if we're doing some user-transparent
1652 // state changes
1653 if (mPauseStateNotify) return;
1654 }
1655 NotificationListener *listener;
1656 {
1657 Mutex::Autolock l(mOutputLock);
1658 listener = mListener;
1659 }
1660 if (idle && listener != NULL) {
1661 listener->notifyIdle();
1662 }
1663}
1664
1665/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001666 * Camera3Device private methods
1667 */
1668
1669sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1670 const CameraMetadata &request) {
1671 ATRACE_CALL();
1672 status_t res;
1673
1674 sp<CaptureRequest> newRequest = new CaptureRequest;
1675 newRequest->mSettings = request;
1676
1677 camera_metadata_entry_t inputStreams =
1678 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1679 if (inputStreams.count > 0) {
1680 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001681 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001682 CLOGE("Request references unknown input stream %d",
1683 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001684 return NULL;
1685 }
1686 // Lazy completion of stream configuration (allocation/registration)
1687 // on first use
1688 if (mInputStream->isConfiguring()) {
1689 res = mInputStream->finishConfiguration(mHal3Device);
1690 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001691 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001692 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001693 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001694 return NULL;
1695 }
1696 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001697 // Check if stream is being prepared
1698 if (mInputStream->isPreparing()) {
1699 CLOGE("Request references an input stream that's being prepared!");
1700 return NULL;
1701 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001702
1703 newRequest->mInputStream = mInputStream;
1704 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1705 }
1706
1707 camera_metadata_entry_t streams =
1708 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1709 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001710 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001711 return NULL;
1712 }
1713
1714 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001715 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001716 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001717 CLOGE("Request references unknown stream %d",
1718 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001719 return NULL;
1720 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001721 sp<Camera3OutputStreamInterface> stream =
1722 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001723
1724 // Lazy completion of stream configuration (allocation/registration)
1725 // on first use
1726 if (stream->isConfiguring()) {
1727 res = stream->finishConfiguration(mHal3Device);
1728 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001729 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1730 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001731 return NULL;
1732 }
1733 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001734 // Check if stream is being prepared
1735 if (stream->isPreparing()) {
1736 CLOGE("Request references an output stream that's being prepared!");
1737 return NULL;
1738 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001739
1740 newRequest->mOutputStreams.push(stream);
1741 }
1742 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001743 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001744
1745 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001746}
1747
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001748bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1749 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1750 Size size = mSupportedOpaqueInputSizes[i];
1751 if (size.width == width && size.height == height) {
1752 return true;
1753 }
1754 }
1755
1756 return false;
1757}
1758
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001759status_t Camera3Device::configureStreamsLocked() {
1760 ATRACE_CALL();
1761 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001762
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001763 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001764 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001765 return INVALID_OPERATION;
1766 }
1767
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001768 if (!mNeedConfig) {
1769 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1770 return OK;
1771 }
1772
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001773 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1774 // adding a dummy stream instead.
1775 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1776 if (mOutputStreams.size() == 0) {
1777 addDummyStreamLocked();
1778 } else {
1779 tryRemoveDummyStreamLocked();
1780 }
1781
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001782 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001783 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001784
1785 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001786 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1787 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1788 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001789 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1790
1791 Vector<camera3_stream_t*> streams;
1792 streams.setCapacity(config.num_streams);
1793
1794 if (mInputStream != NULL) {
1795 camera3_stream_t *inputStream;
1796 inputStream = mInputStream->startConfiguration();
1797 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001798 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001799 return INVALID_OPERATION;
1800 }
1801 streams.add(inputStream);
1802 }
1803
1804 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001805
1806 // Don't configure bidi streams twice, nor add them twice to the list
1807 if (mOutputStreams[i].get() ==
1808 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1809
1810 config.num_streams--;
1811 continue;
1812 }
1813
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001814 camera3_stream_t *outputStream;
1815 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1816 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001817 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001818 return INVALID_OPERATION;
1819 }
1820 streams.add(outputStream);
1821 }
1822
1823 config.streams = streams.editArray();
1824
1825 // Do the HAL configuration; will potentially touch stream
1826 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001827 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001828 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001829 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001830
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001831 if (res == BAD_VALUE) {
1832 // HAL rejected this set of streams as unsupported, clean up config
1833 // attempt and return to unconfigured state
1834 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1835 res = mInputStream->cancelConfiguration();
1836 if (res != OK) {
1837 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1838 mInputStream->getId(), strerror(-res), res);
1839 return res;
1840 }
1841 }
1842
1843 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1844 sp<Camera3OutputStreamInterface> outputStream =
1845 mOutputStreams.editValueAt(i);
1846 if (outputStream->isConfiguring()) {
1847 res = outputStream->cancelConfiguration();
1848 if (res != OK) {
1849 SET_ERR_L(
1850 "Can't cancel configuring output stream %d: %s (%d)",
1851 outputStream->getId(), strerror(-res), res);
1852 return res;
1853 }
1854 }
1855 }
1856
1857 // Return state to that at start of call, so that future configures
1858 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001859 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001860 mNeedConfig = true;
1861
1862 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1863 return BAD_VALUE;
1864 } else if (res != OK) {
1865 // Some other kind of error from configure_streams - this is not
1866 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001867 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1868 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001869 return res;
1870 }
1871
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001872 // Finish all stream configuration immediately.
1873 // TODO: Try to relax this later back to lazy completion, which should be
1874 // faster
1875
Igor Murashkin073f8572013-05-02 14:59:28 -07001876 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001877 res = mInputStream->finishConfiguration(mHal3Device);
1878 if (res != OK) {
1879 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1880 mInputStream->getId(), strerror(-res), res);
1881 return res;
1882 }
1883 }
1884
1885 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001886 sp<Camera3OutputStreamInterface> outputStream =
1887 mOutputStreams.editValueAt(i);
1888 if (outputStream->isConfiguring()) {
1889 res = outputStream->finishConfiguration(mHal3Device);
1890 if (res != OK) {
1891 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1892 outputStream->getId(), strerror(-res), res);
1893 return res;
1894 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001895 }
1896 }
1897
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001898 // Request thread needs to know to avoid using repeat-last-settings protocol
1899 // across configure_streams() calls
1900 mRequestThread->configurationComplete();
1901
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07001902 // Boost priority of request thread for high speed recording to SCHED_FIFO
1903 if (mIsConstrainedHighSpeedConfiguration) {
1904 pid_t requestThreadTid = mRequestThread->getTid();
1905 res = requestPriority(getpid(), requestThreadTid,
1906 kConstrainedHighSpeedThreadPriority, true);
1907 if (res != OK) {
1908 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
1909 strerror(-res), res);
1910 } else {
1911 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
1912 }
1913 } else {
1914 // TODO: Set/restore normal priority for normal use cases
1915 }
1916
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001917 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001918
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001919 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001920
Ruben Brunk183f0562015-08-12 12:55:02 -07001921 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1922 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001923
1924 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1925
Zhijun He0a210512014-07-24 13:45:15 -07001926 // tear down the deleted streams after configure streams.
1927 mDeletedStreams.clear();
1928
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001929 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001930}
1931
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001932status_t Camera3Device::addDummyStreamLocked() {
1933 ATRACE_CALL();
1934 status_t res;
1935
1936 if (mDummyStreamId != NO_STREAM) {
1937 // Should never be adding a second dummy stream when one is already
1938 // active
1939 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1940 __FUNCTION__, mId);
1941 return INVALID_OPERATION;
1942 }
1943
1944 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1945
1946 sp<Camera3OutputStreamInterface> dummyStream =
1947 new Camera3DummyStream(mNextStreamId);
1948
1949 res = mOutputStreams.add(mNextStreamId, dummyStream);
1950 if (res < 0) {
1951 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1952 return res;
1953 }
1954
1955 mDummyStreamId = mNextStreamId;
1956 mNextStreamId++;
1957
1958 return OK;
1959}
1960
1961status_t Camera3Device::tryRemoveDummyStreamLocked() {
1962 ATRACE_CALL();
1963 status_t res;
1964
1965 if (mDummyStreamId == NO_STREAM) return OK;
1966 if (mOutputStreams.size() == 1) return OK;
1967
1968 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1969
1970 // Ok, have a dummy stream and there's at least one other output stream,
1971 // so remove the dummy
1972
1973 sp<Camera3StreamInterface> deletedStream;
1974 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1975 if (outputStreamIdx == NAME_NOT_FOUND) {
1976 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1977 return INVALID_OPERATION;
1978 }
1979
1980 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1981 mOutputStreams.removeItemsAt(outputStreamIdx);
1982
1983 // Free up the stream endpoint so that it can be used by some other stream
1984 res = deletedStream->disconnect();
1985 if (res != OK) {
1986 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1987 // fall through since we want to still list the stream as deleted.
1988 }
1989 mDeletedStreams.add(deletedStream);
1990 mDummyStreamId = NO_STREAM;
1991
1992 return res;
1993}
1994
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001995void Camera3Device::setErrorState(const char *fmt, ...) {
1996 Mutex::Autolock l(mLock);
1997 va_list args;
1998 va_start(args, fmt);
1999
2000 setErrorStateLockedV(fmt, args);
2001
2002 va_end(args);
2003}
2004
2005void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2006 Mutex::Autolock l(mLock);
2007 setErrorStateLockedV(fmt, args);
2008}
2009
2010void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2011 va_list args;
2012 va_start(args, fmt);
2013
2014 setErrorStateLockedV(fmt, args);
2015
2016 va_end(args);
2017}
2018
2019void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002020 // Print out all error messages to log
2021 String8 errorCause = String8::formatV(fmt, args);
2022 ALOGE("Camera %d: %s", mId, errorCause.string());
2023
2024 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002025 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002026
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002027 mErrorCause = errorCause;
2028
2029 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002030 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002031
2032 // Notify upstream about a device error
2033 if (mListener != NULL) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002034 mListener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002035 CaptureResultExtras());
2036 }
2037
2038 // Save stack trace. View by dumping it later.
2039 CameraTraces::saveTrace();
2040 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002041}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002042
2043/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002044 * In-flight request management
2045 */
2046
Jianing Weicb0652e2014-03-12 18:29:36 -07002047status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002048 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2049 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002050 ATRACE_CALL();
2051 Mutex::Autolock l(mInFlightLock);
2052
2053 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002054 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2055 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002056 if (res < 0) return res;
2057
2058 return OK;
2059}
2060
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002061void Camera3Device::returnOutputBuffers(
2062 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2063 nsecs_t timestamp) {
2064 for (size_t i = 0; i < numBuffers; i++)
2065 {
2066 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2067 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2068 // Note: stream may be deallocated at this point, if this buffer was
2069 // the last reference to it.
2070 if (res != OK) {
2071 ALOGE("Can't return buffer to its stream: %s (%d)",
2072 strerror(-res), res);
2073 }
2074 }
2075}
2076
2077
2078void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2079
2080 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2081 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2082
2083 nsecs_t sensorTimestamp = request.sensorTimestamp;
2084 nsecs_t shutterTimestamp = request.shutterTimestamp;
2085
2086 // Check if it's okay to remove the request from InFlightMap:
2087 // In the case of a successful request:
2088 // all input and output buffers, all result metadata, shutter callback
2089 // arrived.
2090 // In the case of a unsuccessful request:
2091 // all input and output buffers arrived.
2092 if (request.numBuffersLeft == 0 &&
2093 (request.requestStatus != OK ||
2094 (request.haveResultMetadata && shutterTimestamp != 0))) {
2095 ATRACE_ASYNC_END("frame capture", frameNumber);
2096
2097 // Sanity check - if sensor timestamp matches shutter timestamp
2098 if (request.requestStatus == OK &&
2099 sensorTimestamp != shutterTimestamp) {
2100 SET_ERR("sensor timestamp (%" PRId64
2101 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2102 sensorTimestamp, frameNumber, shutterTimestamp);
2103 }
2104
2105 // for an unsuccessful request, it may have pending output buffers to
2106 // return.
2107 assert(request.requestStatus != OK ||
2108 request.pendingOutputBuffers.size() == 0);
2109 returnOutputBuffers(request.pendingOutputBuffers.array(),
2110 request.pendingOutputBuffers.size(), 0);
2111
2112 mInFlightMap.removeItemsAt(idx, 1);
2113
2114 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2115 }
2116
2117 // Sanity check - if we have too many in-flight frames, something has
2118 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002119 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002120 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002121 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2122 kInFlightWarnLimitHighSpeed) {
2123 CLOGE("In-flight list too large for high speed configuration: %zu",
2124 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002125 }
2126}
2127
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002128void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2129 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2130 if (result == nullptr) return;
2131
2132 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2133 (int32_t*)&frameNumber, 1) != OK) {
2134 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2135 return;
2136 }
2137
2138 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2139 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2140 return;
2141 }
2142
2143 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2144
2145 // Valid result, insert into queue
2146 List<CaptureResult>::iterator queuedResult =
2147 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2148 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2149 ", burstId = %" PRId32, __FUNCTION__,
2150 queuedResult->mResultExtras.requestId,
2151 queuedResult->mResultExtras.frameNumber,
2152 queuedResult->mResultExtras.burstId);
2153
2154 mResultSignal.signal();
2155}
2156
2157
2158void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2159 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2160 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2161 Mutex::Autolock l(mOutputLock);
2162
2163 CaptureResult captureResult;
2164 captureResult.mResultExtras = resultExtras;
2165 captureResult.mMetadata = partialResult;
2166
2167 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2168}
2169
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002170
2171void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2172 CaptureResultExtras &resultExtras,
2173 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002174 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002175 bool reprocess,
2176 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002177 if (pendingMetadata.isEmpty())
2178 return;
2179
2180 Mutex::Autolock l(mOutputLock);
2181
2182 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002183 if (reprocess) {
2184 if (frameNumber < mNextReprocessResultFrameNumber) {
2185 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002186 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002187 frameNumber, mNextReprocessResultFrameNumber);
2188 return;
2189 }
2190 mNextReprocessResultFrameNumber = frameNumber + 1;
2191 } else {
2192 if (frameNumber < mNextResultFrameNumber) {
2193 SET_ERR("Out-of-order capture result metadata submitted! "
2194 "(got frame number %d, expecting %d)",
2195 frameNumber, mNextResultFrameNumber);
2196 return;
2197 }
2198 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002199 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002200
2201 CaptureResult captureResult;
2202 captureResult.mResultExtras = resultExtras;
2203 captureResult.mMetadata = pendingMetadata;
2204
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002205 // Append any previous partials to form a complete result
2206 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2207 captureResult.mMetadata.append(collectedPartialResult);
2208 }
2209
2210 captureResult.mMetadata.sort();
2211
2212 // Check that there's a timestamp in the result metadata
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002213 camera_metadata_entry entry = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002214 if (entry.count == 0) {
2215 SET_ERR("No timestamp provided by HAL for frame %d!",
2216 frameNumber);
2217 return;
2218 }
2219
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002220 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002221}
2222
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002223/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002224 * Camera HAL device callback methods
2225 */
2226
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002227void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002228 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002229
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002230 status_t res;
2231
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002232 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002233 if (result->result == NULL && result->num_output_buffers == 0 &&
2234 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002235 SET_ERR("No result data provided by HAL for frame %d",
2236 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002237 return;
2238 }
Zhijun He204e3292014-07-14 17:09:23 -07002239
2240 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2241 // partial_result to 1 when metadata is included in this result.
2242 if (!mUsePartialResult &&
2243 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2244 result->result != NULL &&
2245 result->partial_result != 1) {
2246 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2247 " if partial result is not supported",
2248 frameNumber, result->partial_result);
2249 return;
2250 }
2251
2252 bool isPartialResult = false;
2253 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002254 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002255 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002256
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002257 // Get shutter timestamp and resultExtras from list of in-flight requests,
2258 // where it was added by the shutter notification for this frame. If the
2259 // shutter timestamp isn't received yet, append the output buffers to the
2260 // in-flight request and they will be returned when the shutter timestamp
2261 // arrives. Update the in-flight status and remove the in-flight entry if
2262 // all result data and shutter timestamp have been received.
2263 nsecs_t shutterTimestamp = 0;
2264
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002265 {
2266 Mutex::Autolock l(mInFlightLock);
2267 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2268 if (idx == NAME_NOT_FOUND) {
2269 SET_ERR("Unknown frame number for capture result: %d",
2270 frameNumber);
2271 return;
2272 }
2273 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002274 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2275 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2276 ", partialResultCount = %d",
2277 __FUNCTION__, request.resultExtras.requestId,
2278 request.resultExtras.frameNumber, request.resultExtras.burstId,
2279 result->partial_result);
2280 // Always update the partial count to the latest one if it's not 0
2281 // (buffers only). When framework aggregates adjacent partial results
2282 // into one, the latest partial count will be used.
2283 if (result->partial_result != 0)
2284 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002285
2286 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002287 if (mUsePartialResult && result->result != NULL) {
2288 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2289 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2290 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2291 " the range of [1, %d] when metadata is included in the result",
2292 frameNumber, result->partial_result, mNumPartialResults);
2293 return;
2294 }
2295 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002296 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002297 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002298 }
Zhijun He204e3292014-07-14 17:09:23 -07002299 } else {
2300 camera_metadata_ro_entry_t partialResultEntry;
2301 res = find_camera_metadata_ro_entry(result->result,
2302 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2303 if (res != NAME_NOT_FOUND &&
2304 partialResultEntry.count > 0 &&
2305 partialResultEntry.data.u8[0] ==
2306 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2307 // A partial result. Flag this as such, and collect this
2308 // set of metadata into the in-flight entry.
2309 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002310 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002311 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002312 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002313 ANDROID_QUIRKS_PARTIAL_RESULT);
2314 }
2315 }
2316
2317 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002318 // Send partial capture result
2319 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2320 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002321 }
2322 }
2323
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002324 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002325 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002326
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002327 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002328 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002329 if (request.haveResultMetadata) {
2330 SET_ERR("Called multiple times with metadata for frame %d",
2331 frameNumber);
2332 return;
2333 }
Zhijun He204e3292014-07-14 17:09:23 -07002334 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002335 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002336 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002337 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002338 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002339 request.haveResultMetadata = true;
2340 }
2341
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002342 uint32_t numBuffersReturned = result->num_output_buffers;
2343 if (result->input_buffer != NULL) {
2344 if (hasInputBufferInRequest) {
2345 numBuffersReturned += 1;
2346 } else {
2347 ALOGW("%s: Input buffer should be NULL if there is no input"
2348 " buffer sent in the request",
2349 __FUNCTION__);
2350 }
2351 }
2352 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002353 if (request.numBuffersLeft < 0) {
2354 SET_ERR("Too many buffers returned for frame %d",
2355 frameNumber);
2356 return;
2357 }
2358
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002359 camera_metadata_ro_entry_t entry;
2360 res = find_camera_metadata_ro_entry(result->result,
2361 ANDROID_SENSOR_TIMESTAMP, &entry);
2362 if (res == OK && entry.count == 1) {
2363 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002364 }
2365
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002366 // If shutter event isn't received yet, append the output buffers to
2367 // the in-flight request. Otherwise, return the output buffers to
2368 // streams.
2369 if (shutterTimestamp == 0) {
2370 request.pendingOutputBuffers.appendArray(result->output_buffers,
2371 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002372 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002373 returnOutputBuffers(result->output_buffers,
2374 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002375 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002376
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002377 if (result->result != NULL && !isPartialResult) {
2378 if (shutterTimestamp == 0) {
2379 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002380 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002381 } else {
2382 CameraMetadata metadata;
2383 metadata = result->result;
2384 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002385 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2386 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002387 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002388 }
2389
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002390 removeInFlightRequestIfReadyLocked(idx);
2391 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002392
Zhijun Hef0d962a2014-06-30 10:24:11 -07002393 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002394 if (hasInputBufferInRequest) {
2395 Camera3Stream *stream =
2396 Camera3Stream::cast(result->input_buffer->stream);
2397 res = stream->returnInputBuffer(*(result->input_buffer));
2398 // Note: stream may be deallocated at this point, if this buffer was the
2399 // last reference to it.
2400 if (res != OK) {
2401 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2402 " its stream:%s (%d)", __FUNCTION__,
2403 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002404 }
2405 } else {
2406 ALOGW("%s: Input buffer should be NULL if there is no input"
2407 " buffer sent in the request, skipping input buffer return.",
2408 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002409 }
2410 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002411}
2412
2413void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002414 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002415 NotificationListener *listener;
2416 {
2417 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002418 listener = mListener;
2419 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002420
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002421 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002422 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002423 return;
2424 }
2425
2426 switch (msg->type) {
2427 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002428 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002429 break;
2430 }
2431 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002432 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002433 break;
2434 }
2435 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002436 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002437 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002438 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002439}
2440
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002441void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2442 NotificationListener *listener) {
2443
2444 // Map camera HAL error codes to ICameraDeviceCallback error codes
2445 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002446 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002447 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002448 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002449 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002450 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002451 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002452 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002453 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002454 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002455 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002456 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002457 };
2458
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002459 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002460 ((msg.error_code >= 0) &&
2461 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2462 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002463 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002464
2465 int streamId = 0;
2466 if (msg.error_stream != NULL) {
2467 Camera3Stream *stream =
2468 Camera3Stream::cast(msg.error_stream);
2469 streamId = stream->getId();
2470 }
2471 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2472 mId, __FUNCTION__, msg.frame_number,
2473 streamId, msg.error_code);
2474
2475 CaptureResultExtras resultExtras;
2476 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002477 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002478 // SET_ERR calls notifyError
2479 SET_ERR("Camera HAL reported serious device error");
2480 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002481 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2482 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2483 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002484 {
2485 Mutex::Autolock l(mInFlightLock);
2486 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2487 if (idx >= 0) {
2488 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2489 r.requestStatus = msg.error_code;
2490 resultExtras = r.resultExtras;
2491 } else {
2492 resultExtras.frameNumber = msg.frame_number;
2493 ALOGE("Camera %d: %s: cannot find in-flight request on "
2494 "frame %" PRId64 " error", mId, __FUNCTION__,
2495 resultExtras.frameNumber);
2496 }
2497 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002498 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002499 if (listener != NULL) {
2500 listener->notifyError(errorCode, resultExtras);
2501 } else {
2502 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2503 }
2504 break;
2505 default:
2506 // SET_ERR calls notifyError
2507 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2508 break;
2509 }
2510}
2511
2512void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2513 NotificationListener *listener) {
2514 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002515
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002516 // Set timestamp for the request in the in-flight tracking
2517 // and get the request ID to send upstream
2518 {
2519 Mutex::Autolock l(mInFlightLock);
2520 idx = mInFlightMap.indexOfKey(msg.frame_number);
2521 if (idx >= 0) {
2522 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002523
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002524 // Verify ordering of shutter notifications
2525 {
2526 Mutex::Autolock l(mOutputLock);
2527 // TODO: need to track errors for tighter bounds on expected frame number.
2528 if (r.hasInputBuffer) {
2529 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2530 SET_ERR("Shutter notification out-of-order. Expected "
2531 "notification for frame %d, got frame %d",
2532 mNextReprocessShutterFrameNumber, msg.frame_number);
2533 return;
2534 }
2535 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2536 } else {
2537 if (msg.frame_number < mNextShutterFrameNumber) {
2538 SET_ERR("Shutter notification out-of-order. Expected "
2539 "notification for frame %d, got frame %d",
2540 mNextShutterFrameNumber, msg.frame_number);
2541 return;
2542 }
2543 mNextShutterFrameNumber = msg.frame_number + 1;
2544 }
2545 }
2546
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002547 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2548 mId, __FUNCTION__,
2549 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2550 // Call listener, if any
2551 if (listener != NULL) {
2552 listener->notifyShutter(r.resultExtras, msg.timestamp);
2553 }
2554
2555 r.shutterTimestamp = msg.timestamp;
2556
2557 // send pending result and buffers
2558 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002559 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002560 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002561 returnOutputBuffers(r.pendingOutputBuffers.array(),
2562 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2563 r.pendingOutputBuffers.clear();
2564
2565 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002566 }
2567 }
2568 if (idx < 0) {
2569 SET_ERR("Shutter notification for non-existent frame number %d",
2570 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002571 }
2572}
2573
2574
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002575CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002576 ALOGV("%s", __FUNCTION__);
2577
Igor Murashkin1e479c02013-09-06 16:55:14 -07002578 CameraMetadata retVal;
2579
2580 if (mRequestThread != NULL) {
2581 retVal = mRequestThread->getLatestRequest();
2582 }
2583
Igor Murashkin1e479c02013-09-06 16:55:14 -07002584 return retVal;
2585}
2586
Jianing Weicb0652e2014-03-12 18:29:36 -07002587
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002588/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002589 * RequestThread inner class methods
2590 */
2591
2592Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002593 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002594 camera3_device_t *hal3Device,
2595 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002596 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002597 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002598 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002599 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002600 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002601 mReconfigured(false),
2602 mDoPause(false),
2603 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002604 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002605 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002606 mCurrentAfTriggerId(0),
2607 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002608 mRepeatingLastFrameNumber(
2609 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002610 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002611 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002612}
2613
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002614void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002615 NotificationListener *listener) {
2616 Mutex::Autolock l(mRequestLock);
2617 mListener = listener;
2618}
2619
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002620void Camera3Device::RequestThread::configurationComplete() {
2621 Mutex::Autolock l(mRequestLock);
2622 mReconfigured = true;
2623}
2624
Jianing Wei90e59c92014-03-12 18:29:36 -07002625status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002626 List<sp<CaptureRequest> > &requests,
2627 /*out*/
2628 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002629 Mutex::Autolock l(mRequestLock);
2630 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2631 ++it) {
2632 mRequestQueue.push_back(*it);
2633 }
2634
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002635 if (lastFrameNumber != NULL) {
2636 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2637 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2638 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2639 *lastFrameNumber);
2640 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002641
Jianing Wei90e59c92014-03-12 18:29:36 -07002642 unpauseForNewRequests();
2643
2644 return OK;
2645}
2646
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002647
2648status_t Camera3Device::RequestThread::queueTrigger(
2649 RequestTrigger trigger[],
2650 size_t count) {
2651
2652 Mutex::Autolock l(mTriggerMutex);
2653 status_t ret;
2654
2655 for (size_t i = 0; i < count; ++i) {
2656 ret = queueTriggerLocked(trigger[i]);
2657
2658 if (ret != OK) {
2659 return ret;
2660 }
2661 }
2662
2663 return OK;
2664}
2665
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002666int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2667 sp<Camera3Device> d = device.promote();
2668 if (d != NULL) return d->mId;
2669 return 0;
2670}
2671
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002672status_t Camera3Device::RequestThread::queueTriggerLocked(
2673 RequestTrigger trigger) {
2674
2675 uint32_t tag = trigger.metadataTag;
2676 ssize_t index = mTriggerMap.indexOfKey(tag);
2677
2678 switch (trigger.getTagType()) {
2679 case TYPE_BYTE:
2680 // fall-through
2681 case TYPE_INT32:
2682 break;
2683 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002684 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2685 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002686 return INVALID_OPERATION;
2687 }
2688
2689 /**
2690 * Collect only the latest trigger, since we only have 1 field
2691 * in the request settings per trigger tag, and can't send more than 1
2692 * trigger per request.
2693 */
2694 if (index != NAME_NOT_FOUND) {
2695 mTriggerMap.editValueAt(index) = trigger;
2696 } else {
2697 mTriggerMap.add(tag, trigger);
2698 }
2699
2700 return OK;
2701}
2702
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002703status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002704 const RequestList &requests,
2705 /*out*/
2706 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002707 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002708 if (lastFrameNumber != NULL) {
2709 *lastFrameNumber = mRepeatingLastFrameNumber;
2710 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002711 mRepeatingRequests.clear();
2712 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2713 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002714
2715 unpauseForNewRequests();
2716
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002717 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002718 return OK;
2719}
2720
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002721bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2722 if (mRepeatingRequests.empty()) {
2723 return false;
2724 }
2725 int32_t requestId = requestIn->mResultExtras.requestId;
2726 const RequestList &repeatRequests = mRepeatingRequests;
2727 // All repeating requests are guaranteed to have same id so only check first quest
2728 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2729 return (firstRequest->mResultExtras.requestId == requestId);
2730}
2731
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002732status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002733 Mutex::Autolock l(mRequestLock);
2734 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002735 if (lastFrameNumber != NULL) {
2736 *lastFrameNumber = mRepeatingLastFrameNumber;
2737 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002738 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002739 return OK;
2740}
2741
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002742status_t Camera3Device::RequestThread::clear(
2743 NotificationListener *listener,
2744 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002745 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002746 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002747
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002748 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002749
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002750 // Send errors for all requests pending in the request queue, including
2751 // pending repeating requests
2752 if (listener != NULL) {
2753 for (RequestList::iterator it = mRequestQueue.begin();
2754 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002755 // Abort the input buffers for reprocess requests.
2756 if ((*it)->mInputStream != NULL) {
2757 camera3_stream_buffer_t inputBuffer;
2758 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2759 if (res != OK) {
2760 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2761 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2762 } else {
2763 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2764 if (res != OK) {
2765 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2766 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2767 }
2768 }
2769 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002770 // Set the frame number this request would have had, if it
2771 // had been submitted; this frame number will not be reused.
2772 // The requestId and burstId fields were set when the request was
2773 // submitted originally (in convertMetadataListToRequestListLocked)
2774 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002775 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002776 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002777 }
2778 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002779 mRequestQueue.clear();
2780 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002781 if (lastFrameNumber != NULL) {
2782 *lastFrameNumber = mRepeatingLastFrameNumber;
2783 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002784 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002785 return OK;
2786}
2787
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002788status_t Camera3Device::RequestThread::flush() {
2789 ATRACE_CALL();
2790 Mutex::Autolock l(mFlushLock);
2791
2792 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2793 return mHal3Device->ops->flush(mHal3Device);
2794 }
2795
2796 return -ENOTSUP;
2797}
2798
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002799void Camera3Device::RequestThread::setPaused(bool paused) {
2800 Mutex::Autolock l(mPauseLock);
2801 mDoPause = paused;
2802 mDoPauseSignal.signal();
2803}
2804
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002805status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2806 int32_t requestId, nsecs_t timeout) {
2807 Mutex::Autolock l(mLatestRequestMutex);
2808 status_t res;
2809 while (mLatestRequestId != requestId) {
2810 nsecs_t startTime = systemTime();
2811
2812 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2813 if (res != OK) return res;
2814
2815 timeout -= (systemTime() - startTime);
2816 }
2817
2818 return OK;
2819}
2820
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002821void Camera3Device::RequestThread::requestExit() {
2822 // Call parent to set up shutdown
2823 Thread::requestExit();
2824 // The exit from any possible waits
2825 mDoPauseSignal.signal();
2826 mRequestSignal.signal();
2827}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002828
Chien-Yu Chend196d612015-06-22 19:49:01 -07002829
2830/**
2831 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2832 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2833 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2834 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2835 * request.
2836 */
2837void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2838 request->mAeTriggerCancelOverride.applyAeLock = false;
2839 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2840
2841 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2842 return;
2843 }
2844
2845 camera_metadata_entry_t aePrecaptureTrigger =
2846 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2847 if (aePrecaptureTrigger.count > 0 &&
2848 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2849 // Always override CANCEL to IDLE
2850 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2851 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2852 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2853 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2854 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2855
2856 if (mAeLockAvailable == true) {
2857 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2858 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2859 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2860 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2861 request->mAeTriggerCancelOverride.applyAeLock = true;
2862 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2863 }
2864 }
2865 }
2866}
2867
2868/**
2869 * Override result metadata for cancelling AE precapture trigger applied in
2870 * handleAePrecaptureCancelRequest().
2871 */
2872void Camera3Device::overrideResultForPrecaptureCancel(
2873 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2874 if (aeTriggerCancelOverride.applyAeLock) {
2875 // Only devices <= v3.2 should have this override
2876 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2877 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2878 }
2879
2880 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2881 // Only devices <= v3.2 should have this override
2882 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2883 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2884 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2885 }
2886}
2887
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002888bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002889 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002890 status_t res;
2891
2892 // Handle paused state.
2893 if (waitIfPaused()) {
2894 return true;
2895 }
2896
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002897 // Wait for the next batch of requests.
2898 waitForNextRequestBatch();
2899 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002900 return true;
2901 }
2902
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002903 // Get the latest request ID, if any
2904 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002905 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002906 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002907 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002908 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002909 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002910 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
2911 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002912 }
2913
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002914 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002915 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002916 if (res == TIMED_OUT) {
2917 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002918 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002919 return true;
2920 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002921 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002922 return false;
2923 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002924
Zhijun Hecc27e112013-10-03 16:12:43 -07002925 // Inform waitUntilRequestProcessed thread of a new request ID
2926 {
2927 Mutex::Autolock al(mLatestRequestMutex);
2928
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002929 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07002930 mLatestRequestSignal.signal();
2931 }
2932
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002933 // Submit a batch of requests to HAL.
2934 // Use flush lock only when submitting multilple requests in a batch.
2935 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
2936 // which may take a long time to finish so synchronizing flush() and
2937 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
2938 // For now, only synchronize for high speed recording and we should figure something out for
2939 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002940 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002941
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002942 if (useFlushLock) {
2943 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002944 }
2945
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002946 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002947 mNextRequests.size());
2948 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002949 // Submit request and block until ready for next one
2950 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
2951 ATRACE_BEGIN("camera3->process_capture_request");
2952 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
2953 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07002954
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002955 if (res != OK) {
2956 // Should only get a failure here for malformed requests or device-level
2957 // errors, so consider all errors fatal. Bad metadata failures should
2958 // come through notify.
2959 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
2960 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
2961 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002962 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002963 if (useFlushLock) {
2964 mFlushLock.unlock();
2965 }
2966 return false;
2967 }
2968
2969 // Mark that the request has be submitted successfully.
2970 nextRequest.submitted = true;
2971
2972 // Update the latest request sent to HAL
2973 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
2974 Mutex::Autolock al(mLatestRequestMutex);
2975
2976 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
2977 mLatestRequest.acquire(cloned);
2978 }
2979
2980 if (nextRequest.halRequest.settings != NULL) {
2981 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
2982 }
2983
2984 // Remove any previously queued triggers (after unlock)
2985 res = removeTriggers(mPrevRequest);
2986 if (res != OK) {
2987 SET_ERR("RequestThread: Unable to remove triggers "
2988 "(capture request %d, HAL device: %s (%d)",
2989 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002990 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002991 if (useFlushLock) {
2992 mFlushLock.unlock();
2993 }
2994 return false;
2995 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07002996 }
2997
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002998 if (useFlushLock) {
2999 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003000 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003001
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003002 // Unset as current request
3003 {
3004 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003005 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003006 }
3007
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003008 return true;
3009}
3010
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003011status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003012 ATRACE_CALL();
3013
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003014 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003015 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3016 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3017 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3018
3019 // Prepare a request to HAL
3020 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3021
3022 // Insert any queued triggers (before metadata is locked)
3023 status_t res = insertTriggers(captureRequest);
3024
3025 if (res < 0) {
3026 SET_ERR("RequestThread: Unable to insert triggers "
3027 "(capture request %d, HAL device: %s (%d)",
3028 halRequest->frame_number, strerror(-res), res);
3029 return INVALID_OPERATION;
3030 }
3031 int triggerCount = res;
3032 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3033 mPrevTriggers = triggerCount;
3034
3035 // If the request is the same as last, or we had triggers last time
3036 if (mPrevRequest != captureRequest || triggersMixedIn) {
3037 /**
3038 * HAL workaround:
3039 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3040 */
3041 res = addDummyTriggerIds(captureRequest);
3042 if (res != OK) {
3043 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3044 "(capture request %d, HAL device: %s (%d)",
3045 halRequest->frame_number, strerror(-res), res);
3046 return INVALID_OPERATION;
3047 }
3048
3049 /**
3050 * The request should be presorted so accesses in HAL
3051 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3052 */
3053 captureRequest->mSettings.sort();
3054 halRequest->settings = captureRequest->mSettings.getAndLock();
3055 mPrevRequest = captureRequest;
3056 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3057
3058 IF_ALOGV() {
3059 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3060 find_camera_metadata_ro_entry(
3061 halRequest->settings,
3062 ANDROID_CONTROL_AF_TRIGGER,
3063 &e
3064 );
3065 if (e.count > 0) {
3066 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3067 __FUNCTION__,
3068 halRequest->frame_number,
3069 e.data.u8[0]);
3070 }
3071 }
3072 } else {
3073 // leave request.settings NULL to indicate 'reuse latest given'
3074 ALOGVV("%s: Request settings are REUSED",
3075 __FUNCTION__);
3076 }
3077
3078 uint32_t totalNumBuffers = 0;
3079
3080 // Fill in buffers
3081 if (captureRequest->mInputStream != NULL) {
3082 halRequest->input_buffer = &captureRequest->mInputBuffer;
3083 totalNumBuffers += 1;
3084 } else {
3085 halRequest->input_buffer = NULL;
3086 }
3087
3088 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3089 captureRequest->mOutputStreams.size());
3090 halRequest->output_buffers = outputBuffers->array();
3091 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3092 res = captureRequest->mOutputStreams.editItemAt(i)->
3093 getBuffer(&outputBuffers->editItemAt(i));
3094 if (res != OK) {
3095 // Can't get output buffer from gralloc queue - this could be due to
3096 // abandoned queue or other consumer misbehavior, so not a fatal
3097 // error
3098 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3099 " %s (%d)", strerror(-res), res);
3100
3101 return TIMED_OUT;
3102 }
3103 halRequest->num_output_buffers++;
3104 }
3105 totalNumBuffers += halRequest->num_output_buffers;
3106
3107 // Log request in the in-flight queue
3108 sp<Camera3Device> parent = mParent.promote();
3109 if (parent == NULL) {
3110 // Should not happen, and nowhere to send errors to, so just log it
3111 CLOGE("RequestThread: Parent is gone");
3112 return INVALID_OPERATION;
3113 }
3114 res = parent->registerInFlight(halRequest->frame_number,
3115 totalNumBuffers, captureRequest->mResultExtras,
3116 /*hasInput*/halRequest->input_buffer != NULL,
3117 captureRequest->mAeTriggerCancelOverride);
3118 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3119 ", burstId = %" PRId32 ".",
3120 __FUNCTION__,
3121 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3122 captureRequest->mResultExtras.burstId);
3123 if (res != OK) {
3124 SET_ERR("RequestThread: Unable to register new in-flight request:"
3125 " %s (%d)", strerror(-res), res);
3126 return INVALID_OPERATION;
3127 }
3128 }
3129
3130 return OK;
3131}
3132
Igor Murashkin1e479c02013-09-06 16:55:14 -07003133CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3134 Mutex::Autolock al(mLatestRequestMutex);
3135
3136 ALOGV("RequestThread::%s", __FUNCTION__);
3137
3138 return mLatestRequest;
3139}
3140
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003141bool Camera3Device::RequestThread::isStreamPending(
3142 sp<Camera3StreamInterface>& stream) {
3143 Mutex::Autolock l(mRequestLock);
3144
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003145 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003146 if (!nextRequest.submitted) {
3147 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3148 if (stream == s) return true;
3149 }
3150 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003151 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003152 }
3153
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003154 for (const auto& request : mRequestQueue) {
3155 for (const auto& s : request->mOutputStreams) {
3156 if (stream == s) return true;
3157 }
3158 if (stream == request->mInputStream) return true;
3159 }
3160
3161 for (const auto& request : mRepeatingRequests) {
3162 for (const auto& s : request->mOutputStreams) {
3163 if (stream == s) return true;
3164 }
3165 if (stream == request->mInputStream) return true;
3166 }
3167
3168 return false;
3169}
Jianing Weicb0652e2014-03-12 18:29:36 -07003170
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003171void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3172 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003173 return;
3174 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003175
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003176 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003177 // Skip the ones that have been submitted successfully.
3178 if (nextRequest.submitted) {
3179 continue;
3180 }
3181
3182 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3183 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3184 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3185
3186 if (halRequest->settings != NULL) {
3187 captureRequest->mSettings.unlock(halRequest->settings);
3188 }
3189
3190 if (captureRequest->mInputStream != NULL) {
3191 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3192 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3193 }
3194
3195 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3196 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3197 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3198 }
3199
3200 if (sendRequestError) {
3201 Mutex::Autolock l(mRequestLock);
3202 if (mListener != NULL) {
3203 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003204 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003205 captureRequest->mResultExtras);
3206 }
3207 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003208 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003209
3210 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003211 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003212}
3213
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003214void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003215 // Optimized a bit for the simple steady-state case (single repeating
3216 // request), to avoid putting that request in the queue temporarily.
3217 Mutex::Autolock l(mRequestLock);
3218
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003219 assert(mNextRequests.empty());
3220
3221 NextRequest nextRequest;
3222 nextRequest.captureRequest = waitForNextRequestLocked();
3223 if (nextRequest.captureRequest == nullptr) {
3224 return;
3225 }
3226
3227 nextRequest.halRequest = camera3_capture_request_t();
3228 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003229 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003230
3231 // Wait for additional requests
3232 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3233
3234 for (size_t i = 1; i < batchSize; i++) {
3235 NextRequest additionalRequest;
3236 additionalRequest.captureRequest = waitForNextRequestLocked();
3237 if (additionalRequest.captureRequest == nullptr) {
3238 break;
3239 }
3240
3241 additionalRequest.halRequest = camera3_capture_request_t();
3242 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003243 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003244 }
3245
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003246 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003247 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003248 mNextRequests.size(), batchSize);
3249 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003250 }
3251
3252 return;
3253}
3254
3255sp<Camera3Device::CaptureRequest>
3256 Camera3Device::RequestThread::waitForNextRequestLocked() {
3257 status_t res;
3258 sp<CaptureRequest> nextRequest;
3259
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003260 while (mRequestQueue.empty()) {
3261 if (!mRepeatingRequests.empty()) {
3262 // Always atomically enqueue all requests in a repeating request
3263 // list. Guarantees a complete in-sequence set of captures to
3264 // application.
3265 const RequestList &requests = mRepeatingRequests;
3266 RequestList::const_iterator firstRequest =
3267 requests.begin();
3268 nextRequest = *firstRequest;
3269 mRequestQueue.insert(mRequestQueue.end(),
3270 ++firstRequest,
3271 requests.end());
3272 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003273
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003274 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003275
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003276 break;
3277 }
3278
3279 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3280
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003281 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3282 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003283 Mutex::Autolock pl(mPauseLock);
3284 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003285 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003286 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003287 // Let the tracker know
3288 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3289 if (statusTracker != 0) {
3290 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3291 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003292 }
3293 // Stop waiting for now and let thread management happen
3294 return NULL;
3295 }
3296 }
3297
3298 if (nextRequest == NULL) {
3299 // Don't have a repeating request already in hand, so queue
3300 // must have an entry now.
3301 RequestList::iterator firstRequest =
3302 mRequestQueue.begin();
3303 nextRequest = *firstRequest;
3304 mRequestQueue.erase(firstRequest);
3305 }
3306
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003307 // In case we've been unpaused by setPaused clearing mDoPause, need to
3308 // update internal pause state (capture/setRepeatingRequest unpause
3309 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003310 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003311 if (mPaused) {
3312 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3313 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3314 if (statusTracker != 0) {
3315 statusTracker->markComponentActive(mStatusId);
3316 }
3317 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003318 mPaused = false;
3319
3320 // Check if we've reconfigured since last time, and reset the preview
3321 // request if so. Can't use 'NULL request == repeat' across configure calls.
3322 if (mReconfigured) {
3323 mPrevRequest.clear();
3324 mReconfigured = false;
3325 }
3326
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003327 if (nextRequest != NULL) {
3328 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003329 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3330 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003331
3332 // Since RequestThread::clear() removes buffers from the input stream,
3333 // get the right buffer here before unlocking mRequestLock
3334 if (nextRequest->mInputStream != NULL) {
3335 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3336 if (res != OK) {
3337 // Can't get input buffer from gralloc queue - this could be due to
3338 // disconnected queue or other producer misbehavior, so not a fatal
3339 // error
3340 ALOGE("%s: Can't get input buffer, skipping request:"
3341 " %s (%d)", __FUNCTION__, strerror(-res), res);
3342 if (mListener != NULL) {
3343 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003344 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003345 nextRequest->mResultExtras);
3346 }
3347 return NULL;
3348 }
3349 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003350 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003351
3352 handleAePrecaptureCancelRequest(nextRequest);
3353
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003354 return nextRequest;
3355}
3356
3357bool Camera3Device::RequestThread::waitIfPaused() {
3358 status_t res;
3359 Mutex::Autolock l(mPauseLock);
3360 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003361 if (mPaused == false) {
3362 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003363 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3364 // Let the tracker know
3365 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3366 if (statusTracker != 0) {
3367 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3368 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003369 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003370
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003371 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003372 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003373 return true;
3374 }
3375 }
3376 // We don't set mPaused to false here, because waitForNextRequest needs
3377 // to further manage the paused state in case of starvation.
3378 return false;
3379}
3380
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003381void Camera3Device::RequestThread::unpauseForNewRequests() {
3382 // With work to do, mark thread as unpaused.
3383 // If paused by request (setPaused), don't resume, to avoid
3384 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003385 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003386 Mutex::Autolock p(mPauseLock);
3387 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003388 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3389 if (mPaused) {
3390 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3391 if (statusTracker != 0) {
3392 statusTracker->markComponentActive(mStatusId);
3393 }
3394 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003395 mPaused = false;
3396 }
3397}
3398
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003399void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3400 sp<Camera3Device> parent = mParent.promote();
3401 if (parent != NULL) {
3402 va_list args;
3403 va_start(args, fmt);
3404
3405 parent->setErrorStateV(fmt, args);
3406
3407 va_end(args);
3408 }
3409}
3410
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003411status_t Camera3Device::RequestThread::insertTriggers(
3412 const sp<CaptureRequest> &request) {
3413
3414 Mutex::Autolock al(mTriggerMutex);
3415
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003416 sp<Camera3Device> parent = mParent.promote();
3417 if (parent == NULL) {
3418 CLOGE("RequestThread: Parent is gone");
3419 return DEAD_OBJECT;
3420 }
3421
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003422 CameraMetadata &metadata = request->mSettings;
3423 size_t count = mTriggerMap.size();
3424
3425 for (size_t i = 0; i < count; ++i) {
3426 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003427 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003428
3429 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3430 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3431 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003432 if (isAeTrigger) {
3433 request->mResultExtras.precaptureTriggerId = triggerId;
3434 mCurrentPreCaptureTriggerId = triggerId;
3435 } else {
3436 request->mResultExtras.afTriggerId = triggerId;
3437 mCurrentAfTriggerId = triggerId;
3438 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003439 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3440 continue; // Trigger ID tag is deprecated since device HAL 3.2
3441 }
3442 }
3443
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003444 camera_metadata_entry entry = metadata.find(tag);
3445
3446 if (entry.count > 0) {
3447 /**
3448 * Already has an entry for this trigger in the request.
3449 * Rewrite it with our requested trigger value.
3450 */
3451 RequestTrigger oldTrigger = trigger;
3452
3453 oldTrigger.entryValue = entry.data.u8[0];
3454
3455 mTriggerReplacedMap.add(tag, oldTrigger);
3456 } else {
3457 /**
3458 * More typical, no trigger entry, so we just add it
3459 */
3460 mTriggerRemovedMap.add(tag, trigger);
3461 }
3462
3463 status_t res;
3464
3465 switch (trigger.getTagType()) {
3466 case TYPE_BYTE: {
3467 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3468 res = metadata.update(tag,
3469 &entryValue,
3470 /*count*/1);
3471 break;
3472 }
3473 case TYPE_INT32:
3474 res = metadata.update(tag,
3475 &trigger.entryValue,
3476 /*count*/1);
3477 break;
3478 default:
3479 ALOGE("%s: Type not supported: 0x%x",
3480 __FUNCTION__,
3481 trigger.getTagType());
3482 return INVALID_OPERATION;
3483 }
3484
3485 if (res != OK) {
3486 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3487 ", value %d", __FUNCTION__, trigger.getTagName(),
3488 trigger.entryValue);
3489 return res;
3490 }
3491
3492 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3493 trigger.getTagName(),
3494 trigger.entryValue);
3495 }
3496
3497 mTriggerMap.clear();
3498
3499 return count;
3500}
3501
3502status_t Camera3Device::RequestThread::removeTriggers(
3503 const sp<CaptureRequest> &request) {
3504 Mutex::Autolock al(mTriggerMutex);
3505
3506 CameraMetadata &metadata = request->mSettings;
3507
3508 /**
3509 * Replace all old entries with their old values.
3510 */
3511 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3512 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3513
3514 status_t res;
3515
3516 uint32_t tag = trigger.metadataTag;
3517 switch (trigger.getTagType()) {
3518 case TYPE_BYTE: {
3519 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3520 res = metadata.update(tag,
3521 &entryValue,
3522 /*count*/1);
3523 break;
3524 }
3525 case TYPE_INT32:
3526 res = metadata.update(tag,
3527 &trigger.entryValue,
3528 /*count*/1);
3529 break;
3530 default:
3531 ALOGE("%s: Type not supported: 0x%x",
3532 __FUNCTION__,
3533 trigger.getTagType());
3534 return INVALID_OPERATION;
3535 }
3536
3537 if (res != OK) {
3538 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3539 ", trigger value %d", __FUNCTION__,
3540 trigger.getTagName(), trigger.entryValue);
3541 return res;
3542 }
3543 }
3544 mTriggerReplacedMap.clear();
3545
3546 /**
3547 * Remove all new entries.
3548 */
3549 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3550 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3551 status_t res = metadata.erase(trigger.metadataTag);
3552
3553 if (res != OK) {
3554 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3555 ", trigger value %d", __FUNCTION__,
3556 trigger.getTagName(), trigger.entryValue);
3557 return res;
3558 }
3559 }
3560 mTriggerRemovedMap.clear();
3561
3562 return OK;
3563}
3564
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003565status_t Camera3Device::RequestThread::addDummyTriggerIds(
3566 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003567 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003568 static const int32_t dummyTriggerId = 1;
3569 status_t res;
3570
3571 CameraMetadata &metadata = request->mSettings;
3572
3573 // If AF trigger is active, insert a dummy AF trigger ID if none already
3574 // exists
3575 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3576 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3577 if (afTrigger.count > 0 &&
3578 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3579 afId.count == 0) {
3580 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3581 if (res != OK) return res;
3582 }
3583
3584 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3585 // if none already exists
3586 camera_metadata_entry pcTrigger =
3587 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3588 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3589 if (pcTrigger.count > 0 &&
3590 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3591 pcId.count == 0) {
3592 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3593 &dummyTriggerId, 1);
3594 if (res != OK) return res;
3595 }
3596
3597 return OK;
3598}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003599
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003600/**
3601 * PreparerThread inner class methods
3602 */
3603
3604Camera3Device::PreparerThread::PreparerThread() :
3605 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3606}
3607
3608Camera3Device::PreparerThread::~PreparerThread() {
3609 Thread::requestExitAndWait();
3610 if (mCurrentStream != nullptr) {
3611 mCurrentStream->cancelPrepare();
3612 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3613 mCurrentStream.clear();
3614 }
3615 clear();
3616}
3617
Ruben Brunkc78ac262015-08-13 17:58:46 -07003618status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003619 status_t res;
3620
3621 Mutex::Autolock l(mLock);
3622
Ruben Brunkc78ac262015-08-13 17:58:46 -07003623 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003624 if (res == OK) {
3625 // No preparation needed, fire listener right off
3626 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3627 if (mListener) {
3628 mListener->notifyPrepared(stream->getId());
3629 }
3630 return OK;
3631 } else if (res != NOT_ENOUGH_DATA) {
3632 return res;
3633 }
3634
3635 // Need to prepare, start up thread if necessary
3636 if (!mActive) {
3637 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3638 // isn't running
3639 Thread::requestExitAndWait();
3640 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3641 if (res != OK) {
3642 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3643 if (mListener) {
3644 mListener->notifyPrepared(stream->getId());
3645 }
3646 return res;
3647 }
3648 mCancelNow = false;
3649 mActive = true;
3650 ALOGV("%s: Preparer stream started", __FUNCTION__);
3651 }
3652
3653 // queue up the work
3654 mPendingStreams.push_back(stream);
3655 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3656
3657 return OK;
3658}
3659
3660status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003661 Mutex::Autolock l(mLock);
3662
3663 for (const auto& stream : mPendingStreams) {
3664 stream->cancelPrepare();
3665 }
3666 mPendingStreams.clear();
3667 mCancelNow = true;
3668
3669 return OK;
3670}
3671
3672void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3673 Mutex::Autolock l(mLock);
3674 mListener = listener;
3675}
3676
3677bool Camera3Device::PreparerThread::threadLoop() {
3678 status_t res;
3679 {
3680 Mutex::Autolock l(mLock);
3681 if (mCurrentStream == nullptr) {
3682 // End thread if done with work
3683 if (mPendingStreams.empty()) {
3684 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3685 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3686 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3687 mActive = false;
3688 return false;
3689 }
3690
3691 // Get next stream to prepare
3692 auto it = mPendingStreams.begin();
3693 mCurrentStream = *it;
3694 mPendingStreams.erase(it);
3695 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3696 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3697 } else if (mCancelNow) {
3698 mCurrentStream->cancelPrepare();
3699 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3700 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3701 mCurrentStream.clear();
3702 mCancelNow = false;
3703 return true;
3704 }
3705 }
3706
3707 res = mCurrentStream->prepareNextBuffer();
3708 if (res == NOT_ENOUGH_DATA) return true;
3709 if (res != OK) {
3710 // Something bad happened; try to recover by cancelling prepare and
3711 // signalling listener anyway
3712 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3713 mCurrentStream->getId(), res, strerror(-res));
3714 mCurrentStream->cancelPrepare();
3715 }
3716
3717 // This stream has finished, notify listener
3718 Mutex::Autolock l(mLock);
3719 if (mListener) {
3720 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3721 mCurrentStream->getId());
3722 mListener->notifyPrepared(mCurrentStream->getId());
3723 }
3724
3725 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3726 mCurrentStream.clear();
3727
3728 return true;
3729}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003730
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003731/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003732 * Static callback forwarding methods from HAL to instance
3733 */
3734
3735void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3736 const camera3_capture_result *result) {
3737 Camera3Device *d =
3738 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003739
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003740 d->processCaptureResult(result);
3741}
3742
3743void Camera3Device::sNotify(const camera3_callback_ops *cb,
3744 const camera3_notify_msg *msg) {
3745 Camera3Device *d =
3746 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3747 d->notify(msg);
3748}
3749
3750}; // namespace android