blob: 5b3509f9cd74d0f183d9c2508819425291c3938e [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>
Zhijun He90f7c372016-08-16 16:19:43 -070045#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070046
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080047#include <android/hardware/camera2/ICameraDeviceUser.h>
48
Igor Murashkinff3e31d2013-10-23 16:40:06 -070049#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070050#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070051#include "device3/Camera3Device.h"
52#include "device3/Camera3OutputStream.h"
53#include "device3/Camera3InputStream.h"
54#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070055#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070056#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080057
58using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080059
60namespace android {
61
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080062Camera3Device::Camera3Device(const String8 &id):
63 mId(atoi(id.string())),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070064 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080065 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070066 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070067 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070068 mUsePartialResult(false),
69 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080070 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070071 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070072 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070073 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070074 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070075 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080076{
77 ATRACE_CALL();
78 camera3_callback_ops::notify = &sNotify;
79 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080080 ALOGV("%s: Created device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080081}
82
83Camera3Device::~Camera3Device()
84{
85 ATRACE_CALL();
86 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
87 disconnect();
88}
89
Igor Murashkin71381052013-03-04 14:53:08 -080090int Camera3Device::getId() const {
91 return mId;
92}
93
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080094/**
95 * CameraDeviceBase interface
96 */
97
Yin-Chia Yehe074a932015-01-30 10:29:02 -080098status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080099{
100 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700101 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800102 Mutex::Autolock l(mLock);
103
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800104 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800105 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700106 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800107 return INVALID_OPERATION;
108 }
109
110 /** Open HAL device */
111
112 status_t res;
113 String8 deviceName = String8::format("%d", mId);
114
115 camera3_device_t *device;
116
Zhijun He213ce792013-11-19 08:45:15 -0800117 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800118 res = module->open(deviceName.string(),
119 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800120 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800121
122 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700123 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800124 return res;
125 }
126
127 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700128 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700129 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700130 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700131 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800132 device->common.version);
133 device->common.close(&device->common);
134 return BAD_VALUE;
135 }
136
137 camera_info info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800138 res = module->getCameraInfo(mId, &info);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 if (res != OK) return res;
140
141 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700142 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
143 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700144 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800145 device->common.close(&device->common);
146 return BAD_VALUE;
147 }
148
149 /** Initialize device with callback functions */
150
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700151 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800152 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700153 ATRACE_END();
154
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800155 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700156 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
157 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800158 device->common.close(&device->common);
159 return BAD_VALUE;
160 }
161
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700162 /** Start up status tracker thread */
163 mStatusTracker = new StatusTracker(this);
164 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
165 if (res != OK) {
166 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
167 strerror(-res), res);
168 device->common.close(&device->common);
169 mStatusTracker.clear();
170 return res;
171 }
172
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700173 /** Register in-flight map to the status tracker */
174 mInFlightStatusId = mStatusTracker->addComponent();
175
Zhijun He125684a2015-12-26 15:07:30 -0800176 /** Create buffer manager */
177 mBufferManager = new Camera3BufferManager();
178
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700179 bool aeLockAvailable = false;
180 camera_metadata_ro_entry aeLockAvailableEntry;
181 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
182 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
183 if (res == OK && aeLockAvailableEntry.count > 0) {
184 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
185 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
186 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700188 /** Start up request queue thread */
189 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800190 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800191 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700192 SET_ERR_L("Unable to start request queue thread: %s (%d)",
193 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800194 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800195 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800196 return res;
197 }
198
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700199 mPreparerThread = new PreparerThread();
200
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800201 /** Everything is good to go */
202
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700203 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800204 mDeviceInfo = info.static_camera_characteristics;
205 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700206
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700207 // Determine whether we need to derive sensitivity boost values for older devices.
208 // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
209 // be listed (as the default value 100)
210 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_4 &&
211 mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
212 mDerivePostRawSensKey = true;
213 }
214
Ruben Brunk183f0562015-08-12 12:55:02 -0700215 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800216 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700217 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700218 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700219 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800220
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800221 // Measure the clock domain offset between camera and video/hw_composer
222 camera_metadata_entry timestampSource =
223 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
224 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
225 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
226 mTimestampOffset = getMonoToBoottimeOffset();
227 }
228
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700229 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700230 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
231 camera_metadata_entry partialResultsCount =
232 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
233 if (partialResultsCount.count > 0) {
234 mNumPartialResults = partialResultsCount.data.i32[0];
235 mUsePartialResult = (mNumPartialResults > 1);
236 }
237 } else {
238 camera_metadata_entry partialResultsQuirk =
239 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
240 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
241 mUsePartialResult = true;
242 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700243 }
244
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700245 camera_metadata_entry configs =
246 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
247 for (uint32_t i = 0; i < configs.count; i += 4) {
248 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
249 configs.data.i32[i + 3] ==
250 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
251 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
252 configs.data.i32[i + 2]));
253 }
254 }
255
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800256 return OK;
257}
258
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800259status_t Camera3Device::initialize(sp<CameraProviderManager> manager) {
260 (void) manager;
261 ALOGE("%s: Not supported yet", __FUNCTION__);
262 return INVALID_OPERATION;
263}
264
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800265status_t Camera3Device::disconnect() {
266 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700267 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800268
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700269 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800270
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700271 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800272
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700273 {
274 Mutex::Autolock l(mLock);
275 if (mStatus == STATUS_UNINITIALIZED) return res;
276
277 if (mStatus == STATUS_ACTIVE ||
278 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
279 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700280 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700281 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700282 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700283 } else {
284 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
285 if (res != OK) {
286 SET_ERR_L("Timeout waiting for HAL to drain");
287 // Continue to close device even in case of error
288 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700289 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800290 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800291
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700292 if (mStatus == STATUS_ERROR) {
293 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700294 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700295
296 if (mStatusTracker != NULL) {
297 mStatusTracker->requestExit();
298 }
299
300 if (mRequestThread != NULL) {
301 mRequestThread->requestExit();
302 }
303
304 mOutputStreams.clear();
305 mInputStream.clear();
306 }
307
308 // Joining done without holding mLock, otherwise deadlocks may ensue
309 // as the threads try to access parent state
310 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
311 // HAL may be in a bad state, so waiting for request thread
312 // (which may be stuck in the HAL processCaptureRequest call)
313 // could be dangerous.
314 mRequestThread->join();
315 }
316
317 if (mStatusTracker != NULL) {
318 mStatusTracker->join();
319 }
320
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700321 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700322 {
323 Mutex::Autolock l(mLock);
324
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800325 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700326 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800327 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800328
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700329 hal3Device = mHal3Device;
330 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800331
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700332 // Call close without internal mutex held, as the HAL close may need to
333 // wait on assorted callbacks,etc, to complete before it can return.
334 if (hal3Device != NULL) {
335 ATRACE_BEGIN("camera3->close");
336 hal3Device->common.close(&hal3Device->common);
337 ATRACE_END();
338 }
339
340 {
341 Mutex::Autolock l(mLock);
342 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700343 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700344 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800345
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700346 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700347 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800348}
349
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700350// For dumping/debugging only -
351// try to acquire a lock a few times, eventually give up to proceed with
352// debug/dump operations
353bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
354 bool gotLock = false;
355 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
356 if (lock.tryLock() == NO_ERROR) {
357 gotLock = true;
358 break;
359 } else {
360 usleep(kDumpSleepDuration);
361 }
362 }
363 return gotLock;
364}
365
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700366Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
367 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
368 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
369 const int STREAM_CONFIGURATION_SIZE = 4;
370 const int STREAM_FORMAT_OFFSET = 0;
371 const int STREAM_WIDTH_OFFSET = 1;
372 const int STREAM_HEIGHT_OFFSET = 2;
373 const int STREAM_IS_INPUT_OFFSET = 3;
374 camera_metadata_ro_entry_t availableStreamConfigs =
375 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
376 if (availableStreamConfigs.count == 0 ||
377 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
378 return Size(0, 0);
379 }
380
381 // Get max jpeg size (area-wise).
382 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
383 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
384 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
385 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
386 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
387 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
388 && format == HAL_PIXEL_FORMAT_BLOB &&
389 (width * height > maxJpegWidth * maxJpegHeight)) {
390 maxJpegWidth = width;
391 maxJpegHeight = height;
392 }
393 }
394 } else {
395 camera_metadata_ro_entry availableJpegSizes =
396 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
397 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
398 return Size(0, 0);
399 }
400
401 // Get max jpeg size (area-wise).
402 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
403 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
404 > (maxJpegWidth * maxJpegHeight)) {
405 maxJpegWidth = availableJpegSizes.data.i32[i];
406 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
407 }
408 }
409 }
410 return Size(maxJpegWidth, maxJpegHeight);
411}
412
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800413nsecs_t Camera3Device::getMonoToBoottimeOffset() {
414 // try three times to get the clock offset, choose the one
415 // with the minimum gap in measurements.
416 const int tries = 3;
417 nsecs_t bestGap, measured;
418 for (int i = 0; i < tries; ++i) {
419 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
420 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
421 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
422 const nsecs_t gap = tmono2 - tmono;
423 if (i == 0 || gap < bestGap) {
424 bestGap = gap;
425 measured = tbase - ((tmono + tmono2) >> 1);
426 }
427 }
428 return measured;
429}
430
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -0700431/**
432 * Map Android N dataspace definitions back to Android M definitions, for
433 * use with HALv3.3 or older.
434 *
435 * Only map where correspondences exist, and otherwise preserve the value.
436 */
437android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) {
438 switch (dataSpace) {
439 case HAL_DATASPACE_V0_SRGB_LINEAR:
440 return HAL_DATASPACE_SRGB_LINEAR;
441 case HAL_DATASPACE_V0_SRGB:
442 return HAL_DATASPACE_SRGB;
443 case HAL_DATASPACE_V0_JFIF:
444 return HAL_DATASPACE_JFIF;
445 case HAL_DATASPACE_V0_BT601_625:
446 return HAL_DATASPACE_BT601_625;
447 case HAL_DATASPACE_V0_BT601_525:
448 return HAL_DATASPACE_BT601_525;
449 case HAL_DATASPACE_V0_BT709:
450 return HAL_DATASPACE_BT709;
451 default:
452 return dataSpace;
453 }
454}
455
Zhijun Hef7da0962014-04-24 13:27:56 -0700456ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700457 // Get max jpeg size (area-wise).
458 Size maxJpegResolution = getMaxJpegResolution();
459 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700460 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700461 __FUNCTION__, mId);
462 return BAD_VALUE;
463 }
464
Zhijun Hef7da0962014-04-24 13:27:56 -0700465 // Get max jpeg buffer size
466 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700467 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
468 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700469 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
470 return BAD_VALUE;
471 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700472 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800473 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700474
475 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700476 float scaleFactor = ((float) (width * height)) /
477 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800478 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
479 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700480 if (jpegBufferSize > maxJpegBufferSize) {
481 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700482 }
483
484 return jpegBufferSize;
485}
486
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700487ssize_t Camera3Device::getPointCloudBufferSize() const {
488 const int FLOATS_PER_POINT=4;
489 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
490 if (maxPointCount.count == 0) {
491 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
492 __FUNCTION__, mId);
493 return BAD_VALUE;
494 }
495 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
496 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
497 return maxBytesForPointCloud;
498}
499
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800500ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800501 const int PER_CONFIGURATION_SIZE = 3;
502 const int WIDTH_OFFSET = 0;
503 const int HEIGHT_OFFSET = 1;
504 const int SIZE_OFFSET = 2;
505 camera_metadata_ro_entry rawOpaqueSizes =
506 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800507 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800508 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800509 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800510 __FUNCTION__, mId, count);
511 return BAD_VALUE;
512 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700513
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800514 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
515 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
516 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
517 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
518 }
519 }
520
521 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
522 __FUNCTION__, mId, width, height);
523 return BAD_VALUE;
524}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700525
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800526status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
527 ATRACE_CALL();
528 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700529
530 // Try to lock, but continue in case of failure (to avoid blocking in
531 // deadlocks)
532 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
533 bool gotLock = tryLockSpinRightRound(mLock);
534
535 ALOGW_IF(!gotInterfaceLock,
536 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
537 mId, __FUNCTION__);
538 ALOGW_IF(!gotLock,
539 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
540 mId, __FUNCTION__);
541
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800542 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700543
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800544 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700545 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800546 int n = args.size();
547 for (int i = 0; i < n; i++) {
548 if (args[i] == templatesOption) {
549 dumpTemplates = true;
550 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700551 if (args[i] == monitorOption) {
552 if (i + 1 < n) {
553 String8 monitorTags = String8(args[i + 1]);
554 if (monitorTags == "off") {
555 mTagMonitor.disableMonitoring();
556 } else {
557 mTagMonitor.parseTagsToMonitor(monitorTags);
558 }
559 } else {
560 mTagMonitor.disableMonitoring();
561 }
562 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800563 }
564
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800565 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800566
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800567 const char *status =
568 mStatus == STATUS_ERROR ? "ERROR" :
569 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700570 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
571 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800572 mStatus == STATUS_ACTIVE ? "ACTIVE" :
573 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700574
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800575 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700576 if (mStatus == STATUS_ERROR) {
577 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
578 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800579 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700580 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700581 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800582
583 if (mInputStream != NULL) {
584 write(fd, lines.string(), lines.size());
585 mInputStream->dump(fd, args);
586 } else {
587 lines.appendFormat(" No input stream.\n");
588 write(fd, lines.string(), lines.size());
589 }
590 for (size_t i = 0; i < mOutputStreams.size(); i++) {
591 mOutputStreams[i]->dump(fd,args);
592 }
593
Zhijun He431503c2016-03-07 17:30:16 -0800594 if (mBufferManager != NULL) {
595 lines = String8(" Camera3 Buffer Manager:\n");
596 write(fd, lines.string(), lines.size());
597 mBufferManager->dump(fd, args);
598 }
Zhijun He125684a2015-12-26 15:07:30 -0800599
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700600 lines = String8(" In-flight requests:\n");
601 if (mInFlightMap.size() == 0) {
602 lines.append(" None\n");
603 } else {
604 for (size_t i = 0; i < mInFlightMap.size(); i++) {
605 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700606 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700607 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800608 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700609 r.numBuffersLeft);
610 }
611 }
612 write(fd, lines.string(), lines.size());
613
Igor Murashkin1e479c02013-09-06 16:55:14 -0700614 {
615 lines = String8(" Last request sent:\n");
616 write(fd, lines.string(), lines.size());
617
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700618 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700619 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
620 }
621
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800622 if (dumpTemplates) {
623 const char *templateNames[] = {
624 "TEMPLATE_PREVIEW",
625 "TEMPLATE_STILL_CAPTURE",
626 "TEMPLATE_VIDEO_RECORD",
627 "TEMPLATE_VIDEO_SNAPSHOT",
628 "TEMPLATE_ZERO_SHUTTER_LAG",
629 "TEMPLATE_MANUAL"
630 };
631
632 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
633 const camera_metadata_t *templateRequest;
634 templateRequest =
635 mHal3Device->ops->construct_default_request_settings(
636 mHal3Device, i);
637 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
638 if (templateRequest == NULL) {
639 lines.append(" Not supported\n");
640 write(fd, lines.string(), lines.size());
641 } else {
642 write(fd, lines.string(), lines.size());
643 dump_indented_camera_metadata(templateRequest,
644 fd, /*verbosity*/2, /*indentation*/8);
645 }
646 }
647 }
648
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700649 mTagMonitor.dumpMonitoredMetadata(fd);
650
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800651 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700652 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800653 write(fd, lines.string(), lines.size());
654 mHal3Device->ops->dump(mHal3Device, fd);
655 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800656
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700657 if (gotLock) mLock.unlock();
658 if (gotInterfaceLock) mInterfaceLock.unlock();
659
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800660 return OK;
661}
662
663const CameraMetadata& Camera3Device::info() const {
664 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800665 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
666 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700667 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800668 mStatus == STATUS_ERROR ?
669 "when in error state" : "before init");
670 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800671 return mDeviceInfo;
672}
673
Jianing Wei90e59c92014-03-12 18:29:36 -0700674status_t Camera3Device::checkStatusOkToCaptureLocked() {
675 switch (mStatus) {
676 case STATUS_ERROR:
677 CLOGE("Device has encountered a serious error");
678 return INVALID_OPERATION;
679 case STATUS_UNINITIALIZED:
680 CLOGE("Device not initialized");
681 return INVALID_OPERATION;
682 case STATUS_UNCONFIGURED:
683 case STATUS_CONFIGURED:
684 case STATUS_ACTIVE:
685 // OK
686 break;
687 default:
688 SET_ERR_L("Unexpected status: %d", mStatus);
689 return INVALID_OPERATION;
690 }
691 return OK;
692}
693
694status_t Camera3Device::convertMetadataListToRequestListLocked(
Shuzhen Wang9d066012016-09-30 11:30:20 -0700695 const List<const CameraMetadata> &metadataList, bool repeating,
696 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700697 if (requestList == NULL) {
698 CLOGE("requestList cannot be NULL.");
699 return BAD_VALUE;
700 }
701
Jianing Weicb0652e2014-03-12 18:29:36 -0700702 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700703 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
704 it != metadataList.end(); ++it) {
705 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
706 if (newRequest == 0) {
707 CLOGE("Can't create capture request");
708 return BAD_VALUE;
709 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700710
Shuzhen Wang9d066012016-09-30 11:30:20 -0700711 newRequest->mRepeating = repeating;
712
Jianing Weicb0652e2014-03-12 18:29:36 -0700713 // Setup burst Id and request Id
714 newRequest->mResultExtras.burstId = burstId++;
715 if (it->exists(ANDROID_REQUEST_ID)) {
716 if (it->find(ANDROID_REQUEST_ID).count == 0) {
717 CLOGE("RequestID entry exists; but must not be empty in metadata");
718 return BAD_VALUE;
719 }
720 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
721 } else {
722 CLOGE("RequestID does not exist in metadata");
723 return BAD_VALUE;
724 }
725
Jianing Wei90e59c92014-03-12 18:29:36 -0700726 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700727
728 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700729 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700730
731 // Setup batch size if this is a high speed video recording request.
732 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
733 auto firstRequest = requestList->begin();
734 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
735 if (outputStream->isVideoStream()) {
736 (*firstRequest)->mBatchSize = requestList->size();
737 break;
738 }
739 }
740 }
741
Jianing Wei90e59c92014-03-12 18:29:36 -0700742 return OK;
743}
744
Jianing Weicb0652e2014-03-12 18:29:36 -0700745status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800746 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800747
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700748 List<const CameraMetadata> requests;
749 requests.push_back(request);
750 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800751}
752
Jianing Wei90e59c92014-03-12 18:29:36 -0700753status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700754 const List<const CameraMetadata> &requests, bool repeating,
755 /*out*/
756 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700757 ATRACE_CALL();
758 Mutex::Autolock il(mInterfaceLock);
759 Mutex::Autolock l(mLock);
760
761 status_t res = checkStatusOkToCaptureLocked();
762 if (res != OK) {
763 // error logged by previous call
764 return res;
765 }
766
767 RequestList requestList;
768
Shuzhen Wang9d066012016-09-30 11:30:20 -0700769 res = convertMetadataListToRequestListLocked(requests, repeating,
770 /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700771 if (res != OK) {
772 // error logged by previous call
773 return res;
774 }
775
776 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700777 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700778 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700779 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700780 }
781
782 if (res == OK) {
783 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
784 if (res != OK) {
785 SET_ERR_L("Can't transition to active in %f seconds!",
786 kActiveTimeout/1e9);
787 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700788 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
789 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700790 } else {
791 CLOGE("Cannot queue request. Impossible.");
792 return BAD_VALUE;
793 }
794
795 return res;
796}
797
Jianing Weicb0652e2014-03-12 18:29:36 -0700798status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
799 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700800 ATRACE_CALL();
801
Jianing Weicb0652e2014-03-12 18:29:36 -0700802 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700803}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800804
Jianing Weicb0652e2014-03-12 18:29:36 -0700805status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
806 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800807 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800808
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700809 List<const CameraMetadata> requests;
810 requests.push_back(request);
811 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800812}
813
Jianing Weicb0652e2014-03-12 18:29:36 -0700814status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
815 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700816 ATRACE_CALL();
817
Jianing Weicb0652e2014-03-12 18:29:36 -0700818 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700819}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800820
821sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
822 const CameraMetadata &request) {
823 status_t res;
824
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700825 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800826 res = configureStreamsLocked();
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700827 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800828 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700829 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800830 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700831 } else if (mStatus == STATUS_UNCONFIGURED) {
832 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700833 CLOGE("No streams configured");
834 return NULL;
835 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800836 }
837
838 sp<CaptureRequest> newRequest = createCaptureRequest(request);
839 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800840}
841
Jianing Weicb0652e2014-03-12 18:29:36 -0700842status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800843 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700844 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800845 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800846
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800847 switch (mStatus) {
848 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700849 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800850 return INVALID_OPERATION;
851 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700852 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800853 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700854 case STATUS_UNCONFIGURED:
855 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800856 case STATUS_ACTIVE:
857 // OK
858 break;
859 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700860 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800861 return INVALID_OPERATION;
862 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700863 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700864
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700865 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800866}
867
868status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
869 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700870 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800871
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700872 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800873}
874
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700875status_t Camera3Device::createInputStream(
876 uint32_t width, uint32_t height, int format, int *id) {
877 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700878 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700879 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700880 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
881 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700882
883 status_t res;
884 bool wasActive = false;
885
886 switch (mStatus) {
887 case STATUS_ERROR:
888 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
889 return INVALID_OPERATION;
890 case STATUS_UNINITIALIZED:
891 ALOGE("%s: Device not initialized", __FUNCTION__);
892 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700893 case STATUS_UNCONFIGURED:
894 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700895 // OK
896 break;
897 case STATUS_ACTIVE:
898 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700899 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700900 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700901 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700902 return res;
903 }
904 wasActive = true;
905 break;
906 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700907 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700908 return INVALID_OPERATION;
909 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700910 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700911
912 if (mInputStream != 0) {
913 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
914 return INVALID_OPERATION;
915 }
916
917 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
918 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700919 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700920
921 mInputStream = newStream;
922
923 *id = mNextStreamId++;
924
925 // Continue captures if active at start
926 if (wasActive) {
927 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
928 res = configureStreamsLocked();
929 if (res != OK) {
930 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
931 __FUNCTION__, mNextStreamId, strerror(-res), res);
932 return res;
933 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700934 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700935 }
936
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700937 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700938 return OK;
939}
940
Igor Murashkin2fba5842013-04-22 14:03:54 -0700941
942status_t Camera3Device::createZslStream(
943 uint32_t width, uint32_t height,
944 int depth,
945 /*out*/
946 int *id,
947 sp<Camera3ZslStream>* zslStream) {
948 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700949 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700950 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700951 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
952 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700953
954 status_t res;
955 bool wasActive = false;
956
957 switch (mStatus) {
958 case STATUS_ERROR:
959 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
960 return INVALID_OPERATION;
961 case STATUS_UNINITIALIZED:
962 ALOGE("%s: Device not initialized", __FUNCTION__);
963 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700964 case STATUS_UNCONFIGURED:
965 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700966 // OK
967 break;
968 case STATUS_ACTIVE:
969 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700970 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700971 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700972 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700973 return res;
974 }
975 wasActive = true;
976 break;
977 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700978 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700979 return INVALID_OPERATION;
980 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700981 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700982
983 if (mInputStream != 0) {
984 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
985 return INVALID_OPERATION;
986 }
987
988 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
989 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700990 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700991
992 res = mOutputStreams.add(mNextStreamId, newStream);
993 if (res < 0) {
994 ALOGE("%s: Can't add new stream to set: %s (%d)",
995 __FUNCTION__, strerror(-res), res);
996 return res;
997 }
998 mInputStream = newStream;
999
Yuvraj Pasie5e3d082014-04-15 18:37:45 +05301000 mNeedConfig = true;
1001
Igor Murashkin2fba5842013-04-22 14:03:54 -07001002 *id = mNextStreamId++;
1003 *zslStream = newStream;
1004
1005 // Continue captures if active at start
1006 if (wasActive) {
1007 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1008 res = configureStreamsLocked();
1009 if (res != OK) {
1010 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1011 __FUNCTION__, mNextStreamId, strerror(-res), res);
1012 return res;
1013 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001014 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -07001015 }
1016
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001017 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -07001018 return OK;
1019}
1020
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001021status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001022 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -07001023 camera3_stream_rotation_t rotation, int *id, int streamSetId, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001024 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001025 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001026 Mutex::Autolock l(mLock);
Zhijun He5d677d12016-05-29 16:52:39 -07001027 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
1028 " consumer usage 0x%x", mId, mNextStreamId, width, height, format, dataSpace, rotation,
1029 consumerUsage);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001030
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001031 status_t res;
1032 bool wasActive = false;
1033
1034 switch (mStatus) {
1035 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001036 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001037 return INVALID_OPERATION;
1038 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001039 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001040 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001041 case STATUS_UNCONFIGURED:
1042 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001043 // OK
1044 break;
1045 case STATUS_ACTIVE:
1046 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001047 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001048 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001049 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001050 return res;
1051 }
1052 wasActive = true;
1053 break;
1054 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001055 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001056 return INVALID_OPERATION;
1057 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001058 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001059
1060 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001061 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001062 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001063 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001064 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1065 }
Zhijun He5d677d12016-05-29 16:52:39 -07001066
1067 // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
1068 // which requires a consumer surface to be available.
1069 if (consumer == nullptr && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1070 ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
1071 return BAD_VALUE;
1072 }
1073
1074 if (consumer == nullptr && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1075 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1076 return BAD_VALUE;
1077 }
1078
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001079 // Use legacy dataspace values for older HALs
1080 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1081 dataSpace = mapToLegacyDataspace(dataSpace);
1082 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001083 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001084 ssize_t blobBufferSize;
1085 if (dataSpace != HAL_DATASPACE_DEPTH) {
1086 blobBufferSize = getJpegBufferSize(width, height);
1087 if (blobBufferSize <= 0) {
1088 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1089 return BAD_VALUE;
1090 }
1091 } else {
1092 blobBufferSize = getPointCloudBufferSize();
1093 if (blobBufferSize <= 0) {
1094 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1095 return BAD_VALUE;
1096 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001097 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001098 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001099 width, height, blobBufferSize, format, dataSpace, rotation,
1100 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001101 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1102 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1103 if (rawOpaqueBufferSize <= 0) {
1104 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1105 return BAD_VALUE;
1106 }
1107 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001108 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1109 mTimestampOffset, streamSetId);
Zhijun He5d677d12016-05-29 16:52:39 -07001110 } else if (consumer == nullptr) {
1111 newStream = new Camera3OutputStream(mNextStreamId,
1112 width, height, format, consumerUsage, dataSpace, rotation,
1113 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001114 } else {
1115 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001116 width, height, format, dataSpace, rotation,
1117 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001118 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001119 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001120
Zhijun He125684a2015-12-26 15:07:30 -08001121 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001122 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1123 * requires buffers to be statically allocated for internal static buffer registration, while
1124 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1125 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001126 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001127 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001128 newStream->setBufferManager(mBufferManager);
1129 }
1130
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001131 res = mOutputStreams.add(mNextStreamId, newStream);
1132 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001133 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001134 return res;
1135 }
1136
1137 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001138 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001139
1140 // Continue captures if active at start
1141 if (wasActive) {
1142 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1143 res = configureStreamsLocked();
1144 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001145 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1146 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001147 return res;
1148 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001149 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001150 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001151 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001152 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001153}
1154
1155status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1156 ATRACE_CALL();
1157 (void)outputId; (void)id;
1158
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001159 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001160 return INVALID_OPERATION;
1161}
1162
1163
1164status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001165 uint32_t *width, uint32_t *height,
1166 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001167 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001168 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001169 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001170
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001171 switch (mStatus) {
1172 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001173 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001174 return INVALID_OPERATION;
1175 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001176 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001178 case STATUS_UNCONFIGURED:
1179 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001180 case STATUS_ACTIVE:
1181 // OK
1182 break;
1183 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001184 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001185 return INVALID_OPERATION;
1186 }
1187
1188 ssize_t idx = mOutputStreams.indexOfKey(id);
1189 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001190 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001191 return idx;
1192 }
1193
1194 if (width) *width = mOutputStreams[idx]->getWidth();
1195 if (height) *height = mOutputStreams[idx]->getHeight();
1196 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001197 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001198 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001199}
1200
1201status_t Camera3Device::setStreamTransform(int id,
1202 int transform) {
1203 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001204 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001205 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001206
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001207 switch (mStatus) {
1208 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001209 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001210 return INVALID_OPERATION;
1211 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001212 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001213 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001214 case STATUS_UNCONFIGURED:
1215 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001216 case STATUS_ACTIVE:
1217 // OK
1218 break;
1219 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001220 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001221 return INVALID_OPERATION;
1222 }
1223
1224 ssize_t idx = mOutputStreams.indexOfKey(id);
1225 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001226 CLOGE("Stream %d does not exist",
1227 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001228 return BAD_VALUE;
1229 }
1230
1231 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001232}
1233
1234status_t Camera3Device::deleteStream(int id) {
1235 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001236 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001237 Mutex::Autolock l(mLock);
1238 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001239
Igor Murashkine2172be2013-05-28 15:31:39 -07001240 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1241
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001242 // CameraDevice semantics require device to already be idle before
1243 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001244 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001245 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1246 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001247 }
1248
Igor Murashkin2fba5842013-04-22 14:03:54 -07001249 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001250 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001251 if (mInputStream != NULL && id == mInputStream->getId()) {
1252 deletedStream = mInputStream;
1253 mInputStream.clear();
1254 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001255 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001256 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001257 return BAD_VALUE;
1258 }
Zhijun He5f446352014-01-22 09:49:33 -08001259 }
1260
1261 // Delete output stream or the output part of a bi-directional stream.
1262 if (outputStreamIdx != NAME_NOT_FOUND) {
1263 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001264 mOutputStreams.removeItem(id);
1265 }
1266
1267 // Free up the stream endpoint so that it can be used by some other stream
1268 res = deletedStream->disconnect();
1269 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001270 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001271 // fall through since we want to still list the stream as deleted.
1272 }
1273 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001274 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001275
1276 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001277}
1278
1279status_t Camera3Device::deleteReprocessStream(int id) {
1280 ATRACE_CALL();
1281 (void)id;
1282
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001283 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001284 return INVALID_OPERATION;
1285}
1286
Zhijun He1fa89992015-06-01 15:44:31 -07001287status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001288 ATRACE_CALL();
1289 ALOGV("%s: E", __FUNCTION__);
1290
1291 Mutex::Autolock il(mInterfaceLock);
1292 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001293
1294 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1295 mNeedConfig = true;
1296 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1297 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001298
1299 return configureStreamsLocked();
1300}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001301
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001302status_t Camera3Device::getInputBufferProducer(
1303 sp<IGraphicBufferProducer> *producer) {
1304 Mutex::Autolock il(mInterfaceLock);
1305 Mutex::Autolock l(mLock);
1306
1307 if (producer == NULL) {
1308 return BAD_VALUE;
1309 } else if (mInputStream == NULL) {
1310 return INVALID_OPERATION;
1311 }
1312
1313 return mInputStream->getInputBufferProducer(producer);
1314}
1315
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001316status_t Camera3Device::createDefaultRequest(int templateId,
1317 CameraMetadata *request) {
1318 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001319 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001320
1321 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1322 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1323 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1324 return BAD_VALUE;
1325 }
1326
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001327 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001328 Mutex::Autolock l(mLock);
1329
1330 switch (mStatus) {
1331 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001332 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001333 return INVALID_OPERATION;
1334 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001335 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001336 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001337 case STATUS_UNCONFIGURED:
1338 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001339 case STATUS_ACTIVE:
1340 // OK
1341 break;
1342 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001343 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001344 return INVALID_OPERATION;
1345 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001346
Zhijun Hea1530f12014-09-14 12:44:20 -07001347 if (!mRequestTemplateCache[templateId].isEmpty()) {
1348 *request = mRequestTemplateCache[templateId];
1349 return OK;
1350 }
1351
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001352 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001353 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001354 rawRequest = mHal3Device->ops->construct_default_request_settings(
1355 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001356 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001357 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001358 ALOGI("%s: template %d is not supported on this camera device",
1359 __FUNCTION__, templateId);
1360 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001361 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001362
Zhijun Hea1530f12014-09-14 12:44:20 -07001363 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001364
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001365 // Derive some new keys for backward compatibility
1366 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
1367 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
1368 int32_t defaultBoost[1] = {100};
1369 mRequestTemplateCache[templateId].update(
1370 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1371 defaultBoost, 1);
1372 }
1373
1374 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001375 return OK;
1376}
1377
1378status_t Camera3Device::waitUntilDrained() {
1379 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001380 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001382
Zhijun He69a37482014-03-23 18:44:49 -07001383 return waitUntilDrainedLocked();
1384}
1385
1386status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001387 switch (mStatus) {
1388 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001389 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001390 ALOGV("%s: Already idle", __FUNCTION__);
1391 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001392 case STATUS_CONFIGURED:
1393 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001394 case STATUS_ERROR:
1395 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001396 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001397 break;
1398 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001399 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001400 return INVALID_OPERATION;
1401 }
1402
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001403 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1404 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001405 if (res != OK) {
1406 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1407 res);
1408 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001409 return res;
1410}
1411
Ruben Brunk183f0562015-08-12 12:55:02 -07001412
1413void Camera3Device::internalUpdateStatusLocked(Status status) {
1414 mStatus = status;
1415 mRecentStatusUpdates.add(mStatus);
1416 mStatusChanged.broadcast();
1417}
1418
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001419// Pause to reconfigure
1420status_t Camera3Device::internalPauseAndWaitLocked() {
1421 mRequestThread->setPaused(true);
1422 mPauseStateNotify = true;
1423
1424 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1425 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1426 if (res != OK) {
1427 SET_ERR_L("Can't idle device in %f seconds!",
1428 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001429 }
1430
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001431 return res;
1432}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001433
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001434// Resume after internalPauseAndWaitLocked
1435status_t Camera3Device::internalResumeLocked() {
1436 status_t res;
1437
1438 mRequestThread->setPaused(false);
1439
1440 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1441 if (res != OK) {
1442 SET_ERR_L("Can't transition to active in %f seconds!",
1443 kActiveTimeout/1e9);
1444 }
1445 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001446 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001447}
1448
Ruben Brunk183f0562015-08-12 12:55:02 -07001449status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001450 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001451
1452 size_t startIndex = 0;
1453 if (mStatusWaiters == 0) {
1454 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1455 // this status list
1456 mRecentStatusUpdates.clear();
1457 } else {
1458 // If other threads are waiting on updates to this status list, set the position of the
1459 // first element that this list will check rather than clearing the list.
1460 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001461 }
1462
Ruben Brunk183f0562015-08-12 12:55:02 -07001463 mStatusWaiters++;
1464
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001465 bool stateSeen = false;
1466 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001467 if (active == (mStatus == STATUS_ACTIVE)) {
1468 // Desired state is current
1469 break;
1470 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001471
1472 res = mStatusChanged.waitRelative(mLock, timeout);
1473 if (res != OK) break;
1474
Ruben Brunk183f0562015-08-12 12:55:02 -07001475 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1476 // transitions.
1477 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1478 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1479 __FUNCTION__);
1480
1481 // Encountered desired state since we began waiting
1482 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001483 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1484 stateSeen = true;
1485 break;
1486 }
1487 }
1488 } while (!stateSeen);
1489
Ruben Brunk183f0562015-08-12 12:55:02 -07001490 mStatusWaiters--;
1491
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001492 return res;
1493}
1494
1495
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001496status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001497 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001498 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001499
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001500 if (listener != NULL && mListener != NULL) {
1501 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1502 }
1503 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001504 mRequestThread->setNotificationListener(listener);
1505 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001506
1507 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001508}
1509
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001510bool Camera3Device::willNotify3A() {
1511 return false;
1512}
1513
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001514status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001515 status_t res;
1516 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001517
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001518 while (mResultQueue.empty()) {
1519 res = mResultSignal.waitRelative(mOutputLock, timeout);
1520 if (res == TIMED_OUT) {
1521 return res;
1522 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001523 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001524 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001525 return res;
1526 }
1527 }
1528 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001529}
1530
Jianing Weicb0652e2014-03-12 18:29:36 -07001531status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001532 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001533 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001534
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001535 if (mResultQueue.empty()) {
1536 return NOT_ENOUGH_DATA;
1537 }
1538
Jianing Weicb0652e2014-03-12 18:29:36 -07001539 if (frame == NULL) {
1540 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1541 return BAD_VALUE;
1542 }
1543
1544 CaptureResult &result = *(mResultQueue.begin());
1545 frame->mResultExtras = result.mResultExtras;
1546 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001547 mResultQueue.erase(mResultQueue.begin());
1548
1549 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001550}
1551
1552status_t Camera3Device::triggerAutofocus(uint32_t id) {
1553 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001554 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001555
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001556 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1557 // Mix-in this trigger into the next request and only the next request.
1558 RequestTrigger trigger[] = {
1559 {
1560 ANDROID_CONTROL_AF_TRIGGER,
1561 ANDROID_CONTROL_AF_TRIGGER_START
1562 },
1563 {
1564 ANDROID_CONTROL_AF_TRIGGER_ID,
1565 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001566 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001567 };
1568
1569 return mRequestThread->queueTrigger(trigger,
1570 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001571}
1572
1573status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1574 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001575 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001576
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001577 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1578 // Mix-in this trigger into the next request and only the next request.
1579 RequestTrigger trigger[] = {
1580 {
1581 ANDROID_CONTROL_AF_TRIGGER,
1582 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1583 },
1584 {
1585 ANDROID_CONTROL_AF_TRIGGER_ID,
1586 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001587 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001588 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001589
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001590 return mRequestThread->queueTrigger(trigger,
1591 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001592}
1593
1594status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1595 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001596 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001597
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001598 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1599 // Mix-in this trigger into the next request and only the next request.
1600 RequestTrigger trigger[] = {
1601 {
1602 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1603 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1604 },
1605 {
1606 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1607 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001608 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001609 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001610
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001611 return mRequestThread->queueTrigger(trigger,
1612 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001613}
1614
1615status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1616 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1617 ATRACE_CALL();
1618 (void)reprocessStreamId; (void)buffer; (void)listener;
1619
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001620 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001621 return INVALID_OPERATION;
1622}
1623
Jianing Weicb0652e2014-03-12 18:29:36 -07001624status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001625 ATRACE_CALL();
1626 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001627 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001628
Zhijun He7ef20392014-04-21 16:04:17 -07001629 {
1630 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001631 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001632 }
1633
Zhijun He491e3412013-12-27 10:57:44 -08001634 status_t res;
1635 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001636 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001637 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001638 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001639 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001640 }
1641
1642 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001643}
1644
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001645status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001646 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1647}
1648
1649status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001650 ATRACE_CALL();
1651 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001652 Mutex::Autolock il(mInterfaceLock);
1653 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001654
1655 sp<Camera3StreamInterface> stream;
1656 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1657 if (outputStreamIdx == NAME_NOT_FOUND) {
1658 CLOGE("Stream %d does not exist", streamId);
1659 return BAD_VALUE;
1660 }
1661
1662 stream = mOutputStreams.editValueAt(outputStreamIdx);
1663
1664 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001665 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001666 return BAD_VALUE;
1667 }
1668
1669 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001670 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001671 return BAD_VALUE;
1672 }
1673
Ruben Brunkc78ac262015-08-13 17:58:46 -07001674 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001675}
1676
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001677status_t Camera3Device::tearDown(int streamId) {
1678 ATRACE_CALL();
1679 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1680 Mutex::Autolock il(mInterfaceLock);
1681 Mutex::Autolock l(mLock);
1682
1683 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1684 // since we cannot call register_stream_buffers except right after configure_streams.
1685 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1686 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1687 __FUNCTION__, mHal3Device->common.version);
1688 return NO_INIT;
1689 }
1690
1691 sp<Camera3StreamInterface> stream;
1692 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1693 if (outputStreamIdx == NAME_NOT_FOUND) {
1694 CLOGE("Stream %d does not exist", streamId);
1695 return BAD_VALUE;
1696 }
1697
1698 stream = mOutputStreams.editValueAt(outputStreamIdx);
1699
1700 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1701 CLOGE("Stream %d is a target of a in-progress request", streamId);
1702 return BAD_VALUE;
1703 }
1704
1705 return stream->tearDown();
1706}
1707
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001708status_t Camera3Device::addBufferListenerForStream(int streamId,
1709 wp<Camera3StreamBufferListener> listener) {
1710 ATRACE_CALL();
1711 ALOGV("%s: Camera %d: Adding buffer listener for stream %d", __FUNCTION__, mId, streamId);
1712 Mutex::Autolock il(mInterfaceLock);
1713 Mutex::Autolock l(mLock);
1714
1715 sp<Camera3StreamInterface> stream;
1716 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1717 if (outputStreamIdx == NAME_NOT_FOUND) {
1718 CLOGE("Stream %d does not exist", streamId);
1719 return BAD_VALUE;
1720 }
1721
1722 stream = mOutputStreams.editValueAt(outputStreamIdx);
1723 stream->addBufferListener(listener);
1724
1725 return OK;
1726}
1727
Zhijun He204e3292014-07-14 17:09:23 -07001728uint32_t Camera3Device::getDeviceVersion() {
1729 ATRACE_CALL();
1730 Mutex::Autolock il(mInterfaceLock);
1731 return mDeviceVersion;
1732}
1733
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001734/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001735 * Methods called by subclasses
1736 */
1737
1738void Camera3Device::notifyStatus(bool idle) {
1739 {
1740 // Need mLock to safely update state and synchronize to current
1741 // state of methods in flight.
1742 Mutex::Autolock l(mLock);
1743 // We can get various system-idle notices from the status tracker
1744 // while starting up. Only care about them if we've actually sent
1745 // in some requests recently.
1746 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1747 return;
1748 }
1749 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1750 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001751 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001752
1753 // Skip notifying listener if we're doing some user-transparent
1754 // state changes
1755 if (mPauseStateNotify) return;
1756 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001757
1758 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001759 {
1760 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001761 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001762 }
1763 if (idle && listener != NULL) {
1764 listener->notifyIdle();
1765 }
1766}
1767
Zhijun He5d677d12016-05-29 16:52:39 -07001768status_t Camera3Device::setConsumerSurface(int streamId, sp<Surface> consumer) {
1769 ATRACE_CALL();
1770 ALOGV("%s: Camera %d: set consumer surface for stream %d", __FUNCTION__, mId, streamId);
1771 Mutex::Autolock il(mInterfaceLock);
1772 Mutex::Autolock l(mLock);
1773
1774 if (consumer == nullptr) {
1775 CLOGE("Null consumer is passed!");
1776 return BAD_VALUE;
1777 }
1778
1779 ssize_t idx = mOutputStreams.indexOfKey(streamId);
1780 if (idx == NAME_NOT_FOUND) {
1781 CLOGE("Stream %d is unknown", streamId);
1782 return idx;
1783 }
1784 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
1785 status_t res = stream->setConsumer(consumer);
1786 if (res != OK) {
1787 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1788 return res;
1789 }
1790
1791 if (!stream->isConfiguring()) {
1792 CLOGE("Stream %d was already fully configured.", streamId);
1793 return INVALID_OPERATION;
1794 }
1795
1796 res = stream->finishConfiguration(mHal3Device);
1797 if (res != OK) {
1798 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1799 stream->getId(), strerror(-res), res);
1800 return res;
1801 }
1802
1803 return OK;
1804}
1805
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001806/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001807 * Camera3Device private methods
1808 */
1809
1810sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1811 const CameraMetadata &request) {
1812 ATRACE_CALL();
1813 status_t res;
1814
1815 sp<CaptureRequest> newRequest = new CaptureRequest;
1816 newRequest->mSettings = request;
1817
1818 camera_metadata_entry_t inputStreams =
1819 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1820 if (inputStreams.count > 0) {
1821 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001822 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001823 CLOGE("Request references unknown input stream %d",
1824 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001825 return NULL;
1826 }
1827 // Lazy completion of stream configuration (allocation/registration)
1828 // on first use
1829 if (mInputStream->isConfiguring()) {
1830 res = mInputStream->finishConfiguration(mHal3Device);
1831 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001832 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001833 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001834 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001835 return NULL;
1836 }
1837 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001838 // Check if stream is being prepared
1839 if (mInputStream->isPreparing()) {
1840 CLOGE("Request references an input stream that's being prepared!");
1841 return NULL;
1842 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001843
1844 newRequest->mInputStream = mInputStream;
1845 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1846 }
1847
1848 camera_metadata_entry_t streams =
1849 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1850 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001851 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001852 return NULL;
1853 }
1854
1855 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001856 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001857 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001858 CLOGE("Request references unknown stream %d",
1859 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001860 return NULL;
1861 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001862 sp<Camera3OutputStreamInterface> stream =
1863 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001864
Zhijun He5d677d12016-05-29 16:52:39 -07001865 // It is illegal to include a deferred consumer output stream into a request
1866 if (stream->isConsumerConfigurationDeferred()) {
1867 CLOGE("Stream %d hasn't finished configuration yet due to deferred consumer",
1868 stream->getId());
1869 return NULL;
1870 }
1871
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001872 // Lazy completion of stream configuration (allocation/registration)
1873 // on first use
1874 if (stream->isConfiguring()) {
1875 res = stream->finishConfiguration(mHal3Device);
1876 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001877 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1878 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001879 return NULL;
1880 }
1881 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001882 // Check if stream is being prepared
1883 if (stream->isPreparing()) {
1884 CLOGE("Request references an output stream that's being prepared!");
1885 return NULL;
1886 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001887
1888 newRequest->mOutputStreams.push(stream);
1889 }
1890 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001891 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001892
1893 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001894}
1895
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001896bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1897 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1898 Size size = mSupportedOpaqueInputSizes[i];
1899 if (size.width == width && size.height == height) {
1900 return true;
1901 }
1902 }
1903
1904 return false;
1905}
1906
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001907void Camera3Device::cancelStreamsConfigurationLocked() {
1908 int res = OK;
1909 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1910 res = mInputStream->cancelConfiguration();
1911 if (res != OK) {
1912 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
1913 mInputStream->getId(), strerror(-res), res);
1914 }
1915 }
1916
1917 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1918 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
1919 if (outputStream->isConfiguring()) {
1920 res = outputStream->cancelConfiguration();
1921 if (res != OK) {
1922 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
1923 outputStream->getId(), strerror(-res), res);
1924 }
1925 }
1926 }
1927
1928 // Return state to that at start of call, so that future configures
1929 // properly clean things up
1930 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
1931 mNeedConfig = true;
1932}
1933
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001934status_t Camera3Device::configureStreamsLocked() {
1935 ATRACE_CALL();
1936 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001937
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001938 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001939 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001940 return INVALID_OPERATION;
1941 }
1942
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001943 if (!mNeedConfig) {
1944 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1945 return OK;
1946 }
1947
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001948 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1949 // adding a dummy stream instead.
1950 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1951 if (mOutputStreams.size() == 0) {
1952 addDummyStreamLocked();
1953 } else {
1954 tryRemoveDummyStreamLocked();
1955 }
1956
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001957 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001958 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001959
1960 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001961 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1962 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1963 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001964 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1965
1966 Vector<camera3_stream_t*> streams;
1967 streams.setCapacity(config.num_streams);
1968
1969 if (mInputStream != NULL) {
1970 camera3_stream_t *inputStream;
1971 inputStream = mInputStream->startConfiguration();
1972 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001973 CLOGE("Can't start input stream configuration");
1974 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001975 return INVALID_OPERATION;
1976 }
1977 streams.add(inputStream);
1978 }
1979
1980 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001981
1982 // Don't configure bidi streams twice, nor add them twice to the list
1983 if (mOutputStreams[i].get() ==
1984 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1985
1986 config.num_streams--;
1987 continue;
1988 }
1989
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001990 camera3_stream_t *outputStream;
1991 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1992 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001993 CLOGE("Can't start output stream configuration");
1994 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001995 return INVALID_OPERATION;
1996 }
1997 streams.add(outputStream);
1998 }
1999
2000 config.streams = streams.editArray();
2001
2002 // Do the HAL configuration; will potentially touch stream
2003 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002004 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002005 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002006 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002007
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002008 if (res == BAD_VALUE) {
2009 // HAL rejected this set of streams as unsupported, clean up config
2010 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002011 CLOGE("Set of requested inputs/outputs not supported by HAL");
2012 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002013 return BAD_VALUE;
2014 } else if (res != OK) {
2015 // Some other kind of error from configure_streams - this is not
2016 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002017 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2018 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002019 return res;
2020 }
2021
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002022 // Finish all stream configuration immediately.
2023 // TODO: Try to relax this later back to lazy completion, which should be
2024 // faster
2025
Igor Murashkin073f8572013-05-02 14:59:28 -07002026 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002027 res = mInputStream->finishConfiguration(mHal3Device);
2028 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002029 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002030 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002031 cancelStreamsConfigurationLocked();
2032 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002033 }
2034 }
2035
2036 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002037 sp<Camera3OutputStreamInterface> outputStream =
2038 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002039 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002040 res = outputStream->finishConfiguration(mHal3Device);
2041 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002042 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002043 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002044 cancelStreamsConfigurationLocked();
2045 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002046 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002047 }
2048 }
2049
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002050 // Request thread needs to know to avoid using repeat-last-settings protocol
2051 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002052 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002053
Zhijun He90f7c372016-08-16 16:19:43 -07002054 char value[PROPERTY_VALUE_MAX];
2055 property_get("camera.fifo.disable", value, "0");
2056 int32_t disableFifo = atoi(value);
2057 if (disableFifo != 1) {
2058 // Boost priority of request thread to SCHED_FIFO.
2059 pid_t requestThreadTid = mRequestThread->getTid();
2060 res = requestPriority(getpid(), requestThreadTid,
2061 kRequestThreadPriority, /*asynchronous*/ false);
2062 if (res != OK) {
2063 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2064 strerror(-res), res);
2065 } else {
2066 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2067 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002068 }
2069
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002070 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002071
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002072 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002073
Ruben Brunk183f0562015-08-12 12:55:02 -07002074 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2075 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002076
2077 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
2078
Zhijun He0a210512014-07-24 13:45:15 -07002079 // tear down the deleted streams after configure streams.
2080 mDeletedStreams.clear();
2081
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002082 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002083}
2084
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002085status_t Camera3Device::addDummyStreamLocked() {
2086 ATRACE_CALL();
2087 status_t res;
2088
2089 if (mDummyStreamId != NO_STREAM) {
2090 // Should never be adding a second dummy stream when one is already
2091 // active
2092 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
2093 __FUNCTION__, mId);
2094 return INVALID_OPERATION;
2095 }
2096
2097 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
2098
2099 sp<Camera3OutputStreamInterface> dummyStream =
2100 new Camera3DummyStream(mNextStreamId);
2101
2102 res = mOutputStreams.add(mNextStreamId, dummyStream);
2103 if (res < 0) {
2104 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2105 return res;
2106 }
2107
2108 mDummyStreamId = mNextStreamId;
2109 mNextStreamId++;
2110
2111 return OK;
2112}
2113
2114status_t Camera3Device::tryRemoveDummyStreamLocked() {
2115 ATRACE_CALL();
2116 status_t res;
2117
2118 if (mDummyStreamId == NO_STREAM) return OK;
2119 if (mOutputStreams.size() == 1) return OK;
2120
2121 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
2122
2123 // Ok, have a dummy stream and there's at least one other output stream,
2124 // so remove the dummy
2125
2126 sp<Camera3StreamInterface> deletedStream;
2127 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2128 if (outputStreamIdx == NAME_NOT_FOUND) {
2129 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2130 return INVALID_OPERATION;
2131 }
2132
2133 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2134 mOutputStreams.removeItemsAt(outputStreamIdx);
2135
2136 // Free up the stream endpoint so that it can be used by some other stream
2137 res = deletedStream->disconnect();
2138 if (res != OK) {
2139 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2140 // fall through since we want to still list the stream as deleted.
2141 }
2142 mDeletedStreams.add(deletedStream);
2143 mDummyStreamId = NO_STREAM;
2144
2145 return res;
2146}
2147
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002148void Camera3Device::setErrorState(const char *fmt, ...) {
2149 Mutex::Autolock l(mLock);
2150 va_list args;
2151 va_start(args, fmt);
2152
2153 setErrorStateLockedV(fmt, args);
2154
2155 va_end(args);
2156}
2157
2158void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2159 Mutex::Autolock l(mLock);
2160 setErrorStateLockedV(fmt, args);
2161}
2162
2163void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2164 va_list args;
2165 va_start(args, fmt);
2166
2167 setErrorStateLockedV(fmt, args);
2168
2169 va_end(args);
2170}
2171
2172void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002173 // Print out all error messages to log
2174 String8 errorCause = String8::formatV(fmt, args);
2175 ALOGE("Camera %d: %s", mId, errorCause.string());
2176
2177 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002178 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002179
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002180 mErrorCause = errorCause;
2181
2182 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002183 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002184
2185 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002186 sp<NotificationListener> listener = mListener.promote();
2187 if (listener != NULL) {
2188 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002189 CaptureResultExtras());
2190 }
2191
2192 // Save stack trace. View by dumping it later.
2193 CameraTraces::saveTrace();
2194 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002195}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002196
2197/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002198 * In-flight request management
2199 */
2200
Jianing Weicb0652e2014-03-12 18:29:36 -07002201status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002202 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2203 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002204 ATRACE_CALL();
2205 Mutex::Autolock l(mInFlightLock);
2206
2207 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002208 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2209 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002210 if (res < 0) return res;
2211
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002212 if (mInFlightMap.size() == 1) {
2213 mStatusTracker->markComponentActive(mInFlightStatusId);
2214 }
2215
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002216 return OK;
2217}
2218
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002219void Camera3Device::returnOutputBuffers(
2220 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2221 nsecs_t timestamp) {
2222 for (size_t i = 0; i < numBuffers; i++)
2223 {
2224 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2225 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2226 // Note: stream may be deallocated at this point, if this buffer was
2227 // the last reference to it.
2228 if (res != OK) {
2229 ALOGE("Can't return buffer to its stream: %s (%d)",
2230 strerror(-res), res);
2231 }
2232 }
2233}
2234
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002235void Camera3Device::removeInFlightMapEntryLocked(int idx) {
2236 mInFlightMap.removeItemsAt(idx, 1);
2237
2238 // Indicate idle inFlightMap to the status tracker
2239 if (mInFlightMap.size() == 0) {
2240 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2241 }
2242}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002243
2244void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2245
2246 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2247 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2248
2249 nsecs_t sensorTimestamp = request.sensorTimestamp;
2250 nsecs_t shutterTimestamp = request.shutterTimestamp;
2251
2252 // Check if it's okay to remove the request from InFlightMap:
2253 // In the case of a successful request:
2254 // all input and output buffers, all result metadata, shutter callback
2255 // arrived.
2256 // In the case of a unsuccessful request:
2257 // all input and output buffers arrived.
2258 if (request.numBuffersLeft == 0 &&
2259 (request.requestStatus != OK ||
2260 (request.haveResultMetadata && shutterTimestamp != 0))) {
2261 ATRACE_ASYNC_END("frame capture", frameNumber);
2262
2263 // Sanity check - if sensor timestamp matches shutter timestamp
2264 if (request.requestStatus == OK &&
2265 sensorTimestamp != shutterTimestamp) {
2266 SET_ERR("sensor timestamp (%" PRId64
2267 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2268 sensorTimestamp, frameNumber, shutterTimestamp);
2269 }
2270
2271 // for an unsuccessful request, it may have pending output buffers to
2272 // return.
2273 assert(request.requestStatus != OK ||
2274 request.pendingOutputBuffers.size() == 0);
2275 returnOutputBuffers(request.pendingOutputBuffers.array(),
2276 request.pendingOutputBuffers.size(), 0);
2277
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002278 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002279 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2280 }
2281
2282 // Sanity check - if we have too many in-flight frames, something has
2283 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002284 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002285 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002286 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2287 kInFlightWarnLimitHighSpeed) {
2288 CLOGE("In-flight list too large for high speed configuration: %zu",
2289 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002290 }
2291}
2292
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002293void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2294 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2295 if (result == nullptr) return;
2296
2297 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2298 (int32_t*)&frameNumber, 1) != OK) {
2299 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2300 return;
2301 }
2302
2303 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2304 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2305 return;
2306 }
2307
2308 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2309
2310 // Valid result, insert into queue
2311 List<CaptureResult>::iterator queuedResult =
2312 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2313 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2314 ", burstId = %" PRId32, __FUNCTION__,
2315 queuedResult->mResultExtras.requestId,
2316 queuedResult->mResultExtras.frameNumber,
2317 queuedResult->mResultExtras.burstId);
2318
2319 mResultSignal.signal();
2320}
2321
2322
2323void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2324 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2325 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2326 Mutex::Autolock l(mOutputLock);
2327
2328 CaptureResult captureResult;
2329 captureResult.mResultExtras = resultExtras;
2330 captureResult.mMetadata = partialResult;
2331
2332 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2333}
2334
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002335
2336void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2337 CaptureResultExtras &resultExtras,
2338 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002339 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002340 bool reprocess,
2341 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002342 if (pendingMetadata.isEmpty())
2343 return;
2344
2345 Mutex::Autolock l(mOutputLock);
2346
2347 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002348 if (reprocess) {
2349 if (frameNumber < mNextReprocessResultFrameNumber) {
2350 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002351 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002352 frameNumber, mNextReprocessResultFrameNumber);
2353 return;
2354 }
2355 mNextReprocessResultFrameNumber = frameNumber + 1;
2356 } else {
2357 if (frameNumber < mNextResultFrameNumber) {
2358 SET_ERR("Out-of-order capture result metadata submitted! "
2359 "(got frame number %d, expecting %d)",
2360 frameNumber, mNextResultFrameNumber);
2361 return;
2362 }
2363 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002364 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002365
2366 CaptureResult captureResult;
2367 captureResult.mResultExtras = resultExtras;
2368 captureResult.mMetadata = pendingMetadata;
2369
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002370 // Append any previous partials to form a complete result
2371 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2372 captureResult.mMetadata.append(collectedPartialResult);
2373 }
2374
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07002375 // Derive some new keys for backward compaibility
2376 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
2377 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
2378 int32_t defaultBoost[1] = {100};
2379 captureResult.mMetadata.update(
2380 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
2381 defaultBoost, 1);
2382 }
2383
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002384 captureResult.mMetadata.sort();
2385
2386 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002387 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2388 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002389 SET_ERR("No timestamp provided by HAL for frame %d!",
2390 frameNumber);
2391 return;
2392 }
2393
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002394 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2395 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2396
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002397 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002398}
2399
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002400/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002401 * Camera HAL device callback methods
2402 */
2403
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002404void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002405 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002406
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002407 status_t res;
2408
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002409 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002410 if (result->result == NULL && result->num_output_buffers == 0 &&
2411 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002412 SET_ERR("No result data provided by HAL for frame %d",
2413 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002414 return;
2415 }
Zhijun He204e3292014-07-14 17:09:23 -07002416
2417 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2418 // partial_result to 1 when metadata is included in this result.
2419 if (!mUsePartialResult &&
2420 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2421 result->result != NULL &&
2422 result->partial_result != 1) {
2423 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2424 " if partial result is not supported",
2425 frameNumber, result->partial_result);
2426 return;
2427 }
2428
2429 bool isPartialResult = false;
2430 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002431 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002432 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002433
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002434 // Get shutter timestamp and resultExtras from list of in-flight requests,
2435 // where it was added by the shutter notification for this frame. If the
2436 // shutter timestamp isn't received yet, append the output buffers to the
2437 // in-flight request and they will be returned when the shutter timestamp
2438 // arrives. Update the in-flight status and remove the in-flight entry if
2439 // all result data and shutter timestamp have been received.
2440 nsecs_t shutterTimestamp = 0;
2441
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002442 {
2443 Mutex::Autolock l(mInFlightLock);
2444 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2445 if (idx == NAME_NOT_FOUND) {
2446 SET_ERR("Unknown frame number for capture result: %d",
2447 frameNumber);
2448 return;
2449 }
2450 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002451 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2452 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2453 ", partialResultCount = %d",
2454 __FUNCTION__, request.resultExtras.requestId,
2455 request.resultExtras.frameNumber, request.resultExtras.burstId,
2456 result->partial_result);
2457 // Always update the partial count to the latest one if it's not 0
2458 // (buffers only). When framework aggregates adjacent partial results
2459 // into one, the latest partial count will be used.
2460 if (result->partial_result != 0)
2461 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002462
2463 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002464 if (mUsePartialResult && result->result != NULL) {
2465 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2466 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2467 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2468 " the range of [1, %d] when metadata is included in the result",
2469 frameNumber, result->partial_result, mNumPartialResults);
2470 return;
2471 }
2472 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002473 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002474 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002475 }
Zhijun He204e3292014-07-14 17:09:23 -07002476 } else {
2477 camera_metadata_ro_entry_t partialResultEntry;
2478 res = find_camera_metadata_ro_entry(result->result,
2479 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2480 if (res != NAME_NOT_FOUND &&
2481 partialResultEntry.count > 0 &&
2482 partialResultEntry.data.u8[0] ==
2483 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2484 // A partial result. Flag this as such, and collect this
2485 // set of metadata into the in-flight entry.
2486 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002487 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002488 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002489 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002490 ANDROID_QUIRKS_PARTIAL_RESULT);
2491 }
2492 }
2493
2494 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002495 // Send partial capture result
2496 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2497 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002498 }
2499 }
2500
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002501 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002502 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002503
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002504 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002505 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002506 if (request.haveResultMetadata) {
2507 SET_ERR("Called multiple times with metadata for frame %d",
2508 frameNumber);
2509 return;
2510 }
Zhijun He204e3292014-07-14 17:09:23 -07002511 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002512 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002513 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002514 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002515 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002516 request.haveResultMetadata = true;
2517 }
2518
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002519 uint32_t numBuffersReturned = result->num_output_buffers;
2520 if (result->input_buffer != NULL) {
2521 if (hasInputBufferInRequest) {
2522 numBuffersReturned += 1;
2523 } else {
2524 ALOGW("%s: Input buffer should be NULL if there is no input"
2525 " buffer sent in the request",
2526 __FUNCTION__);
2527 }
2528 }
2529 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002530 if (request.numBuffersLeft < 0) {
2531 SET_ERR("Too many buffers returned for frame %d",
2532 frameNumber);
2533 return;
2534 }
2535
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002536 camera_metadata_ro_entry_t entry;
2537 res = find_camera_metadata_ro_entry(result->result,
2538 ANDROID_SENSOR_TIMESTAMP, &entry);
2539 if (res == OK && entry.count == 1) {
2540 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002541 }
2542
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002543 // If shutter event isn't received yet, append the output buffers to
2544 // the in-flight request. Otherwise, return the output buffers to
2545 // streams.
2546 if (shutterTimestamp == 0) {
2547 request.pendingOutputBuffers.appendArray(result->output_buffers,
2548 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002549 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002550 returnOutputBuffers(result->output_buffers,
2551 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002552 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002553
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002554 if (result->result != NULL && !isPartialResult) {
2555 if (shutterTimestamp == 0) {
2556 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002557 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002558 } else {
2559 CameraMetadata metadata;
2560 metadata = result->result;
2561 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002562 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2563 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002564 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002565 }
2566
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002567 removeInFlightRequestIfReadyLocked(idx);
2568 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002569
Zhijun Hef0d962a2014-06-30 10:24:11 -07002570 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002571 if (hasInputBufferInRequest) {
2572 Camera3Stream *stream =
2573 Camera3Stream::cast(result->input_buffer->stream);
2574 res = stream->returnInputBuffer(*(result->input_buffer));
2575 // Note: stream may be deallocated at this point, if this buffer was the
2576 // last reference to it.
2577 if (res != OK) {
2578 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2579 " its stream:%s (%d)", __FUNCTION__,
2580 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002581 }
2582 } else {
2583 ALOGW("%s: Input buffer should be NULL if there is no input"
2584 " buffer sent in the request, skipping input buffer return.",
2585 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002586 }
2587 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002588}
2589
2590void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002591 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002592 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002593 {
2594 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002595 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002596 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002597
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002598 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002599 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002600 return;
2601 }
2602
2603 switch (msg->type) {
2604 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002605 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002606 break;
2607 }
2608 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002609 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002610 break;
2611 }
2612 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002613 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002614 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002615 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002616}
2617
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002618void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002619 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002620
2621 // Map camera HAL error codes to ICameraDeviceCallback error codes
2622 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002623 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002624 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002625 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002626 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002627 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002628 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002629 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002630 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002631 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002632 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002633 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002634 };
2635
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002636 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002637 ((msg.error_code >= 0) &&
2638 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2639 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002640 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002641
2642 int streamId = 0;
2643 if (msg.error_stream != NULL) {
2644 Camera3Stream *stream =
2645 Camera3Stream::cast(msg.error_stream);
2646 streamId = stream->getId();
2647 }
2648 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2649 mId, __FUNCTION__, msg.frame_number,
2650 streamId, msg.error_code);
2651
2652 CaptureResultExtras resultExtras;
2653 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002654 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002655 // SET_ERR calls notifyError
2656 SET_ERR("Camera HAL reported serious device error");
2657 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002658 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2659 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2660 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002661 {
2662 Mutex::Autolock l(mInFlightLock);
2663 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2664 if (idx >= 0) {
2665 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2666 r.requestStatus = msg.error_code;
2667 resultExtras = r.resultExtras;
2668 } else {
2669 resultExtras.frameNumber = msg.frame_number;
2670 ALOGE("Camera %d: %s: cannot find in-flight request on "
2671 "frame %" PRId64 " error", mId, __FUNCTION__,
2672 resultExtras.frameNumber);
2673 }
2674 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002675 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002676 if (listener != NULL) {
2677 listener->notifyError(errorCode, resultExtras);
2678 } else {
2679 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2680 }
2681 break;
2682 default:
2683 // SET_ERR calls notifyError
2684 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2685 break;
2686 }
2687}
2688
2689void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002690 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002691 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002692
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002693 // Set timestamp for the request in the in-flight tracking
2694 // and get the request ID to send upstream
2695 {
2696 Mutex::Autolock l(mInFlightLock);
2697 idx = mInFlightMap.indexOfKey(msg.frame_number);
2698 if (idx >= 0) {
2699 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002700
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002701 // Verify ordering of shutter notifications
2702 {
2703 Mutex::Autolock l(mOutputLock);
2704 // TODO: need to track errors for tighter bounds on expected frame number.
2705 if (r.hasInputBuffer) {
2706 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2707 SET_ERR("Shutter notification out-of-order. Expected "
2708 "notification for frame %d, got frame %d",
2709 mNextReprocessShutterFrameNumber, msg.frame_number);
2710 return;
2711 }
2712 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2713 } else {
2714 if (msg.frame_number < mNextShutterFrameNumber) {
2715 SET_ERR("Shutter notification out-of-order. Expected "
2716 "notification for frame %d, got frame %d",
2717 mNextShutterFrameNumber, msg.frame_number);
2718 return;
2719 }
2720 mNextShutterFrameNumber = msg.frame_number + 1;
2721 }
2722 }
2723
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002724 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2725 mId, __FUNCTION__,
2726 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2727 // Call listener, if any
2728 if (listener != NULL) {
2729 listener->notifyShutter(r.resultExtras, msg.timestamp);
2730 }
2731
2732 r.shutterTimestamp = msg.timestamp;
2733
2734 // send pending result and buffers
2735 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002736 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002737 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002738 returnOutputBuffers(r.pendingOutputBuffers.array(),
2739 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2740 r.pendingOutputBuffers.clear();
2741
2742 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002743 }
2744 }
2745 if (idx < 0) {
2746 SET_ERR("Shutter notification for non-existent frame number %d",
2747 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002748 }
2749}
2750
2751
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002752CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002753 ALOGV("%s", __FUNCTION__);
2754
Igor Murashkin1e479c02013-09-06 16:55:14 -07002755 CameraMetadata retVal;
2756
2757 if (mRequestThread != NULL) {
2758 retVal = mRequestThread->getLatestRequest();
2759 }
2760
Igor Murashkin1e479c02013-09-06 16:55:14 -07002761 return retVal;
2762}
2763
Jianing Weicb0652e2014-03-12 18:29:36 -07002764
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002765void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
2766 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
2767 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
2768}
2769
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002770/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002771 * RequestThread inner class methods
2772 */
2773
2774Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002775 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002776 camera3_device_t *hal3Device,
2777 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002778 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002779 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002780 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002781 mHal3Device(hal3Device),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002782 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002783 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002784 mReconfigured(false),
2785 mDoPause(false),
2786 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002787 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002788 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002789 mCurrentAfTriggerId(0),
2790 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002791 mRepeatingLastFrameNumber(
2792 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002793 mAeLockAvailable(aeLockAvailable),
2794 mPrepareVideoStream(false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002795 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002796}
2797
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002798void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002799 wp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002800 Mutex::Autolock l(mRequestLock);
2801 mListener = listener;
2802}
2803
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002804void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002805 Mutex::Autolock l(mRequestLock);
2806 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002807 // Prepare video stream for high speed recording.
2808 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002809}
2810
Jianing Wei90e59c92014-03-12 18:29:36 -07002811status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002812 List<sp<CaptureRequest> > &requests,
2813 /*out*/
2814 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002815 Mutex::Autolock l(mRequestLock);
2816 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2817 ++it) {
2818 mRequestQueue.push_back(*it);
2819 }
2820
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002821 if (lastFrameNumber != NULL) {
2822 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2823 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2824 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2825 *lastFrameNumber);
2826 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002827
Jianing Wei90e59c92014-03-12 18:29:36 -07002828 unpauseForNewRequests();
2829
2830 return OK;
2831}
2832
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002833
2834status_t Camera3Device::RequestThread::queueTrigger(
2835 RequestTrigger trigger[],
2836 size_t count) {
2837
2838 Mutex::Autolock l(mTriggerMutex);
2839 status_t ret;
2840
2841 for (size_t i = 0; i < count; ++i) {
2842 ret = queueTriggerLocked(trigger[i]);
2843
2844 if (ret != OK) {
2845 return ret;
2846 }
2847 }
2848
2849 return OK;
2850}
2851
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002852int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2853 sp<Camera3Device> d = device.promote();
2854 if (d != NULL) return d->mId;
2855 return 0;
2856}
2857
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002858status_t Camera3Device::RequestThread::queueTriggerLocked(
2859 RequestTrigger trigger) {
2860
2861 uint32_t tag = trigger.metadataTag;
2862 ssize_t index = mTriggerMap.indexOfKey(tag);
2863
2864 switch (trigger.getTagType()) {
2865 case TYPE_BYTE:
2866 // fall-through
2867 case TYPE_INT32:
2868 break;
2869 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002870 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2871 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002872 return INVALID_OPERATION;
2873 }
2874
2875 /**
2876 * Collect only the latest trigger, since we only have 1 field
2877 * in the request settings per trigger tag, and can't send more than 1
2878 * trigger per request.
2879 */
2880 if (index != NAME_NOT_FOUND) {
2881 mTriggerMap.editValueAt(index) = trigger;
2882 } else {
2883 mTriggerMap.add(tag, trigger);
2884 }
2885
2886 return OK;
2887}
2888
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002889status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002890 const RequestList &requests,
2891 /*out*/
2892 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002893 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002894 if (lastFrameNumber != NULL) {
2895 *lastFrameNumber = mRepeatingLastFrameNumber;
2896 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002897 mRepeatingRequests.clear();
2898 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2899 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002900
2901 unpauseForNewRequests();
2902
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002903 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002904 return OK;
2905}
2906
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07002907bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002908 if (mRepeatingRequests.empty()) {
2909 return false;
2910 }
2911 int32_t requestId = requestIn->mResultExtras.requestId;
2912 const RequestList &repeatRequests = mRepeatingRequests;
2913 // All repeating requests are guaranteed to have same id so only check first quest
2914 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2915 return (firstRequest->mResultExtras.requestId == requestId);
2916}
2917
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002918status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002919 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002920 return clearRepeatingRequestsLocked(lastFrameNumber);
2921
2922}
2923
2924status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002925 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002926 if (lastFrameNumber != NULL) {
2927 *lastFrameNumber = mRepeatingLastFrameNumber;
2928 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002929 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002930 return OK;
2931}
2932
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002933status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002934 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002935 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002936 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002937
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002938 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002939
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002940 // Send errors for all requests pending in the request queue, including
2941 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002942 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002943 if (listener != NULL) {
2944 for (RequestList::iterator it = mRequestQueue.begin();
2945 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002946 // Abort the input buffers for reprocess requests.
2947 if ((*it)->mInputStream != NULL) {
2948 camera3_stream_buffer_t inputBuffer;
2949 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2950 if (res != OK) {
2951 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2952 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2953 } else {
2954 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2955 if (res != OK) {
2956 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2957 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2958 }
2959 }
2960 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002961 // Set the frame number this request would have had, if it
2962 // had been submitted; this frame number will not be reused.
2963 // The requestId and burstId fields were set when the request was
2964 // submitted originally (in convertMetadataListToRequestListLocked)
2965 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002966 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002967 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002968 }
2969 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002970 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08002971
2972 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002973 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002974 if (lastFrameNumber != NULL) {
2975 *lastFrameNumber = mRepeatingLastFrameNumber;
2976 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002977 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002978 return OK;
2979}
2980
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002981status_t Camera3Device::RequestThread::flush() {
2982 ATRACE_CALL();
2983 Mutex::Autolock l(mFlushLock);
2984
2985 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2986 return mHal3Device->ops->flush(mHal3Device);
2987 }
2988
2989 return -ENOTSUP;
2990}
2991
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002992void Camera3Device::RequestThread::setPaused(bool paused) {
2993 Mutex::Autolock l(mPauseLock);
2994 mDoPause = paused;
2995 mDoPauseSignal.signal();
2996}
2997
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002998status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2999 int32_t requestId, nsecs_t timeout) {
3000 Mutex::Autolock l(mLatestRequestMutex);
3001 status_t res;
3002 while (mLatestRequestId != requestId) {
3003 nsecs_t startTime = systemTime();
3004
3005 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3006 if (res != OK) return res;
3007
3008 timeout -= (systemTime() - startTime);
3009 }
3010
3011 return OK;
3012}
3013
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003014void Camera3Device::RequestThread::requestExit() {
3015 // Call parent to set up shutdown
3016 Thread::requestExit();
3017 // The exit from any possible waits
3018 mDoPauseSignal.signal();
3019 mRequestSignal.signal();
3020}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003021
Chien-Yu Chend196d612015-06-22 19:49:01 -07003022
3023/**
3024 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
3025 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
3026 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
3027 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
3028 * request.
3029 */
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003030void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(const sp<CaptureRequest>& request) {
Chien-Yu Chend196d612015-06-22 19:49:01 -07003031 request->mAeTriggerCancelOverride.applyAeLock = false;
3032 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
3033
3034 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
3035 return;
3036 }
3037
3038 camera_metadata_entry_t aePrecaptureTrigger =
3039 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3040 if (aePrecaptureTrigger.count > 0 &&
3041 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
3042 // Always override CANCEL to IDLE
3043 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
3044 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
3045 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
3046 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
3047 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3048
3049 if (mAeLockAvailable == true) {
3050 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3051 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3052 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3053 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3054 request->mAeTriggerCancelOverride.applyAeLock = true;
3055 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3056 }
3057 }
3058 }
3059}
3060
3061/**
3062 * Override result metadata for cancelling AE precapture trigger applied in
3063 * handleAePrecaptureCancelRequest().
3064 */
3065void Camera3Device::overrideResultForPrecaptureCancel(
3066 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3067 if (aeTriggerCancelOverride.applyAeLock) {
3068 // Only devices <= v3.2 should have this override
3069 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3070 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3071 }
3072
3073 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3074 // Only devices <= v3.2 should have this override
3075 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3076 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3077 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3078 }
3079}
3080
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003081void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003082 bool surfaceAbandoned = false;
3083 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003084 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003085 {
3086 Mutex::Autolock l(mRequestLock);
3087 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3088 // repeating requests.
3089 for (const auto& request : mRepeatingRequests) {
3090 for (const auto& s : request->mOutputStreams) {
3091 if (s->isAbandoned()) {
3092 surfaceAbandoned = true;
3093 clearRepeatingRequestsLocked(&lastFrameNumber);
3094 break;
3095 }
3096 }
3097 if (surfaceAbandoned) {
3098 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003099 }
3100 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003101 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003102 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003103
3104 if (listener != NULL && surfaceAbandoned) {
3105 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003106 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003107}
3108
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003109bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003110 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003111 status_t res;
3112
3113 // Handle paused state.
3114 if (waitIfPaused()) {
3115 return true;
3116 }
3117
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003118 // Wait for the next batch of requests.
3119 waitForNextRequestBatch();
3120 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003121 return true;
3122 }
3123
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003124 // Get the latest request ID, if any
3125 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003126 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003127 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003128 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003129 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003130 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003131 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3132 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003133 }
3134
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003135 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003136 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003137 if (res == TIMED_OUT) {
3138 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003139 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003140 // Check if any stream is abandoned.
3141 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003142 return true;
3143 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003144 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003145 return false;
3146 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003147
Zhijun Hecc27e112013-10-03 16:12:43 -07003148 // Inform waitUntilRequestProcessed thread of a new request ID
3149 {
3150 Mutex::Autolock al(mLatestRequestMutex);
3151
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003152 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003153 mLatestRequestSignal.signal();
3154 }
3155
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003156 // Submit a batch of requests to HAL.
3157 // Use flush lock only when submitting multilple requests in a batch.
3158 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3159 // which may take a long time to finish so synchronizing flush() and
3160 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3161 // For now, only synchronize for high speed recording and we should figure something out for
3162 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003163 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003164
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003165 if (useFlushLock) {
3166 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003167 }
3168
Zhijun Hef0645c12016-08-02 00:58:11 -07003169 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003170 mNextRequests.size());
3171 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003172 // Submit request and block until ready for next one
3173 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3174 ATRACE_BEGIN("camera3->process_capture_request");
3175 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3176 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003177
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003178 if (res != OK) {
3179 // Should only get a failure here for malformed requests or device-level
3180 // errors, so consider all errors fatal. Bad metadata failures should
3181 // come through notify.
3182 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3183 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3184 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003185 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003186 if (useFlushLock) {
3187 mFlushLock.unlock();
3188 }
3189 return false;
3190 }
3191
3192 // Mark that the request has be submitted successfully.
3193 nextRequest.submitted = true;
3194
3195 // Update the latest request sent to HAL
3196 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3197 Mutex::Autolock al(mLatestRequestMutex);
3198
3199 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3200 mLatestRequest.acquire(cloned);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003201
3202 sp<Camera3Device> parent = mParent.promote();
3203 if (parent != NULL) {
3204 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
3205 0, mLatestRequest);
3206 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003207 }
3208
3209 if (nextRequest.halRequest.settings != NULL) {
3210 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3211 }
3212
3213 // Remove any previously queued triggers (after unlock)
3214 res = removeTriggers(mPrevRequest);
3215 if (res != OK) {
3216 SET_ERR("RequestThread: Unable to remove triggers "
3217 "(capture request %d, HAL device: %s (%d)",
3218 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003219 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003220 if (useFlushLock) {
3221 mFlushLock.unlock();
3222 }
3223 return false;
3224 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003225 }
3226
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003227 if (useFlushLock) {
3228 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003229 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003230
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003231 // Unset as current request
3232 {
3233 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003234 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003235 }
3236
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003237 return true;
3238}
3239
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003240status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003241 ATRACE_CALL();
3242
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003243 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003244 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3245 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3246 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3247
3248 // Prepare a request to HAL
3249 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3250
3251 // Insert any queued triggers (before metadata is locked)
3252 status_t res = insertTriggers(captureRequest);
3253
3254 if (res < 0) {
3255 SET_ERR("RequestThread: Unable to insert triggers "
3256 "(capture request %d, HAL device: %s (%d)",
3257 halRequest->frame_number, strerror(-res), res);
3258 return INVALID_OPERATION;
3259 }
3260 int triggerCount = res;
3261 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3262 mPrevTriggers = triggerCount;
3263
3264 // If the request is the same as last, or we had triggers last time
3265 if (mPrevRequest != captureRequest || triggersMixedIn) {
3266 /**
3267 * HAL workaround:
3268 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3269 */
3270 res = addDummyTriggerIds(captureRequest);
3271 if (res != OK) {
3272 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3273 "(capture request %d, HAL device: %s (%d)",
3274 halRequest->frame_number, strerror(-res), res);
3275 return INVALID_OPERATION;
3276 }
3277
3278 /**
3279 * The request should be presorted so accesses in HAL
3280 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3281 */
3282 captureRequest->mSettings.sort();
3283 halRequest->settings = captureRequest->mSettings.getAndLock();
3284 mPrevRequest = captureRequest;
3285 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3286
3287 IF_ALOGV() {
3288 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3289 find_camera_metadata_ro_entry(
3290 halRequest->settings,
3291 ANDROID_CONTROL_AF_TRIGGER,
3292 &e
3293 );
3294 if (e.count > 0) {
3295 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3296 __FUNCTION__,
3297 halRequest->frame_number,
3298 e.data.u8[0]);
3299 }
3300 }
3301 } else {
3302 // leave request.settings NULL to indicate 'reuse latest given'
3303 ALOGVV("%s: Request settings are REUSED",
3304 __FUNCTION__);
3305 }
3306
3307 uint32_t totalNumBuffers = 0;
3308
3309 // Fill in buffers
3310 if (captureRequest->mInputStream != NULL) {
3311 halRequest->input_buffer = &captureRequest->mInputBuffer;
3312 totalNumBuffers += 1;
3313 } else {
3314 halRequest->input_buffer = NULL;
3315 }
3316
3317 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3318 captureRequest->mOutputStreams.size());
3319 halRequest->output_buffers = outputBuffers->array();
3320 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003321 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(i);
3322
3323 // Prepare video buffers for high speed recording on the first video request.
3324 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3325 // Only try to prepare video stream on the first video request.
3326 mPrepareVideoStream = false;
3327
3328 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
3329 while (res == NOT_ENOUGH_DATA) {
3330 res = outputStream->prepareNextBuffer();
3331 }
3332 if (res != OK) {
3333 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3334 __FUNCTION__, strerror(-res), res);
3335 outputStream->cancelPrepare();
3336 }
3337 }
3338
3339 res = outputStream->getBuffer(&outputBuffers->editItemAt(i));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003340 if (res != OK) {
3341 // Can't get output buffer from gralloc queue - this could be due to
3342 // abandoned queue or other consumer misbehavior, so not a fatal
3343 // error
3344 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3345 " %s (%d)", strerror(-res), res);
3346
3347 return TIMED_OUT;
3348 }
3349 halRequest->num_output_buffers++;
3350 }
3351 totalNumBuffers += halRequest->num_output_buffers;
3352
3353 // Log request in the in-flight queue
3354 sp<Camera3Device> parent = mParent.promote();
3355 if (parent == NULL) {
3356 // Should not happen, and nowhere to send errors to, so just log it
3357 CLOGE("RequestThread: Parent is gone");
3358 return INVALID_OPERATION;
3359 }
3360 res = parent->registerInFlight(halRequest->frame_number,
3361 totalNumBuffers, captureRequest->mResultExtras,
3362 /*hasInput*/halRequest->input_buffer != NULL,
3363 captureRequest->mAeTriggerCancelOverride);
3364 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3365 ", burstId = %" PRId32 ".",
3366 __FUNCTION__,
3367 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3368 captureRequest->mResultExtras.burstId);
3369 if (res != OK) {
3370 SET_ERR("RequestThread: Unable to register new in-flight request:"
3371 " %s (%d)", strerror(-res), res);
3372 return INVALID_OPERATION;
3373 }
3374 }
3375
3376 return OK;
3377}
3378
Igor Murashkin1e479c02013-09-06 16:55:14 -07003379CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3380 Mutex::Autolock al(mLatestRequestMutex);
3381
3382 ALOGV("RequestThread::%s", __FUNCTION__);
3383
3384 return mLatestRequest;
3385}
3386
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003387bool Camera3Device::RequestThread::isStreamPending(
3388 sp<Camera3StreamInterface>& stream) {
3389 Mutex::Autolock l(mRequestLock);
3390
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003391 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003392 if (!nextRequest.submitted) {
3393 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3394 if (stream == s) return true;
3395 }
3396 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003397 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003398 }
3399
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003400 for (const auto& request : mRequestQueue) {
3401 for (const auto& s : request->mOutputStreams) {
3402 if (stream == s) return true;
3403 }
3404 if (stream == request->mInputStream) return true;
3405 }
3406
3407 for (const auto& request : mRepeatingRequests) {
3408 for (const auto& s : request->mOutputStreams) {
3409 if (stream == s) return true;
3410 }
3411 if (stream == request->mInputStream) return true;
3412 }
3413
3414 return false;
3415}
Jianing Weicb0652e2014-03-12 18:29:36 -07003416
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003417void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3418 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003419 return;
3420 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003421
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003422 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003423 // Skip the ones that have been submitted successfully.
3424 if (nextRequest.submitted) {
3425 continue;
3426 }
3427
3428 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3429 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3430 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3431
3432 if (halRequest->settings != NULL) {
3433 captureRequest->mSettings.unlock(halRequest->settings);
3434 }
3435
3436 if (captureRequest->mInputStream != NULL) {
3437 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3438 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3439 }
3440
3441 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3442 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3443 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3444 }
3445
3446 if (sendRequestError) {
3447 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003448 sp<NotificationListener> listener = mListener.promote();
3449 if (listener != NULL) {
3450 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003451 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003452 captureRequest->mResultExtras);
3453 }
3454 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003455
3456 // Remove yet-to-be submitted inflight request from inflightMap
3457 {
3458 sp<Camera3Device> parent = mParent.promote();
3459 if (parent != NULL) {
3460 Mutex::Autolock l(parent->mInFlightLock);
3461 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
3462 if (idx >= 0) {
3463 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
3464 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
3465 parent->removeInFlightMapEntryLocked(idx);
3466 }
3467 }
3468 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003469 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003470
3471 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003472 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003473}
3474
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003475void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003476 // Optimized a bit for the simple steady-state case (single repeating
3477 // request), to avoid putting that request in the queue temporarily.
3478 Mutex::Autolock l(mRequestLock);
3479
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003480 assert(mNextRequests.empty());
3481
3482 NextRequest nextRequest;
3483 nextRequest.captureRequest = waitForNextRequestLocked();
3484 if (nextRequest.captureRequest == nullptr) {
3485 return;
3486 }
3487
3488 nextRequest.halRequest = camera3_capture_request_t();
3489 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003490 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003491
3492 // Wait for additional requests
3493 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3494
3495 for (size_t i = 1; i < batchSize; i++) {
3496 NextRequest additionalRequest;
3497 additionalRequest.captureRequest = waitForNextRequestLocked();
3498 if (additionalRequest.captureRequest == nullptr) {
3499 break;
3500 }
3501
3502 additionalRequest.halRequest = camera3_capture_request_t();
3503 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003504 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003505 }
3506
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003507 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003508 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003509 mNextRequests.size(), batchSize);
3510 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003511 }
3512
3513 return;
3514}
3515
3516sp<Camera3Device::CaptureRequest>
3517 Camera3Device::RequestThread::waitForNextRequestLocked() {
3518 status_t res;
3519 sp<CaptureRequest> nextRequest;
3520
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003521 while (mRequestQueue.empty()) {
3522 if (!mRepeatingRequests.empty()) {
3523 // Always atomically enqueue all requests in a repeating request
3524 // list. Guarantees a complete in-sequence set of captures to
3525 // application.
3526 const RequestList &requests = mRepeatingRequests;
3527 RequestList::const_iterator firstRequest =
3528 requests.begin();
3529 nextRequest = *firstRequest;
3530 mRequestQueue.insert(mRequestQueue.end(),
3531 ++firstRequest,
3532 requests.end());
3533 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003534
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003535 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003536
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003537 break;
3538 }
3539
3540 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3541
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003542 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3543 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003544 Mutex::Autolock pl(mPauseLock);
3545 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003546 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003547 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003548 // Let the tracker know
3549 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3550 if (statusTracker != 0) {
3551 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3552 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003553 }
3554 // Stop waiting for now and let thread management happen
3555 return NULL;
3556 }
3557 }
3558
3559 if (nextRequest == NULL) {
3560 // Don't have a repeating request already in hand, so queue
3561 // must have an entry now.
3562 RequestList::iterator firstRequest =
3563 mRequestQueue.begin();
3564 nextRequest = *firstRequest;
3565 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07003566 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
3567 sp<NotificationListener> listener = mListener.promote();
3568 if (listener != NULL) {
3569 listener->notifyRequestQueueEmpty();
3570 }
3571 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003572 }
3573
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003574 // In case we've been unpaused by setPaused clearing mDoPause, need to
3575 // update internal pause state (capture/setRepeatingRequest unpause
3576 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003577 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003578 if (mPaused) {
3579 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3580 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3581 if (statusTracker != 0) {
3582 statusTracker->markComponentActive(mStatusId);
3583 }
3584 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003585 mPaused = false;
3586
3587 // Check if we've reconfigured since last time, and reset the preview
3588 // request if so. Can't use 'NULL request == repeat' across configure calls.
3589 if (mReconfigured) {
3590 mPrevRequest.clear();
3591 mReconfigured = false;
3592 }
3593
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003594 if (nextRequest != NULL) {
3595 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003596 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3597 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003598
3599 // Since RequestThread::clear() removes buffers from the input stream,
3600 // get the right buffer here before unlocking mRequestLock
3601 if (nextRequest->mInputStream != NULL) {
3602 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3603 if (res != OK) {
3604 // Can't get input buffer from gralloc queue - this could be due to
3605 // disconnected queue or other producer misbehavior, so not a fatal
3606 // error
3607 ALOGE("%s: Can't get input buffer, skipping request:"
3608 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003609
3610 sp<NotificationListener> listener = mListener.promote();
3611 if (listener != NULL) {
3612 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003613 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003614 nextRequest->mResultExtras);
3615 }
3616 return NULL;
3617 }
3618 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003619 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003620
3621 handleAePrecaptureCancelRequest(nextRequest);
3622
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003623 return nextRequest;
3624}
3625
3626bool Camera3Device::RequestThread::waitIfPaused() {
3627 status_t res;
3628 Mutex::Autolock l(mPauseLock);
3629 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003630 if (mPaused == false) {
3631 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003632 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3633 // Let the tracker know
3634 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3635 if (statusTracker != 0) {
3636 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3637 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003638 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003639
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003640 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003641 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003642 return true;
3643 }
3644 }
3645 // We don't set mPaused to false here, because waitForNextRequest needs
3646 // to further manage the paused state in case of starvation.
3647 return false;
3648}
3649
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003650void Camera3Device::RequestThread::unpauseForNewRequests() {
3651 // With work to do, mark thread as unpaused.
3652 // If paused by request (setPaused), don't resume, to avoid
3653 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003654 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003655 Mutex::Autolock p(mPauseLock);
3656 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003657 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3658 if (mPaused) {
3659 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3660 if (statusTracker != 0) {
3661 statusTracker->markComponentActive(mStatusId);
3662 }
3663 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003664 mPaused = false;
3665 }
3666}
3667
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003668void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3669 sp<Camera3Device> parent = mParent.promote();
3670 if (parent != NULL) {
3671 va_list args;
3672 va_start(args, fmt);
3673
3674 parent->setErrorStateV(fmt, args);
3675
3676 va_end(args);
3677 }
3678}
3679
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003680status_t Camera3Device::RequestThread::insertTriggers(
3681 const sp<CaptureRequest> &request) {
3682
3683 Mutex::Autolock al(mTriggerMutex);
3684
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003685 sp<Camera3Device> parent = mParent.promote();
3686 if (parent == NULL) {
3687 CLOGE("RequestThread: Parent is gone");
3688 return DEAD_OBJECT;
3689 }
3690
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003691 CameraMetadata &metadata = request->mSettings;
3692 size_t count = mTriggerMap.size();
3693
3694 for (size_t i = 0; i < count; ++i) {
3695 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003696 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003697
3698 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3699 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3700 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003701 if (isAeTrigger) {
3702 request->mResultExtras.precaptureTriggerId = triggerId;
3703 mCurrentPreCaptureTriggerId = triggerId;
3704 } else {
3705 request->mResultExtras.afTriggerId = triggerId;
3706 mCurrentAfTriggerId = triggerId;
3707 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003708 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3709 continue; // Trigger ID tag is deprecated since device HAL 3.2
3710 }
3711 }
3712
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003713 camera_metadata_entry entry = metadata.find(tag);
3714
3715 if (entry.count > 0) {
3716 /**
3717 * Already has an entry for this trigger in the request.
3718 * Rewrite it with our requested trigger value.
3719 */
3720 RequestTrigger oldTrigger = trigger;
3721
3722 oldTrigger.entryValue = entry.data.u8[0];
3723
3724 mTriggerReplacedMap.add(tag, oldTrigger);
3725 } else {
3726 /**
3727 * More typical, no trigger entry, so we just add it
3728 */
3729 mTriggerRemovedMap.add(tag, trigger);
3730 }
3731
3732 status_t res;
3733
3734 switch (trigger.getTagType()) {
3735 case TYPE_BYTE: {
3736 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3737 res = metadata.update(tag,
3738 &entryValue,
3739 /*count*/1);
3740 break;
3741 }
3742 case TYPE_INT32:
3743 res = metadata.update(tag,
3744 &trigger.entryValue,
3745 /*count*/1);
3746 break;
3747 default:
3748 ALOGE("%s: Type not supported: 0x%x",
3749 __FUNCTION__,
3750 trigger.getTagType());
3751 return INVALID_OPERATION;
3752 }
3753
3754 if (res != OK) {
3755 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3756 ", value %d", __FUNCTION__, trigger.getTagName(),
3757 trigger.entryValue);
3758 return res;
3759 }
3760
3761 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3762 trigger.getTagName(),
3763 trigger.entryValue);
3764 }
3765
3766 mTriggerMap.clear();
3767
3768 return count;
3769}
3770
3771status_t Camera3Device::RequestThread::removeTriggers(
3772 const sp<CaptureRequest> &request) {
3773 Mutex::Autolock al(mTriggerMutex);
3774
3775 CameraMetadata &metadata = request->mSettings;
3776
3777 /**
3778 * Replace all old entries with their old values.
3779 */
3780 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3781 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3782
3783 status_t res;
3784
3785 uint32_t tag = trigger.metadataTag;
3786 switch (trigger.getTagType()) {
3787 case TYPE_BYTE: {
3788 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3789 res = metadata.update(tag,
3790 &entryValue,
3791 /*count*/1);
3792 break;
3793 }
3794 case TYPE_INT32:
3795 res = metadata.update(tag,
3796 &trigger.entryValue,
3797 /*count*/1);
3798 break;
3799 default:
3800 ALOGE("%s: Type not supported: 0x%x",
3801 __FUNCTION__,
3802 trigger.getTagType());
3803 return INVALID_OPERATION;
3804 }
3805
3806 if (res != OK) {
3807 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3808 ", trigger value %d", __FUNCTION__,
3809 trigger.getTagName(), trigger.entryValue);
3810 return res;
3811 }
3812 }
3813 mTriggerReplacedMap.clear();
3814
3815 /**
3816 * Remove all new entries.
3817 */
3818 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3819 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3820 status_t res = metadata.erase(trigger.metadataTag);
3821
3822 if (res != OK) {
3823 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3824 ", trigger value %d", __FUNCTION__,
3825 trigger.getTagName(), trigger.entryValue);
3826 return res;
3827 }
3828 }
3829 mTriggerRemovedMap.clear();
3830
3831 return OK;
3832}
3833
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003834status_t Camera3Device::RequestThread::addDummyTriggerIds(
3835 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003836 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003837 static const int32_t dummyTriggerId = 1;
3838 status_t res;
3839
3840 CameraMetadata &metadata = request->mSettings;
3841
3842 // If AF trigger is active, insert a dummy AF trigger ID if none already
3843 // exists
3844 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3845 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3846 if (afTrigger.count > 0 &&
3847 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3848 afId.count == 0) {
3849 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3850 if (res != OK) return res;
3851 }
3852
3853 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3854 // if none already exists
3855 camera_metadata_entry pcTrigger =
3856 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3857 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3858 if (pcTrigger.count > 0 &&
3859 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3860 pcId.count == 0) {
3861 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3862 &dummyTriggerId, 1);
3863 if (res != OK) return res;
3864 }
3865
3866 return OK;
3867}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003868
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003869/**
3870 * PreparerThread inner class methods
3871 */
3872
3873Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003874 Thread(/*canCallJava*/false), mListener(nullptr),
3875 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003876}
3877
3878Camera3Device::PreparerThread::~PreparerThread() {
3879 Thread::requestExitAndWait();
3880 if (mCurrentStream != nullptr) {
3881 mCurrentStream->cancelPrepare();
3882 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3883 mCurrentStream.clear();
3884 }
3885 clear();
3886}
3887
Ruben Brunkc78ac262015-08-13 17:58:46 -07003888status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003889 status_t res;
3890
3891 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003892 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003893
Ruben Brunkc78ac262015-08-13 17:58:46 -07003894 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003895 if (res == OK) {
3896 // No preparation needed, fire listener right off
3897 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003898 if (listener != NULL) {
3899 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003900 }
3901 return OK;
3902 } else if (res != NOT_ENOUGH_DATA) {
3903 return res;
3904 }
3905
3906 // Need to prepare, start up thread if necessary
3907 if (!mActive) {
3908 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3909 // isn't running
3910 Thread::requestExitAndWait();
3911 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3912 if (res != OK) {
3913 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003914 if (listener != NULL) {
3915 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003916 }
3917 return res;
3918 }
3919 mCancelNow = false;
3920 mActive = true;
3921 ALOGV("%s: Preparer stream started", __FUNCTION__);
3922 }
3923
3924 // queue up the work
3925 mPendingStreams.push_back(stream);
3926 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3927
3928 return OK;
3929}
3930
3931status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003932 Mutex::Autolock l(mLock);
3933
3934 for (const auto& stream : mPendingStreams) {
3935 stream->cancelPrepare();
3936 }
3937 mPendingStreams.clear();
3938 mCancelNow = true;
3939
3940 return OK;
3941}
3942
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003943void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003944 Mutex::Autolock l(mLock);
3945 mListener = listener;
3946}
3947
3948bool Camera3Device::PreparerThread::threadLoop() {
3949 status_t res;
3950 {
3951 Mutex::Autolock l(mLock);
3952 if (mCurrentStream == nullptr) {
3953 // End thread if done with work
3954 if (mPendingStreams.empty()) {
3955 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3956 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3957 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3958 mActive = false;
3959 return false;
3960 }
3961
3962 // Get next stream to prepare
3963 auto it = mPendingStreams.begin();
3964 mCurrentStream = *it;
3965 mPendingStreams.erase(it);
3966 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3967 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3968 } else if (mCancelNow) {
3969 mCurrentStream->cancelPrepare();
3970 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3971 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3972 mCurrentStream.clear();
3973 mCancelNow = false;
3974 return true;
3975 }
3976 }
3977
3978 res = mCurrentStream->prepareNextBuffer();
3979 if (res == NOT_ENOUGH_DATA) return true;
3980 if (res != OK) {
3981 // Something bad happened; try to recover by cancelling prepare and
3982 // signalling listener anyway
3983 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3984 mCurrentStream->getId(), res, strerror(-res));
3985 mCurrentStream->cancelPrepare();
3986 }
3987
3988 // This stream has finished, notify listener
3989 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003990 sp<NotificationListener> listener = mListener.promote();
3991 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003992 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3993 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003994 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003995 }
3996
3997 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3998 mCurrentStream.clear();
3999
4000 return true;
4001}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004002
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004003/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004004 * Static callback forwarding methods from HAL to instance
4005 */
4006
4007void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
4008 const camera3_capture_result *result) {
4009 Camera3Device *d =
4010 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07004011
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004012 d->processCaptureResult(result);
4013}
4014
4015void Camera3Device::sNotify(const camera3_callback_ops *cb,
4016 const camera3_notify_msg *msg) {
4017 Camera3Device *d =
4018 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
4019 d->notify(msg);
4020}
4021
4022}; // namespace android