blob: 4b55dadc31ba08ac2feda19c12be45594b5f4d7d [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Igor Murashkinff3e31d2013-10-23 16:40:06 -070046#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070047#include "device3/Camera3Device.h"
48#include "device3/Camera3OutputStream.h"
49#include "device3/Camera3InputStream.h"
50#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070051#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070052#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080053
54using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080055
56namespace android {
57
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058Camera3Device::Camera3Device(int id):
59 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070060 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080061 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070062 mStatus(STATUS_UNINITIALIZED),
Zhijun He204e3292014-07-14 17:09:23 -070063 mUsePartialResult(false),
64 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070065 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070066 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070067 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070068 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080069{
70 ATRACE_CALL();
71 camera3_callback_ops::notify = &sNotify;
72 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
73 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
74}
75
76Camera3Device::~Camera3Device()
77{
78 ATRACE_CALL();
79 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
80 disconnect();
81}
82
Igor Murashkin71381052013-03-04 14:53:08 -080083int Camera3Device::getId() const {
84 return mId;
85}
86
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080087/**
88 * CameraDeviceBase interface
89 */
90
Yin-Chia Yehe074a932015-01-30 10:29:02 -080091status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080092{
93 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070094 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080095 Mutex::Autolock l(mLock);
96
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080097 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080098 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070099 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800100 return INVALID_OPERATION;
101 }
102
103 /** Open HAL device */
104
105 status_t res;
106 String8 deviceName = String8::format("%d", mId);
107
108 camera3_device_t *device;
109
Zhijun He213ce792013-11-19 08:45:15 -0800110 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800111 res = module->open(deviceName.string(),
112 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800113 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800114
115 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700116 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117 return res;
118 }
119
120 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700121 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700123 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700124 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800125 device->common.version);
126 device->common.close(&device->common);
127 return BAD_VALUE;
128 }
129
130 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800131 res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700132 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800133 if (res != OK) return res;
134
135 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700136 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
137 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700138 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 device->common.close(&device->common);
140 return BAD_VALUE;
141 }
142
143 /** Initialize device with callback functions */
144
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700145 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800146 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700147 ATRACE_END();
148
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800149 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700150 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
151 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800152 device->common.close(&device->common);
153 return BAD_VALUE;
154 }
155
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700156 /** Start up status tracker thread */
157 mStatusTracker = new StatusTracker(this);
158 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
159 if (res != OK) {
160 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
161 strerror(-res), res);
162 device->common.close(&device->common);
163 mStatusTracker.clear();
164 return res;
165 }
166
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800167 /** Start up request queue thread */
168
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700169 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800170 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800171 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700172 SET_ERR_L("Unable to start request queue thread: %s (%d)",
173 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800174 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800175 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800176 return res;
177 }
178
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700179 mPreparerThread = new PreparerThread();
180
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800181 /** Everything is good to go */
182
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700183 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800184 mDeviceInfo = info.static_camera_characteristics;
185 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700186 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800187 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700188 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700189 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700190 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800191
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700192 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700193 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
194 camera_metadata_entry partialResultsCount =
195 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
196 if (partialResultsCount.count > 0) {
197 mNumPartialResults = partialResultsCount.data.i32[0];
198 mUsePartialResult = (mNumPartialResults > 1);
199 }
200 } else {
201 camera_metadata_entry partialResultsQuirk =
202 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
203 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
204 mUsePartialResult = true;
205 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700206 }
207
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700208 camera_metadata_entry configs =
209 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
210 for (uint32_t i = 0; i < configs.count; i += 4) {
211 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
212 configs.data.i32[i + 3] ==
213 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
214 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
215 configs.data.i32[i + 2]));
216 }
217 }
218
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800219 return OK;
220}
221
222status_t Camera3Device::disconnect() {
223 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700224 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800225
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800226 ALOGV("%s: E", __FUNCTION__);
227
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700228 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800229
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700230 {
231 Mutex::Autolock l(mLock);
232 if (mStatus == STATUS_UNINITIALIZED) return res;
233
234 if (mStatus == STATUS_ACTIVE ||
235 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
236 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700237 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700238 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700239 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700240 } else {
241 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
242 if (res != OK) {
243 SET_ERR_L("Timeout waiting for HAL to drain");
244 // Continue to close device even in case of error
245 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700246 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800247 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800248
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700249 if (mStatus == STATUS_ERROR) {
250 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700251 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700252
253 if (mStatusTracker != NULL) {
254 mStatusTracker->requestExit();
255 }
256
257 if (mRequestThread != NULL) {
258 mRequestThread->requestExit();
259 }
260
261 mOutputStreams.clear();
262 mInputStream.clear();
263 }
264
265 // Joining done without holding mLock, otherwise deadlocks may ensue
266 // as the threads try to access parent state
267 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
268 // HAL may be in a bad state, so waiting for request thread
269 // (which may be stuck in the HAL processCaptureRequest call)
270 // could be dangerous.
271 mRequestThread->join();
272 }
273
274 if (mStatusTracker != NULL) {
275 mStatusTracker->join();
276 }
277
278 {
279 Mutex::Autolock l(mLock);
280
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800281 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700282 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800283
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700284 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800285 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700286 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800287 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700288 mHal3Device = NULL;
289 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800290
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700291 mStatus = STATUS_UNINITIALIZED;
292 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800293
294 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700295 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800296}
297
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700298// For dumping/debugging only -
299// try to acquire a lock a few times, eventually give up to proceed with
300// debug/dump operations
301bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
302 bool gotLock = false;
303 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
304 if (lock.tryLock() == NO_ERROR) {
305 gotLock = true;
306 break;
307 } else {
308 usleep(kDumpSleepDuration);
309 }
310 }
311 return gotLock;
312}
313
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700314Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
315 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
316 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
317 const int STREAM_CONFIGURATION_SIZE = 4;
318 const int STREAM_FORMAT_OFFSET = 0;
319 const int STREAM_WIDTH_OFFSET = 1;
320 const int STREAM_HEIGHT_OFFSET = 2;
321 const int STREAM_IS_INPUT_OFFSET = 3;
322 camera_metadata_ro_entry_t availableStreamConfigs =
323 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
324 if (availableStreamConfigs.count == 0 ||
325 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
326 return Size(0, 0);
327 }
328
329 // Get max jpeg size (area-wise).
330 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
331 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
332 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
333 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
334 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
335 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
336 && format == HAL_PIXEL_FORMAT_BLOB &&
337 (width * height > maxJpegWidth * maxJpegHeight)) {
338 maxJpegWidth = width;
339 maxJpegHeight = height;
340 }
341 }
342 } else {
343 camera_metadata_ro_entry availableJpegSizes =
344 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
345 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
346 return Size(0, 0);
347 }
348
349 // Get max jpeg size (area-wise).
350 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
351 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
352 > (maxJpegWidth * maxJpegHeight)) {
353 maxJpegWidth = availableJpegSizes.data.i32[i];
354 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
355 }
356 }
357 }
358 return Size(maxJpegWidth, maxJpegHeight);
359}
360
Zhijun Hef7da0962014-04-24 13:27:56 -0700361ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700362 // Get max jpeg size (area-wise).
363 Size maxJpegResolution = getMaxJpegResolution();
364 if (maxJpegResolution.width == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700365 ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
366 __FUNCTION__, mId);
367 return BAD_VALUE;
368 }
369
Zhijun Hef7da0962014-04-24 13:27:56 -0700370 // Get max jpeg buffer size
371 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700372 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
373 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700374 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
375 return BAD_VALUE;
376 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700377 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800378 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700379
380 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700381 float scaleFactor = ((float) (width * height)) /
382 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800383 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
384 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700385 if (jpegBufferSize > maxJpegBufferSize) {
386 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700387 }
388
389 return jpegBufferSize;
390}
391
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800392status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
393 ATRACE_CALL();
394 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700395
396 // Try to lock, but continue in case of failure (to avoid blocking in
397 // deadlocks)
398 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
399 bool gotLock = tryLockSpinRightRound(mLock);
400
401 ALOGW_IF(!gotInterfaceLock,
402 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
403 mId, __FUNCTION__);
404 ALOGW_IF(!gotLock,
405 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
406 mId, __FUNCTION__);
407
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800408 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800409
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800410 const char *status =
411 mStatus == STATUS_ERROR ? "ERROR" :
412 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700413 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
414 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800415 mStatus == STATUS_ACTIVE ? "ACTIVE" :
416 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700417
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800418 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700419 if (mStatus == STATUS_ERROR) {
420 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
421 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800422 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700423 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700424 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800425
426 if (mInputStream != NULL) {
427 write(fd, lines.string(), lines.size());
428 mInputStream->dump(fd, args);
429 } else {
430 lines.appendFormat(" No input stream.\n");
431 write(fd, lines.string(), lines.size());
432 }
433 for (size_t i = 0; i < mOutputStreams.size(); i++) {
434 mOutputStreams[i]->dump(fd,args);
435 }
436
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700437 lines = String8(" In-flight requests:\n");
438 if (mInFlightMap.size() == 0) {
439 lines.append(" None\n");
440 } else {
441 for (size_t i = 0; i < mInFlightMap.size(); i++) {
442 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700443 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700444 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800445 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700446 r.numBuffersLeft);
447 }
448 }
449 write(fd, lines.string(), lines.size());
450
Igor Murashkin1e479c02013-09-06 16:55:14 -0700451 {
452 lines = String8(" Last request sent:\n");
453 write(fd, lines.string(), lines.size());
454
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700455 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700456 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
457 }
458
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800459 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700460 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800461 write(fd, lines.string(), lines.size());
462 mHal3Device->ops->dump(mHal3Device, fd);
463 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800464
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700465 if (gotLock) mLock.unlock();
466 if (gotInterfaceLock) mInterfaceLock.unlock();
467
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800468 return OK;
469}
470
471const CameraMetadata& Camera3Device::info() const {
472 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800473 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
474 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700475 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800476 mStatus == STATUS_ERROR ?
477 "when in error state" : "before init");
478 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800479 return mDeviceInfo;
480}
481
Jianing Wei90e59c92014-03-12 18:29:36 -0700482status_t Camera3Device::checkStatusOkToCaptureLocked() {
483 switch (mStatus) {
484 case STATUS_ERROR:
485 CLOGE("Device has encountered a serious error");
486 return INVALID_OPERATION;
487 case STATUS_UNINITIALIZED:
488 CLOGE("Device not initialized");
489 return INVALID_OPERATION;
490 case STATUS_UNCONFIGURED:
491 case STATUS_CONFIGURED:
492 case STATUS_ACTIVE:
493 // OK
494 break;
495 default:
496 SET_ERR_L("Unexpected status: %d", mStatus);
497 return INVALID_OPERATION;
498 }
499 return OK;
500}
501
502status_t Camera3Device::convertMetadataListToRequestListLocked(
503 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
504 if (requestList == NULL) {
505 CLOGE("requestList cannot be NULL.");
506 return BAD_VALUE;
507 }
508
Jianing Weicb0652e2014-03-12 18:29:36 -0700509 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700510 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
511 it != metadataList.end(); ++it) {
512 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
513 if (newRequest == 0) {
514 CLOGE("Can't create capture request");
515 return BAD_VALUE;
516 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700517
518 // Setup burst Id and request Id
519 newRequest->mResultExtras.burstId = burstId++;
520 if (it->exists(ANDROID_REQUEST_ID)) {
521 if (it->find(ANDROID_REQUEST_ID).count == 0) {
522 CLOGE("RequestID entry exists; but must not be empty in metadata");
523 return BAD_VALUE;
524 }
525 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
526 } else {
527 CLOGE("RequestID does not exist in metadata");
528 return BAD_VALUE;
529 }
530
Jianing Wei90e59c92014-03-12 18:29:36 -0700531 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700532
533 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700534 }
535 return OK;
536}
537
Jianing Weicb0652e2014-03-12 18:29:36 -0700538status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800539 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800540
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700541 List<const CameraMetadata> requests;
542 requests.push_back(request);
543 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800544}
545
Jianing Wei90e59c92014-03-12 18:29:36 -0700546status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700547 const List<const CameraMetadata> &requests, bool repeating,
548 /*out*/
549 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700550 ATRACE_CALL();
551 Mutex::Autolock il(mInterfaceLock);
552 Mutex::Autolock l(mLock);
553
554 status_t res = checkStatusOkToCaptureLocked();
555 if (res != OK) {
556 // error logged by previous call
557 return res;
558 }
559
560 RequestList requestList;
561
562 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
563 if (res != OK) {
564 // error logged by previous call
565 return res;
566 }
567
568 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700569 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700570 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700571 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700572 }
573
574 if (res == OK) {
575 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
576 if (res != OK) {
577 SET_ERR_L("Can't transition to active in %f seconds!",
578 kActiveTimeout/1e9);
579 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700580 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
581 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700582 } else {
583 CLOGE("Cannot queue request. Impossible.");
584 return BAD_VALUE;
585 }
586
587 return res;
588}
589
Jianing Weicb0652e2014-03-12 18:29:36 -0700590status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
591 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700592 ATRACE_CALL();
593
Jianing Weicb0652e2014-03-12 18:29:36 -0700594 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700595}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800596
Jianing Weicb0652e2014-03-12 18:29:36 -0700597status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
598 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800599 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800600
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700601 List<const CameraMetadata> requests;
602 requests.push_back(request);
603 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800604}
605
Jianing Weicb0652e2014-03-12 18:29:36 -0700606status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
607 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700608 ATRACE_CALL();
609
Jianing Weicb0652e2014-03-12 18:29:36 -0700610 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700611}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800612
613sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
614 const CameraMetadata &request) {
615 status_t res;
616
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700617 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800618 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700619 // Stream configuration failed due to unsupported configuration.
620 // Device back to unconfigured state. Client might try other configuraitons
621 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
622 CLOGE("No streams configured");
623 return NULL;
624 }
625 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800626 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700627 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800628 return NULL;
629 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700630 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700631 if (mStatus == STATUS_UNCONFIGURED) {
632 CLOGE("No streams configured");
633 return NULL;
634 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800635 }
636
637 sp<CaptureRequest> newRequest = createCaptureRequest(request);
638 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800639}
640
Jianing Weicb0652e2014-03-12 18:29:36 -0700641status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800642 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700643 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800644 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800645
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800646 switch (mStatus) {
647 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700648 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800649 return INVALID_OPERATION;
650 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700651 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800652 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700653 case STATUS_UNCONFIGURED:
654 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800655 case STATUS_ACTIVE:
656 // OK
657 break;
658 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700659 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800660 return INVALID_OPERATION;
661 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700662 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700663
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700664 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800665}
666
667status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
668 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700669 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800670
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700671 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800672}
673
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700674status_t Camera3Device::createInputStream(
675 uint32_t width, uint32_t height, int format, int *id) {
676 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700677 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700678 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700679 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
680 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700681
682 status_t res;
683 bool wasActive = false;
684
685 switch (mStatus) {
686 case STATUS_ERROR:
687 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
688 return INVALID_OPERATION;
689 case STATUS_UNINITIALIZED:
690 ALOGE("%s: Device not initialized", __FUNCTION__);
691 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700692 case STATUS_UNCONFIGURED:
693 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700694 // OK
695 break;
696 case STATUS_ACTIVE:
697 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700698 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700699 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700700 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700701 return res;
702 }
703 wasActive = true;
704 break;
705 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700706 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700707 return INVALID_OPERATION;
708 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700709 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700710
711 if (mInputStream != 0) {
712 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
713 return INVALID_OPERATION;
714 }
715
716 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
717 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700718 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700719
720 mInputStream = newStream;
721
722 *id = mNextStreamId++;
723
724 // Continue captures if active at start
725 if (wasActive) {
726 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
727 res = configureStreamsLocked();
728 if (res != OK) {
729 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
730 __FUNCTION__, mNextStreamId, strerror(-res), res);
731 return res;
732 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700733 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700734 }
735
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700736 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700737 return OK;
738}
739
Igor Murashkin2fba5842013-04-22 14:03:54 -0700740
741status_t Camera3Device::createZslStream(
742 uint32_t width, uint32_t height,
743 int depth,
744 /*out*/
745 int *id,
746 sp<Camera3ZslStream>* zslStream) {
747 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700748 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700749 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700750 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
751 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700752
753 status_t res;
754 bool wasActive = false;
755
756 switch (mStatus) {
757 case STATUS_ERROR:
758 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
759 return INVALID_OPERATION;
760 case STATUS_UNINITIALIZED:
761 ALOGE("%s: Device not initialized", __FUNCTION__);
762 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700763 case STATUS_UNCONFIGURED:
764 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700765 // OK
766 break;
767 case STATUS_ACTIVE:
768 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700769 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700770 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700771 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700772 return res;
773 }
774 wasActive = true;
775 break;
776 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700777 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700778 return INVALID_OPERATION;
779 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700780 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700781
782 if (mInputStream != 0) {
783 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
784 return INVALID_OPERATION;
785 }
786
787 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
788 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700789 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700790
791 res = mOutputStreams.add(mNextStreamId, newStream);
792 if (res < 0) {
793 ALOGE("%s: Can't add new stream to set: %s (%d)",
794 __FUNCTION__, strerror(-res), res);
795 return res;
796 }
797 mInputStream = newStream;
798
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530799 mNeedConfig = true;
800
Igor Murashkin2fba5842013-04-22 14:03:54 -0700801 *id = mNextStreamId++;
802 *zslStream = newStream;
803
804 // Continue captures if active at start
805 if (wasActive) {
806 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
807 res = configureStreamsLocked();
808 if (res != OK) {
809 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
810 __FUNCTION__, mNextStreamId, strerror(-res), res);
811 return res;
812 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700813 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700814 }
815
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700816 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700817 return OK;
818}
819
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700820status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800821 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700822 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800823 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700824 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800825 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700826 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
827 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800828
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800829 status_t res;
830 bool wasActive = false;
831
832 switch (mStatus) {
833 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700834 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800835 return INVALID_OPERATION;
836 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700837 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800838 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700839 case STATUS_UNCONFIGURED:
840 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800841 // OK
842 break;
843 case STATUS_ACTIVE:
844 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700845 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800846 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700847 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800848 return res;
849 }
850 wasActive = true;
851 break;
852 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700853 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800854 return INVALID_OPERATION;
855 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700856 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800857
858 sp<Camera3OutputStream> newStream;
859 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700860 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700861 if (jpegBufferSize <= 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700862 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
863 return BAD_VALUE;
864 }
865
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800866 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700867 width, height, jpegBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800868 } else {
869 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700870 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800871 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700872 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800873
874 res = mOutputStreams.add(mNextStreamId, newStream);
875 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700876 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800877 return res;
878 }
879
880 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700881 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800882
883 // Continue captures if active at start
884 if (wasActive) {
885 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
886 res = configureStreamsLocked();
887 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700888 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
889 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800890 return res;
891 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700892 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800893 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700894 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800895 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800896}
897
898status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
899 ATRACE_CALL();
900 (void)outputId; (void)id;
901
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700902 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800903 return INVALID_OPERATION;
904}
905
906
907status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700908 uint32_t *width, uint32_t *height,
909 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800910 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700911 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800912 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800913
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800914 switch (mStatus) {
915 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700916 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800917 return INVALID_OPERATION;
918 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700919 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800920 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700921 case STATUS_UNCONFIGURED:
922 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800923 case STATUS_ACTIVE:
924 // OK
925 break;
926 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700927 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800928 return INVALID_OPERATION;
929 }
930
931 ssize_t idx = mOutputStreams.indexOfKey(id);
932 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700933 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800934 return idx;
935 }
936
937 if (width) *width = mOutputStreams[idx]->getWidth();
938 if (height) *height = mOutputStreams[idx]->getHeight();
939 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700940 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800941 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800942}
943
944status_t Camera3Device::setStreamTransform(int id,
945 int transform) {
946 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700947 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800948 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800949
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800950 switch (mStatus) {
951 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700952 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800953 return INVALID_OPERATION;
954 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700955 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800956 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700957 case STATUS_UNCONFIGURED:
958 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800959 case STATUS_ACTIVE:
960 // OK
961 break;
962 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700963 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800964 return INVALID_OPERATION;
965 }
966
967 ssize_t idx = mOutputStreams.indexOfKey(id);
968 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700969 CLOGE("Stream %d does not exist",
970 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800971 return BAD_VALUE;
972 }
973
974 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800975}
976
977status_t Camera3Device::deleteStream(int id) {
978 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700979 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800980 Mutex::Autolock l(mLock);
981 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800982
Igor Murashkine2172be2013-05-28 15:31:39 -0700983 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
984
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800985 // CameraDevice semantics require device to already be idle before
986 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700987 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700988 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
989 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800990 }
991
Igor Murashkin2fba5842013-04-22 14:03:54 -0700992 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800993 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800994 if (mInputStream != NULL && id == mInputStream->getId()) {
995 deletedStream = mInputStream;
996 mInputStream.clear();
997 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800998 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700999 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001000 return BAD_VALUE;
1001 }
Zhijun He5f446352014-01-22 09:49:33 -08001002 }
1003
1004 // Delete output stream or the output part of a bi-directional stream.
1005 if (outputStreamIdx != NAME_NOT_FOUND) {
1006 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001007 mOutputStreams.removeItem(id);
1008 }
1009
1010 // Free up the stream endpoint so that it can be used by some other stream
1011 res = deletedStream->disconnect();
1012 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001013 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001014 // fall through since we want to still list the stream as deleted.
1015 }
1016 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001017 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001018
1019 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001020}
1021
1022status_t Camera3Device::deleteReprocessStream(int id) {
1023 ATRACE_CALL();
1024 (void)id;
1025
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001026 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001027 return INVALID_OPERATION;
1028}
1029
Zhijun He1fa89992015-06-01 15:44:31 -07001030status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001031 ATRACE_CALL();
1032 ALOGV("%s: E", __FUNCTION__);
1033
1034 Mutex::Autolock il(mInterfaceLock);
1035 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001036
1037 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1038 mNeedConfig = true;
1039 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1040 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001041
1042 return configureStreamsLocked();
1043}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001044
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001045status_t Camera3Device::getInputBufferProducer(
1046 sp<IGraphicBufferProducer> *producer) {
1047 Mutex::Autolock il(mInterfaceLock);
1048 Mutex::Autolock l(mLock);
1049
1050 if (producer == NULL) {
1051 return BAD_VALUE;
1052 } else if (mInputStream == NULL) {
1053 return INVALID_OPERATION;
1054 }
1055
1056 return mInputStream->getInputBufferProducer(producer);
1057}
1058
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001059status_t Camera3Device::createDefaultRequest(int templateId,
1060 CameraMetadata *request) {
1061 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001062 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001063 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001064 Mutex::Autolock l(mLock);
1065
1066 switch (mStatus) {
1067 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001068 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 return INVALID_OPERATION;
1070 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001071 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001072 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001073 case STATUS_UNCONFIGURED:
1074 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001075 case STATUS_ACTIVE:
1076 // OK
1077 break;
1078 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001079 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001080 return INVALID_OPERATION;
1081 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001082
Zhijun Hea1530f12014-09-14 12:44:20 -07001083 if (!mRequestTemplateCache[templateId].isEmpty()) {
1084 *request = mRequestTemplateCache[templateId];
1085 return OK;
1086 }
1087
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001088 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001089 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001090 rawRequest = mHal3Device->ops->construct_default_request_settings(
1091 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001092 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001093 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001094 ALOGI("%s: template %d is not supported on this camera device",
1095 __FUNCTION__, templateId);
1096 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001097 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001098 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001099 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001100
1101 return OK;
1102}
1103
1104status_t Camera3Device::waitUntilDrained() {
1105 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001106 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001107 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001108
Zhijun He69a37482014-03-23 18:44:49 -07001109 return waitUntilDrainedLocked();
1110}
1111
1112status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001113 switch (mStatus) {
1114 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001115 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001116 ALOGV("%s: Already idle", __FUNCTION__);
1117 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001118 case STATUS_CONFIGURED:
1119 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001120 case STATUS_ERROR:
1121 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001122 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001123 break;
1124 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001125 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001126 return INVALID_OPERATION;
1127 }
1128
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001129 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1130 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001131 if (res != OK) {
1132 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1133 res);
1134 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001135 return res;
1136}
1137
1138// Pause to reconfigure
1139status_t Camera3Device::internalPauseAndWaitLocked() {
1140 mRequestThread->setPaused(true);
1141 mPauseStateNotify = true;
1142
1143 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1144 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1145 if (res != OK) {
1146 SET_ERR_L("Can't idle device in %f seconds!",
1147 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001148 }
1149
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001150 return res;
1151}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001152
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001153// Resume after internalPauseAndWaitLocked
1154status_t Camera3Device::internalResumeLocked() {
1155 status_t res;
1156
1157 mRequestThread->setPaused(false);
1158
1159 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1160 if (res != OK) {
1161 SET_ERR_L("Can't transition to active in %f seconds!",
1162 kActiveTimeout/1e9);
1163 }
1164 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001165 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001166}
1167
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001168status_t Camera3Device::waitUntilStateThenRelock(bool active,
1169 nsecs_t timeout) {
1170 status_t res = OK;
1171 if (active == (mStatus == STATUS_ACTIVE)) {
1172 // Desired state already reached
1173 return res;
1174 }
1175
1176 bool stateSeen = false;
1177 do {
1178 mRecentStatusUpdates.clear();
1179
1180 res = mStatusChanged.waitRelative(mLock, timeout);
1181 if (res != OK) break;
1182
1183 // Check state change history during wait
1184 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1185 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1186 stateSeen = true;
1187 break;
1188 }
1189 }
1190 } while (!stateSeen);
1191
1192 return res;
1193}
1194
1195
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001196status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1197 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001198 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001199
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001200 if (listener != NULL && mListener != NULL) {
1201 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1202 }
1203 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001204 mRequestThread->setNotificationListener(listener);
1205 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001206
1207 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001208}
1209
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001210bool Camera3Device::willNotify3A() {
1211 return false;
1212}
1213
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001214status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001215 status_t res;
1216 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001217
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001218 while (mResultQueue.empty()) {
1219 res = mResultSignal.waitRelative(mOutputLock, timeout);
1220 if (res == TIMED_OUT) {
1221 return res;
1222 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001223 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001224 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001225 return res;
1226 }
1227 }
1228 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001229}
1230
Jianing Weicb0652e2014-03-12 18:29:36 -07001231status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001232 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001233 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001234
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001235 if (mResultQueue.empty()) {
1236 return NOT_ENOUGH_DATA;
1237 }
1238
Jianing Weicb0652e2014-03-12 18:29:36 -07001239 if (frame == NULL) {
1240 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1241 return BAD_VALUE;
1242 }
1243
1244 CaptureResult &result = *(mResultQueue.begin());
1245 frame->mResultExtras = result.mResultExtras;
1246 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001247 mResultQueue.erase(mResultQueue.begin());
1248
1249 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001250}
1251
1252status_t Camera3Device::triggerAutofocus(uint32_t id) {
1253 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001254 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001255
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001256 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1257 // Mix-in this trigger into the next request and only the next request.
1258 RequestTrigger trigger[] = {
1259 {
1260 ANDROID_CONTROL_AF_TRIGGER,
1261 ANDROID_CONTROL_AF_TRIGGER_START
1262 },
1263 {
1264 ANDROID_CONTROL_AF_TRIGGER_ID,
1265 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001266 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001267 };
1268
1269 return mRequestThread->queueTrigger(trigger,
1270 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001271}
1272
1273status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1274 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001275 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001276
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001277 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1278 // Mix-in this trigger into the next request and only the next request.
1279 RequestTrigger trigger[] = {
1280 {
1281 ANDROID_CONTROL_AF_TRIGGER,
1282 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1283 },
1284 {
1285 ANDROID_CONTROL_AF_TRIGGER_ID,
1286 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001287 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001288 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001289
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001290 return mRequestThread->queueTrigger(trigger,
1291 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001292}
1293
1294status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1295 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001296 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001297
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001298 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1299 // Mix-in this trigger into the next request and only the next request.
1300 RequestTrigger trigger[] = {
1301 {
1302 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1303 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1304 },
1305 {
1306 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1307 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001308 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001309 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001310
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001311 return mRequestThread->queueTrigger(trigger,
1312 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001313}
1314
1315status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1316 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1317 ATRACE_CALL();
1318 (void)reprocessStreamId; (void)buffer; (void)listener;
1319
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001320 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001321 return INVALID_OPERATION;
1322}
1323
Jianing Weicb0652e2014-03-12 18:29:36 -07001324status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001325 ATRACE_CALL();
1326 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001327 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001328
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001329 NotificationListener* listener;
1330 {
1331 Mutex::Autolock l(mOutputLock);
1332 listener = mListener;
1333 }
1334
Zhijun He7ef20392014-04-21 16:04:17 -07001335 {
1336 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001337 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001338 }
1339
Zhijun He491e3412013-12-27 10:57:44 -08001340 status_t res;
1341 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1342 res = mHal3Device->ops->flush(mHal3Device);
1343 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001344 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001345 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001346 }
1347
1348 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001349}
1350
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001351status_t Camera3Device::prepare(int streamId) {
1352 ATRACE_CALL();
1353 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001354 Mutex::Autolock il(mInterfaceLock);
1355 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001356
1357 sp<Camera3StreamInterface> stream;
1358 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1359 if (outputStreamIdx == NAME_NOT_FOUND) {
1360 CLOGE("Stream %d does not exist", streamId);
1361 return BAD_VALUE;
1362 }
1363
1364 stream = mOutputStreams.editValueAt(outputStreamIdx);
1365
1366 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001367 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001368 return BAD_VALUE;
1369 }
1370
1371 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001372 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001373 return BAD_VALUE;
1374 }
1375
1376 return mPreparerThread->prepare(stream);
1377}
1378
Zhijun He204e3292014-07-14 17:09:23 -07001379uint32_t Camera3Device::getDeviceVersion() {
1380 ATRACE_CALL();
1381 Mutex::Autolock il(mInterfaceLock);
1382 return mDeviceVersion;
1383}
1384
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001385/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001386 * Methods called by subclasses
1387 */
1388
1389void Camera3Device::notifyStatus(bool idle) {
1390 {
1391 // Need mLock to safely update state and synchronize to current
1392 // state of methods in flight.
1393 Mutex::Autolock l(mLock);
1394 // We can get various system-idle notices from the status tracker
1395 // while starting up. Only care about them if we've actually sent
1396 // in some requests recently.
1397 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1398 return;
1399 }
1400 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1401 idle ? "idle" : "active");
1402 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1403 mRecentStatusUpdates.add(mStatus);
1404 mStatusChanged.signal();
1405
1406 // Skip notifying listener if we're doing some user-transparent
1407 // state changes
1408 if (mPauseStateNotify) return;
1409 }
1410 NotificationListener *listener;
1411 {
1412 Mutex::Autolock l(mOutputLock);
1413 listener = mListener;
1414 }
1415 if (idle && listener != NULL) {
1416 listener->notifyIdle();
1417 }
1418}
1419
1420/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001421 * Camera3Device private methods
1422 */
1423
1424sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1425 const CameraMetadata &request) {
1426 ATRACE_CALL();
1427 status_t res;
1428
1429 sp<CaptureRequest> newRequest = new CaptureRequest;
1430 newRequest->mSettings = request;
1431
1432 camera_metadata_entry_t inputStreams =
1433 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1434 if (inputStreams.count > 0) {
1435 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001436 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001437 CLOGE("Request references unknown input stream %d",
1438 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001439 return NULL;
1440 }
1441 // Lazy completion of stream configuration (allocation/registration)
1442 // on first use
1443 if (mInputStream->isConfiguring()) {
1444 res = mInputStream->finishConfiguration(mHal3Device);
1445 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001446 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001447 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001448 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001449 return NULL;
1450 }
1451 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001452 // Check if stream is being prepared
1453 if (mInputStream->isPreparing()) {
1454 CLOGE("Request references an input stream that's being prepared!");
1455 return NULL;
1456 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001457
1458 newRequest->mInputStream = mInputStream;
1459 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1460 }
1461
1462 camera_metadata_entry_t streams =
1463 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1464 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001465 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001466 return NULL;
1467 }
1468
1469 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001470 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001471 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001472 CLOGE("Request references unknown stream %d",
1473 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001474 return NULL;
1475 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001476 sp<Camera3OutputStreamInterface> stream =
1477 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001478
1479 // Lazy completion of stream configuration (allocation/registration)
1480 // on first use
1481 if (stream->isConfiguring()) {
1482 res = stream->finishConfiguration(mHal3Device);
1483 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001484 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1485 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001486 return NULL;
1487 }
1488 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001489 // Check if stream is being prepared
1490 if (stream->isPreparing()) {
1491 CLOGE("Request references an output stream that's being prepared!");
1492 return NULL;
1493 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001494
1495 newRequest->mOutputStreams.push(stream);
1496 }
1497 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1498
1499 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001500}
1501
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001502bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1503 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1504 Size size = mSupportedOpaqueInputSizes[i];
1505 if (size.width == width && size.height == height) {
1506 return true;
1507 }
1508 }
1509
1510 return false;
1511}
1512
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001513status_t Camera3Device::configureStreamsLocked() {
1514 ATRACE_CALL();
1515 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001516
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001517 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001518 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001519 return INVALID_OPERATION;
1520 }
1521
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001522 if (!mNeedConfig) {
1523 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1524 return OK;
1525 }
1526
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001527 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1528 // adding a dummy stream instead.
1529 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1530 if (mOutputStreams.size() == 0) {
1531 addDummyStreamLocked();
1532 } else {
1533 tryRemoveDummyStreamLocked();
1534 }
1535
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001536 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001537 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001538
1539 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001540 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1541 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1542 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001543 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1544
1545 Vector<camera3_stream_t*> streams;
1546 streams.setCapacity(config.num_streams);
1547
1548 if (mInputStream != NULL) {
1549 camera3_stream_t *inputStream;
1550 inputStream = mInputStream->startConfiguration();
1551 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001552 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001553 return INVALID_OPERATION;
1554 }
1555 streams.add(inputStream);
1556 }
1557
1558 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001559
1560 // Don't configure bidi streams twice, nor add them twice to the list
1561 if (mOutputStreams[i].get() ==
1562 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1563
1564 config.num_streams--;
1565 continue;
1566 }
1567
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001568 camera3_stream_t *outputStream;
1569 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1570 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001571 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001572 return INVALID_OPERATION;
1573 }
1574 streams.add(outputStream);
1575 }
1576
1577 config.streams = streams.editArray();
1578
1579 // Do the HAL configuration; will potentially touch stream
1580 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001581 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001582 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001583 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001584
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001585 if (res == BAD_VALUE) {
1586 // HAL rejected this set of streams as unsupported, clean up config
1587 // attempt and return to unconfigured state
1588 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1589 res = mInputStream->cancelConfiguration();
1590 if (res != OK) {
1591 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1592 mInputStream->getId(), strerror(-res), res);
1593 return res;
1594 }
1595 }
1596
1597 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1598 sp<Camera3OutputStreamInterface> outputStream =
1599 mOutputStreams.editValueAt(i);
1600 if (outputStream->isConfiguring()) {
1601 res = outputStream->cancelConfiguration();
1602 if (res != OK) {
1603 SET_ERR_L(
1604 "Can't cancel configuring output stream %d: %s (%d)",
1605 outputStream->getId(), strerror(-res), res);
1606 return res;
1607 }
1608 }
1609 }
1610
1611 // Return state to that at start of call, so that future configures
1612 // properly clean things up
1613 mStatus = STATUS_UNCONFIGURED;
1614 mNeedConfig = true;
1615
1616 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1617 return BAD_VALUE;
1618 } else if (res != OK) {
1619 // Some other kind of error from configure_streams - this is not
1620 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001621 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1622 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001623 return res;
1624 }
1625
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001626 // Finish all stream configuration immediately.
1627 // TODO: Try to relax this later back to lazy completion, which should be
1628 // faster
1629
Igor Murashkin073f8572013-05-02 14:59:28 -07001630 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001631 res = mInputStream->finishConfiguration(mHal3Device);
1632 if (res != OK) {
1633 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1634 mInputStream->getId(), strerror(-res), res);
1635 return res;
1636 }
1637 }
1638
1639 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001640 sp<Camera3OutputStreamInterface> outputStream =
1641 mOutputStreams.editValueAt(i);
1642 if (outputStream->isConfiguring()) {
1643 res = outputStream->finishConfiguration(mHal3Device);
1644 if (res != OK) {
1645 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1646 outputStream->getId(), strerror(-res), res);
1647 return res;
1648 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001649 }
1650 }
1651
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001652 // Request thread needs to know to avoid using repeat-last-settings protocol
1653 // across configure_streams() calls
1654 mRequestThread->configurationComplete();
1655
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001656 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001657
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001658 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001659
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001660 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001661 mStatus = STATUS_CONFIGURED;
1662 } else {
1663 mStatus = STATUS_UNCONFIGURED;
1664 }
1665
1666 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1667
Zhijun He0a210512014-07-24 13:45:15 -07001668 // tear down the deleted streams after configure streams.
1669 mDeletedStreams.clear();
1670
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001671 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001672}
1673
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001674status_t Camera3Device::addDummyStreamLocked() {
1675 ATRACE_CALL();
1676 status_t res;
1677
1678 if (mDummyStreamId != NO_STREAM) {
1679 // Should never be adding a second dummy stream when one is already
1680 // active
1681 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1682 __FUNCTION__, mId);
1683 return INVALID_OPERATION;
1684 }
1685
1686 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1687
1688 sp<Camera3OutputStreamInterface> dummyStream =
1689 new Camera3DummyStream(mNextStreamId);
1690
1691 res = mOutputStreams.add(mNextStreamId, dummyStream);
1692 if (res < 0) {
1693 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1694 return res;
1695 }
1696
1697 mDummyStreamId = mNextStreamId;
1698 mNextStreamId++;
1699
1700 return OK;
1701}
1702
1703status_t Camera3Device::tryRemoveDummyStreamLocked() {
1704 ATRACE_CALL();
1705 status_t res;
1706
1707 if (mDummyStreamId == NO_STREAM) return OK;
1708 if (mOutputStreams.size() == 1) return OK;
1709
1710 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1711
1712 // Ok, have a dummy stream and there's at least one other output stream,
1713 // so remove the dummy
1714
1715 sp<Camera3StreamInterface> deletedStream;
1716 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1717 if (outputStreamIdx == NAME_NOT_FOUND) {
1718 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1719 return INVALID_OPERATION;
1720 }
1721
1722 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1723 mOutputStreams.removeItemsAt(outputStreamIdx);
1724
1725 // Free up the stream endpoint so that it can be used by some other stream
1726 res = deletedStream->disconnect();
1727 if (res != OK) {
1728 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1729 // fall through since we want to still list the stream as deleted.
1730 }
1731 mDeletedStreams.add(deletedStream);
1732 mDummyStreamId = NO_STREAM;
1733
1734 return res;
1735}
1736
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001737void Camera3Device::setErrorState(const char *fmt, ...) {
1738 Mutex::Autolock l(mLock);
1739 va_list args;
1740 va_start(args, fmt);
1741
1742 setErrorStateLockedV(fmt, args);
1743
1744 va_end(args);
1745}
1746
1747void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1748 Mutex::Autolock l(mLock);
1749 setErrorStateLockedV(fmt, args);
1750}
1751
1752void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1753 va_list args;
1754 va_start(args, fmt);
1755
1756 setErrorStateLockedV(fmt, args);
1757
1758 va_end(args);
1759}
1760
1761void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001762 // Print out all error messages to log
1763 String8 errorCause = String8::formatV(fmt, args);
1764 ALOGE("Camera %d: %s", mId, errorCause.string());
1765
1766 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001767 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001768
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001769 mErrorCause = errorCause;
1770
1771 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001772 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001773
1774 // Notify upstream about a device error
1775 if (mListener != NULL) {
1776 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1777 CaptureResultExtras());
1778 }
1779
1780 // Save stack trace. View by dumping it later.
1781 CameraTraces::saveTrace();
1782 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001783}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001784
1785/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001786 * In-flight request management
1787 */
1788
Jianing Weicb0652e2014-03-12 18:29:36 -07001789status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07001790 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1791 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001792 ATRACE_CALL();
1793 Mutex::Autolock l(mInFlightLock);
1794
1795 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07001796 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
1797 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001798 if (res < 0) return res;
1799
1800 return OK;
1801}
1802
1803/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001804 * Check if all 3A fields are ready, and send off a partial 3A-only result
1805 * to the output frame queue
1806 */
Zhijun He204e3292014-07-14 17:09:23 -07001807bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001808 uint32_t frameNumber,
1809 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001810
1811 // Check if all 3A states are present
1812 // The full list of fields is
1813 // android.control.afMode
1814 // android.control.awbMode
1815 // android.control.aeState
1816 // android.control.awbState
1817 // android.control.afState
1818 // android.control.afTriggerID
1819 // android.control.aePrecaptureID
1820 // TODO: Add android.control.aeMode
1821
1822 bool gotAllStates = true;
1823
1824 uint8_t afMode;
1825 uint8_t awbMode;
1826 uint8_t aeState;
1827 uint8_t afState;
1828 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001829
1830 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1831 &afMode, frameNumber);
1832
1833 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1834 &awbMode, frameNumber);
1835
1836 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1837 &aeState, frameNumber);
1838
1839 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1840 &afState, frameNumber);
1841
1842 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1843 &awbState, frameNumber);
1844
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001845 if (!gotAllStates) return false;
1846
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001847 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001848 "AF state %d, AE state %d, AWB state %d, "
1849 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001850 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001851 afMode, awbMode,
1852 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001853 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001854
1855 // Got all states, so construct a minimal result to send
1856 // In addition to the above fields, this means adding in
1857 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001858 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001859 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001860
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001861 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001862
1863 Mutex::Autolock l(mOutputLock);
1864
Jianing Weicb0652e2014-03-12 18:29:36 -07001865 CaptureResult captureResult;
1866 captureResult.mResultExtras = resultExtras;
1867 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1868 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1869 // but not limited to CameraDeviceBase::getNextResult
1870 CaptureResult& min3AResult =
1871 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001872
Jianing Weicb0652e2014-03-12 18:29:36 -07001873 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1874 // TODO: This is problematic casting. Need to fix CameraMetadata.
1875 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001876 return false;
1877 }
1878
Jianing Weicb0652e2014-03-12 18:29:36 -07001879 int32_t requestId = resultExtras.requestId;
1880 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001881 &requestId, frameNumber)) {
1882 return false;
1883 }
1884
Zhijun He204e3292014-07-14 17:09:23 -07001885 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1886 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1887 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1888 &partialResult, frameNumber)) {
1889 return false;
1890 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001891 }
1892
Jianing Weicb0652e2014-03-12 18:29:36 -07001893 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001894 &afMode, frameNumber)) {
1895 return false;
1896 }
1897
Jianing Weicb0652e2014-03-12 18:29:36 -07001898 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001899 &awbMode, frameNumber)) {
1900 return false;
1901 }
1902
Jianing Weicb0652e2014-03-12 18:29:36 -07001903 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001904 &aeState, frameNumber)) {
1905 return false;
1906 }
1907
Jianing Weicb0652e2014-03-12 18:29:36 -07001908 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001909 &afState, frameNumber)) {
1910 return false;
1911 }
1912
Jianing Weicb0652e2014-03-12 18:29:36 -07001913 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001914 &awbState, frameNumber)) {
1915 return false;
1916 }
1917
Jianing Weicb0652e2014-03-12 18:29:36 -07001918 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001919 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001920 return false;
1921 }
1922
Jianing Weicb0652e2014-03-12 18:29:36 -07001923 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001924 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001925 return false;
1926 }
1927
Zhijun He204e3292014-07-14 17:09:23 -07001928 // We only send the aggregated partial when all 3A related metadata are available
1929 // For both API1 and API2.
1930 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001931 mResultSignal.signal();
1932
1933 return true;
1934}
1935
1936template<typename T>
1937bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001938 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001939 (void) frameNumber;
1940
1941 camera_metadata_ro_entry_t entry;
1942
1943 entry = result.find(tag);
1944 if (entry.count == 0) {
1945 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1946 mId, frameNumber, get_camera_metadata_tag_name(tag));
1947 return false;
1948 }
1949
1950 if (sizeof(T) == sizeof(uint8_t)) {
1951 *value = entry.data.u8[0];
1952 } else if (sizeof(T) == sizeof(int32_t)) {
1953 *value = entry.data.i32[0];
1954 } else {
1955 ALOGE("%s: Unexpected type", __FUNCTION__);
1956 return false;
1957 }
1958 return true;
1959}
1960
1961template<typename T>
1962bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001963 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001964 if (result.update(tag, value, 1) != NO_ERROR) {
1965 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1966 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1967 frameNumber, get_camera_metadata_tag_name(tag));
1968 return false;
1969 }
1970 return true;
1971}
1972
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08001973void Camera3Device::returnOutputBuffers(
1974 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
1975 nsecs_t timestamp) {
1976 for (size_t i = 0; i < numBuffers; i++)
1977 {
1978 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
1979 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
1980 // Note: stream may be deallocated at this point, if this buffer was
1981 // the last reference to it.
1982 if (res != OK) {
1983 ALOGE("Can't return buffer to its stream: %s (%d)",
1984 strerror(-res), res);
1985 }
1986 }
1987}
1988
1989
1990void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
1991
1992 const InFlightRequest &request = mInFlightMap.valueAt(idx);
1993 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
1994
1995 nsecs_t sensorTimestamp = request.sensorTimestamp;
1996 nsecs_t shutterTimestamp = request.shutterTimestamp;
1997
1998 // Check if it's okay to remove the request from InFlightMap:
1999 // In the case of a successful request:
2000 // all input and output buffers, all result metadata, shutter callback
2001 // arrived.
2002 // In the case of a unsuccessful request:
2003 // all input and output buffers arrived.
2004 if (request.numBuffersLeft == 0 &&
2005 (request.requestStatus != OK ||
2006 (request.haveResultMetadata && shutterTimestamp != 0))) {
2007 ATRACE_ASYNC_END("frame capture", frameNumber);
2008
2009 // Sanity check - if sensor timestamp matches shutter timestamp
2010 if (request.requestStatus == OK &&
2011 sensorTimestamp != shutterTimestamp) {
2012 SET_ERR("sensor timestamp (%" PRId64
2013 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2014 sensorTimestamp, frameNumber, shutterTimestamp);
2015 }
2016
2017 // for an unsuccessful request, it may have pending output buffers to
2018 // return.
2019 assert(request.requestStatus != OK ||
2020 request.pendingOutputBuffers.size() == 0);
2021 returnOutputBuffers(request.pendingOutputBuffers.array(),
2022 request.pendingOutputBuffers.size(), 0);
2023
2024 mInFlightMap.removeItemsAt(idx, 1);
2025
2026 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2027 }
2028
2029 // Sanity check - if we have too many in-flight frames, something has
2030 // likely gone wrong
2031 if (mInFlightMap.size() > kInFlightWarnLimit) {
2032 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
2033 }
2034}
2035
2036
2037void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2038 CaptureResultExtras &resultExtras,
2039 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002040 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002041 bool reprocess,
2042 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002043 if (pendingMetadata.isEmpty())
2044 return;
2045
2046 Mutex::Autolock l(mOutputLock);
2047
2048 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002049 if (reprocess) {
2050 if (frameNumber < mNextReprocessResultFrameNumber) {
2051 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002052 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002053 frameNumber, mNextReprocessResultFrameNumber);
2054 return;
2055 }
2056 mNextReprocessResultFrameNumber = frameNumber + 1;
2057 } else {
2058 if (frameNumber < mNextResultFrameNumber) {
2059 SET_ERR("Out-of-order capture result metadata submitted! "
2060 "(got frame number %d, expecting %d)",
2061 frameNumber, mNextResultFrameNumber);
2062 return;
2063 }
2064 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002065 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002066
2067 CaptureResult captureResult;
2068 captureResult.mResultExtras = resultExtras;
2069 captureResult.mMetadata = pendingMetadata;
2070
2071 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2072 (int32_t*)&frameNumber, 1) != OK) {
2073 SET_ERR("Failed to set frame# in metadata (%d)",
2074 frameNumber);
2075 return;
2076 } else {
2077 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2078 __FUNCTION__, mId, frameNumber);
2079 }
2080
2081 // Append any previous partials to form a complete result
2082 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2083 captureResult.mMetadata.append(collectedPartialResult);
2084 }
2085
2086 captureResult.mMetadata.sort();
2087
2088 // Check that there's a timestamp in the result metadata
2089 camera_metadata_entry entry =
2090 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2091 if (entry.count == 0) {
2092 SET_ERR("No timestamp provided by HAL for frame %d!",
2093 frameNumber);
2094 return;
2095 }
2096
Chien-Yu Chend196d612015-06-22 19:49:01 -07002097 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2098
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002099 // Valid result, insert into queue
2100 List<CaptureResult>::iterator queuedResult =
2101 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2102 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2103 ", burstId = %" PRId32, __FUNCTION__,
2104 queuedResult->mResultExtras.requestId,
2105 queuedResult->mResultExtras.frameNumber,
2106 queuedResult->mResultExtras.burstId);
2107
2108 mResultSignal.signal();
2109}
2110
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002111/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002112 * Camera HAL device callback methods
2113 */
2114
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002115void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002116 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002117
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002118 status_t res;
2119
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002120 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002121 if (result->result == NULL && result->num_output_buffers == 0 &&
2122 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002123 SET_ERR("No result data provided by HAL for frame %d",
2124 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002125 return;
2126 }
Zhijun He204e3292014-07-14 17:09:23 -07002127
2128 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2129 // partial_result to 1 when metadata is included in this result.
2130 if (!mUsePartialResult &&
2131 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2132 result->result != NULL &&
2133 result->partial_result != 1) {
2134 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2135 " if partial result is not supported",
2136 frameNumber, result->partial_result);
2137 return;
2138 }
2139
2140 bool isPartialResult = false;
2141 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002142 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002143 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002144
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002145 // Get shutter timestamp and resultExtras from list of in-flight requests,
2146 // where it was added by the shutter notification for this frame. If the
2147 // shutter timestamp isn't received yet, append the output buffers to the
2148 // in-flight request and they will be returned when the shutter timestamp
2149 // arrives. Update the in-flight status and remove the in-flight entry if
2150 // all result data and shutter timestamp have been received.
2151 nsecs_t shutterTimestamp = 0;
2152
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002153 {
2154 Mutex::Autolock l(mInFlightLock);
2155 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2156 if (idx == NAME_NOT_FOUND) {
2157 SET_ERR("Unknown frame number for capture result: %d",
2158 frameNumber);
2159 return;
2160 }
2161 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002162 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2163 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2164 ", partialResultCount = %d",
2165 __FUNCTION__, request.resultExtras.requestId,
2166 request.resultExtras.frameNumber, request.resultExtras.burstId,
2167 result->partial_result);
2168 // Always update the partial count to the latest one if it's not 0
2169 // (buffers only). When framework aggregates adjacent partial results
2170 // into one, the latest partial count will be used.
2171 if (result->partial_result != 0)
2172 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002173
2174 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002175 if (mUsePartialResult && result->result != NULL) {
2176 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2177 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2178 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2179 " the range of [1, %d] when metadata is included in the result",
2180 frameNumber, result->partial_result, mNumPartialResults);
2181 return;
2182 }
2183 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002184 if (isPartialResult) {
2185 request.partialResult.collectedResult.append(result->result);
2186 }
Zhijun He204e3292014-07-14 17:09:23 -07002187 } else {
2188 camera_metadata_ro_entry_t partialResultEntry;
2189 res = find_camera_metadata_ro_entry(result->result,
2190 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2191 if (res != NAME_NOT_FOUND &&
2192 partialResultEntry.count > 0 &&
2193 partialResultEntry.data.u8[0] ==
2194 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2195 // A partial result. Flag this as such, and collect this
2196 // set of metadata into the in-flight entry.
2197 isPartialResult = true;
2198 request.partialResult.collectedResult.append(
2199 result->result);
2200 request.partialResult.collectedResult.erase(
2201 ANDROID_QUIRKS_PARTIAL_RESULT);
2202 }
2203 }
2204
2205 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002206 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002207 if (!request.partialResult.haveSent3A) {
2208 request.partialResult.haveSent3A =
2209 processPartial3AResult(frameNumber,
2210 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002211 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002212 }
2213 }
2214 }
2215
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002216 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002217 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002218
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002219 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002220 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002221 if (request.haveResultMetadata) {
2222 SET_ERR("Called multiple times with metadata for frame %d",
2223 frameNumber);
2224 return;
2225 }
Zhijun He204e3292014-07-14 17:09:23 -07002226 if (mUsePartialResult &&
2227 !request.partialResult.collectedResult.isEmpty()) {
2228 collectedPartialResult.acquire(
2229 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002230 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002231 request.haveResultMetadata = true;
2232 }
2233
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002234 uint32_t numBuffersReturned = result->num_output_buffers;
2235 if (result->input_buffer != NULL) {
2236 if (hasInputBufferInRequest) {
2237 numBuffersReturned += 1;
2238 } else {
2239 ALOGW("%s: Input buffer should be NULL if there is no input"
2240 " buffer sent in the request",
2241 __FUNCTION__);
2242 }
2243 }
2244 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002245 if (request.numBuffersLeft < 0) {
2246 SET_ERR("Too many buffers returned for frame %d",
2247 frameNumber);
2248 return;
2249 }
2250
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002251 camera_metadata_ro_entry_t entry;
2252 res = find_camera_metadata_ro_entry(result->result,
2253 ANDROID_SENSOR_TIMESTAMP, &entry);
2254 if (res == OK && entry.count == 1) {
2255 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002256 }
2257
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002258 // If shutter event isn't received yet, append the output buffers to
2259 // the in-flight request. Otherwise, return the output buffers to
2260 // streams.
2261 if (shutterTimestamp == 0) {
2262 request.pendingOutputBuffers.appendArray(result->output_buffers,
2263 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002264 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002265 returnOutputBuffers(result->output_buffers,
2266 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002267 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002268
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002269 if (result->result != NULL && !isPartialResult) {
2270 if (shutterTimestamp == 0) {
2271 request.pendingMetadata = result->result;
2272 request.partialResult.collectedResult = collectedPartialResult;
2273 } else {
2274 CameraMetadata metadata;
2275 metadata = result->result;
2276 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002277 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2278 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002279 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002280 }
2281
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002282 removeInFlightRequestIfReadyLocked(idx);
2283 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002284
Zhijun Hef0d962a2014-06-30 10:24:11 -07002285 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002286 if (hasInputBufferInRequest) {
2287 Camera3Stream *stream =
2288 Camera3Stream::cast(result->input_buffer->stream);
2289 res = stream->returnInputBuffer(*(result->input_buffer));
2290 // Note: stream may be deallocated at this point, if this buffer was the
2291 // last reference to it.
2292 if (res != OK) {
2293 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2294 " its stream:%s (%d)", __FUNCTION__,
2295 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002296 }
2297 } else {
2298 ALOGW("%s: Input buffer should be NULL if there is no input"
2299 " buffer sent in the request, skipping input buffer return.",
2300 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002301 }
2302 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002303}
2304
2305void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002306 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002307 NotificationListener *listener;
2308 {
2309 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002310 listener = mListener;
2311 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002312
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002313 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002314 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002315 return;
2316 }
2317
2318 switch (msg->type) {
2319 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002320 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002321 break;
2322 }
2323 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002324 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002325 break;
2326 }
2327 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002328 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002329 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002330 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002331}
2332
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002333void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2334 NotificationListener *listener) {
2335
2336 // Map camera HAL error codes to ICameraDeviceCallback error codes
2337 // Index into this with the HAL error code
2338 static const ICameraDeviceCallbacks::CameraErrorCode
2339 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2340 // 0 = Unused error code
2341 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2342 // 1 = CAMERA3_MSG_ERROR_DEVICE
2343 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2344 // 2 = CAMERA3_MSG_ERROR_REQUEST
2345 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2346 // 3 = CAMERA3_MSG_ERROR_RESULT
2347 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2348 // 4 = CAMERA3_MSG_ERROR_BUFFER
2349 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2350 };
2351
2352 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2353 ((msg.error_code >= 0) &&
2354 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2355 halErrorMap[msg.error_code] :
2356 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2357
2358 int streamId = 0;
2359 if (msg.error_stream != NULL) {
2360 Camera3Stream *stream =
2361 Camera3Stream::cast(msg.error_stream);
2362 streamId = stream->getId();
2363 }
2364 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2365 mId, __FUNCTION__, msg.frame_number,
2366 streamId, msg.error_code);
2367
2368 CaptureResultExtras resultExtras;
2369 switch (errorCode) {
2370 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2371 // SET_ERR calls notifyError
2372 SET_ERR("Camera HAL reported serious device error");
2373 break;
2374 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2375 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2376 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2377 {
2378 Mutex::Autolock l(mInFlightLock);
2379 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2380 if (idx >= 0) {
2381 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2382 r.requestStatus = msg.error_code;
2383 resultExtras = r.resultExtras;
2384 } else {
2385 resultExtras.frameNumber = msg.frame_number;
2386 ALOGE("Camera %d: %s: cannot find in-flight request on "
2387 "frame %" PRId64 " error", mId, __FUNCTION__,
2388 resultExtras.frameNumber);
2389 }
2390 }
2391 if (listener != NULL) {
2392 listener->notifyError(errorCode, resultExtras);
2393 } else {
2394 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2395 }
2396 break;
2397 default:
2398 // SET_ERR calls notifyError
2399 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2400 break;
2401 }
2402}
2403
2404void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2405 NotificationListener *listener) {
2406 ssize_t idx;
2407 // Verify ordering of shutter notifications
2408 {
2409 Mutex::Autolock l(mOutputLock);
2410 // TODO: need to track errors for tighter bounds on expected frame number.
2411 if (msg.frame_number < mNextShutterFrameNumber) {
2412 SET_ERR("Shutter notification out-of-order. Expected "
2413 "notification for frame %d, got frame %d",
2414 mNextShutterFrameNumber, msg.frame_number);
2415 return;
2416 }
2417 mNextShutterFrameNumber = msg.frame_number + 1;
2418 }
2419
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002420 // Set timestamp for the request in the in-flight tracking
2421 // and get the request ID to send upstream
2422 {
2423 Mutex::Autolock l(mInFlightLock);
2424 idx = mInFlightMap.indexOfKey(msg.frame_number);
2425 if (idx >= 0) {
2426 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002427
2428 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2429 mId, __FUNCTION__,
2430 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2431 // Call listener, if any
2432 if (listener != NULL) {
2433 listener->notifyShutter(r.resultExtras, msg.timestamp);
2434 }
2435
2436 r.shutterTimestamp = msg.timestamp;
2437
2438 // send pending result and buffers
2439 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002440 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002441 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002442 returnOutputBuffers(r.pendingOutputBuffers.array(),
2443 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2444 r.pendingOutputBuffers.clear();
2445
2446 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002447 }
2448 }
2449 if (idx < 0) {
2450 SET_ERR("Shutter notification for non-existent frame number %d",
2451 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002452 }
2453}
2454
2455
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002456CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002457 ALOGV("%s", __FUNCTION__);
2458
Igor Murashkin1e479c02013-09-06 16:55:14 -07002459 CameraMetadata retVal;
2460
2461 if (mRequestThread != NULL) {
2462 retVal = mRequestThread->getLatestRequest();
2463 }
2464
Igor Murashkin1e479c02013-09-06 16:55:14 -07002465 return retVal;
2466}
2467
Jianing Weicb0652e2014-03-12 18:29:36 -07002468
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002469/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002470 * RequestThread inner class methods
2471 */
2472
2473Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002474 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002475 camera3_device_t *hal3Device) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002476 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002477 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002478 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002479 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002480 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002481 mReconfigured(false),
2482 mDoPause(false),
2483 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002484 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002485 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002486 mCurrentAfTriggerId(0),
2487 mCurrentPreCaptureTriggerId(0),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002488 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002489 mStatusId = statusTracker->addComponent();
Chien-Yu Chend196d612015-06-22 19:49:01 -07002490
2491 mAeLockAvailable = false;
2492 sp<Camera3Device> p = parent.promote();
2493 if (p != NULL) {
2494 camera_metadata_ro_entry aeLockAvailable =
2495 p->info().find(ANDROID_CONTROL_AE_LOCK_AVAILABLE);
2496 if (aeLockAvailable.count > 0) {
2497 mAeLockAvailable = (aeLockAvailable.data.u8[0] ==
2498 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
2499 }
2500 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002501}
2502
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002503void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002504 NotificationListener *listener) {
2505 Mutex::Autolock l(mRequestLock);
2506 mListener = listener;
2507}
2508
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002509void Camera3Device::RequestThread::configurationComplete() {
2510 Mutex::Autolock l(mRequestLock);
2511 mReconfigured = true;
2512}
2513
Jianing Wei90e59c92014-03-12 18:29:36 -07002514status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002515 List<sp<CaptureRequest> > &requests,
2516 /*out*/
2517 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002518 Mutex::Autolock l(mRequestLock);
2519 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2520 ++it) {
2521 mRequestQueue.push_back(*it);
2522 }
2523
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002524 if (lastFrameNumber != NULL) {
2525 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2526 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2527 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2528 *lastFrameNumber);
2529 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002530
Jianing Wei90e59c92014-03-12 18:29:36 -07002531 unpauseForNewRequests();
2532
2533 return OK;
2534}
2535
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002536
2537status_t Camera3Device::RequestThread::queueTrigger(
2538 RequestTrigger trigger[],
2539 size_t count) {
2540
2541 Mutex::Autolock l(mTriggerMutex);
2542 status_t ret;
2543
2544 for (size_t i = 0; i < count; ++i) {
2545 ret = queueTriggerLocked(trigger[i]);
2546
2547 if (ret != OK) {
2548 return ret;
2549 }
2550 }
2551
2552 return OK;
2553}
2554
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002555int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2556 sp<Camera3Device> d = device.promote();
2557 if (d != NULL) return d->mId;
2558 return 0;
2559}
2560
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002561status_t Camera3Device::RequestThread::queueTriggerLocked(
2562 RequestTrigger trigger) {
2563
2564 uint32_t tag = trigger.metadataTag;
2565 ssize_t index = mTriggerMap.indexOfKey(tag);
2566
2567 switch (trigger.getTagType()) {
2568 case TYPE_BYTE:
2569 // fall-through
2570 case TYPE_INT32:
2571 break;
2572 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002573 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2574 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002575 return INVALID_OPERATION;
2576 }
2577
2578 /**
2579 * Collect only the latest trigger, since we only have 1 field
2580 * in the request settings per trigger tag, and can't send more than 1
2581 * trigger per request.
2582 */
2583 if (index != NAME_NOT_FOUND) {
2584 mTriggerMap.editValueAt(index) = trigger;
2585 } else {
2586 mTriggerMap.add(tag, trigger);
2587 }
2588
2589 return OK;
2590}
2591
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002592status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002593 const RequestList &requests,
2594 /*out*/
2595 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002596 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002597 if (lastFrameNumber != NULL) {
2598 *lastFrameNumber = mRepeatingLastFrameNumber;
2599 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002600 mRepeatingRequests.clear();
2601 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2602 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002603
2604 unpauseForNewRequests();
2605
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002606 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002607 return OK;
2608}
2609
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002610bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2611 if (mRepeatingRequests.empty()) {
2612 return false;
2613 }
2614 int32_t requestId = requestIn->mResultExtras.requestId;
2615 const RequestList &repeatRequests = mRepeatingRequests;
2616 // All repeating requests are guaranteed to have same id so only check first quest
2617 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2618 return (firstRequest->mResultExtras.requestId == requestId);
2619}
2620
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002621status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002622 Mutex::Autolock l(mRequestLock);
2623 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002624 if (lastFrameNumber != NULL) {
2625 *lastFrameNumber = mRepeatingLastFrameNumber;
2626 }
2627 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002628 return OK;
2629}
2630
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002631status_t Camera3Device::RequestThread::clear(
2632 NotificationListener *listener,
2633 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002634 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002635 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002636
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002637 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002638
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002639 // Send errors for all requests pending in the request queue, including
2640 // pending repeating requests
2641 if (listener != NULL) {
2642 for (RequestList::iterator it = mRequestQueue.begin();
2643 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002644 // Abort the input buffers for reprocess requests.
2645 if ((*it)->mInputStream != NULL) {
2646 camera3_stream_buffer_t inputBuffer;
2647 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2648 if (res != OK) {
2649 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2650 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2651 } else {
2652 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2653 if (res != OK) {
2654 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2655 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2656 }
2657 }
2658 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002659 // Set the frame number this request would have had, if it
2660 // had been submitted; this frame number will not be reused.
2661 // The requestId and burstId fields were set when the request was
2662 // submitted originally (in convertMetadataListToRequestListLocked)
2663 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2664 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2665 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002666 }
2667 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002668 mRequestQueue.clear();
2669 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002670 if (lastFrameNumber != NULL) {
2671 *lastFrameNumber = mRepeatingLastFrameNumber;
2672 }
2673 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002674 return OK;
2675}
2676
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002677void Camera3Device::RequestThread::setPaused(bool paused) {
2678 Mutex::Autolock l(mPauseLock);
2679 mDoPause = paused;
2680 mDoPauseSignal.signal();
2681}
2682
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002683status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2684 int32_t requestId, nsecs_t timeout) {
2685 Mutex::Autolock l(mLatestRequestMutex);
2686 status_t res;
2687 while (mLatestRequestId != requestId) {
2688 nsecs_t startTime = systemTime();
2689
2690 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2691 if (res != OK) return res;
2692
2693 timeout -= (systemTime() - startTime);
2694 }
2695
2696 return OK;
2697}
2698
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002699void Camera3Device::RequestThread::requestExit() {
2700 // Call parent to set up shutdown
2701 Thread::requestExit();
2702 // The exit from any possible waits
2703 mDoPauseSignal.signal();
2704 mRequestSignal.signal();
2705}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002706
Chien-Yu Chend196d612015-06-22 19:49:01 -07002707
2708/**
2709 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2710 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2711 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2712 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2713 * request.
2714 */
2715void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2716 request->mAeTriggerCancelOverride.applyAeLock = false;
2717 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2718
2719 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2720 return;
2721 }
2722
2723 camera_metadata_entry_t aePrecaptureTrigger =
2724 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2725 if (aePrecaptureTrigger.count > 0 &&
2726 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2727 // Always override CANCEL to IDLE
2728 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2729 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2730 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2731 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2732 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2733
2734 if (mAeLockAvailable == true) {
2735 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2736 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2737 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2738 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2739 request->mAeTriggerCancelOverride.applyAeLock = true;
2740 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2741 }
2742 }
2743 }
2744}
2745
2746/**
2747 * Override result metadata for cancelling AE precapture trigger applied in
2748 * handleAePrecaptureCancelRequest().
2749 */
2750void Camera3Device::overrideResultForPrecaptureCancel(
2751 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2752 if (aeTriggerCancelOverride.applyAeLock) {
2753 // Only devices <= v3.2 should have this override
2754 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2755 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2756 }
2757
2758 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2759 // Only devices <= v3.2 should have this override
2760 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2761 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2762 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2763 }
2764}
2765
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002766bool Camera3Device::RequestThread::threadLoop() {
2767
2768 status_t res;
2769
2770 // Handle paused state.
2771 if (waitIfPaused()) {
2772 return true;
2773 }
2774
2775 // Get work to do
2776
2777 sp<CaptureRequest> nextRequest = waitForNextRequest();
2778 if (nextRequest == NULL) {
2779 return true;
2780 }
2781
2782 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002783 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002784 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002785 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002786
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002787 // Get the request ID, if any
2788 int requestId;
2789 camera_metadata_entry_t requestIdEntry =
2790 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2791 if (requestIdEntry.count > 0) {
2792 requestId = requestIdEntry.data.i32[0];
2793 } else {
2794 ALOGW("%s: Did not have android.request.id set in the request",
2795 __FUNCTION__);
2796 requestId = NAME_NOT_FOUND;
2797 }
2798
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002799 // Insert any queued triggers (before metadata is locked)
2800 int32_t triggerCount;
2801 res = insertTriggers(nextRequest);
2802 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002803 SET_ERR("RequestThread: Unable to insert triggers "
2804 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002805 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002806 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2807 return false;
2808 }
2809 triggerCount = res;
2810
2811 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2812
2813 // If the request is the same as last, or we had triggers last time
2814 if (mPrevRequest != nextRequest || triggersMixedIn) {
2815 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002816 * HAL workaround:
2817 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2818 */
2819 res = addDummyTriggerIds(nextRequest);
2820 if (res != OK) {
2821 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2822 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002823 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002824 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2825 return false;
2826 }
2827
2828 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002829 * The request should be presorted so accesses in HAL
2830 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2831 */
2832 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002833 request.settings = nextRequest->mSettings.getAndLock();
2834 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002835 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2836
2837 IF_ALOGV() {
2838 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2839 find_camera_metadata_ro_entry(
2840 request.settings,
2841 ANDROID_CONTROL_AF_TRIGGER,
2842 &e
2843 );
2844 if (e.count > 0) {
2845 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2846 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002847 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002848 e.data.u8[0]);
2849 }
2850 }
2851 } else {
2852 // leave request.settings NULL to indicate 'reuse latest given'
2853 ALOGVV("%s: Request settings are REUSED",
2854 __FUNCTION__);
2855 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002856
Zhijun Hef0d962a2014-06-30 10:24:11 -07002857 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002858
2859 // Fill in buffers
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002860 if (nextRequest->mInputStream != NULL) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002861 request.input_buffer = &nextRequest->mInputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002862 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002863 } else {
2864 request.input_buffer = NULL;
2865 }
2866
2867 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2868 nextRequest->mOutputStreams.size());
2869 request.output_buffers = outputBuffers.array();
2870 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2871 res = nextRequest->mOutputStreams.editItemAt(i)->
2872 getBuffer(&outputBuffers.editItemAt(i));
2873 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002874 // Can't get output buffer from gralloc queue - this could be due to
2875 // abandoned queue or other consumer misbehavior, so not a fatal
2876 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002877 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2878 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala0ec23d32015-05-28 15:15:54 -07002879 {
2880 Mutex::Autolock l(mRequestLock);
2881 if (mListener != NULL) {
2882 mListener->notifyError(
2883 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2884 nextRequest->mResultExtras);
2885 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002886 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002887 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2888 return true;
2889 }
2890 request.num_output_buffers++;
2891 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002892 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002893
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002894 // Log request in the in-flight queue
2895 sp<Camera3Device> parent = mParent.promote();
2896 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002897 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002898 CLOGE("RequestThread: Parent is gone");
2899 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2900 return false;
2901 }
2902
Jianing Weicb0652e2014-03-12 18:29:36 -07002903 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002904 totalNumBuffers, nextRequest->mResultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002905 /*hasInput*/request.input_buffer != NULL,
2906 nextRequest->mAeTriggerCancelOverride);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002907 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2908 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002909 __FUNCTION__,
2910 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2911 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002912 if (res != OK) {
2913 SET_ERR("RequestThread: Unable to register new in-flight request:"
2914 " %s (%d)", strerror(-res), res);
2915 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2916 return false;
2917 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002918
Zhijun Hecc27e112013-10-03 16:12:43 -07002919 // Inform waitUntilRequestProcessed thread of a new request ID
2920 {
2921 Mutex::Autolock al(mLatestRequestMutex);
2922
2923 mLatestRequestId = requestId;
2924 mLatestRequestSignal.signal();
2925 }
2926
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002927 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002928 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2929 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002930 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002931 ATRACE_END();
2932
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002933 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002934 // Should only get a failure here for malformed requests or device-level
2935 // errors, so consider all errors fatal. Bad metadata failures should
2936 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002937 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002938 " device: %s (%d)", request.frame_number, strerror(-res), res);
2939 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2940 return false;
2941 }
2942
Igor Murashkin1e479c02013-09-06 16:55:14 -07002943 // Update the latest request sent to HAL
2944 if (request.settings != NULL) { // Don't update them if they were unchanged
2945 Mutex::Autolock al(mLatestRequestMutex);
2946
2947 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2948 mLatestRequest.acquire(cloned);
2949 }
2950
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002951 if (request.settings != NULL) {
2952 nextRequest->mSettings.unlock(request.settings);
2953 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002954
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002955 // Unset as current request
2956 {
2957 Mutex::Autolock l(mRequestLock);
2958 mNextRequest.clear();
2959 }
2960
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002961 // Remove any previously queued triggers (after unlock)
2962 res = removeTriggers(mPrevRequest);
2963 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002964 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002965 "(capture request %d, HAL device: %s (%d)",
2966 request.frame_number, strerror(-res), res);
2967 return false;
2968 }
2969 mPrevTriggers = triggerCount;
2970
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002971 return true;
2972}
2973
Igor Murashkin1e479c02013-09-06 16:55:14 -07002974CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2975 Mutex::Autolock al(mLatestRequestMutex);
2976
2977 ALOGV("RequestThread::%s", __FUNCTION__);
2978
2979 return mLatestRequest;
2980}
2981
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002982bool Camera3Device::RequestThread::isStreamPending(
2983 sp<Camera3StreamInterface>& stream) {
2984 Mutex::Autolock l(mRequestLock);
2985
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002986 if (mNextRequest != nullptr) {
2987 for (const auto& s : mNextRequest->mOutputStreams) {
2988 if (stream == s) return true;
2989 }
2990 if (stream == mNextRequest->mInputStream) return true;
2991 }
2992
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002993 for (const auto& request : mRequestQueue) {
2994 for (const auto& s : request->mOutputStreams) {
2995 if (stream == s) return true;
2996 }
2997 if (stream == request->mInputStream) return true;
2998 }
2999
3000 for (const auto& request : mRepeatingRequests) {
3001 for (const auto& s : request->mOutputStreams) {
3002 if (stream == s) return true;
3003 }
3004 if (stream == request->mInputStream) return true;
3005 }
3006
3007 return false;
3008}
Jianing Weicb0652e2014-03-12 18:29:36 -07003009
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003010void Camera3Device::RequestThread::cleanUpFailedRequest(
3011 camera3_capture_request_t &request,
3012 sp<CaptureRequest> &nextRequest,
3013 Vector<camera3_stream_buffer_t> &outputBuffers) {
3014
3015 if (request.settings != NULL) {
3016 nextRequest->mSettings.unlock(request.settings);
3017 }
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003018 if (nextRequest->mInputStream != NULL) {
3019 nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3020 nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003021 }
3022 for (size_t i = 0; i < request.num_output_buffers; i++) {
3023 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3024 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
3025 outputBuffers[i], 0);
3026 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003027
3028 Mutex::Autolock l(mRequestLock);
3029 mNextRequest.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003030}
3031
3032sp<Camera3Device::CaptureRequest>
3033 Camera3Device::RequestThread::waitForNextRequest() {
3034 status_t res;
3035 sp<CaptureRequest> nextRequest;
3036
3037 // Optimized a bit for the simple steady-state case (single repeating
3038 // request), to avoid putting that request in the queue temporarily.
3039 Mutex::Autolock l(mRequestLock);
3040
3041 while (mRequestQueue.empty()) {
3042 if (!mRepeatingRequests.empty()) {
3043 // Always atomically enqueue all requests in a repeating request
3044 // list. Guarantees a complete in-sequence set of captures to
3045 // application.
3046 const RequestList &requests = mRepeatingRequests;
3047 RequestList::const_iterator firstRequest =
3048 requests.begin();
3049 nextRequest = *firstRequest;
3050 mRequestQueue.insert(mRequestQueue.end(),
3051 ++firstRequest,
3052 requests.end());
3053 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003054
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003055 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003056
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003057 break;
3058 }
3059
3060 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3061
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003062 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3063 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003064 Mutex::Autolock pl(mPauseLock);
3065 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003066 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003067 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003068 // Let the tracker know
3069 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3070 if (statusTracker != 0) {
3071 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3072 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003073 }
3074 // Stop waiting for now and let thread management happen
3075 return NULL;
3076 }
3077 }
3078
3079 if (nextRequest == NULL) {
3080 // Don't have a repeating request already in hand, so queue
3081 // must have an entry now.
3082 RequestList::iterator firstRequest =
3083 mRequestQueue.begin();
3084 nextRequest = *firstRequest;
3085 mRequestQueue.erase(firstRequest);
3086 }
3087
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003088 // In case we've been unpaused by setPaused clearing mDoPause, need to
3089 // update internal pause state (capture/setRepeatingRequest unpause
3090 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003091 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003092 if (mPaused) {
3093 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3094 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3095 if (statusTracker != 0) {
3096 statusTracker->markComponentActive(mStatusId);
3097 }
3098 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003099 mPaused = false;
3100
3101 // Check if we've reconfigured since last time, and reset the preview
3102 // request if so. Can't use 'NULL request == repeat' across configure calls.
3103 if (mReconfigured) {
3104 mPrevRequest.clear();
3105 mReconfigured = false;
3106 }
3107
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003108 if (nextRequest != NULL) {
3109 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003110 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3111 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003112
3113 // Since RequestThread::clear() removes buffers from the input stream,
3114 // get the right buffer here before unlocking mRequestLock
3115 if (nextRequest->mInputStream != NULL) {
3116 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3117 if (res != OK) {
3118 // Can't get input buffer from gralloc queue - this could be due to
3119 // disconnected queue or other producer misbehavior, so not a fatal
3120 // error
3121 ALOGE("%s: Can't get input buffer, skipping request:"
3122 " %s (%d)", __FUNCTION__, strerror(-res), res);
3123 if (mListener != NULL) {
3124 mListener->notifyError(
3125 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3126 nextRequest->mResultExtras);
3127 }
3128 return NULL;
3129 }
3130 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003131 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003132
3133 handleAePrecaptureCancelRequest(nextRequest);
3134
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003135 mNextRequest = nextRequest;
3136
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003137 return nextRequest;
3138}
3139
3140bool Camera3Device::RequestThread::waitIfPaused() {
3141 status_t res;
3142 Mutex::Autolock l(mPauseLock);
3143 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003144 if (mPaused == false) {
3145 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003146 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3147 // Let the tracker know
3148 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3149 if (statusTracker != 0) {
3150 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3151 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003152 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003153
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003154 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003155 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003156 return true;
3157 }
3158 }
3159 // We don't set mPaused to false here, because waitForNextRequest needs
3160 // to further manage the paused state in case of starvation.
3161 return false;
3162}
3163
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003164void Camera3Device::RequestThread::unpauseForNewRequests() {
3165 // With work to do, mark thread as unpaused.
3166 // If paused by request (setPaused), don't resume, to avoid
3167 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003168 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003169 Mutex::Autolock p(mPauseLock);
3170 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003171 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3172 if (mPaused) {
3173 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3174 if (statusTracker != 0) {
3175 statusTracker->markComponentActive(mStatusId);
3176 }
3177 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003178 mPaused = false;
3179 }
3180}
3181
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003182void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3183 sp<Camera3Device> parent = mParent.promote();
3184 if (parent != NULL) {
3185 va_list args;
3186 va_start(args, fmt);
3187
3188 parent->setErrorStateV(fmt, args);
3189
3190 va_end(args);
3191 }
3192}
3193
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003194status_t Camera3Device::RequestThread::insertTriggers(
3195 const sp<CaptureRequest> &request) {
3196
3197 Mutex::Autolock al(mTriggerMutex);
3198
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003199 sp<Camera3Device> parent = mParent.promote();
3200 if (parent == NULL) {
3201 CLOGE("RequestThread: Parent is gone");
3202 return DEAD_OBJECT;
3203 }
3204
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003205 CameraMetadata &metadata = request->mSettings;
3206 size_t count = mTriggerMap.size();
3207
3208 for (size_t i = 0; i < count; ++i) {
3209 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003210 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003211
3212 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3213 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3214 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003215 if (isAeTrigger) {
3216 request->mResultExtras.precaptureTriggerId = triggerId;
3217 mCurrentPreCaptureTriggerId = triggerId;
3218 } else {
3219 request->mResultExtras.afTriggerId = triggerId;
3220 mCurrentAfTriggerId = triggerId;
3221 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003222 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3223 continue; // Trigger ID tag is deprecated since device HAL 3.2
3224 }
3225 }
3226
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003227 camera_metadata_entry entry = metadata.find(tag);
3228
3229 if (entry.count > 0) {
3230 /**
3231 * Already has an entry for this trigger in the request.
3232 * Rewrite it with our requested trigger value.
3233 */
3234 RequestTrigger oldTrigger = trigger;
3235
3236 oldTrigger.entryValue = entry.data.u8[0];
3237
3238 mTriggerReplacedMap.add(tag, oldTrigger);
3239 } else {
3240 /**
3241 * More typical, no trigger entry, so we just add it
3242 */
3243 mTriggerRemovedMap.add(tag, trigger);
3244 }
3245
3246 status_t res;
3247
3248 switch (trigger.getTagType()) {
3249 case TYPE_BYTE: {
3250 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3251 res = metadata.update(tag,
3252 &entryValue,
3253 /*count*/1);
3254 break;
3255 }
3256 case TYPE_INT32:
3257 res = metadata.update(tag,
3258 &trigger.entryValue,
3259 /*count*/1);
3260 break;
3261 default:
3262 ALOGE("%s: Type not supported: 0x%x",
3263 __FUNCTION__,
3264 trigger.getTagType());
3265 return INVALID_OPERATION;
3266 }
3267
3268 if (res != OK) {
3269 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3270 ", value %d", __FUNCTION__, trigger.getTagName(),
3271 trigger.entryValue);
3272 return res;
3273 }
3274
3275 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3276 trigger.getTagName(),
3277 trigger.entryValue);
3278 }
3279
3280 mTriggerMap.clear();
3281
3282 return count;
3283}
3284
3285status_t Camera3Device::RequestThread::removeTriggers(
3286 const sp<CaptureRequest> &request) {
3287 Mutex::Autolock al(mTriggerMutex);
3288
3289 CameraMetadata &metadata = request->mSettings;
3290
3291 /**
3292 * Replace all old entries with their old values.
3293 */
3294 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3295 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3296
3297 status_t res;
3298
3299 uint32_t tag = trigger.metadataTag;
3300 switch (trigger.getTagType()) {
3301 case TYPE_BYTE: {
3302 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3303 res = metadata.update(tag,
3304 &entryValue,
3305 /*count*/1);
3306 break;
3307 }
3308 case TYPE_INT32:
3309 res = metadata.update(tag,
3310 &trigger.entryValue,
3311 /*count*/1);
3312 break;
3313 default:
3314 ALOGE("%s: Type not supported: 0x%x",
3315 __FUNCTION__,
3316 trigger.getTagType());
3317 return INVALID_OPERATION;
3318 }
3319
3320 if (res != OK) {
3321 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3322 ", trigger value %d", __FUNCTION__,
3323 trigger.getTagName(), trigger.entryValue);
3324 return res;
3325 }
3326 }
3327 mTriggerReplacedMap.clear();
3328
3329 /**
3330 * Remove all new entries.
3331 */
3332 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3333 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3334 status_t res = metadata.erase(trigger.metadataTag);
3335
3336 if (res != OK) {
3337 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3338 ", trigger value %d", __FUNCTION__,
3339 trigger.getTagName(), trigger.entryValue);
3340 return res;
3341 }
3342 }
3343 mTriggerRemovedMap.clear();
3344
3345 return OK;
3346}
3347
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003348status_t Camera3Device::RequestThread::addDummyTriggerIds(
3349 const sp<CaptureRequest> &request) {
3350 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3351 static const int32_t dummyTriggerId = 1;
3352 status_t res;
3353
3354 CameraMetadata &metadata = request->mSettings;
3355
3356 // If AF trigger is active, insert a dummy AF trigger ID if none already
3357 // exists
3358 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3359 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3360 if (afTrigger.count > 0 &&
3361 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3362 afId.count == 0) {
3363 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3364 if (res != OK) return res;
3365 }
3366
3367 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3368 // if none already exists
3369 camera_metadata_entry pcTrigger =
3370 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3371 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3372 if (pcTrigger.count > 0 &&
3373 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3374 pcId.count == 0) {
3375 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3376 &dummyTriggerId, 1);
3377 if (res != OK) return res;
3378 }
3379
3380 return OK;
3381}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003382
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003383/**
3384 * PreparerThread inner class methods
3385 */
3386
3387Camera3Device::PreparerThread::PreparerThread() :
3388 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3389}
3390
3391Camera3Device::PreparerThread::~PreparerThread() {
3392 Thread::requestExitAndWait();
3393 if (mCurrentStream != nullptr) {
3394 mCurrentStream->cancelPrepare();
3395 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3396 mCurrentStream.clear();
3397 }
3398 clear();
3399}
3400
3401status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) {
3402 status_t res;
3403
3404 Mutex::Autolock l(mLock);
3405
3406 res = stream->startPrepare();
3407 if (res == OK) {
3408 // No preparation needed, fire listener right off
3409 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3410 if (mListener) {
3411 mListener->notifyPrepared(stream->getId());
3412 }
3413 return OK;
3414 } else if (res != NOT_ENOUGH_DATA) {
3415 return res;
3416 }
3417
3418 // Need to prepare, start up thread if necessary
3419 if (!mActive) {
3420 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3421 // isn't running
3422 Thread::requestExitAndWait();
3423 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3424 if (res != OK) {
3425 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3426 if (mListener) {
3427 mListener->notifyPrepared(stream->getId());
3428 }
3429 return res;
3430 }
3431 mCancelNow = false;
3432 mActive = true;
3433 ALOGV("%s: Preparer stream started", __FUNCTION__);
3434 }
3435
3436 // queue up the work
3437 mPendingStreams.push_back(stream);
3438 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3439
3440 return OK;
3441}
3442
3443status_t Camera3Device::PreparerThread::clear() {
3444 status_t res;
3445
3446 Mutex::Autolock l(mLock);
3447
3448 for (const auto& stream : mPendingStreams) {
3449 stream->cancelPrepare();
3450 }
3451 mPendingStreams.clear();
3452 mCancelNow = true;
3453
3454 return OK;
3455}
3456
3457void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3458 Mutex::Autolock l(mLock);
3459 mListener = listener;
3460}
3461
3462bool Camera3Device::PreparerThread::threadLoop() {
3463 status_t res;
3464 {
3465 Mutex::Autolock l(mLock);
3466 if (mCurrentStream == nullptr) {
3467 // End thread if done with work
3468 if (mPendingStreams.empty()) {
3469 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3470 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3471 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3472 mActive = false;
3473 return false;
3474 }
3475
3476 // Get next stream to prepare
3477 auto it = mPendingStreams.begin();
3478 mCurrentStream = *it;
3479 mPendingStreams.erase(it);
3480 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3481 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3482 } else if (mCancelNow) {
3483 mCurrentStream->cancelPrepare();
3484 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3485 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3486 mCurrentStream.clear();
3487 mCancelNow = false;
3488 return true;
3489 }
3490 }
3491
3492 res = mCurrentStream->prepareNextBuffer();
3493 if (res == NOT_ENOUGH_DATA) return true;
3494 if (res != OK) {
3495 // Something bad happened; try to recover by cancelling prepare and
3496 // signalling listener anyway
3497 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3498 mCurrentStream->getId(), res, strerror(-res));
3499 mCurrentStream->cancelPrepare();
3500 }
3501
3502 // This stream has finished, notify listener
3503 Mutex::Autolock l(mLock);
3504 if (mListener) {
3505 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3506 mCurrentStream->getId());
3507 mListener->notifyPrepared(mCurrentStream->getId());
3508 }
3509
3510 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3511 mCurrentStream.clear();
3512
3513 return true;
3514}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003515
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003516/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003517 * Static callback forwarding methods from HAL to instance
3518 */
3519
3520void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3521 const camera3_capture_result *result) {
3522 Camera3Device *d =
3523 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003524
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003525 d->processCaptureResult(result);
3526}
3527
3528void Camera3Device::sNotify(const camera3_callback_ops *cb,
3529 const camera3_notify_msg *msg) {
3530 Camera3Device *d =
3531 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3532 d->notify(msg);
3533}
3534
3535}; // namespace android