blob: 7acd1502131998f4507e46fe3c5e42433626cd86 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Igor Murashkinff3e31d2013-10-23 16:40:06 -070046#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070047#include "mediautils/SchedulingPolicyService.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),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070070 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070071 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080072{
73 ATRACE_CALL();
74 camera3_callback_ops::notify = &sNotify;
75 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
76 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
77}
78
79Camera3Device::~Camera3Device()
80{
81 ATRACE_CALL();
82 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
83 disconnect();
84}
85
Igor Murashkin71381052013-03-04 14:53:08 -080086int Camera3Device::getId() const {
87 return mId;
88}
89
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080090/**
91 * CameraDeviceBase interface
92 */
93
Yin-Chia Yehe074a932015-01-30 10:29:02 -080094status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080095{
96 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070097 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080098 Mutex::Autolock l(mLock);
99
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800100 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800101 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700102 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 return INVALID_OPERATION;
104 }
105
106 /** Open HAL device */
107
108 status_t res;
109 String8 deviceName = String8::format("%d", mId);
110
111 camera3_device_t *device;
112
Zhijun He213ce792013-11-19 08:45:15 -0800113 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800114 res = module->open(deviceName.string(),
115 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800116 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117
118 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700119 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800120 return res;
121 }
122
123 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700124 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700125 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700126 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700127 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800128 device->common.version);
129 device->common.close(&device->common);
130 return BAD_VALUE;
131 }
132
133 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800134 res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700135 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800136 if (res != OK) return res;
137
138 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700139 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
140 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700141 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800142 device->common.close(&device->common);
143 return BAD_VALUE;
144 }
145
146 /** Initialize device with callback functions */
147
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700148 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800149 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700150 ATRACE_END();
151
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800152 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700153 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
154 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800155 device->common.close(&device->common);
156 return BAD_VALUE;
157 }
158
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700159 /** Start up status tracker thread */
160 mStatusTracker = new StatusTracker(this);
161 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
162 if (res != OK) {
163 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
164 strerror(-res), res);
165 device->common.close(&device->common);
166 mStatusTracker.clear();
167 return res;
168 }
169
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700170 bool aeLockAvailable = false;
171 camera_metadata_ro_entry aeLockAvailableEntry;
172 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
173 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
174 if (res == OK && aeLockAvailableEntry.count > 0) {
175 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
176 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
177 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800178
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700179 /** Start up request queue thread */
180 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800181 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800182 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700183 SET_ERR_L("Unable to start request queue thread: %s (%d)",
184 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800185 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800186 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187 return res;
188 }
189
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700190 mPreparerThread = new PreparerThread();
191
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 /** Everything is good to go */
193
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700194 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800195 mDeviceInfo = info.static_camera_characteristics;
196 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700197
198 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800199 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700200 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700201 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700202 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800203
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700204 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700205 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
206 camera_metadata_entry partialResultsCount =
207 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
208 if (partialResultsCount.count > 0) {
209 mNumPartialResults = partialResultsCount.data.i32[0];
210 mUsePartialResult = (mNumPartialResults > 1);
211 }
212 } else {
213 camera_metadata_entry partialResultsQuirk =
214 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
215 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
216 mUsePartialResult = true;
217 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700218 }
219
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700220 camera_metadata_entry configs =
221 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
222 for (uint32_t i = 0; i < configs.count; i += 4) {
223 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
224 configs.data.i32[i + 3] ==
225 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
226 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
227 configs.data.i32[i + 2]));
228 }
229 }
230
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800231 return OK;
232}
233
234status_t Camera3Device::disconnect() {
235 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700236 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800237
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800238 ALOGV("%s: E", __FUNCTION__);
239
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700240 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800241
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700242 {
243 Mutex::Autolock l(mLock);
244 if (mStatus == STATUS_UNINITIALIZED) return res;
245
246 if (mStatus == STATUS_ACTIVE ||
247 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
248 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700249 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700250 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700251 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700252 } else {
253 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
254 if (res != OK) {
255 SET_ERR_L("Timeout waiting for HAL to drain");
256 // Continue to close device even in case of error
257 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700258 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800259 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800260
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700261 if (mStatus == STATUS_ERROR) {
262 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700263 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700264
265 if (mStatusTracker != NULL) {
266 mStatusTracker->requestExit();
267 }
268
269 if (mRequestThread != NULL) {
270 mRequestThread->requestExit();
271 }
272
273 mOutputStreams.clear();
274 mInputStream.clear();
275 }
276
277 // Joining done without holding mLock, otherwise deadlocks may ensue
278 // as the threads try to access parent state
279 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
280 // HAL may be in a bad state, so waiting for request thread
281 // (which may be stuck in the HAL processCaptureRequest call)
282 // could be dangerous.
283 mRequestThread->join();
284 }
285
286 if (mStatusTracker != NULL) {
287 mStatusTracker->join();
288 }
289
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700290 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700291 {
292 Mutex::Autolock l(mLock);
293
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800294 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700295 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800296
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700297 hal3Device = mHal3Device;
298 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800299
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700300 // Call close without internal mutex held, as the HAL close may need to
301 // wait on assorted callbacks,etc, to complete before it can return.
302 if (hal3Device != NULL) {
303 ATRACE_BEGIN("camera3->close");
304 hal3Device->common.close(&hal3Device->common);
305 ATRACE_END();
306 }
307
308 {
309 Mutex::Autolock l(mLock);
310 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700311 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700312 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800313
314 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700315 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800316}
317
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700318// For dumping/debugging only -
319// try to acquire a lock a few times, eventually give up to proceed with
320// debug/dump operations
321bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
322 bool gotLock = false;
323 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
324 if (lock.tryLock() == NO_ERROR) {
325 gotLock = true;
326 break;
327 } else {
328 usleep(kDumpSleepDuration);
329 }
330 }
331 return gotLock;
332}
333
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700334Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
335 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
336 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
337 const int STREAM_CONFIGURATION_SIZE = 4;
338 const int STREAM_FORMAT_OFFSET = 0;
339 const int STREAM_WIDTH_OFFSET = 1;
340 const int STREAM_HEIGHT_OFFSET = 2;
341 const int STREAM_IS_INPUT_OFFSET = 3;
342 camera_metadata_ro_entry_t availableStreamConfigs =
343 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
344 if (availableStreamConfigs.count == 0 ||
345 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
346 return Size(0, 0);
347 }
348
349 // Get max jpeg size (area-wise).
350 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
351 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
352 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
353 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
354 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
355 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
356 && format == HAL_PIXEL_FORMAT_BLOB &&
357 (width * height > maxJpegWidth * maxJpegHeight)) {
358 maxJpegWidth = width;
359 maxJpegHeight = height;
360 }
361 }
362 } else {
363 camera_metadata_ro_entry availableJpegSizes =
364 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
365 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
366 return Size(0, 0);
367 }
368
369 // Get max jpeg size (area-wise).
370 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
371 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
372 > (maxJpegWidth * maxJpegHeight)) {
373 maxJpegWidth = availableJpegSizes.data.i32[i];
374 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
375 }
376 }
377 }
378 return Size(maxJpegWidth, maxJpegHeight);
379}
380
Zhijun Hef7da0962014-04-24 13:27:56 -0700381ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700382 // Get max jpeg size (area-wise).
383 Size maxJpegResolution = getMaxJpegResolution();
384 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700385 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700386 __FUNCTION__, mId);
387 return BAD_VALUE;
388 }
389
Zhijun Hef7da0962014-04-24 13:27:56 -0700390 // Get max jpeg buffer size
391 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700392 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
393 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700394 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
395 return BAD_VALUE;
396 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700397 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800398 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700399
400 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700401 float scaleFactor = ((float) (width * height)) /
402 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800403 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
404 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700405 if (jpegBufferSize > maxJpegBufferSize) {
406 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700407 }
408
409 return jpegBufferSize;
410}
411
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700412ssize_t Camera3Device::getPointCloudBufferSize() const {
413 const int FLOATS_PER_POINT=4;
414 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
415 if (maxPointCount.count == 0) {
416 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
417 __FUNCTION__, mId);
418 return BAD_VALUE;
419 }
420 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
421 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
422 return maxBytesForPointCloud;
423}
424
425
426
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800427status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
428 ATRACE_CALL();
429 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700430
431 // Try to lock, but continue in case of failure (to avoid blocking in
432 // deadlocks)
433 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
434 bool gotLock = tryLockSpinRightRound(mLock);
435
436 ALOGW_IF(!gotInterfaceLock,
437 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
438 mId, __FUNCTION__);
439 ALOGW_IF(!gotLock,
440 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
441 mId, __FUNCTION__);
442
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800443 bool dumpTemplates = false;
444 String16 templatesOption("-t");
445 int n = args.size();
446 for (int i = 0; i < n; i++) {
447 if (args[i] == templatesOption) {
448 dumpTemplates = true;
449 }
450 }
451
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800452 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800453
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800454 const char *status =
455 mStatus == STATUS_ERROR ? "ERROR" :
456 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700457 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
458 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800459 mStatus == STATUS_ACTIVE ? "ACTIVE" :
460 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700461
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800462 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700463 if (mStatus == STATUS_ERROR) {
464 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
465 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800466 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700467 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700468 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800469
470 if (mInputStream != NULL) {
471 write(fd, lines.string(), lines.size());
472 mInputStream->dump(fd, args);
473 } else {
474 lines.appendFormat(" No input stream.\n");
475 write(fd, lines.string(), lines.size());
476 }
477 for (size_t i = 0; i < mOutputStreams.size(); i++) {
478 mOutputStreams[i]->dump(fd,args);
479 }
480
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700481 lines = String8(" In-flight requests:\n");
482 if (mInFlightMap.size() == 0) {
483 lines.append(" None\n");
484 } else {
485 for (size_t i = 0; i < mInFlightMap.size(); i++) {
486 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700487 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700488 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800489 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700490 r.numBuffersLeft);
491 }
492 }
493 write(fd, lines.string(), lines.size());
494
Igor Murashkin1e479c02013-09-06 16:55:14 -0700495 {
496 lines = String8(" Last request sent:\n");
497 write(fd, lines.string(), lines.size());
498
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700499 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700500 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
501 }
502
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800503 if (dumpTemplates) {
504 const char *templateNames[] = {
505 "TEMPLATE_PREVIEW",
506 "TEMPLATE_STILL_CAPTURE",
507 "TEMPLATE_VIDEO_RECORD",
508 "TEMPLATE_VIDEO_SNAPSHOT",
509 "TEMPLATE_ZERO_SHUTTER_LAG",
510 "TEMPLATE_MANUAL"
511 };
512
513 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
514 const camera_metadata_t *templateRequest;
515 templateRequest =
516 mHal3Device->ops->construct_default_request_settings(
517 mHal3Device, i);
518 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
519 if (templateRequest == NULL) {
520 lines.append(" Not supported\n");
521 write(fd, lines.string(), lines.size());
522 } else {
523 write(fd, lines.string(), lines.size());
524 dump_indented_camera_metadata(templateRequest,
525 fd, /*verbosity*/2, /*indentation*/8);
526 }
527 }
528 }
529
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800530 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700531 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800532 write(fd, lines.string(), lines.size());
533 mHal3Device->ops->dump(mHal3Device, fd);
534 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800535
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700536 if (gotLock) mLock.unlock();
537 if (gotInterfaceLock) mInterfaceLock.unlock();
538
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800539 return OK;
540}
541
542const CameraMetadata& Camera3Device::info() const {
543 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800544 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
545 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700546 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800547 mStatus == STATUS_ERROR ?
548 "when in error state" : "before init");
549 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800550 return mDeviceInfo;
551}
552
Jianing Wei90e59c92014-03-12 18:29:36 -0700553status_t Camera3Device::checkStatusOkToCaptureLocked() {
554 switch (mStatus) {
555 case STATUS_ERROR:
556 CLOGE("Device has encountered a serious error");
557 return INVALID_OPERATION;
558 case STATUS_UNINITIALIZED:
559 CLOGE("Device not initialized");
560 return INVALID_OPERATION;
561 case STATUS_UNCONFIGURED:
562 case STATUS_CONFIGURED:
563 case STATUS_ACTIVE:
564 // OK
565 break;
566 default:
567 SET_ERR_L("Unexpected status: %d", mStatus);
568 return INVALID_OPERATION;
569 }
570 return OK;
571}
572
573status_t Camera3Device::convertMetadataListToRequestListLocked(
574 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
575 if (requestList == NULL) {
576 CLOGE("requestList cannot be NULL.");
577 return BAD_VALUE;
578 }
579
Jianing Weicb0652e2014-03-12 18:29:36 -0700580 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700581 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
582 it != metadataList.end(); ++it) {
583 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
584 if (newRequest == 0) {
585 CLOGE("Can't create capture request");
586 return BAD_VALUE;
587 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700588
589 // Setup burst Id and request Id
590 newRequest->mResultExtras.burstId = burstId++;
591 if (it->exists(ANDROID_REQUEST_ID)) {
592 if (it->find(ANDROID_REQUEST_ID).count == 0) {
593 CLOGE("RequestID entry exists; but must not be empty in metadata");
594 return BAD_VALUE;
595 }
596 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
597 } else {
598 CLOGE("RequestID does not exist in metadata");
599 return BAD_VALUE;
600 }
601
Jianing Wei90e59c92014-03-12 18:29:36 -0700602 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700603
604 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700605 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700606
607 // Setup batch size if this is a high speed video recording request.
608 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
609 auto firstRequest = requestList->begin();
610 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
611 if (outputStream->isVideoStream()) {
612 (*firstRequest)->mBatchSize = requestList->size();
613 break;
614 }
615 }
616 }
617
Jianing Wei90e59c92014-03-12 18:29:36 -0700618 return OK;
619}
620
Jianing Weicb0652e2014-03-12 18:29:36 -0700621status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800622 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800623
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700624 List<const CameraMetadata> requests;
625 requests.push_back(request);
626 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800627}
628
Jianing Wei90e59c92014-03-12 18:29:36 -0700629status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700630 const List<const CameraMetadata> &requests, bool repeating,
631 /*out*/
632 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700633 ATRACE_CALL();
634 Mutex::Autolock il(mInterfaceLock);
635 Mutex::Autolock l(mLock);
636
637 status_t res = checkStatusOkToCaptureLocked();
638 if (res != OK) {
639 // error logged by previous call
640 return res;
641 }
642
643 RequestList requestList;
644
645 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
646 if (res != OK) {
647 // error logged by previous call
648 return res;
649 }
650
651 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700652 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700653 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700654 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700655 }
656
657 if (res == OK) {
658 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
659 if (res != OK) {
660 SET_ERR_L("Can't transition to active in %f seconds!",
661 kActiveTimeout/1e9);
662 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700663 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
664 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700665 } else {
666 CLOGE("Cannot queue request. Impossible.");
667 return BAD_VALUE;
668 }
669
670 return res;
671}
672
Jianing Weicb0652e2014-03-12 18:29:36 -0700673status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
674 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700675 ATRACE_CALL();
676
Jianing Weicb0652e2014-03-12 18:29:36 -0700677 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700678}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800679
Jianing Weicb0652e2014-03-12 18:29:36 -0700680status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
681 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800682 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800683
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700684 List<const CameraMetadata> requests;
685 requests.push_back(request);
686 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800687}
688
Jianing Weicb0652e2014-03-12 18:29:36 -0700689status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
690 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700691 ATRACE_CALL();
692
Jianing Weicb0652e2014-03-12 18:29:36 -0700693 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700694}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800695
696sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
697 const CameraMetadata &request) {
698 status_t res;
699
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700700 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800701 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700702 // Stream configuration failed due to unsupported configuration.
703 // Device back to unconfigured state. Client might try other configuraitons
704 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
705 CLOGE("No streams configured");
706 return NULL;
707 }
708 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800709 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700710 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800711 return NULL;
712 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700713 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700714 if (mStatus == STATUS_UNCONFIGURED) {
715 CLOGE("No streams configured");
716 return NULL;
717 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800718 }
719
720 sp<CaptureRequest> newRequest = createCaptureRequest(request);
721 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800722}
723
Jianing Weicb0652e2014-03-12 18:29:36 -0700724status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800725 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700726 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800727 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800728
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800729 switch (mStatus) {
730 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700731 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800732 return INVALID_OPERATION;
733 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700734 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800735 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700736 case STATUS_UNCONFIGURED:
737 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800738 case STATUS_ACTIVE:
739 // OK
740 break;
741 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700742 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800743 return INVALID_OPERATION;
744 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700745 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700746
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700747 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800748}
749
750status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
751 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700752 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800753
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700754 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800755}
756
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700757status_t Camera3Device::createInputStream(
758 uint32_t width, uint32_t height, int format, int *id) {
759 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700760 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700761 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700762 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
763 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700764
765 status_t res;
766 bool wasActive = false;
767
768 switch (mStatus) {
769 case STATUS_ERROR:
770 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
771 return INVALID_OPERATION;
772 case STATUS_UNINITIALIZED:
773 ALOGE("%s: Device not initialized", __FUNCTION__);
774 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700775 case STATUS_UNCONFIGURED:
776 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700777 // OK
778 break;
779 case STATUS_ACTIVE:
780 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700781 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700782 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700783 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700784 return res;
785 }
786 wasActive = true;
787 break;
788 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700789 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700790 return INVALID_OPERATION;
791 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700792 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700793
794 if (mInputStream != 0) {
795 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
796 return INVALID_OPERATION;
797 }
798
799 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
800 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700801 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700802
803 mInputStream = newStream;
804
805 *id = mNextStreamId++;
806
807 // Continue captures if active at start
808 if (wasActive) {
809 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
810 res = configureStreamsLocked();
811 if (res != OK) {
812 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
813 __FUNCTION__, mNextStreamId, strerror(-res), res);
814 return res;
815 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700816 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700817 }
818
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700819 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700820 return OK;
821}
822
Igor Murashkin2fba5842013-04-22 14:03:54 -0700823
824status_t Camera3Device::createZslStream(
825 uint32_t width, uint32_t height,
826 int depth,
827 /*out*/
828 int *id,
829 sp<Camera3ZslStream>* zslStream) {
830 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700831 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700832 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700833 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
834 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700835
836 status_t res;
837 bool wasActive = false;
838
839 switch (mStatus) {
840 case STATUS_ERROR:
841 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
842 return INVALID_OPERATION;
843 case STATUS_UNINITIALIZED:
844 ALOGE("%s: Device not initialized", __FUNCTION__);
845 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700846 case STATUS_UNCONFIGURED:
847 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700848 // OK
849 break;
850 case STATUS_ACTIVE:
851 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700852 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700853 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700854 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700855 return res;
856 }
857 wasActive = true;
858 break;
859 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700860 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700861 return INVALID_OPERATION;
862 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700863 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700864
865 if (mInputStream != 0) {
866 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
867 return INVALID_OPERATION;
868 }
869
870 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
871 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700872 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700873
874 res = mOutputStreams.add(mNextStreamId, newStream);
875 if (res < 0) {
876 ALOGE("%s: Can't add new stream to set: %s (%d)",
877 __FUNCTION__, strerror(-res), res);
878 return res;
879 }
880 mInputStream = newStream;
881
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530882 mNeedConfig = true;
883
Igor Murashkin2fba5842013-04-22 14:03:54 -0700884 *id = mNextStreamId++;
885 *zslStream = newStream;
886
887 // Continue captures if active at start
888 if (wasActive) {
889 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
890 res = configureStreamsLocked();
891 if (res != OK) {
892 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
893 __FUNCTION__, mNextStreamId, strerror(-res), res);
894 return res;
895 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700896 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700897 }
898
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700899 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700900 return OK;
901}
902
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700903status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800904 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700905 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800906 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700907 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800908 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700909 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
910 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800911
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800912 status_t res;
913 bool wasActive = false;
914
915 switch (mStatus) {
916 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700917 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800918 return INVALID_OPERATION;
919 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700920 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800921 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700922 case STATUS_UNCONFIGURED:
923 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800924 // OK
925 break;
926 case STATUS_ACTIVE:
927 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700928 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800929 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700930 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800931 return res;
932 }
933 wasActive = true;
934 break;
935 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700936 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800937 return INVALID_OPERATION;
938 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700939 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800940
941 sp<Camera3OutputStream> newStream;
942 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700943 ssize_t blobBufferSize;
944 if (dataSpace != HAL_DATASPACE_DEPTH) {
945 blobBufferSize = getJpegBufferSize(width, height);
946 if (blobBufferSize <= 0) {
947 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
948 return BAD_VALUE;
949 }
950 } else {
951 blobBufferSize = getPointCloudBufferSize();
952 if (blobBufferSize <= 0) {
953 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
954 return BAD_VALUE;
955 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700956 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800957 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700958 width, height, blobBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800959 } else {
960 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700961 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800962 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700963 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800964
965 res = mOutputStreams.add(mNextStreamId, newStream);
966 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700967 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800968 return res;
969 }
970
971 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700972 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800973
974 // Continue captures if active at start
975 if (wasActive) {
976 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
977 res = configureStreamsLocked();
978 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700979 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
980 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800981 return res;
982 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700983 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800984 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700985 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800986 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800987}
988
989status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
990 ATRACE_CALL();
991 (void)outputId; (void)id;
992
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700993 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800994 return INVALID_OPERATION;
995}
996
997
998status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700999 uint32_t *width, uint32_t *height,
1000 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001001 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001002 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001003 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001004
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001005 switch (mStatus) {
1006 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001007 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001008 return INVALID_OPERATION;
1009 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001010 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001011 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001012 case STATUS_UNCONFIGURED:
1013 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001014 case STATUS_ACTIVE:
1015 // OK
1016 break;
1017 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001018 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001019 return INVALID_OPERATION;
1020 }
1021
1022 ssize_t idx = mOutputStreams.indexOfKey(id);
1023 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001024 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001025 return idx;
1026 }
1027
1028 if (width) *width = mOutputStreams[idx]->getWidth();
1029 if (height) *height = mOutputStreams[idx]->getHeight();
1030 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001031 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001032 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001033}
1034
1035status_t Camera3Device::setStreamTransform(int id,
1036 int transform) {
1037 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001038 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001039 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001040
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001041 switch (mStatus) {
1042 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001043 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001044 return INVALID_OPERATION;
1045 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001046 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001047 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001048 case STATUS_UNCONFIGURED:
1049 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001050 case STATUS_ACTIVE:
1051 // OK
1052 break;
1053 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001054 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001055 return INVALID_OPERATION;
1056 }
1057
1058 ssize_t idx = mOutputStreams.indexOfKey(id);
1059 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001060 CLOGE("Stream %d does not exist",
1061 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001062 return BAD_VALUE;
1063 }
1064
1065 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001066}
1067
1068status_t Camera3Device::deleteStream(int id) {
1069 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001070 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071 Mutex::Autolock l(mLock);
1072 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001073
Igor Murashkine2172be2013-05-28 15:31:39 -07001074 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1075
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001076 // CameraDevice semantics require device to already be idle before
1077 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001078 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001079 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1080 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001081 }
1082
Igor Murashkin2fba5842013-04-22 14:03:54 -07001083 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001084 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001085 if (mInputStream != NULL && id == mInputStream->getId()) {
1086 deletedStream = mInputStream;
1087 mInputStream.clear();
1088 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001089 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001090 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001091 return BAD_VALUE;
1092 }
Zhijun He5f446352014-01-22 09:49:33 -08001093 }
1094
1095 // Delete output stream or the output part of a bi-directional stream.
1096 if (outputStreamIdx != NAME_NOT_FOUND) {
1097 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001098 mOutputStreams.removeItem(id);
1099 }
1100
1101 // Free up the stream endpoint so that it can be used by some other stream
1102 res = deletedStream->disconnect();
1103 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001104 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001105 // fall through since we want to still list the stream as deleted.
1106 }
1107 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001108 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001109
1110 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001111}
1112
1113status_t Camera3Device::deleteReprocessStream(int id) {
1114 ATRACE_CALL();
1115 (void)id;
1116
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001117 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001118 return INVALID_OPERATION;
1119}
1120
Zhijun He1fa89992015-06-01 15:44:31 -07001121status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001122 ATRACE_CALL();
1123 ALOGV("%s: E", __FUNCTION__);
1124
1125 Mutex::Autolock il(mInterfaceLock);
1126 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001127
1128 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1129 mNeedConfig = true;
1130 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1131 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001132
1133 return configureStreamsLocked();
1134}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001135
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001136status_t Camera3Device::getInputBufferProducer(
1137 sp<IGraphicBufferProducer> *producer) {
1138 Mutex::Autolock il(mInterfaceLock);
1139 Mutex::Autolock l(mLock);
1140
1141 if (producer == NULL) {
1142 return BAD_VALUE;
1143 } else if (mInputStream == NULL) {
1144 return INVALID_OPERATION;
1145 }
1146
1147 return mInputStream->getInputBufferProducer(producer);
1148}
1149
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001150status_t Camera3Device::createDefaultRequest(int templateId,
1151 CameraMetadata *request) {
1152 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001153 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001154 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001155 Mutex::Autolock l(mLock);
1156
1157 switch (mStatus) {
1158 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001159 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001160 return INVALID_OPERATION;
1161 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001162 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001163 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001164 case STATUS_UNCONFIGURED:
1165 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001166 case STATUS_ACTIVE:
1167 // OK
1168 break;
1169 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001170 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001171 return INVALID_OPERATION;
1172 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001173
Zhijun Hea1530f12014-09-14 12:44:20 -07001174 if (!mRequestTemplateCache[templateId].isEmpty()) {
1175 *request = mRequestTemplateCache[templateId];
1176 return OK;
1177 }
1178
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001179 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001180 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001181 rawRequest = mHal3Device->ops->construct_default_request_settings(
1182 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001183 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001184 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001185 ALOGI("%s: template %d is not supported on this camera device",
1186 __FUNCTION__, templateId);
1187 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001188 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001189 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001190 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001191
1192 return OK;
1193}
1194
1195status_t Camera3Device::waitUntilDrained() {
1196 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001197 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001198 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001199
Zhijun He69a37482014-03-23 18:44:49 -07001200 return waitUntilDrainedLocked();
1201}
1202
1203status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001204 switch (mStatus) {
1205 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001206 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001207 ALOGV("%s: Already idle", __FUNCTION__);
1208 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001209 case STATUS_CONFIGURED:
1210 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001211 case STATUS_ERROR:
1212 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001213 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001214 break;
1215 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001216 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001217 return INVALID_OPERATION;
1218 }
1219
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001220 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1221 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001222 if (res != OK) {
1223 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1224 res);
1225 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001226 return res;
1227}
1228
Ruben Brunk183f0562015-08-12 12:55:02 -07001229
1230void Camera3Device::internalUpdateStatusLocked(Status status) {
1231 mStatus = status;
1232 mRecentStatusUpdates.add(mStatus);
1233 mStatusChanged.broadcast();
1234}
1235
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001236// Pause to reconfigure
1237status_t Camera3Device::internalPauseAndWaitLocked() {
1238 mRequestThread->setPaused(true);
1239 mPauseStateNotify = true;
1240
1241 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1242 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1243 if (res != OK) {
1244 SET_ERR_L("Can't idle device in %f seconds!",
1245 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001246 }
1247
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001248 return res;
1249}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001250
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001251// Resume after internalPauseAndWaitLocked
1252status_t Camera3Device::internalResumeLocked() {
1253 status_t res;
1254
1255 mRequestThread->setPaused(false);
1256
1257 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1258 if (res != OK) {
1259 SET_ERR_L("Can't transition to active in %f seconds!",
1260 kActiveTimeout/1e9);
1261 }
1262 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001263 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001264}
1265
Ruben Brunk183f0562015-08-12 12:55:02 -07001266status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001267 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001268
1269 size_t startIndex = 0;
1270 if (mStatusWaiters == 0) {
1271 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1272 // this status list
1273 mRecentStatusUpdates.clear();
1274 } else {
1275 // If other threads are waiting on updates to this status list, set the position of the
1276 // first element that this list will check rather than clearing the list.
1277 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001278 }
1279
Ruben Brunk183f0562015-08-12 12:55:02 -07001280 mStatusWaiters++;
1281
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001282 bool stateSeen = false;
1283 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001284 if (active == (mStatus == STATUS_ACTIVE)) {
1285 // Desired state is current
1286 break;
1287 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001288
1289 res = mStatusChanged.waitRelative(mLock, timeout);
1290 if (res != OK) break;
1291
Ruben Brunk183f0562015-08-12 12:55:02 -07001292 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1293 // transitions.
1294 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1295 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1296 __FUNCTION__);
1297
1298 // Encountered desired state since we began waiting
1299 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001300 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1301 stateSeen = true;
1302 break;
1303 }
1304 }
1305 } while (!stateSeen);
1306
Ruben Brunk183f0562015-08-12 12:55:02 -07001307 mStatusWaiters--;
1308
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001309 return res;
1310}
1311
1312
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001313status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1314 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001315 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001316
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001317 if (listener != NULL && mListener != NULL) {
1318 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1319 }
1320 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001321 mRequestThread->setNotificationListener(listener);
1322 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001323
1324 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001325}
1326
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001327bool Camera3Device::willNotify3A() {
1328 return false;
1329}
1330
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001331status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001332 status_t res;
1333 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001334
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001335 while (mResultQueue.empty()) {
1336 res = mResultSignal.waitRelative(mOutputLock, timeout);
1337 if (res == TIMED_OUT) {
1338 return res;
1339 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001340 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001341 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001342 return res;
1343 }
1344 }
1345 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001346}
1347
Jianing Weicb0652e2014-03-12 18:29:36 -07001348status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001349 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001350 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001351
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001352 if (mResultQueue.empty()) {
1353 return NOT_ENOUGH_DATA;
1354 }
1355
Jianing Weicb0652e2014-03-12 18:29:36 -07001356 if (frame == NULL) {
1357 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1358 return BAD_VALUE;
1359 }
1360
1361 CaptureResult &result = *(mResultQueue.begin());
1362 frame->mResultExtras = result.mResultExtras;
1363 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001364 mResultQueue.erase(mResultQueue.begin());
1365
1366 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001367}
1368
1369status_t Camera3Device::triggerAutofocus(uint32_t id) {
1370 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001371 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001372
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001373 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1374 // Mix-in this trigger into the next request and only the next request.
1375 RequestTrigger trigger[] = {
1376 {
1377 ANDROID_CONTROL_AF_TRIGGER,
1378 ANDROID_CONTROL_AF_TRIGGER_START
1379 },
1380 {
1381 ANDROID_CONTROL_AF_TRIGGER_ID,
1382 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001383 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001384 };
1385
1386 return mRequestThread->queueTrigger(trigger,
1387 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001388}
1389
1390status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1391 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001392 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001393
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001394 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1395 // Mix-in this trigger into the next request and only the next request.
1396 RequestTrigger trigger[] = {
1397 {
1398 ANDROID_CONTROL_AF_TRIGGER,
1399 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1400 },
1401 {
1402 ANDROID_CONTROL_AF_TRIGGER_ID,
1403 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001404 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001405 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001406
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001407 return mRequestThread->queueTrigger(trigger,
1408 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001409}
1410
1411status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1412 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001413 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001414
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001415 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1416 // Mix-in this trigger into the next request and only the next request.
1417 RequestTrigger trigger[] = {
1418 {
1419 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1420 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1421 },
1422 {
1423 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1424 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001425 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001426 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001427
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001428 return mRequestThread->queueTrigger(trigger,
1429 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001430}
1431
1432status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1433 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1434 ATRACE_CALL();
1435 (void)reprocessStreamId; (void)buffer; (void)listener;
1436
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001437 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001438 return INVALID_OPERATION;
1439}
1440
Jianing Weicb0652e2014-03-12 18:29:36 -07001441status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001442 ATRACE_CALL();
1443 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001444 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001445
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001446 NotificationListener* listener;
1447 {
1448 Mutex::Autolock l(mOutputLock);
1449 listener = mListener;
1450 }
1451
Zhijun He7ef20392014-04-21 16:04:17 -07001452 {
1453 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001454 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001455 }
1456
Zhijun He491e3412013-12-27 10:57:44 -08001457 status_t res;
1458 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001459 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001460 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001461 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001462 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001463 }
1464
1465 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001466}
1467
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001468status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001469 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1470}
1471
1472status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001473 ATRACE_CALL();
1474 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001475 Mutex::Autolock il(mInterfaceLock);
1476 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001477
1478 sp<Camera3StreamInterface> stream;
1479 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1480 if (outputStreamIdx == NAME_NOT_FOUND) {
1481 CLOGE("Stream %d does not exist", streamId);
1482 return BAD_VALUE;
1483 }
1484
1485 stream = mOutputStreams.editValueAt(outputStreamIdx);
1486
1487 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001488 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001489 return BAD_VALUE;
1490 }
1491
1492 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001493 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001494 return BAD_VALUE;
1495 }
1496
Ruben Brunkc78ac262015-08-13 17:58:46 -07001497 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001498}
1499
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001500status_t Camera3Device::tearDown(int streamId) {
1501 ATRACE_CALL();
1502 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1503 Mutex::Autolock il(mInterfaceLock);
1504 Mutex::Autolock l(mLock);
1505
1506 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1507 // since we cannot call register_stream_buffers except right after configure_streams.
1508 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1509 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1510 __FUNCTION__, mHal3Device->common.version);
1511 return NO_INIT;
1512 }
1513
1514 sp<Camera3StreamInterface> stream;
1515 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1516 if (outputStreamIdx == NAME_NOT_FOUND) {
1517 CLOGE("Stream %d does not exist", streamId);
1518 return BAD_VALUE;
1519 }
1520
1521 stream = mOutputStreams.editValueAt(outputStreamIdx);
1522
1523 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1524 CLOGE("Stream %d is a target of a in-progress request", streamId);
1525 return BAD_VALUE;
1526 }
1527
1528 return stream->tearDown();
1529}
1530
Zhijun He204e3292014-07-14 17:09:23 -07001531uint32_t Camera3Device::getDeviceVersion() {
1532 ATRACE_CALL();
1533 Mutex::Autolock il(mInterfaceLock);
1534 return mDeviceVersion;
1535}
1536
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001537/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001538 * Methods called by subclasses
1539 */
1540
1541void Camera3Device::notifyStatus(bool idle) {
1542 {
1543 // Need mLock to safely update state and synchronize to current
1544 // state of methods in flight.
1545 Mutex::Autolock l(mLock);
1546 // We can get various system-idle notices from the status tracker
1547 // while starting up. Only care about them if we've actually sent
1548 // in some requests recently.
1549 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1550 return;
1551 }
1552 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1553 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001554 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001555
1556 // Skip notifying listener if we're doing some user-transparent
1557 // state changes
1558 if (mPauseStateNotify) return;
1559 }
1560 NotificationListener *listener;
1561 {
1562 Mutex::Autolock l(mOutputLock);
1563 listener = mListener;
1564 }
1565 if (idle && listener != NULL) {
1566 listener->notifyIdle();
1567 }
1568}
1569
1570/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001571 * Camera3Device private methods
1572 */
1573
1574sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1575 const CameraMetadata &request) {
1576 ATRACE_CALL();
1577 status_t res;
1578
1579 sp<CaptureRequest> newRequest = new CaptureRequest;
1580 newRequest->mSettings = request;
1581
1582 camera_metadata_entry_t inputStreams =
1583 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1584 if (inputStreams.count > 0) {
1585 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001586 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001587 CLOGE("Request references unknown input stream %d",
1588 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001589 return NULL;
1590 }
1591 // Lazy completion of stream configuration (allocation/registration)
1592 // on first use
1593 if (mInputStream->isConfiguring()) {
1594 res = mInputStream->finishConfiguration(mHal3Device);
1595 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001596 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001597 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001598 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001599 return NULL;
1600 }
1601 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001602 // Check if stream is being prepared
1603 if (mInputStream->isPreparing()) {
1604 CLOGE("Request references an input stream that's being prepared!");
1605 return NULL;
1606 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001607
1608 newRequest->mInputStream = mInputStream;
1609 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1610 }
1611
1612 camera_metadata_entry_t streams =
1613 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1614 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001615 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001616 return NULL;
1617 }
1618
1619 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001620 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001621 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001622 CLOGE("Request references unknown stream %d",
1623 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001624 return NULL;
1625 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001626 sp<Camera3OutputStreamInterface> stream =
1627 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001628
1629 // Lazy completion of stream configuration (allocation/registration)
1630 // on first use
1631 if (stream->isConfiguring()) {
1632 res = stream->finishConfiguration(mHal3Device);
1633 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001634 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1635 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001636 return NULL;
1637 }
1638 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001639 // Check if stream is being prepared
1640 if (stream->isPreparing()) {
1641 CLOGE("Request references an output stream that's being prepared!");
1642 return NULL;
1643 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001644
1645 newRequest->mOutputStreams.push(stream);
1646 }
1647 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001648 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001649
1650 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001651}
1652
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001653bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1654 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1655 Size size = mSupportedOpaqueInputSizes[i];
1656 if (size.width == width && size.height == height) {
1657 return true;
1658 }
1659 }
1660
1661 return false;
1662}
1663
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001664status_t Camera3Device::configureStreamsLocked() {
1665 ATRACE_CALL();
1666 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001667
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001668 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001669 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001670 return INVALID_OPERATION;
1671 }
1672
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001673 if (!mNeedConfig) {
1674 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1675 return OK;
1676 }
1677
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001678 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1679 // adding a dummy stream instead.
1680 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1681 if (mOutputStreams.size() == 0) {
1682 addDummyStreamLocked();
1683 } else {
1684 tryRemoveDummyStreamLocked();
1685 }
1686
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001687 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001688 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001689
1690 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001691 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1692 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1693 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001694 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1695
1696 Vector<camera3_stream_t*> streams;
1697 streams.setCapacity(config.num_streams);
1698
1699 if (mInputStream != NULL) {
1700 camera3_stream_t *inputStream;
1701 inputStream = mInputStream->startConfiguration();
1702 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001703 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001704 return INVALID_OPERATION;
1705 }
1706 streams.add(inputStream);
1707 }
1708
1709 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001710
1711 // Don't configure bidi streams twice, nor add them twice to the list
1712 if (mOutputStreams[i].get() ==
1713 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1714
1715 config.num_streams--;
1716 continue;
1717 }
1718
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001719 camera3_stream_t *outputStream;
1720 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1721 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001722 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001723 return INVALID_OPERATION;
1724 }
1725 streams.add(outputStream);
1726 }
1727
1728 config.streams = streams.editArray();
1729
1730 // Do the HAL configuration; will potentially touch stream
1731 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001732 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001733 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001734 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001735
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001736 if (res == BAD_VALUE) {
1737 // HAL rejected this set of streams as unsupported, clean up config
1738 // attempt and return to unconfigured state
1739 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1740 res = mInputStream->cancelConfiguration();
1741 if (res != OK) {
1742 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1743 mInputStream->getId(), strerror(-res), res);
1744 return res;
1745 }
1746 }
1747
1748 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1749 sp<Camera3OutputStreamInterface> outputStream =
1750 mOutputStreams.editValueAt(i);
1751 if (outputStream->isConfiguring()) {
1752 res = outputStream->cancelConfiguration();
1753 if (res != OK) {
1754 SET_ERR_L(
1755 "Can't cancel configuring output stream %d: %s (%d)",
1756 outputStream->getId(), strerror(-res), res);
1757 return res;
1758 }
1759 }
1760 }
1761
1762 // Return state to that at start of call, so that future configures
1763 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001764 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001765 mNeedConfig = true;
1766
1767 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1768 return BAD_VALUE;
1769 } else if (res != OK) {
1770 // Some other kind of error from configure_streams - this is not
1771 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001772 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1773 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001774 return res;
1775 }
1776
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001777 // Finish all stream configuration immediately.
1778 // TODO: Try to relax this later back to lazy completion, which should be
1779 // faster
1780
Igor Murashkin073f8572013-05-02 14:59:28 -07001781 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001782 res = mInputStream->finishConfiguration(mHal3Device);
1783 if (res != OK) {
1784 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1785 mInputStream->getId(), strerror(-res), res);
1786 return res;
1787 }
1788 }
1789
1790 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001791 sp<Camera3OutputStreamInterface> outputStream =
1792 mOutputStreams.editValueAt(i);
1793 if (outputStream->isConfiguring()) {
1794 res = outputStream->finishConfiguration(mHal3Device);
1795 if (res != OK) {
1796 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1797 outputStream->getId(), strerror(-res), res);
1798 return res;
1799 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001800 }
1801 }
1802
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001803 // Request thread needs to know to avoid using repeat-last-settings protocol
1804 // across configure_streams() calls
1805 mRequestThread->configurationComplete();
1806
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07001807 // Boost priority of request thread for high speed recording to SCHED_FIFO
1808 if (mIsConstrainedHighSpeedConfiguration) {
1809 pid_t requestThreadTid = mRequestThread->getTid();
1810 res = requestPriority(getpid(), requestThreadTid,
1811 kConstrainedHighSpeedThreadPriority, true);
1812 if (res != OK) {
1813 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
1814 strerror(-res), res);
1815 } else {
1816 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
1817 }
1818 } else {
1819 // TODO: Set/restore normal priority for normal use cases
1820 }
1821
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001822 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001823
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001824 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001825
Ruben Brunk183f0562015-08-12 12:55:02 -07001826 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1827 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001828
1829 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1830
Zhijun He0a210512014-07-24 13:45:15 -07001831 // tear down the deleted streams after configure streams.
1832 mDeletedStreams.clear();
1833
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001834 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001835}
1836
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001837status_t Camera3Device::addDummyStreamLocked() {
1838 ATRACE_CALL();
1839 status_t res;
1840
1841 if (mDummyStreamId != NO_STREAM) {
1842 // Should never be adding a second dummy stream when one is already
1843 // active
1844 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1845 __FUNCTION__, mId);
1846 return INVALID_OPERATION;
1847 }
1848
1849 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1850
1851 sp<Camera3OutputStreamInterface> dummyStream =
1852 new Camera3DummyStream(mNextStreamId);
1853
1854 res = mOutputStreams.add(mNextStreamId, dummyStream);
1855 if (res < 0) {
1856 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1857 return res;
1858 }
1859
1860 mDummyStreamId = mNextStreamId;
1861 mNextStreamId++;
1862
1863 return OK;
1864}
1865
1866status_t Camera3Device::tryRemoveDummyStreamLocked() {
1867 ATRACE_CALL();
1868 status_t res;
1869
1870 if (mDummyStreamId == NO_STREAM) return OK;
1871 if (mOutputStreams.size() == 1) return OK;
1872
1873 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1874
1875 // Ok, have a dummy stream and there's at least one other output stream,
1876 // so remove the dummy
1877
1878 sp<Camera3StreamInterface> deletedStream;
1879 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1880 if (outputStreamIdx == NAME_NOT_FOUND) {
1881 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1882 return INVALID_OPERATION;
1883 }
1884
1885 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1886 mOutputStreams.removeItemsAt(outputStreamIdx);
1887
1888 // Free up the stream endpoint so that it can be used by some other stream
1889 res = deletedStream->disconnect();
1890 if (res != OK) {
1891 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1892 // fall through since we want to still list the stream as deleted.
1893 }
1894 mDeletedStreams.add(deletedStream);
1895 mDummyStreamId = NO_STREAM;
1896
1897 return res;
1898}
1899
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001900void Camera3Device::setErrorState(const char *fmt, ...) {
1901 Mutex::Autolock l(mLock);
1902 va_list args;
1903 va_start(args, fmt);
1904
1905 setErrorStateLockedV(fmt, args);
1906
1907 va_end(args);
1908}
1909
1910void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1911 Mutex::Autolock l(mLock);
1912 setErrorStateLockedV(fmt, args);
1913}
1914
1915void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1916 va_list args;
1917 va_start(args, fmt);
1918
1919 setErrorStateLockedV(fmt, args);
1920
1921 va_end(args);
1922}
1923
1924void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001925 // Print out all error messages to log
1926 String8 errorCause = String8::formatV(fmt, args);
1927 ALOGE("Camera %d: %s", mId, errorCause.string());
1928
1929 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001930 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001931
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001932 mErrorCause = errorCause;
1933
1934 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07001935 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001936
1937 // Notify upstream about a device error
1938 if (mListener != NULL) {
1939 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1940 CaptureResultExtras());
1941 }
1942
1943 // Save stack trace. View by dumping it later.
1944 CameraTraces::saveTrace();
1945 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001946}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001947
1948/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001949 * In-flight request management
1950 */
1951
Jianing Weicb0652e2014-03-12 18:29:36 -07001952status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07001953 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1954 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001955 ATRACE_CALL();
1956 Mutex::Autolock l(mInFlightLock);
1957
1958 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07001959 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
1960 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001961 if (res < 0) return res;
1962
1963 return OK;
1964}
1965
1966/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001967 * Check if all 3A fields are ready, and send off a partial 3A-only result
1968 * to the output frame queue
1969 */
Zhijun He204e3292014-07-14 17:09:23 -07001970bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001971 uint32_t frameNumber,
1972 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001973
1974 // Check if all 3A states are present
1975 // The full list of fields is
1976 // android.control.afMode
1977 // android.control.awbMode
1978 // android.control.aeState
1979 // android.control.awbState
1980 // android.control.afState
1981 // android.control.afTriggerID
1982 // android.control.aePrecaptureID
1983 // TODO: Add android.control.aeMode
1984
1985 bool gotAllStates = true;
1986
1987 uint8_t afMode;
1988 uint8_t awbMode;
1989 uint8_t aeState;
1990 uint8_t afState;
1991 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001992
1993 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1994 &afMode, frameNumber);
1995
1996 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1997 &awbMode, frameNumber);
1998
1999 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
2000 &aeState, frameNumber);
2001
2002 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
2003 &afState, frameNumber);
2004
2005 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
2006 &awbState, frameNumber);
2007
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002008 if (!gotAllStates) return false;
2009
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002010 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002011 "AF state %d, AE state %d, AWB state %d, "
2012 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002013 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002014 afMode, awbMode,
2015 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002016 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002017
2018 // Got all states, so construct a minimal result to send
2019 // In addition to the above fields, this means adding in
2020 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002021 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07002022 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002023
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002024 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002025
2026 Mutex::Autolock l(mOutputLock);
2027
Jianing Weicb0652e2014-03-12 18:29:36 -07002028 CaptureResult captureResult;
2029 captureResult.mResultExtras = resultExtras;
2030 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
2031 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
2032 // but not limited to CameraDeviceBase::getNextResult
2033 CaptureResult& min3AResult =
2034 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002035
Jianing Weicb0652e2014-03-12 18:29:36 -07002036 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
2037 // TODO: This is problematic casting. Need to fix CameraMetadata.
2038 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002039 return false;
2040 }
2041
Jianing Weicb0652e2014-03-12 18:29:36 -07002042 int32_t requestId = resultExtras.requestId;
2043 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002044 &requestId, frameNumber)) {
2045 return false;
2046 }
2047
Zhijun He204e3292014-07-14 17:09:23 -07002048 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
2049 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
2050 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
2051 &partialResult, frameNumber)) {
2052 return false;
2053 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002054 }
2055
Jianing Weicb0652e2014-03-12 18:29:36 -07002056 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002057 &afMode, frameNumber)) {
2058 return false;
2059 }
2060
Jianing Weicb0652e2014-03-12 18:29:36 -07002061 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002062 &awbMode, frameNumber)) {
2063 return false;
2064 }
2065
Jianing Weicb0652e2014-03-12 18:29:36 -07002066 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002067 &aeState, frameNumber)) {
2068 return false;
2069 }
2070
Jianing Weicb0652e2014-03-12 18:29:36 -07002071 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002072 &afState, frameNumber)) {
2073 return false;
2074 }
2075
Jianing Weicb0652e2014-03-12 18:29:36 -07002076 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002077 &awbState, frameNumber)) {
2078 return false;
2079 }
2080
Jianing Weicb0652e2014-03-12 18:29:36 -07002081 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002082 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002083 return false;
2084 }
2085
Jianing Weicb0652e2014-03-12 18:29:36 -07002086 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002087 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002088 return false;
2089 }
2090
Zhijun He204e3292014-07-14 17:09:23 -07002091 // We only send the aggregated partial when all 3A related metadata are available
2092 // For both API1 and API2.
2093 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002094 mResultSignal.signal();
2095
2096 return true;
2097}
2098
2099template<typename T>
2100bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002101 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002102 (void) frameNumber;
2103
2104 camera_metadata_ro_entry_t entry;
2105
2106 entry = result.find(tag);
2107 if (entry.count == 0) {
2108 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2109 mId, frameNumber, get_camera_metadata_tag_name(tag));
2110 return false;
2111 }
2112
2113 if (sizeof(T) == sizeof(uint8_t)) {
2114 *value = entry.data.u8[0];
2115 } else if (sizeof(T) == sizeof(int32_t)) {
2116 *value = entry.data.i32[0];
2117 } else {
2118 ALOGE("%s: Unexpected type", __FUNCTION__);
2119 return false;
2120 }
2121 return true;
2122}
2123
2124template<typename T>
2125bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002126 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002127 if (result.update(tag, value, 1) != NO_ERROR) {
2128 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2129 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2130 frameNumber, get_camera_metadata_tag_name(tag));
2131 return false;
2132 }
2133 return true;
2134}
2135
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002136void Camera3Device::returnOutputBuffers(
2137 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2138 nsecs_t timestamp) {
2139 for (size_t i = 0; i < numBuffers; i++)
2140 {
2141 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2142 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2143 // Note: stream may be deallocated at this point, if this buffer was
2144 // the last reference to it.
2145 if (res != OK) {
2146 ALOGE("Can't return buffer to its stream: %s (%d)",
2147 strerror(-res), res);
2148 }
2149 }
2150}
2151
2152
2153void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2154
2155 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2156 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2157
2158 nsecs_t sensorTimestamp = request.sensorTimestamp;
2159 nsecs_t shutterTimestamp = request.shutterTimestamp;
2160
2161 // Check if it's okay to remove the request from InFlightMap:
2162 // In the case of a successful request:
2163 // all input and output buffers, all result metadata, shutter callback
2164 // arrived.
2165 // In the case of a unsuccessful request:
2166 // all input and output buffers arrived.
2167 if (request.numBuffersLeft == 0 &&
2168 (request.requestStatus != OK ||
2169 (request.haveResultMetadata && shutterTimestamp != 0))) {
2170 ATRACE_ASYNC_END("frame capture", frameNumber);
2171
2172 // Sanity check - if sensor timestamp matches shutter timestamp
2173 if (request.requestStatus == OK &&
2174 sensorTimestamp != shutterTimestamp) {
2175 SET_ERR("sensor timestamp (%" PRId64
2176 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2177 sensorTimestamp, frameNumber, shutterTimestamp);
2178 }
2179
2180 // for an unsuccessful request, it may have pending output buffers to
2181 // return.
2182 assert(request.requestStatus != OK ||
2183 request.pendingOutputBuffers.size() == 0);
2184 returnOutputBuffers(request.pendingOutputBuffers.array(),
2185 request.pendingOutputBuffers.size(), 0);
2186
2187 mInFlightMap.removeItemsAt(idx, 1);
2188
2189 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2190 }
2191
2192 // Sanity check - if we have too many in-flight frames, something has
2193 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002194 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002195 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002196 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2197 kInFlightWarnLimitHighSpeed) {
2198 CLOGE("In-flight list too large for high speed configuration: %zu",
2199 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002200 }
2201}
2202
2203
2204void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2205 CaptureResultExtras &resultExtras,
2206 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002207 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002208 bool reprocess,
2209 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002210 if (pendingMetadata.isEmpty())
2211 return;
2212
2213 Mutex::Autolock l(mOutputLock);
2214
2215 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002216 if (reprocess) {
2217 if (frameNumber < mNextReprocessResultFrameNumber) {
2218 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002219 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002220 frameNumber, mNextReprocessResultFrameNumber);
2221 return;
2222 }
2223 mNextReprocessResultFrameNumber = frameNumber + 1;
2224 } else {
2225 if (frameNumber < mNextResultFrameNumber) {
2226 SET_ERR("Out-of-order capture result metadata submitted! "
2227 "(got frame number %d, expecting %d)",
2228 frameNumber, mNextResultFrameNumber);
2229 return;
2230 }
2231 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002232 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002233
2234 CaptureResult captureResult;
2235 captureResult.mResultExtras = resultExtras;
2236 captureResult.mMetadata = pendingMetadata;
2237
2238 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2239 (int32_t*)&frameNumber, 1) != OK) {
2240 SET_ERR("Failed to set frame# in metadata (%d)",
2241 frameNumber);
2242 return;
2243 } else {
2244 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2245 __FUNCTION__, mId, frameNumber);
2246 }
2247
2248 // Append any previous partials to form a complete result
2249 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2250 captureResult.mMetadata.append(collectedPartialResult);
2251 }
2252
2253 captureResult.mMetadata.sort();
2254
2255 // Check that there's a timestamp in the result metadata
2256 camera_metadata_entry entry =
2257 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2258 if (entry.count == 0) {
2259 SET_ERR("No timestamp provided by HAL for frame %d!",
2260 frameNumber);
2261 return;
2262 }
2263
Chien-Yu Chend196d612015-06-22 19:49:01 -07002264 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2265
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002266 // Valid result, insert into queue
2267 List<CaptureResult>::iterator queuedResult =
2268 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2269 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2270 ", burstId = %" PRId32, __FUNCTION__,
2271 queuedResult->mResultExtras.requestId,
2272 queuedResult->mResultExtras.frameNumber,
2273 queuedResult->mResultExtras.burstId);
2274
2275 mResultSignal.signal();
2276}
2277
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002278/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002279 * Camera HAL device callback methods
2280 */
2281
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002282void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002283 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002284
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002285 status_t res;
2286
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002287 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002288 if (result->result == NULL && result->num_output_buffers == 0 &&
2289 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002290 SET_ERR("No result data provided by HAL for frame %d",
2291 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002292 return;
2293 }
Zhijun He204e3292014-07-14 17:09:23 -07002294
2295 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2296 // partial_result to 1 when metadata is included in this result.
2297 if (!mUsePartialResult &&
2298 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2299 result->result != NULL &&
2300 result->partial_result != 1) {
2301 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2302 " if partial result is not supported",
2303 frameNumber, result->partial_result);
2304 return;
2305 }
2306
2307 bool isPartialResult = false;
2308 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002309 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002310 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002311
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002312 // Get shutter timestamp and resultExtras from list of in-flight requests,
2313 // where it was added by the shutter notification for this frame. If the
2314 // shutter timestamp isn't received yet, append the output buffers to the
2315 // in-flight request and they will be returned when the shutter timestamp
2316 // arrives. Update the in-flight status and remove the in-flight entry if
2317 // all result data and shutter timestamp have been received.
2318 nsecs_t shutterTimestamp = 0;
2319
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002320 {
2321 Mutex::Autolock l(mInFlightLock);
2322 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2323 if (idx == NAME_NOT_FOUND) {
2324 SET_ERR("Unknown frame number for capture result: %d",
2325 frameNumber);
2326 return;
2327 }
2328 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002329 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2330 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2331 ", partialResultCount = %d",
2332 __FUNCTION__, request.resultExtras.requestId,
2333 request.resultExtras.frameNumber, request.resultExtras.burstId,
2334 result->partial_result);
2335 // Always update the partial count to the latest one if it's not 0
2336 // (buffers only). When framework aggregates adjacent partial results
2337 // into one, the latest partial count will be used.
2338 if (result->partial_result != 0)
2339 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002340
2341 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002342 if (mUsePartialResult && result->result != NULL) {
2343 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2344 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2345 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2346 " the range of [1, %d] when metadata is included in the result",
2347 frameNumber, result->partial_result, mNumPartialResults);
2348 return;
2349 }
2350 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002351 if (isPartialResult) {
2352 request.partialResult.collectedResult.append(result->result);
2353 }
Zhijun He204e3292014-07-14 17:09:23 -07002354 } else {
2355 camera_metadata_ro_entry_t partialResultEntry;
2356 res = find_camera_metadata_ro_entry(result->result,
2357 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2358 if (res != NAME_NOT_FOUND &&
2359 partialResultEntry.count > 0 &&
2360 partialResultEntry.data.u8[0] ==
2361 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2362 // A partial result. Flag this as such, and collect this
2363 // set of metadata into the in-flight entry.
2364 isPartialResult = true;
2365 request.partialResult.collectedResult.append(
2366 result->result);
2367 request.partialResult.collectedResult.erase(
2368 ANDROID_QUIRKS_PARTIAL_RESULT);
2369 }
2370 }
2371
2372 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002373 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002374 if (!request.partialResult.haveSent3A) {
2375 request.partialResult.haveSent3A =
2376 processPartial3AResult(frameNumber,
2377 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002378 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002379 }
2380 }
2381 }
2382
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002383 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002384 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002385
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002386 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002387 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002388 if (request.haveResultMetadata) {
2389 SET_ERR("Called multiple times with metadata for frame %d",
2390 frameNumber);
2391 return;
2392 }
Zhijun He204e3292014-07-14 17:09:23 -07002393 if (mUsePartialResult &&
2394 !request.partialResult.collectedResult.isEmpty()) {
2395 collectedPartialResult.acquire(
2396 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002397 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002398 request.haveResultMetadata = true;
2399 }
2400
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002401 uint32_t numBuffersReturned = result->num_output_buffers;
2402 if (result->input_buffer != NULL) {
2403 if (hasInputBufferInRequest) {
2404 numBuffersReturned += 1;
2405 } else {
2406 ALOGW("%s: Input buffer should be NULL if there is no input"
2407 " buffer sent in the request",
2408 __FUNCTION__);
2409 }
2410 }
2411 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002412 if (request.numBuffersLeft < 0) {
2413 SET_ERR("Too many buffers returned for frame %d",
2414 frameNumber);
2415 return;
2416 }
2417
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002418 camera_metadata_ro_entry_t entry;
2419 res = find_camera_metadata_ro_entry(result->result,
2420 ANDROID_SENSOR_TIMESTAMP, &entry);
2421 if (res == OK && entry.count == 1) {
2422 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002423 }
2424
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002425 // If shutter event isn't received yet, append the output buffers to
2426 // the in-flight request. Otherwise, return the output buffers to
2427 // streams.
2428 if (shutterTimestamp == 0) {
2429 request.pendingOutputBuffers.appendArray(result->output_buffers,
2430 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002431 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002432 returnOutputBuffers(result->output_buffers,
2433 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002434 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002435
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002436 if (result->result != NULL && !isPartialResult) {
2437 if (shutterTimestamp == 0) {
2438 request.pendingMetadata = result->result;
2439 request.partialResult.collectedResult = collectedPartialResult;
2440 } else {
2441 CameraMetadata metadata;
2442 metadata = result->result;
2443 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002444 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2445 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002446 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002447 }
2448
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002449 removeInFlightRequestIfReadyLocked(idx);
2450 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002451
Zhijun Hef0d962a2014-06-30 10:24:11 -07002452 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002453 if (hasInputBufferInRequest) {
2454 Camera3Stream *stream =
2455 Camera3Stream::cast(result->input_buffer->stream);
2456 res = stream->returnInputBuffer(*(result->input_buffer));
2457 // Note: stream may be deallocated at this point, if this buffer was the
2458 // last reference to it.
2459 if (res != OK) {
2460 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2461 " its stream:%s (%d)", __FUNCTION__,
2462 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002463 }
2464 } else {
2465 ALOGW("%s: Input buffer should be NULL if there is no input"
2466 " buffer sent in the request, skipping input buffer return.",
2467 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002468 }
2469 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002470}
2471
2472void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002473 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002474 NotificationListener *listener;
2475 {
2476 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002477 listener = mListener;
2478 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002479
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002480 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002481 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002482 return;
2483 }
2484
2485 switch (msg->type) {
2486 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002487 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002488 break;
2489 }
2490 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002491 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002492 break;
2493 }
2494 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002495 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002496 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002497 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002498}
2499
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002500void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2501 NotificationListener *listener) {
2502
2503 // Map camera HAL error codes to ICameraDeviceCallback error codes
2504 // Index into this with the HAL error code
2505 static const ICameraDeviceCallbacks::CameraErrorCode
2506 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2507 // 0 = Unused error code
2508 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2509 // 1 = CAMERA3_MSG_ERROR_DEVICE
2510 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2511 // 2 = CAMERA3_MSG_ERROR_REQUEST
2512 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2513 // 3 = CAMERA3_MSG_ERROR_RESULT
2514 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2515 // 4 = CAMERA3_MSG_ERROR_BUFFER
2516 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2517 };
2518
2519 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2520 ((msg.error_code >= 0) &&
2521 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2522 halErrorMap[msg.error_code] :
2523 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2524
2525 int streamId = 0;
2526 if (msg.error_stream != NULL) {
2527 Camera3Stream *stream =
2528 Camera3Stream::cast(msg.error_stream);
2529 streamId = stream->getId();
2530 }
2531 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2532 mId, __FUNCTION__, msg.frame_number,
2533 streamId, msg.error_code);
2534
2535 CaptureResultExtras resultExtras;
2536 switch (errorCode) {
2537 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2538 // SET_ERR calls notifyError
2539 SET_ERR("Camera HAL reported serious device error");
2540 break;
2541 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2542 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2543 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2544 {
2545 Mutex::Autolock l(mInFlightLock);
2546 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2547 if (idx >= 0) {
2548 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2549 r.requestStatus = msg.error_code;
2550 resultExtras = r.resultExtras;
2551 } else {
2552 resultExtras.frameNumber = msg.frame_number;
2553 ALOGE("Camera %d: %s: cannot find in-flight request on "
2554 "frame %" PRId64 " error", mId, __FUNCTION__,
2555 resultExtras.frameNumber);
2556 }
2557 }
2558 if (listener != NULL) {
2559 listener->notifyError(errorCode, resultExtras);
2560 } else {
2561 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2562 }
2563 break;
2564 default:
2565 // SET_ERR calls notifyError
2566 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2567 break;
2568 }
2569}
2570
2571void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2572 NotificationListener *listener) {
2573 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002574
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002575 // Set timestamp for the request in the in-flight tracking
2576 // and get the request ID to send upstream
2577 {
2578 Mutex::Autolock l(mInFlightLock);
2579 idx = mInFlightMap.indexOfKey(msg.frame_number);
2580 if (idx >= 0) {
2581 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002582
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002583 // Verify ordering of shutter notifications
2584 {
2585 Mutex::Autolock l(mOutputLock);
2586 // TODO: need to track errors for tighter bounds on expected frame number.
2587 if (r.hasInputBuffer) {
2588 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2589 SET_ERR("Shutter notification out-of-order. Expected "
2590 "notification for frame %d, got frame %d",
2591 mNextReprocessShutterFrameNumber, msg.frame_number);
2592 return;
2593 }
2594 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2595 } else {
2596 if (msg.frame_number < mNextShutterFrameNumber) {
2597 SET_ERR("Shutter notification out-of-order. Expected "
2598 "notification for frame %d, got frame %d",
2599 mNextShutterFrameNumber, msg.frame_number);
2600 return;
2601 }
2602 mNextShutterFrameNumber = msg.frame_number + 1;
2603 }
2604 }
2605
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002606 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2607 mId, __FUNCTION__,
2608 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2609 // Call listener, if any
2610 if (listener != NULL) {
2611 listener->notifyShutter(r.resultExtras, msg.timestamp);
2612 }
2613
2614 r.shutterTimestamp = msg.timestamp;
2615
2616 // send pending result and buffers
2617 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002618 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002619 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002620 returnOutputBuffers(r.pendingOutputBuffers.array(),
2621 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2622 r.pendingOutputBuffers.clear();
2623
2624 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002625 }
2626 }
2627 if (idx < 0) {
2628 SET_ERR("Shutter notification for non-existent frame number %d",
2629 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002630 }
2631}
2632
2633
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002634CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002635 ALOGV("%s", __FUNCTION__);
2636
Igor Murashkin1e479c02013-09-06 16:55:14 -07002637 CameraMetadata retVal;
2638
2639 if (mRequestThread != NULL) {
2640 retVal = mRequestThread->getLatestRequest();
2641 }
2642
Igor Murashkin1e479c02013-09-06 16:55:14 -07002643 return retVal;
2644}
2645
Jianing Weicb0652e2014-03-12 18:29:36 -07002646
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002647/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002648 * RequestThread inner class methods
2649 */
2650
2651Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002652 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002653 camera3_device_t *hal3Device,
2654 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002655 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002656 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002657 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002658 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002659 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002660 mReconfigured(false),
2661 mDoPause(false),
2662 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002663 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002664 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002665 mCurrentAfTriggerId(0),
2666 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002667 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2668 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002669 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002670}
2671
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002672void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002673 NotificationListener *listener) {
2674 Mutex::Autolock l(mRequestLock);
2675 mListener = listener;
2676}
2677
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002678void Camera3Device::RequestThread::configurationComplete() {
2679 Mutex::Autolock l(mRequestLock);
2680 mReconfigured = true;
2681}
2682
Jianing Wei90e59c92014-03-12 18:29:36 -07002683status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002684 List<sp<CaptureRequest> > &requests,
2685 /*out*/
2686 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002687 Mutex::Autolock l(mRequestLock);
2688 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2689 ++it) {
2690 mRequestQueue.push_back(*it);
2691 }
2692
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002693 if (lastFrameNumber != NULL) {
2694 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2695 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2696 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2697 *lastFrameNumber);
2698 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002699
Jianing Wei90e59c92014-03-12 18:29:36 -07002700 unpauseForNewRequests();
2701
2702 return OK;
2703}
2704
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002705
2706status_t Camera3Device::RequestThread::queueTrigger(
2707 RequestTrigger trigger[],
2708 size_t count) {
2709
2710 Mutex::Autolock l(mTriggerMutex);
2711 status_t ret;
2712
2713 for (size_t i = 0; i < count; ++i) {
2714 ret = queueTriggerLocked(trigger[i]);
2715
2716 if (ret != OK) {
2717 return ret;
2718 }
2719 }
2720
2721 return OK;
2722}
2723
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002724int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2725 sp<Camera3Device> d = device.promote();
2726 if (d != NULL) return d->mId;
2727 return 0;
2728}
2729
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002730status_t Camera3Device::RequestThread::queueTriggerLocked(
2731 RequestTrigger trigger) {
2732
2733 uint32_t tag = trigger.metadataTag;
2734 ssize_t index = mTriggerMap.indexOfKey(tag);
2735
2736 switch (trigger.getTagType()) {
2737 case TYPE_BYTE:
2738 // fall-through
2739 case TYPE_INT32:
2740 break;
2741 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002742 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2743 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002744 return INVALID_OPERATION;
2745 }
2746
2747 /**
2748 * Collect only the latest trigger, since we only have 1 field
2749 * in the request settings per trigger tag, and can't send more than 1
2750 * trigger per request.
2751 */
2752 if (index != NAME_NOT_FOUND) {
2753 mTriggerMap.editValueAt(index) = trigger;
2754 } else {
2755 mTriggerMap.add(tag, trigger);
2756 }
2757
2758 return OK;
2759}
2760
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002761status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002762 const RequestList &requests,
2763 /*out*/
2764 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002765 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002766 if (lastFrameNumber != NULL) {
2767 *lastFrameNumber = mRepeatingLastFrameNumber;
2768 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002769 mRepeatingRequests.clear();
2770 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2771 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002772
2773 unpauseForNewRequests();
2774
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002775 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002776 return OK;
2777}
2778
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002779bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2780 if (mRepeatingRequests.empty()) {
2781 return false;
2782 }
2783 int32_t requestId = requestIn->mResultExtras.requestId;
2784 const RequestList &repeatRequests = mRepeatingRequests;
2785 // All repeating requests are guaranteed to have same id so only check first quest
2786 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2787 return (firstRequest->mResultExtras.requestId == requestId);
2788}
2789
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002790status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002791 Mutex::Autolock l(mRequestLock);
2792 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002793 if (lastFrameNumber != NULL) {
2794 *lastFrameNumber = mRepeatingLastFrameNumber;
2795 }
2796 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002797 return OK;
2798}
2799
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002800status_t Camera3Device::RequestThread::clear(
2801 NotificationListener *listener,
2802 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002803 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002804 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002805
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002806 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002807
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002808 // Send errors for all requests pending in the request queue, including
2809 // pending repeating requests
2810 if (listener != NULL) {
2811 for (RequestList::iterator it = mRequestQueue.begin();
2812 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002813 // Abort the input buffers for reprocess requests.
2814 if ((*it)->mInputStream != NULL) {
2815 camera3_stream_buffer_t inputBuffer;
2816 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2817 if (res != OK) {
2818 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2819 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2820 } else {
2821 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2822 if (res != OK) {
2823 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2824 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2825 }
2826 }
2827 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002828 // Set the frame number this request would have had, if it
2829 // had been submitted; this frame number will not be reused.
2830 // The requestId and burstId fields were set when the request was
2831 // submitted originally (in convertMetadataListToRequestListLocked)
2832 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2833 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2834 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002835 }
2836 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002837 mRequestQueue.clear();
2838 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002839 if (lastFrameNumber != NULL) {
2840 *lastFrameNumber = mRepeatingLastFrameNumber;
2841 }
2842 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002843 return OK;
2844}
2845
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002846status_t Camera3Device::RequestThread::flush() {
2847 ATRACE_CALL();
2848 Mutex::Autolock l(mFlushLock);
2849
2850 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2851 return mHal3Device->ops->flush(mHal3Device);
2852 }
2853
2854 return -ENOTSUP;
2855}
2856
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002857void Camera3Device::RequestThread::setPaused(bool paused) {
2858 Mutex::Autolock l(mPauseLock);
2859 mDoPause = paused;
2860 mDoPauseSignal.signal();
2861}
2862
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002863status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2864 int32_t requestId, nsecs_t timeout) {
2865 Mutex::Autolock l(mLatestRequestMutex);
2866 status_t res;
2867 while (mLatestRequestId != requestId) {
2868 nsecs_t startTime = systemTime();
2869
2870 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2871 if (res != OK) return res;
2872
2873 timeout -= (systemTime() - startTime);
2874 }
2875
2876 return OK;
2877}
2878
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002879void Camera3Device::RequestThread::requestExit() {
2880 // Call parent to set up shutdown
2881 Thread::requestExit();
2882 // The exit from any possible waits
2883 mDoPauseSignal.signal();
2884 mRequestSignal.signal();
2885}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002886
Chien-Yu Chend196d612015-06-22 19:49:01 -07002887
2888/**
2889 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2890 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2891 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2892 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2893 * request.
2894 */
2895void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2896 request->mAeTriggerCancelOverride.applyAeLock = false;
2897 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2898
2899 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2900 return;
2901 }
2902
2903 camera_metadata_entry_t aePrecaptureTrigger =
2904 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2905 if (aePrecaptureTrigger.count > 0 &&
2906 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2907 // Always override CANCEL to IDLE
2908 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2909 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2910 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2911 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2912 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2913
2914 if (mAeLockAvailable == true) {
2915 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2916 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2917 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2918 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2919 request->mAeTriggerCancelOverride.applyAeLock = true;
2920 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2921 }
2922 }
2923 }
2924}
2925
2926/**
2927 * Override result metadata for cancelling AE precapture trigger applied in
2928 * handleAePrecaptureCancelRequest().
2929 */
2930void Camera3Device::overrideResultForPrecaptureCancel(
2931 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2932 if (aeTriggerCancelOverride.applyAeLock) {
2933 // Only devices <= v3.2 should have this override
2934 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2935 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2936 }
2937
2938 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2939 // Only devices <= v3.2 should have this override
2940 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2941 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2942 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2943 }
2944}
2945
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002946bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002947 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002948 status_t res;
2949
2950 // Handle paused state.
2951 if (waitIfPaused()) {
2952 return true;
2953 }
2954
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002955 // Wait for the next batch of requests.
2956 waitForNextRequestBatch();
2957 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002958 return true;
2959 }
2960
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002961 // Get the latest request ID, if any
2962 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002963 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002964 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002965 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002966 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002967 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002968 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
2969 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002970 }
2971
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002972 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002973 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002974 if (res == TIMED_OUT) {
2975 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002976 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002977 return true;
2978 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002979 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002980 return false;
2981 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002982
Zhijun Hecc27e112013-10-03 16:12:43 -07002983 // Inform waitUntilRequestProcessed thread of a new request ID
2984 {
2985 Mutex::Autolock al(mLatestRequestMutex);
2986
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002987 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07002988 mLatestRequestSignal.signal();
2989 }
2990
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002991 // Submit a batch of requests to HAL.
2992 // Use flush lock only when submitting multilple requests in a batch.
2993 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
2994 // which may take a long time to finish so synchronizing flush() and
2995 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
2996 // For now, only synchronize for high speed recording and we should figure something out for
2997 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002998 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002999
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003000 if (useFlushLock) {
3001 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003002 }
3003
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003004 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003005 mNextRequests.size());
3006 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003007 // Submit request and block until ready for next one
3008 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3009 ATRACE_BEGIN("camera3->process_capture_request");
3010 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3011 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003012
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003013 if (res != OK) {
3014 // Should only get a failure here for malformed requests or device-level
3015 // errors, so consider all errors fatal. Bad metadata failures should
3016 // come through notify.
3017 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3018 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3019 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003020 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003021 if (useFlushLock) {
3022 mFlushLock.unlock();
3023 }
3024 return false;
3025 }
3026
3027 // Mark that the request has be submitted successfully.
3028 nextRequest.submitted = true;
3029
3030 // Update the latest request sent to HAL
3031 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3032 Mutex::Autolock al(mLatestRequestMutex);
3033
3034 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3035 mLatestRequest.acquire(cloned);
3036 }
3037
3038 if (nextRequest.halRequest.settings != NULL) {
3039 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3040 }
3041
3042 // Remove any previously queued triggers (after unlock)
3043 res = removeTriggers(mPrevRequest);
3044 if (res != OK) {
3045 SET_ERR("RequestThread: Unable to remove triggers "
3046 "(capture request %d, HAL device: %s (%d)",
3047 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003048 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003049 if (useFlushLock) {
3050 mFlushLock.unlock();
3051 }
3052 return false;
3053 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003054 }
3055
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003056 if (useFlushLock) {
3057 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003058 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003059
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003060 // Unset as current request
3061 {
3062 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003063 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003064 }
3065
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003066 return true;
3067}
3068
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003069status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003070 ATRACE_CALL();
3071
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003072 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003073 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3074 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3075 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3076
3077 // Prepare a request to HAL
3078 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3079
3080 // Insert any queued triggers (before metadata is locked)
3081 status_t res = insertTriggers(captureRequest);
3082
3083 if (res < 0) {
3084 SET_ERR("RequestThread: Unable to insert triggers "
3085 "(capture request %d, HAL device: %s (%d)",
3086 halRequest->frame_number, strerror(-res), res);
3087 return INVALID_OPERATION;
3088 }
3089 int triggerCount = res;
3090 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3091 mPrevTriggers = triggerCount;
3092
3093 // If the request is the same as last, or we had triggers last time
3094 if (mPrevRequest != captureRequest || triggersMixedIn) {
3095 /**
3096 * HAL workaround:
3097 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3098 */
3099 res = addDummyTriggerIds(captureRequest);
3100 if (res != OK) {
3101 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3102 "(capture request %d, HAL device: %s (%d)",
3103 halRequest->frame_number, strerror(-res), res);
3104 return INVALID_OPERATION;
3105 }
3106
3107 /**
3108 * The request should be presorted so accesses in HAL
3109 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3110 */
3111 captureRequest->mSettings.sort();
3112 halRequest->settings = captureRequest->mSettings.getAndLock();
3113 mPrevRequest = captureRequest;
3114 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3115
3116 IF_ALOGV() {
3117 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3118 find_camera_metadata_ro_entry(
3119 halRequest->settings,
3120 ANDROID_CONTROL_AF_TRIGGER,
3121 &e
3122 );
3123 if (e.count > 0) {
3124 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3125 __FUNCTION__,
3126 halRequest->frame_number,
3127 e.data.u8[0]);
3128 }
3129 }
3130 } else {
3131 // leave request.settings NULL to indicate 'reuse latest given'
3132 ALOGVV("%s: Request settings are REUSED",
3133 __FUNCTION__);
3134 }
3135
3136 uint32_t totalNumBuffers = 0;
3137
3138 // Fill in buffers
3139 if (captureRequest->mInputStream != NULL) {
3140 halRequest->input_buffer = &captureRequest->mInputBuffer;
3141 totalNumBuffers += 1;
3142 } else {
3143 halRequest->input_buffer = NULL;
3144 }
3145
3146 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3147 captureRequest->mOutputStreams.size());
3148 halRequest->output_buffers = outputBuffers->array();
3149 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3150 res = captureRequest->mOutputStreams.editItemAt(i)->
3151 getBuffer(&outputBuffers->editItemAt(i));
3152 if (res != OK) {
3153 // Can't get output buffer from gralloc queue - this could be due to
3154 // abandoned queue or other consumer misbehavior, so not a fatal
3155 // error
3156 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3157 " %s (%d)", strerror(-res), res);
3158
3159 return TIMED_OUT;
3160 }
3161 halRequest->num_output_buffers++;
3162 }
3163 totalNumBuffers += halRequest->num_output_buffers;
3164
3165 // Log request in the in-flight queue
3166 sp<Camera3Device> parent = mParent.promote();
3167 if (parent == NULL) {
3168 // Should not happen, and nowhere to send errors to, so just log it
3169 CLOGE("RequestThread: Parent is gone");
3170 return INVALID_OPERATION;
3171 }
3172 res = parent->registerInFlight(halRequest->frame_number,
3173 totalNumBuffers, captureRequest->mResultExtras,
3174 /*hasInput*/halRequest->input_buffer != NULL,
3175 captureRequest->mAeTriggerCancelOverride);
3176 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3177 ", burstId = %" PRId32 ".",
3178 __FUNCTION__,
3179 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3180 captureRequest->mResultExtras.burstId);
3181 if (res != OK) {
3182 SET_ERR("RequestThread: Unable to register new in-flight request:"
3183 " %s (%d)", strerror(-res), res);
3184 return INVALID_OPERATION;
3185 }
3186 }
3187
3188 return OK;
3189}
3190
Igor Murashkin1e479c02013-09-06 16:55:14 -07003191CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3192 Mutex::Autolock al(mLatestRequestMutex);
3193
3194 ALOGV("RequestThread::%s", __FUNCTION__);
3195
3196 return mLatestRequest;
3197}
3198
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003199bool Camera3Device::RequestThread::isStreamPending(
3200 sp<Camera3StreamInterface>& stream) {
3201 Mutex::Autolock l(mRequestLock);
3202
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003203 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003204 if (!nextRequest.submitted) {
3205 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3206 if (stream == s) return true;
3207 }
3208 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003209 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003210 }
3211
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003212 for (const auto& request : mRequestQueue) {
3213 for (const auto& s : request->mOutputStreams) {
3214 if (stream == s) return true;
3215 }
3216 if (stream == request->mInputStream) return true;
3217 }
3218
3219 for (const auto& request : mRepeatingRequests) {
3220 for (const auto& s : request->mOutputStreams) {
3221 if (stream == s) return true;
3222 }
3223 if (stream == request->mInputStream) return true;
3224 }
3225
3226 return false;
3227}
Jianing Weicb0652e2014-03-12 18:29:36 -07003228
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003229void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3230 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003231 return;
3232 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003233
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003234 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003235 // Skip the ones that have been submitted successfully.
3236 if (nextRequest.submitted) {
3237 continue;
3238 }
3239
3240 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3241 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3242 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3243
3244 if (halRequest->settings != NULL) {
3245 captureRequest->mSettings.unlock(halRequest->settings);
3246 }
3247
3248 if (captureRequest->mInputStream != NULL) {
3249 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3250 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3251 }
3252
3253 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3254 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3255 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3256 }
3257
3258 if (sendRequestError) {
3259 Mutex::Autolock l(mRequestLock);
3260 if (mListener != NULL) {
3261 mListener->notifyError(
3262 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3263 captureRequest->mResultExtras);
3264 }
3265 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003266 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003267
3268 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003269 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003270}
3271
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003272void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003273 // Optimized a bit for the simple steady-state case (single repeating
3274 // request), to avoid putting that request in the queue temporarily.
3275 Mutex::Autolock l(mRequestLock);
3276
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003277 assert(mNextRequests.empty());
3278
3279 NextRequest nextRequest;
3280 nextRequest.captureRequest = waitForNextRequestLocked();
3281 if (nextRequest.captureRequest == nullptr) {
3282 return;
3283 }
3284
3285 nextRequest.halRequest = camera3_capture_request_t();
3286 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003287 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003288
3289 // Wait for additional requests
3290 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3291
3292 for (size_t i = 1; i < batchSize; i++) {
3293 NextRequest additionalRequest;
3294 additionalRequest.captureRequest = waitForNextRequestLocked();
3295 if (additionalRequest.captureRequest == nullptr) {
3296 break;
3297 }
3298
3299 additionalRequest.halRequest = camera3_capture_request_t();
3300 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003301 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003302 }
3303
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003304 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003305 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003306 mNextRequests.size(), batchSize);
3307 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003308 }
3309
3310 return;
3311}
3312
3313sp<Camera3Device::CaptureRequest>
3314 Camera3Device::RequestThread::waitForNextRequestLocked() {
3315 status_t res;
3316 sp<CaptureRequest> nextRequest;
3317
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003318 while (mRequestQueue.empty()) {
3319 if (!mRepeatingRequests.empty()) {
3320 // Always atomically enqueue all requests in a repeating request
3321 // list. Guarantees a complete in-sequence set of captures to
3322 // application.
3323 const RequestList &requests = mRepeatingRequests;
3324 RequestList::const_iterator firstRequest =
3325 requests.begin();
3326 nextRequest = *firstRequest;
3327 mRequestQueue.insert(mRequestQueue.end(),
3328 ++firstRequest,
3329 requests.end());
3330 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003331
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003332 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003333
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003334 break;
3335 }
3336
3337 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3338
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003339 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3340 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003341 Mutex::Autolock pl(mPauseLock);
3342 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003343 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003344 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003345 // Let the tracker know
3346 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3347 if (statusTracker != 0) {
3348 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3349 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003350 }
3351 // Stop waiting for now and let thread management happen
3352 return NULL;
3353 }
3354 }
3355
3356 if (nextRequest == NULL) {
3357 // Don't have a repeating request already in hand, so queue
3358 // must have an entry now.
3359 RequestList::iterator firstRequest =
3360 mRequestQueue.begin();
3361 nextRequest = *firstRequest;
3362 mRequestQueue.erase(firstRequest);
3363 }
3364
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003365 // In case we've been unpaused by setPaused clearing mDoPause, need to
3366 // update internal pause state (capture/setRepeatingRequest unpause
3367 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003368 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003369 if (mPaused) {
3370 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3371 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3372 if (statusTracker != 0) {
3373 statusTracker->markComponentActive(mStatusId);
3374 }
3375 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003376 mPaused = false;
3377
3378 // Check if we've reconfigured since last time, and reset the preview
3379 // request if so. Can't use 'NULL request == repeat' across configure calls.
3380 if (mReconfigured) {
3381 mPrevRequest.clear();
3382 mReconfigured = false;
3383 }
3384
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003385 if (nextRequest != NULL) {
3386 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003387 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3388 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003389
3390 // Since RequestThread::clear() removes buffers from the input stream,
3391 // get the right buffer here before unlocking mRequestLock
3392 if (nextRequest->mInputStream != NULL) {
3393 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3394 if (res != OK) {
3395 // Can't get input buffer from gralloc queue - this could be due to
3396 // disconnected queue or other producer misbehavior, so not a fatal
3397 // error
3398 ALOGE("%s: Can't get input buffer, skipping request:"
3399 " %s (%d)", __FUNCTION__, strerror(-res), res);
3400 if (mListener != NULL) {
3401 mListener->notifyError(
3402 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3403 nextRequest->mResultExtras);
3404 }
3405 return NULL;
3406 }
3407 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003408 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003409
3410 handleAePrecaptureCancelRequest(nextRequest);
3411
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003412 return nextRequest;
3413}
3414
3415bool Camera3Device::RequestThread::waitIfPaused() {
3416 status_t res;
3417 Mutex::Autolock l(mPauseLock);
3418 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003419 if (mPaused == false) {
3420 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003421 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3422 // Let the tracker know
3423 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3424 if (statusTracker != 0) {
3425 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3426 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003427 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003428
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003429 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003430 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003431 return true;
3432 }
3433 }
3434 // We don't set mPaused to false here, because waitForNextRequest needs
3435 // to further manage the paused state in case of starvation.
3436 return false;
3437}
3438
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003439void Camera3Device::RequestThread::unpauseForNewRequests() {
3440 // With work to do, mark thread as unpaused.
3441 // If paused by request (setPaused), don't resume, to avoid
3442 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003443 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003444 Mutex::Autolock p(mPauseLock);
3445 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003446 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3447 if (mPaused) {
3448 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3449 if (statusTracker != 0) {
3450 statusTracker->markComponentActive(mStatusId);
3451 }
3452 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003453 mPaused = false;
3454 }
3455}
3456
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003457void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3458 sp<Camera3Device> parent = mParent.promote();
3459 if (parent != NULL) {
3460 va_list args;
3461 va_start(args, fmt);
3462
3463 parent->setErrorStateV(fmt, args);
3464
3465 va_end(args);
3466 }
3467}
3468
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003469status_t Camera3Device::RequestThread::insertTriggers(
3470 const sp<CaptureRequest> &request) {
3471
3472 Mutex::Autolock al(mTriggerMutex);
3473
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003474 sp<Camera3Device> parent = mParent.promote();
3475 if (parent == NULL) {
3476 CLOGE("RequestThread: Parent is gone");
3477 return DEAD_OBJECT;
3478 }
3479
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003480 CameraMetadata &metadata = request->mSettings;
3481 size_t count = mTriggerMap.size();
3482
3483 for (size_t i = 0; i < count; ++i) {
3484 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003485 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003486
3487 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3488 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3489 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003490 if (isAeTrigger) {
3491 request->mResultExtras.precaptureTriggerId = triggerId;
3492 mCurrentPreCaptureTriggerId = triggerId;
3493 } else {
3494 request->mResultExtras.afTriggerId = triggerId;
3495 mCurrentAfTriggerId = triggerId;
3496 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003497 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3498 continue; // Trigger ID tag is deprecated since device HAL 3.2
3499 }
3500 }
3501
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003502 camera_metadata_entry entry = metadata.find(tag);
3503
3504 if (entry.count > 0) {
3505 /**
3506 * Already has an entry for this trigger in the request.
3507 * Rewrite it with our requested trigger value.
3508 */
3509 RequestTrigger oldTrigger = trigger;
3510
3511 oldTrigger.entryValue = entry.data.u8[0];
3512
3513 mTriggerReplacedMap.add(tag, oldTrigger);
3514 } else {
3515 /**
3516 * More typical, no trigger entry, so we just add it
3517 */
3518 mTriggerRemovedMap.add(tag, trigger);
3519 }
3520
3521 status_t res;
3522
3523 switch (trigger.getTagType()) {
3524 case TYPE_BYTE: {
3525 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3526 res = metadata.update(tag,
3527 &entryValue,
3528 /*count*/1);
3529 break;
3530 }
3531 case TYPE_INT32:
3532 res = metadata.update(tag,
3533 &trigger.entryValue,
3534 /*count*/1);
3535 break;
3536 default:
3537 ALOGE("%s: Type not supported: 0x%x",
3538 __FUNCTION__,
3539 trigger.getTagType());
3540 return INVALID_OPERATION;
3541 }
3542
3543 if (res != OK) {
3544 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3545 ", value %d", __FUNCTION__, trigger.getTagName(),
3546 trigger.entryValue);
3547 return res;
3548 }
3549
3550 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3551 trigger.getTagName(),
3552 trigger.entryValue);
3553 }
3554
3555 mTriggerMap.clear();
3556
3557 return count;
3558}
3559
3560status_t Camera3Device::RequestThread::removeTriggers(
3561 const sp<CaptureRequest> &request) {
3562 Mutex::Autolock al(mTriggerMutex);
3563
3564 CameraMetadata &metadata = request->mSettings;
3565
3566 /**
3567 * Replace all old entries with their old values.
3568 */
3569 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3570 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3571
3572 status_t res;
3573
3574 uint32_t tag = trigger.metadataTag;
3575 switch (trigger.getTagType()) {
3576 case TYPE_BYTE: {
3577 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3578 res = metadata.update(tag,
3579 &entryValue,
3580 /*count*/1);
3581 break;
3582 }
3583 case TYPE_INT32:
3584 res = metadata.update(tag,
3585 &trigger.entryValue,
3586 /*count*/1);
3587 break;
3588 default:
3589 ALOGE("%s: Type not supported: 0x%x",
3590 __FUNCTION__,
3591 trigger.getTagType());
3592 return INVALID_OPERATION;
3593 }
3594
3595 if (res != OK) {
3596 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3597 ", trigger value %d", __FUNCTION__,
3598 trigger.getTagName(), trigger.entryValue);
3599 return res;
3600 }
3601 }
3602 mTriggerReplacedMap.clear();
3603
3604 /**
3605 * Remove all new entries.
3606 */
3607 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3608 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3609 status_t res = metadata.erase(trigger.metadataTag);
3610
3611 if (res != OK) {
3612 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3613 ", trigger value %d", __FUNCTION__,
3614 trigger.getTagName(), trigger.entryValue);
3615 return res;
3616 }
3617 }
3618 mTriggerRemovedMap.clear();
3619
3620 return OK;
3621}
3622
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003623status_t Camera3Device::RequestThread::addDummyTriggerIds(
3624 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003625 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003626 static const int32_t dummyTriggerId = 1;
3627 status_t res;
3628
3629 CameraMetadata &metadata = request->mSettings;
3630
3631 // If AF trigger is active, insert a dummy AF trigger ID if none already
3632 // exists
3633 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3634 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3635 if (afTrigger.count > 0 &&
3636 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3637 afId.count == 0) {
3638 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3639 if (res != OK) return res;
3640 }
3641
3642 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3643 // if none already exists
3644 camera_metadata_entry pcTrigger =
3645 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3646 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3647 if (pcTrigger.count > 0 &&
3648 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3649 pcId.count == 0) {
3650 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3651 &dummyTriggerId, 1);
3652 if (res != OK) return res;
3653 }
3654
3655 return OK;
3656}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003657
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003658/**
3659 * PreparerThread inner class methods
3660 */
3661
3662Camera3Device::PreparerThread::PreparerThread() :
3663 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3664}
3665
3666Camera3Device::PreparerThread::~PreparerThread() {
3667 Thread::requestExitAndWait();
3668 if (mCurrentStream != nullptr) {
3669 mCurrentStream->cancelPrepare();
3670 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3671 mCurrentStream.clear();
3672 }
3673 clear();
3674}
3675
Ruben Brunkc78ac262015-08-13 17:58:46 -07003676status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003677 status_t res;
3678
3679 Mutex::Autolock l(mLock);
3680
Ruben Brunkc78ac262015-08-13 17:58:46 -07003681 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003682 if (res == OK) {
3683 // No preparation needed, fire listener right off
3684 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3685 if (mListener) {
3686 mListener->notifyPrepared(stream->getId());
3687 }
3688 return OK;
3689 } else if (res != NOT_ENOUGH_DATA) {
3690 return res;
3691 }
3692
3693 // Need to prepare, start up thread if necessary
3694 if (!mActive) {
3695 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3696 // isn't running
3697 Thread::requestExitAndWait();
3698 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3699 if (res != OK) {
3700 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3701 if (mListener) {
3702 mListener->notifyPrepared(stream->getId());
3703 }
3704 return res;
3705 }
3706 mCancelNow = false;
3707 mActive = true;
3708 ALOGV("%s: Preparer stream started", __FUNCTION__);
3709 }
3710
3711 // queue up the work
3712 mPendingStreams.push_back(stream);
3713 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3714
3715 return OK;
3716}
3717
3718status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003719 Mutex::Autolock l(mLock);
3720
3721 for (const auto& stream : mPendingStreams) {
3722 stream->cancelPrepare();
3723 }
3724 mPendingStreams.clear();
3725 mCancelNow = true;
3726
3727 return OK;
3728}
3729
3730void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3731 Mutex::Autolock l(mLock);
3732 mListener = listener;
3733}
3734
3735bool Camera3Device::PreparerThread::threadLoop() {
3736 status_t res;
3737 {
3738 Mutex::Autolock l(mLock);
3739 if (mCurrentStream == nullptr) {
3740 // End thread if done with work
3741 if (mPendingStreams.empty()) {
3742 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3743 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3744 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3745 mActive = false;
3746 return false;
3747 }
3748
3749 // Get next stream to prepare
3750 auto it = mPendingStreams.begin();
3751 mCurrentStream = *it;
3752 mPendingStreams.erase(it);
3753 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3754 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3755 } else if (mCancelNow) {
3756 mCurrentStream->cancelPrepare();
3757 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3758 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3759 mCurrentStream.clear();
3760 mCancelNow = false;
3761 return true;
3762 }
3763 }
3764
3765 res = mCurrentStream->prepareNextBuffer();
3766 if (res == NOT_ENOUGH_DATA) return true;
3767 if (res != OK) {
3768 // Something bad happened; try to recover by cancelling prepare and
3769 // signalling listener anyway
3770 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3771 mCurrentStream->getId(), res, strerror(-res));
3772 mCurrentStream->cancelPrepare();
3773 }
3774
3775 // This stream has finished, notify listener
3776 Mutex::Autolock l(mLock);
3777 if (mListener) {
3778 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3779 mCurrentStream->getId());
3780 mListener->notifyPrepared(mCurrentStream->getId());
3781 }
3782
3783 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3784 mCurrentStream.clear();
3785
3786 return true;
3787}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003788
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003789/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003790 * Static callback forwarding methods from HAL to instance
3791 */
3792
3793void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3794 const camera3_capture_result *result) {
3795 Camera3Device *d =
3796 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003797
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003798 d->processCaptureResult(result);
3799}
3800
3801void Camera3Device::sNotify(const camera3_callback_ops *cb,
3802 const camera3_notify_msg *msg) {
3803 Camera3Device *d =
3804 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3805 d->notify(msg);
3806}
3807
3808}; // namespace android