blob: 5f990a93e4acc22803a15409978e12948d69f4fb [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),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080067 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070068 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070069 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070070 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070071 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070072 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080073{
74 ATRACE_CALL();
75 camera3_callback_ops::notify = &sNotify;
76 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
77 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
78}
79
80Camera3Device::~Camera3Device()
81{
82 ATRACE_CALL();
83 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
84 disconnect();
85}
86
Igor Murashkin71381052013-03-04 14:53:08 -080087int Camera3Device::getId() const {
88 return mId;
89}
90
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080091/**
92 * CameraDeviceBase interface
93 */
94
Yin-Chia Yehe074a932015-01-30 10:29:02 -080095status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080096{
97 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070098 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080099 Mutex::Autolock l(mLock);
100
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800101 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800102 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700103 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800104 return INVALID_OPERATION;
105 }
106
107 /** Open HAL device */
108
109 status_t res;
110 String8 deviceName = String8::format("%d", mId);
111
112 camera3_device_t *device;
113
Zhijun He213ce792013-11-19 08:45:15 -0800114 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800115 res = module->open(deviceName.string(),
116 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800117 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800118
119 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700120 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800121 return res;
122 }
123
124 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700125 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700126 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700127 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700128 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800129 device->common.version);
130 device->common.close(&device->common);
131 return BAD_VALUE;
132 }
133
134 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800135 res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700136 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800137 if (res != OK) return res;
138
139 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700140 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
141 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700142 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800143 device->common.close(&device->common);
144 return BAD_VALUE;
145 }
146
147 /** Initialize device with callback functions */
148
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700149 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800150 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700151 ATRACE_END();
152
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800153 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700154 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
155 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800156 device->common.close(&device->common);
157 return BAD_VALUE;
158 }
159
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700160 /** Start up status tracker thread */
161 mStatusTracker = new StatusTracker(this);
162 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
163 if (res != OK) {
164 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
165 strerror(-res), res);
166 device->common.close(&device->common);
167 mStatusTracker.clear();
168 return res;
169 }
170
Zhijun He125684a2015-12-26 15:07:30 -0800171 /** Create buffer manager */
172 mBufferManager = new Camera3BufferManager();
173
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700174 bool aeLockAvailable = false;
175 camera_metadata_ro_entry aeLockAvailableEntry;
176 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
177 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
178 if (res == OK && aeLockAvailableEntry.count > 0) {
179 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
180 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
181 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800182
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700183 /** Start up request queue thread */
184 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800185 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800186 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700187 SET_ERR_L("Unable to start request queue thread: %s (%d)",
188 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800189 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800190 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800191 return res;
192 }
193
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700194 mPreparerThread = new PreparerThread();
195
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800196 /** Everything is good to go */
197
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700198 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800199 mDeviceInfo = info.static_camera_characteristics;
200 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700201
202 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800203 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700204 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700205 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700206 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800207
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800208 // Measure the clock domain offset between camera and video/hw_composer
209 camera_metadata_entry timestampSource =
210 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
211 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
212 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
213 mTimestampOffset = getMonoToBoottimeOffset();
214 }
215
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700216 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700217 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
218 camera_metadata_entry partialResultsCount =
219 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
220 if (partialResultsCount.count > 0) {
221 mNumPartialResults = partialResultsCount.data.i32[0];
222 mUsePartialResult = (mNumPartialResults > 1);
223 }
224 } else {
225 camera_metadata_entry partialResultsQuirk =
226 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
227 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
228 mUsePartialResult = true;
229 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700230 }
231
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700232 camera_metadata_entry configs =
233 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
234 for (uint32_t i = 0; i < configs.count; i += 4) {
235 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
236 configs.data.i32[i + 3] ==
237 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
238 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
239 configs.data.i32[i + 2]));
240 }
241 }
242
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800243 return OK;
244}
245
246status_t Camera3Device::disconnect() {
247 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700248 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800249
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800250 ALOGV("%s: E", __FUNCTION__);
251
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700252 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800253
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700254 {
255 Mutex::Autolock l(mLock);
256 if (mStatus == STATUS_UNINITIALIZED) return res;
257
258 if (mStatus == STATUS_ACTIVE ||
259 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
260 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700261 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700262 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700263 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700264 } else {
265 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
266 if (res != OK) {
267 SET_ERR_L("Timeout waiting for HAL to drain");
268 // Continue to close device even in case of error
269 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700270 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800271 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800272
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700273 if (mStatus == STATUS_ERROR) {
274 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700275 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700276
277 if (mStatusTracker != NULL) {
278 mStatusTracker->requestExit();
279 }
280
281 if (mRequestThread != NULL) {
282 mRequestThread->requestExit();
283 }
284
285 mOutputStreams.clear();
286 mInputStream.clear();
287 }
288
289 // Joining done without holding mLock, otherwise deadlocks may ensue
290 // as the threads try to access parent state
291 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
292 // HAL may be in a bad state, so waiting for request thread
293 // (which may be stuck in the HAL processCaptureRequest call)
294 // could be dangerous.
295 mRequestThread->join();
296 }
297
298 if (mStatusTracker != NULL) {
299 mStatusTracker->join();
300 }
301
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700302 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700303 {
304 Mutex::Autolock l(mLock);
305
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800306 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700307 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800308 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800309
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700310 hal3Device = mHal3Device;
311 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800312
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700313 // Call close without internal mutex held, as the HAL close may need to
314 // wait on assorted callbacks,etc, to complete before it can return.
315 if (hal3Device != NULL) {
316 ATRACE_BEGIN("camera3->close");
317 hal3Device->common.close(&hal3Device->common);
318 ATRACE_END();
319 }
320
321 {
322 Mutex::Autolock l(mLock);
323 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700324 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700325 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800326
327 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700328 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800329}
330
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700331// For dumping/debugging only -
332// try to acquire a lock a few times, eventually give up to proceed with
333// debug/dump operations
334bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
335 bool gotLock = false;
336 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
337 if (lock.tryLock() == NO_ERROR) {
338 gotLock = true;
339 break;
340 } else {
341 usleep(kDumpSleepDuration);
342 }
343 }
344 return gotLock;
345}
346
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700347Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
348 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
349 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
350 const int STREAM_CONFIGURATION_SIZE = 4;
351 const int STREAM_FORMAT_OFFSET = 0;
352 const int STREAM_WIDTH_OFFSET = 1;
353 const int STREAM_HEIGHT_OFFSET = 2;
354 const int STREAM_IS_INPUT_OFFSET = 3;
355 camera_metadata_ro_entry_t availableStreamConfigs =
356 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
357 if (availableStreamConfigs.count == 0 ||
358 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
359 return Size(0, 0);
360 }
361
362 // Get max jpeg size (area-wise).
363 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
364 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
365 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
366 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
367 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
368 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
369 && format == HAL_PIXEL_FORMAT_BLOB &&
370 (width * height > maxJpegWidth * maxJpegHeight)) {
371 maxJpegWidth = width;
372 maxJpegHeight = height;
373 }
374 }
375 } else {
376 camera_metadata_ro_entry availableJpegSizes =
377 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
378 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
379 return Size(0, 0);
380 }
381
382 // Get max jpeg size (area-wise).
383 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
384 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
385 > (maxJpegWidth * maxJpegHeight)) {
386 maxJpegWidth = availableJpegSizes.data.i32[i];
387 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
388 }
389 }
390 }
391 return Size(maxJpegWidth, maxJpegHeight);
392}
393
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800394nsecs_t Camera3Device::getMonoToBoottimeOffset() {
395 // try three times to get the clock offset, choose the one
396 // with the minimum gap in measurements.
397 const int tries = 3;
398 nsecs_t bestGap, measured;
399 for (int i = 0; i < tries; ++i) {
400 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
401 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
402 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
403 const nsecs_t gap = tmono2 - tmono;
404 if (i == 0 || gap < bestGap) {
405 bestGap = gap;
406 measured = tbase - ((tmono + tmono2) >> 1);
407 }
408 }
409 return measured;
410}
411
Zhijun Hef7da0962014-04-24 13:27:56 -0700412ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700413 // Get max jpeg size (area-wise).
414 Size maxJpegResolution = getMaxJpegResolution();
415 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700416 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700417 __FUNCTION__, mId);
418 return BAD_VALUE;
419 }
420
Zhijun Hef7da0962014-04-24 13:27:56 -0700421 // Get max jpeg buffer size
422 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700423 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
424 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700425 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
426 return BAD_VALUE;
427 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700428 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800429 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700430
431 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700432 float scaleFactor = ((float) (width * height)) /
433 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800434 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
435 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700436 if (jpegBufferSize > maxJpegBufferSize) {
437 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700438 }
439
440 return jpegBufferSize;
441}
442
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700443ssize_t Camera3Device::getPointCloudBufferSize() const {
444 const int FLOATS_PER_POINT=4;
445 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
446 if (maxPointCount.count == 0) {
447 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
448 __FUNCTION__, mId);
449 return BAD_VALUE;
450 }
451 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
452 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
453 return maxBytesForPointCloud;
454}
455
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800456ssize_t Camera3Device::getRawOpaqueBufferSize(uint32_t width, uint32_t height) const {
457 const int PER_CONFIGURATION_SIZE = 3;
458 const int WIDTH_OFFSET = 0;
459 const int HEIGHT_OFFSET = 1;
460 const int SIZE_OFFSET = 2;
461 camera_metadata_ro_entry rawOpaqueSizes =
462 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800463 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800464 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
465 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%d)!",
466 __FUNCTION__, mId, count);
467 return BAD_VALUE;
468 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700469
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800470 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
471 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
472 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
473 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
474 }
475 }
476
477 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
478 __FUNCTION__, mId, width, height);
479 return BAD_VALUE;
480}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700481
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800482status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
483 ATRACE_CALL();
484 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700485
486 // Try to lock, but continue in case of failure (to avoid blocking in
487 // deadlocks)
488 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
489 bool gotLock = tryLockSpinRightRound(mLock);
490
491 ALOGW_IF(!gotInterfaceLock,
492 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
493 mId, __FUNCTION__);
494 ALOGW_IF(!gotLock,
495 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
496 mId, __FUNCTION__);
497
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800498 bool dumpTemplates = false;
499 String16 templatesOption("-t");
500 int n = args.size();
501 for (int i = 0; i < n; i++) {
502 if (args[i] == templatesOption) {
503 dumpTemplates = true;
504 }
505 }
506
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800507 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800508
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800509 const char *status =
510 mStatus == STATUS_ERROR ? "ERROR" :
511 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700512 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
513 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800514 mStatus == STATUS_ACTIVE ? "ACTIVE" :
515 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700516
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800517 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700518 if (mStatus == STATUS_ERROR) {
519 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
520 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800521 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700522 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700523 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800524
525 if (mInputStream != NULL) {
526 write(fd, lines.string(), lines.size());
527 mInputStream->dump(fd, args);
528 } else {
529 lines.appendFormat(" No input stream.\n");
530 write(fd, lines.string(), lines.size());
531 }
532 for (size_t i = 0; i < mOutputStreams.size(); i++) {
533 mOutputStreams[i]->dump(fd,args);
534 }
535
Zhijun He125684a2015-12-26 15:07:30 -0800536 lines = String8(" Camera3 Buffer Manager:\n");
537 write(fd, lines.string(), lines.size());
538 mBufferManager->dump(fd, args);
539
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700540 lines = String8(" In-flight requests:\n");
541 if (mInFlightMap.size() == 0) {
542 lines.append(" None\n");
543 } else {
544 for (size_t i = 0; i < mInFlightMap.size(); i++) {
545 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700546 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700547 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800548 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700549 r.numBuffersLeft);
550 }
551 }
552 write(fd, lines.string(), lines.size());
553
Igor Murashkin1e479c02013-09-06 16:55:14 -0700554 {
555 lines = String8(" Last request sent:\n");
556 write(fd, lines.string(), lines.size());
557
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700558 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700559 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
560 }
561
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800562 if (dumpTemplates) {
563 const char *templateNames[] = {
564 "TEMPLATE_PREVIEW",
565 "TEMPLATE_STILL_CAPTURE",
566 "TEMPLATE_VIDEO_RECORD",
567 "TEMPLATE_VIDEO_SNAPSHOT",
568 "TEMPLATE_ZERO_SHUTTER_LAG",
569 "TEMPLATE_MANUAL"
570 };
571
572 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
573 const camera_metadata_t *templateRequest;
574 templateRequest =
575 mHal3Device->ops->construct_default_request_settings(
576 mHal3Device, i);
577 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
578 if (templateRequest == NULL) {
579 lines.append(" Not supported\n");
580 write(fd, lines.string(), lines.size());
581 } else {
582 write(fd, lines.string(), lines.size());
583 dump_indented_camera_metadata(templateRequest,
584 fd, /*verbosity*/2, /*indentation*/8);
585 }
586 }
587 }
588
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800589 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700590 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800591 write(fd, lines.string(), lines.size());
592 mHal3Device->ops->dump(mHal3Device, fd);
593 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800594
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700595 if (gotLock) mLock.unlock();
596 if (gotInterfaceLock) mInterfaceLock.unlock();
597
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800598 return OK;
599}
600
601const CameraMetadata& Camera3Device::info() const {
602 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800603 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
604 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700605 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800606 mStatus == STATUS_ERROR ?
607 "when in error state" : "before init");
608 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800609 return mDeviceInfo;
610}
611
Jianing Wei90e59c92014-03-12 18:29:36 -0700612status_t Camera3Device::checkStatusOkToCaptureLocked() {
613 switch (mStatus) {
614 case STATUS_ERROR:
615 CLOGE("Device has encountered a serious error");
616 return INVALID_OPERATION;
617 case STATUS_UNINITIALIZED:
618 CLOGE("Device not initialized");
619 return INVALID_OPERATION;
620 case STATUS_UNCONFIGURED:
621 case STATUS_CONFIGURED:
622 case STATUS_ACTIVE:
623 // OK
624 break;
625 default:
626 SET_ERR_L("Unexpected status: %d", mStatus);
627 return INVALID_OPERATION;
628 }
629 return OK;
630}
631
632status_t Camera3Device::convertMetadataListToRequestListLocked(
633 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
634 if (requestList == NULL) {
635 CLOGE("requestList cannot be NULL.");
636 return BAD_VALUE;
637 }
638
Jianing Weicb0652e2014-03-12 18:29:36 -0700639 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700640 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
641 it != metadataList.end(); ++it) {
642 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
643 if (newRequest == 0) {
644 CLOGE("Can't create capture request");
645 return BAD_VALUE;
646 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700647
648 // Setup burst Id and request Id
649 newRequest->mResultExtras.burstId = burstId++;
650 if (it->exists(ANDROID_REQUEST_ID)) {
651 if (it->find(ANDROID_REQUEST_ID).count == 0) {
652 CLOGE("RequestID entry exists; but must not be empty in metadata");
653 return BAD_VALUE;
654 }
655 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
656 } else {
657 CLOGE("RequestID does not exist in metadata");
658 return BAD_VALUE;
659 }
660
Jianing Wei90e59c92014-03-12 18:29:36 -0700661 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700662
663 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700664 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700665
666 // Setup batch size if this is a high speed video recording request.
667 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
668 auto firstRequest = requestList->begin();
669 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
670 if (outputStream->isVideoStream()) {
671 (*firstRequest)->mBatchSize = requestList->size();
672 break;
673 }
674 }
675 }
676
Jianing Wei90e59c92014-03-12 18:29:36 -0700677 return OK;
678}
679
Jianing Weicb0652e2014-03-12 18:29:36 -0700680status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800681 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800682
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700683 List<const CameraMetadata> requests;
684 requests.push_back(request);
685 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800686}
687
Jianing Wei90e59c92014-03-12 18:29:36 -0700688status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700689 const List<const CameraMetadata> &requests, bool repeating,
690 /*out*/
691 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700692 ATRACE_CALL();
693 Mutex::Autolock il(mInterfaceLock);
694 Mutex::Autolock l(mLock);
695
696 status_t res = checkStatusOkToCaptureLocked();
697 if (res != OK) {
698 // error logged by previous call
699 return res;
700 }
701
702 RequestList requestList;
703
704 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
705 if (res != OK) {
706 // error logged by previous call
707 return res;
708 }
709
710 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700711 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700712 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700713 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700714 }
715
716 if (res == OK) {
717 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
718 if (res != OK) {
719 SET_ERR_L("Can't transition to active in %f seconds!",
720 kActiveTimeout/1e9);
721 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700722 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
723 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700724 } else {
725 CLOGE("Cannot queue request. Impossible.");
726 return BAD_VALUE;
727 }
728
729 return res;
730}
731
Jianing Weicb0652e2014-03-12 18:29:36 -0700732status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
733 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700734 ATRACE_CALL();
735
Jianing Weicb0652e2014-03-12 18:29:36 -0700736 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700737}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800738
Jianing Weicb0652e2014-03-12 18:29:36 -0700739status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
740 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800741 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800742
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700743 List<const CameraMetadata> requests;
744 requests.push_back(request);
745 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800746}
747
Jianing Weicb0652e2014-03-12 18:29:36 -0700748status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
749 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700750 ATRACE_CALL();
751
Jianing Weicb0652e2014-03-12 18:29:36 -0700752 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700753}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800754
755sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
756 const CameraMetadata &request) {
757 status_t res;
758
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700759 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800760 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700761 // Stream configuration failed due to unsupported configuration.
762 // Device back to unconfigured state. Client might try other configuraitons
763 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
764 CLOGE("No streams configured");
765 return NULL;
766 }
767 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800768 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700769 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800770 return NULL;
771 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700772 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700773 if (mStatus == STATUS_UNCONFIGURED) {
774 CLOGE("No streams configured");
775 return NULL;
776 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800777 }
778
779 sp<CaptureRequest> newRequest = createCaptureRequest(request);
780 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800781}
782
Jianing Weicb0652e2014-03-12 18:29:36 -0700783status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800784 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700785 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800786 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800787
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800788 switch (mStatus) {
789 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700790 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800791 return INVALID_OPERATION;
792 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700793 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800794 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700795 case STATUS_UNCONFIGURED:
796 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800797 case STATUS_ACTIVE:
798 // OK
799 break;
800 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700801 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800802 return INVALID_OPERATION;
803 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700804 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700805
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700806 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800807}
808
809status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
810 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700811 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800812
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700813 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800814}
815
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700816status_t Camera3Device::createInputStream(
817 uint32_t width, uint32_t height, int format, int *id) {
818 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700819 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700820 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700821 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
822 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700823
824 status_t res;
825 bool wasActive = false;
826
827 switch (mStatus) {
828 case STATUS_ERROR:
829 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
830 return INVALID_OPERATION;
831 case STATUS_UNINITIALIZED:
832 ALOGE("%s: Device not initialized", __FUNCTION__);
833 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700834 case STATUS_UNCONFIGURED:
835 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700836 // OK
837 break;
838 case STATUS_ACTIVE:
839 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700840 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700841 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700842 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700843 return res;
844 }
845 wasActive = true;
846 break;
847 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700848 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700849 return INVALID_OPERATION;
850 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700851 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700852
853 if (mInputStream != 0) {
854 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
855 return INVALID_OPERATION;
856 }
857
858 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
859 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700860 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700861
862 mInputStream = newStream;
863
864 *id = mNextStreamId++;
865
866 // Continue captures if active at start
867 if (wasActive) {
868 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
869 res = configureStreamsLocked();
870 if (res != OK) {
871 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
872 __FUNCTION__, mNextStreamId, strerror(-res), res);
873 return res;
874 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700875 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700876 }
877
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700878 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700879 return OK;
880}
881
Igor Murashkin2fba5842013-04-22 14:03:54 -0700882
883status_t Camera3Device::createZslStream(
884 uint32_t width, uint32_t height,
885 int depth,
886 /*out*/
887 int *id,
888 sp<Camera3ZslStream>* zslStream) {
889 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700890 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700891 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700892 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
893 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700894
895 status_t res;
896 bool wasActive = false;
897
898 switch (mStatus) {
899 case STATUS_ERROR:
900 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
901 return INVALID_OPERATION;
902 case STATUS_UNINITIALIZED:
903 ALOGE("%s: Device not initialized", __FUNCTION__);
904 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700905 case STATUS_UNCONFIGURED:
906 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700907 // OK
908 break;
909 case STATUS_ACTIVE:
910 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700911 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700912 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700913 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700914 return res;
915 }
916 wasActive = true;
917 break;
918 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700919 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700920 return INVALID_OPERATION;
921 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700922 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700923
924 if (mInputStream != 0) {
925 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
926 return INVALID_OPERATION;
927 }
928
929 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
930 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700931 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700932
933 res = mOutputStreams.add(mNextStreamId, newStream);
934 if (res < 0) {
935 ALOGE("%s: Can't add new stream to set: %s (%d)",
936 __FUNCTION__, strerror(-res), res);
937 return res;
938 }
939 mInputStream = newStream;
940
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530941 mNeedConfig = true;
942
Igor Murashkin2fba5842013-04-22 14:03:54 -0700943 *id = mNextStreamId++;
944 *zslStream = newStream;
945
946 // Continue captures if active at start
947 if (wasActive) {
948 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
949 res = configureStreamsLocked();
950 if (res != OK) {
951 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
952 __FUNCTION__, mNextStreamId, strerror(-res), res);
953 return res;
954 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700955 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700956 }
957
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700958 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700959 return OK;
960}
961
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700962status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800963 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -0800964 camera3_stream_rotation_t rotation, int *id, int streamSetId) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800965 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700966 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800967 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700968 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
969 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800970
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800971 status_t res;
972 bool wasActive = false;
973
974 switch (mStatus) {
975 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700976 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800977 return INVALID_OPERATION;
978 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700979 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800980 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700981 case STATUS_UNCONFIGURED:
982 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800983 // OK
984 break;
985 case STATUS_ACTIVE:
986 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700987 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800988 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700989 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800990 return res;
991 }
992 wasActive = true;
993 break;
994 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700995 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800996 return INVALID_OPERATION;
997 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700998 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800999
1000 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001001 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001002 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001003 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001004 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1005 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001006 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001007 ssize_t blobBufferSize;
1008 if (dataSpace != HAL_DATASPACE_DEPTH) {
1009 blobBufferSize = getJpegBufferSize(width, height);
1010 if (blobBufferSize <= 0) {
1011 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1012 return BAD_VALUE;
1013 }
1014 } else {
1015 blobBufferSize = getPointCloudBufferSize();
1016 if (blobBufferSize <= 0) {
1017 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1018 return BAD_VALUE;
1019 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001020 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001021 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001022 width, height, blobBufferSize, format, dataSpace, rotation,
1023 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001024 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1025 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1026 if (rawOpaqueBufferSize <= 0) {
1027 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1028 return BAD_VALUE;
1029 }
1030 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001031 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1032 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001033 } else {
1034 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001035 width, height, format, dataSpace, rotation,
1036 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001037 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001038 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001039
Zhijun He125684a2015-12-26 15:07:30 -08001040 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001041 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1042 * requires buffers to be statically allocated for internal static buffer registration, while
1043 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1044 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001045 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001046 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001047 newStream->setBufferManager(mBufferManager);
1048 }
1049
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001050 res = mOutputStreams.add(mNextStreamId, newStream);
1051 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001052 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053 return res;
1054 }
1055
1056 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001057 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001058
1059 // Continue captures if active at start
1060 if (wasActive) {
1061 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1062 res = configureStreamsLocked();
1063 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001064 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1065 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001066 return res;
1067 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001068 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001070 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001072}
1073
1074status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1075 ATRACE_CALL();
1076 (void)outputId; (void)id;
1077
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001078 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001079 return INVALID_OPERATION;
1080}
1081
1082
1083status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001084 uint32_t *width, uint32_t *height,
1085 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001086 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001087 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001088 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001089
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001090 switch (mStatus) {
1091 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001092 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001093 return INVALID_OPERATION;
1094 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001095 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001096 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001097 case STATUS_UNCONFIGURED:
1098 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001099 case STATUS_ACTIVE:
1100 // OK
1101 break;
1102 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001103 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001104 return INVALID_OPERATION;
1105 }
1106
1107 ssize_t idx = mOutputStreams.indexOfKey(id);
1108 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001109 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001110 return idx;
1111 }
1112
1113 if (width) *width = mOutputStreams[idx]->getWidth();
1114 if (height) *height = mOutputStreams[idx]->getHeight();
1115 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001116 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001117 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001118}
1119
1120status_t Camera3Device::setStreamTransform(int id,
1121 int transform) {
1122 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001123 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001124 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001125
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001126 switch (mStatus) {
1127 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001128 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001129 return INVALID_OPERATION;
1130 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001131 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001132 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001133 case STATUS_UNCONFIGURED:
1134 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001135 case STATUS_ACTIVE:
1136 // OK
1137 break;
1138 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001139 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001140 return INVALID_OPERATION;
1141 }
1142
1143 ssize_t idx = mOutputStreams.indexOfKey(id);
1144 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001145 CLOGE("Stream %d does not exist",
1146 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001147 return BAD_VALUE;
1148 }
1149
1150 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001151}
1152
1153status_t Camera3Device::deleteStream(int id) {
1154 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001155 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001156 Mutex::Autolock l(mLock);
1157 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001158
Igor Murashkine2172be2013-05-28 15:31:39 -07001159 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1160
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001161 // CameraDevice semantics require device to already be idle before
1162 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001163 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001164 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1165 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001166 }
1167
Igor Murashkin2fba5842013-04-22 14:03:54 -07001168 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001169 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001170 if (mInputStream != NULL && id == mInputStream->getId()) {
1171 deletedStream = mInputStream;
1172 mInputStream.clear();
1173 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001174 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001175 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001176 return BAD_VALUE;
1177 }
Zhijun He5f446352014-01-22 09:49:33 -08001178 }
1179
1180 // Delete output stream or the output part of a bi-directional stream.
1181 if (outputStreamIdx != NAME_NOT_FOUND) {
1182 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001183 mOutputStreams.removeItem(id);
1184 }
1185
1186 // Free up the stream endpoint so that it can be used by some other stream
1187 res = deletedStream->disconnect();
1188 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001189 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001190 // fall through since we want to still list the stream as deleted.
1191 }
1192 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001193 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001194
1195 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001196}
1197
1198status_t Camera3Device::deleteReprocessStream(int id) {
1199 ATRACE_CALL();
1200 (void)id;
1201
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001202 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001203 return INVALID_OPERATION;
1204}
1205
Zhijun He1fa89992015-06-01 15:44:31 -07001206status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001207 ATRACE_CALL();
1208 ALOGV("%s: E", __FUNCTION__);
1209
1210 Mutex::Autolock il(mInterfaceLock);
1211 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001212
1213 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1214 mNeedConfig = true;
1215 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1216 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001217
1218 return configureStreamsLocked();
1219}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001220
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001221status_t Camera3Device::getInputBufferProducer(
1222 sp<IGraphicBufferProducer> *producer) {
1223 Mutex::Autolock il(mInterfaceLock);
1224 Mutex::Autolock l(mLock);
1225
1226 if (producer == NULL) {
1227 return BAD_VALUE;
1228 } else if (mInputStream == NULL) {
1229 return INVALID_OPERATION;
1230 }
1231
1232 return mInputStream->getInputBufferProducer(producer);
1233}
1234
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001235status_t Camera3Device::createDefaultRequest(int templateId,
1236 CameraMetadata *request) {
1237 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001238 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001239 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001240 Mutex::Autolock l(mLock);
1241
1242 switch (mStatus) {
1243 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001244 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001245 return INVALID_OPERATION;
1246 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001247 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001248 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001249 case STATUS_UNCONFIGURED:
1250 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001251 case STATUS_ACTIVE:
1252 // OK
1253 break;
1254 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001255 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001256 return INVALID_OPERATION;
1257 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001258
Zhijun Hea1530f12014-09-14 12:44:20 -07001259 if (!mRequestTemplateCache[templateId].isEmpty()) {
1260 *request = mRequestTemplateCache[templateId];
1261 return OK;
1262 }
1263
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001264 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001265 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001266 rawRequest = mHal3Device->ops->construct_default_request_settings(
1267 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001268 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001269 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001270 ALOGI("%s: template %d is not supported on this camera device",
1271 __FUNCTION__, templateId);
1272 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001273 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001274 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001275 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001276
1277 return OK;
1278}
1279
1280status_t Camera3Device::waitUntilDrained() {
1281 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001282 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001283 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001284
Zhijun He69a37482014-03-23 18:44:49 -07001285 return waitUntilDrainedLocked();
1286}
1287
1288status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001289 switch (mStatus) {
1290 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001291 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001292 ALOGV("%s: Already idle", __FUNCTION__);
1293 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001294 case STATUS_CONFIGURED:
1295 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001296 case STATUS_ERROR:
1297 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001298 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001299 break;
1300 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001301 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001302 return INVALID_OPERATION;
1303 }
1304
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001305 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1306 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001307 if (res != OK) {
1308 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1309 res);
1310 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001311 return res;
1312}
1313
Ruben Brunk183f0562015-08-12 12:55:02 -07001314
1315void Camera3Device::internalUpdateStatusLocked(Status status) {
1316 mStatus = status;
1317 mRecentStatusUpdates.add(mStatus);
1318 mStatusChanged.broadcast();
1319}
1320
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001321// Pause to reconfigure
1322status_t Camera3Device::internalPauseAndWaitLocked() {
1323 mRequestThread->setPaused(true);
1324 mPauseStateNotify = true;
1325
1326 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1327 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1328 if (res != OK) {
1329 SET_ERR_L("Can't idle device in %f seconds!",
1330 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001331 }
1332
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001333 return res;
1334}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001335
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001336// Resume after internalPauseAndWaitLocked
1337status_t Camera3Device::internalResumeLocked() {
1338 status_t res;
1339
1340 mRequestThread->setPaused(false);
1341
1342 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1343 if (res != OK) {
1344 SET_ERR_L("Can't transition to active in %f seconds!",
1345 kActiveTimeout/1e9);
1346 }
1347 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001348 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001349}
1350
Ruben Brunk183f0562015-08-12 12:55:02 -07001351status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001352 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001353
1354 size_t startIndex = 0;
1355 if (mStatusWaiters == 0) {
1356 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1357 // this status list
1358 mRecentStatusUpdates.clear();
1359 } else {
1360 // If other threads are waiting on updates to this status list, set the position of the
1361 // first element that this list will check rather than clearing the list.
1362 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001363 }
1364
Ruben Brunk183f0562015-08-12 12:55:02 -07001365 mStatusWaiters++;
1366
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001367 bool stateSeen = false;
1368 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001369 if (active == (mStatus == STATUS_ACTIVE)) {
1370 // Desired state is current
1371 break;
1372 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001373
1374 res = mStatusChanged.waitRelative(mLock, timeout);
1375 if (res != OK) break;
1376
Ruben Brunk183f0562015-08-12 12:55:02 -07001377 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1378 // transitions.
1379 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1380 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1381 __FUNCTION__);
1382
1383 // Encountered desired state since we began waiting
1384 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001385 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1386 stateSeen = true;
1387 break;
1388 }
1389 }
1390 } while (!stateSeen);
1391
Ruben Brunk183f0562015-08-12 12:55:02 -07001392 mStatusWaiters--;
1393
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001394 return res;
1395}
1396
1397
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001398status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1399 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001400 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001401
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001402 if (listener != NULL && mListener != NULL) {
1403 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1404 }
1405 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001406 mRequestThread->setNotificationListener(listener);
1407 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001408
1409 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001410}
1411
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001412bool Camera3Device::willNotify3A() {
1413 return false;
1414}
1415
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001416status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001417 status_t res;
1418 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001419
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001420 while (mResultQueue.empty()) {
1421 res = mResultSignal.waitRelative(mOutputLock, timeout);
1422 if (res == TIMED_OUT) {
1423 return res;
1424 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001425 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001426 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001427 return res;
1428 }
1429 }
1430 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001431}
1432
Jianing Weicb0652e2014-03-12 18:29:36 -07001433status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001434 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001435 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001436
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001437 if (mResultQueue.empty()) {
1438 return NOT_ENOUGH_DATA;
1439 }
1440
Jianing Weicb0652e2014-03-12 18:29:36 -07001441 if (frame == NULL) {
1442 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1443 return BAD_VALUE;
1444 }
1445
1446 CaptureResult &result = *(mResultQueue.begin());
1447 frame->mResultExtras = result.mResultExtras;
1448 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001449 mResultQueue.erase(mResultQueue.begin());
1450
1451 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001452}
1453
1454status_t Camera3Device::triggerAutofocus(uint32_t id) {
1455 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001456 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001457
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001458 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1459 // Mix-in this trigger into the next request and only the next request.
1460 RequestTrigger trigger[] = {
1461 {
1462 ANDROID_CONTROL_AF_TRIGGER,
1463 ANDROID_CONTROL_AF_TRIGGER_START
1464 },
1465 {
1466 ANDROID_CONTROL_AF_TRIGGER_ID,
1467 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001468 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001469 };
1470
1471 return mRequestThread->queueTrigger(trigger,
1472 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001473}
1474
1475status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1476 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001477 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001478
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001479 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1480 // Mix-in this trigger into the next request and only the next request.
1481 RequestTrigger trigger[] = {
1482 {
1483 ANDROID_CONTROL_AF_TRIGGER,
1484 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1485 },
1486 {
1487 ANDROID_CONTROL_AF_TRIGGER_ID,
1488 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001489 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001490 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001491
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001492 return mRequestThread->queueTrigger(trigger,
1493 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001494}
1495
1496status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1497 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001498 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001499
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001500 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1501 // Mix-in this trigger into the next request and only the next request.
1502 RequestTrigger trigger[] = {
1503 {
1504 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1505 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1506 },
1507 {
1508 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1509 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001510 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001511 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001512
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001513 return mRequestThread->queueTrigger(trigger,
1514 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001515}
1516
1517status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1518 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1519 ATRACE_CALL();
1520 (void)reprocessStreamId; (void)buffer; (void)listener;
1521
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001522 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001523 return INVALID_OPERATION;
1524}
1525
Jianing Weicb0652e2014-03-12 18:29:36 -07001526status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001527 ATRACE_CALL();
1528 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001529 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001530
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001531 NotificationListener* listener;
1532 {
1533 Mutex::Autolock l(mOutputLock);
1534 listener = mListener;
1535 }
1536
Zhijun He7ef20392014-04-21 16:04:17 -07001537 {
1538 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001539 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001540 }
1541
Zhijun He491e3412013-12-27 10:57:44 -08001542 status_t res;
1543 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001544 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001545 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001546 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001547 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001548 }
1549
1550 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001551}
1552
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001553status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001554 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1555}
1556
1557status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001558 ATRACE_CALL();
1559 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001560 Mutex::Autolock il(mInterfaceLock);
1561 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001562
1563 sp<Camera3StreamInterface> stream;
1564 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1565 if (outputStreamIdx == NAME_NOT_FOUND) {
1566 CLOGE("Stream %d does not exist", streamId);
1567 return BAD_VALUE;
1568 }
1569
1570 stream = mOutputStreams.editValueAt(outputStreamIdx);
1571
1572 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001573 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001574 return BAD_VALUE;
1575 }
1576
1577 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001578 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001579 return BAD_VALUE;
1580 }
1581
Ruben Brunkc78ac262015-08-13 17:58:46 -07001582 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001583}
1584
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001585status_t Camera3Device::tearDown(int streamId) {
1586 ATRACE_CALL();
1587 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1588 Mutex::Autolock il(mInterfaceLock);
1589 Mutex::Autolock l(mLock);
1590
1591 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1592 // since we cannot call register_stream_buffers except right after configure_streams.
1593 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1594 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1595 __FUNCTION__, mHal3Device->common.version);
1596 return NO_INIT;
1597 }
1598
1599 sp<Camera3StreamInterface> stream;
1600 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1601 if (outputStreamIdx == NAME_NOT_FOUND) {
1602 CLOGE("Stream %d does not exist", streamId);
1603 return BAD_VALUE;
1604 }
1605
1606 stream = mOutputStreams.editValueAt(outputStreamIdx);
1607
1608 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1609 CLOGE("Stream %d is a target of a in-progress request", streamId);
1610 return BAD_VALUE;
1611 }
1612
1613 return stream->tearDown();
1614}
1615
Zhijun He204e3292014-07-14 17:09:23 -07001616uint32_t Camera3Device::getDeviceVersion() {
1617 ATRACE_CALL();
1618 Mutex::Autolock il(mInterfaceLock);
1619 return mDeviceVersion;
1620}
1621
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001622/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001623 * Methods called by subclasses
1624 */
1625
1626void Camera3Device::notifyStatus(bool idle) {
1627 {
1628 // Need mLock to safely update state and synchronize to current
1629 // state of methods in flight.
1630 Mutex::Autolock l(mLock);
1631 // We can get various system-idle notices from the status tracker
1632 // while starting up. Only care about them if we've actually sent
1633 // in some requests recently.
1634 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1635 return;
1636 }
1637 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1638 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001639 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001640
1641 // Skip notifying listener if we're doing some user-transparent
1642 // state changes
1643 if (mPauseStateNotify) return;
1644 }
1645 NotificationListener *listener;
1646 {
1647 Mutex::Autolock l(mOutputLock);
1648 listener = mListener;
1649 }
1650 if (idle && listener != NULL) {
1651 listener->notifyIdle();
1652 }
1653}
1654
1655/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001656 * Camera3Device private methods
1657 */
1658
1659sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1660 const CameraMetadata &request) {
1661 ATRACE_CALL();
1662 status_t res;
1663
1664 sp<CaptureRequest> newRequest = new CaptureRequest;
1665 newRequest->mSettings = request;
1666
1667 camera_metadata_entry_t inputStreams =
1668 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1669 if (inputStreams.count > 0) {
1670 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001671 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001672 CLOGE("Request references unknown input stream %d",
1673 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001674 return NULL;
1675 }
1676 // Lazy completion of stream configuration (allocation/registration)
1677 // on first use
1678 if (mInputStream->isConfiguring()) {
1679 res = mInputStream->finishConfiguration(mHal3Device);
1680 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001681 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001682 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001683 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001684 return NULL;
1685 }
1686 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001687 // Check if stream is being prepared
1688 if (mInputStream->isPreparing()) {
1689 CLOGE("Request references an input stream that's being prepared!");
1690 return NULL;
1691 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001692
1693 newRequest->mInputStream = mInputStream;
1694 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1695 }
1696
1697 camera_metadata_entry_t streams =
1698 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1699 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001700 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001701 return NULL;
1702 }
1703
1704 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001705 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001706 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001707 CLOGE("Request references unknown stream %d",
1708 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001709 return NULL;
1710 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001711 sp<Camera3OutputStreamInterface> stream =
1712 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001713
1714 // Lazy completion of stream configuration (allocation/registration)
1715 // on first use
1716 if (stream->isConfiguring()) {
1717 res = stream->finishConfiguration(mHal3Device);
1718 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001719 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1720 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001721 return NULL;
1722 }
1723 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001724 // Check if stream is being prepared
1725 if (stream->isPreparing()) {
1726 CLOGE("Request references an output stream that's being prepared!");
1727 return NULL;
1728 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001729
1730 newRequest->mOutputStreams.push(stream);
1731 }
1732 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001733 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001734
1735 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001736}
1737
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001738bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1739 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1740 Size size = mSupportedOpaqueInputSizes[i];
1741 if (size.width == width && size.height == height) {
1742 return true;
1743 }
1744 }
1745
1746 return false;
1747}
1748
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001749status_t Camera3Device::configureStreamsLocked() {
1750 ATRACE_CALL();
1751 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001752
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001753 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001754 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001755 return INVALID_OPERATION;
1756 }
1757
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001758 if (!mNeedConfig) {
1759 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1760 return OK;
1761 }
1762
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001763 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1764 // adding a dummy stream instead.
1765 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1766 if (mOutputStreams.size() == 0) {
1767 addDummyStreamLocked();
1768 } else {
1769 tryRemoveDummyStreamLocked();
1770 }
1771
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001772 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001773 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001774
1775 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001776 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1777 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1778 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001779 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1780
1781 Vector<camera3_stream_t*> streams;
1782 streams.setCapacity(config.num_streams);
1783
1784 if (mInputStream != NULL) {
1785 camera3_stream_t *inputStream;
1786 inputStream = mInputStream->startConfiguration();
1787 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001788 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001789 return INVALID_OPERATION;
1790 }
1791 streams.add(inputStream);
1792 }
1793
1794 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001795
1796 // Don't configure bidi streams twice, nor add them twice to the list
1797 if (mOutputStreams[i].get() ==
1798 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1799
1800 config.num_streams--;
1801 continue;
1802 }
1803
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001804 camera3_stream_t *outputStream;
1805 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1806 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001807 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001808 return INVALID_OPERATION;
1809 }
1810 streams.add(outputStream);
1811 }
1812
1813 config.streams = streams.editArray();
1814
1815 // Do the HAL configuration; will potentially touch stream
1816 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001817 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001818 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001819 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001820
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001821 if (res == BAD_VALUE) {
1822 // HAL rejected this set of streams as unsupported, clean up config
1823 // attempt and return to unconfigured state
1824 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1825 res = mInputStream->cancelConfiguration();
1826 if (res != OK) {
1827 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1828 mInputStream->getId(), strerror(-res), res);
1829 return res;
1830 }
1831 }
1832
1833 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1834 sp<Camera3OutputStreamInterface> outputStream =
1835 mOutputStreams.editValueAt(i);
1836 if (outputStream->isConfiguring()) {
1837 res = outputStream->cancelConfiguration();
1838 if (res != OK) {
1839 SET_ERR_L(
1840 "Can't cancel configuring output stream %d: %s (%d)",
1841 outputStream->getId(), strerror(-res), res);
1842 return res;
1843 }
1844 }
1845 }
1846
1847 // Return state to that at start of call, so that future configures
1848 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001849 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001850 mNeedConfig = true;
1851
1852 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1853 return BAD_VALUE;
1854 } else if (res != OK) {
1855 // Some other kind of error from configure_streams - this is not
1856 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001857 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1858 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001859 return res;
1860 }
1861
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001862 // Finish all stream configuration immediately.
1863 // TODO: Try to relax this later back to lazy completion, which should be
1864 // faster
1865
Igor Murashkin073f8572013-05-02 14:59:28 -07001866 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001867 res = mInputStream->finishConfiguration(mHal3Device);
1868 if (res != OK) {
1869 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1870 mInputStream->getId(), strerror(-res), res);
1871 return res;
1872 }
1873 }
1874
1875 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001876 sp<Camera3OutputStreamInterface> outputStream =
1877 mOutputStreams.editValueAt(i);
1878 if (outputStream->isConfiguring()) {
1879 res = outputStream->finishConfiguration(mHal3Device);
1880 if (res != OK) {
1881 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1882 outputStream->getId(), strerror(-res), res);
1883 return res;
1884 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001885 }
1886 }
1887
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001888 // Request thread needs to know to avoid using repeat-last-settings protocol
1889 // across configure_streams() calls
1890 mRequestThread->configurationComplete();
1891
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07001892 // Boost priority of request thread for high speed recording to SCHED_FIFO
1893 if (mIsConstrainedHighSpeedConfiguration) {
1894 pid_t requestThreadTid = mRequestThread->getTid();
1895 res = requestPriority(getpid(), requestThreadTid,
1896 kConstrainedHighSpeedThreadPriority, true);
1897 if (res != OK) {
1898 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
1899 strerror(-res), res);
1900 } else {
1901 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
1902 }
1903 } else {
1904 // TODO: Set/restore normal priority for normal use cases
1905 }
1906
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001907 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001908
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001909 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001910
Ruben Brunk183f0562015-08-12 12:55:02 -07001911 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1912 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001913
1914 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1915
Zhijun He0a210512014-07-24 13:45:15 -07001916 // tear down the deleted streams after configure streams.
1917 mDeletedStreams.clear();
1918
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001919 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001920}
1921
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001922status_t Camera3Device::addDummyStreamLocked() {
1923 ATRACE_CALL();
1924 status_t res;
1925
1926 if (mDummyStreamId != NO_STREAM) {
1927 // Should never be adding a second dummy stream when one is already
1928 // active
1929 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1930 __FUNCTION__, mId);
1931 return INVALID_OPERATION;
1932 }
1933
1934 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1935
1936 sp<Camera3OutputStreamInterface> dummyStream =
1937 new Camera3DummyStream(mNextStreamId);
1938
1939 res = mOutputStreams.add(mNextStreamId, dummyStream);
1940 if (res < 0) {
1941 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1942 return res;
1943 }
1944
1945 mDummyStreamId = mNextStreamId;
1946 mNextStreamId++;
1947
1948 return OK;
1949}
1950
1951status_t Camera3Device::tryRemoveDummyStreamLocked() {
1952 ATRACE_CALL();
1953 status_t res;
1954
1955 if (mDummyStreamId == NO_STREAM) return OK;
1956 if (mOutputStreams.size() == 1) return OK;
1957
1958 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1959
1960 // Ok, have a dummy stream and there's at least one other output stream,
1961 // so remove the dummy
1962
1963 sp<Camera3StreamInterface> deletedStream;
1964 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1965 if (outputStreamIdx == NAME_NOT_FOUND) {
1966 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1967 return INVALID_OPERATION;
1968 }
1969
1970 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1971 mOutputStreams.removeItemsAt(outputStreamIdx);
1972
1973 // Free up the stream endpoint so that it can be used by some other stream
1974 res = deletedStream->disconnect();
1975 if (res != OK) {
1976 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1977 // fall through since we want to still list the stream as deleted.
1978 }
1979 mDeletedStreams.add(deletedStream);
1980 mDummyStreamId = NO_STREAM;
1981
1982 return res;
1983}
1984
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001985void Camera3Device::setErrorState(const char *fmt, ...) {
1986 Mutex::Autolock l(mLock);
1987 va_list args;
1988 va_start(args, fmt);
1989
1990 setErrorStateLockedV(fmt, args);
1991
1992 va_end(args);
1993}
1994
1995void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1996 Mutex::Autolock l(mLock);
1997 setErrorStateLockedV(fmt, args);
1998}
1999
2000void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2001 va_list args;
2002 va_start(args, fmt);
2003
2004 setErrorStateLockedV(fmt, args);
2005
2006 va_end(args);
2007}
2008
2009void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002010 // Print out all error messages to log
2011 String8 errorCause = String8::formatV(fmt, args);
2012 ALOGE("Camera %d: %s", mId, errorCause.string());
2013
2014 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002015 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002016
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002017 mErrorCause = errorCause;
2018
2019 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002020 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002021
2022 // Notify upstream about a device error
2023 if (mListener != NULL) {
2024 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2025 CaptureResultExtras());
2026 }
2027
2028 // Save stack trace. View by dumping it later.
2029 CameraTraces::saveTrace();
2030 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002031}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002032
2033/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002034 * In-flight request management
2035 */
2036
Jianing Weicb0652e2014-03-12 18:29:36 -07002037status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002038 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2039 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002040 ATRACE_CALL();
2041 Mutex::Autolock l(mInFlightLock);
2042
2043 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002044 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2045 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002046 if (res < 0) return res;
2047
2048 return OK;
2049}
2050
2051/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002052 * Check if all 3A fields are ready, and send off a partial 3A-only result
2053 * to the output frame queue
2054 */
Zhijun He204e3292014-07-14 17:09:23 -07002055bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07002056 uint32_t frameNumber,
2057 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002058
2059 // Check if all 3A states are present
2060 // The full list of fields is
2061 // android.control.afMode
2062 // android.control.awbMode
2063 // android.control.aeState
2064 // android.control.awbState
2065 // android.control.afState
2066 // android.control.afTriggerID
2067 // android.control.aePrecaptureID
2068 // TODO: Add android.control.aeMode
2069
2070 bool gotAllStates = true;
2071
2072 uint8_t afMode;
2073 uint8_t awbMode;
2074 uint8_t aeState;
2075 uint8_t afState;
2076 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002077
2078 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
2079 &afMode, frameNumber);
2080
2081 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
2082 &awbMode, frameNumber);
2083
2084 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
2085 &aeState, frameNumber);
2086
2087 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
2088 &afState, frameNumber);
2089
2090 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
2091 &awbState, frameNumber);
2092
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002093 if (!gotAllStates) return false;
2094
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002095 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002096 "AF state %d, AE state %d, AWB state %d, "
2097 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002098 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002099 afMode, awbMode,
2100 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002101 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002102
2103 // Got all states, so construct a minimal result to send
2104 // In addition to the above fields, this means adding in
2105 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002106 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07002107 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002108
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002109 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002110
2111 Mutex::Autolock l(mOutputLock);
2112
Jianing Weicb0652e2014-03-12 18:29:36 -07002113 CaptureResult captureResult;
2114 captureResult.mResultExtras = resultExtras;
2115 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
2116 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
2117 // but not limited to CameraDeviceBase::getNextResult
2118 CaptureResult& min3AResult =
2119 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002120
Jianing Weicb0652e2014-03-12 18:29:36 -07002121 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
2122 // TODO: This is problematic casting. Need to fix CameraMetadata.
2123 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002124 return false;
2125 }
2126
Jianing Weicb0652e2014-03-12 18:29:36 -07002127 int32_t requestId = resultExtras.requestId;
2128 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002129 &requestId, frameNumber)) {
2130 return false;
2131 }
2132
Zhijun He204e3292014-07-14 17:09:23 -07002133 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
2134 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
2135 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
2136 &partialResult, frameNumber)) {
2137 return false;
2138 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002139 }
2140
Jianing Weicb0652e2014-03-12 18:29:36 -07002141 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002142 &afMode, frameNumber)) {
2143 return false;
2144 }
2145
Jianing Weicb0652e2014-03-12 18:29:36 -07002146 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002147 &awbMode, frameNumber)) {
2148 return false;
2149 }
2150
Jianing Weicb0652e2014-03-12 18:29:36 -07002151 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002152 &aeState, frameNumber)) {
2153 return false;
2154 }
2155
Jianing Weicb0652e2014-03-12 18:29:36 -07002156 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002157 &afState, frameNumber)) {
2158 return false;
2159 }
2160
Jianing Weicb0652e2014-03-12 18:29:36 -07002161 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002162 &awbState, frameNumber)) {
2163 return false;
2164 }
2165
Jianing Weicb0652e2014-03-12 18:29:36 -07002166 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002167 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002168 return false;
2169 }
2170
Jianing Weicb0652e2014-03-12 18:29:36 -07002171 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002172 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002173 return false;
2174 }
2175
Zhijun He204e3292014-07-14 17:09:23 -07002176 // We only send the aggregated partial when all 3A related metadata are available
2177 // For both API1 and API2.
2178 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002179 mResultSignal.signal();
2180
2181 return true;
2182}
2183
2184template<typename T>
2185bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002186 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002187 (void) frameNumber;
2188
2189 camera_metadata_ro_entry_t entry;
2190
2191 entry = result.find(tag);
2192 if (entry.count == 0) {
2193 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2194 mId, frameNumber, get_camera_metadata_tag_name(tag));
2195 return false;
2196 }
2197
2198 if (sizeof(T) == sizeof(uint8_t)) {
2199 *value = entry.data.u8[0];
2200 } else if (sizeof(T) == sizeof(int32_t)) {
2201 *value = entry.data.i32[0];
2202 } else {
2203 ALOGE("%s: Unexpected type", __FUNCTION__);
2204 return false;
2205 }
2206 return true;
2207}
2208
2209template<typename T>
2210bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002211 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002212 if (result.update(tag, value, 1) != NO_ERROR) {
2213 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2214 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2215 frameNumber, get_camera_metadata_tag_name(tag));
2216 return false;
2217 }
2218 return true;
2219}
2220
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002221void Camera3Device::returnOutputBuffers(
2222 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2223 nsecs_t timestamp) {
2224 for (size_t i = 0; i < numBuffers; i++)
2225 {
2226 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2227 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2228 // Note: stream may be deallocated at this point, if this buffer was
2229 // the last reference to it.
2230 if (res != OK) {
2231 ALOGE("Can't return buffer to its stream: %s (%d)",
2232 strerror(-res), res);
2233 }
2234 }
2235}
2236
2237
2238void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2239
2240 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2241 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2242
2243 nsecs_t sensorTimestamp = request.sensorTimestamp;
2244 nsecs_t shutterTimestamp = request.shutterTimestamp;
2245
2246 // Check if it's okay to remove the request from InFlightMap:
2247 // In the case of a successful request:
2248 // all input and output buffers, all result metadata, shutter callback
2249 // arrived.
2250 // In the case of a unsuccessful request:
2251 // all input and output buffers arrived.
2252 if (request.numBuffersLeft == 0 &&
2253 (request.requestStatus != OK ||
2254 (request.haveResultMetadata && shutterTimestamp != 0))) {
2255 ATRACE_ASYNC_END("frame capture", frameNumber);
2256
2257 // Sanity check - if sensor timestamp matches shutter timestamp
2258 if (request.requestStatus == OK &&
2259 sensorTimestamp != shutterTimestamp) {
2260 SET_ERR("sensor timestamp (%" PRId64
2261 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2262 sensorTimestamp, frameNumber, shutterTimestamp);
2263 }
2264
2265 // for an unsuccessful request, it may have pending output buffers to
2266 // return.
2267 assert(request.requestStatus != OK ||
2268 request.pendingOutputBuffers.size() == 0);
2269 returnOutputBuffers(request.pendingOutputBuffers.array(),
2270 request.pendingOutputBuffers.size(), 0);
2271
2272 mInFlightMap.removeItemsAt(idx, 1);
2273
2274 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2275 }
2276
2277 // Sanity check - if we have too many in-flight frames, something has
2278 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002279 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002280 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002281 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2282 kInFlightWarnLimitHighSpeed) {
2283 CLOGE("In-flight list too large for high speed configuration: %zu",
2284 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002285 }
2286}
2287
2288
2289void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2290 CaptureResultExtras &resultExtras,
2291 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002292 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002293 bool reprocess,
2294 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002295 if (pendingMetadata.isEmpty())
2296 return;
2297
2298 Mutex::Autolock l(mOutputLock);
2299
2300 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002301 if (reprocess) {
2302 if (frameNumber < mNextReprocessResultFrameNumber) {
2303 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002304 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002305 frameNumber, mNextReprocessResultFrameNumber);
2306 return;
2307 }
2308 mNextReprocessResultFrameNumber = frameNumber + 1;
2309 } else {
2310 if (frameNumber < mNextResultFrameNumber) {
2311 SET_ERR("Out-of-order capture result metadata submitted! "
2312 "(got frame number %d, expecting %d)",
2313 frameNumber, mNextResultFrameNumber);
2314 return;
2315 }
2316 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002317 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002318
2319 CaptureResult captureResult;
2320 captureResult.mResultExtras = resultExtras;
2321 captureResult.mMetadata = pendingMetadata;
2322
2323 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2324 (int32_t*)&frameNumber, 1) != OK) {
2325 SET_ERR("Failed to set frame# in metadata (%d)",
2326 frameNumber);
2327 return;
2328 } else {
2329 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2330 __FUNCTION__, mId, frameNumber);
2331 }
2332
2333 // Append any previous partials to form a complete result
2334 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2335 captureResult.mMetadata.append(collectedPartialResult);
2336 }
2337
2338 captureResult.mMetadata.sort();
2339
2340 // Check that there's a timestamp in the result metadata
2341 camera_metadata_entry entry =
2342 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2343 if (entry.count == 0) {
2344 SET_ERR("No timestamp provided by HAL for frame %d!",
2345 frameNumber);
2346 return;
2347 }
2348
Chien-Yu Chend196d612015-06-22 19:49:01 -07002349 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2350
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002351 // Valid result, insert into queue
2352 List<CaptureResult>::iterator queuedResult =
2353 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2354 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2355 ", burstId = %" PRId32, __FUNCTION__,
2356 queuedResult->mResultExtras.requestId,
2357 queuedResult->mResultExtras.frameNumber,
2358 queuedResult->mResultExtras.burstId);
2359
2360 mResultSignal.signal();
2361}
2362
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002363/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002364 * Camera HAL device callback methods
2365 */
2366
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002367void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002368 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002369
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002370 status_t res;
2371
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002372 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002373 if (result->result == NULL && result->num_output_buffers == 0 &&
2374 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002375 SET_ERR("No result data provided by HAL for frame %d",
2376 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002377 return;
2378 }
Zhijun He204e3292014-07-14 17:09:23 -07002379
2380 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2381 // partial_result to 1 when metadata is included in this result.
2382 if (!mUsePartialResult &&
2383 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2384 result->result != NULL &&
2385 result->partial_result != 1) {
2386 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2387 " if partial result is not supported",
2388 frameNumber, result->partial_result);
2389 return;
2390 }
2391
2392 bool isPartialResult = false;
2393 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002394 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002395 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002396
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002397 // Get shutter timestamp and resultExtras from list of in-flight requests,
2398 // where it was added by the shutter notification for this frame. If the
2399 // shutter timestamp isn't received yet, append the output buffers to the
2400 // in-flight request and they will be returned when the shutter timestamp
2401 // arrives. Update the in-flight status and remove the in-flight entry if
2402 // all result data and shutter timestamp have been received.
2403 nsecs_t shutterTimestamp = 0;
2404
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002405 {
2406 Mutex::Autolock l(mInFlightLock);
2407 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2408 if (idx == NAME_NOT_FOUND) {
2409 SET_ERR("Unknown frame number for capture result: %d",
2410 frameNumber);
2411 return;
2412 }
2413 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002414 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2415 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2416 ", partialResultCount = %d",
2417 __FUNCTION__, request.resultExtras.requestId,
2418 request.resultExtras.frameNumber, request.resultExtras.burstId,
2419 result->partial_result);
2420 // Always update the partial count to the latest one if it's not 0
2421 // (buffers only). When framework aggregates adjacent partial results
2422 // into one, the latest partial count will be used.
2423 if (result->partial_result != 0)
2424 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002425
2426 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002427 if (mUsePartialResult && result->result != NULL) {
2428 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2429 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2430 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2431 " the range of [1, %d] when metadata is included in the result",
2432 frameNumber, result->partial_result, mNumPartialResults);
2433 return;
2434 }
2435 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002436 if (isPartialResult) {
2437 request.partialResult.collectedResult.append(result->result);
2438 }
Zhijun He204e3292014-07-14 17:09:23 -07002439 } else {
2440 camera_metadata_ro_entry_t partialResultEntry;
2441 res = find_camera_metadata_ro_entry(result->result,
2442 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2443 if (res != NAME_NOT_FOUND &&
2444 partialResultEntry.count > 0 &&
2445 partialResultEntry.data.u8[0] ==
2446 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2447 // A partial result. Flag this as such, and collect this
2448 // set of metadata into the in-flight entry.
2449 isPartialResult = true;
2450 request.partialResult.collectedResult.append(
2451 result->result);
2452 request.partialResult.collectedResult.erase(
2453 ANDROID_QUIRKS_PARTIAL_RESULT);
2454 }
2455 }
2456
2457 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002458 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002459 if (!request.partialResult.haveSent3A) {
2460 request.partialResult.haveSent3A =
2461 processPartial3AResult(frameNumber,
2462 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002463 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002464 }
2465 }
2466 }
2467
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002468 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002469 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002470
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002471 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002472 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002473 if (request.haveResultMetadata) {
2474 SET_ERR("Called multiple times with metadata for frame %d",
2475 frameNumber);
2476 return;
2477 }
Zhijun He204e3292014-07-14 17:09:23 -07002478 if (mUsePartialResult &&
2479 !request.partialResult.collectedResult.isEmpty()) {
2480 collectedPartialResult.acquire(
2481 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002482 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002483 request.haveResultMetadata = true;
2484 }
2485
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002486 uint32_t numBuffersReturned = result->num_output_buffers;
2487 if (result->input_buffer != NULL) {
2488 if (hasInputBufferInRequest) {
2489 numBuffersReturned += 1;
2490 } else {
2491 ALOGW("%s: Input buffer should be NULL if there is no input"
2492 " buffer sent in the request",
2493 __FUNCTION__);
2494 }
2495 }
2496 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002497 if (request.numBuffersLeft < 0) {
2498 SET_ERR("Too many buffers returned for frame %d",
2499 frameNumber);
2500 return;
2501 }
2502
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002503 camera_metadata_ro_entry_t entry;
2504 res = find_camera_metadata_ro_entry(result->result,
2505 ANDROID_SENSOR_TIMESTAMP, &entry);
2506 if (res == OK && entry.count == 1) {
2507 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002508 }
2509
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002510 // If shutter event isn't received yet, append the output buffers to
2511 // the in-flight request. Otherwise, return the output buffers to
2512 // streams.
2513 if (shutterTimestamp == 0) {
2514 request.pendingOutputBuffers.appendArray(result->output_buffers,
2515 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002516 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002517 returnOutputBuffers(result->output_buffers,
2518 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002519 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002520
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002521 if (result->result != NULL && !isPartialResult) {
2522 if (shutterTimestamp == 0) {
2523 request.pendingMetadata = result->result;
2524 request.partialResult.collectedResult = collectedPartialResult;
2525 } else {
2526 CameraMetadata metadata;
2527 metadata = result->result;
2528 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002529 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2530 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002531 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002532 }
2533
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002534 removeInFlightRequestIfReadyLocked(idx);
2535 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002536
Zhijun Hef0d962a2014-06-30 10:24:11 -07002537 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002538 if (hasInputBufferInRequest) {
2539 Camera3Stream *stream =
2540 Camera3Stream::cast(result->input_buffer->stream);
2541 res = stream->returnInputBuffer(*(result->input_buffer));
2542 // Note: stream may be deallocated at this point, if this buffer was the
2543 // last reference to it.
2544 if (res != OK) {
2545 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2546 " its stream:%s (%d)", __FUNCTION__,
2547 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002548 }
2549 } else {
2550 ALOGW("%s: Input buffer should be NULL if there is no input"
2551 " buffer sent in the request, skipping input buffer return.",
2552 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002553 }
2554 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002555}
2556
2557void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002558 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002559 NotificationListener *listener;
2560 {
2561 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002562 listener = mListener;
2563 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002564
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002565 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002566 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002567 return;
2568 }
2569
2570 switch (msg->type) {
2571 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002572 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002573 break;
2574 }
2575 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002576 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002577 break;
2578 }
2579 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002580 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002581 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002582 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002583}
2584
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002585void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2586 NotificationListener *listener) {
2587
2588 // Map camera HAL error codes to ICameraDeviceCallback error codes
2589 // Index into this with the HAL error code
2590 static const ICameraDeviceCallbacks::CameraErrorCode
2591 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2592 // 0 = Unused error code
2593 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2594 // 1 = CAMERA3_MSG_ERROR_DEVICE
2595 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2596 // 2 = CAMERA3_MSG_ERROR_REQUEST
2597 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2598 // 3 = CAMERA3_MSG_ERROR_RESULT
2599 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2600 // 4 = CAMERA3_MSG_ERROR_BUFFER
2601 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2602 };
2603
2604 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2605 ((msg.error_code >= 0) &&
2606 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2607 halErrorMap[msg.error_code] :
2608 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2609
2610 int streamId = 0;
2611 if (msg.error_stream != NULL) {
2612 Camera3Stream *stream =
2613 Camera3Stream::cast(msg.error_stream);
2614 streamId = stream->getId();
2615 }
2616 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2617 mId, __FUNCTION__, msg.frame_number,
2618 streamId, msg.error_code);
2619
2620 CaptureResultExtras resultExtras;
2621 switch (errorCode) {
2622 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2623 // SET_ERR calls notifyError
2624 SET_ERR("Camera HAL reported serious device error");
2625 break;
2626 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2627 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2628 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2629 {
2630 Mutex::Autolock l(mInFlightLock);
2631 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2632 if (idx >= 0) {
2633 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2634 r.requestStatus = msg.error_code;
2635 resultExtras = r.resultExtras;
2636 } else {
2637 resultExtras.frameNumber = msg.frame_number;
2638 ALOGE("Camera %d: %s: cannot find in-flight request on "
2639 "frame %" PRId64 " error", mId, __FUNCTION__,
2640 resultExtras.frameNumber);
2641 }
2642 }
2643 if (listener != NULL) {
2644 listener->notifyError(errorCode, resultExtras);
2645 } else {
2646 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2647 }
2648 break;
2649 default:
2650 // SET_ERR calls notifyError
2651 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2652 break;
2653 }
2654}
2655
2656void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2657 NotificationListener *listener) {
2658 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002659
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002660 // Set timestamp for the request in the in-flight tracking
2661 // and get the request ID to send upstream
2662 {
2663 Mutex::Autolock l(mInFlightLock);
2664 idx = mInFlightMap.indexOfKey(msg.frame_number);
2665 if (idx >= 0) {
2666 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002667
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002668 // Verify ordering of shutter notifications
2669 {
2670 Mutex::Autolock l(mOutputLock);
2671 // TODO: need to track errors for tighter bounds on expected frame number.
2672 if (r.hasInputBuffer) {
2673 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2674 SET_ERR("Shutter notification out-of-order. Expected "
2675 "notification for frame %d, got frame %d",
2676 mNextReprocessShutterFrameNumber, msg.frame_number);
2677 return;
2678 }
2679 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2680 } else {
2681 if (msg.frame_number < mNextShutterFrameNumber) {
2682 SET_ERR("Shutter notification out-of-order. Expected "
2683 "notification for frame %d, got frame %d",
2684 mNextShutterFrameNumber, msg.frame_number);
2685 return;
2686 }
2687 mNextShutterFrameNumber = msg.frame_number + 1;
2688 }
2689 }
2690
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002691 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2692 mId, __FUNCTION__,
2693 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2694 // Call listener, if any
2695 if (listener != NULL) {
2696 listener->notifyShutter(r.resultExtras, msg.timestamp);
2697 }
2698
2699 r.shutterTimestamp = msg.timestamp;
2700
2701 // send pending result and buffers
2702 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002703 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002704 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002705 returnOutputBuffers(r.pendingOutputBuffers.array(),
2706 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2707 r.pendingOutputBuffers.clear();
2708
2709 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002710 }
2711 }
2712 if (idx < 0) {
2713 SET_ERR("Shutter notification for non-existent frame number %d",
2714 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002715 }
2716}
2717
2718
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002719CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002720 ALOGV("%s", __FUNCTION__);
2721
Igor Murashkin1e479c02013-09-06 16:55:14 -07002722 CameraMetadata retVal;
2723
2724 if (mRequestThread != NULL) {
2725 retVal = mRequestThread->getLatestRequest();
2726 }
2727
Igor Murashkin1e479c02013-09-06 16:55:14 -07002728 return retVal;
2729}
2730
Jianing Weicb0652e2014-03-12 18:29:36 -07002731
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002732/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002733 * RequestThread inner class methods
2734 */
2735
2736Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002737 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002738 camera3_device_t *hal3Device,
2739 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002740 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002741 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002742 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002743 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002744 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002745 mReconfigured(false),
2746 mDoPause(false),
2747 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002748 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002749 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002750 mCurrentAfTriggerId(0),
2751 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002752 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2753 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002754 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002755}
2756
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002757void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002758 NotificationListener *listener) {
2759 Mutex::Autolock l(mRequestLock);
2760 mListener = listener;
2761}
2762
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002763void Camera3Device::RequestThread::configurationComplete() {
2764 Mutex::Autolock l(mRequestLock);
2765 mReconfigured = true;
2766}
2767
Jianing Wei90e59c92014-03-12 18:29:36 -07002768status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002769 List<sp<CaptureRequest> > &requests,
2770 /*out*/
2771 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002772 Mutex::Autolock l(mRequestLock);
2773 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2774 ++it) {
2775 mRequestQueue.push_back(*it);
2776 }
2777
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002778 if (lastFrameNumber != NULL) {
2779 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2780 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2781 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2782 *lastFrameNumber);
2783 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002784
Jianing Wei90e59c92014-03-12 18:29:36 -07002785 unpauseForNewRequests();
2786
2787 return OK;
2788}
2789
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002790
2791status_t Camera3Device::RequestThread::queueTrigger(
2792 RequestTrigger trigger[],
2793 size_t count) {
2794
2795 Mutex::Autolock l(mTriggerMutex);
2796 status_t ret;
2797
2798 for (size_t i = 0; i < count; ++i) {
2799 ret = queueTriggerLocked(trigger[i]);
2800
2801 if (ret != OK) {
2802 return ret;
2803 }
2804 }
2805
2806 return OK;
2807}
2808
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002809int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2810 sp<Camera3Device> d = device.promote();
2811 if (d != NULL) return d->mId;
2812 return 0;
2813}
2814
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002815status_t Camera3Device::RequestThread::queueTriggerLocked(
2816 RequestTrigger trigger) {
2817
2818 uint32_t tag = trigger.metadataTag;
2819 ssize_t index = mTriggerMap.indexOfKey(tag);
2820
2821 switch (trigger.getTagType()) {
2822 case TYPE_BYTE:
2823 // fall-through
2824 case TYPE_INT32:
2825 break;
2826 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002827 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2828 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002829 return INVALID_OPERATION;
2830 }
2831
2832 /**
2833 * Collect only the latest trigger, since we only have 1 field
2834 * in the request settings per trigger tag, and can't send more than 1
2835 * trigger per request.
2836 */
2837 if (index != NAME_NOT_FOUND) {
2838 mTriggerMap.editValueAt(index) = trigger;
2839 } else {
2840 mTriggerMap.add(tag, trigger);
2841 }
2842
2843 return OK;
2844}
2845
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002846status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002847 const RequestList &requests,
2848 /*out*/
2849 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002850 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002851 if (lastFrameNumber != NULL) {
2852 *lastFrameNumber = mRepeatingLastFrameNumber;
2853 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002854 mRepeatingRequests.clear();
2855 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2856 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002857
2858 unpauseForNewRequests();
2859
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002860 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002861 return OK;
2862}
2863
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002864bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2865 if (mRepeatingRequests.empty()) {
2866 return false;
2867 }
2868 int32_t requestId = requestIn->mResultExtras.requestId;
2869 const RequestList &repeatRequests = mRepeatingRequests;
2870 // All repeating requests are guaranteed to have same id so only check first quest
2871 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2872 return (firstRequest->mResultExtras.requestId == requestId);
2873}
2874
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002875status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002876 Mutex::Autolock l(mRequestLock);
2877 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002878 if (lastFrameNumber != NULL) {
2879 *lastFrameNumber = mRepeatingLastFrameNumber;
2880 }
2881 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002882 return OK;
2883}
2884
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002885status_t Camera3Device::RequestThread::clear(
2886 NotificationListener *listener,
2887 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002888 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002889 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002890
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002891 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002892
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002893 // Send errors for all requests pending in the request queue, including
2894 // pending repeating requests
2895 if (listener != NULL) {
2896 for (RequestList::iterator it = mRequestQueue.begin();
2897 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002898 // Abort the input buffers for reprocess requests.
2899 if ((*it)->mInputStream != NULL) {
2900 camera3_stream_buffer_t inputBuffer;
2901 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2902 if (res != OK) {
2903 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2904 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2905 } else {
2906 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2907 if (res != OK) {
2908 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2909 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2910 }
2911 }
2912 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002913 // Set the frame number this request would have had, if it
2914 // had been submitted; this frame number will not be reused.
2915 // The requestId and burstId fields were set when the request was
2916 // submitted originally (in convertMetadataListToRequestListLocked)
2917 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2918 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2919 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002920 }
2921 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002922 mRequestQueue.clear();
2923 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002924 if (lastFrameNumber != NULL) {
2925 *lastFrameNumber = mRepeatingLastFrameNumber;
2926 }
2927 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002928 return OK;
2929}
2930
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002931status_t Camera3Device::RequestThread::flush() {
2932 ATRACE_CALL();
2933 Mutex::Autolock l(mFlushLock);
2934
2935 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2936 return mHal3Device->ops->flush(mHal3Device);
2937 }
2938
2939 return -ENOTSUP;
2940}
2941
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002942void Camera3Device::RequestThread::setPaused(bool paused) {
2943 Mutex::Autolock l(mPauseLock);
2944 mDoPause = paused;
2945 mDoPauseSignal.signal();
2946}
2947
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002948status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2949 int32_t requestId, nsecs_t timeout) {
2950 Mutex::Autolock l(mLatestRequestMutex);
2951 status_t res;
2952 while (mLatestRequestId != requestId) {
2953 nsecs_t startTime = systemTime();
2954
2955 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2956 if (res != OK) return res;
2957
2958 timeout -= (systemTime() - startTime);
2959 }
2960
2961 return OK;
2962}
2963
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002964void Camera3Device::RequestThread::requestExit() {
2965 // Call parent to set up shutdown
2966 Thread::requestExit();
2967 // The exit from any possible waits
2968 mDoPauseSignal.signal();
2969 mRequestSignal.signal();
2970}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002971
Chien-Yu Chend196d612015-06-22 19:49:01 -07002972
2973/**
2974 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2975 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2976 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2977 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2978 * request.
2979 */
2980void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2981 request->mAeTriggerCancelOverride.applyAeLock = false;
2982 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2983
2984 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2985 return;
2986 }
2987
2988 camera_metadata_entry_t aePrecaptureTrigger =
2989 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2990 if (aePrecaptureTrigger.count > 0 &&
2991 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2992 // Always override CANCEL to IDLE
2993 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2994 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2995 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2996 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2997 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2998
2999 if (mAeLockAvailable == true) {
3000 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3001 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3002 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3003 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3004 request->mAeTriggerCancelOverride.applyAeLock = true;
3005 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3006 }
3007 }
3008 }
3009}
3010
3011/**
3012 * Override result metadata for cancelling AE precapture trigger applied in
3013 * handleAePrecaptureCancelRequest().
3014 */
3015void Camera3Device::overrideResultForPrecaptureCancel(
3016 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3017 if (aeTriggerCancelOverride.applyAeLock) {
3018 // Only devices <= v3.2 should have this override
3019 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3020 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3021 }
3022
3023 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3024 // Only devices <= v3.2 should have this override
3025 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3026 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3027 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3028 }
3029}
3030
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003031bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003032 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003033 status_t res;
3034
3035 // Handle paused state.
3036 if (waitIfPaused()) {
3037 return true;
3038 }
3039
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003040 // Wait for the next batch of requests.
3041 waitForNextRequestBatch();
3042 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003043 return true;
3044 }
3045
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003046 // Get the latest request ID, if any
3047 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003048 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003049 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003050 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003051 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003052 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003053 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3054 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003055 }
3056
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003057 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003058 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003059 if (res == TIMED_OUT) {
3060 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003061 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003062 return true;
3063 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003064 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003065 return false;
3066 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003067
Zhijun Hecc27e112013-10-03 16:12:43 -07003068 // Inform waitUntilRequestProcessed thread of a new request ID
3069 {
3070 Mutex::Autolock al(mLatestRequestMutex);
3071
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003072 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003073 mLatestRequestSignal.signal();
3074 }
3075
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003076 // Submit a batch of requests to HAL.
3077 // Use flush lock only when submitting multilple requests in a batch.
3078 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3079 // which may take a long time to finish so synchronizing flush() and
3080 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3081 // For now, only synchronize for high speed recording and we should figure something out for
3082 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003083 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003084
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003085 if (useFlushLock) {
3086 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003087 }
3088
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003089 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003090 mNextRequests.size());
3091 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003092 // Submit request and block until ready for next one
3093 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3094 ATRACE_BEGIN("camera3->process_capture_request");
3095 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3096 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003097
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003098 if (res != OK) {
3099 // Should only get a failure here for malformed requests or device-level
3100 // errors, so consider all errors fatal. Bad metadata failures should
3101 // come through notify.
3102 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3103 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3104 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003105 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003106 if (useFlushLock) {
3107 mFlushLock.unlock();
3108 }
3109 return false;
3110 }
3111
3112 // Mark that the request has be submitted successfully.
3113 nextRequest.submitted = true;
3114
3115 // Update the latest request sent to HAL
3116 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3117 Mutex::Autolock al(mLatestRequestMutex);
3118
3119 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3120 mLatestRequest.acquire(cloned);
3121 }
3122
3123 if (nextRequest.halRequest.settings != NULL) {
3124 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3125 }
3126
3127 // Remove any previously queued triggers (after unlock)
3128 res = removeTriggers(mPrevRequest);
3129 if (res != OK) {
3130 SET_ERR("RequestThread: Unable to remove triggers "
3131 "(capture request %d, HAL device: %s (%d)",
3132 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003133 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003134 if (useFlushLock) {
3135 mFlushLock.unlock();
3136 }
3137 return false;
3138 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003139 }
3140
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003141 if (useFlushLock) {
3142 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003143 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003144
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003145 // Unset as current request
3146 {
3147 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003148 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003149 }
3150
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003151 return true;
3152}
3153
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003154status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003155 ATRACE_CALL();
3156
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003157 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003158 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3159 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3160 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3161
3162 // Prepare a request to HAL
3163 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3164
3165 // Insert any queued triggers (before metadata is locked)
3166 status_t res = insertTriggers(captureRequest);
3167
3168 if (res < 0) {
3169 SET_ERR("RequestThread: Unable to insert triggers "
3170 "(capture request %d, HAL device: %s (%d)",
3171 halRequest->frame_number, strerror(-res), res);
3172 return INVALID_OPERATION;
3173 }
3174 int triggerCount = res;
3175 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3176 mPrevTriggers = triggerCount;
3177
3178 // If the request is the same as last, or we had triggers last time
3179 if (mPrevRequest != captureRequest || triggersMixedIn) {
3180 /**
3181 * HAL workaround:
3182 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3183 */
3184 res = addDummyTriggerIds(captureRequest);
3185 if (res != OK) {
3186 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3187 "(capture request %d, HAL device: %s (%d)",
3188 halRequest->frame_number, strerror(-res), res);
3189 return INVALID_OPERATION;
3190 }
3191
3192 /**
3193 * The request should be presorted so accesses in HAL
3194 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3195 */
3196 captureRequest->mSettings.sort();
3197 halRequest->settings = captureRequest->mSettings.getAndLock();
3198 mPrevRequest = captureRequest;
3199 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3200
3201 IF_ALOGV() {
3202 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3203 find_camera_metadata_ro_entry(
3204 halRequest->settings,
3205 ANDROID_CONTROL_AF_TRIGGER,
3206 &e
3207 );
3208 if (e.count > 0) {
3209 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3210 __FUNCTION__,
3211 halRequest->frame_number,
3212 e.data.u8[0]);
3213 }
3214 }
3215 } else {
3216 // leave request.settings NULL to indicate 'reuse latest given'
3217 ALOGVV("%s: Request settings are REUSED",
3218 __FUNCTION__);
3219 }
3220
3221 uint32_t totalNumBuffers = 0;
3222
3223 // Fill in buffers
3224 if (captureRequest->mInputStream != NULL) {
3225 halRequest->input_buffer = &captureRequest->mInputBuffer;
3226 totalNumBuffers += 1;
3227 } else {
3228 halRequest->input_buffer = NULL;
3229 }
3230
3231 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3232 captureRequest->mOutputStreams.size());
3233 halRequest->output_buffers = outputBuffers->array();
3234 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3235 res = captureRequest->mOutputStreams.editItemAt(i)->
3236 getBuffer(&outputBuffers->editItemAt(i));
3237 if (res != OK) {
3238 // Can't get output buffer from gralloc queue - this could be due to
3239 // abandoned queue or other consumer misbehavior, so not a fatal
3240 // error
3241 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3242 " %s (%d)", strerror(-res), res);
3243
3244 return TIMED_OUT;
3245 }
3246 halRequest->num_output_buffers++;
3247 }
3248 totalNumBuffers += halRequest->num_output_buffers;
3249
3250 // Log request in the in-flight queue
3251 sp<Camera3Device> parent = mParent.promote();
3252 if (parent == NULL) {
3253 // Should not happen, and nowhere to send errors to, so just log it
3254 CLOGE("RequestThread: Parent is gone");
3255 return INVALID_OPERATION;
3256 }
3257 res = parent->registerInFlight(halRequest->frame_number,
3258 totalNumBuffers, captureRequest->mResultExtras,
3259 /*hasInput*/halRequest->input_buffer != NULL,
3260 captureRequest->mAeTriggerCancelOverride);
3261 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3262 ", burstId = %" PRId32 ".",
3263 __FUNCTION__,
3264 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3265 captureRequest->mResultExtras.burstId);
3266 if (res != OK) {
3267 SET_ERR("RequestThread: Unable to register new in-flight request:"
3268 " %s (%d)", strerror(-res), res);
3269 return INVALID_OPERATION;
3270 }
3271 }
3272
3273 return OK;
3274}
3275
Igor Murashkin1e479c02013-09-06 16:55:14 -07003276CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3277 Mutex::Autolock al(mLatestRequestMutex);
3278
3279 ALOGV("RequestThread::%s", __FUNCTION__);
3280
3281 return mLatestRequest;
3282}
3283
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003284bool Camera3Device::RequestThread::isStreamPending(
3285 sp<Camera3StreamInterface>& stream) {
3286 Mutex::Autolock l(mRequestLock);
3287
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003288 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003289 if (!nextRequest.submitted) {
3290 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3291 if (stream == s) return true;
3292 }
3293 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003294 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003295 }
3296
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003297 for (const auto& request : mRequestQueue) {
3298 for (const auto& s : request->mOutputStreams) {
3299 if (stream == s) return true;
3300 }
3301 if (stream == request->mInputStream) return true;
3302 }
3303
3304 for (const auto& request : mRepeatingRequests) {
3305 for (const auto& s : request->mOutputStreams) {
3306 if (stream == s) return true;
3307 }
3308 if (stream == request->mInputStream) return true;
3309 }
3310
3311 return false;
3312}
Jianing Weicb0652e2014-03-12 18:29:36 -07003313
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003314void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3315 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003316 return;
3317 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003318
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003319 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003320 // Skip the ones that have been submitted successfully.
3321 if (nextRequest.submitted) {
3322 continue;
3323 }
3324
3325 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3326 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3327 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3328
3329 if (halRequest->settings != NULL) {
3330 captureRequest->mSettings.unlock(halRequest->settings);
3331 }
3332
3333 if (captureRequest->mInputStream != NULL) {
3334 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3335 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3336 }
3337
3338 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3339 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3340 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3341 }
3342
3343 if (sendRequestError) {
3344 Mutex::Autolock l(mRequestLock);
3345 if (mListener != NULL) {
3346 mListener->notifyError(
3347 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3348 captureRequest->mResultExtras);
3349 }
3350 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003351 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003352
3353 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003354 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003355}
3356
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003357void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003358 // Optimized a bit for the simple steady-state case (single repeating
3359 // request), to avoid putting that request in the queue temporarily.
3360 Mutex::Autolock l(mRequestLock);
3361
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003362 assert(mNextRequests.empty());
3363
3364 NextRequest nextRequest;
3365 nextRequest.captureRequest = waitForNextRequestLocked();
3366 if (nextRequest.captureRequest == nullptr) {
3367 return;
3368 }
3369
3370 nextRequest.halRequest = camera3_capture_request_t();
3371 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003372 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003373
3374 // Wait for additional requests
3375 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3376
3377 for (size_t i = 1; i < batchSize; i++) {
3378 NextRequest additionalRequest;
3379 additionalRequest.captureRequest = waitForNextRequestLocked();
3380 if (additionalRequest.captureRequest == nullptr) {
3381 break;
3382 }
3383
3384 additionalRequest.halRequest = camera3_capture_request_t();
3385 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003386 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003387 }
3388
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003389 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003390 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003391 mNextRequests.size(), batchSize);
3392 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003393 }
3394
3395 return;
3396}
3397
3398sp<Camera3Device::CaptureRequest>
3399 Camera3Device::RequestThread::waitForNextRequestLocked() {
3400 status_t res;
3401 sp<CaptureRequest> nextRequest;
3402
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003403 while (mRequestQueue.empty()) {
3404 if (!mRepeatingRequests.empty()) {
3405 // Always atomically enqueue all requests in a repeating request
3406 // list. Guarantees a complete in-sequence set of captures to
3407 // application.
3408 const RequestList &requests = mRepeatingRequests;
3409 RequestList::const_iterator firstRequest =
3410 requests.begin();
3411 nextRequest = *firstRequest;
3412 mRequestQueue.insert(mRequestQueue.end(),
3413 ++firstRequest,
3414 requests.end());
3415 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003416
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003417 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003418
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003419 break;
3420 }
3421
3422 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3423
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003424 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3425 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003426 Mutex::Autolock pl(mPauseLock);
3427 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003428 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003429 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003430 // Let the tracker know
3431 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3432 if (statusTracker != 0) {
3433 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3434 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003435 }
3436 // Stop waiting for now and let thread management happen
3437 return NULL;
3438 }
3439 }
3440
3441 if (nextRequest == NULL) {
3442 // Don't have a repeating request already in hand, so queue
3443 // must have an entry now.
3444 RequestList::iterator firstRequest =
3445 mRequestQueue.begin();
3446 nextRequest = *firstRequest;
3447 mRequestQueue.erase(firstRequest);
3448 }
3449
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003450 // In case we've been unpaused by setPaused clearing mDoPause, need to
3451 // update internal pause state (capture/setRepeatingRequest unpause
3452 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003453 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003454 if (mPaused) {
3455 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3456 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3457 if (statusTracker != 0) {
3458 statusTracker->markComponentActive(mStatusId);
3459 }
3460 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003461 mPaused = false;
3462
3463 // Check if we've reconfigured since last time, and reset the preview
3464 // request if so. Can't use 'NULL request == repeat' across configure calls.
3465 if (mReconfigured) {
3466 mPrevRequest.clear();
3467 mReconfigured = false;
3468 }
3469
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003470 if (nextRequest != NULL) {
3471 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003472 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3473 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003474
3475 // Since RequestThread::clear() removes buffers from the input stream,
3476 // get the right buffer here before unlocking mRequestLock
3477 if (nextRequest->mInputStream != NULL) {
3478 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3479 if (res != OK) {
3480 // Can't get input buffer from gralloc queue - this could be due to
3481 // disconnected queue or other producer misbehavior, so not a fatal
3482 // error
3483 ALOGE("%s: Can't get input buffer, skipping request:"
3484 " %s (%d)", __FUNCTION__, strerror(-res), res);
3485 if (mListener != NULL) {
3486 mListener->notifyError(
3487 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3488 nextRequest->mResultExtras);
3489 }
3490 return NULL;
3491 }
3492 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003493 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003494
3495 handleAePrecaptureCancelRequest(nextRequest);
3496
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003497 return nextRequest;
3498}
3499
3500bool Camera3Device::RequestThread::waitIfPaused() {
3501 status_t res;
3502 Mutex::Autolock l(mPauseLock);
3503 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003504 if (mPaused == false) {
3505 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003506 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3507 // Let the tracker know
3508 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3509 if (statusTracker != 0) {
3510 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3511 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003512 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003513
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003514 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003515 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003516 return true;
3517 }
3518 }
3519 // We don't set mPaused to false here, because waitForNextRequest needs
3520 // to further manage the paused state in case of starvation.
3521 return false;
3522}
3523
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003524void Camera3Device::RequestThread::unpauseForNewRequests() {
3525 // With work to do, mark thread as unpaused.
3526 // If paused by request (setPaused), don't resume, to avoid
3527 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003528 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003529 Mutex::Autolock p(mPauseLock);
3530 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003531 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3532 if (mPaused) {
3533 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3534 if (statusTracker != 0) {
3535 statusTracker->markComponentActive(mStatusId);
3536 }
3537 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003538 mPaused = false;
3539 }
3540}
3541
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003542void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3543 sp<Camera3Device> parent = mParent.promote();
3544 if (parent != NULL) {
3545 va_list args;
3546 va_start(args, fmt);
3547
3548 parent->setErrorStateV(fmt, args);
3549
3550 va_end(args);
3551 }
3552}
3553
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003554status_t Camera3Device::RequestThread::insertTriggers(
3555 const sp<CaptureRequest> &request) {
3556
3557 Mutex::Autolock al(mTriggerMutex);
3558
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003559 sp<Camera3Device> parent = mParent.promote();
3560 if (parent == NULL) {
3561 CLOGE("RequestThread: Parent is gone");
3562 return DEAD_OBJECT;
3563 }
3564
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003565 CameraMetadata &metadata = request->mSettings;
3566 size_t count = mTriggerMap.size();
3567
3568 for (size_t i = 0; i < count; ++i) {
3569 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003570 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003571
3572 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3573 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3574 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003575 if (isAeTrigger) {
3576 request->mResultExtras.precaptureTriggerId = triggerId;
3577 mCurrentPreCaptureTriggerId = triggerId;
3578 } else {
3579 request->mResultExtras.afTriggerId = triggerId;
3580 mCurrentAfTriggerId = triggerId;
3581 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003582 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3583 continue; // Trigger ID tag is deprecated since device HAL 3.2
3584 }
3585 }
3586
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003587 camera_metadata_entry entry = metadata.find(tag);
3588
3589 if (entry.count > 0) {
3590 /**
3591 * Already has an entry for this trigger in the request.
3592 * Rewrite it with our requested trigger value.
3593 */
3594 RequestTrigger oldTrigger = trigger;
3595
3596 oldTrigger.entryValue = entry.data.u8[0];
3597
3598 mTriggerReplacedMap.add(tag, oldTrigger);
3599 } else {
3600 /**
3601 * More typical, no trigger entry, so we just add it
3602 */
3603 mTriggerRemovedMap.add(tag, trigger);
3604 }
3605
3606 status_t res;
3607
3608 switch (trigger.getTagType()) {
3609 case TYPE_BYTE: {
3610 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3611 res = metadata.update(tag,
3612 &entryValue,
3613 /*count*/1);
3614 break;
3615 }
3616 case TYPE_INT32:
3617 res = metadata.update(tag,
3618 &trigger.entryValue,
3619 /*count*/1);
3620 break;
3621 default:
3622 ALOGE("%s: Type not supported: 0x%x",
3623 __FUNCTION__,
3624 trigger.getTagType());
3625 return INVALID_OPERATION;
3626 }
3627
3628 if (res != OK) {
3629 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3630 ", value %d", __FUNCTION__, trigger.getTagName(),
3631 trigger.entryValue);
3632 return res;
3633 }
3634
3635 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3636 trigger.getTagName(),
3637 trigger.entryValue);
3638 }
3639
3640 mTriggerMap.clear();
3641
3642 return count;
3643}
3644
3645status_t Camera3Device::RequestThread::removeTriggers(
3646 const sp<CaptureRequest> &request) {
3647 Mutex::Autolock al(mTriggerMutex);
3648
3649 CameraMetadata &metadata = request->mSettings;
3650
3651 /**
3652 * Replace all old entries with their old values.
3653 */
3654 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3655 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3656
3657 status_t res;
3658
3659 uint32_t tag = trigger.metadataTag;
3660 switch (trigger.getTagType()) {
3661 case TYPE_BYTE: {
3662 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3663 res = metadata.update(tag,
3664 &entryValue,
3665 /*count*/1);
3666 break;
3667 }
3668 case TYPE_INT32:
3669 res = metadata.update(tag,
3670 &trigger.entryValue,
3671 /*count*/1);
3672 break;
3673 default:
3674 ALOGE("%s: Type not supported: 0x%x",
3675 __FUNCTION__,
3676 trigger.getTagType());
3677 return INVALID_OPERATION;
3678 }
3679
3680 if (res != OK) {
3681 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3682 ", trigger value %d", __FUNCTION__,
3683 trigger.getTagName(), trigger.entryValue);
3684 return res;
3685 }
3686 }
3687 mTriggerReplacedMap.clear();
3688
3689 /**
3690 * Remove all new entries.
3691 */
3692 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3693 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3694 status_t res = metadata.erase(trigger.metadataTag);
3695
3696 if (res != OK) {
3697 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3698 ", trigger value %d", __FUNCTION__,
3699 trigger.getTagName(), trigger.entryValue);
3700 return res;
3701 }
3702 }
3703 mTriggerRemovedMap.clear();
3704
3705 return OK;
3706}
3707
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003708status_t Camera3Device::RequestThread::addDummyTriggerIds(
3709 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003710 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003711 static const int32_t dummyTriggerId = 1;
3712 status_t res;
3713
3714 CameraMetadata &metadata = request->mSettings;
3715
3716 // If AF trigger is active, insert a dummy AF trigger ID if none already
3717 // exists
3718 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3719 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3720 if (afTrigger.count > 0 &&
3721 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3722 afId.count == 0) {
3723 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3724 if (res != OK) return res;
3725 }
3726
3727 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3728 // if none already exists
3729 camera_metadata_entry pcTrigger =
3730 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3731 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3732 if (pcTrigger.count > 0 &&
3733 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3734 pcId.count == 0) {
3735 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3736 &dummyTriggerId, 1);
3737 if (res != OK) return res;
3738 }
3739
3740 return OK;
3741}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003742
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003743/**
3744 * PreparerThread inner class methods
3745 */
3746
3747Camera3Device::PreparerThread::PreparerThread() :
3748 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3749}
3750
3751Camera3Device::PreparerThread::~PreparerThread() {
3752 Thread::requestExitAndWait();
3753 if (mCurrentStream != nullptr) {
3754 mCurrentStream->cancelPrepare();
3755 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3756 mCurrentStream.clear();
3757 }
3758 clear();
3759}
3760
Ruben Brunkc78ac262015-08-13 17:58:46 -07003761status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003762 status_t res;
3763
3764 Mutex::Autolock l(mLock);
3765
Ruben Brunkc78ac262015-08-13 17:58:46 -07003766 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003767 if (res == OK) {
3768 // No preparation needed, fire listener right off
3769 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3770 if (mListener) {
3771 mListener->notifyPrepared(stream->getId());
3772 }
3773 return OK;
3774 } else if (res != NOT_ENOUGH_DATA) {
3775 return res;
3776 }
3777
3778 // Need to prepare, start up thread if necessary
3779 if (!mActive) {
3780 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3781 // isn't running
3782 Thread::requestExitAndWait();
3783 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3784 if (res != OK) {
3785 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3786 if (mListener) {
3787 mListener->notifyPrepared(stream->getId());
3788 }
3789 return res;
3790 }
3791 mCancelNow = false;
3792 mActive = true;
3793 ALOGV("%s: Preparer stream started", __FUNCTION__);
3794 }
3795
3796 // queue up the work
3797 mPendingStreams.push_back(stream);
3798 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3799
3800 return OK;
3801}
3802
3803status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003804 Mutex::Autolock l(mLock);
3805
3806 for (const auto& stream : mPendingStreams) {
3807 stream->cancelPrepare();
3808 }
3809 mPendingStreams.clear();
3810 mCancelNow = true;
3811
3812 return OK;
3813}
3814
3815void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3816 Mutex::Autolock l(mLock);
3817 mListener = listener;
3818}
3819
3820bool Camera3Device::PreparerThread::threadLoop() {
3821 status_t res;
3822 {
3823 Mutex::Autolock l(mLock);
3824 if (mCurrentStream == nullptr) {
3825 // End thread if done with work
3826 if (mPendingStreams.empty()) {
3827 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3828 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3829 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3830 mActive = false;
3831 return false;
3832 }
3833
3834 // Get next stream to prepare
3835 auto it = mPendingStreams.begin();
3836 mCurrentStream = *it;
3837 mPendingStreams.erase(it);
3838 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3839 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3840 } else if (mCancelNow) {
3841 mCurrentStream->cancelPrepare();
3842 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3843 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3844 mCurrentStream.clear();
3845 mCancelNow = false;
3846 return true;
3847 }
3848 }
3849
3850 res = mCurrentStream->prepareNextBuffer();
3851 if (res == NOT_ENOUGH_DATA) return true;
3852 if (res != OK) {
3853 // Something bad happened; try to recover by cancelling prepare and
3854 // signalling listener anyway
3855 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3856 mCurrentStream->getId(), res, strerror(-res));
3857 mCurrentStream->cancelPrepare();
3858 }
3859
3860 // This stream has finished, notify listener
3861 Mutex::Autolock l(mLock);
3862 if (mListener) {
3863 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3864 mCurrentStream->getId());
3865 mListener->notifyPrepared(mCurrentStream->getId());
3866 }
3867
3868 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3869 mCurrentStream.clear();
3870
3871 return true;
3872}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003873
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003874/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003875 * Static callback forwarding methods from HAL to instance
3876 */
3877
3878void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3879 const camera3_capture_result *result) {
3880 Camera3Device *d =
3881 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003882
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003883 d->processCaptureResult(result);
3884}
3885
3886void Camera3Device::sNotify(const camera3_callback_ops *cb,
3887 const camera3_notify_msg *msg) {
3888 Camera3Device *d =
3889 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3890 d->notify(msg);
3891}
3892
3893}; // namespace android