blob: c930ac0a9b487ff01f9c4f4af2e6b481faeec9d8 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046#include <android/hardware/camera2/ICameraDeviceUser.h>
47
Igor Murashkinff3e31d2013-10-23 16:40:06 -070048#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070049#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070050#include "device3/Camera3Device.h"
51#include "device3/Camera3OutputStream.h"
52#include "device3/Camera3InputStream.h"
53#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070054#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070055#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080056
57using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058
59namespace android {
60
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061Camera3Device::Camera3Device(int id):
62 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070063 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080064 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070065 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070066 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070067 mUsePartialResult(false),
68 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080069 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070070 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070071 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070072 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070073 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070074 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080075{
76 ATRACE_CALL();
77 camera3_callback_ops::notify = &sNotify;
78 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
79 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
80}
81
82Camera3Device::~Camera3Device()
83{
84 ATRACE_CALL();
85 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
86 disconnect();
87}
88
Igor Murashkin71381052013-03-04 14:53:08 -080089int Camera3Device::getId() const {
90 return mId;
91}
92
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080093/**
94 * CameraDeviceBase interface
95 */
96
Yin-Chia Yehe074a932015-01-30 10:29:02 -080097status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080098{
99 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700100 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800101 Mutex::Autolock l(mLock);
102
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800104 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700105 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800106 return INVALID_OPERATION;
107 }
108
109 /** Open HAL device */
110
111 status_t res;
112 String8 deviceName = String8::format("%d", mId);
113
114 camera3_device_t *device;
115
Zhijun He213ce792013-11-19 08:45:15 -0800116 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800117 res = module->open(deviceName.string(),
118 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800119 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800120
121 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800123 return res;
124 }
125
126 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700127 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700128 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700129 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700130 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800131 device->common.version);
132 device->common.close(&device->common);
133 return BAD_VALUE;
134 }
135
136 camera_info info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800137 res = module->getCameraInfo(mId, &info);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 if (res != OK) return res;
139
140 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700141 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
142 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700143 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800144 device->common.close(&device->common);
145 return BAD_VALUE;
146 }
147
148 /** Initialize device with callback functions */
149
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700150 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700152 ATRACE_END();
153
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800154 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700155 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
156 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800157 device->common.close(&device->common);
158 return BAD_VALUE;
159 }
160
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700161 /** Start up status tracker thread */
162 mStatusTracker = new StatusTracker(this);
163 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
164 if (res != OK) {
165 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
166 strerror(-res), res);
167 device->common.close(&device->common);
168 mStatusTracker.clear();
169 return res;
170 }
171
Eino-Ville Talvalae4df8ab2016-07-21 12:53:07 -0700172 /** Register in-flight map to the status tracker */
173 mInFlightStatusId = mStatusTracker->addComponent();
174
Zhijun He125684a2015-12-26 15:07:30 -0800175 /** Create buffer manager */
176 mBufferManager = new Camera3BufferManager();
177
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700178 bool aeLockAvailable = false;
179 camera_metadata_ro_entry aeLockAvailableEntry;
180 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
181 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
182 if (res == OK && aeLockAvailableEntry.count > 0) {
183 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
184 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
185 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800186
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700187 /** Start up request queue thread */
188 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800189 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700191 SET_ERR_L("Unable to start request queue thread: %s (%d)",
192 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800193 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800194 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800195 return res;
196 }
197
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700198 mPreparerThread = new PreparerThread();
199
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800200 /** Everything is good to go */
201
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700202 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800203 mDeviceInfo = info.static_camera_characteristics;
204 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700205
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700206 // Determine whether we need to derive sensitivity boost values for older devices.
207 // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
208 // be listed (as the default value 100)
209 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_4 &&
210 mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
211 mDerivePostRawSensKey = true;
212 }
213
Ruben Brunk183f0562015-08-12 12:55:02 -0700214 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800215 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700216 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700217 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700218 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800219
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800220 // Measure the clock domain offset between camera and video/hw_composer
221 camera_metadata_entry timestampSource =
222 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
223 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
224 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
225 mTimestampOffset = getMonoToBoottimeOffset();
226 }
227
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700228 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700229 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
230 camera_metadata_entry partialResultsCount =
231 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
232 if (partialResultsCount.count > 0) {
233 mNumPartialResults = partialResultsCount.data.i32[0];
234 mUsePartialResult = (mNumPartialResults > 1);
235 }
236 } else {
237 camera_metadata_entry partialResultsQuirk =
238 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
239 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
240 mUsePartialResult = true;
241 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700242 }
243
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700244 camera_metadata_entry configs =
245 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
246 for (uint32_t i = 0; i < configs.count; i += 4) {
247 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
248 configs.data.i32[i + 3] ==
249 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
250 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
251 configs.data.i32[i + 2]));
252 }
253 }
254
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800255 return OK;
256}
257
258status_t Camera3Device::disconnect() {
259 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700260 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800261
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800262 ALOGV("%s: E", __FUNCTION__);
263
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700264 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800265
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700266 {
267 Mutex::Autolock l(mLock);
268 if (mStatus == STATUS_UNINITIALIZED) return res;
269
270 if (mStatus == STATUS_ACTIVE ||
271 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
272 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700273 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700274 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700275 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700276 } else {
277 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
278 if (res != OK) {
279 SET_ERR_L("Timeout waiting for HAL to drain");
280 // Continue to close device even in case of error
281 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700282 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800283 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800284
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700285 if (mStatus == STATUS_ERROR) {
286 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700287 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700288
289 if (mStatusTracker != NULL) {
290 mStatusTracker->requestExit();
291 }
292
293 if (mRequestThread != NULL) {
294 mRequestThread->requestExit();
295 }
296
297 mOutputStreams.clear();
298 mInputStream.clear();
299 }
300
301 // Joining done without holding mLock, otherwise deadlocks may ensue
302 // as the threads try to access parent state
303 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
304 // HAL may be in a bad state, so waiting for request thread
305 // (which may be stuck in the HAL processCaptureRequest call)
306 // could be dangerous.
307 mRequestThread->join();
308 }
309
310 if (mStatusTracker != NULL) {
311 mStatusTracker->join();
312 }
313
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700314 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700315 {
316 Mutex::Autolock l(mLock);
317
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800318 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700319 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800320 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800321
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700322 hal3Device = mHal3Device;
323 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800324
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700325 // Call close without internal mutex held, as the HAL close may need to
326 // wait on assorted callbacks,etc, to complete before it can return.
327 if (hal3Device != NULL) {
328 ATRACE_BEGIN("camera3->close");
329 hal3Device->common.close(&hal3Device->common);
330 ATRACE_END();
331 }
332
333 {
334 Mutex::Autolock l(mLock);
335 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700336 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700337 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800338
339 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700340 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800341}
342
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700343// For dumping/debugging only -
344// try to acquire a lock a few times, eventually give up to proceed with
345// debug/dump operations
346bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
347 bool gotLock = false;
348 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
349 if (lock.tryLock() == NO_ERROR) {
350 gotLock = true;
351 break;
352 } else {
353 usleep(kDumpSleepDuration);
354 }
355 }
356 return gotLock;
357}
358
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700359Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
360 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
361 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
362 const int STREAM_CONFIGURATION_SIZE = 4;
363 const int STREAM_FORMAT_OFFSET = 0;
364 const int STREAM_WIDTH_OFFSET = 1;
365 const int STREAM_HEIGHT_OFFSET = 2;
366 const int STREAM_IS_INPUT_OFFSET = 3;
367 camera_metadata_ro_entry_t availableStreamConfigs =
368 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
369 if (availableStreamConfigs.count == 0 ||
370 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
371 return Size(0, 0);
372 }
373
374 // Get max jpeg size (area-wise).
375 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
376 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
377 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
378 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
379 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
380 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
381 && format == HAL_PIXEL_FORMAT_BLOB &&
382 (width * height > maxJpegWidth * maxJpegHeight)) {
383 maxJpegWidth = width;
384 maxJpegHeight = height;
385 }
386 }
387 } else {
388 camera_metadata_ro_entry availableJpegSizes =
389 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
390 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
391 return Size(0, 0);
392 }
393
394 // Get max jpeg size (area-wise).
395 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
396 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
397 > (maxJpegWidth * maxJpegHeight)) {
398 maxJpegWidth = availableJpegSizes.data.i32[i];
399 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
400 }
401 }
402 }
403 return Size(maxJpegWidth, maxJpegHeight);
404}
405
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800406nsecs_t Camera3Device::getMonoToBoottimeOffset() {
407 // try three times to get the clock offset, choose the one
408 // with the minimum gap in measurements.
409 const int tries = 3;
410 nsecs_t bestGap, measured;
411 for (int i = 0; i < tries; ++i) {
412 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
413 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
414 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
415 const nsecs_t gap = tmono2 - tmono;
416 if (i == 0 || gap < bestGap) {
417 bestGap = gap;
418 measured = tbase - ((tmono + tmono2) >> 1);
419 }
420 }
421 return measured;
422}
423
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -0700424/**
425 * Map Android N dataspace definitions back to Android M definitions, for
426 * use with HALv3.3 or older.
427 *
428 * Only map where correspondences exist, and otherwise preserve the value.
429 */
430android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) {
431 switch (dataSpace) {
432 case HAL_DATASPACE_V0_SRGB_LINEAR:
433 return HAL_DATASPACE_SRGB_LINEAR;
434 case HAL_DATASPACE_V0_SRGB:
435 return HAL_DATASPACE_SRGB;
436 case HAL_DATASPACE_V0_JFIF:
437 return HAL_DATASPACE_JFIF;
438 case HAL_DATASPACE_V0_BT601_625:
439 return HAL_DATASPACE_BT601_625;
440 case HAL_DATASPACE_V0_BT601_525:
441 return HAL_DATASPACE_BT601_525;
442 case HAL_DATASPACE_V0_BT709:
443 return HAL_DATASPACE_BT709;
444 default:
445 return dataSpace;
446 }
447}
448
Zhijun Hef7da0962014-04-24 13:27:56 -0700449ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700450 // Get max jpeg size (area-wise).
451 Size maxJpegResolution = getMaxJpegResolution();
452 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700453 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700454 __FUNCTION__, mId);
455 return BAD_VALUE;
456 }
457
Zhijun Hef7da0962014-04-24 13:27:56 -0700458 // Get max jpeg buffer size
459 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700460 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
461 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700462 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
463 return BAD_VALUE;
464 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700465 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800466 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700467
468 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700469 float scaleFactor = ((float) (width * height)) /
470 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800471 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
472 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700473 if (jpegBufferSize > maxJpegBufferSize) {
474 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700475 }
476
477 return jpegBufferSize;
478}
479
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700480ssize_t Camera3Device::getPointCloudBufferSize() const {
481 const int FLOATS_PER_POINT=4;
482 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
483 if (maxPointCount.count == 0) {
484 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
485 __FUNCTION__, mId);
486 return BAD_VALUE;
487 }
488 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
489 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
490 return maxBytesForPointCloud;
491}
492
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800493ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800494 const int PER_CONFIGURATION_SIZE = 3;
495 const int WIDTH_OFFSET = 0;
496 const int HEIGHT_OFFSET = 1;
497 const int SIZE_OFFSET = 2;
498 camera_metadata_ro_entry rawOpaqueSizes =
499 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800500 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800501 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800502 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800503 __FUNCTION__, mId, count);
504 return BAD_VALUE;
505 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700506
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800507 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
508 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
509 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
510 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
511 }
512 }
513
514 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
515 __FUNCTION__, mId, width, height);
516 return BAD_VALUE;
517}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700518
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800519status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
520 ATRACE_CALL();
521 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700522
523 // Try to lock, but continue in case of failure (to avoid blocking in
524 // deadlocks)
525 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
526 bool gotLock = tryLockSpinRightRound(mLock);
527
528 ALOGW_IF(!gotInterfaceLock,
529 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
530 mId, __FUNCTION__);
531 ALOGW_IF(!gotLock,
532 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
533 mId, __FUNCTION__);
534
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800535 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700536
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800537 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700538 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800539 int n = args.size();
540 for (int i = 0; i < n; i++) {
541 if (args[i] == templatesOption) {
542 dumpTemplates = true;
543 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700544 if (args[i] == monitorOption) {
545 if (i + 1 < n) {
546 String8 monitorTags = String8(args[i + 1]);
547 if (monitorTags == "off") {
548 mTagMonitor.disableMonitoring();
549 } else {
550 mTagMonitor.parseTagsToMonitor(monitorTags);
551 }
552 } else {
553 mTagMonitor.disableMonitoring();
554 }
555 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800556 }
557
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800558 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800559
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800560 const char *status =
561 mStatus == STATUS_ERROR ? "ERROR" :
562 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700563 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
564 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800565 mStatus == STATUS_ACTIVE ? "ACTIVE" :
566 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700567
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800568 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700569 if (mStatus == STATUS_ERROR) {
570 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
571 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800572 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700573 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700574 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800575
576 if (mInputStream != NULL) {
577 write(fd, lines.string(), lines.size());
578 mInputStream->dump(fd, args);
579 } else {
580 lines.appendFormat(" No input stream.\n");
581 write(fd, lines.string(), lines.size());
582 }
583 for (size_t i = 0; i < mOutputStreams.size(); i++) {
584 mOutputStreams[i]->dump(fd,args);
585 }
586
Zhijun He431503c2016-03-07 17:30:16 -0800587 if (mBufferManager != NULL) {
588 lines = String8(" Camera3 Buffer Manager:\n");
589 write(fd, lines.string(), lines.size());
590 mBufferManager->dump(fd, args);
591 }
Zhijun He125684a2015-12-26 15:07:30 -0800592
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700593 lines = String8(" In-flight requests:\n");
594 if (mInFlightMap.size() == 0) {
595 lines.append(" None\n");
596 } else {
597 for (size_t i = 0; i < mInFlightMap.size(); i++) {
598 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700599 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700600 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800601 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700602 r.numBuffersLeft);
603 }
604 }
605 write(fd, lines.string(), lines.size());
606
Igor Murashkin1e479c02013-09-06 16:55:14 -0700607 {
608 lines = String8(" Last request sent:\n");
609 write(fd, lines.string(), lines.size());
610
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700611 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700612 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
613 }
614
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800615 if (dumpTemplates) {
616 const char *templateNames[] = {
617 "TEMPLATE_PREVIEW",
618 "TEMPLATE_STILL_CAPTURE",
619 "TEMPLATE_VIDEO_RECORD",
620 "TEMPLATE_VIDEO_SNAPSHOT",
621 "TEMPLATE_ZERO_SHUTTER_LAG",
622 "TEMPLATE_MANUAL"
623 };
624
625 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
626 const camera_metadata_t *templateRequest;
627 templateRequest =
628 mHal3Device->ops->construct_default_request_settings(
629 mHal3Device, i);
630 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
631 if (templateRequest == NULL) {
632 lines.append(" Not supported\n");
633 write(fd, lines.string(), lines.size());
634 } else {
635 write(fd, lines.string(), lines.size());
636 dump_indented_camera_metadata(templateRequest,
637 fd, /*verbosity*/2, /*indentation*/8);
638 }
639 }
640 }
641
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700642 mTagMonitor.dumpMonitoredMetadata(fd);
643
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800644 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700645 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800646 write(fd, lines.string(), lines.size());
647 mHal3Device->ops->dump(mHal3Device, fd);
648 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800649
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700650 if (gotLock) mLock.unlock();
651 if (gotInterfaceLock) mInterfaceLock.unlock();
652
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800653 return OK;
654}
655
656const CameraMetadata& Camera3Device::info() const {
657 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800658 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
659 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700660 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800661 mStatus == STATUS_ERROR ?
662 "when in error state" : "before init");
663 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800664 return mDeviceInfo;
665}
666
Jianing Wei90e59c92014-03-12 18:29:36 -0700667status_t Camera3Device::checkStatusOkToCaptureLocked() {
668 switch (mStatus) {
669 case STATUS_ERROR:
670 CLOGE("Device has encountered a serious error");
671 return INVALID_OPERATION;
672 case STATUS_UNINITIALIZED:
673 CLOGE("Device not initialized");
674 return INVALID_OPERATION;
675 case STATUS_UNCONFIGURED:
676 case STATUS_CONFIGURED:
677 case STATUS_ACTIVE:
678 // OK
679 break;
680 default:
681 SET_ERR_L("Unexpected status: %d", mStatus);
682 return INVALID_OPERATION;
683 }
684 return OK;
685}
686
687status_t Camera3Device::convertMetadataListToRequestListLocked(
688 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
689 if (requestList == NULL) {
690 CLOGE("requestList cannot be NULL.");
691 return BAD_VALUE;
692 }
693
Jianing Weicb0652e2014-03-12 18:29:36 -0700694 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700695 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
696 it != metadataList.end(); ++it) {
697 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
698 if (newRequest == 0) {
699 CLOGE("Can't create capture request");
700 return BAD_VALUE;
701 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700702
703 // Setup burst Id and request Id
704 newRequest->mResultExtras.burstId = burstId++;
705 if (it->exists(ANDROID_REQUEST_ID)) {
706 if (it->find(ANDROID_REQUEST_ID).count == 0) {
707 CLOGE("RequestID entry exists; but must not be empty in metadata");
708 return BAD_VALUE;
709 }
710 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
711 } else {
712 CLOGE("RequestID does not exist in metadata");
713 return BAD_VALUE;
714 }
715
Jianing Wei90e59c92014-03-12 18:29:36 -0700716 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700717
718 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700719 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700720
721 // Setup batch size if this is a high speed video recording request.
722 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
723 auto firstRequest = requestList->begin();
724 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
725 if (outputStream->isVideoStream()) {
726 (*firstRequest)->mBatchSize = requestList->size();
727 break;
728 }
729 }
730 }
731
Jianing Wei90e59c92014-03-12 18:29:36 -0700732 return OK;
733}
734
Jianing Weicb0652e2014-03-12 18:29:36 -0700735status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800736 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800737
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700738 List<const CameraMetadata> requests;
739 requests.push_back(request);
740 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800741}
742
Jianing Wei90e59c92014-03-12 18:29:36 -0700743status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700744 const List<const CameraMetadata> &requests, bool repeating,
745 /*out*/
746 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700747 ATRACE_CALL();
748 Mutex::Autolock il(mInterfaceLock);
749 Mutex::Autolock l(mLock);
750
751 status_t res = checkStatusOkToCaptureLocked();
752 if (res != OK) {
753 // error logged by previous call
754 return res;
755 }
756
757 RequestList requestList;
758
759 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
760 if (res != OK) {
761 // error logged by previous call
762 return res;
763 }
764
765 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700766 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700767 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700768 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700769 }
770
771 if (res == OK) {
772 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
773 if (res != OK) {
774 SET_ERR_L("Can't transition to active in %f seconds!",
775 kActiveTimeout/1e9);
776 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700777 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
778 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700779 } else {
780 CLOGE("Cannot queue request. Impossible.");
781 return BAD_VALUE;
782 }
783
784 return res;
785}
786
Jianing Weicb0652e2014-03-12 18:29:36 -0700787status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
788 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700789 ATRACE_CALL();
790
Jianing Weicb0652e2014-03-12 18:29:36 -0700791 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700792}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800793
Jianing Weicb0652e2014-03-12 18:29:36 -0700794status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
795 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800796 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800797
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700798 List<const CameraMetadata> requests;
799 requests.push_back(request);
800 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800801}
802
Jianing Weicb0652e2014-03-12 18:29:36 -0700803status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
804 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700805 ATRACE_CALL();
806
Jianing Weicb0652e2014-03-12 18:29:36 -0700807 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700808}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800809
810sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
811 const CameraMetadata &request) {
812 status_t res;
813
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700814 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800815 res = configureStreamsLocked();
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700816 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800817 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700818 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800819 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700820 } else if (mStatus == STATUS_UNCONFIGURED) {
821 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700822 CLOGE("No streams configured");
823 return NULL;
824 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800825 }
826
827 sp<CaptureRequest> newRequest = createCaptureRequest(request);
828 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800829}
830
Jianing Weicb0652e2014-03-12 18:29:36 -0700831status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800832 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700833 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800834 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800835
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800836 switch (mStatus) {
837 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700838 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800839 return INVALID_OPERATION;
840 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700841 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800842 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700843 case STATUS_UNCONFIGURED:
844 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800845 case STATUS_ACTIVE:
846 // OK
847 break;
848 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700849 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800850 return INVALID_OPERATION;
851 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700852 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700853
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700854 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800855}
856
857status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
858 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700859 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800860
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700861 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800862}
863
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700864status_t Camera3Device::createInputStream(
865 uint32_t width, uint32_t height, int format, int *id) {
866 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700867 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700868 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700869 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
870 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700871
872 status_t res;
873 bool wasActive = false;
874
875 switch (mStatus) {
876 case STATUS_ERROR:
877 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
878 return INVALID_OPERATION;
879 case STATUS_UNINITIALIZED:
880 ALOGE("%s: Device not initialized", __FUNCTION__);
881 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700882 case STATUS_UNCONFIGURED:
883 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700884 // OK
885 break;
886 case STATUS_ACTIVE:
887 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700888 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700889 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700890 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700891 return res;
892 }
893 wasActive = true;
894 break;
895 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700896 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700897 return INVALID_OPERATION;
898 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700899 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700900
901 if (mInputStream != 0) {
902 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
903 return INVALID_OPERATION;
904 }
905
906 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
907 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700908 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700909
910 mInputStream = newStream;
911
912 *id = mNextStreamId++;
913
914 // Continue captures if active at start
915 if (wasActive) {
916 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
917 res = configureStreamsLocked();
918 if (res != OK) {
919 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
920 __FUNCTION__, mNextStreamId, strerror(-res), res);
921 return res;
922 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700923 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700924 }
925
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700926 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700927 return OK;
928}
929
Igor Murashkin2fba5842013-04-22 14:03:54 -0700930
931status_t Camera3Device::createZslStream(
932 uint32_t width, uint32_t height,
933 int depth,
934 /*out*/
935 int *id,
936 sp<Camera3ZslStream>* zslStream) {
937 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700938 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700939 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700940 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
941 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700942
943 status_t res;
944 bool wasActive = false;
945
946 switch (mStatus) {
947 case STATUS_ERROR:
948 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
949 return INVALID_OPERATION;
950 case STATUS_UNINITIALIZED:
951 ALOGE("%s: Device not initialized", __FUNCTION__);
952 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700953 case STATUS_UNCONFIGURED:
954 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700955 // OK
956 break;
957 case STATUS_ACTIVE:
958 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700959 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700960 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700961 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700962 return res;
963 }
964 wasActive = true;
965 break;
966 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700967 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700968 return INVALID_OPERATION;
969 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700970 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700971
972 if (mInputStream != 0) {
973 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
974 return INVALID_OPERATION;
975 }
976
977 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
978 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700979 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700980
981 res = mOutputStreams.add(mNextStreamId, newStream);
982 if (res < 0) {
983 ALOGE("%s: Can't add new stream to set: %s (%d)",
984 __FUNCTION__, strerror(-res), res);
985 return res;
986 }
987 mInputStream = newStream;
988
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530989 mNeedConfig = true;
990
Igor Murashkin2fba5842013-04-22 14:03:54 -0700991 *id = mNextStreamId++;
992 *zslStream = newStream;
993
994 // Continue captures if active at start
995 if (wasActive) {
996 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
997 res = configureStreamsLocked();
998 if (res != OK) {
999 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1000 __FUNCTION__, mNextStreamId, strerror(-res), res);
1001 return res;
1002 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001003 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -07001004 }
1005
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001006 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -07001007 return OK;
1008}
1009
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001010status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001011 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -07001012 camera3_stream_rotation_t rotation, int *id, int streamSetId, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001013 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001014 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001015 Mutex::Autolock l(mLock);
Zhijun He5d677d12016-05-29 16:52:39 -07001016 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
1017 " consumer usage 0x%x", mId, mNextStreamId, width, height, format, dataSpace, rotation,
1018 consumerUsage);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001019
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001020 status_t res;
1021 bool wasActive = false;
1022
1023 switch (mStatus) {
1024 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001025 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001026 return INVALID_OPERATION;
1027 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001028 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001029 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001030 case STATUS_UNCONFIGURED:
1031 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001032 // OK
1033 break;
1034 case STATUS_ACTIVE:
1035 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001036 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001037 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001038 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001039 return res;
1040 }
1041 wasActive = true;
1042 break;
1043 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001044 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001045 return INVALID_OPERATION;
1046 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001047 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001048
1049 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001050 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001051 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001052 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001053 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1054 }
Zhijun He5d677d12016-05-29 16:52:39 -07001055
1056 // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
1057 // which requires a consumer surface to be available.
1058 if (consumer == nullptr && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1059 ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
1060 return BAD_VALUE;
1061 }
1062
1063 if (consumer == nullptr && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1064 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1065 return BAD_VALUE;
1066 }
1067
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001068 // Use legacy dataspace values for older HALs
1069 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1070 dataSpace = mapToLegacyDataspace(dataSpace);
1071 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001072 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001073 ssize_t blobBufferSize;
1074 if (dataSpace != HAL_DATASPACE_DEPTH) {
1075 blobBufferSize = getJpegBufferSize(width, height);
1076 if (blobBufferSize <= 0) {
1077 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1078 return BAD_VALUE;
1079 }
1080 } else {
1081 blobBufferSize = getPointCloudBufferSize();
1082 if (blobBufferSize <= 0) {
1083 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1084 return BAD_VALUE;
1085 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001086 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001087 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001088 width, height, blobBufferSize, format, dataSpace, rotation,
1089 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001090 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1091 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1092 if (rawOpaqueBufferSize <= 0) {
1093 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1094 return BAD_VALUE;
1095 }
1096 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001097 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1098 mTimestampOffset, streamSetId);
Zhijun He5d677d12016-05-29 16:52:39 -07001099 } else if (consumer == nullptr) {
1100 newStream = new Camera3OutputStream(mNextStreamId,
1101 width, height, format, consumerUsage, dataSpace, rotation,
1102 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001103 } else {
1104 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001105 width, height, format, dataSpace, rotation,
1106 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001107 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001108 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001109
Zhijun He125684a2015-12-26 15:07:30 -08001110 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001111 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1112 * requires buffers to be statically allocated for internal static buffer registration, while
1113 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1114 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001115 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001116 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001117 newStream->setBufferManager(mBufferManager);
1118 }
1119
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001120 res = mOutputStreams.add(mNextStreamId, newStream);
1121 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001122 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001123 return res;
1124 }
1125
1126 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001127 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001128
1129 // Continue captures if active at start
1130 if (wasActive) {
1131 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1132 res = configureStreamsLocked();
1133 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001134 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1135 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001136 return res;
1137 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001138 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001139 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001140 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001142}
1143
1144status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1145 ATRACE_CALL();
1146 (void)outputId; (void)id;
1147
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001148 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001149 return INVALID_OPERATION;
1150}
1151
1152
1153status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001154 uint32_t *width, uint32_t *height,
1155 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001156 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001157 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001158 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001159
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001160 switch (mStatus) {
1161 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001162 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001163 return INVALID_OPERATION;
1164 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001165 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001166 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001167 case STATUS_UNCONFIGURED:
1168 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001169 case STATUS_ACTIVE:
1170 // OK
1171 break;
1172 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001173 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001174 return INVALID_OPERATION;
1175 }
1176
1177 ssize_t idx = mOutputStreams.indexOfKey(id);
1178 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001179 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001180 return idx;
1181 }
1182
1183 if (width) *width = mOutputStreams[idx]->getWidth();
1184 if (height) *height = mOutputStreams[idx]->getHeight();
1185 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001186 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001187 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001188}
1189
1190status_t Camera3Device::setStreamTransform(int id,
1191 int transform) {
1192 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001193 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001194 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001195
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001196 switch (mStatus) {
1197 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001198 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001199 return INVALID_OPERATION;
1200 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001201 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001202 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001203 case STATUS_UNCONFIGURED:
1204 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001205 case STATUS_ACTIVE:
1206 // OK
1207 break;
1208 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001209 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001210 return INVALID_OPERATION;
1211 }
1212
1213 ssize_t idx = mOutputStreams.indexOfKey(id);
1214 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001215 CLOGE("Stream %d does not exist",
1216 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001217 return BAD_VALUE;
1218 }
1219
1220 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001221}
1222
1223status_t Camera3Device::deleteStream(int id) {
1224 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001225 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001226 Mutex::Autolock l(mLock);
1227 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001228
Igor Murashkine2172be2013-05-28 15:31:39 -07001229 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1230
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001231 // CameraDevice semantics require device to already be idle before
1232 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001233 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001234 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1235 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001236 }
1237
Igor Murashkin2fba5842013-04-22 14:03:54 -07001238 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001239 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001240 if (mInputStream != NULL && id == mInputStream->getId()) {
1241 deletedStream = mInputStream;
1242 mInputStream.clear();
1243 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001244 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001245 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001246 return BAD_VALUE;
1247 }
Zhijun He5f446352014-01-22 09:49:33 -08001248 }
1249
1250 // Delete output stream or the output part of a bi-directional stream.
1251 if (outputStreamIdx != NAME_NOT_FOUND) {
1252 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001253 mOutputStreams.removeItem(id);
1254 }
1255
1256 // Free up the stream endpoint so that it can be used by some other stream
1257 res = deletedStream->disconnect();
1258 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001259 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001260 // fall through since we want to still list the stream as deleted.
1261 }
1262 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001263 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001264
1265 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001266}
1267
1268status_t Camera3Device::deleteReprocessStream(int id) {
1269 ATRACE_CALL();
1270 (void)id;
1271
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001272 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001273 return INVALID_OPERATION;
1274}
1275
Zhijun He1fa89992015-06-01 15:44:31 -07001276status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001277 ATRACE_CALL();
1278 ALOGV("%s: E", __FUNCTION__);
1279
1280 Mutex::Autolock il(mInterfaceLock);
1281 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001282
1283 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1284 mNeedConfig = true;
1285 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1286 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001287
1288 return configureStreamsLocked();
1289}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001290
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001291status_t Camera3Device::getInputBufferProducer(
1292 sp<IGraphicBufferProducer> *producer) {
1293 Mutex::Autolock il(mInterfaceLock);
1294 Mutex::Autolock l(mLock);
1295
1296 if (producer == NULL) {
1297 return BAD_VALUE;
1298 } else if (mInputStream == NULL) {
1299 return INVALID_OPERATION;
1300 }
1301
1302 return mInputStream->getInputBufferProducer(producer);
1303}
1304
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001305status_t Camera3Device::createDefaultRequest(int templateId,
1306 CameraMetadata *request) {
1307 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001308 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001309
1310 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1311 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1312 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1313 return BAD_VALUE;
1314 }
1315
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001316 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001317 Mutex::Autolock l(mLock);
1318
1319 switch (mStatus) {
1320 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001321 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001322 return INVALID_OPERATION;
1323 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001324 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001325 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001326 case STATUS_UNCONFIGURED:
1327 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001328 case STATUS_ACTIVE:
1329 // OK
1330 break;
1331 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001332 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001333 return INVALID_OPERATION;
1334 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001335
Zhijun Hea1530f12014-09-14 12:44:20 -07001336 if (!mRequestTemplateCache[templateId].isEmpty()) {
1337 *request = mRequestTemplateCache[templateId];
1338 return OK;
1339 }
1340
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001341 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001342 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001343 rawRequest = mHal3Device->ops->construct_default_request_settings(
1344 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001345 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001346 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001347 ALOGI("%s: template %d is not supported on this camera device",
1348 __FUNCTION__, templateId);
1349 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001350 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001351
Zhijun Hea1530f12014-09-14 12:44:20 -07001352 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001353
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001354 // Derive some new keys for backward compatibility
1355 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
1356 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
1357 int32_t defaultBoost[1] = {100};
1358 mRequestTemplateCache[templateId].update(
1359 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1360 defaultBoost, 1);
1361 }
1362
1363 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001364 return OK;
1365}
1366
1367status_t Camera3Device::waitUntilDrained() {
1368 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001369 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001370 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001371
Zhijun He69a37482014-03-23 18:44:49 -07001372 return waitUntilDrainedLocked();
1373}
1374
1375status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001376 switch (mStatus) {
1377 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001378 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001379 ALOGV("%s: Already idle", __FUNCTION__);
1380 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001381 case STATUS_CONFIGURED:
1382 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001383 case STATUS_ERROR:
1384 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001385 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001386 break;
1387 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001388 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001389 return INVALID_OPERATION;
1390 }
1391
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001392 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1393 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001394 if (res != OK) {
1395 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1396 res);
1397 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001398 return res;
1399}
1400
Ruben Brunk183f0562015-08-12 12:55:02 -07001401
1402void Camera3Device::internalUpdateStatusLocked(Status status) {
1403 mStatus = status;
1404 mRecentStatusUpdates.add(mStatus);
1405 mStatusChanged.broadcast();
1406}
1407
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001408// Pause to reconfigure
1409status_t Camera3Device::internalPauseAndWaitLocked() {
1410 mRequestThread->setPaused(true);
1411 mPauseStateNotify = true;
1412
1413 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1414 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1415 if (res != OK) {
1416 SET_ERR_L("Can't idle device in %f seconds!",
1417 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001418 }
1419
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001420 return res;
1421}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001422
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001423// Resume after internalPauseAndWaitLocked
1424status_t Camera3Device::internalResumeLocked() {
1425 status_t res;
1426
1427 mRequestThread->setPaused(false);
1428
1429 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1430 if (res != OK) {
1431 SET_ERR_L("Can't transition to active in %f seconds!",
1432 kActiveTimeout/1e9);
1433 }
1434 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001435 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001436}
1437
Ruben Brunk183f0562015-08-12 12:55:02 -07001438status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001439 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001440
1441 size_t startIndex = 0;
1442 if (mStatusWaiters == 0) {
1443 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1444 // this status list
1445 mRecentStatusUpdates.clear();
1446 } else {
1447 // If other threads are waiting on updates to this status list, set the position of the
1448 // first element that this list will check rather than clearing the list.
1449 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001450 }
1451
Ruben Brunk183f0562015-08-12 12:55:02 -07001452 mStatusWaiters++;
1453
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001454 bool stateSeen = false;
1455 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001456 if (active == (mStatus == STATUS_ACTIVE)) {
1457 // Desired state is current
1458 break;
1459 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001460
1461 res = mStatusChanged.waitRelative(mLock, timeout);
1462 if (res != OK) break;
1463
Ruben Brunk183f0562015-08-12 12:55:02 -07001464 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1465 // transitions.
1466 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1467 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1468 __FUNCTION__);
1469
1470 // Encountered desired state since we began waiting
1471 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001472 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1473 stateSeen = true;
1474 break;
1475 }
1476 }
1477 } while (!stateSeen);
1478
Ruben Brunk183f0562015-08-12 12:55:02 -07001479 mStatusWaiters--;
1480
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001481 return res;
1482}
1483
1484
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001485status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1486 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001487 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001488
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001489 if (listener != NULL && mListener != NULL) {
1490 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1491 }
1492 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001493 mRequestThread->setNotificationListener(listener);
1494 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001495
1496 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001497}
1498
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001499bool Camera3Device::willNotify3A() {
1500 return false;
1501}
1502
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001503status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001504 status_t res;
1505 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001506
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001507 while (mResultQueue.empty()) {
1508 res = mResultSignal.waitRelative(mOutputLock, timeout);
1509 if (res == TIMED_OUT) {
1510 return res;
1511 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001512 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001513 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001514 return res;
1515 }
1516 }
1517 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001518}
1519
Jianing Weicb0652e2014-03-12 18:29:36 -07001520status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001521 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001522 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001523
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001524 if (mResultQueue.empty()) {
1525 return NOT_ENOUGH_DATA;
1526 }
1527
Jianing Weicb0652e2014-03-12 18:29:36 -07001528 if (frame == NULL) {
1529 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1530 return BAD_VALUE;
1531 }
1532
1533 CaptureResult &result = *(mResultQueue.begin());
1534 frame->mResultExtras = result.mResultExtras;
1535 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001536 mResultQueue.erase(mResultQueue.begin());
1537
1538 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001539}
1540
1541status_t Camera3Device::triggerAutofocus(uint32_t id) {
1542 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001543 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001544
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001545 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1546 // Mix-in this trigger into the next request and only the next request.
1547 RequestTrigger trigger[] = {
1548 {
1549 ANDROID_CONTROL_AF_TRIGGER,
1550 ANDROID_CONTROL_AF_TRIGGER_START
1551 },
1552 {
1553 ANDROID_CONTROL_AF_TRIGGER_ID,
1554 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001555 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001556 };
1557
1558 return mRequestThread->queueTrigger(trigger,
1559 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001560}
1561
1562status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1563 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001564 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001565
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001566 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1567 // Mix-in this trigger into the next request and only the next request.
1568 RequestTrigger trigger[] = {
1569 {
1570 ANDROID_CONTROL_AF_TRIGGER,
1571 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1572 },
1573 {
1574 ANDROID_CONTROL_AF_TRIGGER_ID,
1575 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001576 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001577 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001578
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001579 return mRequestThread->queueTrigger(trigger,
1580 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001581}
1582
1583status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1584 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001585 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001586
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001587 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1588 // Mix-in this trigger into the next request and only the next request.
1589 RequestTrigger trigger[] = {
1590 {
1591 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1592 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1593 },
1594 {
1595 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1596 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001597 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001598 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001599
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001600 return mRequestThread->queueTrigger(trigger,
1601 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001602}
1603
1604status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1605 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1606 ATRACE_CALL();
1607 (void)reprocessStreamId; (void)buffer; (void)listener;
1608
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001609 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001610 return INVALID_OPERATION;
1611}
1612
Jianing Weicb0652e2014-03-12 18:29:36 -07001613status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001614 ATRACE_CALL();
1615 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001616 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001617
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001618 NotificationListener* listener;
1619 {
1620 Mutex::Autolock l(mOutputLock);
1621 listener = mListener;
1622 }
1623
Zhijun He7ef20392014-04-21 16:04:17 -07001624 {
1625 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001626 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001627 }
1628
Zhijun He491e3412013-12-27 10:57:44 -08001629 status_t res;
1630 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001631 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001632 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001633 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001634 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001635 }
1636
1637 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001638}
1639
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001640status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001641 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1642}
1643
1644status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001645 ATRACE_CALL();
1646 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001647 Mutex::Autolock il(mInterfaceLock);
1648 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001649
1650 sp<Camera3StreamInterface> stream;
1651 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1652 if (outputStreamIdx == NAME_NOT_FOUND) {
1653 CLOGE("Stream %d does not exist", streamId);
1654 return BAD_VALUE;
1655 }
1656
1657 stream = mOutputStreams.editValueAt(outputStreamIdx);
1658
1659 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001660 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001661 return BAD_VALUE;
1662 }
1663
1664 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001665 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001666 return BAD_VALUE;
1667 }
1668
Ruben Brunkc78ac262015-08-13 17:58:46 -07001669 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001670}
1671
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001672status_t Camera3Device::tearDown(int streamId) {
1673 ATRACE_CALL();
1674 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1675 Mutex::Autolock il(mInterfaceLock);
1676 Mutex::Autolock l(mLock);
1677
1678 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1679 // since we cannot call register_stream_buffers except right after configure_streams.
1680 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1681 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1682 __FUNCTION__, mHal3Device->common.version);
1683 return NO_INIT;
1684 }
1685
1686 sp<Camera3StreamInterface> stream;
1687 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1688 if (outputStreamIdx == NAME_NOT_FOUND) {
1689 CLOGE("Stream %d does not exist", streamId);
1690 return BAD_VALUE;
1691 }
1692
1693 stream = mOutputStreams.editValueAt(outputStreamIdx);
1694
1695 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1696 CLOGE("Stream %d is a target of a in-progress request", streamId);
1697 return BAD_VALUE;
1698 }
1699
1700 return stream->tearDown();
1701}
1702
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001703status_t Camera3Device::addBufferListenerForStream(int streamId,
1704 wp<Camera3StreamBufferListener> listener) {
1705 ATRACE_CALL();
1706 ALOGV("%s: Camera %d: Adding buffer listener for stream %d", __FUNCTION__, mId, streamId);
1707 Mutex::Autolock il(mInterfaceLock);
1708 Mutex::Autolock l(mLock);
1709
1710 sp<Camera3StreamInterface> stream;
1711 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1712 if (outputStreamIdx == NAME_NOT_FOUND) {
1713 CLOGE("Stream %d does not exist", streamId);
1714 return BAD_VALUE;
1715 }
1716
1717 stream = mOutputStreams.editValueAt(outputStreamIdx);
1718 stream->addBufferListener(listener);
1719
1720 return OK;
1721}
1722
Zhijun He204e3292014-07-14 17:09:23 -07001723uint32_t Camera3Device::getDeviceVersion() {
1724 ATRACE_CALL();
1725 Mutex::Autolock il(mInterfaceLock);
1726 return mDeviceVersion;
1727}
1728
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001729/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001730 * Methods called by subclasses
1731 */
1732
1733void Camera3Device::notifyStatus(bool idle) {
1734 {
1735 // Need mLock to safely update state and synchronize to current
1736 // state of methods in flight.
1737 Mutex::Autolock l(mLock);
1738 // We can get various system-idle notices from the status tracker
1739 // while starting up. Only care about them if we've actually sent
1740 // in some requests recently.
1741 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1742 return;
1743 }
1744 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1745 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001746 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001747
1748 // Skip notifying listener if we're doing some user-transparent
1749 // state changes
1750 if (mPauseStateNotify) return;
1751 }
1752 NotificationListener *listener;
1753 {
1754 Mutex::Autolock l(mOutputLock);
1755 listener = mListener;
1756 }
1757 if (idle && listener != NULL) {
1758 listener->notifyIdle();
1759 }
1760}
1761
Zhijun He5d677d12016-05-29 16:52:39 -07001762status_t Camera3Device::setConsumerSurface(int streamId, sp<Surface> consumer) {
1763 ATRACE_CALL();
1764 ALOGV("%s: Camera %d: set consumer surface for stream %d", __FUNCTION__, mId, streamId);
1765 Mutex::Autolock il(mInterfaceLock);
1766 Mutex::Autolock l(mLock);
1767
1768 if (consumer == nullptr) {
1769 CLOGE("Null consumer is passed!");
1770 return BAD_VALUE;
1771 }
1772
1773 ssize_t idx = mOutputStreams.indexOfKey(streamId);
1774 if (idx == NAME_NOT_FOUND) {
1775 CLOGE("Stream %d is unknown", streamId);
1776 return idx;
1777 }
1778 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
1779 status_t res = stream->setConsumer(consumer);
1780 if (res != OK) {
1781 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1782 return res;
1783 }
1784
1785 if (!stream->isConfiguring()) {
1786 CLOGE("Stream %d was already fully configured.", streamId);
1787 return INVALID_OPERATION;
1788 }
1789
1790 res = stream->finishConfiguration(mHal3Device);
1791 if (res != OK) {
1792 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1793 stream->getId(), strerror(-res), res);
1794 return res;
1795 }
1796
1797 return OK;
1798}
1799
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001800/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001801 * Camera3Device private methods
1802 */
1803
1804sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1805 const CameraMetadata &request) {
1806 ATRACE_CALL();
1807 status_t res;
1808
1809 sp<CaptureRequest> newRequest = new CaptureRequest;
1810 newRequest->mSettings = request;
1811
1812 camera_metadata_entry_t inputStreams =
1813 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1814 if (inputStreams.count > 0) {
1815 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001816 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001817 CLOGE("Request references unknown input stream %d",
1818 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001819 return NULL;
1820 }
1821 // Lazy completion of stream configuration (allocation/registration)
1822 // on first use
1823 if (mInputStream->isConfiguring()) {
1824 res = mInputStream->finishConfiguration(mHal3Device);
1825 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001826 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001827 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001828 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001829 return NULL;
1830 }
1831 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001832 // Check if stream is being prepared
1833 if (mInputStream->isPreparing()) {
1834 CLOGE("Request references an input stream that's being prepared!");
1835 return NULL;
1836 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001837
1838 newRequest->mInputStream = mInputStream;
1839 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1840 }
1841
1842 camera_metadata_entry_t streams =
1843 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1844 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001845 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001846 return NULL;
1847 }
1848
1849 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001850 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001851 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001852 CLOGE("Request references unknown stream %d",
1853 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001854 return NULL;
1855 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001856 sp<Camera3OutputStreamInterface> stream =
1857 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001858
Zhijun He5d677d12016-05-29 16:52:39 -07001859 // It is illegal to include a deferred consumer output stream into a request
1860 if (stream->isConsumerConfigurationDeferred()) {
1861 CLOGE("Stream %d hasn't finished configuration yet due to deferred consumer",
1862 stream->getId());
1863 return NULL;
1864 }
1865
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001866 // Lazy completion of stream configuration (allocation/registration)
1867 // on first use
1868 if (stream->isConfiguring()) {
1869 res = stream->finishConfiguration(mHal3Device);
1870 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001871 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1872 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001873 return NULL;
1874 }
1875 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001876 // Check if stream is being prepared
1877 if (stream->isPreparing()) {
1878 CLOGE("Request references an output stream that's being prepared!");
1879 return NULL;
1880 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001881
1882 newRequest->mOutputStreams.push(stream);
1883 }
1884 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001885 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001886
1887 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001888}
1889
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001890bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1891 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1892 Size size = mSupportedOpaqueInputSizes[i];
1893 if (size.width == width && size.height == height) {
1894 return true;
1895 }
1896 }
1897
1898 return false;
1899}
1900
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001901void Camera3Device::cancelStreamsConfigurationLocked() {
1902 int res = OK;
1903 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1904 res = mInputStream->cancelConfiguration();
1905 if (res != OK) {
1906 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
1907 mInputStream->getId(), strerror(-res), res);
1908 }
1909 }
1910
1911 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1912 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
1913 if (outputStream->isConfiguring()) {
1914 res = outputStream->cancelConfiguration();
1915 if (res != OK) {
1916 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
1917 outputStream->getId(), strerror(-res), res);
1918 }
1919 }
1920 }
1921
1922 // Return state to that at start of call, so that future configures
1923 // properly clean things up
1924 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
1925 mNeedConfig = true;
1926}
1927
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001928status_t Camera3Device::configureStreamsLocked() {
1929 ATRACE_CALL();
1930 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001931
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001932 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001933 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001934 return INVALID_OPERATION;
1935 }
1936
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001937 if (!mNeedConfig) {
1938 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1939 return OK;
1940 }
1941
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001942 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1943 // adding a dummy stream instead.
1944 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1945 if (mOutputStreams.size() == 0) {
1946 addDummyStreamLocked();
1947 } else {
1948 tryRemoveDummyStreamLocked();
1949 }
1950
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001951 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001952 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001953
1954 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001955 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1956 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1957 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001958 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1959
1960 Vector<camera3_stream_t*> streams;
1961 streams.setCapacity(config.num_streams);
1962
1963 if (mInputStream != NULL) {
1964 camera3_stream_t *inputStream;
1965 inputStream = mInputStream->startConfiguration();
1966 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001967 CLOGE("Can't start input stream configuration");
1968 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001969 return INVALID_OPERATION;
1970 }
1971 streams.add(inputStream);
1972 }
1973
1974 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001975
1976 // Don't configure bidi streams twice, nor add them twice to the list
1977 if (mOutputStreams[i].get() ==
1978 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1979
1980 config.num_streams--;
1981 continue;
1982 }
1983
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001984 camera3_stream_t *outputStream;
1985 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1986 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001987 CLOGE("Can't start output stream configuration");
1988 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001989 return INVALID_OPERATION;
1990 }
1991 streams.add(outputStream);
1992 }
1993
1994 config.streams = streams.editArray();
1995
1996 // Do the HAL configuration; will potentially touch stream
1997 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001998 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001999 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002000 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002001
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002002 if (res == BAD_VALUE) {
2003 // HAL rejected this set of streams as unsupported, clean up config
2004 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002005 CLOGE("Set of requested inputs/outputs not supported by HAL");
2006 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002007 return BAD_VALUE;
2008 } else if (res != OK) {
2009 // Some other kind of error from configure_streams - this is not
2010 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002011 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2012 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002013 return res;
2014 }
2015
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002016 // Finish all stream configuration immediately.
2017 // TODO: Try to relax this later back to lazy completion, which should be
2018 // faster
2019
Igor Murashkin073f8572013-05-02 14:59:28 -07002020 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002021 res = mInputStream->finishConfiguration(mHal3Device);
2022 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002023 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002024 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002025 cancelStreamsConfigurationLocked();
2026 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002027 }
2028 }
2029
2030 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002031 sp<Camera3OutputStreamInterface> outputStream =
2032 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002033 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002034 res = outputStream->finishConfiguration(mHal3Device);
2035 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002036 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002037 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002038 cancelStreamsConfigurationLocked();
2039 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002040 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002041 }
2042 }
2043
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002044 // Request thread needs to know to avoid using repeat-last-settings protocol
2045 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002046 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002047
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002048 // Boost priority of request thread for high speed recording to SCHED_FIFO
2049 if (mIsConstrainedHighSpeedConfiguration) {
2050 pid_t requestThreadTid = mRequestThread->getTid();
2051 res = requestPriority(getpid(), requestThreadTid,
Eino-Ville Talvala0f6778e2016-04-25 16:57:04 -07002052 kConstrainedHighSpeedThreadPriority, /*asynchronous*/ false);
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002053 if (res != OK) {
2054 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2055 strerror(-res), res);
2056 } else {
2057 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2058 }
2059 } else {
2060 // TODO: Set/restore normal priority for normal use cases
2061 }
2062
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002063 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002064
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002065 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002066
Ruben Brunk183f0562015-08-12 12:55:02 -07002067 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2068 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002069
2070 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
2071
Zhijun He0a210512014-07-24 13:45:15 -07002072 // tear down the deleted streams after configure streams.
2073 mDeletedStreams.clear();
2074
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002075 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002076}
2077
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002078status_t Camera3Device::addDummyStreamLocked() {
2079 ATRACE_CALL();
2080 status_t res;
2081
2082 if (mDummyStreamId != NO_STREAM) {
2083 // Should never be adding a second dummy stream when one is already
2084 // active
2085 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
2086 __FUNCTION__, mId);
2087 return INVALID_OPERATION;
2088 }
2089
2090 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
2091
2092 sp<Camera3OutputStreamInterface> dummyStream =
2093 new Camera3DummyStream(mNextStreamId);
2094
2095 res = mOutputStreams.add(mNextStreamId, dummyStream);
2096 if (res < 0) {
2097 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2098 return res;
2099 }
2100
2101 mDummyStreamId = mNextStreamId;
2102 mNextStreamId++;
2103
2104 return OK;
2105}
2106
2107status_t Camera3Device::tryRemoveDummyStreamLocked() {
2108 ATRACE_CALL();
2109 status_t res;
2110
2111 if (mDummyStreamId == NO_STREAM) return OK;
2112 if (mOutputStreams.size() == 1) return OK;
2113
2114 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
2115
2116 // Ok, have a dummy stream and there's at least one other output stream,
2117 // so remove the dummy
2118
2119 sp<Camera3StreamInterface> deletedStream;
2120 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2121 if (outputStreamIdx == NAME_NOT_FOUND) {
2122 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2123 return INVALID_OPERATION;
2124 }
2125
2126 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2127 mOutputStreams.removeItemsAt(outputStreamIdx);
2128
2129 // Free up the stream endpoint so that it can be used by some other stream
2130 res = deletedStream->disconnect();
2131 if (res != OK) {
2132 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2133 // fall through since we want to still list the stream as deleted.
2134 }
2135 mDeletedStreams.add(deletedStream);
2136 mDummyStreamId = NO_STREAM;
2137
2138 return res;
2139}
2140
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002141void Camera3Device::setErrorState(const char *fmt, ...) {
2142 Mutex::Autolock l(mLock);
2143 va_list args;
2144 va_start(args, fmt);
2145
2146 setErrorStateLockedV(fmt, args);
2147
2148 va_end(args);
2149}
2150
2151void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2152 Mutex::Autolock l(mLock);
2153 setErrorStateLockedV(fmt, args);
2154}
2155
2156void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2157 va_list args;
2158 va_start(args, fmt);
2159
2160 setErrorStateLockedV(fmt, args);
2161
2162 va_end(args);
2163}
2164
2165void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002166 // Print out all error messages to log
2167 String8 errorCause = String8::formatV(fmt, args);
2168 ALOGE("Camera %d: %s", mId, errorCause.string());
2169
2170 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002171 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002172
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002173 mErrorCause = errorCause;
2174
2175 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002176 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002177
2178 // Notify upstream about a device error
2179 if (mListener != NULL) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002180 mListener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002181 CaptureResultExtras());
2182 }
2183
2184 // Save stack trace. View by dumping it later.
2185 CameraTraces::saveTrace();
2186 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002187}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002188
2189/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002190 * In-flight request management
2191 */
2192
Jianing Weicb0652e2014-03-12 18:29:36 -07002193status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002194 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2195 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002196 ATRACE_CALL();
2197 Mutex::Autolock l(mInFlightLock);
2198
2199 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002200 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2201 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002202 if (res < 0) return res;
2203
Eino-Ville Talvalae4df8ab2016-07-21 12:53:07 -07002204 if (mInFlightMap.size() == 1) {
2205 mStatusTracker->markComponentActive(mInFlightStatusId);
2206 }
2207
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002208 return OK;
2209}
2210
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002211void Camera3Device::returnOutputBuffers(
2212 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2213 nsecs_t timestamp) {
2214 for (size_t i = 0; i < numBuffers; i++)
2215 {
2216 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2217 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2218 // Note: stream may be deallocated at this point, if this buffer was
2219 // the last reference to it.
2220 if (res != OK) {
2221 ALOGE("Can't return buffer to its stream: %s (%d)",
2222 strerror(-res), res);
2223 }
2224 }
2225}
2226
2227
2228void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2229
2230 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2231 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2232
2233 nsecs_t sensorTimestamp = request.sensorTimestamp;
2234 nsecs_t shutterTimestamp = request.shutterTimestamp;
2235
2236 // Check if it's okay to remove the request from InFlightMap:
2237 // In the case of a successful request:
2238 // all input and output buffers, all result metadata, shutter callback
2239 // arrived.
2240 // In the case of a unsuccessful request:
2241 // all input and output buffers arrived.
2242 if (request.numBuffersLeft == 0 &&
2243 (request.requestStatus != OK ||
2244 (request.haveResultMetadata && shutterTimestamp != 0))) {
2245 ATRACE_ASYNC_END("frame capture", frameNumber);
2246
2247 // Sanity check - if sensor timestamp matches shutter timestamp
2248 if (request.requestStatus == OK &&
2249 sensorTimestamp != shutterTimestamp) {
2250 SET_ERR("sensor timestamp (%" PRId64
2251 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2252 sensorTimestamp, frameNumber, shutterTimestamp);
2253 }
2254
2255 // for an unsuccessful request, it may have pending output buffers to
2256 // return.
2257 assert(request.requestStatus != OK ||
2258 request.pendingOutputBuffers.size() == 0);
2259 returnOutputBuffers(request.pendingOutputBuffers.array(),
2260 request.pendingOutputBuffers.size(), 0);
2261
2262 mInFlightMap.removeItemsAt(idx, 1);
2263
Eino-Ville Talvalae4df8ab2016-07-21 12:53:07 -07002264 // Indicate idle inFlightMap to the status tracker
2265 if (mInFlightMap.size() == 0) {
2266 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2267 }
2268
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002269 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2270 }
2271
2272 // Sanity check - if we have too many in-flight frames, something has
2273 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002274 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002275 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002276 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2277 kInFlightWarnLimitHighSpeed) {
2278 CLOGE("In-flight list too large for high speed configuration: %zu",
2279 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002280 }
2281}
2282
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002283void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2284 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2285 if (result == nullptr) return;
2286
2287 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2288 (int32_t*)&frameNumber, 1) != OK) {
2289 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2290 return;
2291 }
2292
2293 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2294 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2295 return;
2296 }
2297
2298 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2299
2300 // Valid result, insert into queue
2301 List<CaptureResult>::iterator queuedResult =
2302 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2303 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2304 ", burstId = %" PRId32, __FUNCTION__,
2305 queuedResult->mResultExtras.requestId,
2306 queuedResult->mResultExtras.frameNumber,
2307 queuedResult->mResultExtras.burstId);
2308
2309 mResultSignal.signal();
2310}
2311
2312
2313void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2314 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2315 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2316 Mutex::Autolock l(mOutputLock);
2317
2318 CaptureResult captureResult;
2319 captureResult.mResultExtras = resultExtras;
2320 captureResult.mMetadata = partialResult;
2321
2322 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2323}
2324
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002325
2326void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2327 CaptureResultExtras &resultExtras,
2328 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002329 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002330 bool reprocess,
2331 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002332 if (pendingMetadata.isEmpty())
2333 return;
2334
2335 Mutex::Autolock l(mOutputLock);
2336
2337 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002338 if (reprocess) {
2339 if (frameNumber < mNextReprocessResultFrameNumber) {
2340 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002341 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002342 frameNumber, mNextReprocessResultFrameNumber);
2343 return;
2344 }
2345 mNextReprocessResultFrameNumber = frameNumber + 1;
2346 } else {
2347 if (frameNumber < mNextResultFrameNumber) {
2348 SET_ERR("Out-of-order capture result metadata submitted! "
2349 "(got frame number %d, expecting %d)",
2350 frameNumber, mNextResultFrameNumber);
2351 return;
2352 }
2353 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002354 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002355
2356 CaptureResult captureResult;
2357 captureResult.mResultExtras = resultExtras;
2358 captureResult.mMetadata = pendingMetadata;
2359
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002360 // Append any previous partials to form a complete result
2361 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2362 captureResult.mMetadata.append(collectedPartialResult);
2363 }
2364
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07002365 // Derive some new keys for backward compaibility
2366 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
2367 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
2368 int32_t defaultBoost[1] = {100};
2369 captureResult.mMetadata.update(
2370 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
2371 defaultBoost, 1);
2372 }
2373
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002374 captureResult.mMetadata.sort();
2375
2376 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002377 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2378 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002379 SET_ERR("No timestamp provided by HAL for frame %d!",
2380 frameNumber);
2381 return;
2382 }
2383
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002384 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2385 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2386
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002387 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002388}
2389
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002390/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002391 * Camera HAL device callback methods
2392 */
2393
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002394void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002395 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002396
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002397 status_t res;
2398
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002399 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002400 if (result->result == NULL && result->num_output_buffers == 0 &&
2401 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002402 SET_ERR("No result data provided by HAL for frame %d",
2403 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002404 return;
2405 }
Zhijun He204e3292014-07-14 17:09:23 -07002406
2407 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2408 // partial_result to 1 when metadata is included in this result.
2409 if (!mUsePartialResult &&
2410 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2411 result->result != NULL &&
2412 result->partial_result != 1) {
2413 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2414 " if partial result is not supported",
2415 frameNumber, result->partial_result);
2416 return;
2417 }
2418
2419 bool isPartialResult = false;
2420 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002421 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002422 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002423
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002424 // Get shutter timestamp and resultExtras from list of in-flight requests,
2425 // where it was added by the shutter notification for this frame. If the
2426 // shutter timestamp isn't received yet, append the output buffers to the
2427 // in-flight request and they will be returned when the shutter timestamp
2428 // arrives. Update the in-flight status and remove the in-flight entry if
2429 // all result data and shutter timestamp have been received.
2430 nsecs_t shutterTimestamp = 0;
2431
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002432 {
2433 Mutex::Autolock l(mInFlightLock);
2434 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2435 if (idx == NAME_NOT_FOUND) {
2436 SET_ERR("Unknown frame number for capture result: %d",
2437 frameNumber);
2438 return;
2439 }
2440 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002441 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2442 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2443 ", partialResultCount = %d",
2444 __FUNCTION__, request.resultExtras.requestId,
2445 request.resultExtras.frameNumber, request.resultExtras.burstId,
2446 result->partial_result);
2447 // Always update the partial count to the latest one if it's not 0
2448 // (buffers only). When framework aggregates adjacent partial results
2449 // into one, the latest partial count will be used.
2450 if (result->partial_result != 0)
2451 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002452
2453 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002454 if (mUsePartialResult && result->result != NULL) {
2455 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2456 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2457 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2458 " the range of [1, %d] when metadata is included in the result",
2459 frameNumber, result->partial_result, mNumPartialResults);
2460 return;
2461 }
2462 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002463 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002464 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002465 }
Zhijun He204e3292014-07-14 17:09:23 -07002466 } else {
2467 camera_metadata_ro_entry_t partialResultEntry;
2468 res = find_camera_metadata_ro_entry(result->result,
2469 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2470 if (res != NAME_NOT_FOUND &&
2471 partialResultEntry.count > 0 &&
2472 partialResultEntry.data.u8[0] ==
2473 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2474 // A partial result. Flag this as such, and collect this
2475 // set of metadata into the in-flight entry.
2476 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002477 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002478 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002479 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002480 ANDROID_QUIRKS_PARTIAL_RESULT);
2481 }
2482 }
2483
2484 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002485 // Send partial capture result
2486 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2487 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002488 }
2489 }
2490
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002491 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002492 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002493
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002494 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002495 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002496 if (request.haveResultMetadata) {
2497 SET_ERR("Called multiple times with metadata for frame %d",
2498 frameNumber);
2499 return;
2500 }
Zhijun He204e3292014-07-14 17:09:23 -07002501 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002502 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002503 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002504 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002505 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002506 request.haveResultMetadata = true;
2507 }
2508
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002509 uint32_t numBuffersReturned = result->num_output_buffers;
2510 if (result->input_buffer != NULL) {
2511 if (hasInputBufferInRequest) {
2512 numBuffersReturned += 1;
2513 } else {
2514 ALOGW("%s: Input buffer should be NULL if there is no input"
2515 " buffer sent in the request",
2516 __FUNCTION__);
2517 }
2518 }
2519 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002520 if (request.numBuffersLeft < 0) {
2521 SET_ERR("Too many buffers returned for frame %d",
2522 frameNumber);
2523 return;
2524 }
2525
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002526 camera_metadata_ro_entry_t entry;
2527 res = find_camera_metadata_ro_entry(result->result,
2528 ANDROID_SENSOR_TIMESTAMP, &entry);
2529 if (res == OK && entry.count == 1) {
2530 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002531 }
2532
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002533 // If shutter event isn't received yet, append the output buffers to
2534 // the in-flight request. Otherwise, return the output buffers to
2535 // streams.
2536 if (shutterTimestamp == 0) {
2537 request.pendingOutputBuffers.appendArray(result->output_buffers,
2538 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002539 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002540 returnOutputBuffers(result->output_buffers,
2541 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002542 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002543
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002544 if (result->result != NULL && !isPartialResult) {
2545 if (shutterTimestamp == 0) {
2546 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002547 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002548 } else {
2549 CameraMetadata metadata;
2550 metadata = result->result;
2551 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002552 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2553 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002554 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002555 }
2556
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002557 removeInFlightRequestIfReadyLocked(idx);
2558 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002559
Zhijun Hef0d962a2014-06-30 10:24:11 -07002560 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002561 if (hasInputBufferInRequest) {
2562 Camera3Stream *stream =
2563 Camera3Stream::cast(result->input_buffer->stream);
2564 res = stream->returnInputBuffer(*(result->input_buffer));
2565 // Note: stream may be deallocated at this point, if this buffer was the
2566 // last reference to it.
2567 if (res != OK) {
2568 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2569 " its stream:%s (%d)", __FUNCTION__,
2570 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002571 }
2572 } else {
2573 ALOGW("%s: Input buffer should be NULL if there is no input"
2574 " buffer sent in the request, skipping input buffer return.",
2575 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002576 }
2577 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002578}
2579
2580void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002581 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002582 NotificationListener *listener;
2583 {
2584 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002585 listener = mListener;
2586 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002587
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002588 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002589 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002590 return;
2591 }
2592
2593 switch (msg->type) {
2594 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002595 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002596 break;
2597 }
2598 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002599 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002600 break;
2601 }
2602 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002603 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002604 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002605 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002606}
2607
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002608void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2609 NotificationListener *listener) {
2610
2611 // Map camera HAL error codes to ICameraDeviceCallback error codes
2612 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002613 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002614 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002615 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002616 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002617 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002618 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002619 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002620 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002621 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002622 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002623 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002624 };
2625
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002626 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002627 ((msg.error_code >= 0) &&
2628 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2629 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002630 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002631
2632 int streamId = 0;
2633 if (msg.error_stream != NULL) {
2634 Camera3Stream *stream =
2635 Camera3Stream::cast(msg.error_stream);
2636 streamId = stream->getId();
2637 }
2638 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2639 mId, __FUNCTION__, msg.frame_number,
2640 streamId, msg.error_code);
2641
2642 CaptureResultExtras resultExtras;
2643 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002644 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002645 // SET_ERR calls notifyError
2646 SET_ERR("Camera HAL reported serious device error");
2647 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002648 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2649 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2650 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002651 {
2652 Mutex::Autolock l(mInFlightLock);
2653 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2654 if (idx >= 0) {
2655 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2656 r.requestStatus = msg.error_code;
2657 resultExtras = r.resultExtras;
2658 } else {
2659 resultExtras.frameNumber = msg.frame_number;
2660 ALOGE("Camera %d: %s: cannot find in-flight request on "
2661 "frame %" PRId64 " error", mId, __FUNCTION__,
2662 resultExtras.frameNumber);
2663 }
2664 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002665 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002666 if (listener != NULL) {
2667 listener->notifyError(errorCode, resultExtras);
2668 } else {
2669 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2670 }
2671 break;
2672 default:
2673 // SET_ERR calls notifyError
2674 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2675 break;
2676 }
2677}
2678
2679void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2680 NotificationListener *listener) {
2681 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002682
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002683 // Set timestamp for the request in the in-flight tracking
2684 // and get the request ID to send upstream
2685 {
2686 Mutex::Autolock l(mInFlightLock);
2687 idx = mInFlightMap.indexOfKey(msg.frame_number);
2688 if (idx >= 0) {
2689 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002690
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002691 // Verify ordering of shutter notifications
2692 {
2693 Mutex::Autolock l(mOutputLock);
2694 // TODO: need to track errors for tighter bounds on expected frame number.
2695 if (r.hasInputBuffer) {
2696 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2697 SET_ERR("Shutter notification out-of-order. Expected "
2698 "notification for frame %d, got frame %d",
2699 mNextReprocessShutterFrameNumber, msg.frame_number);
2700 return;
2701 }
2702 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2703 } else {
2704 if (msg.frame_number < mNextShutterFrameNumber) {
2705 SET_ERR("Shutter notification out-of-order. Expected "
2706 "notification for frame %d, got frame %d",
2707 mNextShutterFrameNumber, msg.frame_number);
2708 return;
2709 }
2710 mNextShutterFrameNumber = msg.frame_number + 1;
2711 }
2712 }
2713
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002714 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2715 mId, __FUNCTION__,
2716 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2717 // Call listener, if any
2718 if (listener != NULL) {
2719 listener->notifyShutter(r.resultExtras, msg.timestamp);
2720 }
2721
2722 r.shutterTimestamp = msg.timestamp;
2723
2724 // send pending result and buffers
2725 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002726 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002727 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002728 returnOutputBuffers(r.pendingOutputBuffers.array(),
2729 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2730 r.pendingOutputBuffers.clear();
2731
2732 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002733 }
2734 }
2735 if (idx < 0) {
2736 SET_ERR("Shutter notification for non-existent frame number %d",
2737 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002738 }
2739}
2740
2741
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002742CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002743 ALOGV("%s", __FUNCTION__);
2744
Igor Murashkin1e479c02013-09-06 16:55:14 -07002745 CameraMetadata retVal;
2746
2747 if (mRequestThread != NULL) {
2748 retVal = mRequestThread->getLatestRequest();
2749 }
2750
Igor Murashkin1e479c02013-09-06 16:55:14 -07002751 return retVal;
2752}
2753
Jianing Weicb0652e2014-03-12 18:29:36 -07002754
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002755void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
2756 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
2757 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
2758}
2759
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002760/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002761 * RequestThread inner class methods
2762 */
2763
2764Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002765 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002766 camera3_device_t *hal3Device,
2767 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002768 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002769 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002770 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002771 mHal3Device(hal3Device),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002772 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002773 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002774 mReconfigured(false),
2775 mDoPause(false),
2776 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002777 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002778 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002779 mCurrentAfTriggerId(0),
2780 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002781 mRepeatingLastFrameNumber(
2782 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002783 mAeLockAvailable(aeLockAvailable),
2784 mPrepareVideoStream(false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002785 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002786}
2787
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002788void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002789 NotificationListener *listener) {
2790 Mutex::Autolock l(mRequestLock);
2791 mListener = listener;
2792}
2793
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002794void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002795 Mutex::Autolock l(mRequestLock);
2796 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002797 // Prepare video stream for high speed recording.
2798 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002799}
2800
Jianing Wei90e59c92014-03-12 18:29:36 -07002801status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002802 List<sp<CaptureRequest> > &requests,
2803 /*out*/
2804 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002805 Mutex::Autolock l(mRequestLock);
2806 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2807 ++it) {
2808 mRequestQueue.push_back(*it);
2809 }
2810
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002811 if (lastFrameNumber != NULL) {
2812 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2813 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2814 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2815 *lastFrameNumber);
2816 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002817
Jianing Wei90e59c92014-03-12 18:29:36 -07002818 unpauseForNewRequests();
2819
2820 return OK;
2821}
2822
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002823
2824status_t Camera3Device::RequestThread::queueTrigger(
2825 RequestTrigger trigger[],
2826 size_t count) {
2827
2828 Mutex::Autolock l(mTriggerMutex);
2829 status_t ret;
2830
2831 for (size_t i = 0; i < count; ++i) {
2832 ret = queueTriggerLocked(trigger[i]);
2833
2834 if (ret != OK) {
2835 return ret;
2836 }
2837 }
2838
2839 return OK;
2840}
2841
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002842int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2843 sp<Camera3Device> d = device.promote();
2844 if (d != NULL) return d->mId;
2845 return 0;
2846}
2847
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002848status_t Camera3Device::RequestThread::queueTriggerLocked(
2849 RequestTrigger trigger) {
2850
2851 uint32_t tag = trigger.metadataTag;
2852 ssize_t index = mTriggerMap.indexOfKey(tag);
2853
2854 switch (trigger.getTagType()) {
2855 case TYPE_BYTE:
2856 // fall-through
2857 case TYPE_INT32:
2858 break;
2859 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002860 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2861 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002862 return INVALID_OPERATION;
2863 }
2864
2865 /**
2866 * Collect only the latest trigger, since we only have 1 field
2867 * in the request settings per trigger tag, and can't send more than 1
2868 * trigger per request.
2869 */
2870 if (index != NAME_NOT_FOUND) {
2871 mTriggerMap.editValueAt(index) = trigger;
2872 } else {
2873 mTriggerMap.add(tag, trigger);
2874 }
2875
2876 return OK;
2877}
2878
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002879status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002880 const RequestList &requests,
2881 /*out*/
2882 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002883 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002884 if (lastFrameNumber != NULL) {
2885 *lastFrameNumber = mRepeatingLastFrameNumber;
2886 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002887 mRepeatingRequests.clear();
2888 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2889 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002890
2891 unpauseForNewRequests();
2892
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002893 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002894 return OK;
2895}
2896
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002897bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2898 if (mRepeatingRequests.empty()) {
2899 return false;
2900 }
2901 int32_t requestId = requestIn->mResultExtras.requestId;
2902 const RequestList &repeatRequests = mRepeatingRequests;
2903 // All repeating requests are guaranteed to have same id so only check first quest
2904 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2905 return (firstRequest->mResultExtras.requestId == requestId);
2906}
2907
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002908status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002909 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002910 return clearRepeatingRequestsLocked(lastFrameNumber);
2911
2912}
2913
2914status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002915 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002916 if (lastFrameNumber != NULL) {
2917 *lastFrameNumber = mRepeatingLastFrameNumber;
2918 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002919 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002920 return OK;
2921}
2922
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002923status_t Camera3Device::RequestThread::clear(
2924 NotificationListener *listener,
2925 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002926 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002927 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002928
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002929 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002930
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002931 // Send errors for all requests pending in the request queue, including
2932 // pending repeating requests
2933 if (listener != NULL) {
2934 for (RequestList::iterator it = mRequestQueue.begin();
2935 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002936 // Abort the input buffers for reprocess requests.
2937 if ((*it)->mInputStream != NULL) {
2938 camera3_stream_buffer_t inputBuffer;
2939 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2940 if (res != OK) {
2941 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2942 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2943 } else {
2944 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2945 if (res != OK) {
2946 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2947 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2948 }
2949 }
2950 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002951 // Set the frame number this request would have had, if it
2952 // had been submitted; this frame number will not be reused.
2953 // The requestId and burstId fields were set when the request was
2954 // submitted originally (in convertMetadataListToRequestListLocked)
2955 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002956 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002957 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002958 }
2959 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002960 mRequestQueue.clear();
2961 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002962 if (lastFrameNumber != NULL) {
2963 *lastFrameNumber = mRepeatingLastFrameNumber;
2964 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002965 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002966 return OK;
2967}
2968
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002969status_t Camera3Device::RequestThread::flush() {
2970 ATRACE_CALL();
2971 Mutex::Autolock l(mFlushLock);
2972
2973 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2974 return mHal3Device->ops->flush(mHal3Device);
2975 }
2976
2977 return -ENOTSUP;
2978}
2979
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002980void Camera3Device::RequestThread::setPaused(bool paused) {
2981 Mutex::Autolock l(mPauseLock);
2982 mDoPause = paused;
2983 mDoPauseSignal.signal();
2984}
2985
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002986status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2987 int32_t requestId, nsecs_t timeout) {
2988 Mutex::Autolock l(mLatestRequestMutex);
2989 status_t res;
2990 while (mLatestRequestId != requestId) {
2991 nsecs_t startTime = systemTime();
2992
2993 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2994 if (res != OK) return res;
2995
2996 timeout -= (systemTime() - startTime);
2997 }
2998
2999 return OK;
3000}
3001
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003002void Camera3Device::RequestThread::requestExit() {
3003 // Call parent to set up shutdown
3004 Thread::requestExit();
3005 // The exit from any possible waits
3006 mDoPauseSignal.signal();
3007 mRequestSignal.signal();
3008}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003009
Chien-Yu Chend196d612015-06-22 19:49:01 -07003010
3011/**
3012 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
3013 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
3014 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
3015 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
3016 * request.
3017 */
3018void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
3019 request->mAeTriggerCancelOverride.applyAeLock = false;
3020 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
3021
3022 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
3023 return;
3024 }
3025
3026 camera_metadata_entry_t aePrecaptureTrigger =
3027 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3028 if (aePrecaptureTrigger.count > 0 &&
3029 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
3030 // Always override CANCEL to IDLE
3031 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
3032 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
3033 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
3034 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
3035 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3036
3037 if (mAeLockAvailable == true) {
3038 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3039 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3040 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3041 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3042 request->mAeTriggerCancelOverride.applyAeLock = true;
3043 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3044 }
3045 }
3046 }
3047}
3048
3049/**
3050 * Override result metadata for cancelling AE precapture trigger applied in
3051 * handleAePrecaptureCancelRequest().
3052 */
3053void Camera3Device::overrideResultForPrecaptureCancel(
3054 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3055 if (aeTriggerCancelOverride.applyAeLock) {
3056 // Only devices <= v3.2 should have this override
3057 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3058 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3059 }
3060
3061 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3062 // Only devices <= v3.2 should have this override
3063 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3064 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3065 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3066 }
3067}
3068
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003069void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003070 bool surfaceAbandoned = false;
3071 int64_t lastFrameNumber = 0;
3072 {
3073 Mutex::Autolock l(mRequestLock);
3074 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3075 // repeating requests.
3076 for (const auto& request : mRepeatingRequests) {
3077 for (const auto& s : request->mOutputStreams) {
3078 if (s->isAbandoned()) {
3079 surfaceAbandoned = true;
3080 clearRepeatingRequestsLocked(&lastFrameNumber);
3081 break;
3082 }
3083 }
3084 if (surfaceAbandoned) {
3085 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003086 }
3087 }
3088 }
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003089 if (surfaceAbandoned) {
3090 mListener->notifyRepeatingRequestError(lastFrameNumber);
3091 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003092}
3093
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003094bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003095 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003096 status_t res;
3097
3098 // Handle paused state.
3099 if (waitIfPaused()) {
3100 return true;
3101 }
3102
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003103 // Wait for the next batch of requests.
3104 waitForNextRequestBatch();
3105 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003106 return true;
3107 }
3108
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003109 // Get the latest request ID, if any
3110 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003111 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003112 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003113 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003114 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003115 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003116 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3117 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003118 }
3119
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003120 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003121 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003122 if (res == TIMED_OUT) {
3123 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003124 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003125 // Check if any stream is abandoned.
3126 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003127 return true;
3128 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003129 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003130 return false;
3131 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003132
Zhijun Hecc27e112013-10-03 16:12:43 -07003133 // Inform waitUntilRequestProcessed thread of a new request ID
3134 {
3135 Mutex::Autolock al(mLatestRequestMutex);
3136
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003137 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003138 mLatestRequestSignal.signal();
3139 }
3140
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003141 // Submit a batch of requests to HAL.
3142 // Use flush lock only when submitting multilple requests in a batch.
3143 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3144 // which may take a long time to finish so synchronizing flush() and
3145 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3146 // For now, only synchronize for high speed recording and we should figure something out for
3147 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003148 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003149
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003150 if (useFlushLock) {
3151 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003152 }
3153
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003154 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003155 mNextRequests.size());
3156 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003157 // Submit request and block until ready for next one
3158 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3159 ATRACE_BEGIN("camera3->process_capture_request");
3160 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3161 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003162
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003163 if (res != OK) {
3164 // Should only get a failure here for malformed requests or device-level
3165 // errors, so consider all errors fatal. Bad metadata failures should
3166 // come through notify.
3167 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3168 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3169 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003170 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003171 if (useFlushLock) {
3172 mFlushLock.unlock();
3173 }
3174 return false;
3175 }
3176
3177 // Mark that the request has be submitted successfully.
3178 nextRequest.submitted = true;
3179
3180 // Update the latest request sent to HAL
3181 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3182 Mutex::Autolock al(mLatestRequestMutex);
3183
3184 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3185 mLatestRequest.acquire(cloned);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003186
3187 sp<Camera3Device> parent = mParent.promote();
3188 if (parent != NULL) {
3189 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
3190 0, mLatestRequest);
3191 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003192 }
3193
3194 if (nextRequest.halRequest.settings != NULL) {
3195 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3196 }
3197
3198 // Remove any previously queued triggers (after unlock)
3199 res = removeTriggers(mPrevRequest);
3200 if (res != OK) {
3201 SET_ERR("RequestThread: Unable to remove triggers "
3202 "(capture request %d, HAL device: %s (%d)",
3203 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003204 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003205 if (useFlushLock) {
3206 mFlushLock.unlock();
3207 }
3208 return false;
3209 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003210 }
3211
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003212 if (useFlushLock) {
3213 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003214 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003215
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003216 // Unset as current request
3217 {
3218 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003219 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003220 }
3221
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003222 return true;
3223}
3224
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003225status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003226 ATRACE_CALL();
3227
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003228 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003229 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3230 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3231 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3232
3233 // Prepare a request to HAL
3234 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3235
3236 // Insert any queued triggers (before metadata is locked)
3237 status_t res = insertTriggers(captureRequest);
3238
3239 if (res < 0) {
3240 SET_ERR("RequestThread: Unable to insert triggers "
3241 "(capture request %d, HAL device: %s (%d)",
3242 halRequest->frame_number, strerror(-res), res);
3243 return INVALID_OPERATION;
3244 }
3245 int triggerCount = res;
3246 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3247 mPrevTriggers = triggerCount;
3248
3249 // If the request is the same as last, or we had triggers last time
3250 if (mPrevRequest != captureRequest || triggersMixedIn) {
3251 /**
3252 * HAL workaround:
3253 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3254 */
3255 res = addDummyTriggerIds(captureRequest);
3256 if (res != OK) {
3257 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3258 "(capture request %d, HAL device: %s (%d)",
3259 halRequest->frame_number, strerror(-res), res);
3260 return INVALID_OPERATION;
3261 }
3262
3263 /**
3264 * The request should be presorted so accesses in HAL
3265 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3266 */
3267 captureRequest->mSettings.sort();
3268 halRequest->settings = captureRequest->mSettings.getAndLock();
3269 mPrevRequest = captureRequest;
3270 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3271
3272 IF_ALOGV() {
3273 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3274 find_camera_metadata_ro_entry(
3275 halRequest->settings,
3276 ANDROID_CONTROL_AF_TRIGGER,
3277 &e
3278 );
3279 if (e.count > 0) {
3280 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3281 __FUNCTION__,
3282 halRequest->frame_number,
3283 e.data.u8[0]);
3284 }
3285 }
3286 } else {
3287 // leave request.settings NULL to indicate 'reuse latest given'
3288 ALOGVV("%s: Request settings are REUSED",
3289 __FUNCTION__);
3290 }
3291
3292 uint32_t totalNumBuffers = 0;
3293
3294 // Fill in buffers
3295 if (captureRequest->mInputStream != NULL) {
3296 halRequest->input_buffer = &captureRequest->mInputBuffer;
3297 totalNumBuffers += 1;
3298 } else {
3299 halRequest->input_buffer = NULL;
3300 }
3301
3302 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3303 captureRequest->mOutputStreams.size());
3304 halRequest->output_buffers = outputBuffers->array();
3305 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003306 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(i);
3307
3308 // Prepare video buffers for high speed recording on the first video request.
3309 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3310 // Only try to prepare video stream on the first video request.
3311 mPrepareVideoStream = false;
3312
3313 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
3314 while (res == NOT_ENOUGH_DATA) {
3315 res = outputStream->prepareNextBuffer();
3316 }
3317 if (res != OK) {
3318 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3319 __FUNCTION__, strerror(-res), res);
3320 outputStream->cancelPrepare();
3321 }
3322 }
3323
3324 res = outputStream->getBuffer(&outputBuffers->editItemAt(i));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003325 if (res != OK) {
3326 // Can't get output buffer from gralloc queue - this could be due to
3327 // abandoned queue or other consumer misbehavior, so not a fatal
3328 // error
3329 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3330 " %s (%d)", strerror(-res), res);
3331
3332 return TIMED_OUT;
3333 }
3334 halRequest->num_output_buffers++;
3335 }
3336 totalNumBuffers += halRequest->num_output_buffers;
3337
3338 // Log request in the in-flight queue
3339 sp<Camera3Device> parent = mParent.promote();
3340 if (parent == NULL) {
3341 // Should not happen, and nowhere to send errors to, so just log it
3342 CLOGE("RequestThread: Parent is gone");
3343 return INVALID_OPERATION;
3344 }
3345 res = parent->registerInFlight(halRequest->frame_number,
3346 totalNumBuffers, captureRequest->mResultExtras,
3347 /*hasInput*/halRequest->input_buffer != NULL,
3348 captureRequest->mAeTriggerCancelOverride);
3349 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3350 ", burstId = %" PRId32 ".",
3351 __FUNCTION__,
3352 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3353 captureRequest->mResultExtras.burstId);
3354 if (res != OK) {
3355 SET_ERR("RequestThread: Unable to register new in-flight request:"
3356 " %s (%d)", strerror(-res), res);
3357 return INVALID_OPERATION;
3358 }
3359 }
3360
3361 return OK;
3362}
3363
Igor Murashkin1e479c02013-09-06 16:55:14 -07003364CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3365 Mutex::Autolock al(mLatestRequestMutex);
3366
3367 ALOGV("RequestThread::%s", __FUNCTION__);
3368
3369 return mLatestRequest;
3370}
3371
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003372bool Camera3Device::RequestThread::isStreamPending(
3373 sp<Camera3StreamInterface>& stream) {
3374 Mutex::Autolock l(mRequestLock);
3375
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003376 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003377 if (!nextRequest.submitted) {
3378 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3379 if (stream == s) return true;
3380 }
3381 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003382 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003383 }
3384
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003385 for (const auto& request : mRequestQueue) {
3386 for (const auto& s : request->mOutputStreams) {
3387 if (stream == s) return true;
3388 }
3389 if (stream == request->mInputStream) return true;
3390 }
3391
3392 for (const auto& request : mRepeatingRequests) {
3393 for (const auto& s : request->mOutputStreams) {
3394 if (stream == s) return true;
3395 }
3396 if (stream == request->mInputStream) return true;
3397 }
3398
3399 return false;
3400}
Jianing Weicb0652e2014-03-12 18:29:36 -07003401
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003402void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3403 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003404 return;
3405 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003406
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003407 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003408 // Skip the ones that have been submitted successfully.
3409 if (nextRequest.submitted) {
3410 continue;
3411 }
3412
3413 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3414 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3415 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3416
3417 if (halRequest->settings != NULL) {
3418 captureRequest->mSettings.unlock(halRequest->settings);
3419 }
3420
3421 if (captureRequest->mInputStream != NULL) {
3422 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3423 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3424 }
3425
3426 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3427 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3428 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3429 }
3430
3431 if (sendRequestError) {
3432 Mutex::Autolock l(mRequestLock);
3433 if (mListener != NULL) {
3434 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003435 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003436 captureRequest->mResultExtras);
3437 }
3438 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003439 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003440
3441 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003442 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003443}
3444
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003445void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003446 // Optimized a bit for the simple steady-state case (single repeating
3447 // request), to avoid putting that request in the queue temporarily.
3448 Mutex::Autolock l(mRequestLock);
3449
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003450 assert(mNextRequests.empty());
3451
3452 NextRequest nextRequest;
3453 nextRequest.captureRequest = waitForNextRequestLocked();
3454 if (nextRequest.captureRequest == nullptr) {
3455 return;
3456 }
3457
3458 nextRequest.halRequest = camera3_capture_request_t();
3459 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003460 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003461
3462 // Wait for additional requests
3463 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3464
3465 for (size_t i = 1; i < batchSize; i++) {
3466 NextRequest additionalRequest;
3467 additionalRequest.captureRequest = waitForNextRequestLocked();
3468 if (additionalRequest.captureRequest == nullptr) {
3469 break;
3470 }
3471
3472 additionalRequest.halRequest = camera3_capture_request_t();
3473 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003474 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003475 }
3476
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003477 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003478 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003479 mNextRequests.size(), batchSize);
3480 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003481 }
3482
3483 return;
3484}
3485
3486sp<Camera3Device::CaptureRequest>
3487 Camera3Device::RequestThread::waitForNextRequestLocked() {
3488 status_t res;
3489 sp<CaptureRequest> nextRequest;
3490
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003491 while (mRequestQueue.empty()) {
3492 if (!mRepeatingRequests.empty()) {
3493 // Always atomically enqueue all requests in a repeating request
3494 // list. Guarantees a complete in-sequence set of captures to
3495 // application.
3496 const RequestList &requests = mRepeatingRequests;
3497 RequestList::const_iterator firstRequest =
3498 requests.begin();
3499 nextRequest = *firstRequest;
3500 mRequestQueue.insert(mRequestQueue.end(),
3501 ++firstRequest,
3502 requests.end());
3503 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003504
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003505 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003506
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003507 break;
3508 }
3509
3510 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3511
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003512 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3513 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003514 Mutex::Autolock pl(mPauseLock);
3515 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003516 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003517 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003518 // Let the tracker know
3519 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3520 if (statusTracker != 0) {
3521 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3522 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003523 }
3524 // Stop waiting for now and let thread management happen
3525 return NULL;
3526 }
3527 }
3528
3529 if (nextRequest == NULL) {
3530 // Don't have a repeating request already in hand, so queue
3531 // must have an entry now.
3532 RequestList::iterator firstRequest =
3533 mRequestQueue.begin();
3534 nextRequest = *firstRequest;
3535 mRequestQueue.erase(firstRequest);
3536 }
3537
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003538 // In case we've been unpaused by setPaused clearing mDoPause, need to
3539 // update internal pause state (capture/setRepeatingRequest unpause
3540 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003541 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003542 if (mPaused) {
3543 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3544 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3545 if (statusTracker != 0) {
3546 statusTracker->markComponentActive(mStatusId);
3547 }
3548 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003549 mPaused = false;
3550
3551 // Check if we've reconfigured since last time, and reset the preview
3552 // request if so. Can't use 'NULL request == repeat' across configure calls.
3553 if (mReconfigured) {
3554 mPrevRequest.clear();
3555 mReconfigured = false;
3556 }
3557
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003558 if (nextRequest != NULL) {
3559 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003560 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3561 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003562
3563 // Since RequestThread::clear() removes buffers from the input stream,
3564 // get the right buffer here before unlocking mRequestLock
3565 if (nextRequest->mInputStream != NULL) {
3566 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3567 if (res != OK) {
3568 // Can't get input buffer from gralloc queue - this could be due to
3569 // disconnected queue or other producer misbehavior, so not a fatal
3570 // error
3571 ALOGE("%s: Can't get input buffer, skipping request:"
3572 " %s (%d)", __FUNCTION__, strerror(-res), res);
3573 if (mListener != NULL) {
3574 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003575 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003576 nextRequest->mResultExtras);
3577 }
3578 return NULL;
3579 }
3580 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003581 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003582
3583 handleAePrecaptureCancelRequest(nextRequest);
3584
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003585 return nextRequest;
3586}
3587
3588bool Camera3Device::RequestThread::waitIfPaused() {
3589 status_t res;
3590 Mutex::Autolock l(mPauseLock);
3591 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003592 if (mPaused == false) {
3593 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003594 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3595 // Let the tracker know
3596 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3597 if (statusTracker != 0) {
3598 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3599 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003600 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003601
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003602 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003603 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003604 return true;
3605 }
3606 }
3607 // We don't set mPaused to false here, because waitForNextRequest needs
3608 // to further manage the paused state in case of starvation.
3609 return false;
3610}
3611
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003612void Camera3Device::RequestThread::unpauseForNewRequests() {
3613 // With work to do, mark thread as unpaused.
3614 // If paused by request (setPaused), don't resume, to avoid
3615 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003616 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003617 Mutex::Autolock p(mPauseLock);
3618 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003619 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3620 if (mPaused) {
3621 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3622 if (statusTracker != 0) {
3623 statusTracker->markComponentActive(mStatusId);
3624 }
3625 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003626 mPaused = false;
3627 }
3628}
3629
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003630void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3631 sp<Camera3Device> parent = mParent.promote();
3632 if (parent != NULL) {
3633 va_list args;
3634 va_start(args, fmt);
3635
3636 parent->setErrorStateV(fmt, args);
3637
3638 va_end(args);
3639 }
3640}
3641
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003642status_t Camera3Device::RequestThread::insertTriggers(
3643 const sp<CaptureRequest> &request) {
3644
3645 Mutex::Autolock al(mTriggerMutex);
3646
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003647 sp<Camera3Device> parent = mParent.promote();
3648 if (parent == NULL) {
3649 CLOGE("RequestThread: Parent is gone");
3650 return DEAD_OBJECT;
3651 }
3652
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003653 CameraMetadata &metadata = request->mSettings;
3654 size_t count = mTriggerMap.size();
3655
3656 for (size_t i = 0; i < count; ++i) {
3657 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003658 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003659
3660 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3661 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3662 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003663 if (isAeTrigger) {
3664 request->mResultExtras.precaptureTriggerId = triggerId;
3665 mCurrentPreCaptureTriggerId = triggerId;
3666 } else {
3667 request->mResultExtras.afTriggerId = triggerId;
3668 mCurrentAfTriggerId = triggerId;
3669 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003670 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3671 continue; // Trigger ID tag is deprecated since device HAL 3.2
3672 }
3673 }
3674
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003675 camera_metadata_entry entry = metadata.find(tag);
3676
3677 if (entry.count > 0) {
3678 /**
3679 * Already has an entry for this trigger in the request.
3680 * Rewrite it with our requested trigger value.
3681 */
3682 RequestTrigger oldTrigger = trigger;
3683
3684 oldTrigger.entryValue = entry.data.u8[0];
3685
3686 mTriggerReplacedMap.add(tag, oldTrigger);
3687 } else {
3688 /**
3689 * More typical, no trigger entry, so we just add it
3690 */
3691 mTriggerRemovedMap.add(tag, trigger);
3692 }
3693
3694 status_t res;
3695
3696 switch (trigger.getTagType()) {
3697 case TYPE_BYTE: {
3698 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3699 res = metadata.update(tag,
3700 &entryValue,
3701 /*count*/1);
3702 break;
3703 }
3704 case TYPE_INT32:
3705 res = metadata.update(tag,
3706 &trigger.entryValue,
3707 /*count*/1);
3708 break;
3709 default:
3710 ALOGE("%s: Type not supported: 0x%x",
3711 __FUNCTION__,
3712 trigger.getTagType());
3713 return INVALID_OPERATION;
3714 }
3715
3716 if (res != OK) {
3717 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3718 ", value %d", __FUNCTION__, trigger.getTagName(),
3719 trigger.entryValue);
3720 return res;
3721 }
3722
3723 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3724 trigger.getTagName(),
3725 trigger.entryValue);
3726 }
3727
3728 mTriggerMap.clear();
3729
3730 return count;
3731}
3732
3733status_t Camera3Device::RequestThread::removeTriggers(
3734 const sp<CaptureRequest> &request) {
3735 Mutex::Autolock al(mTriggerMutex);
3736
3737 CameraMetadata &metadata = request->mSettings;
3738
3739 /**
3740 * Replace all old entries with their old values.
3741 */
3742 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3743 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3744
3745 status_t res;
3746
3747 uint32_t tag = trigger.metadataTag;
3748 switch (trigger.getTagType()) {
3749 case TYPE_BYTE: {
3750 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3751 res = metadata.update(tag,
3752 &entryValue,
3753 /*count*/1);
3754 break;
3755 }
3756 case TYPE_INT32:
3757 res = metadata.update(tag,
3758 &trigger.entryValue,
3759 /*count*/1);
3760 break;
3761 default:
3762 ALOGE("%s: Type not supported: 0x%x",
3763 __FUNCTION__,
3764 trigger.getTagType());
3765 return INVALID_OPERATION;
3766 }
3767
3768 if (res != OK) {
3769 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3770 ", trigger value %d", __FUNCTION__,
3771 trigger.getTagName(), trigger.entryValue);
3772 return res;
3773 }
3774 }
3775 mTriggerReplacedMap.clear();
3776
3777 /**
3778 * Remove all new entries.
3779 */
3780 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3781 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3782 status_t res = metadata.erase(trigger.metadataTag);
3783
3784 if (res != OK) {
3785 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3786 ", trigger value %d", __FUNCTION__,
3787 trigger.getTagName(), trigger.entryValue);
3788 return res;
3789 }
3790 }
3791 mTriggerRemovedMap.clear();
3792
3793 return OK;
3794}
3795
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003796status_t Camera3Device::RequestThread::addDummyTriggerIds(
3797 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003798 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003799 static const int32_t dummyTriggerId = 1;
3800 status_t res;
3801
3802 CameraMetadata &metadata = request->mSettings;
3803
3804 // If AF trigger is active, insert a dummy AF trigger ID if none already
3805 // exists
3806 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3807 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3808 if (afTrigger.count > 0 &&
3809 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3810 afId.count == 0) {
3811 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3812 if (res != OK) return res;
3813 }
3814
3815 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3816 // if none already exists
3817 camera_metadata_entry pcTrigger =
3818 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3819 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3820 if (pcTrigger.count > 0 &&
3821 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3822 pcId.count == 0) {
3823 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3824 &dummyTriggerId, 1);
3825 if (res != OK) return res;
3826 }
3827
3828 return OK;
3829}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003830
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003831/**
3832 * PreparerThread inner class methods
3833 */
3834
3835Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003836 Thread(/*canCallJava*/false), mListener(nullptr),
3837 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003838}
3839
3840Camera3Device::PreparerThread::~PreparerThread() {
3841 Thread::requestExitAndWait();
3842 if (mCurrentStream != nullptr) {
3843 mCurrentStream->cancelPrepare();
3844 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3845 mCurrentStream.clear();
3846 }
3847 clear();
3848}
3849
Ruben Brunkc78ac262015-08-13 17:58:46 -07003850status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003851 status_t res;
3852
3853 Mutex::Autolock l(mLock);
3854
Ruben Brunkc78ac262015-08-13 17:58:46 -07003855 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003856 if (res == OK) {
3857 // No preparation needed, fire listener right off
3858 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3859 if (mListener) {
3860 mListener->notifyPrepared(stream->getId());
3861 }
3862 return OK;
3863 } else if (res != NOT_ENOUGH_DATA) {
3864 return res;
3865 }
3866
3867 // Need to prepare, start up thread if necessary
3868 if (!mActive) {
3869 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3870 // isn't running
3871 Thread::requestExitAndWait();
3872 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3873 if (res != OK) {
3874 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3875 if (mListener) {
3876 mListener->notifyPrepared(stream->getId());
3877 }
3878 return res;
3879 }
3880 mCancelNow = false;
3881 mActive = true;
3882 ALOGV("%s: Preparer stream started", __FUNCTION__);
3883 }
3884
3885 // queue up the work
3886 mPendingStreams.push_back(stream);
3887 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3888
3889 return OK;
3890}
3891
3892status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003893 Mutex::Autolock l(mLock);
3894
3895 for (const auto& stream : mPendingStreams) {
3896 stream->cancelPrepare();
3897 }
3898 mPendingStreams.clear();
3899 mCancelNow = true;
3900
3901 return OK;
3902}
3903
3904void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3905 Mutex::Autolock l(mLock);
3906 mListener = listener;
3907}
3908
3909bool Camera3Device::PreparerThread::threadLoop() {
3910 status_t res;
3911 {
3912 Mutex::Autolock l(mLock);
3913 if (mCurrentStream == nullptr) {
3914 // End thread if done with work
3915 if (mPendingStreams.empty()) {
3916 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3917 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3918 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3919 mActive = false;
3920 return false;
3921 }
3922
3923 // Get next stream to prepare
3924 auto it = mPendingStreams.begin();
3925 mCurrentStream = *it;
3926 mPendingStreams.erase(it);
3927 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3928 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3929 } else if (mCancelNow) {
3930 mCurrentStream->cancelPrepare();
3931 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3932 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3933 mCurrentStream.clear();
3934 mCancelNow = false;
3935 return true;
3936 }
3937 }
3938
3939 res = mCurrentStream->prepareNextBuffer();
3940 if (res == NOT_ENOUGH_DATA) return true;
3941 if (res != OK) {
3942 // Something bad happened; try to recover by cancelling prepare and
3943 // signalling listener anyway
3944 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3945 mCurrentStream->getId(), res, strerror(-res));
3946 mCurrentStream->cancelPrepare();
3947 }
3948
3949 // This stream has finished, notify listener
3950 Mutex::Autolock l(mLock);
3951 if (mListener) {
3952 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3953 mCurrentStream->getId());
3954 mListener->notifyPrepared(mCurrentStream->getId());
3955 }
3956
3957 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3958 mCurrentStream.clear();
3959
3960 return true;
3961}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003962
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003963/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003964 * Static callback forwarding methods from HAL to instance
3965 */
3966
3967void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3968 const camera3_capture_result *result) {
3969 Camera3Device *d =
3970 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003971
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003972 d->processCaptureResult(result);
3973}
3974
3975void Camera3Device::sNotify(const camera3_callback_ops *cb,
3976 const camera3_notify_msg *msg) {
3977 Camera3Device *d =
3978 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3979 d->notify(msg);
3980}
3981
3982}; // namespace android