blob: dfbe200911b74b9be184c1be360c90c570d61f48 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046#include <android/hardware/camera2/ICameraDeviceUser.h>
47
Igor Murashkinff3e31d2013-10-23 16:40:06 -070048#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070049#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070050#include "device3/Camera3Device.h"
51#include "device3/Camera3OutputStream.h"
52#include "device3/Camera3InputStream.h"
53#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070054#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070055#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080056
57using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058
59namespace android {
60
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061Camera3Device::Camera3Device(int id):
62 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070063 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080064 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070065 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070066 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070067 mUsePartialResult(false),
68 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080069 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070070 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070071 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070072 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070073 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070074 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080075{
76 ATRACE_CALL();
77 camera3_callback_ops::notify = &sNotify;
78 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
79 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
80}
81
82Camera3Device::~Camera3Device()
83{
84 ATRACE_CALL();
85 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
86 disconnect();
87}
88
Igor Murashkin71381052013-03-04 14:53:08 -080089int Camera3Device::getId() const {
90 return mId;
91}
92
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080093/**
94 * CameraDeviceBase interface
95 */
96
Yin-Chia Yehe074a932015-01-30 10:29:02 -080097status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080098{
99 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700100 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800101 Mutex::Autolock l(mLock);
102
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800104 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700105 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800106 return INVALID_OPERATION;
107 }
108
109 /** Open HAL device */
110
111 status_t res;
112 String8 deviceName = String8::format("%d", mId);
113
114 camera3_device_t *device;
115
Zhijun He213ce792013-11-19 08:45:15 -0800116 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800117 res = module->open(deviceName.string(),
118 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800119 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800120
121 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800123 return res;
124 }
125
126 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700127 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700128 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700129 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700130 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800131 device->common.version);
132 device->common.close(&device->common);
133 return BAD_VALUE;
134 }
135
136 camera_info info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800137 res = module->getCameraInfo(mId, &info);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 if (res != OK) return res;
139
140 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700141 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
142 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700143 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800144 device->common.close(&device->common);
145 return BAD_VALUE;
146 }
147
148 /** Initialize device with callback functions */
149
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700150 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700152 ATRACE_END();
153
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800154 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700155 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
156 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800157 device->common.close(&device->common);
158 return BAD_VALUE;
159 }
160
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700161 /** Start up status tracker thread */
162 mStatusTracker = new StatusTracker(this);
163 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
164 if (res != OK) {
165 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
166 strerror(-res), res);
167 device->common.close(&device->common);
168 mStatusTracker.clear();
169 return res;
170 }
171
Zhijun He125684a2015-12-26 15:07:30 -0800172 /** Create buffer manager */
173 mBufferManager = new Camera3BufferManager();
174
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700175 bool aeLockAvailable = false;
176 camera_metadata_ro_entry aeLockAvailableEntry;
177 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
178 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
179 if (res == OK && aeLockAvailableEntry.count > 0) {
180 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
181 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
182 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800183
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700184 /** Start up request queue thread */
185 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800186 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700188 SET_ERR_L("Unable to start request queue thread: %s (%d)",
189 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800191 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 return res;
193 }
194
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700195 mPreparerThread = new PreparerThread();
196
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800197 /** Everything is good to go */
198
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700199 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800200 mDeviceInfo = info.static_camera_characteristics;
201 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700202
203 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800204 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700205 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700206 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700207 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800208
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800209 // Measure the clock domain offset between camera and video/hw_composer
210 camera_metadata_entry timestampSource =
211 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
212 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
213 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
214 mTimestampOffset = getMonoToBoottimeOffset();
215 }
216
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700217 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700218 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
219 camera_metadata_entry partialResultsCount =
220 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
221 if (partialResultsCount.count > 0) {
222 mNumPartialResults = partialResultsCount.data.i32[0];
223 mUsePartialResult = (mNumPartialResults > 1);
224 }
225 } else {
226 camera_metadata_entry partialResultsQuirk =
227 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
228 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
229 mUsePartialResult = true;
230 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700231 }
232
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700233 camera_metadata_entry configs =
234 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
235 for (uint32_t i = 0; i < configs.count; i += 4) {
236 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
237 configs.data.i32[i + 3] ==
238 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
239 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
240 configs.data.i32[i + 2]));
241 }
242 }
243
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800244 return OK;
245}
246
247status_t Camera3Device::disconnect() {
248 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700249 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800250
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800251 ALOGV("%s: E", __FUNCTION__);
252
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700253 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800254
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700255 {
256 Mutex::Autolock l(mLock);
257 if (mStatus == STATUS_UNINITIALIZED) return res;
258
259 if (mStatus == STATUS_ACTIVE ||
260 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
261 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700262 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700263 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700264 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700265 } else {
266 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
267 if (res != OK) {
268 SET_ERR_L("Timeout waiting for HAL to drain");
269 // Continue to close device even in case of error
270 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700271 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800272 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800273
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700274 if (mStatus == STATUS_ERROR) {
275 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700276 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277
278 if (mStatusTracker != NULL) {
279 mStatusTracker->requestExit();
280 }
281
282 if (mRequestThread != NULL) {
283 mRequestThread->requestExit();
284 }
285
286 mOutputStreams.clear();
287 mInputStream.clear();
288 }
289
290 // Joining done without holding mLock, otherwise deadlocks may ensue
291 // as the threads try to access parent state
292 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
293 // HAL may be in a bad state, so waiting for request thread
294 // (which may be stuck in the HAL processCaptureRequest call)
295 // could be dangerous.
296 mRequestThread->join();
297 }
298
299 if (mStatusTracker != NULL) {
300 mStatusTracker->join();
301 }
302
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700303 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700304 {
305 Mutex::Autolock l(mLock);
306
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800307 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700308 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800309 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800310
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700311 hal3Device = mHal3Device;
312 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800313
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700314 // Call close without internal mutex held, as the HAL close may need to
315 // wait on assorted callbacks,etc, to complete before it can return.
316 if (hal3Device != NULL) {
317 ATRACE_BEGIN("camera3->close");
318 hal3Device->common.close(&hal3Device->common);
319 ATRACE_END();
320 }
321
322 {
323 Mutex::Autolock l(mLock);
324 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700325 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700326 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800327
328 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700329 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800330}
331
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700332// For dumping/debugging only -
333// try to acquire a lock a few times, eventually give up to proceed with
334// debug/dump operations
335bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
336 bool gotLock = false;
337 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
338 if (lock.tryLock() == NO_ERROR) {
339 gotLock = true;
340 break;
341 } else {
342 usleep(kDumpSleepDuration);
343 }
344 }
345 return gotLock;
346}
347
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700348Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
349 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
350 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
351 const int STREAM_CONFIGURATION_SIZE = 4;
352 const int STREAM_FORMAT_OFFSET = 0;
353 const int STREAM_WIDTH_OFFSET = 1;
354 const int STREAM_HEIGHT_OFFSET = 2;
355 const int STREAM_IS_INPUT_OFFSET = 3;
356 camera_metadata_ro_entry_t availableStreamConfigs =
357 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
358 if (availableStreamConfigs.count == 0 ||
359 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
360 return Size(0, 0);
361 }
362
363 // Get max jpeg size (area-wise).
364 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
365 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
366 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
367 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
368 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
369 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
370 && format == HAL_PIXEL_FORMAT_BLOB &&
371 (width * height > maxJpegWidth * maxJpegHeight)) {
372 maxJpegWidth = width;
373 maxJpegHeight = height;
374 }
375 }
376 } else {
377 camera_metadata_ro_entry availableJpegSizes =
378 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
379 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
380 return Size(0, 0);
381 }
382
383 // Get max jpeg size (area-wise).
384 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
385 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
386 > (maxJpegWidth * maxJpegHeight)) {
387 maxJpegWidth = availableJpegSizes.data.i32[i];
388 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
389 }
390 }
391 }
392 return Size(maxJpegWidth, maxJpegHeight);
393}
394
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800395nsecs_t Camera3Device::getMonoToBoottimeOffset() {
396 // try three times to get the clock offset, choose the one
397 // with the minimum gap in measurements.
398 const int tries = 3;
399 nsecs_t bestGap, measured;
400 for (int i = 0; i < tries; ++i) {
401 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
402 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
403 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
404 const nsecs_t gap = tmono2 - tmono;
405 if (i == 0 || gap < bestGap) {
406 bestGap = gap;
407 measured = tbase - ((tmono + tmono2) >> 1);
408 }
409 }
410 return measured;
411}
412
Zhijun Hef7da0962014-04-24 13:27:56 -0700413ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700414 // Get max jpeg size (area-wise).
415 Size maxJpegResolution = getMaxJpegResolution();
416 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700417 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700418 __FUNCTION__, mId);
419 return BAD_VALUE;
420 }
421
Zhijun Hef7da0962014-04-24 13:27:56 -0700422 // Get max jpeg buffer size
423 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700424 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
425 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700426 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
427 return BAD_VALUE;
428 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700429 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800430 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700431
432 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700433 float scaleFactor = ((float) (width * height)) /
434 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800435 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
436 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700437 if (jpegBufferSize > maxJpegBufferSize) {
438 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700439 }
440
441 return jpegBufferSize;
442}
443
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700444ssize_t Camera3Device::getPointCloudBufferSize() const {
445 const int FLOATS_PER_POINT=4;
446 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
447 if (maxPointCount.count == 0) {
448 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
449 __FUNCTION__, mId);
450 return BAD_VALUE;
451 }
452 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
453 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
454 return maxBytesForPointCloud;
455}
456
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800457ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800458 const int PER_CONFIGURATION_SIZE = 3;
459 const int WIDTH_OFFSET = 0;
460 const int HEIGHT_OFFSET = 1;
461 const int SIZE_OFFSET = 2;
462 camera_metadata_ro_entry rawOpaqueSizes =
463 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800464 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800465 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800466 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800467 __FUNCTION__, mId, count);
468 return BAD_VALUE;
469 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700470
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800471 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
472 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
473 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
474 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
475 }
476 }
477
478 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
479 __FUNCTION__, mId, width, height);
480 return BAD_VALUE;
481}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700482
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800483status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
484 ATRACE_CALL();
485 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700486
487 // Try to lock, but continue in case of failure (to avoid blocking in
488 // deadlocks)
489 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
490 bool gotLock = tryLockSpinRightRound(mLock);
491
492 ALOGW_IF(!gotInterfaceLock,
493 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
494 mId, __FUNCTION__);
495 ALOGW_IF(!gotLock,
496 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
497 mId, __FUNCTION__);
498
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800499 bool dumpTemplates = false;
500 String16 templatesOption("-t");
501 int n = args.size();
502 for (int i = 0; i < n; i++) {
503 if (args[i] == templatesOption) {
504 dumpTemplates = true;
505 }
506 }
507
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800508 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800509
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800510 const char *status =
511 mStatus == STATUS_ERROR ? "ERROR" :
512 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700513 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
514 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800515 mStatus == STATUS_ACTIVE ? "ACTIVE" :
516 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700517
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800518 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700519 if (mStatus == STATUS_ERROR) {
520 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
521 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800522 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700523 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700524 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800525
526 if (mInputStream != NULL) {
527 write(fd, lines.string(), lines.size());
528 mInputStream->dump(fd, args);
529 } else {
530 lines.appendFormat(" No input stream.\n");
531 write(fd, lines.string(), lines.size());
532 }
533 for (size_t i = 0; i < mOutputStreams.size(); i++) {
534 mOutputStreams[i]->dump(fd,args);
535 }
536
Zhijun He125684a2015-12-26 15:07:30 -0800537 lines = String8(" Camera3 Buffer Manager:\n");
538 write(fd, lines.string(), lines.size());
539 mBufferManager->dump(fd, args);
540
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700541 lines = String8(" In-flight requests:\n");
542 if (mInFlightMap.size() == 0) {
543 lines.append(" None\n");
544 } else {
545 for (size_t i = 0; i < mInFlightMap.size(); i++) {
546 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700547 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700548 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800549 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700550 r.numBuffersLeft);
551 }
552 }
553 write(fd, lines.string(), lines.size());
554
Igor Murashkin1e479c02013-09-06 16:55:14 -0700555 {
556 lines = String8(" Last request sent:\n");
557 write(fd, lines.string(), lines.size());
558
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700559 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700560 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
561 }
562
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800563 if (dumpTemplates) {
564 const char *templateNames[] = {
565 "TEMPLATE_PREVIEW",
566 "TEMPLATE_STILL_CAPTURE",
567 "TEMPLATE_VIDEO_RECORD",
568 "TEMPLATE_VIDEO_SNAPSHOT",
569 "TEMPLATE_ZERO_SHUTTER_LAG",
570 "TEMPLATE_MANUAL"
571 };
572
573 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
574 const camera_metadata_t *templateRequest;
575 templateRequest =
576 mHal3Device->ops->construct_default_request_settings(
577 mHal3Device, i);
578 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
579 if (templateRequest == NULL) {
580 lines.append(" Not supported\n");
581 write(fd, lines.string(), lines.size());
582 } else {
583 write(fd, lines.string(), lines.size());
584 dump_indented_camera_metadata(templateRequest,
585 fd, /*verbosity*/2, /*indentation*/8);
586 }
587 }
588 }
589
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800590 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700591 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800592 write(fd, lines.string(), lines.size());
593 mHal3Device->ops->dump(mHal3Device, fd);
594 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800595
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700596 if (gotLock) mLock.unlock();
597 if (gotInterfaceLock) mInterfaceLock.unlock();
598
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800599 return OK;
600}
601
602const CameraMetadata& Camera3Device::info() const {
603 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800604 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
605 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700606 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800607 mStatus == STATUS_ERROR ?
608 "when in error state" : "before init");
609 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800610 return mDeviceInfo;
611}
612
Jianing Wei90e59c92014-03-12 18:29:36 -0700613status_t Camera3Device::checkStatusOkToCaptureLocked() {
614 switch (mStatus) {
615 case STATUS_ERROR:
616 CLOGE("Device has encountered a serious error");
617 return INVALID_OPERATION;
618 case STATUS_UNINITIALIZED:
619 CLOGE("Device not initialized");
620 return INVALID_OPERATION;
621 case STATUS_UNCONFIGURED:
622 case STATUS_CONFIGURED:
623 case STATUS_ACTIVE:
624 // OK
625 break;
626 default:
627 SET_ERR_L("Unexpected status: %d", mStatus);
628 return INVALID_OPERATION;
629 }
630 return OK;
631}
632
633status_t Camera3Device::convertMetadataListToRequestListLocked(
634 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
635 if (requestList == NULL) {
636 CLOGE("requestList cannot be NULL.");
637 return BAD_VALUE;
638 }
639
Jianing Weicb0652e2014-03-12 18:29:36 -0700640 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700641 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
642 it != metadataList.end(); ++it) {
643 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
644 if (newRequest == 0) {
645 CLOGE("Can't create capture request");
646 return BAD_VALUE;
647 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700648
649 // Setup burst Id and request Id
650 newRequest->mResultExtras.burstId = burstId++;
651 if (it->exists(ANDROID_REQUEST_ID)) {
652 if (it->find(ANDROID_REQUEST_ID).count == 0) {
653 CLOGE("RequestID entry exists; but must not be empty in metadata");
654 return BAD_VALUE;
655 }
656 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
657 } else {
658 CLOGE("RequestID does not exist in metadata");
659 return BAD_VALUE;
660 }
661
Jianing Wei90e59c92014-03-12 18:29:36 -0700662 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700663
664 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700665 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700666
667 // Setup batch size if this is a high speed video recording request.
668 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
669 auto firstRequest = requestList->begin();
670 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
671 if (outputStream->isVideoStream()) {
672 (*firstRequest)->mBatchSize = requestList->size();
673 break;
674 }
675 }
676 }
677
Jianing Wei90e59c92014-03-12 18:29:36 -0700678 return OK;
679}
680
Jianing Weicb0652e2014-03-12 18:29:36 -0700681status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800682 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800683
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700684 List<const CameraMetadata> requests;
685 requests.push_back(request);
686 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800687}
688
Jianing Wei90e59c92014-03-12 18:29:36 -0700689status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700690 const List<const CameraMetadata> &requests, bool repeating,
691 /*out*/
692 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700693 ATRACE_CALL();
694 Mutex::Autolock il(mInterfaceLock);
695 Mutex::Autolock l(mLock);
696
697 status_t res = checkStatusOkToCaptureLocked();
698 if (res != OK) {
699 // error logged by previous call
700 return res;
701 }
702
703 RequestList requestList;
704
705 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
706 if (res != OK) {
707 // error logged by previous call
708 return res;
709 }
710
711 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700712 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700713 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700714 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700715 }
716
717 if (res == OK) {
718 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
719 if (res != OK) {
720 SET_ERR_L("Can't transition to active in %f seconds!",
721 kActiveTimeout/1e9);
722 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700723 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
724 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700725 } else {
726 CLOGE("Cannot queue request. Impossible.");
727 return BAD_VALUE;
728 }
729
730 return res;
731}
732
Jianing Weicb0652e2014-03-12 18:29:36 -0700733status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
734 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700735 ATRACE_CALL();
736
Jianing Weicb0652e2014-03-12 18:29:36 -0700737 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700738}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800739
Jianing Weicb0652e2014-03-12 18:29:36 -0700740status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
741 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800742 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800743
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700744 List<const CameraMetadata> requests;
745 requests.push_back(request);
746 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800747}
748
Jianing Weicb0652e2014-03-12 18:29:36 -0700749status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
750 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700751 ATRACE_CALL();
752
Jianing Weicb0652e2014-03-12 18:29:36 -0700753 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700754}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800755
756sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
757 const CameraMetadata &request) {
758 status_t res;
759
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700760 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800761 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700762 // Stream configuration failed due to unsupported configuration.
763 // Device back to unconfigured state. Client might try other configuraitons
764 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
765 CLOGE("No streams configured");
766 return NULL;
767 }
768 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800769 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700770 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800771 return NULL;
772 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700773 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700774 if (mStatus == STATUS_UNCONFIGURED) {
775 CLOGE("No streams configured");
776 return NULL;
777 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800778 }
779
780 sp<CaptureRequest> newRequest = createCaptureRequest(request);
781 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800782}
783
Jianing Weicb0652e2014-03-12 18:29:36 -0700784status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800785 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700786 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800787 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800788
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800789 switch (mStatus) {
790 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700791 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800792 return INVALID_OPERATION;
793 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700794 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800795 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700796 case STATUS_UNCONFIGURED:
797 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800798 case STATUS_ACTIVE:
799 // OK
800 break;
801 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700802 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800803 return INVALID_OPERATION;
804 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700805 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700806
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700807 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800808}
809
810status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
811 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700812 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800813
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700814 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800815}
816
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700817status_t Camera3Device::createInputStream(
818 uint32_t width, uint32_t height, int format, int *id) {
819 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700820 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700821 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700822 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
823 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700824
825 status_t res;
826 bool wasActive = false;
827
828 switch (mStatus) {
829 case STATUS_ERROR:
830 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
831 return INVALID_OPERATION;
832 case STATUS_UNINITIALIZED:
833 ALOGE("%s: Device not initialized", __FUNCTION__);
834 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700835 case STATUS_UNCONFIGURED:
836 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700837 // OK
838 break;
839 case STATUS_ACTIVE:
840 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700841 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700842 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700843 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700844 return res;
845 }
846 wasActive = true;
847 break;
848 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700849 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700850 return INVALID_OPERATION;
851 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700852 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700853
854 if (mInputStream != 0) {
855 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
856 return INVALID_OPERATION;
857 }
858
859 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
860 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700861 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700862
863 mInputStream = newStream;
864
865 *id = mNextStreamId++;
866
867 // Continue captures if active at start
868 if (wasActive) {
869 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
870 res = configureStreamsLocked();
871 if (res != OK) {
872 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
873 __FUNCTION__, mNextStreamId, strerror(-res), res);
874 return res;
875 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700876 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700877 }
878
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700879 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700880 return OK;
881}
882
Igor Murashkin2fba5842013-04-22 14:03:54 -0700883
884status_t Camera3Device::createZslStream(
885 uint32_t width, uint32_t height,
886 int depth,
887 /*out*/
888 int *id,
889 sp<Camera3ZslStream>* zslStream) {
890 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700891 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700892 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700893 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
894 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700895
896 status_t res;
897 bool wasActive = false;
898
899 switch (mStatus) {
900 case STATUS_ERROR:
901 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
902 return INVALID_OPERATION;
903 case STATUS_UNINITIALIZED:
904 ALOGE("%s: Device not initialized", __FUNCTION__);
905 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700906 case STATUS_UNCONFIGURED:
907 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700908 // OK
909 break;
910 case STATUS_ACTIVE:
911 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700912 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700913 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700914 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700915 return res;
916 }
917 wasActive = true;
918 break;
919 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700920 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700921 return INVALID_OPERATION;
922 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700923 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700924
925 if (mInputStream != 0) {
926 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
927 return INVALID_OPERATION;
928 }
929
930 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
931 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700932 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700933
934 res = mOutputStreams.add(mNextStreamId, newStream);
935 if (res < 0) {
936 ALOGE("%s: Can't add new stream to set: %s (%d)",
937 __FUNCTION__, strerror(-res), res);
938 return res;
939 }
940 mInputStream = newStream;
941
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530942 mNeedConfig = true;
943
Igor Murashkin2fba5842013-04-22 14:03:54 -0700944 *id = mNextStreamId++;
945 *zslStream = newStream;
946
947 // Continue captures if active at start
948 if (wasActive) {
949 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
950 res = configureStreamsLocked();
951 if (res != OK) {
952 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
953 __FUNCTION__, mNextStreamId, strerror(-res), res);
954 return res;
955 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700956 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700957 }
958
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700959 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700960 return OK;
961}
962
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700963status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800964 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -0800965 camera3_stream_rotation_t rotation, int *id, int streamSetId) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800966 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700967 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800968 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700969 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
970 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800971
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800972 status_t res;
973 bool wasActive = false;
974
975 switch (mStatus) {
976 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700977 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800978 return INVALID_OPERATION;
979 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700980 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800981 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700982 case STATUS_UNCONFIGURED:
983 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800984 // OK
985 break;
986 case STATUS_ACTIVE:
987 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700988 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800989 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700990 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800991 return res;
992 }
993 wasActive = true;
994 break;
995 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700996 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800997 return INVALID_OPERATION;
998 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700999 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001000
1001 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001002 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001003 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001004 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001005 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1006 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001007 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001008 ssize_t blobBufferSize;
1009 if (dataSpace != HAL_DATASPACE_DEPTH) {
1010 blobBufferSize = getJpegBufferSize(width, height);
1011 if (blobBufferSize <= 0) {
1012 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1013 return BAD_VALUE;
1014 }
1015 } else {
1016 blobBufferSize = getPointCloudBufferSize();
1017 if (blobBufferSize <= 0) {
1018 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1019 return BAD_VALUE;
1020 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001021 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001022 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001023 width, height, blobBufferSize, format, dataSpace, rotation,
1024 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001025 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1026 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1027 if (rawOpaqueBufferSize <= 0) {
1028 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1029 return BAD_VALUE;
1030 }
1031 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001032 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1033 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001034 } else {
1035 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001036 width, height, format, dataSpace, rotation,
1037 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001038 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001039 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001040
Zhijun He125684a2015-12-26 15:07:30 -08001041 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001042 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1043 * requires buffers to be statically allocated for internal static buffer registration, while
1044 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1045 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001046 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001047 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001048 newStream->setBufferManager(mBufferManager);
1049 }
1050
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001051 res = mOutputStreams.add(mNextStreamId, newStream);
1052 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001053 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001054 return res;
1055 }
1056
1057 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001058 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001059
1060 // Continue captures if active at start
1061 if (wasActive) {
1062 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1063 res = configureStreamsLocked();
1064 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001065 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1066 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001067 return res;
1068 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001069 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001070 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001071 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001072 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001073}
1074
1075status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1076 ATRACE_CALL();
1077 (void)outputId; (void)id;
1078
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001079 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001080 return INVALID_OPERATION;
1081}
1082
1083
1084status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001085 uint32_t *width, uint32_t *height,
1086 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001087 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001088 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001089 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001090
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001091 switch (mStatus) {
1092 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001093 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001094 return INVALID_OPERATION;
1095 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001096 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001097 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001098 case STATUS_UNCONFIGURED:
1099 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001100 case STATUS_ACTIVE:
1101 // OK
1102 break;
1103 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001104 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001105 return INVALID_OPERATION;
1106 }
1107
1108 ssize_t idx = mOutputStreams.indexOfKey(id);
1109 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001110 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001111 return idx;
1112 }
1113
1114 if (width) *width = mOutputStreams[idx]->getWidth();
1115 if (height) *height = mOutputStreams[idx]->getHeight();
1116 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001117 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001118 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001119}
1120
1121status_t Camera3Device::setStreamTransform(int id,
1122 int transform) {
1123 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001124 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001125 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001126
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001127 switch (mStatus) {
1128 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001129 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001130 return INVALID_OPERATION;
1131 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001132 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001133 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001134 case STATUS_UNCONFIGURED:
1135 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001136 case STATUS_ACTIVE:
1137 // OK
1138 break;
1139 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001140 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141 return INVALID_OPERATION;
1142 }
1143
1144 ssize_t idx = mOutputStreams.indexOfKey(id);
1145 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001146 CLOGE("Stream %d does not exist",
1147 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001148 return BAD_VALUE;
1149 }
1150
1151 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001152}
1153
1154status_t Camera3Device::deleteStream(int id) {
1155 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001156 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001157 Mutex::Autolock l(mLock);
1158 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001159
Igor Murashkine2172be2013-05-28 15:31:39 -07001160 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1161
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001162 // CameraDevice semantics require device to already be idle before
1163 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001164 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001165 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1166 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001167 }
1168
Igor Murashkin2fba5842013-04-22 14:03:54 -07001169 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001170 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001171 if (mInputStream != NULL && id == mInputStream->getId()) {
1172 deletedStream = mInputStream;
1173 mInputStream.clear();
1174 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001175 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001176 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177 return BAD_VALUE;
1178 }
Zhijun He5f446352014-01-22 09:49:33 -08001179 }
1180
1181 // Delete output stream or the output part of a bi-directional stream.
1182 if (outputStreamIdx != NAME_NOT_FOUND) {
1183 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001184 mOutputStreams.removeItem(id);
1185 }
1186
1187 // Free up the stream endpoint so that it can be used by some other stream
1188 res = deletedStream->disconnect();
1189 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001190 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001191 // fall through since we want to still list the stream as deleted.
1192 }
1193 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001194 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001195
1196 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001197}
1198
1199status_t Camera3Device::deleteReprocessStream(int id) {
1200 ATRACE_CALL();
1201 (void)id;
1202
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001203 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001204 return INVALID_OPERATION;
1205}
1206
Zhijun He1fa89992015-06-01 15:44:31 -07001207status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001208 ATRACE_CALL();
1209 ALOGV("%s: E", __FUNCTION__);
1210
1211 Mutex::Autolock il(mInterfaceLock);
1212 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001213
1214 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1215 mNeedConfig = true;
1216 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1217 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001218
1219 return configureStreamsLocked();
1220}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001221
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001222status_t Camera3Device::getInputBufferProducer(
1223 sp<IGraphicBufferProducer> *producer) {
1224 Mutex::Autolock il(mInterfaceLock);
1225 Mutex::Autolock l(mLock);
1226
1227 if (producer == NULL) {
1228 return BAD_VALUE;
1229 } else if (mInputStream == NULL) {
1230 return INVALID_OPERATION;
1231 }
1232
1233 return mInputStream->getInputBufferProducer(producer);
1234}
1235
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001236status_t Camera3Device::createDefaultRequest(int templateId,
1237 CameraMetadata *request) {
1238 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001239 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001240
1241 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1242 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1243 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1244 return BAD_VALUE;
1245 }
1246
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001247 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001248 Mutex::Autolock l(mLock);
1249
1250 switch (mStatus) {
1251 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001252 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001253 return INVALID_OPERATION;
1254 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001255 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001256 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001257 case STATUS_UNCONFIGURED:
1258 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001259 case STATUS_ACTIVE:
1260 // OK
1261 break;
1262 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001263 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001264 return INVALID_OPERATION;
1265 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001266
Zhijun Hea1530f12014-09-14 12:44:20 -07001267 if (!mRequestTemplateCache[templateId].isEmpty()) {
1268 *request = mRequestTemplateCache[templateId];
1269 return OK;
1270 }
1271
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001272 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001273 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001274 rawRequest = mHal3Device->ops->construct_default_request_settings(
1275 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001276 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001277 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001278 ALOGI("%s: template %d is not supported on this camera device",
1279 __FUNCTION__, templateId);
1280 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001281 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001282 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001283 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001284
1285 return OK;
1286}
1287
1288status_t Camera3Device::waitUntilDrained() {
1289 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001290 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001291 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001292
Zhijun He69a37482014-03-23 18:44:49 -07001293 return waitUntilDrainedLocked();
1294}
1295
1296status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001297 switch (mStatus) {
1298 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001299 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001300 ALOGV("%s: Already idle", __FUNCTION__);
1301 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001302 case STATUS_CONFIGURED:
1303 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001304 case STATUS_ERROR:
1305 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001306 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001307 break;
1308 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001309 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001310 return INVALID_OPERATION;
1311 }
1312
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001313 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1314 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001315 if (res != OK) {
1316 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1317 res);
1318 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001319 return res;
1320}
1321
Ruben Brunk183f0562015-08-12 12:55:02 -07001322
1323void Camera3Device::internalUpdateStatusLocked(Status status) {
1324 mStatus = status;
1325 mRecentStatusUpdates.add(mStatus);
1326 mStatusChanged.broadcast();
1327}
1328
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001329// Pause to reconfigure
1330status_t Camera3Device::internalPauseAndWaitLocked() {
1331 mRequestThread->setPaused(true);
1332 mPauseStateNotify = true;
1333
1334 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1335 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1336 if (res != OK) {
1337 SET_ERR_L("Can't idle device in %f seconds!",
1338 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001339 }
1340
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001341 return res;
1342}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001343
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001344// Resume after internalPauseAndWaitLocked
1345status_t Camera3Device::internalResumeLocked() {
1346 status_t res;
1347
1348 mRequestThread->setPaused(false);
1349
1350 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1351 if (res != OK) {
1352 SET_ERR_L("Can't transition to active in %f seconds!",
1353 kActiveTimeout/1e9);
1354 }
1355 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001356 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001357}
1358
Ruben Brunk183f0562015-08-12 12:55:02 -07001359status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001360 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001361
1362 size_t startIndex = 0;
1363 if (mStatusWaiters == 0) {
1364 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1365 // this status list
1366 mRecentStatusUpdates.clear();
1367 } else {
1368 // If other threads are waiting on updates to this status list, set the position of the
1369 // first element that this list will check rather than clearing the list.
1370 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001371 }
1372
Ruben Brunk183f0562015-08-12 12:55:02 -07001373 mStatusWaiters++;
1374
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001375 bool stateSeen = false;
1376 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001377 if (active == (mStatus == STATUS_ACTIVE)) {
1378 // Desired state is current
1379 break;
1380 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001381
1382 res = mStatusChanged.waitRelative(mLock, timeout);
1383 if (res != OK) break;
1384
Ruben Brunk183f0562015-08-12 12:55:02 -07001385 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1386 // transitions.
1387 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1388 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1389 __FUNCTION__);
1390
1391 // Encountered desired state since we began waiting
1392 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001393 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1394 stateSeen = true;
1395 break;
1396 }
1397 }
1398 } while (!stateSeen);
1399
Ruben Brunk183f0562015-08-12 12:55:02 -07001400 mStatusWaiters--;
1401
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001402 return res;
1403}
1404
1405
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001406status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1407 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001408 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001409
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001410 if (listener != NULL && mListener != NULL) {
1411 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1412 }
1413 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001414 mRequestThread->setNotificationListener(listener);
1415 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001416
1417 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001418}
1419
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001420bool Camera3Device::willNotify3A() {
1421 return false;
1422}
1423
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001424status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001425 status_t res;
1426 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001427
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001428 while (mResultQueue.empty()) {
1429 res = mResultSignal.waitRelative(mOutputLock, timeout);
1430 if (res == TIMED_OUT) {
1431 return res;
1432 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001433 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001434 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001435 return res;
1436 }
1437 }
1438 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001439}
1440
Jianing Weicb0652e2014-03-12 18:29:36 -07001441status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001442 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001443 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001444
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001445 if (mResultQueue.empty()) {
1446 return NOT_ENOUGH_DATA;
1447 }
1448
Jianing Weicb0652e2014-03-12 18:29:36 -07001449 if (frame == NULL) {
1450 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1451 return BAD_VALUE;
1452 }
1453
1454 CaptureResult &result = *(mResultQueue.begin());
1455 frame->mResultExtras = result.mResultExtras;
1456 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001457 mResultQueue.erase(mResultQueue.begin());
1458
1459 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001460}
1461
1462status_t Camera3Device::triggerAutofocus(uint32_t id) {
1463 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001464 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001465
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001466 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1467 // Mix-in this trigger into the next request and only the next request.
1468 RequestTrigger trigger[] = {
1469 {
1470 ANDROID_CONTROL_AF_TRIGGER,
1471 ANDROID_CONTROL_AF_TRIGGER_START
1472 },
1473 {
1474 ANDROID_CONTROL_AF_TRIGGER_ID,
1475 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001476 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001477 };
1478
1479 return mRequestThread->queueTrigger(trigger,
1480 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001481}
1482
1483status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1484 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001485 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001486
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001487 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1488 // Mix-in this trigger into the next request and only the next request.
1489 RequestTrigger trigger[] = {
1490 {
1491 ANDROID_CONTROL_AF_TRIGGER,
1492 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1493 },
1494 {
1495 ANDROID_CONTROL_AF_TRIGGER_ID,
1496 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001497 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001498 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001499
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001500 return mRequestThread->queueTrigger(trigger,
1501 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001502}
1503
1504status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1505 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001506 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001507
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001508 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1509 // Mix-in this trigger into the next request and only the next request.
1510 RequestTrigger trigger[] = {
1511 {
1512 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1513 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1514 },
1515 {
1516 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1517 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001518 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001519 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001520
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001521 return mRequestThread->queueTrigger(trigger,
1522 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001523}
1524
1525status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1526 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1527 ATRACE_CALL();
1528 (void)reprocessStreamId; (void)buffer; (void)listener;
1529
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001530 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001531 return INVALID_OPERATION;
1532}
1533
Jianing Weicb0652e2014-03-12 18:29:36 -07001534status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001535 ATRACE_CALL();
1536 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001537 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001538
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001539 NotificationListener* listener;
1540 {
1541 Mutex::Autolock l(mOutputLock);
1542 listener = mListener;
1543 }
1544
Zhijun He7ef20392014-04-21 16:04:17 -07001545 {
1546 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001547 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001548 }
1549
Zhijun He491e3412013-12-27 10:57:44 -08001550 status_t res;
1551 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001552 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001553 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001554 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001555 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001556 }
1557
1558 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001559}
1560
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001561status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001562 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1563}
1564
1565status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001566 ATRACE_CALL();
1567 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001568 Mutex::Autolock il(mInterfaceLock);
1569 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001570
1571 sp<Camera3StreamInterface> stream;
1572 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1573 if (outputStreamIdx == NAME_NOT_FOUND) {
1574 CLOGE("Stream %d does not exist", streamId);
1575 return BAD_VALUE;
1576 }
1577
1578 stream = mOutputStreams.editValueAt(outputStreamIdx);
1579
1580 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001581 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001582 return BAD_VALUE;
1583 }
1584
1585 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001586 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001587 return BAD_VALUE;
1588 }
1589
Ruben Brunkc78ac262015-08-13 17:58:46 -07001590 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001591}
1592
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001593status_t Camera3Device::tearDown(int streamId) {
1594 ATRACE_CALL();
1595 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1596 Mutex::Autolock il(mInterfaceLock);
1597 Mutex::Autolock l(mLock);
1598
1599 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1600 // since we cannot call register_stream_buffers except right after configure_streams.
1601 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1602 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1603 __FUNCTION__, mHal3Device->common.version);
1604 return NO_INIT;
1605 }
1606
1607 sp<Camera3StreamInterface> stream;
1608 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1609 if (outputStreamIdx == NAME_NOT_FOUND) {
1610 CLOGE("Stream %d does not exist", streamId);
1611 return BAD_VALUE;
1612 }
1613
1614 stream = mOutputStreams.editValueAt(outputStreamIdx);
1615
1616 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1617 CLOGE("Stream %d is a target of a in-progress request", streamId);
1618 return BAD_VALUE;
1619 }
1620
1621 return stream->tearDown();
1622}
1623
Zhijun He204e3292014-07-14 17:09:23 -07001624uint32_t Camera3Device::getDeviceVersion() {
1625 ATRACE_CALL();
1626 Mutex::Autolock il(mInterfaceLock);
1627 return mDeviceVersion;
1628}
1629
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001630/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001631 * Methods called by subclasses
1632 */
1633
1634void Camera3Device::notifyStatus(bool idle) {
1635 {
1636 // Need mLock to safely update state and synchronize to current
1637 // state of methods in flight.
1638 Mutex::Autolock l(mLock);
1639 // We can get various system-idle notices from the status tracker
1640 // while starting up. Only care about them if we've actually sent
1641 // in some requests recently.
1642 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1643 return;
1644 }
1645 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1646 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001647 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001648
1649 // Skip notifying listener if we're doing some user-transparent
1650 // state changes
1651 if (mPauseStateNotify) return;
1652 }
1653 NotificationListener *listener;
1654 {
1655 Mutex::Autolock l(mOutputLock);
1656 listener = mListener;
1657 }
1658 if (idle && listener != NULL) {
1659 listener->notifyIdle();
1660 }
1661}
1662
1663/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001664 * Camera3Device private methods
1665 */
1666
1667sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1668 const CameraMetadata &request) {
1669 ATRACE_CALL();
1670 status_t res;
1671
1672 sp<CaptureRequest> newRequest = new CaptureRequest;
1673 newRequest->mSettings = request;
1674
1675 camera_metadata_entry_t inputStreams =
1676 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1677 if (inputStreams.count > 0) {
1678 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001679 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001680 CLOGE("Request references unknown input stream %d",
1681 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001682 return NULL;
1683 }
1684 // Lazy completion of stream configuration (allocation/registration)
1685 // on first use
1686 if (mInputStream->isConfiguring()) {
1687 res = mInputStream->finishConfiguration(mHal3Device);
1688 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001689 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001690 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001691 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001692 return NULL;
1693 }
1694 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001695 // Check if stream is being prepared
1696 if (mInputStream->isPreparing()) {
1697 CLOGE("Request references an input stream that's being prepared!");
1698 return NULL;
1699 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001700
1701 newRequest->mInputStream = mInputStream;
1702 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1703 }
1704
1705 camera_metadata_entry_t streams =
1706 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1707 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001708 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001709 return NULL;
1710 }
1711
1712 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001713 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001714 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001715 CLOGE("Request references unknown stream %d",
1716 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001717 return NULL;
1718 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001719 sp<Camera3OutputStreamInterface> stream =
1720 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001721
1722 // Lazy completion of stream configuration (allocation/registration)
1723 // on first use
1724 if (stream->isConfiguring()) {
1725 res = stream->finishConfiguration(mHal3Device);
1726 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001727 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1728 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001729 return NULL;
1730 }
1731 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001732 // Check if stream is being prepared
1733 if (stream->isPreparing()) {
1734 CLOGE("Request references an output stream that's being prepared!");
1735 return NULL;
1736 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001737
1738 newRequest->mOutputStreams.push(stream);
1739 }
1740 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001741 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001742
1743 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001744}
1745
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001746bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1747 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1748 Size size = mSupportedOpaqueInputSizes[i];
1749 if (size.width == width && size.height == height) {
1750 return true;
1751 }
1752 }
1753
1754 return false;
1755}
1756
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001757status_t Camera3Device::configureStreamsLocked() {
1758 ATRACE_CALL();
1759 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001760
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001761 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001762 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001763 return INVALID_OPERATION;
1764 }
1765
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001766 if (!mNeedConfig) {
1767 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1768 return OK;
1769 }
1770
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001771 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1772 // adding a dummy stream instead.
1773 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1774 if (mOutputStreams.size() == 0) {
1775 addDummyStreamLocked();
1776 } else {
1777 tryRemoveDummyStreamLocked();
1778 }
1779
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001780 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001781 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001782
1783 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001784 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1785 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1786 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001787 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1788
1789 Vector<camera3_stream_t*> streams;
1790 streams.setCapacity(config.num_streams);
1791
1792 if (mInputStream != NULL) {
1793 camera3_stream_t *inputStream;
1794 inputStream = mInputStream->startConfiguration();
1795 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001796 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001797 return INVALID_OPERATION;
1798 }
1799 streams.add(inputStream);
1800 }
1801
1802 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001803
1804 // Don't configure bidi streams twice, nor add them twice to the list
1805 if (mOutputStreams[i].get() ==
1806 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1807
1808 config.num_streams--;
1809 continue;
1810 }
1811
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001812 camera3_stream_t *outputStream;
1813 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1814 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001815 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001816 return INVALID_OPERATION;
1817 }
1818 streams.add(outputStream);
1819 }
1820
1821 config.streams = streams.editArray();
1822
1823 // Do the HAL configuration; will potentially touch stream
1824 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001825 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001826 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001827 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001828
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001829 if (res == BAD_VALUE) {
1830 // HAL rejected this set of streams as unsupported, clean up config
1831 // attempt and return to unconfigured state
1832 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1833 res = mInputStream->cancelConfiguration();
1834 if (res != OK) {
1835 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1836 mInputStream->getId(), strerror(-res), res);
1837 return res;
1838 }
1839 }
1840
1841 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1842 sp<Camera3OutputStreamInterface> outputStream =
1843 mOutputStreams.editValueAt(i);
1844 if (outputStream->isConfiguring()) {
1845 res = outputStream->cancelConfiguration();
1846 if (res != OK) {
1847 SET_ERR_L(
1848 "Can't cancel configuring output stream %d: %s (%d)",
1849 outputStream->getId(), strerror(-res), res);
1850 return res;
1851 }
1852 }
1853 }
1854
1855 // Return state to that at start of call, so that future configures
1856 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001857 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001858 mNeedConfig = true;
1859
1860 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1861 return BAD_VALUE;
1862 } else if (res != OK) {
1863 // Some other kind of error from configure_streams - this is not
1864 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001865 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1866 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001867 return res;
1868 }
1869
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001870 // Finish all stream configuration immediately.
1871 // TODO: Try to relax this later back to lazy completion, which should be
1872 // faster
1873
Igor Murashkin073f8572013-05-02 14:59:28 -07001874 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001875 res = mInputStream->finishConfiguration(mHal3Device);
1876 if (res != OK) {
1877 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1878 mInputStream->getId(), strerror(-res), res);
1879 return res;
1880 }
1881 }
1882
1883 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001884 sp<Camera3OutputStreamInterface> outputStream =
1885 mOutputStreams.editValueAt(i);
1886 if (outputStream->isConfiguring()) {
1887 res = outputStream->finishConfiguration(mHal3Device);
1888 if (res != OK) {
1889 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1890 outputStream->getId(), strerror(-res), res);
1891 return res;
1892 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001893 }
1894 }
1895
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001896 // Request thread needs to know to avoid using repeat-last-settings protocol
1897 // across configure_streams() calls
1898 mRequestThread->configurationComplete();
1899
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07001900 // Boost priority of request thread for high speed recording to SCHED_FIFO
1901 if (mIsConstrainedHighSpeedConfiguration) {
1902 pid_t requestThreadTid = mRequestThread->getTid();
1903 res = requestPriority(getpid(), requestThreadTid,
1904 kConstrainedHighSpeedThreadPriority, true);
1905 if (res != OK) {
1906 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
1907 strerror(-res), res);
1908 } else {
1909 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
1910 }
1911 } else {
1912 // TODO: Set/restore normal priority for normal use cases
1913 }
1914
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001915 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001916
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001917 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001918
Ruben Brunk183f0562015-08-12 12:55:02 -07001919 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1920 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001921
1922 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1923
Zhijun He0a210512014-07-24 13:45:15 -07001924 // tear down the deleted streams after configure streams.
1925 mDeletedStreams.clear();
1926
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001927 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001928}
1929
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001930status_t Camera3Device::addDummyStreamLocked() {
1931 ATRACE_CALL();
1932 status_t res;
1933
1934 if (mDummyStreamId != NO_STREAM) {
1935 // Should never be adding a second dummy stream when one is already
1936 // active
1937 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1938 __FUNCTION__, mId);
1939 return INVALID_OPERATION;
1940 }
1941
1942 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1943
1944 sp<Camera3OutputStreamInterface> dummyStream =
1945 new Camera3DummyStream(mNextStreamId);
1946
1947 res = mOutputStreams.add(mNextStreamId, dummyStream);
1948 if (res < 0) {
1949 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1950 return res;
1951 }
1952
1953 mDummyStreamId = mNextStreamId;
1954 mNextStreamId++;
1955
1956 return OK;
1957}
1958
1959status_t Camera3Device::tryRemoveDummyStreamLocked() {
1960 ATRACE_CALL();
1961 status_t res;
1962
1963 if (mDummyStreamId == NO_STREAM) return OK;
1964 if (mOutputStreams.size() == 1) return OK;
1965
1966 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1967
1968 // Ok, have a dummy stream and there's at least one other output stream,
1969 // so remove the dummy
1970
1971 sp<Camera3StreamInterface> deletedStream;
1972 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1973 if (outputStreamIdx == NAME_NOT_FOUND) {
1974 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1975 return INVALID_OPERATION;
1976 }
1977
1978 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1979 mOutputStreams.removeItemsAt(outputStreamIdx);
1980
1981 // Free up the stream endpoint so that it can be used by some other stream
1982 res = deletedStream->disconnect();
1983 if (res != OK) {
1984 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1985 // fall through since we want to still list the stream as deleted.
1986 }
1987 mDeletedStreams.add(deletedStream);
1988 mDummyStreamId = NO_STREAM;
1989
1990 return res;
1991}
1992
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001993void Camera3Device::setErrorState(const char *fmt, ...) {
1994 Mutex::Autolock l(mLock);
1995 va_list args;
1996 va_start(args, fmt);
1997
1998 setErrorStateLockedV(fmt, args);
1999
2000 va_end(args);
2001}
2002
2003void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2004 Mutex::Autolock l(mLock);
2005 setErrorStateLockedV(fmt, args);
2006}
2007
2008void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2009 va_list args;
2010 va_start(args, fmt);
2011
2012 setErrorStateLockedV(fmt, args);
2013
2014 va_end(args);
2015}
2016
2017void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002018 // Print out all error messages to log
2019 String8 errorCause = String8::formatV(fmt, args);
2020 ALOGE("Camera %d: %s", mId, errorCause.string());
2021
2022 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002023 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002024
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002025 mErrorCause = errorCause;
2026
2027 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002028 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002029
2030 // Notify upstream about a device error
2031 if (mListener != NULL) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002032 mListener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002033 CaptureResultExtras());
2034 }
2035
2036 // Save stack trace. View by dumping it later.
2037 CameraTraces::saveTrace();
2038 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002039}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002040
2041/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002042 * In-flight request management
2043 */
2044
Jianing Weicb0652e2014-03-12 18:29:36 -07002045status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002046 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2047 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002048 ATRACE_CALL();
2049 Mutex::Autolock l(mInFlightLock);
2050
2051 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002052 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2053 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002054 if (res < 0) return res;
2055
2056 return OK;
2057}
2058
2059/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002060 * Check if all 3A fields are ready, and send off a partial 3A-only result
2061 * to the output frame queue
2062 */
Zhijun He204e3292014-07-14 17:09:23 -07002063bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07002064 uint32_t frameNumber,
2065 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002066
2067 // Check if all 3A states are present
2068 // The full list of fields is
2069 // android.control.afMode
2070 // android.control.awbMode
2071 // android.control.aeState
2072 // android.control.awbState
2073 // android.control.afState
2074 // android.control.afTriggerID
2075 // android.control.aePrecaptureID
2076 // TODO: Add android.control.aeMode
2077
2078 bool gotAllStates = true;
2079
2080 uint8_t afMode;
2081 uint8_t awbMode;
2082 uint8_t aeState;
2083 uint8_t afState;
2084 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002085
2086 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
2087 &afMode, frameNumber);
2088
2089 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
2090 &awbMode, frameNumber);
2091
2092 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
2093 &aeState, frameNumber);
2094
2095 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
2096 &afState, frameNumber);
2097
2098 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
2099 &awbState, frameNumber);
2100
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002101 if (!gotAllStates) return false;
2102
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002103 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002104 "AF state %d, AE state %d, AWB state %d, "
2105 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002106 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002107 afMode, awbMode,
2108 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002109 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002110
2111 // Got all states, so construct a minimal result to send
2112 // In addition to the above fields, this means adding in
2113 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002114 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07002115 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002116
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002117 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002118
2119 Mutex::Autolock l(mOutputLock);
2120
Jianing Weicb0652e2014-03-12 18:29:36 -07002121 CaptureResult captureResult;
2122 captureResult.mResultExtras = resultExtras;
2123 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
2124 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
2125 // but not limited to CameraDeviceBase::getNextResult
2126 CaptureResult& min3AResult =
2127 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002128
Jianing Weicb0652e2014-03-12 18:29:36 -07002129 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
2130 // TODO: This is problematic casting. Need to fix CameraMetadata.
2131 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002132 return false;
2133 }
2134
Jianing Weicb0652e2014-03-12 18:29:36 -07002135 int32_t requestId = resultExtras.requestId;
2136 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002137 &requestId, frameNumber)) {
2138 return false;
2139 }
2140
Zhijun He204e3292014-07-14 17:09:23 -07002141 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
2142 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
2143 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
2144 &partialResult, frameNumber)) {
2145 return false;
2146 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002147 }
2148
Jianing Weicb0652e2014-03-12 18:29:36 -07002149 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002150 &afMode, frameNumber)) {
2151 return false;
2152 }
2153
Jianing Weicb0652e2014-03-12 18:29:36 -07002154 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002155 &awbMode, frameNumber)) {
2156 return false;
2157 }
2158
Jianing Weicb0652e2014-03-12 18:29:36 -07002159 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002160 &aeState, frameNumber)) {
2161 return false;
2162 }
2163
Jianing Weicb0652e2014-03-12 18:29:36 -07002164 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002165 &afState, frameNumber)) {
2166 return false;
2167 }
2168
Jianing Weicb0652e2014-03-12 18:29:36 -07002169 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002170 &awbState, frameNumber)) {
2171 return false;
2172 }
2173
Jianing Weicb0652e2014-03-12 18:29:36 -07002174 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002175 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002176 return false;
2177 }
2178
Jianing Weicb0652e2014-03-12 18:29:36 -07002179 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002180 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002181 return false;
2182 }
2183
Zhijun He204e3292014-07-14 17:09:23 -07002184 // We only send the aggregated partial when all 3A related metadata are available
2185 // For both API1 and API2.
2186 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002187 mResultSignal.signal();
2188
2189 return true;
2190}
2191
2192template<typename T>
2193bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002194 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002195 (void) frameNumber;
2196
2197 camera_metadata_ro_entry_t entry;
2198
2199 entry = result.find(tag);
2200 if (entry.count == 0) {
2201 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2202 mId, frameNumber, get_camera_metadata_tag_name(tag));
2203 return false;
2204 }
2205
2206 if (sizeof(T) == sizeof(uint8_t)) {
2207 *value = entry.data.u8[0];
2208 } else if (sizeof(T) == sizeof(int32_t)) {
2209 *value = entry.data.i32[0];
2210 } else {
2211 ALOGE("%s: Unexpected type", __FUNCTION__);
2212 return false;
2213 }
2214 return true;
2215}
2216
2217template<typename T>
2218bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002219 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002220 if (result.update(tag, value, 1) != NO_ERROR) {
2221 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2222 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2223 frameNumber, get_camera_metadata_tag_name(tag));
2224 return false;
2225 }
2226 return true;
2227}
2228
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002229void Camera3Device::returnOutputBuffers(
2230 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2231 nsecs_t timestamp) {
2232 for (size_t i = 0; i < numBuffers; i++)
2233 {
2234 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2235 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2236 // Note: stream may be deallocated at this point, if this buffer was
2237 // the last reference to it.
2238 if (res != OK) {
2239 ALOGE("Can't return buffer to its stream: %s (%d)",
2240 strerror(-res), res);
2241 }
2242 }
2243}
2244
2245
2246void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2247
2248 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2249 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2250
2251 nsecs_t sensorTimestamp = request.sensorTimestamp;
2252 nsecs_t shutterTimestamp = request.shutterTimestamp;
2253
2254 // Check if it's okay to remove the request from InFlightMap:
2255 // In the case of a successful request:
2256 // all input and output buffers, all result metadata, shutter callback
2257 // arrived.
2258 // In the case of a unsuccessful request:
2259 // all input and output buffers arrived.
2260 if (request.numBuffersLeft == 0 &&
2261 (request.requestStatus != OK ||
2262 (request.haveResultMetadata && shutterTimestamp != 0))) {
2263 ATRACE_ASYNC_END("frame capture", frameNumber);
2264
2265 // Sanity check - if sensor timestamp matches shutter timestamp
2266 if (request.requestStatus == OK &&
2267 sensorTimestamp != shutterTimestamp) {
2268 SET_ERR("sensor timestamp (%" PRId64
2269 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2270 sensorTimestamp, frameNumber, shutterTimestamp);
2271 }
2272
2273 // for an unsuccessful request, it may have pending output buffers to
2274 // return.
2275 assert(request.requestStatus != OK ||
2276 request.pendingOutputBuffers.size() == 0);
2277 returnOutputBuffers(request.pendingOutputBuffers.array(),
2278 request.pendingOutputBuffers.size(), 0);
2279
2280 mInFlightMap.removeItemsAt(idx, 1);
2281
2282 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2283 }
2284
2285 // Sanity check - if we have too many in-flight frames, something has
2286 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002287 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002288 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002289 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2290 kInFlightWarnLimitHighSpeed) {
2291 CLOGE("In-flight list too large for high speed configuration: %zu",
2292 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002293 }
2294}
2295
2296
2297void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2298 CaptureResultExtras &resultExtras,
2299 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002300 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002301 bool reprocess,
2302 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002303 if (pendingMetadata.isEmpty())
2304 return;
2305
2306 Mutex::Autolock l(mOutputLock);
2307
2308 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002309 if (reprocess) {
2310 if (frameNumber < mNextReprocessResultFrameNumber) {
2311 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002312 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002313 frameNumber, mNextReprocessResultFrameNumber);
2314 return;
2315 }
2316 mNextReprocessResultFrameNumber = frameNumber + 1;
2317 } else {
2318 if (frameNumber < mNextResultFrameNumber) {
2319 SET_ERR("Out-of-order capture result metadata submitted! "
2320 "(got frame number %d, expecting %d)",
2321 frameNumber, mNextResultFrameNumber);
2322 return;
2323 }
2324 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002325 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002326
2327 CaptureResult captureResult;
2328 captureResult.mResultExtras = resultExtras;
2329 captureResult.mMetadata = pendingMetadata;
2330
2331 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2332 (int32_t*)&frameNumber, 1) != OK) {
2333 SET_ERR("Failed to set frame# in metadata (%d)",
2334 frameNumber);
2335 return;
2336 } else {
2337 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2338 __FUNCTION__, mId, frameNumber);
2339 }
2340
2341 // Append any previous partials to form a complete result
2342 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2343 captureResult.mMetadata.append(collectedPartialResult);
2344 }
2345
2346 captureResult.mMetadata.sort();
2347
2348 // Check that there's a timestamp in the result metadata
2349 camera_metadata_entry entry =
2350 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2351 if (entry.count == 0) {
2352 SET_ERR("No timestamp provided by HAL for frame %d!",
2353 frameNumber);
2354 return;
2355 }
2356
Chien-Yu Chend196d612015-06-22 19:49:01 -07002357 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2358
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002359 // Valid result, insert into queue
2360 List<CaptureResult>::iterator queuedResult =
2361 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2362 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2363 ", burstId = %" PRId32, __FUNCTION__,
2364 queuedResult->mResultExtras.requestId,
2365 queuedResult->mResultExtras.frameNumber,
2366 queuedResult->mResultExtras.burstId);
2367
2368 mResultSignal.signal();
2369}
2370
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002371/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002372 * Camera HAL device callback methods
2373 */
2374
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002375void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002376 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002377
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002378 status_t res;
2379
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002380 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002381 if (result->result == NULL && result->num_output_buffers == 0 &&
2382 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002383 SET_ERR("No result data provided by HAL for frame %d",
2384 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002385 return;
2386 }
Zhijun He204e3292014-07-14 17:09:23 -07002387
2388 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2389 // partial_result to 1 when metadata is included in this result.
2390 if (!mUsePartialResult &&
2391 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2392 result->result != NULL &&
2393 result->partial_result != 1) {
2394 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2395 " if partial result is not supported",
2396 frameNumber, result->partial_result);
2397 return;
2398 }
2399
2400 bool isPartialResult = false;
2401 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002402 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002403 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002404
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002405 // Get shutter timestamp and resultExtras from list of in-flight requests,
2406 // where it was added by the shutter notification for this frame. If the
2407 // shutter timestamp isn't received yet, append the output buffers to the
2408 // in-flight request and they will be returned when the shutter timestamp
2409 // arrives. Update the in-flight status and remove the in-flight entry if
2410 // all result data and shutter timestamp have been received.
2411 nsecs_t shutterTimestamp = 0;
2412
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002413 {
2414 Mutex::Autolock l(mInFlightLock);
2415 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2416 if (idx == NAME_NOT_FOUND) {
2417 SET_ERR("Unknown frame number for capture result: %d",
2418 frameNumber);
2419 return;
2420 }
2421 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002422 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2423 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2424 ", partialResultCount = %d",
2425 __FUNCTION__, request.resultExtras.requestId,
2426 request.resultExtras.frameNumber, request.resultExtras.burstId,
2427 result->partial_result);
2428 // Always update the partial count to the latest one if it's not 0
2429 // (buffers only). When framework aggregates adjacent partial results
2430 // into one, the latest partial count will be used.
2431 if (result->partial_result != 0)
2432 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002433
2434 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002435 if (mUsePartialResult && result->result != NULL) {
2436 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2437 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2438 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2439 " the range of [1, %d] when metadata is included in the result",
2440 frameNumber, result->partial_result, mNumPartialResults);
2441 return;
2442 }
2443 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002444 if (isPartialResult) {
2445 request.partialResult.collectedResult.append(result->result);
2446 }
Zhijun He204e3292014-07-14 17:09:23 -07002447 } else {
2448 camera_metadata_ro_entry_t partialResultEntry;
2449 res = find_camera_metadata_ro_entry(result->result,
2450 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2451 if (res != NAME_NOT_FOUND &&
2452 partialResultEntry.count > 0 &&
2453 partialResultEntry.data.u8[0] ==
2454 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2455 // A partial result. Flag this as such, and collect this
2456 // set of metadata into the in-flight entry.
2457 isPartialResult = true;
2458 request.partialResult.collectedResult.append(
2459 result->result);
2460 request.partialResult.collectedResult.erase(
2461 ANDROID_QUIRKS_PARTIAL_RESULT);
2462 }
2463 }
2464
2465 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002466 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002467 if (!request.partialResult.haveSent3A) {
2468 request.partialResult.haveSent3A =
2469 processPartial3AResult(frameNumber,
2470 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002471 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002472 }
2473 }
2474 }
2475
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002476 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002477 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002478
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002479 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002480 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002481 if (request.haveResultMetadata) {
2482 SET_ERR("Called multiple times with metadata for frame %d",
2483 frameNumber);
2484 return;
2485 }
Zhijun He204e3292014-07-14 17:09:23 -07002486 if (mUsePartialResult &&
2487 !request.partialResult.collectedResult.isEmpty()) {
2488 collectedPartialResult.acquire(
2489 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002490 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002491 request.haveResultMetadata = true;
2492 }
2493
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002494 uint32_t numBuffersReturned = result->num_output_buffers;
2495 if (result->input_buffer != NULL) {
2496 if (hasInputBufferInRequest) {
2497 numBuffersReturned += 1;
2498 } else {
2499 ALOGW("%s: Input buffer should be NULL if there is no input"
2500 " buffer sent in the request",
2501 __FUNCTION__);
2502 }
2503 }
2504 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002505 if (request.numBuffersLeft < 0) {
2506 SET_ERR("Too many buffers returned for frame %d",
2507 frameNumber);
2508 return;
2509 }
2510
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002511 camera_metadata_ro_entry_t entry;
2512 res = find_camera_metadata_ro_entry(result->result,
2513 ANDROID_SENSOR_TIMESTAMP, &entry);
2514 if (res == OK && entry.count == 1) {
2515 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002516 }
2517
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002518 // If shutter event isn't received yet, append the output buffers to
2519 // the in-flight request. Otherwise, return the output buffers to
2520 // streams.
2521 if (shutterTimestamp == 0) {
2522 request.pendingOutputBuffers.appendArray(result->output_buffers,
2523 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002524 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002525 returnOutputBuffers(result->output_buffers,
2526 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002527 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002528
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002529 if (result->result != NULL && !isPartialResult) {
2530 if (shutterTimestamp == 0) {
2531 request.pendingMetadata = result->result;
2532 request.partialResult.collectedResult = collectedPartialResult;
2533 } else {
2534 CameraMetadata metadata;
2535 metadata = result->result;
2536 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002537 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2538 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002539 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002540 }
2541
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002542 removeInFlightRequestIfReadyLocked(idx);
2543 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002544
Zhijun Hef0d962a2014-06-30 10:24:11 -07002545 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002546 if (hasInputBufferInRequest) {
2547 Camera3Stream *stream =
2548 Camera3Stream::cast(result->input_buffer->stream);
2549 res = stream->returnInputBuffer(*(result->input_buffer));
2550 // Note: stream may be deallocated at this point, if this buffer was the
2551 // last reference to it.
2552 if (res != OK) {
2553 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2554 " its stream:%s (%d)", __FUNCTION__,
2555 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002556 }
2557 } else {
2558 ALOGW("%s: Input buffer should be NULL if there is no input"
2559 " buffer sent in the request, skipping input buffer return.",
2560 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002561 }
2562 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002563}
2564
2565void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002566 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002567 NotificationListener *listener;
2568 {
2569 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002570 listener = mListener;
2571 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002572
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002573 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002574 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002575 return;
2576 }
2577
2578 switch (msg->type) {
2579 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002580 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002581 break;
2582 }
2583 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002584 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002585 break;
2586 }
2587 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002588 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002589 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002590 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002591}
2592
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002593void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2594 NotificationListener *listener) {
2595
2596 // Map camera HAL error codes to ICameraDeviceCallback error codes
2597 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002598 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002599 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002600 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002601 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002602 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002603 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002604 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002605 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002606 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002607 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002608 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002609 };
2610
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002611 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002612 ((msg.error_code >= 0) &&
2613 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2614 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002615 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002616
2617 int streamId = 0;
2618 if (msg.error_stream != NULL) {
2619 Camera3Stream *stream =
2620 Camera3Stream::cast(msg.error_stream);
2621 streamId = stream->getId();
2622 }
2623 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2624 mId, __FUNCTION__, msg.frame_number,
2625 streamId, msg.error_code);
2626
2627 CaptureResultExtras resultExtras;
2628 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002629 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002630 // SET_ERR calls notifyError
2631 SET_ERR("Camera HAL reported serious device error");
2632 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002633 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2634 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2635 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002636 {
2637 Mutex::Autolock l(mInFlightLock);
2638 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2639 if (idx >= 0) {
2640 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2641 r.requestStatus = msg.error_code;
2642 resultExtras = r.resultExtras;
2643 } else {
2644 resultExtras.frameNumber = msg.frame_number;
2645 ALOGE("Camera %d: %s: cannot find in-flight request on "
2646 "frame %" PRId64 " error", mId, __FUNCTION__,
2647 resultExtras.frameNumber);
2648 }
2649 }
2650 if (listener != NULL) {
2651 listener->notifyError(errorCode, resultExtras);
2652 } else {
2653 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2654 }
2655 break;
2656 default:
2657 // SET_ERR calls notifyError
2658 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2659 break;
2660 }
2661}
2662
2663void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2664 NotificationListener *listener) {
2665 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002666
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002667 // Set timestamp for the request in the in-flight tracking
2668 // and get the request ID to send upstream
2669 {
2670 Mutex::Autolock l(mInFlightLock);
2671 idx = mInFlightMap.indexOfKey(msg.frame_number);
2672 if (idx >= 0) {
2673 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002674
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002675 // Verify ordering of shutter notifications
2676 {
2677 Mutex::Autolock l(mOutputLock);
2678 // TODO: need to track errors for tighter bounds on expected frame number.
2679 if (r.hasInputBuffer) {
2680 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2681 SET_ERR("Shutter notification out-of-order. Expected "
2682 "notification for frame %d, got frame %d",
2683 mNextReprocessShutterFrameNumber, msg.frame_number);
2684 return;
2685 }
2686 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2687 } else {
2688 if (msg.frame_number < mNextShutterFrameNumber) {
2689 SET_ERR("Shutter notification out-of-order. Expected "
2690 "notification for frame %d, got frame %d",
2691 mNextShutterFrameNumber, msg.frame_number);
2692 return;
2693 }
2694 mNextShutterFrameNumber = msg.frame_number + 1;
2695 }
2696 }
2697
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002698 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2699 mId, __FUNCTION__,
2700 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2701 // Call listener, if any
2702 if (listener != NULL) {
2703 listener->notifyShutter(r.resultExtras, msg.timestamp);
2704 }
2705
2706 r.shutterTimestamp = msg.timestamp;
2707
2708 // send pending result and buffers
2709 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002710 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002711 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002712 returnOutputBuffers(r.pendingOutputBuffers.array(),
2713 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2714 r.pendingOutputBuffers.clear();
2715
2716 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002717 }
2718 }
2719 if (idx < 0) {
2720 SET_ERR("Shutter notification for non-existent frame number %d",
2721 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002722 }
2723}
2724
2725
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002726CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002727 ALOGV("%s", __FUNCTION__);
2728
Igor Murashkin1e479c02013-09-06 16:55:14 -07002729 CameraMetadata retVal;
2730
2731 if (mRequestThread != NULL) {
2732 retVal = mRequestThread->getLatestRequest();
2733 }
2734
Igor Murashkin1e479c02013-09-06 16:55:14 -07002735 return retVal;
2736}
2737
Jianing Weicb0652e2014-03-12 18:29:36 -07002738
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002739/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002740 * RequestThread inner class methods
2741 */
2742
2743Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002744 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002745 camera3_device_t *hal3Device,
2746 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002747 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002748 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002749 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002750 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002751 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002752 mReconfigured(false),
2753 mDoPause(false),
2754 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002755 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002756 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002757 mCurrentAfTriggerId(0),
2758 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002759 mRepeatingLastFrameNumber(
2760 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002761 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002762 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002763}
2764
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002765void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002766 NotificationListener *listener) {
2767 Mutex::Autolock l(mRequestLock);
2768 mListener = listener;
2769}
2770
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002771void Camera3Device::RequestThread::configurationComplete() {
2772 Mutex::Autolock l(mRequestLock);
2773 mReconfigured = true;
2774}
2775
Jianing Wei90e59c92014-03-12 18:29:36 -07002776status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002777 List<sp<CaptureRequest> > &requests,
2778 /*out*/
2779 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002780 Mutex::Autolock l(mRequestLock);
2781 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2782 ++it) {
2783 mRequestQueue.push_back(*it);
2784 }
2785
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002786 if (lastFrameNumber != NULL) {
2787 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2788 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2789 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2790 *lastFrameNumber);
2791 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002792
Jianing Wei90e59c92014-03-12 18:29:36 -07002793 unpauseForNewRequests();
2794
2795 return OK;
2796}
2797
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002798
2799status_t Camera3Device::RequestThread::queueTrigger(
2800 RequestTrigger trigger[],
2801 size_t count) {
2802
2803 Mutex::Autolock l(mTriggerMutex);
2804 status_t ret;
2805
2806 for (size_t i = 0; i < count; ++i) {
2807 ret = queueTriggerLocked(trigger[i]);
2808
2809 if (ret != OK) {
2810 return ret;
2811 }
2812 }
2813
2814 return OK;
2815}
2816
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002817int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2818 sp<Camera3Device> d = device.promote();
2819 if (d != NULL) return d->mId;
2820 return 0;
2821}
2822
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002823status_t Camera3Device::RequestThread::queueTriggerLocked(
2824 RequestTrigger trigger) {
2825
2826 uint32_t tag = trigger.metadataTag;
2827 ssize_t index = mTriggerMap.indexOfKey(tag);
2828
2829 switch (trigger.getTagType()) {
2830 case TYPE_BYTE:
2831 // fall-through
2832 case TYPE_INT32:
2833 break;
2834 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002835 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2836 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002837 return INVALID_OPERATION;
2838 }
2839
2840 /**
2841 * Collect only the latest trigger, since we only have 1 field
2842 * in the request settings per trigger tag, and can't send more than 1
2843 * trigger per request.
2844 */
2845 if (index != NAME_NOT_FOUND) {
2846 mTriggerMap.editValueAt(index) = trigger;
2847 } else {
2848 mTriggerMap.add(tag, trigger);
2849 }
2850
2851 return OK;
2852}
2853
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002854status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002855 const RequestList &requests,
2856 /*out*/
2857 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002858 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002859 if (lastFrameNumber != NULL) {
2860 *lastFrameNumber = mRepeatingLastFrameNumber;
2861 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002862 mRepeatingRequests.clear();
2863 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2864 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002865
2866 unpauseForNewRequests();
2867
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002868 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002869 return OK;
2870}
2871
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002872bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2873 if (mRepeatingRequests.empty()) {
2874 return false;
2875 }
2876 int32_t requestId = requestIn->mResultExtras.requestId;
2877 const RequestList &repeatRequests = mRepeatingRequests;
2878 // All repeating requests are guaranteed to have same id so only check first quest
2879 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2880 return (firstRequest->mResultExtras.requestId == requestId);
2881}
2882
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002883status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002884 Mutex::Autolock l(mRequestLock);
2885 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002886 if (lastFrameNumber != NULL) {
2887 *lastFrameNumber = mRepeatingLastFrameNumber;
2888 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002889 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002890 return OK;
2891}
2892
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002893status_t Camera3Device::RequestThread::clear(
2894 NotificationListener *listener,
2895 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002896 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002897 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002898
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002899 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002900
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002901 // Send errors for all requests pending in the request queue, including
2902 // pending repeating requests
2903 if (listener != NULL) {
2904 for (RequestList::iterator it = mRequestQueue.begin();
2905 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002906 // Abort the input buffers for reprocess requests.
2907 if ((*it)->mInputStream != NULL) {
2908 camera3_stream_buffer_t inputBuffer;
2909 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2910 if (res != OK) {
2911 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2912 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2913 } else {
2914 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2915 if (res != OK) {
2916 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2917 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2918 }
2919 }
2920 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002921 // Set the frame number this request would have had, if it
2922 // had been submitted; this frame number will not be reused.
2923 // The requestId and burstId fields were set when the request was
2924 // submitted originally (in convertMetadataListToRequestListLocked)
2925 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002926 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002927 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002928 }
2929 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002930 mRequestQueue.clear();
2931 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002932 if (lastFrameNumber != NULL) {
2933 *lastFrameNumber = mRepeatingLastFrameNumber;
2934 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002935 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002936 return OK;
2937}
2938
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002939status_t Camera3Device::RequestThread::flush() {
2940 ATRACE_CALL();
2941 Mutex::Autolock l(mFlushLock);
2942
2943 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2944 return mHal3Device->ops->flush(mHal3Device);
2945 }
2946
2947 return -ENOTSUP;
2948}
2949
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002950void Camera3Device::RequestThread::setPaused(bool paused) {
2951 Mutex::Autolock l(mPauseLock);
2952 mDoPause = paused;
2953 mDoPauseSignal.signal();
2954}
2955
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002956status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2957 int32_t requestId, nsecs_t timeout) {
2958 Mutex::Autolock l(mLatestRequestMutex);
2959 status_t res;
2960 while (mLatestRequestId != requestId) {
2961 nsecs_t startTime = systemTime();
2962
2963 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2964 if (res != OK) return res;
2965
2966 timeout -= (systemTime() - startTime);
2967 }
2968
2969 return OK;
2970}
2971
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002972void Camera3Device::RequestThread::requestExit() {
2973 // Call parent to set up shutdown
2974 Thread::requestExit();
2975 // The exit from any possible waits
2976 mDoPauseSignal.signal();
2977 mRequestSignal.signal();
2978}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002979
Chien-Yu Chend196d612015-06-22 19:49:01 -07002980
2981/**
2982 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2983 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2984 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2985 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2986 * request.
2987 */
2988void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2989 request->mAeTriggerCancelOverride.applyAeLock = false;
2990 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2991
2992 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2993 return;
2994 }
2995
2996 camera_metadata_entry_t aePrecaptureTrigger =
2997 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2998 if (aePrecaptureTrigger.count > 0 &&
2999 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
3000 // Always override CANCEL to IDLE
3001 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
3002 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
3003 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
3004 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
3005 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3006
3007 if (mAeLockAvailable == true) {
3008 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3009 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3010 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3011 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3012 request->mAeTriggerCancelOverride.applyAeLock = true;
3013 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3014 }
3015 }
3016 }
3017}
3018
3019/**
3020 * Override result metadata for cancelling AE precapture trigger applied in
3021 * handleAePrecaptureCancelRequest().
3022 */
3023void Camera3Device::overrideResultForPrecaptureCancel(
3024 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3025 if (aeTriggerCancelOverride.applyAeLock) {
3026 // Only devices <= v3.2 should have this override
3027 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3028 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3029 }
3030
3031 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3032 // Only devices <= v3.2 should have this override
3033 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3034 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3035 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3036 }
3037}
3038
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003039bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003040 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003041 status_t res;
3042
3043 // Handle paused state.
3044 if (waitIfPaused()) {
3045 return true;
3046 }
3047
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003048 // Wait for the next batch of requests.
3049 waitForNextRequestBatch();
3050 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003051 return true;
3052 }
3053
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003054 // Get the latest request ID, if any
3055 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003056 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003057 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003058 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003059 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003060 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003061 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3062 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003063 }
3064
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003065 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003066 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003067 if (res == TIMED_OUT) {
3068 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003069 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003070 return true;
3071 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003072 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003073 return false;
3074 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003075
Zhijun Hecc27e112013-10-03 16:12:43 -07003076 // Inform waitUntilRequestProcessed thread of a new request ID
3077 {
3078 Mutex::Autolock al(mLatestRequestMutex);
3079
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003080 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003081 mLatestRequestSignal.signal();
3082 }
3083
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003084 // Submit a batch of requests to HAL.
3085 // Use flush lock only when submitting multilple requests in a batch.
3086 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3087 // which may take a long time to finish so synchronizing flush() and
3088 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3089 // For now, only synchronize for high speed recording and we should figure something out for
3090 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003091 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003092
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003093 if (useFlushLock) {
3094 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003095 }
3096
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003097 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003098 mNextRequests.size());
3099 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003100 // Submit request and block until ready for next one
3101 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3102 ATRACE_BEGIN("camera3->process_capture_request");
3103 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3104 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003105
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003106 if (res != OK) {
3107 // Should only get a failure here for malformed requests or device-level
3108 // errors, so consider all errors fatal. Bad metadata failures should
3109 // come through notify.
3110 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3111 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3112 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003113 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003114 if (useFlushLock) {
3115 mFlushLock.unlock();
3116 }
3117 return false;
3118 }
3119
3120 // Mark that the request has be submitted successfully.
3121 nextRequest.submitted = true;
3122
3123 // Update the latest request sent to HAL
3124 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3125 Mutex::Autolock al(mLatestRequestMutex);
3126
3127 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3128 mLatestRequest.acquire(cloned);
3129 }
3130
3131 if (nextRequest.halRequest.settings != NULL) {
3132 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3133 }
3134
3135 // Remove any previously queued triggers (after unlock)
3136 res = removeTriggers(mPrevRequest);
3137 if (res != OK) {
3138 SET_ERR("RequestThread: Unable to remove triggers "
3139 "(capture request %d, HAL device: %s (%d)",
3140 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003141 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003142 if (useFlushLock) {
3143 mFlushLock.unlock();
3144 }
3145 return false;
3146 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003147 }
3148
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003149 if (useFlushLock) {
3150 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003151 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003152
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003153 // Unset as current request
3154 {
3155 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003156 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003157 }
3158
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003159 return true;
3160}
3161
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003162status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003163 ATRACE_CALL();
3164
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003165 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003166 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3167 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3168 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3169
3170 // Prepare a request to HAL
3171 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3172
3173 // Insert any queued triggers (before metadata is locked)
3174 status_t res = insertTriggers(captureRequest);
3175
3176 if (res < 0) {
3177 SET_ERR("RequestThread: Unable to insert triggers "
3178 "(capture request %d, HAL device: %s (%d)",
3179 halRequest->frame_number, strerror(-res), res);
3180 return INVALID_OPERATION;
3181 }
3182 int triggerCount = res;
3183 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3184 mPrevTriggers = triggerCount;
3185
3186 // If the request is the same as last, or we had triggers last time
3187 if (mPrevRequest != captureRequest || triggersMixedIn) {
3188 /**
3189 * HAL workaround:
3190 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3191 */
3192 res = addDummyTriggerIds(captureRequest);
3193 if (res != OK) {
3194 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3195 "(capture request %d, HAL device: %s (%d)",
3196 halRequest->frame_number, strerror(-res), res);
3197 return INVALID_OPERATION;
3198 }
3199
3200 /**
3201 * The request should be presorted so accesses in HAL
3202 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3203 */
3204 captureRequest->mSettings.sort();
3205 halRequest->settings = captureRequest->mSettings.getAndLock();
3206 mPrevRequest = captureRequest;
3207 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3208
3209 IF_ALOGV() {
3210 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3211 find_camera_metadata_ro_entry(
3212 halRequest->settings,
3213 ANDROID_CONTROL_AF_TRIGGER,
3214 &e
3215 );
3216 if (e.count > 0) {
3217 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3218 __FUNCTION__,
3219 halRequest->frame_number,
3220 e.data.u8[0]);
3221 }
3222 }
3223 } else {
3224 // leave request.settings NULL to indicate 'reuse latest given'
3225 ALOGVV("%s: Request settings are REUSED",
3226 __FUNCTION__);
3227 }
3228
3229 uint32_t totalNumBuffers = 0;
3230
3231 // Fill in buffers
3232 if (captureRequest->mInputStream != NULL) {
3233 halRequest->input_buffer = &captureRequest->mInputBuffer;
3234 totalNumBuffers += 1;
3235 } else {
3236 halRequest->input_buffer = NULL;
3237 }
3238
3239 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3240 captureRequest->mOutputStreams.size());
3241 halRequest->output_buffers = outputBuffers->array();
3242 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3243 res = captureRequest->mOutputStreams.editItemAt(i)->
3244 getBuffer(&outputBuffers->editItemAt(i));
3245 if (res != OK) {
3246 // Can't get output buffer from gralloc queue - this could be due to
3247 // abandoned queue or other consumer misbehavior, so not a fatal
3248 // error
3249 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3250 " %s (%d)", strerror(-res), res);
3251
3252 return TIMED_OUT;
3253 }
3254 halRequest->num_output_buffers++;
3255 }
3256 totalNumBuffers += halRequest->num_output_buffers;
3257
3258 // Log request in the in-flight queue
3259 sp<Camera3Device> parent = mParent.promote();
3260 if (parent == NULL) {
3261 // Should not happen, and nowhere to send errors to, so just log it
3262 CLOGE("RequestThread: Parent is gone");
3263 return INVALID_OPERATION;
3264 }
3265 res = parent->registerInFlight(halRequest->frame_number,
3266 totalNumBuffers, captureRequest->mResultExtras,
3267 /*hasInput*/halRequest->input_buffer != NULL,
3268 captureRequest->mAeTriggerCancelOverride);
3269 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3270 ", burstId = %" PRId32 ".",
3271 __FUNCTION__,
3272 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3273 captureRequest->mResultExtras.burstId);
3274 if (res != OK) {
3275 SET_ERR("RequestThread: Unable to register new in-flight request:"
3276 " %s (%d)", strerror(-res), res);
3277 return INVALID_OPERATION;
3278 }
3279 }
3280
3281 return OK;
3282}
3283
Igor Murashkin1e479c02013-09-06 16:55:14 -07003284CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3285 Mutex::Autolock al(mLatestRequestMutex);
3286
3287 ALOGV("RequestThread::%s", __FUNCTION__);
3288
3289 return mLatestRequest;
3290}
3291
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003292bool Camera3Device::RequestThread::isStreamPending(
3293 sp<Camera3StreamInterface>& stream) {
3294 Mutex::Autolock l(mRequestLock);
3295
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003296 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003297 if (!nextRequest.submitted) {
3298 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3299 if (stream == s) return true;
3300 }
3301 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003302 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003303 }
3304
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003305 for (const auto& request : mRequestQueue) {
3306 for (const auto& s : request->mOutputStreams) {
3307 if (stream == s) return true;
3308 }
3309 if (stream == request->mInputStream) return true;
3310 }
3311
3312 for (const auto& request : mRepeatingRequests) {
3313 for (const auto& s : request->mOutputStreams) {
3314 if (stream == s) return true;
3315 }
3316 if (stream == request->mInputStream) return true;
3317 }
3318
3319 return false;
3320}
Jianing Weicb0652e2014-03-12 18:29:36 -07003321
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003322void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3323 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003324 return;
3325 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003326
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003327 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003328 // Skip the ones that have been submitted successfully.
3329 if (nextRequest.submitted) {
3330 continue;
3331 }
3332
3333 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3334 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3335 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3336
3337 if (halRequest->settings != NULL) {
3338 captureRequest->mSettings.unlock(halRequest->settings);
3339 }
3340
3341 if (captureRequest->mInputStream != NULL) {
3342 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3343 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3344 }
3345
3346 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3347 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3348 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3349 }
3350
3351 if (sendRequestError) {
3352 Mutex::Autolock l(mRequestLock);
3353 if (mListener != NULL) {
3354 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003355 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003356 captureRequest->mResultExtras);
3357 }
3358 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003359 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003360
3361 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003362 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003363}
3364
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003365void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003366 // Optimized a bit for the simple steady-state case (single repeating
3367 // request), to avoid putting that request in the queue temporarily.
3368 Mutex::Autolock l(mRequestLock);
3369
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003370 assert(mNextRequests.empty());
3371
3372 NextRequest nextRequest;
3373 nextRequest.captureRequest = waitForNextRequestLocked();
3374 if (nextRequest.captureRequest == nullptr) {
3375 return;
3376 }
3377
3378 nextRequest.halRequest = camera3_capture_request_t();
3379 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003380 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003381
3382 // Wait for additional requests
3383 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3384
3385 for (size_t i = 1; i < batchSize; i++) {
3386 NextRequest additionalRequest;
3387 additionalRequest.captureRequest = waitForNextRequestLocked();
3388 if (additionalRequest.captureRequest == nullptr) {
3389 break;
3390 }
3391
3392 additionalRequest.halRequest = camera3_capture_request_t();
3393 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003394 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003395 }
3396
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003397 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003398 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003399 mNextRequests.size(), batchSize);
3400 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003401 }
3402
3403 return;
3404}
3405
3406sp<Camera3Device::CaptureRequest>
3407 Camera3Device::RequestThread::waitForNextRequestLocked() {
3408 status_t res;
3409 sp<CaptureRequest> nextRequest;
3410
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003411 while (mRequestQueue.empty()) {
3412 if (!mRepeatingRequests.empty()) {
3413 // Always atomically enqueue all requests in a repeating request
3414 // list. Guarantees a complete in-sequence set of captures to
3415 // application.
3416 const RequestList &requests = mRepeatingRequests;
3417 RequestList::const_iterator firstRequest =
3418 requests.begin();
3419 nextRequest = *firstRequest;
3420 mRequestQueue.insert(mRequestQueue.end(),
3421 ++firstRequest,
3422 requests.end());
3423 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003424
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003425 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003426
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003427 break;
3428 }
3429
3430 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3431
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003432 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3433 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003434 Mutex::Autolock pl(mPauseLock);
3435 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003436 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003437 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003438 // Let the tracker know
3439 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3440 if (statusTracker != 0) {
3441 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3442 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003443 }
3444 // Stop waiting for now and let thread management happen
3445 return NULL;
3446 }
3447 }
3448
3449 if (nextRequest == NULL) {
3450 // Don't have a repeating request already in hand, so queue
3451 // must have an entry now.
3452 RequestList::iterator firstRequest =
3453 mRequestQueue.begin();
3454 nextRequest = *firstRequest;
3455 mRequestQueue.erase(firstRequest);
3456 }
3457
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003458 // In case we've been unpaused by setPaused clearing mDoPause, need to
3459 // update internal pause state (capture/setRepeatingRequest unpause
3460 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003461 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003462 if (mPaused) {
3463 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3464 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3465 if (statusTracker != 0) {
3466 statusTracker->markComponentActive(mStatusId);
3467 }
3468 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003469 mPaused = false;
3470
3471 // Check if we've reconfigured since last time, and reset the preview
3472 // request if so. Can't use 'NULL request == repeat' across configure calls.
3473 if (mReconfigured) {
3474 mPrevRequest.clear();
3475 mReconfigured = false;
3476 }
3477
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003478 if (nextRequest != NULL) {
3479 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003480 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3481 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003482
3483 // Since RequestThread::clear() removes buffers from the input stream,
3484 // get the right buffer here before unlocking mRequestLock
3485 if (nextRequest->mInputStream != NULL) {
3486 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3487 if (res != OK) {
3488 // Can't get input buffer from gralloc queue - this could be due to
3489 // disconnected queue or other producer misbehavior, so not a fatal
3490 // error
3491 ALOGE("%s: Can't get input buffer, skipping request:"
3492 " %s (%d)", __FUNCTION__, strerror(-res), res);
3493 if (mListener != NULL) {
3494 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003495 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003496 nextRequest->mResultExtras);
3497 }
3498 return NULL;
3499 }
3500 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003501 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003502
3503 handleAePrecaptureCancelRequest(nextRequest);
3504
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003505 return nextRequest;
3506}
3507
3508bool Camera3Device::RequestThread::waitIfPaused() {
3509 status_t res;
3510 Mutex::Autolock l(mPauseLock);
3511 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003512 if (mPaused == false) {
3513 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003514 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3515 // Let the tracker know
3516 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3517 if (statusTracker != 0) {
3518 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3519 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003520 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003521
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003522 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003523 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003524 return true;
3525 }
3526 }
3527 // We don't set mPaused to false here, because waitForNextRequest needs
3528 // to further manage the paused state in case of starvation.
3529 return false;
3530}
3531
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003532void Camera3Device::RequestThread::unpauseForNewRequests() {
3533 // With work to do, mark thread as unpaused.
3534 // If paused by request (setPaused), don't resume, to avoid
3535 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003536 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003537 Mutex::Autolock p(mPauseLock);
3538 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003539 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3540 if (mPaused) {
3541 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3542 if (statusTracker != 0) {
3543 statusTracker->markComponentActive(mStatusId);
3544 }
3545 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003546 mPaused = false;
3547 }
3548}
3549
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003550void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3551 sp<Camera3Device> parent = mParent.promote();
3552 if (parent != NULL) {
3553 va_list args;
3554 va_start(args, fmt);
3555
3556 parent->setErrorStateV(fmt, args);
3557
3558 va_end(args);
3559 }
3560}
3561
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003562status_t Camera3Device::RequestThread::insertTriggers(
3563 const sp<CaptureRequest> &request) {
3564
3565 Mutex::Autolock al(mTriggerMutex);
3566
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003567 sp<Camera3Device> parent = mParent.promote();
3568 if (parent == NULL) {
3569 CLOGE("RequestThread: Parent is gone");
3570 return DEAD_OBJECT;
3571 }
3572
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003573 CameraMetadata &metadata = request->mSettings;
3574 size_t count = mTriggerMap.size();
3575
3576 for (size_t i = 0; i < count; ++i) {
3577 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003578 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003579
3580 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3581 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3582 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003583 if (isAeTrigger) {
3584 request->mResultExtras.precaptureTriggerId = triggerId;
3585 mCurrentPreCaptureTriggerId = triggerId;
3586 } else {
3587 request->mResultExtras.afTriggerId = triggerId;
3588 mCurrentAfTriggerId = triggerId;
3589 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003590 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3591 continue; // Trigger ID tag is deprecated since device HAL 3.2
3592 }
3593 }
3594
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003595 camera_metadata_entry entry = metadata.find(tag);
3596
3597 if (entry.count > 0) {
3598 /**
3599 * Already has an entry for this trigger in the request.
3600 * Rewrite it with our requested trigger value.
3601 */
3602 RequestTrigger oldTrigger = trigger;
3603
3604 oldTrigger.entryValue = entry.data.u8[0];
3605
3606 mTriggerReplacedMap.add(tag, oldTrigger);
3607 } else {
3608 /**
3609 * More typical, no trigger entry, so we just add it
3610 */
3611 mTriggerRemovedMap.add(tag, trigger);
3612 }
3613
3614 status_t res;
3615
3616 switch (trigger.getTagType()) {
3617 case TYPE_BYTE: {
3618 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3619 res = metadata.update(tag,
3620 &entryValue,
3621 /*count*/1);
3622 break;
3623 }
3624 case TYPE_INT32:
3625 res = metadata.update(tag,
3626 &trigger.entryValue,
3627 /*count*/1);
3628 break;
3629 default:
3630 ALOGE("%s: Type not supported: 0x%x",
3631 __FUNCTION__,
3632 trigger.getTagType());
3633 return INVALID_OPERATION;
3634 }
3635
3636 if (res != OK) {
3637 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3638 ", value %d", __FUNCTION__, trigger.getTagName(),
3639 trigger.entryValue);
3640 return res;
3641 }
3642
3643 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3644 trigger.getTagName(),
3645 trigger.entryValue);
3646 }
3647
3648 mTriggerMap.clear();
3649
3650 return count;
3651}
3652
3653status_t Camera3Device::RequestThread::removeTriggers(
3654 const sp<CaptureRequest> &request) {
3655 Mutex::Autolock al(mTriggerMutex);
3656
3657 CameraMetadata &metadata = request->mSettings;
3658
3659 /**
3660 * Replace all old entries with their old values.
3661 */
3662 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3663 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3664
3665 status_t res;
3666
3667 uint32_t tag = trigger.metadataTag;
3668 switch (trigger.getTagType()) {
3669 case TYPE_BYTE: {
3670 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3671 res = metadata.update(tag,
3672 &entryValue,
3673 /*count*/1);
3674 break;
3675 }
3676 case TYPE_INT32:
3677 res = metadata.update(tag,
3678 &trigger.entryValue,
3679 /*count*/1);
3680 break;
3681 default:
3682 ALOGE("%s: Type not supported: 0x%x",
3683 __FUNCTION__,
3684 trigger.getTagType());
3685 return INVALID_OPERATION;
3686 }
3687
3688 if (res != OK) {
3689 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3690 ", trigger value %d", __FUNCTION__,
3691 trigger.getTagName(), trigger.entryValue);
3692 return res;
3693 }
3694 }
3695 mTriggerReplacedMap.clear();
3696
3697 /**
3698 * Remove all new entries.
3699 */
3700 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3701 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3702 status_t res = metadata.erase(trigger.metadataTag);
3703
3704 if (res != OK) {
3705 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3706 ", trigger value %d", __FUNCTION__,
3707 trigger.getTagName(), trigger.entryValue);
3708 return res;
3709 }
3710 }
3711 mTriggerRemovedMap.clear();
3712
3713 return OK;
3714}
3715
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003716status_t Camera3Device::RequestThread::addDummyTriggerIds(
3717 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003718 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003719 static const int32_t dummyTriggerId = 1;
3720 status_t res;
3721
3722 CameraMetadata &metadata = request->mSettings;
3723
3724 // If AF trigger is active, insert a dummy AF trigger ID if none already
3725 // exists
3726 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3727 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3728 if (afTrigger.count > 0 &&
3729 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3730 afId.count == 0) {
3731 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3732 if (res != OK) return res;
3733 }
3734
3735 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3736 // if none already exists
3737 camera_metadata_entry pcTrigger =
3738 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3739 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3740 if (pcTrigger.count > 0 &&
3741 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3742 pcId.count == 0) {
3743 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3744 &dummyTriggerId, 1);
3745 if (res != OK) return res;
3746 }
3747
3748 return OK;
3749}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003750
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003751/**
3752 * PreparerThread inner class methods
3753 */
3754
3755Camera3Device::PreparerThread::PreparerThread() :
3756 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3757}
3758
3759Camera3Device::PreparerThread::~PreparerThread() {
3760 Thread::requestExitAndWait();
3761 if (mCurrentStream != nullptr) {
3762 mCurrentStream->cancelPrepare();
3763 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3764 mCurrentStream.clear();
3765 }
3766 clear();
3767}
3768
Ruben Brunkc78ac262015-08-13 17:58:46 -07003769status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003770 status_t res;
3771
3772 Mutex::Autolock l(mLock);
3773
Ruben Brunkc78ac262015-08-13 17:58:46 -07003774 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003775 if (res == OK) {
3776 // No preparation needed, fire listener right off
3777 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3778 if (mListener) {
3779 mListener->notifyPrepared(stream->getId());
3780 }
3781 return OK;
3782 } else if (res != NOT_ENOUGH_DATA) {
3783 return res;
3784 }
3785
3786 // Need to prepare, start up thread if necessary
3787 if (!mActive) {
3788 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3789 // isn't running
3790 Thread::requestExitAndWait();
3791 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3792 if (res != OK) {
3793 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3794 if (mListener) {
3795 mListener->notifyPrepared(stream->getId());
3796 }
3797 return res;
3798 }
3799 mCancelNow = false;
3800 mActive = true;
3801 ALOGV("%s: Preparer stream started", __FUNCTION__);
3802 }
3803
3804 // queue up the work
3805 mPendingStreams.push_back(stream);
3806 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3807
3808 return OK;
3809}
3810
3811status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003812 Mutex::Autolock l(mLock);
3813
3814 for (const auto& stream : mPendingStreams) {
3815 stream->cancelPrepare();
3816 }
3817 mPendingStreams.clear();
3818 mCancelNow = true;
3819
3820 return OK;
3821}
3822
3823void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3824 Mutex::Autolock l(mLock);
3825 mListener = listener;
3826}
3827
3828bool Camera3Device::PreparerThread::threadLoop() {
3829 status_t res;
3830 {
3831 Mutex::Autolock l(mLock);
3832 if (mCurrentStream == nullptr) {
3833 // End thread if done with work
3834 if (mPendingStreams.empty()) {
3835 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3836 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3837 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3838 mActive = false;
3839 return false;
3840 }
3841
3842 // Get next stream to prepare
3843 auto it = mPendingStreams.begin();
3844 mCurrentStream = *it;
3845 mPendingStreams.erase(it);
3846 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3847 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3848 } else if (mCancelNow) {
3849 mCurrentStream->cancelPrepare();
3850 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3851 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3852 mCurrentStream.clear();
3853 mCancelNow = false;
3854 return true;
3855 }
3856 }
3857
3858 res = mCurrentStream->prepareNextBuffer();
3859 if (res == NOT_ENOUGH_DATA) return true;
3860 if (res != OK) {
3861 // Something bad happened; try to recover by cancelling prepare and
3862 // signalling listener anyway
3863 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3864 mCurrentStream->getId(), res, strerror(-res));
3865 mCurrentStream->cancelPrepare();
3866 }
3867
3868 // This stream has finished, notify listener
3869 Mutex::Autolock l(mLock);
3870 if (mListener) {
3871 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3872 mCurrentStream->getId());
3873 mListener->notifyPrepared(mCurrentStream->getId());
3874 }
3875
3876 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3877 mCurrentStream.clear();
3878
3879 return true;
3880}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003881
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003882/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003883 * Static callback forwarding methods from HAL to instance
3884 */
3885
3886void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3887 const camera3_capture_result *result) {
3888 Camera3Device *d =
3889 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003890
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003891 d->processCaptureResult(result);
3892}
3893
3894void Camera3Device::sNotify(const camera3_callback_ops *cb,
3895 const camera3_notify_msg *msg) {
3896 Camera3Device *d =
3897 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3898 d->notify(msg);
3899}
3900
3901}; // namespace android