blob: 1caf157a7327a77547c64c5f702e833f97c0c1eb [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046#include <android/hardware/camera2/ICameraDeviceUser.h>
47
Igor Murashkinff3e31d2013-10-23 16:40:06 -070048#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070049#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070050#include "device3/Camera3Device.h"
51#include "device3/Camera3OutputStream.h"
52#include "device3/Camera3InputStream.h"
53#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070054#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070055#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080056
57using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058
59namespace android {
60
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061Camera3Device::Camera3Device(int id):
62 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070063 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080064 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070065 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070066 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070067 mUsePartialResult(false),
68 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080069 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070070 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070071 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070072 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070073 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070074 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080075{
76 ATRACE_CALL();
77 camera3_callback_ops::notify = &sNotify;
78 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
79 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
80}
81
82Camera3Device::~Camera3Device()
83{
84 ATRACE_CALL();
85 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
86 disconnect();
87}
88
Igor Murashkin71381052013-03-04 14:53:08 -080089int Camera3Device::getId() const {
90 return mId;
91}
92
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080093/**
94 * CameraDeviceBase interface
95 */
96
Yin-Chia Yehe074a932015-01-30 10:29:02 -080097status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080098{
99 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700100 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800101 Mutex::Autolock l(mLock);
102
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800104 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700105 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800106 return INVALID_OPERATION;
107 }
108
109 /** Open HAL device */
110
111 status_t res;
112 String8 deviceName = String8::format("%d", mId);
113
114 camera3_device_t *device;
115
Zhijun He213ce792013-11-19 08:45:15 -0800116 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800117 res = module->open(deviceName.string(),
118 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800119 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800120
121 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800123 return res;
124 }
125
126 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700127 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700128 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700129 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700130 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800131 device->common.version);
132 device->common.close(&device->common);
133 return BAD_VALUE;
134 }
135
136 camera_info info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800137 res = module->getCameraInfo(mId, &info);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 if (res != OK) return res;
139
140 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700141 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
142 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700143 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800144 device->common.close(&device->common);
145 return BAD_VALUE;
146 }
147
148 /** Initialize device with callback functions */
149
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700150 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700152 ATRACE_END();
153
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800154 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700155 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
156 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800157 device->common.close(&device->common);
158 return BAD_VALUE;
159 }
160
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700161 /** Start up status tracker thread */
162 mStatusTracker = new StatusTracker(this);
163 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
164 if (res != OK) {
165 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
166 strerror(-res), res);
167 device->common.close(&device->common);
168 mStatusTracker.clear();
169 return res;
170 }
171
Zhijun He125684a2015-12-26 15:07:30 -0800172 /** Create buffer manager */
173 mBufferManager = new Camera3BufferManager();
174
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700175 bool aeLockAvailable = false;
176 camera_metadata_ro_entry aeLockAvailableEntry;
177 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
178 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
179 if (res == OK && aeLockAvailableEntry.count > 0) {
180 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
181 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
182 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800183
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700184 /** Start up request queue thread */
185 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800186 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700188 SET_ERR_L("Unable to start request queue thread: %s (%d)",
189 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800191 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 return res;
193 }
194
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700195 mPreparerThread = new PreparerThread();
196
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800197 /** Everything is good to go */
198
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700199 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800200 mDeviceInfo = info.static_camera_characteristics;
201 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700202
203 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800204 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700205 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700206 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700207 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800208
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800209 // Measure the clock domain offset between camera and video/hw_composer
210 camera_metadata_entry timestampSource =
211 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
212 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
213 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
214 mTimestampOffset = getMonoToBoottimeOffset();
215 }
216
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700217 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700218 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
219 camera_metadata_entry partialResultsCount =
220 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
221 if (partialResultsCount.count > 0) {
222 mNumPartialResults = partialResultsCount.data.i32[0];
223 mUsePartialResult = (mNumPartialResults > 1);
224 }
225 } else {
226 camera_metadata_entry partialResultsQuirk =
227 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
228 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
229 mUsePartialResult = true;
230 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700231 }
232
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700233 camera_metadata_entry configs =
234 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
235 for (uint32_t i = 0; i < configs.count; i += 4) {
236 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
237 configs.data.i32[i + 3] ==
238 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
239 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
240 configs.data.i32[i + 2]));
241 }
242 }
243
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800244 return OK;
245}
246
247status_t Camera3Device::disconnect() {
248 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700249 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800250
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800251 ALOGV("%s: E", __FUNCTION__);
252
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700253 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800254
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700255 {
256 Mutex::Autolock l(mLock);
257 if (mStatus == STATUS_UNINITIALIZED) return res;
258
259 if (mStatus == STATUS_ACTIVE ||
260 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
261 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700262 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700263 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700264 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700265 } else {
266 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
267 if (res != OK) {
268 SET_ERR_L("Timeout waiting for HAL to drain");
269 // Continue to close device even in case of error
270 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700271 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800272 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800273
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700274 if (mStatus == STATUS_ERROR) {
275 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700276 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277
278 if (mStatusTracker != NULL) {
279 mStatusTracker->requestExit();
280 }
281
282 if (mRequestThread != NULL) {
283 mRequestThread->requestExit();
284 }
285
286 mOutputStreams.clear();
287 mInputStream.clear();
288 }
289
290 // Joining done without holding mLock, otherwise deadlocks may ensue
291 // as the threads try to access parent state
292 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
293 // HAL may be in a bad state, so waiting for request thread
294 // (which may be stuck in the HAL processCaptureRequest call)
295 // could be dangerous.
296 mRequestThread->join();
297 }
298
299 if (mStatusTracker != NULL) {
300 mStatusTracker->join();
301 }
302
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700303 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700304 {
305 Mutex::Autolock l(mLock);
306
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800307 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700308 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800309 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800310
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700311 hal3Device = mHal3Device;
312 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800313
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700314 // Call close without internal mutex held, as the HAL close may need to
315 // wait on assorted callbacks,etc, to complete before it can return.
316 if (hal3Device != NULL) {
317 ATRACE_BEGIN("camera3->close");
318 hal3Device->common.close(&hal3Device->common);
319 ATRACE_END();
320 }
321
322 {
323 Mutex::Autolock l(mLock);
324 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700325 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700326 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800327
328 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700329 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800330}
331
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700332// For dumping/debugging only -
333// try to acquire a lock a few times, eventually give up to proceed with
334// debug/dump operations
335bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
336 bool gotLock = false;
337 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
338 if (lock.tryLock() == NO_ERROR) {
339 gotLock = true;
340 break;
341 } else {
342 usleep(kDumpSleepDuration);
343 }
344 }
345 return gotLock;
346}
347
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700348Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
349 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
350 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
351 const int STREAM_CONFIGURATION_SIZE = 4;
352 const int STREAM_FORMAT_OFFSET = 0;
353 const int STREAM_WIDTH_OFFSET = 1;
354 const int STREAM_HEIGHT_OFFSET = 2;
355 const int STREAM_IS_INPUT_OFFSET = 3;
356 camera_metadata_ro_entry_t availableStreamConfigs =
357 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
358 if (availableStreamConfigs.count == 0 ||
359 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
360 return Size(0, 0);
361 }
362
363 // Get max jpeg size (area-wise).
364 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
365 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
366 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
367 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
368 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
369 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
370 && format == HAL_PIXEL_FORMAT_BLOB &&
371 (width * height > maxJpegWidth * maxJpegHeight)) {
372 maxJpegWidth = width;
373 maxJpegHeight = height;
374 }
375 }
376 } else {
377 camera_metadata_ro_entry availableJpegSizes =
378 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
379 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
380 return Size(0, 0);
381 }
382
383 // Get max jpeg size (area-wise).
384 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
385 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
386 > (maxJpegWidth * maxJpegHeight)) {
387 maxJpegWidth = availableJpegSizes.data.i32[i];
388 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
389 }
390 }
391 }
392 return Size(maxJpegWidth, maxJpegHeight);
393}
394
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800395nsecs_t Camera3Device::getMonoToBoottimeOffset() {
396 // try three times to get the clock offset, choose the one
397 // with the minimum gap in measurements.
398 const int tries = 3;
399 nsecs_t bestGap, measured;
400 for (int i = 0; i < tries; ++i) {
401 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
402 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
403 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
404 const nsecs_t gap = tmono2 - tmono;
405 if (i == 0 || gap < bestGap) {
406 bestGap = gap;
407 measured = tbase - ((tmono + tmono2) >> 1);
408 }
409 }
410 return measured;
411}
412
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -0700413/**
414 * Map Android N dataspace definitions back to Android M definitions, for
415 * use with HALv3.3 or older.
416 *
417 * Only map where correspondences exist, and otherwise preserve the value.
418 */
419android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) {
420 switch (dataSpace) {
421 case HAL_DATASPACE_V0_SRGB_LINEAR:
422 return HAL_DATASPACE_SRGB_LINEAR;
423 case HAL_DATASPACE_V0_SRGB:
424 return HAL_DATASPACE_SRGB;
425 case HAL_DATASPACE_V0_JFIF:
426 return HAL_DATASPACE_JFIF;
427 case HAL_DATASPACE_V0_BT601_625:
428 return HAL_DATASPACE_BT601_625;
429 case HAL_DATASPACE_V0_BT601_525:
430 return HAL_DATASPACE_BT601_525;
431 case HAL_DATASPACE_V0_BT709:
432 return HAL_DATASPACE_BT709;
433 default:
434 return dataSpace;
435 }
436}
437
Zhijun Hef7da0962014-04-24 13:27:56 -0700438ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700439 // Get max jpeg size (area-wise).
440 Size maxJpegResolution = getMaxJpegResolution();
441 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700442 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700443 __FUNCTION__, mId);
444 return BAD_VALUE;
445 }
446
Zhijun Hef7da0962014-04-24 13:27:56 -0700447 // Get max jpeg buffer size
448 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700449 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
450 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700451 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
452 return BAD_VALUE;
453 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700454 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800455 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700456
457 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700458 float scaleFactor = ((float) (width * height)) /
459 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800460 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
461 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700462 if (jpegBufferSize > maxJpegBufferSize) {
463 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700464 }
465
466 return jpegBufferSize;
467}
468
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700469ssize_t Camera3Device::getPointCloudBufferSize() const {
470 const int FLOATS_PER_POINT=4;
471 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
472 if (maxPointCount.count == 0) {
473 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
474 __FUNCTION__, mId);
475 return BAD_VALUE;
476 }
477 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
478 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
479 return maxBytesForPointCloud;
480}
481
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800482ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800483 const int PER_CONFIGURATION_SIZE = 3;
484 const int WIDTH_OFFSET = 0;
485 const int HEIGHT_OFFSET = 1;
486 const int SIZE_OFFSET = 2;
487 camera_metadata_ro_entry rawOpaqueSizes =
488 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800489 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800490 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -0800491 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800492 __FUNCTION__, mId, count);
493 return BAD_VALUE;
494 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700495
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800496 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
497 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
498 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
499 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
500 }
501 }
502
503 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
504 __FUNCTION__, mId, width, height);
505 return BAD_VALUE;
506}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700507
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800508status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
509 ATRACE_CALL();
510 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700511
512 // Try to lock, but continue in case of failure (to avoid blocking in
513 // deadlocks)
514 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
515 bool gotLock = tryLockSpinRightRound(mLock);
516
517 ALOGW_IF(!gotInterfaceLock,
518 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
519 mId, __FUNCTION__);
520 ALOGW_IF(!gotLock,
521 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
522 mId, __FUNCTION__);
523
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800524 bool dumpTemplates = false;
525 String16 templatesOption("-t");
526 int n = args.size();
527 for (int i = 0; i < n; i++) {
528 if (args[i] == templatesOption) {
529 dumpTemplates = true;
530 }
531 }
532
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800533 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800534
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800535 const char *status =
536 mStatus == STATUS_ERROR ? "ERROR" :
537 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700538 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
539 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800540 mStatus == STATUS_ACTIVE ? "ACTIVE" :
541 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700542
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800543 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700544 if (mStatus == STATUS_ERROR) {
545 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
546 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800547 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700548 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700549 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800550
551 if (mInputStream != NULL) {
552 write(fd, lines.string(), lines.size());
553 mInputStream->dump(fd, args);
554 } else {
555 lines.appendFormat(" No input stream.\n");
556 write(fd, lines.string(), lines.size());
557 }
558 for (size_t i = 0; i < mOutputStreams.size(); i++) {
559 mOutputStreams[i]->dump(fd,args);
560 }
561
Zhijun He431503c2016-03-07 17:30:16 -0800562 if (mBufferManager != NULL) {
563 lines = String8(" Camera3 Buffer Manager:\n");
564 write(fd, lines.string(), lines.size());
565 mBufferManager->dump(fd, args);
566 }
Zhijun He125684a2015-12-26 15:07:30 -0800567
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700568 lines = String8(" In-flight requests:\n");
569 if (mInFlightMap.size() == 0) {
570 lines.append(" None\n");
571 } else {
572 for (size_t i = 0; i < mInFlightMap.size(); i++) {
573 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700574 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700575 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800576 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700577 r.numBuffersLeft);
578 }
579 }
580 write(fd, lines.string(), lines.size());
581
Igor Murashkin1e479c02013-09-06 16:55:14 -0700582 {
583 lines = String8(" Last request sent:\n");
584 write(fd, lines.string(), lines.size());
585
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700586 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700587 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
588 }
589
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800590 if (dumpTemplates) {
591 const char *templateNames[] = {
592 "TEMPLATE_PREVIEW",
593 "TEMPLATE_STILL_CAPTURE",
594 "TEMPLATE_VIDEO_RECORD",
595 "TEMPLATE_VIDEO_SNAPSHOT",
596 "TEMPLATE_ZERO_SHUTTER_LAG",
597 "TEMPLATE_MANUAL"
598 };
599
600 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
601 const camera_metadata_t *templateRequest;
602 templateRequest =
603 mHal3Device->ops->construct_default_request_settings(
604 mHal3Device, i);
605 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
606 if (templateRequest == NULL) {
607 lines.append(" Not supported\n");
608 write(fd, lines.string(), lines.size());
609 } else {
610 write(fd, lines.string(), lines.size());
611 dump_indented_camera_metadata(templateRequest,
612 fd, /*verbosity*/2, /*indentation*/8);
613 }
614 }
615 }
616
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800617 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700618 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800619 write(fd, lines.string(), lines.size());
620 mHal3Device->ops->dump(mHal3Device, fd);
621 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800622
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700623 if (gotLock) mLock.unlock();
624 if (gotInterfaceLock) mInterfaceLock.unlock();
625
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800626 return OK;
627}
628
629const CameraMetadata& Camera3Device::info() const {
630 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800631 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
632 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700633 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800634 mStatus == STATUS_ERROR ?
635 "when in error state" : "before init");
636 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800637 return mDeviceInfo;
638}
639
Jianing Wei90e59c92014-03-12 18:29:36 -0700640status_t Camera3Device::checkStatusOkToCaptureLocked() {
641 switch (mStatus) {
642 case STATUS_ERROR:
643 CLOGE("Device has encountered a serious error");
644 return INVALID_OPERATION;
645 case STATUS_UNINITIALIZED:
646 CLOGE("Device not initialized");
647 return INVALID_OPERATION;
648 case STATUS_UNCONFIGURED:
649 case STATUS_CONFIGURED:
650 case STATUS_ACTIVE:
651 // OK
652 break;
653 default:
654 SET_ERR_L("Unexpected status: %d", mStatus);
655 return INVALID_OPERATION;
656 }
657 return OK;
658}
659
660status_t Camera3Device::convertMetadataListToRequestListLocked(
661 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
662 if (requestList == NULL) {
663 CLOGE("requestList cannot be NULL.");
664 return BAD_VALUE;
665 }
666
Jianing Weicb0652e2014-03-12 18:29:36 -0700667 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700668 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
669 it != metadataList.end(); ++it) {
670 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
671 if (newRequest == 0) {
672 CLOGE("Can't create capture request");
673 return BAD_VALUE;
674 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700675
676 // Setup burst Id and request Id
677 newRequest->mResultExtras.burstId = burstId++;
678 if (it->exists(ANDROID_REQUEST_ID)) {
679 if (it->find(ANDROID_REQUEST_ID).count == 0) {
680 CLOGE("RequestID entry exists; but must not be empty in metadata");
681 return BAD_VALUE;
682 }
683 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
684 } else {
685 CLOGE("RequestID does not exist in metadata");
686 return BAD_VALUE;
687 }
688
Jianing Wei90e59c92014-03-12 18:29:36 -0700689 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700690
691 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700692 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700693
694 // Setup batch size if this is a high speed video recording request.
695 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
696 auto firstRequest = requestList->begin();
697 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
698 if (outputStream->isVideoStream()) {
699 (*firstRequest)->mBatchSize = requestList->size();
700 break;
701 }
702 }
703 }
704
Jianing Wei90e59c92014-03-12 18:29:36 -0700705 return OK;
706}
707
Jianing Weicb0652e2014-03-12 18:29:36 -0700708status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800709 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800710
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700711 List<const CameraMetadata> requests;
712 requests.push_back(request);
713 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800714}
715
Jianing Wei90e59c92014-03-12 18:29:36 -0700716status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700717 const List<const CameraMetadata> &requests, bool repeating,
718 /*out*/
719 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700720 ATRACE_CALL();
721 Mutex::Autolock il(mInterfaceLock);
722 Mutex::Autolock l(mLock);
723
724 status_t res = checkStatusOkToCaptureLocked();
725 if (res != OK) {
726 // error logged by previous call
727 return res;
728 }
729
730 RequestList requestList;
731
732 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
733 if (res != OK) {
734 // error logged by previous call
735 return res;
736 }
737
738 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700739 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700740 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700741 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700742 }
743
744 if (res == OK) {
745 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
746 if (res != OK) {
747 SET_ERR_L("Can't transition to active in %f seconds!",
748 kActiveTimeout/1e9);
749 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700750 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
751 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700752 } else {
753 CLOGE("Cannot queue request. Impossible.");
754 return BAD_VALUE;
755 }
756
757 return res;
758}
759
Jianing Weicb0652e2014-03-12 18:29:36 -0700760status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
761 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700762 ATRACE_CALL();
763
Jianing Weicb0652e2014-03-12 18:29:36 -0700764 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700765}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800766
Jianing Weicb0652e2014-03-12 18:29:36 -0700767status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
768 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800769 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800770
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700771 List<const CameraMetadata> requests;
772 requests.push_back(request);
773 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800774}
775
Jianing Weicb0652e2014-03-12 18:29:36 -0700776status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
777 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700778 ATRACE_CALL();
779
Jianing Weicb0652e2014-03-12 18:29:36 -0700780 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700781}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800782
783sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
784 const CameraMetadata &request) {
785 status_t res;
786
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700787 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800788 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700789 // Stream configuration failed due to unsupported configuration.
790 // Device back to unconfigured state. Client might try other configuraitons
791 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
792 CLOGE("No streams configured");
793 return NULL;
794 }
795 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800796 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700797 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800798 return NULL;
799 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700800 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700801 if (mStatus == STATUS_UNCONFIGURED) {
802 CLOGE("No streams configured");
803 return NULL;
804 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800805 }
806
807 sp<CaptureRequest> newRequest = createCaptureRequest(request);
808 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800809}
810
Jianing Weicb0652e2014-03-12 18:29:36 -0700811status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800812 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700813 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800814 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800815
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800816 switch (mStatus) {
817 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700818 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800819 return INVALID_OPERATION;
820 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700821 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800822 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700823 case STATUS_UNCONFIGURED:
824 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800825 case STATUS_ACTIVE:
826 // OK
827 break;
828 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700829 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800830 return INVALID_OPERATION;
831 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700832 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700833
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700834 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800835}
836
837status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
838 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700839 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800840
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700841 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800842}
843
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700844status_t Camera3Device::createInputStream(
845 uint32_t width, uint32_t height, int format, int *id) {
846 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700847 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700848 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700849 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
850 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700851
852 status_t res;
853 bool wasActive = false;
854
855 switch (mStatus) {
856 case STATUS_ERROR:
857 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
858 return INVALID_OPERATION;
859 case STATUS_UNINITIALIZED:
860 ALOGE("%s: Device not initialized", __FUNCTION__);
861 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700862 case STATUS_UNCONFIGURED:
863 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700864 // OK
865 break;
866 case STATUS_ACTIVE:
867 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700868 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700869 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700870 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700871 return res;
872 }
873 wasActive = true;
874 break;
875 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700876 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700877 return INVALID_OPERATION;
878 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700879 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700880
881 if (mInputStream != 0) {
882 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
883 return INVALID_OPERATION;
884 }
885
886 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
887 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700888 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700889
890 mInputStream = newStream;
891
892 *id = mNextStreamId++;
893
894 // Continue captures if active at start
895 if (wasActive) {
896 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
897 res = configureStreamsLocked();
898 if (res != OK) {
899 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
900 __FUNCTION__, mNextStreamId, strerror(-res), res);
901 return res;
902 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700903 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700904 }
905
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700906 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700907 return OK;
908}
909
Igor Murashkin2fba5842013-04-22 14:03:54 -0700910
911status_t Camera3Device::createZslStream(
912 uint32_t width, uint32_t height,
913 int depth,
914 /*out*/
915 int *id,
916 sp<Camera3ZslStream>* zslStream) {
917 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700918 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700919 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700920 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
921 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700922
923 status_t res;
924 bool wasActive = false;
925
926 switch (mStatus) {
927 case STATUS_ERROR:
928 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
929 return INVALID_OPERATION;
930 case STATUS_UNINITIALIZED:
931 ALOGE("%s: Device not initialized", __FUNCTION__);
932 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700933 case STATUS_UNCONFIGURED:
934 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700935 // OK
936 break;
937 case STATUS_ACTIVE:
938 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700939 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700940 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700941 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700942 return res;
943 }
944 wasActive = true;
945 break;
946 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700947 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700948 return INVALID_OPERATION;
949 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700950 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700951
952 if (mInputStream != 0) {
953 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
954 return INVALID_OPERATION;
955 }
956
957 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
958 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700959 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700960
961 res = mOutputStreams.add(mNextStreamId, newStream);
962 if (res < 0) {
963 ALOGE("%s: Can't add new stream to set: %s (%d)",
964 __FUNCTION__, strerror(-res), res);
965 return res;
966 }
967 mInputStream = newStream;
968
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530969 mNeedConfig = true;
970
Igor Murashkin2fba5842013-04-22 14:03:54 -0700971 *id = mNextStreamId++;
972 *zslStream = newStream;
973
974 // Continue captures if active at start
975 if (wasActive) {
976 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
977 res = configureStreamsLocked();
978 if (res != OK) {
979 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
980 __FUNCTION__, mNextStreamId, strerror(-res), res);
981 return res;
982 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700983 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700984 }
985
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700986 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700987 return OK;
988}
989
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700990status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800991 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -0800992 camera3_stream_rotation_t rotation, int *id, int streamSetId) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800993 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700994 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800995 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700996 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
997 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800998
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800999 status_t res;
1000 bool wasActive = false;
1001
1002 switch (mStatus) {
1003 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001004 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001005 return INVALID_OPERATION;
1006 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001007 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001008 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001009 case STATUS_UNCONFIGURED:
1010 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001011 // OK
1012 break;
1013 case STATUS_ACTIVE:
1014 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001015 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001016 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001017 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001018 return res;
1019 }
1020 wasActive = true;
1021 break;
1022 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001023 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001024 return INVALID_OPERATION;
1025 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001026 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001027
1028 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001029 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001030 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001031 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001032 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1033 }
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001034 // Use legacy dataspace values for older HALs
1035 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1036 dataSpace = mapToLegacyDataspace(dataSpace);
1037 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001038 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001039 ssize_t blobBufferSize;
1040 if (dataSpace != HAL_DATASPACE_DEPTH) {
1041 blobBufferSize = getJpegBufferSize(width, height);
1042 if (blobBufferSize <= 0) {
1043 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1044 return BAD_VALUE;
1045 }
1046 } else {
1047 blobBufferSize = getPointCloudBufferSize();
1048 if (blobBufferSize <= 0) {
1049 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1050 return BAD_VALUE;
1051 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001052 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001054 width, height, blobBufferSize, format, dataSpace, rotation,
1055 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001056 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1057 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1058 if (rawOpaqueBufferSize <= 0) {
1059 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1060 return BAD_VALUE;
1061 }
1062 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001063 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1064 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001065 } else {
1066 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001067 width, height, format, dataSpace, rotation,
1068 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001070 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071
Zhijun He125684a2015-12-26 15:07:30 -08001072 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001073 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1074 * requires buffers to be statically allocated for internal static buffer registration, while
1075 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1076 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001077 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001078 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001079 newStream->setBufferManager(mBufferManager);
1080 }
1081
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001082 res = mOutputStreams.add(mNextStreamId, newStream);
1083 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001084 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001085 return res;
1086 }
1087
1088 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001089 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001090
1091 // Continue captures if active at start
1092 if (wasActive) {
1093 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1094 res = configureStreamsLocked();
1095 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001096 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1097 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001098 return res;
1099 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001100 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001101 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001102 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001103 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001104}
1105
1106status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1107 ATRACE_CALL();
1108 (void)outputId; (void)id;
1109
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001110 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001111 return INVALID_OPERATION;
1112}
1113
1114
1115status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001116 uint32_t *width, uint32_t *height,
1117 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001118 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001119 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001120 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001121
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001122 switch (mStatus) {
1123 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001124 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001125 return INVALID_OPERATION;
1126 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001127 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001128 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001129 case STATUS_UNCONFIGURED:
1130 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001131 case STATUS_ACTIVE:
1132 // OK
1133 break;
1134 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001135 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001136 return INVALID_OPERATION;
1137 }
1138
1139 ssize_t idx = mOutputStreams.indexOfKey(id);
1140 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001141 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001142 return idx;
1143 }
1144
1145 if (width) *width = mOutputStreams[idx]->getWidth();
1146 if (height) *height = mOutputStreams[idx]->getHeight();
1147 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001148 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001149 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001150}
1151
1152status_t Camera3Device::setStreamTransform(int id,
1153 int transform) {
1154 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001155 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001156 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001157
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001158 switch (mStatus) {
1159 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001160 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001161 return INVALID_OPERATION;
1162 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001163 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001164 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001165 case STATUS_UNCONFIGURED:
1166 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001167 case STATUS_ACTIVE:
1168 // OK
1169 break;
1170 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001171 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001172 return INVALID_OPERATION;
1173 }
1174
1175 ssize_t idx = mOutputStreams.indexOfKey(id);
1176 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001177 CLOGE("Stream %d does not exist",
1178 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001179 return BAD_VALUE;
1180 }
1181
1182 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001183}
1184
1185status_t Camera3Device::deleteStream(int id) {
1186 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001187 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001188 Mutex::Autolock l(mLock);
1189 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001190
Igor Murashkine2172be2013-05-28 15:31:39 -07001191 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1192
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001193 // CameraDevice semantics require device to already be idle before
1194 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001195 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001196 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1197 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001198 }
1199
Igor Murashkin2fba5842013-04-22 14:03:54 -07001200 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001201 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001202 if (mInputStream != NULL && id == mInputStream->getId()) {
1203 deletedStream = mInputStream;
1204 mInputStream.clear();
1205 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001206 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001207 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001208 return BAD_VALUE;
1209 }
Zhijun He5f446352014-01-22 09:49:33 -08001210 }
1211
1212 // Delete output stream or the output part of a bi-directional stream.
1213 if (outputStreamIdx != NAME_NOT_FOUND) {
1214 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001215 mOutputStreams.removeItem(id);
1216 }
1217
1218 // Free up the stream endpoint so that it can be used by some other stream
1219 res = deletedStream->disconnect();
1220 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001221 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001222 // fall through since we want to still list the stream as deleted.
1223 }
1224 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001225 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001226
1227 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001228}
1229
1230status_t Camera3Device::deleteReprocessStream(int id) {
1231 ATRACE_CALL();
1232 (void)id;
1233
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001234 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001235 return INVALID_OPERATION;
1236}
1237
Zhijun He1fa89992015-06-01 15:44:31 -07001238status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001239 ATRACE_CALL();
1240 ALOGV("%s: E", __FUNCTION__);
1241
1242 Mutex::Autolock il(mInterfaceLock);
1243 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001244
1245 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1246 mNeedConfig = true;
1247 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1248 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001249
1250 return configureStreamsLocked();
1251}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001252
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001253status_t Camera3Device::getInputBufferProducer(
1254 sp<IGraphicBufferProducer> *producer) {
1255 Mutex::Autolock il(mInterfaceLock);
1256 Mutex::Autolock l(mLock);
1257
1258 if (producer == NULL) {
1259 return BAD_VALUE;
1260 } else if (mInputStream == NULL) {
1261 return INVALID_OPERATION;
1262 }
1263
1264 return mInputStream->getInputBufferProducer(producer);
1265}
1266
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001267status_t Camera3Device::createDefaultRequest(int templateId,
1268 CameraMetadata *request) {
1269 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001270 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001271
1272 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1273 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1274 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1275 return BAD_VALUE;
1276 }
1277
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001278 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001279 Mutex::Autolock l(mLock);
1280
1281 switch (mStatus) {
1282 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001283 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001284 return INVALID_OPERATION;
1285 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001286 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001287 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001288 case STATUS_UNCONFIGURED:
1289 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001290 case STATUS_ACTIVE:
1291 // OK
1292 break;
1293 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001294 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001295 return INVALID_OPERATION;
1296 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001297
Zhijun Hea1530f12014-09-14 12:44:20 -07001298 if (!mRequestTemplateCache[templateId].isEmpty()) {
1299 *request = mRequestTemplateCache[templateId];
1300 return OK;
1301 }
1302
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001303 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001304 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001305 rawRequest = mHal3Device->ops->construct_default_request_settings(
1306 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001307 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001308 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001309 ALOGI("%s: template %d is not supported on this camera device",
1310 __FUNCTION__, templateId);
1311 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001312 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001313 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001314 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001315
1316 return OK;
1317}
1318
1319status_t Camera3Device::waitUntilDrained() {
1320 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001321 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001322 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001323
Zhijun He69a37482014-03-23 18:44:49 -07001324 return waitUntilDrainedLocked();
1325}
1326
1327status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001328 switch (mStatus) {
1329 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001330 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001331 ALOGV("%s: Already idle", __FUNCTION__);
1332 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001333 case STATUS_CONFIGURED:
1334 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001335 case STATUS_ERROR:
1336 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001337 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001338 break;
1339 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001340 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001341 return INVALID_OPERATION;
1342 }
1343
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001344 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1345 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001346 if (res != OK) {
1347 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1348 res);
1349 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001350 return res;
1351}
1352
Ruben Brunk183f0562015-08-12 12:55:02 -07001353
1354void Camera3Device::internalUpdateStatusLocked(Status status) {
1355 mStatus = status;
1356 mRecentStatusUpdates.add(mStatus);
1357 mStatusChanged.broadcast();
1358}
1359
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001360// Pause to reconfigure
1361status_t Camera3Device::internalPauseAndWaitLocked() {
1362 mRequestThread->setPaused(true);
1363 mPauseStateNotify = true;
1364
1365 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1366 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1367 if (res != OK) {
1368 SET_ERR_L("Can't idle device in %f seconds!",
1369 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001370 }
1371
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001372 return res;
1373}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001374
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001375// Resume after internalPauseAndWaitLocked
1376status_t Camera3Device::internalResumeLocked() {
1377 status_t res;
1378
1379 mRequestThread->setPaused(false);
1380
1381 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1382 if (res != OK) {
1383 SET_ERR_L("Can't transition to active in %f seconds!",
1384 kActiveTimeout/1e9);
1385 }
1386 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001387 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001388}
1389
Ruben Brunk183f0562015-08-12 12:55:02 -07001390status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001391 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001392
1393 size_t startIndex = 0;
1394 if (mStatusWaiters == 0) {
1395 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1396 // this status list
1397 mRecentStatusUpdates.clear();
1398 } else {
1399 // If other threads are waiting on updates to this status list, set the position of the
1400 // first element that this list will check rather than clearing the list.
1401 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001402 }
1403
Ruben Brunk183f0562015-08-12 12:55:02 -07001404 mStatusWaiters++;
1405
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001406 bool stateSeen = false;
1407 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001408 if (active == (mStatus == STATUS_ACTIVE)) {
1409 // Desired state is current
1410 break;
1411 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001412
1413 res = mStatusChanged.waitRelative(mLock, timeout);
1414 if (res != OK) break;
1415
Ruben Brunk183f0562015-08-12 12:55:02 -07001416 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1417 // transitions.
1418 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1419 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1420 __FUNCTION__);
1421
1422 // Encountered desired state since we began waiting
1423 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001424 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1425 stateSeen = true;
1426 break;
1427 }
1428 }
1429 } while (!stateSeen);
1430
Ruben Brunk183f0562015-08-12 12:55:02 -07001431 mStatusWaiters--;
1432
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001433 return res;
1434}
1435
1436
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001437status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1438 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001439 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001440
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001441 if (listener != NULL && mListener != NULL) {
1442 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1443 }
1444 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001445 mRequestThread->setNotificationListener(listener);
1446 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001447
1448 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001449}
1450
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001451bool Camera3Device::willNotify3A() {
1452 return false;
1453}
1454
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001455status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001456 status_t res;
1457 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001458
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001459 while (mResultQueue.empty()) {
1460 res = mResultSignal.waitRelative(mOutputLock, timeout);
1461 if (res == TIMED_OUT) {
1462 return res;
1463 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001464 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001465 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001466 return res;
1467 }
1468 }
1469 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001470}
1471
Jianing Weicb0652e2014-03-12 18:29:36 -07001472status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001473 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001474 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001475
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001476 if (mResultQueue.empty()) {
1477 return NOT_ENOUGH_DATA;
1478 }
1479
Jianing Weicb0652e2014-03-12 18:29:36 -07001480 if (frame == NULL) {
1481 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1482 return BAD_VALUE;
1483 }
1484
1485 CaptureResult &result = *(mResultQueue.begin());
1486 frame->mResultExtras = result.mResultExtras;
1487 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001488 mResultQueue.erase(mResultQueue.begin());
1489
1490 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001491}
1492
1493status_t Camera3Device::triggerAutofocus(uint32_t id) {
1494 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001495 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001496
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001497 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1498 // Mix-in this trigger into the next request and only the next request.
1499 RequestTrigger trigger[] = {
1500 {
1501 ANDROID_CONTROL_AF_TRIGGER,
1502 ANDROID_CONTROL_AF_TRIGGER_START
1503 },
1504 {
1505 ANDROID_CONTROL_AF_TRIGGER_ID,
1506 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001507 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001508 };
1509
1510 return mRequestThread->queueTrigger(trigger,
1511 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001512}
1513
1514status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1515 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001516 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001517
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001518 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1519 // Mix-in this trigger into the next request and only the next request.
1520 RequestTrigger trigger[] = {
1521 {
1522 ANDROID_CONTROL_AF_TRIGGER,
1523 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1524 },
1525 {
1526 ANDROID_CONTROL_AF_TRIGGER_ID,
1527 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001528 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001529 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001530
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001531 return mRequestThread->queueTrigger(trigger,
1532 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001533}
1534
1535status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1536 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001537 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001538
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001539 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1540 // Mix-in this trigger into the next request and only the next request.
1541 RequestTrigger trigger[] = {
1542 {
1543 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1544 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1545 },
1546 {
1547 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1548 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001549 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001550 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001551
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001552 return mRequestThread->queueTrigger(trigger,
1553 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001554}
1555
1556status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1557 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1558 ATRACE_CALL();
1559 (void)reprocessStreamId; (void)buffer; (void)listener;
1560
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001561 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001562 return INVALID_OPERATION;
1563}
1564
Jianing Weicb0652e2014-03-12 18:29:36 -07001565status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001566 ATRACE_CALL();
1567 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001568 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001569
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001570 NotificationListener* listener;
1571 {
1572 Mutex::Autolock l(mOutputLock);
1573 listener = mListener;
1574 }
1575
Zhijun He7ef20392014-04-21 16:04:17 -07001576 {
1577 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001578 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001579 }
1580
Zhijun He491e3412013-12-27 10:57:44 -08001581 status_t res;
1582 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001583 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001584 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001585 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001586 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001587 }
1588
1589 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001590}
1591
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001592status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001593 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1594}
1595
1596status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001597 ATRACE_CALL();
1598 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001599 Mutex::Autolock il(mInterfaceLock);
1600 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001601
1602 sp<Camera3StreamInterface> stream;
1603 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1604 if (outputStreamIdx == NAME_NOT_FOUND) {
1605 CLOGE("Stream %d does not exist", streamId);
1606 return BAD_VALUE;
1607 }
1608
1609 stream = mOutputStreams.editValueAt(outputStreamIdx);
1610
1611 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001612 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001613 return BAD_VALUE;
1614 }
1615
1616 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001617 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001618 return BAD_VALUE;
1619 }
1620
Ruben Brunkc78ac262015-08-13 17:58:46 -07001621 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001622}
1623
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001624status_t Camera3Device::tearDown(int streamId) {
1625 ATRACE_CALL();
1626 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1627 Mutex::Autolock il(mInterfaceLock);
1628 Mutex::Autolock l(mLock);
1629
1630 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1631 // since we cannot call register_stream_buffers except right after configure_streams.
1632 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1633 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1634 __FUNCTION__, mHal3Device->common.version);
1635 return NO_INIT;
1636 }
1637
1638 sp<Camera3StreamInterface> stream;
1639 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1640 if (outputStreamIdx == NAME_NOT_FOUND) {
1641 CLOGE("Stream %d does not exist", streamId);
1642 return BAD_VALUE;
1643 }
1644
1645 stream = mOutputStreams.editValueAt(outputStreamIdx);
1646
1647 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1648 CLOGE("Stream %d is a target of a in-progress request", streamId);
1649 return BAD_VALUE;
1650 }
1651
1652 return stream->tearDown();
1653}
1654
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001655status_t Camera3Device::addBufferListenerForStream(int streamId,
1656 wp<Camera3StreamBufferListener> listener) {
1657 ATRACE_CALL();
1658 ALOGV("%s: Camera %d: Adding buffer listener for stream %d", __FUNCTION__, mId, streamId);
1659 Mutex::Autolock il(mInterfaceLock);
1660 Mutex::Autolock l(mLock);
1661
1662 sp<Camera3StreamInterface> stream;
1663 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1664 if (outputStreamIdx == NAME_NOT_FOUND) {
1665 CLOGE("Stream %d does not exist", streamId);
1666 return BAD_VALUE;
1667 }
1668
1669 stream = mOutputStreams.editValueAt(outputStreamIdx);
1670 stream->addBufferListener(listener);
1671
1672 return OK;
1673}
1674
Zhijun He204e3292014-07-14 17:09:23 -07001675uint32_t Camera3Device::getDeviceVersion() {
1676 ATRACE_CALL();
1677 Mutex::Autolock il(mInterfaceLock);
1678 return mDeviceVersion;
1679}
1680
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001681/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001682 * Methods called by subclasses
1683 */
1684
1685void Camera3Device::notifyStatus(bool idle) {
1686 {
1687 // Need mLock to safely update state and synchronize to current
1688 // state of methods in flight.
1689 Mutex::Autolock l(mLock);
1690 // We can get various system-idle notices from the status tracker
1691 // while starting up. Only care about them if we've actually sent
1692 // in some requests recently.
1693 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1694 return;
1695 }
1696 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1697 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001698 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001699
1700 // Skip notifying listener if we're doing some user-transparent
1701 // state changes
1702 if (mPauseStateNotify) return;
1703 }
1704 NotificationListener *listener;
1705 {
1706 Mutex::Autolock l(mOutputLock);
1707 listener = mListener;
1708 }
1709 if (idle && listener != NULL) {
1710 listener->notifyIdle();
1711 }
1712}
1713
1714/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001715 * Camera3Device private methods
1716 */
1717
1718sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1719 const CameraMetadata &request) {
1720 ATRACE_CALL();
1721 status_t res;
1722
1723 sp<CaptureRequest> newRequest = new CaptureRequest;
1724 newRequest->mSettings = request;
1725
1726 camera_metadata_entry_t inputStreams =
1727 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1728 if (inputStreams.count > 0) {
1729 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001730 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001731 CLOGE("Request references unknown input stream %d",
1732 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001733 return NULL;
1734 }
1735 // Lazy completion of stream configuration (allocation/registration)
1736 // on first use
1737 if (mInputStream->isConfiguring()) {
1738 res = mInputStream->finishConfiguration(mHal3Device);
1739 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001740 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001741 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001742 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001743 return NULL;
1744 }
1745 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001746 // Check if stream is being prepared
1747 if (mInputStream->isPreparing()) {
1748 CLOGE("Request references an input stream that's being prepared!");
1749 return NULL;
1750 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001751
1752 newRequest->mInputStream = mInputStream;
1753 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1754 }
1755
1756 camera_metadata_entry_t streams =
1757 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1758 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001759 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001760 return NULL;
1761 }
1762
1763 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001764 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001765 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001766 CLOGE("Request references unknown stream %d",
1767 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001768 return NULL;
1769 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001770 sp<Camera3OutputStreamInterface> stream =
1771 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001772
1773 // Lazy completion of stream configuration (allocation/registration)
1774 // on first use
1775 if (stream->isConfiguring()) {
1776 res = stream->finishConfiguration(mHal3Device);
1777 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001778 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1779 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001780 return NULL;
1781 }
1782 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001783 // Check if stream is being prepared
1784 if (stream->isPreparing()) {
1785 CLOGE("Request references an output stream that's being prepared!");
1786 return NULL;
1787 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001788
1789 newRequest->mOutputStreams.push(stream);
1790 }
1791 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001792 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001793
1794 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001795}
1796
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001797bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1798 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1799 Size size = mSupportedOpaqueInputSizes[i];
1800 if (size.width == width && size.height == height) {
1801 return true;
1802 }
1803 }
1804
1805 return false;
1806}
1807
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001808status_t Camera3Device::configureStreamsLocked() {
1809 ATRACE_CALL();
1810 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001811
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001812 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001813 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001814 return INVALID_OPERATION;
1815 }
1816
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001817 if (!mNeedConfig) {
1818 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1819 return OK;
1820 }
1821
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001822 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1823 // adding a dummy stream instead.
1824 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1825 if (mOutputStreams.size() == 0) {
1826 addDummyStreamLocked();
1827 } else {
1828 tryRemoveDummyStreamLocked();
1829 }
1830
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001831 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001832 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001833
1834 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001835 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1836 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1837 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001838 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1839
1840 Vector<camera3_stream_t*> streams;
1841 streams.setCapacity(config.num_streams);
1842
1843 if (mInputStream != NULL) {
1844 camera3_stream_t *inputStream;
1845 inputStream = mInputStream->startConfiguration();
1846 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001847 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001848 return INVALID_OPERATION;
1849 }
1850 streams.add(inputStream);
1851 }
1852
1853 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001854
1855 // Don't configure bidi streams twice, nor add them twice to the list
1856 if (mOutputStreams[i].get() ==
1857 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1858
1859 config.num_streams--;
1860 continue;
1861 }
1862
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001863 camera3_stream_t *outputStream;
1864 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1865 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001866 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001867 return INVALID_OPERATION;
1868 }
1869 streams.add(outputStream);
1870 }
1871
1872 config.streams = streams.editArray();
1873
1874 // Do the HAL configuration; will potentially touch stream
1875 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001876 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001877 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001878 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001879
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001880 if (res == BAD_VALUE) {
1881 // HAL rejected this set of streams as unsupported, clean up config
1882 // attempt and return to unconfigured state
1883 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1884 res = mInputStream->cancelConfiguration();
1885 if (res != OK) {
1886 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1887 mInputStream->getId(), strerror(-res), res);
1888 return res;
1889 }
1890 }
1891
1892 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1893 sp<Camera3OutputStreamInterface> outputStream =
1894 mOutputStreams.editValueAt(i);
1895 if (outputStream->isConfiguring()) {
1896 res = outputStream->cancelConfiguration();
1897 if (res != OK) {
1898 SET_ERR_L(
1899 "Can't cancel configuring output stream %d: %s (%d)",
1900 outputStream->getId(), strerror(-res), res);
1901 return res;
1902 }
1903 }
1904 }
1905
1906 // Return state to that at start of call, so that future configures
1907 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001908 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001909 mNeedConfig = true;
1910
1911 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1912 return BAD_VALUE;
1913 } else if (res != OK) {
1914 // Some other kind of error from configure_streams - this is not
1915 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001916 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1917 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001918 return res;
1919 }
1920
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001921 // Finish all stream configuration immediately.
1922 // TODO: Try to relax this later back to lazy completion, which should be
1923 // faster
1924
Igor Murashkin073f8572013-05-02 14:59:28 -07001925 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001926 res = mInputStream->finishConfiguration(mHal3Device);
1927 if (res != OK) {
1928 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1929 mInputStream->getId(), strerror(-res), res);
1930 return res;
1931 }
1932 }
1933
1934 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001935 sp<Camera3OutputStreamInterface> outputStream =
1936 mOutputStreams.editValueAt(i);
1937 if (outputStream->isConfiguring()) {
1938 res = outputStream->finishConfiguration(mHal3Device);
1939 if (res != OK) {
1940 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1941 outputStream->getId(), strerror(-res), res);
1942 return res;
1943 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001944 }
1945 }
1946
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001947 // Request thread needs to know to avoid using repeat-last-settings protocol
1948 // across configure_streams() calls
1949 mRequestThread->configurationComplete();
1950
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07001951 // Boost priority of request thread for high speed recording to SCHED_FIFO
1952 if (mIsConstrainedHighSpeedConfiguration) {
1953 pid_t requestThreadTid = mRequestThread->getTid();
1954 res = requestPriority(getpid(), requestThreadTid,
1955 kConstrainedHighSpeedThreadPriority, true);
1956 if (res != OK) {
1957 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
1958 strerror(-res), res);
1959 } else {
1960 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
1961 }
1962 } else {
1963 // TODO: Set/restore normal priority for normal use cases
1964 }
1965
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001966 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001967
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001968 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001969
Ruben Brunk183f0562015-08-12 12:55:02 -07001970 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1971 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001972
1973 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1974
Zhijun He0a210512014-07-24 13:45:15 -07001975 // tear down the deleted streams after configure streams.
1976 mDeletedStreams.clear();
1977
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001978 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001979}
1980
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001981status_t Camera3Device::addDummyStreamLocked() {
1982 ATRACE_CALL();
1983 status_t res;
1984
1985 if (mDummyStreamId != NO_STREAM) {
1986 // Should never be adding a second dummy stream when one is already
1987 // active
1988 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1989 __FUNCTION__, mId);
1990 return INVALID_OPERATION;
1991 }
1992
1993 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1994
1995 sp<Camera3OutputStreamInterface> dummyStream =
1996 new Camera3DummyStream(mNextStreamId);
1997
1998 res = mOutputStreams.add(mNextStreamId, dummyStream);
1999 if (res < 0) {
2000 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2001 return res;
2002 }
2003
2004 mDummyStreamId = mNextStreamId;
2005 mNextStreamId++;
2006
2007 return OK;
2008}
2009
2010status_t Camera3Device::tryRemoveDummyStreamLocked() {
2011 ATRACE_CALL();
2012 status_t res;
2013
2014 if (mDummyStreamId == NO_STREAM) return OK;
2015 if (mOutputStreams.size() == 1) return OK;
2016
2017 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
2018
2019 // Ok, have a dummy stream and there's at least one other output stream,
2020 // so remove the dummy
2021
2022 sp<Camera3StreamInterface> deletedStream;
2023 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2024 if (outputStreamIdx == NAME_NOT_FOUND) {
2025 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2026 return INVALID_OPERATION;
2027 }
2028
2029 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2030 mOutputStreams.removeItemsAt(outputStreamIdx);
2031
2032 // Free up the stream endpoint so that it can be used by some other stream
2033 res = deletedStream->disconnect();
2034 if (res != OK) {
2035 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2036 // fall through since we want to still list the stream as deleted.
2037 }
2038 mDeletedStreams.add(deletedStream);
2039 mDummyStreamId = NO_STREAM;
2040
2041 return res;
2042}
2043
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002044void Camera3Device::setErrorState(const char *fmt, ...) {
2045 Mutex::Autolock l(mLock);
2046 va_list args;
2047 va_start(args, fmt);
2048
2049 setErrorStateLockedV(fmt, args);
2050
2051 va_end(args);
2052}
2053
2054void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2055 Mutex::Autolock l(mLock);
2056 setErrorStateLockedV(fmt, args);
2057}
2058
2059void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2060 va_list args;
2061 va_start(args, fmt);
2062
2063 setErrorStateLockedV(fmt, args);
2064
2065 va_end(args);
2066}
2067
2068void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002069 // Print out all error messages to log
2070 String8 errorCause = String8::formatV(fmt, args);
2071 ALOGE("Camera %d: %s", mId, errorCause.string());
2072
2073 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002074 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002075
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002076 mErrorCause = errorCause;
2077
2078 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002079 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002080
2081 // Notify upstream about a device error
2082 if (mListener != NULL) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002083 mListener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002084 CaptureResultExtras());
2085 }
2086
2087 // Save stack trace. View by dumping it later.
2088 CameraTraces::saveTrace();
2089 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002090}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002091
2092/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002093 * In-flight request management
2094 */
2095
Jianing Weicb0652e2014-03-12 18:29:36 -07002096status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002097 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2098 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002099 ATRACE_CALL();
2100 Mutex::Autolock l(mInFlightLock);
2101
2102 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002103 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2104 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002105 if (res < 0) return res;
2106
2107 return OK;
2108}
2109
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002110void Camera3Device::returnOutputBuffers(
2111 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2112 nsecs_t timestamp) {
2113 for (size_t i = 0; i < numBuffers; i++)
2114 {
2115 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2116 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2117 // Note: stream may be deallocated at this point, if this buffer was
2118 // the last reference to it.
2119 if (res != OK) {
2120 ALOGE("Can't return buffer to its stream: %s (%d)",
2121 strerror(-res), res);
2122 }
2123 }
2124}
2125
2126
2127void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2128
2129 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2130 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2131
2132 nsecs_t sensorTimestamp = request.sensorTimestamp;
2133 nsecs_t shutterTimestamp = request.shutterTimestamp;
2134
2135 // Check if it's okay to remove the request from InFlightMap:
2136 // In the case of a successful request:
2137 // all input and output buffers, all result metadata, shutter callback
2138 // arrived.
2139 // In the case of a unsuccessful request:
2140 // all input and output buffers arrived.
2141 if (request.numBuffersLeft == 0 &&
2142 (request.requestStatus != OK ||
2143 (request.haveResultMetadata && shutterTimestamp != 0))) {
2144 ATRACE_ASYNC_END("frame capture", frameNumber);
2145
2146 // Sanity check - if sensor timestamp matches shutter timestamp
2147 if (request.requestStatus == OK &&
2148 sensorTimestamp != shutterTimestamp) {
2149 SET_ERR("sensor timestamp (%" PRId64
2150 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2151 sensorTimestamp, frameNumber, shutterTimestamp);
2152 }
2153
2154 // for an unsuccessful request, it may have pending output buffers to
2155 // return.
2156 assert(request.requestStatus != OK ||
2157 request.pendingOutputBuffers.size() == 0);
2158 returnOutputBuffers(request.pendingOutputBuffers.array(),
2159 request.pendingOutputBuffers.size(), 0);
2160
2161 mInFlightMap.removeItemsAt(idx, 1);
2162
2163 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2164 }
2165
2166 // Sanity check - if we have too many in-flight frames, something has
2167 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002168 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002169 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002170 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2171 kInFlightWarnLimitHighSpeed) {
2172 CLOGE("In-flight list too large for high speed configuration: %zu",
2173 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002174 }
2175}
2176
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002177void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2178 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2179 if (result == nullptr) return;
2180
2181 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2182 (int32_t*)&frameNumber, 1) != OK) {
2183 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2184 return;
2185 }
2186
2187 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2188 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2189 return;
2190 }
2191
2192 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2193
2194 // Valid result, insert into queue
2195 List<CaptureResult>::iterator queuedResult =
2196 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2197 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2198 ", burstId = %" PRId32, __FUNCTION__,
2199 queuedResult->mResultExtras.requestId,
2200 queuedResult->mResultExtras.frameNumber,
2201 queuedResult->mResultExtras.burstId);
2202
2203 mResultSignal.signal();
2204}
2205
2206
2207void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2208 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2209 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2210 Mutex::Autolock l(mOutputLock);
2211
2212 CaptureResult captureResult;
2213 captureResult.mResultExtras = resultExtras;
2214 captureResult.mMetadata = partialResult;
2215
2216 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2217}
2218
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002219
2220void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2221 CaptureResultExtras &resultExtras,
2222 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002223 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002224 bool reprocess,
2225 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002226 if (pendingMetadata.isEmpty())
2227 return;
2228
2229 Mutex::Autolock l(mOutputLock);
2230
2231 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002232 if (reprocess) {
2233 if (frameNumber < mNextReprocessResultFrameNumber) {
2234 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002235 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002236 frameNumber, mNextReprocessResultFrameNumber);
2237 return;
2238 }
2239 mNextReprocessResultFrameNumber = frameNumber + 1;
2240 } else {
2241 if (frameNumber < mNextResultFrameNumber) {
2242 SET_ERR("Out-of-order capture result metadata submitted! "
2243 "(got frame number %d, expecting %d)",
2244 frameNumber, mNextResultFrameNumber);
2245 return;
2246 }
2247 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002248 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002249
2250 CaptureResult captureResult;
2251 captureResult.mResultExtras = resultExtras;
2252 captureResult.mMetadata = pendingMetadata;
2253
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002254 // Append any previous partials to form a complete result
2255 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2256 captureResult.mMetadata.append(collectedPartialResult);
2257 }
2258
2259 captureResult.mMetadata.sort();
2260
2261 // Check that there's a timestamp in the result metadata
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002262 camera_metadata_entry entry = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002263 if (entry.count == 0) {
2264 SET_ERR("No timestamp provided by HAL for frame %d!",
2265 frameNumber);
2266 return;
2267 }
2268
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002269 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002270}
2271
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002272/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002273 * Camera HAL device callback methods
2274 */
2275
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002276void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002277 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002278
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002279 status_t res;
2280
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002281 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002282 if (result->result == NULL && result->num_output_buffers == 0 &&
2283 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002284 SET_ERR("No result data provided by HAL for frame %d",
2285 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002286 return;
2287 }
Zhijun He204e3292014-07-14 17:09:23 -07002288
2289 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2290 // partial_result to 1 when metadata is included in this result.
2291 if (!mUsePartialResult &&
2292 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2293 result->result != NULL &&
2294 result->partial_result != 1) {
2295 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2296 " if partial result is not supported",
2297 frameNumber, result->partial_result);
2298 return;
2299 }
2300
2301 bool isPartialResult = false;
2302 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002303 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002304 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002305
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002306 // Get shutter timestamp and resultExtras from list of in-flight requests,
2307 // where it was added by the shutter notification for this frame. If the
2308 // shutter timestamp isn't received yet, append the output buffers to the
2309 // in-flight request and they will be returned when the shutter timestamp
2310 // arrives. Update the in-flight status and remove the in-flight entry if
2311 // all result data and shutter timestamp have been received.
2312 nsecs_t shutterTimestamp = 0;
2313
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002314 {
2315 Mutex::Autolock l(mInFlightLock);
2316 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2317 if (idx == NAME_NOT_FOUND) {
2318 SET_ERR("Unknown frame number for capture result: %d",
2319 frameNumber);
2320 return;
2321 }
2322 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002323 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2324 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2325 ", partialResultCount = %d",
2326 __FUNCTION__, request.resultExtras.requestId,
2327 request.resultExtras.frameNumber, request.resultExtras.burstId,
2328 result->partial_result);
2329 // Always update the partial count to the latest one if it's not 0
2330 // (buffers only). When framework aggregates adjacent partial results
2331 // into one, the latest partial count will be used.
2332 if (result->partial_result != 0)
2333 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002334
2335 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002336 if (mUsePartialResult && result->result != NULL) {
2337 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2338 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2339 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2340 " the range of [1, %d] when metadata is included in the result",
2341 frameNumber, result->partial_result, mNumPartialResults);
2342 return;
2343 }
2344 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002345 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002346 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002347 }
Zhijun He204e3292014-07-14 17:09:23 -07002348 } else {
2349 camera_metadata_ro_entry_t partialResultEntry;
2350 res = find_camera_metadata_ro_entry(result->result,
2351 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2352 if (res != NAME_NOT_FOUND &&
2353 partialResultEntry.count > 0 &&
2354 partialResultEntry.data.u8[0] ==
2355 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2356 // A partial result. Flag this as such, and collect this
2357 // set of metadata into the in-flight entry.
2358 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002359 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002360 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002361 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002362 ANDROID_QUIRKS_PARTIAL_RESULT);
2363 }
2364 }
2365
2366 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002367 // Send partial capture result
2368 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2369 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002370 }
2371 }
2372
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002373 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002374 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002375
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002376 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002377 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002378 if (request.haveResultMetadata) {
2379 SET_ERR("Called multiple times with metadata for frame %d",
2380 frameNumber);
2381 return;
2382 }
Zhijun He204e3292014-07-14 17:09:23 -07002383 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002384 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002385 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002386 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002387 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002388 request.haveResultMetadata = true;
2389 }
2390
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002391 uint32_t numBuffersReturned = result->num_output_buffers;
2392 if (result->input_buffer != NULL) {
2393 if (hasInputBufferInRequest) {
2394 numBuffersReturned += 1;
2395 } else {
2396 ALOGW("%s: Input buffer should be NULL if there is no input"
2397 " buffer sent in the request",
2398 __FUNCTION__);
2399 }
2400 }
2401 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002402 if (request.numBuffersLeft < 0) {
2403 SET_ERR("Too many buffers returned for frame %d",
2404 frameNumber);
2405 return;
2406 }
2407
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002408 camera_metadata_ro_entry_t entry;
2409 res = find_camera_metadata_ro_entry(result->result,
2410 ANDROID_SENSOR_TIMESTAMP, &entry);
2411 if (res == OK && entry.count == 1) {
2412 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002413 }
2414
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002415 // If shutter event isn't received yet, append the output buffers to
2416 // the in-flight request. Otherwise, return the output buffers to
2417 // streams.
2418 if (shutterTimestamp == 0) {
2419 request.pendingOutputBuffers.appendArray(result->output_buffers,
2420 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002421 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002422 returnOutputBuffers(result->output_buffers,
2423 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002424 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002425
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002426 if (result->result != NULL && !isPartialResult) {
2427 if (shutterTimestamp == 0) {
2428 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002429 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002430 } else {
2431 CameraMetadata metadata;
2432 metadata = result->result;
2433 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002434 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2435 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002436 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002437 }
2438
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002439 removeInFlightRequestIfReadyLocked(idx);
2440 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002441
Zhijun Hef0d962a2014-06-30 10:24:11 -07002442 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002443 if (hasInputBufferInRequest) {
2444 Camera3Stream *stream =
2445 Camera3Stream::cast(result->input_buffer->stream);
2446 res = stream->returnInputBuffer(*(result->input_buffer));
2447 // Note: stream may be deallocated at this point, if this buffer was the
2448 // last reference to it.
2449 if (res != OK) {
2450 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2451 " its stream:%s (%d)", __FUNCTION__,
2452 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002453 }
2454 } else {
2455 ALOGW("%s: Input buffer should be NULL if there is no input"
2456 " buffer sent in the request, skipping input buffer return.",
2457 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002458 }
2459 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002460}
2461
2462void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002463 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002464 NotificationListener *listener;
2465 {
2466 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002467 listener = mListener;
2468 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002469
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002470 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002471 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002472 return;
2473 }
2474
2475 switch (msg->type) {
2476 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002477 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002478 break;
2479 }
2480 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002481 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002482 break;
2483 }
2484 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002485 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002486 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002487 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002488}
2489
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002490void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2491 NotificationListener *listener) {
2492
2493 // Map camera HAL error codes to ICameraDeviceCallback error codes
2494 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002495 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002496 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002497 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002498 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002499 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002500 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002501 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002502 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002503 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002504 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002505 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002506 };
2507
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002508 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002509 ((msg.error_code >= 0) &&
2510 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2511 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002512 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002513
2514 int streamId = 0;
2515 if (msg.error_stream != NULL) {
2516 Camera3Stream *stream =
2517 Camera3Stream::cast(msg.error_stream);
2518 streamId = stream->getId();
2519 }
2520 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2521 mId, __FUNCTION__, msg.frame_number,
2522 streamId, msg.error_code);
2523
2524 CaptureResultExtras resultExtras;
2525 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002526 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002527 // SET_ERR calls notifyError
2528 SET_ERR("Camera HAL reported serious device error");
2529 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002530 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2531 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2532 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002533 {
2534 Mutex::Autolock l(mInFlightLock);
2535 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2536 if (idx >= 0) {
2537 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2538 r.requestStatus = msg.error_code;
2539 resultExtras = r.resultExtras;
2540 } else {
2541 resultExtras.frameNumber = msg.frame_number;
2542 ALOGE("Camera %d: %s: cannot find in-flight request on "
2543 "frame %" PRId64 " error", mId, __FUNCTION__,
2544 resultExtras.frameNumber);
2545 }
2546 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002547 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002548 if (listener != NULL) {
2549 listener->notifyError(errorCode, resultExtras);
2550 } else {
2551 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2552 }
2553 break;
2554 default:
2555 // SET_ERR calls notifyError
2556 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2557 break;
2558 }
2559}
2560
2561void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2562 NotificationListener *listener) {
2563 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002564
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002565 // Set timestamp for the request in the in-flight tracking
2566 // and get the request ID to send upstream
2567 {
2568 Mutex::Autolock l(mInFlightLock);
2569 idx = mInFlightMap.indexOfKey(msg.frame_number);
2570 if (idx >= 0) {
2571 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002572
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002573 // Verify ordering of shutter notifications
2574 {
2575 Mutex::Autolock l(mOutputLock);
2576 // TODO: need to track errors for tighter bounds on expected frame number.
2577 if (r.hasInputBuffer) {
2578 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2579 SET_ERR("Shutter notification out-of-order. Expected "
2580 "notification for frame %d, got frame %d",
2581 mNextReprocessShutterFrameNumber, msg.frame_number);
2582 return;
2583 }
2584 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2585 } else {
2586 if (msg.frame_number < mNextShutterFrameNumber) {
2587 SET_ERR("Shutter notification out-of-order. Expected "
2588 "notification for frame %d, got frame %d",
2589 mNextShutterFrameNumber, msg.frame_number);
2590 return;
2591 }
2592 mNextShutterFrameNumber = msg.frame_number + 1;
2593 }
2594 }
2595
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002596 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2597 mId, __FUNCTION__,
2598 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2599 // Call listener, if any
2600 if (listener != NULL) {
2601 listener->notifyShutter(r.resultExtras, msg.timestamp);
2602 }
2603
2604 r.shutterTimestamp = msg.timestamp;
2605
2606 // send pending result and buffers
2607 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002608 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002609 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002610 returnOutputBuffers(r.pendingOutputBuffers.array(),
2611 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2612 r.pendingOutputBuffers.clear();
2613
2614 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002615 }
2616 }
2617 if (idx < 0) {
2618 SET_ERR("Shutter notification for non-existent frame number %d",
2619 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002620 }
2621}
2622
2623
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002624CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002625 ALOGV("%s", __FUNCTION__);
2626
Igor Murashkin1e479c02013-09-06 16:55:14 -07002627 CameraMetadata retVal;
2628
2629 if (mRequestThread != NULL) {
2630 retVal = mRequestThread->getLatestRequest();
2631 }
2632
Igor Murashkin1e479c02013-09-06 16:55:14 -07002633 return retVal;
2634}
2635
Jianing Weicb0652e2014-03-12 18:29:36 -07002636
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002637/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002638 * RequestThread inner class methods
2639 */
2640
2641Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002642 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002643 camera3_device_t *hal3Device,
2644 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002645 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002646 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002647 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002648 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002649 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002650 mReconfigured(false),
2651 mDoPause(false),
2652 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002653 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002654 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002655 mCurrentAfTriggerId(0),
2656 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002657 mRepeatingLastFrameNumber(
2658 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002659 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002660 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002661}
2662
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002663void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002664 NotificationListener *listener) {
2665 Mutex::Autolock l(mRequestLock);
2666 mListener = listener;
2667}
2668
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002669void Camera3Device::RequestThread::configurationComplete() {
2670 Mutex::Autolock l(mRequestLock);
2671 mReconfigured = true;
2672}
2673
Jianing Wei90e59c92014-03-12 18:29:36 -07002674status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002675 List<sp<CaptureRequest> > &requests,
2676 /*out*/
2677 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002678 Mutex::Autolock l(mRequestLock);
2679 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2680 ++it) {
2681 mRequestQueue.push_back(*it);
2682 }
2683
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002684 if (lastFrameNumber != NULL) {
2685 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2686 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2687 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2688 *lastFrameNumber);
2689 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002690
Jianing Wei90e59c92014-03-12 18:29:36 -07002691 unpauseForNewRequests();
2692
2693 return OK;
2694}
2695
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002696
2697status_t Camera3Device::RequestThread::queueTrigger(
2698 RequestTrigger trigger[],
2699 size_t count) {
2700
2701 Mutex::Autolock l(mTriggerMutex);
2702 status_t ret;
2703
2704 for (size_t i = 0; i < count; ++i) {
2705 ret = queueTriggerLocked(trigger[i]);
2706
2707 if (ret != OK) {
2708 return ret;
2709 }
2710 }
2711
2712 return OK;
2713}
2714
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002715int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2716 sp<Camera3Device> d = device.promote();
2717 if (d != NULL) return d->mId;
2718 return 0;
2719}
2720
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002721status_t Camera3Device::RequestThread::queueTriggerLocked(
2722 RequestTrigger trigger) {
2723
2724 uint32_t tag = trigger.metadataTag;
2725 ssize_t index = mTriggerMap.indexOfKey(tag);
2726
2727 switch (trigger.getTagType()) {
2728 case TYPE_BYTE:
2729 // fall-through
2730 case TYPE_INT32:
2731 break;
2732 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002733 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2734 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002735 return INVALID_OPERATION;
2736 }
2737
2738 /**
2739 * Collect only the latest trigger, since we only have 1 field
2740 * in the request settings per trigger tag, and can't send more than 1
2741 * trigger per request.
2742 */
2743 if (index != NAME_NOT_FOUND) {
2744 mTriggerMap.editValueAt(index) = trigger;
2745 } else {
2746 mTriggerMap.add(tag, trigger);
2747 }
2748
2749 return OK;
2750}
2751
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002752status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002753 const RequestList &requests,
2754 /*out*/
2755 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002756 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002757 if (lastFrameNumber != NULL) {
2758 *lastFrameNumber = mRepeatingLastFrameNumber;
2759 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002760 mRepeatingRequests.clear();
2761 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2762 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002763
2764 unpauseForNewRequests();
2765
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002766 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002767 return OK;
2768}
2769
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002770bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2771 if (mRepeatingRequests.empty()) {
2772 return false;
2773 }
2774 int32_t requestId = requestIn->mResultExtras.requestId;
2775 const RequestList &repeatRequests = mRepeatingRequests;
2776 // All repeating requests are guaranteed to have same id so only check first quest
2777 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2778 return (firstRequest->mResultExtras.requestId == requestId);
2779}
2780
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002781status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002782 Mutex::Autolock l(mRequestLock);
2783 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002784 if (lastFrameNumber != NULL) {
2785 *lastFrameNumber = mRepeatingLastFrameNumber;
2786 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002787 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002788 return OK;
2789}
2790
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002791status_t Camera3Device::RequestThread::clear(
2792 NotificationListener *listener,
2793 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002794 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002795 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002796
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002797 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002798
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002799 // Send errors for all requests pending in the request queue, including
2800 // pending repeating requests
2801 if (listener != NULL) {
2802 for (RequestList::iterator it = mRequestQueue.begin();
2803 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002804 // Abort the input buffers for reprocess requests.
2805 if ((*it)->mInputStream != NULL) {
2806 camera3_stream_buffer_t inputBuffer;
2807 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2808 if (res != OK) {
2809 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2810 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2811 } else {
2812 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2813 if (res != OK) {
2814 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2815 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2816 }
2817 }
2818 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002819 // Set the frame number this request would have had, if it
2820 // had been submitted; this frame number will not be reused.
2821 // The requestId and burstId fields were set when the request was
2822 // submitted originally (in convertMetadataListToRequestListLocked)
2823 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002824 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002825 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002826 }
2827 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002828 mRequestQueue.clear();
2829 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002830 if (lastFrameNumber != NULL) {
2831 *lastFrameNumber = mRepeatingLastFrameNumber;
2832 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002833 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002834 return OK;
2835}
2836
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002837status_t Camera3Device::RequestThread::flush() {
2838 ATRACE_CALL();
2839 Mutex::Autolock l(mFlushLock);
2840
2841 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2842 return mHal3Device->ops->flush(mHal3Device);
2843 }
2844
2845 return -ENOTSUP;
2846}
2847
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002848void Camera3Device::RequestThread::setPaused(bool paused) {
2849 Mutex::Autolock l(mPauseLock);
2850 mDoPause = paused;
2851 mDoPauseSignal.signal();
2852}
2853
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002854status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2855 int32_t requestId, nsecs_t timeout) {
2856 Mutex::Autolock l(mLatestRequestMutex);
2857 status_t res;
2858 while (mLatestRequestId != requestId) {
2859 nsecs_t startTime = systemTime();
2860
2861 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2862 if (res != OK) return res;
2863
2864 timeout -= (systemTime() - startTime);
2865 }
2866
2867 return OK;
2868}
2869
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002870void Camera3Device::RequestThread::requestExit() {
2871 // Call parent to set up shutdown
2872 Thread::requestExit();
2873 // The exit from any possible waits
2874 mDoPauseSignal.signal();
2875 mRequestSignal.signal();
2876}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002877
Chien-Yu Chend196d612015-06-22 19:49:01 -07002878
2879/**
2880 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2881 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2882 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2883 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2884 * request.
2885 */
2886void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2887 request->mAeTriggerCancelOverride.applyAeLock = false;
2888 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2889
2890 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2891 return;
2892 }
2893
2894 camera_metadata_entry_t aePrecaptureTrigger =
2895 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2896 if (aePrecaptureTrigger.count > 0 &&
2897 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2898 // Always override CANCEL to IDLE
2899 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2900 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2901 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2902 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2903 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2904
2905 if (mAeLockAvailable == true) {
2906 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2907 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2908 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2909 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2910 request->mAeTriggerCancelOverride.applyAeLock = true;
2911 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2912 }
2913 }
2914 }
2915}
2916
2917/**
2918 * Override result metadata for cancelling AE precapture trigger applied in
2919 * handleAePrecaptureCancelRequest().
2920 */
2921void Camera3Device::overrideResultForPrecaptureCancel(
2922 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2923 if (aeTriggerCancelOverride.applyAeLock) {
2924 // Only devices <= v3.2 should have this override
2925 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2926 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2927 }
2928
2929 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2930 // Only devices <= v3.2 should have this override
2931 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2932 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2933 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2934 }
2935}
2936
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002937bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002938 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002939 status_t res;
2940
2941 // Handle paused state.
2942 if (waitIfPaused()) {
2943 return true;
2944 }
2945
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002946 // Wait for the next batch of requests.
2947 waitForNextRequestBatch();
2948 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002949 return true;
2950 }
2951
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002952 // Get the latest request ID, if any
2953 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002954 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002955 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002956 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002957 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002958 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002959 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
2960 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002961 }
2962
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002963 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002964 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002965 if (res == TIMED_OUT) {
2966 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002967 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002968 return true;
2969 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002970 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002971 return false;
2972 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002973
Zhijun Hecc27e112013-10-03 16:12:43 -07002974 // Inform waitUntilRequestProcessed thread of a new request ID
2975 {
2976 Mutex::Autolock al(mLatestRequestMutex);
2977
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002978 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07002979 mLatestRequestSignal.signal();
2980 }
2981
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002982 // Submit a batch of requests to HAL.
2983 // Use flush lock only when submitting multilple requests in a batch.
2984 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
2985 // which may take a long time to finish so synchronizing flush() and
2986 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
2987 // For now, only synchronize for high speed recording and we should figure something out for
2988 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002989 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002990
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002991 if (useFlushLock) {
2992 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002993 }
2994
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002995 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002996 mNextRequests.size());
2997 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002998 // Submit request and block until ready for next one
2999 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3000 ATRACE_BEGIN("camera3->process_capture_request");
3001 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3002 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003003
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003004 if (res != OK) {
3005 // Should only get a failure here for malformed requests or device-level
3006 // errors, so consider all errors fatal. Bad metadata failures should
3007 // come through notify.
3008 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3009 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3010 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003011 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003012 if (useFlushLock) {
3013 mFlushLock.unlock();
3014 }
3015 return false;
3016 }
3017
3018 // Mark that the request has be submitted successfully.
3019 nextRequest.submitted = true;
3020
3021 // Update the latest request sent to HAL
3022 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3023 Mutex::Autolock al(mLatestRequestMutex);
3024
3025 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3026 mLatestRequest.acquire(cloned);
3027 }
3028
3029 if (nextRequest.halRequest.settings != NULL) {
3030 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3031 }
3032
3033 // Remove any previously queued triggers (after unlock)
3034 res = removeTriggers(mPrevRequest);
3035 if (res != OK) {
3036 SET_ERR("RequestThread: Unable to remove triggers "
3037 "(capture request %d, HAL device: %s (%d)",
3038 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003039 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003040 if (useFlushLock) {
3041 mFlushLock.unlock();
3042 }
3043 return false;
3044 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003045 }
3046
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003047 if (useFlushLock) {
3048 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003049 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003050
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003051 // Unset as current request
3052 {
3053 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003054 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003055 }
3056
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003057 return true;
3058}
3059
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003060status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003061 ATRACE_CALL();
3062
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003063 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003064 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3065 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3066 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3067
3068 // Prepare a request to HAL
3069 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3070
3071 // Insert any queued triggers (before metadata is locked)
3072 status_t res = insertTriggers(captureRequest);
3073
3074 if (res < 0) {
3075 SET_ERR("RequestThread: Unable to insert triggers "
3076 "(capture request %d, HAL device: %s (%d)",
3077 halRequest->frame_number, strerror(-res), res);
3078 return INVALID_OPERATION;
3079 }
3080 int triggerCount = res;
3081 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3082 mPrevTriggers = triggerCount;
3083
3084 // If the request is the same as last, or we had triggers last time
3085 if (mPrevRequest != captureRequest || triggersMixedIn) {
3086 /**
3087 * HAL workaround:
3088 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3089 */
3090 res = addDummyTriggerIds(captureRequest);
3091 if (res != OK) {
3092 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3093 "(capture request %d, HAL device: %s (%d)",
3094 halRequest->frame_number, strerror(-res), res);
3095 return INVALID_OPERATION;
3096 }
3097
3098 /**
3099 * The request should be presorted so accesses in HAL
3100 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3101 */
3102 captureRequest->mSettings.sort();
3103 halRequest->settings = captureRequest->mSettings.getAndLock();
3104 mPrevRequest = captureRequest;
3105 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3106
3107 IF_ALOGV() {
3108 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3109 find_camera_metadata_ro_entry(
3110 halRequest->settings,
3111 ANDROID_CONTROL_AF_TRIGGER,
3112 &e
3113 );
3114 if (e.count > 0) {
3115 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3116 __FUNCTION__,
3117 halRequest->frame_number,
3118 e.data.u8[0]);
3119 }
3120 }
3121 } else {
3122 // leave request.settings NULL to indicate 'reuse latest given'
3123 ALOGVV("%s: Request settings are REUSED",
3124 __FUNCTION__);
3125 }
3126
3127 uint32_t totalNumBuffers = 0;
3128
3129 // Fill in buffers
3130 if (captureRequest->mInputStream != NULL) {
3131 halRequest->input_buffer = &captureRequest->mInputBuffer;
3132 totalNumBuffers += 1;
3133 } else {
3134 halRequest->input_buffer = NULL;
3135 }
3136
3137 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3138 captureRequest->mOutputStreams.size());
3139 halRequest->output_buffers = outputBuffers->array();
3140 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3141 res = captureRequest->mOutputStreams.editItemAt(i)->
3142 getBuffer(&outputBuffers->editItemAt(i));
3143 if (res != OK) {
3144 // Can't get output buffer from gralloc queue - this could be due to
3145 // abandoned queue or other consumer misbehavior, so not a fatal
3146 // error
3147 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3148 " %s (%d)", strerror(-res), res);
3149
3150 return TIMED_OUT;
3151 }
3152 halRequest->num_output_buffers++;
3153 }
3154 totalNumBuffers += halRequest->num_output_buffers;
3155
3156 // Log request in the in-flight queue
3157 sp<Camera3Device> parent = mParent.promote();
3158 if (parent == NULL) {
3159 // Should not happen, and nowhere to send errors to, so just log it
3160 CLOGE("RequestThread: Parent is gone");
3161 return INVALID_OPERATION;
3162 }
3163 res = parent->registerInFlight(halRequest->frame_number,
3164 totalNumBuffers, captureRequest->mResultExtras,
3165 /*hasInput*/halRequest->input_buffer != NULL,
3166 captureRequest->mAeTriggerCancelOverride);
3167 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3168 ", burstId = %" PRId32 ".",
3169 __FUNCTION__,
3170 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3171 captureRequest->mResultExtras.burstId);
3172 if (res != OK) {
3173 SET_ERR("RequestThread: Unable to register new in-flight request:"
3174 " %s (%d)", strerror(-res), res);
3175 return INVALID_OPERATION;
3176 }
3177 }
3178
3179 return OK;
3180}
3181
Igor Murashkin1e479c02013-09-06 16:55:14 -07003182CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3183 Mutex::Autolock al(mLatestRequestMutex);
3184
3185 ALOGV("RequestThread::%s", __FUNCTION__);
3186
3187 return mLatestRequest;
3188}
3189
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003190bool Camera3Device::RequestThread::isStreamPending(
3191 sp<Camera3StreamInterface>& stream) {
3192 Mutex::Autolock l(mRequestLock);
3193
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003194 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003195 if (!nextRequest.submitted) {
3196 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3197 if (stream == s) return true;
3198 }
3199 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003200 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003201 }
3202
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003203 for (const auto& request : mRequestQueue) {
3204 for (const auto& s : request->mOutputStreams) {
3205 if (stream == s) return true;
3206 }
3207 if (stream == request->mInputStream) return true;
3208 }
3209
3210 for (const auto& request : mRepeatingRequests) {
3211 for (const auto& s : request->mOutputStreams) {
3212 if (stream == s) return true;
3213 }
3214 if (stream == request->mInputStream) return true;
3215 }
3216
3217 return false;
3218}
Jianing Weicb0652e2014-03-12 18:29:36 -07003219
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003220void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3221 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003222 return;
3223 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003224
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003225 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003226 // Skip the ones that have been submitted successfully.
3227 if (nextRequest.submitted) {
3228 continue;
3229 }
3230
3231 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3232 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3233 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3234
3235 if (halRequest->settings != NULL) {
3236 captureRequest->mSettings.unlock(halRequest->settings);
3237 }
3238
3239 if (captureRequest->mInputStream != NULL) {
3240 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3241 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3242 }
3243
3244 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3245 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3246 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3247 }
3248
3249 if (sendRequestError) {
3250 Mutex::Autolock l(mRequestLock);
3251 if (mListener != NULL) {
3252 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003253 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003254 captureRequest->mResultExtras);
3255 }
3256 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003257 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003258
3259 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003260 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003261}
3262
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003263void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003264 // Optimized a bit for the simple steady-state case (single repeating
3265 // request), to avoid putting that request in the queue temporarily.
3266 Mutex::Autolock l(mRequestLock);
3267
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003268 assert(mNextRequests.empty());
3269
3270 NextRequest nextRequest;
3271 nextRequest.captureRequest = waitForNextRequestLocked();
3272 if (nextRequest.captureRequest == nullptr) {
3273 return;
3274 }
3275
3276 nextRequest.halRequest = camera3_capture_request_t();
3277 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003278 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003279
3280 // Wait for additional requests
3281 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3282
3283 for (size_t i = 1; i < batchSize; i++) {
3284 NextRequest additionalRequest;
3285 additionalRequest.captureRequest = waitForNextRequestLocked();
3286 if (additionalRequest.captureRequest == nullptr) {
3287 break;
3288 }
3289
3290 additionalRequest.halRequest = camera3_capture_request_t();
3291 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003292 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003293 }
3294
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003295 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003296 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003297 mNextRequests.size(), batchSize);
3298 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003299 }
3300
3301 return;
3302}
3303
3304sp<Camera3Device::CaptureRequest>
3305 Camera3Device::RequestThread::waitForNextRequestLocked() {
3306 status_t res;
3307 sp<CaptureRequest> nextRequest;
3308
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003309 while (mRequestQueue.empty()) {
3310 if (!mRepeatingRequests.empty()) {
3311 // Always atomically enqueue all requests in a repeating request
3312 // list. Guarantees a complete in-sequence set of captures to
3313 // application.
3314 const RequestList &requests = mRepeatingRequests;
3315 RequestList::const_iterator firstRequest =
3316 requests.begin();
3317 nextRequest = *firstRequest;
3318 mRequestQueue.insert(mRequestQueue.end(),
3319 ++firstRequest,
3320 requests.end());
3321 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003322
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003323 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003324
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003325 break;
3326 }
3327
3328 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3329
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003330 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3331 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003332 Mutex::Autolock pl(mPauseLock);
3333 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003334 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003335 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003336 // Let the tracker know
3337 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3338 if (statusTracker != 0) {
3339 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3340 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003341 }
3342 // Stop waiting for now and let thread management happen
3343 return NULL;
3344 }
3345 }
3346
3347 if (nextRequest == NULL) {
3348 // Don't have a repeating request already in hand, so queue
3349 // must have an entry now.
3350 RequestList::iterator firstRequest =
3351 mRequestQueue.begin();
3352 nextRequest = *firstRequest;
3353 mRequestQueue.erase(firstRequest);
3354 }
3355
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003356 // In case we've been unpaused by setPaused clearing mDoPause, need to
3357 // update internal pause state (capture/setRepeatingRequest unpause
3358 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003359 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003360 if (mPaused) {
3361 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3362 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3363 if (statusTracker != 0) {
3364 statusTracker->markComponentActive(mStatusId);
3365 }
3366 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003367 mPaused = false;
3368
3369 // Check if we've reconfigured since last time, and reset the preview
3370 // request if so. Can't use 'NULL request == repeat' across configure calls.
3371 if (mReconfigured) {
3372 mPrevRequest.clear();
3373 mReconfigured = false;
3374 }
3375
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003376 if (nextRequest != NULL) {
3377 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003378 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3379 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003380
3381 // Since RequestThread::clear() removes buffers from the input stream,
3382 // get the right buffer here before unlocking mRequestLock
3383 if (nextRequest->mInputStream != NULL) {
3384 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3385 if (res != OK) {
3386 // Can't get input buffer from gralloc queue - this could be due to
3387 // disconnected queue or other producer misbehavior, so not a fatal
3388 // error
3389 ALOGE("%s: Can't get input buffer, skipping request:"
3390 " %s (%d)", __FUNCTION__, strerror(-res), res);
3391 if (mListener != NULL) {
3392 mListener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003393 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003394 nextRequest->mResultExtras);
3395 }
3396 return NULL;
3397 }
3398 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003399 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003400
3401 handleAePrecaptureCancelRequest(nextRequest);
3402
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003403 return nextRequest;
3404}
3405
3406bool Camera3Device::RequestThread::waitIfPaused() {
3407 status_t res;
3408 Mutex::Autolock l(mPauseLock);
3409 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003410 if (mPaused == false) {
3411 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003412 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3413 // Let the tracker know
3414 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3415 if (statusTracker != 0) {
3416 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3417 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003418 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003419
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003420 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003421 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003422 return true;
3423 }
3424 }
3425 // We don't set mPaused to false here, because waitForNextRequest needs
3426 // to further manage the paused state in case of starvation.
3427 return false;
3428}
3429
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003430void Camera3Device::RequestThread::unpauseForNewRequests() {
3431 // With work to do, mark thread as unpaused.
3432 // If paused by request (setPaused), don't resume, to avoid
3433 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003434 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003435 Mutex::Autolock p(mPauseLock);
3436 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003437 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3438 if (mPaused) {
3439 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3440 if (statusTracker != 0) {
3441 statusTracker->markComponentActive(mStatusId);
3442 }
3443 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003444 mPaused = false;
3445 }
3446}
3447
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003448void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3449 sp<Camera3Device> parent = mParent.promote();
3450 if (parent != NULL) {
3451 va_list args;
3452 va_start(args, fmt);
3453
3454 parent->setErrorStateV(fmt, args);
3455
3456 va_end(args);
3457 }
3458}
3459
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003460status_t Camera3Device::RequestThread::insertTriggers(
3461 const sp<CaptureRequest> &request) {
3462
3463 Mutex::Autolock al(mTriggerMutex);
3464
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003465 sp<Camera3Device> parent = mParent.promote();
3466 if (parent == NULL) {
3467 CLOGE("RequestThread: Parent is gone");
3468 return DEAD_OBJECT;
3469 }
3470
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003471 CameraMetadata &metadata = request->mSettings;
3472 size_t count = mTriggerMap.size();
3473
3474 for (size_t i = 0; i < count; ++i) {
3475 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003476 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003477
3478 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3479 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3480 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003481 if (isAeTrigger) {
3482 request->mResultExtras.precaptureTriggerId = triggerId;
3483 mCurrentPreCaptureTriggerId = triggerId;
3484 } else {
3485 request->mResultExtras.afTriggerId = triggerId;
3486 mCurrentAfTriggerId = triggerId;
3487 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003488 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3489 continue; // Trigger ID tag is deprecated since device HAL 3.2
3490 }
3491 }
3492
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003493 camera_metadata_entry entry = metadata.find(tag);
3494
3495 if (entry.count > 0) {
3496 /**
3497 * Already has an entry for this trigger in the request.
3498 * Rewrite it with our requested trigger value.
3499 */
3500 RequestTrigger oldTrigger = trigger;
3501
3502 oldTrigger.entryValue = entry.data.u8[0];
3503
3504 mTriggerReplacedMap.add(tag, oldTrigger);
3505 } else {
3506 /**
3507 * More typical, no trigger entry, so we just add it
3508 */
3509 mTriggerRemovedMap.add(tag, trigger);
3510 }
3511
3512 status_t res;
3513
3514 switch (trigger.getTagType()) {
3515 case TYPE_BYTE: {
3516 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3517 res = metadata.update(tag,
3518 &entryValue,
3519 /*count*/1);
3520 break;
3521 }
3522 case TYPE_INT32:
3523 res = metadata.update(tag,
3524 &trigger.entryValue,
3525 /*count*/1);
3526 break;
3527 default:
3528 ALOGE("%s: Type not supported: 0x%x",
3529 __FUNCTION__,
3530 trigger.getTagType());
3531 return INVALID_OPERATION;
3532 }
3533
3534 if (res != OK) {
3535 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3536 ", value %d", __FUNCTION__, trigger.getTagName(),
3537 trigger.entryValue);
3538 return res;
3539 }
3540
3541 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3542 trigger.getTagName(),
3543 trigger.entryValue);
3544 }
3545
3546 mTriggerMap.clear();
3547
3548 return count;
3549}
3550
3551status_t Camera3Device::RequestThread::removeTriggers(
3552 const sp<CaptureRequest> &request) {
3553 Mutex::Autolock al(mTriggerMutex);
3554
3555 CameraMetadata &metadata = request->mSettings;
3556
3557 /**
3558 * Replace all old entries with their old values.
3559 */
3560 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3561 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3562
3563 status_t res;
3564
3565 uint32_t tag = trigger.metadataTag;
3566 switch (trigger.getTagType()) {
3567 case TYPE_BYTE: {
3568 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3569 res = metadata.update(tag,
3570 &entryValue,
3571 /*count*/1);
3572 break;
3573 }
3574 case TYPE_INT32:
3575 res = metadata.update(tag,
3576 &trigger.entryValue,
3577 /*count*/1);
3578 break;
3579 default:
3580 ALOGE("%s: Type not supported: 0x%x",
3581 __FUNCTION__,
3582 trigger.getTagType());
3583 return INVALID_OPERATION;
3584 }
3585
3586 if (res != OK) {
3587 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3588 ", trigger value %d", __FUNCTION__,
3589 trigger.getTagName(), trigger.entryValue);
3590 return res;
3591 }
3592 }
3593 mTriggerReplacedMap.clear();
3594
3595 /**
3596 * Remove all new entries.
3597 */
3598 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3599 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3600 status_t res = metadata.erase(trigger.metadataTag);
3601
3602 if (res != OK) {
3603 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3604 ", trigger value %d", __FUNCTION__,
3605 trigger.getTagName(), trigger.entryValue);
3606 return res;
3607 }
3608 }
3609 mTriggerRemovedMap.clear();
3610
3611 return OK;
3612}
3613
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003614status_t Camera3Device::RequestThread::addDummyTriggerIds(
3615 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003616 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003617 static const int32_t dummyTriggerId = 1;
3618 status_t res;
3619
3620 CameraMetadata &metadata = request->mSettings;
3621
3622 // If AF trigger is active, insert a dummy AF trigger ID if none already
3623 // exists
3624 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3625 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3626 if (afTrigger.count > 0 &&
3627 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3628 afId.count == 0) {
3629 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3630 if (res != OK) return res;
3631 }
3632
3633 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3634 // if none already exists
3635 camera_metadata_entry pcTrigger =
3636 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3637 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3638 if (pcTrigger.count > 0 &&
3639 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3640 pcId.count == 0) {
3641 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3642 &dummyTriggerId, 1);
3643 if (res != OK) return res;
3644 }
3645
3646 return OK;
3647}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003648
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003649/**
3650 * PreparerThread inner class methods
3651 */
3652
3653Camera3Device::PreparerThread::PreparerThread() :
3654 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3655}
3656
3657Camera3Device::PreparerThread::~PreparerThread() {
3658 Thread::requestExitAndWait();
3659 if (mCurrentStream != nullptr) {
3660 mCurrentStream->cancelPrepare();
3661 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3662 mCurrentStream.clear();
3663 }
3664 clear();
3665}
3666
Ruben Brunkc78ac262015-08-13 17:58:46 -07003667status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003668 status_t res;
3669
3670 Mutex::Autolock l(mLock);
3671
Ruben Brunkc78ac262015-08-13 17:58:46 -07003672 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003673 if (res == OK) {
3674 // No preparation needed, fire listener right off
3675 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3676 if (mListener) {
3677 mListener->notifyPrepared(stream->getId());
3678 }
3679 return OK;
3680 } else if (res != NOT_ENOUGH_DATA) {
3681 return res;
3682 }
3683
3684 // Need to prepare, start up thread if necessary
3685 if (!mActive) {
3686 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3687 // isn't running
3688 Thread::requestExitAndWait();
3689 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3690 if (res != OK) {
3691 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3692 if (mListener) {
3693 mListener->notifyPrepared(stream->getId());
3694 }
3695 return res;
3696 }
3697 mCancelNow = false;
3698 mActive = true;
3699 ALOGV("%s: Preparer stream started", __FUNCTION__);
3700 }
3701
3702 // queue up the work
3703 mPendingStreams.push_back(stream);
3704 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3705
3706 return OK;
3707}
3708
3709status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003710 Mutex::Autolock l(mLock);
3711
3712 for (const auto& stream : mPendingStreams) {
3713 stream->cancelPrepare();
3714 }
3715 mPendingStreams.clear();
3716 mCancelNow = true;
3717
3718 return OK;
3719}
3720
3721void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3722 Mutex::Autolock l(mLock);
3723 mListener = listener;
3724}
3725
3726bool Camera3Device::PreparerThread::threadLoop() {
3727 status_t res;
3728 {
3729 Mutex::Autolock l(mLock);
3730 if (mCurrentStream == nullptr) {
3731 // End thread if done with work
3732 if (mPendingStreams.empty()) {
3733 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3734 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3735 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3736 mActive = false;
3737 return false;
3738 }
3739
3740 // Get next stream to prepare
3741 auto it = mPendingStreams.begin();
3742 mCurrentStream = *it;
3743 mPendingStreams.erase(it);
3744 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3745 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3746 } else if (mCancelNow) {
3747 mCurrentStream->cancelPrepare();
3748 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3749 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3750 mCurrentStream.clear();
3751 mCancelNow = false;
3752 return true;
3753 }
3754 }
3755
3756 res = mCurrentStream->prepareNextBuffer();
3757 if (res == NOT_ENOUGH_DATA) return true;
3758 if (res != OK) {
3759 // Something bad happened; try to recover by cancelling prepare and
3760 // signalling listener anyway
3761 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3762 mCurrentStream->getId(), res, strerror(-res));
3763 mCurrentStream->cancelPrepare();
3764 }
3765
3766 // This stream has finished, notify listener
3767 Mutex::Autolock l(mLock);
3768 if (mListener) {
3769 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3770 mCurrentStream->getId());
3771 mListener->notifyPrepared(mCurrentStream->getId());
3772 }
3773
3774 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3775 mCurrentStream.clear();
3776
3777 return true;
3778}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003779
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003780/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003781 * Static callback forwarding methods from HAL to instance
3782 */
3783
3784void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3785 const camera3_capture_result *result) {
3786 Camera3Device *d =
3787 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003788
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003789 d->processCaptureResult(result);
3790}
3791
3792void Camera3Device::sNotify(const camera3_callback_ops *cb,
3793 const camera3_notify_msg *msg) {
3794 Camera3Device *d =
3795 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3796 d->notify(msg);
3797}
3798
3799}; // namespace android