blob: 6c07aef7f0c2a3fd17fc46081d0202628f58e4c3 [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
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800425ssize_t Camera3Device::getRawOpaqueBufferSize(uint32_t width, uint32_t height) const {
426 const int PER_CONFIGURATION_SIZE = 3;
427 const int WIDTH_OFFSET = 0;
428 const int HEIGHT_OFFSET = 1;
429 const int SIZE_OFFSET = 2;
430 camera_metadata_ro_entry rawOpaqueSizes =
431 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
432 int count = rawOpaqueSizes.count;
433 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
434 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%d)!",
435 __FUNCTION__, mId, count);
436 return BAD_VALUE;
437 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700438
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800439 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
440 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
441 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
442 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
443 }
444 }
445
446 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
447 __FUNCTION__, mId, width, height);
448 return BAD_VALUE;
449}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700450
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800451status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
452 ATRACE_CALL();
453 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700454
455 // Try to lock, but continue in case of failure (to avoid blocking in
456 // deadlocks)
457 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
458 bool gotLock = tryLockSpinRightRound(mLock);
459
460 ALOGW_IF(!gotInterfaceLock,
461 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
462 mId, __FUNCTION__);
463 ALOGW_IF(!gotLock,
464 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
465 mId, __FUNCTION__);
466
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800467 bool dumpTemplates = false;
468 String16 templatesOption("-t");
469 int n = args.size();
470 for (int i = 0; i < n; i++) {
471 if (args[i] == templatesOption) {
472 dumpTemplates = true;
473 }
474 }
475
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800476 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800477
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800478 const char *status =
479 mStatus == STATUS_ERROR ? "ERROR" :
480 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700481 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
482 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800483 mStatus == STATUS_ACTIVE ? "ACTIVE" :
484 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700485
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800486 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700487 if (mStatus == STATUS_ERROR) {
488 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
489 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800490 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700491 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700492 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800493
494 if (mInputStream != NULL) {
495 write(fd, lines.string(), lines.size());
496 mInputStream->dump(fd, args);
497 } else {
498 lines.appendFormat(" No input stream.\n");
499 write(fd, lines.string(), lines.size());
500 }
501 for (size_t i = 0; i < mOutputStreams.size(); i++) {
502 mOutputStreams[i]->dump(fd,args);
503 }
504
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700505 lines = String8(" In-flight requests:\n");
506 if (mInFlightMap.size() == 0) {
507 lines.append(" None\n");
508 } else {
509 for (size_t i = 0; i < mInFlightMap.size(); i++) {
510 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700511 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700512 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800513 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700514 r.numBuffersLeft);
515 }
516 }
517 write(fd, lines.string(), lines.size());
518
Igor Murashkin1e479c02013-09-06 16:55:14 -0700519 {
520 lines = String8(" Last request sent:\n");
521 write(fd, lines.string(), lines.size());
522
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700523 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700524 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
525 }
526
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800527 if (dumpTemplates) {
528 const char *templateNames[] = {
529 "TEMPLATE_PREVIEW",
530 "TEMPLATE_STILL_CAPTURE",
531 "TEMPLATE_VIDEO_RECORD",
532 "TEMPLATE_VIDEO_SNAPSHOT",
533 "TEMPLATE_ZERO_SHUTTER_LAG",
534 "TEMPLATE_MANUAL"
535 };
536
537 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
538 const camera_metadata_t *templateRequest;
539 templateRequest =
540 mHal3Device->ops->construct_default_request_settings(
541 mHal3Device, i);
542 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
543 if (templateRequest == NULL) {
544 lines.append(" Not supported\n");
545 write(fd, lines.string(), lines.size());
546 } else {
547 write(fd, lines.string(), lines.size());
548 dump_indented_camera_metadata(templateRequest,
549 fd, /*verbosity*/2, /*indentation*/8);
550 }
551 }
552 }
553
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800554 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700555 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800556 write(fd, lines.string(), lines.size());
557 mHal3Device->ops->dump(mHal3Device, fd);
558 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800559
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700560 if (gotLock) mLock.unlock();
561 if (gotInterfaceLock) mInterfaceLock.unlock();
562
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800563 return OK;
564}
565
566const CameraMetadata& Camera3Device::info() const {
567 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800568 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
569 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700570 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800571 mStatus == STATUS_ERROR ?
572 "when in error state" : "before init");
573 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800574 return mDeviceInfo;
575}
576
Jianing Wei90e59c92014-03-12 18:29:36 -0700577status_t Camera3Device::checkStatusOkToCaptureLocked() {
578 switch (mStatus) {
579 case STATUS_ERROR:
580 CLOGE("Device has encountered a serious error");
581 return INVALID_OPERATION;
582 case STATUS_UNINITIALIZED:
583 CLOGE("Device not initialized");
584 return INVALID_OPERATION;
585 case STATUS_UNCONFIGURED:
586 case STATUS_CONFIGURED:
587 case STATUS_ACTIVE:
588 // OK
589 break;
590 default:
591 SET_ERR_L("Unexpected status: %d", mStatus);
592 return INVALID_OPERATION;
593 }
594 return OK;
595}
596
597status_t Camera3Device::convertMetadataListToRequestListLocked(
598 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
599 if (requestList == NULL) {
600 CLOGE("requestList cannot be NULL.");
601 return BAD_VALUE;
602 }
603
Jianing Weicb0652e2014-03-12 18:29:36 -0700604 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700605 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
606 it != metadataList.end(); ++it) {
607 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
608 if (newRequest == 0) {
609 CLOGE("Can't create capture request");
610 return BAD_VALUE;
611 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700612
613 // Setup burst Id and request Id
614 newRequest->mResultExtras.burstId = burstId++;
615 if (it->exists(ANDROID_REQUEST_ID)) {
616 if (it->find(ANDROID_REQUEST_ID).count == 0) {
617 CLOGE("RequestID entry exists; but must not be empty in metadata");
618 return BAD_VALUE;
619 }
620 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
621 } else {
622 CLOGE("RequestID does not exist in metadata");
623 return BAD_VALUE;
624 }
625
Jianing Wei90e59c92014-03-12 18:29:36 -0700626 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700627
628 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700629 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700630
631 // Setup batch size if this is a high speed video recording request.
632 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
633 auto firstRequest = requestList->begin();
634 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
635 if (outputStream->isVideoStream()) {
636 (*firstRequest)->mBatchSize = requestList->size();
637 break;
638 }
639 }
640 }
641
Jianing Wei90e59c92014-03-12 18:29:36 -0700642 return OK;
643}
644
Jianing Weicb0652e2014-03-12 18:29:36 -0700645status_t Camera3Device::capture(CameraMetadata &request, 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 captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800651}
652
Jianing Wei90e59c92014-03-12 18:29:36 -0700653status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700654 const List<const CameraMetadata> &requests, bool repeating,
655 /*out*/
656 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700657 ATRACE_CALL();
658 Mutex::Autolock il(mInterfaceLock);
659 Mutex::Autolock l(mLock);
660
661 status_t res = checkStatusOkToCaptureLocked();
662 if (res != OK) {
663 // error logged by previous call
664 return res;
665 }
666
667 RequestList requestList;
668
669 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
670 if (res != OK) {
671 // error logged by previous call
672 return res;
673 }
674
675 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700676 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700677 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700678 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700679 }
680
681 if (res == OK) {
682 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
683 if (res != OK) {
684 SET_ERR_L("Can't transition to active in %f seconds!",
685 kActiveTimeout/1e9);
686 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700687 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
688 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700689 } else {
690 CLOGE("Cannot queue request. Impossible.");
691 return BAD_VALUE;
692 }
693
694 return res;
695}
696
Jianing Weicb0652e2014-03-12 18:29:36 -0700697status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
698 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700699 ATRACE_CALL();
700
Jianing Weicb0652e2014-03-12 18:29:36 -0700701 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700702}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800703
Jianing Weicb0652e2014-03-12 18:29:36 -0700704status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
705 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800706 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800707
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700708 List<const CameraMetadata> requests;
709 requests.push_back(request);
710 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800711}
712
Jianing Weicb0652e2014-03-12 18:29:36 -0700713status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
714 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700715 ATRACE_CALL();
716
Jianing Weicb0652e2014-03-12 18:29:36 -0700717 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700718}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800719
720sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
721 const CameraMetadata &request) {
722 status_t res;
723
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700724 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800725 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700726 // Stream configuration failed due to unsupported configuration.
727 // Device back to unconfigured state. Client might try other configuraitons
728 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
729 CLOGE("No streams configured");
730 return NULL;
731 }
732 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800733 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700734 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800735 return NULL;
736 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700737 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700738 if (mStatus == STATUS_UNCONFIGURED) {
739 CLOGE("No streams configured");
740 return NULL;
741 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800742 }
743
744 sp<CaptureRequest> newRequest = createCaptureRequest(request);
745 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800746}
747
Jianing Weicb0652e2014-03-12 18:29:36 -0700748status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800749 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700750 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800751 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800752
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800753 switch (mStatus) {
754 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700755 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800756 return INVALID_OPERATION;
757 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700758 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800759 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700760 case STATUS_UNCONFIGURED:
761 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800762 case STATUS_ACTIVE:
763 // OK
764 break;
765 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700766 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800767 return INVALID_OPERATION;
768 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700769 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700770
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700771 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800772}
773
774status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
775 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700776 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800777
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700778 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800779}
780
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700781status_t Camera3Device::createInputStream(
782 uint32_t width, uint32_t height, int format, int *id) {
783 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700784 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700785 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700786 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
787 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700788
789 status_t res;
790 bool wasActive = false;
791
792 switch (mStatus) {
793 case STATUS_ERROR:
794 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
795 return INVALID_OPERATION;
796 case STATUS_UNINITIALIZED:
797 ALOGE("%s: Device not initialized", __FUNCTION__);
798 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700799 case STATUS_UNCONFIGURED:
800 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700801 // OK
802 break;
803 case STATUS_ACTIVE:
804 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700805 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700806 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700807 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700808 return res;
809 }
810 wasActive = true;
811 break;
812 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700813 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700814 return INVALID_OPERATION;
815 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700816 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700817
818 if (mInputStream != 0) {
819 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
820 return INVALID_OPERATION;
821 }
822
823 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
824 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700825 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700826
827 mInputStream = newStream;
828
829 *id = mNextStreamId++;
830
831 // Continue captures if active at start
832 if (wasActive) {
833 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
834 res = configureStreamsLocked();
835 if (res != OK) {
836 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
837 __FUNCTION__, mNextStreamId, strerror(-res), res);
838 return res;
839 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700840 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700841 }
842
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700843 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700844 return OK;
845}
846
Igor Murashkin2fba5842013-04-22 14:03:54 -0700847
848status_t Camera3Device::createZslStream(
849 uint32_t width, uint32_t height,
850 int depth,
851 /*out*/
852 int *id,
853 sp<Camera3ZslStream>* zslStream) {
854 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700855 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700856 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700857 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
858 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700859
860 status_t res;
861 bool wasActive = false;
862
863 switch (mStatus) {
864 case STATUS_ERROR:
865 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
866 return INVALID_OPERATION;
867 case STATUS_UNINITIALIZED:
868 ALOGE("%s: Device not initialized", __FUNCTION__);
869 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700870 case STATUS_UNCONFIGURED:
871 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700872 // OK
873 break;
874 case STATUS_ACTIVE:
875 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700876 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700877 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700878 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700879 return res;
880 }
881 wasActive = true;
882 break;
883 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700884 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700885 return INVALID_OPERATION;
886 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700887 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700888
889 if (mInputStream != 0) {
890 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
891 return INVALID_OPERATION;
892 }
893
894 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
895 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700896 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700897
898 res = mOutputStreams.add(mNextStreamId, newStream);
899 if (res < 0) {
900 ALOGE("%s: Can't add new stream to set: %s (%d)",
901 __FUNCTION__, strerror(-res), res);
902 return res;
903 }
904 mInputStream = newStream;
905
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530906 mNeedConfig = true;
907
Igor Murashkin2fba5842013-04-22 14:03:54 -0700908 *id = mNextStreamId++;
909 *zslStream = newStream;
910
911 // Continue captures if active at start
912 if (wasActive) {
913 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
914 res = configureStreamsLocked();
915 if (res != OK) {
916 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
917 __FUNCTION__, mNextStreamId, strerror(-res), res);
918 return res;
919 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700920 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700921 }
922
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700923 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700924 return OK;
925}
926
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700927status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800928 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700929 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800930 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700931 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800932 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700933 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
934 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800935
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800936 status_t res;
937 bool wasActive = false;
938
939 switch (mStatus) {
940 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700941 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800942 return INVALID_OPERATION;
943 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700944 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800945 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700946 case STATUS_UNCONFIGURED:
947 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800948 // OK
949 break;
950 case STATUS_ACTIVE:
951 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700952 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800953 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700954 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800955 return res;
956 }
957 wasActive = true;
958 break;
959 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700960 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800961 return INVALID_OPERATION;
962 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700963 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800964
965 sp<Camera3OutputStream> newStream;
966 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700967 ssize_t blobBufferSize;
968 if (dataSpace != HAL_DATASPACE_DEPTH) {
969 blobBufferSize = getJpegBufferSize(width, height);
970 if (blobBufferSize <= 0) {
971 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
972 return BAD_VALUE;
973 }
974 } else {
975 blobBufferSize = getPointCloudBufferSize();
976 if (blobBufferSize <= 0) {
977 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
978 return BAD_VALUE;
979 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700980 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800981 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700982 width, height, blobBufferSize, format, dataSpace, rotation);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800983 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
984 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
985 if (rawOpaqueBufferSize <= 0) {
986 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
987 return BAD_VALUE;
988 }
989 newStream = new Camera3OutputStream(mNextStreamId, consumer,
990 width, height, rawOpaqueBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800991 } else {
992 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700993 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800994 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700995 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800996
997 res = mOutputStreams.add(mNextStreamId, newStream);
998 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700999 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001000 return res;
1001 }
1002
1003 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001004 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001005
1006 // Continue captures if active at start
1007 if (wasActive) {
1008 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1009 res = configureStreamsLocked();
1010 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001011 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1012 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001013 return res;
1014 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001015 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001016 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001017 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001018 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001019}
1020
1021status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1022 ATRACE_CALL();
1023 (void)outputId; (void)id;
1024
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001025 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001026 return INVALID_OPERATION;
1027}
1028
1029
1030status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001031 uint32_t *width, uint32_t *height,
1032 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001033 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);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001036
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001037 switch (mStatus) {
1038 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001039 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001040 return INVALID_OPERATION;
1041 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001042 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001043 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001044 case STATUS_UNCONFIGURED:
1045 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001046 case STATUS_ACTIVE:
1047 // OK
1048 break;
1049 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001050 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001051 return INVALID_OPERATION;
1052 }
1053
1054 ssize_t idx = mOutputStreams.indexOfKey(id);
1055 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001056 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001057 return idx;
1058 }
1059
1060 if (width) *width = mOutputStreams[idx]->getWidth();
1061 if (height) *height = mOutputStreams[idx]->getHeight();
1062 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001063 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001064 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001065}
1066
1067status_t Camera3Device::setStreamTransform(int id,
1068 int transform) {
1069 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001070 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001072
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001073 switch (mStatus) {
1074 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001075 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001076 return INVALID_OPERATION;
1077 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001078 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001079 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001080 case STATUS_UNCONFIGURED:
1081 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001082 case STATUS_ACTIVE:
1083 // OK
1084 break;
1085 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001086 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001087 return INVALID_OPERATION;
1088 }
1089
1090 ssize_t idx = mOutputStreams.indexOfKey(id);
1091 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001092 CLOGE("Stream %d does not exist",
1093 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001094 return BAD_VALUE;
1095 }
1096
1097 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001098}
1099
1100status_t Camera3Device::deleteStream(int id) {
1101 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001102 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001103 Mutex::Autolock l(mLock);
1104 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001105
Igor Murashkine2172be2013-05-28 15:31:39 -07001106 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1107
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001108 // CameraDevice semantics require device to already be idle before
1109 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001110 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001111 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1112 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001113 }
1114
Igor Murashkin2fba5842013-04-22 14:03:54 -07001115 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001116 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001117 if (mInputStream != NULL && id == mInputStream->getId()) {
1118 deletedStream = mInputStream;
1119 mInputStream.clear();
1120 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001121 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001122 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001123 return BAD_VALUE;
1124 }
Zhijun He5f446352014-01-22 09:49:33 -08001125 }
1126
1127 // Delete output stream or the output part of a bi-directional stream.
1128 if (outputStreamIdx != NAME_NOT_FOUND) {
1129 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001130 mOutputStreams.removeItem(id);
1131 }
1132
1133 // Free up the stream endpoint so that it can be used by some other stream
1134 res = deletedStream->disconnect();
1135 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001136 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001137 // fall through since we want to still list the stream as deleted.
1138 }
1139 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001140 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141
1142 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001143}
1144
1145status_t Camera3Device::deleteReprocessStream(int id) {
1146 ATRACE_CALL();
1147 (void)id;
1148
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001149 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001150 return INVALID_OPERATION;
1151}
1152
Zhijun He1fa89992015-06-01 15:44:31 -07001153status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001154 ATRACE_CALL();
1155 ALOGV("%s: E", __FUNCTION__);
1156
1157 Mutex::Autolock il(mInterfaceLock);
1158 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001159
1160 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1161 mNeedConfig = true;
1162 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1163 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001164
1165 return configureStreamsLocked();
1166}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001167
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001168status_t Camera3Device::getInputBufferProducer(
1169 sp<IGraphicBufferProducer> *producer) {
1170 Mutex::Autolock il(mInterfaceLock);
1171 Mutex::Autolock l(mLock);
1172
1173 if (producer == NULL) {
1174 return BAD_VALUE;
1175 } else if (mInputStream == NULL) {
1176 return INVALID_OPERATION;
1177 }
1178
1179 return mInputStream->getInputBufferProducer(producer);
1180}
1181
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001182status_t Camera3Device::createDefaultRequest(int templateId,
1183 CameraMetadata *request) {
1184 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001185 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001186 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001187 Mutex::Autolock l(mLock);
1188
1189 switch (mStatus) {
1190 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001191 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001192 return INVALID_OPERATION;
1193 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001194 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001195 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001196 case STATUS_UNCONFIGURED:
1197 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001198 case STATUS_ACTIVE:
1199 // OK
1200 break;
1201 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001202 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001203 return INVALID_OPERATION;
1204 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001205
Zhijun Hea1530f12014-09-14 12:44:20 -07001206 if (!mRequestTemplateCache[templateId].isEmpty()) {
1207 *request = mRequestTemplateCache[templateId];
1208 return OK;
1209 }
1210
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001211 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001212 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001213 rawRequest = mHal3Device->ops->construct_default_request_settings(
1214 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001215 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001216 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001217 ALOGI("%s: template %d is not supported on this camera device",
1218 __FUNCTION__, templateId);
1219 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001220 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001221 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001222 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001223
1224 return OK;
1225}
1226
1227status_t Camera3Device::waitUntilDrained() {
1228 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001229 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001230 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001231
Zhijun He69a37482014-03-23 18:44:49 -07001232 return waitUntilDrainedLocked();
1233}
1234
1235status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001236 switch (mStatus) {
1237 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001238 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001239 ALOGV("%s: Already idle", __FUNCTION__);
1240 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001241 case STATUS_CONFIGURED:
1242 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001243 case STATUS_ERROR:
1244 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001245 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001246 break;
1247 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001248 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001249 return INVALID_OPERATION;
1250 }
1251
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001252 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1253 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001254 if (res != OK) {
1255 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1256 res);
1257 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001258 return res;
1259}
1260
Ruben Brunk183f0562015-08-12 12:55:02 -07001261
1262void Camera3Device::internalUpdateStatusLocked(Status status) {
1263 mStatus = status;
1264 mRecentStatusUpdates.add(mStatus);
1265 mStatusChanged.broadcast();
1266}
1267
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001268// Pause to reconfigure
1269status_t Camera3Device::internalPauseAndWaitLocked() {
1270 mRequestThread->setPaused(true);
1271 mPauseStateNotify = true;
1272
1273 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1274 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1275 if (res != OK) {
1276 SET_ERR_L("Can't idle device in %f seconds!",
1277 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001278 }
1279
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001280 return res;
1281}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001282
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001283// Resume after internalPauseAndWaitLocked
1284status_t Camera3Device::internalResumeLocked() {
1285 status_t res;
1286
1287 mRequestThread->setPaused(false);
1288
1289 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1290 if (res != OK) {
1291 SET_ERR_L("Can't transition to active in %f seconds!",
1292 kActiveTimeout/1e9);
1293 }
1294 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001295 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001296}
1297
Ruben Brunk183f0562015-08-12 12:55:02 -07001298status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001299 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001300
1301 size_t startIndex = 0;
1302 if (mStatusWaiters == 0) {
1303 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1304 // this status list
1305 mRecentStatusUpdates.clear();
1306 } else {
1307 // If other threads are waiting on updates to this status list, set the position of the
1308 // first element that this list will check rather than clearing the list.
1309 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001310 }
1311
Ruben Brunk183f0562015-08-12 12:55:02 -07001312 mStatusWaiters++;
1313
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001314 bool stateSeen = false;
1315 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001316 if (active == (mStatus == STATUS_ACTIVE)) {
1317 // Desired state is current
1318 break;
1319 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001320
1321 res = mStatusChanged.waitRelative(mLock, timeout);
1322 if (res != OK) break;
1323
Ruben Brunk183f0562015-08-12 12:55:02 -07001324 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1325 // transitions.
1326 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1327 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1328 __FUNCTION__);
1329
1330 // Encountered desired state since we began waiting
1331 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001332 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1333 stateSeen = true;
1334 break;
1335 }
1336 }
1337 } while (!stateSeen);
1338
Ruben Brunk183f0562015-08-12 12:55:02 -07001339 mStatusWaiters--;
1340
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001341 return res;
1342}
1343
1344
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001345status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1346 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001347 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001348
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001349 if (listener != NULL && mListener != NULL) {
1350 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1351 }
1352 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001353 mRequestThread->setNotificationListener(listener);
1354 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001355
1356 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001357}
1358
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001359bool Camera3Device::willNotify3A() {
1360 return false;
1361}
1362
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001363status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001364 status_t res;
1365 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001366
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001367 while (mResultQueue.empty()) {
1368 res = mResultSignal.waitRelative(mOutputLock, timeout);
1369 if (res == TIMED_OUT) {
1370 return res;
1371 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001372 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001373 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001374 return res;
1375 }
1376 }
1377 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001378}
1379
Jianing Weicb0652e2014-03-12 18:29:36 -07001380status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001381 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001382 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001383
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001384 if (mResultQueue.empty()) {
1385 return NOT_ENOUGH_DATA;
1386 }
1387
Jianing Weicb0652e2014-03-12 18:29:36 -07001388 if (frame == NULL) {
1389 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1390 return BAD_VALUE;
1391 }
1392
1393 CaptureResult &result = *(mResultQueue.begin());
1394 frame->mResultExtras = result.mResultExtras;
1395 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001396 mResultQueue.erase(mResultQueue.begin());
1397
1398 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001399}
1400
1401status_t Camera3Device::triggerAutofocus(uint32_t id) {
1402 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001403 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001404
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001405 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1406 // Mix-in this trigger into the next request and only the next request.
1407 RequestTrigger trigger[] = {
1408 {
1409 ANDROID_CONTROL_AF_TRIGGER,
1410 ANDROID_CONTROL_AF_TRIGGER_START
1411 },
1412 {
1413 ANDROID_CONTROL_AF_TRIGGER_ID,
1414 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001415 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001416 };
1417
1418 return mRequestThread->queueTrigger(trigger,
1419 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001420}
1421
1422status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1423 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001424 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001425
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001426 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1427 // Mix-in this trigger into the next request and only the next request.
1428 RequestTrigger trigger[] = {
1429 {
1430 ANDROID_CONTROL_AF_TRIGGER,
1431 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1432 },
1433 {
1434 ANDROID_CONTROL_AF_TRIGGER_ID,
1435 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001436 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001437 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001438
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001439 return mRequestThread->queueTrigger(trigger,
1440 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001441}
1442
1443status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1444 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001445 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001446
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001447 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1448 // Mix-in this trigger into the next request and only the next request.
1449 RequestTrigger trigger[] = {
1450 {
1451 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1452 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1453 },
1454 {
1455 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1456 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001457 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001458 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001459
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001460 return mRequestThread->queueTrigger(trigger,
1461 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001462}
1463
1464status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1465 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1466 ATRACE_CALL();
1467 (void)reprocessStreamId; (void)buffer; (void)listener;
1468
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001469 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001470 return INVALID_OPERATION;
1471}
1472
Jianing Weicb0652e2014-03-12 18:29:36 -07001473status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001474 ATRACE_CALL();
1475 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001476 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001477
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001478 NotificationListener* listener;
1479 {
1480 Mutex::Autolock l(mOutputLock);
1481 listener = mListener;
1482 }
1483
Zhijun He7ef20392014-04-21 16:04:17 -07001484 {
1485 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001486 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001487 }
1488
Zhijun He491e3412013-12-27 10:57:44 -08001489 status_t res;
1490 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001491 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001492 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001493 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001494 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001495 }
1496
1497 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001498}
1499
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001500status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001501 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1502}
1503
1504status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001505 ATRACE_CALL();
1506 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001507 Mutex::Autolock il(mInterfaceLock);
1508 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001509
1510 sp<Camera3StreamInterface> stream;
1511 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1512 if (outputStreamIdx == NAME_NOT_FOUND) {
1513 CLOGE("Stream %d does not exist", streamId);
1514 return BAD_VALUE;
1515 }
1516
1517 stream = mOutputStreams.editValueAt(outputStreamIdx);
1518
1519 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001520 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001521 return BAD_VALUE;
1522 }
1523
1524 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001525 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001526 return BAD_VALUE;
1527 }
1528
Ruben Brunkc78ac262015-08-13 17:58:46 -07001529 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001530}
1531
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001532status_t Camera3Device::tearDown(int streamId) {
1533 ATRACE_CALL();
1534 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1535 Mutex::Autolock il(mInterfaceLock);
1536 Mutex::Autolock l(mLock);
1537
1538 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1539 // since we cannot call register_stream_buffers except right after configure_streams.
1540 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1541 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1542 __FUNCTION__, mHal3Device->common.version);
1543 return NO_INIT;
1544 }
1545
1546 sp<Camera3StreamInterface> stream;
1547 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1548 if (outputStreamIdx == NAME_NOT_FOUND) {
1549 CLOGE("Stream %d does not exist", streamId);
1550 return BAD_VALUE;
1551 }
1552
1553 stream = mOutputStreams.editValueAt(outputStreamIdx);
1554
1555 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1556 CLOGE("Stream %d is a target of a in-progress request", streamId);
1557 return BAD_VALUE;
1558 }
1559
1560 return stream->tearDown();
1561}
1562
Zhijun He204e3292014-07-14 17:09:23 -07001563uint32_t Camera3Device::getDeviceVersion() {
1564 ATRACE_CALL();
1565 Mutex::Autolock il(mInterfaceLock);
1566 return mDeviceVersion;
1567}
1568
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001569/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001570 * Methods called by subclasses
1571 */
1572
1573void Camera3Device::notifyStatus(bool idle) {
1574 {
1575 // Need mLock to safely update state and synchronize to current
1576 // state of methods in flight.
1577 Mutex::Autolock l(mLock);
1578 // We can get various system-idle notices from the status tracker
1579 // while starting up. Only care about them if we've actually sent
1580 // in some requests recently.
1581 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1582 return;
1583 }
1584 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1585 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001586 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001587
1588 // Skip notifying listener if we're doing some user-transparent
1589 // state changes
1590 if (mPauseStateNotify) return;
1591 }
1592 NotificationListener *listener;
1593 {
1594 Mutex::Autolock l(mOutputLock);
1595 listener = mListener;
1596 }
1597 if (idle && listener != NULL) {
1598 listener->notifyIdle();
1599 }
1600}
1601
1602/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001603 * Camera3Device private methods
1604 */
1605
1606sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1607 const CameraMetadata &request) {
1608 ATRACE_CALL();
1609 status_t res;
1610
1611 sp<CaptureRequest> newRequest = new CaptureRequest;
1612 newRequest->mSettings = request;
1613
1614 camera_metadata_entry_t inputStreams =
1615 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1616 if (inputStreams.count > 0) {
1617 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001618 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001619 CLOGE("Request references unknown input stream %d",
1620 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001621 return NULL;
1622 }
1623 // Lazy completion of stream configuration (allocation/registration)
1624 // on first use
1625 if (mInputStream->isConfiguring()) {
1626 res = mInputStream->finishConfiguration(mHal3Device);
1627 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001628 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001629 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001630 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001631 return NULL;
1632 }
1633 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001634 // Check if stream is being prepared
1635 if (mInputStream->isPreparing()) {
1636 CLOGE("Request references an input stream that's being prepared!");
1637 return NULL;
1638 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001639
1640 newRequest->mInputStream = mInputStream;
1641 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1642 }
1643
1644 camera_metadata_entry_t streams =
1645 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1646 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001647 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001648 return NULL;
1649 }
1650
1651 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001652 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001653 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001654 CLOGE("Request references unknown stream %d",
1655 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001656 return NULL;
1657 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001658 sp<Camera3OutputStreamInterface> stream =
1659 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001660
1661 // Lazy completion of stream configuration (allocation/registration)
1662 // on first use
1663 if (stream->isConfiguring()) {
1664 res = stream->finishConfiguration(mHal3Device);
1665 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001666 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1667 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001668 return NULL;
1669 }
1670 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001671 // Check if stream is being prepared
1672 if (stream->isPreparing()) {
1673 CLOGE("Request references an output stream that's being prepared!");
1674 return NULL;
1675 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001676
1677 newRequest->mOutputStreams.push(stream);
1678 }
1679 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001680 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001681
1682 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001683}
1684
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001685bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1686 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1687 Size size = mSupportedOpaqueInputSizes[i];
1688 if (size.width == width && size.height == height) {
1689 return true;
1690 }
1691 }
1692
1693 return false;
1694}
1695
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001696status_t Camera3Device::configureStreamsLocked() {
1697 ATRACE_CALL();
1698 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001699
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001700 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001701 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001702 return INVALID_OPERATION;
1703 }
1704
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001705 if (!mNeedConfig) {
1706 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1707 return OK;
1708 }
1709
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001710 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1711 // adding a dummy stream instead.
1712 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1713 if (mOutputStreams.size() == 0) {
1714 addDummyStreamLocked();
1715 } else {
1716 tryRemoveDummyStreamLocked();
1717 }
1718
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001719 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001720 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001721
1722 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001723 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1724 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1725 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001726 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1727
1728 Vector<camera3_stream_t*> streams;
1729 streams.setCapacity(config.num_streams);
1730
1731 if (mInputStream != NULL) {
1732 camera3_stream_t *inputStream;
1733 inputStream = mInputStream->startConfiguration();
1734 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001735 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001736 return INVALID_OPERATION;
1737 }
1738 streams.add(inputStream);
1739 }
1740
1741 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001742
1743 // Don't configure bidi streams twice, nor add them twice to the list
1744 if (mOutputStreams[i].get() ==
1745 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1746
1747 config.num_streams--;
1748 continue;
1749 }
1750
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001751 camera3_stream_t *outputStream;
1752 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1753 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001754 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001755 return INVALID_OPERATION;
1756 }
1757 streams.add(outputStream);
1758 }
1759
1760 config.streams = streams.editArray();
1761
1762 // Do the HAL configuration; will potentially touch stream
1763 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001764 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001765 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001766 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001767
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001768 if (res == BAD_VALUE) {
1769 // HAL rejected this set of streams as unsupported, clean up config
1770 // attempt and return to unconfigured state
1771 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1772 res = mInputStream->cancelConfiguration();
1773 if (res != OK) {
1774 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1775 mInputStream->getId(), strerror(-res), res);
1776 return res;
1777 }
1778 }
1779
1780 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1781 sp<Camera3OutputStreamInterface> outputStream =
1782 mOutputStreams.editValueAt(i);
1783 if (outputStream->isConfiguring()) {
1784 res = outputStream->cancelConfiguration();
1785 if (res != OK) {
1786 SET_ERR_L(
1787 "Can't cancel configuring output stream %d: %s (%d)",
1788 outputStream->getId(), strerror(-res), res);
1789 return res;
1790 }
1791 }
1792 }
1793
1794 // Return state to that at start of call, so that future configures
1795 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001796 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001797 mNeedConfig = true;
1798
1799 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1800 return BAD_VALUE;
1801 } else if (res != OK) {
1802 // Some other kind of error from configure_streams - this is not
1803 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001804 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1805 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001806 return res;
1807 }
1808
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001809 // Finish all stream configuration immediately.
1810 // TODO: Try to relax this later back to lazy completion, which should be
1811 // faster
1812
Igor Murashkin073f8572013-05-02 14:59:28 -07001813 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001814 res = mInputStream->finishConfiguration(mHal3Device);
1815 if (res != OK) {
1816 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1817 mInputStream->getId(), strerror(-res), res);
1818 return res;
1819 }
1820 }
1821
1822 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001823 sp<Camera3OutputStreamInterface> outputStream =
1824 mOutputStreams.editValueAt(i);
1825 if (outputStream->isConfiguring()) {
1826 res = outputStream->finishConfiguration(mHal3Device);
1827 if (res != OK) {
1828 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1829 outputStream->getId(), strerror(-res), res);
1830 return res;
1831 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001832 }
1833 }
1834
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001835 // Request thread needs to know to avoid using repeat-last-settings protocol
1836 // across configure_streams() calls
1837 mRequestThread->configurationComplete();
1838
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07001839 // Boost priority of request thread for high speed recording to SCHED_FIFO
1840 if (mIsConstrainedHighSpeedConfiguration) {
1841 pid_t requestThreadTid = mRequestThread->getTid();
1842 res = requestPriority(getpid(), requestThreadTid,
1843 kConstrainedHighSpeedThreadPriority, true);
1844 if (res != OK) {
1845 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
1846 strerror(-res), res);
1847 } else {
1848 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
1849 }
1850 } else {
1851 // TODO: Set/restore normal priority for normal use cases
1852 }
1853
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001854 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001855
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001856 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001857
Ruben Brunk183f0562015-08-12 12:55:02 -07001858 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1859 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001860
1861 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1862
Zhijun He0a210512014-07-24 13:45:15 -07001863 // tear down the deleted streams after configure streams.
1864 mDeletedStreams.clear();
1865
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001866 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001867}
1868
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001869status_t Camera3Device::addDummyStreamLocked() {
1870 ATRACE_CALL();
1871 status_t res;
1872
1873 if (mDummyStreamId != NO_STREAM) {
1874 // Should never be adding a second dummy stream when one is already
1875 // active
1876 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1877 __FUNCTION__, mId);
1878 return INVALID_OPERATION;
1879 }
1880
1881 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1882
1883 sp<Camera3OutputStreamInterface> dummyStream =
1884 new Camera3DummyStream(mNextStreamId);
1885
1886 res = mOutputStreams.add(mNextStreamId, dummyStream);
1887 if (res < 0) {
1888 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1889 return res;
1890 }
1891
1892 mDummyStreamId = mNextStreamId;
1893 mNextStreamId++;
1894
1895 return OK;
1896}
1897
1898status_t Camera3Device::tryRemoveDummyStreamLocked() {
1899 ATRACE_CALL();
1900 status_t res;
1901
1902 if (mDummyStreamId == NO_STREAM) return OK;
1903 if (mOutputStreams.size() == 1) return OK;
1904
1905 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1906
1907 // Ok, have a dummy stream and there's at least one other output stream,
1908 // so remove the dummy
1909
1910 sp<Camera3StreamInterface> deletedStream;
1911 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1912 if (outputStreamIdx == NAME_NOT_FOUND) {
1913 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1914 return INVALID_OPERATION;
1915 }
1916
1917 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1918 mOutputStreams.removeItemsAt(outputStreamIdx);
1919
1920 // Free up the stream endpoint so that it can be used by some other stream
1921 res = deletedStream->disconnect();
1922 if (res != OK) {
1923 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1924 // fall through since we want to still list the stream as deleted.
1925 }
1926 mDeletedStreams.add(deletedStream);
1927 mDummyStreamId = NO_STREAM;
1928
1929 return res;
1930}
1931
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001932void Camera3Device::setErrorState(const char *fmt, ...) {
1933 Mutex::Autolock l(mLock);
1934 va_list args;
1935 va_start(args, fmt);
1936
1937 setErrorStateLockedV(fmt, args);
1938
1939 va_end(args);
1940}
1941
1942void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1943 Mutex::Autolock l(mLock);
1944 setErrorStateLockedV(fmt, args);
1945}
1946
1947void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1948 va_list args;
1949 va_start(args, fmt);
1950
1951 setErrorStateLockedV(fmt, args);
1952
1953 va_end(args);
1954}
1955
1956void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001957 // Print out all error messages to log
1958 String8 errorCause = String8::formatV(fmt, args);
1959 ALOGE("Camera %d: %s", mId, errorCause.string());
1960
1961 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001962 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001963
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001964 mErrorCause = errorCause;
1965
1966 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07001967 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001968
1969 // Notify upstream about a device error
1970 if (mListener != NULL) {
1971 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1972 CaptureResultExtras());
1973 }
1974
1975 // Save stack trace. View by dumping it later.
1976 CameraTraces::saveTrace();
1977 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001978}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001979
1980/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001981 * In-flight request management
1982 */
1983
Jianing Weicb0652e2014-03-12 18:29:36 -07001984status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07001985 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1986 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001987 ATRACE_CALL();
1988 Mutex::Autolock l(mInFlightLock);
1989
1990 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07001991 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
1992 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001993 if (res < 0) return res;
1994
1995 return OK;
1996}
1997
1998/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001999 * Check if all 3A fields are ready, and send off a partial 3A-only result
2000 * to the output frame queue
2001 */
Zhijun He204e3292014-07-14 17:09:23 -07002002bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07002003 uint32_t frameNumber,
2004 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002005
2006 // Check if all 3A states are present
2007 // The full list of fields is
2008 // android.control.afMode
2009 // android.control.awbMode
2010 // android.control.aeState
2011 // android.control.awbState
2012 // android.control.afState
2013 // android.control.afTriggerID
2014 // android.control.aePrecaptureID
2015 // TODO: Add android.control.aeMode
2016
2017 bool gotAllStates = true;
2018
2019 uint8_t afMode;
2020 uint8_t awbMode;
2021 uint8_t aeState;
2022 uint8_t afState;
2023 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002024
2025 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
2026 &afMode, frameNumber);
2027
2028 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
2029 &awbMode, frameNumber);
2030
2031 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
2032 &aeState, frameNumber);
2033
2034 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
2035 &afState, frameNumber);
2036
2037 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
2038 &awbState, frameNumber);
2039
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002040 if (!gotAllStates) return false;
2041
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002042 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002043 "AF state %d, AE state %d, AWB state %d, "
2044 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002045 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002046 afMode, awbMode,
2047 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002048 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002049
2050 // Got all states, so construct a minimal result to send
2051 // In addition to the above fields, this means adding in
2052 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002053 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07002054 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002055
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002056 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002057
2058 Mutex::Autolock l(mOutputLock);
2059
Jianing Weicb0652e2014-03-12 18:29:36 -07002060 CaptureResult captureResult;
2061 captureResult.mResultExtras = resultExtras;
2062 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
2063 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
2064 // but not limited to CameraDeviceBase::getNextResult
2065 CaptureResult& min3AResult =
2066 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002067
Jianing Weicb0652e2014-03-12 18:29:36 -07002068 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
2069 // TODO: This is problematic casting. Need to fix CameraMetadata.
2070 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002071 return false;
2072 }
2073
Jianing Weicb0652e2014-03-12 18:29:36 -07002074 int32_t requestId = resultExtras.requestId;
2075 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002076 &requestId, frameNumber)) {
2077 return false;
2078 }
2079
Zhijun He204e3292014-07-14 17:09:23 -07002080 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
2081 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
2082 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
2083 &partialResult, frameNumber)) {
2084 return false;
2085 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002086 }
2087
Jianing Weicb0652e2014-03-12 18:29:36 -07002088 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002089 &afMode, frameNumber)) {
2090 return false;
2091 }
2092
Jianing Weicb0652e2014-03-12 18:29:36 -07002093 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002094 &awbMode, frameNumber)) {
2095 return false;
2096 }
2097
Jianing Weicb0652e2014-03-12 18:29:36 -07002098 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002099 &aeState, frameNumber)) {
2100 return false;
2101 }
2102
Jianing Weicb0652e2014-03-12 18:29:36 -07002103 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002104 &afState, frameNumber)) {
2105 return false;
2106 }
2107
Jianing Weicb0652e2014-03-12 18:29:36 -07002108 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002109 &awbState, frameNumber)) {
2110 return false;
2111 }
2112
Jianing Weicb0652e2014-03-12 18:29:36 -07002113 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002114 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002115 return false;
2116 }
2117
Jianing Weicb0652e2014-03-12 18:29:36 -07002118 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002119 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002120 return false;
2121 }
2122
Zhijun He204e3292014-07-14 17:09:23 -07002123 // We only send the aggregated partial when all 3A related metadata are available
2124 // For both API1 and API2.
2125 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002126 mResultSignal.signal();
2127
2128 return true;
2129}
2130
2131template<typename T>
2132bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002133 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002134 (void) frameNumber;
2135
2136 camera_metadata_ro_entry_t entry;
2137
2138 entry = result.find(tag);
2139 if (entry.count == 0) {
2140 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2141 mId, frameNumber, get_camera_metadata_tag_name(tag));
2142 return false;
2143 }
2144
2145 if (sizeof(T) == sizeof(uint8_t)) {
2146 *value = entry.data.u8[0];
2147 } else if (sizeof(T) == sizeof(int32_t)) {
2148 *value = entry.data.i32[0];
2149 } else {
2150 ALOGE("%s: Unexpected type", __FUNCTION__);
2151 return false;
2152 }
2153 return true;
2154}
2155
2156template<typename T>
2157bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002158 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002159 if (result.update(tag, value, 1) != NO_ERROR) {
2160 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2161 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2162 frameNumber, get_camera_metadata_tag_name(tag));
2163 return false;
2164 }
2165 return true;
2166}
2167
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002168void Camera3Device::returnOutputBuffers(
2169 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2170 nsecs_t timestamp) {
2171 for (size_t i = 0; i < numBuffers; i++)
2172 {
2173 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2174 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2175 // Note: stream may be deallocated at this point, if this buffer was
2176 // the last reference to it.
2177 if (res != OK) {
2178 ALOGE("Can't return buffer to its stream: %s (%d)",
2179 strerror(-res), res);
2180 }
2181 }
2182}
2183
2184
2185void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2186
2187 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2188 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2189
2190 nsecs_t sensorTimestamp = request.sensorTimestamp;
2191 nsecs_t shutterTimestamp = request.shutterTimestamp;
2192
2193 // Check if it's okay to remove the request from InFlightMap:
2194 // In the case of a successful request:
2195 // all input and output buffers, all result metadata, shutter callback
2196 // arrived.
2197 // In the case of a unsuccessful request:
2198 // all input and output buffers arrived.
2199 if (request.numBuffersLeft == 0 &&
2200 (request.requestStatus != OK ||
2201 (request.haveResultMetadata && shutterTimestamp != 0))) {
2202 ATRACE_ASYNC_END("frame capture", frameNumber);
2203
2204 // Sanity check - if sensor timestamp matches shutter timestamp
2205 if (request.requestStatus == OK &&
2206 sensorTimestamp != shutterTimestamp) {
2207 SET_ERR("sensor timestamp (%" PRId64
2208 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2209 sensorTimestamp, frameNumber, shutterTimestamp);
2210 }
2211
2212 // for an unsuccessful request, it may have pending output buffers to
2213 // return.
2214 assert(request.requestStatus != OK ||
2215 request.pendingOutputBuffers.size() == 0);
2216 returnOutputBuffers(request.pendingOutputBuffers.array(),
2217 request.pendingOutputBuffers.size(), 0);
2218
2219 mInFlightMap.removeItemsAt(idx, 1);
2220
2221 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2222 }
2223
2224 // Sanity check - if we have too many in-flight frames, something has
2225 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002226 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002227 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002228 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2229 kInFlightWarnLimitHighSpeed) {
2230 CLOGE("In-flight list too large for high speed configuration: %zu",
2231 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002232 }
2233}
2234
2235
2236void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2237 CaptureResultExtras &resultExtras,
2238 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002239 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002240 bool reprocess,
2241 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002242 if (pendingMetadata.isEmpty())
2243 return;
2244
2245 Mutex::Autolock l(mOutputLock);
2246
2247 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002248 if (reprocess) {
2249 if (frameNumber < mNextReprocessResultFrameNumber) {
2250 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002251 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002252 frameNumber, mNextReprocessResultFrameNumber);
2253 return;
2254 }
2255 mNextReprocessResultFrameNumber = frameNumber + 1;
2256 } else {
2257 if (frameNumber < mNextResultFrameNumber) {
2258 SET_ERR("Out-of-order capture result metadata submitted! "
2259 "(got frame number %d, expecting %d)",
2260 frameNumber, mNextResultFrameNumber);
2261 return;
2262 }
2263 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002264 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002265
2266 CaptureResult captureResult;
2267 captureResult.mResultExtras = resultExtras;
2268 captureResult.mMetadata = pendingMetadata;
2269
2270 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2271 (int32_t*)&frameNumber, 1) != OK) {
2272 SET_ERR("Failed to set frame# in metadata (%d)",
2273 frameNumber);
2274 return;
2275 } else {
2276 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2277 __FUNCTION__, mId, frameNumber);
2278 }
2279
2280 // Append any previous partials to form a complete result
2281 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2282 captureResult.mMetadata.append(collectedPartialResult);
2283 }
2284
2285 captureResult.mMetadata.sort();
2286
2287 // Check that there's a timestamp in the result metadata
2288 camera_metadata_entry entry =
2289 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2290 if (entry.count == 0) {
2291 SET_ERR("No timestamp provided by HAL for frame %d!",
2292 frameNumber);
2293 return;
2294 }
2295
Chien-Yu Chend196d612015-06-22 19:49:01 -07002296 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2297
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002298 // Valid result, insert into queue
2299 List<CaptureResult>::iterator queuedResult =
2300 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2301 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2302 ", burstId = %" PRId32, __FUNCTION__,
2303 queuedResult->mResultExtras.requestId,
2304 queuedResult->mResultExtras.frameNumber,
2305 queuedResult->mResultExtras.burstId);
2306
2307 mResultSignal.signal();
2308}
2309
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002310/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002311 * Camera HAL device callback methods
2312 */
2313
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002314void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002315 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002316
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002317 status_t res;
2318
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002319 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002320 if (result->result == NULL && result->num_output_buffers == 0 &&
2321 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002322 SET_ERR("No result data provided by HAL for frame %d",
2323 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002324 return;
2325 }
Zhijun He204e3292014-07-14 17:09:23 -07002326
2327 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2328 // partial_result to 1 when metadata is included in this result.
2329 if (!mUsePartialResult &&
2330 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2331 result->result != NULL &&
2332 result->partial_result != 1) {
2333 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2334 " if partial result is not supported",
2335 frameNumber, result->partial_result);
2336 return;
2337 }
2338
2339 bool isPartialResult = false;
2340 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002341 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002342 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002343
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002344 // Get shutter timestamp and resultExtras from list of in-flight requests,
2345 // where it was added by the shutter notification for this frame. If the
2346 // shutter timestamp isn't received yet, append the output buffers to the
2347 // in-flight request and they will be returned when the shutter timestamp
2348 // arrives. Update the in-flight status and remove the in-flight entry if
2349 // all result data and shutter timestamp have been received.
2350 nsecs_t shutterTimestamp = 0;
2351
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002352 {
2353 Mutex::Autolock l(mInFlightLock);
2354 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2355 if (idx == NAME_NOT_FOUND) {
2356 SET_ERR("Unknown frame number for capture result: %d",
2357 frameNumber);
2358 return;
2359 }
2360 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002361 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2362 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2363 ", partialResultCount = %d",
2364 __FUNCTION__, request.resultExtras.requestId,
2365 request.resultExtras.frameNumber, request.resultExtras.burstId,
2366 result->partial_result);
2367 // Always update the partial count to the latest one if it's not 0
2368 // (buffers only). When framework aggregates adjacent partial results
2369 // into one, the latest partial count will be used.
2370 if (result->partial_result != 0)
2371 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002372
2373 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002374 if (mUsePartialResult && result->result != NULL) {
2375 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2376 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2377 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2378 " the range of [1, %d] when metadata is included in the result",
2379 frameNumber, result->partial_result, mNumPartialResults);
2380 return;
2381 }
2382 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002383 if (isPartialResult) {
2384 request.partialResult.collectedResult.append(result->result);
2385 }
Zhijun He204e3292014-07-14 17:09:23 -07002386 } else {
2387 camera_metadata_ro_entry_t partialResultEntry;
2388 res = find_camera_metadata_ro_entry(result->result,
2389 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2390 if (res != NAME_NOT_FOUND &&
2391 partialResultEntry.count > 0 &&
2392 partialResultEntry.data.u8[0] ==
2393 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2394 // A partial result. Flag this as such, and collect this
2395 // set of metadata into the in-flight entry.
2396 isPartialResult = true;
2397 request.partialResult.collectedResult.append(
2398 result->result);
2399 request.partialResult.collectedResult.erase(
2400 ANDROID_QUIRKS_PARTIAL_RESULT);
2401 }
2402 }
2403
2404 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002405 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002406 if (!request.partialResult.haveSent3A) {
2407 request.partialResult.haveSent3A =
2408 processPartial3AResult(frameNumber,
2409 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002410 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002411 }
2412 }
2413 }
2414
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002415 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002416 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002417
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002418 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002419 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002420 if (request.haveResultMetadata) {
2421 SET_ERR("Called multiple times with metadata for frame %d",
2422 frameNumber);
2423 return;
2424 }
Zhijun He204e3292014-07-14 17:09:23 -07002425 if (mUsePartialResult &&
2426 !request.partialResult.collectedResult.isEmpty()) {
2427 collectedPartialResult.acquire(
2428 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002429 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002430 request.haveResultMetadata = true;
2431 }
2432
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002433 uint32_t numBuffersReturned = result->num_output_buffers;
2434 if (result->input_buffer != NULL) {
2435 if (hasInputBufferInRequest) {
2436 numBuffersReturned += 1;
2437 } else {
2438 ALOGW("%s: Input buffer should be NULL if there is no input"
2439 " buffer sent in the request",
2440 __FUNCTION__);
2441 }
2442 }
2443 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002444 if (request.numBuffersLeft < 0) {
2445 SET_ERR("Too many buffers returned for frame %d",
2446 frameNumber);
2447 return;
2448 }
2449
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002450 camera_metadata_ro_entry_t entry;
2451 res = find_camera_metadata_ro_entry(result->result,
2452 ANDROID_SENSOR_TIMESTAMP, &entry);
2453 if (res == OK && entry.count == 1) {
2454 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002455 }
2456
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002457 // If shutter event isn't received yet, append the output buffers to
2458 // the in-flight request. Otherwise, return the output buffers to
2459 // streams.
2460 if (shutterTimestamp == 0) {
2461 request.pendingOutputBuffers.appendArray(result->output_buffers,
2462 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002463 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002464 returnOutputBuffers(result->output_buffers,
2465 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002466 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002467
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002468 if (result->result != NULL && !isPartialResult) {
2469 if (shutterTimestamp == 0) {
2470 request.pendingMetadata = result->result;
2471 request.partialResult.collectedResult = collectedPartialResult;
2472 } else {
2473 CameraMetadata metadata;
2474 metadata = result->result;
2475 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002476 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2477 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002478 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002479 }
2480
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002481 removeInFlightRequestIfReadyLocked(idx);
2482 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002483
Zhijun Hef0d962a2014-06-30 10:24:11 -07002484 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002485 if (hasInputBufferInRequest) {
2486 Camera3Stream *stream =
2487 Camera3Stream::cast(result->input_buffer->stream);
2488 res = stream->returnInputBuffer(*(result->input_buffer));
2489 // Note: stream may be deallocated at this point, if this buffer was the
2490 // last reference to it.
2491 if (res != OK) {
2492 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2493 " its stream:%s (%d)", __FUNCTION__,
2494 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002495 }
2496 } else {
2497 ALOGW("%s: Input buffer should be NULL if there is no input"
2498 " buffer sent in the request, skipping input buffer return.",
2499 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002500 }
2501 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002502}
2503
2504void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002505 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002506 NotificationListener *listener;
2507 {
2508 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002509 listener = mListener;
2510 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002511
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002512 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002513 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002514 return;
2515 }
2516
2517 switch (msg->type) {
2518 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002519 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002520 break;
2521 }
2522 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002523 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002524 break;
2525 }
2526 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002527 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002528 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002529 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002530}
2531
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002532void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2533 NotificationListener *listener) {
2534
2535 // Map camera HAL error codes to ICameraDeviceCallback error codes
2536 // Index into this with the HAL error code
2537 static const ICameraDeviceCallbacks::CameraErrorCode
2538 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2539 // 0 = Unused error code
2540 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2541 // 1 = CAMERA3_MSG_ERROR_DEVICE
2542 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2543 // 2 = CAMERA3_MSG_ERROR_REQUEST
2544 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2545 // 3 = CAMERA3_MSG_ERROR_RESULT
2546 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2547 // 4 = CAMERA3_MSG_ERROR_BUFFER
2548 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2549 };
2550
2551 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2552 ((msg.error_code >= 0) &&
2553 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2554 halErrorMap[msg.error_code] :
2555 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2556
2557 int streamId = 0;
2558 if (msg.error_stream != NULL) {
2559 Camera3Stream *stream =
2560 Camera3Stream::cast(msg.error_stream);
2561 streamId = stream->getId();
2562 }
2563 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2564 mId, __FUNCTION__, msg.frame_number,
2565 streamId, msg.error_code);
2566
2567 CaptureResultExtras resultExtras;
2568 switch (errorCode) {
2569 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2570 // SET_ERR calls notifyError
2571 SET_ERR("Camera HAL reported serious device error");
2572 break;
2573 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2574 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2575 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2576 {
2577 Mutex::Autolock l(mInFlightLock);
2578 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2579 if (idx >= 0) {
2580 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2581 r.requestStatus = msg.error_code;
2582 resultExtras = r.resultExtras;
2583 } else {
2584 resultExtras.frameNumber = msg.frame_number;
2585 ALOGE("Camera %d: %s: cannot find in-flight request on "
2586 "frame %" PRId64 " error", mId, __FUNCTION__,
2587 resultExtras.frameNumber);
2588 }
2589 }
2590 if (listener != NULL) {
2591 listener->notifyError(errorCode, resultExtras);
2592 } else {
2593 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2594 }
2595 break;
2596 default:
2597 // SET_ERR calls notifyError
2598 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2599 break;
2600 }
2601}
2602
2603void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2604 NotificationListener *listener) {
2605 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002606
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002607 // Set timestamp for the request in the in-flight tracking
2608 // and get the request ID to send upstream
2609 {
2610 Mutex::Autolock l(mInFlightLock);
2611 idx = mInFlightMap.indexOfKey(msg.frame_number);
2612 if (idx >= 0) {
2613 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002614
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002615 // Verify ordering of shutter notifications
2616 {
2617 Mutex::Autolock l(mOutputLock);
2618 // TODO: need to track errors for tighter bounds on expected frame number.
2619 if (r.hasInputBuffer) {
2620 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2621 SET_ERR("Shutter notification out-of-order. Expected "
2622 "notification for frame %d, got frame %d",
2623 mNextReprocessShutterFrameNumber, msg.frame_number);
2624 return;
2625 }
2626 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2627 } else {
2628 if (msg.frame_number < mNextShutterFrameNumber) {
2629 SET_ERR("Shutter notification out-of-order. Expected "
2630 "notification for frame %d, got frame %d",
2631 mNextShutterFrameNumber, msg.frame_number);
2632 return;
2633 }
2634 mNextShutterFrameNumber = msg.frame_number + 1;
2635 }
2636 }
2637
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002638 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2639 mId, __FUNCTION__,
2640 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2641 // Call listener, if any
2642 if (listener != NULL) {
2643 listener->notifyShutter(r.resultExtras, msg.timestamp);
2644 }
2645
2646 r.shutterTimestamp = msg.timestamp;
2647
2648 // send pending result and buffers
2649 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002650 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002651 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002652 returnOutputBuffers(r.pendingOutputBuffers.array(),
2653 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2654 r.pendingOutputBuffers.clear();
2655
2656 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002657 }
2658 }
2659 if (idx < 0) {
2660 SET_ERR("Shutter notification for non-existent frame number %d",
2661 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002662 }
2663}
2664
2665
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002666CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002667 ALOGV("%s", __FUNCTION__);
2668
Igor Murashkin1e479c02013-09-06 16:55:14 -07002669 CameraMetadata retVal;
2670
2671 if (mRequestThread != NULL) {
2672 retVal = mRequestThread->getLatestRequest();
2673 }
2674
Igor Murashkin1e479c02013-09-06 16:55:14 -07002675 return retVal;
2676}
2677
Jianing Weicb0652e2014-03-12 18:29:36 -07002678
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002679/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002680 * RequestThread inner class methods
2681 */
2682
2683Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002684 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002685 camera3_device_t *hal3Device,
2686 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002687 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002688 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002689 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002690 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002691 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002692 mReconfigured(false),
2693 mDoPause(false),
2694 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002695 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002696 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002697 mCurrentAfTriggerId(0),
2698 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002699 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2700 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002701 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002702}
2703
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002704void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002705 NotificationListener *listener) {
2706 Mutex::Autolock l(mRequestLock);
2707 mListener = listener;
2708}
2709
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002710void Camera3Device::RequestThread::configurationComplete() {
2711 Mutex::Autolock l(mRequestLock);
2712 mReconfigured = true;
2713}
2714
Jianing Wei90e59c92014-03-12 18:29:36 -07002715status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002716 List<sp<CaptureRequest> > &requests,
2717 /*out*/
2718 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002719 Mutex::Autolock l(mRequestLock);
2720 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2721 ++it) {
2722 mRequestQueue.push_back(*it);
2723 }
2724
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002725 if (lastFrameNumber != NULL) {
2726 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2727 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2728 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2729 *lastFrameNumber);
2730 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002731
Jianing Wei90e59c92014-03-12 18:29:36 -07002732 unpauseForNewRequests();
2733
2734 return OK;
2735}
2736
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002737
2738status_t Camera3Device::RequestThread::queueTrigger(
2739 RequestTrigger trigger[],
2740 size_t count) {
2741
2742 Mutex::Autolock l(mTriggerMutex);
2743 status_t ret;
2744
2745 for (size_t i = 0; i < count; ++i) {
2746 ret = queueTriggerLocked(trigger[i]);
2747
2748 if (ret != OK) {
2749 return ret;
2750 }
2751 }
2752
2753 return OK;
2754}
2755
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002756int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2757 sp<Camera3Device> d = device.promote();
2758 if (d != NULL) return d->mId;
2759 return 0;
2760}
2761
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002762status_t Camera3Device::RequestThread::queueTriggerLocked(
2763 RequestTrigger trigger) {
2764
2765 uint32_t tag = trigger.metadataTag;
2766 ssize_t index = mTriggerMap.indexOfKey(tag);
2767
2768 switch (trigger.getTagType()) {
2769 case TYPE_BYTE:
2770 // fall-through
2771 case TYPE_INT32:
2772 break;
2773 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002774 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2775 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002776 return INVALID_OPERATION;
2777 }
2778
2779 /**
2780 * Collect only the latest trigger, since we only have 1 field
2781 * in the request settings per trigger tag, and can't send more than 1
2782 * trigger per request.
2783 */
2784 if (index != NAME_NOT_FOUND) {
2785 mTriggerMap.editValueAt(index) = trigger;
2786 } else {
2787 mTriggerMap.add(tag, trigger);
2788 }
2789
2790 return OK;
2791}
2792
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002793status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002794 const RequestList &requests,
2795 /*out*/
2796 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002797 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002798 if (lastFrameNumber != NULL) {
2799 *lastFrameNumber = mRepeatingLastFrameNumber;
2800 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002801 mRepeatingRequests.clear();
2802 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2803 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002804
2805 unpauseForNewRequests();
2806
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002807 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002808 return OK;
2809}
2810
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002811bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2812 if (mRepeatingRequests.empty()) {
2813 return false;
2814 }
2815 int32_t requestId = requestIn->mResultExtras.requestId;
2816 const RequestList &repeatRequests = mRepeatingRequests;
2817 // All repeating requests are guaranteed to have same id so only check first quest
2818 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2819 return (firstRequest->mResultExtras.requestId == requestId);
2820}
2821
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002822status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002823 Mutex::Autolock l(mRequestLock);
2824 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002825 if (lastFrameNumber != NULL) {
2826 *lastFrameNumber = mRepeatingLastFrameNumber;
2827 }
2828 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002829 return OK;
2830}
2831
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002832status_t Camera3Device::RequestThread::clear(
2833 NotificationListener *listener,
2834 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002835 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002836 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002837
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002838 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002839
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002840 // Send errors for all requests pending in the request queue, including
2841 // pending repeating requests
2842 if (listener != NULL) {
2843 for (RequestList::iterator it = mRequestQueue.begin();
2844 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002845 // Abort the input buffers for reprocess requests.
2846 if ((*it)->mInputStream != NULL) {
2847 camera3_stream_buffer_t inputBuffer;
2848 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2849 if (res != OK) {
2850 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2851 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2852 } else {
2853 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2854 if (res != OK) {
2855 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2856 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2857 }
2858 }
2859 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002860 // Set the frame number this request would have had, if it
2861 // had been submitted; this frame number will not be reused.
2862 // The requestId and burstId fields were set when the request was
2863 // submitted originally (in convertMetadataListToRequestListLocked)
2864 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2865 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2866 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002867 }
2868 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002869 mRequestQueue.clear();
2870 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002871 if (lastFrameNumber != NULL) {
2872 *lastFrameNumber = mRepeatingLastFrameNumber;
2873 }
2874 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002875 return OK;
2876}
2877
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002878status_t Camera3Device::RequestThread::flush() {
2879 ATRACE_CALL();
2880 Mutex::Autolock l(mFlushLock);
2881
2882 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2883 return mHal3Device->ops->flush(mHal3Device);
2884 }
2885
2886 return -ENOTSUP;
2887}
2888
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002889void Camera3Device::RequestThread::setPaused(bool paused) {
2890 Mutex::Autolock l(mPauseLock);
2891 mDoPause = paused;
2892 mDoPauseSignal.signal();
2893}
2894
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002895status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2896 int32_t requestId, nsecs_t timeout) {
2897 Mutex::Autolock l(mLatestRequestMutex);
2898 status_t res;
2899 while (mLatestRequestId != requestId) {
2900 nsecs_t startTime = systemTime();
2901
2902 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2903 if (res != OK) return res;
2904
2905 timeout -= (systemTime() - startTime);
2906 }
2907
2908 return OK;
2909}
2910
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002911void Camera3Device::RequestThread::requestExit() {
2912 // Call parent to set up shutdown
2913 Thread::requestExit();
2914 // The exit from any possible waits
2915 mDoPauseSignal.signal();
2916 mRequestSignal.signal();
2917}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002918
Chien-Yu Chend196d612015-06-22 19:49:01 -07002919
2920/**
2921 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2922 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2923 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2924 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2925 * request.
2926 */
2927void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2928 request->mAeTriggerCancelOverride.applyAeLock = false;
2929 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2930
2931 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2932 return;
2933 }
2934
2935 camera_metadata_entry_t aePrecaptureTrigger =
2936 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2937 if (aePrecaptureTrigger.count > 0 &&
2938 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2939 // Always override CANCEL to IDLE
2940 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2941 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2942 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2943 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2944 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2945
2946 if (mAeLockAvailable == true) {
2947 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2948 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2949 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2950 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2951 request->mAeTriggerCancelOverride.applyAeLock = true;
2952 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2953 }
2954 }
2955 }
2956}
2957
2958/**
2959 * Override result metadata for cancelling AE precapture trigger applied in
2960 * handleAePrecaptureCancelRequest().
2961 */
2962void Camera3Device::overrideResultForPrecaptureCancel(
2963 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2964 if (aeTriggerCancelOverride.applyAeLock) {
2965 // Only devices <= v3.2 should have this override
2966 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2967 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2968 }
2969
2970 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2971 // Only devices <= v3.2 should have this override
2972 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2973 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2974 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2975 }
2976}
2977
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002978bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002979 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002980 status_t res;
2981
2982 // Handle paused state.
2983 if (waitIfPaused()) {
2984 return true;
2985 }
2986
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002987 // Wait for the next batch of requests.
2988 waitForNextRequestBatch();
2989 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002990 return true;
2991 }
2992
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002993 // Get the latest request ID, if any
2994 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07002995 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002996 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002997 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002998 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002999 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003000 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3001 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003002 }
3003
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003004 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003005 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003006 if (res == TIMED_OUT) {
3007 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003008 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003009 return true;
3010 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003011 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003012 return false;
3013 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003014
Zhijun Hecc27e112013-10-03 16:12:43 -07003015 // Inform waitUntilRequestProcessed thread of a new request ID
3016 {
3017 Mutex::Autolock al(mLatestRequestMutex);
3018
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003019 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003020 mLatestRequestSignal.signal();
3021 }
3022
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003023 // Submit a batch of requests to HAL.
3024 // Use flush lock only when submitting multilple requests in a batch.
3025 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3026 // which may take a long time to finish so synchronizing flush() and
3027 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3028 // For now, only synchronize for high speed recording and we should figure something out for
3029 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003030 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003031
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003032 if (useFlushLock) {
3033 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003034 }
3035
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003036 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003037 mNextRequests.size());
3038 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003039 // Submit request and block until ready for next one
3040 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3041 ATRACE_BEGIN("camera3->process_capture_request");
3042 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3043 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003044
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003045 if (res != OK) {
3046 // Should only get a failure here for malformed requests or device-level
3047 // errors, so consider all errors fatal. Bad metadata failures should
3048 // come through notify.
3049 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3050 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3051 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003052 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003053 if (useFlushLock) {
3054 mFlushLock.unlock();
3055 }
3056 return false;
3057 }
3058
3059 // Mark that the request has be submitted successfully.
3060 nextRequest.submitted = true;
3061
3062 // Update the latest request sent to HAL
3063 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3064 Mutex::Autolock al(mLatestRequestMutex);
3065
3066 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3067 mLatestRequest.acquire(cloned);
3068 }
3069
3070 if (nextRequest.halRequest.settings != NULL) {
3071 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3072 }
3073
3074 // Remove any previously queued triggers (after unlock)
3075 res = removeTriggers(mPrevRequest);
3076 if (res != OK) {
3077 SET_ERR("RequestThread: Unable to remove triggers "
3078 "(capture request %d, HAL device: %s (%d)",
3079 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003080 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003081 if (useFlushLock) {
3082 mFlushLock.unlock();
3083 }
3084 return false;
3085 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003086 }
3087
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003088 if (useFlushLock) {
3089 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003090 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003091
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003092 // Unset as current request
3093 {
3094 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003095 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003096 }
3097
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003098 return true;
3099}
3100
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003101status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003102 ATRACE_CALL();
3103
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003104 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003105 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3106 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3107 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3108
3109 // Prepare a request to HAL
3110 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3111
3112 // Insert any queued triggers (before metadata is locked)
3113 status_t res = insertTriggers(captureRequest);
3114
3115 if (res < 0) {
3116 SET_ERR("RequestThread: Unable to insert triggers "
3117 "(capture request %d, HAL device: %s (%d)",
3118 halRequest->frame_number, strerror(-res), res);
3119 return INVALID_OPERATION;
3120 }
3121 int triggerCount = res;
3122 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3123 mPrevTriggers = triggerCount;
3124
3125 // If the request is the same as last, or we had triggers last time
3126 if (mPrevRequest != captureRequest || triggersMixedIn) {
3127 /**
3128 * HAL workaround:
3129 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3130 */
3131 res = addDummyTriggerIds(captureRequest);
3132 if (res != OK) {
3133 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3134 "(capture request %d, HAL device: %s (%d)",
3135 halRequest->frame_number, strerror(-res), res);
3136 return INVALID_OPERATION;
3137 }
3138
3139 /**
3140 * The request should be presorted so accesses in HAL
3141 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3142 */
3143 captureRequest->mSettings.sort();
3144 halRequest->settings = captureRequest->mSettings.getAndLock();
3145 mPrevRequest = captureRequest;
3146 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3147
3148 IF_ALOGV() {
3149 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3150 find_camera_metadata_ro_entry(
3151 halRequest->settings,
3152 ANDROID_CONTROL_AF_TRIGGER,
3153 &e
3154 );
3155 if (e.count > 0) {
3156 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3157 __FUNCTION__,
3158 halRequest->frame_number,
3159 e.data.u8[0]);
3160 }
3161 }
3162 } else {
3163 // leave request.settings NULL to indicate 'reuse latest given'
3164 ALOGVV("%s: Request settings are REUSED",
3165 __FUNCTION__);
3166 }
3167
3168 uint32_t totalNumBuffers = 0;
3169
3170 // Fill in buffers
3171 if (captureRequest->mInputStream != NULL) {
3172 halRequest->input_buffer = &captureRequest->mInputBuffer;
3173 totalNumBuffers += 1;
3174 } else {
3175 halRequest->input_buffer = NULL;
3176 }
3177
3178 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3179 captureRequest->mOutputStreams.size());
3180 halRequest->output_buffers = outputBuffers->array();
3181 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3182 res = captureRequest->mOutputStreams.editItemAt(i)->
3183 getBuffer(&outputBuffers->editItemAt(i));
3184 if (res != OK) {
3185 // Can't get output buffer from gralloc queue - this could be due to
3186 // abandoned queue or other consumer misbehavior, so not a fatal
3187 // error
3188 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3189 " %s (%d)", strerror(-res), res);
3190
3191 return TIMED_OUT;
3192 }
3193 halRequest->num_output_buffers++;
3194 }
3195 totalNumBuffers += halRequest->num_output_buffers;
3196
3197 // Log request in the in-flight queue
3198 sp<Camera3Device> parent = mParent.promote();
3199 if (parent == NULL) {
3200 // Should not happen, and nowhere to send errors to, so just log it
3201 CLOGE("RequestThread: Parent is gone");
3202 return INVALID_OPERATION;
3203 }
3204 res = parent->registerInFlight(halRequest->frame_number,
3205 totalNumBuffers, captureRequest->mResultExtras,
3206 /*hasInput*/halRequest->input_buffer != NULL,
3207 captureRequest->mAeTriggerCancelOverride);
3208 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3209 ", burstId = %" PRId32 ".",
3210 __FUNCTION__,
3211 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3212 captureRequest->mResultExtras.burstId);
3213 if (res != OK) {
3214 SET_ERR("RequestThread: Unable to register new in-flight request:"
3215 " %s (%d)", strerror(-res), res);
3216 return INVALID_OPERATION;
3217 }
3218 }
3219
3220 return OK;
3221}
3222
Igor Murashkin1e479c02013-09-06 16:55:14 -07003223CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3224 Mutex::Autolock al(mLatestRequestMutex);
3225
3226 ALOGV("RequestThread::%s", __FUNCTION__);
3227
3228 return mLatestRequest;
3229}
3230
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003231bool Camera3Device::RequestThread::isStreamPending(
3232 sp<Camera3StreamInterface>& stream) {
3233 Mutex::Autolock l(mRequestLock);
3234
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003235 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003236 if (!nextRequest.submitted) {
3237 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3238 if (stream == s) return true;
3239 }
3240 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003241 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003242 }
3243
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003244 for (const auto& request : mRequestQueue) {
3245 for (const auto& s : request->mOutputStreams) {
3246 if (stream == s) return true;
3247 }
3248 if (stream == request->mInputStream) return true;
3249 }
3250
3251 for (const auto& request : mRepeatingRequests) {
3252 for (const auto& s : request->mOutputStreams) {
3253 if (stream == s) return true;
3254 }
3255 if (stream == request->mInputStream) return true;
3256 }
3257
3258 return false;
3259}
Jianing Weicb0652e2014-03-12 18:29:36 -07003260
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003261void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3262 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003263 return;
3264 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003265
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003266 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003267 // Skip the ones that have been submitted successfully.
3268 if (nextRequest.submitted) {
3269 continue;
3270 }
3271
3272 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3273 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3274 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3275
3276 if (halRequest->settings != NULL) {
3277 captureRequest->mSettings.unlock(halRequest->settings);
3278 }
3279
3280 if (captureRequest->mInputStream != NULL) {
3281 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3282 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3283 }
3284
3285 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3286 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3287 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3288 }
3289
3290 if (sendRequestError) {
3291 Mutex::Autolock l(mRequestLock);
3292 if (mListener != NULL) {
3293 mListener->notifyError(
3294 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3295 captureRequest->mResultExtras);
3296 }
3297 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003298 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003299
3300 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003301 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003302}
3303
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003304void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003305 // Optimized a bit for the simple steady-state case (single repeating
3306 // request), to avoid putting that request in the queue temporarily.
3307 Mutex::Autolock l(mRequestLock);
3308
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003309 assert(mNextRequests.empty());
3310
3311 NextRequest nextRequest;
3312 nextRequest.captureRequest = waitForNextRequestLocked();
3313 if (nextRequest.captureRequest == nullptr) {
3314 return;
3315 }
3316
3317 nextRequest.halRequest = camera3_capture_request_t();
3318 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003319 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003320
3321 // Wait for additional requests
3322 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3323
3324 for (size_t i = 1; i < batchSize; i++) {
3325 NextRequest additionalRequest;
3326 additionalRequest.captureRequest = waitForNextRequestLocked();
3327 if (additionalRequest.captureRequest == nullptr) {
3328 break;
3329 }
3330
3331 additionalRequest.halRequest = camera3_capture_request_t();
3332 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003333 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003334 }
3335
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003336 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003337 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003338 mNextRequests.size(), batchSize);
3339 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003340 }
3341
3342 return;
3343}
3344
3345sp<Camera3Device::CaptureRequest>
3346 Camera3Device::RequestThread::waitForNextRequestLocked() {
3347 status_t res;
3348 sp<CaptureRequest> nextRequest;
3349
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003350 while (mRequestQueue.empty()) {
3351 if (!mRepeatingRequests.empty()) {
3352 // Always atomically enqueue all requests in a repeating request
3353 // list. Guarantees a complete in-sequence set of captures to
3354 // application.
3355 const RequestList &requests = mRepeatingRequests;
3356 RequestList::const_iterator firstRequest =
3357 requests.begin();
3358 nextRequest = *firstRequest;
3359 mRequestQueue.insert(mRequestQueue.end(),
3360 ++firstRequest,
3361 requests.end());
3362 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003363
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003364 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003365
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003366 break;
3367 }
3368
3369 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3370
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003371 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3372 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003373 Mutex::Autolock pl(mPauseLock);
3374 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003375 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003376 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003377 // Let the tracker know
3378 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3379 if (statusTracker != 0) {
3380 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3381 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003382 }
3383 // Stop waiting for now and let thread management happen
3384 return NULL;
3385 }
3386 }
3387
3388 if (nextRequest == NULL) {
3389 // Don't have a repeating request already in hand, so queue
3390 // must have an entry now.
3391 RequestList::iterator firstRequest =
3392 mRequestQueue.begin();
3393 nextRequest = *firstRequest;
3394 mRequestQueue.erase(firstRequest);
3395 }
3396
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003397 // In case we've been unpaused by setPaused clearing mDoPause, need to
3398 // update internal pause state (capture/setRepeatingRequest unpause
3399 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003400 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003401 if (mPaused) {
3402 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3403 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3404 if (statusTracker != 0) {
3405 statusTracker->markComponentActive(mStatusId);
3406 }
3407 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003408 mPaused = false;
3409
3410 // Check if we've reconfigured since last time, and reset the preview
3411 // request if so. Can't use 'NULL request == repeat' across configure calls.
3412 if (mReconfigured) {
3413 mPrevRequest.clear();
3414 mReconfigured = false;
3415 }
3416
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003417 if (nextRequest != NULL) {
3418 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003419 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3420 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003421
3422 // Since RequestThread::clear() removes buffers from the input stream,
3423 // get the right buffer here before unlocking mRequestLock
3424 if (nextRequest->mInputStream != NULL) {
3425 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3426 if (res != OK) {
3427 // Can't get input buffer from gralloc queue - this could be due to
3428 // disconnected queue or other producer misbehavior, so not a fatal
3429 // error
3430 ALOGE("%s: Can't get input buffer, skipping request:"
3431 " %s (%d)", __FUNCTION__, strerror(-res), res);
3432 if (mListener != NULL) {
3433 mListener->notifyError(
3434 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3435 nextRequest->mResultExtras);
3436 }
3437 return NULL;
3438 }
3439 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003440 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003441
3442 handleAePrecaptureCancelRequest(nextRequest);
3443
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003444 return nextRequest;
3445}
3446
3447bool Camera3Device::RequestThread::waitIfPaused() {
3448 status_t res;
3449 Mutex::Autolock l(mPauseLock);
3450 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003451 if (mPaused == false) {
3452 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003453 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3454 // Let the tracker know
3455 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3456 if (statusTracker != 0) {
3457 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3458 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003459 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003460
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003461 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003462 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003463 return true;
3464 }
3465 }
3466 // We don't set mPaused to false here, because waitForNextRequest needs
3467 // to further manage the paused state in case of starvation.
3468 return false;
3469}
3470
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003471void Camera3Device::RequestThread::unpauseForNewRequests() {
3472 // With work to do, mark thread as unpaused.
3473 // If paused by request (setPaused), don't resume, to avoid
3474 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003475 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003476 Mutex::Autolock p(mPauseLock);
3477 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003478 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3479 if (mPaused) {
3480 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3481 if (statusTracker != 0) {
3482 statusTracker->markComponentActive(mStatusId);
3483 }
3484 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003485 mPaused = false;
3486 }
3487}
3488
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003489void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3490 sp<Camera3Device> parent = mParent.promote();
3491 if (parent != NULL) {
3492 va_list args;
3493 va_start(args, fmt);
3494
3495 parent->setErrorStateV(fmt, args);
3496
3497 va_end(args);
3498 }
3499}
3500
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003501status_t Camera3Device::RequestThread::insertTriggers(
3502 const sp<CaptureRequest> &request) {
3503
3504 Mutex::Autolock al(mTriggerMutex);
3505
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003506 sp<Camera3Device> parent = mParent.promote();
3507 if (parent == NULL) {
3508 CLOGE("RequestThread: Parent is gone");
3509 return DEAD_OBJECT;
3510 }
3511
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003512 CameraMetadata &metadata = request->mSettings;
3513 size_t count = mTriggerMap.size();
3514
3515 for (size_t i = 0; i < count; ++i) {
3516 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003517 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003518
3519 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3520 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3521 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003522 if (isAeTrigger) {
3523 request->mResultExtras.precaptureTriggerId = triggerId;
3524 mCurrentPreCaptureTriggerId = triggerId;
3525 } else {
3526 request->mResultExtras.afTriggerId = triggerId;
3527 mCurrentAfTriggerId = triggerId;
3528 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003529 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3530 continue; // Trigger ID tag is deprecated since device HAL 3.2
3531 }
3532 }
3533
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003534 camera_metadata_entry entry = metadata.find(tag);
3535
3536 if (entry.count > 0) {
3537 /**
3538 * Already has an entry for this trigger in the request.
3539 * Rewrite it with our requested trigger value.
3540 */
3541 RequestTrigger oldTrigger = trigger;
3542
3543 oldTrigger.entryValue = entry.data.u8[0];
3544
3545 mTriggerReplacedMap.add(tag, oldTrigger);
3546 } else {
3547 /**
3548 * More typical, no trigger entry, so we just add it
3549 */
3550 mTriggerRemovedMap.add(tag, trigger);
3551 }
3552
3553 status_t res;
3554
3555 switch (trigger.getTagType()) {
3556 case TYPE_BYTE: {
3557 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3558 res = metadata.update(tag,
3559 &entryValue,
3560 /*count*/1);
3561 break;
3562 }
3563 case TYPE_INT32:
3564 res = metadata.update(tag,
3565 &trigger.entryValue,
3566 /*count*/1);
3567 break;
3568 default:
3569 ALOGE("%s: Type not supported: 0x%x",
3570 __FUNCTION__,
3571 trigger.getTagType());
3572 return INVALID_OPERATION;
3573 }
3574
3575 if (res != OK) {
3576 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3577 ", value %d", __FUNCTION__, trigger.getTagName(),
3578 trigger.entryValue);
3579 return res;
3580 }
3581
3582 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3583 trigger.getTagName(),
3584 trigger.entryValue);
3585 }
3586
3587 mTriggerMap.clear();
3588
3589 return count;
3590}
3591
3592status_t Camera3Device::RequestThread::removeTriggers(
3593 const sp<CaptureRequest> &request) {
3594 Mutex::Autolock al(mTriggerMutex);
3595
3596 CameraMetadata &metadata = request->mSettings;
3597
3598 /**
3599 * Replace all old entries with their old values.
3600 */
3601 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3602 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3603
3604 status_t res;
3605
3606 uint32_t tag = trigger.metadataTag;
3607 switch (trigger.getTagType()) {
3608 case TYPE_BYTE: {
3609 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3610 res = metadata.update(tag,
3611 &entryValue,
3612 /*count*/1);
3613 break;
3614 }
3615 case TYPE_INT32:
3616 res = metadata.update(tag,
3617 &trigger.entryValue,
3618 /*count*/1);
3619 break;
3620 default:
3621 ALOGE("%s: Type not supported: 0x%x",
3622 __FUNCTION__,
3623 trigger.getTagType());
3624 return INVALID_OPERATION;
3625 }
3626
3627 if (res != OK) {
3628 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3629 ", trigger value %d", __FUNCTION__,
3630 trigger.getTagName(), trigger.entryValue);
3631 return res;
3632 }
3633 }
3634 mTriggerReplacedMap.clear();
3635
3636 /**
3637 * Remove all new entries.
3638 */
3639 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3640 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3641 status_t res = metadata.erase(trigger.metadataTag);
3642
3643 if (res != OK) {
3644 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3645 ", trigger value %d", __FUNCTION__,
3646 trigger.getTagName(), trigger.entryValue);
3647 return res;
3648 }
3649 }
3650 mTriggerRemovedMap.clear();
3651
3652 return OK;
3653}
3654
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003655status_t Camera3Device::RequestThread::addDummyTriggerIds(
3656 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003657 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003658 static const int32_t dummyTriggerId = 1;
3659 status_t res;
3660
3661 CameraMetadata &metadata = request->mSettings;
3662
3663 // If AF trigger is active, insert a dummy AF trigger ID if none already
3664 // exists
3665 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3666 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3667 if (afTrigger.count > 0 &&
3668 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3669 afId.count == 0) {
3670 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3671 if (res != OK) return res;
3672 }
3673
3674 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3675 // if none already exists
3676 camera_metadata_entry pcTrigger =
3677 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3678 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3679 if (pcTrigger.count > 0 &&
3680 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3681 pcId.count == 0) {
3682 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3683 &dummyTriggerId, 1);
3684 if (res != OK) return res;
3685 }
3686
3687 return OK;
3688}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003689
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003690/**
3691 * PreparerThread inner class methods
3692 */
3693
3694Camera3Device::PreparerThread::PreparerThread() :
3695 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3696}
3697
3698Camera3Device::PreparerThread::~PreparerThread() {
3699 Thread::requestExitAndWait();
3700 if (mCurrentStream != nullptr) {
3701 mCurrentStream->cancelPrepare();
3702 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3703 mCurrentStream.clear();
3704 }
3705 clear();
3706}
3707
Ruben Brunkc78ac262015-08-13 17:58:46 -07003708status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003709 status_t res;
3710
3711 Mutex::Autolock l(mLock);
3712
Ruben Brunkc78ac262015-08-13 17:58:46 -07003713 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003714 if (res == OK) {
3715 // No preparation needed, fire listener right off
3716 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3717 if (mListener) {
3718 mListener->notifyPrepared(stream->getId());
3719 }
3720 return OK;
3721 } else if (res != NOT_ENOUGH_DATA) {
3722 return res;
3723 }
3724
3725 // Need to prepare, start up thread if necessary
3726 if (!mActive) {
3727 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3728 // isn't running
3729 Thread::requestExitAndWait();
3730 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3731 if (res != OK) {
3732 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3733 if (mListener) {
3734 mListener->notifyPrepared(stream->getId());
3735 }
3736 return res;
3737 }
3738 mCancelNow = false;
3739 mActive = true;
3740 ALOGV("%s: Preparer stream started", __FUNCTION__);
3741 }
3742
3743 // queue up the work
3744 mPendingStreams.push_back(stream);
3745 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3746
3747 return OK;
3748}
3749
3750status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003751 Mutex::Autolock l(mLock);
3752
3753 for (const auto& stream : mPendingStreams) {
3754 stream->cancelPrepare();
3755 }
3756 mPendingStreams.clear();
3757 mCancelNow = true;
3758
3759 return OK;
3760}
3761
3762void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3763 Mutex::Autolock l(mLock);
3764 mListener = listener;
3765}
3766
3767bool Camera3Device::PreparerThread::threadLoop() {
3768 status_t res;
3769 {
3770 Mutex::Autolock l(mLock);
3771 if (mCurrentStream == nullptr) {
3772 // End thread if done with work
3773 if (mPendingStreams.empty()) {
3774 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3775 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3776 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3777 mActive = false;
3778 return false;
3779 }
3780
3781 // Get next stream to prepare
3782 auto it = mPendingStreams.begin();
3783 mCurrentStream = *it;
3784 mPendingStreams.erase(it);
3785 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3786 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3787 } else if (mCancelNow) {
3788 mCurrentStream->cancelPrepare();
3789 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3790 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3791 mCurrentStream.clear();
3792 mCancelNow = false;
3793 return true;
3794 }
3795 }
3796
3797 res = mCurrentStream->prepareNextBuffer();
3798 if (res == NOT_ENOUGH_DATA) return true;
3799 if (res != OK) {
3800 // Something bad happened; try to recover by cancelling prepare and
3801 // signalling listener anyway
3802 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3803 mCurrentStream->getId(), res, strerror(-res));
3804 mCurrentStream->cancelPrepare();
3805 }
3806
3807 // This stream has finished, notify listener
3808 Mutex::Autolock l(mLock);
3809 if (mListener) {
3810 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3811 mCurrentStream->getId());
3812 mListener->notifyPrepared(mCurrentStream->getId());
3813 }
3814
3815 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3816 mCurrentStream.clear();
3817
3818 return true;
3819}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003820
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003821/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003822 * Static callback forwarding methods from HAL to instance
3823 */
3824
3825void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3826 const camera3_capture_result *result) {
3827 Camera3Device *d =
3828 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003829
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003830 d->processCaptureResult(result);
3831}
3832
3833void Camera3Device::sNotify(const camera3_callback_ops *cb,
3834 const camera3_notify_msg *msg) {
3835 Camera3Device *d =
3836 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3837 d->notify(msg);
3838}
3839
3840}; // namespace android