blob: ee84ff06c45f81b1d8e653130f3ae1be3b8980c5 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046#include <android/hardware/camera2/ICameraDeviceUser.h>
47
Igor Murashkinff3e31d2013-10-23 16:40:06 -070048#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070049#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070050#include "device3/Camera3Device.h"
51#include "device3/Camera3OutputStream.h"
52#include "device3/Camera3InputStream.h"
53#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070054#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070055#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080056
57using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058
59namespace android {
60
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061Camera3Device::Camera3Device(int id):
62 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070063 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080064 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070065 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070066 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070067 mUsePartialResult(false),
68 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080069 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070070 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070071 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070072 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070073 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070074 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080075{
76 ATRACE_CALL();
77 camera3_callback_ops::notify = &sNotify;
78 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
79 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
80}
81
82Camera3Device::~Camera3Device()
83{
84 ATRACE_CALL();
85 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
86 disconnect();
87}
88
Igor Murashkin71381052013-03-04 14:53:08 -080089int Camera3Device::getId() const {
90 return mId;
91}
92
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080093/**
94 * CameraDeviceBase interface
95 */
96
Yin-Chia Yehe074a932015-01-30 10:29:02 -080097status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080098{
99 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700100 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800101 Mutex::Autolock l(mLock);
102
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800104 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700105 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800106 return INVALID_OPERATION;
107 }
108
109 /** Open HAL device */
110
111 status_t res;
112 String8 deviceName = String8::format("%d", mId);
113
114 camera3_device_t *device;
115
Zhijun He213ce792013-11-19 08:45:15 -0800116 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800117 res = module->open(deviceName.string(),
118 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800119 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800120
121 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800123 return res;
124 }
125
126 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700127 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700128 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700129 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700130 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800131 device->common.version);
132 device->common.close(&device->common);
133 return BAD_VALUE;
134 }
135
136 camera_info info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800137 res = module->getCameraInfo(mId, &info);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 if (res != OK) return res;
139
140 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700141 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
142 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700143 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800144 device->common.close(&device->common);
145 return BAD_VALUE;
146 }
147
148 /** Initialize device with callback functions */
149
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700150 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700152 ATRACE_END();
153
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800154 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700155 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
156 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800157 device->common.close(&device->common);
158 return BAD_VALUE;
159 }
160
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700161 /** Start up status tracker thread */
162 mStatusTracker = new StatusTracker(this);
163 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
164 if (res != OK) {
165 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
166 strerror(-res), res);
167 device->common.close(&device->common);
168 mStatusTracker.clear();
169 return res;
170 }
171
Zhijun He125684a2015-12-26 15:07:30 -0800172 /** Create buffer manager */
173 mBufferManager = new Camera3BufferManager();
174
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700175 bool aeLockAvailable = false;
176 camera_metadata_ro_entry aeLockAvailableEntry;
177 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
178 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
179 if (res == OK && aeLockAvailableEntry.count > 0) {
180 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
181 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
182 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800183
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700184 /** Start up request queue thread */
185 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800186 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700188 SET_ERR_L("Unable to start request queue thread: %s (%d)",
189 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800191 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 return res;
193 }
194
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700195 mPreparerThread = new PreparerThread();
196
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800197 /** Everything is good to go */
198
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700199 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800200 mDeviceInfo = info.static_camera_characteristics;
201 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700202
203 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800204 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700205 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700206 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700207 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800208
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800209 // Measure the clock domain offset between camera and video/hw_composer
210 camera_metadata_entry timestampSource =
211 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
212 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
213 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
214 mTimestampOffset = getMonoToBoottimeOffset();
215 }
216
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700217 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700218 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
219 camera_metadata_entry partialResultsCount =
220 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
221 if (partialResultsCount.count > 0) {
222 mNumPartialResults = partialResultsCount.data.i32[0];
223 mUsePartialResult = (mNumPartialResults > 1);
224 }
225 } else {
226 camera_metadata_entry partialResultsQuirk =
227 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
228 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
229 mUsePartialResult = true;
230 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700231 }
232
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700233 camera_metadata_entry configs =
234 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
235 for (uint32_t i = 0; i < configs.count; i += 4) {
236 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
237 configs.data.i32[i + 3] ==
238 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
239 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
240 configs.data.i32[i + 2]));
241 }
242 }
243
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800244 return OK;
245}
246
247status_t Camera3Device::disconnect() {
248 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700249 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800250
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800251 ALOGV("%s: E", __FUNCTION__);
252
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700253 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800254
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700255 {
256 Mutex::Autolock l(mLock);
257 if (mStatus == STATUS_UNINITIALIZED) return res;
258
259 if (mStatus == STATUS_ACTIVE ||
260 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
261 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700262 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700263 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700264 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700265 } else {
266 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
267 if (res != OK) {
268 SET_ERR_L("Timeout waiting for HAL to drain");
269 // Continue to close device even in case of error
270 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700271 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800272 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800273
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700274 if (mStatus == STATUS_ERROR) {
275 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700276 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277
278 if (mStatusTracker != NULL) {
279 mStatusTracker->requestExit();
280 }
281
282 if (mRequestThread != NULL) {
283 mRequestThread->requestExit();
284 }
285
286 mOutputStreams.clear();
287 mInputStream.clear();
288 }
289
290 // Joining done without holding mLock, otherwise deadlocks may ensue
291 // as the threads try to access parent state
292 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
293 // HAL may be in a bad state, so waiting for request thread
294 // (which may be stuck in the HAL processCaptureRequest call)
295 // could be dangerous.
296 mRequestThread->join();
297 }
298
299 if (mStatusTracker != NULL) {
300 mStatusTracker->join();
301 }
302
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700303 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700304 {
305 Mutex::Autolock l(mLock);
306
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800307 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700308 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800309 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800310
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700311 hal3Device = mHal3Device;
312 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800313
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700314 // Call close without internal mutex held, as the HAL close may need to
315 // wait on assorted callbacks,etc, to complete before it can return.
316 if (hal3Device != NULL) {
317 ATRACE_BEGIN("camera3->close");
318 hal3Device->common.close(&hal3Device->common);
319 ATRACE_END();
320 }
321
322 {
323 Mutex::Autolock l(mLock);
324 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700325 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700326 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800327
328 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700329 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800330}
331
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700332// For dumping/debugging only -
333// try to acquire a lock a few times, eventually give up to proceed with
334// debug/dump operations
335bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
336 bool gotLock = false;
337 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
338 if (lock.tryLock() == NO_ERROR) {
339 gotLock = true;
340 break;
341 } else {
342 usleep(kDumpSleepDuration);
343 }
344 }
345 return gotLock;
346}
347
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700348Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
349 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
350 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
351 const int STREAM_CONFIGURATION_SIZE = 4;
352 const int STREAM_FORMAT_OFFSET = 0;
353 const int STREAM_WIDTH_OFFSET = 1;
354 const int STREAM_HEIGHT_OFFSET = 2;
355 const int STREAM_IS_INPUT_OFFSET = 3;
356 camera_metadata_ro_entry_t availableStreamConfigs =
357 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
358 if (availableStreamConfigs.count == 0 ||
359 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
360 return Size(0, 0);
361 }
362
363 // Get max jpeg size (area-wise).
364 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
365 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
366 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
367 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
368 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
369 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
370 && format == HAL_PIXEL_FORMAT_BLOB &&
371 (width * height > maxJpegWidth * maxJpegHeight)) {
372 maxJpegWidth = width;
373 maxJpegHeight = height;
374 }
375 }
376 } else {
377 camera_metadata_ro_entry availableJpegSizes =
378 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
379 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
380 return Size(0, 0);
381 }
382
383 // Get max jpeg size (area-wise).
384 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
385 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
386 > (maxJpegWidth * maxJpegHeight)) {
387 maxJpegWidth = availableJpegSizes.data.i32[i];
388 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
389 }
390 }
391 }
392 return Size(maxJpegWidth, maxJpegHeight);
393}
394
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800395nsecs_t Camera3Device::getMonoToBoottimeOffset() {
396 // try three times to get the clock offset, choose the one
397 // with the minimum gap in measurements.
398 const int tries = 3;
399 nsecs_t bestGap, measured;
400 for (int i = 0; i < tries; ++i) {
401 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
402 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
403 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
404 const nsecs_t gap = tmono2 - tmono;
405 if (i == 0 || gap < bestGap) {
406 bestGap = gap;
407 measured = tbase - ((tmono + tmono2) >> 1);
408 }
409 }
410 return measured;
411}
412
Zhijun Hef7da0962014-04-24 13:27:56 -0700413ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700414 // Get max jpeg size (area-wise).
415 Size maxJpegResolution = getMaxJpegResolution();
416 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700417 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700418 __FUNCTION__, mId);
419 return BAD_VALUE;
420 }
421
Zhijun Hef7da0962014-04-24 13:27:56 -0700422 // Get max jpeg buffer size
423 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700424 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
425 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700426 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
427 return BAD_VALUE;
428 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700429 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800430 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700431
432 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700433 float scaleFactor = ((float) (width * height)) /
434 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800435 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
436 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700437 if (jpegBufferSize > maxJpegBufferSize) {
438 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700439 }
440
441 return jpegBufferSize;
442}
443
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700444ssize_t Camera3Device::getPointCloudBufferSize() const {
445 const int FLOATS_PER_POINT=4;
446 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
447 if (maxPointCount.count == 0) {
448 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
449 __FUNCTION__, mId);
450 return BAD_VALUE;
451 }
452 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
453 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
454 return maxBytesForPointCloud;
455}
456
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800457ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800458 const int PER_CONFIGURATION_SIZE = 3;
459 const int WIDTH_OFFSET = 0;
460 const int HEIGHT_OFFSET = 1;
461 const int SIZE_OFFSET = 2;
462 camera_metadata_ro_entry rawOpaqueSizes =
463 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800464 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800465 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800466 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800467 __FUNCTION__, mId, count);
468 return BAD_VALUE;
469 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700470
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800471 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
472 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
473 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
474 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
475 }
476 }
477
478 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
479 __FUNCTION__, mId, width, height);
480 return BAD_VALUE;
481}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700482
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800483status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
484 ATRACE_CALL();
485 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700486
487 // Try to lock, but continue in case of failure (to avoid blocking in
488 // deadlocks)
489 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
490 bool gotLock = tryLockSpinRightRound(mLock);
491
492 ALOGW_IF(!gotInterfaceLock,
493 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
494 mId, __FUNCTION__);
495 ALOGW_IF(!gotLock,
496 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
497 mId, __FUNCTION__);
498
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800499 bool dumpTemplates = false;
500 String16 templatesOption("-t");
501 int n = args.size();
502 for (int i = 0; i < n; i++) {
503 if (args[i] == templatesOption) {
504 dumpTemplates = true;
505 }
506 }
507
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800508 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800509
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800510 const char *status =
511 mStatus == STATUS_ERROR ? "ERROR" :
512 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700513 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
514 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800515 mStatus == STATUS_ACTIVE ? "ACTIVE" :
516 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700517
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800518 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700519 if (mStatus == STATUS_ERROR) {
520 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
521 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800522 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700523 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700524 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800525
526 if (mInputStream != NULL) {
527 write(fd, lines.string(), lines.size());
528 mInputStream->dump(fd, args);
529 } else {
530 lines.appendFormat(" No input stream.\n");
531 write(fd, lines.string(), lines.size());
532 }
533 for (size_t i = 0; i < mOutputStreams.size(); i++) {
534 mOutputStreams[i]->dump(fd,args);
535 }
536
Zhijun He125684a2015-12-26 15:07:30 -0800537 lines = String8(" Camera3 Buffer Manager:\n");
538 write(fd, lines.string(), lines.size());
539 mBufferManager->dump(fd, args);
540
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700541 lines = String8(" In-flight requests:\n");
542 if (mInFlightMap.size() == 0) {
543 lines.append(" None\n");
544 } else {
545 for (size_t i = 0; i < mInFlightMap.size(); i++) {
546 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700547 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700548 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800549 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700550 r.numBuffersLeft);
551 }
552 }
553 write(fd, lines.string(), lines.size());
554
Igor Murashkin1e479c02013-09-06 16:55:14 -0700555 {
556 lines = String8(" Last request sent:\n");
557 write(fd, lines.string(), lines.size());
558
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700559 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700560 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
561 }
562
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800563 if (dumpTemplates) {
564 const char *templateNames[] = {
565 "TEMPLATE_PREVIEW",
566 "TEMPLATE_STILL_CAPTURE",
567 "TEMPLATE_VIDEO_RECORD",
568 "TEMPLATE_VIDEO_SNAPSHOT",
569 "TEMPLATE_ZERO_SHUTTER_LAG",
570 "TEMPLATE_MANUAL"
571 };
572
573 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
574 const camera_metadata_t *templateRequest;
575 templateRequest =
576 mHal3Device->ops->construct_default_request_settings(
577 mHal3Device, i);
578 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
579 if (templateRequest == NULL) {
580 lines.append(" Not supported\n");
581 write(fd, lines.string(), lines.size());
582 } else {
583 write(fd, lines.string(), lines.size());
584 dump_indented_camera_metadata(templateRequest,
585 fd, /*verbosity*/2, /*indentation*/8);
586 }
587 }
588 }
589
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800590 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700591 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800592 write(fd, lines.string(), lines.size());
593 mHal3Device->ops->dump(mHal3Device, fd);
594 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800595
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700596 if (gotLock) mLock.unlock();
597 if (gotInterfaceLock) mInterfaceLock.unlock();
598
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800599 return OK;
600}
601
602const CameraMetadata& Camera3Device::info() const {
603 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800604 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
605 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700606 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800607 mStatus == STATUS_ERROR ?
608 "when in error state" : "before init");
609 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800610 return mDeviceInfo;
611}
612
Jianing Wei90e59c92014-03-12 18:29:36 -0700613status_t Camera3Device::checkStatusOkToCaptureLocked() {
614 switch (mStatus) {
615 case STATUS_ERROR:
616 CLOGE("Device has encountered a serious error");
617 return INVALID_OPERATION;
618 case STATUS_UNINITIALIZED:
619 CLOGE("Device not initialized");
620 return INVALID_OPERATION;
621 case STATUS_UNCONFIGURED:
622 case STATUS_CONFIGURED:
623 case STATUS_ACTIVE:
624 // OK
625 break;
626 default:
627 SET_ERR_L("Unexpected status: %d", mStatus);
628 return INVALID_OPERATION;
629 }
630 return OK;
631}
632
633status_t Camera3Device::convertMetadataListToRequestListLocked(
634 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
635 if (requestList == NULL) {
636 CLOGE("requestList cannot be NULL.");
637 return BAD_VALUE;
638 }
639
Jianing Weicb0652e2014-03-12 18:29:36 -0700640 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700641 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
642 it != metadataList.end(); ++it) {
643 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
644 if (newRequest == 0) {
645 CLOGE("Can't create capture request");
646 return BAD_VALUE;
647 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700648
649 // Setup burst Id and request Id
650 newRequest->mResultExtras.burstId = burstId++;
651 if (it->exists(ANDROID_REQUEST_ID)) {
652 if (it->find(ANDROID_REQUEST_ID).count == 0) {
653 CLOGE("RequestID entry exists; but must not be empty in metadata");
654 return BAD_VALUE;
655 }
656 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
657 } else {
658 CLOGE("RequestID does not exist in metadata");
659 return BAD_VALUE;
660 }
661
Jianing Wei90e59c92014-03-12 18:29:36 -0700662 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700663
664 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700665 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700666
667 // Setup batch size if this is a high speed video recording request.
668 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
669 auto firstRequest = requestList->begin();
670 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
671 if (outputStream->isVideoStream()) {
672 (*firstRequest)->mBatchSize = requestList->size();
673 break;
674 }
675 }
676 }
677
Jianing Wei90e59c92014-03-12 18:29:36 -0700678 return OK;
679}
680
Jianing Weicb0652e2014-03-12 18:29:36 -0700681status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800682 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800683
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700684 List<const CameraMetadata> requests;
685 requests.push_back(request);
686 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800687}
688
Jianing Wei90e59c92014-03-12 18:29:36 -0700689status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700690 const List<const CameraMetadata> &requests, bool repeating,
691 /*out*/
692 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700693 ATRACE_CALL();
694 Mutex::Autolock il(mInterfaceLock);
695 Mutex::Autolock l(mLock);
696
697 status_t res = checkStatusOkToCaptureLocked();
698 if (res != OK) {
699 // error logged by previous call
700 return res;
701 }
702
703 RequestList requestList;
704
705 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
706 if (res != OK) {
707 // error logged by previous call
708 return res;
709 }
710
711 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700712 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700713 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700714 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700715 }
716
717 if (res == OK) {
718 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
719 if (res != OK) {
720 SET_ERR_L("Can't transition to active in %f seconds!",
721 kActiveTimeout/1e9);
722 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700723 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
724 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700725 } else {
726 CLOGE("Cannot queue request. Impossible.");
727 return BAD_VALUE;
728 }
729
730 return res;
731}
732
Jianing Weicb0652e2014-03-12 18:29:36 -0700733status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
734 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700735 ATRACE_CALL();
736
Jianing Weicb0652e2014-03-12 18:29:36 -0700737 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700738}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800739
Jianing Weicb0652e2014-03-12 18:29:36 -0700740status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
741 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800742 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800743
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700744 List<const CameraMetadata> requests;
745 requests.push_back(request);
746 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800747}
748
Jianing Weicb0652e2014-03-12 18:29:36 -0700749status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
750 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700751 ATRACE_CALL();
752
Jianing Weicb0652e2014-03-12 18:29:36 -0700753 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700754}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800755
756sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
757 const CameraMetadata &request) {
758 status_t res;
759
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700760 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800761 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700762 // Stream configuration failed due to unsupported configuration.
763 // Device back to unconfigured state. Client might try other configuraitons
764 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
765 CLOGE("No streams configured");
766 return NULL;
767 }
768 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800769 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700770 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800771 return NULL;
772 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700773 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700774 if (mStatus == STATUS_UNCONFIGURED) {
775 CLOGE("No streams configured");
776 return NULL;
777 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800778 }
779
780 sp<CaptureRequest> newRequest = createCaptureRequest(request);
781 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800782}
783
Jianing Weicb0652e2014-03-12 18:29:36 -0700784status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800785 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700786 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800787 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800788
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800789 switch (mStatus) {
790 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700791 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800792 return INVALID_OPERATION;
793 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700794 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800795 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700796 case STATUS_UNCONFIGURED:
797 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800798 case STATUS_ACTIVE:
799 // OK
800 break;
801 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700802 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800803 return INVALID_OPERATION;
804 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700805 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700806
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700807 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800808}
809
810status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
811 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700812 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800813
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700814 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800815}
816
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700817status_t Camera3Device::createInputStream(
818 uint32_t width, uint32_t height, int format, int *id) {
819 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700820 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700821 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700822 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
823 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700824
825 status_t res;
826 bool wasActive = false;
827
828 switch (mStatus) {
829 case STATUS_ERROR:
830 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
831 return INVALID_OPERATION;
832 case STATUS_UNINITIALIZED:
833 ALOGE("%s: Device not initialized", __FUNCTION__);
834 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700835 case STATUS_UNCONFIGURED:
836 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700837 // OK
838 break;
839 case STATUS_ACTIVE:
840 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700841 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700842 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700843 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700844 return res;
845 }
846 wasActive = true;
847 break;
848 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700849 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700850 return INVALID_OPERATION;
851 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700852 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700853
854 if (mInputStream != 0) {
855 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
856 return INVALID_OPERATION;
857 }
858
859 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
860 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700861 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700862
863 mInputStream = newStream;
864
865 *id = mNextStreamId++;
866
867 // Continue captures if active at start
868 if (wasActive) {
869 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
870 res = configureStreamsLocked();
871 if (res != OK) {
872 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
873 __FUNCTION__, mNextStreamId, strerror(-res), res);
874 return res;
875 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700876 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700877 }
878
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700879 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700880 return OK;
881}
882
Igor Murashkin2fba5842013-04-22 14:03:54 -0700883
884status_t Camera3Device::createZslStream(
885 uint32_t width, uint32_t height,
886 int depth,
887 /*out*/
888 int *id,
889 sp<Camera3ZslStream>* zslStream) {
890 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700891 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700892 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700893 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
894 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700895
896 status_t res;
897 bool wasActive = false;
898
899 switch (mStatus) {
900 case STATUS_ERROR:
901 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
902 return INVALID_OPERATION;
903 case STATUS_UNINITIALIZED:
904 ALOGE("%s: Device not initialized", __FUNCTION__);
905 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700906 case STATUS_UNCONFIGURED:
907 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700908 // OK
909 break;
910 case STATUS_ACTIVE:
911 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700912 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700913 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700914 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700915 return res;
916 }
917 wasActive = true;
918 break;
919 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700920 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700921 return INVALID_OPERATION;
922 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700923 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700924
925 if (mInputStream != 0) {
926 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
927 return INVALID_OPERATION;
928 }
929
930 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
931 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700932 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700933
934 res = mOutputStreams.add(mNextStreamId, newStream);
935 if (res < 0) {
936 ALOGE("%s: Can't add new stream to set: %s (%d)",
937 __FUNCTION__, strerror(-res), res);
938 return res;
939 }
940 mInputStream = newStream;
941
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530942 mNeedConfig = true;
943
Igor Murashkin2fba5842013-04-22 14:03:54 -0700944 *id = mNextStreamId++;
945 *zslStream = newStream;
946
947 // Continue captures if active at start
948 if (wasActive) {
949 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
950 res = configureStreamsLocked();
951 if (res != OK) {
952 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
953 __FUNCTION__, mNextStreamId, strerror(-res), res);
954 return res;
955 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700956 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700957 }
958
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700959 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700960 return OK;
961}
962
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700963status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800964 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -0800965 camera3_stream_rotation_t rotation, int *id, int streamSetId) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800966 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700967 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800968 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700969 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
970 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800971
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800972 status_t res;
973 bool wasActive = false;
974
975 switch (mStatus) {
976 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700977 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800978 return INVALID_OPERATION;
979 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700980 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800981 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700982 case STATUS_UNCONFIGURED:
983 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800984 // OK
985 break;
986 case STATUS_ACTIVE:
987 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700988 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800989 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700990 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800991 return res;
992 }
993 wasActive = true;
994 break;
995 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700996 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800997 return INVALID_OPERATION;
998 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700999 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001000
1001 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001002 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001003 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001004 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001005 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1006 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001007 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001008 ssize_t blobBufferSize;
1009 if (dataSpace != HAL_DATASPACE_DEPTH) {
1010 blobBufferSize = getJpegBufferSize(width, height);
1011 if (blobBufferSize <= 0) {
1012 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1013 return BAD_VALUE;
1014 }
1015 } else {
1016 blobBufferSize = getPointCloudBufferSize();
1017 if (blobBufferSize <= 0) {
1018 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1019 return BAD_VALUE;
1020 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001021 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001022 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001023 width, height, blobBufferSize, format, dataSpace, rotation,
1024 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001025 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1026 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1027 if (rawOpaqueBufferSize <= 0) {
1028 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1029 return BAD_VALUE;
1030 }
1031 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001032 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1033 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001034 } else {
1035 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001036 width, height, format, dataSpace, rotation,
1037 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001038 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001039 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001040
Zhijun He125684a2015-12-26 15:07:30 -08001041 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001042 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1043 * requires buffers to be statically allocated for internal static buffer registration, while
1044 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1045 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001046 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001047 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001048 newStream->setBufferManager(mBufferManager);
1049 }
1050
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001051 res = mOutputStreams.add(mNextStreamId, newStream);
1052 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001053 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001054 return res;
1055 }
1056
1057 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001058 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001059
1060 // Continue captures if active at start
1061 if (wasActive) {
1062 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1063 res = configureStreamsLocked();
1064 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001065 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1066 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001067 return res;
1068 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001069 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001070 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001071 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001072 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001073}
1074
1075status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1076 ATRACE_CALL();
1077 (void)outputId; (void)id;
1078
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001079 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001080 return INVALID_OPERATION;
1081}
1082
1083
1084status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001085 uint32_t *width, uint32_t *height,
1086 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001087 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001088 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001089 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001090
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001091 switch (mStatus) {
1092 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001093 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001094 return INVALID_OPERATION;
1095 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001096 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001097 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001098 case STATUS_UNCONFIGURED:
1099 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001100 case STATUS_ACTIVE:
1101 // OK
1102 break;
1103 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001104 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001105 return INVALID_OPERATION;
1106 }
1107
1108 ssize_t idx = mOutputStreams.indexOfKey(id);
1109 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001110 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001111 return idx;
1112 }
1113
1114 if (width) *width = mOutputStreams[idx]->getWidth();
1115 if (height) *height = mOutputStreams[idx]->getHeight();
1116 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001117 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001118 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001119}
1120
1121status_t Camera3Device::setStreamTransform(int id,
1122 int transform) {
1123 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001124 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001125 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001126
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001127 switch (mStatus) {
1128 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001129 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001130 return INVALID_OPERATION;
1131 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001132 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001133 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001134 case STATUS_UNCONFIGURED:
1135 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001136 case STATUS_ACTIVE:
1137 // OK
1138 break;
1139 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001140 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141 return INVALID_OPERATION;
1142 }
1143
1144 ssize_t idx = mOutputStreams.indexOfKey(id);
1145 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001146 CLOGE("Stream %d does not exist",
1147 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001148 return BAD_VALUE;
1149 }
1150
1151 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001152}
1153
1154status_t Camera3Device::deleteStream(int id) {
1155 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001156 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001157 Mutex::Autolock l(mLock);
1158 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001159
Igor Murashkine2172be2013-05-28 15:31:39 -07001160 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1161
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001162 // CameraDevice semantics require device to already be idle before
1163 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001164 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001165 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1166 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001167 }
1168
Igor Murashkin2fba5842013-04-22 14:03:54 -07001169 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001170 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001171 if (mInputStream != NULL && id == mInputStream->getId()) {
1172 deletedStream = mInputStream;
1173 mInputStream.clear();
1174 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001175 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001176 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177 return BAD_VALUE;
1178 }
Zhijun He5f446352014-01-22 09:49:33 -08001179 }
1180
1181 // Delete output stream or the output part of a bi-directional stream.
1182 if (outputStreamIdx != NAME_NOT_FOUND) {
1183 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001184 mOutputStreams.removeItem(id);
1185 }
1186
1187 // Free up the stream endpoint so that it can be used by some other stream
1188 res = deletedStream->disconnect();
1189 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001190 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001191 // fall through since we want to still list the stream as deleted.
1192 }
1193 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001194 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001195
1196 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001197}
1198
1199status_t Camera3Device::deleteReprocessStream(int id) {
1200 ATRACE_CALL();
1201 (void)id;
1202
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001203 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001204 return INVALID_OPERATION;
1205}
1206
Zhijun He1fa89992015-06-01 15:44:31 -07001207status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001208 ATRACE_CALL();
1209 ALOGV("%s: E", __FUNCTION__);
1210
1211 Mutex::Autolock il(mInterfaceLock);
1212 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001213
1214 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1215 mNeedConfig = true;
1216 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1217 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001218
1219 return configureStreamsLocked();
1220}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001221
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001222status_t Camera3Device::getInputBufferProducer(
1223 sp<IGraphicBufferProducer> *producer) {
1224 Mutex::Autolock il(mInterfaceLock);
1225 Mutex::Autolock l(mLock);
1226
1227 if (producer == NULL) {
1228 return BAD_VALUE;
1229 } else if (mInputStream == NULL) {
1230 return INVALID_OPERATION;
1231 }
1232
1233 return mInputStream->getInputBufferProducer(producer);
1234}
1235
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001236status_t Camera3Device::createDefaultRequest(int templateId,
1237 CameraMetadata *request) {
1238 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001239 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001240 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001241 Mutex::Autolock l(mLock);
1242
1243 switch (mStatus) {
1244 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001245 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001246 return INVALID_OPERATION;
1247 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001248 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001249 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001250 case STATUS_UNCONFIGURED:
1251 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001252 case STATUS_ACTIVE:
1253 // OK
1254 break;
1255 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001256 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001257 return INVALID_OPERATION;
1258 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001259
Zhijun Hea1530f12014-09-14 12:44:20 -07001260 if (!mRequestTemplateCache[templateId].isEmpty()) {
1261 *request = mRequestTemplateCache[templateId];
1262 return OK;
1263 }
1264
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001265 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001266 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001267 rawRequest = mHal3Device->ops->construct_default_request_settings(
1268 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001269 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001270 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001271 ALOGI("%s: template %d is not supported on this camera device",
1272 __FUNCTION__, templateId);
1273 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001274 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001275 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001276 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001277
1278 return OK;
1279}
1280
1281status_t Camera3Device::waitUntilDrained() {
1282 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001283 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001284 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001285
Zhijun He69a37482014-03-23 18:44:49 -07001286 return waitUntilDrainedLocked();
1287}
1288
1289status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001290 switch (mStatus) {
1291 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001292 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001293 ALOGV("%s: Already idle", __FUNCTION__);
1294 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001295 case STATUS_CONFIGURED:
1296 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001297 case STATUS_ERROR:
1298 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001299 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001300 break;
1301 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001302 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001303 return INVALID_OPERATION;
1304 }
1305
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001306 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1307 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001308 if (res != OK) {
1309 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1310 res);
1311 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001312 return res;
1313}
1314
Ruben Brunk183f0562015-08-12 12:55:02 -07001315
1316void Camera3Device::internalUpdateStatusLocked(Status status) {
1317 mStatus = status;
1318 mRecentStatusUpdates.add(mStatus);
1319 mStatusChanged.broadcast();
1320}
1321
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001322// Pause to reconfigure
1323status_t Camera3Device::internalPauseAndWaitLocked() {
1324 mRequestThread->setPaused(true);
1325 mPauseStateNotify = true;
1326
1327 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1328 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1329 if (res != OK) {
1330 SET_ERR_L("Can't idle device in %f seconds!",
1331 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001332 }
1333
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001334 return res;
1335}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001336
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001337// Resume after internalPauseAndWaitLocked
1338status_t Camera3Device::internalResumeLocked() {
1339 status_t res;
1340
1341 mRequestThread->setPaused(false);
1342
1343 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1344 if (res != OK) {
1345 SET_ERR_L("Can't transition to active in %f seconds!",
1346 kActiveTimeout/1e9);
1347 }
1348 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001349 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001350}
1351
Ruben Brunk183f0562015-08-12 12:55:02 -07001352status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001353 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001354
1355 size_t startIndex = 0;
1356 if (mStatusWaiters == 0) {
1357 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1358 // this status list
1359 mRecentStatusUpdates.clear();
1360 } else {
1361 // If other threads are waiting on updates to this status list, set the position of the
1362 // first element that this list will check rather than clearing the list.
1363 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001364 }
1365
Ruben Brunk183f0562015-08-12 12:55:02 -07001366 mStatusWaiters++;
1367
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001368 bool stateSeen = false;
1369 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001370 if (active == (mStatus == STATUS_ACTIVE)) {
1371 // Desired state is current
1372 break;
1373 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001374
1375 res = mStatusChanged.waitRelative(mLock, timeout);
1376 if (res != OK) break;
1377
Ruben Brunk183f0562015-08-12 12:55:02 -07001378 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1379 // transitions.
1380 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1381 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1382 __FUNCTION__);
1383
1384 // Encountered desired state since we began waiting
1385 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001386 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1387 stateSeen = true;
1388 break;
1389 }
1390 }
1391 } while (!stateSeen);
1392
Ruben Brunk183f0562015-08-12 12:55:02 -07001393 mStatusWaiters--;
1394
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001395 return res;
1396}
1397
1398
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001399status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1400 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001401 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001402
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001403 if (listener != NULL && mListener != NULL) {
1404 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1405 }
1406 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001407 mRequestThread->setNotificationListener(listener);
1408 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001409
1410 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001411}
1412
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001413bool Camera3Device::willNotify3A() {
1414 return false;
1415}
1416
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001417status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001418 status_t res;
1419 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001420
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001421 while (mResultQueue.empty()) {
1422 res = mResultSignal.waitRelative(mOutputLock, timeout);
1423 if (res == TIMED_OUT) {
1424 return res;
1425 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001426 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001427 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001428 return res;
1429 }
1430 }
1431 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001432}
1433
Jianing Weicb0652e2014-03-12 18:29:36 -07001434status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001435 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001436 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001437
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001438 if (mResultQueue.empty()) {
1439 return NOT_ENOUGH_DATA;
1440 }
1441
Jianing Weicb0652e2014-03-12 18:29:36 -07001442 if (frame == NULL) {
1443 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1444 return BAD_VALUE;
1445 }
1446
1447 CaptureResult &result = *(mResultQueue.begin());
1448 frame->mResultExtras = result.mResultExtras;
1449 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001450 mResultQueue.erase(mResultQueue.begin());
1451
1452 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001453}
1454
1455status_t Camera3Device::triggerAutofocus(uint32_t id) {
1456 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001457 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001458
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001459 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1460 // Mix-in this trigger into the next request and only the next request.
1461 RequestTrigger trigger[] = {
1462 {
1463 ANDROID_CONTROL_AF_TRIGGER,
1464 ANDROID_CONTROL_AF_TRIGGER_START
1465 },
1466 {
1467 ANDROID_CONTROL_AF_TRIGGER_ID,
1468 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001469 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001470 };
1471
1472 return mRequestThread->queueTrigger(trigger,
1473 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001474}
1475
1476status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1477 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001478 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001479
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001480 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1481 // Mix-in this trigger into the next request and only the next request.
1482 RequestTrigger trigger[] = {
1483 {
1484 ANDROID_CONTROL_AF_TRIGGER,
1485 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1486 },
1487 {
1488 ANDROID_CONTROL_AF_TRIGGER_ID,
1489 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001490 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001491 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001492
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001493 return mRequestThread->queueTrigger(trigger,
1494 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001495}
1496
1497status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1498 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001499 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001500
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001501 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1502 // Mix-in this trigger into the next request and only the next request.
1503 RequestTrigger trigger[] = {
1504 {
1505 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1506 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1507 },
1508 {
1509 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1510 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001511 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001512 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001513
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001514 return mRequestThread->queueTrigger(trigger,
1515 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001516}
1517
1518status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1519 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1520 ATRACE_CALL();
1521 (void)reprocessStreamId; (void)buffer; (void)listener;
1522
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001523 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001524 return INVALID_OPERATION;
1525}
1526
Jianing Weicb0652e2014-03-12 18:29:36 -07001527status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001528 ATRACE_CALL();
1529 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001530 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001531
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001532 NotificationListener* listener;
1533 {
1534 Mutex::Autolock l(mOutputLock);
1535 listener = mListener;
1536 }
1537
Zhijun He7ef20392014-04-21 16:04:17 -07001538 {
1539 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001540 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001541 }
1542
Zhijun He491e3412013-12-27 10:57:44 -08001543 status_t res;
1544 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001545 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001546 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001547 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001548 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001549 }
1550
1551 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001552}
1553
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001554status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001555 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1556}
1557
1558status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001559 ATRACE_CALL();
1560 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001561 Mutex::Autolock il(mInterfaceLock);
1562 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001563
1564 sp<Camera3StreamInterface> stream;
1565 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1566 if (outputStreamIdx == NAME_NOT_FOUND) {
1567 CLOGE("Stream %d does not exist", streamId);
1568 return BAD_VALUE;
1569 }
1570
1571 stream = mOutputStreams.editValueAt(outputStreamIdx);
1572
1573 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001574 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001575 return BAD_VALUE;
1576 }
1577
1578 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001579 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001580 return BAD_VALUE;
1581 }
1582
Ruben Brunkc78ac262015-08-13 17:58:46 -07001583 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001584}
1585
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001586status_t Camera3Device::tearDown(int streamId) {
1587 ATRACE_CALL();
1588 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1589 Mutex::Autolock il(mInterfaceLock);
1590 Mutex::Autolock l(mLock);
1591
1592 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1593 // since we cannot call register_stream_buffers except right after configure_streams.
1594 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1595 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1596 __FUNCTION__, mHal3Device->common.version);
1597 return NO_INIT;
1598 }
1599
1600 sp<Camera3StreamInterface> stream;
1601 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1602 if (outputStreamIdx == NAME_NOT_FOUND) {
1603 CLOGE("Stream %d does not exist", streamId);
1604 return BAD_VALUE;
1605 }
1606
1607 stream = mOutputStreams.editValueAt(outputStreamIdx);
1608
1609 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1610 CLOGE("Stream %d is a target of a in-progress request", streamId);
1611 return BAD_VALUE;
1612 }
1613
1614 return stream->tearDown();
1615}
1616
Zhijun He204e3292014-07-14 17:09:23 -07001617uint32_t Camera3Device::getDeviceVersion() {
1618 ATRACE_CALL();
1619 Mutex::Autolock il(mInterfaceLock);
1620 return mDeviceVersion;
1621}
1622
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001623/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001624 * Methods called by subclasses
1625 */
1626
1627void Camera3Device::notifyStatus(bool idle) {
1628 {
1629 // Need mLock to safely update state and synchronize to current
1630 // state of methods in flight.
1631 Mutex::Autolock l(mLock);
1632 // We can get various system-idle notices from the status tracker
1633 // while starting up. Only care about them if we've actually sent
1634 // in some requests recently.
1635 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1636 return;
1637 }
1638 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1639 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001640 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001641
1642 // Skip notifying listener if we're doing some user-transparent
1643 // state changes
1644 if (mPauseStateNotify) return;
1645 }
1646 NotificationListener *listener;
1647 {
1648 Mutex::Autolock l(mOutputLock);
1649 listener = mListener;
1650 }
1651 if (idle && listener != NULL) {
1652 listener->notifyIdle();
1653 }
1654}
1655
1656/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001657 * Camera3Device private methods
1658 */
1659
1660sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1661 const CameraMetadata &request) {
1662 ATRACE_CALL();
1663 status_t res;
1664
1665 sp<CaptureRequest> newRequest = new CaptureRequest;
1666 newRequest->mSettings = request;
1667
1668 camera_metadata_entry_t inputStreams =
1669 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1670 if (inputStreams.count > 0) {
1671 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001672 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001673 CLOGE("Request references unknown input stream %d",
1674 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001675 return NULL;
1676 }
1677 // Lazy completion of stream configuration (allocation/registration)
1678 // on first use
1679 if (mInputStream->isConfiguring()) {
1680 res = mInputStream->finishConfiguration(mHal3Device);
1681 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001682 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001683 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001684 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001685 return NULL;
1686 }
1687 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001688 // Check if stream is being prepared
1689 if (mInputStream->isPreparing()) {
1690 CLOGE("Request references an input stream that's being prepared!");
1691 return NULL;
1692 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001693
1694 newRequest->mInputStream = mInputStream;
1695 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1696 }
1697
1698 camera_metadata_entry_t streams =
1699 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1700 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001701 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001702 return NULL;
1703 }
1704
1705 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001706 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001707 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001708 CLOGE("Request references unknown stream %d",
1709 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001710 return NULL;
1711 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001712 sp<Camera3OutputStreamInterface> stream =
1713 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001714
1715 // Lazy completion of stream configuration (allocation/registration)
1716 // on first use
1717 if (stream->isConfiguring()) {
1718 res = stream->finishConfiguration(mHal3Device);
1719 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001720 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1721 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001722 return NULL;
1723 }
1724 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001725 // Check if stream is being prepared
1726 if (stream->isPreparing()) {
1727 CLOGE("Request references an output stream that's being prepared!");
1728 return NULL;
1729 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001730
1731 newRequest->mOutputStreams.push(stream);
1732 }
1733 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001734 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001735
1736 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001737}
1738
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001739bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1740 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1741 Size size = mSupportedOpaqueInputSizes[i];
1742 if (size.width == width && size.height == height) {
1743 return true;
1744 }
1745 }
1746
1747 return false;
1748}
1749
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001750status_t Camera3Device::configureStreamsLocked() {
1751 ATRACE_CALL();
1752 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001753
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001754 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001755 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001756 return INVALID_OPERATION;
1757 }
1758
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001759 if (!mNeedConfig) {
1760 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1761 return OK;
1762 }
1763
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001764 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1765 // adding a dummy stream instead.
1766 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1767 if (mOutputStreams.size() == 0) {
1768 addDummyStreamLocked();
1769 } else {
1770 tryRemoveDummyStreamLocked();
1771 }
1772
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001773 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001774 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001775
1776 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001777 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1778 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1779 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001780 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1781
1782 Vector<camera3_stream_t*> streams;
1783 streams.setCapacity(config.num_streams);
1784
1785 if (mInputStream != NULL) {
1786 camera3_stream_t *inputStream;
1787 inputStream = mInputStream->startConfiguration();
1788 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001789 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001790 return INVALID_OPERATION;
1791 }
1792 streams.add(inputStream);
1793 }
1794
1795 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001796
1797 // Don't configure bidi streams twice, nor add them twice to the list
1798 if (mOutputStreams[i].get() ==
1799 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1800
1801 config.num_streams--;
1802 continue;
1803 }
1804
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001805 camera3_stream_t *outputStream;
1806 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1807 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001808 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001809 return INVALID_OPERATION;
1810 }
1811 streams.add(outputStream);
1812 }
1813
1814 config.streams = streams.editArray();
1815
1816 // Do the HAL configuration; will potentially touch stream
1817 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001818 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001819 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001820 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001821
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001822 if (res == BAD_VALUE) {
1823 // HAL rejected this set of streams as unsupported, clean up config
1824 // attempt and return to unconfigured state
1825 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1826 res = mInputStream->cancelConfiguration();
1827 if (res != OK) {
1828 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1829 mInputStream->getId(), strerror(-res), res);
1830 return res;
1831 }
1832 }
1833
1834 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1835 sp<Camera3OutputStreamInterface> outputStream =
1836 mOutputStreams.editValueAt(i);
1837 if (outputStream->isConfiguring()) {
1838 res = outputStream->cancelConfiguration();
1839 if (res != OK) {
1840 SET_ERR_L(
1841 "Can't cancel configuring output stream %d: %s (%d)",
1842 outputStream->getId(), strerror(-res), res);
1843 return res;
1844 }
1845 }
1846 }
1847
1848 // Return state to that at start of call, so that future configures
1849 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001850 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001851 mNeedConfig = true;
1852
1853 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1854 return BAD_VALUE;
1855 } else if (res != OK) {
1856 // Some other kind of error from configure_streams - this is not
1857 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001858 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1859 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001860 return res;
1861 }
1862
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001863 // Finish all stream configuration immediately.
1864 // TODO: Try to relax this later back to lazy completion, which should be
1865 // faster
1866
Igor Murashkin073f8572013-05-02 14:59:28 -07001867 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001868 res = mInputStream->finishConfiguration(mHal3Device);
1869 if (res != OK) {
1870 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1871 mInputStream->getId(), strerror(-res), res);
1872 return res;
1873 }
1874 }
1875
1876 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001877 sp<Camera3OutputStreamInterface> outputStream =
1878 mOutputStreams.editValueAt(i);
1879 if (outputStream->isConfiguring()) {
1880 res = outputStream->finishConfiguration(mHal3Device);
1881 if (res != OK) {
1882 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1883 outputStream->getId(), strerror(-res), res);
1884 return res;
1885 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001886 }
1887 }
1888
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001889 // Request thread needs to know to avoid using repeat-last-settings protocol
1890 // across configure_streams() calls
1891 mRequestThread->configurationComplete();
1892
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07001893 // Boost priority of request thread for high speed recording to SCHED_FIFO
1894 if (mIsConstrainedHighSpeedConfiguration) {
1895 pid_t requestThreadTid = mRequestThread->getTid();
1896 res = requestPriority(getpid(), requestThreadTid,
1897 kConstrainedHighSpeedThreadPriority, true);
1898 if (res != OK) {
1899 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
1900 strerror(-res), res);
1901 } else {
1902 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
1903 }
1904 } else {
1905 // TODO: Set/restore normal priority for normal use cases
1906 }
1907
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001908 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001909
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001910 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001911
Ruben Brunk183f0562015-08-12 12:55:02 -07001912 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1913 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001914
1915 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1916
Zhijun He0a210512014-07-24 13:45:15 -07001917 // tear down the deleted streams after configure streams.
1918 mDeletedStreams.clear();
1919
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001920 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001921}
1922
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001923status_t Camera3Device::addDummyStreamLocked() {
1924 ATRACE_CALL();
1925 status_t res;
1926
1927 if (mDummyStreamId != NO_STREAM) {
1928 // Should never be adding a second dummy stream when one is already
1929 // active
1930 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1931 __FUNCTION__, mId);
1932 return INVALID_OPERATION;
1933 }
1934
1935 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1936
1937 sp<Camera3OutputStreamInterface> dummyStream =
1938 new Camera3DummyStream(mNextStreamId);
1939
1940 res = mOutputStreams.add(mNextStreamId, dummyStream);
1941 if (res < 0) {
1942 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1943 return res;
1944 }
1945
1946 mDummyStreamId = mNextStreamId;
1947 mNextStreamId++;
1948
1949 return OK;
1950}
1951
1952status_t Camera3Device::tryRemoveDummyStreamLocked() {
1953 ATRACE_CALL();
1954 status_t res;
1955
1956 if (mDummyStreamId == NO_STREAM) return OK;
1957 if (mOutputStreams.size() == 1) return OK;
1958
1959 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1960
1961 // Ok, have a dummy stream and there's at least one other output stream,
1962 // so remove the dummy
1963
1964 sp<Camera3StreamInterface> deletedStream;
1965 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1966 if (outputStreamIdx == NAME_NOT_FOUND) {
1967 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1968 return INVALID_OPERATION;
1969 }
1970
1971 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1972 mOutputStreams.removeItemsAt(outputStreamIdx);
1973
1974 // Free up the stream endpoint so that it can be used by some other stream
1975 res = deletedStream->disconnect();
1976 if (res != OK) {
1977 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1978 // fall through since we want to still list the stream as deleted.
1979 }
1980 mDeletedStreams.add(deletedStream);
1981 mDummyStreamId = NO_STREAM;
1982
1983 return res;
1984}
1985
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001986void Camera3Device::setErrorState(const char *fmt, ...) {
1987 Mutex::Autolock l(mLock);
1988 va_list args;
1989 va_start(args, fmt);
1990
1991 setErrorStateLockedV(fmt, args);
1992
1993 va_end(args);
1994}
1995
1996void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1997 Mutex::Autolock l(mLock);
1998 setErrorStateLockedV(fmt, args);
1999}
2000
2001void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2002 va_list args;
2003 va_start(args, fmt);
2004
2005 setErrorStateLockedV(fmt, args);
2006
2007 va_end(args);
2008}
2009
2010void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002011 // Print out all error messages to log
2012 String8 errorCause = String8::formatV(fmt, args);
2013 ALOGE("Camera %d: %s", mId, errorCause.string());
2014
2015 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002016 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002017
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002018 mErrorCause = errorCause;
2019
2020 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002021 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002022
2023 // Notify upstream about a device error
2024 if (mListener != NULL) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002025 mListener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002026 CaptureResultExtras());
2027 }
2028
2029 // Save stack trace. View by dumping it later.
2030 CameraTraces::saveTrace();
2031 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002032}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002033
2034/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002035 * In-flight request management
2036 */
2037
Jianing Weicb0652e2014-03-12 18:29:36 -07002038status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002039 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2040 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002041 ATRACE_CALL();
2042 Mutex::Autolock l(mInFlightLock);
2043
2044 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002045 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2046 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002047 if (res < 0) return res;
2048
2049 return OK;
2050}
2051
2052/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002053 * Check if all 3A fields are ready, and send off a partial 3A-only result
2054 * to the output frame queue
2055 */
Zhijun He204e3292014-07-14 17:09:23 -07002056bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07002057 uint32_t frameNumber,
2058 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002059
2060 // Check if all 3A states are present
2061 // The full list of fields is
2062 // android.control.afMode
2063 // android.control.awbMode
2064 // android.control.aeState
2065 // android.control.awbState
2066 // android.control.afState
2067 // android.control.afTriggerID
2068 // android.control.aePrecaptureID
2069 // TODO: Add android.control.aeMode
2070
2071 bool gotAllStates = true;
2072
2073 uint8_t afMode;
2074 uint8_t awbMode;
2075 uint8_t aeState;
2076 uint8_t afState;
2077 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002078
2079 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
2080 &afMode, frameNumber);
2081
2082 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
2083 &awbMode, frameNumber);
2084
2085 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
2086 &aeState, frameNumber);
2087
2088 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
2089 &afState, frameNumber);
2090
2091 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
2092 &awbState, frameNumber);
2093
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002094 if (!gotAllStates) return false;
2095
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002096 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002097 "AF state %d, AE state %d, AWB state %d, "
2098 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002099 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002100 afMode, awbMode,
2101 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002102 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002103
2104 // Got all states, so construct a minimal result to send
2105 // In addition to the above fields, this means adding in
2106 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002107 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07002108 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002109
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002110 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002111
2112 Mutex::Autolock l(mOutputLock);
2113
Jianing Weicb0652e2014-03-12 18:29:36 -07002114 CaptureResult captureResult;
2115 captureResult.mResultExtras = resultExtras;
2116 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
2117 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
2118 // but not limited to CameraDeviceBase::getNextResult
2119 CaptureResult& min3AResult =
2120 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002121
Jianing Weicb0652e2014-03-12 18:29:36 -07002122 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
2123 // TODO: This is problematic casting. Need to fix CameraMetadata.
2124 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002125 return false;
2126 }
2127
Jianing Weicb0652e2014-03-12 18:29:36 -07002128 int32_t requestId = resultExtras.requestId;
2129 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002130 &requestId, frameNumber)) {
2131 return false;
2132 }
2133
Zhijun He204e3292014-07-14 17:09:23 -07002134 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
2135 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
2136 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
2137 &partialResult, frameNumber)) {
2138 return false;
2139 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002140 }
2141
Jianing Weicb0652e2014-03-12 18:29:36 -07002142 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002143 &afMode, frameNumber)) {
2144 return false;
2145 }
2146
Jianing Weicb0652e2014-03-12 18:29:36 -07002147 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002148 &awbMode, frameNumber)) {
2149 return false;
2150 }
2151
Jianing Weicb0652e2014-03-12 18:29:36 -07002152 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002153 &aeState, frameNumber)) {
2154 return false;
2155 }
2156
Jianing Weicb0652e2014-03-12 18:29:36 -07002157 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002158 &afState, frameNumber)) {
2159 return false;
2160 }
2161
Jianing Weicb0652e2014-03-12 18:29:36 -07002162 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002163 &awbState, frameNumber)) {
2164 return false;
2165 }
2166
Jianing Weicb0652e2014-03-12 18:29:36 -07002167 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002168 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002169 return false;
2170 }
2171
Jianing Weicb0652e2014-03-12 18:29:36 -07002172 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002173 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002174 return false;
2175 }
2176
Zhijun He204e3292014-07-14 17:09:23 -07002177 // We only send the aggregated partial when all 3A related metadata are available
2178 // For both API1 and API2.
2179 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002180 mResultSignal.signal();
2181
2182 return true;
2183}
2184
2185template<typename T>
2186bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002187 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002188 (void) frameNumber;
2189
2190 camera_metadata_ro_entry_t entry;
2191
2192 entry = result.find(tag);
2193 if (entry.count == 0) {
2194 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2195 mId, frameNumber, get_camera_metadata_tag_name(tag));
2196 return false;
2197 }
2198
2199 if (sizeof(T) == sizeof(uint8_t)) {
2200 *value = entry.data.u8[0];
2201 } else if (sizeof(T) == sizeof(int32_t)) {
2202 *value = entry.data.i32[0];
2203 } else {
2204 ALOGE("%s: Unexpected type", __FUNCTION__);
2205 return false;
2206 }
2207 return true;
2208}
2209
2210template<typename T>
2211bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002212 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002213 if (result.update(tag, value, 1) != NO_ERROR) {
2214 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2215 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2216 frameNumber, get_camera_metadata_tag_name(tag));
2217 return false;
2218 }
2219 return true;
2220}
2221
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002222void Camera3Device::returnOutputBuffers(
2223 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2224 nsecs_t timestamp) {
2225 for (size_t i = 0; i < numBuffers; i++)
2226 {
2227 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2228 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2229 // Note: stream may be deallocated at this point, if this buffer was
2230 // the last reference to it.
2231 if (res != OK) {
2232 ALOGE("Can't return buffer to its stream: %s (%d)",
2233 strerror(-res), res);
2234 }
2235 }
2236}
2237
2238
2239void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2240
2241 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2242 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2243
2244 nsecs_t sensorTimestamp = request.sensorTimestamp;
2245 nsecs_t shutterTimestamp = request.shutterTimestamp;
2246
2247 // Check if it's okay to remove the request from InFlightMap:
2248 // In the case of a successful request:
2249 // all input and output buffers, all result metadata, shutter callback
2250 // arrived.
2251 // In the case of a unsuccessful request:
2252 // all input and output buffers arrived.
2253 if (request.numBuffersLeft == 0 &&
2254 (request.requestStatus != OK ||
2255 (request.haveResultMetadata && shutterTimestamp != 0))) {
2256 ATRACE_ASYNC_END("frame capture", frameNumber);
2257
2258 // Sanity check - if sensor timestamp matches shutter timestamp
2259 if (request.requestStatus == OK &&
2260 sensorTimestamp != shutterTimestamp) {
2261 SET_ERR("sensor timestamp (%" PRId64
2262 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2263 sensorTimestamp, frameNumber, shutterTimestamp);
2264 }
2265
2266 // for an unsuccessful request, it may have pending output buffers to
2267 // return.
2268 assert(request.requestStatus != OK ||
2269 request.pendingOutputBuffers.size() == 0);
2270 returnOutputBuffers(request.pendingOutputBuffers.array(),
2271 request.pendingOutputBuffers.size(), 0);
2272
2273 mInFlightMap.removeItemsAt(idx, 1);
2274
2275 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2276 }
2277
2278 // Sanity check - if we have too many in-flight frames, something has
2279 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002280 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002281 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002282 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2283 kInFlightWarnLimitHighSpeed) {
2284 CLOGE("In-flight list too large for high speed configuration: %zu",
2285 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002286 }
2287}
2288
2289
2290void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2291 CaptureResultExtras &resultExtras,
2292 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002293 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002294 bool reprocess,
2295 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002296 if (pendingMetadata.isEmpty())
2297 return;
2298
2299 Mutex::Autolock l(mOutputLock);
2300
2301 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002302 if (reprocess) {
2303 if (frameNumber < mNextReprocessResultFrameNumber) {
2304 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002305 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002306 frameNumber, mNextReprocessResultFrameNumber);
2307 return;
2308 }
2309 mNextReprocessResultFrameNumber = frameNumber + 1;
2310 } else {
2311 if (frameNumber < mNextResultFrameNumber) {
2312 SET_ERR("Out-of-order capture result metadata submitted! "
2313 "(got frame number %d, expecting %d)",
2314 frameNumber, mNextResultFrameNumber);
2315 return;
2316 }
2317 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002318 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002319
2320 CaptureResult captureResult;
2321 captureResult.mResultExtras = resultExtras;
2322 captureResult.mMetadata = pendingMetadata;
2323
2324 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2325 (int32_t*)&frameNumber, 1) != OK) {
2326 SET_ERR("Failed to set frame# in metadata (%d)",
2327 frameNumber);
2328 return;
2329 } else {
2330 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2331 __FUNCTION__, mId, frameNumber);
2332 }
2333
2334 // Append any previous partials to form a complete result
2335 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2336 captureResult.mMetadata.append(collectedPartialResult);
2337 }
2338
2339 captureResult.mMetadata.sort();
2340
2341 // Check that there's a timestamp in the result metadata
2342 camera_metadata_entry entry =
2343 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2344 if (entry.count == 0) {
2345 SET_ERR("No timestamp provided by HAL for frame %d!",
2346 frameNumber);
2347 return;
2348 }
2349
Chien-Yu Chend196d612015-06-22 19:49:01 -07002350 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2351
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002352 // Valid result, insert into queue
2353 List<CaptureResult>::iterator queuedResult =
2354 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2355 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2356 ", burstId = %" PRId32, __FUNCTION__,
2357 queuedResult->mResultExtras.requestId,
2358 queuedResult->mResultExtras.frameNumber,
2359 queuedResult->mResultExtras.burstId);
2360
2361 mResultSignal.signal();
2362}
2363
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002364/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002365 * Camera HAL device callback methods
2366 */
2367
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002368void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002369 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002370
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002371 status_t res;
2372
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002373 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002374 if (result->result == NULL && result->num_output_buffers == 0 &&
2375 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002376 SET_ERR("No result data provided by HAL for frame %d",
2377 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002378 return;
2379 }
Zhijun He204e3292014-07-14 17:09:23 -07002380
2381 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2382 // partial_result to 1 when metadata is included in this result.
2383 if (!mUsePartialResult &&
2384 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2385 result->result != NULL &&
2386 result->partial_result != 1) {
2387 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2388 " if partial result is not supported",
2389 frameNumber, result->partial_result);
2390 return;
2391 }
2392
2393 bool isPartialResult = false;
2394 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002395 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002396 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002397
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002398 // Get shutter timestamp and resultExtras from list of in-flight requests,
2399 // where it was added by the shutter notification for this frame. If the
2400 // shutter timestamp isn't received yet, append the output buffers to the
2401 // in-flight request and they will be returned when the shutter timestamp
2402 // arrives. Update the in-flight status and remove the in-flight entry if
2403 // all result data and shutter timestamp have been received.
2404 nsecs_t shutterTimestamp = 0;
2405
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002406 {
2407 Mutex::Autolock l(mInFlightLock);
2408 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2409 if (idx == NAME_NOT_FOUND) {
2410 SET_ERR("Unknown frame number for capture result: %d",
2411 frameNumber);
2412 return;
2413 }
2414 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002415 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2416 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2417 ", partialResultCount = %d",
2418 __FUNCTION__, request.resultExtras.requestId,
2419 request.resultExtras.frameNumber, request.resultExtras.burstId,
2420 result->partial_result);
2421 // Always update the partial count to the latest one if it's not 0
2422 // (buffers only). When framework aggregates adjacent partial results
2423 // into one, the latest partial count will be used.
2424 if (result->partial_result != 0)
2425 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002426
2427 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002428 if (mUsePartialResult && result->result != NULL) {
2429 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2430 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2431 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2432 " the range of [1, %d] when metadata is included in the result",
2433 frameNumber, result->partial_result, mNumPartialResults);
2434 return;
2435 }
2436 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002437 if (isPartialResult) {
2438 request.partialResult.collectedResult.append(result->result);
2439 }
Zhijun He204e3292014-07-14 17:09:23 -07002440 } else {
2441 camera_metadata_ro_entry_t partialResultEntry;
2442 res = find_camera_metadata_ro_entry(result->result,
2443 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2444 if (res != NAME_NOT_FOUND &&
2445 partialResultEntry.count > 0 &&
2446 partialResultEntry.data.u8[0] ==
2447 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2448 // A partial result. Flag this as such, and collect this
2449 // set of metadata into the in-flight entry.
2450 isPartialResult = true;
2451 request.partialResult.collectedResult.append(
2452 result->result);
2453 request.partialResult.collectedResult.erase(
2454 ANDROID_QUIRKS_PARTIAL_RESULT);
2455 }
2456 }
2457
2458 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002459 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002460 if (!request.partialResult.haveSent3A) {
2461 request.partialResult.haveSent3A =
2462 processPartial3AResult(frameNumber,
2463 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002464 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002465 }
2466 }
2467 }
2468
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002469 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002470 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002471
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002472 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002473 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002474 if (request.haveResultMetadata) {
2475 SET_ERR("Called multiple times with metadata for frame %d",
2476 frameNumber);
2477 return;
2478 }
Zhijun He204e3292014-07-14 17:09:23 -07002479 if (mUsePartialResult &&
2480 !request.partialResult.collectedResult.isEmpty()) {
2481 collectedPartialResult.acquire(
2482 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002483 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002484 request.haveResultMetadata = true;
2485 }
2486
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002487 uint32_t numBuffersReturned = result->num_output_buffers;
2488 if (result->input_buffer != NULL) {
2489 if (hasInputBufferInRequest) {
2490 numBuffersReturned += 1;
2491 } else {
2492 ALOGW("%s: Input buffer should be NULL if there is no input"
2493 " buffer sent in the request",
2494 __FUNCTION__);
2495 }
2496 }
2497 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002498 if (request.numBuffersLeft < 0) {
2499 SET_ERR("Too many buffers returned for frame %d",
2500 frameNumber);
2501 return;
2502 }
2503
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002504 camera_metadata_ro_entry_t entry;
2505 res = find_camera_metadata_ro_entry(result->result,
2506 ANDROID_SENSOR_TIMESTAMP, &entry);
2507 if (res == OK && entry.count == 1) {
2508 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002509 }
2510
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002511 // If shutter event isn't received yet, append the output buffers to
2512 // the in-flight request. Otherwise, return the output buffers to
2513 // streams.
2514 if (shutterTimestamp == 0) {
2515 request.pendingOutputBuffers.appendArray(result->output_buffers,
2516 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002517 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002518 returnOutputBuffers(result->output_buffers,
2519 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002520 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002521
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002522 if (result->result != NULL && !isPartialResult) {
2523 if (shutterTimestamp == 0) {
2524 request.pendingMetadata = result->result;
2525 request.partialResult.collectedResult = collectedPartialResult;
2526 } else {
2527 CameraMetadata metadata;
2528 metadata = result->result;
2529 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002530 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2531 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002532 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002533 }
2534
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002535 removeInFlightRequestIfReadyLocked(idx);
2536 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002537
Zhijun Hef0d962a2014-06-30 10:24:11 -07002538 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002539 if (hasInputBufferInRequest) {
2540 Camera3Stream *stream =
2541 Camera3Stream::cast(result->input_buffer->stream);
2542 res = stream->returnInputBuffer(*(result->input_buffer));
2543 // Note: stream may be deallocated at this point, if this buffer was the
2544 // last reference to it.
2545 if (res != OK) {
2546 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2547 " its stream:%s (%d)", __FUNCTION__,
2548 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002549 }
2550 } else {
2551 ALOGW("%s: Input buffer should be NULL if there is no input"
2552 " buffer sent in the request, skipping input buffer return.",
2553 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002554 }
2555 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002556}
2557
2558void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002559 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002560 NotificationListener *listener;
2561 {
2562 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002563 listener = mListener;
2564 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002565
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002566 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002567 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002568 return;
2569 }
2570
2571 switch (msg->type) {
2572 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002573 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002574 break;
2575 }
2576 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002577 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002578 break;
2579 }
2580 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002581 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002582 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002583 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002584}
2585
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002586void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2587 NotificationListener *listener) {
2588
2589 // Map camera HAL error codes to ICameraDeviceCallback error codes
2590 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002591 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002592 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002593 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002594 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002595 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002596 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002597 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002598 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002599 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002600 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002601 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002602 };
2603
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002604 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002605 ((msg.error_code >= 0) &&
2606 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2607 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002608 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002609
2610 int streamId = 0;
2611 if (msg.error_stream != NULL) {
2612 Camera3Stream *stream =
2613 Camera3Stream::cast(msg.error_stream);
2614 streamId = stream->getId();
2615 }
2616 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2617 mId, __FUNCTION__, msg.frame_number,
2618 streamId, msg.error_code);
2619
2620 CaptureResultExtras resultExtras;
2621 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002622 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002623 // SET_ERR calls notifyError
2624 SET_ERR("Camera HAL reported serious device error");
2625 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002626 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2627 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2628 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002629 {
2630 Mutex::Autolock l(mInFlightLock);
2631 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2632 if (idx >= 0) {
2633 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2634 r.requestStatus = msg.error_code;
2635 resultExtras = r.resultExtras;
2636 } else {
2637 resultExtras.frameNumber = msg.frame_number;
2638 ALOGE("Camera %d: %s: cannot find in-flight request on "
2639 "frame %" PRId64 " error", mId, __FUNCTION__,
2640 resultExtras.frameNumber);
2641 }
2642 }
2643 if (listener != NULL) {
2644 listener->notifyError(errorCode, resultExtras);
2645 } else {
2646 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2647 }
2648 break;
2649 default:
2650 // SET_ERR calls notifyError
2651 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2652 break;
2653 }
2654}
2655
2656void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2657 NotificationListener *listener) {
2658 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002659
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002660 // Set timestamp for the request in the in-flight tracking
2661 // and get the request ID to send upstream
2662 {
2663 Mutex::Autolock l(mInFlightLock);
2664 idx = mInFlightMap.indexOfKey(msg.frame_number);
2665 if (idx >= 0) {
2666 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002667
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002668 // Verify ordering of shutter notifications
2669 {
2670 Mutex::Autolock l(mOutputLock);
2671 // TODO: need to track errors for tighter bounds on expected frame number.
2672 if (r.hasInputBuffer) {
2673 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2674 SET_ERR("Shutter notification out-of-order. Expected "
2675 "notification for frame %d, got frame %d",
2676 mNextReprocessShutterFrameNumber, msg.frame_number);
2677 return;
2678 }
2679 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2680 } else {
2681 if (msg.frame_number < mNextShutterFrameNumber) {
2682 SET_ERR("Shutter notification out-of-order. Expected "
2683 "notification for frame %d, got frame %d",
2684 mNextShutterFrameNumber, msg.frame_number);
2685 return;
2686 }
2687 mNextShutterFrameNumber = msg.frame_number + 1;
2688 }
2689 }
2690
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002691 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2692 mId, __FUNCTION__,
2693 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2694 // Call listener, if any
2695 if (listener != NULL) {
2696 listener->notifyShutter(r.resultExtras, msg.timestamp);
2697 }
2698
2699 r.shutterTimestamp = msg.timestamp;
2700
2701 // send pending result and buffers
2702 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002703 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002704 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002705 returnOutputBuffers(r.pendingOutputBuffers.array(),
2706 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2707 r.pendingOutputBuffers.clear();
2708
2709 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002710 }
2711 }
2712 if (idx < 0) {
2713 SET_ERR("Shutter notification for non-existent frame number %d",
2714 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002715 }
2716}
2717
2718
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002719CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002720 ALOGV("%s", __FUNCTION__);
2721
Igor Murashkin1e479c02013-09-06 16:55:14 -07002722 CameraMetadata retVal;
2723
2724 if (mRequestThread != NULL) {
2725 retVal = mRequestThread->getLatestRequest();
2726 }
2727
Igor Murashkin1e479c02013-09-06 16:55:14 -07002728 return retVal;
2729}
2730
Jianing Weicb0652e2014-03-12 18:29:36 -07002731
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002732/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002733 * RequestThread inner class methods
2734 */
2735
2736Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002737 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002738 camera3_device_t *hal3Device,
2739 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002740 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002741 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002742 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002743 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002744 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002745 mReconfigured(false),
2746 mDoPause(false),
2747 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002748 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002749 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002750 mCurrentAfTriggerId(0),
2751 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002752 mRepeatingLastFrameNumber(
2753 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002754 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002755 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002756}
2757
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002758void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002759 NotificationListener *listener) {
2760 Mutex::Autolock l(mRequestLock);
2761 mListener = listener;
2762}
2763
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002764void Camera3Device::RequestThread::configurationComplete() {
2765 Mutex::Autolock l(mRequestLock);
2766 mReconfigured = true;
2767}
2768
Jianing Wei90e59c92014-03-12 18:29:36 -07002769status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002770 List<sp<CaptureRequest> > &requests,
2771 /*out*/
2772 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002773 Mutex::Autolock l(mRequestLock);
2774 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2775 ++it) {
2776 mRequestQueue.push_back(*it);
2777 }
2778
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002779 if (lastFrameNumber != NULL) {
2780 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2781 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2782 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2783 *lastFrameNumber);
2784 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002785
Jianing Wei90e59c92014-03-12 18:29:36 -07002786 unpauseForNewRequests();
2787
2788 return OK;
2789}
2790
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002791
2792status_t Camera3Device::RequestThread::queueTrigger(
2793 RequestTrigger trigger[],
2794 size_t count) {
2795
2796 Mutex::Autolock l(mTriggerMutex);
2797 status_t ret;
2798
2799 for (size_t i = 0; i < count; ++i) {
2800 ret = queueTriggerLocked(trigger[i]);
2801
2802 if (ret != OK) {
2803 return ret;
2804 }
2805 }
2806
2807 return OK;
2808}
2809
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002810int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2811 sp<Camera3Device> d = device.promote();
2812 if (d != NULL) return d->mId;
2813 return 0;
2814}
2815
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002816status_t Camera3Device::RequestThread::queueTriggerLocked(
2817 RequestTrigger trigger) {
2818
2819 uint32_t tag = trigger.metadataTag;
2820 ssize_t index = mTriggerMap.indexOfKey(tag);
2821
2822 switch (trigger.getTagType()) {
2823 case TYPE_BYTE:
2824 // fall-through
2825 case TYPE_INT32:
2826 break;
2827 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002828 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2829 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002830 return INVALID_OPERATION;
2831 }
2832
2833 /**
2834 * Collect only the latest trigger, since we only have 1 field
2835 * in the request settings per trigger tag, and can't send more than 1
2836 * trigger per request.
2837 */
2838 if (index != NAME_NOT_FOUND) {
2839 mTriggerMap.editValueAt(index) = trigger;
2840 } else {
2841 mTriggerMap.add(tag, trigger);
2842 }
2843
2844 return OK;
2845}
2846
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002847status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002848 const RequestList &requests,
2849 /*out*/
2850 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002851 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002852 if (lastFrameNumber != NULL) {
2853 *lastFrameNumber = mRepeatingLastFrameNumber;
2854 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002855 mRepeatingRequests.clear();
2856 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2857 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002858
2859 unpauseForNewRequests();
2860
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002861 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002862 return OK;
2863}
2864
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002865bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2866 if (mRepeatingRequests.empty()) {
2867 return false;
2868 }
2869 int32_t requestId = requestIn->mResultExtras.requestId;
2870 const RequestList &repeatRequests = mRepeatingRequests;
2871 // All repeating requests are guaranteed to have same id so only check first quest
2872 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2873 return (firstRequest->mResultExtras.requestId == requestId);
2874}
2875
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002876status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002877 Mutex::Autolock l(mRequestLock);
2878 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002879 if (lastFrameNumber != NULL) {
2880 *lastFrameNumber = mRepeatingLastFrameNumber;
2881 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002882 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002883 return OK;
2884}
2885
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002886status_t Camera3Device::RequestThread::clear(
2887 NotificationListener *listener,
2888 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002889 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002890 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002891
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002892 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002893
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002894 // Send errors for all requests pending in the request queue, including
2895 // pending repeating requests
2896 if (listener != NULL) {
2897 for (RequestList::iterator it = mRequestQueue.begin();
2898 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002899 // Abort the input buffers for reprocess requests.
2900 if ((*it)->mInputStream != NULL) {
2901 camera3_stream_buffer_t inputBuffer;
2902 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2903 if (res != OK) {
2904 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2905 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2906 } else {
2907 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2908 if (res != OK) {
2909 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2910 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2911 }
2912 }
2913 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002914 // Set the frame number this request would have had, if it
2915 // had been submitted; this frame number will not be reused.
2916 // The requestId and burstId fields were set when the request was
2917 // submitted originally (in convertMetadataListToRequestListLocked)
2918 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002919 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002920 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002921 }
2922 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002923 mRequestQueue.clear();
2924 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002925 if (lastFrameNumber != NULL) {
2926 *lastFrameNumber = mRepeatingLastFrameNumber;
2927 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002928 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002929 return OK;
2930}
2931
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002932status_t Camera3Device::RequestThread::flush() {
2933 ATRACE_CALL();
2934 Mutex::Autolock l(mFlushLock);
2935
2936 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2937 return mHal3Device->ops->flush(mHal3Device);
2938 }
2939
2940 return -ENOTSUP;
2941}
2942
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002943void Camera3Device::RequestThread::setPaused(bool paused) {
2944 Mutex::Autolock l(mPauseLock);
2945 mDoPause = paused;
2946 mDoPauseSignal.signal();
2947}
2948
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002949status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2950 int32_t requestId, nsecs_t timeout) {
2951 Mutex::Autolock l(mLatestRequestMutex);
2952 status_t res;
2953 while (mLatestRequestId != requestId) {
2954 nsecs_t startTime = systemTime();
2955
2956 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2957 if (res != OK) return res;
2958
2959 timeout -= (systemTime() - startTime);
2960 }
2961
2962 return OK;
2963}
2964
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002965void Camera3Device::RequestThread::requestExit() {
2966 // Call parent to set up shutdown
2967 Thread::requestExit();
2968 // The exit from any possible waits
2969 mDoPauseSignal.signal();
2970 mRequestSignal.signal();
2971}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002972
Chien-Yu Chend196d612015-06-22 19:49:01 -07002973
2974/**
2975 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2976 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2977 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2978 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2979 * request.
2980 */
2981void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2982 request->mAeTriggerCancelOverride.applyAeLock = false;
2983 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2984
2985 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2986 return;
2987 }
2988
2989 camera_metadata_entry_t aePrecaptureTrigger =
2990 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2991 if (aePrecaptureTrigger.count > 0 &&
2992 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2993 // Always override CANCEL to IDLE
2994 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2995 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2996 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2997 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2998 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2999
3000 if (mAeLockAvailable == true) {
3001 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3002 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3003 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3004 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3005 request->mAeTriggerCancelOverride.applyAeLock = true;
3006 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3007 }
3008 }
3009 }
3010}
3011
3012/**
3013 * Override result metadata for cancelling AE precapture trigger applied in
3014 * handleAePrecaptureCancelRequest().
3015 */
3016void Camera3Device::overrideResultForPrecaptureCancel(
3017 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3018 if (aeTriggerCancelOverride.applyAeLock) {
3019 // Only devices <= v3.2 should have this override
3020 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3021 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3022 }
3023
3024 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3025 // Only devices <= v3.2 should have this override
3026 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3027 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3028 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3029 }
3030}
3031
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003032bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003033 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003034 status_t res;
3035
3036 // Handle paused state.
3037 if (waitIfPaused()) {
3038 return true;
3039 }
3040
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003041 // Wait for the next batch of requests.
3042 waitForNextRequestBatch();
3043 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003044 return true;
3045 }
3046
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003047 // Get the latest request ID, if any
3048 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003049 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003050 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003051 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003052 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003053 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003054 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3055 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003056 }
3057
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003058 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003059 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003060 if (res == TIMED_OUT) {
3061 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003062 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003063 return true;
3064 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003065 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003066 return false;
3067 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003068
Zhijun Hecc27e112013-10-03 16:12:43 -07003069 // Inform waitUntilRequestProcessed thread of a new request ID
3070 {
3071 Mutex::Autolock al(mLatestRequestMutex);
3072
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003073 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003074 mLatestRequestSignal.signal();
3075 }
3076
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003077 // Submit a batch of requests to HAL.
3078 // Use flush lock only when submitting multilple requests in a batch.
3079 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3080 // which may take a long time to finish so synchronizing flush() and
3081 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3082 // For now, only synchronize for high speed recording and we should figure something out for
3083 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003084 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003085
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003086 if (useFlushLock) {
3087 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003088 }
3089
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003090 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003091 mNextRequests.size());
3092 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003093 // Submit request and block until ready for next one
3094 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3095 ATRACE_BEGIN("camera3->process_capture_request");
3096 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3097 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003098
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003099 if (res != OK) {
3100 // Should only get a failure here for malformed requests or device-level
3101 // errors, so consider all errors fatal. Bad metadata failures should
3102 // come through notify.
3103 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3104 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3105 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003106 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003107 if (useFlushLock) {
3108 mFlushLock.unlock();
3109 }
3110 return false;
3111 }
3112
3113 // Mark that the request has be submitted successfully.
3114 nextRequest.submitted = true;
3115
3116 // Update the latest request sent to HAL
3117 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3118 Mutex::Autolock al(mLatestRequestMutex);
3119
3120 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3121 mLatestRequest.acquire(cloned);
3122 }
3123
3124 if (nextRequest.halRequest.settings != NULL) {
3125 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3126 }
3127
3128 // Remove any previously queued triggers (after unlock)
3129 res = removeTriggers(mPrevRequest);
3130 if (res != OK) {
3131 SET_ERR("RequestThread: Unable to remove triggers "
3132 "(capture request %d, HAL device: %s (%d)",
3133 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003134 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003135 if (useFlushLock) {
3136 mFlushLock.unlock();
3137 }
3138 return false;
3139 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003140 }
3141
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003142 if (useFlushLock) {
3143 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003144 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003145
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003146 // Unset as current request
3147 {
3148 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003149 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003150 }
3151
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003152 return true;
3153}
3154
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003155status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003156 ATRACE_CALL();
3157
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003158 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003159 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3160 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3161 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3162
3163 // Prepare a request to HAL
3164 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3165
3166 // Insert any queued triggers (before metadata is locked)
3167 status_t res = insertTriggers(captureRequest);
3168
3169 if (res < 0) {
3170 SET_ERR("RequestThread: Unable to insert triggers "
3171 "(capture request %d, HAL device: %s (%d)",
3172 halRequest->frame_number, strerror(-res), res);
3173 return INVALID_OPERATION;
3174 }
3175 int triggerCount = res;
3176 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3177 mPrevTriggers = triggerCount;
3178
3179 // If the request is the same as last, or we had triggers last time
3180 if (mPrevRequest != captureRequest || triggersMixedIn) {
3181 /**
3182 * HAL workaround:
3183 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3184 */
3185 res = addDummyTriggerIds(captureRequest);
3186 if (res != OK) {
3187 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3188 "(capture request %d, HAL device: %s (%d)",
3189 halRequest->frame_number, strerror(-res), res);
3190 return INVALID_OPERATION;
3191 }
3192
3193 /**
3194 * The request should be presorted so accesses in HAL
3195 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3196 */
3197 captureRequest->mSettings.sort();
3198 halRequest->settings = captureRequest->mSettings.getAndLock();
3199 mPrevRequest = captureRequest;
3200 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3201
3202 IF_ALOGV() {
3203 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3204 find_camera_metadata_ro_entry(
3205 halRequest->settings,
3206 ANDROID_CONTROL_AF_TRIGGER,
3207 &e
3208 );
3209 if (e.count > 0) {
3210 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3211 __FUNCTION__,
3212 halRequest->frame_number,
3213 e.data.u8[0]);
3214 }
3215 }
3216 } else {
3217 // leave request.settings NULL to indicate 'reuse latest given'
3218 ALOGVV("%s: Request settings are REUSED",
3219 __FUNCTION__);
3220 }
3221
3222 uint32_t totalNumBuffers = 0;
3223
3224 // Fill in buffers
3225 if (captureRequest->mInputStream != NULL) {
3226 halRequest->input_buffer = &captureRequest->mInputBuffer;
3227 totalNumBuffers += 1;
3228 } else {
3229 halRequest->input_buffer = NULL;
3230 }
3231
3232 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3233 captureRequest->mOutputStreams.size());
3234 halRequest->output_buffers = outputBuffers->array();
3235 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3236 res = captureRequest->mOutputStreams.editItemAt(i)->
3237 getBuffer(&outputBuffers->editItemAt(i));
3238 if (res != OK) {
3239 // Can't get output buffer from gralloc queue - this could be due to
3240 // abandoned queue or other consumer misbehavior, so not a fatal
3241 // error
3242 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3243 " %s (%d)", strerror(-res), res);
3244
3245 return TIMED_OUT;
3246 }
3247 halRequest->num_output_buffers++;
3248 }
3249 totalNumBuffers += halRequest->num_output_buffers;
3250
3251 // Log request in the in-flight queue
3252 sp<Camera3Device> parent = mParent.promote();
3253 if (parent == NULL) {
3254 // Should not happen, and nowhere to send errors to, so just log it
3255 CLOGE("RequestThread: Parent is gone");
3256 return INVALID_OPERATION;
3257 }
3258 res = parent->registerInFlight(halRequest->frame_number,
3259 totalNumBuffers, captureRequest->mResultExtras,
3260 /*hasInput*/halRequest->input_buffer != NULL,
3261 captureRequest->mAeTriggerCancelOverride);
3262 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3263 ", burstId = %" PRId32 ".",
3264 __FUNCTION__,
3265 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3266 captureRequest->mResultExtras.burstId);
3267 if (res != OK) {
3268 SET_ERR("RequestThread: Unable to register new in-flight request:"
3269 " %s (%d)", strerror(-res), res);
3270 return INVALID_OPERATION;
3271 }
3272 }
3273
3274 return OK;
3275}
3276
Igor Murashkin1e479c02013-09-06 16:55:14 -07003277CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3278 Mutex::Autolock al(mLatestRequestMutex);
3279
3280 ALOGV("RequestThread::%s", __FUNCTION__);
3281
3282 return mLatestRequest;
3283}
3284
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003285bool Camera3Device::RequestThread::isStreamPending(
3286 sp<Camera3StreamInterface>& stream) {
3287 Mutex::Autolock l(mRequestLock);
3288
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003289 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003290 if (!nextRequest.submitted) {
3291 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3292 if (stream == s) return true;
3293 }
3294 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003295 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003296 }
3297
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003298 for (const auto& request : mRequestQueue) {
3299 for (const auto& s : request->mOutputStreams) {
3300 if (stream == s) return true;
3301 }
3302 if (stream == request->mInputStream) return true;
3303 }
3304
3305 for (const auto& request : mRepeatingRequests) {
3306 for (const auto& s : request->mOutputStreams) {
3307 if (stream == s) return true;
3308 }
3309 if (stream == request->mInputStream) return true;
3310 }
3311
3312 return false;
3313}
Jianing Weicb0652e2014-03-12 18:29:36 -07003314
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003315void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3316 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003317 return;
3318 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003319
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003320 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003321 // Skip the ones that have been submitted successfully.
3322 if (nextRequest.submitted) {
3323 continue;
3324 }
3325
3326 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3327 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3328 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3329
3330 if (halRequest->settings != NULL) {
3331 captureRequest->mSettings.unlock(halRequest->settings);
3332 }
3333
3334 if (captureRequest->mInputStream != NULL) {
3335 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3336 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3337 }
3338
3339 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3340 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3341 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3342 }
3343
3344 if (sendRequestError) {
3345 Mutex::Autolock l(mRequestLock);
3346 if (mListener != NULL) {
3347 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003348 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003349 captureRequest->mResultExtras);
3350 }
3351 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003352 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003353
3354 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003355 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003356}
3357
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003358void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003359 // Optimized a bit for the simple steady-state case (single repeating
3360 // request), to avoid putting that request in the queue temporarily.
3361 Mutex::Autolock l(mRequestLock);
3362
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003363 assert(mNextRequests.empty());
3364
3365 NextRequest nextRequest;
3366 nextRequest.captureRequest = waitForNextRequestLocked();
3367 if (nextRequest.captureRequest == nullptr) {
3368 return;
3369 }
3370
3371 nextRequest.halRequest = camera3_capture_request_t();
3372 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003373 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003374
3375 // Wait for additional requests
3376 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3377
3378 for (size_t i = 1; i < batchSize; i++) {
3379 NextRequest additionalRequest;
3380 additionalRequest.captureRequest = waitForNextRequestLocked();
3381 if (additionalRequest.captureRequest == nullptr) {
3382 break;
3383 }
3384
3385 additionalRequest.halRequest = camera3_capture_request_t();
3386 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003387 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003388 }
3389
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003390 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003391 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003392 mNextRequests.size(), batchSize);
3393 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003394 }
3395
3396 return;
3397}
3398
3399sp<Camera3Device::CaptureRequest>
3400 Camera3Device::RequestThread::waitForNextRequestLocked() {
3401 status_t res;
3402 sp<CaptureRequest> nextRequest;
3403
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003404 while (mRequestQueue.empty()) {
3405 if (!mRepeatingRequests.empty()) {
3406 // Always atomically enqueue all requests in a repeating request
3407 // list. Guarantees a complete in-sequence set of captures to
3408 // application.
3409 const RequestList &requests = mRepeatingRequests;
3410 RequestList::const_iterator firstRequest =
3411 requests.begin();
3412 nextRequest = *firstRequest;
3413 mRequestQueue.insert(mRequestQueue.end(),
3414 ++firstRequest,
3415 requests.end());
3416 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003417
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003418 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003419
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003420 break;
3421 }
3422
3423 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3424
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003425 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3426 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003427 Mutex::Autolock pl(mPauseLock);
3428 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003429 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003430 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003431 // Let the tracker know
3432 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3433 if (statusTracker != 0) {
3434 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3435 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003436 }
3437 // Stop waiting for now and let thread management happen
3438 return NULL;
3439 }
3440 }
3441
3442 if (nextRequest == NULL) {
3443 // Don't have a repeating request already in hand, so queue
3444 // must have an entry now.
3445 RequestList::iterator firstRequest =
3446 mRequestQueue.begin();
3447 nextRequest = *firstRequest;
3448 mRequestQueue.erase(firstRequest);
3449 }
3450
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003451 // In case we've been unpaused by setPaused clearing mDoPause, need to
3452 // update internal pause state (capture/setRepeatingRequest unpause
3453 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003454 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003455 if (mPaused) {
3456 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3457 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3458 if (statusTracker != 0) {
3459 statusTracker->markComponentActive(mStatusId);
3460 }
3461 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003462 mPaused = false;
3463
3464 // Check if we've reconfigured since last time, and reset the preview
3465 // request if so. Can't use 'NULL request == repeat' across configure calls.
3466 if (mReconfigured) {
3467 mPrevRequest.clear();
3468 mReconfigured = false;
3469 }
3470
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003471 if (nextRequest != NULL) {
3472 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003473 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3474 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003475
3476 // Since RequestThread::clear() removes buffers from the input stream,
3477 // get the right buffer here before unlocking mRequestLock
3478 if (nextRequest->mInputStream != NULL) {
3479 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3480 if (res != OK) {
3481 // Can't get input buffer from gralloc queue - this could be due to
3482 // disconnected queue or other producer misbehavior, so not a fatal
3483 // error
3484 ALOGE("%s: Can't get input buffer, skipping request:"
3485 " %s (%d)", __FUNCTION__, strerror(-res), res);
3486 if (mListener != NULL) {
3487 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003488 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003489 nextRequest->mResultExtras);
3490 }
3491 return NULL;
3492 }
3493 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003494 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003495
3496 handleAePrecaptureCancelRequest(nextRequest);
3497
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003498 return nextRequest;
3499}
3500
3501bool Camera3Device::RequestThread::waitIfPaused() {
3502 status_t res;
3503 Mutex::Autolock l(mPauseLock);
3504 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003505 if (mPaused == false) {
3506 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003507 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3508 // Let the tracker know
3509 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3510 if (statusTracker != 0) {
3511 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3512 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003513 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003514
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003515 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003516 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003517 return true;
3518 }
3519 }
3520 // We don't set mPaused to false here, because waitForNextRequest needs
3521 // to further manage the paused state in case of starvation.
3522 return false;
3523}
3524
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003525void Camera3Device::RequestThread::unpauseForNewRequests() {
3526 // With work to do, mark thread as unpaused.
3527 // If paused by request (setPaused), don't resume, to avoid
3528 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003529 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003530 Mutex::Autolock p(mPauseLock);
3531 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003532 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3533 if (mPaused) {
3534 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3535 if (statusTracker != 0) {
3536 statusTracker->markComponentActive(mStatusId);
3537 }
3538 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003539 mPaused = false;
3540 }
3541}
3542
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003543void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3544 sp<Camera3Device> parent = mParent.promote();
3545 if (parent != NULL) {
3546 va_list args;
3547 va_start(args, fmt);
3548
3549 parent->setErrorStateV(fmt, args);
3550
3551 va_end(args);
3552 }
3553}
3554
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003555status_t Camera3Device::RequestThread::insertTriggers(
3556 const sp<CaptureRequest> &request) {
3557
3558 Mutex::Autolock al(mTriggerMutex);
3559
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003560 sp<Camera3Device> parent = mParent.promote();
3561 if (parent == NULL) {
3562 CLOGE("RequestThread: Parent is gone");
3563 return DEAD_OBJECT;
3564 }
3565
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003566 CameraMetadata &metadata = request->mSettings;
3567 size_t count = mTriggerMap.size();
3568
3569 for (size_t i = 0; i < count; ++i) {
3570 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003571 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003572
3573 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3574 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3575 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003576 if (isAeTrigger) {
3577 request->mResultExtras.precaptureTriggerId = triggerId;
3578 mCurrentPreCaptureTriggerId = triggerId;
3579 } else {
3580 request->mResultExtras.afTriggerId = triggerId;
3581 mCurrentAfTriggerId = triggerId;
3582 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003583 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3584 continue; // Trigger ID tag is deprecated since device HAL 3.2
3585 }
3586 }
3587
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003588 camera_metadata_entry entry = metadata.find(tag);
3589
3590 if (entry.count > 0) {
3591 /**
3592 * Already has an entry for this trigger in the request.
3593 * Rewrite it with our requested trigger value.
3594 */
3595 RequestTrigger oldTrigger = trigger;
3596
3597 oldTrigger.entryValue = entry.data.u8[0];
3598
3599 mTriggerReplacedMap.add(tag, oldTrigger);
3600 } else {
3601 /**
3602 * More typical, no trigger entry, so we just add it
3603 */
3604 mTriggerRemovedMap.add(tag, trigger);
3605 }
3606
3607 status_t res;
3608
3609 switch (trigger.getTagType()) {
3610 case TYPE_BYTE: {
3611 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3612 res = metadata.update(tag,
3613 &entryValue,
3614 /*count*/1);
3615 break;
3616 }
3617 case TYPE_INT32:
3618 res = metadata.update(tag,
3619 &trigger.entryValue,
3620 /*count*/1);
3621 break;
3622 default:
3623 ALOGE("%s: Type not supported: 0x%x",
3624 __FUNCTION__,
3625 trigger.getTagType());
3626 return INVALID_OPERATION;
3627 }
3628
3629 if (res != OK) {
3630 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3631 ", value %d", __FUNCTION__, trigger.getTagName(),
3632 trigger.entryValue);
3633 return res;
3634 }
3635
3636 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3637 trigger.getTagName(),
3638 trigger.entryValue);
3639 }
3640
3641 mTriggerMap.clear();
3642
3643 return count;
3644}
3645
3646status_t Camera3Device::RequestThread::removeTriggers(
3647 const sp<CaptureRequest> &request) {
3648 Mutex::Autolock al(mTriggerMutex);
3649
3650 CameraMetadata &metadata = request->mSettings;
3651
3652 /**
3653 * Replace all old entries with their old values.
3654 */
3655 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3656 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3657
3658 status_t res;
3659
3660 uint32_t tag = trigger.metadataTag;
3661 switch (trigger.getTagType()) {
3662 case TYPE_BYTE: {
3663 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3664 res = metadata.update(tag,
3665 &entryValue,
3666 /*count*/1);
3667 break;
3668 }
3669 case TYPE_INT32:
3670 res = metadata.update(tag,
3671 &trigger.entryValue,
3672 /*count*/1);
3673 break;
3674 default:
3675 ALOGE("%s: Type not supported: 0x%x",
3676 __FUNCTION__,
3677 trigger.getTagType());
3678 return INVALID_OPERATION;
3679 }
3680
3681 if (res != OK) {
3682 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3683 ", trigger value %d", __FUNCTION__,
3684 trigger.getTagName(), trigger.entryValue);
3685 return res;
3686 }
3687 }
3688 mTriggerReplacedMap.clear();
3689
3690 /**
3691 * Remove all new entries.
3692 */
3693 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3694 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3695 status_t res = metadata.erase(trigger.metadataTag);
3696
3697 if (res != OK) {
3698 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3699 ", trigger value %d", __FUNCTION__,
3700 trigger.getTagName(), trigger.entryValue);
3701 return res;
3702 }
3703 }
3704 mTriggerRemovedMap.clear();
3705
3706 return OK;
3707}
3708
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003709status_t Camera3Device::RequestThread::addDummyTriggerIds(
3710 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003711 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003712 static const int32_t dummyTriggerId = 1;
3713 status_t res;
3714
3715 CameraMetadata &metadata = request->mSettings;
3716
3717 // If AF trigger is active, insert a dummy AF trigger ID if none already
3718 // exists
3719 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3720 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3721 if (afTrigger.count > 0 &&
3722 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3723 afId.count == 0) {
3724 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3725 if (res != OK) return res;
3726 }
3727
3728 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3729 // if none already exists
3730 camera_metadata_entry pcTrigger =
3731 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3732 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3733 if (pcTrigger.count > 0 &&
3734 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3735 pcId.count == 0) {
3736 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3737 &dummyTriggerId, 1);
3738 if (res != OK) return res;
3739 }
3740
3741 return OK;
3742}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003743
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003744/**
3745 * PreparerThread inner class methods
3746 */
3747
3748Camera3Device::PreparerThread::PreparerThread() :
3749 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3750}
3751
3752Camera3Device::PreparerThread::~PreparerThread() {
3753 Thread::requestExitAndWait();
3754 if (mCurrentStream != nullptr) {
3755 mCurrentStream->cancelPrepare();
3756 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3757 mCurrentStream.clear();
3758 }
3759 clear();
3760}
3761
Ruben Brunkc78ac262015-08-13 17:58:46 -07003762status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003763 status_t res;
3764
3765 Mutex::Autolock l(mLock);
3766
Ruben Brunkc78ac262015-08-13 17:58:46 -07003767 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003768 if (res == OK) {
3769 // No preparation needed, fire listener right off
3770 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3771 if (mListener) {
3772 mListener->notifyPrepared(stream->getId());
3773 }
3774 return OK;
3775 } else if (res != NOT_ENOUGH_DATA) {
3776 return res;
3777 }
3778
3779 // Need to prepare, start up thread if necessary
3780 if (!mActive) {
3781 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3782 // isn't running
3783 Thread::requestExitAndWait();
3784 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3785 if (res != OK) {
3786 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3787 if (mListener) {
3788 mListener->notifyPrepared(stream->getId());
3789 }
3790 return res;
3791 }
3792 mCancelNow = false;
3793 mActive = true;
3794 ALOGV("%s: Preparer stream started", __FUNCTION__);
3795 }
3796
3797 // queue up the work
3798 mPendingStreams.push_back(stream);
3799 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3800
3801 return OK;
3802}
3803
3804status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003805 Mutex::Autolock l(mLock);
3806
3807 for (const auto& stream : mPendingStreams) {
3808 stream->cancelPrepare();
3809 }
3810 mPendingStreams.clear();
3811 mCancelNow = true;
3812
3813 return OK;
3814}
3815
3816void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3817 Mutex::Autolock l(mLock);
3818 mListener = listener;
3819}
3820
3821bool Camera3Device::PreparerThread::threadLoop() {
3822 status_t res;
3823 {
3824 Mutex::Autolock l(mLock);
3825 if (mCurrentStream == nullptr) {
3826 // End thread if done with work
3827 if (mPendingStreams.empty()) {
3828 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3829 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3830 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3831 mActive = false;
3832 return false;
3833 }
3834
3835 // Get next stream to prepare
3836 auto it = mPendingStreams.begin();
3837 mCurrentStream = *it;
3838 mPendingStreams.erase(it);
3839 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3840 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3841 } else if (mCancelNow) {
3842 mCurrentStream->cancelPrepare();
3843 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3844 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3845 mCurrentStream.clear();
3846 mCancelNow = false;
3847 return true;
3848 }
3849 }
3850
3851 res = mCurrentStream->prepareNextBuffer();
3852 if (res == NOT_ENOUGH_DATA) return true;
3853 if (res != OK) {
3854 // Something bad happened; try to recover by cancelling prepare and
3855 // signalling listener anyway
3856 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3857 mCurrentStream->getId(), res, strerror(-res));
3858 mCurrentStream->cancelPrepare();
3859 }
3860
3861 // This stream has finished, notify listener
3862 Mutex::Autolock l(mLock);
3863 if (mListener) {
3864 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3865 mCurrentStream->getId());
3866 mListener->notifyPrepared(mCurrentStream->getId());
3867 }
3868
3869 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3870 mCurrentStream.clear();
3871
3872 return true;
3873}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003874
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003875/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003876 * Static callback forwarding methods from HAL to instance
3877 */
3878
3879void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3880 const camera3_capture_result *result) {
3881 Camera3Device *d =
3882 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003883
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003884 d->processCaptureResult(result);
3885}
3886
3887void Camera3Device::sNotify(const camera3_callback_ops *cb,
3888 const camera3_notify_msg *msg) {
3889 Camera3Device *d =
3890 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3891 d->notify(msg);
3892}
3893
3894}; // namespace android