blob: 9d725def7888a451f89c0cf1ee006129415100d1 [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),
Zhijun He204e3292014-07-14 17:09:23 -070063 mUsePartialResult(false),
64 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070065 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070066 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070067 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070068 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080069{
70 ATRACE_CALL();
71 camera3_callback_ops::notify = &sNotify;
72 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
73 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
74}
75
76Camera3Device::~Camera3Device()
77{
78 ATRACE_CALL();
79 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
80 disconnect();
81}
82
Igor Murashkin71381052013-03-04 14:53:08 -080083int Camera3Device::getId() const {
84 return mId;
85}
86
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080087/**
88 * CameraDeviceBase interface
89 */
90
Yin-Chia Yehe074a932015-01-30 10:29:02 -080091status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080092{
93 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070094 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080095 Mutex::Autolock l(mLock);
96
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080097 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080098 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070099 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800100 return INVALID_OPERATION;
101 }
102
103 /** Open HAL device */
104
105 status_t res;
106 String8 deviceName = String8::format("%d", mId);
107
108 camera3_device_t *device;
109
Zhijun He213ce792013-11-19 08:45:15 -0800110 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800111 res = module->open(deviceName.string(),
112 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800113 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800114
115 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700116 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117 return res;
118 }
119
120 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700121 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700123 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700124 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800125 device->common.version);
126 device->common.close(&device->common);
127 return BAD_VALUE;
128 }
129
130 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800131 res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700132 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800133 if (res != OK) return res;
134
135 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700136 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
137 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700138 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 device->common.close(&device->common);
140 return BAD_VALUE;
141 }
142
143 /** Initialize device with callback functions */
144
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700145 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800146 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700147 ATRACE_END();
148
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800149 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700150 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
151 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800152 device->common.close(&device->common);
153 return BAD_VALUE;
154 }
155
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700156 /** Start up status tracker thread */
157 mStatusTracker = new StatusTracker(this);
158 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
159 if (res != OK) {
160 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
161 strerror(-res), res);
162 device->common.close(&device->common);
163 mStatusTracker.clear();
164 return res;
165 }
166
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800167 /** Start up request queue thread */
168
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700169 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800170 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800171 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700172 SET_ERR_L("Unable to start request queue thread: %s (%d)",
173 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800174 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800175 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800176 return res;
177 }
178
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700179 mPreparerThread = new PreparerThread();
180
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800181 /** Everything is good to go */
182
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700183 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800184 mDeviceInfo = info.static_camera_characteristics;
185 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700186 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800187 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700188 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700189 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700190 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800191
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700192 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700193 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
194 camera_metadata_entry partialResultsCount =
195 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
196 if (partialResultsCount.count > 0) {
197 mNumPartialResults = partialResultsCount.data.i32[0];
198 mUsePartialResult = (mNumPartialResults > 1);
199 }
200 } else {
201 camera_metadata_entry partialResultsQuirk =
202 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
203 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
204 mUsePartialResult = true;
205 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700206 }
207
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700208 camera_metadata_entry configs =
209 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
210 for (uint32_t i = 0; i < configs.count; i += 4) {
211 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
212 configs.data.i32[i + 3] ==
213 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
214 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
215 configs.data.i32[i + 2]));
216 }
217 }
218
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800219 return OK;
220}
221
222status_t Camera3Device::disconnect() {
223 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700224 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800225
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800226 ALOGV("%s: E", __FUNCTION__);
227
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700228 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800229
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700230 {
231 Mutex::Autolock l(mLock);
232 if (mStatus == STATUS_UNINITIALIZED) return res;
233
234 if (mStatus == STATUS_ACTIVE ||
235 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
236 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700237 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700238 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700239 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700240 } else {
241 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
242 if (res != OK) {
243 SET_ERR_L("Timeout waiting for HAL to drain");
244 // Continue to close device even in case of error
245 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700246 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800247 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800248
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700249 if (mStatus == STATUS_ERROR) {
250 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700251 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700252
253 if (mStatusTracker != NULL) {
254 mStatusTracker->requestExit();
255 }
256
257 if (mRequestThread != NULL) {
258 mRequestThread->requestExit();
259 }
260
261 mOutputStreams.clear();
262 mInputStream.clear();
263 }
264
265 // Joining done without holding mLock, otherwise deadlocks may ensue
266 // as the threads try to access parent state
267 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
268 // HAL may be in a bad state, so waiting for request thread
269 // (which may be stuck in the HAL processCaptureRequest call)
270 // could be dangerous.
271 mRequestThread->join();
272 }
273
274 if (mStatusTracker != NULL) {
275 mStatusTracker->join();
276 }
277
278 {
279 Mutex::Autolock l(mLock);
280
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800281 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700282 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800283
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700284 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800285 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700286 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800287 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700288 mHal3Device = NULL;
289 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800290
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700291 mStatus = STATUS_UNINITIALIZED;
292 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800293
294 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700295 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800296}
297
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700298// For dumping/debugging only -
299// try to acquire a lock a few times, eventually give up to proceed with
300// debug/dump operations
301bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
302 bool gotLock = false;
303 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
304 if (lock.tryLock() == NO_ERROR) {
305 gotLock = true;
306 break;
307 } else {
308 usleep(kDumpSleepDuration);
309 }
310 }
311 return gotLock;
312}
313
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700314Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
315 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
316 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
317 const int STREAM_CONFIGURATION_SIZE = 4;
318 const int STREAM_FORMAT_OFFSET = 0;
319 const int STREAM_WIDTH_OFFSET = 1;
320 const int STREAM_HEIGHT_OFFSET = 2;
321 const int STREAM_IS_INPUT_OFFSET = 3;
322 camera_metadata_ro_entry_t availableStreamConfigs =
323 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
324 if (availableStreamConfigs.count == 0 ||
325 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
326 return Size(0, 0);
327 }
328
329 // Get max jpeg size (area-wise).
330 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
331 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
332 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
333 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
334 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
335 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
336 && format == HAL_PIXEL_FORMAT_BLOB &&
337 (width * height > maxJpegWidth * maxJpegHeight)) {
338 maxJpegWidth = width;
339 maxJpegHeight = height;
340 }
341 }
342 } else {
343 camera_metadata_ro_entry availableJpegSizes =
344 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
345 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
346 return Size(0, 0);
347 }
348
349 // Get max jpeg size (area-wise).
350 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
351 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
352 > (maxJpegWidth * maxJpegHeight)) {
353 maxJpegWidth = availableJpegSizes.data.i32[i];
354 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
355 }
356 }
357 }
358 return Size(maxJpegWidth, maxJpegHeight);
359}
360
Zhijun Hef7da0962014-04-24 13:27:56 -0700361ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700362 // Get max jpeg size (area-wise).
363 Size maxJpegResolution = getMaxJpegResolution();
364 if (maxJpegResolution.width == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700365 ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
366 __FUNCTION__, mId);
367 return BAD_VALUE;
368 }
369
Zhijun Hef7da0962014-04-24 13:27:56 -0700370 // Get max jpeg buffer size
371 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700372 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
373 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700374 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
375 return BAD_VALUE;
376 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700377 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800378 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700379
380 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700381 float scaleFactor = ((float) (width * height)) /
382 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800383 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
384 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700385 if (jpegBufferSize > maxJpegBufferSize) {
386 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700387 }
388
389 return jpegBufferSize;
390}
391
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800392status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
393 ATRACE_CALL();
394 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700395
396 // Try to lock, but continue in case of failure (to avoid blocking in
397 // deadlocks)
398 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
399 bool gotLock = tryLockSpinRightRound(mLock);
400
401 ALOGW_IF(!gotInterfaceLock,
402 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
403 mId, __FUNCTION__);
404 ALOGW_IF(!gotLock,
405 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
406 mId, __FUNCTION__);
407
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800408 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800409
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800410 const char *status =
411 mStatus == STATUS_ERROR ? "ERROR" :
412 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700413 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
414 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800415 mStatus == STATUS_ACTIVE ? "ACTIVE" :
416 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700417
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800418 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700419 if (mStatus == STATUS_ERROR) {
420 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
421 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800422 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700423 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700424 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800425
426 if (mInputStream != NULL) {
427 write(fd, lines.string(), lines.size());
428 mInputStream->dump(fd, args);
429 } else {
430 lines.appendFormat(" No input stream.\n");
431 write(fd, lines.string(), lines.size());
432 }
433 for (size_t i = 0; i < mOutputStreams.size(); i++) {
434 mOutputStreams[i]->dump(fd,args);
435 }
436
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700437 lines = String8(" In-flight requests:\n");
438 if (mInFlightMap.size() == 0) {
439 lines.append(" None\n");
440 } else {
441 for (size_t i = 0; i < mInFlightMap.size(); i++) {
442 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700443 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700444 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800445 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700446 r.numBuffersLeft);
447 }
448 }
449 write(fd, lines.string(), lines.size());
450
Igor Murashkin1e479c02013-09-06 16:55:14 -0700451 {
452 lines = String8(" Last request sent:\n");
453 write(fd, lines.string(), lines.size());
454
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700455 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700456 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
457 }
458
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800459 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700460 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800461 write(fd, lines.string(), lines.size());
462 mHal3Device->ops->dump(mHal3Device, fd);
463 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800464
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700465 if (gotLock) mLock.unlock();
466 if (gotInterfaceLock) mInterfaceLock.unlock();
467
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800468 return OK;
469}
470
471const CameraMetadata& Camera3Device::info() const {
472 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800473 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
474 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700475 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800476 mStatus == STATUS_ERROR ?
477 "when in error state" : "before init");
478 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800479 return mDeviceInfo;
480}
481
Jianing Wei90e59c92014-03-12 18:29:36 -0700482status_t Camera3Device::checkStatusOkToCaptureLocked() {
483 switch (mStatus) {
484 case STATUS_ERROR:
485 CLOGE("Device has encountered a serious error");
486 return INVALID_OPERATION;
487 case STATUS_UNINITIALIZED:
488 CLOGE("Device not initialized");
489 return INVALID_OPERATION;
490 case STATUS_UNCONFIGURED:
491 case STATUS_CONFIGURED:
492 case STATUS_ACTIVE:
493 // OK
494 break;
495 default:
496 SET_ERR_L("Unexpected status: %d", mStatus);
497 return INVALID_OPERATION;
498 }
499 return OK;
500}
501
502status_t Camera3Device::convertMetadataListToRequestListLocked(
503 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
504 if (requestList == NULL) {
505 CLOGE("requestList cannot be NULL.");
506 return BAD_VALUE;
507 }
508
Jianing Weicb0652e2014-03-12 18:29:36 -0700509 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700510 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
511 it != metadataList.end(); ++it) {
512 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
513 if (newRequest == 0) {
514 CLOGE("Can't create capture request");
515 return BAD_VALUE;
516 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700517
518 // Setup burst Id and request Id
519 newRequest->mResultExtras.burstId = burstId++;
520 if (it->exists(ANDROID_REQUEST_ID)) {
521 if (it->find(ANDROID_REQUEST_ID).count == 0) {
522 CLOGE("RequestID entry exists; but must not be empty in metadata");
523 return BAD_VALUE;
524 }
525 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
526 } else {
527 CLOGE("RequestID does not exist in metadata");
528 return BAD_VALUE;
529 }
530
Jianing Wei90e59c92014-03-12 18:29:36 -0700531 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700532
533 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700534 }
535 return OK;
536}
537
Jianing Weicb0652e2014-03-12 18:29:36 -0700538status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800539 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800540
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700541 List<const CameraMetadata> requests;
542 requests.push_back(request);
543 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800544}
545
Jianing Wei90e59c92014-03-12 18:29:36 -0700546status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700547 const List<const CameraMetadata> &requests, bool repeating,
548 /*out*/
549 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700550 ATRACE_CALL();
551 Mutex::Autolock il(mInterfaceLock);
552 Mutex::Autolock l(mLock);
553
554 status_t res = checkStatusOkToCaptureLocked();
555 if (res != OK) {
556 // error logged by previous call
557 return res;
558 }
559
560 RequestList requestList;
561
562 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
563 if (res != OK) {
564 // error logged by previous call
565 return res;
566 }
567
568 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700569 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700570 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700571 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700572 }
573
574 if (res == OK) {
575 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
576 if (res != OK) {
577 SET_ERR_L("Can't transition to active in %f seconds!",
578 kActiveTimeout/1e9);
579 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700580 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
581 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700582 } else {
583 CLOGE("Cannot queue request. Impossible.");
584 return BAD_VALUE;
585 }
586
587 return res;
588}
589
Jianing Weicb0652e2014-03-12 18:29:36 -0700590status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
591 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700592 ATRACE_CALL();
593
Jianing Weicb0652e2014-03-12 18:29:36 -0700594 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700595}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800596
Jianing Weicb0652e2014-03-12 18:29:36 -0700597status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
598 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800599 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800600
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700601 List<const CameraMetadata> requests;
602 requests.push_back(request);
603 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800604}
605
Jianing Weicb0652e2014-03-12 18:29:36 -0700606status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
607 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700608 ATRACE_CALL();
609
Jianing Weicb0652e2014-03-12 18:29:36 -0700610 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700611}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800612
613sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
614 const CameraMetadata &request) {
615 status_t res;
616
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700617 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800618 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700619 // Stream configuration failed due to unsupported configuration.
620 // Device back to unconfigured state. Client might try other configuraitons
621 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
622 CLOGE("No streams configured");
623 return NULL;
624 }
625 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800626 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700627 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800628 return NULL;
629 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700630 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700631 if (mStatus == STATUS_UNCONFIGURED) {
632 CLOGE("No streams configured");
633 return NULL;
634 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800635 }
636
637 sp<CaptureRequest> newRequest = createCaptureRequest(request);
638 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800639}
640
Jianing Weicb0652e2014-03-12 18:29:36 -0700641status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800642 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700643 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800644 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800645
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800646 switch (mStatus) {
647 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700648 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800649 return INVALID_OPERATION;
650 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700651 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800652 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700653 case STATUS_UNCONFIGURED:
654 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800655 case STATUS_ACTIVE:
656 // OK
657 break;
658 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700659 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800660 return INVALID_OPERATION;
661 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700662 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700663
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700664 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800665}
666
667status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
668 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700669 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800670
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700671 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800672}
673
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700674status_t Camera3Device::createInputStream(
675 uint32_t width, uint32_t height, int format, int *id) {
676 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700677 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700678 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700679 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
680 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700681
682 status_t res;
683 bool wasActive = false;
684
685 switch (mStatus) {
686 case STATUS_ERROR:
687 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
688 return INVALID_OPERATION;
689 case STATUS_UNINITIALIZED:
690 ALOGE("%s: Device not initialized", __FUNCTION__);
691 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700692 case STATUS_UNCONFIGURED:
693 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700694 // OK
695 break;
696 case STATUS_ACTIVE:
697 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700698 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700699 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700700 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700701 return res;
702 }
703 wasActive = true;
704 break;
705 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700706 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700707 return INVALID_OPERATION;
708 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700709 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700710
711 if (mInputStream != 0) {
712 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
713 return INVALID_OPERATION;
714 }
715
716 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
717 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700718 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700719
720 mInputStream = newStream;
721
722 *id = mNextStreamId++;
723
724 // Continue captures if active at start
725 if (wasActive) {
726 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
727 res = configureStreamsLocked();
728 if (res != OK) {
729 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
730 __FUNCTION__, mNextStreamId, strerror(-res), res);
731 return res;
732 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700733 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700734 }
735
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700736 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700737 return OK;
738}
739
Igor Murashkin2fba5842013-04-22 14:03:54 -0700740
741status_t Camera3Device::createZslStream(
742 uint32_t width, uint32_t height,
743 int depth,
744 /*out*/
745 int *id,
746 sp<Camera3ZslStream>* zslStream) {
747 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700748 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700749 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700750 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
751 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700752
753 status_t res;
754 bool wasActive = false;
755
756 switch (mStatus) {
757 case STATUS_ERROR:
758 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
759 return INVALID_OPERATION;
760 case STATUS_UNINITIALIZED:
761 ALOGE("%s: Device not initialized", __FUNCTION__);
762 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700763 case STATUS_UNCONFIGURED:
764 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700765 // OK
766 break;
767 case STATUS_ACTIVE:
768 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700769 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700770 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700771 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700772 return res;
773 }
774 wasActive = true;
775 break;
776 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700777 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700778 return INVALID_OPERATION;
779 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700780 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700781
782 if (mInputStream != 0) {
783 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
784 return INVALID_OPERATION;
785 }
786
787 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
788 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700789 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700790
791 res = mOutputStreams.add(mNextStreamId, newStream);
792 if (res < 0) {
793 ALOGE("%s: Can't add new stream to set: %s (%d)",
794 __FUNCTION__, strerror(-res), res);
795 return res;
796 }
797 mInputStream = newStream;
798
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530799 mNeedConfig = true;
800
Igor Murashkin2fba5842013-04-22 14:03:54 -0700801 *id = mNextStreamId++;
802 *zslStream = newStream;
803
804 // Continue captures if active at start
805 if (wasActive) {
806 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
807 res = configureStreamsLocked();
808 if (res != OK) {
809 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
810 __FUNCTION__, mNextStreamId, strerror(-res), res);
811 return res;
812 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700813 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700814 }
815
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700816 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700817 return OK;
818}
819
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700820status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800821 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700822 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800823 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700824 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800825 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700826 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
827 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800828
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800829 status_t res;
830 bool wasActive = false;
831
832 switch (mStatus) {
833 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700834 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800835 return INVALID_OPERATION;
836 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700837 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800838 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700839 case STATUS_UNCONFIGURED:
840 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800841 // OK
842 break;
843 case STATUS_ACTIVE:
844 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700845 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800846 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700847 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800848 return res;
849 }
850 wasActive = true;
851 break;
852 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700853 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800854 return INVALID_OPERATION;
855 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700856 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800857
858 sp<Camera3OutputStream> newStream;
859 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700860 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700861 if (jpegBufferSize <= 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700862 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
863 return BAD_VALUE;
864 }
865
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800866 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700867 width, height, jpegBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800868 } else {
869 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700870 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800871 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700872 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800873
874 res = mOutputStreams.add(mNextStreamId, newStream);
875 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700876 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800877 return res;
878 }
879
880 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700881 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800882
883 // Continue captures if active at start
884 if (wasActive) {
885 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
886 res = configureStreamsLocked();
887 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700888 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
889 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800890 return res;
891 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700892 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800893 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700894 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800895 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800896}
897
898status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
899 ATRACE_CALL();
900 (void)outputId; (void)id;
901
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700902 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800903 return INVALID_OPERATION;
904}
905
906
907status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700908 uint32_t *width, uint32_t *height,
909 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800910 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700911 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800912 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800913
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800914 switch (mStatus) {
915 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700916 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800917 return INVALID_OPERATION;
918 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700919 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800920 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700921 case STATUS_UNCONFIGURED:
922 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800923 case STATUS_ACTIVE:
924 // OK
925 break;
926 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700927 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800928 return INVALID_OPERATION;
929 }
930
931 ssize_t idx = mOutputStreams.indexOfKey(id);
932 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700933 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800934 return idx;
935 }
936
937 if (width) *width = mOutputStreams[idx]->getWidth();
938 if (height) *height = mOutputStreams[idx]->getHeight();
939 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700940 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800941 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800942}
943
944status_t Camera3Device::setStreamTransform(int id,
945 int transform) {
946 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700947 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800948 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800949
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800950 switch (mStatus) {
951 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700952 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800953 return INVALID_OPERATION;
954 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700955 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800956 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700957 case STATUS_UNCONFIGURED:
958 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800959 case STATUS_ACTIVE:
960 // OK
961 break;
962 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700963 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800964 return INVALID_OPERATION;
965 }
966
967 ssize_t idx = mOutputStreams.indexOfKey(id);
968 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700969 CLOGE("Stream %d does not exist",
970 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800971 return BAD_VALUE;
972 }
973
974 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800975}
976
977status_t Camera3Device::deleteStream(int id) {
978 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700979 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800980 Mutex::Autolock l(mLock);
981 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800982
Igor Murashkine2172be2013-05-28 15:31:39 -0700983 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
984
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800985 // CameraDevice semantics require device to already be idle before
986 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700987 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700988 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
989 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800990 }
991
Igor Murashkin2fba5842013-04-22 14:03:54 -0700992 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800993 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800994 if (mInputStream != NULL && id == mInputStream->getId()) {
995 deletedStream = mInputStream;
996 mInputStream.clear();
997 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800998 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700999 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001000 return BAD_VALUE;
1001 }
Zhijun He5f446352014-01-22 09:49:33 -08001002 }
1003
1004 // Delete output stream or the output part of a bi-directional stream.
1005 if (outputStreamIdx != NAME_NOT_FOUND) {
1006 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001007 mOutputStreams.removeItem(id);
1008 }
1009
1010 // Free up the stream endpoint so that it can be used by some other stream
1011 res = deletedStream->disconnect();
1012 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001013 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001014 // fall through since we want to still list the stream as deleted.
1015 }
1016 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001017 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001018
1019 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001020}
1021
1022status_t Camera3Device::deleteReprocessStream(int id) {
1023 ATRACE_CALL();
1024 (void)id;
1025
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001026 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001027 return INVALID_OPERATION;
1028}
1029
Zhijun He1fa89992015-06-01 15:44:31 -07001030status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001031 ATRACE_CALL();
1032 ALOGV("%s: E", __FUNCTION__);
1033
1034 Mutex::Autolock il(mInterfaceLock);
1035 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001036
1037 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1038 mNeedConfig = true;
1039 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1040 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001041
1042 return configureStreamsLocked();
1043}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001044
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001045status_t Camera3Device::getInputBufferProducer(
1046 sp<IGraphicBufferProducer> *producer) {
1047 Mutex::Autolock il(mInterfaceLock);
1048 Mutex::Autolock l(mLock);
1049
1050 if (producer == NULL) {
1051 return BAD_VALUE;
1052 } else if (mInputStream == NULL) {
1053 return INVALID_OPERATION;
1054 }
1055
1056 return mInputStream->getInputBufferProducer(producer);
1057}
1058
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001059status_t Camera3Device::createDefaultRequest(int templateId,
1060 CameraMetadata *request) {
1061 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001062 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001063 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001064 Mutex::Autolock l(mLock);
1065
1066 switch (mStatus) {
1067 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001068 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 return INVALID_OPERATION;
1070 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001071 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001072 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001073 case STATUS_UNCONFIGURED:
1074 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001075 case STATUS_ACTIVE:
1076 // OK
1077 break;
1078 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001079 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001080 return INVALID_OPERATION;
1081 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001082
Zhijun Hea1530f12014-09-14 12:44:20 -07001083 if (!mRequestTemplateCache[templateId].isEmpty()) {
1084 *request = mRequestTemplateCache[templateId];
1085 return OK;
1086 }
1087
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001088 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001089 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001090 rawRequest = mHal3Device->ops->construct_default_request_settings(
1091 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001092 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001093 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001094 ALOGI("%s: template %d is not supported on this camera device",
1095 __FUNCTION__, templateId);
1096 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001097 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001098 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001099 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001100
1101 return OK;
1102}
1103
1104status_t Camera3Device::waitUntilDrained() {
1105 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001106 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001107 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001108
Zhijun He69a37482014-03-23 18:44:49 -07001109 return waitUntilDrainedLocked();
1110}
1111
1112status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001113 switch (mStatus) {
1114 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001115 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001116 ALOGV("%s: Already idle", __FUNCTION__);
1117 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001118 case STATUS_CONFIGURED:
1119 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001120 case STATUS_ERROR:
1121 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001122 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001123 break;
1124 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001125 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001126 return INVALID_OPERATION;
1127 }
1128
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001129 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1130 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001131 if (res != OK) {
1132 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1133 res);
1134 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001135 return res;
1136}
1137
1138// Pause to reconfigure
1139status_t Camera3Device::internalPauseAndWaitLocked() {
1140 mRequestThread->setPaused(true);
1141 mPauseStateNotify = true;
1142
1143 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1144 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1145 if (res != OK) {
1146 SET_ERR_L("Can't idle device in %f seconds!",
1147 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001148 }
1149
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001150 return res;
1151}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001152
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001153// Resume after internalPauseAndWaitLocked
1154status_t Camera3Device::internalResumeLocked() {
1155 status_t res;
1156
1157 mRequestThread->setPaused(false);
1158
1159 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1160 if (res != OK) {
1161 SET_ERR_L("Can't transition to active in %f seconds!",
1162 kActiveTimeout/1e9);
1163 }
1164 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001165 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001166}
1167
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001168status_t Camera3Device::waitUntilStateThenRelock(bool active,
1169 nsecs_t timeout) {
1170 status_t res = OK;
1171 if (active == (mStatus == STATUS_ACTIVE)) {
1172 // Desired state already reached
1173 return res;
1174 }
1175
1176 bool stateSeen = false;
1177 do {
1178 mRecentStatusUpdates.clear();
1179
1180 res = mStatusChanged.waitRelative(mLock, timeout);
1181 if (res != OK) break;
1182
1183 // Check state change history during wait
1184 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1185 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1186 stateSeen = true;
1187 break;
1188 }
1189 }
1190 } while (!stateSeen);
1191
1192 return res;
1193}
1194
1195
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001196status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1197 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001198 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001199
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001200 if (listener != NULL && mListener != NULL) {
1201 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1202 }
1203 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001204 mRequestThread->setNotificationListener(listener);
1205 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001206
1207 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001208}
1209
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001210bool Camera3Device::willNotify3A() {
1211 return false;
1212}
1213
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001214status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001215 status_t res;
1216 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001217
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001218 while (mResultQueue.empty()) {
1219 res = mResultSignal.waitRelative(mOutputLock, timeout);
1220 if (res == TIMED_OUT) {
1221 return res;
1222 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001223 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001224 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001225 return res;
1226 }
1227 }
1228 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001229}
1230
Jianing Weicb0652e2014-03-12 18:29:36 -07001231status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001232 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001233 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001234
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001235 if (mResultQueue.empty()) {
1236 return NOT_ENOUGH_DATA;
1237 }
1238
Jianing Weicb0652e2014-03-12 18:29:36 -07001239 if (frame == NULL) {
1240 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1241 return BAD_VALUE;
1242 }
1243
1244 CaptureResult &result = *(mResultQueue.begin());
1245 frame->mResultExtras = result.mResultExtras;
1246 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001247 mResultQueue.erase(mResultQueue.begin());
1248
1249 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001250}
1251
1252status_t Camera3Device::triggerAutofocus(uint32_t id) {
1253 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001254 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001255
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001256 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1257 // Mix-in this trigger into the next request and only the next request.
1258 RequestTrigger trigger[] = {
1259 {
1260 ANDROID_CONTROL_AF_TRIGGER,
1261 ANDROID_CONTROL_AF_TRIGGER_START
1262 },
1263 {
1264 ANDROID_CONTROL_AF_TRIGGER_ID,
1265 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001266 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001267 };
1268
1269 return mRequestThread->queueTrigger(trigger,
1270 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001271}
1272
1273status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1274 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001275 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001276
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001277 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1278 // Mix-in this trigger into the next request and only the next request.
1279 RequestTrigger trigger[] = {
1280 {
1281 ANDROID_CONTROL_AF_TRIGGER,
1282 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1283 },
1284 {
1285 ANDROID_CONTROL_AF_TRIGGER_ID,
1286 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001287 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001288 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001289
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001290 return mRequestThread->queueTrigger(trigger,
1291 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001292}
1293
1294status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1295 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001296 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001297
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001298 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1299 // Mix-in this trigger into the next request and only the next request.
1300 RequestTrigger trigger[] = {
1301 {
1302 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1303 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1304 },
1305 {
1306 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1307 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001308 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001309 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001310
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001311 return mRequestThread->queueTrigger(trigger,
1312 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001313}
1314
1315status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1316 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1317 ATRACE_CALL();
1318 (void)reprocessStreamId; (void)buffer; (void)listener;
1319
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001320 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001321 return INVALID_OPERATION;
1322}
1323
Jianing Weicb0652e2014-03-12 18:29:36 -07001324status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001325 ATRACE_CALL();
1326 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001327 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001328
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001329 NotificationListener* listener;
1330 {
1331 Mutex::Autolock l(mOutputLock);
1332 listener = mListener;
1333 }
1334
Zhijun He7ef20392014-04-21 16:04:17 -07001335 {
1336 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001337 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001338 }
1339
Zhijun He491e3412013-12-27 10:57:44 -08001340 status_t res;
1341 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1342 res = mHal3Device->ops->flush(mHal3Device);
1343 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001344 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001345 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001346 }
1347
1348 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001349}
1350
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001351status_t Camera3Device::prepare(int streamId) {
1352 ATRACE_CALL();
1353 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001354 Mutex::Autolock il(mInterfaceLock);
1355 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001356
1357 sp<Camera3StreamInterface> stream;
1358 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1359 if (outputStreamIdx == NAME_NOT_FOUND) {
1360 CLOGE("Stream %d does not exist", streamId);
1361 return BAD_VALUE;
1362 }
1363
1364 stream = mOutputStreams.editValueAt(outputStreamIdx);
1365
1366 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001367 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001368 return BAD_VALUE;
1369 }
1370
1371 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001372 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001373 return BAD_VALUE;
1374 }
1375
1376 return mPreparerThread->prepare(stream);
1377}
1378
Zhijun He204e3292014-07-14 17:09:23 -07001379uint32_t Camera3Device::getDeviceVersion() {
1380 ATRACE_CALL();
1381 Mutex::Autolock il(mInterfaceLock);
1382 return mDeviceVersion;
1383}
1384
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001385/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001386 * Methods called by subclasses
1387 */
1388
1389void Camera3Device::notifyStatus(bool idle) {
1390 {
1391 // Need mLock to safely update state and synchronize to current
1392 // state of methods in flight.
1393 Mutex::Autolock l(mLock);
1394 // We can get various system-idle notices from the status tracker
1395 // while starting up. Only care about them if we've actually sent
1396 // in some requests recently.
1397 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1398 return;
1399 }
1400 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1401 idle ? "idle" : "active");
1402 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1403 mRecentStatusUpdates.add(mStatus);
1404 mStatusChanged.signal();
1405
1406 // Skip notifying listener if we're doing some user-transparent
1407 // state changes
1408 if (mPauseStateNotify) return;
1409 }
1410 NotificationListener *listener;
1411 {
1412 Mutex::Autolock l(mOutputLock);
1413 listener = mListener;
1414 }
1415 if (idle && listener != NULL) {
1416 listener->notifyIdle();
1417 }
1418}
1419
1420/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001421 * Camera3Device private methods
1422 */
1423
1424sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1425 const CameraMetadata &request) {
1426 ATRACE_CALL();
1427 status_t res;
1428
1429 sp<CaptureRequest> newRequest = new CaptureRequest;
1430 newRequest->mSettings = request;
1431
1432 camera_metadata_entry_t inputStreams =
1433 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1434 if (inputStreams.count > 0) {
1435 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001436 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001437 CLOGE("Request references unknown input stream %d",
1438 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001439 return NULL;
1440 }
1441 // Lazy completion of stream configuration (allocation/registration)
1442 // on first use
1443 if (mInputStream->isConfiguring()) {
1444 res = mInputStream->finishConfiguration(mHal3Device);
1445 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001446 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001447 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001448 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001449 return NULL;
1450 }
1451 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001452 // Check if stream is being prepared
1453 if (mInputStream->isPreparing()) {
1454 CLOGE("Request references an input stream that's being prepared!");
1455 return NULL;
1456 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001457
1458 newRequest->mInputStream = mInputStream;
1459 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1460 }
1461
1462 camera_metadata_entry_t streams =
1463 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1464 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001465 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001466 return NULL;
1467 }
1468
1469 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001470 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001471 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001472 CLOGE("Request references unknown stream %d",
1473 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001474 return NULL;
1475 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001476 sp<Camera3OutputStreamInterface> stream =
1477 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001478
1479 // Lazy completion of stream configuration (allocation/registration)
1480 // on first use
1481 if (stream->isConfiguring()) {
1482 res = stream->finishConfiguration(mHal3Device);
1483 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001484 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1485 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001486 return NULL;
1487 }
1488 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001489 // Check if stream is being prepared
1490 if (stream->isPreparing()) {
1491 CLOGE("Request references an output stream that's being prepared!");
1492 return NULL;
1493 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001494
1495 newRequest->mOutputStreams.push(stream);
1496 }
1497 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1498
1499 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001500}
1501
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001502bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1503 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1504 Size size = mSupportedOpaqueInputSizes[i];
1505 if (size.width == width && size.height == height) {
1506 return true;
1507 }
1508 }
1509
1510 return false;
1511}
1512
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001513status_t Camera3Device::configureStreamsLocked() {
1514 ATRACE_CALL();
1515 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001516
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001517 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001518 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001519 return INVALID_OPERATION;
1520 }
1521
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001522 if (!mNeedConfig) {
1523 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1524 return OK;
1525 }
1526
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001527 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1528 // adding a dummy stream instead.
1529 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1530 if (mOutputStreams.size() == 0) {
1531 addDummyStreamLocked();
1532 } else {
1533 tryRemoveDummyStreamLocked();
1534 }
1535
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001536 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001537 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001538
1539 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001540 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1541 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1542 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001543 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1544
1545 Vector<camera3_stream_t*> streams;
1546 streams.setCapacity(config.num_streams);
1547
1548 if (mInputStream != NULL) {
1549 camera3_stream_t *inputStream;
1550 inputStream = mInputStream->startConfiguration();
1551 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001552 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001553 return INVALID_OPERATION;
1554 }
1555 streams.add(inputStream);
1556 }
1557
1558 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001559
1560 // Don't configure bidi streams twice, nor add them twice to the list
1561 if (mOutputStreams[i].get() ==
1562 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1563
1564 config.num_streams--;
1565 continue;
1566 }
1567
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001568 camera3_stream_t *outputStream;
1569 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1570 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001571 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001572 return INVALID_OPERATION;
1573 }
1574 streams.add(outputStream);
1575 }
1576
1577 config.streams = streams.editArray();
1578
1579 // Do the HAL configuration; will potentially touch stream
1580 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001581 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001582 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001583 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001584
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001585 if (res == BAD_VALUE) {
1586 // HAL rejected this set of streams as unsupported, clean up config
1587 // attempt and return to unconfigured state
1588 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1589 res = mInputStream->cancelConfiguration();
1590 if (res != OK) {
1591 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1592 mInputStream->getId(), strerror(-res), res);
1593 return res;
1594 }
1595 }
1596
1597 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1598 sp<Camera3OutputStreamInterface> outputStream =
1599 mOutputStreams.editValueAt(i);
1600 if (outputStream->isConfiguring()) {
1601 res = outputStream->cancelConfiguration();
1602 if (res != OK) {
1603 SET_ERR_L(
1604 "Can't cancel configuring output stream %d: %s (%d)",
1605 outputStream->getId(), strerror(-res), res);
1606 return res;
1607 }
1608 }
1609 }
1610
1611 // Return state to that at start of call, so that future configures
1612 // properly clean things up
1613 mStatus = STATUS_UNCONFIGURED;
1614 mNeedConfig = true;
1615
1616 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1617 return BAD_VALUE;
1618 } else if (res != OK) {
1619 // Some other kind of error from configure_streams - this is not
1620 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001621 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1622 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001623 return res;
1624 }
1625
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001626 // Finish all stream configuration immediately.
1627 // TODO: Try to relax this later back to lazy completion, which should be
1628 // faster
1629
Igor Murashkin073f8572013-05-02 14:59:28 -07001630 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001631 res = mInputStream->finishConfiguration(mHal3Device);
1632 if (res != OK) {
1633 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1634 mInputStream->getId(), strerror(-res), res);
1635 return res;
1636 }
1637 }
1638
1639 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001640 sp<Camera3OutputStreamInterface> outputStream =
1641 mOutputStreams.editValueAt(i);
1642 if (outputStream->isConfiguring()) {
1643 res = outputStream->finishConfiguration(mHal3Device);
1644 if (res != OK) {
1645 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1646 outputStream->getId(), strerror(-res), res);
1647 return res;
1648 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001649 }
1650 }
1651
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001652 // Request thread needs to know to avoid using repeat-last-settings protocol
1653 // across configure_streams() calls
1654 mRequestThread->configurationComplete();
1655
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001656 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001657
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001658 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001659
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001660 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001661 mStatus = STATUS_CONFIGURED;
1662 } else {
1663 mStatus = STATUS_UNCONFIGURED;
1664 }
1665
1666 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1667
Zhijun He0a210512014-07-24 13:45:15 -07001668 // tear down the deleted streams after configure streams.
1669 mDeletedStreams.clear();
1670
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001671 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001672}
1673
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001674status_t Camera3Device::addDummyStreamLocked() {
1675 ATRACE_CALL();
1676 status_t res;
1677
1678 if (mDummyStreamId != NO_STREAM) {
1679 // Should never be adding a second dummy stream when one is already
1680 // active
1681 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1682 __FUNCTION__, mId);
1683 return INVALID_OPERATION;
1684 }
1685
1686 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1687
1688 sp<Camera3OutputStreamInterface> dummyStream =
1689 new Camera3DummyStream(mNextStreamId);
1690
1691 res = mOutputStreams.add(mNextStreamId, dummyStream);
1692 if (res < 0) {
1693 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1694 return res;
1695 }
1696
1697 mDummyStreamId = mNextStreamId;
1698 mNextStreamId++;
1699
1700 return OK;
1701}
1702
1703status_t Camera3Device::tryRemoveDummyStreamLocked() {
1704 ATRACE_CALL();
1705 status_t res;
1706
1707 if (mDummyStreamId == NO_STREAM) return OK;
1708 if (mOutputStreams.size() == 1) return OK;
1709
1710 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1711
1712 // Ok, have a dummy stream and there's at least one other output stream,
1713 // so remove the dummy
1714
1715 sp<Camera3StreamInterface> deletedStream;
1716 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1717 if (outputStreamIdx == NAME_NOT_FOUND) {
1718 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1719 return INVALID_OPERATION;
1720 }
1721
1722 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1723 mOutputStreams.removeItemsAt(outputStreamIdx);
1724
1725 // Free up the stream endpoint so that it can be used by some other stream
1726 res = deletedStream->disconnect();
1727 if (res != OK) {
1728 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1729 // fall through since we want to still list the stream as deleted.
1730 }
1731 mDeletedStreams.add(deletedStream);
1732 mDummyStreamId = NO_STREAM;
1733
1734 return res;
1735}
1736
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001737void Camera3Device::setErrorState(const char *fmt, ...) {
1738 Mutex::Autolock l(mLock);
1739 va_list args;
1740 va_start(args, fmt);
1741
1742 setErrorStateLockedV(fmt, args);
1743
1744 va_end(args);
1745}
1746
1747void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1748 Mutex::Autolock l(mLock);
1749 setErrorStateLockedV(fmt, args);
1750}
1751
1752void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1753 va_list args;
1754 va_start(args, fmt);
1755
1756 setErrorStateLockedV(fmt, args);
1757
1758 va_end(args);
1759}
1760
1761void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001762 // Print out all error messages to log
1763 String8 errorCause = String8::formatV(fmt, args);
1764 ALOGE("Camera %d: %s", mId, errorCause.string());
1765
1766 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001767 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001768
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001769 mErrorCause = errorCause;
1770
1771 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001772 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001773
1774 // Notify upstream about a device error
1775 if (mListener != NULL) {
1776 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1777 CaptureResultExtras());
1778 }
1779
1780 // Save stack trace. View by dumping it later.
1781 CameraTraces::saveTrace();
1782 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001783}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001784
1785/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001786 * In-flight request management
1787 */
1788
Jianing Weicb0652e2014-03-12 18:29:36 -07001789status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001790 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001791 ATRACE_CALL();
1792 Mutex::Autolock l(mInFlightLock);
1793
1794 ssize_t res;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001795 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001796 if (res < 0) return res;
1797
1798 return OK;
1799}
1800
1801/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001802 * Check if all 3A fields are ready, and send off a partial 3A-only result
1803 * to the output frame queue
1804 */
Zhijun He204e3292014-07-14 17:09:23 -07001805bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001806 uint32_t frameNumber,
1807 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001808
1809 // Check if all 3A states are present
1810 // The full list of fields is
1811 // android.control.afMode
1812 // android.control.awbMode
1813 // android.control.aeState
1814 // android.control.awbState
1815 // android.control.afState
1816 // android.control.afTriggerID
1817 // android.control.aePrecaptureID
1818 // TODO: Add android.control.aeMode
1819
1820 bool gotAllStates = true;
1821
1822 uint8_t afMode;
1823 uint8_t awbMode;
1824 uint8_t aeState;
1825 uint8_t afState;
1826 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001827
1828 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1829 &afMode, frameNumber);
1830
1831 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1832 &awbMode, frameNumber);
1833
1834 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1835 &aeState, frameNumber);
1836
1837 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1838 &afState, frameNumber);
1839
1840 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1841 &awbState, frameNumber);
1842
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001843 if (!gotAllStates) return false;
1844
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001845 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001846 "AF state %d, AE state %d, AWB state %d, "
1847 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001848 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001849 afMode, awbMode,
1850 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001851 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001852
1853 // Got all states, so construct a minimal result to send
1854 // In addition to the above fields, this means adding in
1855 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001856 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001857 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001858
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001859 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001860
1861 Mutex::Autolock l(mOutputLock);
1862
Jianing Weicb0652e2014-03-12 18:29:36 -07001863 CaptureResult captureResult;
1864 captureResult.mResultExtras = resultExtras;
1865 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1866 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1867 // but not limited to CameraDeviceBase::getNextResult
1868 CaptureResult& min3AResult =
1869 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001870
Jianing Weicb0652e2014-03-12 18:29:36 -07001871 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1872 // TODO: This is problematic casting. Need to fix CameraMetadata.
1873 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001874 return false;
1875 }
1876
Jianing Weicb0652e2014-03-12 18:29:36 -07001877 int32_t requestId = resultExtras.requestId;
1878 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001879 &requestId, frameNumber)) {
1880 return false;
1881 }
1882
Zhijun He204e3292014-07-14 17:09:23 -07001883 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1884 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1885 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1886 &partialResult, frameNumber)) {
1887 return false;
1888 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001889 }
1890
Jianing Weicb0652e2014-03-12 18:29:36 -07001891 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001892 &afMode, frameNumber)) {
1893 return false;
1894 }
1895
Jianing Weicb0652e2014-03-12 18:29:36 -07001896 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001897 &awbMode, frameNumber)) {
1898 return false;
1899 }
1900
Jianing Weicb0652e2014-03-12 18:29:36 -07001901 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001902 &aeState, frameNumber)) {
1903 return false;
1904 }
1905
Jianing Weicb0652e2014-03-12 18:29:36 -07001906 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001907 &afState, frameNumber)) {
1908 return false;
1909 }
1910
Jianing Weicb0652e2014-03-12 18:29:36 -07001911 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001912 &awbState, frameNumber)) {
1913 return false;
1914 }
1915
Jianing Weicb0652e2014-03-12 18:29:36 -07001916 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001917 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001918 return false;
1919 }
1920
Jianing Weicb0652e2014-03-12 18:29:36 -07001921 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001922 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001923 return false;
1924 }
1925
Zhijun He204e3292014-07-14 17:09:23 -07001926 // We only send the aggregated partial when all 3A related metadata are available
1927 // For both API1 and API2.
1928 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001929 mResultSignal.signal();
1930
1931 return true;
1932}
1933
1934template<typename T>
1935bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001936 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001937 (void) frameNumber;
1938
1939 camera_metadata_ro_entry_t entry;
1940
1941 entry = result.find(tag);
1942 if (entry.count == 0) {
1943 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1944 mId, frameNumber, get_camera_metadata_tag_name(tag));
1945 return false;
1946 }
1947
1948 if (sizeof(T) == sizeof(uint8_t)) {
1949 *value = entry.data.u8[0];
1950 } else if (sizeof(T) == sizeof(int32_t)) {
1951 *value = entry.data.i32[0];
1952 } else {
1953 ALOGE("%s: Unexpected type", __FUNCTION__);
1954 return false;
1955 }
1956 return true;
1957}
1958
1959template<typename T>
1960bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001961 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001962 if (result.update(tag, value, 1) != NO_ERROR) {
1963 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1964 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1965 frameNumber, get_camera_metadata_tag_name(tag));
1966 return false;
1967 }
1968 return true;
1969}
1970
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08001971void Camera3Device::returnOutputBuffers(
1972 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
1973 nsecs_t timestamp) {
1974 for (size_t i = 0; i < numBuffers; i++)
1975 {
1976 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
1977 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
1978 // Note: stream may be deallocated at this point, if this buffer was
1979 // the last reference to it.
1980 if (res != OK) {
1981 ALOGE("Can't return buffer to its stream: %s (%d)",
1982 strerror(-res), res);
1983 }
1984 }
1985}
1986
1987
1988void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
1989
1990 const InFlightRequest &request = mInFlightMap.valueAt(idx);
1991 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
1992
1993 nsecs_t sensorTimestamp = request.sensorTimestamp;
1994 nsecs_t shutterTimestamp = request.shutterTimestamp;
1995
1996 // Check if it's okay to remove the request from InFlightMap:
1997 // In the case of a successful request:
1998 // all input and output buffers, all result metadata, shutter callback
1999 // arrived.
2000 // In the case of a unsuccessful request:
2001 // all input and output buffers arrived.
2002 if (request.numBuffersLeft == 0 &&
2003 (request.requestStatus != OK ||
2004 (request.haveResultMetadata && shutterTimestamp != 0))) {
2005 ATRACE_ASYNC_END("frame capture", frameNumber);
2006
2007 // Sanity check - if sensor timestamp matches shutter timestamp
2008 if (request.requestStatus == OK &&
2009 sensorTimestamp != shutterTimestamp) {
2010 SET_ERR("sensor timestamp (%" PRId64
2011 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2012 sensorTimestamp, frameNumber, shutterTimestamp);
2013 }
2014
2015 // for an unsuccessful request, it may have pending output buffers to
2016 // return.
2017 assert(request.requestStatus != OK ||
2018 request.pendingOutputBuffers.size() == 0);
2019 returnOutputBuffers(request.pendingOutputBuffers.array(),
2020 request.pendingOutputBuffers.size(), 0);
2021
2022 mInFlightMap.removeItemsAt(idx, 1);
2023
2024 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2025 }
2026
2027 // Sanity check - if we have too many in-flight frames, something has
2028 // likely gone wrong
2029 if (mInFlightMap.size() > kInFlightWarnLimit) {
2030 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
2031 }
2032}
2033
2034
2035void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2036 CaptureResultExtras &resultExtras,
2037 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002038 uint32_t frameNumber,
2039 bool reprocess) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002040 if (pendingMetadata.isEmpty())
2041 return;
2042
2043 Mutex::Autolock l(mOutputLock);
2044
2045 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002046 if (reprocess) {
2047 if (frameNumber < mNextReprocessResultFrameNumber) {
2048 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002049 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002050 frameNumber, mNextReprocessResultFrameNumber);
2051 return;
2052 }
2053 mNextReprocessResultFrameNumber = frameNumber + 1;
2054 } else {
2055 if (frameNumber < mNextResultFrameNumber) {
2056 SET_ERR("Out-of-order capture result metadata submitted! "
2057 "(got frame number %d, expecting %d)",
2058 frameNumber, mNextResultFrameNumber);
2059 return;
2060 }
2061 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002062 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002063
2064 CaptureResult captureResult;
2065 captureResult.mResultExtras = resultExtras;
2066 captureResult.mMetadata = pendingMetadata;
2067
2068 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2069 (int32_t*)&frameNumber, 1) != OK) {
2070 SET_ERR("Failed to set frame# in metadata (%d)",
2071 frameNumber);
2072 return;
2073 } else {
2074 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2075 __FUNCTION__, mId, frameNumber);
2076 }
2077
2078 // Append any previous partials to form a complete result
2079 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2080 captureResult.mMetadata.append(collectedPartialResult);
2081 }
2082
2083 captureResult.mMetadata.sort();
2084
2085 // Check that there's a timestamp in the result metadata
2086 camera_metadata_entry entry =
2087 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2088 if (entry.count == 0) {
2089 SET_ERR("No timestamp provided by HAL for frame %d!",
2090 frameNumber);
2091 return;
2092 }
2093
2094 // Valid result, insert into queue
2095 List<CaptureResult>::iterator queuedResult =
2096 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2097 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2098 ", burstId = %" PRId32, __FUNCTION__,
2099 queuedResult->mResultExtras.requestId,
2100 queuedResult->mResultExtras.frameNumber,
2101 queuedResult->mResultExtras.burstId);
2102
2103 mResultSignal.signal();
2104}
2105
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002106/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002107 * Camera HAL device callback methods
2108 */
2109
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002110void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002111 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002112
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002113 status_t res;
2114
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002115 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002116 if (result->result == NULL && result->num_output_buffers == 0 &&
2117 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002118 SET_ERR("No result data provided by HAL for frame %d",
2119 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002120 return;
2121 }
Zhijun He204e3292014-07-14 17:09:23 -07002122
2123 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2124 // partial_result to 1 when metadata is included in this result.
2125 if (!mUsePartialResult &&
2126 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2127 result->result != NULL &&
2128 result->partial_result != 1) {
2129 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2130 " if partial result is not supported",
2131 frameNumber, result->partial_result);
2132 return;
2133 }
2134
2135 bool isPartialResult = false;
2136 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002137 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002138 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002139
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002140 // Get shutter timestamp and resultExtras from list of in-flight requests,
2141 // where it was added by the shutter notification for this frame. If the
2142 // shutter timestamp isn't received yet, append the output buffers to the
2143 // in-flight request and they will be returned when the shutter timestamp
2144 // arrives. Update the in-flight status and remove the in-flight entry if
2145 // all result data and shutter timestamp have been received.
2146 nsecs_t shutterTimestamp = 0;
2147
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002148 {
2149 Mutex::Autolock l(mInFlightLock);
2150 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2151 if (idx == NAME_NOT_FOUND) {
2152 SET_ERR("Unknown frame number for capture result: %d",
2153 frameNumber);
2154 return;
2155 }
2156 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002157 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2158 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2159 ", partialResultCount = %d",
2160 __FUNCTION__, request.resultExtras.requestId,
2161 request.resultExtras.frameNumber, request.resultExtras.burstId,
2162 result->partial_result);
2163 // Always update the partial count to the latest one if it's not 0
2164 // (buffers only). When framework aggregates adjacent partial results
2165 // into one, the latest partial count will be used.
2166 if (result->partial_result != 0)
2167 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002168
2169 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002170 if (mUsePartialResult && result->result != NULL) {
2171 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2172 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2173 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2174 " the range of [1, %d] when metadata is included in the result",
2175 frameNumber, result->partial_result, mNumPartialResults);
2176 return;
2177 }
2178 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002179 if (isPartialResult) {
2180 request.partialResult.collectedResult.append(result->result);
2181 }
Zhijun He204e3292014-07-14 17:09:23 -07002182 } else {
2183 camera_metadata_ro_entry_t partialResultEntry;
2184 res = find_camera_metadata_ro_entry(result->result,
2185 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2186 if (res != NAME_NOT_FOUND &&
2187 partialResultEntry.count > 0 &&
2188 partialResultEntry.data.u8[0] ==
2189 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2190 // A partial result. Flag this as such, and collect this
2191 // set of metadata into the in-flight entry.
2192 isPartialResult = true;
2193 request.partialResult.collectedResult.append(
2194 result->result);
2195 request.partialResult.collectedResult.erase(
2196 ANDROID_QUIRKS_PARTIAL_RESULT);
2197 }
2198 }
2199
2200 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002201 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002202 if (!request.partialResult.haveSent3A) {
2203 request.partialResult.haveSent3A =
2204 processPartial3AResult(frameNumber,
2205 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002206 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002207 }
2208 }
2209 }
2210
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002211 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002212 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002213
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002214 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002215 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002216 if (request.haveResultMetadata) {
2217 SET_ERR("Called multiple times with metadata for frame %d",
2218 frameNumber);
2219 return;
2220 }
Zhijun He204e3292014-07-14 17:09:23 -07002221 if (mUsePartialResult &&
2222 !request.partialResult.collectedResult.isEmpty()) {
2223 collectedPartialResult.acquire(
2224 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002225 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002226 request.haveResultMetadata = true;
2227 }
2228
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002229 uint32_t numBuffersReturned = result->num_output_buffers;
2230 if (result->input_buffer != NULL) {
2231 if (hasInputBufferInRequest) {
2232 numBuffersReturned += 1;
2233 } else {
2234 ALOGW("%s: Input buffer should be NULL if there is no input"
2235 " buffer sent in the request",
2236 __FUNCTION__);
2237 }
2238 }
2239 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002240 if (request.numBuffersLeft < 0) {
2241 SET_ERR("Too many buffers returned for frame %d",
2242 frameNumber);
2243 return;
2244 }
2245
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002246 camera_metadata_ro_entry_t entry;
2247 res = find_camera_metadata_ro_entry(result->result,
2248 ANDROID_SENSOR_TIMESTAMP, &entry);
2249 if (res == OK && entry.count == 1) {
2250 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002251 }
2252
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002253 // If shutter event isn't received yet, append the output buffers to
2254 // the in-flight request. Otherwise, return the output buffers to
2255 // streams.
2256 if (shutterTimestamp == 0) {
2257 request.pendingOutputBuffers.appendArray(result->output_buffers,
2258 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002259 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002260 returnOutputBuffers(result->output_buffers,
2261 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002262 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002263
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002264 if (result->result != NULL && !isPartialResult) {
2265 if (shutterTimestamp == 0) {
2266 request.pendingMetadata = result->result;
2267 request.partialResult.collectedResult = collectedPartialResult;
2268 } else {
2269 CameraMetadata metadata;
2270 metadata = result->result;
2271 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002272 collectedPartialResult, frameNumber, hasInputBufferInRequest);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002273 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002274 }
2275
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002276 removeInFlightRequestIfReadyLocked(idx);
2277 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002278
Zhijun Hef0d962a2014-06-30 10:24:11 -07002279 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002280 if (hasInputBufferInRequest) {
2281 Camera3Stream *stream =
2282 Camera3Stream::cast(result->input_buffer->stream);
2283 res = stream->returnInputBuffer(*(result->input_buffer));
2284 // Note: stream may be deallocated at this point, if this buffer was the
2285 // last reference to it.
2286 if (res != OK) {
2287 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2288 " its stream:%s (%d)", __FUNCTION__,
2289 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002290 }
2291 } else {
2292 ALOGW("%s: Input buffer should be NULL if there is no input"
2293 " buffer sent in the request, skipping input buffer return.",
2294 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002295 }
2296 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002297}
2298
2299void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002300 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002301 NotificationListener *listener;
2302 {
2303 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002304 listener = mListener;
2305 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002306
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002307 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002308 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002309 return;
2310 }
2311
2312 switch (msg->type) {
2313 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002314 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002315 break;
2316 }
2317 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002318 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002319 break;
2320 }
2321 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002322 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002323 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002324 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002325}
2326
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002327void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2328 NotificationListener *listener) {
2329
2330 // Map camera HAL error codes to ICameraDeviceCallback error codes
2331 // Index into this with the HAL error code
2332 static const ICameraDeviceCallbacks::CameraErrorCode
2333 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2334 // 0 = Unused error code
2335 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2336 // 1 = CAMERA3_MSG_ERROR_DEVICE
2337 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2338 // 2 = CAMERA3_MSG_ERROR_REQUEST
2339 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2340 // 3 = CAMERA3_MSG_ERROR_RESULT
2341 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2342 // 4 = CAMERA3_MSG_ERROR_BUFFER
2343 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2344 };
2345
2346 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2347 ((msg.error_code >= 0) &&
2348 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2349 halErrorMap[msg.error_code] :
2350 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2351
2352 int streamId = 0;
2353 if (msg.error_stream != NULL) {
2354 Camera3Stream *stream =
2355 Camera3Stream::cast(msg.error_stream);
2356 streamId = stream->getId();
2357 }
2358 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2359 mId, __FUNCTION__, msg.frame_number,
2360 streamId, msg.error_code);
2361
2362 CaptureResultExtras resultExtras;
2363 switch (errorCode) {
2364 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2365 // SET_ERR calls notifyError
2366 SET_ERR("Camera HAL reported serious device error");
2367 break;
2368 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2369 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2370 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2371 {
2372 Mutex::Autolock l(mInFlightLock);
2373 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2374 if (idx >= 0) {
2375 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2376 r.requestStatus = msg.error_code;
2377 resultExtras = r.resultExtras;
2378 } else {
2379 resultExtras.frameNumber = msg.frame_number;
2380 ALOGE("Camera %d: %s: cannot find in-flight request on "
2381 "frame %" PRId64 " error", mId, __FUNCTION__,
2382 resultExtras.frameNumber);
2383 }
2384 }
2385 if (listener != NULL) {
2386 listener->notifyError(errorCode, resultExtras);
2387 } else {
2388 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2389 }
2390 break;
2391 default:
2392 // SET_ERR calls notifyError
2393 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2394 break;
2395 }
2396}
2397
2398void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2399 NotificationListener *listener) {
2400 ssize_t idx;
2401 // Verify ordering of shutter notifications
2402 {
2403 Mutex::Autolock l(mOutputLock);
2404 // TODO: need to track errors for tighter bounds on expected frame number.
2405 if (msg.frame_number < mNextShutterFrameNumber) {
2406 SET_ERR("Shutter notification out-of-order. Expected "
2407 "notification for frame %d, got frame %d",
2408 mNextShutterFrameNumber, msg.frame_number);
2409 return;
2410 }
2411 mNextShutterFrameNumber = msg.frame_number + 1;
2412 }
2413
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002414 // Set timestamp for the request in the in-flight tracking
2415 // and get the request ID to send upstream
2416 {
2417 Mutex::Autolock l(mInFlightLock);
2418 idx = mInFlightMap.indexOfKey(msg.frame_number);
2419 if (idx >= 0) {
2420 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002421
2422 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2423 mId, __FUNCTION__,
2424 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2425 // Call listener, if any
2426 if (listener != NULL) {
2427 listener->notifyShutter(r.resultExtras, msg.timestamp);
2428 }
2429
2430 r.shutterTimestamp = msg.timestamp;
2431
2432 // send pending result and buffers
2433 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002434 r.partialResult.collectedResult, msg.frame_number,
2435 r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002436 returnOutputBuffers(r.pendingOutputBuffers.array(),
2437 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2438 r.pendingOutputBuffers.clear();
2439
2440 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002441 }
2442 }
2443 if (idx < 0) {
2444 SET_ERR("Shutter notification for non-existent frame number %d",
2445 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002446 }
2447}
2448
2449
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002450CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002451 ALOGV("%s", __FUNCTION__);
2452
Igor Murashkin1e479c02013-09-06 16:55:14 -07002453 CameraMetadata retVal;
2454
2455 if (mRequestThread != NULL) {
2456 retVal = mRequestThread->getLatestRequest();
2457 }
2458
Igor Murashkin1e479c02013-09-06 16:55:14 -07002459 return retVal;
2460}
2461
Jianing Weicb0652e2014-03-12 18:29:36 -07002462
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002463/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002464 * RequestThread inner class methods
2465 */
2466
2467Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002468 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002469 camera3_device_t *hal3Device) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002470 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002471 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002472 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002473 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002474 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002475 mReconfigured(false),
2476 mDoPause(false),
2477 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002478 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002479 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002480 mCurrentAfTriggerId(0),
2481 mCurrentPreCaptureTriggerId(0),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002482 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002483 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002484}
2485
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002486void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002487 NotificationListener *listener) {
2488 Mutex::Autolock l(mRequestLock);
2489 mListener = listener;
2490}
2491
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002492void Camera3Device::RequestThread::configurationComplete() {
2493 Mutex::Autolock l(mRequestLock);
2494 mReconfigured = true;
2495}
2496
Jianing Wei90e59c92014-03-12 18:29:36 -07002497status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002498 List<sp<CaptureRequest> > &requests,
2499 /*out*/
2500 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002501 Mutex::Autolock l(mRequestLock);
2502 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2503 ++it) {
2504 mRequestQueue.push_back(*it);
2505 }
2506
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002507 if (lastFrameNumber != NULL) {
2508 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2509 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2510 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2511 *lastFrameNumber);
2512 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002513
Jianing Wei90e59c92014-03-12 18:29:36 -07002514 unpauseForNewRequests();
2515
2516 return OK;
2517}
2518
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002519
2520status_t Camera3Device::RequestThread::queueTrigger(
2521 RequestTrigger trigger[],
2522 size_t count) {
2523
2524 Mutex::Autolock l(mTriggerMutex);
2525 status_t ret;
2526
2527 for (size_t i = 0; i < count; ++i) {
2528 ret = queueTriggerLocked(trigger[i]);
2529
2530 if (ret != OK) {
2531 return ret;
2532 }
2533 }
2534
2535 return OK;
2536}
2537
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002538int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2539 sp<Camera3Device> d = device.promote();
2540 if (d != NULL) return d->mId;
2541 return 0;
2542}
2543
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002544status_t Camera3Device::RequestThread::queueTriggerLocked(
2545 RequestTrigger trigger) {
2546
2547 uint32_t tag = trigger.metadataTag;
2548 ssize_t index = mTriggerMap.indexOfKey(tag);
2549
2550 switch (trigger.getTagType()) {
2551 case TYPE_BYTE:
2552 // fall-through
2553 case TYPE_INT32:
2554 break;
2555 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002556 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2557 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002558 return INVALID_OPERATION;
2559 }
2560
2561 /**
2562 * Collect only the latest trigger, since we only have 1 field
2563 * in the request settings per trigger tag, and can't send more than 1
2564 * trigger per request.
2565 */
2566 if (index != NAME_NOT_FOUND) {
2567 mTriggerMap.editValueAt(index) = trigger;
2568 } else {
2569 mTriggerMap.add(tag, trigger);
2570 }
2571
2572 return OK;
2573}
2574
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002575status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002576 const RequestList &requests,
2577 /*out*/
2578 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002579 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002580 if (lastFrameNumber != NULL) {
2581 *lastFrameNumber = mRepeatingLastFrameNumber;
2582 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002583 mRepeatingRequests.clear();
2584 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2585 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002586
2587 unpauseForNewRequests();
2588
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002589 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002590 return OK;
2591}
2592
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002593bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2594 if (mRepeatingRequests.empty()) {
2595 return false;
2596 }
2597 int32_t requestId = requestIn->mResultExtras.requestId;
2598 const RequestList &repeatRequests = mRepeatingRequests;
2599 // All repeating requests are guaranteed to have same id so only check first quest
2600 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2601 return (firstRequest->mResultExtras.requestId == requestId);
2602}
2603
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002604status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002605 Mutex::Autolock l(mRequestLock);
2606 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002607 if (lastFrameNumber != NULL) {
2608 *lastFrameNumber = mRepeatingLastFrameNumber;
2609 }
2610 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002611 return OK;
2612}
2613
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002614status_t Camera3Device::RequestThread::clear(
2615 NotificationListener *listener,
2616 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002617 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002618 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002619
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002620 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002621
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002622 // Send errors for all requests pending in the request queue, including
2623 // pending repeating requests
2624 if (listener != NULL) {
2625 for (RequestList::iterator it = mRequestQueue.begin();
2626 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002627 // Abort the input buffers for reprocess requests.
2628 if ((*it)->mInputStream != NULL) {
2629 camera3_stream_buffer_t inputBuffer;
2630 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2631 if (res != OK) {
2632 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2633 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2634 } else {
2635 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2636 if (res != OK) {
2637 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2638 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2639 }
2640 }
2641 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002642 // Set the frame number this request would have had, if it
2643 // had been submitted; this frame number will not be reused.
2644 // The requestId and burstId fields were set when the request was
2645 // submitted originally (in convertMetadataListToRequestListLocked)
2646 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2647 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2648 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002649 }
2650 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002651 mRequestQueue.clear();
2652 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002653 if (lastFrameNumber != NULL) {
2654 *lastFrameNumber = mRepeatingLastFrameNumber;
2655 }
2656 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002657 return OK;
2658}
2659
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002660void Camera3Device::RequestThread::setPaused(bool paused) {
2661 Mutex::Autolock l(mPauseLock);
2662 mDoPause = paused;
2663 mDoPauseSignal.signal();
2664}
2665
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002666status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2667 int32_t requestId, nsecs_t timeout) {
2668 Mutex::Autolock l(mLatestRequestMutex);
2669 status_t res;
2670 while (mLatestRequestId != requestId) {
2671 nsecs_t startTime = systemTime();
2672
2673 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2674 if (res != OK) return res;
2675
2676 timeout -= (systemTime() - startTime);
2677 }
2678
2679 return OK;
2680}
2681
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002682void Camera3Device::RequestThread::requestExit() {
2683 // Call parent to set up shutdown
2684 Thread::requestExit();
2685 // The exit from any possible waits
2686 mDoPauseSignal.signal();
2687 mRequestSignal.signal();
2688}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002689
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002690bool Camera3Device::RequestThread::threadLoop() {
2691
2692 status_t res;
2693
2694 // Handle paused state.
2695 if (waitIfPaused()) {
2696 return true;
2697 }
2698
2699 // Get work to do
2700
2701 sp<CaptureRequest> nextRequest = waitForNextRequest();
2702 if (nextRequest == NULL) {
2703 return true;
2704 }
2705
2706 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002707 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002708 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002709 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002710
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002711 // Get the request ID, if any
2712 int requestId;
2713 camera_metadata_entry_t requestIdEntry =
2714 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2715 if (requestIdEntry.count > 0) {
2716 requestId = requestIdEntry.data.i32[0];
2717 } else {
2718 ALOGW("%s: Did not have android.request.id set in the request",
2719 __FUNCTION__);
2720 requestId = NAME_NOT_FOUND;
2721 }
2722
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002723 // Insert any queued triggers (before metadata is locked)
2724 int32_t triggerCount;
2725 res = insertTriggers(nextRequest);
2726 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002727 SET_ERR("RequestThread: Unable to insert triggers "
2728 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002729 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002730 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2731 return false;
2732 }
2733 triggerCount = res;
2734
2735 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2736
2737 // If the request is the same as last, or we had triggers last time
2738 if (mPrevRequest != nextRequest || triggersMixedIn) {
2739 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002740 * HAL workaround:
2741 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2742 */
2743 res = addDummyTriggerIds(nextRequest);
2744 if (res != OK) {
2745 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2746 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002747 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002748 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2749 return false;
2750 }
2751
2752 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002753 * The request should be presorted so accesses in HAL
2754 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2755 */
2756 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002757 request.settings = nextRequest->mSettings.getAndLock();
2758 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002759 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2760
2761 IF_ALOGV() {
2762 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2763 find_camera_metadata_ro_entry(
2764 request.settings,
2765 ANDROID_CONTROL_AF_TRIGGER,
2766 &e
2767 );
2768 if (e.count > 0) {
2769 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2770 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002771 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002772 e.data.u8[0]);
2773 }
2774 }
2775 } else {
2776 // leave request.settings NULL to indicate 'reuse latest given'
2777 ALOGVV("%s: Request settings are REUSED",
2778 __FUNCTION__);
2779 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002780
Zhijun Hef0d962a2014-06-30 10:24:11 -07002781 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002782
2783 // Fill in buffers
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002784 if (nextRequest->mInputStream != NULL) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002785 request.input_buffer = &nextRequest->mInputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002786 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002787 } else {
2788 request.input_buffer = NULL;
2789 }
2790
2791 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2792 nextRequest->mOutputStreams.size());
2793 request.output_buffers = outputBuffers.array();
2794 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2795 res = nextRequest->mOutputStreams.editItemAt(i)->
2796 getBuffer(&outputBuffers.editItemAt(i));
2797 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002798 // Can't get output buffer from gralloc queue - this could be due to
2799 // abandoned queue or other consumer misbehavior, so not a fatal
2800 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002801 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2802 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala0ec23d32015-05-28 15:15:54 -07002803 {
2804 Mutex::Autolock l(mRequestLock);
2805 if (mListener != NULL) {
2806 mListener->notifyError(
2807 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2808 nextRequest->mResultExtras);
2809 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002810 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002811 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2812 return true;
2813 }
2814 request.num_output_buffers++;
2815 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002816 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002817
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002818 // Log request in the in-flight queue
2819 sp<Camera3Device> parent = mParent.promote();
2820 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002821 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002822 CLOGE("RequestThread: Parent is gone");
2823 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2824 return false;
2825 }
2826
Jianing Weicb0652e2014-03-12 18:29:36 -07002827 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002828 totalNumBuffers, nextRequest->mResultExtras,
2829 /*hasInput*/request.input_buffer != NULL);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002830 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2831 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002832 __FUNCTION__,
2833 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2834 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002835 if (res != OK) {
2836 SET_ERR("RequestThread: Unable to register new in-flight request:"
2837 " %s (%d)", strerror(-res), res);
2838 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2839 return false;
2840 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002841
Zhijun Hecc27e112013-10-03 16:12:43 -07002842 // Inform waitUntilRequestProcessed thread of a new request ID
2843 {
2844 Mutex::Autolock al(mLatestRequestMutex);
2845
2846 mLatestRequestId = requestId;
2847 mLatestRequestSignal.signal();
2848 }
2849
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002850 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002851 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2852 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002853 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002854 ATRACE_END();
2855
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002856 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002857 // Should only get a failure here for malformed requests or device-level
2858 // errors, so consider all errors fatal. Bad metadata failures should
2859 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002860 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002861 " device: %s (%d)", request.frame_number, strerror(-res), res);
2862 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2863 return false;
2864 }
2865
Igor Murashkin1e479c02013-09-06 16:55:14 -07002866 // Update the latest request sent to HAL
2867 if (request.settings != NULL) { // Don't update them if they were unchanged
2868 Mutex::Autolock al(mLatestRequestMutex);
2869
2870 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2871 mLatestRequest.acquire(cloned);
2872 }
2873
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002874 if (request.settings != NULL) {
2875 nextRequest->mSettings.unlock(request.settings);
2876 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002877
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002878 // Unset as current request
2879 {
2880 Mutex::Autolock l(mRequestLock);
2881 mNextRequest.clear();
2882 }
2883
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002884 // Remove any previously queued triggers (after unlock)
2885 res = removeTriggers(mPrevRequest);
2886 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002887 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002888 "(capture request %d, HAL device: %s (%d)",
2889 request.frame_number, strerror(-res), res);
2890 return false;
2891 }
2892 mPrevTriggers = triggerCount;
2893
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002894 return true;
2895}
2896
Igor Murashkin1e479c02013-09-06 16:55:14 -07002897CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2898 Mutex::Autolock al(mLatestRequestMutex);
2899
2900 ALOGV("RequestThread::%s", __FUNCTION__);
2901
2902 return mLatestRequest;
2903}
2904
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002905bool Camera3Device::RequestThread::isStreamPending(
2906 sp<Camera3StreamInterface>& stream) {
2907 Mutex::Autolock l(mRequestLock);
2908
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002909 if (mNextRequest != nullptr) {
2910 for (const auto& s : mNextRequest->mOutputStreams) {
2911 if (stream == s) return true;
2912 }
2913 if (stream == mNextRequest->mInputStream) return true;
2914 }
2915
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002916 for (const auto& request : mRequestQueue) {
2917 for (const auto& s : request->mOutputStreams) {
2918 if (stream == s) return true;
2919 }
2920 if (stream == request->mInputStream) return true;
2921 }
2922
2923 for (const auto& request : mRepeatingRequests) {
2924 for (const auto& s : request->mOutputStreams) {
2925 if (stream == s) return true;
2926 }
2927 if (stream == request->mInputStream) return true;
2928 }
2929
2930 return false;
2931}
Jianing Weicb0652e2014-03-12 18:29:36 -07002932
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002933void Camera3Device::RequestThread::cleanUpFailedRequest(
2934 camera3_capture_request_t &request,
2935 sp<CaptureRequest> &nextRequest,
2936 Vector<camera3_stream_buffer_t> &outputBuffers) {
2937
2938 if (request.settings != NULL) {
2939 nextRequest->mSettings.unlock(request.settings);
2940 }
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002941 if (nextRequest->mInputStream != NULL) {
2942 nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
2943 nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002944 }
2945 for (size_t i = 0; i < request.num_output_buffers; i++) {
2946 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2947 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2948 outputBuffers[i], 0);
2949 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002950
2951 Mutex::Autolock l(mRequestLock);
2952 mNextRequest.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002953}
2954
2955sp<Camera3Device::CaptureRequest>
2956 Camera3Device::RequestThread::waitForNextRequest() {
2957 status_t res;
2958 sp<CaptureRequest> nextRequest;
2959
2960 // Optimized a bit for the simple steady-state case (single repeating
2961 // request), to avoid putting that request in the queue temporarily.
2962 Mutex::Autolock l(mRequestLock);
2963
2964 while (mRequestQueue.empty()) {
2965 if (!mRepeatingRequests.empty()) {
2966 // Always atomically enqueue all requests in a repeating request
2967 // list. Guarantees a complete in-sequence set of captures to
2968 // application.
2969 const RequestList &requests = mRepeatingRequests;
2970 RequestList::const_iterator firstRequest =
2971 requests.begin();
2972 nextRequest = *firstRequest;
2973 mRequestQueue.insert(mRequestQueue.end(),
2974 ++firstRequest,
2975 requests.end());
2976 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002977
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002978 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002979
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002980 break;
2981 }
2982
2983 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2984
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002985 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2986 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002987 Mutex::Autolock pl(mPauseLock);
2988 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002989 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002990 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002991 // Let the tracker know
2992 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2993 if (statusTracker != 0) {
2994 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2995 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002996 }
2997 // Stop waiting for now and let thread management happen
2998 return NULL;
2999 }
3000 }
3001
3002 if (nextRequest == NULL) {
3003 // Don't have a repeating request already in hand, so queue
3004 // must have an entry now.
3005 RequestList::iterator firstRequest =
3006 mRequestQueue.begin();
3007 nextRequest = *firstRequest;
3008 mRequestQueue.erase(firstRequest);
3009 }
3010
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003011 // In case we've been unpaused by setPaused clearing mDoPause, need to
3012 // update internal pause state (capture/setRepeatingRequest unpause
3013 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003014 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003015 if (mPaused) {
3016 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3017 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3018 if (statusTracker != 0) {
3019 statusTracker->markComponentActive(mStatusId);
3020 }
3021 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003022 mPaused = false;
3023
3024 // Check if we've reconfigured since last time, and reset the preview
3025 // request if so. Can't use 'NULL request == repeat' across configure calls.
3026 if (mReconfigured) {
3027 mPrevRequest.clear();
3028 mReconfigured = false;
3029 }
3030
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003031 if (nextRequest != NULL) {
3032 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003033 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3034 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003035
3036 // Since RequestThread::clear() removes buffers from the input stream,
3037 // get the right buffer here before unlocking mRequestLock
3038 if (nextRequest->mInputStream != NULL) {
3039 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3040 if (res != OK) {
3041 // Can't get input buffer from gralloc queue - this could be due to
3042 // disconnected queue or other producer misbehavior, so not a fatal
3043 // error
3044 ALOGE("%s: Can't get input buffer, skipping request:"
3045 " %s (%d)", __FUNCTION__, strerror(-res), res);
3046 if (mListener != NULL) {
3047 mListener->notifyError(
3048 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3049 nextRequest->mResultExtras);
3050 }
3051 return NULL;
3052 }
3053 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003054 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003055 mNextRequest = nextRequest;
3056
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003057 return nextRequest;
3058}
3059
3060bool Camera3Device::RequestThread::waitIfPaused() {
3061 status_t res;
3062 Mutex::Autolock l(mPauseLock);
3063 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003064 if (mPaused == false) {
3065 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003066 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3067 // Let the tracker know
3068 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3069 if (statusTracker != 0) {
3070 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3071 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003072 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003073
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003074 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003075 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003076 return true;
3077 }
3078 }
3079 // We don't set mPaused to false here, because waitForNextRequest needs
3080 // to further manage the paused state in case of starvation.
3081 return false;
3082}
3083
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003084void Camera3Device::RequestThread::unpauseForNewRequests() {
3085 // With work to do, mark thread as unpaused.
3086 // If paused by request (setPaused), don't resume, to avoid
3087 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003088 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003089 Mutex::Autolock p(mPauseLock);
3090 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003091 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3092 if (mPaused) {
3093 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3094 if (statusTracker != 0) {
3095 statusTracker->markComponentActive(mStatusId);
3096 }
3097 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003098 mPaused = false;
3099 }
3100}
3101
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003102void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3103 sp<Camera3Device> parent = mParent.promote();
3104 if (parent != NULL) {
3105 va_list args;
3106 va_start(args, fmt);
3107
3108 parent->setErrorStateV(fmt, args);
3109
3110 va_end(args);
3111 }
3112}
3113
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003114status_t Camera3Device::RequestThread::insertTriggers(
3115 const sp<CaptureRequest> &request) {
3116
3117 Mutex::Autolock al(mTriggerMutex);
3118
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003119 sp<Camera3Device> parent = mParent.promote();
3120 if (parent == NULL) {
3121 CLOGE("RequestThread: Parent is gone");
3122 return DEAD_OBJECT;
3123 }
3124
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003125 CameraMetadata &metadata = request->mSettings;
3126 size_t count = mTriggerMap.size();
3127
3128 for (size_t i = 0; i < count; ++i) {
3129 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003130 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003131
3132 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3133 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3134 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003135 if (isAeTrigger) {
3136 request->mResultExtras.precaptureTriggerId = triggerId;
3137 mCurrentPreCaptureTriggerId = triggerId;
3138 } else {
3139 request->mResultExtras.afTriggerId = triggerId;
3140 mCurrentAfTriggerId = triggerId;
3141 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003142 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3143 continue; // Trigger ID tag is deprecated since device HAL 3.2
3144 }
3145 }
3146
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003147 camera_metadata_entry entry = metadata.find(tag);
3148
3149 if (entry.count > 0) {
3150 /**
3151 * Already has an entry for this trigger in the request.
3152 * Rewrite it with our requested trigger value.
3153 */
3154 RequestTrigger oldTrigger = trigger;
3155
3156 oldTrigger.entryValue = entry.data.u8[0];
3157
3158 mTriggerReplacedMap.add(tag, oldTrigger);
3159 } else {
3160 /**
3161 * More typical, no trigger entry, so we just add it
3162 */
3163 mTriggerRemovedMap.add(tag, trigger);
3164 }
3165
3166 status_t res;
3167
3168 switch (trigger.getTagType()) {
3169 case TYPE_BYTE: {
3170 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3171 res = metadata.update(tag,
3172 &entryValue,
3173 /*count*/1);
3174 break;
3175 }
3176 case TYPE_INT32:
3177 res = metadata.update(tag,
3178 &trigger.entryValue,
3179 /*count*/1);
3180 break;
3181 default:
3182 ALOGE("%s: Type not supported: 0x%x",
3183 __FUNCTION__,
3184 trigger.getTagType());
3185 return INVALID_OPERATION;
3186 }
3187
3188 if (res != OK) {
3189 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3190 ", value %d", __FUNCTION__, trigger.getTagName(),
3191 trigger.entryValue);
3192 return res;
3193 }
3194
3195 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3196 trigger.getTagName(),
3197 trigger.entryValue);
3198 }
3199
3200 mTriggerMap.clear();
3201
3202 return count;
3203}
3204
3205status_t Camera3Device::RequestThread::removeTriggers(
3206 const sp<CaptureRequest> &request) {
3207 Mutex::Autolock al(mTriggerMutex);
3208
3209 CameraMetadata &metadata = request->mSettings;
3210
3211 /**
3212 * Replace all old entries with their old values.
3213 */
3214 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3215 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3216
3217 status_t res;
3218
3219 uint32_t tag = trigger.metadataTag;
3220 switch (trigger.getTagType()) {
3221 case TYPE_BYTE: {
3222 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3223 res = metadata.update(tag,
3224 &entryValue,
3225 /*count*/1);
3226 break;
3227 }
3228 case TYPE_INT32:
3229 res = metadata.update(tag,
3230 &trigger.entryValue,
3231 /*count*/1);
3232 break;
3233 default:
3234 ALOGE("%s: Type not supported: 0x%x",
3235 __FUNCTION__,
3236 trigger.getTagType());
3237 return INVALID_OPERATION;
3238 }
3239
3240 if (res != OK) {
3241 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3242 ", trigger value %d", __FUNCTION__,
3243 trigger.getTagName(), trigger.entryValue);
3244 return res;
3245 }
3246 }
3247 mTriggerReplacedMap.clear();
3248
3249 /**
3250 * Remove all new entries.
3251 */
3252 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3253 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3254 status_t res = metadata.erase(trigger.metadataTag);
3255
3256 if (res != OK) {
3257 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3258 ", trigger value %d", __FUNCTION__,
3259 trigger.getTagName(), trigger.entryValue);
3260 return res;
3261 }
3262 }
3263 mTriggerRemovedMap.clear();
3264
3265 return OK;
3266}
3267
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003268status_t Camera3Device::RequestThread::addDummyTriggerIds(
3269 const sp<CaptureRequest> &request) {
3270 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3271 static const int32_t dummyTriggerId = 1;
3272 status_t res;
3273
3274 CameraMetadata &metadata = request->mSettings;
3275
3276 // If AF trigger is active, insert a dummy AF trigger ID if none already
3277 // exists
3278 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3279 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3280 if (afTrigger.count > 0 &&
3281 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3282 afId.count == 0) {
3283 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3284 if (res != OK) return res;
3285 }
3286
3287 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3288 // if none already exists
3289 camera_metadata_entry pcTrigger =
3290 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3291 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3292 if (pcTrigger.count > 0 &&
3293 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3294 pcId.count == 0) {
3295 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3296 &dummyTriggerId, 1);
3297 if (res != OK) return res;
3298 }
3299
3300 return OK;
3301}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003302
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003303/**
3304 * PreparerThread inner class methods
3305 */
3306
3307Camera3Device::PreparerThread::PreparerThread() :
3308 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3309}
3310
3311Camera3Device::PreparerThread::~PreparerThread() {
3312 Thread::requestExitAndWait();
3313 if (mCurrentStream != nullptr) {
3314 mCurrentStream->cancelPrepare();
3315 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3316 mCurrentStream.clear();
3317 }
3318 clear();
3319}
3320
3321status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) {
3322 status_t res;
3323
3324 Mutex::Autolock l(mLock);
3325
3326 res = stream->startPrepare();
3327 if (res == OK) {
3328 // No preparation needed, fire listener right off
3329 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3330 if (mListener) {
3331 mListener->notifyPrepared(stream->getId());
3332 }
3333 return OK;
3334 } else if (res != NOT_ENOUGH_DATA) {
3335 return res;
3336 }
3337
3338 // Need to prepare, start up thread if necessary
3339 if (!mActive) {
3340 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3341 // isn't running
3342 Thread::requestExitAndWait();
3343 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3344 if (res != OK) {
3345 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3346 if (mListener) {
3347 mListener->notifyPrepared(stream->getId());
3348 }
3349 return res;
3350 }
3351 mCancelNow = false;
3352 mActive = true;
3353 ALOGV("%s: Preparer stream started", __FUNCTION__);
3354 }
3355
3356 // queue up the work
3357 mPendingStreams.push_back(stream);
3358 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3359
3360 return OK;
3361}
3362
3363status_t Camera3Device::PreparerThread::clear() {
3364 status_t res;
3365
3366 Mutex::Autolock l(mLock);
3367
3368 for (const auto& stream : mPendingStreams) {
3369 stream->cancelPrepare();
3370 }
3371 mPendingStreams.clear();
3372 mCancelNow = true;
3373
3374 return OK;
3375}
3376
3377void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3378 Mutex::Autolock l(mLock);
3379 mListener = listener;
3380}
3381
3382bool Camera3Device::PreparerThread::threadLoop() {
3383 status_t res;
3384 {
3385 Mutex::Autolock l(mLock);
3386 if (mCurrentStream == nullptr) {
3387 // End thread if done with work
3388 if (mPendingStreams.empty()) {
3389 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3390 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3391 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3392 mActive = false;
3393 return false;
3394 }
3395
3396 // Get next stream to prepare
3397 auto it = mPendingStreams.begin();
3398 mCurrentStream = *it;
3399 mPendingStreams.erase(it);
3400 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3401 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3402 } else if (mCancelNow) {
3403 mCurrentStream->cancelPrepare();
3404 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3405 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3406 mCurrentStream.clear();
3407 mCancelNow = false;
3408 return true;
3409 }
3410 }
3411
3412 res = mCurrentStream->prepareNextBuffer();
3413 if (res == NOT_ENOUGH_DATA) return true;
3414 if (res != OK) {
3415 // Something bad happened; try to recover by cancelling prepare and
3416 // signalling listener anyway
3417 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3418 mCurrentStream->getId(), res, strerror(-res));
3419 mCurrentStream->cancelPrepare();
3420 }
3421
3422 // This stream has finished, notify listener
3423 Mutex::Autolock l(mLock);
3424 if (mListener) {
3425 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3426 mCurrentStream->getId());
3427 mListener->notifyPrepared(mCurrentStream->getId());
3428 }
3429
3430 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3431 mCurrentStream.clear();
3432
3433 return true;
3434}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003435
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003436/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003437 * Static callback forwarding methods from HAL to instance
3438 */
3439
3440void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3441 const camera3_capture_result *result) {
3442 Camera3Device *d =
3443 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3444 d->processCaptureResult(result);
3445}
3446
3447void Camera3Device::sNotify(const camera3_callback_ops *cb,
3448 const camera3_notify_msg *msg) {
3449 Camera3Device *d =
3450 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3451 d->notify(msg);
3452}
3453
3454}; // namespace android