blob: 433a7457b8b1519d6859e72adf09512607b93226 [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
Igor Murashkinff3e31d2013-10-23 16:40:06 -070046#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070047#include "device3/Camera3Device.h"
48#include "device3/Camera3OutputStream.h"
49#include "device3/Camera3InputStream.h"
50#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070051#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070052#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080053
54using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080055
56namespace android {
57
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058Camera3Device::Camera3Device(int id):
59 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070060 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080061 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070062 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070063 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070064 mUsePartialResult(false),
65 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070066 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070067 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070068 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070069 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080070{
71 ATRACE_CALL();
72 camera3_callback_ops::notify = &sNotify;
73 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
74 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
75}
76
77Camera3Device::~Camera3Device()
78{
79 ATRACE_CALL();
80 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
81 disconnect();
82}
83
Igor Murashkin71381052013-03-04 14:53:08 -080084int Camera3Device::getId() const {
85 return mId;
86}
87
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080088/**
89 * CameraDeviceBase interface
90 */
91
Yin-Chia Yehe074a932015-01-30 10:29:02 -080092status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080093{
94 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070095 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080096 Mutex::Autolock l(mLock);
97
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080098 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080099 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700100 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800101 return INVALID_OPERATION;
102 }
103
104 /** Open HAL device */
105
106 status_t res;
107 String8 deviceName = String8::format("%d", mId);
108
109 camera3_device_t *device;
110
Zhijun He213ce792013-11-19 08:45:15 -0800111 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800112 res = module->open(deviceName.string(),
113 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800114 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800115
116 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700117 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800118 return res;
119 }
120
121 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700122 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700123 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700124 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700125 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800126 device->common.version);
127 device->common.close(&device->common);
128 return BAD_VALUE;
129 }
130
131 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800132 res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700133 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800134 if (res != OK) return res;
135
136 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700137 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
138 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700139 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800140 device->common.close(&device->common);
141 return BAD_VALUE;
142 }
143
144 /** Initialize device with callback functions */
145
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700146 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800147 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700148 ATRACE_END();
149
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800150 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700151 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
152 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800153 device->common.close(&device->common);
154 return BAD_VALUE;
155 }
156
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700157 /** Start up status tracker thread */
158 mStatusTracker = new StatusTracker(this);
159 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
160 if (res != OK) {
161 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
162 strerror(-res), res);
163 device->common.close(&device->common);
164 mStatusTracker.clear();
165 return res;
166 }
167
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700168 bool aeLockAvailable = false;
169 camera_metadata_ro_entry aeLockAvailableEntry;
170 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
171 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
172 if (res == OK && aeLockAvailableEntry.count > 0) {
173 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
174 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
175 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800176
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700177 /** Start up request queue thread */
178 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800179 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800180 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700181 SET_ERR_L("Unable to start request queue thread: %s (%d)",
182 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800183 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800184 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800185 return res;
186 }
187
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700188 mPreparerThread = new PreparerThread();
189
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190 /** Everything is good to go */
191
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700192 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800193 mDeviceInfo = info.static_camera_characteristics;
194 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700195
196 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800197 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700198 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700199 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700200 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800201
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700202 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700203 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
204 camera_metadata_entry partialResultsCount =
205 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
206 if (partialResultsCount.count > 0) {
207 mNumPartialResults = partialResultsCount.data.i32[0];
208 mUsePartialResult = (mNumPartialResults > 1);
209 }
210 } else {
211 camera_metadata_entry partialResultsQuirk =
212 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
213 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
214 mUsePartialResult = true;
215 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700216 }
217
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700218 camera_metadata_entry configs =
219 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
220 for (uint32_t i = 0; i < configs.count; i += 4) {
221 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
222 configs.data.i32[i + 3] ==
223 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
224 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
225 configs.data.i32[i + 2]));
226 }
227 }
228
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800229 return OK;
230}
231
232status_t Camera3Device::disconnect() {
233 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700234 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800235
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800236 ALOGV("%s: E", __FUNCTION__);
237
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700238 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800239
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700240 {
241 Mutex::Autolock l(mLock);
242 if (mStatus == STATUS_UNINITIALIZED) return res;
243
244 if (mStatus == STATUS_ACTIVE ||
245 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
246 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700247 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700248 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700249 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700250 } else {
251 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
252 if (res != OK) {
253 SET_ERR_L("Timeout waiting for HAL to drain");
254 // Continue to close device even in case of error
255 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700256 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800257 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800258
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700259 if (mStatus == STATUS_ERROR) {
260 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700261 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700262
263 if (mStatusTracker != NULL) {
264 mStatusTracker->requestExit();
265 }
266
267 if (mRequestThread != NULL) {
268 mRequestThread->requestExit();
269 }
270
271 mOutputStreams.clear();
272 mInputStream.clear();
273 }
274
275 // Joining done without holding mLock, otherwise deadlocks may ensue
276 // as the threads try to access parent state
277 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
278 // HAL may be in a bad state, so waiting for request thread
279 // (which may be stuck in the HAL processCaptureRequest call)
280 // could be dangerous.
281 mRequestThread->join();
282 }
283
284 if (mStatusTracker != NULL) {
285 mStatusTracker->join();
286 }
287
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700288 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700289 {
290 Mutex::Autolock l(mLock);
291
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800292 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700293 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800294
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700295 hal3Device = mHal3Device;
296 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800297
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700298 // Call close without internal mutex held, as the HAL close may need to
299 // wait on assorted callbacks,etc, to complete before it can return.
300 if (hal3Device != NULL) {
301 ATRACE_BEGIN("camera3->close");
302 hal3Device->common.close(&hal3Device->common);
303 ATRACE_END();
304 }
305
306 {
307 Mutex::Autolock l(mLock);
308 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700309 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700310 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800311
312 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700313 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800314}
315
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700316// For dumping/debugging only -
317// try to acquire a lock a few times, eventually give up to proceed with
318// debug/dump operations
319bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
320 bool gotLock = false;
321 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
322 if (lock.tryLock() == NO_ERROR) {
323 gotLock = true;
324 break;
325 } else {
326 usleep(kDumpSleepDuration);
327 }
328 }
329 return gotLock;
330}
331
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700332Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
333 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
334 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
335 const int STREAM_CONFIGURATION_SIZE = 4;
336 const int STREAM_FORMAT_OFFSET = 0;
337 const int STREAM_WIDTH_OFFSET = 1;
338 const int STREAM_HEIGHT_OFFSET = 2;
339 const int STREAM_IS_INPUT_OFFSET = 3;
340 camera_metadata_ro_entry_t availableStreamConfigs =
341 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
342 if (availableStreamConfigs.count == 0 ||
343 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
344 return Size(0, 0);
345 }
346
347 // Get max jpeg size (area-wise).
348 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
349 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
350 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
351 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
352 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
353 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
354 && format == HAL_PIXEL_FORMAT_BLOB &&
355 (width * height > maxJpegWidth * maxJpegHeight)) {
356 maxJpegWidth = width;
357 maxJpegHeight = height;
358 }
359 }
360 } else {
361 camera_metadata_ro_entry availableJpegSizes =
362 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
363 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
364 return Size(0, 0);
365 }
366
367 // Get max jpeg size (area-wise).
368 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
369 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
370 > (maxJpegWidth * maxJpegHeight)) {
371 maxJpegWidth = availableJpegSizes.data.i32[i];
372 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
373 }
374 }
375 }
376 return Size(maxJpegWidth, maxJpegHeight);
377}
378
Zhijun Hef7da0962014-04-24 13:27:56 -0700379ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700380 // Get max jpeg size (area-wise).
381 Size maxJpegResolution = getMaxJpegResolution();
382 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700383 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700384 __FUNCTION__, mId);
385 return BAD_VALUE;
386 }
387
Zhijun Hef7da0962014-04-24 13:27:56 -0700388 // Get max jpeg buffer size
389 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700390 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
391 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700392 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
393 return BAD_VALUE;
394 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700395 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800396 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700397
398 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700399 float scaleFactor = ((float) (width * height)) /
400 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800401 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
402 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700403 if (jpegBufferSize > maxJpegBufferSize) {
404 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700405 }
406
407 return jpegBufferSize;
408}
409
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700410ssize_t Camera3Device::getPointCloudBufferSize() const {
411 const int FLOATS_PER_POINT=4;
412 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
413 if (maxPointCount.count == 0) {
414 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
415 __FUNCTION__, mId);
416 return BAD_VALUE;
417 }
418 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
419 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
420 return maxBytesForPointCloud;
421}
422
423
424
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800425status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
426 ATRACE_CALL();
427 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700428
429 // Try to lock, but continue in case of failure (to avoid blocking in
430 // deadlocks)
431 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
432 bool gotLock = tryLockSpinRightRound(mLock);
433
434 ALOGW_IF(!gotInterfaceLock,
435 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
436 mId, __FUNCTION__);
437 ALOGW_IF(!gotLock,
438 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
439 mId, __FUNCTION__);
440
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800441 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800442
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800443 const char *status =
444 mStatus == STATUS_ERROR ? "ERROR" :
445 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700446 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
447 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800448 mStatus == STATUS_ACTIVE ? "ACTIVE" :
449 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700450
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800451 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700452 if (mStatus == STATUS_ERROR) {
453 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
454 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800455 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700456 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700457 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800458
459 if (mInputStream != NULL) {
460 write(fd, lines.string(), lines.size());
461 mInputStream->dump(fd, args);
462 } else {
463 lines.appendFormat(" No input stream.\n");
464 write(fd, lines.string(), lines.size());
465 }
466 for (size_t i = 0; i < mOutputStreams.size(); i++) {
467 mOutputStreams[i]->dump(fd,args);
468 }
469
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700470 lines = String8(" In-flight requests:\n");
471 if (mInFlightMap.size() == 0) {
472 lines.append(" None\n");
473 } else {
474 for (size_t i = 0; i < mInFlightMap.size(); i++) {
475 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700476 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700477 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800478 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700479 r.numBuffersLeft);
480 }
481 }
482 write(fd, lines.string(), lines.size());
483
Igor Murashkin1e479c02013-09-06 16:55:14 -0700484 {
485 lines = String8(" Last request sent:\n");
486 write(fd, lines.string(), lines.size());
487
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700488 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700489 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
490 }
491
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800492 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700493 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800494 write(fd, lines.string(), lines.size());
495 mHal3Device->ops->dump(mHal3Device, fd);
496 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800497
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700498 if (gotLock) mLock.unlock();
499 if (gotInterfaceLock) mInterfaceLock.unlock();
500
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800501 return OK;
502}
503
504const CameraMetadata& Camera3Device::info() const {
505 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800506 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
507 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700508 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800509 mStatus == STATUS_ERROR ?
510 "when in error state" : "before init");
511 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800512 return mDeviceInfo;
513}
514
Jianing Wei90e59c92014-03-12 18:29:36 -0700515status_t Camera3Device::checkStatusOkToCaptureLocked() {
516 switch (mStatus) {
517 case STATUS_ERROR:
518 CLOGE("Device has encountered a serious error");
519 return INVALID_OPERATION;
520 case STATUS_UNINITIALIZED:
521 CLOGE("Device not initialized");
522 return INVALID_OPERATION;
523 case STATUS_UNCONFIGURED:
524 case STATUS_CONFIGURED:
525 case STATUS_ACTIVE:
526 // OK
527 break;
528 default:
529 SET_ERR_L("Unexpected status: %d", mStatus);
530 return INVALID_OPERATION;
531 }
532 return OK;
533}
534
535status_t Camera3Device::convertMetadataListToRequestListLocked(
536 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
537 if (requestList == NULL) {
538 CLOGE("requestList cannot be NULL.");
539 return BAD_VALUE;
540 }
541
Jianing Weicb0652e2014-03-12 18:29:36 -0700542 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700543 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
544 it != metadataList.end(); ++it) {
545 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
546 if (newRequest == 0) {
547 CLOGE("Can't create capture request");
548 return BAD_VALUE;
549 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700550
551 // Setup burst Id and request Id
552 newRequest->mResultExtras.burstId = burstId++;
553 if (it->exists(ANDROID_REQUEST_ID)) {
554 if (it->find(ANDROID_REQUEST_ID).count == 0) {
555 CLOGE("RequestID entry exists; but must not be empty in metadata");
556 return BAD_VALUE;
557 }
558 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
559 } else {
560 CLOGE("RequestID does not exist in metadata");
561 return BAD_VALUE;
562 }
563
Jianing Wei90e59c92014-03-12 18:29:36 -0700564 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700565
566 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700567 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700568
569 // Setup batch size if this is a high speed video recording request.
570 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
571 auto firstRequest = requestList->begin();
572 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
573 if (outputStream->isVideoStream()) {
574 (*firstRequest)->mBatchSize = requestList->size();
575 break;
576 }
577 }
578 }
579
Jianing Wei90e59c92014-03-12 18:29:36 -0700580 return OK;
581}
582
Jianing Weicb0652e2014-03-12 18:29:36 -0700583status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800584 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800585
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700586 List<const CameraMetadata> requests;
587 requests.push_back(request);
588 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800589}
590
Jianing Wei90e59c92014-03-12 18:29:36 -0700591status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700592 const List<const CameraMetadata> &requests, bool repeating,
593 /*out*/
594 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700595 ATRACE_CALL();
596 Mutex::Autolock il(mInterfaceLock);
597 Mutex::Autolock l(mLock);
598
599 status_t res = checkStatusOkToCaptureLocked();
600 if (res != OK) {
601 // error logged by previous call
602 return res;
603 }
604
605 RequestList requestList;
606
607 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
608 if (res != OK) {
609 // error logged by previous call
610 return res;
611 }
612
613 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700614 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700615 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700616 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700617 }
618
619 if (res == OK) {
620 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
621 if (res != OK) {
622 SET_ERR_L("Can't transition to active in %f seconds!",
623 kActiveTimeout/1e9);
624 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700625 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
626 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700627 } else {
628 CLOGE("Cannot queue request. Impossible.");
629 return BAD_VALUE;
630 }
631
632 return res;
633}
634
Jianing Weicb0652e2014-03-12 18:29:36 -0700635status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
636 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700637 ATRACE_CALL();
638
Jianing Weicb0652e2014-03-12 18:29:36 -0700639 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700640}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800641
Jianing Weicb0652e2014-03-12 18:29:36 -0700642status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
643 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800644 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800645
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700646 List<const CameraMetadata> requests;
647 requests.push_back(request);
648 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800649}
650
Jianing Weicb0652e2014-03-12 18:29:36 -0700651status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
652 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700653 ATRACE_CALL();
654
Jianing Weicb0652e2014-03-12 18:29:36 -0700655 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700656}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800657
658sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
659 const CameraMetadata &request) {
660 status_t res;
661
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700662 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800663 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700664 // Stream configuration failed due to unsupported configuration.
665 // Device back to unconfigured state. Client might try other configuraitons
666 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
667 CLOGE("No streams configured");
668 return NULL;
669 }
670 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800671 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700672 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800673 return NULL;
674 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700675 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700676 if (mStatus == STATUS_UNCONFIGURED) {
677 CLOGE("No streams configured");
678 return NULL;
679 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800680 }
681
682 sp<CaptureRequest> newRequest = createCaptureRequest(request);
683 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800684}
685
Jianing Weicb0652e2014-03-12 18:29:36 -0700686status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800687 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700688 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800689 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800690
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800691 switch (mStatus) {
692 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700693 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800694 return INVALID_OPERATION;
695 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700696 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800697 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700698 case STATUS_UNCONFIGURED:
699 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800700 case STATUS_ACTIVE:
701 // OK
702 break;
703 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700704 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800705 return INVALID_OPERATION;
706 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700707 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700708
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700709 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800710}
711
712status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
713 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700714 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800715
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700716 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800717}
718
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700719status_t Camera3Device::createInputStream(
720 uint32_t width, uint32_t height, int format, int *id) {
721 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700722 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700723 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700724 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
725 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700726
727 status_t res;
728 bool wasActive = false;
729
730 switch (mStatus) {
731 case STATUS_ERROR:
732 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
733 return INVALID_OPERATION;
734 case STATUS_UNINITIALIZED:
735 ALOGE("%s: Device not initialized", __FUNCTION__);
736 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700737 case STATUS_UNCONFIGURED:
738 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700739 // OK
740 break;
741 case STATUS_ACTIVE:
742 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700743 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700744 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700745 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700746 return res;
747 }
748 wasActive = true;
749 break;
750 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700751 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700752 return INVALID_OPERATION;
753 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700754 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700755
756 if (mInputStream != 0) {
757 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
758 return INVALID_OPERATION;
759 }
760
761 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
762 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700763 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700764
765 mInputStream = newStream;
766
767 *id = mNextStreamId++;
768
769 // Continue captures if active at start
770 if (wasActive) {
771 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
772 res = configureStreamsLocked();
773 if (res != OK) {
774 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
775 __FUNCTION__, mNextStreamId, strerror(-res), res);
776 return res;
777 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700778 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700779 }
780
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700781 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700782 return OK;
783}
784
Igor Murashkin2fba5842013-04-22 14:03:54 -0700785
786status_t Camera3Device::createZslStream(
787 uint32_t width, uint32_t height,
788 int depth,
789 /*out*/
790 int *id,
791 sp<Camera3ZslStream>* zslStream) {
792 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700793 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700794 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700795 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
796 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700797
798 status_t res;
799 bool wasActive = false;
800
801 switch (mStatus) {
802 case STATUS_ERROR:
803 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
804 return INVALID_OPERATION;
805 case STATUS_UNINITIALIZED:
806 ALOGE("%s: Device not initialized", __FUNCTION__);
807 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700808 case STATUS_UNCONFIGURED:
809 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700810 // OK
811 break;
812 case STATUS_ACTIVE:
813 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700814 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700815 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700816 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700817 return res;
818 }
819 wasActive = true;
820 break;
821 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700822 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700823 return INVALID_OPERATION;
824 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700825 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700826
827 if (mInputStream != 0) {
828 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
829 return INVALID_OPERATION;
830 }
831
832 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
833 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700834 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700835
836 res = mOutputStreams.add(mNextStreamId, newStream);
837 if (res < 0) {
838 ALOGE("%s: Can't add new stream to set: %s (%d)",
839 __FUNCTION__, strerror(-res), res);
840 return res;
841 }
842 mInputStream = newStream;
843
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530844 mNeedConfig = true;
845
Igor Murashkin2fba5842013-04-22 14:03:54 -0700846 *id = mNextStreamId++;
847 *zslStream = newStream;
848
849 // Continue captures if active at start
850 if (wasActive) {
851 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
852 res = configureStreamsLocked();
853 if (res != OK) {
854 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
855 __FUNCTION__, mNextStreamId, strerror(-res), res);
856 return res;
857 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700858 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700859 }
860
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700861 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700862 return OK;
863}
864
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700865status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800866 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700867 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800868 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700869 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800870 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700871 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
872 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800873
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800874 status_t res;
875 bool wasActive = false;
876
877 switch (mStatus) {
878 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700879 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800880 return INVALID_OPERATION;
881 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700882 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800883 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700884 case STATUS_UNCONFIGURED:
885 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800886 // OK
887 break;
888 case STATUS_ACTIVE:
889 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700890 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800891 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700892 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800893 return res;
894 }
895 wasActive = true;
896 break;
897 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700898 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800899 return INVALID_OPERATION;
900 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700901 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800902
903 sp<Camera3OutputStream> newStream;
904 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700905 ssize_t blobBufferSize;
906 if (dataSpace != HAL_DATASPACE_DEPTH) {
907 blobBufferSize = getJpegBufferSize(width, height);
908 if (blobBufferSize <= 0) {
909 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
910 return BAD_VALUE;
911 }
912 } else {
913 blobBufferSize = getPointCloudBufferSize();
914 if (blobBufferSize <= 0) {
915 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
916 return BAD_VALUE;
917 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700918 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800919 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700920 width, height, blobBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800921 } else {
922 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700923 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800924 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700925 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800926
927 res = mOutputStreams.add(mNextStreamId, newStream);
928 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700929 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800930 return res;
931 }
932
933 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700934 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800935
936 // Continue captures if active at start
937 if (wasActive) {
938 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
939 res = configureStreamsLocked();
940 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700941 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
942 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800943 return res;
944 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700945 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800946 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700947 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800948 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800949}
950
951status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
952 ATRACE_CALL();
953 (void)outputId; (void)id;
954
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700955 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800956 return INVALID_OPERATION;
957}
958
959
960status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700961 uint32_t *width, uint32_t *height,
962 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800963 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700964 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800965 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800966
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800967 switch (mStatus) {
968 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700969 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800970 return INVALID_OPERATION;
971 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700972 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800973 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700974 case STATUS_UNCONFIGURED:
975 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800976 case STATUS_ACTIVE:
977 // OK
978 break;
979 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700980 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800981 return INVALID_OPERATION;
982 }
983
984 ssize_t idx = mOutputStreams.indexOfKey(id);
985 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700986 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800987 return idx;
988 }
989
990 if (width) *width = mOutputStreams[idx]->getWidth();
991 if (height) *height = mOutputStreams[idx]->getHeight();
992 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700993 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800994 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800995}
996
997status_t Camera3Device::setStreamTransform(int id,
998 int transform) {
999 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001000 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001001 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001002
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001003 switch (mStatus) {
1004 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001005 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001006 return INVALID_OPERATION;
1007 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001008 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001009 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001010 case STATUS_UNCONFIGURED:
1011 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001012 case STATUS_ACTIVE:
1013 // OK
1014 break;
1015 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001016 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001017 return INVALID_OPERATION;
1018 }
1019
1020 ssize_t idx = mOutputStreams.indexOfKey(id);
1021 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001022 CLOGE("Stream %d does not exist",
1023 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001024 return BAD_VALUE;
1025 }
1026
1027 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001028}
1029
1030status_t Camera3Device::deleteStream(int id) {
1031 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001032 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001033 Mutex::Autolock l(mLock);
1034 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001035
Igor Murashkine2172be2013-05-28 15:31:39 -07001036 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1037
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001038 // CameraDevice semantics require device to already be idle before
1039 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001040 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001041 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1042 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001043 }
1044
Igor Murashkin2fba5842013-04-22 14:03:54 -07001045 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001046 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001047 if (mInputStream != NULL && id == mInputStream->getId()) {
1048 deletedStream = mInputStream;
1049 mInputStream.clear();
1050 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001051 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001052 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053 return BAD_VALUE;
1054 }
Zhijun He5f446352014-01-22 09:49:33 -08001055 }
1056
1057 // Delete output stream or the output part of a bi-directional stream.
1058 if (outputStreamIdx != NAME_NOT_FOUND) {
1059 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001060 mOutputStreams.removeItem(id);
1061 }
1062
1063 // Free up the stream endpoint so that it can be used by some other stream
1064 res = deletedStream->disconnect();
1065 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001066 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001067 // fall through since we want to still list the stream as deleted.
1068 }
1069 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001070 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071
1072 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001073}
1074
1075status_t Camera3Device::deleteReprocessStream(int id) {
1076 ATRACE_CALL();
1077 (void)id;
1078
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001079 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001080 return INVALID_OPERATION;
1081}
1082
Zhijun He1fa89992015-06-01 15:44:31 -07001083status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001084 ATRACE_CALL();
1085 ALOGV("%s: E", __FUNCTION__);
1086
1087 Mutex::Autolock il(mInterfaceLock);
1088 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001089
1090 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1091 mNeedConfig = true;
1092 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1093 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001094
1095 return configureStreamsLocked();
1096}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001097
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001098status_t Camera3Device::getInputBufferProducer(
1099 sp<IGraphicBufferProducer> *producer) {
1100 Mutex::Autolock il(mInterfaceLock);
1101 Mutex::Autolock l(mLock);
1102
1103 if (producer == NULL) {
1104 return BAD_VALUE;
1105 } else if (mInputStream == NULL) {
1106 return INVALID_OPERATION;
1107 }
1108
1109 return mInputStream->getInputBufferProducer(producer);
1110}
1111
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001112status_t Camera3Device::createDefaultRequest(int templateId,
1113 CameraMetadata *request) {
1114 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001115 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001116 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001117 Mutex::Autolock l(mLock);
1118
1119 switch (mStatus) {
1120 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001121 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001122 return INVALID_OPERATION;
1123 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001124 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001125 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001126 case STATUS_UNCONFIGURED:
1127 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001128 case STATUS_ACTIVE:
1129 // OK
1130 break;
1131 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001132 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001133 return INVALID_OPERATION;
1134 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001135
Zhijun Hea1530f12014-09-14 12:44:20 -07001136 if (!mRequestTemplateCache[templateId].isEmpty()) {
1137 *request = mRequestTemplateCache[templateId];
1138 return OK;
1139 }
1140
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001141 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001142 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001143 rawRequest = mHal3Device->ops->construct_default_request_settings(
1144 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001145 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001146 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001147 ALOGI("%s: template %d is not supported on this camera device",
1148 __FUNCTION__, templateId);
1149 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001150 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001151 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001152 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001153
1154 return OK;
1155}
1156
1157status_t Camera3Device::waitUntilDrained() {
1158 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001159 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001160 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001161
Zhijun He69a37482014-03-23 18:44:49 -07001162 return waitUntilDrainedLocked();
1163}
1164
1165status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001166 switch (mStatus) {
1167 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001168 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001169 ALOGV("%s: Already idle", __FUNCTION__);
1170 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001171 case STATUS_CONFIGURED:
1172 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001173 case STATUS_ERROR:
1174 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001175 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001176 break;
1177 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001178 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001179 return INVALID_OPERATION;
1180 }
1181
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001182 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1183 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001184 if (res != OK) {
1185 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1186 res);
1187 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001188 return res;
1189}
1190
Ruben Brunk183f0562015-08-12 12:55:02 -07001191
1192void Camera3Device::internalUpdateStatusLocked(Status status) {
1193 mStatus = status;
1194 mRecentStatusUpdates.add(mStatus);
1195 mStatusChanged.broadcast();
1196}
1197
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001198// Pause to reconfigure
1199status_t Camera3Device::internalPauseAndWaitLocked() {
1200 mRequestThread->setPaused(true);
1201 mPauseStateNotify = true;
1202
1203 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1204 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1205 if (res != OK) {
1206 SET_ERR_L("Can't idle device in %f seconds!",
1207 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001208 }
1209
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001210 return res;
1211}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001212
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001213// Resume after internalPauseAndWaitLocked
1214status_t Camera3Device::internalResumeLocked() {
1215 status_t res;
1216
1217 mRequestThread->setPaused(false);
1218
1219 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1220 if (res != OK) {
1221 SET_ERR_L("Can't transition to active in %f seconds!",
1222 kActiveTimeout/1e9);
1223 }
1224 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001225 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001226}
1227
Ruben Brunk183f0562015-08-12 12:55:02 -07001228status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001229 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001230
1231 size_t startIndex = 0;
1232 if (mStatusWaiters == 0) {
1233 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1234 // this status list
1235 mRecentStatusUpdates.clear();
1236 } else {
1237 // If other threads are waiting on updates to this status list, set the position of the
1238 // first element that this list will check rather than clearing the list.
1239 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001240 }
1241
Ruben Brunk183f0562015-08-12 12:55:02 -07001242 mStatusWaiters++;
1243
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001244 bool stateSeen = false;
1245 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001246 if (active == (mStatus == STATUS_ACTIVE)) {
1247 // Desired state is current
1248 break;
1249 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001250
1251 res = mStatusChanged.waitRelative(mLock, timeout);
1252 if (res != OK) break;
1253
Ruben Brunk183f0562015-08-12 12:55:02 -07001254 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1255 // transitions.
1256 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1257 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1258 __FUNCTION__);
1259
1260 // Encountered desired state since we began waiting
1261 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001262 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1263 stateSeen = true;
1264 break;
1265 }
1266 }
1267 } while (!stateSeen);
1268
Ruben Brunk183f0562015-08-12 12:55:02 -07001269 mStatusWaiters--;
1270
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001271 return res;
1272}
1273
1274
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001275status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1276 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001277 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001278
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001279 if (listener != NULL && mListener != NULL) {
1280 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1281 }
1282 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001283 mRequestThread->setNotificationListener(listener);
1284 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001285
1286 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001287}
1288
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001289bool Camera3Device::willNotify3A() {
1290 return false;
1291}
1292
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001293status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001294 status_t res;
1295 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001296
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001297 while (mResultQueue.empty()) {
1298 res = mResultSignal.waitRelative(mOutputLock, timeout);
1299 if (res == TIMED_OUT) {
1300 return res;
1301 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001302 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001303 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001304 return res;
1305 }
1306 }
1307 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001308}
1309
Jianing Weicb0652e2014-03-12 18:29:36 -07001310status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001311 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001312 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001313
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001314 if (mResultQueue.empty()) {
1315 return NOT_ENOUGH_DATA;
1316 }
1317
Jianing Weicb0652e2014-03-12 18:29:36 -07001318 if (frame == NULL) {
1319 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1320 return BAD_VALUE;
1321 }
1322
1323 CaptureResult &result = *(mResultQueue.begin());
1324 frame->mResultExtras = result.mResultExtras;
1325 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001326 mResultQueue.erase(mResultQueue.begin());
1327
1328 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001329}
1330
1331status_t Camera3Device::triggerAutofocus(uint32_t id) {
1332 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001333 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001334
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001335 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1336 // Mix-in this trigger into the next request and only the next request.
1337 RequestTrigger trigger[] = {
1338 {
1339 ANDROID_CONTROL_AF_TRIGGER,
1340 ANDROID_CONTROL_AF_TRIGGER_START
1341 },
1342 {
1343 ANDROID_CONTROL_AF_TRIGGER_ID,
1344 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001345 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001346 };
1347
1348 return mRequestThread->queueTrigger(trigger,
1349 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001350}
1351
1352status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1353 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001354 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001355
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001356 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1357 // Mix-in this trigger into the next request and only the next request.
1358 RequestTrigger trigger[] = {
1359 {
1360 ANDROID_CONTROL_AF_TRIGGER,
1361 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1362 },
1363 {
1364 ANDROID_CONTROL_AF_TRIGGER_ID,
1365 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001366 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001367 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001368
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001369 return mRequestThread->queueTrigger(trigger,
1370 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001371}
1372
1373status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1374 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001375 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001376
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001377 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1378 // Mix-in this trigger into the next request and only the next request.
1379 RequestTrigger trigger[] = {
1380 {
1381 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1382 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1383 },
1384 {
1385 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1386 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001387 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001388 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001389
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001390 return mRequestThread->queueTrigger(trigger,
1391 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001392}
1393
1394status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1395 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1396 ATRACE_CALL();
1397 (void)reprocessStreamId; (void)buffer; (void)listener;
1398
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001399 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001400 return INVALID_OPERATION;
1401}
1402
Jianing Weicb0652e2014-03-12 18:29:36 -07001403status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001404 ATRACE_CALL();
1405 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001406 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001407
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001408 NotificationListener* listener;
1409 {
1410 Mutex::Autolock l(mOutputLock);
1411 listener = mListener;
1412 }
1413
Zhijun He7ef20392014-04-21 16:04:17 -07001414 {
1415 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001416 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001417 }
1418
Zhijun He491e3412013-12-27 10:57:44 -08001419 status_t res;
1420 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001421 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001422 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001423 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001424 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001425 }
1426
1427 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001428}
1429
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001430status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001431 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1432}
1433
1434status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001435 ATRACE_CALL();
1436 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001437 Mutex::Autolock il(mInterfaceLock);
1438 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001439
1440 sp<Camera3StreamInterface> stream;
1441 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1442 if (outputStreamIdx == NAME_NOT_FOUND) {
1443 CLOGE("Stream %d does not exist", streamId);
1444 return BAD_VALUE;
1445 }
1446
1447 stream = mOutputStreams.editValueAt(outputStreamIdx);
1448
1449 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001450 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001451 return BAD_VALUE;
1452 }
1453
1454 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001455 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001456 return BAD_VALUE;
1457 }
1458
Ruben Brunkc78ac262015-08-13 17:58:46 -07001459 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001460}
1461
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001462status_t Camera3Device::tearDown(int streamId) {
1463 ATRACE_CALL();
1464 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1465 Mutex::Autolock il(mInterfaceLock);
1466 Mutex::Autolock l(mLock);
1467
1468 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1469 // since we cannot call register_stream_buffers except right after configure_streams.
1470 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1471 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1472 __FUNCTION__, mHal3Device->common.version);
1473 return NO_INIT;
1474 }
1475
1476 sp<Camera3StreamInterface> stream;
1477 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1478 if (outputStreamIdx == NAME_NOT_FOUND) {
1479 CLOGE("Stream %d does not exist", streamId);
1480 return BAD_VALUE;
1481 }
1482
1483 stream = mOutputStreams.editValueAt(outputStreamIdx);
1484
1485 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1486 CLOGE("Stream %d is a target of a in-progress request", streamId);
1487 return BAD_VALUE;
1488 }
1489
1490 return stream->tearDown();
1491}
1492
Zhijun He204e3292014-07-14 17:09:23 -07001493uint32_t Camera3Device::getDeviceVersion() {
1494 ATRACE_CALL();
1495 Mutex::Autolock il(mInterfaceLock);
1496 return mDeviceVersion;
1497}
1498
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001499/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001500 * Methods called by subclasses
1501 */
1502
1503void Camera3Device::notifyStatus(bool idle) {
1504 {
1505 // Need mLock to safely update state and synchronize to current
1506 // state of methods in flight.
1507 Mutex::Autolock l(mLock);
1508 // We can get various system-idle notices from the status tracker
1509 // while starting up. Only care about them if we've actually sent
1510 // in some requests recently.
1511 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1512 return;
1513 }
1514 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1515 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001516 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001517
1518 // Skip notifying listener if we're doing some user-transparent
1519 // state changes
1520 if (mPauseStateNotify) return;
1521 }
1522 NotificationListener *listener;
1523 {
1524 Mutex::Autolock l(mOutputLock);
1525 listener = mListener;
1526 }
1527 if (idle && listener != NULL) {
1528 listener->notifyIdle();
1529 }
1530}
1531
1532/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001533 * Camera3Device private methods
1534 */
1535
1536sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1537 const CameraMetadata &request) {
1538 ATRACE_CALL();
1539 status_t res;
1540
1541 sp<CaptureRequest> newRequest = new CaptureRequest;
1542 newRequest->mSettings = request;
1543
1544 camera_metadata_entry_t inputStreams =
1545 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1546 if (inputStreams.count > 0) {
1547 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001548 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001549 CLOGE("Request references unknown input stream %d",
1550 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001551 return NULL;
1552 }
1553 // Lazy completion of stream configuration (allocation/registration)
1554 // on first use
1555 if (mInputStream->isConfiguring()) {
1556 res = mInputStream->finishConfiguration(mHal3Device);
1557 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001558 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001559 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001560 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001561 return NULL;
1562 }
1563 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001564 // Check if stream is being prepared
1565 if (mInputStream->isPreparing()) {
1566 CLOGE("Request references an input stream that's being prepared!");
1567 return NULL;
1568 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001569
1570 newRequest->mInputStream = mInputStream;
1571 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1572 }
1573
1574 camera_metadata_entry_t streams =
1575 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1576 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001577 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001578 return NULL;
1579 }
1580
1581 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001582 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001583 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001584 CLOGE("Request references unknown stream %d",
1585 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001586 return NULL;
1587 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001588 sp<Camera3OutputStreamInterface> stream =
1589 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001590
1591 // Lazy completion of stream configuration (allocation/registration)
1592 // on first use
1593 if (stream->isConfiguring()) {
1594 res = stream->finishConfiguration(mHal3Device);
1595 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001596 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1597 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001598 return NULL;
1599 }
1600 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001601 // Check if stream is being prepared
1602 if (stream->isPreparing()) {
1603 CLOGE("Request references an output stream that's being prepared!");
1604 return NULL;
1605 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001606
1607 newRequest->mOutputStreams.push(stream);
1608 }
1609 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001610 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001611
1612 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001613}
1614
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001615bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1616 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1617 Size size = mSupportedOpaqueInputSizes[i];
1618 if (size.width == width && size.height == height) {
1619 return true;
1620 }
1621 }
1622
1623 return false;
1624}
1625
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001626status_t Camera3Device::configureStreamsLocked() {
1627 ATRACE_CALL();
1628 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001629
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001630 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001631 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001632 return INVALID_OPERATION;
1633 }
1634
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001635 if (!mNeedConfig) {
1636 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1637 return OK;
1638 }
1639
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001640 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1641 // adding a dummy stream instead.
1642 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1643 if (mOutputStreams.size() == 0) {
1644 addDummyStreamLocked();
1645 } else {
1646 tryRemoveDummyStreamLocked();
1647 }
1648
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001649 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001650 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001651
1652 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001653 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1654 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1655 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001656 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1657
1658 Vector<camera3_stream_t*> streams;
1659 streams.setCapacity(config.num_streams);
1660
1661 if (mInputStream != NULL) {
1662 camera3_stream_t *inputStream;
1663 inputStream = mInputStream->startConfiguration();
1664 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001665 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001666 return INVALID_OPERATION;
1667 }
1668 streams.add(inputStream);
1669 }
1670
1671 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001672
1673 // Don't configure bidi streams twice, nor add them twice to the list
1674 if (mOutputStreams[i].get() ==
1675 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1676
1677 config.num_streams--;
1678 continue;
1679 }
1680
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001681 camera3_stream_t *outputStream;
1682 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1683 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001684 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001685 return INVALID_OPERATION;
1686 }
1687 streams.add(outputStream);
1688 }
1689
1690 config.streams = streams.editArray();
1691
1692 // Do the HAL configuration; will potentially touch stream
1693 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001694 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001695 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001696 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001697
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001698 if (res == BAD_VALUE) {
1699 // HAL rejected this set of streams as unsupported, clean up config
1700 // attempt and return to unconfigured state
1701 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1702 res = mInputStream->cancelConfiguration();
1703 if (res != OK) {
1704 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1705 mInputStream->getId(), strerror(-res), res);
1706 return res;
1707 }
1708 }
1709
1710 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1711 sp<Camera3OutputStreamInterface> outputStream =
1712 mOutputStreams.editValueAt(i);
1713 if (outputStream->isConfiguring()) {
1714 res = outputStream->cancelConfiguration();
1715 if (res != OK) {
1716 SET_ERR_L(
1717 "Can't cancel configuring output stream %d: %s (%d)",
1718 outputStream->getId(), strerror(-res), res);
1719 return res;
1720 }
1721 }
1722 }
1723
1724 // Return state to that at start of call, so that future configures
1725 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001726 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001727 mNeedConfig = true;
1728
1729 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1730 return BAD_VALUE;
1731 } else if (res != OK) {
1732 // Some other kind of error from configure_streams - this is not
1733 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001734 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1735 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001736 return res;
1737 }
1738
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001739 // Finish all stream configuration immediately.
1740 // TODO: Try to relax this later back to lazy completion, which should be
1741 // faster
1742
Igor Murashkin073f8572013-05-02 14:59:28 -07001743 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001744 res = mInputStream->finishConfiguration(mHal3Device);
1745 if (res != OK) {
1746 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1747 mInputStream->getId(), strerror(-res), res);
1748 return res;
1749 }
1750 }
1751
1752 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001753 sp<Camera3OutputStreamInterface> outputStream =
1754 mOutputStreams.editValueAt(i);
1755 if (outputStream->isConfiguring()) {
1756 res = outputStream->finishConfiguration(mHal3Device);
1757 if (res != OK) {
1758 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1759 outputStream->getId(), strerror(-res), res);
1760 return res;
1761 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001762 }
1763 }
1764
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001765 // Request thread needs to know to avoid using repeat-last-settings protocol
1766 // across configure_streams() calls
1767 mRequestThread->configurationComplete();
1768
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001769 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001770
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001771 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001772
Ruben Brunk183f0562015-08-12 12:55:02 -07001773 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1774 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001775
1776 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1777
Zhijun He0a210512014-07-24 13:45:15 -07001778 // tear down the deleted streams after configure streams.
1779 mDeletedStreams.clear();
1780
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001781 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001782}
1783
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001784status_t Camera3Device::addDummyStreamLocked() {
1785 ATRACE_CALL();
1786 status_t res;
1787
1788 if (mDummyStreamId != NO_STREAM) {
1789 // Should never be adding a second dummy stream when one is already
1790 // active
1791 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1792 __FUNCTION__, mId);
1793 return INVALID_OPERATION;
1794 }
1795
1796 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1797
1798 sp<Camera3OutputStreamInterface> dummyStream =
1799 new Camera3DummyStream(mNextStreamId);
1800
1801 res = mOutputStreams.add(mNextStreamId, dummyStream);
1802 if (res < 0) {
1803 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1804 return res;
1805 }
1806
1807 mDummyStreamId = mNextStreamId;
1808 mNextStreamId++;
1809
1810 return OK;
1811}
1812
1813status_t Camera3Device::tryRemoveDummyStreamLocked() {
1814 ATRACE_CALL();
1815 status_t res;
1816
1817 if (mDummyStreamId == NO_STREAM) return OK;
1818 if (mOutputStreams.size() == 1) return OK;
1819
1820 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1821
1822 // Ok, have a dummy stream and there's at least one other output stream,
1823 // so remove the dummy
1824
1825 sp<Camera3StreamInterface> deletedStream;
1826 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1827 if (outputStreamIdx == NAME_NOT_FOUND) {
1828 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1829 return INVALID_OPERATION;
1830 }
1831
1832 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1833 mOutputStreams.removeItemsAt(outputStreamIdx);
1834
1835 // Free up the stream endpoint so that it can be used by some other stream
1836 res = deletedStream->disconnect();
1837 if (res != OK) {
1838 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1839 // fall through since we want to still list the stream as deleted.
1840 }
1841 mDeletedStreams.add(deletedStream);
1842 mDummyStreamId = NO_STREAM;
1843
1844 return res;
1845}
1846
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001847void Camera3Device::setErrorState(const char *fmt, ...) {
1848 Mutex::Autolock l(mLock);
1849 va_list args;
1850 va_start(args, fmt);
1851
1852 setErrorStateLockedV(fmt, args);
1853
1854 va_end(args);
1855}
1856
1857void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1858 Mutex::Autolock l(mLock);
1859 setErrorStateLockedV(fmt, args);
1860}
1861
1862void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1863 va_list args;
1864 va_start(args, fmt);
1865
1866 setErrorStateLockedV(fmt, args);
1867
1868 va_end(args);
1869}
1870
1871void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001872 // Print out all error messages to log
1873 String8 errorCause = String8::formatV(fmt, args);
1874 ALOGE("Camera %d: %s", mId, errorCause.string());
1875
1876 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001877 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001878
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001879 mErrorCause = errorCause;
1880
1881 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07001882 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001883
1884 // Notify upstream about a device error
1885 if (mListener != NULL) {
1886 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1887 CaptureResultExtras());
1888 }
1889
1890 // Save stack trace. View by dumping it later.
1891 CameraTraces::saveTrace();
1892 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001893}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001894
1895/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001896 * In-flight request management
1897 */
1898
Jianing Weicb0652e2014-03-12 18:29:36 -07001899status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07001900 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1901 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001902 ATRACE_CALL();
1903 Mutex::Autolock l(mInFlightLock);
1904
1905 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07001906 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
1907 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001908 if (res < 0) return res;
1909
1910 return OK;
1911}
1912
1913/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001914 * Check if all 3A fields are ready, and send off a partial 3A-only result
1915 * to the output frame queue
1916 */
Zhijun He204e3292014-07-14 17:09:23 -07001917bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001918 uint32_t frameNumber,
1919 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001920
1921 // Check if all 3A states are present
1922 // The full list of fields is
1923 // android.control.afMode
1924 // android.control.awbMode
1925 // android.control.aeState
1926 // android.control.awbState
1927 // android.control.afState
1928 // android.control.afTriggerID
1929 // android.control.aePrecaptureID
1930 // TODO: Add android.control.aeMode
1931
1932 bool gotAllStates = true;
1933
1934 uint8_t afMode;
1935 uint8_t awbMode;
1936 uint8_t aeState;
1937 uint8_t afState;
1938 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001939
1940 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1941 &afMode, frameNumber);
1942
1943 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1944 &awbMode, frameNumber);
1945
1946 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1947 &aeState, frameNumber);
1948
1949 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1950 &afState, frameNumber);
1951
1952 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1953 &awbState, frameNumber);
1954
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001955 if (!gotAllStates) return false;
1956
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001957 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001958 "AF state %d, AE state %d, AWB state %d, "
1959 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001960 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001961 afMode, awbMode,
1962 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001963 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001964
1965 // Got all states, so construct a minimal result to send
1966 // In addition to the above fields, this means adding in
1967 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001968 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001969 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001970
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001971 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001972
1973 Mutex::Autolock l(mOutputLock);
1974
Jianing Weicb0652e2014-03-12 18:29:36 -07001975 CaptureResult captureResult;
1976 captureResult.mResultExtras = resultExtras;
1977 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1978 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1979 // but not limited to CameraDeviceBase::getNextResult
1980 CaptureResult& min3AResult =
1981 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001982
Jianing Weicb0652e2014-03-12 18:29:36 -07001983 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1984 // TODO: This is problematic casting. Need to fix CameraMetadata.
1985 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001986 return false;
1987 }
1988
Jianing Weicb0652e2014-03-12 18:29:36 -07001989 int32_t requestId = resultExtras.requestId;
1990 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001991 &requestId, frameNumber)) {
1992 return false;
1993 }
1994
Zhijun He204e3292014-07-14 17:09:23 -07001995 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1996 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1997 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1998 &partialResult, frameNumber)) {
1999 return false;
2000 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002001 }
2002
Jianing Weicb0652e2014-03-12 18:29:36 -07002003 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002004 &afMode, frameNumber)) {
2005 return false;
2006 }
2007
Jianing Weicb0652e2014-03-12 18:29:36 -07002008 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002009 &awbMode, frameNumber)) {
2010 return false;
2011 }
2012
Jianing Weicb0652e2014-03-12 18:29:36 -07002013 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002014 &aeState, frameNumber)) {
2015 return false;
2016 }
2017
Jianing Weicb0652e2014-03-12 18:29:36 -07002018 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002019 &afState, frameNumber)) {
2020 return false;
2021 }
2022
Jianing Weicb0652e2014-03-12 18:29:36 -07002023 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002024 &awbState, frameNumber)) {
2025 return false;
2026 }
2027
Jianing Weicb0652e2014-03-12 18:29:36 -07002028 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002029 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002030 return false;
2031 }
2032
Jianing Weicb0652e2014-03-12 18:29:36 -07002033 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002034 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002035 return false;
2036 }
2037
Zhijun He204e3292014-07-14 17:09:23 -07002038 // We only send the aggregated partial when all 3A related metadata are available
2039 // For both API1 and API2.
2040 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002041 mResultSignal.signal();
2042
2043 return true;
2044}
2045
2046template<typename T>
2047bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002048 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002049 (void) frameNumber;
2050
2051 camera_metadata_ro_entry_t entry;
2052
2053 entry = result.find(tag);
2054 if (entry.count == 0) {
2055 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2056 mId, frameNumber, get_camera_metadata_tag_name(tag));
2057 return false;
2058 }
2059
2060 if (sizeof(T) == sizeof(uint8_t)) {
2061 *value = entry.data.u8[0];
2062 } else if (sizeof(T) == sizeof(int32_t)) {
2063 *value = entry.data.i32[0];
2064 } else {
2065 ALOGE("%s: Unexpected type", __FUNCTION__);
2066 return false;
2067 }
2068 return true;
2069}
2070
2071template<typename T>
2072bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002073 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002074 if (result.update(tag, value, 1) != NO_ERROR) {
2075 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2076 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2077 frameNumber, get_camera_metadata_tag_name(tag));
2078 return false;
2079 }
2080 return true;
2081}
2082
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002083void Camera3Device::returnOutputBuffers(
2084 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2085 nsecs_t timestamp) {
2086 for (size_t i = 0; i < numBuffers; i++)
2087 {
2088 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2089 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2090 // Note: stream may be deallocated at this point, if this buffer was
2091 // the last reference to it.
2092 if (res != OK) {
2093 ALOGE("Can't return buffer to its stream: %s (%d)",
2094 strerror(-res), res);
2095 }
2096 }
2097}
2098
2099
2100void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2101
2102 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2103 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2104
2105 nsecs_t sensorTimestamp = request.sensorTimestamp;
2106 nsecs_t shutterTimestamp = request.shutterTimestamp;
2107
2108 // Check if it's okay to remove the request from InFlightMap:
2109 // In the case of a successful request:
2110 // all input and output buffers, all result metadata, shutter callback
2111 // arrived.
2112 // In the case of a unsuccessful request:
2113 // all input and output buffers arrived.
2114 if (request.numBuffersLeft == 0 &&
2115 (request.requestStatus != OK ||
2116 (request.haveResultMetadata && shutterTimestamp != 0))) {
2117 ATRACE_ASYNC_END("frame capture", frameNumber);
2118
2119 // Sanity check - if sensor timestamp matches shutter timestamp
2120 if (request.requestStatus == OK &&
2121 sensorTimestamp != shutterTimestamp) {
2122 SET_ERR("sensor timestamp (%" PRId64
2123 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2124 sensorTimestamp, frameNumber, shutterTimestamp);
2125 }
2126
2127 // for an unsuccessful request, it may have pending output buffers to
2128 // return.
2129 assert(request.requestStatus != OK ||
2130 request.pendingOutputBuffers.size() == 0);
2131 returnOutputBuffers(request.pendingOutputBuffers.array(),
2132 request.pendingOutputBuffers.size(), 0);
2133
2134 mInFlightMap.removeItemsAt(idx, 1);
2135
2136 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2137 }
2138
2139 // Sanity check - if we have too many in-flight frames, something has
2140 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002141 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002142 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002143 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2144 kInFlightWarnLimitHighSpeed) {
2145 CLOGE("In-flight list too large for high speed configuration: %zu",
2146 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002147 }
2148}
2149
2150
2151void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2152 CaptureResultExtras &resultExtras,
2153 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002154 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002155 bool reprocess,
2156 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002157 if (pendingMetadata.isEmpty())
2158 return;
2159
2160 Mutex::Autolock l(mOutputLock);
2161
2162 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002163 if (reprocess) {
2164 if (frameNumber < mNextReprocessResultFrameNumber) {
2165 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002166 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002167 frameNumber, mNextReprocessResultFrameNumber);
2168 return;
2169 }
2170 mNextReprocessResultFrameNumber = frameNumber + 1;
2171 } else {
2172 if (frameNumber < mNextResultFrameNumber) {
2173 SET_ERR("Out-of-order capture result metadata submitted! "
2174 "(got frame number %d, expecting %d)",
2175 frameNumber, mNextResultFrameNumber);
2176 return;
2177 }
2178 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002179 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002180
2181 CaptureResult captureResult;
2182 captureResult.mResultExtras = resultExtras;
2183 captureResult.mMetadata = pendingMetadata;
2184
2185 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2186 (int32_t*)&frameNumber, 1) != OK) {
2187 SET_ERR("Failed to set frame# in metadata (%d)",
2188 frameNumber);
2189 return;
2190 } else {
2191 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2192 __FUNCTION__, mId, frameNumber);
2193 }
2194
2195 // Append any previous partials to form a complete result
2196 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2197 captureResult.mMetadata.append(collectedPartialResult);
2198 }
2199
2200 captureResult.mMetadata.sort();
2201
2202 // Check that there's a timestamp in the result metadata
2203 camera_metadata_entry entry =
2204 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2205 if (entry.count == 0) {
2206 SET_ERR("No timestamp provided by HAL for frame %d!",
2207 frameNumber);
2208 return;
2209 }
2210
Chien-Yu Chend196d612015-06-22 19:49:01 -07002211 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2212
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002213 // Valid result, insert into queue
2214 List<CaptureResult>::iterator queuedResult =
2215 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2216 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2217 ", burstId = %" PRId32, __FUNCTION__,
2218 queuedResult->mResultExtras.requestId,
2219 queuedResult->mResultExtras.frameNumber,
2220 queuedResult->mResultExtras.burstId);
2221
2222 mResultSignal.signal();
2223}
2224
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002225/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002226 * Camera HAL device callback methods
2227 */
2228
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002229void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002230 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002231
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002232 status_t res;
2233
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002234 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002235 if (result->result == NULL && result->num_output_buffers == 0 &&
2236 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002237 SET_ERR("No result data provided by HAL for frame %d",
2238 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002239 return;
2240 }
Zhijun He204e3292014-07-14 17:09:23 -07002241
2242 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2243 // partial_result to 1 when metadata is included in this result.
2244 if (!mUsePartialResult &&
2245 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2246 result->result != NULL &&
2247 result->partial_result != 1) {
2248 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2249 " if partial result is not supported",
2250 frameNumber, result->partial_result);
2251 return;
2252 }
2253
2254 bool isPartialResult = false;
2255 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002256 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002257 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002258
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002259 // Get shutter timestamp and resultExtras from list of in-flight requests,
2260 // where it was added by the shutter notification for this frame. If the
2261 // shutter timestamp isn't received yet, append the output buffers to the
2262 // in-flight request and they will be returned when the shutter timestamp
2263 // arrives. Update the in-flight status and remove the in-flight entry if
2264 // all result data and shutter timestamp have been received.
2265 nsecs_t shutterTimestamp = 0;
2266
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002267 {
2268 Mutex::Autolock l(mInFlightLock);
2269 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2270 if (idx == NAME_NOT_FOUND) {
2271 SET_ERR("Unknown frame number for capture result: %d",
2272 frameNumber);
2273 return;
2274 }
2275 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002276 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2277 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2278 ", partialResultCount = %d",
2279 __FUNCTION__, request.resultExtras.requestId,
2280 request.resultExtras.frameNumber, request.resultExtras.burstId,
2281 result->partial_result);
2282 // Always update the partial count to the latest one if it's not 0
2283 // (buffers only). When framework aggregates adjacent partial results
2284 // into one, the latest partial count will be used.
2285 if (result->partial_result != 0)
2286 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002287
2288 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002289 if (mUsePartialResult && result->result != NULL) {
2290 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2291 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2292 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2293 " the range of [1, %d] when metadata is included in the result",
2294 frameNumber, result->partial_result, mNumPartialResults);
2295 return;
2296 }
2297 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002298 if (isPartialResult) {
2299 request.partialResult.collectedResult.append(result->result);
2300 }
Zhijun He204e3292014-07-14 17:09:23 -07002301 } else {
2302 camera_metadata_ro_entry_t partialResultEntry;
2303 res = find_camera_metadata_ro_entry(result->result,
2304 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2305 if (res != NAME_NOT_FOUND &&
2306 partialResultEntry.count > 0 &&
2307 partialResultEntry.data.u8[0] ==
2308 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2309 // A partial result. Flag this as such, and collect this
2310 // set of metadata into the in-flight entry.
2311 isPartialResult = true;
2312 request.partialResult.collectedResult.append(
2313 result->result);
2314 request.partialResult.collectedResult.erase(
2315 ANDROID_QUIRKS_PARTIAL_RESULT);
2316 }
2317 }
2318
2319 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002320 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002321 if (!request.partialResult.haveSent3A) {
2322 request.partialResult.haveSent3A =
2323 processPartial3AResult(frameNumber,
2324 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002325 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002326 }
2327 }
2328 }
2329
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002330 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002331 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002332
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002333 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002334 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002335 if (request.haveResultMetadata) {
2336 SET_ERR("Called multiple times with metadata for frame %d",
2337 frameNumber);
2338 return;
2339 }
Zhijun He204e3292014-07-14 17:09:23 -07002340 if (mUsePartialResult &&
2341 !request.partialResult.collectedResult.isEmpty()) {
2342 collectedPartialResult.acquire(
2343 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002344 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002345 request.haveResultMetadata = true;
2346 }
2347
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002348 uint32_t numBuffersReturned = result->num_output_buffers;
2349 if (result->input_buffer != NULL) {
2350 if (hasInputBufferInRequest) {
2351 numBuffersReturned += 1;
2352 } else {
2353 ALOGW("%s: Input buffer should be NULL if there is no input"
2354 " buffer sent in the request",
2355 __FUNCTION__);
2356 }
2357 }
2358 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002359 if (request.numBuffersLeft < 0) {
2360 SET_ERR("Too many buffers returned for frame %d",
2361 frameNumber);
2362 return;
2363 }
2364
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002365 camera_metadata_ro_entry_t entry;
2366 res = find_camera_metadata_ro_entry(result->result,
2367 ANDROID_SENSOR_TIMESTAMP, &entry);
2368 if (res == OK && entry.count == 1) {
2369 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002370 }
2371
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002372 // If shutter event isn't received yet, append the output buffers to
2373 // the in-flight request. Otherwise, return the output buffers to
2374 // streams.
2375 if (shutterTimestamp == 0) {
2376 request.pendingOutputBuffers.appendArray(result->output_buffers,
2377 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002378 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002379 returnOutputBuffers(result->output_buffers,
2380 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002381 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002382
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002383 if (result->result != NULL && !isPartialResult) {
2384 if (shutterTimestamp == 0) {
2385 request.pendingMetadata = result->result;
2386 request.partialResult.collectedResult = collectedPartialResult;
2387 } else {
2388 CameraMetadata metadata;
2389 metadata = result->result;
2390 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002391 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2392 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002393 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002394 }
2395
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002396 removeInFlightRequestIfReadyLocked(idx);
2397 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002398
Zhijun Hef0d962a2014-06-30 10:24:11 -07002399 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002400 if (hasInputBufferInRequest) {
2401 Camera3Stream *stream =
2402 Camera3Stream::cast(result->input_buffer->stream);
2403 res = stream->returnInputBuffer(*(result->input_buffer));
2404 // Note: stream may be deallocated at this point, if this buffer was the
2405 // last reference to it.
2406 if (res != OK) {
2407 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2408 " its stream:%s (%d)", __FUNCTION__,
2409 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002410 }
2411 } else {
2412 ALOGW("%s: Input buffer should be NULL if there is no input"
2413 " buffer sent in the request, skipping input buffer return.",
2414 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002415 }
2416 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002417}
2418
2419void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002420 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002421 NotificationListener *listener;
2422 {
2423 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002424 listener = mListener;
2425 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002426
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002427 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002428 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002429 return;
2430 }
2431
2432 switch (msg->type) {
2433 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002434 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002435 break;
2436 }
2437 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002438 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002439 break;
2440 }
2441 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002442 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002443 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002444 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002445}
2446
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002447void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2448 NotificationListener *listener) {
2449
2450 // Map camera HAL error codes to ICameraDeviceCallback error codes
2451 // Index into this with the HAL error code
2452 static const ICameraDeviceCallbacks::CameraErrorCode
2453 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2454 // 0 = Unused error code
2455 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2456 // 1 = CAMERA3_MSG_ERROR_DEVICE
2457 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2458 // 2 = CAMERA3_MSG_ERROR_REQUEST
2459 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2460 // 3 = CAMERA3_MSG_ERROR_RESULT
2461 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2462 // 4 = CAMERA3_MSG_ERROR_BUFFER
2463 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2464 };
2465
2466 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2467 ((msg.error_code >= 0) &&
2468 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2469 halErrorMap[msg.error_code] :
2470 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2471
2472 int streamId = 0;
2473 if (msg.error_stream != NULL) {
2474 Camera3Stream *stream =
2475 Camera3Stream::cast(msg.error_stream);
2476 streamId = stream->getId();
2477 }
2478 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2479 mId, __FUNCTION__, msg.frame_number,
2480 streamId, msg.error_code);
2481
2482 CaptureResultExtras resultExtras;
2483 switch (errorCode) {
2484 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2485 // SET_ERR calls notifyError
2486 SET_ERR("Camera HAL reported serious device error");
2487 break;
2488 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2489 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2490 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2491 {
2492 Mutex::Autolock l(mInFlightLock);
2493 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2494 if (idx >= 0) {
2495 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2496 r.requestStatus = msg.error_code;
2497 resultExtras = r.resultExtras;
2498 } else {
2499 resultExtras.frameNumber = msg.frame_number;
2500 ALOGE("Camera %d: %s: cannot find in-flight request on "
2501 "frame %" PRId64 " error", mId, __FUNCTION__,
2502 resultExtras.frameNumber);
2503 }
2504 }
2505 if (listener != NULL) {
2506 listener->notifyError(errorCode, resultExtras);
2507 } else {
2508 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2509 }
2510 break;
2511 default:
2512 // SET_ERR calls notifyError
2513 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2514 break;
2515 }
2516}
2517
2518void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2519 NotificationListener *listener) {
2520 ssize_t idx;
2521 // Verify ordering of shutter notifications
2522 {
2523 Mutex::Autolock l(mOutputLock);
2524 // TODO: need to track errors for tighter bounds on expected frame number.
2525 if (msg.frame_number < mNextShutterFrameNumber) {
2526 SET_ERR("Shutter notification out-of-order. Expected "
2527 "notification for frame %d, got frame %d",
2528 mNextShutterFrameNumber, msg.frame_number);
2529 return;
2530 }
2531 mNextShutterFrameNumber = msg.frame_number + 1;
2532 }
2533
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002534 // Set timestamp for the request in the in-flight tracking
2535 // and get the request ID to send upstream
2536 {
2537 Mutex::Autolock l(mInFlightLock);
2538 idx = mInFlightMap.indexOfKey(msg.frame_number);
2539 if (idx >= 0) {
2540 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002541
2542 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2543 mId, __FUNCTION__,
2544 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2545 // Call listener, if any
2546 if (listener != NULL) {
2547 listener->notifyShutter(r.resultExtras, msg.timestamp);
2548 }
2549
2550 r.shutterTimestamp = msg.timestamp;
2551
2552 // send pending result and buffers
2553 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002554 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002555 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002556 returnOutputBuffers(r.pendingOutputBuffers.array(),
2557 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2558 r.pendingOutputBuffers.clear();
2559
2560 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002561 }
2562 }
2563 if (idx < 0) {
2564 SET_ERR("Shutter notification for non-existent frame number %d",
2565 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002566 }
2567}
2568
2569
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002570CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002571 ALOGV("%s", __FUNCTION__);
2572
Igor Murashkin1e479c02013-09-06 16:55:14 -07002573 CameraMetadata retVal;
2574
2575 if (mRequestThread != NULL) {
2576 retVal = mRequestThread->getLatestRequest();
2577 }
2578
Igor Murashkin1e479c02013-09-06 16:55:14 -07002579 return retVal;
2580}
2581
Jianing Weicb0652e2014-03-12 18:29:36 -07002582
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002583/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002584 * RequestThread inner class methods
2585 */
2586
2587Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002588 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002589 camera3_device_t *hal3Device,
2590 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002591 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002592 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002593 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002594 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002595 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002596 mReconfigured(false),
2597 mDoPause(false),
2598 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002599 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002600 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002601 mCurrentAfTriggerId(0),
2602 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002603 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2604 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002605 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002606}
2607
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002608void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002609 NotificationListener *listener) {
2610 Mutex::Autolock l(mRequestLock);
2611 mListener = listener;
2612}
2613
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002614void Camera3Device::RequestThread::configurationComplete() {
2615 Mutex::Autolock l(mRequestLock);
2616 mReconfigured = true;
2617}
2618
Jianing Wei90e59c92014-03-12 18:29:36 -07002619status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002620 List<sp<CaptureRequest> > &requests,
2621 /*out*/
2622 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002623 Mutex::Autolock l(mRequestLock);
2624 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2625 ++it) {
2626 mRequestQueue.push_back(*it);
2627 }
2628
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002629 if (lastFrameNumber != NULL) {
2630 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2631 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2632 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2633 *lastFrameNumber);
2634 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002635
Jianing Wei90e59c92014-03-12 18:29:36 -07002636 unpauseForNewRequests();
2637
2638 return OK;
2639}
2640
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002641
2642status_t Camera3Device::RequestThread::queueTrigger(
2643 RequestTrigger trigger[],
2644 size_t count) {
2645
2646 Mutex::Autolock l(mTriggerMutex);
2647 status_t ret;
2648
2649 for (size_t i = 0; i < count; ++i) {
2650 ret = queueTriggerLocked(trigger[i]);
2651
2652 if (ret != OK) {
2653 return ret;
2654 }
2655 }
2656
2657 return OK;
2658}
2659
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002660int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2661 sp<Camera3Device> d = device.promote();
2662 if (d != NULL) return d->mId;
2663 return 0;
2664}
2665
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002666status_t Camera3Device::RequestThread::queueTriggerLocked(
2667 RequestTrigger trigger) {
2668
2669 uint32_t tag = trigger.metadataTag;
2670 ssize_t index = mTriggerMap.indexOfKey(tag);
2671
2672 switch (trigger.getTagType()) {
2673 case TYPE_BYTE:
2674 // fall-through
2675 case TYPE_INT32:
2676 break;
2677 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002678 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2679 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002680 return INVALID_OPERATION;
2681 }
2682
2683 /**
2684 * Collect only the latest trigger, since we only have 1 field
2685 * in the request settings per trigger tag, and can't send more than 1
2686 * trigger per request.
2687 */
2688 if (index != NAME_NOT_FOUND) {
2689 mTriggerMap.editValueAt(index) = trigger;
2690 } else {
2691 mTriggerMap.add(tag, trigger);
2692 }
2693
2694 return OK;
2695}
2696
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002697status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002698 const RequestList &requests,
2699 /*out*/
2700 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002701 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002702 if (lastFrameNumber != NULL) {
2703 *lastFrameNumber = mRepeatingLastFrameNumber;
2704 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002705 mRepeatingRequests.clear();
2706 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2707 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002708
2709 unpauseForNewRequests();
2710
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002711 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002712 return OK;
2713}
2714
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002715bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2716 if (mRepeatingRequests.empty()) {
2717 return false;
2718 }
2719 int32_t requestId = requestIn->mResultExtras.requestId;
2720 const RequestList &repeatRequests = mRepeatingRequests;
2721 // All repeating requests are guaranteed to have same id so only check first quest
2722 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2723 return (firstRequest->mResultExtras.requestId == requestId);
2724}
2725
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002726status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002727 Mutex::Autolock l(mRequestLock);
2728 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002729 if (lastFrameNumber != NULL) {
2730 *lastFrameNumber = mRepeatingLastFrameNumber;
2731 }
2732 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002733 return OK;
2734}
2735
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002736status_t Camera3Device::RequestThread::clear(
2737 NotificationListener *listener,
2738 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002739 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002740 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002741
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002742 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002743
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002744 // Send errors for all requests pending in the request queue, including
2745 // pending repeating requests
2746 if (listener != NULL) {
2747 for (RequestList::iterator it = mRequestQueue.begin();
2748 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002749 // Abort the input buffers for reprocess requests.
2750 if ((*it)->mInputStream != NULL) {
2751 camera3_stream_buffer_t inputBuffer;
2752 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2753 if (res != OK) {
2754 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2755 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2756 } else {
2757 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2758 if (res != OK) {
2759 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2760 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2761 }
2762 }
2763 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002764 // Set the frame number this request would have had, if it
2765 // had been submitted; this frame number will not be reused.
2766 // The requestId and burstId fields were set when the request was
2767 // submitted originally (in convertMetadataListToRequestListLocked)
2768 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2769 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2770 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002771 }
2772 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002773 mRequestQueue.clear();
2774 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002775 if (lastFrameNumber != NULL) {
2776 *lastFrameNumber = mRepeatingLastFrameNumber;
2777 }
2778 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002779 return OK;
2780}
2781
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002782status_t Camera3Device::RequestThread::flush() {
2783 ATRACE_CALL();
2784 Mutex::Autolock l(mFlushLock);
2785
2786 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2787 return mHal3Device->ops->flush(mHal3Device);
2788 }
2789
2790 return -ENOTSUP;
2791}
2792
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002793void Camera3Device::RequestThread::setPaused(bool paused) {
2794 Mutex::Autolock l(mPauseLock);
2795 mDoPause = paused;
2796 mDoPauseSignal.signal();
2797}
2798
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002799status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2800 int32_t requestId, nsecs_t timeout) {
2801 Mutex::Autolock l(mLatestRequestMutex);
2802 status_t res;
2803 while (mLatestRequestId != requestId) {
2804 nsecs_t startTime = systemTime();
2805
2806 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2807 if (res != OK) return res;
2808
2809 timeout -= (systemTime() - startTime);
2810 }
2811
2812 return OK;
2813}
2814
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002815void Camera3Device::RequestThread::requestExit() {
2816 // Call parent to set up shutdown
2817 Thread::requestExit();
2818 // The exit from any possible waits
2819 mDoPauseSignal.signal();
2820 mRequestSignal.signal();
2821}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002822
Chien-Yu Chend196d612015-06-22 19:49:01 -07002823
2824/**
2825 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2826 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2827 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2828 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2829 * request.
2830 */
2831void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2832 request->mAeTriggerCancelOverride.applyAeLock = false;
2833 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2834
2835 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2836 return;
2837 }
2838
2839 camera_metadata_entry_t aePrecaptureTrigger =
2840 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2841 if (aePrecaptureTrigger.count > 0 &&
2842 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2843 // Always override CANCEL to IDLE
2844 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2845 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2846 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2847 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2848 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2849
2850 if (mAeLockAvailable == true) {
2851 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2852 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2853 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2854 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2855 request->mAeTriggerCancelOverride.applyAeLock = true;
2856 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2857 }
2858 }
2859 }
2860}
2861
2862/**
2863 * Override result metadata for cancelling AE precapture trigger applied in
2864 * handleAePrecaptureCancelRequest().
2865 */
2866void Camera3Device::overrideResultForPrecaptureCancel(
2867 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2868 if (aeTriggerCancelOverride.applyAeLock) {
2869 // Only devices <= v3.2 should have this override
2870 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2871 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2872 }
2873
2874 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2875 // Only devices <= v3.2 should have this override
2876 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2877 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2878 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2879 }
2880}
2881
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002882bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002883 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002884 status_t res;
2885
2886 // Handle paused state.
2887 if (waitIfPaused()) {
2888 return true;
2889 }
2890
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002891 // Wait for the next batch of requests.
2892 waitForNextRequestBatch();
2893 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002894 return true;
2895 }
2896
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002897 // Get the latest request ID, if any
2898 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002899 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002900 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002901 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002902 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002903 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002904 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
2905 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002906 }
2907
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002908 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002909 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002910 if (res == TIMED_OUT) {
2911 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002912 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002913 return true;
2914 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002915 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002916 return false;
2917 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002918
Zhijun Hecc27e112013-10-03 16:12:43 -07002919 // Inform waitUntilRequestProcessed thread of a new request ID
2920 {
2921 Mutex::Autolock al(mLatestRequestMutex);
2922
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002923 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07002924 mLatestRequestSignal.signal();
2925 }
2926
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002927 // Submit a batch of requests to HAL.
2928 // Use flush lock only when submitting multilple requests in a batch.
2929 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
2930 // which may take a long time to finish so synchronizing flush() and
2931 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
2932 // For now, only synchronize for high speed recording and we should figure something out for
2933 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002934 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002935
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002936 if (useFlushLock) {
2937 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002938 }
2939
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002940 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002941 mNextRequests.size());
2942 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002943 // Submit request and block until ready for next one
2944 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
2945 ATRACE_BEGIN("camera3->process_capture_request");
2946 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
2947 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07002948
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002949 if (res != OK) {
2950 // Should only get a failure here for malformed requests or device-level
2951 // errors, so consider all errors fatal. Bad metadata failures should
2952 // come through notify.
2953 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
2954 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
2955 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002956 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002957 if (useFlushLock) {
2958 mFlushLock.unlock();
2959 }
2960 return false;
2961 }
2962
2963 // Mark that the request has be submitted successfully.
2964 nextRequest.submitted = true;
2965
2966 // Update the latest request sent to HAL
2967 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
2968 Mutex::Autolock al(mLatestRequestMutex);
2969
2970 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
2971 mLatestRequest.acquire(cloned);
2972 }
2973
2974 if (nextRequest.halRequest.settings != NULL) {
2975 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
2976 }
2977
2978 // Remove any previously queued triggers (after unlock)
2979 res = removeTriggers(mPrevRequest);
2980 if (res != OK) {
2981 SET_ERR("RequestThread: Unable to remove triggers "
2982 "(capture request %d, HAL device: %s (%d)",
2983 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002984 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002985 if (useFlushLock) {
2986 mFlushLock.unlock();
2987 }
2988 return false;
2989 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07002990 }
2991
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002992 if (useFlushLock) {
2993 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002994 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002995
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002996 // Unset as current request
2997 {
2998 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002999 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003000 }
3001
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003002 return true;
3003}
3004
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003005status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003006 ATRACE_CALL();
3007
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003008 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003009 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3010 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3011 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3012
3013 // Prepare a request to HAL
3014 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3015
3016 // Insert any queued triggers (before metadata is locked)
3017 status_t res = insertTriggers(captureRequest);
3018
3019 if (res < 0) {
3020 SET_ERR("RequestThread: Unable to insert triggers "
3021 "(capture request %d, HAL device: %s (%d)",
3022 halRequest->frame_number, strerror(-res), res);
3023 return INVALID_OPERATION;
3024 }
3025 int triggerCount = res;
3026 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3027 mPrevTriggers = triggerCount;
3028
3029 // If the request is the same as last, or we had triggers last time
3030 if (mPrevRequest != captureRequest || triggersMixedIn) {
3031 /**
3032 * HAL workaround:
3033 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3034 */
3035 res = addDummyTriggerIds(captureRequest);
3036 if (res != OK) {
3037 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3038 "(capture request %d, HAL device: %s (%d)",
3039 halRequest->frame_number, strerror(-res), res);
3040 return INVALID_OPERATION;
3041 }
3042
3043 /**
3044 * The request should be presorted so accesses in HAL
3045 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3046 */
3047 captureRequest->mSettings.sort();
3048 halRequest->settings = captureRequest->mSettings.getAndLock();
3049 mPrevRequest = captureRequest;
3050 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3051
3052 IF_ALOGV() {
3053 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3054 find_camera_metadata_ro_entry(
3055 halRequest->settings,
3056 ANDROID_CONTROL_AF_TRIGGER,
3057 &e
3058 );
3059 if (e.count > 0) {
3060 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3061 __FUNCTION__,
3062 halRequest->frame_number,
3063 e.data.u8[0]);
3064 }
3065 }
3066 } else {
3067 // leave request.settings NULL to indicate 'reuse latest given'
3068 ALOGVV("%s: Request settings are REUSED",
3069 __FUNCTION__);
3070 }
3071
3072 uint32_t totalNumBuffers = 0;
3073
3074 // Fill in buffers
3075 if (captureRequest->mInputStream != NULL) {
3076 halRequest->input_buffer = &captureRequest->mInputBuffer;
3077 totalNumBuffers += 1;
3078 } else {
3079 halRequest->input_buffer = NULL;
3080 }
3081
3082 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3083 captureRequest->mOutputStreams.size());
3084 halRequest->output_buffers = outputBuffers->array();
3085 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3086 res = captureRequest->mOutputStreams.editItemAt(i)->
3087 getBuffer(&outputBuffers->editItemAt(i));
3088 if (res != OK) {
3089 // Can't get output buffer from gralloc queue - this could be due to
3090 // abandoned queue or other consumer misbehavior, so not a fatal
3091 // error
3092 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3093 " %s (%d)", strerror(-res), res);
3094
3095 return TIMED_OUT;
3096 }
3097 halRequest->num_output_buffers++;
3098 }
3099 totalNumBuffers += halRequest->num_output_buffers;
3100
3101 // Log request in the in-flight queue
3102 sp<Camera3Device> parent = mParent.promote();
3103 if (parent == NULL) {
3104 // Should not happen, and nowhere to send errors to, so just log it
3105 CLOGE("RequestThread: Parent is gone");
3106 return INVALID_OPERATION;
3107 }
3108 res = parent->registerInFlight(halRequest->frame_number,
3109 totalNumBuffers, captureRequest->mResultExtras,
3110 /*hasInput*/halRequest->input_buffer != NULL,
3111 captureRequest->mAeTriggerCancelOverride);
3112 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3113 ", burstId = %" PRId32 ".",
3114 __FUNCTION__,
3115 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3116 captureRequest->mResultExtras.burstId);
3117 if (res != OK) {
3118 SET_ERR("RequestThread: Unable to register new in-flight request:"
3119 " %s (%d)", strerror(-res), res);
3120 return INVALID_OPERATION;
3121 }
3122 }
3123
3124 return OK;
3125}
3126
Igor Murashkin1e479c02013-09-06 16:55:14 -07003127CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3128 Mutex::Autolock al(mLatestRequestMutex);
3129
3130 ALOGV("RequestThread::%s", __FUNCTION__);
3131
3132 return mLatestRequest;
3133}
3134
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003135bool Camera3Device::RequestThread::isStreamPending(
3136 sp<Camera3StreamInterface>& stream) {
3137 Mutex::Autolock l(mRequestLock);
3138
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003139 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003140 if (!nextRequest.submitted) {
3141 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3142 if (stream == s) return true;
3143 }
3144 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003145 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003146 }
3147
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003148 for (const auto& request : mRequestQueue) {
3149 for (const auto& s : request->mOutputStreams) {
3150 if (stream == s) return true;
3151 }
3152 if (stream == request->mInputStream) return true;
3153 }
3154
3155 for (const auto& request : mRepeatingRequests) {
3156 for (const auto& s : request->mOutputStreams) {
3157 if (stream == s) return true;
3158 }
3159 if (stream == request->mInputStream) return true;
3160 }
3161
3162 return false;
3163}
Jianing Weicb0652e2014-03-12 18:29:36 -07003164
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003165void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3166 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003167 return;
3168 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003169
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003170 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003171 // Skip the ones that have been submitted successfully.
3172 if (nextRequest.submitted) {
3173 continue;
3174 }
3175
3176 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3177 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3178 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3179
3180 if (halRequest->settings != NULL) {
3181 captureRequest->mSettings.unlock(halRequest->settings);
3182 }
3183
3184 if (captureRequest->mInputStream != NULL) {
3185 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3186 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3187 }
3188
3189 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3190 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3191 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3192 }
3193
3194 if (sendRequestError) {
3195 Mutex::Autolock l(mRequestLock);
3196 if (mListener != NULL) {
3197 mListener->notifyError(
3198 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3199 captureRequest->mResultExtras);
3200 }
3201 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003202 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003203
3204 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003205 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003206}
3207
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003208void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003209 // Optimized a bit for the simple steady-state case (single repeating
3210 // request), to avoid putting that request in the queue temporarily.
3211 Mutex::Autolock l(mRequestLock);
3212
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003213 assert(mNextRequests.empty());
3214
3215 NextRequest nextRequest;
3216 nextRequest.captureRequest = waitForNextRequestLocked();
3217 if (nextRequest.captureRequest == nullptr) {
3218 return;
3219 }
3220
3221 nextRequest.halRequest = camera3_capture_request_t();
3222 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003223 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003224
3225 // Wait for additional requests
3226 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3227
3228 for (size_t i = 1; i < batchSize; i++) {
3229 NextRequest additionalRequest;
3230 additionalRequest.captureRequest = waitForNextRequestLocked();
3231 if (additionalRequest.captureRequest == nullptr) {
3232 break;
3233 }
3234
3235 additionalRequest.halRequest = camera3_capture_request_t();
3236 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003237 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003238 }
3239
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003240 if (mNextRequests.size() < batchSize) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003241 ALOGE("RequestThread: only get %d out of %d requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003242 mNextRequests.size(), batchSize);
3243 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003244 }
3245
3246 return;
3247}
3248
3249sp<Camera3Device::CaptureRequest>
3250 Camera3Device::RequestThread::waitForNextRequestLocked() {
3251 status_t res;
3252 sp<CaptureRequest> nextRequest;
3253
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003254 while (mRequestQueue.empty()) {
3255 if (!mRepeatingRequests.empty()) {
3256 // Always atomically enqueue all requests in a repeating request
3257 // list. Guarantees a complete in-sequence set of captures to
3258 // application.
3259 const RequestList &requests = mRepeatingRequests;
3260 RequestList::const_iterator firstRequest =
3261 requests.begin();
3262 nextRequest = *firstRequest;
3263 mRequestQueue.insert(mRequestQueue.end(),
3264 ++firstRequest,
3265 requests.end());
3266 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003267
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003268 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003269
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003270 break;
3271 }
3272
3273 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3274
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003275 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3276 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003277 Mutex::Autolock pl(mPauseLock);
3278 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003279 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003280 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003281 // Let the tracker know
3282 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3283 if (statusTracker != 0) {
3284 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3285 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003286 }
3287 // Stop waiting for now and let thread management happen
3288 return NULL;
3289 }
3290 }
3291
3292 if (nextRequest == NULL) {
3293 // Don't have a repeating request already in hand, so queue
3294 // must have an entry now.
3295 RequestList::iterator firstRequest =
3296 mRequestQueue.begin();
3297 nextRequest = *firstRequest;
3298 mRequestQueue.erase(firstRequest);
3299 }
3300
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003301 // In case we've been unpaused by setPaused clearing mDoPause, need to
3302 // update internal pause state (capture/setRepeatingRequest unpause
3303 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003304 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003305 if (mPaused) {
3306 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3307 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3308 if (statusTracker != 0) {
3309 statusTracker->markComponentActive(mStatusId);
3310 }
3311 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003312 mPaused = false;
3313
3314 // Check if we've reconfigured since last time, and reset the preview
3315 // request if so. Can't use 'NULL request == repeat' across configure calls.
3316 if (mReconfigured) {
3317 mPrevRequest.clear();
3318 mReconfigured = false;
3319 }
3320
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003321 if (nextRequest != NULL) {
3322 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003323 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3324 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003325
3326 // Since RequestThread::clear() removes buffers from the input stream,
3327 // get the right buffer here before unlocking mRequestLock
3328 if (nextRequest->mInputStream != NULL) {
3329 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3330 if (res != OK) {
3331 // Can't get input buffer from gralloc queue - this could be due to
3332 // disconnected queue or other producer misbehavior, so not a fatal
3333 // error
3334 ALOGE("%s: Can't get input buffer, skipping request:"
3335 " %s (%d)", __FUNCTION__, strerror(-res), res);
3336 if (mListener != NULL) {
3337 mListener->notifyError(
3338 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3339 nextRequest->mResultExtras);
3340 }
3341 return NULL;
3342 }
3343 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003344 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003345
3346 handleAePrecaptureCancelRequest(nextRequest);
3347
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003348 return nextRequest;
3349}
3350
3351bool Camera3Device::RequestThread::waitIfPaused() {
3352 status_t res;
3353 Mutex::Autolock l(mPauseLock);
3354 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003355 if (mPaused == false) {
3356 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003357 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3358 // Let the tracker know
3359 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3360 if (statusTracker != 0) {
3361 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3362 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003363 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003364
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003365 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003366 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003367 return true;
3368 }
3369 }
3370 // We don't set mPaused to false here, because waitForNextRequest needs
3371 // to further manage the paused state in case of starvation.
3372 return false;
3373}
3374
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003375void Camera3Device::RequestThread::unpauseForNewRequests() {
3376 // With work to do, mark thread as unpaused.
3377 // If paused by request (setPaused), don't resume, to avoid
3378 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003379 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003380 Mutex::Autolock p(mPauseLock);
3381 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003382 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3383 if (mPaused) {
3384 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3385 if (statusTracker != 0) {
3386 statusTracker->markComponentActive(mStatusId);
3387 }
3388 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003389 mPaused = false;
3390 }
3391}
3392
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003393void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3394 sp<Camera3Device> parent = mParent.promote();
3395 if (parent != NULL) {
3396 va_list args;
3397 va_start(args, fmt);
3398
3399 parent->setErrorStateV(fmt, args);
3400
3401 va_end(args);
3402 }
3403}
3404
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003405status_t Camera3Device::RequestThread::insertTriggers(
3406 const sp<CaptureRequest> &request) {
3407
3408 Mutex::Autolock al(mTriggerMutex);
3409
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003410 sp<Camera3Device> parent = mParent.promote();
3411 if (parent == NULL) {
3412 CLOGE("RequestThread: Parent is gone");
3413 return DEAD_OBJECT;
3414 }
3415
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003416 CameraMetadata &metadata = request->mSettings;
3417 size_t count = mTriggerMap.size();
3418
3419 for (size_t i = 0; i < count; ++i) {
3420 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003421 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003422
3423 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3424 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3425 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003426 if (isAeTrigger) {
3427 request->mResultExtras.precaptureTriggerId = triggerId;
3428 mCurrentPreCaptureTriggerId = triggerId;
3429 } else {
3430 request->mResultExtras.afTriggerId = triggerId;
3431 mCurrentAfTriggerId = triggerId;
3432 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003433 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3434 continue; // Trigger ID tag is deprecated since device HAL 3.2
3435 }
3436 }
3437
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003438 camera_metadata_entry entry = metadata.find(tag);
3439
3440 if (entry.count > 0) {
3441 /**
3442 * Already has an entry for this trigger in the request.
3443 * Rewrite it with our requested trigger value.
3444 */
3445 RequestTrigger oldTrigger = trigger;
3446
3447 oldTrigger.entryValue = entry.data.u8[0];
3448
3449 mTriggerReplacedMap.add(tag, oldTrigger);
3450 } else {
3451 /**
3452 * More typical, no trigger entry, so we just add it
3453 */
3454 mTriggerRemovedMap.add(tag, trigger);
3455 }
3456
3457 status_t res;
3458
3459 switch (trigger.getTagType()) {
3460 case TYPE_BYTE: {
3461 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3462 res = metadata.update(tag,
3463 &entryValue,
3464 /*count*/1);
3465 break;
3466 }
3467 case TYPE_INT32:
3468 res = metadata.update(tag,
3469 &trigger.entryValue,
3470 /*count*/1);
3471 break;
3472 default:
3473 ALOGE("%s: Type not supported: 0x%x",
3474 __FUNCTION__,
3475 trigger.getTagType());
3476 return INVALID_OPERATION;
3477 }
3478
3479 if (res != OK) {
3480 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3481 ", value %d", __FUNCTION__, trigger.getTagName(),
3482 trigger.entryValue);
3483 return res;
3484 }
3485
3486 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3487 trigger.getTagName(),
3488 trigger.entryValue);
3489 }
3490
3491 mTriggerMap.clear();
3492
3493 return count;
3494}
3495
3496status_t Camera3Device::RequestThread::removeTriggers(
3497 const sp<CaptureRequest> &request) {
3498 Mutex::Autolock al(mTriggerMutex);
3499
3500 CameraMetadata &metadata = request->mSettings;
3501
3502 /**
3503 * Replace all old entries with their old values.
3504 */
3505 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3506 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3507
3508 status_t res;
3509
3510 uint32_t tag = trigger.metadataTag;
3511 switch (trigger.getTagType()) {
3512 case TYPE_BYTE: {
3513 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3514 res = metadata.update(tag,
3515 &entryValue,
3516 /*count*/1);
3517 break;
3518 }
3519 case TYPE_INT32:
3520 res = metadata.update(tag,
3521 &trigger.entryValue,
3522 /*count*/1);
3523 break;
3524 default:
3525 ALOGE("%s: Type not supported: 0x%x",
3526 __FUNCTION__,
3527 trigger.getTagType());
3528 return INVALID_OPERATION;
3529 }
3530
3531 if (res != OK) {
3532 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3533 ", trigger value %d", __FUNCTION__,
3534 trigger.getTagName(), trigger.entryValue);
3535 return res;
3536 }
3537 }
3538 mTriggerReplacedMap.clear();
3539
3540 /**
3541 * Remove all new entries.
3542 */
3543 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3544 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3545 status_t res = metadata.erase(trigger.metadataTag);
3546
3547 if (res != OK) {
3548 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3549 ", trigger value %d", __FUNCTION__,
3550 trigger.getTagName(), trigger.entryValue);
3551 return res;
3552 }
3553 }
3554 mTriggerRemovedMap.clear();
3555
3556 return OK;
3557}
3558
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003559status_t Camera3Device::RequestThread::addDummyTriggerIds(
3560 const sp<CaptureRequest> &request) {
3561 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3562 static const int32_t dummyTriggerId = 1;
3563 status_t res;
3564
3565 CameraMetadata &metadata = request->mSettings;
3566
3567 // If AF trigger is active, insert a dummy AF trigger ID if none already
3568 // exists
3569 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3570 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3571 if (afTrigger.count > 0 &&
3572 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3573 afId.count == 0) {
3574 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3575 if (res != OK) return res;
3576 }
3577
3578 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3579 // if none already exists
3580 camera_metadata_entry pcTrigger =
3581 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3582 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3583 if (pcTrigger.count > 0 &&
3584 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3585 pcId.count == 0) {
3586 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3587 &dummyTriggerId, 1);
3588 if (res != OK) return res;
3589 }
3590
3591 return OK;
3592}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003593
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003594/**
3595 * PreparerThread inner class methods
3596 */
3597
3598Camera3Device::PreparerThread::PreparerThread() :
3599 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3600}
3601
3602Camera3Device::PreparerThread::~PreparerThread() {
3603 Thread::requestExitAndWait();
3604 if (mCurrentStream != nullptr) {
3605 mCurrentStream->cancelPrepare();
3606 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3607 mCurrentStream.clear();
3608 }
3609 clear();
3610}
3611
Ruben Brunkc78ac262015-08-13 17:58:46 -07003612status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003613 status_t res;
3614
3615 Mutex::Autolock l(mLock);
3616
Ruben Brunkc78ac262015-08-13 17:58:46 -07003617 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003618 if (res == OK) {
3619 // No preparation needed, fire listener right off
3620 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3621 if (mListener) {
3622 mListener->notifyPrepared(stream->getId());
3623 }
3624 return OK;
3625 } else if (res != NOT_ENOUGH_DATA) {
3626 return res;
3627 }
3628
3629 // Need to prepare, start up thread if necessary
3630 if (!mActive) {
3631 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3632 // isn't running
3633 Thread::requestExitAndWait();
3634 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3635 if (res != OK) {
3636 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3637 if (mListener) {
3638 mListener->notifyPrepared(stream->getId());
3639 }
3640 return res;
3641 }
3642 mCancelNow = false;
3643 mActive = true;
3644 ALOGV("%s: Preparer stream started", __FUNCTION__);
3645 }
3646
3647 // queue up the work
3648 mPendingStreams.push_back(stream);
3649 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3650
3651 return OK;
3652}
3653
3654status_t Camera3Device::PreparerThread::clear() {
3655 status_t res;
3656
3657 Mutex::Autolock l(mLock);
3658
3659 for (const auto& stream : mPendingStreams) {
3660 stream->cancelPrepare();
3661 }
3662 mPendingStreams.clear();
3663 mCancelNow = true;
3664
3665 return OK;
3666}
3667
3668void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3669 Mutex::Autolock l(mLock);
3670 mListener = listener;
3671}
3672
3673bool Camera3Device::PreparerThread::threadLoop() {
3674 status_t res;
3675 {
3676 Mutex::Autolock l(mLock);
3677 if (mCurrentStream == nullptr) {
3678 // End thread if done with work
3679 if (mPendingStreams.empty()) {
3680 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3681 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3682 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3683 mActive = false;
3684 return false;
3685 }
3686
3687 // Get next stream to prepare
3688 auto it = mPendingStreams.begin();
3689 mCurrentStream = *it;
3690 mPendingStreams.erase(it);
3691 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3692 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3693 } else if (mCancelNow) {
3694 mCurrentStream->cancelPrepare();
3695 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3696 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3697 mCurrentStream.clear();
3698 mCancelNow = false;
3699 return true;
3700 }
3701 }
3702
3703 res = mCurrentStream->prepareNextBuffer();
3704 if (res == NOT_ENOUGH_DATA) return true;
3705 if (res != OK) {
3706 // Something bad happened; try to recover by cancelling prepare and
3707 // signalling listener anyway
3708 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3709 mCurrentStream->getId(), res, strerror(-res));
3710 mCurrentStream->cancelPrepare();
3711 }
3712
3713 // This stream has finished, notify listener
3714 Mutex::Autolock l(mLock);
3715 if (mListener) {
3716 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3717 mCurrentStream->getId());
3718 mListener->notifyPrepared(mCurrentStream->getId());
3719 }
3720
3721 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3722 mCurrentStream.clear();
3723
3724 return true;
3725}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003726
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003727/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003728 * Static callback forwarding methods from HAL to instance
3729 */
3730
3731void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3732 const camera3_capture_result *result) {
3733 Camera3Device *d =
3734 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003735
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003736 d->processCaptureResult(result);
3737}
3738
3739void Camera3Device::sNotify(const camera3_callback_ops *cb,
3740 const camera3_notify_msg *msg) {
3741 Camera3Device *d =
3742 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3743 d->notify(msg);
3744}
3745
3746}; // namespace android