blob: c28a57eede162aaae7b884c7ff5de57d6f85de8a [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);
Zhijun He1fa89992015-06-01 15:44:31 -07001036 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
Igor Murashkine2d167e2014-08-19 16:19:59 -07001037
1038 return configureStreamsLocked();
1039}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001040
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001041status_t Camera3Device::getInputBufferProducer(
1042 sp<IGraphicBufferProducer> *producer) {
1043 Mutex::Autolock il(mInterfaceLock);
1044 Mutex::Autolock l(mLock);
1045
1046 if (producer == NULL) {
1047 return BAD_VALUE;
1048 } else if (mInputStream == NULL) {
1049 return INVALID_OPERATION;
1050 }
1051
1052 return mInputStream->getInputBufferProducer(producer);
1053}
1054
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001055status_t Camera3Device::createDefaultRequest(int templateId,
1056 CameraMetadata *request) {
1057 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001058 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001059 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001060 Mutex::Autolock l(mLock);
1061
1062 switch (mStatus) {
1063 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001064 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001065 return INVALID_OPERATION;
1066 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001067 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001068 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001069 case STATUS_UNCONFIGURED:
1070 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071 case STATUS_ACTIVE:
1072 // OK
1073 break;
1074 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001075 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001076 return INVALID_OPERATION;
1077 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001078
Zhijun Hea1530f12014-09-14 12:44:20 -07001079 if (!mRequestTemplateCache[templateId].isEmpty()) {
1080 *request = mRequestTemplateCache[templateId];
1081 return OK;
1082 }
1083
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001084 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001085 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001086 rawRequest = mHal3Device->ops->construct_default_request_settings(
1087 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001088 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001089 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001090 ALOGI("%s: template %d is not supported on this camera device",
1091 __FUNCTION__, templateId);
1092 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001093 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001094 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001095 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001096
1097 return OK;
1098}
1099
1100status_t Camera3Device::waitUntilDrained() {
1101 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001102 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001103 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001104
Zhijun He69a37482014-03-23 18:44:49 -07001105 return waitUntilDrainedLocked();
1106}
1107
1108status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001109 switch (mStatus) {
1110 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001111 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001112 ALOGV("%s: Already idle", __FUNCTION__);
1113 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001114 case STATUS_CONFIGURED:
1115 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001116 case STATUS_ERROR:
1117 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001118 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001119 break;
1120 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001121 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001122 return INVALID_OPERATION;
1123 }
1124
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001125 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1126 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001127 if (res != OK) {
1128 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1129 res);
1130 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001131 return res;
1132}
1133
1134// Pause to reconfigure
1135status_t Camera3Device::internalPauseAndWaitLocked() {
1136 mRequestThread->setPaused(true);
1137 mPauseStateNotify = true;
1138
1139 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1140 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1141 if (res != OK) {
1142 SET_ERR_L("Can't idle device in %f seconds!",
1143 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001144 }
1145
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001146 return res;
1147}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001148
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001149// Resume after internalPauseAndWaitLocked
1150status_t Camera3Device::internalResumeLocked() {
1151 status_t res;
1152
1153 mRequestThread->setPaused(false);
1154
1155 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1156 if (res != OK) {
1157 SET_ERR_L("Can't transition to active in %f seconds!",
1158 kActiveTimeout/1e9);
1159 }
1160 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001161 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001162}
1163
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001164status_t Camera3Device::waitUntilStateThenRelock(bool active,
1165 nsecs_t timeout) {
1166 status_t res = OK;
1167 if (active == (mStatus == STATUS_ACTIVE)) {
1168 // Desired state already reached
1169 return res;
1170 }
1171
1172 bool stateSeen = false;
1173 do {
1174 mRecentStatusUpdates.clear();
1175
1176 res = mStatusChanged.waitRelative(mLock, timeout);
1177 if (res != OK) break;
1178
1179 // Check state change history during wait
1180 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1181 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1182 stateSeen = true;
1183 break;
1184 }
1185 }
1186 } while (!stateSeen);
1187
1188 return res;
1189}
1190
1191
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001192status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1193 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001194 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001195
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001196 if (listener != NULL && mListener != NULL) {
1197 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1198 }
1199 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001200 mRequestThread->setNotificationListener(listener);
1201 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001202
1203 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001204}
1205
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001206bool Camera3Device::willNotify3A() {
1207 return false;
1208}
1209
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001210status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001211 status_t res;
1212 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001213
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001214 while (mResultQueue.empty()) {
1215 res = mResultSignal.waitRelative(mOutputLock, timeout);
1216 if (res == TIMED_OUT) {
1217 return res;
1218 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001219 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001220 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001221 return res;
1222 }
1223 }
1224 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001225}
1226
Jianing Weicb0652e2014-03-12 18:29:36 -07001227status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001228 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001229 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001230
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001231 if (mResultQueue.empty()) {
1232 return NOT_ENOUGH_DATA;
1233 }
1234
Jianing Weicb0652e2014-03-12 18:29:36 -07001235 if (frame == NULL) {
1236 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1237 return BAD_VALUE;
1238 }
1239
1240 CaptureResult &result = *(mResultQueue.begin());
1241 frame->mResultExtras = result.mResultExtras;
1242 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001243 mResultQueue.erase(mResultQueue.begin());
1244
1245 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001246}
1247
1248status_t Camera3Device::triggerAutofocus(uint32_t id) {
1249 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001250 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001251
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001252 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1253 // Mix-in this trigger into the next request and only the next request.
1254 RequestTrigger trigger[] = {
1255 {
1256 ANDROID_CONTROL_AF_TRIGGER,
1257 ANDROID_CONTROL_AF_TRIGGER_START
1258 },
1259 {
1260 ANDROID_CONTROL_AF_TRIGGER_ID,
1261 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001262 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001263 };
1264
1265 return mRequestThread->queueTrigger(trigger,
1266 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001267}
1268
1269status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1270 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001271 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001272
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001273 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1274 // Mix-in this trigger into the next request and only the next request.
1275 RequestTrigger trigger[] = {
1276 {
1277 ANDROID_CONTROL_AF_TRIGGER,
1278 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1279 },
1280 {
1281 ANDROID_CONTROL_AF_TRIGGER_ID,
1282 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001283 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001284 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001285
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001286 return mRequestThread->queueTrigger(trigger,
1287 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001288}
1289
1290status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1291 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001292 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001293
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001294 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1295 // Mix-in this trigger into the next request and only the next request.
1296 RequestTrigger trigger[] = {
1297 {
1298 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1299 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1300 },
1301 {
1302 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1303 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001304 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001305 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001306
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001307 return mRequestThread->queueTrigger(trigger,
1308 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001309}
1310
1311status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1312 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1313 ATRACE_CALL();
1314 (void)reprocessStreamId; (void)buffer; (void)listener;
1315
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001316 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001317 return INVALID_OPERATION;
1318}
1319
Jianing Weicb0652e2014-03-12 18:29:36 -07001320status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001321 ATRACE_CALL();
1322 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001323 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001324
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001325 NotificationListener* listener;
1326 {
1327 Mutex::Autolock l(mOutputLock);
1328 listener = mListener;
1329 }
1330
Zhijun He7ef20392014-04-21 16:04:17 -07001331 {
1332 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001333 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001334 }
1335
Zhijun He491e3412013-12-27 10:57:44 -08001336 status_t res;
1337 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1338 res = mHal3Device->ops->flush(mHal3Device);
1339 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001340 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001341 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001342 }
1343
1344 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001345}
1346
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001347status_t Camera3Device::prepare(int streamId) {
1348 ATRACE_CALL();
1349 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001350 Mutex::Autolock il(mInterfaceLock);
1351 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001352
1353 sp<Camera3StreamInterface> stream;
1354 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1355 if (outputStreamIdx == NAME_NOT_FOUND) {
1356 CLOGE("Stream %d does not exist", streamId);
1357 return BAD_VALUE;
1358 }
1359
1360 stream = mOutputStreams.editValueAt(outputStreamIdx);
1361
1362 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001363 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001364 return BAD_VALUE;
1365 }
1366
1367 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001368 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001369 return BAD_VALUE;
1370 }
1371
1372 return mPreparerThread->prepare(stream);
1373}
1374
Zhijun He204e3292014-07-14 17:09:23 -07001375uint32_t Camera3Device::getDeviceVersion() {
1376 ATRACE_CALL();
1377 Mutex::Autolock il(mInterfaceLock);
1378 return mDeviceVersion;
1379}
1380
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001382 * Methods called by subclasses
1383 */
1384
1385void Camera3Device::notifyStatus(bool idle) {
1386 {
1387 // Need mLock to safely update state and synchronize to current
1388 // state of methods in flight.
1389 Mutex::Autolock l(mLock);
1390 // We can get various system-idle notices from the status tracker
1391 // while starting up. Only care about them if we've actually sent
1392 // in some requests recently.
1393 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1394 return;
1395 }
1396 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1397 idle ? "idle" : "active");
1398 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1399 mRecentStatusUpdates.add(mStatus);
1400 mStatusChanged.signal();
1401
1402 // Skip notifying listener if we're doing some user-transparent
1403 // state changes
1404 if (mPauseStateNotify) return;
1405 }
1406 NotificationListener *listener;
1407 {
1408 Mutex::Autolock l(mOutputLock);
1409 listener = mListener;
1410 }
1411 if (idle && listener != NULL) {
1412 listener->notifyIdle();
1413 }
1414}
1415
1416/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001417 * Camera3Device private methods
1418 */
1419
1420sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1421 const CameraMetadata &request) {
1422 ATRACE_CALL();
1423 status_t res;
1424
1425 sp<CaptureRequest> newRequest = new CaptureRequest;
1426 newRequest->mSettings = request;
1427
1428 camera_metadata_entry_t inputStreams =
1429 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1430 if (inputStreams.count > 0) {
1431 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001432 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001433 CLOGE("Request references unknown input stream %d",
1434 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001435 return NULL;
1436 }
1437 // Lazy completion of stream configuration (allocation/registration)
1438 // on first use
1439 if (mInputStream->isConfiguring()) {
1440 res = mInputStream->finishConfiguration(mHal3Device);
1441 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001442 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001443 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001444 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001445 return NULL;
1446 }
1447 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001448 // Check if stream is being prepared
1449 if (mInputStream->isPreparing()) {
1450 CLOGE("Request references an input stream that's being prepared!");
1451 return NULL;
1452 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001453
1454 newRequest->mInputStream = mInputStream;
1455 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1456 }
1457
1458 camera_metadata_entry_t streams =
1459 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1460 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001461 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001462 return NULL;
1463 }
1464
1465 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001466 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001467 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001468 CLOGE("Request references unknown stream %d",
1469 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001470 return NULL;
1471 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001472 sp<Camera3OutputStreamInterface> stream =
1473 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001474
1475 // Lazy completion of stream configuration (allocation/registration)
1476 // on first use
1477 if (stream->isConfiguring()) {
1478 res = stream->finishConfiguration(mHal3Device);
1479 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001480 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1481 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001482 return NULL;
1483 }
1484 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001485 // Check if stream is being prepared
1486 if (stream->isPreparing()) {
1487 CLOGE("Request references an output stream that's being prepared!");
1488 return NULL;
1489 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001490
1491 newRequest->mOutputStreams.push(stream);
1492 }
1493 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1494
1495 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001496}
1497
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001498bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1499 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1500 Size size = mSupportedOpaqueInputSizes[i];
1501 if (size.width == width && size.height == height) {
1502 return true;
1503 }
1504 }
1505
1506 return false;
1507}
1508
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001509status_t Camera3Device::configureStreamsLocked() {
1510 ATRACE_CALL();
1511 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001512
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001513 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001514 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001515 return INVALID_OPERATION;
1516 }
1517
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001518 if (!mNeedConfig) {
1519 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1520 return OK;
1521 }
1522
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001523 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1524 // adding a dummy stream instead.
1525 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1526 if (mOutputStreams.size() == 0) {
1527 addDummyStreamLocked();
1528 } else {
1529 tryRemoveDummyStreamLocked();
1530 }
1531
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001532 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001533 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001534
1535 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001536 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1537 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1538 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001539 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1540
1541 Vector<camera3_stream_t*> streams;
1542 streams.setCapacity(config.num_streams);
1543
1544 if (mInputStream != NULL) {
1545 camera3_stream_t *inputStream;
1546 inputStream = mInputStream->startConfiguration();
1547 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001548 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001549 return INVALID_OPERATION;
1550 }
1551 streams.add(inputStream);
1552 }
1553
1554 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001555
1556 // Don't configure bidi streams twice, nor add them twice to the list
1557 if (mOutputStreams[i].get() ==
1558 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1559
1560 config.num_streams--;
1561 continue;
1562 }
1563
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001564 camera3_stream_t *outputStream;
1565 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1566 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001567 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001568 return INVALID_OPERATION;
1569 }
1570 streams.add(outputStream);
1571 }
1572
1573 config.streams = streams.editArray();
1574
1575 // Do the HAL configuration; will potentially touch stream
1576 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001577 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001578 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001579 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001580
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001581 if (res == BAD_VALUE) {
1582 // HAL rejected this set of streams as unsupported, clean up config
1583 // attempt and return to unconfigured state
1584 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1585 res = mInputStream->cancelConfiguration();
1586 if (res != OK) {
1587 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1588 mInputStream->getId(), strerror(-res), res);
1589 return res;
1590 }
1591 }
1592
1593 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1594 sp<Camera3OutputStreamInterface> outputStream =
1595 mOutputStreams.editValueAt(i);
1596 if (outputStream->isConfiguring()) {
1597 res = outputStream->cancelConfiguration();
1598 if (res != OK) {
1599 SET_ERR_L(
1600 "Can't cancel configuring output stream %d: %s (%d)",
1601 outputStream->getId(), strerror(-res), res);
1602 return res;
1603 }
1604 }
1605 }
1606
1607 // Return state to that at start of call, so that future configures
1608 // properly clean things up
1609 mStatus = STATUS_UNCONFIGURED;
1610 mNeedConfig = true;
1611
1612 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1613 return BAD_VALUE;
1614 } else if (res != OK) {
1615 // Some other kind of error from configure_streams - this is not
1616 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001617 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1618 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001619 return res;
1620 }
1621
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001622 // Finish all stream configuration immediately.
1623 // TODO: Try to relax this later back to lazy completion, which should be
1624 // faster
1625
Igor Murashkin073f8572013-05-02 14:59:28 -07001626 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001627 res = mInputStream->finishConfiguration(mHal3Device);
1628 if (res != OK) {
1629 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1630 mInputStream->getId(), strerror(-res), res);
1631 return res;
1632 }
1633 }
1634
1635 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001636 sp<Camera3OutputStreamInterface> outputStream =
1637 mOutputStreams.editValueAt(i);
1638 if (outputStream->isConfiguring()) {
1639 res = outputStream->finishConfiguration(mHal3Device);
1640 if (res != OK) {
1641 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1642 outputStream->getId(), strerror(-res), res);
1643 return res;
1644 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001645 }
1646 }
1647
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001648 // Request thread needs to know to avoid using repeat-last-settings protocol
1649 // across configure_streams() calls
1650 mRequestThread->configurationComplete();
1651
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001652 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001653
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001654 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001655
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001656 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001657 mStatus = STATUS_CONFIGURED;
1658 } else {
1659 mStatus = STATUS_UNCONFIGURED;
1660 }
1661
1662 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1663
Zhijun He0a210512014-07-24 13:45:15 -07001664 // tear down the deleted streams after configure streams.
1665 mDeletedStreams.clear();
1666
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001667 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001668}
1669
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001670status_t Camera3Device::addDummyStreamLocked() {
1671 ATRACE_CALL();
1672 status_t res;
1673
1674 if (mDummyStreamId != NO_STREAM) {
1675 // Should never be adding a second dummy stream when one is already
1676 // active
1677 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1678 __FUNCTION__, mId);
1679 return INVALID_OPERATION;
1680 }
1681
1682 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1683
1684 sp<Camera3OutputStreamInterface> dummyStream =
1685 new Camera3DummyStream(mNextStreamId);
1686
1687 res = mOutputStreams.add(mNextStreamId, dummyStream);
1688 if (res < 0) {
1689 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1690 return res;
1691 }
1692
1693 mDummyStreamId = mNextStreamId;
1694 mNextStreamId++;
1695
1696 return OK;
1697}
1698
1699status_t Camera3Device::tryRemoveDummyStreamLocked() {
1700 ATRACE_CALL();
1701 status_t res;
1702
1703 if (mDummyStreamId == NO_STREAM) return OK;
1704 if (mOutputStreams.size() == 1) return OK;
1705
1706 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1707
1708 // Ok, have a dummy stream and there's at least one other output stream,
1709 // so remove the dummy
1710
1711 sp<Camera3StreamInterface> deletedStream;
1712 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1713 if (outputStreamIdx == NAME_NOT_FOUND) {
1714 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1715 return INVALID_OPERATION;
1716 }
1717
1718 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1719 mOutputStreams.removeItemsAt(outputStreamIdx);
1720
1721 // Free up the stream endpoint so that it can be used by some other stream
1722 res = deletedStream->disconnect();
1723 if (res != OK) {
1724 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1725 // fall through since we want to still list the stream as deleted.
1726 }
1727 mDeletedStreams.add(deletedStream);
1728 mDummyStreamId = NO_STREAM;
1729
1730 return res;
1731}
1732
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001733void Camera3Device::setErrorState(const char *fmt, ...) {
1734 Mutex::Autolock l(mLock);
1735 va_list args;
1736 va_start(args, fmt);
1737
1738 setErrorStateLockedV(fmt, args);
1739
1740 va_end(args);
1741}
1742
1743void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1744 Mutex::Autolock l(mLock);
1745 setErrorStateLockedV(fmt, args);
1746}
1747
1748void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1749 va_list args;
1750 va_start(args, fmt);
1751
1752 setErrorStateLockedV(fmt, args);
1753
1754 va_end(args);
1755}
1756
1757void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001758 // Print out all error messages to log
1759 String8 errorCause = String8::formatV(fmt, args);
1760 ALOGE("Camera %d: %s", mId, errorCause.string());
1761
1762 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001763 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001764
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001765 mErrorCause = errorCause;
1766
1767 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001768 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001769
1770 // Notify upstream about a device error
1771 if (mListener != NULL) {
1772 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1773 CaptureResultExtras());
1774 }
1775
1776 // Save stack trace. View by dumping it later.
1777 CameraTraces::saveTrace();
1778 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001779}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001780
1781/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001782 * In-flight request management
1783 */
1784
Jianing Weicb0652e2014-03-12 18:29:36 -07001785status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001786 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001787 ATRACE_CALL();
1788 Mutex::Autolock l(mInFlightLock);
1789
1790 ssize_t res;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001791 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001792 if (res < 0) return res;
1793
1794 return OK;
1795}
1796
1797/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001798 * Check if all 3A fields are ready, and send off a partial 3A-only result
1799 * to the output frame queue
1800 */
Zhijun He204e3292014-07-14 17:09:23 -07001801bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001802 uint32_t frameNumber,
1803 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001804
1805 // Check if all 3A states are present
1806 // The full list of fields is
1807 // android.control.afMode
1808 // android.control.awbMode
1809 // android.control.aeState
1810 // android.control.awbState
1811 // android.control.afState
1812 // android.control.afTriggerID
1813 // android.control.aePrecaptureID
1814 // TODO: Add android.control.aeMode
1815
1816 bool gotAllStates = true;
1817
1818 uint8_t afMode;
1819 uint8_t awbMode;
1820 uint8_t aeState;
1821 uint8_t afState;
1822 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001823
1824 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1825 &afMode, frameNumber);
1826
1827 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1828 &awbMode, frameNumber);
1829
1830 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1831 &aeState, frameNumber);
1832
1833 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1834 &afState, frameNumber);
1835
1836 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1837 &awbState, frameNumber);
1838
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001839 if (!gotAllStates) return false;
1840
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001841 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001842 "AF state %d, AE state %d, AWB state %d, "
1843 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001844 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001845 afMode, awbMode,
1846 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001847 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001848
1849 // Got all states, so construct a minimal result to send
1850 // In addition to the above fields, this means adding in
1851 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001852 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001853 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001854
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001855 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001856
1857 Mutex::Autolock l(mOutputLock);
1858
Jianing Weicb0652e2014-03-12 18:29:36 -07001859 CaptureResult captureResult;
1860 captureResult.mResultExtras = resultExtras;
1861 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1862 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1863 // but not limited to CameraDeviceBase::getNextResult
1864 CaptureResult& min3AResult =
1865 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001866
Jianing Weicb0652e2014-03-12 18:29:36 -07001867 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1868 // TODO: This is problematic casting. Need to fix CameraMetadata.
1869 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001870 return false;
1871 }
1872
Jianing Weicb0652e2014-03-12 18:29:36 -07001873 int32_t requestId = resultExtras.requestId;
1874 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001875 &requestId, frameNumber)) {
1876 return false;
1877 }
1878
Zhijun He204e3292014-07-14 17:09:23 -07001879 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1880 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1881 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1882 &partialResult, frameNumber)) {
1883 return false;
1884 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001885 }
1886
Jianing Weicb0652e2014-03-12 18:29:36 -07001887 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001888 &afMode, frameNumber)) {
1889 return false;
1890 }
1891
Jianing Weicb0652e2014-03-12 18:29:36 -07001892 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001893 &awbMode, frameNumber)) {
1894 return false;
1895 }
1896
Jianing Weicb0652e2014-03-12 18:29:36 -07001897 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001898 &aeState, frameNumber)) {
1899 return false;
1900 }
1901
Jianing Weicb0652e2014-03-12 18:29:36 -07001902 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001903 &afState, frameNumber)) {
1904 return false;
1905 }
1906
Jianing Weicb0652e2014-03-12 18:29:36 -07001907 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001908 &awbState, frameNumber)) {
1909 return false;
1910 }
1911
Jianing Weicb0652e2014-03-12 18:29:36 -07001912 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001913 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001914 return false;
1915 }
1916
Jianing Weicb0652e2014-03-12 18:29:36 -07001917 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001918 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001919 return false;
1920 }
1921
Zhijun He204e3292014-07-14 17:09:23 -07001922 // We only send the aggregated partial when all 3A related metadata are available
1923 // For both API1 and API2.
1924 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001925 mResultSignal.signal();
1926
1927 return true;
1928}
1929
1930template<typename T>
1931bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001932 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001933 (void) frameNumber;
1934
1935 camera_metadata_ro_entry_t entry;
1936
1937 entry = result.find(tag);
1938 if (entry.count == 0) {
1939 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1940 mId, frameNumber, get_camera_metadata_tag_name(tag));
1941 return false;
1942 }
1943
1944 if (sizeof(T) == sizeof(uint8_t)) {
1945 *value = entry.data.u8[0];
1946 } else if (sizeof(T) == sizeof(int32_t)) {
1947 *value = entry.data.i32[0];
1948 } else {
1949 ALOGE("%s: Unexpected type", __FUNCTION__);
1950 return false;
1951 }
1952 return true;
1953}
1954
1955template<typename T>
1956bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001957 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001958 if (result.update(tag, value, 1) != NO_ERROR) {
1959 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1960 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1961 frameNumber, get_camera_metadata_tag_name(tag));
1962 return false;
1963 }
1964 return true;
1965}
1966
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08001967void Camera3Device::returnOutputBuffers(
1968 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
1969 nsecs_t timestamp) {
1970 for (size_t i = 0; i < numBuffers; i++)
1971 {
1972 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
1973 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
1974 // Note: stream may be deallocated at this point, if this buffer was
1975 // the last reference to it.
1976 if (res != OK) {
1977 ALOGE("Can't return buffer to its stream: %s (%d)",
1978 strerror(-res), res);
1979 }
1980 }
1981}
1982
1983
1984void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
1985
1986 const InFlightRequest &request = mInFlightMap.valueAt(idx);
1987 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
1988
1989 nsecs_t sensorTimestamp = request.sensorTimestamp;
1990 nsecs_t shutterTimestamp = request.shutterTimestamp;
1991
1992 // Check if it's okay to remove the request from InFlightMap:
1993 // In the case of a successful request:
1994 // all input and output buffers, all result metadata, shutter callback
1995 // arrived.
1996 // In the case of a unsuccessful request:
1997 // all input and output buffers arrived.
1998 if (request.numBuffersLeft == 0 &&
1999 (request.requestStatus != OK ||
2000 (request.haveResultMetadata && shutterTimestamp != 0))) {
2001 ATRACE_ASYNC_END("frame capture", frameNumber);
2002
2003 // Sanity check - if sensor timestamp matches shutter timestamp
2004 if (request.requestStatus == OK &&
2005 sensorTimestamp != shutterTimestamp) {
2006 SET_ERR("sensor timestamp (%" PRId64
2007 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2008 sensorTimestamp, frameNumber, shutterTimestamp);
2009 }
2010
2011 // for an unsuccessful request, it may have pending output buffers to
2012 // return.
2013 assert(request.requestStatus != OK ||
2014 request.pendingOutputBuffers.size() == 0);
2015 returnOutputBuffers(request.pendingOutputBuffers.array(),
2016 request.pendingOutputBuffers.size(), 0);
2017
2018 mInFlightMap.removeItemsAt(idx, 1);
2019
2020 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2021 }
2022
2023 // Sanity check - if we have too many in-flight frames, something has
2024 // likely gone wrong
2025 if (mInFlightMap.size() > kInFlightWarnLimit) {
2026 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
2027 }
2028}
2029
2030
2031void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2032 CaptureResultExtras &resultExtras,
2033 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002034 uint32_t frameNumber,
2035 bool reprocess) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002036 if (pendingMetadata.isEmpty())
2037 return;
2038
2039 Mutex::Autolock l(mOutputLock);
2040
2041 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002042 if (reprocess) {
2043 if (frameNumber < mNextReprocessResultFrameNumber) {
2044 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002045 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002046 frameNumber, mNextReprocessResultFrameNumber);
2047 return;
2048 }
2049 mNextReprocessResultFrameNumber = frameNumber + 1;
2050 } else {
2051 if (frameNumber < mNextResultFrameNumber) {
2052 SET_ERR("Out-of-order capture result metadata submitted! "
2053 "(got frame number %d, expecting %d)",
2054 frameNumber, mNextResultFrameNumber);
2055 return;
2056 }
2057 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002058 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002059
2060 CaptureResult captureResult;
2061 captureResult.mResultExtras = resultExtras;
2062 captureResult.mMetadata = pendingMetadata;
2063
2064 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2065 (int32_t*)&frameNumber, 1) != OK) {
2066 SET_ERR("Failed to set frame# in metadata (%d)",
2067 frameNumber);
2068 return;
2069 } else {
2070 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2071 __FUNCTION__, mId, frameNumber);
2072 }
2073
2074 // Append any previous partials to form a complete result
2075 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2076 captureResult.mMetadata.append(collectedPartialResult);
2077 }
2078
2079 captureResult.mMetadata.sort();
2080
2081 // Check that there's a timestamp in the result metadata
2082 camera_metadata_entry entry =
2083 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2084 if (entry.count == 0) {
2085 SET_ERR("No timestamp provided by HAL for frame %d!",
2086 frameNumber);
2087 return;
2088 }
2089
2090 // Valid result, insert into queue
2091 List<CaptureResult>::iterator queuedResult =
2092 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2093 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2094 ", burstId = %" PRId32, __FUNCTION__,
2095 queuedResult->mResultExtras.requestId,
2096 queuedResult->mResultExtras.frameNumber,
2097 queuedResult->mResultExtras.burstId);
2098
2099 mResultSignal.signal();
2100}
2101
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002102/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002103 * Camera HAL device callback methods
2104 */
2105
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002106void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002107 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002108
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002109 status_t res;
2110
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002111 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002112 if (result->result == NULL && result->num_output_buffers == 0 &&
2113 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002114 SET_ERR("No result data provided by HAL for frame %d",
2115 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002116 return;
2117 }
Zhijun He204e3292014-07-14 17:09:23 -07002118
2119 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2120 // partial_result to 1 when metadata is included in this result.
2121 if (!mUsePartialResult &&
2122 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2123 result->result != NULL &&
2124 result->partial_result != 1) {
2125 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2126 " if partial result is not supported",
2127 frameNumber, result->partial_result);
2128 return;
2129 }
2130
2131 bool isPartialResult = false;
2132 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002133 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002134 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002135
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002136 // Get shutter timestamp and resultExtras from list of in-flight requests,
2137 // where it was added by the shutter notification for this frame. If the
2138 // shutter timestamp isn't received yet, append the output buffers to the
2139 // in-flight request and they will be returned when the shutter timestamp
2140 // arrives. Update the in-flight status and remove the in-flight entry if
2141 // all result data and shutter timestamp have been received.
2142 nsecs_t shutterTimestamp = 0;
2143
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002144 {
2145 Mutex::Autolock l(mInFlightLock);
2146 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2147 if (idx == NAME_NOT_FOUND) {
2148 SET_ERR("Unknown frame number for capture result: %d",
2149 frameNumber);
2150 return;
2151 }
2152 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002153 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2154 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2155 ", partialResultCount = %d",
2156 __FUNCTION__, request.resultExtras.requestId,
2157 request.resultExtras.frameNumber, request.resultExtras.burstId,
2158 result->partial_result);
2159 // Always update the partial count to the latest one if it's not 0
2160 // (buffers only). When framework aggregates adjacent partial results
2161 // into one, the latest partial count will be used.
2162 if (result->partial_result != 0)
2163 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002164
2165 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002166 if (mUsePartialResult && result->result != NULL) {
2167 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2168 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2169 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2170 " the range of [1, %d] when metadata is included in the result",
2171 frameNumber, result->partial_result, mNumPartialResults);
2172 return;
2173 }
2174 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002175 if (isPartialResult) {
2176 request.partialResult.collectedResult.append(result->result);
2177 }
Zhijun He204e3292014-07-14 17:09:23 -07002178 } else {
2179 camera_metadata_ro_entry_t partialResultEntry;
2180 res = find_camera_metadata_ro_entry(result->result,
2181 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2182 if (res != NAME_NOT_FOUND &&
2183 partialResultEntry.count > 0 &&
2184 partialResultEntry.data.u8[0] ==
2185 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2186 // A partial result. Flag this as such, and collect this
2187 // set of metadata into the in-flight entry.
2188 isPartialResult = true;
2189 request.partialResult.collectedResult.append(
2190 result->result);
2191 request.partialResult.collectedResult.erase(
2192 ANDROID_QUIRKS_PARTIAL_RESULT);
2193 }
2194 }
2195
2196 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002197 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002198 if (!request.partialResult.haveSent3A) {
2199 request.partialResult.haveSent3A =
2200 processPartial3AResult(frameNumber,
2201 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002202 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002203 }
2204 }
2205 }
2206
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002207 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002208 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002209
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002210 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002211 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002212 if (request.haveResultMetadata) {
2213 SET_ERR("Called multiple times with metadata for frame %d",
2214 frameNumber);
2215 return;
2216 }
Zhijun He204e3292014-07-14 17:09:23 -07002217 if (mUsePartialResult &&
2218 !request.partialResult.collectedResult.isEmpty()) {
2219 collectedPartialResult.acquire(
2220 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002221 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002222 request.haveResultMetadata = true;
2223 }
2224
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002225 uint32_t numBuffersReturned = result->num_output_buffers;
2226 if (result->input_buffer != NULL) {
2227 if (hasInputBufferInRequest) {
2228 numBuffersReturned += 1;
2229 } else {
2230 ALOGW("%s: Input buffer should be NULL if there is no input"
2231 " buffer sent in the request",
2232 __FUNCTION__);
2233 }
2234 }
2235 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002236 if (request.numBuffersLeft < 0) {
2237 SET_ERR("Too many buffers returned for frame %d",
2238 frameNumber);
2239 return;
2240 }
2241
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002242 camera_metadata_ro_entry_t entry;
2243 res = find_camera_metadata_ro_entry(result->result,
2244 ANDROID_SENSOR_TIMESTAMP, &entry);
2245 if (res == OK && entry.count == 1) {
2246 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002247 }
2248
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002249 // If shutter event isn't received yet, append the output buffers to
2250 // the in-flight request. Otherwise, return the output buffers to
2251 // streams.
2252 if (shutterTimestamp == 0) {
2253 request.pendingOutputBuffers.appendArray(result->output_buffers,
2254 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002255 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002256 returnOutputBuffers(result->output_buffers,
2257 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002258 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002259
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002260 if (result->result != NULL && !isPartialResult) {
2261 if (shutterTimestamp == 0) {
2262 request.pendingMetadata = result->result;
2263 request.partialResult.collectedResult = collectedPartialResult;
2264 } else {
2265 CameraMetadata metadata;
2266 metadata = result->result;
2267 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002268 collectedPartialResult, frameNumber, hasInputBufferInRequest);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002269 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002270 }
2271
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002272 removeInFlightRequestIfReadyLocked(idx);
2273 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002274
Zhijun Hef0d962a2014-06-30 10:24:11 -07002275 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002276 if (hasInputBufferInRequest) {
2277 Camera3Stream *stream =
2278 Camera3Stream::cast(result->input_buffer->stream);
2279 res = stream->returnInputBuffer(*(result->input_buffer));
2280 // Note: stream may be deallocated at this point, if this buffer was the
2281 // last reference to it.
2282 if (res != OK) {
2283 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2284 " its stream:%s (%d)", __FUNCTION__,
2285 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002286 }
2287 } else {
2288 ALOGW("%s: Input buffer should be NULL if there is no input"
2289 " buffer sent in the request, skipping input buffer return.",
2290 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002291 }
2292 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002293}
2294
2295void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002296 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002297 NotificationListener *listener;
2298 {
2299 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002300 listener = mListener;
2301 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002302
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002303 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002304 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002305 return;
2306 }
2307
2308 switch (msg->type) {
2309 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002310 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002311 break;
2312 }
2313 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002314 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002315 break;
2316 }
2317 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002318 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002319 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002320 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002321}
2322
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002323void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2324 NotificationListener *listener) {
2325
2326 // Map camera HAL error codes to ICameraDeviceCallback error codes
2327 // Index into this with the HAL error code
2328 static const ICameraDeviceCallbacks::CameraErrorCode
2329 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2330 // 0 = Unused error code
2331 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2332 // 1 = CAMERA3_MSG_ERROR_DEVICE
2333 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2334 // 2 = CAMERA3_MSG_ERROR_REQUEST
2335 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2336 // 3 = CAMERA3_MSG_ERROR_RESULT
2337 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2338 // 4 = CAMERA3_MSG_ERROR_BUFFER
2339 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2340 };
2341
2342 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2343 ((msg.error_code >= 0) &&
2344 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2345 halErrorMap[msg.error_code] :
2346 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2347
2348 int streamId = 0;
2349 if (msg.error_stream != NULL) {
2350 Camera3Stream *stream =
2351 Camera3Stream::cast(msg.error_stream);
2352 streamId = stream->getId();
2353 }
2354 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2355 mId, __FUNCTION__, msg.frame_number,
2356 streamId, msg.error_code);
2357
2358 CaptureResultExtras resultExtras;
2359 switch (errorCode) {
2360 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2361 // SET_ERR calls notifyError
2362 SET_ERR("Camera HAL reported serious device error");
2363 break;
2364 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2365 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2366 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2367 {
2368 Mutex::Autolock l(mInFlightLock);
2369 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2370 if (idx >= 0) {
2371 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2372 r.requestStatus = msg.error_code;
2373 resultExtras = r.resultExtras;
2374 } else {
2375 resultExtras.frameNumber = msg.frame_number;
2376 ALOGE("Camera %d: %s: cannot find in-flight request on "
2377 "frame %" PRId64 " error", mId, __FUNCTION__,
2378 resultExtras.frameNumber);
2379 }
2380 }
2381 if (listener != NULL) {
2382 listener->notifyError(errorCode, resultExtras);
2383 } else {
2384 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2385 }
2386 break;
2387 default:
2388 // SET_ERR calls notifyError
2389 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2390 break;
2391 }
2392}
2393
2394void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2395 NotificationListener *listener) {
2396 ssize_t idx;
2397 // Verify ordering of shutter notifications
2398 {
2399 Mutex::Autolock l(mOutputLock);
2400 // TODO: need to track errors for tighter bounds on expected frame number.
2401 if (msg.frame_number < mNextShutterFrameNumber) {
2402 SET_ERR("Shutter notification out-of-order. Expected "
2403 "notification for frame %d, got frame %d",
2404 mNextShutterFrameNumber, msg.frame_number);
2405 return;
2406 }
2407 mNextShutterFrameNumber = msg.frame_number + 1;
2408 }
2409
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002410 // Set timestamp for the request in the in-flight tracking
2411 // and get the request ID to send upstream
2412 {
2413 Mutex::Autolock l(mInFlightLock);
2414 idx = mInFlightMap.indexOfKey(msg.frame_number);
2415 if (idx >= 0) {
2416 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002417
2418 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2419 mId, __FUNCTION__,
2420 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2421 // Call listener, if any
2422 if (listener != NULL) {
2423 listener->notifyShutter(r.resultExtras, msg.timestamp);
2424 }
2425
2426 r.shutterTimestamp = msg.timestamp;
2427
2428 // send pending result and buffers
2429 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002430 r.partialResult.collectedResult, msg.frame_number,
2431 r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002432 returnOutputBuffers(r.pendingOutputBuffers.array(),
2433 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2434 r.pendingOutputBuffers.clear();
2435
2436 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002437 }
2438 }
2439 if (idx < 0) {
2440 SET_ERR("Shutter notification for non-existent frame number %d",
2441 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002442 }
2443}
2444
2445
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002446CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002447 ALOGV("%s", __FUNCTION__);
2448
Igor Murashkin1e479c02013-09-06 16:55:14 -07002449 CameraMetadata retVal;
2450
2451 if (mRequestThread != NULL) {
2452 retVal = mRequestThread->getLatestRequest();
2453 }
2454
Igor Murashkin1e479c02013-09-06 16:55:14 -07002455 return retVal;
2456}
2457
Jianing Weicb0652e2014-03-12 18:29:36 -07002458
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002459/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002460 * RequestThread inner class methods
2461 */
2462
2463Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002464 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002465 camera3_device_t *hal3Device) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002466 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002467 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002468 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002469 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002470 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002471 mReconfigured(false),
2472 mDoPause(false),
2473 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002474 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002475 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002476 mCurrentAfTriggerId(0),
2477 mCurrentPreCaptureTriggerId(0),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002478 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002479 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002480}
2481
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002482void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002483 NotificationListener *listener) {
2484 Mutex::Autolock l(mRequestLock);
2485 mListener = listener;
2486}
2487
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002488void Camera3Device::RequestThread::configurationComplete() {
2489 Mutex::Autolock l(mRequestLock);
2490 mReconfigured = true;
2491}
2492
Jianing Wei90e59c92014-03-12 18:29:36 -07002493status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002494 List<sp<CaptureRequest> > &requests,
2495 /*out*/
2496 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002497 Mutex::Autolock l(mRequestLock);
2498 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2499 ++it) {
2500 mRequestQueue.push_back(*it);
2501 }
2502
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002503 if (lastFrameNumber != NULL) {
2504 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2505 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2506 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2507 *lastFrameNumber);
2508 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002509
Jianing Wei90e59c92014-03-12 18:29:36 -07002510 unpauseForNewRequests();
2511
2512 return OK;
2513}
2514
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002515
2516status_t Camera3Device::RequestThread::queueTrigger(
2517 RequestTrigger trigger[],
2518 size_t count) {
2519
2520 Mutex::Autolock l(mTriggerMutex);
2521 status_t ret;
2522
2523 for (size_t i = 0; i < count; ++i) {
2524 ret = queueTriggerLocked(trigger[i]);
2525
2526 if (ret != OK) {
2527 return ret;
2528 }
2529 }
2530
2531 return OK;
2532}
2533
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002534int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2535 sp<Camera3Device> d = device.promote();
2536 if (d != NULL) return d->mId;
2537 return 0;
2538}
2539
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002540status_t Camera3Device::RequestThread::queueTriggerLocked(
2541 RequestTrigger trigger) {
2542
2543 uint32_t tag = trigger.metadataTag;
2544 ssize_t index = mTriggerMap.indexOfKey(tag);
2545
2546 switch (trigger.getTagType()) {
2547 case TYPE_BYTE:
2548 // fall-through
2549 case TYPE_INT32:
2550 break;
2551 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002552 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2553 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002554 return INVALID_OPERATION;
2555 }
2556
2557 /**
2558 * Collect only the latest trigger, since we only have 1 field
2559 * in the request settings per trigger tag, and can't send more than 1
2560 * trigger per request.
2561 */
2562 if (index != NAME_NOT_FOUND) {
2563 mTriggerMap.editValueAt(index) = trigger;
2564 } else {
2565 mTriggerMap.add(tag, trigger);
2566 }
2567
2568 return OK;
2569}
2570
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002571status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002572 const RequestList &requests,
2573 /*out*/
2574 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002575 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002576 if (lastFrameNumber != NULL) {
2577 *lastFrameNumber = mRepeatingLastFrameNumber;
2578 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002579 mRepeatingRequests.clear();
2580 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2581 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002582
2583 unpauseForNewRequests();
2584
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002585 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002586 return OK;
2587}
2588
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002589bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2590 if (mRepeatingRequests.empty()) {
2591 return false;
2592 }
2593 int32_t requestId = requestIn->mResultExtras.requestId;
2594 const RequestList &repeatRequests = mRepeatingRequests;
2595 // All repeating requests are guaranteed to have same id so only check first quest
2596 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2597 return (firstRequest->mResultExtras.requestId == requestId);
2598}
2599
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002600status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002601 Mutex::Autolock l(mRequestLock);
2602 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002603 if (lastFrameNumber != NULL) {
2604 *lastFrameNumber = mRepeatingLastFrameNumber;
2605 }
2606 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002607 return OK;
2608}
2609
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002610status_t Camera3Device::RequestThread::clear(
2611 NotificationListener *listener,
2612 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002613 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002614 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002615
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002616 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002617
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002618 // Send errors for all requests pending in the request queue, including
2619 // pending repeating requests
2620 if (listener != NULL) {
2621 for (RequestList::iterator it = mRequestQueue.begin();
2622 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002623 // Abort the input buffers for reprocess requests.
2624 if ((*it)->mInputStream != NULL) {
2625 camera3_stream_buffer_t inputBuffer;
2626 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2627 if (res != OK) {
2628 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2629 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2630 } else {
2631 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2632 if (res != OK) {
2633 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2634 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2635 }
2636 }
2637 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002638 // Set the frame number this request would have had, if it
2639 // had been submitted; this frame number will not be reused.
2640 // The requestId and burstId fields were set when the request was
2641 // submitted originally (in convertMetadataListToRequestListLocked)
2642 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2643 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2644 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002645 }
2646 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002647 mRequestQueue.clear();
2648 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002649 if (lastFrameNumber != NULL) {
2650 *lastFrameNumber = mRepeatingLastFrameNumber;
2651 }
2652 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002653 return OK;
2654}
2655
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002656void Camera3Device::RequestThread::setPaused(bool paused) {
2657 Mutex::Autolock l(mPauseLock);
2658 mDoPause = paused;
2659 mDoPauseSignal.signal();
2660}
2661
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002662status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2663 int32_t requestId, nsecs_t timeout) {
2664 Mutex::Autolock l(mLatestRequestMutex);
2665 status_t res;
2666 while (mLatestRequestId != requestId) {
2667 nsecs_t startTime = systemTime();
2668
2669 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2670 if (res != OK) return res;
2671
2672 timeout -= (systemTime() - startTime);
2673 }
2674
2675 return OK;
2676}
2677
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002678void Camera3Device::RequestThread::requestExit() {
2679 // Call parent to set up shutdown
2680 Thread::requestExit();
2681 // The exit from any possible waits
2682 mDoPauseSignal.signal();
2683 mRequestSignal.signal();
2684}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002685
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002686bool Camera3Device::RequestThread::threadLoop() {
2687
2688 status_t res;
2689
2690 // Handle paused state.
2691 if (waitIfPaused()) {
2692 return true;
2693 }
2694
2695 // Get work to do
2696
2697 sp<CaptureRequest> nextRequest = waitForNextRequest();
2698 if (nextRequest == NULL) {
2699 return true;
2700 }
2701
2702 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002703 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002704 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002705 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002706
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002707 // Get the request ID, if any
2708 int requestId;
2709 camera_metadata_entry_t requestIdEntry =
2710 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2711 if (requestIdEntry.count > 0) {
2712 requestId = requestIdEntry.data.i32[0];
2713 } else {
2714 ALOGW("%s: Did not have android.request.id set in the request",
2715 __FUNCTION__);
2716 requestId = NAME_NOT_FOUND;
2717 }
2718
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002719 // Insert any queued triggers (before metadata is locked)
2720 int32_t triggerCount;
2721 res = insertTriggers(nextRequest);
2722 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002723 SET_ERR("RequestThread: Unable to insert triggers "
2724 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002725 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002726 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2727 return false;
2728 }
2729 triggerCount = res;
2730
2731 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2732
2733 // If the request is the same as last, or we had triggers last time
2734 if (mPrevRequest != nextRequest || triggersMixedIn) {
2735 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002736 * HAL workaround:
2737 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2738 */
2739 res = addDummyTriggerIds(nextRequest);
2740 if (res != OK) {
2741 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2742 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002743 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002744 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2745 return false;
2746 }
2747
2748 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002749 * The request should be presorted so accesses in HAL
2750 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2751 */
2752 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002753 request.settings = nextRequest->mSettings.getAndLock();
2754 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002755 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2756
2757 IF_ALOGV() {
2758 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2759 find_camera_metadata_ro_entry(
2760 request.settings,
2761 ANDROID_CONTROL_AF_TRIGGER,
2762 &e
2763 );
2764 if (e.count > 0) {
2765 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2766 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002767 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002768 e.data.u8[0]);
2769 }
2770 }
2771 } else {
2772 // leave request.settings NULL to indicate 'reuse latest given'
2773 ALOGVV("%s: Request settings are REUSED",
2774 __FUNCTION__);
2775 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002776
Zhijun Hef0d962a2014-06-30 10:24:11 -07002777 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002778
2779 // Fill in buffers
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002780 if (nextRequest->mInputStream != NULL) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002781 request.input_buffer = &nextRequest->mInputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002782 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002783 } else {
2784 request.input_buffer = NULL;
2785 }
2786
2787 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2788 nextRequest->mOutputStreams.size());
2789 request.output_buffers = outputBuffers.array();
2790 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2791 res = nextRequest->mOutputStreams.editItemAt(i)->
2792 getBuffer(&outputBuffers.editItemAt(i));
2793 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002794 // Can't get output buffer from gralloc queue - this could be due to
2795 // abandoned queue or other consumer misbehavior, so not a fatal
2796 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002797 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2798 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala0ec23d32015-05-28 15:15:54 -07002799 {
2800 Mutex::Autolock l(mRequestLock);
2801 if (mListener != NULL) {
2802 mListener->notifyError(
2803 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2804 nextRequest->mResultExtras);
2805 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002806 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002807 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2808 return true;
2809 }
2810 request.num_output_buffers++;
2811 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002812 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002813
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002814 // Log request in the in-flight queue
2815 sp<Camera3Device> parent = mParent.promote();
2816 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002817 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002818 CLOGE("RequestThread: Parent is gone");
2819 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2820 return false;
2821 }
2822
Jianing Weicb0652e2014-03-12 18:29:36 -07002823 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002824 totalNumBuffers, nextRequest->mResultExtras,
2825 /*hasInput*/request.input_buffer != NULL);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002826 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2827 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002828 __FUNCTION__,
2829 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2830 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002831 if (res != OK) {
2832 SET_ERR("RequestThread: Unable to register new in-flight request:"
2833 " %s (%d)", strerror(-res), res);
2834 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2835 return false;
2836 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002837
Zhijun Hecc27e112013-10-03 16:12:43 -07002838 // Inform waitUntilRequestProcessed thread of a new request ID
2839 {
2840 Mutex::Autolock al(mLatestRequestMutex);
2841
2842 mLatestRequestId = requestId;
2843 mLatestRequestSignal.signal();
2844 }
2845
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002846 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002847 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2848 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002849 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002850 ATRACE_END();
2851
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002852 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002853 // Should only get a failure here for malformed requests or device-level
2854 // errors, so consider all errors fatal. Bad metadata failures should
2855 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002856 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002857 " device: %s (%d)", request.frame_number, strerror(-res), res);
2858 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2859 return false;
2860 }
2861
Igor Murashkin1e479c02013-09-06 16:55:14 -07002862 // Update the latest request sent to HAL
2863 if (request.settings != NULL) { // Don't update them if they were unchanged
2864 Mutex::Autolock al(mLatestRequestMutex);
2865
2866 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2867 mLatestRequest.acquire(cloned);
2868 }
2869
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002870 if (request.settings != NULL) {
2871 nextRequest->mSettings.unlock(request.settings);
2872 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002873
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002874 // Unset as current request
2875 {
2876 Mutex::Autolock l(mRequestLock);
2877 mNextRequest.clear();
2878 }
2879
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002880 // Remove any previously queued triggers (after unlock)
2881 res = removeTriggers(mPrevRequest);
2882 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002883 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002884 "(capture request %d, HAL device: %s (%d)",
2885 request.frame_number, strerror(-res), res);
2886 return false;
2887 }
2888 mPrevTriggers = triggerCount;
2889
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002890 return true;
2891}
2892
Igor Murashkin1e479c02013-09-06 16:55:14 -07002893CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2894 Mutex::Autolock al(mLatestRequestMutex);
2895
2896 ALOGV("RequestThread::%s", __FUNCTION__);
2897
2898 return mLatestRequest;
2899}
2900
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002901bool Camera3Device::RequestThread::isStreamPending(
2902 sp<Camera3StreamInterface>& stream) {
2903 Mutex::Autolock l(mRequestLock);
2904
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002905 if (mNextRequest != nullptr) {
2906 for (const auto& s : mNextRequest->mOutputStreams) {
2907 if (stream == s) return true;
2908 }
2909 if (stream == mNextRequest->mInputStream) return true;
2910 }
2911
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002912 for (const auto& request : mRequestQueue) {
2913 for (const auto& s : request->mOutputStreams) {
2914 if (stream == s) return true;
2915 }
2916 if (stream == request->mInputStream) return true;
2917 }
2918
2919 for (const auto& request : mRepeatingRequests) {
2920 for (const auto& s : request->mOutputStreams) {
2921 if (stream == s) return true;
2922 }
2923 if (stream == request->mInputStream) return true;
2924 }
2925
2926 return false;
2927}
Jianing Weicb0652e2014-03-12 18:29:36 -07002928
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002929void Camera3Device::RequestThread::cleanUpFailedRequest(
2930 camera3_capture_request_t &request,
2931 sp<CaptureRequest> &nextRequest,
2932 Vector<camera3_stream_buffer_t> &outputBuffers) {
2933
2934 if (request.settings != NULL) {
2935 nextRequest->mSettings.unlock(request.settings);
2936 }
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002937 if (nextRequest->mInputStream != NULL) {
2938 nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
2939 nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002940 }
2941 for (size_t i = 0; i < request.num_output_buffers; i++) {
2942 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2943 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2944 outputBuffers[i], 0);
2945 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002946
2947 Mutex::Autolock l(mRequestLock);
2948 mNextRequest.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002949}
2950
2951sp<Camera3Device::CaptureRequest>
2952 Camera3Device::RequestThread::waitForNextRequest() {
2953 status_t res;
2954 sp<CaptureRequest> nextRequest;
2955
2956 // Optimized a bit for the simple steady-state case (single repeating
2957 // request), to avoid putting that request in the queue temporarily.
2958 Mutex::Autolock l(mRequestLock);
2959
2960 while (mRequestQueue.empty()) {
2961 if (!mRepeatingRequests.empty()) {
2962 // Always atomically enqueue all requests in a repeating request
2963 // list. Guarantees a complete in-sequence set of captures to
2964 // application.
2965 const RequestList &requests = mRepeatingRequests;
2966 RequestList::const_iterator firstRequest =
2967 requests.begin();
2968 nextRequest = *firstRequest;
2969 mRequestQueue.insert(mRequestQueue.end(),
2970 ++firstRequest,
2971 requests.end());
2972 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002973
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002974 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002975
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002976 break;
2977 }
2978
2979 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2980
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002981 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2982 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002983 Mutex::Autolock pl(mPauseLock);
2984 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002985 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002986 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002987 // Let the tracker know
2988 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2989 if (statusTracker != 0) {
2990 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2991 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002992 }
2993 // Stop waiting for now and let thread management happen
2994 return NULL;
2995 }
2996 }
2997
2998 if (nextRequest == NULL) {
2999 // Don't have a repeating request already in hand, so queue
3000 // must have an entry now.
3001 RequestList::iterator firstRequest =
3002 mRequestQueue.begin();
3003 nextRequest = *firstRequest;
3004 mRequestQueue.erase(firstRequest);
3005 }
3006
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003007 // In case we've been unpaused by setPaused clearing mDoPause, need to
3008 // update internal pause state (capture/setRepeatingRequest unpause
3009 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003010 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003011 if (mPaused) {
3012 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3013 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3014 if (statusTracker != 0) {
3015 statusTracker->markComponentActive(mStatusId);
3016 }
3017 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003018 mPaused = false;
3019
3020 // Check if we've reconfigured since last time, and reset the preview
3021 // request if so. Can't use 'NULL request == repeat' across configure calls.
3022 if (mReconfigured) {
3023 mPrevRequest.clear();
3024 mReconfigured = false;
3025 }
3026
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003027 if (nextRequest != NULL) {
3028 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003029 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3030 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003031
3032 // Since RequestThread::clear() removes buffers from the input stream,
3033 // get the right buffer here before unlocking mRequestLock
3034 if (nextRequest->mInputStream != NULL) {
3035 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3036 if (res != OK) {
3037 // Can't get input buffer from gralloc queue - this could be due to
3038 // disconnected queue or other producer misbehavior, so not a fatal
3039 // error
3040 ALOGE("%s: Can't get input buffer, skipping request:"
3041 " %s (%d)", __FUNCTION__, strerror(-res), res);
3042 if (mListener != NULL) {
3043 mListener->notifyError(
3044 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3045 nextRequest->mResultExtras);
3046 }
3047 return NULL;
3048 }
3049 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003050 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003051 mNextRequest = nextRequest;
3052
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003053 return nextRequest;
3054}
3055
3056bool Camera3Device::RequestThread::waitIfPaused() {
3057 status_t res;
3058 Mutex::Autolock l(mPauseLock);
3059 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003060 if (mPaused == false) {
3061 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003062 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3063 // Let the tracker know
3064 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3065 if (statusTracker != 0) {
3066 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3067 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003068 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003069
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003070 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003071 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003072 return true;
3073 }
3074 }
3075 // We don't set mPaused to false here, because waitForNextRequest needs
3076 // to further manage the paused state in case of starvation.
3077 return false;
3078}
3079
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003080void Camera3Device::RequestThread::unpauseForNewRequests() {
3081 // With work to do, mark thread as unpaused.
3082 // If paused by request (setPaused), don't resume, to avoid
3083 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003084 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003085 Mutex::Autolock p(mPauseLock);
3086 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003087 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3088 if (mPaused) {
3089 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3090 if (statusTracker != 0) {
3091 statusTracker->markComponentActive(mStatusId);
3092 }
3093 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003094 mPaused = false;
3095 }
3096}
3097
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003098void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3099 sp<Camera3Device> parent = mParent.promote();
3100 if (parent != NULL) {
3101 va_list args;
3102 va_start(args, fmt);
3103
3104 parent->setErrorStateV(fmt, args);
3105
3106 va_end(args);
3107 }
3108}
3109
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003110status_t Camera3Device::RequestThread::insertTriggers(
3111 const sp<CaptureRequest> &request) {
3112
3113 Mutex::Autolock al(mTriggerMutex);
3114
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003115 sp<Camera3Device> parent = mParent.promote();
3116 if (parent == NULL) {
3117 CLOGE("RequestThread: Parent is gone");
3118 return DEAD_OBJECT;
3119 }
3120
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003121 CameraMetadata &metadata = request->mSettings;
3122 size_t count = mTriggerMap.size();
3123
3124 for (size_t i = 0; i < count; ++i) {
3125 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003126 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003127
3128 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3129 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3130 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003131 if (isAeTrigger) {
3132 request->mResultExtras.precaptureTriggerId = triggerId;
3133 mCurrentPreCaptureTriggerId = triggerId;
3134 } else {
3135 request->mResultExtras.afTriggerId = triggerId;
3136 mCurrentAfTriggerId = triggerId;
3137 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003138 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3139 continue; // Trigger ID tag is deprecated since device HAL 3.2
3140 }
3141 }
3142
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003143 camera_metadata_entry entry = metadata.find(tag);
3144
3145 if (entry.count > 0) {
3146 /**
3147 * Already has an entry for this trigger in the request.
3148 * Rewrite it with our requested trigger value.
3149 */
3150 RequestTrigger oldTrigger = trigger;
3151
3152 oldTrigger.entryValue = entry.data.u8[0];
3153
3154 mTriggerReplacedMap.add(tag, oldTrigger);
3155 } else {
3156 /**
3157 * More typical, no trigger entry, so we just add it
3158 */
3159 mTriggerRemovedMap.add(tag, trigger);
3160 }
3161
3162 status_t res;
3163
3164 switch (trigger.getTagType()) {
3165 case TYPE_BYTE: {
3166 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3167 res = metadata.update(tag,
3168 &entryValue,
3169 /*count*/1);
3170 break;
3171 }
3172 case TYPE_INT32:
3173 res = metadata.update(tag,
3174 &trigger.entryValue,
3175 /*count*/1);
3176 break;
3177 default:
3178 ALOGE("%s: Type not supported: 0x%x",
3179 __FUNCTION__,
3180 trigger.getTagType());
3181 return INVALID_OPERATION;
3182 }
3183
3184 if (res != OK) {
3185 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3186 ", value %d", __FUNCTION__, trigger.getTagName(),
3187 trigger.entryValue);
3188 return res;
3189 }
3190
3191 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3192 trigger.getTagName(),
3193 trigger.entryValue);
3194 }
3195
3196 mTriggerMap.clear();
3197
3198 return count;
3199}
3200
3201status_t Camera3Device::RequestThread::removeTriggers(
3202 const sp<CaptureRequest> &request) {
3203 Mutex::Autolock al(mTriggerMutex);
3204
3205 CameraMetadata &metadata = request->mSettings;
3206
3207 /**
3208 * Replace all old entries with their old values.
3209 */
3210 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3211 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3212
3213 status_t res;
3214
3215 uint32_t tag = trigger.metadataTag;
3216 switch (trigger.getTagType()) {
3217 case TYPE_BYTE: {
3218 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3219 res = metadata.update(tag,
3220 &entryValue,
3221 /*count*/1);
3222 break;
3223 }
3224 case TYPE_INT32:
3225 res = metadata.update(tag,
3226 &trigger.entryValue,
3227 /*count*/1);
3228 break;
3229 default:
3230 ALOGE("%s: Type not supported: 0x%x",
3231 __FUNCTION__,
3232 trigger.getTagType());
3233 return INVALID_OPERATION;
3234 }
3235
3236 if (res != OK) {
3237 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3238 ", trigger value %d", __FUNCTION__,
3239 trigger.getTagName(), trigger.entryValue);
3240 return res;
3241 }
3242 }
3243 mTriggerReplacedMap.clear();
3244
3245 /**
3246 * Remove all new entries.
3247 */
3248 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3249 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3250 status_t res = metadata.erase(trigger.metadataTag);
3251
3252 if (res != OK) {
3253 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3254 ", trigger value %d", __FUNCTION__,
3255 trigger.getTagName(), trigger.entryValue);
3256 return res;
3257 }
3258 }
3259 mTriggerRemovedMap.clear();
3260
3261 return OK;
3262}
3263
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003264status_t Camera3Device::RequestThread::addDummyTriggerIds(
3265 const sp<CaptureRequest> &request) {
3266 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3267 static const int32_t dummyTriggerId = 1;
3268 status_t res;
3269
3270 CameraMetadata &metadata = request->mSettings;
3271
3272 // If AF trigger is active, insert a dummy AF trigger ID if none already
3273 // exists
3274 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3275 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3276 if (afTrigger.count > 0 &&
3277 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3278 afId.count == 0) {
3279 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3280 if (res != OK) return res;
3281 }
3282
3283 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3284 // if none already exists
3285 camera_metadata_entry pcTrigger =
3286 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3287 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3288 if (pcTrigger.count > 0 &&
3289 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3290 pcId.count == 0) {
3291 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3292 &dummyTriggerId, 1);
3293 if (res != OK) return res;
3294 }
3295
3296 return OK;
3297}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003298
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003299/**
3300 * PreparerThread inner class methods
3301 */
3302
3303Camera3Device::PreparerThread::PreparerThread() :
3304 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3305}
3306
3307Camera3Device::PreparerThread::~PreparerThread() {
3308 Thread::requestExitAndWait();
3309 if (mCurrentStream != nullptr) {
3310 mCurrentStream->cancelPrepare();
3311 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3312 mCurrentStream.clear();
3313 }
3314 clear();
3315}
3316
3317status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) {
3318 status_t res;
3319
3320 Mutex::Autolock l(mLock);
3321
3322 res = stream->startPrepare();
3323 if (res == OK) {
3324 // No preparation needed, fire listener right off
3325 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3326 if (mListener) {
3327 mListener->notifyPrepared(stream->getId());
3328 }
3329 return OK;
3330 } else if (res != NOT_ENOUGH_DATA) {
3331 return res;
3332 }
3333
3334 // Need to prepare, start up thread if necessary
3335 if (!mActive) {
3336 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3337 // isn't running
3338 Thread::requestExitAndWait();
3339 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3340 if (res != OK) {
3341 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3342 if (mListener) {
3343 mListener->notifyPrepared(stream->getId());
3344 }
3345 return res;
3346 }
3347 mCancelNow = false;
3348 mActive = true;
3349 ALOGV("%s: Preparer stream started", __FUNCTION__);
3350 }
3351
3352 // queue up the work
3353 mPendingStreams.push_back(stream);
3354 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3355
3356 return OK;
3357}
3358
3359status_t Camera3Device::PreparerThread::clear() {
3360 status_t res;
3361
3362 Mutex::Autolock l(mLock);
3363
3364 for (const auto& stream : mPendingStreams) {
3365 stream->cancelPrepare();
3366 }
3367 mPendingStreams.clear();
3368 mCancelNow = true;
3369
3370 return OK;
3371}
3372
3373void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3374 Mutex::Autolock l(mLock);
3375 mListener = listener;
3376}
3377
3378bool Camera3Device::PreparerThread::threadLoop() {
3379 status_t res;
3380 {
3381 Mutex::Autolock l(mLock);
3382 if (mCurrentStream == nullptr) {
3383 // End thread if done with work
3384 if (mPendingStreams.empty()) {
3385 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3386 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3387 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3388 mActive = false;
3389 return false;
3390 }
3391
3392 // Get next stream to prepare
3393 auto it = mPendingStreams.begin();
3394 mCurrentStream = *it;
3395 mPendingStreams.erase(it);
3396 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3397 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3398 } else if (mCancelNow) {
3399 mCurrentStream->cancelPrepare();
3400 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3401 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3402 mCurrentStream.clear();
3403 mCancelNow = false;
3404 return true;
3405 }
3406 }
3407
3408 res = mCurrentStream->prepareNextBuffer();
3409 if (res == NOT_ENOUGH_DATA) return true;
3410 if (res != OK) {
3411 // Something bad happened; try to recover by cancelling prepare and
3412 // signalling listener anyway
3413 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3414 mCurrentStream->getId(), res, strerror(-res));
3415 mCurrentStream->cancelPrepare();
3416 }
3417
3418 // This stream has finished, notify listener
3419 Mutex::Autolock l(mLock);
3420 if (mListener) {
3421 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3422 mCurrentStream->getId());
3423 mListener->notifyPrepared(mCurrentStream->getId());
3424 }
3425
3426 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3427 mCurrentStream.clear();
3428
3429 return true;
3430}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003431
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003432/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003433 * Static callback forwarding methods from HAL to instance
3434 */
3435
3436void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3437 const camera3_capture_result *result) {
3438 Camera3Device *d =
3439 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3440 d->processCaptureResult(result);
3441}
3442
3443void Camera3Device::sNotify(const camera3_callback_ops *cb,
3444 const camera3_notify_msg *msg) {
3445 Camera3Device *d =
3446 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3447 d->notify(msg);
3448}
3449
3450}; // namespace android