blob: 0c941fb89453d511861eb1daa0f4993b3957e04c [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 Talvala7b82efe2013-07-25 17:12:35 -070047#include "device3/Camera3Device.h"
48#include "device3/Camera3OutputStream.h"
49#include "device3/Camera3InputStream.h"
50#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070051#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070052#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080053
54using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080055
56namespace android {
57
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058Camera3Device::Camera3Device(int id):
59 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070060 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080061 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070062 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070063 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070064 mUsePartialResult(false),
65 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070066 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070067 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070068 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070069 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080070{
71 ATRACE_CALL();
72 camera3_callback_ops::notify = &sNotify;
73 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
74 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
75}
76
77Camera3Device::~Camera3Device()
78{
79 ATRACE_CALL();
80 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
81 disconnect();
82}
83
Igor Murashkin71381052013-03-04 14:53:08 -080084int Camera3Device::getId() const {
85 return mId;
86}
87
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080088/**
89 * CameraDeviceBase interface
90 */
91
Yin-Chia Yehe074a932015-01-30 10:29:02 -080092status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080093{
94 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070095 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080096 Mutex::Autolock l(mLock);
97
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080098 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080099 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700100 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800101 return INVALID_OPERATION;
102 }
103
104 /** Open HAL device */
105
106 status_t res;
107 String8 deviceName = String8::format("%d", mId);
108
109 camera3_device_t *device;
110
Zhijun He213ce792013-11-19 08:45:15 -0800111 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800112 res = module->open(deviceName.string(),
113 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800114 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800115
116 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700117 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800118 return res;
119 }
120
121 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700122 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700123 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700124 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700125 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800126 device->common.version);
127 device->common.close(&device->common);
128 return BAD_VALUE;
129 }
130
131 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800132 res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700133 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800134 if (res != OK) return res;
135
136 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700137 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
138 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700139 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800140 device->common.close(&device->common);
141 return BAD_VALUE;
142 }
143
144 /** Initialize device with callback functions */
145
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700146 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800147 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700148 ATRACE_END();
149
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800150 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700151 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
152 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800153 device->common.close(&device->common);
154 return BAD_VALUE;
155 }
156
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700157 /** Start up status tracker thread */
158 mStatusTracker = new StatusTracker(this);
159 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
160 if (res != OK) {
161 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
162 strerror(-res), res);
163 device->common.close(&device->common);
164 mStatusTracker.clear();
165 return res;
166 }
167
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700168 bool aeLockAvailable = false;
169 camera_metadata_ro_entry aeLockAvailableEntry;
170 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
171 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
172 if (res == OK && aeLockAvailableEntry.count > 0) {
173 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
174 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
175 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800176
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700177 /** Start up request queue thread */
178 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800179 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800180 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700181 SET_ERR_L("Unable to start request queue thread: %s (%d)",
182 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800183 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800184 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800185 return res;
186 }
187
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700188 mPreparerThread = new PreparerThread();
189
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190 /** Everything is good to go */
191
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700192 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800193 mDeviceInfo = info.static_camera_characteristics;
194 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700195
196 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800197 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700198 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700199 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700200 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800201
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700202 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700203 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
204 camera_metadata_entry partialResultsCount =
205 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
206 if (partialResultsCount.count > 0) {
207 mNumPartialResults = partialResultsCount.data.i32[0];
208 mUsePartialResult = (mNumPartialResults > 1);
209 }
210 } else {
211 camera_metadata_entry partialResultsQuirk =
212 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
213 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
214 mUsePartialResult = true;
215 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700216 }
217
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700218 camera_metadata_entry configs =
219 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
220 for (uint32_t i = 0; i < configs.count; i += 4) {
221 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
222 configs.data.i32[i + 3] ==
223 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
224 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
225 configs.data.i32[i + 2]));
226 }
227 }
228
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800229 return OK;
230}
231
232status_t Camera3Device::disconnect() {
233 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700234 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800235
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800236 ALOGV("%s: E", __FUNCTION__);
237
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700238 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800239
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700240 {
241 Mutex::Autolock l(mLock);
242 if (mStatus == STATUS_UNINITIALIZED) return res;
243
244 if (mStatus == STATUS_ACTIVE ||
245 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
246 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700247 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700248 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700249 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700250 } else {
251 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
252 if (res != OK) {
253 SET_ERR_L("Timeout waiting for HAL to drain");
254 // Continue to close device even in case of error
255 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700256 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800257 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800258
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700259 if (mStatus == STATUS_ERROR) {
260 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700261 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700262
263 if (mStatusTracker != NULL) {
264 mStatusTracker->requestExit();
265 }
266
267 if (mRequestThread != NULL) {
268 mRequestThread->requestExit();
269 }
270
271 mOutputStreams.clear();
272 mInputStream.clear();
273 }
274
275 // Joining done without holding mLock, otherwise deadlocks may ensue
276 // as the threads try to access parent state
277 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
278 // HAL may be in a bad state, so waiting for request thread
279 // (which may be stuck in the HAL processCaptureRequest call)
280 // could be dangerous.
281 mRequestThread->join();
282 }
283
284 if (mStatusTracker != NULL) {
285 mStatusTracker->join();
286 }
287
288 {
289 Mutex::Autolock l(mLock);
290
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800291 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700292 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800293
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700294 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800295 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700296 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800297 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700298 mHal3Device = NULL;
299 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800300
Ruben Brunk183f0562015-08-12 12:55:02 -0700301 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700302 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800303
304 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700305 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800306}
307
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700308// For dumping/debugging only -
309// try to acquire a lock a few times, eventually give up to proceed with
310// debug/dump operations
311bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
312 bool gotLock = false;
313 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
314 if (lock.tryLock() == NO_ERROR) {
315 gotLock = true;
316 break;
317 } else {
318 usleep(kDumpSleepDuration);
319 }
320 }
321 return gotLock;
322}
323
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700324Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
325 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
326 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
327 const int STREAM_CONFIGURATION_SIZE = 4;
328 const int STREAM_FORMAT_OFFSET = 0;
329 const int STREAM_WIDTH_OFFSET = 1;
330 const int STREAM_HEIGHT_OFFSET = 2;
331 const int STREAM_IS_INPUT_OFFSET = 3;
332 camera_metadata_ro_entry_t availableStreamConfigs =
333 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
334 if (availableStreamConfigs.count == 0 ||
335 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
336 return Size(0, 0);
337 }
338
339 // Get max jpeg size (area-wise).
340 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
341 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
342 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
343 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
344 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
345 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
346 && format == HAL_PIXEL_FORMAT_BLOB &&
347 (width * height > maxJpegWidth * maxJpegHeight)) {
348 maxJpegWidth = width;
349 maxJpegHeight = height;
350 }
351 }
352 } else {
353 camera_metadata_ro_entry availableJpegSizes =
354 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
355 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
356 return Size(0, 0);
357 }
358
359 // Get max jpeg size (area-wise).
360 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
361 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
362 > (maxJpegWidth * maxJpegHeight)) {
363 maxJpegWidth = availableJpegSizes.data.i32[i];
364 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
365 }
366 }
367 }
368 return Size(maxJpegWidth, maxJpegHeight);
369}
370
Zhijun Hef7da0962014-04-24 13:27:56 -0700371ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700372 // Get max jpeg size (area-wise).
373 Size maxJpegResolution = getMaxJpegResolution();
374 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700375 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700376 __FUNCTION__, mId);
377 return BAD_VALUE;
378 }
379
Zhijun Hef7da0962014-04-24 13:27:56 -0700380 // Get max jpeg buffer size
381 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700382 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
383 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700384 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
385 return BAD_VALUE;
386 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700387 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800388 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700389
390 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700391 float scaleFactor = ((float) (width * height)) /
392 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800393 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
394 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700395 if (jpegBufferSize > maxJpegBufferSize) {
396 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700397 }
398
399 return jpegBufferSize;
400}
401
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700402ssize_t Camera3Device::getPointCloudBufferSize() const {
403 const int FLOATS_PER_POINT=4;
404 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
405 if (maxPointCount.count == 0) {
406 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
407 __FUNCTION__, mId);
408 return BAD_VALUE;
409 }
410 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
411 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
412 return maxBytesForPointCloud;
413}
414
415
416
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800417status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
418 ATRACE_CALL();
419 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700420
421 // Try to lock, but continue in case of failure (to avoid blocking in
422 // deadlocks)
423 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
424 bool gotLock = tryLockSpinRightRound(mLock);
425
426 ALOGW_IF(!gotInterfaceLock,
427 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
428 mId, __FUNCTION__);
429 ALOGW_IF(!gotLock,
430 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
431 mId, __FUNCTION__);
432
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800433 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800434
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800435 const char *status =
436 mStatus == STATUS_ERROR ? "ERROR" :
437 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700438 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
439 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800440 mStatus == STATUS_ACTIVE ? "ACTIVE" :
441 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700442
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800443 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700444 if (mStatus == STATUS_ERROR) {
445 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
446 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800447 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700448 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700449 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800450
451 if (mInputStream != NULL) {
452 write(fd, lines.string(), lines.size());
453 mInputStream->dump(fd, args);
454 } else {
455 lines.appendFormat(" No input stream.\n");
456 write(fd, lines.string(), lines.size());
457 }
458 for (size_t i = 0; i < mOutputStreams.size(); i++) {
459 mOutputStreams[i]->dump(fd,args);
460 }
461
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700462 lines = String8(" In-flight requests:\n");
463 if (mInFlightMap.size() == 0) {
464 lines.append(" None\n");
465 } else {
466 for (size_t i = 0; i < mInFlightMap.size(); i++) {
467 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700468 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700469 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800470 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700471 r.numBuffersLeft);
472 }
473 }
474 write(fd, lines.string(), lines.size());
475
Igor Murashkin1e479c02013-09-06 16:55:14 -0700476 {
477 lines = String8(" Last request sent:\n");
478 write(fd, lines.string(), lines.size());
479
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700480 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700481 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
482 }
483
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800484 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700485 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800486 write(fd, lines.string(), lines.size());
487 mHal3Device->ops->dump(mHal3Device, fd);
488 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800489
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700490 if (gotLock) mLock.unlock();
491 if (gotInterfaceLock) mInterfaceLock.unlock();
492
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800493 return OK;
494}
495
496const CameraMetadata& Camera3Device::info() const {
497 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800498 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
499 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700500 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800501 mStatus == STATUS_ERROR ?
502 "when in error state" : "before init");
503 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800504 return mDeviceInfo;
505}
506
Jianing Wei90e59c92014-03-12 18:29:36 -0700507status_t Camera3Device::checkStatusOkToCaptureLocked() {
508 switch (mStatus) {
509 case STATUS_ERROR:
510 CLOGE("Device has encountered a serious error");
511 return INVALID_OPERATION;
512 case STATUS_UNINITIALIZED:
513 CLOGE("Device not initialized");
514 return INVALID_OPERATION;
515 case STATUS_UNCONFIGURED:
516 case STATUS_CONFIGURED:
517 case STATUS_ACTIVE:
518 // OK
519 break;
520 default:
521 SET_ERR_L("Unexpected status: %d", mStatus);
522 return INVALID_OPERATION;
523 }
524 return OK;
525}
526
527status_t Camera3Device::convertMetadataListToRequestListLocked(
528 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
529 if (requestList == NULL) {
530 CLOGE("requestList cannot be NULL.");
531 return BAD_VALUE;
532 }
533
Jianing Weicb0652e2014-03-12 18:29:36 -0700534 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700535 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
536 it != metadataList.end(); ++it) {
537 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
538 if (newRequest == 0) {
539 CLOGE("Can't create capture request");
540 return BAD_VALUE;
541 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700542
543 // Setup burst Id and request Id
544 newRequest->mResultExtras.burstId = burstId++;
545 if (it->exists(ANDROID_REQUEST_ID)) {
546 if (it->find(ANDROID_REQUEST_ID).count == 0) {
547 CLOGE("RequestID entry exists; but must not be empty in metadata");
548 return BAD_VALUE;
549 }
550 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
551 } else {
552 CLOGE("RequestID does not exist in metadata");
553 return BAD_VALUE;
554 }
555
Jianing Wei90e59c92014-03-12 18:29:36 -0700556 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700557
558 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700559 }
560 return OK;
561}
562
Jianing Weicb0652e2014-03-12 18:29:36 -0700563status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800564 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800565
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700566 List<const CameraMetadata> requests;
567 requests.push_back(request);
568 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800569}
570
Jianing Wei90e59c92014-03-12 18:29:36 -0700571status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700572 const List<const CameraMetadata> &requests, bool repeating,
573 /*out*/
574 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700575 ATRACE_CALL();
576 Mutex::Autolock il(mInterfaceLock);
577 Mutex::Autolock l(mLock);
578
579 status_t res = checkStatusOkToCaptureLocked();
580 if (res != OK) {
581 // error logged by previous call
582 return res;
583 }
584
585 RequestList requestList;
586
587 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
588 if (res != OK) {
589 // error logged by previous call
590 return res;
591 }
592
593 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700594 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700595 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700596 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700597 }
598
599 if (res == OK) {
600 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
601 if (res != OK) {
602 SET_ERR_L("Can't transition to active in %f seconds!",
603 kActiveTimeout/1e9);
604 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700605 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
606 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700607 } else {
608 CLOGE("Cannot queue request. Impossible.");
609 return BAD_VALUE;
610 }
611
612 return res;
613}
614
Jianing Weicb0652e2014-03-12 18:29:36 -0700615status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
616 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700617 ATRACE_CALL();
618
Jianing Weicb0652e2014-03-12 18:29:36 -0700619 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700620}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800621
Jianing Weicb0652e2014-03-12 18:29:36 -0700622status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
623 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800624 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800625
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700626 List<const CameraMetadata> requests;
627 requests.push_back(request);
628 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800629}
630
Jianing Weicb0652e2014-03-12 18:29:36 -0700631status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
632 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700633 ATRACE_CALL();
634
Jianing Weicb0652e2014-03-12 18:29:36 -0700635 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700636}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800637
638sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
639 const CameraMetadata &request) {
640 status_t res;
641
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700642 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800643 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700644 // Stream configuration failed due to unsupported configuration.
645 // Device back to unconfigured state. Client might try other configuraitons
646 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
647 CLOGE("No streams configured");
648 return NULL;
649 }
650 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800651 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700652 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800653 return NULL;
654 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700655 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700656 if (mStatus == STATUS_UNCONFIGURED) {
657 CLOGE("No streams configured");
658 return NULL;
659 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800660 }
661
662 sp<CaptureRequest> newRequest = createCaptureRequest(request);
663 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800664}
665
Jianing Weicb0652e2014-03-12 18:29:36 -0700666status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800667 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700668 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800669 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800670
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800671 switch (mStatus) {
672 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700673 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800674 return INVALID_OPERATION;
675 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700676 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800677 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700678 case STATUS_UNCONFIGURED:
679 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800680 case STATUS_ACTIVE:
681 // OK
682 break;
683 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700684 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800685 return INVALID_OPERATION;
686 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700687 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700688
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700689 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800690}
691
692status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
693 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700694 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800695
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700696 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800697}
698
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700699status_t Camera3Device::createInputStream(
700 uint32_t width, uint32_t height, int format, int *id) {
701 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700702 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700703 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700704 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
705 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700706
707 status_t res;
708 bool wasActive = false;
709
710 switch (mStatus) {
711 case STATUS_ERROR:
712 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
713 return INVALID_OPERATION;
714 case STATUS_UNINITIALIZED:
715 ALOGE("%s: Device not initialized", __FUNCTION__);
716 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700717 case STATUS_UNCONFIGURED:
718 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700719 // OK
720 break;
721 case STATUS_ACTIVE:
722 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700723 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700724 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700725 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700726 return res;
727 }
728 wasActive = true;
729 break;
730 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700731 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700732 return INVALID_OPERATION;
733 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700734 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700735
736 if (mInputStream != 0) {
737 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
738 return INVALID_OPERATION;
739 }
740
741 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
742 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700743 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700744
745 mInputStream = newStream;
746
747 *id = mNextStreamId++;
748
749 // Continue captures if active at start
750 if (wasActive) {
751 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
752 res = configureStreamsLocked();
753 if (res != OK) {
754 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
755 __FUNCTION__, mNextStreamId, strerror(-res), res);
756 return res;
757 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700758 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700759 }
760
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700761 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700762 return OK;
763}
764
Igor Murashkin2fba5842013-04-22 14:03:54 -0700765
766status_t Camera3Device::createZslStream(
767 uint32_t width, uint32_t height,
768 int depth,
769 /*out*/
770 int *id,
771 sp<Camera3ZslStream>* zslStream) {
772 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700773 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700774 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700775 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
776 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700777
778 status_t res;
779 bool wasActive = false;
780
781 switch (mStatus) {
782 case STATUS_ERROR:
783 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
784 return INVALID_OPERATION;
785 case STATUS_UNINITIALIZED:
786 ALOGE("%s: Device not initialized", __FUNCTION__);
787 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700788 case STATUS_UNCONFIGURED:
789 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700790 // OK
791 break;
792 case STATUS_ACTIVE:
793 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700794 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700795 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700796 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700797 return res;
798 }
799 wasActive = true;
800 break;
801 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700802 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700803 return INVALID_OPERATION;
804 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700805 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700806
807 if (mInputStream != 0) {
808 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
809 return INVALID_OPERATION;
810 }
811
812 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
813 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700814 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700815
816 res = mOutputStreams.add(mNextStreamId, newStream);
817 if (res < 0) {
818 ALOGE("%s: Can't add new stream to set: %s (%d)",
819 __FUNCTION__, strerror(-res), res);
820 return res;
821 }
822 mInputStream = newStream;
823
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530824 mNeedConfig = true;
825
Igor Murashkin2fba5842013-04-22 14:03:54 -0700826 *id = mNextStreamId++;
827 *zslStream = newStream;
828
829 // Continue captures if active at start
830 if (wasActive) {
831 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
832 res = configureStreamsLocked();
833 if (res != OK) {
834 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
835 __FUNCTION__, mNextStreamId, strerror(-res), res);
836 return res;
837 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700838 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700839 }
840
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700841 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700842 return OK;
843}
844
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700845status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800846 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700847 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800848 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700849 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800850 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700851 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
852 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800853
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800854 status_t res;
855 bool wasActive = false;
856
857 switch (mStatus) {
858 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700859 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800860 return INVALID_OPERATION;
861 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700862 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800863 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700864 case STATUS_UNCONFIGURED:
865 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800866 // OK
867 break;
868 case STATUS_ACTIVE:
869 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700870 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800871 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700872 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800873 return res;
874 }
875 wasActive = true;
876 break;
877 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700878 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800879 return INVALID_OPERATION;
880 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700881 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800882
883 sp<Camera3OutputStream> newStream;
884 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700885 ssize_t blobBufferSize;
886 if (dataSpace != HAL_DATASPACE_DEPTH) {
887 blobBufferSize = getJpegBufferSize(width, height);
888 if (blobBufferSize <= 0) {
889 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
890 return BAD_VALUE;
891 }
892 } else {
893 blobBufferSize = getPointCloudBufferSize();
894 if (blobBufferSize <= 0) {
895 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
896 return BAD_VALUE;
897 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700898 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800899 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700900 width, height, blobBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800901 } else {
902 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700903 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800904 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700905 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800906
907 res = mOutputStreams.add(mNextStreamId, newStream);
908 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700909 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800910 return res;
911 }
912
913 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700914 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800915
916 // Continue captures if active at start
917 if (wasActive) {
918 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
919 res = configureStreamsLocked();
920 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700921 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
922 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800923 return res;
924 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700925 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800926 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700927 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800928 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800929}
930
931status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
932 ATRACE_CALL();
933 (void)outputId; (void)id;
934
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700935 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800936 return INVALID_OPERATION;
937}
938
939
940status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700941 uint32_t *width, uint32_t *height,
942 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800943 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700944 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800945 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800946
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800947 switch (mStatus) {
948 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700949 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800950 return INVALID_OPERATION;
951 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700952 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800953 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700954 case STATUS_UNCONFIGURED:
955 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800956 case STATUS_ACTIVE:
957 // OK
958 break;
959 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700960 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800961 return INVALID_OPERATION;
962 }
963
964 ssize_t idx = mOutputStreams.indexOfKey(id);
965 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700966 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800967 return idx;
968 }
969
970 if (width) *width = mOutputStreams[idx]->getWidth();
971 if (height) *height = mOutputStreams[idx]->getHeight();
972 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700973 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800974 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800975}
976
977status_t Camera3Device::setStreamTransform(int id,
978 int transform) {
979 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700980 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800981 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800982
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800983 switch (mStatus) {
984 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700985 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800986 return INVALID_OPERATION;
987 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700988 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800989 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700990 case STATUS_UNCONFIGURED:
991 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800992 case STATUS_ACTIVE:
993 // OK
994 break;
995 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700996 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800997 return INVALID_OPERATION;
998 }
999
1000 ssize_t idx = mOutputStreams.indexOfKey(id);
1001 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001002 CLOGE("Stream %d does not exist",
1003 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001004 return BAD_VALUE;
1005 }
1006
1007 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001008}
1009
1010status_t Camera3Device::deleteStream(int id) {
1011 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001012 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001013 Mutex::Autolock l(mLock);
1014 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001015
Igor Murashkine2172be2013-05-28 15:31:39 -07001016 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1017
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001018 // CameraDevice semantics require device to already be idle before
1019 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001020 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001021 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1022 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001023 }
1024
Igor Murashkin2fba5842013-04-22 14:03:54 -07001025 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001026 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001027 if (mInputStream != NULL && id == mInputStream->getId()) {
1028 deletedStream = mInputStream;
1029 mInputStream.clear();
1030 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001031 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001032 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001033 return BAD_VALUE;
1034 }
Zhijun He5f446352014-01-22 09:49:33 -08001035 }
1036
1037 // Delete output stream or the output part of a bi-directional stream.
1038 if (outputStreamIdx != NAME_NOT_FOUND) {
1039 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001040 mOutputStreams.removeItem(id);
1041 }
1042
1043 // Free up the stream endpoint so that it can be used by some other stream
1044 res = deletedStream->disconnect();
1045 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001046 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001047 // fall through since we want to still list the stream as deleted.
1048 }
1049 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001050 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001051
1052 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001053}
1054
1055status_t Camera3Device::deleteReprocessStream(int id) {
1056 ATRACE_CALL();
1057 (void)id;
1058
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001059 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001060 return INVALID_OPERATION;
1061}
1062
Zhijun He1fa89992015-06-01 15:44:31 -07001063status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001064 ATRACE_CALL();
1065 ALOGV("%s: E", __FUNCTION__);
1066
1067 Mutex::Autolock il(mInterfaceLock);
1068 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001069
1070 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1071 mNeedConfig = true;
1072 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1073 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001074
1075 return configureStreamsLocked();
1076}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001077
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001078status_t Camera3Device::getInputBufferProducer(
1079 sp<IGraphicBufferProducer> *producer) {
1080 Mutex::Autolock il(mInterfaceLock);
1081 Mutex::Autolock l(mLock);
1082
1083 if (producer == NULL) {
1084 return BAD_VALUE;
1085 } else if (mInputStream == NULL) {
1086 return INVALID_OPERATION;
1087 }
1088
1089 return mInputStream->getInputBufferProducer(producer);
1090}
1091
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001092status_t Camera3Device::createDefaultRequest(int templateId,
1093 CameraMetadata *request) {
1094 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001095 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001096 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001097 Mutex::Autolock l(mLock);
1098
1099 switch (mStatus) {
1100 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001101 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001102 return INVALID_OPERATION;
1103 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001104 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001105 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001106 case STATUS_UNCONFIGURED:
1107 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001108 case STATUS_ACTIVE:
1109 // OK
1110 break;
1111 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001112 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001113 return INVALID_OPERATION;
1114 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001115
Zhijun Hea1530f12014-09-14 12:44:20 -07001116 if (!mRequestTemplateCache[templateId].isEmpty()) {
1117 *request = mRequestTemplateCache[templateId];
1118 return OK;
1119 }
1120
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001121 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001122 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001123 rawRequest = mHal3Device->ops->construct_default_request_settings(
1124 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001125 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001126 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001127 ALOGI("%s: template %d is not supported on this camera device",
1128 __FUNCTION__, templateId);
1129 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001130 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001131 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001132 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001133
1134 return OK;
1135}
1136
1137status_t Camera3Device::waitUntilDrained() {
1138 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001139 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001140 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001141
Zhijun He69a37482014-03-23 18:44:49 -07001142 return waitUntilDrainedLocked();
1143}
1144
1145status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001146 switch (mStatus) {
1147 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001148 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001149 ALOGV("%s: Already idle", __FUNCTION__);
1150 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001151 case STATUS_CONFIGURED:
1152 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001153 case STATUS_ERROR:
1154 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001155 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001156 break;
1157 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001158 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001159 return INVALID_OPERATION;
1160 }
1161
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001162 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1163 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001164 if (res != OK) {
1165 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1166 res);
1167 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001168 return res;
1169}
1170
Ruben Brunk183f0562015-08-12 12:55:02 -07001171
1172void Camera3Device::internalUpdateStatusLocked(Status status) {
1173 mStatus = status;
1174 mRecentStatusUpdates.add(mStatus);
1175 mStatusChanged.broadcast();
1176}
1177
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001178// Pause to reconfigure
1179status_t Camera3Device::internalPauseAndWaitLocked() {
1180 mRequestThread->setPaused(true);
1181 mPauseStateNotify = true;
1182
1183 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1184 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1185 if (res != OK) {
1186 SET_ERR_L("Can't idle device in %f seconds!",
1187 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001188 }
1189
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001190 return res;
1191}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001192
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001193// Resume after internalPauseAndWaitLocked
1194status_t Camera3Device::internalResumeLocked() {
1195 status_t res;
1196
1197 mRequestThread->setPaused(false);
1198
1199 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1200 if (res != OK) {
1201 SET_ERR_L("Can't transition to active in %f seconds!",
1202 kActiveTimeout/1e9);
1203 }
1204 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001205 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001206}
1207
Ruben Brunk183f0562015-08-12 12:55:02 -07001208status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001209 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001210
1211 size_t startIndex = 0;
1212 if (mStatusWaiters == 0) {
1213 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1214 // this status list
1215 mRecentStatusUpdates.clear();
1216 } else {
1217 // If other threads are waiting on updates to this status list, set the position of the
1218 // first element that this list will check rather than clearing the list.
1219 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001220 }
1221
Ruben Brunk183f0562015-08-12 12:55:02 -07001222 mStatusWaiters++;
1223
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001224 bool stateSeen = false;
1225 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001226 if (active == (mStatus == STATUS_ACTIVE)) {
1227 // Desired state is current
1228 break;
1229 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001230
1231 res = mStatusChanged.waitRelative(mLock, timeout);
1232 if (res != OK) break;
1233
Ruben Brunk183f0562015-08-12 12:55:02 -07001234 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1235 // transitions.
1236 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1237 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1238 __FUNCTION__);
1239
1240 // Encountered desired state since we began waiting
1241 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001242 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1243 stateSeen = true;
1244 break;
1245 }
1246 }
1247 } while (!stateSeen);
1248
Ruben Brunk183f0562015-08-12 12:55:02 -07001249 mStatusWaiters--;
1250
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001251 return res;
1252}
1253
1254
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001255status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1256 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001257 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001258
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001259 if (listener != NULL && mListener != NULL) {
1260 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1261 }
1262 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001263 mRequestThread->setNotificationListener(listener);
1264 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001265
1266 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001267}
1268
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001269bool Camera3Device::willNotify3A() {
1270 return false;
1271}
1272
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001273status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001274 status_t res;
1275 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001276
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001277 while (mResultQueue.empty()) {
1278 res = mResultSignal.waitRelative(mOutputLock, timeout);
1279 if (res == TIMED_OUT) {
1280 return res;
1281 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001282 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001283 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001284 return res;
1285 }
1286 }
1287 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001288}
1289
Jianing Weicb0652e2014-03-12 18:29:36 -07001290status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001291 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001292 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001293
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001294 if (mResultQueue.empty()) {
1295 return NOT_ENOUGH_DATA;
1296 }
1297
Jianing Weicb0652e2014-03-12 18:29:36 -07001298 if (frame == NULL) {
1299 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1300 return BAD_VALUE;
1301 }
1302
1303 CaptureResult &result = *(mResultQueue.begin());
1304 frame->mResultExtras = result.mResultExtras;
1305 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001306 mResultQueue.erase(mResultQueue.begin());
1307
1308 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001309}
1310
1311status_t Camera3Device::triggerAutofocus(uint32_t id) {
1312 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001313 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001314
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001315 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1316 // Mix-in this trigger into the next request and only the next request.
1317 RequestTrigger trigger[] = {
1318 {
1319 ANDROID_CONTROL_AF_TRIGGER,
1320 ANDROID_CONTROL_AF_TRIGGER_START
1321 },
1322 {
1323 ANDROID_CONTROL_AF_TRIGGER_ID,
1324 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001325 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001326 };
1327
1328 return mRequestThread->queueTrigger(trigger,
1329 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001330}
1331
1332status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1333 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001334 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001335
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001336 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1337 // Mix-in this trigger into the next request and only the next request.
1338 RequestTrigger trigger[] = {
1339 {
1340 ANDROID_CONTROL_AF_TRIGGER,
1341 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1342 },
1343 {
1344 ANDROID_CONTROL_AF_TRIGGER_ID,
1345 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001346 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001347 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001348
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001349 return mRequestThread->queueTrigger(trigger,
1350 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001351}
1352
1353status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1354 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001355 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001356
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001357 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1358 // Mix-in this trigger into the next request and only the next request.
1359 RequestTrigger trigger[] = {
1360 {
1361 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1362 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1363 },
1364 {
1365 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1366 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001367 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001368 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001369
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001370 return mRequestThread->queueTrigger(trigger,
1371 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001372}
1373
1374status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1375 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1376 ATRACE_CALL();
1377 (void)reprocessStreamId; (void)buffer; (void)listener;
1378
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001379 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001380 return INVALID_OPERATION;
1381}
1382
Jianing Weicb0652e2014-03-12 18:29:36 -07001383status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001384 ATRACE_CALL();
1385 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001386 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001387
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001388 NotificationListener* listener;
1389 {
1390 Mutex::Autolock l(mOutputLock);
1391 listener = mListener;
1392 }
1393
Zhijun He7ef20392014-04-21 16:04:17 -07001394 {
1395 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001396 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001397 }
1398
Zhijun He491e3412013-12-27 10:57:44 -08001399 status_t res;
1400 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1401 res = mHal3Device->ops->flush(mHal3Device);
1402 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001403 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001404 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001405 }
1406
1407 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001408}
1409
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001410status_t Camera3Device::prepare(int streamId) {
1411 ATRACE_CALL();
1412 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001413 Mutex::Autolock il(mInterfaceLock);
1414 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001415
1416 sp<Camera3StreamInterface> stream;
1417 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1418 if (outputStreamIdx == NAME_NOT_FOUND) {
1419 CLOGE("Stream %d does not exist", streamId);
1420 return BAD_VALUE;
1421 }
1422
1423 stream = mOutputStreams.editValueAt(outputStreamIdx);
1424
1425 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001426 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001427 return BAD_VALUE;
1428 }
1429
1430 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001431 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001432 return BAD_VALUE;
1433 }
1434
1435 return mPreparerThread->prepare(stream);
1436}
1437
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001438status_t Camera3Device::tearDown(int streamId) {
1439 ATRACE_CALL();
1440 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1441 Mutex::Autolock il(mInterfaceLock);
1442 Mutex::Autolock l(mLock);
1443
1444 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1445 // since we cannot call register_stream_buffers except right after configure_streams.
1446 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1447 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1448 __FUNCTION__, mHal3Device->common.version);
1449 return NO_INIT;
1450 }
1451
1452 sp<Camera3StreamInterface> stream;
1453 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1454 if (outputStreamIdx == NAME_NOT_FOUND) {
1455 CLOGE("Stream %d does not exist", streamId);
1456 return BAD_VALUE;
1457 }
1458
1459 stream = mOutputStreams.editValueAt(outputStreamIdx);
1460
1461 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1462 CLOGE("Stream %d is a target of a in-progress request", streamId);
1463 return BAD_VALUE;
1464 }
1465
1466 return stream->tearDown();
1467}
1468
Zhijun He204e3292014-07-14 17:09:23 -07001469uint32_t Camera3Device::getDeviceVersion() {
1470 ATRACE_CALL();
1471 Mutex::Autolock il(mInterfaceLock);
1472 return mDeviceVersion;
1473}
1474
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001475/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001476 * Methods called by subclasses
1477 */
1478
1479void Camera3Device::notifyStatus(bool idle) {
1480 {
1481 // Need mLock to safely update state and synchronize to current
1482 // state of methods in flight.
1483 Mutex::Autolock l(mLock);
1484 // We can get various system-idle notices from the status tracker
1485 // while starting up. Only care about them if we've actually sent
1486 // in some requests recently.
1487 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1488 return;
1489 }
1490 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1491 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001492 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001493
1494 // Skip notifying listener if we're doing some user-transparent
1495 // state changes
1496 if (mPauseStateNotify) return;
1497 }
1498 NotificationListener *listener;
1499 {
1500 Mutex::Autolock l(mOutputLock);
1501 listener = mListener;
1502 }
1503 if (idle && listener != NULL) {
1504 listener->notifyIdle();
1505 }
1506}
1507
1508/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001509 * Camera3Device private methods
1510 */
1511
1512sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1513 const CameraMetadata &request) {
1514 ATRACE_CALL();
1515 status_t res;
1516
1517 sp<CaptureRequest> newRequest = new CaptureRequest;
1518 newRequest->mSettings = request;
1519
1520 camera_metadata_entry_t inputStreams =
1521 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1522 if (inputStreams.count > 0) {
1523 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001524 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001525 CLOGE("Request references unknown input stream %d",
1526 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001527 return NULL;
1528 }
1529 // Lazy completion of stream configuration (allocation/registration)
1530 // on first use
1531 if (mInputStream->isConfiguring()) {
1532 res = mInputStream->finishConfiguration(mHal3Device);
1533 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001534 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001535 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001536 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001537 return NULL;
1538 }
1539 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001540 // Check if stream is being prepared
1541 if (mInputStream->isPreparing()) {
1542 CLOGE("Request references an input stream that's being prepared!");
1543 return NULL;
1544 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001545
1546 newRequest->mInputStream = mInputStream;
1547 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1548 }
1549
1550 camera_metadata_entry_t streams =
1551 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1552 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001553 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001554 return NULL;
1555 }
1556
1557 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001558 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001559 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001560 CLOGE("Request references unknown stream %d",
1561 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001562 return NULL;
1563 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001564 sp<Camera3OutputStreamInterface> stream =
1565 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001566
1567 // Lazy completion of stream configuration (allocation/registration)
1568 // on first use
1569 if (stream->isConfiguring()) {
1570 res = stream->finishConfiguration(mHal3Device);
1571 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001572 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1573 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001574 return NULL;
1575 }
1576 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001577 // Check if stream is being prepared
1578 if (stream->isPreparing()) {
1579 CLOGE("Request references an output stream that's being prepared!");
1580 return NULL;
1581 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001582
1583 newRequest->mOutputStreams.push(stream);
1584 }
1585 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1586
1587 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001588}
1589
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001590bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1591 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1592 Size size = mSupportedOpaqueInputSizes[i];
1593 if (size.width == width && size.height == height) {
1594 return true;
1595 }
1596 }
1597
1598 return false;
1599}
1600
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001601status_t Camera3Device::configureStreamsLocked() {
1602 ATRACE_CALL();
1603 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001604
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001605 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001606 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001607 return INVALID_OPERATION;
1608 }
1609
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001610 if (!mNeedConfig) {
1611 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1612 return OK;
1613 }
1614
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001615 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1616 // adding a dummy stream instead.
1617 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1618 if (mOutputStreams.size() == 0) {
1619 addDummyStreamLocked();
1620 } else {
1621 tryRemoveDummyStreamLocked();
1622 }
1623
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001624 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001625 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001626
1627 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001628 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1629 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1630 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001631 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1632
1633 Vector<camera3_stream_t*> streams;
1634 streams.setCapacity(config.num_streams);
1635
1636 if (mInputStream != NULL) {
1637 camera3_stream_t *inputStream;
1638 inputStream = mInputStream->startConfiguration();
1639 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001640 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001641 return INVALID_OPERATION;
1642 }
1643 streams.add(inputStream);
1644 }
1645
1646 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001647
1648 // Don't configure bidi streams twice, nor add them twice to the list
1649 if (mOutputStreams[i].get() ==
1650 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1651
1652 config.num_streams--;
1653 continue;
1654 }
1655
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001656 camera3_stream_t *outputStream;
1657 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1658 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001659 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001660 return INVALID_OPERATION;
1661 }
1662 streams.add(outputStream);
1663 }
1664
1665 config.streams = streams.editArray();
1666
1667 // Do the HAL configuration; will potentially touch stream
1668 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001669 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001670 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001671 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001672
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001673 if (res == BAD_VALUE) {
1674 // HAL rejected this set of streams as unsupported, clean up config
1675 // attempt and return to unconfigured state
1676 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1677 res = mInputStream->cancelConfiguration();
1678 if (res != OK) {
1679 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1680 mInputStream->getId(), strerror(-res), res);
1681 return res;
1682 }
1683 }
1684
1685 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1686 sp<Camera3OutputStreamInterface> outputStream =
1687 mOutputStreams.editValueAt(i);
1688 if (outputStream->isConfiguring()) {
1689 res = outputStream->cancelConfiguration();
1690 if (res != OK) {
1691 SET_ERR_L(
1692 "Can't cancel configuring output stream %d: %s (%d)",
1693 outputStream->getId(), strerror(-res), res);
1694 return res;
1695 }
1696 }
1697 }
1698
1699 // Return state to that at start of call, so that future configures
1700 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001701 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001702 mNeedConfig = true;
1703
1704 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1705 return BAD_VALUE;
1706 } else if (res != OK) {
1707 // Some other kind of error from configure_streams - this is not
1708 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001709 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1710 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001711 return res;
1712 }
1713
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001714 // Finish all stream configuration immediately.
1715 // TODO: Try to relax this later back to lazy completion, which should be
1716 // faster
1717
Igor Murashkin073f8572013-05-02 14:59:28 -07001718 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001719 res = mInputStream->finishConfiguration(mHal3Device);
1720 if (res != OK) {
1721 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1722 mInputStream->getId(), strerror(-res), res);
1723 return res;
1724 }
1725 }
1726
1727 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001728 sp<Camera3OutputStreamInterface> outputStream =
1729 mOutputStreams.editValueAt(i);
1730 if (outputStream->isConfiguring()) {
1731 res = outputStream->finishConfiguration(mHal3Device);
1732 if (res != OK) {
1733 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1734 outputStream->getId(), strerror(-res), res);
1735 return res;
1736 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001737 }
1738 }
1739
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001740 // Request thread needs to know to avoid using repeat-last-settings protocol
1741 // across configure_streams() calls
1742 mRequestThread->configurationComplete();
1743
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001744 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001745
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001746 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001747
Ruben Brunk183f0562015-08-12 12:55:02 -07001748 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1749 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001750
1751 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1752
Zhijun He0a210512014-07-24 13:45:15 -07001753 // tear down the deleted streams after configure streams.
1754 mDeletedStreams.clear();
1755
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001756 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001757}
1758
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001759status_t Camera3Device::addDummyStreamLocked() {
1760 ATRACE_CALL();
1761 status_t res;
1762
1763 if (mDummyStreamId != NO_STREAM) {
1764 // Should never be adding a second dummy stream when one is already
1765 // active
1766 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1767 __FUNCTION__, mId);
1768 return INVALID_OPERATION;
1769 }
1770
1771 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1772
1773 sp<Camera3OutputStreamInterface> dummyStream =
1774 new Camera3DummyStream(mNextStreamId);
1775
1776 res = mOutputStreams.add(mNextStreamId, dummyStream);
1777 if (res < 0) {
1778 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1779 return res;
1780 }
1781
1782 mDummyStreamId = mNextStreamId;
1783 mNextStreamId++;
1784
1785 return OK;
1786}
1787
1788status_t Camera3Device::tryRemoveDummyStreamLocked() {
1789 ATRACE_CALL();
1790 status_t res;
1791
1792 if (mDummyStreamId == NO_STREAM) return OK;
1793 if (mOutputStreams.size() == 1) return OK;
1794
1795 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1796
1797 // Ok, have a dummy stream and there's at least one other output stream,
1798 // so remove the dummy
1799
1800 sp<Camera3StreamInterface> deletedStream;
1801 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1802 if (outputStreamIdx == NAME_NOT_FOUND) {
1803 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1804 return INVALID_OPERATION;
1805 }
1806
1807 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1808 mOutputStreams.removeItemsAt(outputStreamIdx);
1809
1810 // Free up the stream endpoint so that it can be used by some other stream
1811 res = deletedStream->disconnect();
1812 if (res != OK) {
1813 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1814 // fall through since we want to still list the stream as deleted.
1815 }
1816 mDeletedStreams.add(deletedStream);
1817 mDummyStreamId = NO_STREAM;
1818
1819 return res;
1820}
1821
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001822void Camera3Device::setErrorState(const char *fmt, ...) {
1823 Mutex::Autolock l(mLock);
1824 va_list args;
1825 va_start(args, fmt);
1826
1827 setErrorStateLockedV(fmt, args);
1828
1829 va_end(args);
1830}
1831
1832void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1833 Mutex::Autolock l(mLock);
1834 setErrorStateLockedV(fmt, args);
1835}
1836
1837void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1838 va_list args;
1839 va_start(args, fmt);
1840
1841 setErrorStateLockedV(fmt, args);
1842
1843 va_end(args);
1844}
1845
1846void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001847 // Print out all error messages to log
1848 String8 errorCause = String8::formatV(fmt, args);
1849 ALOGE("Camera %d: %s", mId, errorCause.string());
1850
1851 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001852 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001853
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001854 mErrorCause = errorCause;
1855
1856 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07001857 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001858
1859 // Notify upstream about a device error
1860 if (mListener != NULL) {
1861 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1862 CaptureResultExtras());
1863 }
1864
1865 // Save stack trace. View by dumping it later.
1866 CameraTraces::saveTrace();
1867 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001868}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001869
1870/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001871 * In-flight request management
1872 */
1873
Jianing Weicb0652e2014-03-12 18:29:36 -07001874status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07001875 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1876 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001877 ATRACE_CALL();
1878 Mutex::Autolock l(mInFlightLock);
1879
1880 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07001881 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
1882 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001883 if (res < 0) return res;
1884
1885 return OK;
1886}
1887
1888/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001889 * Check if all 3A fields are ready, and send off a partial 3A-only result
1890 * to the output frame queue
1891 */
Zhijun He204e3292014-07-14 17:09:23 -07001892bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001893 uint32_t frameNumber,
1894 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001895
1896 // Check if all 3A states are present
1897 // The full list of fields is
1898 // android.control.afMode
1899 // android.control.awbMode
1900 // android.control.aeState
1901 // android.control.awbState
1902 // android.control.afState
1903 // android.control.afTriggerID
1904 // android.control.aePrecaptureID
1905 // TODO: Add android.control.aeMode
1906
1907 bool gotAllStates = true;
1908
1909 uint8_t afMode;
1910 uint8_t awbMode;
1911 uint8_t aeState;
1912 uint8_t afState;
1913 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001914
1915 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1916 &afMode, frameNumber);
1917
1918 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1919 &awbMode, frameNumber);
1920
1921 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1922 &aeState, frameNumber);
1923
1924 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1925 &afState, frameNumber);
1926
1927 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1928 &awbState, frameNumber);
1929
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001930 if (!gotAllStates) return false;
1931
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001932 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001933 "AF state %d, AE state %d, AWB state %d, "
1934 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001935 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001936 afMode, awbMode,
1937 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001938 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001939
1940 // Got all states, so construct a minimal result to send
1941 // In addition to the above fields, this means adding in
1942 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001943 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001944 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001945
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001946 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001947
1948 Mutex::Autolock l(mOutputLock);
1949
Jianing Weicb0652e2014-03-12 18:29:36 -07001950 CaptureResult captureResult;
1951 captureResult.mResultExtras = resultExtras;
1952 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1953 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1954 // but not limited to CameraDeviceBase::getNextResult
1955 CaptureResult& min3AResult =
1956 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001957
Jianing Weicb0652e2014-03-12 18:29:36 -07001958 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1959 // TODO: This is problematic casting. Need to fix CameraMetadata.
1960 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001961 return false;
1962 }
1963
Jianing Weicb0652e2014-03-12 18:29:36 -07001964 int32_t requestId = resultExtras.requestId;
1965 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001966 &requestId, frameNumber)) {
1967 return false;
1968 }
1969
Zhijun He204e3292014-07-14 17:09:23 -07001970 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1971 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1972 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1973 &partialResult, frameNumber)) {
1974 return false;
1975 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001976 }
1977
Jianing Weicb0652e2014-03-12 18:29:36 -07001978 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001979 &afMode, frameNumber)) {
1980 return false;
1981 }
1982
Jianing Weicb0652e2014-03-12 18:29:36 -07001983 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001984 &awbMode, frameNumber)) {
1985 return false;
1986 }
1987
Jianing Weicb0652e2014-03-12 18:29:36 -07001988 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001989 &aeState, frameNumber)) {
1990 return false;
1991 }
1992
Jianing Weicb0652e2014-03-12 18:29:36 -07001993 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001994 &afState, frameNumber)) {
1995 return false;
1996 }
1997
Jianing Weicb0652e2014-03-12 18:29:36 -07001998 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001999 &awbState, frameNumber)) {
2000 return false;
2001 }
2002
Jianing Weicb0652e2014-03-12 18:29:36 -07002003 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002004 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002005 return false;
2006 }
2007
Jianing Weicb0652e2014-03-12 18:29:36 -07002008 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002009 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002010 return false;
2011 }
2012
Zhijun He204e3292014-07-14 17:09:23 -07002013 // We only send the aggregated partial when all 3A related metadata are available
2014 // For both API1 and API2.
2015 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002016 mResultSignal.signal();
2017
2018 return true;
2019}
2020
2021template<typename T>
2022bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002023 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002024 (void) frameNumber;
2025
2026 camera_metadata_ro_entry_t entry;
2027
2028 entry = result.find(tag);
2029 if (entry.count == 0) {
2030 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2031 mId, frameNumber, get_camera_metadata_tag_name(tag));
2032 return false;
2033 }
2034
2035 if (sizeof(T) == sizeof(uint8_t)) {
2036 *value = entry.data.u8[0];
2037 } else if (sizeof(T) == sizeof(int32_t)) {
2038 *value = entry.data.i32[0];
2039 } else {
2040 ALOGE("%s: Unexpected type", __FUNCTION__);
2041 return false;
2042 }
2043 return true;
2044}
2045
2046template<typename T>
2047bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002048 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002049 if (result.update(tag, value, 1) != NO_ERROR) {
2050 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2051 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2052 frameNumber, get_camera_metadata_tag_name(tag));
2053 return false;
2054 }
2055 return true;
2056}
2057
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002058void Camera3Device::returnOutputBuffers(
2059 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2060 nsecs_t timestamp) {
2061 for (size_t i = 0; i < numBuffers; i++)
2062 {
2063 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2064 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2065 // Note: stream may be deallocated at this point, if this buffer was
2066 // the last reference to it.
2067 if (res != OK) {
2068 ALOGE("Can't return buffer to its stream: %s (%d)",
2069 strerror(-res), res);
2070 }
2071 }
2072}
2073
2074
2075void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2076
2077 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2078 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2079
2080 nsecs_t sensorTimestamp = request.sensorTimestamp;
2081 nsecs_t shutterTimestamp = request.shutterTimestamp;
2082
2083 // Check if it's okay to remove the request from InFlightMap:
2084 // In the case of a successful request:
2085 // all input and output buffers, all result metadata, shutter callback
2086 // arrived.
2087 // In the case of a unsuccessful request:
2088 // all input and output buffers arrived.
2089 if (request.numBuffersLeft == 0 &&
2090 (request.requestStatus != OK ||
2091 (request.haveResultMetadata && shutterTimestamp != 0))) {
2092 ATRACE_ASYNC_END("frame capture", frameNumber);
2093
2094 // Sanity check - if sensor timestamp matches shutter timestamp
2095 if (request.requestStatus == OK &&
2096 sensorTimestamp != shutterTimestamp) {
2097 SET_ERR("sensor timestamp (%" PRId64
2098 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2099 sensorTimestamp, frameNumber, shutterTimestamp);
2100 }
2101
2102 // for an unsuccessful request, it may have pending output buffers to
2103 // return.
2104 assert(request.requestStatus != OK ||
2105 request.pendingOutputBuffers.size() == 0);
2106 returnOutputBuffers(request.pendingOutputBuffers.array(),
2107 request.pendingOutputBuffers.size(), 0);
2108
2109 mInFlightMap.removeItemsAt(idx, 1);
2110
2111 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2112 }
2113
2114 // Sanity check - if we have too many in-flight frames, something has
2115 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002116 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002117 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002118 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2119 kInFlightWarnLimitHighSpeed) {
2120 CLOGE("In-flight list too large for high speed configuration: %zu",
2121 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002122 }
2123}
2124
2125
2126void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2127 CaptureResultExtras &resultExtras,
2128 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002129 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002130 bool reprocess,
2131 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002132 if (pendingMetadata.isEmpty())
2133 return;
2134
2135 Mutex::Autolock l(mOutputLock);
2136
2137 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002138 if (reprocess) {
2139 if (frameNumber < mNextReprocessResultFrameNumber) {
2140 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002141 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002142 frameNumber, mNextReprocessResultFrameNumber);
2143 return;
2144 }
2145 mNextReprocessResultFrameNumber = frameNumber + 1;
2146 } else {
2147 if (frameNumber < mNextResultFrameNumber) {
2148 SET_ERR("Out-of-order capture result metadata submitted! "
2149 "(got frame number %d, expecting %d)",
2150 frameNumber, mNextResultFrameNumber);
2151 return;
2152 }
2153 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002154 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002155
2156 CaptureResult captureResult;
2157 captureResult.mResultExtras = resultExtras;
2158 captureResult.mMetadata = pendingMetadata;
2159
2160 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2161 (int32_t*)&frameNumber, 1) != OK) {
2162 SET_ERR("Failed to set frame# in metadata (%d)",
2163 frameNumber);
2164 return;
2165 } else {
2166 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2167 __FUNCTION__, mId, frameNumber);
2168 }
2169
2170 // Append any previous partials to form a complete result
2171 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2172 captureResult.mMetadata.append(collectedPartialResult);
2173 }
2174
2175 captureResult.mMetadata.sort();
2176
2177 // Check that there's a timestamp in the result metadata
2178 camera_metadata_entry entry =
2179 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2180 if (entry.count == 0) {
2181 SET_ERR("No timestamp provided by HAL for frame %d!",
2182 frameNumber);
2183 return;
2184 }
2185
Chien-Yu Chend196d612015-06-22 19:49:01 -07002186 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2187
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002188 // Valid result, insert into queue
2189 List<CaptureResult>::iterator queuedResult =
2190 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2191 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2192 ", burstId = %" PRId32, __FUNCTION__,
2193 queuedResult->mResultExtras.requestId,
2194 queuedResult->mResultExtras.frameNumber,
2195 queuedResult->mResultExtras.burstId);
2196
2197 mResultSignal.signal();
2198}
2199
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002200/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002201 * Camera HAL device callback methods
2202 */
2203
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002204void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002205 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002206
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002207 status_t res;
2208
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002209 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002210 if (result->result == NULL && result->num_output_buffers == 0 &&
2211 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002212 SET_ERR("No result data provided by HAL for frame %d",
2213 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002214 return;
2215 }
Zhijun He204e3292014-07-14 17:09:23 -07002216
2217 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2218 // partial_result to 1 when metadata is included in this result.
2219 if (!mUsePartialResult &&
2220 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2221 result->result != NULL &&
2222 result->partial_result != 1) {
2223 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2224 " if partial result is not supported",
2225 frameNumber, result->partial_result);
2226 return;
2227 }
2228
2229 bool isPartialResult = false;
2230 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002231 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002232 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002233
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002234 // Get shutter timestamp and resultExtras from list of in-flight requests,
2235 // where it was added by the shutter notification for this frame. If the
2236 // shutter timestamp isn't received yet, append the output buffers to the
2237 // in-flight request and they will be returned when the shutter timestamp
2238 // arrives. Update the in-flight status and remove the in-flight entry if
2239 // all result data and shutter timestamp have been received.
2240 nsecs_t shutterTimestamp = 0;
2241
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002242 {
2243 Mutex::Autolock l(mInFlightLock);
2244 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2245 if (idx == NAME_NOT_FOUND) {
2246 SET_ERR("Unknown frame number for capture result: %d",
2247 frameNumber);
2248 return;
2249 }
2250 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002251 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2252 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2253 ", partialResultCount = %d",
2254 __FUNCTION__, request.resultExtras.requestId,
2255 request.resultExtras.frameNumber, request.resultExtras.burstId,
2256 result->partial_result);
2257 // Always update the partial count to the latest one if it's not 0
2258 // (buffers only). When framework aggregates adjacent partial results
2259 // into one, the latest partial count will be used.
2260 if (result->partial_result != 0)
2261 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002262
2263 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002264 if (mUsePartialResult && result->result != NULL) {
2265 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2266 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2267 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2268 " the range of [1, %d] when metadata is included in the result",
2269 frameNumber, result->partial_result, mNumPartialResults);
2270 return;
2271 }
2272 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002273 if (isPartialResult) {
2274 request.partialResult.collectedResult.append(result->result);
2275 }
Zhijun He204e3292014-07-14 17:09:23 -07002276 } else {
2277 camera_metadata_ro_entry_t partialResultEntry;
2278 res = find_camera_metadata_ro_entry(result->result,
2279 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2280 if (res != NAME_NOT_FOUND &&
2281 partialResultEntry.count > 0 &&
2282 partialResultEntry.data.u8[0] ==
2283 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2284 // A partial result. Flag this as such, and collect this
2285 // set of metadata into the in-flight entry.
2286 isPartialResult = true;
2287 request.partialResult.collectedResult.append(
2288 result->result);
2289 request.partialResult.collectedResult.erase(
2290 ANDROID_QUIRKS_PARTIAL_RESULT);
2291 }
2292 }
2293
2294 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002295 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002296 if (!request.partialResult.haveSent3A) {
2297 request.partialResult.haveSent3A =
2298 processPartial3AResult(frameNumber,
2299 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002300 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002301 }
2302 }
2303 }
2304
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002305 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002306 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002307
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002308 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002309 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002310 if (request.haveResultMetadata) {
2311 SET_ERR("Called multiple times with metadata for frame %d",
2312 frameNumber);
2313 return;
2314 }
Zhijun He204e3292014-07-14 17:09:23 -07002315 if (mUsePartialResult &&
2316 !request.partialResult.collectedResult.isEmpty()) {
2317 collectedPartialResult.acquire(
2318 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002319 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002320 request.haveResultMetadata = true;
2321 }
2322
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002323 uint32_t numBuffersReturned = result->num_output_buffers;
2324 if (result->input_buffer != NULL) {
2325 if (hasInputBufferInRequest) {
2326 numBuffersReturned += 1;
2327 } else {
2328 ALOGW("%s: Input buffer should be NULL if there is no input"
2329 " buffer sent in the request",
2330 __FUNCTION__);
2331 }
2332 }
2333 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002334 if (request.numBuffersLeft < 0) {
2335 SET_ERR("Too many buffers returned for frame %d",
2336 frameNumber);
2337 return;
2338 }
2339
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002340 camera_metadata_ro_entry_t entry;
2341 res = find_camera_metadata_ro_entry(result->result,
2342 ANDROID_SENSOR_TIMESTAMP, &entry);
2343 if (res == OK && entry.count == 1) {
2344 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002345 }
2346
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002347 // If shutter event isn't received yet, append the output buffers to
2348 // the in-flight request. Otherwise, return the output buffers to
2349 // streams.
2350 if (shutterTimestamp == 0) {
2351 request.pendingOutputBuffers.appendArray(result->output_buffers,
2352 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002353 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002354 returnOutputBuffers(result->output_buffers,
2355 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002356 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002357
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002358 if (result->result != NULL && !isPartialResult) {
2359 if (shutterTimestamp == 0) {
2360 request.pendingMetadata = result->result;
2361 request.partialResult.collectedResult = collectedPartialResult;
2362 } else {
2363 CameraMetadata metadata;
2364 metadata = result->result;
2365 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002366 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2367 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002368 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002369 }
2370
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002371 removeInFlightRequestIfReadyLocked(idx);
2372 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002373
Zhijun Hef0d962a2014-06-30 10:24:11 -07002374 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002375 if (hasInputBufferInRequest) {
2376 Camera3Stream *stream =
2377 Camera3Stream::cast(result->input_buffer->stream);
2378 res = stream->returnInputBuffer(*(result->input_buffer));
2379 // Note: stream may be deallocated at this point, if this buffer was the
2380 // last reference to it.
2381 if (res != OK) {
2382 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2383 " its stream:%s (%d)", __FUNCTION__,
2384 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002385 }
2386 } else {
2387 ALOGW("%s: Input buffer should be NULL if there is no input"
2388 " buffer sent in the request, skipping input buffer return.",
2389 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002390 }
2391 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002392}
2393
2394void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002395 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002396 NotificationListener *listener;
2397 {
2398 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002399 listener = mListener;
2400 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002401
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002402 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002403 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002404 return;
2405 }
2406
2407 switch (msg->type) {
2408 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002409 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002410 break;
2411 }
2412 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002413 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002414 break;
2415 }
2416 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002417 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002418 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002419 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002420}
2421
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002422void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2423 NotificationListener *listener) {
2424
2425 // Map camera HAL error codes to ICameraDeviceCallback error codes
2426 // Index into this with the HAL error code
2427 static const ICameraDeviceCallbacks::CameraErrorCode
2428 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2429 // 0 = Unused error code
2430 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2431 // 1 = CAMERA3_MSG_ERROR_DEVICE
2432 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2433 // 2 = CAMERA3_MSG_ERROR_REQUEST
2434 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2435 // 3 = CAMERA3_MSG_ERROR_RESULT
2436 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2437 // 4 = CAMERA3_MSG_ERROR_BUFFER
2438 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2439 };
2440
2441 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2442 ((msg.error_code >= 0) &&
2443 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2444 halErrorMap[msg.error_code] :
2445 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2446
2447 int streamId = 0;
2448 if (msg.error_stream != NULL) {
2449 Camera3Stream *stream =
2450 Camera3Stream::cast(msg.error_stream);
2451 streamId = stream->getId();
2452 }
2453 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2454 mId, __FUNCTION__, msg.frame_number,
2455 streamId, msg.error_code);
2456
2457 CaptureResultExtras resultExtras;
2458 switch (errorCode) {
2459 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2460 // SET_ERR calls notifyError
2461 SET_ERR("Camera HAL reported serious device error");
2462 break;
2463 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2464 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2465 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2466 {
2467 Mutex::Autolock l(mInFlightLock);
2468 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2469 if (idx >= 0) {
2470 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2471 r.requestStatus = msg.error_code;
2472 resultExtras = r.resultExtras;
2473 } else {
2474 resultExtras.frameNumber = msg.frame_number;
2475 ALOGE("Camera %d: %s: cannot find in-flight request on "
2476 "frame %" PRId64 " error", mId, __FUNCTION__,
2477 resultExtras.frameNumber);
2478 }
2479 }
2480 if (listener != NULL) {
2481 listener->notifyError(errorCode, resultExtras);
2482 } else {
2483 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2484 }
2485 break;
2486 default:
2487 // SET_ERR calls notifyError
2488 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2489 break;
2490 }
2491}
2492
2493void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2494 NotificationListener *listener) {
2495 ssize_t idx;
2496 // Verify ordering of shutter notifications
2497 {
2498 Mutex::Autolock l(mOutputLock);
2499 // TODO: need to track errors for tighter bounds on expected frame number.
2500 if (msg.frame_number < mNextShutterFrameNumber) {
2501 SET_ERR("Shutter notification out-of-order. Expected "
2502 "notification for frame %d, got frame %d",
2503 mNextShutterFrameNumber, msg.frame_number);
2504 return;
2505 }
2506 mNextShutterFrameNumber = msg.frame_number + 1;
2507 }
2508
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002509 // Set timestamp for the request in the in-flight tracking
2510 // and get the request ID to send upstream
2511 {
2512 Mutex::Autolock l(mInFlightLock);
2513 idx = mInFlightMap.indexOfKey(msg.frame_number);
2514 if (idx >= 0) {
2515 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002516
2517 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2518 mId, __FUNCTION__,
2519 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2520 // Call listener, if any
2521 if (listener != NULL) {
2522 listener->notifyShutter(r.resultExtras, msg.timestamp);
2523 }
2524
2525 r.shutterTimestamp = msg.timestamp;
2526
2527 // send pending result and buffers
2528 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002529 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002530 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002531 returnOutputBuffers(r.pendingOutputBuffers.array(),
2532 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2533 r.pendingOutputBuffers.clear();
2534
2535 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002536 }
2537 }
2538 if (idx < 0) {
2539 SET_ERR("Shutter notification for non-existent frame number %d",
2540 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002541 }
2542}
2543
2544
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002545CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002546 ALOGV("%s", __FUNCTION__);
2547
Igor Murashkin1e479c02013-09-06 16:55:14 -07002548 CameraMetadata retVal;
2549
2550 if (mRequestThread != NULL) {
2551 retVal = mRequestThread->getLatestRequest();
2552 }
2553
Igor Murashkin1e479c02013-09-06 16:55:14 -07002554 return retVal;
2555}
2556
Jianing Weicb0652e2014-03-12 18:29:36 -07002557
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002558/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002559 * RequestThread inner class methods
2560 */
2561
2562Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002563 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002564 camera3_device_t *hal3Device,
2565 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002566 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002567 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002568 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002569 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002570 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002571 mReconfigured(false),
2572 mDoPause(false),
2573 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002574 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002575 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002576 mCurrentAfTriggerId(0),
2577 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002578 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2579 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002580 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002581}
2582
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002583void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002584 NotificationListener *listener) {
2585 Mutex::Autolock l(mRequestLock);
2586 mListener = listener;
2587}
2588
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002589void Camera3Device::RequestThread::configurationComplete() {
2590 Mutex::Autolock l(mRequestLock);
2591 mReconfigured = true;
2592}
2593
Jianing Wei90e59c92014-03-12 18:29:36 -07002594status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002595 List<sp<CaptureRequest> > &requests,
2596 /*out*/
2597 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002598 Mutex::Autolock l(mRequestLock);
2599 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2600 ++it) {
2601 mRequestQueue.push_back(*it);
2602 }
2603
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002604 if (lastFrameNumber != NULL) {
2605 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2606 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2607 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2608 *lastFrameNumber);
2609 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002610
Jianing Wei90e59c92014-03-12 18:29:36 -07002611 unpauseForNewRequests();
2612
2613 return OK;
2614}
2615
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002616
2617status_t Camera3Device::RequestThread::queueTrigger(
2618 RequestTrigger trigger[],
2619 size_t count) {
2620
2621 Mutex::Autolock l(mTriggerMutex);
2622 status_t ret;
2623
2624 for (size_t i = 0; i < count; ++i) {
2625 ret = queueTriggerLocked(trigger[i]);
2626
2627 if (ret != OK) {
2628 return ret;
2629 }
2630 }
2631
2632 return OK;
2633}
2634
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002635int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2636 sp<Camera3Device> d = device.promote();
2637 if (d != NULL) return d->mId;
2638 return 0;
2639}
2640
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002641status_t Camera3Device::RequestThread::queueTriggerLocked(
2642 RequestTrigger trigger) {
2643
2644 uint32_t tag = trigger.metadataTag;
2645 ssize_t index = mTriggerMap.indexOfKey(tag);
2646
2647 switch (trigger.getTagType()) {
2648 case TYPE_BYTE:
2649 // fall-through
2650 case TYPE_INT32:
2651 break;
2652 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002653 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2654 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002655 return INVALID_OPERATION;
2656 }
2657
2658 /**
2659 * Collect only the latest trigger, since we only have 1 field
2660 * in the request settings per trigger tag, and can't send more than 1
2661 * trigger per request.
2662 */
2663 if (index != NAME_NOT_FOUND) {
2664 mTriggerMap.editValueAt(index) = trigger;
2665 } else {
2666 mTriggerMap.add(tag, trigger);
2667 }
2668
2669 return OK;
2670}
2671
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002672status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002673 const RequestList &requests,
2674 /*out*/
2675 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002676 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002677 if (lastFrameNumber != NULL) {
2678 *lastFrameNumber = mRepeatingLastFrameNumber;
2679 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002680 mRepeatingRequests.clear();
2681 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2682 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002683
2684 unpauseForNewRequests();
2685
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002686 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002687 return OK;
2688}
2689
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002690bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2691 if (mRepeatingRequests.empty()) {
2692 return false;
2693 }
2694 int32_t requestId = requestIn->mResultExtras.requestId;
2695 const RequestList &repeatRequests = mRepeatingRequests;
2696 // All repeating requests are guaranteed to have same id so only check first quest
2697 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2698 return (firstRequest->mResultExtras.requestId == requestId);
2699}
2700
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002701status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002702 Mutex::Autolock l(mRequestLock);
2703 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002704 if (lastFrameNumber != NULL) {
2705 *lastFrameNumber = mRepeatingLastFrameNumber;
2706 }
2707 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002708 return OK;
2709}
2710
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002711status_t Camera3Device::RequestThread::clear(
2712 NotificationListener *listener,
2713 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002714 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002715 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002716
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002717 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002718
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002719 // Send errors for all requests pending in the request queue, including
2720 // pending repeating requests
2721 if (listener != NULL) {
2722 for (RequestList::iterator it = mRequestQueue.begin();
2723 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002724 // Abort the input buffers for reprocess requests.
2725 if ((*it)->mInputStream != NULL) {
2726 camera3_stream_buffer_t inputBuffer;
2727 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2728 if (res != OK) {
2729 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2730 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2731 } else {
2732 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2733 if (res != OK) {
2734 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2735 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2736 }
2737 }
2738 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002739 // Set the frame number this request would have had, if it
2740 // had been submitted; this frame number will not be reused.
2741 // The requestId and burstId fields were set when the request was
2742 // submitted originally (in convertMetadataListToRequestListLocked)
2743 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2744 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2745 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002746 }
2747 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002748 mRequestQueue.clear();
2749 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002750 if (lastFrameNumber != NULL) {
2751 *lastFrameNumber = mRepeatingLastFrameNumber;
2752 }
2753 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002754 return OK;
2755}
2756
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002757void Camera3Device::RequestThread::setPaused(bool paused) {
2758 Mutex::Autolock l(mPauseLock);
2759 mDoPause = paused;
2760 mDoPauseSignal.signal();
2761}
2762
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002763status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2764 int32_t requestId, nsecs_t timeout) {
2765 Mutex::Autolock l(mLatestRequestMutex);
2766 status_t res;
2767 while (mLatestRequestId != requestId) {
2768 nsecs_t startTime = systemTime();
2769
2770 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2771 if (res != OK) return res;
2772
2773 timeout -= (systemTime() - startTime);
2774 }
2775
2776 return OK;
2777}
2778
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002779void Camera3Device::RequestThread::requestExit() {
2780 // Call parent to set up shutdown
2781 Thread::requestExit();
2782 // The exit from any possible waits
2783 mDoPauseSignal.signal();
2784 mRequestSignal.signal();
2785}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002786
Chien-Yu Chend196d612015-06-22 19:49:01 -07002787
2788/**
2789 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2790 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2791 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2792 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2793 * request.
2794 */
2795void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2796 request->mAeTriggerCancelOverride.applyAeLock = false;
2797 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2798
2799 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2800 return;
2801 }
2802
2803 camera_metadata_entry_t aePrecaptureTrigger =
2804 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2805 if (aePrecaptureTrigger.count > 0 &&
2806 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2807 // Always override CANCEL to IDLE
2808 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2809 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2810 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2811 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2812 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2813
2814 if (mAeLockAvailable == true) {
2815 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2816 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2817 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2818 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2819 request->mAeTriggerCancelOverride.applyAeLock = true;
2820 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2821 }
2822 }
2823 }
2824}
2825
2826/**
2827 * Override result metadata for cancelling AE precapture trigger applied in
2828 * handleAePrecaptureCancelRequest().
2829 */
2830void Camera3Device::overrideResultForPrecaptureCancel(
2831 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2832 if (aeTriggerCancelOverride.applyAeLock) {
2833 // Only devices <= v3.2 should have this override
2834 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2835 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2836 }
2837
2838 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2839 // Only devices <= v3.2 should have this override
2840 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2841 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2842 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2843 }
2844}
2845
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002846bool Camera3Device::RequestThread::threadLoop() {
2847
2848 status_t res;
2849
2850 // Handle paused state.
2851 if (waitIfPaused()) {
2852 return true;
2853 }
2854
2855 // Get work to do
2856
2857 sp<CaptureRequest> nextRequest = waitForNextRequest();
2858 if (nextRequest == NULL) {
2859 return true;
2860 }
2861
2862 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002863 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002864 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002865 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002866
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002867 // Get the request ID, if any
2868 int requestId;
2869 camera_metadata_entry_t requestIdEntry =
2870 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2871 if (requestIdEntry.count > 0) {
2872 requestId = requestIdEntry.data.i32[0];
2873 } else {
2874 ALOGW("%s: Did not have android.request.id set in the request",
2875 __FUNCTION__);
2876 requestId = NAME_NOT_FOUND;
2877 }
2878
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002879 // Insert any queued triggers (before metadata is locked)
2880 int32_t triggerCount;
2881 res = insertTriggers(nextRequest);
2882 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002883 SET_ERR("RequestThread: Unable to insert triggers "
2884 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002885 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002886 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2887 return false;
2888 }
2889 triggerCount = res;
2890
2891 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2892
2893 // If the request is the same as last, or we had triggers last time
2894 if (mPrevRequest != nextRequest || triggersMixedIn) {
2895 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002896 * HAL workaround:
2897 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2898 */
2899 res = addDummyTriggerIds(nextRequest);
2900 if (res != OK) {
2901 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2902 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002903 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002904 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2905 return false;
2906 }
2907
2908 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002909 * The request should be presorted so accesses in HAL
2910 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2911 */
2912 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002913 request.settings = nextRequest->mSettings.getAndLock();
2914 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002915 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2916
2917 IF_ALOGV() {
2918 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2919 find_camera_metadata_ro_entry(
2920 request.settings,
2921 ANDROID_CONTROL_AF_TRIGGER,
2922 &e
2923 );
2924 if (e.count > 0) {
2925 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2926 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002927 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002928 e.data.u8[0]);
2929 }
2930 }
2931 } else {
2932 // leave request.settings NULL to indicate 'reuse latest given'
2933 ALOGVV("%s: Request settings are REUSED",
2934 __FUNCTION__);
2935 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002936
Zhijun Hef0d962a2014-06-30 10:24:11 -07002937 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002938
2939 // Fill in buffers
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002940 if (nextRequest->mInputStream != NULL) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002941 request.input_buffer = &nextRequest->mInputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002942 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002943 } else {
2944 request.input_buffer = NULL;
2945 }
2946
2947 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2948 nextRequest->mOutputStreams.size());
2949 request.output_buffers = outputBuffers.array();
2950 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2951 res = nextRequest->mOutputStreams.editItemAt(i)->
2952 getBuffer(&outputBuffers.editItemAt(i));
2953 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002954 // Can't get output buffer from gralloc queue - this could be due to
2955 // abandoned queue or other consumer misbehavior, so not a fatal
2956 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002957 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2958 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala0ec23d32015-05-28 15:15:54 -07002959 {
2960 Mutex::Autolock l(mRequestLock);
2961 if (mListener != NULL) {
2962 mListener->notifyError(
2963 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2964 nextRequest->mResultExtras);
2965 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002966 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002967 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2968 return true;
2969 }
2970 request.num_output_buffers++;
2971 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002972 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002973
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002974 // Log request in the in-flight queue
2975 sp<Camera3Device> parent = mParent.promote();
2976 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002977 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002978 CLOGE("RequestThread: Parent is gone");
2979 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2980 return false;
2981 }
2982
Jianing Weicb0652e2014-03-12 18:29:36 -07002983 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002984 totalNumBuffers, nextRequest->mResultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002985 /*hasInput*/request.input_buffer != NULL,
2986 nextRequest->mAeTriggerCancelOverride);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002987 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2988 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002989 __FUNCTION__,
2990 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2991 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002992 if (res != OK) {
2993 SET_ERR("RequestThread: Unable to register new in-flight request:"
2994 " %s (%d)", strerror(-res), res);
2995 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2996 return false;
2997 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002998
Zhijun Hecc27e112013-10-03 16:12:43 -07002999 // Inform waitUntilRequestProcessed thread of a new request ID
3000 {
3001 Mutex::Autolock al(mLatestRequestMutex);
3002
3003 mLatestRequestId = requestId;
3004 mLatestRequestSignal.signal();
3005 }
3006
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003007 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003008 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
3009 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003010 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003011 ATRACE_END();
3012
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003013 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003014 // Should only get a failure here for malformed requests or device-level
3015 // errors, so consider all errors fatal. Bad metadata failures should
3016 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003017 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003018 " device: %s (%d)", request.frame_number, strerror(-res), res);
3019 cleanUpFailedRequest(request, nextRequest, outputBuffers);
3020 return false;
3021 }
3022
Igor Murashkin1e479c02013-09-06 16:55:14 -07003023 // Update the latest request sent to HAL
3024 if (request.settings != NULL) { // Don't update them if they were unchanged
3025 Mutex::Autolock al(mLatestRequestMutex);
3026
3027 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
3028 mLatestRequest.acquire(cloned);
3029 }
3030
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003031 if (request.settings != NULL) {
3032 nextRequest->mSettings.unlock(request.settings);
3033 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003034
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003035 // Unset as current request
3036 {
3037 Mutex::Autolock l(mRequestLock);
3038 mNextRequest.clear();
3039 }
3040
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003041 // Remove any previously queued triggers (after unlock)
3042 res = removeTriggers(mPrevRequest);
3043 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003044 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003045 "(capture request %d, HAL device: %s (%d)",
3046 request.frame_number, strerror(-res), res);
3047 return false;
3048 }
3049 mPrevTriggers = triggerCount;
3050
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003051 return true;
3052}
3053
Igor Murashkin1e479c02013-09-06 16:55:14 -07003054CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3055 Mutex::Autolock al(mLatestRequestMutex);
3056
3057 ALOGV("RequestThread::%s", __FUNCTION__);
3058
3059 return mLatestRequest;
3060}
3061
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003062bool Camera3Device::RequestThread::isStreamPending(
3063 sp<Camera3StreamInterface>& stream) {
3064 Mutex::Autolock l(mRequestLock);
3065
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003066 if (mNextRequest != nullptr) {
3067 for (const auto& s : mNextRequest->mOutputStreams) {
3068 if (stream == s) return true;
3069 }
3070 if (stream == mNextRequest->mInputStream) return true;
3071 }
3072
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003073 for (const auto& request : mRequestQueue) {
3074 for (const auto& s : request->mOutputStreams) {
3075 if (stream == s) return true;
3076 }
3077 if (stream == request->mInputStream) return true;
3078 }
3079
3080 for (const auto& request : mRepeatingRequests) {
3081 for (const auto& s : request->mOutputStreams) {
3082 if (stream == s) return true;
3083 }
3084 if (stream == request->mInputStream) return true;
3085 }
3086
3087 return false;
3088}
Jianing Weicb0652e2014-03-12 18:29:36 -07003089
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003090void Camera3Device::RequestThread::cleanUpFailedRequest(
3091 camera3_capture_request_t &request,
3092 sp<CaptureRequest> &nextRequest,
3093 Vector<camera3_stream_buffer_t> &outputBuffers) {
3094
3095 if (request.settings != NULL) {
3096 nextRequest->mSettings.unlock(request.settings);
3097 }
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003098 if (nextRequest->mInputStream != NULL) {
3099 nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3100 nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003101 }
3102 for (size_t i = 0; i < request.num_output_buffers; i++) {
3103 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3104 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
3105 outputBuffers[i], 0);
3106 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003107
3108 Mutex::Autolock l(mRequestLock);
3109 mNextRequest.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003110}
3111
3112sp<Camera3Device::CaptureRequest>
3113 Camera3Device::RequestThread::waitForNextRequest() {
3114 status_t res;
3115 sp<CaptureRequest> nextRequest;
3116
3117 // Optimized a bit for the simple steady-state case (single repeating
3118 // request), to avoid putting that request in the queue temporarily.
3119 Mutex::Autolock l(mRequestLock);
3120
3121 while (mRequestQueue.empty()) {
3122 if (!mRepeatingRequests.empty()) {
3123 // Always atomically enqueue all requests in a repeating request
3124 // list. Guarantees a complete in-sequence set of captures to
3125 // application.
3126 const RequestList &requests = mRepeatingRequests;
3127 RequestList::const_iterator firstRequest =
3128 requests.begin();
3129 nextRequest = *firstRequest;
3130 mRequestQueue.insert(mRequestQueue.end(),
3131 ++firstRequest,
3132 requests.end());
3133 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003134
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003135 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003136
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003137 break;
3138 }
3139
3140 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3141
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003142 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3143 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003144 Mutex::Autolock pl(mPauseLock);
3145 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003146 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003147 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003148 // Let the tracker know
3149 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3150 if (statusTracker != 0) {
3151 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3152 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003153 }
3154 // Stop waiting for now and let thread management happen
3155 return NULL;
3156 }
3157 }
3158
3159 if (nextRequest == NULL) {
3160 // Don't have a repeating request already in hand, so queue
3161 // must have an entry now.
3162 RequestList::iterator firstRequest =
3163 mRequestQueue.begin();
3164 nextRequest = *firstRequest;
3165 mRequestQueue.erase(firstRequest);
3166 }
3167
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003168 // In case we've been unpaused by setPaused clearing mDoPause, need to
3169 // update internal pause state (capture/setRepeatingRequest unpause
3170 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003171 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003172 if (mPaused) {
3173 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3174 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3175 if (statusTracker != 0) {
3176 statusTracker->markComponentActive(mStatusId);
3177 }
3178 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003179 mPaused = false;
3180
3181 // Check if we've reconfigured since last time, and reset the preview
3182 // request if so. Can't use 'NULL request == repeat' across configure calls.
3183 if (mReconfigured) {
3184 mPrevRequest.clear();
3185 mReconfigured = false;
3186 }
3187
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003188 if (nextRequest != NULL) {
3189 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003190 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3191 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003192
3193 // Since RequestThread::clear() removes buffers from the input stream,
3194 // get the right buffer here before unlocking mRequestLock
3195 if (nextRequest->mInputStream != NULL) {
3196 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3197 if (res != OK) {
3198 // Can't get input buffer from gralloc queue - this could be due to
3199 // disconnected queue or other producer misbehavior, so not a fatal
3200 // error
3201 ALOGE("%s: Can't get input buffer, skipping request:"
3202 " %s (%d)", __FUNCTION__, strerror(-res), res);
3203 if (mListener != NULL) {
3204 mListener->notifyError(
3205 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3206 nextRequest->mResultExtras);
3207 }
3208 return NULL;
3209 }
3210 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003211 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003212
3213 handleAePrecaptureCancelRequest(nextRequest);
3214
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003215 mNextRequest = nextRequest;
3216
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003217 return nextRequest;
3218}
3219
3220bool Camera3Device::RequestThread::waitIfPaused() {
3221 status_t res;
3222 Mutex::Autolock l(mPauseLock);
3223 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003224 if (mPaused == false) {
3225 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003226 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3227 // Let the tracker know
3228 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3229 if (statusTracker != 0) {
3230 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3231 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003232 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003233
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003234 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003235 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003236 return true;
3237 }
3238 }
3239 // We don't set mPaused to false here, because waitForNextRequest needs
3240 // to further manage the paused state in case of starvation.
3241 return false;
3242}
3243
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003244void Camera3Device::RequestThread::unpauseForNewRequests() {
3245 // With work to do, mark thread as unpaused.
3246 // If paused by request (setPaused), don't resume, to avoid
3247 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003248 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003249 Mutex::Autolock p(mPauseLock);
3250 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003251 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3252 if (mPaused) {
3253 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3254 if (statusTracker != 0) {
3255 statusTracker->markComponentActive(mStatusId);
3256 }
3257 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003258 mPaused = false;
3259 }
3260}
3261
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003262void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3263 sp<Camera3Device> parent = mParent.promote();
3264 if (parent != NULL) {
3265 va_list args;
3266 va_start(args, fmt);
3267
3268 parent->setErrorStateV(fmt, args);
3269
3270 va_end(args);
3271 }
3272}
3273
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003274status_t Camera3Device::RequestThread::insertTriggers(
3275 const sp<CaptureRequest> &request) {
3276
3277 Mutex::Autolock al(mTriggerMutex);
3278
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003279 sp<Camera3Device> parent = mParent.promote();
3280 if (parent == NULL) {
3281 CLOGE("RequestThread: Parent is gone");
3282 return DEAD_OBJECT;
3283 }
3284
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003285 CameraMetadata &metadata = request->mSettings;
3286 size_t count = mTriggerMap.size();
3287
3288 for (size_t i = 0; i < count; ++i) {
3289 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003290 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003291
3292 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3293 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3294 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003295 if (isAeTrigger) {
3296 request->mResultExtras.precaptureTriggerId = triggerId;
3297 mCurrentPreCaptureTriggerId = triggerId;
3298 } else {
3299 request->mResultExtras.afTriggerId = triggerId;
3300 mCurrentAfTriggerId = triggerId;
3301 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003302 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3303 continue; // Trigger ID tag is deprecated since device HAL 3.2
3304 }
3305 }
3306
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003307 camera_metadata_entry entry = metadata.find(tag);
3308
3309 if (entry.count > 0) {
3310 /**
3311 * Already has an entry for this trigger in the request.
3312 * Rewrite it with our requested trigger value.
3313 */
3314 RequestTrigger oldTrigger = trigger;
3315
3316 oldTrigger.entryValue = entry.data.u8[0];
3317
3318 mTriggerReplacedMap.add(tag, oldTrigger);
3319 } else {
3320 /**
3321 * More typical, no trigger entry, so we just add it
3322 */
3323 mTriggerRemovedMap.add(tag, trigger);
3324 }
3325
3326 status_t res;
3327
3328 switch (trigger.getTagType()) {
3329 case TYPE_BYTE: {
3330 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3331 res = metadata.update(tag,
3332 &entryValue,
3333 /*count*/1);
3334 break;
3335 }
3336 case TYPE_INT32:
3337 res = metadata.update(tag,
3338 &trigger.entryValue,
3339 /*count*/1);
3340 break;
3341 default:
3342 ALOGE("%s: Type not supported: 0x%x",
3343 __FUNCTION__,
3344 trigger.getTagType());
3345 return INVALID_OPERATION;
3346 }
3347
3348 if (res != OK) {
3349 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3350 ", value %d", __FUNCTION__, trigger.getTagName(),
3351 trigger.entryValue);
3352 return res;
3353 }
3354
3355 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3356 trigger.getTagName(),
3357 trigger.entryValue);
3358 }
3359
3360 mTriggerMap.clear();
3361
3362 return count;
3363}
3364
3365status_t Camera3Device::RequestThread::removeTriggers(
3366 const sp<CaptureRequest> &request) {
3367 Mutex::Autolock al(mTriggerMutex);
3368
3369 CameraMetadata &metadata = request->mSettings;
3370
3371 /**
3372 * Replace all old entries with their old values.
3373 */
3374 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3375 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3376
3377 status_t res;
3378
3379 uint32_t tag = trigger.metadataTag;
3380 switch (trigger.getTagType()) {
3381 case TYPE_BYTE: {
3382 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3383 res = metadata.update(tag,
3384 &entryValue,
3385 /*count*/1);
3386 break;
3387 }
3388 case TYPE_INT32:
3389 res = metadata.update(tag,
3390 &trigger.entryValue,
3391 /*count*/1);
3392 break;
3393 default:
3394 ALOGE("%s: Type not supported: 0x%x",
3395 __FUNCTION__,
3396 trigger.getTagType());
3397 return INVALID_OPERATION;
3398 }
3399
3400 if (res != OK) {
3401 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3402 ", trigger value %d", __FUNCTION__,
3403 trigger.getTagName(), trigger.entryValue);
3404 return res;
3405 }
3406 }
3407 mTriggerReplacedMap.clear();
3408
3409 /**
3410 * Remove all new entries.
3411 */
3412 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3413 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3414 status_t res = metadata.erase(trigger.metadataTag);
3415
3416 if (res != OK) {
3417 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3418 ", trigger value %d", __FUNCTION__,
3419 trigger.getTagName(), trigger.entryValue);
3420 return res;
3421 }
3422 }
3423 mTriggerRemovedMap.clear();
3424
3425 return OK;
3426}
3427
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003428status_t Camera3Device::RequestThread::addDummyTriggerIds(
3429 const sp<CaptureRequest> &request) {
3430 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3431 static const int32_t dummyTriggerId = 1;
3432 status_t res;
3433
3434 CameraMetadata &metadata = request->mSettings;
3435
3436 // If AF trigger is active, insert a dummy AF trigger ID if none already
3437 // exists
3438 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3439 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3440 if (afTrigger.count > 0 &&
3441 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3442 afId.count == 0) {
3443 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3444 if (res != OK) return res;
3445 }
3446
3447 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3448 // if none already exists
3449 camera_metadata_entry pcTrigger =
3450 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3451 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3452 if (pcTrigger.count > 0 &&
3453 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3454 pcId.count == 0) {
3455 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3456 &dummyTriggerId, 1);
3457 if (res != OK) return res;
3458 }
3459
3460 return OK;
3461}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003462
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003463/**
3464 * PreparerThread inner class methods
3465 */
3466
3467Camera3Device::PreparerThread::PreparerThread() :
3468 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3469}
3470
3471Camera3Device::PreparerThread::~PreparerThread() {
3472 Thread::requestExitAndWait();
3473 if (mCurrentStream != nullptr) {
3474 mCurrentStream->cancelPrepare();
3475 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3476 mCurrentStream.clear();
3477 }
3478 clear();
3479}
3480
3481status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) {
3482 status_t res;
3483
3484 Mutex::Autolock l(mLock);
3485
3486 res = stream->startPrepare();
3487 if (res == OK) {
3488 // No preparation needed, fire listener right off
3489 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3490 if (mListener) {
3491 mListener->notifyPrepared(stream->getId());
3492 }
3493 return OK;
3494 } else if (res != NOT_ENOUGH_DATA) {
3495 return res;
3496 }
3497
3498 // Need to prepare, start up thread if necessary
3499 if (!mActive) {
3500 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3501 // isn't running
3502 Thread::requestExitAndWait();
3503 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3504 if (res != OK) {
3505 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3506 if (mListener) {
3507 mListener->notifyPrepared(stream->getId());
3508 }
3509 return res;
3510 }
3511 mCancelNow = false;
3512 mActive = true;
3513 ALOGV("%s: Preparer stream started", __FUNCTION__);
3514 }
3515
3516 // queue up the work
3517 mPendingStreams.push_back(stream);
3518 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3519
3520 return OK;
3521}
3522
3523status_t Camera3Device::PreparerThread::clear() {
3524 status_t res;
3525
3526 Mutex::Autolock l(mLock);
3527
3528 for (const auto& stream : mPendingStreams) {
3529 stream->cancelPrepare();
3530 }
3531 mPendingStreams.clear();
3532 mCancelNow = true;
3533
3534 return OK;
3535}
3536
3537void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3538 Mutex::Autolock l(mLock);
3539 mListener = listener;
3540}
3541
3542bool Camera3Device::PreparerThread::threadLoop() {
3543 status_t res;
3544 {
3545 Mutex::Autolock l(mLock);
3546 if (mCurrentStream == nullptr) {
3547 // End thread if done with work
3548 if (mPendingStreams.empty()) {
3549 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3550 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3551 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3552 mActive = false;
3553 return false;
3554 }
3555
3556 // Get next stream to prepare
3557 auto it = mPendingStreams.begin();
3558 mCurrentStream = *it;
3559 mPendingStreams.erase(it);
3560 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3561 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3562 } else if (mCancelNow) {
3563 mCurrentStream->cancelPrepare();
3564 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3565 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3566 mCurrentStream.clear();
3567 mCancelNow = false;
3568 return true;
3569 }
3570 }
3571
3572 res = mCurrentStream->prepareNextBuffer();
3573 if (res == NOT_ENOUGH_DATA) return true;
3574 if (res != OK) {
3575 // Something bad happened; try to recover by cancelling prepare and
3576 // signalling listener anyway
3577 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3578 mCurrentStream->getId(), res, strerror(-res));
3579 mCurrentStream->cancelPrepare();
3580 }
3581
3582 // This stream has finished, notify listener
3583 Mutex::Autolock l(mLock);
3584 if (mListener) {
3585 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3586 mCurrentStream->getId());
3587 mListener->notifyPrepared(mCurrentStream->getId());
3588 }
3589
3590 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3591 mCurrentStream.clear();
3592
3593 return true;
3594}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003595
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003596/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003597 * Static callback forwarding methods from HAL to instance
3598 */
3599
3600void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3601 const camera3_capture_result *result) {
3602 Camera3Device *d =
3603 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003604
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003605 d->processCaptureResult(result);
3606}
3607
3608void Camera3Device::sNotify(const camera3_callback_ops *cb,
3609 const camera3_notify_msg *msg) {
3610 Camera3Device *d =
3611 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3612 d->notify(msg);
3613}
3614
3615}; // namespace android