blob: 67bd22f6e43da0cb995fd327e7ac87e1d8862fcb [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
Chien-Yu Chen116a1892016-03-09 12:21:01 -080046#include "CameraService.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070047#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070048#include "device3/Camera3Device.h"
49#include "device3/Camera3OutputStream.h"
50#include "device3/Camera3InputStream.h"
51#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070052#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070053#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080054
55using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080056
57namespace android {
58
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080059Camera3Device::Camera3Device(int id):
60 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070061 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080062 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070063 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070064 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070065 mUsePartialResult(false),
66 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070067 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070068 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070069 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070070 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080071{
72 ATRACE_CALL();
73 camera3_callback_ops::notify = &sNotify;
74 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
75 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
76}
77
78Camera3Device::~Camera3Device()
79{
80 ATRACE_CALL();
81 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
82 disconnect();
83}
84
Igor Murashkin71381052013-03-04 14:53:08 -080085int Camera3Device::getId() const {
86 return mId;
87}
88
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080089/**
90 * CameraDeviceBase interface
91 */
92
Yin-Chia Yehe074a932015-01-30 10:29:02 -080093status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080094{
95 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070096 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080097 Mutex::Autolock l(mLock);
98
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080099 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800100 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700101 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800102 return INVALID_OPERATION;
103 }
104
105 /** Open HAL device */
106
107 status_t res;
108 String8 deviceName = String8::format("%d", mId);
109
110 camera3_device_t *device;
111
Zhijun He213ce792013-11-19 08:45:15 -0800112 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800113 res = module->open(deviceName.string(),
114 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800115 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800116
117 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700118 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800119 return res;
120 }
121
122 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700123 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700124 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700125 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700126 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800127 device->common.version);
128 device->common.close(&device->common);
129 return BAD_VALUE;
130 }
131
132 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800133 res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700134 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800135 if (res != OK) return res;
136
137 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700138 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
139 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700140 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800141 device->common.close(&device->common);
142 return BAD_VALUE;
143 }
144
145 /** Initialize device with callback functions */
146
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700147 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800148 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700149 ATRACE_END();
150
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700152 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
153 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800154 device->common.close(&device->common);
155 return BAD_VALUE;
156 }
157
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700158 /** Start up status tracker thread */
159 mStatusTracker = new StatusTracker(this);
160 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
161 if (res != OK) {
162 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
163 strerror(-res), res);
164 device->common.close(&device->common);
165 mStatusTracker.clear();
166 return res;
167 }
168
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700169 bool aeLockAvailable = false;
170 camera_metadata_ro_entry aeLockAvailableEntry;
171 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
172 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
173 if (res == OK && aeLockAvailableEntry.count > 0) {
174 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
175 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
176 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800177
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700178 /** Start up request queue thread */
179 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800180 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800181 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700182 SET_ERR_L("Unable to start request queue thread: %s (%d)",
183 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800184 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800185 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800186 return res;
187 }
188
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700189 mPreparerThread = new PreparerThread();
190
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800191 /** Everything is good to go */
192
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700193 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800194 mDeviceInfo = info.static_camera_characteristics;
195 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700196
197 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800198 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700199 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700200 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700201 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800202
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700203 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700204 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
205 camera_metadata_entry partialResultsCount =
206 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
207 if (partialResultsCount.count > 0) {
208 mNumPartialResults = partialResultsCount.data.i32[0];
209 mUsePartialResult = (mNumPartialResults > 1);
210 }
211 } else {
212 camera_metadata_entry partialResultsQuirk =
213 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
214 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
215 mUsePartialResult = true;
216 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700217 }
218
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700219 camera_metadata_entry configs =
220 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
221 for (uint32_t i = 0; i < configs.count; i += 4) {
222 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
223 configs.data.i32[i + 3] ==
224 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
225 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
226 configs.data.i32[i + 2]));
227 }
228 }
229
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800230 return OK;
231}
232
233status_t Camera3Device::disconnect() {
234 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700235 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800236
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800237 ALOGV("%s: E", __FUNCTION__);
238
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700239 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800240
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700241 {
242 Mutex::Autolock l(mLock);
243 if (mStatus == STATUS_UNINITIALIZED) return res;
244
245 if (mStatus == STATUS_ACTIVE ||
246 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
247 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700248 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700249 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700250 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700251 } else {
252 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
253 if (res != OK) {
254 SET_ERR_L("Timeout waiting for HAL to drain");
255 // Continue to close device even in case of error
256 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700257 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800258 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800259
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700260 if (mStatus == STATUS_ERROR) {
261 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700262 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700263
264 if (mStatusTracker != NULL) {
265 mStatusTracker->requestExit();
266 }
267
268 if (mRequestThread != NULL) {
269 mRequestThread->requestExit();
270 }
271
272 mOutputStreams.clear();
273 mInputStream.clear();
274 }
275
276 // Joining done without holding mLock, otherwise deadlocks may ensue
277 // as the threads try to access parent state
278 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
279 // HAL may be in a bad state, so waiting for request thread
280 // (which may be stuck in the HAL processCaptureRequest call)
281 // could be dangerous.
282 mRequestThread->join();
283 }
284
285 if (mStatusTracker != NULL) {
286 mStatusTracker->join();
287 }
288
289 {
290 Mutex::Autolock l(mLock);
291
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800292 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700293 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800294
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700295 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800296 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700297 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800298 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700299 mHal3Device = NULL;
300 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800301
Ruben Brunk183f0562015-08-12 12:55:02 -0700302 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700303 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800304
305 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700306 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800307}
308
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700309// For dumping/debugging only -
310// try to acquire a lock a few times, eventually give up to proceed with
311// debug/dump operations
312bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
313 bool gotLock = false;
314 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
315 if (lock.tryLock() == NO_ERROR) {
316 gotLock = true;
317 break;
318 } else {
319 usleep(kDumpSleepDuration);
320 }
321 }
322 return gotLock;
323}
324
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700325Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
326 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
327 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
328 const int STREAM_CONFIGURATION_SIZE = 4;
329 const int STREAM_FORMAT_OFFSET = 0;
330 const int STREAM_WIDTH_OFFSET = 1;
331 const int STREAM_HEIGHT_OFFSET = 2;
332 const int STREAM_IS_INPUT_OFFSET = 3;
333 camera_metadata_ro_entry_t availableStreamConfigs =
334 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
335 if (availableStreamConfigs.count == 0 ||
336 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
337 return Size(0, 0);
338 }
339
340 // Get max jpeg size (area-wise).
341 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
342 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
343 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
344 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
345 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
346 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
347 && format == HAL_PIXEL_FORMAT_BLOB &&
348 (width * height > maxJpegWidth * maxJpegHeight)) {
349 maxJpegWidth = width;
350 maxJpegHeight = height;
351 }
352 }
353 } else {
354 camera_metadata_ro_entry availableJpegSizes =
355 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
356 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
357 return Size(0, 0);
358 }
359
360 // Get max jpeg size (area-wise).
361 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
362 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
363 > (maxJpegWidth * maxJpegHeight)) {
364 maxJpegWidth = availableJpegSizes.data.i32[i];
365 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
366 }
367 }
368 }
369 return Size(maxJpegWidth, maxJpegHeight);
370}
371
Zhijun Hef7da0962014-04-24 13:27:56 -0700372ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700373 // Get max jpeg size (area-wise).
374 Size maxJpegResolution = getMaxJpegResolution();
375 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700376 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700377 __FUNCTION__, mId);
378 return BAD_VALUE;
379 }
380
Zhijun Hef7da0962014-04-24 13:27:56 -0700381 // Get max jpeg buffer size
382 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700383 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
384 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700385 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
386 return BAD_VALUE;
387 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700388 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800389 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700390
391 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700392 float scaleFactor = ((float) (width * height)) /
393 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800394 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
395 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700396 if (jpegBufferSize > maxJpegBufferSize) {
397 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700398 }
399
400 return jpegBufferSize;
401}
402
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700403ssize_t Camera3Device::getPointCloudBufferSize() const {
404 const int FLOATS_PER_POINT=4;
405 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
406 if (maxPointCount.count == 0) {
407 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
408 __FUNCTION__, mId);
409 return BAD_VALUE;
410 }
411 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
412 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
413 return maxBytesForPointCloud;
414}
415
416
417
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800418status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
419 ATRACE_CALL();
420 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700421
422 // Try to lock, but continue in case of failure (to avoid blocking in
423 // deadlocks)
424 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
425 bool gotLock = tryLockSpinRightRound(mLock);
426
427 ALOGW_IF(!gotInterfaceLock,
428 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
429 mId, __FUNCTION__);
430 ALOGW_IF(!gotLock,
431 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
432 mId, __FUNCTION__);
433
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800434 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800435
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800436 const char *status =
437 mStatus == STATUS_ERROR ? "ERROR" :
438 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700439 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
440 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800441 mStatus == STATUS_ACTIVE ? "ACTIVE" :
442 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700443
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800444 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700445 if (mStatus == STATUS_ERROR) {
446 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
447 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800448 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700449 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700450 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800451
452 if (mInputStream != NULL) {
453 write(fd, lines.string(), lines.size());
454 mInputStream->dump(fd, args);
455 } else {
456 lines.appendFormat(" No input stream.\n");
457 write(fd, lines.string(), lines.size());
458 }
459 for (size_t i = 0; i < mOutputStreams.size(); i++) {
460 mOutputStreams[i]->dump(fd,args);
461 }
462
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700463 lines = String8(" In-flight requests:\n");
464 if (mInFlightMap.size() == 0) {
465 lines.append(" None\n");
466 } else {
467 for (size_t i = 0; i < mInFlightMap.size(); i++) {
468 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700469 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700470 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800471 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700472 r.numBuffersLeft);
473 }
474 }
475 write(fd, lines.string(), lines.size());
476
Igor Murashkin1e479c02013-09-06 16:55:14 -0700477 {
478 lines = String8(" Last request sent:\n");
479 write(fd, lines.string(), lines.size());
480
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700481 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700482 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
483 }
484
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800485 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700486 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800487 write(fd, lines.string(), lines.size());
488 mHal3Device->ops->dump(mHal3Device, fd);
489 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800490
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700491 if (gotLock) mLock.unlock();
492 if (gotInterfaceLock) mInterfaceLock.unlock();
493
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800494 return OK;
495}
496
497const CameraMetadata& Camera3Device::info() const {
498 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800499 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
500 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700501 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800502 mStatus == STATUS_ERROR ?
503 "when in error state" : "before init");
504 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800505 return mDeviceInfo;
506}
507
Jianing Wei90e59c92014-03-12 18:29:36 -0700508status_t Camera3Device::checkStatusOkToCaptureLocked() {
509 switch (mStatus) {
510 case STATUS_ERROR:
511 CLOGE("Device has encountered a serious error");
512 return INVALID_OPERATION;
513 case STATUS_UNINITIALIZED:
514 CLOGE("Device not initialized");
515 return INVALID_OPERATION;
516 case STATUS_UNCONFIGURED:
517 case STATUS_CONFIGURED:
518 case STATUS_ACTIVE:
519 // OK
520 break;
521 default:
522 SET_ERR_L("Unexpected status: %d", mStatus);
523 return INVALID_OPERATION;
524 }
525 return OK;
526}
527
528status_t Camera3Device::convertMetadataListToRequestListLocked(
529 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
530 if (requestList == NULL) {
531 CLOGE("requestList cannot be NULL.");
532 return BAD_VALUE;
533 }
534
Jianing Weicb0652e2014-03-12 18:29:36 -0700535 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700536 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
537 it != metadataList.end(); ++it) {
538 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
539 if (newRequest == 0) {
540 CLOGE("Can't create capture request");
541 return BAD_VALUE;
542 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700543
544 // Setup burst Id and request Id
545 newRequest->mResultExtras.burstId = burstId++;
546 if (it->exists(ANDROID_REQUEST_ID)) {
547 if (it->find(ANDROID_REQUEST_ID).count == 0) {
548 CLOGE("RequestID entry exists; but must not be empty in metadata");
549 return BAD_VALUE;
550 }
551 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
552 } else {
553 CLOGE("RequestID does not exist in metadata");
554 return BAD_VALUE;
555 }
556
Jianing Wei90e59c92014-03-12 18:29:36 -0700557 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700558
559 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700560 }
561 return OK;
562}
563
Jianing Weicb0652e2014-03-12 18:29:36 -0700564status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800565 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800566
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700567 List<const CameraMetadata> requests;
568 requests.push_back(request);
569 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800570}
571
Jianing Wei90e59c92014-03-12 18:29:36 -0700572status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700573 const List<const CameraMetadata> &requests, bool repeating,
574 /*out*/
575 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700576 ATRACE_CALL();
577 Mutex::Autolock il(mInterfaceLock);
578 Mutex::Autolock l(mLock);
579
580 status_t res = checkStatusOkToCaptureLocked();
581 if (res != OK) {
582 // error logged by previous call
583 return res;
584 }
585
586 RequestList requestList;
587
588 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
589 if (res != OK) {
590 // error logged by previous call
591 return res;
592 }
593
594 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700595 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700596 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700597 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700598 }
599
600 if (res == OK) {
601 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
602 if (res != OK) {
603 SET_ERR_L("Can't transition to active in %f seconds!",
604 kActiveTimeout/1e9);
605 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700606 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
607 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700608 } else {
609 CLOGE("Cannot queue request. Impossible.");
610 return BAD_VALUE;
611 }
612
613 return res;
614}
615
Jianing Weicb0652e2014-03-12 18:29:36 -0700616status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
617 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700618 ATRACE_CALL();
619
Jianing Weicb0652e2014-03-12 18:29:36 -0700620 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700621}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800622
Jianing Weicb0652e2014-03-12 18:29:36 -0700623status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
624 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800625 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800626
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700627 List<const CameraMetadata> requests;
628 requests.push_back(request);
629 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800630}
631
Jianing Weicb0652e2014-03-12 18:29:36 -0700632status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
633 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700634 ATRACE_CALL();
635
Jianing Weicb0652e2014-03-12 18:29:36 -0700636 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700637}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800638
639sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
640 const CameraMetadata &request) {
641 status_t res;
642
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700643 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800644 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700645 // Stream configuration failed due to unsupported configuration.
646 // Device back to unconfigured state. Client might try other configuraitons
647 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
648 CLOGE("No streams configured");
649 return NULL;
650 }
651 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800652 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700653 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800654 return NULL;
655 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700656 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700657 if (mStatus == STATUS_UNCONFIGURED) {
658 CLOGE("No streams configured");
659 return NULL;
660 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800661 }
662
663 sp<CaptureRequest> newRequest = createCaptureRequest(request);
664 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800665}
666
Jianing Weicb0652e2014-03-12 18:29:36 -0700667status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800668 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700669 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800670 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800671
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800672 switch (mStatus) {
673 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700674 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800675 return INVALID_OPERATION;
676 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700677 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800678 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700679 case STATUS_UNCONFIGURED:
680 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800681 case STATUS_ACTIVE:
682 // OK
683 break;
684 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700685 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800686 return INVALID_OPERATION;
687 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700688 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700689
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700690 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800691}
692
693status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
694 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700695 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800696
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700697 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800698}
699
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700700status_t Camera3Device::createInputStream(
701 uint32_t width, uint32_t height, int format, int *id) {
702 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700703 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700704 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700705 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
706 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700707
708 status_t res;
709 bool wasActive = false;
710
711 switch (mStatus) {
712 case STATUS_ERROR:
713 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
714 return INVALID_OPERATION;
715 case STATUS_UNINITIALIZED:
716 ALOGE("%s: Device not initialized", __FUNCTION__);
717 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700718 case STATUS_UNCONFIGURED:
719 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700720 // OK
721 break;
722 case STATUS_ACTIVE:
723 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700724 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700725 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700726 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700727 return res;
728 }
729 wasActive = true;
730 break;
731 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700732 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700733 return INVALID_OPERATION;
734 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700735 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700736
737 if (mInputStream != 0) {
738 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
739 return INVALID_OPERATION;
740 }
741
742 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
743 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700744 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700745
746 mInputStream = newStream;
747
748 *id = mNextStreamId++;
749
750 // Continue captures if active at start
751 if (wasActive) {
752 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
753 res = configureStreamsLocked();
754 if (res != OK) {
755 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
756 __FUNCTION__, mNextStreamId, strerror(-res), res);
757 return res;
758 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700759 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700760 }
761
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700762 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700763 return OK;
764}
765
Igor Murashkin2fba5842013-04-22 14:03:54 -0700766
767status_t Camera3Device::createZslStream(
768 uint32_t width, uint32_t height,
769 int depth,
770 /*out*/
771 int *id,
772 sp<Camera3ZslStream>* zslStream) {
773 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700774 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700775 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700776 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
777 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700778
779 status_t res;
780 bool wasActive = false;
781
782 switch (mStatus) {
783 case STATUS_ERROR:
784 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
785 return INVALID_OPERATION;
786 case STATUS_UNINITIALIZED:
787 ALOGE("%s: Device not initialized", __FUNCTION__);
788 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700789 case STATUS_UNCONFIGURED:
790 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700791 // OK
792 break;
793 case STATUS_ACTIVE:
794 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700795 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700796 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700797 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700798 return res;
799 }
800 wasActive = true;
801 break;
802 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700803 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700804 return INVALID_OPERATION;
805 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700806 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700807
808 if (mInputStream != 0) {
809 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
810 return INVALID_OPERATION;
811 }
812
813 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
814 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700815 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700816
817 res = mOutputStreams.add(mNextStreamId, newStream);
818 if (res < 0) {
819 ALOGE("%s: Can't add new stream to set: %s (%d)",
820 __FUNCTION__, strerror(-res), res);
821 return res;
822 }
823 mInputStream = newStream;
824
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530825 mNeedConfig = true;
826
Igor Murashkin2fba5842013-04-22 14:03:54 -0700827 *id = mNextStreamId++;
828 *zslStream = newStream;
829
830 // Continue captures if active at start
831 if (wasActive) {
832 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
833 res = configureStreamsLocked();
834 if (res != OK) {
835 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
836 __FUNCTION__, mNextStreamId, strerror(-res), res);
837 return res;
838 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700839 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700840 }
841
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700842 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700843 return OK;
844}
845
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700846status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800847 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700848 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800849 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700850 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800851 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700852 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
853 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800854
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800855 status_t res;
856 bool wasActive = false;
857
858 switch (mStatus) {
859 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700860 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800861 return INVALID_OPERATION;
862 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700863 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800864 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700865 case STATUS_UNCONFIGURED:
866 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800867 // OK
868 break;
869 case STATUS_ACTIVE:
870 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700871 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800872 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700873 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800874 return res;
875 }
876 wasActive = true;
877 break;
878 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700879 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800880 return INVALID_OPERATION;
881 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700882 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800883
884 sp<Camera3OutputStream> newStream;
885 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700886 ssize_t blobBufferSize;
887 if (dataSpace != HAL_DATASPACE_DEPTH) {
888 blobBufferSize = getJpegBufferSize(width, height);
889 if (blobBufferSize <= 0) {
890 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
891 return BAD_VALUE;
892 }
893 } else {
894 blobBufferSize = getPointCloudBufferSize();
895 if (blobBufferSize <= 0) {
896 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
897 return BAD_VALUE;
898 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700899 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800900 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700901 width, height, blobBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800902 } else {
903 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700904 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800905 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700906 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800907
908 res = mOutputStreams.add(mNextStreamId, newStream);
909 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700910 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800911 return res;
912 }
913
914 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700915 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800916
917 // Continue captures if active at start
918 if (wasActive) {
919 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
920 res = configureStreamsLocked();
921 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700922 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
923 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800924 return res;
925 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700926 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800927 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700928 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800929 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800930}
931
932status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
933 ATRACE_CALL();
934 (void)outputId; (void)id;
935
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700936 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800937 return INVALID_OPERATION;
938}
939
940
941status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700942 uint32_t *width, uint32_t *height,
943 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800944 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700945 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800946 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800947
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800948 switch (mStatus) {
949 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700950 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800951 return INVALID_OPERATION;
952 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700953 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800954 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700955 case STATUS_UNCONFIGURED:
956 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800957 case STATUS_ACTIVE:
958 // OK
959 break;
960 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700961 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800962 return INVALID_OPERATION;
963 }
964
965 ssize_t idx = mOutputStreams.indexOfKey(id);
966 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700967 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800968 return idx;
969 }
970
971 if (width) *width = mOutputStreams[idx]->getWidth();
972 if (height) *height = mOutputStreams[idx]->getHeight();
973 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700974 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800975 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800976}
977
978status_t Camera3Device::setStreamTransform(int id,
979 int transform) {
980 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700981 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800982 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800983
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800984 switch (mStatus) {
985 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700986 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800987 return INVALID_OPERATION;
988 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700989 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800990 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700991 case STATUS_UNCONFIGURED:
992 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800993 case STATUS_ACTIVE:
994 // OK
995 break;
996 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700997 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800998 return INVALID_OPERATION;
999 }
1000
1001 ssize_t idx = mOutputStreams.indexOfKey(id);
1002 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001003 CLOGE("Stream %d does not exist",
1004 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001005 return BAD_VALUE;
1006 }
1007
1008 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001009}
1010
1011status_t Camera3Device::deleteStream(int id) {
1012 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001013 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001014 Mutex::Autolock l(mLock);
1015 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001016
Igor Murashkine2172be2013-05-28 15:31:39 -07001017 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1018
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001019 // CameraDevice semantics require device to already be idle before
1020 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001021 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001022 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1023 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001024 }
1025
Igor Murashkin2fba5842013-04-22 14:03:54 -07001026 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001027 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001028 if (mInputStream != NULL && id == mInputStream->getId()) {
1029 deletedStream = mInputStream;
1030 mInputStream.clear();
1031 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001032 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001033 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001034 return BAD_VALUE;
1035 }
Zhijun He5f446352014-01-22 09:49:33 -08001036 }
1037
1038 // Delete output stream or the output part of a bi-directional stream.
1039 if (outputStreamIdx != NAME_NOT_FOUND) {
1040 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001041 mOutputStreams.removeItem(id);
1042 }
1043
1044 // Free up the stream endpoint so that it can be used by some other stream
1045 res = deletedStream->disconnect();
1046 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001047 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001048 // fall through since we want to still list the stream as deleted.
1049 }
1050 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001051 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001052
1053 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001054}
1055
1056status_t Camera3Device::deleteReprocessStream(int id) {
1057 ATRACE_CALL();
1058 (void)id;
1059
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001060 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001061 return INVALID_OPERATION;
1062}
1063
Zhijun He1fa89992015-06-01 15:44:31 -07001064status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001065 ATRACE_CALL();
1066 ALOGV("%s: E", __FUNCTION__);
1067
1068 Mutex::Autolock il(mInterfaceLock);
1069 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001070
1071 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1072 mNeedConfig = true;
1073 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1074 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001075
1076 return configureStreamsLocked();
1077}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001078
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001079status_t Camera3Device::getInputBufferProducer(
1080 sp<IGraphicBufferProducer> *producer) {
1081 Mutex::Autolock il(mInterfaceLock);
1082 Mutex::Autolock l(mLock);
1083
1084 if (producer == NULL) {
1085 return BAD_VALUE;
1086 } else if (mInputStream == NULL) {
1087 return INVALID_OPERATION;
1088 }
1089
1090 return mInputStream->getInputBufferProducer(producer);
1091}
1092
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001093status_t Camera3Device::createDefaultRequest(int templateId,
1094 CameraMetadata *request) {
1095 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001096 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen116a1892016-03-09 12:21:01 -08001097
1098 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1099 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1100 IPCThreadState::self()->getCallingUid(), NULL, 0);
1101 return BAD_VALUE;
1102 }
1103
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001104 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001105 Mutex::Autolock l(mLock);
1106
1107 switch (mStatus) {
1108 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001109 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001110 return INVALID_OPERATION;
1111 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001112 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001113 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001114 case STATUS_UNCONFIGURED:
1115 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001116 case STATUS_ACTIVE:
1117 // OK
1118 break;
1119 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001120 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001121 return INVALID_OPERATION;
1122 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001123
Zhijun Hea1530f12014-09-14 12:44:20 -07001124 if (!mRequestTemplateCache[templateId].isEmpty()) {
1125 *request = mRequestTemplateCache[templateId];
1126 return OK;
1127 }
1128
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001129 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001130 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001131 rawRequest = mHal3Device->ops->construct_default_request_settings(
1132 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001133 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001134 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001135 ALOGI("%s: template %d is not supported on this camera device",
1136 __FUNCTION__, templateId);
1137 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001138 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001139 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001140 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001141
1142 return OK;
1143}
1144
1145status_t Camera3Device::waitUntilDrained() {
1146 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001147 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001148 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001149
Zhijun He69a37482014-03-23 18:44:49 -07001150 return waitUntilDrainedLocked();
1151}
1152
1153status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001154 switch (mStatus) {
1155 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001156 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001157 ALOGV("%s: Already idle", __FUNCTION__);
1158 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001159 case STATUS_CONFIGURED:
1160 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001161 case STATUS_ERROR:
1162 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001163 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001164 break;
1165 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001166 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001167 return INVALID_OPERATION;
1168 }
1169
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001170 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1171 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001172 if (res != OK) {
1173 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1174 res);
1175 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001176 return res;
1177}
1178
Ruben Brunk183f0562015-08-12 12:55:02 -07001179
1180void Camera3Device::internalUpdateStatusLocked(Status status) {
1181 mStatus = status;
1182 mRecentStatusUpdates.add(mStatus);
1183 mStatusChanged.broadcast();
1184}
1185
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001186// Pause to reconfigure
1187status_t Camera3Device::internalPauseAndWaitLocked() {
1188 mRequestThread->setPaused(true);
1189 mPauseStateNotify = true;
1190
1191 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1192 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1193 if (res != OK) {
1194 SET_ERR_L("Can't idle device in %f seconds!",
1195 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001196 }
1197
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001198 return res;
1199}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001200
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001201// Resume after internalPauseAndWaitLocked
1202status_t Camera3Device::internalResumeLocked() {
1203 status_t res;
1204
1205 mRequestThread->setPaused(false);
1206
1207 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1208 if (res != OK) {
1209 SET_ERR_L("Can't transition to active in %f seconds!",
1210 kActiveTimeout/1e9);
1211 }
1212 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001213 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001214}
1215
Ruben Brunk183f0562015-08-12 12:55:02 -07001216status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001217 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001218
1219 size_t startIndex = 0;
1220 if (mStatusWaiters == 0) {
1221 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1222 // this status list
1223 mRecentStatusUpdates.clear();
1224 } else {
1225 // If other threads are waiting on updates to this status list, set the position of the
1226 // first element that this list will check rather than clearing the list.
1227 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001228 }
1229
Ruben Brunk183f0562015-08-12 12:55:02 -07001230 mStatusWaiters++;
1231
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001232 bool stateSeen = false;
1233 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001234 if (active == (mStatus == STATUS_ACTIVE)) {
1235 // Desired state is current
1236 break;
1237 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001238
1239 res = mStatusChanged.waitRelative(mLock, timeout);
1240 if (res != OK) break;
1241
Ruben Brunk183f0562015-08-12 12:55:02 -07001242 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1243 // transitions.
1244 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1245 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1246 __FUNCTION__);
1247
1248 // Encountered desired state since we began waiting
1249 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001250 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1251 stateSeen = true;
1252 break;
1253 }
1254 }
1255 } while (!stateSeen);
1256
Ruben Brunk183f0562015-08-12 12:55:02 -07001257 mStatusWaiters--;
1258
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001259 return res;
1260}
1261
1262
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001263status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1264 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001265 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001266
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001267 if (listener != NULL && mListener != NULL) {
1268 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1269 }
1270 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001271 mRequestThread->setNotificationListener(listener);
1272 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001273
1274 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001275}
1276
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001277bool Camera3Device::willNotify3A() {
1278 return false;
1279}
1280
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001281status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001282 status_t res;
1283 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001284
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001285 while (mResultQueue.empty()) {
1286 res = mResultSignal.waitRelative(mOutputLock, timeout);
1287 if (res == TIMED_OUT) {
1288 return res;
1289 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001290 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001291 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001292 return res;
1293 }
1294 }
1295 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001296}
1297
Jianing Weicb0652e2014-03-12 18:29:36 -07001298status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001299 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001300 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001301
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001302 if (mResultQueue.empty()) {
1303 return NOT_ENOUGH_DATA;
1304 }
1305
Jianing Weicb0652e2014-03-12 18:29:36 -07001306 if (frame == NULL) {
1307 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1308 return BAD_VALUE;
1309 }
1310
1311 CaptureResult &result = *(mResultQueue.begin());
1312 frame->mResultExtras = result.mResultExtras;
1313 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001314 mResultQueue.erase(mResultQueue.begin());
1315
1316 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001317}
1318
1319status_t Camera3Device::triggerAutofocus(uint32_t id) {
1320 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001321 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001322
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001323 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1324 // Mix-in this trigger into the next request and only the next request.
1325 RequestTrigger trigger[] = {
1326 {
1327 ANDROID_CONTROL_AF_TRIGGER,
1328 ANDROID_CONTROL_AF_TRIGGER_START
1329 },
1330 {
1331 ANDROID_CONTROL_AF_TRIGGER_ID,
1332 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001333 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001334 };
1335
1336 return mRequestThread->queueTrigger(trigger,
1337 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001338}
1339
1340status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1341 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001342 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001343
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001344 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1345 // Mix-in this trigger into the next request and only the next request.
1346 RequestTrigger trigger[] = {
1347 {
1348 ANDROID_CONTROL_AF_TRIGGER,
1349 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1350 },
1351 {
1352 ANDROID_CONTROL_AF_TRIGGER_ID,
1353 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001354 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001355 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001356
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001357 return mRequestThread->queueTrigger(trigger,
1358 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001359}
1360
1361status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1362 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001363 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001364
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001365 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1366 // Mix-in this trigger into the next request and only the next request.
1367 RequestTrigger trigger[] = {
1368 {
1369 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1370 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1371 },
1372 {
1373 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1374 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001375 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001376 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001377
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001378 return mRequestThread->queueTrigger(trigger,
1379 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001380}
1381
1382status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1383 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1384 ATRACE_CALL();
1385 (void)reprocessStreamId; (void)buffer; (void)listener;
1386
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001387 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001388 return INVALID_OPERATION;
1389}
1390
Jianing Weicb0652e2014-03-12 18:29:36 -07001391status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001392 ATRACE_CALL();
1393 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001394 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001395
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001396 NotificationListener* listener;
1397 {
1398 Mutex::Autolock l(mOutputLock);
1399 listener = mListener;
1400 }
1401
Zhijun He7ef20392014-04-21 16:04:17 -07001402 {
1403 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001404 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001405 }
1406
Zhijun He491e3412013-12-27 10:57:44 -08001407 status_t res;
1408 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1409 res = mHal3Device->ops->flush(mHal3Device);
1410 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001411 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001412 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001413 }
1414
1415 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001416}
1417
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001418status_t Camera3Device::prepare(int streamId) {
1419 ATRACE_CALL();
1420 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001421 Mutex::Autolock il(mInterfaceLock);
1422 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001423
1424 sp<Camera3StreamInterface> stream;
1425 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1426 if (outputStreamIdx == NAME_NOT_FOUND) {
1427 CLOGE("Stream %d does not exist", streamId);
1428 return BAD_VALUE;
1429 }
1430
1431 stream = mOutputStreams.editValueAt(outputStreamIdx);
1432
1433 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001434 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001435 return BAD_VALUE;
1436 }
1437
1438 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001439 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001440 return BAD_VALUE;
1441 }
1442
1443 return mPreparerThread->prepare(stream);
1444}
1445
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001446status_t Camera3Device::tearDown(int streamId) {
1447 ATRACE_CALL();
1448 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1449 Mutex::Autolock il(mInterfaceLock);
1450 Mutex::Autolock l(mLock);
1451
1452 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1453 // since we cannot call register_stream_buffers except right after configure_streams.
1454 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1455 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1456 __FUNCTION__, mHal3Device->common.version);
1457 return NO_INIT;
1458 }
1459
1460 sp<Camera3StreamInterface> stream;
1461 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1462 if (outputStreamIdx == NAME_NOT_FOUND) {
1463 CLOGE("Stream %d does not exist", streamId);
1464 return BAD_VALUE;
1465 }
1466
1467 stream = mOutputStreams.editValueAt(outputStreamIdx);
1468
1469 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1470 CLOGE("Stream %d is a target of a in-progress request", streamId);
1471 return BAD_VALUE;
1472 }
1473
1474 return stream->tearDown();
1475}
1476
Zhijun He204e3292014-07-14 17:09:23 -07001477uint32_t Camera3Device::getDeviceVersion() {
1478 ATRACE_CALL();
1479 Mutex::Autolock il(mInterfaceLock);
1480 return mDeviceVersion;
1481}
1482
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001483/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001484 * Methods called by subclasses
1485 */
1486
1487void Camera3Device::notifyStatus(bool idle) {
1488 {
1489 // Need mLock to safely update state and synchronize to current
1490 // state of methods in flight.
1491 Mutex::Autolock l(mLock);
1492 // We can get various system-idle notices from the status tracker
1493 // while starting up. Only care about them if we've actually sent
1494 // in some requests recently.
1495 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1496 return;
1497 }
1498 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1499 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001500 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001501
1502 // Skip notifying listener if we're doing some user-transparent
1503 // state changes
1504 if (mPauseStateNotify) return;
1505 }
1506 NotificationListener *listener;
1507 {
1508 Mutex::Autolock l(mOutputLock);
1509 listener = mListener;
1510 }
1511 if (idle && listener != NULL) {
1512 listener->notifyIdle();
1513 }
1514}
1515
1516/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001517 * Camera3Device private methods
1518 */
1519
1520sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1521 const CameraMetadata &request) {
1522 ATRACE_CALL();
1523 status_t res;
1524
1525 sp<CaptureRequest> newRequest = new CaptureRequest;
1526 newRequest->mSettings = request;
1527
1528 camera_metadata_entry_t inputStreams =
1529 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1530 if (inputStreams.count > 0) {
1531 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001532 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001533 CLOGE("Request references unknown input stream %d",
1534 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001535 return NULL;
1536 }
1537 // Lazy completion of stream configuration (allocation/registration)
1538 // on first use
1539 if (mInputStream->isConfiguring()) {
1540 res = mInputStream->finishConfiguration(mHal3Device);
1541 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001542 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001543 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001544 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001545 return NULL;
1546 }
1547 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001548 // Check if stream is being prepared
1549 if (mInputStream->isPreparing()) {
1550 CLOGE("Request references an input stream that's being prepared!");
1551 return NULL;
1552 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001553
1554 newRequest->mInputStream = mInputStream;
1555 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1556 }
1557
1558 camera_metadata_entry_t streams =
1559 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1560 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001561 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001562 return NULL;
1563 }
1564
1565 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001566 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001567 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001568 CLOGE("Request references unknown stream %d",
1569 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001570 return NULL;
1571 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001572 sp<Camera3OutputStreamInterface> stream =
1573 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001574
1575 // Lazy completion of stream configuration (allocation/registration)
1576 // on first use
1577 if (stream->isConfiguring()) {
1578 res = stream->finishConfiguration(mHal3Device);
1579 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001580 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1581 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001582 return NULL;
1583 }
1584 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001585 // Check if stream is being prepared
1586 if (stream->isPreparing()) {
1587 CLOGE("Request references an output stream that's being prepared!");
1588 return NULL;
1589 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001590
1591 newRequest->mOutputStreams.push(stream);
1592 }
1593 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1594
1595 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001596}
1597
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001598bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1599 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1600 Size size = mSupportedOpaqueInputSizes[i];
1601 if (size.width == width && size.height == height) {
1602 return true;
1603 }
1604 }
1605
1606 return false;
1607}
1608
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001609status_t Camera3Device::configureStreamsLocked() {
1610 ATRACE_CALL();
1611 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001612
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001613 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001614 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001615 return INVALID_OPERATION;
1616 }
1617
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001618 if (!mNeedConfig) {
1619 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1620 return OK;
1621 }
1622
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001623 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1624 // adding a dummy stream instead.
1625 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1626 if (mOutputStreams.size() == 0) {
1627 addDummyStreamLocked();
1628 } else {
1629 tryRemoveDummyStreamLocked();
1630 }
1631
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001632 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001633 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001634
1635 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001636 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1637 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1638 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001639 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1640
1641 Vector<camera3_stream_t*> streams;
1642 streams.setCapacity(config.num_streams);
1643
1644 if (mInputStream != NULL) {
1645 camera3_stream_t *inputStream;
1646 inputStream = mInputStream->startConfiguration();
1647 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001648 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001649 return INVALID_OPERATION;
1650 }
1651 streams.add(inputStream);
1652 }
1653
1654 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001655
1656 // Don't configure bidi streams twice, nor add them twice to the list
1657 if (mOutputStreams[i].get() ==
1658 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1659
1660 config.num_streams--;
1661 continue;
1662 }
1663
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001664 camera3_stream_t *outputStream;
1665 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1666 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001667 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001668 return INVALID_OPERATION;
1669 }
1670 streams.add(outputStream);
1671 }
1672
1673 config.streams = streams.editArray();
1674
1675 // Do the HAL configuration; will potentially touch stream
1676 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001677 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001678 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001679 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001680
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001681 if (res == BAD_VALUE) {
1682 // HAL rejected this set of streams as unsupported, clean up config
1683 // attempt and return to unconfigured state
1684 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1685 res = mInputStream->cancelConfiguration();
1686 if (res != OK) {
1687 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1688 mInputStream->getId(), strerror(-res), res);
1689 return res;
1690 }
1691 }
1692
1693 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1694 sp<Camera3OutputStreamInterface> outputStream =
1695 mOutputStreams.editValueAt(i);
1696 if (outputStream->isConfiguring()) {
1697 res = outputStream->cancelConfiguration();
1698 if (res != OK) {
1699 SET_ERR_L(
1700 "Can't cancel configuring output stream %d: %s (%d)",
1701 outputStream->getId(), strerror(-res), res);
1702 return res;
1703 }
1704 }
1705 }
1706
1707 // Return state to that at start of call, so that future configures
1708 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001709 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001710 mNeedConfig = true;
1711
1712 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1713 return BAD_VALUE;
1714 } else if (res != OK) {
1715 // Some other kind of error from configure_streams - this is not
1716 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001717 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1718 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001719 return res;
1720 }
1721
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001722 // Finish all stream configuration immediately.
1723 // TODO: Try to relax this later back to lazy completion, which should be
1724 // faster
1725
Igor Murashkin073f8572013-05-02 14:59:28 -07001726 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001727 res = mInputStream->finishConfiguration(mHal3Device);
1728 if (res != OK) {
1729 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1730 mInputStream->getId(), strerror(-res), res);
1731 return res;
1732 }
1733 }
1734
1735 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001736 sp<Camera3OutputStreamInterface> outputStream =
1737 mOutputStreams.editValueAt(i);
1738 if (outputStream->isConfiguring()) {
1739 res = outputStream->finishConfiguration(mHal3Device);
1740 if (res != OK) {
1741 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1742 outputStream->getId(), strerror(-res), res);
1743 return res;
1744 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001745 }
1746 }
1747
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001748 // Request thread needs to know to avoid using repeat-last-settings protocol
1749 // across configure_streams() calls
1750 mRequestThread->configurationComplete();
1751
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001752 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001753
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001754 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001755
Ruben Brunk183f0562015-08-12 12:55:02 -07001756 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1757 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001758
1759 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1760
Zhijun He0a210512014-07-24 13:45:15 -07001761 // tear down the deleted streams after configure streams.
1762 mDeletedStreams.clear();
1763
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001764 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001765}
1766
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001767status_t Camera3Device::addDummyStreamLocked() {
1768 ATRACE_CALL();
1769 status_t res;
1770
1771 if (mDummyStreamId != NO_STREAM) {
1772 // Should never be adding a second dummy stream when one is already
1773 // active
1774 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1775 __FUNCTION__, mId);
1776 return INVALID_OPERATION;
1777 }
1778
1779 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1780
1781 sp<Camera3OutputStreamInterface> dummyStream =
1782 new Camera3DummyStream(mNextStreamId);
1783
1784 res = mOutputStreams.add(mNextStreamId, dummyStream);
1785 if (res < 0) {
1786 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1787 return res;
1788 }
1789
1790 mDummyStreamId = mNextStreamId;
1791 mNextStreamId++;
1792
1793 return OK;
1794}
1795
1796status_t Camera3Device::tryRemoveDummyStreamLocked() {
1797 ATRACE_CALL();
1798 status_t res;
1799
1800 if (mDummyStreamId == NO_STREAM) return OK;
1801 if (mOutputStreams.size() == 1) return OK;
1802
1803 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1804
1805 // Ok, have a dummy stream and there's at least one other output stream,
1806 // so remove the dummy
1807
1808 sp<Camera3StreamInterface> deletedStream;
1809 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1810 if (outputStreamIdx == NAME_NOT_FOUND) {
1811 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1812 return INVALID_OPERATION;
1813 }
1814
1815 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1816 mOutputStreams.removeItemsAt(outputStreamIdx);
1817
1818 // Free up the stream endpoint so that it can be used by some other stream
1819 res = deletedStream->disconnect();
1820 if (res != OK) {
1821 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1822 // fall through since we want to still list the stream as deleted.
1823 }
1824 mDeletedStreams.add(deletedStream);
1825 mDummyStreamId = NO_STREAM;
1826
1827 return res;
1828}
1829
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001830void Camera3Device::setErrorState(const char *fmt, ...) {
1831 Mutex::Autolock l(mLock);
1832 va_list args;
1833 va_start(args, fmt);
1834
1835 setErrorStateLockedV(fmt, args);
1836
1837 va_end(args);
1838}
1839
1840void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1841 Mutex::Autolock l(mLock);
1842 setErrorStateLockedV(fmt, args);
1843}
1844
1845void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1846 va_list args;
1847 va_start(args, fmt);
1848
1849 setErrorStateLockedV(fmt, args);
1850
1851 va_end(args);
1852}
1853
1854void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001855 // Print out all error messages to log
1856 String8 errorCause = String8::formatV(fmt, args);
1857 ALOGE("Camera %d: %s", mId, errorCause.string());
1858
1859 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001860 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001861
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001862 mErrorCause = errorCause;
1863
1864 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07001865 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001866
1867 // Notify upstream about a device error
1868 if (mListener != NULL) {
1869 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1870 CaptureResultExtras());
1871 }
1872
1873 // Save stack trace. View by dumping it later.
1874 CameraTraces::saveTrace();
1875 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001876}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001877
1878/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001879 * In-flight request management
1880 */
1881
Jianing Weicb0652e2014-03-12 18:29:36 -07001882status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07001883 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1884 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001885 ATRACE_CALL();
1886 Mutex::Autolock l(mInFlightLock);
1887
1888 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07001889 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
1890 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001891 if (res < 0) return res;
1892
1893 return OK;
1894}
1895
1896/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001897 * Check if all 3A fields are ready, and send off a partial 3A-only result
1898 * to the output frame queue
1899 */
Zhijun He204e3292014-07-14 17:09:23 -07001900bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001901 uint32_t frameNumber,
1902 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001903
1904 // Check if all 3A states are present
1905 // The full list of fields is
1906 // android.control.afMode
1907 // android.control.awbMode
1908 // android.control.aeState
1909 // android.control.awbState
1910 // android.control.afState
1911 // android.control.afTriggerID
1912 // android.control.aePrecaptureID
1913 // TODO: Add android.control.aeMode
1914
1915 bool gotAllStates = true;
1916
1917 uint8_t afMode;
1918 uint8_t awbMode;
1919 uint8_t aeState;
1920 uint8_t afState;
1921 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001922
1923 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1924 &afMode, frameNumber);
1925
1926 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1927 &awbMode, frameNumber);
1928
1929 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1930 &aeState, frameNumber);
1931
1932 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1933 &afState, frameNumber);
1934
1935 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1936 &awbState, frameNumber);
1937
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001938 if (!gotAllStates) return false;
1939
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001940 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001941 "AF state %d, AE state %d, AWB state %d, "
1942 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001943 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001944 afMode, awbMode,
1945 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001946 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001947
1948 // Got all states, so construct a minimal result to send
1949 // In addition to the above fields, this means adding in
1950 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001951 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001952 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001953
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001954 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001955
1956 Mutex::Autolock l(mOutputLock);
1957
Jianing Weicb0652e2014-03-12 18:29:36 -07001958 CaptureResult captureResult;
1959 captureResult.mResultExtras = resultExtras;
1960 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1961 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1962 // but not limited to CameraDeviceBase::getNextResult
1963 CaptureResult& min3AResult =
1964 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001965
Jianing Weicb0652e2014-03-12 18:29:36 -07001966 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1967 // TODO: This is problematic casting. Need to fix CameraMetadata.
1968 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001969 return false;
1970 }
1971
Jianing Weicb0652e2014-03-12 18:29:36 -07001972 int32_t requestId = resultExtras.requestId;
1973 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001974 &requestId, frameNumber)) {
1975 return false;
1976 }
1977
Zhijun He204e3292014-07-14 17:09:23 -07001978 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1979 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1980 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1981 &partialResult, frameNumber)) {
1982 return false;
1983 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001984 }
1985
Jianing Weicb0652e2014-03-12 18:29:36 -07001986 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001987 &afMode, frameNumber)) {
1988 return false;
1989 }
1990
Jianing Weicb0652e2014-03-12 18:29:36 -07001991 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001992 &awbMode, frameNumber)) {
1993 return false;
1994 }
1995
Jianing Weicb0652e2014-03-12 18:29:36 -07001996 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001997 &aeState, frameNumber)) {
1998 return false;
1999 }
2000
Jianing Weicb0652e2014-03-12 18:29:36 -07002001 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002002 &afState, frameNumber)) {
2003 return false;
2004 }
2005
Jianing Weicb0652e2014-03-12 18:29:36 -07002006 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002007 &awbState, frameNumber)) {
2008 return false;
2009 }
2010
Jianing Weicb0652e2014-03-12 18:29:36 -07002011 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002012 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002013 return false;
2014 }
2015
Jianing Weicb0652e2014-03-12 18:29:36 -07002016 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002017 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002018 return false;
2019 }
2020
Zhijun He204e3292014-07-14 17:09:23 -07002021 // We only send the aggregated partial when all 3A related metadata are available
2022 // For both API1 and API2.
2023 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002024 mResultSignal.signal();
2025
2026 return true;
2027}
2028
2029template<typename T>
2030bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002031 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002032 (void) frameNumber;
2033
2034 camera_metadata_ro_entry_t entry;
2035
2036 entry = result.find(tag);
2037 if (entry.count == 0) {
2038 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2039 mId, frameNumber, get_camera_metadata_tag_name(tag));
2040 return false;
2041 }
2042
2043 if (sizeof(T) == sizeof(uint8_t)) {
2044 *value = entry.data.u8[0];
2045 } else if (sizeof(T) == sizeof(int32_t)) {
2046 *value = entry.data.i32[0];
2047 } else {
2048 ALOGE("%s: Unexpected type", __FUNCTION__);
2049 return false;
2050 }
2051 return true;
2052}
2053
2054template<typename T>
2055bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002056 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002057 if (result.update(tag, value, 1) != NO_ERROR) {
2058 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2059 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2060 frameNumber, get_camera_metadata_tag_name(tag));
2061 return false;
2062 }
2063 return true;
2064}
2065
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002066void Camera3Device::returnOutputBuffers(
2067 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2068 nsecs_t timestamp) {
2069 for (size_t i = 0; i < numBuffers; i++)
2070 {
2071 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2072 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2073 // Note: stream may be deallocated at this point, if this buffer was
2074 // the last reference to it.
2075 if (res != OK) {
2076 ALOGE("Can't return buffer to its stream: %s (%d)",
2077 strerror(-res), res);
2078 }
2079 }
2080}
2081
2082
2083void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2084
2085 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2086 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2087
2088 nsecs_t sensorTimestamp = request.sensorTimestamp;
2089 nsecs_t shutterTimestamp = request.shutterTimestamp;
2090
2091 // Check if it's okay to remove the request from InFlightMap:
2092 // In the case of a successful request:
2093 // all input and output buffers, all result metadata, shutter callback
2094 // arrived.
2095 // In the case of a unsuccessful request:
2096 // all input and output buffers arrived.
2097 if (request.numBuffersLeft == 0 &&
2098 (request.requestStatus != OK ||
2099 (request.haveResultMetadata && shutterTimestamp != 0))) {
2100 ATRACE_ASYNC_END("frame capture", frameNumber);
2101
2102 // Sanity check - if sensor timestamp matches shutter timestamp
2103 if (request.requestStatus == OK &&
2104 sensorTimestamp != shutterTimestamp) {
2105 SET_ERR("sensor timestamp (%" PRId64
2106 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2107 sensorTimestamp, frameNumber, shutterTimestamp);
2108 }
2109
2110 // for an unsuccessful request, it may have pending output buffers to
2111 // return.
2112 assert(request.requestStatus != OK ||
2113 request.pendingOutputBuffers.size() == 0);
2114 returnOutputBuffers(request.pendingOutputBuffers.array(),
2115 request.pendingOutputBuffers.size(), 0);
2116
2117 mInFlightMap.removeItemsAt(idx, 1);
2118
2119 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2120 }
2121
2122 // Sanity check - if we have too many in-flight frames, something has
2123 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002124 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002125 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002126 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2127 kInFlightWarnLimitHighSpeed) {
2128 CLOGE("In-flight list too large for high speed configuration: %zu",
2129 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002130 }
2131}
2132
2133
2134void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2135 CaptureResultExtras &resultExtras,
2136 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002137 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002138 bool reprocess,
2139 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002140 if (pendingMetadata.isEmpty())
2141 return;
2142
2143 Mutex::Autolock l(mOutputLock);
2144
2145 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002146 if (reprocess) {
2147 if (frameNumber < mNextReprocessResultFrameNumber) {
2148 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002149 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002150 frameNumber, mNextReprocessResultFrameNumber);
2151 return;
2152 }
2153 mNextReprocessResultFrameNumber = frameNumber + 1;
2154 } else {
2155 if (frameNumber < mNextResultFrameNumber) {
2156 SET_ERR("Out-of-order capture result metadata submitted! "
2157 "(got frame number %d, expecting %d)",
2158 frameNumber, mNextResultFrameNumber);
2159 return;
2160 }
2161 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002162 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002163
2164 CaptureResult captureResult;
2165 captureResult.mResultExtras = resultExtras;
2166 captureResult.mMetadata = pendingMetadata;
2167
2168 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2169 (int32_t*)&frameNumber, 1) != OK) {
2170 SET_ERR("Failed to set frame# in metadata (%d)",
2171 frameNumber);
2172 return;
2173 } else {
2174 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2175 __FUNCTION__, mId, frameNumber);
2176 }
2177
2178 // Append any previous partials to form a complete result
2179 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2180 captureResult.mMetadata.append(collectedPartialResult);
2181 }
2182
2183 captureResult.mMetadata.sort();
2184
2185 // Check that there's a timestamp in the result metadata
2186 camera_metadata_entry entry =
2187 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2188 if (entry.count == 0) {
2189 SET_ERR("No timestamp provided by HAL for frame %d!",
2190 frameNumber);
2191 return;
2192 }
2193
Chien-Yu Chend196d612015-06-22 19:49:01 -07002194 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2195
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002196 // Valid result, insert into queue
2197 List<CaptureResult>::iterator queuedResult =
2198 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2199 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2200 ", burstId = %" PRId32, __FUNCTION__,
2201 queuedResult->mResultExtras.requestId,
2202 queuedResult->mResultExtras.frameNumber,
2203 queuedResult->mResultExtras.burstId);
2204
2205 mResultSignal.signal();
2206}
2207
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002208/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002209 * Camera HAL device callback methods
2210 */
2211
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002212void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002213 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002214
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002215 status_t res;
2216
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002217 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002218 if (result->result == NULL && result->num_output_buffers == 0 &&
2219 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002220 SET_ERR("No result data provided by HAL for frame %d",
2221 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002222 return;
2223 }
Zhijun He204e3292014-07-14 17:09:23 -07002224
2225 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2226 // partial_result to 1 when metadata is included in this result.
2227 if (!mUsePartialResult &&
2228 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2229 result->result != NULL &&
2230 result->partial_result != 1) {
2231 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2232 " if partial result is not supported",
2233 frameNumber, result->partial_result);
2234 return;
2235 }
2236
2237 bool isPartialResult = false;
2238 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002239 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002240 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002241
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002242 // Get shutter timestamp and resultExtras from list of in-flight requests,
2243 // where it was added by the shutter notification for this frame. If the
2244 // shutter timestamp isn't received yet, append the output buffers to the
2245 // in-flight request and they will be returned when the shutter timestamp
2246 // arrives. Update the in-flight status and remove the in-flight entry if
2247 // all result data and shutter timestamp have been received.
2248 nsecs_t shutterTimestamp = 0;
2249
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002250 {
2251 Mutex::Autolock l(mInFlightLock);
2252 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2253 if (idx == NAME_NOT_FOUND) {
2254 SET_ERR("Unknown frame number for capture result: %d",
2255 frameNumber);
2256 return;
2257 }
2258 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002259 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2260 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2261 ", partialResultCount = %d",
2262 __FUNCTION__, request.resultExtras.requestId,
2263 request.resultExtras.frameNumber, request.resultExtras.burstId,
2264 result->partial_result);
2265 // Always update the partial count to the latest one if it's not 0
2266 // (buffers only). When framework aggregates adjacent partial results
2267 // into one, the latest partial count will be used.
2268 if (result->partial_result != 0)
2269 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002270
2271 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002272 if (mUsePartialResult && result->result != NULL) {
2273 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2274 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2275 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2276 " the range of [1, %d] when metadata is included in the result",
2277 frameNumber, result->partial_result, mNumPartialResults);
2278 return;
2279 }
2280 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002281 if (isPartialResult) {
2282 request.partialResult.collectedResult.append(result->result);
2283 }
Zhijun He204e3292014-07-14 17:09:23 -07002284 } else {
2285 camera_metadata_ro_entry_t partialResultEntry;
2286 res = find_camera_metadata_ro_entry(result->result,
2287 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2288 if (res != NAME_NOT_FOUND &&
2289 partialResultEntry.count > 0 &&
2290 partialResultEntry.data.u8[0] ==
2291 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2292 // A partial result. Flag this as such, and collect this
2293 // set of metadata into the in-flight entry.
2294 isPartialResult = true;
2295 request.partialResult.collectedResult.append(
2296 result->result);
2297 request.partialResult.collectedResult.erase(
2298 ANDROID_QUIRKS_PARTIAL_RESULT);
2299 }
2300 }
2301
2302 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002303 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002304 if (!request.partialResult.haveSent3A) {
2305 request.partialResult.haveSent3A =
2306 processPartial3AResult(frameNumber,
2307 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002308 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002309 }
2310 }
2311 }
2312
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002313 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002314 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002315
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002316 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002317 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002318 if (request.haveResultMetadata) {
2319 SET_ERR("Called multiple times with metadata for frame %d",
2320 frameNumber);
2321 return;
2322 }
Zhijun He204e3292014-07-14 17:09:23 -07002323 if (mUsePartialResult &&
2324 !request.partialResult.collectedResult.isEmpty()) {
2325 collectedPartialResult.acquire(
2326 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002327 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002328 request.haveResultMetadata = true;
2329 }
2330
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002331 uint32_t numBuffersReturned = result->num_output_buffers;
2332 if (result->input_buffer != NULL) {
2333 if (hasInputBufferInRequest) {
2334 numBuffersReturned += 1;
2335 } else {
2336 ALOGW("%s: Input buffer should be NULL if there is no input"
2337 " buffer sent in the request",
2338 __FUNCTION__);
2339 }
2340 }
2341 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002342 if (request.numBuffersLeft < 0) {
2343 SET_ERR("Too many buffers returned for frame %d",
2344 frameNumber);
2345 return;
2346 }
2347
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002348 camera_metadata_ro_entry_t entry;
2349 res = find_camera_metadata_ro_entry(result->result,
2350 ANDROID_SENSOR_TIMESTAMP, &entry);
2351 if (res == OK && entry.count == 1) {
2352 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002353 }
2354
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002355 // If shutter event isn't received yet, append the output buffers to
2356 // the in-flight request. Otherwise, return the output buffers to
2357 // streams.
2358 if (shutterTimestamp == 0) {
2359 request.pendingOutputBuffers.appendArray(result->output_buffers,
2360 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002361 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002362 returnOutputBuffers(result->output_buffers,
2363 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002364 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002365
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002366 if (result->result != NULL && !isPartialResult) {
2367 if (shutterTimestamp == 0) {
2368 request.pendingMetadata = result->result;
2369 request.partialResult.collectedResult = collectedPartialResult;
2370 } else {
2371 CameraMetadata metadata;
2372 metadata = result->result;
2373 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002374 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2375 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002376 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002377 }
2378
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002379 removeInFlightRequestIfReadyLocked(idx);
2380 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002381
Zhijun Hef0d962a2014-06-30 10:24:11 -07002382 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002383 if (hasInputBufferInRequest) {
2384 Camera3Stream *stream =
2385 Camera3Stream::cast(result->input_buffer->stream);
2386 res = stream->returnInputBuffer(*(result->input_buffer));
2387 // Note: stream may be deallocated at this point, if this buffer was the
2388 // last reference to it.
2389 if (res != OK) {
2390 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2391 " its stream:%s (%d)", __FUNCTION__,
2392 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002393 }
2394 } else {
2395 ALOGW("%s: Input buffer should be NULL if there is no input"
2396 " buffer sent in the request, skipping input buffer return.",
2397 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002398 }
2399 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002400}
2401
2402void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002403 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002404 NotificationListener *listener;
2405 {
2406 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002407 listener = mListener;
2408 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002409
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002410 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002411 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002412 return;
2413 }
2414
2415 switch (msg->type) {
2416 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002417 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002418 break;
2419 }
2420 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002421 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002422 break;
2423 }
2424 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002425 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002426 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002427 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002428}
2429
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002430void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2431 NotificationListener *listener) {
2432
2433 // Map camera HAL error codes to ICameraDeviceCallback error codes
2434 // Index into this with the HAL error code
2435 static const ICameraDeviceCallbacks::CameraErrorCode
2436 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2437 // 0 = Unused error code
2438 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2439 // 1 = CAMERA3_MSG_ERROR_DEVICE
2440 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2441 // 2 = CAMERA3_MSG_ERROR_REQUEST
2442 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2443 // 3 = CAMERA3_MSG_ERROR_RESULT
2444 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2445 // 4 = CAMERA3_MSG_ERROR_BUFFER
2446 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2447 };
2448
2449 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2450 ((msg.error_code >= 0) &&
2451 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2452 halErrorMap[msg.error_code] :
2453 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2454
2455 int streamId = 0;
2456 if (msg.error_stream != NULL) {
2457 Camera3Stream *stream =
2458 Camera3Stream::cast(msg.error_stream);
2459 streamId = stream->getId();
2460 }
2461 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2462 mId, __FUNCTION__, msg.frame_number,
2463 streamId, msg.error_code);
2464
2465 CaptureResultExtras resultExtras;
2466 switch (errorCode) {
2467 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2468 // SET_ERR calls notifyError
2469 SET_ERR("Camera HAL reported serious device error");
2470 break;
2471 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2472 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2473 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2474 {
2475 Mutex::Autolock l(mInFlightLock);
2476 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2477 if (idx >= 0) {
2478 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2479 r.requestStatus = msg.error_code;
2480 resultExtras = r.resultExtras;
2481 } else {
2482 resultExtras.frameNumber = msg.frame_number;
2483 ALOGE("Camera %d: %s: cannot find in-flight request on "
2484 "frame %" PRId64 " error", mId, __FUNCTION__,
2485 resultExtras.frameNumber);
2486 }
2487 }
2488 if (listener != NULL) {
2489 listener->notifyError(errorCode, resultExtras);
2490 } else {
2491 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2492 }
2493 break;
2494 default:
2495 // SET_ERR calls notifyError
2496 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2497 break;
2498 }
2499}
2500
2501void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2502 NotificationListener *listener) {
2503 ssize_t idx;
2504 // Verify ordering of shutter notifications
2505 {
2506 Mutex::Autolock l(mOutputLock);
2507 // TODO: need to track errors for tighter bounds on expected frame number.
2508 if (msg.frame_number < mNextShutterFrameNumber) {
2509 SET_ERR("Shutter notification out-of-order. Expected "
2510 "notification for frame %d, got frame %d",
2511 mNextShutterFrameNumber, msg.frame_number);
2512 return;
2513 }
2514 mNextShutterFrameNumber = msg.frame_number + 1;
2515 }
2516
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002517 // Set timestamp for the request in the in-flight tracking
2518 // and get the request ID to send upstream
2519 {
2520 Mutex::Autolock l(mInFlightLock);
2521 idx = mInFlightMap.indexOfKey(msg.frame_number);
2522 if (idx >= 0) {
2523 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002524
2525 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2526 mId, __FUNCTION__,
2527 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2528 // Call listener, if any
2529 if (listener != NULL) {
2530 listener->notifyShutter(r.resultExtras, msg.timestamp);
2531 }
2532
2533 r.shutterTimestamp = msg.timestamp;
2534
2535 // send pending result and buffers
2536 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002537 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002538 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002539 returnOutputBuffers(r.pendingOutputBuffers.array(),
2540 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2541 r.pendingOutputBuffers.clear();
2542
2543 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002544 }
2545 }
2546 if (idx < 0) {
2547 SET_ERR("Shutter notification for non-existent frame number %d",
2548 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002549 }
2550}
2551
2552
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002553CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002554 ALOGV("%s", __FUNCTION__);
2555
Igor Murashkin1e479c02013-09-06 16:55:14 -07002556 CameraMetadata retVal;
2557
2558 if (mRequestThread != NULL) {
2559 retVal = mRequestThread->getLatestRequest();
2560 }
2561
Igor Murashkin1e479c02013-09-06 16:55:14 -07002562 return retVal;
2563}
2564
Jianing Weicb0652e2014-03-12 18:29:36 -07002565
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002566/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002567 * RequestThread inner class methods
2568 */
2569
2570Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002571 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002572 camera3_device_t *hal3Device,
2573 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002574 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002575 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002576 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002577 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002578 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002579 mReconfigured(false),
2580 mDoPause(false),
2581 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002582 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002583 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002584 mCurrentAfTriggerId(0),
2585 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002586 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2587 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002588 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002589}
2590
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002591void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002592 NotificationListener *listener) {
2593 Mutex::Autolock l(mRequestLock);
2594 mListener = listener;
2595}
2596
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002597void Camera3Device::RequestThread::configurationComplete() {
2598 Mutex::Autolock l(mRequestLock);
2599 mReconfigured = true;
2600}
2601
Jianing Wei90e59c92014-03-12 18:29:36 -07002602status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002603 List<sp<CaptureRequest> > &requests,
2604 /*out*/
2605 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002606 Mutex::Autolock l(mRequestLock);
2607 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2608 ++it) {
2609 mRequestQueue.push_back(*it);
2610 }
2611
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002612 if (lastFrameNumber != NULL) {
2613 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2614 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2615 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2616 *lastFrameNumber);
2617 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002618
Jianing Wei90e59c92014-03-12 18:29:36 -07002619 unpauseForNewRequests();
2620
2621 return OK;
2622}
2623
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002624
2625status_t Camera3Device::RequestThread::queueTrigger(
2626 RequestTrigger trigger[],
2627 size_t count) {
2628
2629 Mutex::Autolock l(mTriggerMutex);
2630 status_t ret;
2631
2632 for (size_t i = 0; i < count; ++i) {
2633 ret = queueTriggerLocked(trigger[i]);
2634
2635 if (ret != OK) {
2636 return ret;
2637 }
2638 }
2639
2640 return OK;
2641}
2642
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002643int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2644 sp<Camera3Device> d = device.promote();
2645 if (d != NULL) return d->mId;
2646 return 0;
2647}
2648
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002649status_t Camera3Device::RequestThread::queueTriggerLocked(
2650 RequestTrigger trigger) {
2651
2652 uint32_t tag = trigger.metadataTag;
2653 ssize_t index = mTriggerMap.indexOfKey(tag);
2654
2655 switch (trigger.getTagType()) {
2656 case TYPE_BYTE:
2657 // fall-through
2658 case TYPE_INT32:
2659 break;
2660 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002661 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2662 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002663 return INVALID_OPERATION;
2664 }
2665
2666 /**
2667 * Collect only the latest trigger, since we only have 1 field
2668 * in the request settings per trigger tag, and can't send more than 1
2669 * trigger per request.
2670 */
2671 if (index != NAME_NOT_FOUND) {
2672 mTriggerMap.editValueAt(index) = trigger;
2673 } else {
2674 mTriggerMap.add(tag, trigger);
2675 }
2676
2677 return OK;
2678}
2679
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002680status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002681 const RequestList &requests,
2682 /*out*/
2683 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002684 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002685 if (lastFrameNumber != NULL) {
2686 *lastFrameNumber = mRepeatingLastFrameNumber;
2687 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002688 mRepeatingRequests.clear();
2689 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2690 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002691
2692 unpauseForNewRequests();
2693
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002694 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002695 return OK;
2696}
2697
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002698bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2699 if (mRepeatingRequests.empty()) {
2700 return false;
2701 }
2702 int32_t requestId = requestIn->mResultExtras.requestId;
2703 const RequestList &repeatRequests = mRepeatingRequests;
2704 // All repeating requests are guaranteed to have same id so only check first quest
2705 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2706 return (firstRequest->mResultExtras.requestId == requestId);
2707}
2708
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002709status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002710 Mutex::Autolock l(mRequestLock);
2711 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002712 if (lastFrameNumber != NULL) {
2713 *lastFrameNumber = mRepeatingLastFrameNumber;
2714 }
2715 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002716 return OK;
2717}
2718
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002719status_t Camera3Device::RequestThread::clear(
2720 NotificationListener *listener,
2721 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002722 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002723 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002724
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002725 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002726
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002727 // Send errors for all requests pending in the request queue, including
2728 // pending repeating requests
2729 if (listener != NULL) {
2730 for (RequestList::iterator it = mRequestQueue.begin();
2731 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002732 // Abort the input buffers for reprocess requests.
2733 if ((*it)->mInputStream != NULL) {
2734 camera3_stream_buffer_t inputBuffer;
2735 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2736 if (res != OK) {
2737 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2738 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2739 } else {
2740 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2741 if (res != OK) {
2742 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2743 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2744 }
2745 }
2746 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002747 // Set the frame number this request would have had, if it
2748 // had been submitted; this frame number will not be reused.
2749 // The requestId and burstId fields were set when the request was
2750 // submitted originally (in convertMetadataListToRequestListLocked)
2751 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2752 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2753 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002754 }
2755 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002756 mRequestQueue.clear();
2757 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002758 if (lastFrameNumber != NULL) {
2759 *lastFrameNumber = mRepeatingLastFrameNumber;
2760 }
2761 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002762 return OK;
2763}
2764
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002765void Camera3Device::RequestThread::setPaused(bool paused) {
2766 Mutex::Autolock l(mPauseLock);
2767 mDoPause = paused;
2768 mDoPauseSignal.signal();
2769}
2770
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002771status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2772 int32_t requestId, nsecs_t timeout) {
2773 Mutex::Autolock l(mLatestRequestMutex);
2774 status_t res;
2775 while (mLatestRequestId != requestId) {
2776 nsecs_t startTime = systemTime();
2777
2778 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2779 if (res != OK) return res;
2780
2781 timeout -= (systemTime() - startTime);
2782 }
2783
2784 return OK;
2785}
2786
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002787void Camera3Device::RequestThread::requestExit() {
2788 // Call parent to set up shutdown
2789 Thread::requestExit();
2790 // The exit from any possible waits
2791 mDoPauseSignal.signal();
2792 mRequestSignal.signal();
2793}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002794
Chien-Yu Chend196d612015-06-22 19:49:01 -07002795
2796/**
2797 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2798 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2799 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2800 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2801 * request.
2802 */
2803void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2804 request->mAeTriggerCancelOverride.applyAeLock = false;
2805 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2806
2807 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2808 return;
2809 }
2810
2811 camera_metadata_entry_t aePrecaptureTrigger =
2812 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2813 if (aePrecaptureTrigger.count > 0 &&
2814 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2815 // Always override CANCEL to IDLE
2816 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2817 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2818 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2819 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2820 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2821
2822 if (mAeLockAvailable == true) {
2823 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2824 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2825 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2826 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2827 request->mAeTriggerCancelOverride.applyAeLock = true;
2828 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2829 }
2830 }
2831 }
2832}
2833
2834/**
2835 * Override result metadata for cancelling AE precapture trigger applied in
2836 * handleAePrecaptureCancelRequest().
2837 */
2838void Camera3Device::overrideResultForPrecaptureCancel(
2839 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2840 if (aeTriggerCancelOverride.applyAeLock) {
2841 // Only devices <= v3.2 should have this override
2842 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2843 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2844 }
2845
2846 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2847 // Only devices <= v3.2 should have this override
2848 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2849 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2850 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2851 }
2852}
2853
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002854bool Camera3Device::RequestThread::threadLoop() {
2855
2856 status_t res;
2857
2858 // Handle paused state.
2859 if (waitIfPaused()) {
2860 return true;
2861 }
2862
2863 // Get work to do
2864
2865 sp<CaptureRequest> nextRequest = waitForNextRequest();
2866 if (nextRequest == NULL) {
2867 return true;
2868 }
2869
2870 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002871 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002872 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002873 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002874
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002875 // Get the request ID, if any
2876 int requestId;
2877 camera_metadata_entry_t requestIdEntry =
2878 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2879 if (requestIdEntry.count > 0) {
2880 requestId = requestIdEntry.data.i32[0];
2881 } else {
2882 ALOGW("%s: Did not have android.request.id set in the request",
2883 __FUNCTION__);
2884 requestId = NAME_NOT_FOUND;
2885 }
2886
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002887 // Insert any queued triggers (before metadata is locked)
2888 int32_t triggerCount;
2889 res = insertTriggers(nextRequest);
2890 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002891 SET_ERR("RequestThread: Unable to insert triggers "
2892 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002893 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002894 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2895 return false;
2896 }
2897 triggerCount = res;
2898
2899 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2900
2901 // If the request is the same as last, or we had triggers last time
2902 if (mPrevRequest != nextRequest || triggersMixedIn) {
2903 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002904 * HAL workaround:
2905 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2906 */
2907 res = addDummyTriggerIds(nextRequest);
2908 if (res != OK) {
2909 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2910 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002911 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002912 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2913 return false;
2914 }
2915
2916 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002917 * The request should be presorted so accesses in HAL
2918 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2919 */
2920 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002921 request.settings = nextRequest->mSettings.getAndLock();
2922 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002923 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2924
2925 IF_ALOGV() {
2926 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2927 find_camera_metadata_ro_entry(
2928 request.settings,
2929 ANDROID_CONTROL_AF_TRIGGER,
2930 &e
2931 );
2932 if (e.count > 0) {
2933 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2934 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002935 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002936 e.data.u8[0]);
2937 }
2938 }
2939 } else {
2940 // leave request.settings NULL to indicate 'reuse latest given'
2941 ALOGVV("%s: Request settings are REUSED",
2942 __FUNCTION__);
2943 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002944
Zhijun Hef0d962a2014-06-30 10:24:11 -07002945 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002946
2947 // Fill in buffers
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002948 if (nextRequest->mInputStream != NULL) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002949 request.input_buffer = &nextRequest->mInputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002950 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002951 } else {
2952 request.input_buffer = NULL;
2953 }
2954
2955 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2956 nextRequest->mOutputStreams.size());
2957 request.output_buffers = outputBuffers.array();
2958 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2959 res = nextRequest->mOutputStreams.editItemAt(i)->
2960 getBuffer(&outputBuffers.editItemAt(i));
2961 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002962 // Can't get output buffer from gralloc queue - this could be due to
2963 // abandoned queue or other consumer misbehavior, so not a fatal
2964 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002965 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2966 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala0ec23d32015-05-28 15:15:54 -07002967 {
2968 Mutex::Autolock l(mRequestLock);
2969 if (mListener != NULL) {
2970 mListener->notifyError(
2971 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2972 nextRequest->mResultExtras);
2973 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002974 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002975 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2976 return true;
2977 }
2978 request.num_output_buffers++;
2979 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002980 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002981
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002982 // Log request in the in-flight queue
2983 sp<Camera3Device> parent = mParent.promote();
2984 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002985 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002986 CLOGE("RequestThread: Parent is gone");
2987 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2988 return false;
2989 }
2990
Jianing Weicb0652e2014-03-12 18:29:36 -07002991 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002992 totalNumBuffers, nextRequest->mResultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002993 /*hasInput*/request.input_buffer != NULL,
2994 nextRequest->mAeTriggerCancelOverride);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002995 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2996 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002997 __FUNCTION__,
2998 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2999 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003000 if (res != OK) {
3001 SET_ERR("RequestThread: Unable to register new in-flight request:"
3002 " %s (%d)", strerror(-res), res);
3003 cleanUpFailedRequest(request, nextRequest, outputBuffers);
3004 return false;
3005 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003006
Zhijun Hecc27e112013-10-03 16:12:43 -07003007 // Inform waitUntilRequestProcessed thread of a new request ID
3008 {
3009 Mutex::Autolock al(mLatestRequestMutex);
3010
3011 mLatestRequestId = requestId;
3012 mLatestRequestSignal.signal();
3013 }
3014
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003015 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003016 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
3017 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003018 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003019 ATRACE_END();
3020
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003021 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003022 // Should only get a failure here for malformed requests or device-level
3023 // errors, so consider all errors fatal. Bad metadata failures should
3024 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003025 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003026 " device: %s (%d)", request.frame_number, strerror(-res), res);
3027 cleanUpFailedRequest(request, nextRequest, outputBuffers);
3028 return false;
3029 }
3030
Igor Murashkin1e479c02013-09-06 16:55:14 -07003031 // Update the latest request sent to HAL
3032 if (request.settings != NULL) { // Don't update them if they were unchanged
3033 Mutex::Autolock al(mLatestRequestMutex);
3034
3035 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
3036 mLatestRequest.acquire(cloned);
3037 }
3038
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003039 if (request.settings != NULL) {
3040 nextRequest->mSettings.unlock(request.settings);
3041 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003042
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003043 // Unset as current request
3044 {
3045 Mutex::Autolock l(mRequestLock);
3046 mNextRequest.clear();
3047 }
3048
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003049 // Remove any previously queued triggers (after unlock)
3050 res = removeTriggers(mPrevRequest);
3051 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003052 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003053 "(capture request %d, HAL device: %s (%d)",
3054 request.frame_number, strerror(-res), res);
3055 return false;
3056 }
3057 mPrevTriggers = triggerCount;
3058
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003059 return true;
3060}
3061
Igor Murashkin1e479c02013-09-06 16:55:14 -07003062CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3063 Mutex::Autolock al(mLatestRequestMutex);
3064
3065 ALOGV("RequestThread::%s", __FUNCTION__);
3066
3067 return mLatestRequest;
3068}
3069
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003070bool Camera3Device::RequestThread::isStreamPending(
3071 sp<Camera3StreamInterface>& stream) {
3072 Mutex::Autolock l(mRequestLock);
3073
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003074 if (mNextRequest != nullptr) {
3075 for (const auto& s : mNextRequest->mOutputStreams) {
3076 if (stream == s) return true;
3077 }
3078 if (stream == mNextRequest->mInputStream) return true;
3079 }
3080
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003081 for (const auto& request : mRequestQueue) {
3082 for (const auto& s : request->mOutputStreams) {
3083 if (stream == s) return true;
3084 }
3085 if (stream == request->mInputStream) return true;
3086 }
3087
3088 for (const auto& request : mRepeatingRequests) {
3089 for (const auto& s : request->mOutputStreams) {
3090 if (stream == s) return true;
3091 }
3092 if (stream == request->mInputStream) return true;
3093 }
3094
3095 return false;
3096}
Jianing Weicb0652e2014-03-12 18:29:36 -07003097
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003098void Camera3Device::RequestThread::cleanUpFailedRequest(
3099 camera3_capture_request_t &request,
3100 sp<CaptureRequest> &nextRequest,
3101 Vector<camera3_stream_buffer_t> &outputBuffers) {
3102
3103 if (request.settings != NULL) {
3104 nextRequest->mSettings.unlock(request.settings);
3105 }
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003106 if (nextRequest->mInputStream != NULL) {
3107 nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3108 nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003109 }
3110 for (size_t i = 0; i < request.num_output_buffers; i++) {
3111 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3112 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
3113 outputBuffers[i], 0);
3114 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003115
3116 Mutex::Autolock l(mRequestLock);
3117 mNextRequest.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003118}
3119
3120sp<Camera3Device::CaptureRequest>
3121 Camera3Device::RequestThread::waitForNextRequest() {
3122 status_t res;
3123 sp<CaptureRequest> nextRequest;
3124
3125 // Optimized a bit for the simple steady-state case (single repeating
3126 // request), to avoid putting that request in the queue temporarily.
3127 Mutex::Autolock l(mRequestLock);
3128
3129 while (mRequestQueue.empty()) {
3130 if (!mRepeatingRequests.empty()) {
3131 // Always atomically enqueue all requests in a repeating request
3132 // list. Guarantees a complete in-sequence set of captures to
3133 // application.
3134 const RequestList &requests = mRepeatingRequests;
3135 RequestList::const_iterator firstRequest =
3136 requests.begin();
3137 nextRequest = *firstRequest;
3138 mRequestQueue.insert(mRequestQueue.end(),
3139 ++firstRequest,
3140 requests.end());
3141 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003142
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003143 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003144
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003145 break;
3146 }
3147
3148 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3149
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003150 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3151 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003152 Mutex::Autolock pl(mPauseLock);
3153 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003154 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003155 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003156 // Let the tracker know
3157 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3158 if (statusTracker != 0) {
3159 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3160 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003161 }
3162 // Stop waiting for now and let thread management happen
3163 return NULL;
3164 }
3165 }
3166
3167 if (nextRequest == NULL) {
3168 // Don't have a repeating request already in hand, so queue
3169 // must have an entry now.
3170 RequestList::iterator firstRequest =
3171 mRequestQueue.begin();
3172 nextRequest = *firstRequest;
3173 mRequestQueue.erase(firstRequest);
3174 }
3175
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003176 // In case we've been unpaused by setPaused clearing mDoPause, need to
3177 // update internal pause state (capture/setRepeatingRequest unpause
3178 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003179 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003180 if (mPaused) {
3181 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3182 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3183 if (statusTracker != 0) {
3184 statusTracker->markComponentActive(mStatusId);
3185 }
3186 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003187 mPaused = false;
3188
3189 // Check if we've reconfigured since last time, and reset the preview
3190 // request if so. Can't use 'NULL request == repeat' across configure calls.
3191 if (mReconfigured) {
3192 mPrevRequest.clear();
3193 mReconfigured = false;
3194 }
3195
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003196 if (nextRequest != NULL) {
3197 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003198 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3199 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003200
3201 // Since RequestThread::clear() removes buffers from the input stream,
3202 // get the right buffer here before unlocking mRequestLock
3203 if (nextRequest->mInputStream != NULL) {
3204 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3205 if (res != OK) {
3206 // Can't get input buffer from gralloc queue - this could be due to
3207 // disconnected queue or other producer misbehavior, so not a fatal
3208 // error
3209 ALOGE("%s: Can't get input buffer, skipping request:"
3210 " %s (%d)", __FUNCTION__, strerror(-res), res);
3211 if (mListener != NULL) {
3212 mListener->notifyError(
3213 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3214 nextRequest->mResultExtras);
3215 }
3216 return NULL;
3217 }
3218 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003219 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003220
3221 handleAePrecaptureCancelRequest(nextRequest);
3222
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003223 mNextRequest = nextRequest;
3224
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003225 return nextRequest;
3226}
3227
3228bool Camera3Device::RequestThread::waitIfPaused() {
3229 status_t res;
3230 Mutex::Autolock l(mPauseLock);
3231 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003232 if (mPaused == false) {
3233 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003234 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3235 // Let the tracker know
3236 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3237 if (statusTracker != 0) {
3238 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3239 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003240 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003241
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003242 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003243 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003244 return true;
3245 }
3246 }
3247 // We don't set mPaused to false here, because waitForNextRequest needs
3248 // to further manage the paused state in case of starvation.
3249 return false;
3250}
3251
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003252void Camera3Device::RequestThread::unpauseForNewRequests() {
3253 // With work to do, mark thread as unpaused.
3254 // If paused by request (setPaused), don't resume, to avoid
3255 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003256 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003257 Mutex::Autolock p(mPauseLock);
3258 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003259 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3260 if (mPaused) {
3261 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3262 if (statusTracker != 0) {
3263 statusTracker->markComponentActive(mStatusId);
3264 }
3265 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003266 mPaused = false;
3267 }
3268}
3269
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003270void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3271 sp<Camera3Device> parent = mParent.promote();
3272 if (parent != NULL) {
3273 va_list args;
3274 va_start(args, fmt);
3275
3276 parent->setErrorStateV(fmt, args);
3277
3278 va_end(args);
3279 }
3280}
3281
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003282status_t Camera3Device::RequestThread::insertTriggers(
3283 const sp<CaptureRequest> &request) {
3284
3285 Mutex::Autolock al(mTriggerMutex);
3286
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003287 sp<Camera3Device> parent = mParent.promote();
3288 if (parent == NULL) {
3289 CLOGE("RequestThread: Parent is gone");
3290 return DEAD_OBJECT;
3291 }
3292
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003293 CameraMetadata &metadata = request->mSettings;
3294 size_t count = mTriggerMap.size();
3295
3296 for (size_t i = 0; i < count; ++i) {
3297 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003298 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003299
3300 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3301 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3302 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003303 if (isAeTrigger) {
3304 request->mResultExtras.precaptureTriggerId = triggerId;
3305 mCurrentPreCaptureTriggerId = triggerId;
3306 } else {
3307 request->mResultExtras.afTriggerId = triggerId;
3308 mCurrentAfTriggerId = triggerId;
3309 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003310 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3311 continue; // Trigger ID tag is deprecated since device HAL 3.2
3312 }
3313 }
3314
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003315 camera_metadata_entry entry = metadata.find(tag);
3316
3317 if (entry.count > 0) {
3318 /**
3319 * Already has an entry for this trigger in the request.
3320 * Rewrite it with our requested trigger value.
3321 */
3322 RequestTrigger oldTrigger = trigger;
3323
3324 oldTrigger.entryValue = entry.data.u8[0];
3325
3326 mTriggerReplacedMap.add(tag, oldTrigger);
3327 } else {
3328 /**
3329 * More typical, no trigger entry, so we just add it
3330 */
3331 mTriggerRemovedMap.add(tag, trigger);
3332 }
3333
3334 status_t res;
3335
3336 switch (trigger.getTagType()) {
3337 case TYPE_BYTE: {
3338 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3339 res = metadata.update(tag,
3340 &entryValue,
3341 /*count*/1);
3342 break;
3343 }
3344 case TYPE_INT32:
3345 res = metadata.update(tag,
3346 &trigger.entryValue,
3347 /*count*/1);
3348 break;
3349 default:
3350 ALOGE("%s: Type not supported: 0x%x",
3351 __FUNCTION__,
3352 trigger.getTagType());
3353 return INVALID_OPERATION;
3354 }
3355
3356 if (res != OK) {
3357 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3358 ", value %d", __FUNCTION__, trigger.getTagName(),
3359 trigger.entryValue);
3360 return res;
3361 }
3362
3363 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3364 trigger.getTagName(),
3365 trigger.entryValue);
3366 }
3367
3368 mTriggerMap.clear();
3369
3370 return count;
3371}
3372
3373status_t Camera3Device::RequestThread::removeTriggers(
3374 const sp<CaptureRequest> &request) {
3375 Mutex::Autolock al(mTriggerMutex);
3376
3377 CameraMetadata &metadata = request->mSettings;
3378
3379 /**
3380 * Replace all old entries with their old values.
3381 */
3382 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3383 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3384
3385 status_t res;
3386
3387 uint32_t tag = trigger.metadataTag;
3388 switch (trigger.getTagType()) {
3389 case TYPE_BYTE: {
3390 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3391 res = metadata.update(tag,
3392 &entryValue,
3393 /*count*/1);
3394 break;
3395 }
3396 case TYPE_INT32:
3397 res = metadata.update(tag,
3398 &trigger.entryValue,
3399 /*count*/1);
3400 break;
3401 default:
3402 ALOGE("%s: Type not supported: 0x%x",
3403 __FUNCTION__,
3404 trigger.getTagType());
3405 return INVALID_OPERATION;
3406 }
3407
3408 if (res != OK) {
3409 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3410 ", trigger value %d", __FUNCTION__,
3411 trigger.getTagName(), trigger.entryValue);
3412 return res;
3413 }
3414 }
3415 mTriggerReplacedMap.clear();
3416
3417 /**
3418 * Remove all new entries.
3419 */
3420 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3421 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3422 status_t res = metadata.erase(trigger.metadataTag);
3423
3424 if (res != OK) {
3425 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3426 ", trigger value %d", __FUNCTION__,
3427 trigger.getTagName(), trigger.entryValue);
3428 return res;
3429 }
3430 }
3431 mTriggerRemovedMap.clear();
3432
3433 return OK;
3434}
3435
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003436status_t Camera3Device::RequestThread::addDummyTriggerIds(
3437 const sp<CaptureRequest> &request) {
3438 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3439 static const int32_t dummyTriggerId = 1;
3440 status_t res;
3441
3442 CameraMetadata &metadata = request->mSettings;
3443
3444 // If AF trigger is active, insert a dummy AF trigger ID if none already
3445 // exists
3446 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3447 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3448 if (afTrigger.count > 0 &&
3449 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3450 afId.count == 0) {
3451 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3452 if (res != OK) return res;
3453 }
3454
3455 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3456 // if none already exists
3457 camera_metadata_entry pcTrigger =
3458 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3459 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3460 if (pcTrigger.count > 0 &&
3461 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3462 pcId.count == 0) {
3463 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3464 &dummyTriggerId, 1);
3465 if (res != OK) return res;
3466 }
3467
3468 return OK;
3469}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003470
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003471/**
3472 * PreparerThread inner class methods
3473 */
3474
3475Camera3Device::PreparerThread::PreparerThread() :
3476 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3477}
3478
3479Camera3Device::PreparerThread::~PreparerThread() {
3480 Thread::requestExitAndWait();
3481 if (mCurrentStream != nullptr) {
3482 mCurrentStream->cancelPrepare();
3483 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3484 mCurrentStream.clear();
3485 }
3486 clear();
3487}
3488
3489status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) {
3490 status_t res;
3491
3492 Mutex::Autolock l(mLock);
3493
3494 res = stream->startPrepare();
3495 if (res == OK) {
3496 // No preparation needed, fire listener right off
3497 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3498 if (mListener) {
3499 mListener->notifyPrepared(stream->getId());
3500 }
3501 return OK;
3502 } else if (res != NOT_ENOUGH_DATA) {
3503 return res;
3504 }
3505
3506 // Need to prepare, start up thread if necessary
3507 if (!mActive) {
3508 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3509 // isn't running
3510 Thread::requestExitAndWait();
3511 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3512 if (res != OK) {
3513 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3514 if (mListener) {
3515 mListener->notifyPrepared(stream->getId());
3516 }
3517 return res;
3518 }
3519 mCancelNow = false;
3520 mActive = true;
3521 ALOGV("%s: Preparer stream started", __FUNCTION__);
3522 }
3523
3524 // queue up the work
3525 mPendingStreams.push_back(stream);
3526 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3527
3528 return OK;
3529}
3530
3531status_t Camera3Device::PreparerThread::clear() {
3532 status_t res;
3533
3534 Mutex::Autolock l(mLock);
3535
3536 for (const auto& stream : mPendingStreams) {
3537 stream->cancelPrepare();
3538 }
3539 mPendingStreams.clear();
3540 mCancelNow = true;
3541
3542 return OK;
3543}
3544
3545void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3546 Mutex::Autolock l(mLock);
3547 mListener = listener;
3548}
3549
3550bool Camera3Device::PreparerThread::threadLoop() {
3551 status_t res;
3552 {
3553 Mutex::Autolock l(mLock);
3554 if (mCurrentStream == nullptr) {
3555 // End thread if done with work
3556 if (mPendingStreams.empty()) {
3557 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3558 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3559 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3560 mActive = false;
3561 return false;
3562 }
3563
3564 // Get next stream to prepare
3565 auto it = mPendingStreams.begin();
3566 mCurrentStream = *it;
3567 mPendingStreams.erase(it);
3568 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3569 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3570 } else if (mCancelNow) {
3571 mCurrentStream->cancelPrepare();
3572 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3573 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3574 mCurrentStream.clear();
3575 mCancelNow = false;
3576 return true;
3577 }
3578 }
3579
3580 res = mCurrentStream->prepareNextBuffer();
3581 if (res == NOT_ENOUGH_DATA) return true;
3582 if (res != OK) {
3583 // Something bad happened; try to recover by cancelling prepare and
3584 // signalling listener anyway
3585 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3586 mCurrentStream->getId(), res, strerror(-res));
3587 mCurrentStream->cancelPrepare();
3588 }
3589
3590 // This stream has finished, notify listener
3591 Mutex::Autolock l(mLock);
3592 if (mListener) {
3593 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3594 mCurrentStream->getId());
3595 mListener->notifyPrepared(mCurrentStream->getId());
3596 }
3597
3598 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3599 mCurrentStream.clear();
3600
3601 return true;
3602}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003603
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003604/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003605 * Static callback forwarding methods from HAL to instance
3606 */
3607
3608void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3609 const camera3_capture_result *result) {
3610 Camera3Device *d =
3611 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003612
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003613 d->processCaptureResult(result);
3614}
3615
3616void Camera3Device::sNotify(const camera3_callback_ops *cb,
3617 const camera3_notify_msg *msg) {
3618 Camera3Device *d =
3619 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3620 d->notify(msg);
3621}
3622
3623}; // namespace android