blob: 50d9d75c31df924e872bf51631a30fb14ca3af63 [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 Talvalaf76e0272013-02-27 18:02:26 -0800443 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800444
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800445 const char *status =
446 mStatus == STATUS_ERROR ? "ERROR" :
447 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700448 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
449 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800450 mStatus == STATUS_ACTIVE ? "ACTIVE" :
451 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700452
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800453 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700454 if (mStatus == STATUS_ERROR) {
455 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
456 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800457 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700458 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700459 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800460
461 if (mInputStream != NULL) {
462 write(fd, lines.string(), lines.size());
463 mInputStream->dump(fd, args);
464 } else {
465 lines.appendFormat(" No input stream.\n");
466 write(fd, lines.string(), lines.size());
467 }
468 for (size_t i = 0; i < mOutputStreams.size(); i++) {
469 mOutputStreams[i]->dump(fd,args);
470 }
471
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700472 lines = String8(" In-flight requests:\n");
473 if (mInFlightMap.size() == 0) {
474 lines.append(" None\n");
475 } else {
476 for (size_t i = 0; i < mInFlightMap.size(); i++) {
477 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700478 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700479 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800480 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700481 r.numBuffersLeft);
482 }
483 }
484 write(fd, lines.string(), lines.size());
485
Igor Murashkin1e479c02013-09-06 16:55:14 -0700486 {
487 lines = String8(" Last request sent:\n");
488 write(fd, lines.string(), lines.size());
489
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700490 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700491 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
492 }
493
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800494 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700495 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800496 write(fd, lines.string(), lines.size());
497 mHal3Device->ops->dump(mHal3Device, fd);
498 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800499
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700500 if (gotLock) mLock.unlock();
501 if (gotInterfaceLock) mInterfaceLock.unlock();
502
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800503 return OK;
504}
505
506const CameraMetadata& Camera3Device::info() const {
507 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800508 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
509 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700510 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800511 mStatus == STATUS_ERROR ?
512 "when in error state" : "before init");
513 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800514 return mDeviceInfo;
515}
516
Jianing Wei90e59c92014-03-12 18:29:36 -0700517status_t Camera3Device::checkStatusOkToCaptureLocked() {
518 switch (mStatus) {
519 case STATUS_ERROR:
520 CLOGE("Device has encountered a serious error");
521 return INVALID_OPERATION;
522 case STATUS_UNINITIALIZED:
523 CLOGE("Device not initialized");
524 return INVALID_OPERATION;
525 case STATUS_UNCONFIGURED:
526 case STATUS_CONFIGURED:
527 case STATUS_ACTIVE:
528 // OK
529 break;
530 default:
531 SET_ERR_L("Unexpected status: %d", mStatus);
532 return INVALID_OPERATION;
533 }
534 return OK;
535}
536
537status_t Camera3Device::convertMetadataListToRequestListLocked(
538 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
539 if (requestList == NULL) {
540 CLOGE("requestList cannot be NULL.");
541 return BAD_VALUE;
542 }
543
Jianing Weicb0652e2014-03-12 18:29:36 -0700544 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700545 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
546 it != metadataList.end(); ++it) {
547 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
548 if (newRequest == 0) {
549 CLOGE("Can't create capture request");
550 return BAD_VALUE;
551 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700552
553 // Setup burst Id and request Id
554 newRequest->mResultExtras.burstId = burstId++;
555 if (it->exists(ANDROID_REQUEST_ID)) {
556 if (it->find(ANDROID_REQUEST_ID).count == 0) {
557 CLOGE("RequestID entry exists; but must not be empty in metadata");
558 return BAD_VALUE;
559 }
560 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
561 } else {
562 CLOGE("RequestID does not exist in metadata");
563 return BAD_VALUE;
564 }
565
Jianing Wei90e59c92014-03-12 18:29:36 -0700566 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700567
568 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700569 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700570
571 // Setup batch size if this is a high speed video recording request.
572 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
573 auto firstRequest = requestList->begin();
574 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
575 if (outputStream->isVideoStream()) {
576 (*firstRequest)->mBatchSize = requestList->size();
577 break;
578 }
579 }
580 }
581
Jianing Wei90e59c92014-03-12 18:29:36 -0700582 return OK;
583}
584
Jianing Weicb0652e2014-03-12 18:29:36 -0700585status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800586 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800587
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700588 List<const CameraMetadata> requests;
589 requests.push_back(request);
590 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800591}
592
Jianing Wei90e59c92014-03-12 18:29:36 -0700593status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700594 const List<const CameraMetadata> &requests, bool repeating,
595 /*out*/
596 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700597 ATRACE_CALL();
598 Mutex::Autolock il(mInterfaceLock);
599 Mutex::Autolock l(mLock);
600
601 status_t res = checkStatusOkToCaptureLocked();
602 if (res != OK) {
603 // error logged by previous call
604 return res;
605 }
606
607 RequestList requestList;
608
609 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
610 if (res != OK) {
611 // error logged by previous call
612 return res;
613 }
614
615 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700616 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700617 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700618 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700619 }
620
621 if (res == OK) {
622 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
623 if (res != OK) {
624 SET_ERR_L("Can't transition to active in %f seconds!",
625 kActiveTimeout/1e9);
626 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700627 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
628 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700629 } else {
630 CLOGE("Cannot queue request. Impossible.");
631 return BAD_VALUE;
632 }
633
634 return res;
635}
636
Jianing Weicb0652e2014-03-12 18:29:36 -0700637status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
638 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700639 ATRACE_CALL();
640
Jianing Weicb0652e2014-03-12 18:29:36 -0700641 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700642}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800643
Jianing Weicb0652e2014-03-12 18:29:36 -0700644status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
645 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800646 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800647
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700648 List<const CameraMetadata> requests;
649 requests.push_back(request);
650 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800651}
652
Jianing Weicb0652e2014-03-12 18:29:36 -0700653status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
654 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700655 ATRACE_CALL();
656
Jianing Weicb0652e2014-03-12 18:29:36 -0700657 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700658}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800659
660sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
661 const CameraMetadata &request) {
662 status_t res;
663
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700664 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800665 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700666 // Stream configuration failed due to unsupported configuration.
667 // Device back to unconfigured state. Client might try other configuraitons
668 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
669 CLOGE("No streams configured");
670 return NULL;
671 }
672 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800673 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700674 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800675 return NULL;
676 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700677 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700678 if (mStatus == STATUS_UNCONFIGURED) {
679 CLOGE("No streams configured");
680 return NULL;
681 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800682 }
683
684 sp<CaptureRequest> newRequest = createCaptureRequest(request);
685 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800686}
687
Jianing Weicb0652e2014-03-12 18:29:36 -0700688status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800689 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700690 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800691 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800692
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800693 switch (mStatus) {
694 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700695 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800696 return INVALID_OPERATION;
697 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700698 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800699 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700700 case STATUS_UNCONFIGURED:
701 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800702 case STATUS_ACTIVE:
703 // OK
704 break;
705 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700706 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800707 return INVALID_OPERATION;
708 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700709 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700710
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700711 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800712}
713
714status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
715 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700716 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800717
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700718 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800719}
720
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700721status_t Camera3Device::createInputStream(
722 uint32_t width, uint32_t height, int format, int *id) {
723 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700724 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700725 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700726 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
727 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700728
729 status_t res;
730 bool wasActive = false;
731
732 switch (mStatus) {
733 case STATUS_ERROR:
734 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
735 return INVALID_OPERATION;
736 case STATUS_UNINITIALIZED:
737 ALOGE("%s: Device not initialized", __FUNCTION__);
738 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700739 case STATUS_UNCONFIGURED:
740 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700741 // OK
742 break;
743 case STATUS_ACTIVE:
744 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700745 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700746 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700747 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700748 return res;
749 }
750 wasActive = true;
751 break;
752 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700753 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700754 return INVALID_OPERATION;
755 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700756 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700757
758 if (mInputStream != 0) {
759 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
760 return INVALID_OPERATION;
761 }
762
763 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
764 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700765 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700766
767 mInputStream = newStream;
768
769 *id = mNextStreamId++;
770
771 // Continue captures if active at start
772 if (wasActive) {
773 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
774 res = configureStreamsLocked();
775 if (res != OK) {
776 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
777 __FUNCTION__, mNextStreamId, strerror(-res), res);
778 return res;
779 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700780 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700781 }
782
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700783 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700784 return OK;
785}
786
Igor Murashkin2fba5842013-04-22 14:03:54 -0700787
788status_t Camera3Device::createZslStream(
789 uint32_t width, uint32_t height,
790 int depth,
791 /*out*/
792 int *id,
793 sp<Camera3ZslStream>* zslStream) {
794 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700795 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700796 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700797 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
798 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700799
800 status_t res;
801 bool wasActive = false;
802
803 switch (mStatus) {
804 case STATUS_ERROR:
805 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
806 return INVALID_OPERATION;
807 case STATUS_UNINITIALIZED:
808 ALOGE("%s: Device not initialized", __FUNCTION__);
809 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700810 case STATUS_UNCONFIGURED:
811 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700812 // OK
813 break;
814 case STATUS_ACTIVE:
815 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700816 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700817 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700818 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700819 return res;
820 }
821 wasActive = true;
822 break;
823 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700824 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700825 return INVALID_OPERATION;
826 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700827 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700828
829 if (mInputStream != 0) {
830 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
831 return INVALID_OPERATION;
832 }
833
834 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
835 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700836 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700837
838 res = mOutputStreams.add(mNextStreamId, newStream);
839 if (res < 0) {
840 ALOGE("%s: Can't add new stream to set: %s (%d)",
841 __FUNCTION__, strerror(-res), res);
842 return res;
843 }
844 mInputStream = newStream;
845
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530846 mNeedConfig = true;
847
Igor Murashkin2fba5842013-04-22 14:03:54 -0700848 *id = mNextStreamId++;
849 *zslStream = newStream;
850
851 // Continue captures if active at start
852 if (wasActive) {
853 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
854 res = configureStreamsLocked();
855 if (res != OK) {
856 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
857 __FUNCTION__, mNextStreamId, strerror(-res), res);
858 return res;
859 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700860 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700861 }
862
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700863 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700864 return OK;
865}
866
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700867status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800868 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700869 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800870 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700871 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800872 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700873 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
874 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800875
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800876 status_t res;
877 bool wasActive = false;
878
879 switch (mStatus) {
880 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700881 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800882 return INVALID_OPERATION;
883 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700884 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800885 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700886 case STATUS_UNCONFIGURED:
887 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800888 // OK
889 break;
890 case STATUS_ACTIVE:
891 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700892 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800893 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700894 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800895 return res;
896 }
897 wasActive = true;
898 break;
899 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700900 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800901 return INVALID_OPERATION;
902 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700903 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800904
905 sp<Camera3OutputStream> newStream;
906 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700907 ssize_t blobBufferSize;
908 if (dataSpace != HAL_DATASPACE_DEPTH) {
909 blobBufferSize = getJpegBufferSize(width, height);
910 if (blobBufferSize <= 0) {
911 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
912 return BAD_VALUE;
913 }
914 } else {
915 blobBufferSize = getPointCloudBufferSize();
916 if (blobBufferSize <= 0) {
917 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
918 return BAD_VALUE;
919 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700920 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800921 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700922 width, height, blobBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800923 } else {
924 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700925 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800926 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700927 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800928
929 res = mOutputStreams.add(mNextStreamId, newStream);
930 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700931 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800932 return res;
933 }
934
935 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700936 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800937
938 // Continue captures if active at start
939 if (wasActive) {
940 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
941 res = configureStreamsLocked();
942 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700943 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
944 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800945 return res;
946 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700947 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800948 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700949 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800950 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800951}
952
953status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
954 ATRACE_CALL();
955 (void)outputId; (void)id;
956
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700957 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800958 return INVALID_OPERATION;
959}
960
961
962status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700963 uint32_t *width, uint32_t *height,
964 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800965 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700966 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800967 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800968
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800969 switch (mStatus) {
970 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700971 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800972 return INVALID_OPERATION;
973 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700974 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800975 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700976 case STATUS_UNCONFIGURED:
977 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800978 case STATUS_ACTIVE:
979 // OK
980 break;
981 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700982 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800983 return INVALID_OPERATION;
984 }
985
986 ssize_t idx = mOutputStreams.indexOfKey(id);
987 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700988 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800989 return idx;
990 }
991
992 if (width) *width = mOutputStreams[idx]->getWidth();
993 if (height) *height = mOutputStreams[idx]->getHeight();
994 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700995 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800996 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800997}
998
999status_t Camera3Device::setStreamTransform(int id,
1000 int transform) {
1001 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 does not exist",
1025 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001026 return BAD_VALUE;
1027 }
1028
1029 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001030}
1031
1032status_t Camera3Device::deleteStream(int id) {
1033 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001034 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001035 Mutex::Autolock l(mLock);
1036 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001037
Igor Murashkine2172be2013-05-28 15:31:39 -07001038 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1039
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001040 // CameraDevice semantics require device to already be idle before
1041 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001042 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001043 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1044 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001045 }
1046
Igor Murashkin2fba5842013-04-22 14:03:54 -07001047 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001048 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001049 if (mInputStream != NULL && id == mInputStream->getId()) {
1050 deletedStream = mInputStream;
1051 mInputStream.clear();
1052 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001053 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001054 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001055 return BAD_VALUE;
1056 }
Zhijun He5f446352014-01-22 09:49:33 -08001057 }
1058
1059 // Delete output stream or the output part of a bi-directional stream.
1060 if (outputStreamIdx != NAME_NOT_FOUND) {
1061 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001062 mOutputStreams.removeItem(id);
1063 }
1064
1065 // Free up the stream endpoint so that it can be used by some other stream
1066 res = deletedStream->disconnect();
1067 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001068 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 // fall through since we want to still list the stream as deleted.
1070 }
1071 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001072 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001073
1074 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001075}
1076
1077status_t Camera3Device::deleteReprocessStream(int id) {
1078 ATRACE_CALL();
1079 (void)id;
1080
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001081 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001082 return INVALID_OPERATION;
1083}
1084
Zhijun He1fa89992015-06-01 15:44:31 -07001085status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001086 ATRACE_CALL();
1087 ALOGV("%s: E", __FUNCTION__);
1088
1089 Mutex::Autolock il(mInterfaceLock);
1090 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001091
1092 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1093 mNeedConfig = true;
1094 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1095 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001096
1097 return configureStreamsLocked();
1098}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001099
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001100status_t Camera3Device::getInputBufferProducer(
1101 sp<IGraphicBufferProducer> *producer) {
1102 Mutex::Autolock il(mInterfaceLock);
1103 Mutex::Autolock l(mLock);
1104
1105 if (producer == NULL) {
1106 return BAD_VALUE;
1107 } else if (mInputStream == NULL) {
1108 return INVALID_OPERATION;
1109 }
1110
1111 return mInputStream->getInputBufferProducer(producer);
1112}
1113
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001114status_t Camera3Device::createDefaultRequest(int templateId,
1115 CameraMetadata *request) {
1116 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001117 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001118 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001119 Mutex::Autolock l(mLock);
1120
1121 switch (mStatus) {
1122 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001123 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001124 return INVALID_OPERATION;
1125 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001126 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001127 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001128 case STATUS_UNCONFIGURED:
1129 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001130 case STATUS_ACTIVE:
1131 // OK
1132 break;
1133 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001134 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001135 return INVALID_OPERATION;
1136 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001137
Zhijun Hea1530f12014-09-14 12:44:20 -07001138 if (!mRequestTemplateCache[templateId].isEmpty()) {
1139 *request = mRequestTemplateCache[templateId];
1140 return OK;
1141 }
1142
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001143 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001144 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001145 rawRequest = mHal3Device->ops->construct_default_request_settings(
1146 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001147 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001148 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001149 ALOGI("%s: template %d is not supported on this camera device",
1150 __FUNCTION__, templateId);
1151 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001152 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001153 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001154 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001155
1156 return OK;
1157}
1158
1159status_t Camera3Device::waitUntilDrained() {
1160 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001161 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001162 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001163
Zhijun He69a37482014-03-23 18:44:49 -07001164 return waitUntilDrainedLocked();
1165}
1166
1167status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001168 switch (mStatus) {
1169 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001170 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001171 ALOGV("%s: Already idle", __FUNCTION__);
1172 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001173 case STATUS_CONFIGURED:
1174 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001175 case STATUS_ERROR:
1176 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001177 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001178 break;
1179 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001180 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001181 return INVALID_OPERATION;
1182 }
1183
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001184 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1185 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001186 if (res != OK) {
1187 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1188 res);
1189 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001190 return res;
1191}
1192
Ruben Brunk183f0562015-08-12 12:55:02 -07001193
1194void Camera3Device::internalUpdateStatusLocked(Status status) {
1195 mStatus = status;
1196 mRecentStatusUpdates.add(mStatus);
1197 mStatusChanged.broadcast();
1198}
1199
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001200// Pause to reconfigure
1201status_t Camera3Device::internalPauseAndWaitLocked() {
1202 mRequestThread->setPaused(true);
1203 mPauseStateNotify = true;
1204
1205 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1206 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1207 if (res != OK) {
1208 SET_ERR_L("Can't idle device in %f seconds!",
1209 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001210 }
1211
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001212 return res;
1213}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001214
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001215// Resume after internalPauseAndWaitLocked
1216status_t Camera3Device::internalResumeLocked() {
1217 status_t res;
1218
1219 mRequestThread->setPaused(false);
1220
1221 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1222 if (res != OK) {
1223 SET_ERR_L("Can't transition to active in %f seconds!",
1224 kActiveTimeout/1e9);
1225 }
1226 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001227 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001228}
1229
Ruben Brunk183f0562015-08-12 12:55:02 -07001230status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001231 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001232
1233 size_t startIndex = 0;
1234 if (mStatusWaiters == 0) {
1235 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1236 // this status list
1237 mRecentStatusUpdates.clear();
1238 } else {
1239 // If other threads are waiting on updates to this status list, set the position of the
1240 // first element that this list will check rather than clearing the list.
1241 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001242 }
1243
Ruben Brunk183f0562015-08-12 12:55:02 -07001244 mStatusWaiters++;
1245
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001246 bool stateSeen = false;
1247 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001248 if (active == (mStatus == STATUS_ACTIVE)) {
1249 // Desired state is current
1250 break;
1251 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001252
1253 res = mStatusChanged.waitRelative(mLock, timeout);
1254 if (res != OK) break;
1255
Ruben Brunk183f0562015-08-12 12:55:02 -07001256 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1257 // transitions.
1258 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1259 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1260 __FUNCTION__);
1261
1262 // Encountered desired state since we began waiting
1263 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001264 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1265 stateSeen = true;
1266 break;
1267 }
1268 }
1269 } while (!stateSeen);
1270
Ruben Brunk183f0562015-08-12 12:55:02 -07001271 mStatusWaiters--;
1272
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001273 return res;
1274}
1275
1276
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001277status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1278 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001279 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001280
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001281 if (listener != NULL && mListener != NULL) {
1282 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1283 }
1284 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001285 mRequestThread->setNotificationListener(listener);
1286 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001287
1288 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001289}
1290
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001291bool Camera3Device::willNotify3A() {
1292 return false;
1293}
1294
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001295status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001296 status_t res;
1297 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001298
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001299 while (mResultQueue.empty()) {
1300 res = mResultSignal.waitRelative(mOutputLock, timeout);
1301 if (res == TIMED_OUT) {
1302 return res;
1303 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001304 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001305 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001306 return res;
1307 }
1308 }
1309 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001310}
1311
Jianing Weicb0652e2014-03-12 18:29:36 -07001312status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001313 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001314 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001315
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001316 if (mResultQueue.empty()) {
1317 return NOT_ENOUGH_DATA;
1318 }
1319
Jianing Weicb0652e2014-03-12 18:29:36 -07001320 if (frame == NULL) {
1321 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1322 return BAD_VALUE;
1323 }
1324
1325 CaptureResult &result = *(mResultQueue.begin());
1326 frame->mResultExtras = result.mResultExtras;
1327 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001328 mResultQueue.erase(mResultQueue.begin());
1329
1330 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001331}
1332
1333status_t Camera3Device::triggerAutofocus(uint32_t id) {
1334 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001335 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001336
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001337 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1338 // Mix-in this trigger into the next request and only the next request.
1339 RequestTrigger trigger[] = {
1340 {
1341 ANDROID_CONTROL_AF_TRIGGER,
1342 ANDROID_CONTROL_AF_TRIGGER_START
1343 },
1344 {
1345 ANDROID_CONTROL_AF_TRIGGER_ID,
1346 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001347 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001348 };
1349
1350 return mRequestThread->queueTrigger(trigger,
1351 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001352}
1353
1354status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1355 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001356 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001357
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001358 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1359 // Mix-in this trigger into the next request and only the next request.
1360 RequestTrigger trigger[] = {
1361 {
1362 ANDROID_CONTROL_AF_TRIGGER,
1363 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1364 },
1365 {
1366 ANDROID_CONTROL_AF_TRIGGER_ID,
1367 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001368 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001369 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001370
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001371 return mRequestThread->queueTrigger(trigger,
1372 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001373}
1374
1375status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1376 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001377 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001378
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001379 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1380 // Mix-in this trigger into the next request and only the next request.
1381 RequestTrigger trigger[] = {
1382 {
1383 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1384 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1385 },
1386 {
1387 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1388 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001389 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001390 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001391
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001392 return mRequestThread->queueTrigger(trigger,
1393 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001394}
1395
1396status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1397 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1398 ATRACE_CALL();
1399 (void)reprocessStreamId; (void)buffer; (void)listener;
1400
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001401 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001402 return INVALID_OPERATION;
1403}
1404
Jianing Weicb0652e2014-03-12 18:29:36 -07001405status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001406 ATRACE_CALL();
1407 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001408 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001409
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001410 NotificationListener* listener;
1411 {
1412 Mutex::Autolock l(mOutputLock);
1413 listener = mListener;
1414 }
1415
Zhijun He7ef20392014-04-21 16:04:17 -07001416 {
1417 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001418 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001419 }
1420
Zhijun He491e3412013-12-27 10:57:44 -08001421 status_t res;
1422 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001423 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001424 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001425 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001426 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001427 }
1428
1429 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001430}
1431
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001432status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001433 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1434}
1435
1436status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001437 ATRACE_CALL();
1438 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001439 Mutex::Autolock il(mInterfaceLock);
1440 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001441
1442 sp<Camera3StreamInterface> stream;
1443 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1444 if (outputStreamIdx == NAME_NOT_FOUND) {
1445 CLOGE("Stream %d does not exist", streamId);
1446 return BAD_VALUE;
1447 }
1448
1449 stream = mOutputStreams.editValueAt(outputStreamIdx);
1450
1451 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001452 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001453 return BAD_VALUE;
1454 }
1455
1456 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001457 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001458 return BAD_VALUE;
1459 }
1460
Ruben Brunkc78ac262015-08-13 17:58:46 -07001461 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001462}
1463
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001464status_t Camera3Device::tearDown(int streamId) {
1465 ATRACE_CALL();
1466 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1467 Mutex::Autolock il(mInterfaceLock);
1468 Mutex::Autolock l(mLock);
1469
1470 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1471 // since we cannot call register_stream_buffers except right after configure_streams.
1472 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1473 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1474 __FUNCTION__, mHal3Device->common.version);
1475 return NO_INIT;
1476 }
1477
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->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1488 CLOGE("Stream %d is a target of a in-progress request", streamId);
1489 return BAD_VALUE;
1490 }
1491
1492 return stream->tearDown();
1493}
1494
Zhijun He204e3292014-07-14 17:09:23 -07001495uint32_t Camera3Device::getDeviceVersion() {
1496 ATRACE_CALL();
1497 Mutex::Autolock il(mInterfaceLock);
1498 return mDeviceVersion;
1499}
1500
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001501/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001502 * Methods called by subclasses
1503 */
1504
1505void Camera3Device::notifyStatus(bool idle) {
1506 {
1507 // Need mLock to safely update state and synchronize to current
1508 // state of methods in flight.
1509 Mutex::Autolock l(mLock);
1510 // We can get various system-idle notices from the status tracker
1511 // while starting up. Only care about them if we've actually sent
1512 // in some requests recently.
1513 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1514 return;
1515 }
1516 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1517 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001518 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001519
1520 // Skip notifying listener if we're doing some user-transparent
1521 // state changes
1522 if (mPauseStateNotify) return;
1523 }
1524 NotificationListener *listener;
1525 {
1526 Mutex::Autolock l(mOutputLock);
1527 listener = mListener;
1528 }
1529 if (idle && listener != NULL) {
1530 listener->notifyIdle();
1531 }
1532}
1533
1534/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001535 * Camera3Device private methods
1536 */
1537
1538sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1539 const CameraMetadata &request) {
1540 ATRACE_CALL();
1541 status_t res;
1542
1543 sp<CaptureRequest> newRequest = new CaptureRequest;
1544 newRequest->mSettings = request;
1545
1546 camera_metadata_entry_t inputStreams =
1547 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1548 if (inputStreams.count > 0) {
1549 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001550 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001551 CLOGE("Request references unknown input stream %d",
1552 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001553 return NULL;
1554 }
1555 // Lazy completion of stream configuration (allocation/registration)
1556 // on first use
1557 if (mInputStream->isConfiguring()) {
1558 res = mInputStream->finishConfiguration(mHal3Device);
1559 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001560 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001561 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001562 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001563 return NULL;
1564 }
1565 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001566 // Check if stream is being prepared
1567 if (mInputStream->isPreparing()) {
1568 CLOGE("Request references an input stream that's being prepared!");
1569 return NULL;
1570 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001571
1572 newRequest->mInputStream = mInputStream;
1573 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1574 }
1575
1576 camera_metadata_entry_t streams =
1577 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1578 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001579 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001580 return NULL;
1581 }
1582
1583 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001584 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001585 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001586 CLOGE("Request references unknown stream %d",
1587 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001588 return NULL;
1589 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001590 sp<Camera3OutputStreamInterface> stream =
1591 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001592
1593 // Lazy completion of stream configuration (allocation/registration)
1594 // on first use
1595 if (stream->isConfiguring()) {
1596 res = stream->finishConfiguration(mHal3Device);
1597 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001598 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1599 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001600 return NULL;
1601 }
1602 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001603 // Check if stream is being prepared
1604 if (stream->isPreparing()) {
1605 CLOGE("Request references an output stream that's being prepared!");
1606 return NULL;
1607 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001608
1609 newRequest->mOutputStreams.push(stream);
1610 }
1611 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001612 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001613
1614 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001615}
1616
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001617bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1618 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1619 Size size = mSupportedOpaqueInputSizes[i];
1620 if (size.width == width && size.height == height) {
1621 return true;
1622 }
1623 }
1624
1625 return false;
1626}
1627
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001628status_t Camera3Device::configureStreamsLocked() {
1629 ATRACE_CALL();
1630 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001631
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001632 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001633 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001634 return INVALID_OPERATION;
1635 }
1636
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001637 if (!mNeedConfig) {
1638 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1639 return OK;
1640 }
1641
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001642 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1643 // adding a dummy stream instead.
1644 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1645 if (mOutputStreams.size() == 0) {
1646 addDummyStreamLocked();
1647 } else {
1648 tryRemoveDummyStreamLocked();
1649 }
1650
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001651 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001652 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001653
1654 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001655 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1656 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1657 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001658 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1659
1660 Vector<camera3_stream_t*> streams;
1661 streams.setCapacity(config.num_streams);
1662
1663 if (mInputStream != NULL) {
1664 camera3_stream_t *inputStream;
1665 inputStream = mInputStream->startConfiguration();
1666 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001667 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001668 return INVALID_OPERATION;
1669 }
1670 streams.add(inputStream);
1671 }
1672
1673 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001674
1675 // Don't configure bidi streams twice, nor add them twice to the list
1676 if (mOutputStreams[i].get() ==
1677 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1678
1679 config.num_streams--;
1680 continue;
1681 }
1682
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001683 camera3_stream_t *outputStream;
1684 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1685 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001686 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001687 return INVALID_OPERATION;
1688 }
1689 streams.add(outputStream);
1690 }
1691
1692 config.streams = streams.editArray();
1693
1694 // Do the HAL configuration; will potentially touch stream
1695 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001696 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001697 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001698 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001699
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001700 if (res == BAD_VALUE) {
1701 // HAL rejected this set of streams as unsupported, clean up config
1702 // attempt and return to unconfigured state
1703 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1704 res = mInputStream->cancelConfiguration();
1705 if (res != OK) {
1706 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1707 mInputStream->getId(), strerror(-res), res);
1708 return res;
1709 }
1710 }
1711
1712 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1713 sp<Camera3OutputStreamInterface> outputStream =
1714 mOutputStreams.editValueAt(i);
1715 if (outputStream->isConfiguring()) {
1716 res = outputStream->cancelConfiguration();
1717 if (res != OK) {
1718 SET_ERR_L(
1719 "Can't cancel configuring output stream %d: %s (%d)",
1720 outputStream->getId(), strerror(-res), res);
1721 return res;
1722 }
1723 }
1724 }
1725
1726 // Return state to that at start of call, so that future configures
1727 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001728 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001729 mNeedConfig = true;
1730
1731 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1732 return BAD_VALUE;
1733 } else if (res != OK) {
1734 // Some other kind of error from configure_streams - this is not
1735 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001736 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1737 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001738 return res;
1739 }
1740
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001741 // Finish all stream configuration immediately.
1742 // TODO: Try to relax this later back to lazy completion, which should be
1743 // faster
1744
Igor Murashkin073f8572013-05-02 14:59:28 -07001745 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001746 res = mInputStream->finishConfiguration(mHal3Device);
1747 if (res != OK) {
1748 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1749 mInputStream->getId(), strerror(-res), res);
1750 return res;
1751 }
1752 }
1753
1754 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001755 sp<Camera3OutputStreamInterface> outputStream =
1756 mOutputStreams.editValueAt(i);
1757 if (outputStream->isConfiguring()) {
1758 res = outputStream->finishConfiguration(mHal3Device);
1759 if (res != OK) {
1760 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1761 outputStream->getId(), strerror(-res), res);
1762 return res;
1763 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001764 }
1765 }
1766
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001767 // Request thread needs to know to avoid using repeat-last-settings protocol
1768 // across configure_streams() calls
1769 mRequestThread->configurationComplete();
1770
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07001771 // Boost priority of request thread for high speed recording to SCHED_FIFO
1772 if (mIsConstrainedHighSpeedConfiguration) {
1773 pid_t requestThreadTid = mRequestThread->getTid();
1774 res = requestPriority(getpid(), requestThreadTid,
1775 kConstrainedHighSpeedThreadPriority, true);
1776 if (res != OK) {
1777 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
1778 strerror(-res), res);
1779 } else {
1780 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
1781 }
1782 } else {
1783 // TODO: Set/restore normal priority for normal use cases
1784 }
1785
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001786 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001787
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001788 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001789
Ruben Brunk183f0562015-08-12 12:55:02 -07001790 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1791 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001792
1793 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1794
Zhijun He0a210512014-07-24 13:45:15 -07001795 // tear down the deleted streams after configure streams.
1796 mDeletedStreams.clear();
1797
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001798 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001799}
1800
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001801status_t Camera3Device::addDummyStreamLocked() {
1802 ATRACE_CALL();
1803 status_t res;
1804
1805 if (mDummyStreamId != NO_STREAM) {
1806 // Should never be adding a second dummy stream when one is already
1807 // active
1808 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1809 __FUNCTION__, mId);
1810 return INVALID_OPERATION;
1811 }
1812
1813 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1814
1815 sp<Camera3OutputStreamInterface> dummyStream =
1816 new Camera3DummyStream(mNextStreamId);
1817
1818 res = mOutputStreams.add(mNextStreamId, dummyStream);
1819 if (res < 0) {
1820 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1821 return res;
1822 }
1823
1824 mDummyStreamId = mNextStreamId;
1825 mNextStreamId++;
1826
1827 return OK;
1828}
1829
1830status_t Camera3Device::tryRemoveDummyStreamLocked() {
1831 ATRACE_CALL();
1832 status_t res;
1833
1834 if (mDummyStreamId == NO_STREAM) return OK;
1835 if (mOutputStreams.size() == 1) return OK;
1836
1837 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1838
1839 // Ok, have a dummy stream and there's at least one other output stream,
1840 // so remove the dummy
1841
1842 sp<Camera3StreamInterface> deletedStream;
1843 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1844 if (outputStreamIdx == NAME_NOT_FOUND) {
1845 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1846 return INVALID_OPERATION;
1847 }
1848
1849 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1850 mOutputStreams.removeItemsAt(outputStreamIdx);
1851
1852 // Free up the stream endpoint so that it can be used by some other stream
1853 res = deletedStream->disconnect();
1854 if (res != OK) {
1855 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1856 // fall through since we want to still list the stream as deleted.
1857 }
1858 mDeletedStreams.add(deletedStream);
1859 mDummyStreamId = NO_STREAM;
1860
1861 return res;
1862}
1863
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001864void Camera3Device::setErrorState(const char *fmt, ...) {
1865 Mutex::Autolock l(mLock);
1866 va_list args;
1867 va_start(args, fmt);
1868
1869 setErrorStateLockedV(fmt, args);
1870
1871 va_end(args);
1872}
1873
1874void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1875 Mutex::Autolock l(mLock);
1876 setErrorStateLockedV(fmt, args);
1877}
1878
1879void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1880 va_list args;
1881 va_start(args, fmt);
1882
1883 setErrorStateLockedV(fmt, args);
1884
1885 va_end(args);
1886}
1887
1888void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001889 // Print out all error messages to log
1890 String8 errorCause = String8::formatV(fmt, args);
1891 ALOGE("Camera %d: %s", mId, errorCause.string());
1892
1893 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001894 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001895
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001896 mErrorCause = errorCause;
1897
1898 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07001899 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001900
1901 // Notify upstream about a device error
1902 if (mListener != NULL) {
1903 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1904 CaptureResultExtras());
1905 }
1906
1907 // Save stack trace. View by dumping it later.
1908 CameraTraces::saveTrace();
1909 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001910}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001911
1912/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001913 * In-flight request management
1914 */
1915
Jianing Weicb0652e2014-03-12 18:29:36 -07001916status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07001917 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1918 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001919 ATRACE_CALL();
1920 Mutex::Autolock l(mInFlightLock);
1921
1922 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07001923 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
1924 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001925 if (res < 0) return res;
1926
1927 return OK;
1928}
1929
1930/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001931 * Check if all 3A fields are ready, and send off a partial 3A-only result
1932 * to the output frame queue
1933 */
Zhijun He204e3292014-07-14 17:09:23 -07001934bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001935 uint32_t frameNumber,
1936 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001937
1938 // Check if all 3A states are present
1939 // The full list of fields is
1940 // android.control.afMode
1941 // android.control.awbMode
1942 // android.control.aeState
1943 // android.control.awbState
1944 // android.control.afState
1945 // android.control.afTriggerID
1946 // android.control.aePrecaptureID
1947 // TODO: Add android.control.aeMode
1948
1949 bool gotAllStates = true;
1950
1951 uint8_t afMode;
1952 uint8_t awbMode;
1953 uint8_t aeState;
1954 uint8_t afState;
1955 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001956
1957 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1958 &afMode, frameNumber);
1959
1960 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1961 &awbMode, frameNumber);
1962
1963 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1964 &aeState, frameNumber);
1965
1966 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1967 &afState, frameNumber);
1968
1969 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1970 &awbState, frameNumber);
1971
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001972 if (!gotAllStates) return false;
1973
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001974 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001975 "AF state %d, AE state %d, AWB state %d, "
1976 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001977 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001978 afMode, awbMode,
1979 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001980 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001981
1982 // Got all states, so construct a minimal result to send
1983 // In addition to the above fields, this means adding in
1984 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001985 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001986 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001987
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001988 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001989
1990 Mutex::Autolock l(mOutputLock);
1991
Jianing Weicb0652e2014-03-12 18:29:36 -07001992 CaptureResult captureResult;
1993 captureResult.mResultExtras = resultExtras;
1994 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1995 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1996 // but not limited to CameraDeviceBase::getNextResult
1997 CaptureResult& min3AResult =
1998 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001999
Jianing Weicb0652e2014-03-12 18:29:36 -07002000 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
2001 // TODO: This is problematic casting. Need to fix CameraMetadata.
2002 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002003 return false;
2004 }
2005
Jianing Weicb0652e2014-03-12 18:29:36 -07002006 int32_t requestId = resultExtras.requestId;
2007 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002008 &requestId, frameNumber)) {
2009 return false;
2010 }
2011
Zhijun He204e3292014-07-14 17:09:23 -07002012 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
2013 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
2014 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
2015 &partialResult, frameNumber)) {
2016 return false;
2017 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002018 }
2019
Jianing Weicb0652e2014-03-12 18:29:36 -07002020 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002021 &afMode, frameNumber)) {
2022 return false;
2023 }
2024
Jianing Weicb0652e2014-03-12 18:29:36 -07002025 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002026 &awbMode, frameNumber)) {
2027 return false;
2028 }
2029
Jianing Weicb0652e2014-03-12 18:29:36 -07002030 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002031 &aeState, frameNumber)) {
2032 return false;
2033 }
2034
Jianing Weicb0652e2014-03-12 18:29:36 -07002035 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002036 &afState, frameNumber)) {
2037 return false;
2038 }
2039
Jianing Weicb0652e2014-03-12 18:29:36 -07002040 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002041 &awbState, frameNumber)) {
2042 return false;
2043 }
2044
Jianing Weicb0652e2014-03-12 18:29:36 -07002045 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002046 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002047 return false;
2048 }
2049
Jianing Weicb0652e2014-03-12 18:29:36 -07002050 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002051 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002052 return false;
2053 }
2054
Zhijun He204e3292014-07-14 17:09:23 -07002055 // We only send the aggregated partial when all 3A related metadata are available
2056 // For both API1 and API2.
2057 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002058 mResultSignal.signal();
2059
2060 return true;
2061}
2062
2063template<typename T>
2064bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002065 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002066 (void) frameNumber;
2067
2068 camera_metadata_ro_entry_t entry;
2069
2070 entry = result.find(tag);
2071 if (entry.count == 0) {
2072 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2073 mId, frameNumber, get_camera_metadata_tag_name(tag));
2074 return false;
2075 }
2076
2077 if (sizeof(T) == sizeof(uint8_t)) {
2078 *value = entry.data.u8[0];
2079 } else if (sizeof(T) == sizeof(int32_t)) {
2080 *value = entry.data.i32[0];
2081 } else {
2082 ALOGE("%s: Unexpected type", __FUNCTION__);
2083 return false;
2084 }
2085 return true;
2086}
2087
2088template<typename T>
2089bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002090 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002091 if (result.update(tag, value, 1) != NO_ERROR) {
2092 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2093 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2094 frameNumber, get_camera_metadata_tag_name(tag));
2095 return false;
2096 }
2097 return true;
2098}
2099
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002100void Camera3Device::returnOutputBuffers(
2101 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2102 nsecs_t timestamp) {
2103 for (size_t i = 0; i < numBuffers; i++)
2104 {
2105 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2106 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2107 // Note: stream may be deallocated at this point, if this buffer was
2108 // the last reference to it.
2109 if (res != OK) {
2110 ALOGE("Can't return buffer to its stream: %s (%d)",
2111 strerror(-res), res);
2112 }
2113 }
2114}
2115
2116
2117void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2118
2119 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2120 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2121
2122 nsecs_t sensorTimestamp = request.sensorTimestamp;
2123 nsecs_t shutterTimestamp = request.shutterTimestamp;
2124
2125 // Check if it's okay to remove the request from InFlightMap:
2126 // In the case of a successful request:
2127 // all input and output buffers, all result metadata, shutter callback
2128 // arrived.
2129 // In the case of a unsuccessful request:
2130 // all input and output buffers arrived.
2131 if (request.numBuffersLeft == 0 &&
2132 (request.requestStatus != OK ||
2133 (request.haveResultMetadata && shutterTimestamp != 0))) {
2134 ATRACE_ASYNC_END("frame capture", frameNumber);
2135
2136 // Sanity check - if sensor timestamp matches shutter timestamp
2137 if (request.requestStatus == OK &&
2138 sensorTimestamp != shutterTimestamp) {
2139 SET_ERR("sensor timestamp (%" PRId64
2140 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2141 sensorTimestamp, frameNumber, shutterTimestamp);
2142 }
2143
2144 // for an unsuccessful request, it may have pending output buffers to
2145 // return.
2146 assert(request.requestStatus != OK ||
2147 request.pendingOutputBuffers.size() == 0);
2148 returnOutputBuffers(request.pendingOutputBuffers.array(),
2149 request.pendingOutputBuffers.size(), 0);
2150
2151 mInFlightMap.removeItemsAt(idx, 1);
2152
2153 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2154 }
2155
2156 // Sanity check - if we have too many in-flight frames, something has
2157 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002158 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002159 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002160 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2161 kInFlightWarnLimitHighSpeed) {
2162 CLOGE("In-flight list too large for high speed configuration: %zu",
2163 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002164 }
2165}
2166
2167
2168void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2169 CaptureResultExtras &resultExtras,
2170 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002171 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002172 bool reprocess,
2173 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002174 if (pendingMetadata.isEmpty())
2175 return;
2176
2177 Mutex::Autolock l(mOutputLock);
2178
2179 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002180 if (reprocess) {
2181 if (frameNumber < mNextReprocessResultFrameNumber) {
2182 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002183 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002184 frameNumber, mNextReprocessResultFrameNumber);
2185 return;
2186 }
2187 mNextReprocessResultFrameNumber = frameNumber + 1;
2188 } else {
2189 if (frameNumber < mNextResultFrameNumber) {
2190 SET_ERR("Out-of-order capture result metadata submitted! "
2191 "(got frame number %d, expecting %d)",
2192 frameNumber, mNextResultFrameNumber);
2193 return;
2194 }
2195 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002196 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002197
2198 CaptureResult captureResult;
2199 captureResult.mResultExtras = resultExtras;
2200 captureResult.mMetadata = pendingMetadata;
2201
2202 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2203 (int32_t*)&frameNumber, 1) != OK) {
2204 SET_ERR("Failed to set frame# in metadata (%d)",
2205 frameNumber);
2206 return;
2207 } else {
2208 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2209 __FUNCTION__, mId, frameNumber);
2210 }
2211
2212 // Append any previous partials to form a complete result
2213 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2214 captureResult.mMetadata.append(collectedPartialResult);
2215 }
2216
2217 captureResult.mMetadata.sort();
2218
2219 // Check that there's a timestamp in the result metadata
2220 camera_metadata_entry entry =
2221 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2222 if (entry.count == 0) {
2223 SET_ERR("No timestamp provided by HAL for frame %d!",
2224 frameNumber);
2225 return;
2226 }
2227
Chien-Yu Chend196d612015-06-22 19:49:01 -07002228 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2229
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002230 // Valid result, insert into queue
2231 List<CaptureResult>::iterator queuedResult =
2232 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2233 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2234 ", burstId = %" PRId32, __FUNCTION__,
2235 queuedResult->mResultExtras.requestId,
2236 queuedResult->mResultExtras.frameNumber,
2237 queuedResult->mResultExtras.burstId);
2238
2239 mResultSignal.signal();
2240}
2241
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002242/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002243 * Camera HAL device callback methods
2244 */
2245
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002246void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002247 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002248
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002249 status_t res;
2250
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002251 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002252 if (result->result == NULL && result->num_output_buffers == 0 &&
2253 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002254 SET_ERR("No result data provided by HAL for frame %d",
2255 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002256 return;
2257 }
Zhijun He204e3292014-07-14 17:09:23 -07002258
2259 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2260 // partial_result to 1 when metadata is included in this result.
2261 if (!mUsePartialResult &&
2262 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2263 result->result != NULL &&
2264 result->partial_result != 1) {
2265 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2266 " if partial result is not supported",
2267 frameNumber, result->partial_result);
2268 return;
2269 }
2270
2271 bool isPartialResult = false;
2272 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002273 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002274 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002275
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002276 // Get shutter timestamp and resultExtras from list of in-flight requests,
2277 // where it was added by the shutter notification for this frame. If the
2278 // shutter timestamp isn't received yet, append the output buffers to the
2279 // in-flight request and they will be returned when the shutter timestamp
2280 // arrives. Update the in-flight status and remove the in-flight entry if
2281 // all result data and shutter timestamp have been received.
2282 nsecs_t shutterTimestamp = 0;
2283
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002284 {
2285 Mutex::Autolock l(mInFlightLock);
2286 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2287 if (idx == NAME_NOT_FOUND) {
2288 SET_ERR("Unknown frame number for capture result: %d",
2289 frameNumber);
2290 return;
2291 }
2292 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002293 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2294 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2295 ", partialResultCount = %d",
2296 __FUNCTION__, request.resultExtras.requestId,
2297 request.resultExtras.frameNumber, request.resultExtras.burstId,
2298 result->partial_result);
2299 // Always update the partial count to the latest one if it's not 0
2300 // (buffers only). When framework aggregates adjacent partial results
2301 // into one, the latest partial count will be used.
2302 if (result->partial_result != 0)
2303 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002304
2305 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002306 if (mUsePartialResult && result->result != NULL) {
2307 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2308 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2309 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2310 " the range of [1, %d] when metadata is included in the result",
2311 frameNumber, result->partial_result, mNumPartialResults);
2312 return;
2313 }
2314 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002315 if (isPartialResult) {
2316 request.partialResult.collectedResult.append(result->result);
2317 }
Zhijun He204e3292014-07-14 17:09:23 -07002318 } else {
2319 camera_metadata_ro_entry_t partialResultEntry;
2320 res = find_camera_metadata_ro_entry(result->result,
2321 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2322 if (res != NAME_NOT_FOUND &&
2323 partialResultEntry.count > 0 &&
2324 partialResultEntry.data.u8[0] ==
2325 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2326 // A partial result. Flag this as such, and collect this
2327 // set of metadata into the in-flight entry.
2328 isPartialResult = true;
2329 request.partialResult.collectedResult.append(
2330 result->result);
2331 request.partialResult.collectedResult.erase(
2332 ANDROID_QUIRKS_PARTIAL_RESULT);
2333 }
2334 }
2335
2336 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002337 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002338 if (!request.partialResult.haveSent3A) {
2339 request.partialResult.haveSent3A =
2340 processPartial3AResult(frameNumber,
2341 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002342 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002343 }
2344 }
2345 }
2346
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002347 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002348 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002349
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002350 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002351 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002352 if (request.haveResultMetadata) {
2353 SET_ERR("Called multiple times with metadata for frame %d",
2354 frameNumber);
2355 return;
2356 }
Zhijun He204e3292014-07-14 17:09:23 -07002357 if (mUsePartialResult &&
2358 !request.partialResult.collectedResult.isEmpty()) {
2359 collectedPartialResult.acquire(
2360 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002361 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002362 request.haveResultMetadata = true;
2363 }
2364
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002365 uint32_t numBuffersReturned = result->num_output_buffers;
2366 if (result->input_buffer != NULL) {
2367 if (hasInputBufferInRequest) {
2368 numBuffersReturned += 1;
2369 } else {
2370 ALOGW("%s: Input buffer should be NULL if there is no input"
2371 " buffer sent in the request",
2372 __FUNCTION__);
2373 }
2374 }
2375 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002376 if (request.numBuffersLeft < 0) {
2377 SET_ERR("Too many buffers returned for frame %d",
2378 frameNumber);
2379 return;
2380 }
2381
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002382 camera_metadata_ro_entry_t entry;
2383 res = find_camera_metadata_ro_entry(result->result,
2384 ANDROID_SENSOR_TIMESTAMP, &entry);
2385 if (res == OK && entry.count == 1) {
2386 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002387 }
2388
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002389 // If shutter event isn't received yet, append the output buffers to
2390 // the in-flight request. Otherwise, return the output buffers to
2391 // streams.
2392 if (shutterTimestamp == 0) {
2393 request.pendingOutputBuffers.appendArray(result->output_buffers,
2394 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002395 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002396 returnOutputBuffers(result->output_buffers,
2397 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002398 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002399
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002400 if (result->result != NULL && !isPartialResult) {
2401 if (shutterTimestamp == 0) {
2402 request.pendingMetadata = result->result;
2403 request.partialResult.collectedResult = collectedPartialResult;
2404 } else {
2405 CameraMetadata metadata;
2406 metadata = result->result;
2407 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002408 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2409 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002410 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002411 }
2412
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002413 removeInFlightRequestIfReadyLocked(idx);
2414 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002415
Zhijun Hef0d962a2014-06-30 10:24:11 -07002416 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002417 if (hasInputBufferInRequest) {
2418 Camera3Stream *stream =
2419 Camera3Stream::cast(result->input_buffer->stream);
2420 res = stream->returnInputBuffer(*(result->input_buffer));
2421 // Note: stream may be deallocated at this point, if this buffer was the
2422 // last reference to it.
2423 if (res != OK) {
2424 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2425 " its stream:%s (%d)", __FUNCTION__,
2426 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002427 }
2428 } else {
2429 ALOGW("%s: Input buffer should be NULL if there is no input"
2430 " buffer sent in the request, skipping input buffer return.",
2431 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002432 }
2433 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002434}
2435
2436void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002437 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002438 NotificationListener *listener;
2439 {
2440 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002441 listener = mListener;
2442 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002443
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002444 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002445 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002446 return;
2447 }
2448
2449 switch (msg->type) {
2450 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002451 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002452 break;
2453 }
2454 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002455 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002456 break;
2457 }
2458 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002459 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002460 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002461 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002462}
2463
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002464void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2465 NotificationListener *listener) {
2466
2467 // Map camera HAL error codes to ICameraDeviceCallback error codes
2468 // Index into this with the HAL error code
2469 static const ICameraDeviceCallbacks::CameraErrorCode
2470 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2471 // 0 = Unused error code
2472 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2473 // 1 = CAMERA3_MSG_ERROR_DEVICE
2474 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2475 // 2 = CAMERA3_MSG_ERROR_REQUEST
2476 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2477 // 3 = CAMERA3_MSG_ERROR_RESULT
2478 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2479 // 4 = CAMERA3_MSG_ERROR_BUFFER
2480 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2481 };
2482
2483 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2484 ((msg.error_code >= 0) &&
2485 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2486 halErrorMap[msg.error_code] :
2487 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2488
2489 int streamId = 0;
2490 if (msg.error_stream != NULL) {
2491 Camera3Stream *stream =
2492 Camera3Stream::cast(msg.error_stream);
2493 streamId = stream->getId();
2494 }
2495 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2496 mId, __FUNCTION__, msg.frame_number,
2497 streamId, msg.error_code);
2498
2499 CaptureResultExtras resultExtras;
2500 switch (errorCode) {
2501 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2502 // SET_ERR calls notifyError
2503 SET_ERR("Camera HAL reported serious device error");
2504 break;
2505 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2506 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2507 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2508 {
2509 Mutex::Autolock l(mInFlightLock);
2510 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2511 if (idx >= 0) {
2512 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2513 r.requestStatus = msg.error_code;
2514 resultExtras = r.resultExtras;
2515 } else {
2516 resultExtras.frameNumber = msg.frame_number;
2517 ALOGE("Camera %d: %s: cannot find in-flight request on "
2518 "frame %" PRId64 " error", mId, __FUNCTION__,
2519 resultExtras.frameNumber);
2520 }
2521 }
2522 if (listener != NULL) {
2523 listener->notifyError(errorCode, resultExtras);
2524 } else {
2525 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2526 }
2527 break;
2528 default:
2529 // SET_ERR calls notifyError
2530 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2531 break;
2532 }
2533}
2534
2535void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2536 NotificationListener *listener) {
2537 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002538
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002539 // Set timestamp for the request in the in-flight tracking
2540 // and get the request ID to send upstream
2541 {
2542 Mutex::Autolock l(mInFlightLock);
2543 idx = mInFlightMap.indexOfKey(msg.frame_number);
2544 if (idx >= 0) {
2545 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002546
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002547 // Verify ordering of shutter notifications
2548 {
2549 Mutex::Autolock l(mOutputLock);
2550 // TODO: need to track errors for tighter bounds on expected frame number.
2551 if (r.hasInputBuffer) {
2552 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2553 SET_ERR("Shutter notification out-of-order. Expected "
2554 "notification for frame %d, got frame %d",
2555 mNextReprocessShutterFrameNumber, msg.frame_number);
2556 return;
2557 }
2558 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2559 } else {
2560 if (msg.frame_number < mNextShutterFrameNumber) {
2561 SET_ERR("Shutter notification out-of-order. Expected "
2562 "notification for frame %d, got frame %d",
2563 mNextShutterFrameNumber, msg.frame_number);
2564 return;
2565 }
2566 mNextShutterFrameNumber = msg.frame_number + 1;
2567 }
2568 }
2569
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002570 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2571 mId, __FUNCTION__,
2572 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2573 // Call listener, if any
2574 if (listener != NULL) {
2575 listener->notifyShutter(r.resultExtras, msg.timestamp);
2576 }
2577
2578 r.shutterTimestamp = msg.timestamp;
2579
2580 // send pending result and buffers
2581 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002582 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002583 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002584 returnOutputBuffers(r.pendingOutputBuffers.array(),
2585 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2586 r.pendingOutputBuffers.clear();
2587
2588 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002589 }
2590 }
2591 if (idx < 0) {
2592 SET_ERR("Shutter notification for non-existent frame number %d",
2593 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002594 }
2595}
2596
2597
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002598CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002599 ALOGV("%s", __FUNCTION__);
2600
Igor Murashkin1e479c02013-09-06 16:55:14 -07002601 CameraMetadata retVal;
2602
2603 if (mRequestThread != NULL) {
2604 retVal = mRequestThread->getLatestRequest();
2605 }
2606
Igor Murashkin1e479c02013-09-06 16:55:14 -07002607 return retVal;
2608}
2609
Jianing Weicb0652e2014-03-12 18:29:36 -07002610
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002611/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002612 * RequestThread inner class methods
2613 */
2614
2615Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002616 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002617 camera3_device_t *hal3Device,
2618 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002619 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002620 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002621 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002622 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002623 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002624 mReconfigured(false),
2625 mDoPause(false),
2626 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002627 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002628 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002629 mCurrentAfTriggerId(0),
2630 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002631 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2632 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002633 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002634}
2635
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002636void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002637 NotificationListener *listener) {
2638 Mutex::Autolock l(mRequestLock);
2639 mListener = listener;
2640}
2641
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002642void Camera3Device::RequestThread::configurationComplete() {
2643 Mutex::Autolock l(mRequestLock);
2644 mReconfigured = true;
2645}
2646
Jianing Wei90e59c92014-03-12 18:29:36 -07002647status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002648 List<sp<CaptureRequest> > &requests,
2649 /*out*/
2650 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002651 Mutex::Autolock l(mRequestLock);
2652 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2653 ++it) {
2654 mRequestQueue.push_back(*it);
2655 }
2656
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002657 if (lastFrameNumber != NULL) {
2658 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2659 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2660 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2661 *lastFrameNumber);
2662 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002663
Jianing Wei90e59c92014-03-12 18:29:36 -07002664 unpauseForNewRequests();
2665
2666 return OK;
2667}
2668
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002669
2670status_t Camera3Device::RequestThread::queueTrigger(
2671 RequestTrigger trigger[],
2672 size_t count) {
2673
2674 Mutex::Autolock l(mTriggerMutex);
2675 status_t ret;
2676
2677 for (size_t i = 0; i < count; ++i) {
2678 ret = queueTriggerLocked(trigger[i]);
2679
2680 if (ret != OK) {
2681 return ret;
2682 }
2683 }
2684
2685 return OK;
2686}
2687
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002688int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2689 sp<Camera3Device> d = device.promote();
2690 if (d != NULL) return d->mId;
2691 return 0;
2692}
2693
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002694status_t Camera3Device::RequestThread::queueTriggerLocked(
2695 RequestTrigger trigger) {
2696
2697 uint32_t tag = trigger.metadataTag;
2698 ssize_t index = mTriggerMap.indexOfKey(tag);
2699
2700 switch (trigger.getTagType()) {
2701 case TYPE_BYTE:
2702 // fall-through
2703 case TYPE_INT32:
2704 break;
2705 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002706 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2707 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002708 return INVALID_OPERATION;
2709 }
2710
2711 /**
2712 * Collect only the latest trigger, since we only have 1 field
2713 * in the request settings per trigger tag, and can't send more than 1
2714 * trigger per request.
2715 */
2716 if (index != NAME_NOT_FOUND) {
2717 mTriggerMap.editValueAt(index) = trigger;
2718 } else {
2719 mTriggerMap.add(tag, trigger);
2720 }
2721
2722 return OK;
2723}
2724
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002725status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002726 const RequestList &requests,
2727 /*out*/
2728 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002729 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002730 if (lastFrameNumber != NULL) {
2731 *lastFrameNumber = mRepeatingLastFrameNumber;
2732 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002733 mRepeatingRequests.clear();
2734 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2735 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002736
2737 unpauseForNewRequests();
2738
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002739 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002740 return OK;
2741}
2742
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002743bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2744 if (mRepeatingRequests.empty()) {
2745 return false;
2746 }
2747 int32_t requestId = requestIn->mResultExtras.requestId;
2748 const RequestList &repeatRequests = mRepeatingRequests;
2749 // All repeating requests are guaranteed to have same id so only check first quest
2750 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2751 return (firstRequest->mResultExtras.requestId == requestId);
2752}
2753
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002754status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002755 Mutex::Autolock l(mRequestLock);
2756 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002757 if (lastFrameNumber != NULL) {
2758 *lastFrameNumber = mRepeatingLastFrameNumber;
2759 }
2760 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002761 return OK;
2762}
2763
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002764status_t Camera3Device::RequestThread::clear(
2765 NotificationListener *listener,
2766 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002767 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002768 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002769
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002770 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002771
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002772 // Send errors for all requests pending in the request queue, including
2773 // pending repeating requests
2774 if (listener != NULL) {
2775 for (RequestList::iterator it = mRequestQueue.begin();
2776 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002777 // Abort the input buffers for reprocess requests.
2778 if ((*it)->mInputStream != NULL) {
2779 camera3_stream_buffer_t inputBuffer;
2780 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2781 if (res != OK) {
2782 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2783 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2784 } else {
2785 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2786 if (res != OK) {
2787 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2788 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2789 }
2790 }
2791 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002792 // Set the frame number this request would have had, if it
2793 // had been submitted; this frame number will not be reused.
2794 // The requestId and burstId fields were set when the request was
2795 // submitted originally (in convertMetadataListToRequestListLocked)
2796 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2797 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2798 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002799 }
2800 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002801 mRequestQueue.clear();
2802 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002803 if (lastFrameNumber != NULL) {
2804 *lastFrameNumber = mRepeatingLastFrameNumber;
2805 }
2806 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002807 return OK;
2808}
2809
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002810status_t Camera3Device::RequestThread::flush() {
2811 ATRACE_CALL();
2812 Mutex::Autolock l(mFlushLock);
2813
2814 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2815 return mHal3Device->ops->flush(mHal3Device);
2816 }
2817
2818 return -ENOTSUP;
2819}
2820
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002821void Camera3Device::RequestThread::setPaused(bool paused) {
2822 Mutex::Autolock l(mPauseLock);
2823 mDoPause = paused;
2824 mDoPauseSignal.signal();
2825}
2826
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002827status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2828 int32_t requestId, nsecs_t timeout) {
2829 Mutex::Autolock l(mLatestRequestMutex);
2830 status_t res;
2831 while (mLatestRequestId != requestId) {
2832 nsecs_t startTime = systemTime();
2833
2834 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2835 if (res != OK) return res;
2836
2837 timeout -= (systemTime() - startTime);
2838 }
2839
2840 return OK;
2841}
2842
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002843void Camera3Device::RequestThread::requestExit() {
2844 // Call parent to set up shutdown
2845 Thread::requestExit();
2846 // The exit from any possible waits
2847 mDoPauseSignal.signal();
2848 mRequestSignal.signal();
2849}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002850
Chien-Yu Chend196d612015-06-22 19:49:01 -07002851
2852/**
2853 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2854 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2855 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2856 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2857 * request.
2858 */
2859void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2860 request->mAeTriggerCancelOverride.applyAeLock = false;
2861 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2862
2863 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2864 return;
2865 }
2866
2867 camera_metadata_entry_t aePrecaptureTrigger =
2868 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2869 if (aePrecaptureTrigger.count > 0 &&
2870 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2871 // Always override CANCEL to IDLE
2872 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2873 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2874 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2875 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2876 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2877
2878 if (mAeLockAvailable == true) {
2879 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2880 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2881 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2882 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2883 request->mAeTriggerCancelOverride.applyAeLock = true;
2884 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2885 }
2886 }
2887 }
2888}
2889
2890/**
2891 * Override result metadata for cancelling AE precapture trigger applied in
2892 * handleAePrecaptureCancelRequest().
2893 */
2894void Camera3Device::overrideResultForPrecaptureCancel(
2895 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2896 if (aeTriggerCancelOverride.applyAeLock) {
2897 // Only devices <= v3.2 should have this override
2898 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2899 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2900 }
2901
2902 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2903 // Only devices <= v3.2 should have this override
2904 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2905 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2906 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2907 }
2908}
2909
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002910bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002911 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002912 status_t res;
2913
2914 // Handle paused state.
2915 if (waitIfPaused()) {
2916 return true;
2917 }
2918
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002919 // Wait for the next batch of requests.
2920 waitForNextRequestBatch();
2921 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002922 return true;
2923 }
2924
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002925 // Get the latest request ID, if any
2926 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002927 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002928 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002929 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002930 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002931 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002932 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
2933 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002934 }
2935
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002936 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002937 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002938 if (res == TIMED_OUT) {
2939 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002940 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002941 return true;
2942 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002943 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002944 return false;
2945 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002946
Zhijun Hecc27e112013-10-03 16:12:43 -07002947 // Inform waitUntilRequestProcessed thread of a new request ID
2948 {
2949 Mutex::Autolock al(mLatestRequestMutex);
2950
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002951 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07002952 mLatestRequestSignal.signal();
2953 }
2954
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002955 // Submit a batch of requests to HAL.
2956 // Use flush lock only when submitting multilple requests in a batch.
2957 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
2958 // which may take a long time to finish so synchronizing flush() and
2959 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
2960 // For now, only synchronize for high speed recording and we should figure something out for
2961 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002962 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002963
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002964 if (useFlushLock) {
2965 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002966 }
2967
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002968 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002969 mNextRequests.size());
2970 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002971 // Submit request and block until ready for next one
2972 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
2973 ATRACE_BEGIN("camera3->process_capture_request");
2974 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
2975 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07002976
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002977 if (res != OK) {
2978 // Should only get a failure here for malformed requests or device-level
2979 // errors, so consider all errors fatal. Bad metadata failures should
2980 // come through notify.
2981 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
2982 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
2983 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002984 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002985 if (useFlushLock) {
2986 mFlushLock.unlock();
2987 }
2988 return false;
2989 }
2990
2991 // Mark that the request has be submitted successfully.
2992 nextRequest.submitted = true;
2993
2994 // Update the latest request sent to HAL
2995 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
2996 Mutex::Autolock al(mLatestRequestMutex);
2997
2998 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
2999 mLatestRequest.acquire(cloned);
3000 }
3001
3002 if (nextRequest.halRequest.settings != NULL) {
3003 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3004 }
3005
3006 // Remove any previously queued triggers (after unlock)
3007 res = removeTriggers(mPrevRequest);
3008 if (res != OK) {
3009 SET_ERR("RequestThread: Unable to remove triggers "
3010 "(capture request %d, HAL device: %s (%d)",
3011 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003012 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003013 if (useFlushLock) {
3014 mFlushLock.unlock();
3015 }
3016 return false;
3017 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003018 }
3019
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003020 if (useFlushLock) {
3021 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003022 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003023
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003024 // Unset as current request
3025 {
3026 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003027 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003028 }
3029
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003030 return true;
3031}
3032
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003033status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003034 ATRACE_CALL();
3035
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003036 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003037 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3038 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3039 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3040
3041 // Prepare a request to HAL
3042 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3043
3044 // Insert any queued triggers (before metadata is locked)
3045 status_t res = insertTriggers(captureRequest);
3046
3047 if (res < 0) {
3048 SET_ERR("RequestThread: Unable to insert triggers "
3049 "(capture request %d, HAL device: %s (%d)",
3050 halRequest->frame_number, strerror(-res), res);
3051 return INVALID_OPERATION;
3052 }
3053 int triggerCount = res;
3054 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3055 mPrevTriggers = triggerCount;
3056
3057 // If the request is the same as last, or we had triggers last time
3058 if (mPrevRequest != captureRequest || triggersMixedIn) {
3059 /**
3060 * HAL workaround:
3061 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3062 */
3063 res = addDummyTriggerIds(captureRequest);
3064 if (res != OK) {
3065 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3066 "(capture request %d, HAL device: %s (%d)",
3067 halRequest->frame_number, strerror(-res), res);
3068 return INVALID_OPERATION;
3069 }
3070
3071 /**
3072 * The request should be presorted so accesses in HAL
3073 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3074 */
3075 captureRequest->mSettings.sort();
3076 halRequest->settings = captureRequest->mSettings.getAndLock();
3077 mPrevRequest = captureRequest;
3078 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3079
3080 IF_ALOGV() {
3081 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3082 find_camera_metadata_ro_entry(
3083 halRequest->settings,
3084 ANDROID_CONTROL_AF_TRIGGER,
3085 &e
3086 );
3087 if (e.count > 0) {
3088 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3089 __FUNCTION__,
3090 halRequest->frame_number,
3091 e.data.u8[0]);
3092 }
3093 }
3094 } else {
3095 // leave request.settings NULL to indicate 'reuse latest given'
3096 ALOGVV("%s: Request settings are REUSED",
3097 __FUNCTION__);
3098 }
3099
3100 uint32_t totalNumBuffers = 0;
3101
3102 // Fill in buffers
3103 if (captureRequest->mInputStream != NULL) {
3104 halRequest->input_buffer = &captureRequest->mInputBuffer;
3105 totalNumBuffers += 1;
3106 } else {
3107 halRequest->input_buffer = NULL;
3108 }
3109
3110 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3111 captureRequest->mOutputStreams.size());
3112 halRequest->output_buffers = outputBuffers->array();
3113 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3114 res = captureRequest->mOutputStreams.editItemAt(i)->
3115 getBuffer(&outputBuffers->editItemAt(i));
3116 if (res != OK) {
3117 // Can't get output buffer from gralloc queue - this could be due to
3118 // abandoned queue or other consumer misbehavior, so not a fatal
3119 // error
3120 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3121 " %s (%d)", strerror(-res), res);
3122
3123 return TIMED_OUT;
3124 }
3125 halRequest->num_output_buffers++;
3126 }
3127 totalNumBuffers += halRequest->num_output_buffers;
3128
3129 // Log request in the in-flight queue
3130 sp<Camera3Device> parent = mParent.promote();
3131 if (parent == NULL) {
3132 // Should not happen, and nowhere to send errors to, so just log it
3133 CLOGE("RequestThread: Parent is gone");
3134 return INVALID_OPERATION;
3135 }
3136 res = parent->registerInFlight(halRequest->frame_number,
3137 totalNumBuffers, captureRequest->mResultExtras,
3138 /*hasInput*/halRequest->input_buffer != NULL,
3139 captureRequest->mAeTriggerCancelOverride);
3140 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3141 ", burstId = %" PRId32 ".",
3142 __FUNCTION__,
3143 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3144 captureRequest->mResultExtras.burstId);
3145 if (res != OK) {
3146 SET_ERR("RequestThread: Unable to register new in-flight request:"
3147 " %s (%d)", strerror(-res), res);
3148 return INVALID_OPERATION;
3149 }
3150 }
3151
3152 return OK;
3153}
3154
Igor Murashkin1e479c02013-09-06 16:55:14 -07003155CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3156 Mutex::Autolock al(mLatestRequestMutex);
3157
3158 ALOGV("RequestThread::%s", __FUNCTION__);
3159
3160 return mLatestRequest;
3161}
3162
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003163bool Camera3Device::RequestThread::isStreamPending(
3164 sp<Camera3StreamInterface>& stream) {
3165 Mutex::Autolock l(mRequestLock);
3166
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003167 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003168 if (!nextRequest.submitted) {
3169 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3170 if (stream == s) return true;
3171 }
3172 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003173 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003174 }
3175
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003176 for (const auto& request : mRequestQueue) {
3177 for (const auto& s : request->mOutputStreams) {
3178 if (stream == s) return true;
3179 }
3180 if (stream == request->mInputStream) return true;
3181 }
3182
3183 for (const auto& request : mRepeatingRequests) {
3184 for (const auto& s : request->mOutputStreams) {
3185 if (stream == s) return true;
3186 }
3187 if (stream == request->mInputStream) return true;
3188 }
3189
3190 return false;
3191}
Jianing Weicb0652e2014-03-12 18:29:36 -07003192
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003193void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3194 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003195 return;
3196 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003197
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003198 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003199 // Skip the ones that have been submitted successfully.
3200 if (nextRequest.submitted) {
3201 continue;
3202 }
3203
3204 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3205 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3206 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3207
3208 if (halRequest->settings != NULL) {
3209 captureRequest->mSettings.unlock(halRequest->settings);
3210 }
3211
3212 if (captureRequest->mInputStream != NULL) {
3213 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3214 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3215 }
3216
3217 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3218 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3219 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3220 }
3221
3222 if (sendRequestError) {
3223 Mutex::Autolock l(mRequestLock);
3224 if (mListener != NULL) {
3225 mListener->notifyError(
3226 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3227 captureRequest->mResultExtras);
3228 }
3229 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003230 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003231
3232 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003233 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003234}
3235
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003236void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003237 // Optimized a bit for the simple steady-state case (single repeating
3238 // request), to avoid putting that request in the queue temporarily.
3239 Mutex::Autolock l(mRequestLock);
3240
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003241 assert(mNextRequests.empty());
3242
3243 NextRequest nextRequest;
3244 nextRequest.captureRequest = waitForNextRequestLocked();
3245 if (nextRequest.captureRequest == nullptr) {
3246 return;
3247 }
3248
3249 nextRequest.halRequest = camera3_capture_request_t();
3250 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003251 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003252
3253 // Wait for additional requests
3254 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3255
3256 for (size_t i = 1; i < batchSize; i++) {
3257 NextRequest additionalRequest;
3258 additionalRequest.captureRequest = waitForNextRequestLocked();
3259 if (additionalRequest.captureRequest == nullptr) {
3260 break;
3261 }
3262
3263 additionalRequest.halRequest = camera3_capture_request_t();
3264 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003265 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003266 }
3267
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003268 if (mNextRequests.size() < batchSize) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003269 ALOGE("RequestThread: only get %d out of %d requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003270 mNextRequests.size(), batchSize);
3271 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003272 }
3273
3274 return;
3275}
3276
3277sp<Camera3Device::CaptureRequest>
3278 Camera3Device::RequestThread::waitForNextRequestLocked() {
3279 status_t res;
3280 sp<CaptureRequest> nextRequest;
3281
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003282 while (mRequestQueue.empty()) {
3283 if (!mRepeatingRequests.empty()) {
3284 // Always atomically enqueue all requests in a repeating request
3285 // list. Guarantees a complete in-sequence set of captures to
3286 // application.
3287 const RequestList &requests = mRepeatingRequests;
3288 RequestList::const_iterator firstRequest =
3289 requests.begin();
3290 nextRequest = *firstRequest;
3291 mRequestQueue.insert(mRequestQueue.end(),
3292 ++firstRequest,
3293 requests.end());
3294 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003295
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003296 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003297
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003298 break;
3299 }
3300
3301 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3302
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003303 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3304 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003305 Mutex::Autolock pl(mPauseLock);
3306 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003307 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003308 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003309 // Let the tracker know
3310 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3311 if (statusTracker != 0) {
3312 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3313 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003314 }
3315 // Stop waiting for now and let thread management happen
3316 return NULL;
3317 }
3318 }
3319
3320 if (nextRequest == NULL) {
3321 // Don't have a repeating request already in hand, so queue
3322 // must have an entry now.
3323 RequestList::iterator firstRequest =
3324 mRequestQueue.begin();
3325 nextRequest = *firstRequest;
3326 mRequestQueue.erase(firstRequest);
3327 }
3328
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003329 // In case we've been unpaused by setPaused clearing mDoPause, need to
3330 // update internal pause state (capture/setRepeatingRequest unpause
3331 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003332 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003333 if (mPaused) {
3334 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3335 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3336 if (statusTracker != 0) {
3337 statusTracker->markComponentActive(mStatusId);
3338 }
3339 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003340 mPaused = false;
3341
3342 // Check if we've reconfigured since last time, and reset the preview
3343 // request if so. Can't use 'NULL request == repeat' across configure calls.
3344 if (mReconfigured) {
3345 mPrevRequest.clear();
3346 mReconfigured = false;
3347 }
3348
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003349 if (nextRequest != NULL) {
3350 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003351 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3352 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003353
3354 // Since RequestThread::clear() removes buffers from the input stream,
3355 // get the right buffer here before unlocking mRequestLock
3356 if (nextRequest->mInputStream != NULL) {
3357 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3358 if (res != OK) {
3359 // Can't get input buffer from gralloc queue - this could be due to
3360 // disconnected queue or other producer misbehavior, so not a fatal
3361 // error
3362 ALOGE("%s: Can't get input buffer, skipping request:"
3363 " %s (%d)", __FUNCTION__, strerror(-res), res);
3364 if (mListener != NULL) {
3365 mListener->notifyError(
3366 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3367 nextRequest->mResultExtras);
3368 }
3369 return NULL;
3370 }
3371 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003372 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003373
3374 handleAePrecaptureCancelRequest(nextRequest);
3375
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003376 return nextRequest;
3377}
3378
3379bool Camera3Device::RequestThread::waitIfPaused() {
3380 status_t res;
3381 Mutex::Autolock l(mPauseLock);
3382 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003383 if (mPaused == false) {
3384 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003385 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3386 // Let the tracker know
3387 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3388 if (statusTracker != 0) {
3389 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3390 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003391 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003392
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003393 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003394 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003395 return true;
3396 }
3397 }
3398 // We don't set mPaused to false here, because waitForNextRequest needs
3399 // to further manage the paused state in case of starvation.
3400 return false;
3401}
3402
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003403void Camera3Device::RequestThread::unpauseForNewRequests() {
3404 // With work to do, mark thread as unpaused.
3405 // If paused by request (setPaused), don't resume, to avoid
3406 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003407 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003408 Mutex::Autolock p(mPauseLock);
3409 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003410 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3411 if (mPaused) {
3412 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3413 if (statusTracker != 0) {
3414 statusTracker->markComponentActive(mStatusId);
3415 }
3416 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003417 mPaused = false;
3418 }
3419}
3420
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003421void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3422 sp<Camera3Device> parent = mParent.promote();
3423 if (parent != NULL) {
3424 va_list args;
3425 va_start(args, fmt);
3426
3427 parent->setErrorStateV(fmt, args);
3428
3429 va_end(args);
3430 }
3431}
3432
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003433status_t Camera3Device::RequestThread::insertTriggers(
3434 const sp<CaptureRequest> &request) {
3435
3436 Mutex::Autolock al(mTriggerMutex);
3437
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003438 sp<Camera3Device> parent = mParent.promote();
3439 if (parent == NULL) {
3440 CLOGE("RequestThread: Parent is gone");
3441 return DEAD_OBJECT;
3442 }
3443
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003444 CameraMetadata &metadata = request->mSettings;
3445 size_t count = mTriggerMap.size();
3446
3447 for (size_t i = 0; i < count; ++i) {
3448 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003449 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003450
3451 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3452 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3453 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003454 if (isAeTrigger) {
3455 request->mResultExtras.precaptureTriggerId = triggerId;
3456 mCurrentPreCaptureTriggerId = triggerId;
3457 } else {
3458 request->mResultExtras.afTriggerId = triggerId;
3459 mCurrentAfTriggerId = triggerId;
3460 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003461 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3462 continue; // Trigger ID tag is deprecated since device HAL 3.2
3463 }
3464 }
3465
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003466 camera_metadata_entry entry = metadata.find(tag);
3467
3468 if (entry.count > 0) {
3469 /**
3470 * Already has an entry for this trigger in the request.
3471 * Rewrite it with our requested trigger value.
3472 */
3473 RequestTrigger oldTrigger = trigger;
3474
3475 oldTrigger.entryValue = entry.data.u8[0];
3476
3477 mTriggerReplacedMap.add(tag, oldTrigger);
3478 } else {
3479 /**
3480 * More typical, no trigger entry, so we just add it
3481 */
3482 mTriggerRemovedMap.add(tag, trigger);
3483 }
3484
3485 status_t res;
3486
3487 switch (trigger.getTagType()) {
3488 case TYPE_BYTE: {
3489 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3490 res = metadata.update(tag,
3491 &entryValue,
3492 /*count*/1);
3493 break;
3494 }
3495 case TYPE_INT32:
3496 res = metadata.update(tag,
3497 &trigger.entryValue,
3498 /*count*/1);
3499 break;
3500 default:
3501 ALOGE("%s: Type not supported: 0x%x",
3502 __FUNCTION__,
3503 trigger.getTagType());
3504 return INVALID_OPERATION;
3505 }
3506
3507 if (res != OK) {
3508 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3509 ", value %d", __FUNCTION__, trigger.getTagName(),
3510 trigger.entryValue);
3511 return res;
3512 }
3513
3514 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3515 trigger.getTagName(),
3516 trigger.entryValue);
3517 }
3518
3519 mTriggerMap.clear();
3520
3521 return count;
3522}
3523
3524status_t Camera3Device::RequestThread::removeTriggers(
3525 const sp<CaptureRequest> &request) {
3526 Mutex::Autolock al(mTriggerMutex);
3527
3528 CameraMetadata &metadata = request->mSettings;
3529
3530 /**
3531 * Replace all old entries with their old values.
3532 */
3533 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3534 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3535
3536 status_t res;
3537
3538 uint32_t tag = trigger.metadataTag;
3539 switch (trigger.getTagType()) {
3540 case TYPE_BYTE: {
3541 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3542 res = metadata.update(tag,
3543 &entryValue,
3544 /*count*/1);
3545 break;
3546 }
3547 case TYPE_INT32:
3548 res = metadata.update(tag,
3549 &trigger.entryValue,
3550 /*count*/1);
3551 break;
3552 default:
3553 ALOGE("%s: Type not supported: 0x%x",
3554 __FUNCTION__,
3555 trigger.getTagType());
3556 return INVALID_OPERATION;
3557 }
3558
3559 if (res != OK) {
3560 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3561 ", trigger value %d", __FUNCTION__,
3562 trigger.getTagName(), trigger.entryValue);
3563 return res;
3564 }
3565 }
3566 mTriggerReplacedMap.clear();
3567
3568 /**
3569 * Remove all new entries.
3570 */
3571 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3572 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3573 status_t res = metadata.erase(trigger.metadataTag);
3574
3575 if (res != OK) {
3576 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3577 ", trigger value %d", __FUNCTION__,
3578 trigger.getTagName(), trigger.entryValue);
3579 return res;
3580 }
3581 }
3582 mTriggerRemovedMap.clear();
3583
3584 return OK;
3585}
3586
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003587status_t Camera3Device::RequestThread::addDummyTriggerIds(
3588 const sp<CaptureRequest> &request) {
3589 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3590 static const int32_t dummyTriggerId = 1;
3591 status_t res;
3592
3593 CameraMetadata &metadata = request->mSettings;
3594
3595 // If AF trigger is active, insert a dummy AF trigger ID if none already
3596 // exists
3597 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3598 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3599 if (afTrigger.count > 0 &&
3600 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3601 afId.count == 0) {
3602 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3603 if (res != OK) return res;
3604 }
3605
3606 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3607 // if none already exists
3608 camera_metadata_entry pcTrigger =
3609 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3610 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3611 if (pcTrigger.count > 0 &&
3612 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3613 pcId.count == 0) {
3614 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3615 &dummyTriggerId, 1);
3616 if (res != OK) return res;
3617 }
3618
3619 return OK;
3620}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003621
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003622/**
3623 * PreparerThread inner class methods
3624 */
3625
3626Camera3Device::PreparerThread::PreparerThread() :
3627 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3628}
3629
3630Camera3Device::PreparerThread::~PreparerThread() {
3631 Thread::requestExitAndWait();
3632 if (mCurrentStream != nullptr) {
3633 mCurrentStream->cancelPrepare();
3634 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3635 mCurrentStream.clear();
3636 }
3637 clear();
3638}
3639
Ruben Brunkc78ac262015-08-13 17:58:46 -07003640status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003641 status_t res;
3642
3643 Mutex::Autolock l(mLock);
3644
Ruben Brunkc78ac262015-08-13 17:58:46 -07003645 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003646 if (res == OK) {
3647 // No preparation needed, fire listener right off
3648 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3649 if (mListener) {
3650 mListener->notifyPrepared(stream->getId());
3651 }
3652 return OK;
3653 } else if (res != NOT_ENOUGH_DATA) {
3654 return res;
3655 }
3656
3657 // Need to prepare, start up thread if necessary
3658 if (!mActive) {
3659 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3660 // isn't running
3661 Thread::requestExitAndWait();
3662 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3663 if (res != OK) {
3664 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3665 if (mListener) {
3666 mListener->notifyPrepared(stream->getId());
3667 }
3668 return res;
3669 }
3670 mCancelNow = false;
3671 mActive = true;
3672 ALOGV("%s: Preparer stream started", __FUNCTION__);
3673 }
3674
3675 // queue up the work
3676 mPendingStreams.push_back(stream);
3677 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3678
3679 return OK;
3680}
3681
3682status_t Camera3Device::PreparerThread::clear() {
3683 status_t res;
3684
3685 Mutex::Autolock l(mLock);
3686
3687 for (const auto& stream : mPendingStreams) {
3688 stream->cancelPrepare();
3689 }
3690 mPendingStreams.clear();
3691 mCancelNow = true;
3692
3693 return OK;
3694}
3695
3696void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3697 Mutex::Autolock l(mLock);
3698 mListener = listener;
3699}
3700
3701bool Camera3Device::PreparerThread::threadLoop() {
3702 status_t res;
3703 {
3704 Mutex::Autolock l(mLock);
3705 if (mCurrentStream == nullptr) {
3706 // End thread if done with work
3707 if (mPendingStreams.empty()) {
3708 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3709 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3710 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3711 mActive = false;
3712 return false;
3713 }
3714
3715 // Get next stream to prepare
3716 auto it = mPendingStreams.begin();
3717 mCurrentStream = *it;
3718 mPendingStreams.erase(it);
3719 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3720 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3721 } else if (mCancelNow) {
3722 mCurrentStream->cancelPrepare();
3723 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3724 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3725 mCurrentStream.clear();
3726 mCancelNow = false;
3727 return true;
3728 }
3729 }
3730
3731 res = mCurrentStream->prepareNextBuffer();
3732 if (res == NOT_ENOUGH_DATA) return true;
3733 if (res != OK) {
3734 // Something bad happened; try to recover by cancelling prepare and
3735 // signalling listener anyway
3736 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3737 mCurrentStream->getId(), res, strerror(-res));
3738 mCurrentStream->cancelPrepare();
3739 }
3740
3741 // This stream has finished, notify listener
3742 Mutex::Autolock l(mLock);
3743 if (mListener) {
3744 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3745 mCurrentStream->getId());
3746 mListener->notifyPrepared(mCurrentStream->getId());
3747 }
3748
3749 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3750 mCurrentStream.clear();
3751
3752 return true;
3753}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003754
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003755/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003756 * Static callback forwarding methods from HAL to instance
3757 */
3758
3759void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3760 const camera3_capture_result *result) {
3761 Camera3Device *d =
3762 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003763
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003764 d->processCaptureResult(result);
3765}
3766
3767void Camera3Device::sNotify(const camera3_callback_ops *cb,
3768 const camera3_notify_msg *msg) {
3769 Camera3Device *d =
3770 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3771 d->notify(msg);
3772}
3773
3774}; // namespace android