blob: b44cbd239b6eecabd8577c323d091cc813f78182 [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);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001242 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001243 Mutex::Autolock l(mLock);
1244
1245 switch (mStatus) {
1246 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001247 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001248 return INVALID_OPERATION;
1249 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001250 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001251 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001252 case STATUS_UNCONFIGURED:
1253 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001254 case STATUS_ACTIVE:
1255 // OK
1256 break;
1257 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001258 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001259 return INVALID_OPERATION;
1260 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001261
Zhijun Hea1530f12014-09-14 12:44:20 -07001262 if (!mRequestTemplateCache[templateId].isEmpty()) {
1263 *request = mRequestTemplateCache[templateId];
1264 return OK;
1265 }
1266
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001267 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001268 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001269 rawRequest = mHal3Device->ops->construct_default_request_settings(
1270 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001271 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001272 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001273 ALOGI("%s: template %d is not supported on this camera device",
1274 __FUNCTION__, templateId);
1275 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001276 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001277 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001278 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001279
1280 return OK;
1281}
1282
1283status_t Camera3Device::waitUntilDrained() {
1284 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001285 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001286 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001287
Zhijun He69a37482014-03-23 18:44:49 -07001288 return waitUntilDrainedLocked();
1289}
1290
1291status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001292 switch (mStatus) {
1293 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001294 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001295 ALOGV("%s: Already idle", __FUNCTION__);
1296 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001297 case STATUS_CONFIGURED:
1298 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001299 case STATUS_ERROR:
1300 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001301 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001302 break;
1303 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001304 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001305 return INVALID_OPERATION;
1306 }
1307
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001308 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1309 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001310 if (res != OK) {
1311 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1312 res);
1313 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001314 return res;
1315}
1316
Ruben Brunk183f0562015-08-12 12:55:02 -07001317
1318void Camera3Device::internalUpdateStatusLocked(Status status) {
1319 mStatus = status;
1320 mRecentStatusUpdates.add(mStatus);
1321 mStatusChanged.broadcast();
1322}
1323
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001324// Pause to reconfigure
1325status_t Camera3Device::internalPauseAndWaitLocked() {
1326 mRequestThread->setPaused(true);
1327 mPauseStateNotify = true;
1328
1329 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1330 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1331 if (res != OK) {
1332 SET_ERR_L("Can't idle device in %f seconds!",
1333 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001334 }
1335
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001336 return res;
1337}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001338
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001339// Resume after internalPauseAndWaitLocked
1340status_t Camera3Device::internalResumeLocked() {
1341 status_t res;
1342
1343 mRequestThread->setPaused(false);
1344
1345 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1346 if (res != OK) {
1347 SET_ERR_L("Can't transition to active in %f seconds!",
1348 kActiveTimeout/1e9);
1349 }
1350 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001351 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001352}
1353
Ruben Brunk183f0562015-08-12 12:55:02 -07001354status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001355 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001356
1357 size_t startIndex = 0;
1358 if (mStatusWaiters == 0) {
1359 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1360 // this status list
1361 mRecentStatusUpdates.clear();
1362 } else {
1363 // If other threads are waiting on updates to this status list, set the position of the
1364 // first element that this list will check rather than clearing the list.
1365 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001366 }
1367
Ruben Brunk183f0562015-08-12 12:55:02 -07001368 mStatusWaiters++;
1369
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001370 bool stateSeen = false;
1371 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001372 if (active == (mStatus == STATUS_ACTIVE)) {
1373 // Desired state is current
1374 break;
1375 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001376
1377 res = mStatusChanged.waitRelative(mLock, timeout);
1378 if (res != OK) break;
1379
Ruben Brunk183f0562015-08-12 12:55:02 -07001380 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1381 // transitions.
1382 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1383 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1384 __FUNCTION__);
1385
1386 // Encountered desired state since we began waiting
1387 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001388 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1389 stateSeen = true;
1390 break;
1391 }
1392 }
1393 } while (!stateSeen);
1394
Ruben Brunk183f0562015-08-12 12:55:02 -07001395 mStatusWaiters--;
1396
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001397 return res;
1398}
1399
1400
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001401status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1402 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001403 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001404
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001405 if (listener != NULL && mListener != NULL) {
1406 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1407 }
1408 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001409 mRequestThread->setNotificationListener(listener);
1410 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001411
1412 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001413}
1414
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001415bool Camera3Device::willNotify3A() {
1416 return false;
1417}
1418
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001419status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001420 status_t res;
1421 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001422
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001423 while (mResultQueue.empty()) {
1424 res = mResultSignal.waitRelative(mOutputLock, timeout);
1425 if (res == TIMED_OUT) {
1426 return res;
1427 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001428 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001429 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001430 return res;
1431 }
1432 }
1433 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001434}
1435
Jianing Weicb0652e2014-03-12 18:29:36 -07001436status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001437 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001438 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001439
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001440 if (mResultQueue.empty()) {
1441 return NOT_ENOUGH_DATA;
1442 }
1443
Jianing Weicb0652e2014-03-12 18:29:36 -07001444 if (frame == NULL) {
1445 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1446 return BAD_VALUE;
1447 }
1448
1449 CaptureResult &result = *(mResultQueue.begin());
1450 frame->mResultExtras = result.mResultExtras;
1451 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001452 mResultQueue.erase(mResultQueue.begin());
1453
1454 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001455}
1456
1457status_t Camera3Device::triggerAutofocus(uint32_t id) {
1458 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001459 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001460
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001461 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1462 // Mix-in this trigger into the next request and only the next request.
1463 RequestTrigger trigger[] = {
1464 {
1465 ANDROID_CONTROL_AF_TRIGGER,
1466 ANDROID_CONTROL_AF_TRIGGER_START
1467 },
1468 {
1469 ANDROID_CONTROL_AF_TRIGGER_ID,
1470 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001471 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001472 };
1473
1474 return mRequestThread->queueTrigger(trigger,
1475 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001476}
1477
1478status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1479 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001480 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001481
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001482 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1483 // Mix-in this trigger into the next request and only the next request.
1484 RequestTrigger trigger[] = {
1485 {
1486 ANDROID_CONTROL_AF_TRIGGER,
1487 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1488 },
1489 {
1490 ANDROID_CONTROL_AF_TRIGGER_ID,
1491 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001492 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001493 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001494
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001495 return mRequestThread->queueTrigger(trigger,
1496 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001497}
1498
1499status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1500 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001501 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001502
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001503 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1504 // Mix-in this trigger into the next request and only the next request.
1505 RequestTrigger trigger[] = {
1506 {
1507 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1508 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1509 },
1510 {
1511 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1512 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001513 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001514 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001515
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001516 return mRequestThread->queueTrigger(trigger,
1517 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001518}
1519
1520status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1521 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1522 ATRACE_CALL();
1523 (void)reprocessStreamId; (void)buffer; (void)listener;
1524
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001525 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001526 return INVALID_OPERATION;
1527}
1528
Jianing Weicb0652e2014-03-12 18:29:36 -07001529status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001530 ATRACE_CALL();
1531 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001532 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001533
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001534 NotificationListener* listener;
1535 {
1536 Mutex::Autolock l(mOutputLock);
1537 listener = mListener;
1538 }
1539
Zhijun He7ef20392014-04-21 16:04:17 -07001540 {
1541 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001542 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001543 }
1544
Zhijun He491e3412013-12-27 10:57:44 -08001545 status_t res;
1546 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001547 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001548 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001549 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001550 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001551 }
1552
1553 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001554}
1555
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001556status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001557 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1558}
1559
1560status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001561 ATRACE_CALL();
1562 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001563 Mutex::Autolock il(mInterfaceLock);
1564 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001565
1566 sp<Camera3StreamInterface> stream;
1567 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1568 if (outputStreamIdx == NAME_NOT_FOUND) {
1569 CLOGE("Stream %d does not exist", streamId);
1570 return BAD_VALUE;
1571 }
1572
1573 stream = mOutputStreams.editValueAt(outputStreamIdx);
1574
1575 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001576 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001577 return BAD_VALUE;
1578 }
1579
1580 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001581 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001582 return BAD_VALUE;
1583 }
1584
Ruben Brunkc78ac262015-08-13 17:58:46 -07001585 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001586}
1587
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001588status_t Camera3Device::tearDown(int streamId) {
1589 ATRACE_CALL();
1590 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1591 Mutex::Autolock il(mInterfaceLock);
1592 Mutex::Autolock l(mLock);
1593
1594 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1595 // since we cannot call register_stream_buffers except right after configure_streams.
1596 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1597 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1598 __FUNCTION__, mHal3Device->common.version);
1599 return NO_INIT;
1600 }
1601
1602 sp<Camera3StreamInterface> stream;
1603 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1604 if (outputStreamIdx == NAME_NOT_FOUND) {
1605 CLOGE("Stream %d does not exist", streamId);
1606 return BAD_VALUE;
1607 }
1608
1609 stream = mOutputStreams.editValueAt(outputStreamIdx);
1610
1611 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1612 CLOGE("Stream %d is a target of a in-progress request", streamId);
1613 return BAD_VALUE;
1614 }
1615
1616 return stream->tearDown();
1617}
1618
Zhijun He204e3292014-07-14 17:09:23 -07001619uint32_t Camera3Device::getDeviceVersion() {
1620 ATRACE_CALL();
1621 Mutex::Autolock il(mInterfaceLock);
1622 return mDeviceVersion;
1623}
1624
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001625/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001626 * Methods called by subclasses
1627 */
1628
1629void Camera3Device::notifyStatus(bool idle) {
1630 {
1631 // Need mLock to safely update state and synchronize to current
1632 // state of methods in flight.
1633 Mutex::Autolock l(mLock);
1634 // We can get various system-idle notices from the status tracker
1635 // while starting up. Only care about them if we've actually sent
1636 // in some requests recently.
1637 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1638 return;
1639 }
1640 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1641 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001642 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001643
1644 // Skip notifying listener if we're doing some user-transparent
1645 // state changes
1646 if (mPauseStateNotify) return;
1647 }
1648 NotificationListener *listener;
1649 {
1650 Mutex::Autolock l(mOutputLock);
1651 listener = mListener;
1652 }
1653 if (idle && listener != NULL) {
1654 listener->notifyIdle();
1655 }
1656}
1657
1658/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001659 * Camera3Device private methods
1660 */
1661
1662sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1663 const CameraMetadata &request) {
1664 ATRACE_CALL();
1665 status_t res;
1666
1667 sp<CaptureRequest> newRequest = new CaptureRequest;
1668 newRequest->mSettings = request;
1669
1670 camera_metadata_entry_t inputStreams =
1671 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1672 if (inputStreams.count > 0) {
1673 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001674 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001675 CLOGE("Request references unknown input stream %d",
1676 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001677 return NULL;
1678 }
1679 // Lazy completion of stream configuration (allocation/registration)
1680 // on first use
1681 if (mInputStream->isConfiguring()) {
1682 res = mInputStream->finishConfiguration(mHal3Device);
1683 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001684 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001685 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001686 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001687 return NULL;
1688 }
1689 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001690 // Check if stream is being prepared
1691 if (mInputStream->isPreparing()) {
1692 CLOGE("Request references an input stream that's being prepared!");
1693 return NULL;
1694 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001695
1696 newRequest->mInputStream = mInputStream;
1697 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1698 }
1699
1700 camera_metadata_entry_t streams =
1701 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1702 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001703 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001704 return NULL;
1705 }
1706
1707 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001708 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001709 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001710 CLOGE("Request references unknown stream %d",
1711 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001712 return NULL;
1713 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001714 sp<Camera3OutputStreamInterface> stream =
1715 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001716
1717 // Lazy completion of stream configuration (allocation/registration)
1718 // on first use
1719 if (stream->isConfiguring()) {
1720 res = stream->finishConfiguration(mHal3Device);
1721 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001722 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1723 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001724 return NULL;
1725 }
1726 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001727 // Check if stream is being prepared
1728 if (stream->isPreparing()) {
1729 CLOGE("Request references an output stream that's being prepared!");
1730 return NULL;
1731 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001732
1733 newRequest->mOutputStreams.push(stream);
1734 }
1735 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001736 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001737
1738 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001739}
1740
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001741bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1742 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1743 Size size = mSupportedOpaqueInputSizes[i];
1744 if (size.width == width && size.height == height) {
1745 return true;
1746 }
1747 }
1748
1749 return false;
1750}
1751
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001752status_t Camera3Device::configureStreamsLocked() {
1753 ATRACE_CALL();
1754 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001755
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001756 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001757 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001758 return INVALID_OPERATION;
1759 }
1760
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001761 if (!mNeedConfig) {
1762 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1763 return OK;
1764 }
1765
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001766 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1767 // adding a dummy stream instead.
1768 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1769 if (mOutputStreams.size() == 0) {
1770 addDummyStreamLocked();
1771 } else {
1772 tryRemoveDummyStreamLocked();
1773 }
1774
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001775 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001776 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001777
1778 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001779 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1780 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1781 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001782 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1783
1784 Vector<camera3_stream_t*> streams;
1785 streams.setCapacity(config.num_streams);
1786
1787 if (mInputStream != NULL) {
1788 camera3_stream_t *inputStream;
1789 inputStream = mInputStream->startConfiguration();
1790 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001791 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001792 return INVALID_OPERATION;
1793 }
1794 streams.add(inputStream);
1795 }
1796
1797 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001798
1799 // Don't configure bidi streams twice, nor add them twice to the list
1800 if (mOutputStreams[i].get() ==
1801 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1802
1803 config.num_streams--;
1804 continue;
1805 }
1806
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001807 camera3_stream_t *outputStream;
1808 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1809 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001810 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001811 return INVALID_OPERATION;
1812 }
1813 streams.add(outputStream);
1814 }
1815
1816 config.streams = streams.editArray();
1817
1818 // Do the HAL configuration; will potentially touch stream
1819 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001820 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001821 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001822 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001823
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001824 if (res == BAD_VALUE) {
1825 // HAL rejected this set of streams as unsupported, clean up config
1826 // attempt and return to unconfigured state
1827 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1828 res = mInputStream->cancelConfiguration();
1829 if (res != OK) {
1830 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1831 mInputStream->getId(), strerror(-res), res);
1832 return res;
1833 }
1834 }
1835
1836 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1837 sp<Camera3OutputStreamInterface> outputStream =
1838 mOutputStreams.editValueAt(i);
1839 if (outputStream->isConfiguring()) {
1840 res = outputStream->cancelConfiguration();
1841 if (res != OK) {
1842 SET_ERR_L(
1843 "Can't cancel configuring output stream %d: %s (%d)",
1844 outputStream->getId(), strerror(-res), res);
1845 return res;
1846 }
1847 }
1848 }
1849
1850 // Return state to that at start of call, so that future configures
1851 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001852 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001853 mNeedConfig = true;
1854
1855 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1856 return BAD_VALUE;
1857 } else if (res != OK) {
1858 // Some other kind of error from configure_streams - this is not
1859 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001860 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1861 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001862 return res;
1863 }
1864
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001865 // Finish all stream configuration immediately.
1866 // TODO: Try to relax this later back to lazy completion, which should be
1867 // faster
1868
Igor Murashkin073f8572013-05-02 14:59:28 -07001869 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001870 res = mInputStream->finishConfiguration(mHal3Device);
1871 if (res != OK) {
1872 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1873 mInputStream->getId(), strerror(-res), res);
1874 return res;
1875 }
1876 }
1877
1878 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001879 sp<Camera3OutputStreamInterface> outputStream =
1880 mOutputStreams.editValueAt(i);
1881 if (outputStream->isConfiguring()) {
1882 res = outputStream->finishConfiguration(mHal3Device);
1883 if (res != OK) {
1884 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1885 outputStream->getId(), strerror(-res), res);
1886 return res;
1887 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001888 }
1889 }
1890
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001891 // Request thread needs to know to avoid using repeat-last-settings protocol
1892 // across configure_streams() calls
1893 mRequestThread->configurationComplete();
1894
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07001895 // Boost priority of request thread for high speed recording to SCHED_FIFO
1896 if (mIsConstrainedHighSpeedConfiguration) {
1897 pid_t requestThreadTid = mRequestThread->getTid();
1898 res = requestPriority(getpid(), requestThreadTid,
1899 kConstrainedHighSpeedThreadPriority, true);
1900 if (res != OK) {
1901 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
1902 strerror(-res), res);
1903 } else {
1904 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
1905 }
1906 } else {
1907 // TODO: Set/restore normal priority for normal use cases
1908 }
1909
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001910 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001911
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001912 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001913
Ruben Brunk183f0562015-08-12 12:55:02 -07001914 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1915 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001916
1917 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1918
Zhijun He0a210512014-07-24 13:45:15 -07001919 // tear down the deleted streams after configure streams.
1920 mDeletedStreams.clear();
1921
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001922 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001923}
1924
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001925status_t Camera3Device::addDummyStreamLocked() {
1926 ATRACE_CALL();
1927 status_t res;
1928
1929 if (mDummyStreamId != NO_STREAM) {
1930 // Should never be adding a second dummy stream when one is already
1931 // active
1932 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1933 __FUNCTION__, mId);
1934 return INVALID_OPERATION;
1935 }
1936
1937 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1938
1939 sp<Camera3OutputStreamInterface> dummyStream =
1940 new Camera3DummyStream(mNextStreamId);
1941
1942 res = mOutputStreams.add(mNextStreamId, dummyStream);
1943 if (res < 0) {
1944 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1945 return res;
1946 }
1947
1948 mDummyStreamId = mNextStreamId;
1949 mNextStreamId++;
1950
1951 return OK;
1952}
1953
1954status_t Camera3Device::tryRemoveDummyStreamLocked() {
1955 ATRACE_CALL();
1956 status_t res;
1957
1958 if (mDummyStreamId == NO_STREAM) return OK;
1959 if (mOutputStreams.size() == 1) return OK;
1960
1961 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1962
1963 // Ok, have a dummy stream and there's at least one other output stream,
1964 // so remove the dummy
1965
1966 sp<Camera3StreamInterface> deletedStream;
1967 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1968 if (outputStreamIdx == NAME_NOT_FOUND) {
1969 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1970 return INVALID_OPERATION;
1971 }
1972
1973 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1974 mOutputStreams.removeItemsAt(outputStreamIdx);
1975
1976 // Free up the stream endpoint so that it can be used by some other stream
1977 res = deletedStream->disconnect();
1978 if (res != OK) {
1979 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1980 // fall through since we want to still list the stream as deleted.
1981 }
1982 mDeletedStreams.add(deletedStream);
1983 mDummyStreamId = NO_STREAM;
1984
1985 return res;
1986}
1987
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001988void Camera3Device::setErrorState(const char *fmt, ...) {
1989 Mutex::Autolock l(mLock);
1990 va_list args;
1991 va_start(args, fmt);
1992
1993 setErrorStateLockedV(fmt, args);
1994
1995 va_end(args);
1996}
1997
1998void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1999 Mutex::Autolock l(mLock);
2000 setErrorStateLockedV(fmt, args);
2001}
2002
2003void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2004 va_list args;
2005 va_start(args, fmt);
2006
2007 setErrorStateLockedV(fmt, args);
2008
2009 va_end(args);
2010}
2011
2012void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002013 // Print out all error messages to log
2014 String8 errorCause = String8::formatV(fmt, args);
2015 ALOGE("Camera %d: %s", mId, errorCause.string());
2016
2017 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002018 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002019
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002020 mErrorCause = errorCause;
2021
2022 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002023 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002024
2025 // Notify upstream about a device error
2026 if (mListener != NULL) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002027 mListener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002028 CaptureResultExtras());
2029 }
2030
2031 // Save stack trace. View by dumping it later.
2032 CameraTraces::saveTrace();
2033 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002034}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002035
2036/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002037 * In-flight request management
2038 */
2039
Jianing Weicb0652e2014-03-12 18:29:36 -07002040status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002041 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2042 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002043 ATRACE_CALL();
2044 Mutex::Autolock l(mInFlightLock);
2045
2046 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002047 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2048 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002049 if (res < 0) return res;
2050
2051 return OK;
2052}
2053
2054/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002055 * Check if all 3A fields are ready, and send off a partial 3A-only result
2056 * to the output frame queue
2057 */
Zhijun He204e3292014-07-14 17:09:23 -07002058bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07002059 uint32_t frameNumber,
2060 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002061
2062 // Check if all 3A states are present
2063 // The full list of fields is
2064 // android.control.afMode
2065 // android.control.awbMode
2066 // android.control.aeState
2067 // android.control.awbState
2068 // android.control.afState
2069 // android.control.afTriggerID
2070 // android.control.aePrecaptureID
2071 // TODO: Add android.control.aeMode
2072
2073 bool gotAllStates = true;
2074
2075 uint8_t afMode;
2076 uint8_t awbMode;
2077 uint8_t aeState;
2078 uint8_t afState;
2079 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002080
2081 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
2082 &afMode, frameNumber);
2083
2084 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
2085 &awbMode, frameNumber);
2086
2087 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
2088 &aeState, frameNumber);
2089
2090 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
2091 &afState, frameNumber);
2092
2093 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
2094 &awbState, frameNumber);
2095
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002096 if (!gotAllStates) return false;
2097
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002098 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002099 "AF state %d, AE state %d, AWB state %d, "
2100 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002101 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002102 afMode, awbMode,
2103 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002104 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002105
2106 // Got all states, so construct a minimal result to send
2107 // In addition to the above fields, this means adding in
2108 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002109 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07002110 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002111
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002112 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002113
2114 Mutex::Autolock l(mOutputLock);
2115
Jianing Weicb0652e2014-03-12 18:29:36 -07002116 CaptureResult captureResult;
2117 captureResult.mResultExtras = resultExtras;
2118 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
2119 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
2120 // but not limited to CameraDeviceBase::getNextResult
2121 CaptureResult& min3AResult =
2122 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002123
Jianing Weicb0652e2014-03-12 18:29:36 -07002124 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
2125 // TODO: This is problematic casting. Need to fix CameraMetadata.
2126 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002127 return false;
2128 }
2129
Jianing Weicb0652e2014-03-12 18:29:36 -07002130 int32_t requestId = resultExtras.requestId;
2131 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002132 &requestId, frameNumber)) {
2133 return false;
2134 }
2135
Zhijun He204e3292014-07-14 17:09:23 -07002136 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
2137 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
2138 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
2139 &partialResult, frameNumber)) {
2140 return false;
2141 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002142 }
2143
Jianing Weicb0652e2014-03-12 18:29:36 -07002144 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002145 &afMode, frameNumber)) {
2146 return false;
2147 }
2148
Jianing Weicb0652e2014-03-12 18:29:36 -07002149 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002150 &awbMode, frameNumber)) {
2151 return false;
2152 }
2153
Jianing Weicb0652e2014-03-12 18:29:36 -07002154 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002155 &aeState, frameNumber)) {
2156 return false;
2157 }
2158
Jianing Weicb0652e2014-03-12 18:29:36 -07002159 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002160 &afState, frameNumber)) {
2161 return false;
2162 }
2163
Jianing Weicb0652e2014-03-12 18:29:36 -07002164 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002165 &awbState, frameNumber)) {
2166 return false;
2167 }
2168
Jianing Weicb0652e2014-03-12 18:29:36 -07002169 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002170 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002171 return false;
2172 }
2173
Jianing Weicb0652e2014-03-12 18:29:36 -07002174 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002175 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002176 return false;
2177 }
2178
Zhijun He204e3292014-07-14 17:09:23 -07002179 // We only send the aggregated partial when all 3A related metadata are available
2180 // For both API1 and API2.
2181 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002182 mResultSignal.signal();
2183
2184 return true;
2185}
2186
2187template<typename T>
2188bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002189 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002190 (void) frameNumber;
2191
2192 camera_metadata_ro_entry_t entry;
2193
2194 entry = result.find(tag);
2195 if (entry.count == 0) {
2196 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2197 mId, frameNumber, get_camera_metadata_tag_name(tag));
2198 return false;
2199 }
2200
2201 if (sizeof(T) == sizeof(uint8_t)) {
2202 *value = entry.data.u8[0];
2203 } else if (sizeof(T) == sizeof(int32_t)) {
2204 *value = entry.data.i32[0];
2205 } else {
2206 ALOGE("%s: Unexpected type", __FUNCTION__);
2207 return false;
2208 }
2209 return true;
2210}
2211
2212template<typename T>
2213bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002214 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002215 if (result.update(tag, value, 1) != NO_ERROR) {
2216 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2217 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2218 frameNumber, get_camera_metadata_tag_name(tag));
2219 return false;
2220 }
2221 return true;
2222}
2223
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002224void Camera3Device::returnOutputBuffers(
2225 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2226 nsecs_t timestamp) {
2227 for (size_t i = 0; i < numBuffers; i++)
2228 {
2229 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2230 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2231 // Note: stream may be deallocated at this point, if this buffer was
2232 // the last reference to it.
2233 if (res != OK) {
2234 ALOGE("Can't return buffer to its stream: %s (%d)",
2235 strerror(-res), res);
2236 }
2237 }
2238}
2239
2240
2241void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2242
2243 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2244 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2245
2246 nsecs_t sensorTimestamp = request.sensorTimestamp;
2247 nsecs_t shutterTimestamp = request.shutterTimestamp;
2248
2249 // Check if it's okay to remove the request from InFlightMap:
2250 // In the case of a successful request:
2251 // all input and output buffers, all result metadata, shutter callback
2252 // arrived.
2253 // In the case of a unsuccessful request:
2254 // all input and output buffers arrived.
2255 if (request.numBuffersLeft == 0 &&
2256 (request.requestStatus != OK ||
2257 (request.haveResultMetadata && shutterTimestamp != 0))) {
2258 ATRACE_ASYNC_END("frame capture", frameNumber);
2259
2260 // Sanity check - if sensor timestamp matches shutter timestamp
2261 if (request.requestStatus == OK &&
2262 sensorTimestamp != shutterTimestamp) {
2263 SET_ERR("sensor timestamp (%" PRId64
2264 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2265 sensorTimestamp, frameNumber, shutterTimestamp);
2266 }
2267
2268 // for an unsuccessful request, it may have pending output buffers to
2269 // return.
2270 assert(request.requestStatus != OK ||
2271 request.pendingOutputBuffers.size() == 0);
2272 returnOutputBuffers(request.pendingOutputBuffers.array(),
2273 request.pendingOutputBuffers.size(), 0);
2274
2275 mInFlightMap.removeItemsAt(idx, 1);
2276
2277 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2278 }
2279
2280 // Sanity check - if we have too many in-flight frames, something has
2281 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002282 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002283 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002284 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2285 kInFlightWarnLimitHighSpeed) {
2286 CLOGE("In-flight list too large for high speed configuration: %zu",
2287 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002288 }
2289}
2290
2291
2292void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2293 CaptureResultExtras &resultExtras,
2294 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002295 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002296 bool reprocess,
2297 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002298 if (pendingMetadata.isEmpty())
2299 return;
2300
2301 Mutex::Autolock l(mOutputLock);
2302
2303 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002304 if (reprocess) {
2305 if (frameNumber < mNextReprocessResultFrameNumber) {
2306 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002307 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002308 frameNumber, mNextReprocessResultFrameNumber);
2309 return;
2310 }
2311 mNextReprocessResultFrameNumber = frameNumber + 1;
2312 } else {
2313 if (frameNumber < mNextResultFrameNumber) {
2314 SET_ERR("Out-of-order capture result metadata submitted! "
2315 "(got frame number %d, expecting %d)",
2316 frameNumber, mNextResultFrameNumber);
2317 return;
2318 }
2319 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002320 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002321
2322 CaptureResult captureResult;
2323 captureResult.mResultExtras = resultExtras;
2324 captureResult.mMetadata = pendingMetadata;
2325
2326 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2327 (int32_t*)&frameNumber, 1) != OK) {
2328 SET_ERR("Failed to set frame# in metadata (%d)",
2329 frameNumber);
2330 return;
2331 } else {
2332 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2333 __FUNCTION__, mId, frameNumber);
2334 }
2335
2336 // Append any previous partials to form a complete result
2337 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2338 captureResult.mMetadata.append(collectedPartialResult);
2339 }
2340
2341 captureResult.mMetadata.sort();
2342
2343 // Check that there's a timestamp in the result metadata
2344 camera_metadata_entry entry =
2345 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2346 if (entry.count == 0) {
2347 SET_ERR("No timestamp provided by HAL for frame %d!",
2348 frameNumber);
2349 return;
2350 }
2351
Chien-Yu Chend196d612015-06-22 19:49:01 -07002352 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2353
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002354 // Valid result, insert into queue
2355 List<CaptureResult>::iterator queuedResult =
2356 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2357 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2358 ", burstId = %" PRId32, __FUNCTION__,
2359 queuedResult->mResultExtras.requestId,
2360 queuedResult->mResultExtras.frameNumber,
2361 queuedResult->mResultExtras.burstId);
2362
2363 mResultSignal.signal();
2364}
2365
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002366/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002367 * Camera HAL device callback methods
2368 */
2369
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002370void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002371 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002372
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002373 status_t res;
2374
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002375 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002376 if (result->result == NULL && result->num_output_buffers == 0 &&
2377 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002378 SET_ERR("No result data provided by HAL for frame %d",
2379 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002380 return;
2381 }
Zhijun He204e3292014-07-14 17:09:23 -07002382
2383 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2384 // partial_result to 1 when metadata is included in this result.
2385 if (!mUsePartialResult &&
2386 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2387 result->result != NULL &&
2388 result->partial_result != 1) {
2389 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2390 " if partial result is not supported",
2391 frameNumber, result->partial_result);
2392 return;
2393 }
2394
2395 bool isPartialResult = false;
2396 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002397 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002398 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002399
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002400 // Get shutter timestamp and resultExtras from list of in-flight requests,
2401 // where it was added by the shutter notification for this frame. If the
2402 // shutter timestamp isn't received yet, append the output buffers to the
2403 // in-flight request and they will be returned when the shutter timestamp
2404 // arrives. Update the in-flight status and remove the in-flight entry if
2405 // all result data and shutter timestamp have been received.
2406 nsecs_t shutterTimestamp = 0;
2407
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002408 {
2409 Mutex::Autolock l(mInFlightLock);
2410 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2411 if (idx == NAME_NOT_FOUND) {
2412 SET_ERR("Unknown frame number for capture result: %d",
2413 frameNumber);
2414 return;
2415 }
2416 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002417 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2418 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2419 ", partialResultCount = %d",
2420 __FUNCTION__, request.resultExtras.requestId,
2421 request.resultExtras.frameNumber, request.resultExtras.burstId,
2422 result->partial_result);
2423 // Always update the partial count to the latest one if it's not 0
2424 // (buffers only). When framework aggregates adjacent partial results
2425 // into one, the latest partial count will be used.
2426 if (result->partial_result != 0)
2427 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002428
2429 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002430 if (mUsePartialResult && result->result != NULL) {
2431 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2432 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2433 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2434 " the range of [1, %d] when metadata is included in the result",
2435 frameNumber, result->partial_result, mNumPartialResults);
2436 return;
2437 }
2438 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002439 if (isPartialResult) {
2440 request.partialResult.collectedResult.append(result->result);
2441 }
Zhijun He204e3292014-07-14 17:09:23 -07002442 } else {
2443 camera_metadata_ro_entry_t partialResultEntry;
2444 res = find_camera_metadata_ro_entry(result->result,
2445 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2446 if (res != NAME_NOT_FOUND &&
2447 partialResultEntry.count > 0 &&
2448 partialResultEntry.data.u8[0] ==
2449 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2450 // A partial result. Flag this as such, and collect this
2451 // set of metadata into the in-flight entry.
2452 isPartialResult = true;
2453 request.partialResult.collectedResult.append(
2454 result->result);
2455 request.partialResult.collectedResult.erase(
2456 ANDROID_QUIRKS_PARTIAL_RESULT);
2457 }
2458 }
2459
2460 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002461 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002462 if (!request.partialResult.haveSent3A) {
2463 request.partialResult.haveSent3A =
2464 processPartial3AResult(frameNumber,
2465 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002466 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002467 }
2468 }
2469 }
2470
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002471 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002472 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002473
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002474 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002475 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002476 if (request.haveResultMetadata) {
2477 SET_ERR("Called multiple times with metadata for frame %d",
2478 frameNumber);
2479 return;
2480 }
Zhijun He204e3292014-07-14 17:09:23 -07002481 if (mUsePartialResult &&
2482 !request.partialResult.collectedResult.isEmpty()) {
2483 collectedPartialResult.acquire(
2484 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002485 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002486 request.haveResultMetadata = true;
2487 }
2488
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002489 uint32_t numBuffersReturned = result->num_output_buffers;
2490 if (result->input_buffer != NULL) {
2491 if (hasInputBufferInRequest) {
2492 numBuffersReturned += 1;
2493 } else {
2494 ALOGW("%s: Input buffer should be NULL if there is no input"
2495 " buffer sent in the request",
2496 __FUNCTION__);
2497 }
2498 }
2499 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002500 if (request.numBuffersLeft < 0) {
2501 SET_ERR("Too many buffers returned for frame %d",
2502 frameNumber);
2503 return;
2504 }
2505
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002506 camera_metadata_ro_entry_t entry;
2507 res = find_camera_metadata_ro_entry(result->result,
2508 ANDROID_SENSOR_TIMESTAMP, &entry);
2509 if (res == OK && entry.count == 1) {
2510 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002511 }
2512
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002513 // If shutter event isn't received yet, append the output buffers to
2514 // the in-flight request. Otherwise, return the output buffers to
2515 // streams.
2516 if (shutterTimestamp == 0) {
2517 request.pendingOutputBuffers.appendArray(result->output_buffers,
2518 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002519 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002520 returnOutputBuffers(result->output_buffers,
2521 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002522 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002523
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002524 if (result->result != NULL && !isPartialResult) {
2525 if (shutterTimestamp == 0) {
2526 request.pendingMetadata = result->result;
2527 request.partialResult.collectedResult = collectedPartialResult;
2528 } else {
2529 CameraMetadata metadata;
2530 metadata = result->result;
2531 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002532 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2533 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002534 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002535 }
2536
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002537 removeInFlightRequestIfReadyLocked(idx);
2538 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002539
Zhijun Hef0d962a2014-06-30 10:24:11 -07002540 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002541 if (hasInputBufferInRequest) {
2542 Camera3Stream *stream =
2543 Camera3Stream::cast(result->input_buffer->stream);
2544 res = stream->returnInputBuffer(*(result->input_buffer));
2545 // Note: stream may be deallocated at this point, if this buffer was the
2546 // last reference to it.
2547 if (res != OK) {
2548 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2549 " its stream:%s (%d)", __FUNCTION__,
2550 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002551 }
2552 } else {
2553 ALOGW("%s: Input buffer should be NULL if there is no input"
2554 " buffer sent in the request, skipping input buffer return.",
2555 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002556 }
2557 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002558}
2559
2560void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002561 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002562 NotificationListener *listener;
2563 {
2564 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002565 listener = mListener;
2566 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002567
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002568 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002569 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002570 return;
2571 }
2572
2573 switch (msg->type) {
2574 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002575 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002576 break;
2577 }
2578 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002579 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002580 break;
2581 }
2582 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002583 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002584 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002585 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002586}
2587
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002588void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2589 NotificationListener *listener) {
2590
2591 // Map camera HAL error codes to ICameraDeviceCallback error codes
2592 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002593 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002594 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002595 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002596 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002597 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002598 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002599 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002600 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002601 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002602 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002603 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002604 };
2605
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002606 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002607 ((msg.error_code >= 0) &&
2608 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2609 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002610 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002611
2612 int streamId = 0;
2613 if (msg.error_stream != NULL) {
2614 Camera3Stream *stream =
2615 Camera3Stream::cast(msg.error_stream);
2616 streamId = stream->getId();
2617 }
2618 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2619 mId, __FUNCTION__, msg.frame_number,
2620 streamId, msg.error_code);
2621
2622 CaptureResultExtras resultExtras;
2623 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002624 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002625 // SET_ERR calls notifyError
2626 SET_ERR("Camera HAL reported serious device error");
2627 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002628 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2629 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2630 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002631 {
2632 Mutex::Autolock l(mInFlightLock);
2633 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2634 if (idx >= 0) {
2635 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2636 r.requestStatus = msg.error_code;
2637 resultExtras = r.resultExtras;
2638 } else {
2639 resultExtras.frameNumber = msg.frame_number;
2640 ALOGE("Camera %d: %s: cannot find in-flight request on "
2641 "frame %" PRId64 " error", mId, __FUNCTION__,
2642 resultExtras.frameNumber);
2643 }
2644 }
2645 if (listener != NULL) {
2646 listener->notifyError(errorCode, resultExtras);
2647 } else {
2648 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2649 }
2650 break;
2651 default:
2652 // SET_ERR calls notifyError
2653 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2654 break;
2655 }
2656}
2657
2658void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2659 NotificationListener *listener) {
2660 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002661
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002662 // Set timestamp for the request in the in-flight tracking
2663 // and get the request ID to send upstream
2664 {
2665 Mutex::Autolock l(mInFlightLock);
2666 idx = mInFlightMap.indexOfKey(msg.frame_number);
2667 if (idx >= 0) {
2668 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002669
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002670 // Verify ordering of shutter notifications
2671 {
2672 Mutex::Autolock l(mOutputLock);
2673 // TODO: need to track errors for tighter bounds on expected frame number.
2674 if (r.hasInputBuffer) {
2675 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2676 SET_ERR("Shutter notification out-of-order. Expected "
2677 "notification for frame %d, got frame %d",
2678 mNextReprocessShutterFrameNumber, msg.frame_number);
2679 return;
2680 }
2681 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2682 } else {
2683 if (msg.frame_number < mNextShutterFrameNumber) {
2684 SET_ERR("Shutter notification out-of-order. Expected "
2685 "notification for frame %d, got frame %d",
2686 mNextShutterFrameNumber, msg.frame_number);
2687 return;
2688 }
2689 mNextShutterFrameNumber = msg.frame_number + 1;
2690 }
2691 }
2692
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002693 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2694 mId, __FUNCTION__,
2695 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2696 // Call listener, if any
2697 if (listener != NULL) {
2698 listener->notifyShutter(r.resultExtras, msg.timestamp);
2699 }
2700
2701 r.shutterTimestamp = msg.timestamp;
2702
2703 // send pending result and buffers
2704 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002705 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002706 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002707 returnOutputBuffers(r.pendingOutputBuffers.array(),
2708 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2709 r.pendingOutputBuffers.clear();
2710
2711 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002712 }
2713 }
2714 if (idx < 0) {
2715 SET_ERR("Shutter notification for non-existent frame number %d",
2716 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002717 }
2718}
2719
2720
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002721CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002722 ALOGV("%s", __FUNCTION__);
2723
Igor Murashkin1e479c02013-09-06 16:55:14 -07002724 CameraMetadata retVal;
2725
2726 if (mRequestThread != NULL) {
2727 retVal = mRequestThread->getLatestRequest();
2728 }
2729
Igor Murashkin1e479c02013-09-06 16:55:14 -07002730 return retVal;
2731}
2732
Jianing Weicb0652e2014-03-12 18:29:36 -07002733
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002734/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002735 * RequestThread inner class methods
2736 */
2737
2738Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002739 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002740 camera3_device_t *hal3Device,
2741 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002742 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002743 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002744 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002745 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002746 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002747 mReconfigured(false),
2748 mDoPause(false),
2749 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002750 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002751 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002752 mCurrentAfTriggerId(0),
2753 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002754 mRepeatingLastFrameNumber(
2755 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002756 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002757 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002758}
2759
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002760void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002761 NotificationListener *listener) {
2762 Mutex::Autolock l(mRequestLock);
2763 mListener = listener;
2764}
2765
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002766void Camera3Device::RequestThread::configurationComplete() {
2767 Mutex::Autolock l(mRequestLock);
2768 mReconfigured = true;
2769}
2770
Jianing Wei90e59c92014-03-12 18:29:36 -07002771status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002772 List<sp<CaptureRequest> > &requests,
2773 /*out*/
2774 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002775 Mutex::Autolock l(mRequestLock);
2776 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2777 ++it) {
2778 mRequestQueue.push_back(*it);
2779 }
2780
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002781 if (lastFrameNumber != NULL) {
2782 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2783 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2784 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2785 *lastFrameNumber);
2786 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002787
Jianing Wei90e59c92014-03-12 18:29:36 -07002788 unpauseForNewRequests();
2789
2790 return OK;
2791}
2792
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002793
2794status_t Camera3Device::RequestThread::queueTrigger(
2795 RequestTrigger trigger[],
2796 size_t count) {
2797
2798 Mutex::Autolock l(mTriggerMutex);
2799 status_t ret;
2800
2801 for (size_t i = 0; i < count; ++i) {
2802 ret = queueTriggerLocked(trigger[i]);
2803
2804 if (ret != OK) {
2805 return ret;
2806 }
2807 }
2808
2809 return OK;
2810}
2811
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002812int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2813 sp<Camera3Device> d = device.promote();
2814 if (d != NULL) return d->mId;
2815 return 0;
2816}
2817
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002818status_t Camera3Device::RequestThread::queueTriggerLocked(
2819 RequestTrigger trigger) {
2820
2821 uint32_t tag = trigger.metadataTag;
2822 ssize_t index = mTriggerMap.indexOfKey(tag);
2823
2824 switch (trigger.getTagType()) {
2825 case TYPE_BYTE:
2826 // fall-through
2827 case TYPE_INT32:
2828 break;
2829 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002830 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2831 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002832 return INVALID_OPERATION;
2833 }
2834
2835 /**
2836 * Collect only the latest trigger, since we only have 1 field
2837 * in the request settings per trigger tag, and can't send more than 1
2838 * trigger per request.
2839 */
2840 if (index != NAME_NOT_FOUND) {
2841 mTriggerMap.editValueAt(index) = trigger;
2842 } else {
2843 mTriggerMap.add(tag, trigger);
2844 }
2845
2846 return OK;
2847}
2848
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002849status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002850 const RequestList &requests,
2851 /*out*/
2852 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002853 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002854 if (lastFrameNumber != NULL) {
2855 *lastFrameNumber = mRepeatingLastFrameNumber;
2856 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002857 mRepeatingRequests.clear();
2858 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2859 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002860
2861 unpauseForNewRequests();
2862
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002863 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002864 return OK;
2865}
2866
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002867bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2868 if (mRepeatingRequests.empty()) {
2869 return false;
2870 }
2871 int32_t requestId = requestIn->mResultExtras.requestId;
2872 const RequestList &repeatRequests = mRepeatingRequests;
2873 // All repeating requests are guaranteed to have same id so only check first quest
2874 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2875 return (firstRequest->mResultExtras.requestId == requestId);
2876}
2877
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002878status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002879 Mutex::Autolock l(mRequestLock);
2880 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002881 if (lastFrameNumber != NULL) {
2882 *lastFrameNumber = mRepeatingLastFrameNumber;
2883 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002884 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002885 return OK;
2886}
2887
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002888status_t Camera3Device::RequestThread::clear(
2889 NotificationListener *listener,
2890 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002891 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002892 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002893
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002894 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002895
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002896 // Send errors for all requests pending in the request queue, including
2897 // pending repeating requests
2898 if (listener != NULL) {
2899 for (RequestList::iterator it = mRequestQueue.begin();
2900 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002901 // Abort the input buffers for reprocess requests.
2902 if ((*it)->mInputStream != NULL) {
2903 camera3_stream_buffer_t inputBuffer;
2904 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2905 if (res != OK) {
2906 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2907 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2908 } else {
2909 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2910 if (res != OK) {
2911 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2912 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2913 }
2914 }
2915 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002916 // Set the frame number this request would have had, if it
2917 // had been submitted; this frame number will not be reused.
2918 // The requestId and burstId fields were set when the request was
2919 // submitted originally (in convertMetadataListToRequestListLocked)
2920 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002921 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002922 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002923 }
2924 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002925 mRequestQueue.clear();
2926 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002927 if (lastFrameNumber != NULL) {
2928 *lastFrameNumber = mRepeatingLastFrameNumber;
2929 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002930 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002931 return OK;
2932}
2933
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002934status_t Camera3Device::RequestThread::flush() {
2935 ATRACE_CALL();
2936 Mutex::Autolock l(mFlushLock);
2937
2938 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2939 return mHal3Device->ops->flush(mHal3Device);
2940 }
2941
2942 return -ENOTSUP;
2943}
2944
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002945void Camera3Device::RequestThread::setPaused(bool paused) {
2946 Mutex::Autolock l(mPauseLock);
2947 mDoPause = paused;
2948 mDoPauseSignal.signal();
2949}
2950
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002951status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2952 int32_t requestId, nsecs_t timeout) {
2953 Mutex::Autolock l(mLatestRequestMutex);
2954 status_t res;
2955 while (mLatestRequestId != requestId) {
2956 nsecs_t startTime = systemTime();
2957
2958 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2959 if (res != OK) return res;
2960
2961 timeout -= (systemTime() - startTime);
2962 }
2963
2964 return OK;
2965}
2966
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002967void Camera3Device::RequestThread::requestExit() {
2968 // Call parent to set up shutdown
2969 Thread::requestExit();
2970 // The exit from any possible waits
2971 mDoPauseSignal.signal();
2972 mRequestSignal.signal();
2973}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002974
Chien-Yu Chend196d612015-06-22 19:49:01 -07002975
2976/**
2977 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2978 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2979 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2980 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2981 * request.
2982 */
2983void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2984 request->mAeTriggerCancelOverride.applyAeLock = false;
2985 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2986
2987 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2988 return;
2989 }
2990
2991 camera_metadata_entry_t aePrecaptureTrigger =
2992 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2993 if (aePrecaptureTrigger.count > 0 &&
2994 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2995 // Always override CANCEL to IDLE
2996 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2997 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2998 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2999 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
3000 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3001
3002 if (mAeLockAvailable == true) {
3003 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3004 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3005 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3006 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3007 request->mAeTriggerCancelOverride.applyAeLock = true;
3008 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3009 }
3010 }
3011 }
3012}
3013
3014/**
3015 * Override result metadata for cancelling AE precapture trigger applied in
3016 * handleAePrecaptureCancelRequest().
3017 */
3018void Camera3Device::overrideResultForPrecaptureCancel(
3019 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3020 if (aeTriggerCancelOverride.applyAeLock) {
3021 // Only devices <= v3.2 should have this override
3022 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3023 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3024 }
3025
3026 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3027 // Only devices <= v3.2 should have this override
3028 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3029 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3030 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3031 }
3032}
3033
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003034bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003035 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003036 status_t res;
3037
3038 // Handle paused state.
3039 if (waitIfPaused()) {
3040 return true;
3041 }
3042
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003043 // Wait for the next batch of requests.
3044 waitForNextRequestBatch();
3045 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003046 return true;
3047 }
3048
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003049 // Get the latest request ID, if any
3050 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003051 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003052 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003053 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003054 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003055 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003056 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3057 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003058 }
3059
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003060 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003061 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003062 if (res == TIMED_OUT) {
3063 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003064 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003065 return true;
3066 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003067 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003068 return false;
3069 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003070
Zhijun Hecc27e112013-10-03 16:12:43 -07003071 // Inform waitUntilRequestProcessed thread of a new request ID
3072 {
3073 Mutex::Autolock al(mLatestRequestMutex);
3074
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003075 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003076 mLatestRequestSignal.signal();
3077 }
3078
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003079 // Submit a batch of requests to HAL.
3080 // Use flush lock only when submitting multilple requests in a batch.
3081 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3082 // which may take a long time to finish so synchronizing flush() and
3083 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3084 // For now, only synchronize for high speed recording and we should figure something out for
3085 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003086 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003087
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003088 if (useFlushLock) {
3089 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003090 }
3091
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003092 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003093 mNextRequests.size());
3094 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003095 // Submit request and block until ready for next one
3096 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3097 ATRACE_BEGIN("camera3->process_capture_request");
3098 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3099 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003100
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003101 if (res != OK) {
3102 // Should only get a failure here for malformed requests or device-level
3103 // errors, so consider all errors fatal. Bad metadata failures should
3104 // come through notify.
3105 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3106 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3107 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003108 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003109 if (useFlushLock) {
3110 mFlushLock.unlock();
3111 }
3112 return false;
3113 }
3114
3115 // Mark that the request has be submitted successfully.
3116 nextRequest.submitted = true;
3117
3118 // Update the latest request sent to HAL
3119 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3120 Mutex::Autolock al(mLatestRequestMutex);
3121
3122 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3123 mLatestRequest.acquire(cloned);
3124 }
3125
3126 if (nextRequest.halRequest.settings != NULL) {
3127 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3128 }
3129
3130 // Remove any previously queued triggers (after unlock)
3131 res = removeTriggers(mPrevRequest);
3132 if (res != OK) {
3133 SET_ERR("RequestThread: Unable to remove triggers "
3134 "(capture request %d, HAL device: %s (%d)",
3135 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003136 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003137 if (useFlushLock) {
3138 mFlushLock.unlock();
3139 }
3140 return false;
3141 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003142 }
3143
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003144 if (useFlushLock) {
3145 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003146 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003147
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003148 // Unset as current request
3149 {
3150 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003151 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003152 }
3153
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003154 return true;
3155}
3156
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003157status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003158 ATRACE_CALL();
3159
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003160 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003161 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3162 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3163 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3164
3165 // Prepare a request to HAL
3166 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3167
3168 // Insert any queued triggers (before metadata is locked)
3169 status_t res = insertTriggers(captureRequest);
3170
3171 if (res < 0) {
3172 SET_ERR("RequestThread: Unable to insert triggers "
3173 "(capture request %d, HAL device: %s (%d)",
3174 halRequest->frame_number, strerror(-res), res);
3175 return INVALID_OPERATION;
3176 }
3177 int triggerCount = res;
3178 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3179 mPrevTriggers = triggerCount;
3180
3181 // If the request is the same as last, or we had triggers last time
3182 if (mPrevRequest != captureRequest || triggersMixedIn) {
3183 /**
3184 * HAL workaround:
3185 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3186 */
3187 res = addDummyTriggerIds(captureRequest);
3188 if (res != OK) {
3189 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3190 "(capture request %d, HAL device: %s (%d)",
3191 halRequest->frame_number, strerror(-res), res);
3192 return INVALID_OPERATION;
3193 }
3194
3195 /**
3196 * The request should be presorted so accesses in HAL
3197 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3198 */
3199 captureRequest->mSettings.sort();
3200 halRequest->settings = captureRequest->mSettings.getAndLock();
3201 mPrevRequest = captureRequest;
3202 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3203
3204 IF_ALOGV() {
3205 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3206 find_camera_metadata_ro_entry(
3207 halRequest->settings,
3208 ANDROID_CONTROL_AF_TRIGGER,
3209 &e
3210 );
3211 if (e.count > 0) {
3212 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3213 __FUNCTION__,
3214 halRequest->frame_number,
3215 e.data.u8[0]);
3216 }
3217 }
3218 } else {
3219 // leave request.settings NULL to indicate 'reuse latest given'
3220 ALOGVV("%s: Request settings are REUSED",
3221 __FUNCTION__);
3222 }
3223
3224 uint32_t totalNumBuffers = 0;
3225
3226 // Fill in buffers
3227 if (captureRequest->mInputStream != NULL) {
3228 halRequest->input_buffer = &captureRequest->mInputBuffer;
3229 totalNumBuffers += 1;
3230 } else {
3231 halRequest->input_buffer = NULL;
3232 }
3233
3234 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3235 captureRequest->mOutputStreams.size());
3236 halRequest->output_buffers = outputBuffers->array();
3237 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3238 res = captureRequest->mOutputStreams.editItemAt(i)->
3239 getBuffer(&outputBuffers->editItemAt(i));
3240 if (res != OK) {
3241 // Can't get output buffer from gralloc queue - this could be due to
3242 // abandoned queue or other consumer misbehavior, so not a fatal
3243 // error
3244 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3245 " %s (%d)", strerror(-res), res);
3246
3247 return TIMED_OUT;
3248 }
3249 halRequest->num_output_buffers++;
3250 }
3251 totalNumBuffers += halRequest->num_output_buffers;
3252
3253 // Log request in the in-flight queue
3254 sp<Camera3Device> parent = mParent.promote();
3255 if (parent == NULL) {
3256 // Should not happen, and nowhere to send errors to, so just log it
3257 CLOGE("RequestThread: Parent is gone");
3258 return INVALID_OPERATION;
3259 }
3260 res = parent->registerInFlight(halRequest->frame_number,
3261 totalNumBuffers, captureRequest->mResultExtras,
3262 /*hasInput*/halRequest->input_buffer != NULL,
3263 captureRequest->mAeTriggerCancelOverride);
3264 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3265 ", burstId = %" PRId32 ".",
3266 __FUNCTION__,
3267 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3268 captureRequest->mResultExtras.burstId);
3269 if (res != OK) {
3270 SET_ERR("RequestThread: Unable to register new in-flight request:"
3271 " %s (%d)", strerror(-res), res);
3272 return INVALID_OPERATION;
3273 }
3274 }
3275
3276 return OK;
3277}
3278
Igor Murashkin1e479c02013-09-06 16:55:14 -07003279CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3280 Mutex::Autolock al(mLatestRequestMutex);
3281
3282 ALOGV("RequestThread::%s", __FUNCTION__);
3283
3284 return mLatestRequest;
3285}
3286
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003287bool Camera3Device::RequestThread::isStreamPending(
3288 sp<Camera3StreamInterface>& stream) {
3289 Mutex::Autolock l(mRequestLock);
3290
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003291 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003292 if (!nextRequest.submitted) {
3293 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3294 if (stream == s) return true;
3295 }
3296 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003297 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003298 }
3299
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003300 for (const auto& request : mRequestQueue) {
3301 for (const auto& s : request->mOutputStreams) {
3302 if (stream == s) return true;
3303 }
3304 if (stream == request->mInputStream) return true;
3305 }
3306
3307 for (const auto& request : mRepeatingRequests) {
3308 for (const auto& s : request->mOutputStreams) {
3309 if (stream == s) return true;
3310 }
3311 if (stream == request->mInputStream) return true;
3312 }
3313
3314 return false;
3315}
Jianing Weicb0652e2014-03-12 18:29:36 -07003316
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003317void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3318 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003319 return;
3320 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003321
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003322 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003323 // Skip the ones that have been submitted successfully.
3324 if (nextRequest.submitted) {
3325 continue;
3326 }
3327
3328 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3329 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3330 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3331
3332 if (halRequest->settings != NULL) {
3333 captureRequest->mSettings.unlock(halRequest->settings);
3334 }
3335
3336 if (captureRequest->mInputStream != NULL) {
3337 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3338 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3339 }
3340
3341 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3342 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3343 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3344 }
3345
3346 if (sendRequestError) {
3347 Mutex::Autolock l(mRequestLock);
3348 if (mListener != NULL) {
3349 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003350 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003351 captureRequest->mResultExtras);
3352 }
3353 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003354 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003355
3356 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003357 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003358}
3359
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003360void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003361 // Optimized a bit for the simple steady-state case (single repeating
3362 // request), to avoid putting that request in the queue temporarily.
3363 Mutex::Autolock l(mRequestLock);
3364
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003365 assert(mNextRequests.empty());
3366
3367 NextRequest nextRequest;
3368 nextRequest.captureRequest = waitForNextRequestLocked();
3369 if (nextRequest.captureRequest == nullptr) {
3370 return;
3371 }
3372
3373 nextRequest.halRequest = camera3_capture_request_t();
3374 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003375 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003376
3377 // Wait for additional requests
3378 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3379
3380 for (size_t i = 1; i < batchSize; i++) {
3381 NextRequest additionalRequest;
3382 additionalRequest.captureRequest = waitForNextRequestLocked();
3383 if (additionalRequest.captureRequest == nullptr) {
3384 break;
3385 }
3386
3387 additionalRequest.halRequest = camera3_capture_request_t();
3388 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003389 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003390 }
3391
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003392 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003393 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003394 mNextRequests.size(), batchSize);
3395 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003396 }
3397
3398 return;
3399}
3400
3401sp<Camera3Device::CaptureRequest>
3402 Camera3Device::RequestThread::waitForNextRequestLocked() {
3403 status_t res;
3404 sp<CaptureRequest> nextRequest;
3405
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003406 while (mRequestQueue.empty()) {
3407 if (!mRepeatingRequests.empty()) {
3408 // Always atomically enqueue all requests in a repeating request
3409 // list. Guarantees a complete in-sequence set of captures to
3410 // application.
3411 const RequestList &requests = mRepeatingRequests;
3412 RequestList::const_iterator firstRequest =
3413 requests.begin();
3414 nextRequest = *firstRequest;
3415 mRequestQueue.insert(mRequestQueue.end(),
3416 ++firstRequest,
3417 requests.end());
3418 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003419
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003420 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003421
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003422 break;
3423 }
3424
3425 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3426
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003427 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3428 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003429 Mutex::Autolock pl(mPauseLock);
3430 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003431 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003432 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003433 // Let the tracker know
3434 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3435 if (statusTracker != 0) {
3436 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3437 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003438 }
3439 // Stop waiting for now and let thread management happen
3440 return NULL;
3441 }
3442 }
3443
3444 if (nextRequest == NULL) {
3445 // Don't have a repeating request already in hand, so queue
3446 // must have an entry now.
3447 RequestList::iterator firstRequest =
3448 mRequestQueue.begin();
3449 nextRequest = *firstRequest;
3450 mRequestQueue.erase(firstRequest);
3451 }
3452
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003453 // In case we've been unpaused by setPaused clearing mDoPause, need to
3454 // update internal pause state (capture/setRepeatingRequest unpause
3455 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003456 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003457 if (mPaused) {
3458 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3459 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3460 if (statusTracker != 0) {
3461 statusTracker->markComponentActive(mStatusId);
3462 }
3463 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003464 mPaused = false;
3465
3466 // Check if we've reconfigured since last time, and reset the preview
3467 // request if so. Can't use 'NULL request == repeat' across configure calls.
3468 if (mReconfigured) {
3469 mPrevRequest.clear();
3470 mReconfigured = false;
3471 }
3472
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003473 if (nextRequest != NULL) {
3474 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003475 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3476 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003477
3478 // Since RequestThread::clear() removes buffers from the input stream,
3479 // get the right buffer here before unlocking mRequestLock
3480 if (nextRequest->mInputStream != NULL) {
3481 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3482 if (res != OK) {
3483 // Can't get input buffer from gralloc queue - this could be due to
3484 // disconnected queue or other producer misbehavior, so not a fatal
3485 // error
3486 ALOGE("%s: Can't get input buffer, skipping request:"
3487 " %s (%d)", __FUNCTION__, strerror(-res), res);
3488 if (mListener != NULL) {
3489 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003490 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003491 nextRequest->mResultExtras);
3492 }
3493 return NULL;
3494 }
3495 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003496 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003497
3498 handleAePrecaptureCancelRequest(nextRequest);
3499
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003500 return nextRequest;
3501}
3502
3503bool Camera3Device::RequestThread::waitIfPaused() {
3504 status_t res;
3505 Mutex::Autolock l(mPauseLock);
3506 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003507 if (mPaused == false) {
3508 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003509 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3510 // Let the tracker know
3511 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3512 if (statusTracker != 0) {
3513 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3514 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003515 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003516
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003517 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003518 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003519 return true;
3520 }
3521 }
3522 // We don't set mPaused to false here, because waitForNextRequest needs
3523 // to further manage the paused state in case of starvation.
3524 return false;
3525}
3526
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003527void Camera3Device::RequestThread::unpauseForNewRequests() {
3528 // With work to do, mark thread as unpaused.
3529 // If paused by request (setPaused), don't resume, to avoid
3530 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003531 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003532 Mutex::Autolock p(mPauseLock);
3533 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003534 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3535 if (mPaused) {
3536 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3537 if (statusTracker != 0) {
3538 statusTracker->markComponentActive(mStatusId);
3539 }
3540 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003541 mPaused = false;
3542 }
3543}
3544
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003545void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3546 sp<Camera3Device> parent = mParent.promote();
3547 if (parent != NULL) {
3548 va_list args;
3549 va_start(args, fmt);
3550
3551 parent->setErrorStateV(fmt, args);
3552
3553 va_end(args);
3554 }
3555}
3556
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003557status_t Camera3Device::RequestThread::insertTriggers(
3558 const sp<CaptureRequest> &request) {
3559
3560 Mutex::Autolock al(mTriggerMutex);
3561
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003562 sp<Camera3Device> parent = mParent.promote();
3563 if (parent == NULL) {
3564 CLOGE("RequestThread: Parent is gone");
3565 return DEAD_OBJECT;
3566 }
3567
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003568 CameraMetadata &metadata = request->mSettings;
3569 size_t count = mTriggerMap.size();
3570
3571 for (size_t i = 0; i < count; ++i) {
3572 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003573 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003574
3575 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3576 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3577 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003578 if (isAeTrigger) {
3579 request->mResultExtras.precaptureTriggerId = triggerId;
3580 mCurrentPreCaptureTriggerId = triggerId;
3581 } else {
3582 request->mResultExtras.afTriggerId = triggerId;
3583 mCurrentAfTriggerId = triggerId;
3584 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003585 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3586 continue; // Trigger ID tag is deprecated since device HAL 3.2
3587 }
3588 }
3589
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003590 camera_metadata_entry entry = metadata.find(tag);
3591
3592 if (entry.count > 0) {
3593 /**
3594 * Already has an entry for this trigger in the request.
3595 * Rewrite it with our requested trigger value.
3596 */
3597 RequestTrigger oldTrigger = trigger;
3598
3599 oldTrigger.entryValue = entry.data.u8[0];
3600
3601 mTriggerReplacedMap.add(tag, oldTrigger);
3602 } else {
3603 /**
3604 * More typical, no trigger entry, so we just add it
3605 */
3606 mTriggerRemovedMap.add(tag, trigger);
3607 }
3608
3609 status_t res;
3610
3611 switch (trigger.getTagType()) {
3612 case TYPE_BYTE: {
3613 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3614 res = metadata.update(tag,
3615 &entryValue,
3616 /*count*/1);
3617 break;
3618 }
3619 case TYPE_INT32:
3620 res = metadata.update(tag,
3621 &trigger.entryValue,
3622 /*count*/1);
3623 break;
3624 default:
3625 ALOGE("%s: Type not supported: 0x%x",
3626 __FUNCTION__,
3627 trigger.getTagType());
3628 return INVALID_OPERATION;
3629 }
3630
3631 if (res != OK) {
3632 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3633 ", value %d", __FUNCTION__, trigger.getTagName(),
3634 trigger.entryValue);
3635 return res;
3636 }
3637
3638 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3639 trigger.getTagName(),
3640 trigger.entryValue);
3641 }
3642
3643 mTriggerMap.clear();
3644
3645 return count;
3646}
3647
3648status_t Camera3Device::RequestThread::removeTriggers(
3649 const sp<CaptureRequest> &request) {
3650 Mutex::Autolock al(mTriggerMutex);
3651
3652 CameraMetadata &metadata = request->mSettings;
3653
3654 /**
3655 * Replace all old entries with their old values.
3656 */
3657 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3658 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3659
3660 status_t res;
3661
3662 uint32_t tag = trigger.metadataTag;
3663 switch (trigger.getTagType()) {
3664 case TYPE_BYTE: {
3665 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3666 res = metadata.update(tag,
3667 &entryValue,
3668 /*count*/1);
3669 break;
3670 }
3671 case TYPE_INT32:
3672 res = metadata.update(tag,
3673 &trigger.entryValue,
3674 /*count*/1);
3675 break;
3676 default:
3677 ALOGE("%s: Type not supported: 0x%x",
3678 __FUNCTION__,
3679 trigger.getTagType());
3680 return INVALID_OPERATION;
3681 }
3682
3683 if (res != OK) {
3684 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3685 ", trigger value %d", __FUNCTION__,
3686 trigger.getTagName(), trigger.entryValue);
3687 return res;
3688 }
3689 }
3690 mTriggerReplacedMap.clear();
3691
3692 /**
3693 * Remove all new entries.
3694 */
3695 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3696 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3697 status_t res = metadata.erase(trigger.metadataTag);
3698
3699 if (res != OK) {
3700 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3701 ", trigger value %d", __FUNCTION__,
3702 trigger.getTagName(), trigger.entryValue);
3703 return res;
3704 }
3705 }
3706 mTriggerRemovedMap.clear();
3707
3708 return OK;
3709}
3710
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003711status_t Camera3Device::RequestThread::addDummyTriggerIds(
3712 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003713 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003714 static const int32_t dummyTriggerId = 1;
3715 status_t res;
3716
3717 CameraMetadata &metadata = request->mSettings;
3718
3719 // If AF trigger is active, insert a dummy AF trigger ID if none already
3720 // exists
3721 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3722 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3723 if (afTrigger.count > 0 &&
3724 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3725 afId.count == 0) {
3726 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3727 if (res != OK) return res;
3728 }
3729
3730 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3731 // if none already exists
3732 camera_metadata_entry pcTrigger =
3733 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3734 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3735 if (pcTrigger.count > 0 &&
3736 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3737 pcId.count == 0) {
3738 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3739 &dummyTriggerId, 1);
3740 if (res != OK) return res;
3741 }
3742
3743 return OK;
3744}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003745
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003746/**
3747 * PreparerThread inner class methods
3748 */
3749
3750Camera3Device::PreparerThread::PreparerThread() :
3751 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3752}
3753
3754Camera3Device::PreparerThread::~PreparerThread() {
3755 Thread::requestExitAndWait();
3756 if (mCurrentStream != nullptr) {
3757 mCurrentStream->cancelPrepare();
3758 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3759 mCurrentStream.clear();
3760 }
3761 clear();
3762}
3763
Ruben Brunkc78ac262015-08-13 17:58:46 -07003764status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003765 status_t res;
3766
3767 Mutex::Autolock l(mLock);
3768
Ruben Brunkc78ac262015-08-13 17:58:46 -07003769 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003770 if (res == OK) {
3771 // No preparation needed, fire listener right off
3772 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3773 if (mListener) {
3774 mListener->notifyPrepared(stream->getId());
3775 }
3776 return OK;
3777 } else if (res != NOT_ENOUGH_DATA) {
3778 return res;
3779 }
3780
3781 // Need to prepare, start up thread if necessary
3782 if (!mActive) {
3783 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3784 // isn't running
3785 Thread::requestExitAndWait();
3786 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3787 if (res != OK) {
3788 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3789 if (mListener) {
3790 mListener->notifyPrepared(stream->getId());
3791 }
3792 return res;
3793 }
3794 mCancelNow = false;
3795 mActive = true;
3796 ALOGV("%s: Preparer stream started", __FUNCTION__);
3797 }
3798
3799 // queue up the work
3800 mPendingStreams.push_back(stream);
3801 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3802
3803 return OK;
3804}
3805
3806status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003807 Mutex::Autolock l(mLock);
3808
3809 for (const auto& stream : mPendingStreams) {
3810 stream->cancelPrepare();
3811 }
3812 mPendingStreams.clear();
3813 mCancelNow = true;
3814
3815 return OK;
3816}
3817
3818void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3819 Mutex::Autolock l(mLock);
3820 mListener = listener;
3821}
3822
3823bool Camera3Device::PreparerThread::threadLoop() {
3824 status_t res;
3825 {
3826 Mutex::Autolock l(mLock);
3827 if (mCurrentStream == nullptr) {
3828 // End thread if done with work
3829 if (mPendingStreams.empty()) {
3830 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3831 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3832 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3833 mActive = false;
3834 return false;
3835 }
3836
3837 // Get next stream to prepare
3838 auto it = mPendingStreams.begin();
3839 mCurrentStream = *it;
3840 mPendingStreams.erase(it);
3841 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3842 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3843 } else if (mCancelNow) {
3844 mCurrentStream->cancelPrepare();
3845 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3846 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3847 mCurrentStream.clear();
3848 mCancelNow = false;
3849 return true;
3850 }
3851 }
3852
3853 res = mCurrentStream->prepareNextBuffer();
3854 if (res == NOT_ENOUGH_DATA) return true;
3855 if (res != OK) {
3856 // Something bad happened; try to recover by cancelling prepare and
3857 // signalling listener anyway
3858 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3859 mCurrentStream->getId(), res, strerror(-res));
3860 mCurrentStream->cancelPrepare();
3861 }
3862
3863 // This stream has finished, notify listener
3864 Mutex::Autolock l(mLock);
3865 if (mListener) {
3866 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3867 mCurrentStream->getId());
3868 mListener->notifyPrepared(mCurrentStream->getId());
3869 }
3870
3871 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3872 mCurrentStream.clear();
3873
3874 return true;
3875}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003876
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003877/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003878 * Static callback forwarding methods from HAL to instance
3879 */
3880
3881void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3882 const camera3_capture_result *result) {
3883 Camera3Device *d =
3884 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003885
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003886 d->processCaptureResult(result);
3887}
3888
3889void Camera3Device::sNotify(const camera3_callback_ops *cb,
3890 const camera3_notify_msg *msg) {
3891 Camera3Device *d =
3892 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3893 d->notify(msg);
3894}
3895
3896}; // namespace android