blob: e4d75cb4fe3639bbcadb1df7cffa6fb01133ae95 [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) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001411 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1412}
1413
1414status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001415 ATRACE_CALL();
1416 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001417 Mutex::Autolock il(mInterfaceLock);
1418 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001419
1420 sp<Camera3StreamInterface> stream;
1421 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1422 if (outputStreamIdx == NAME_NOT_FOUND) {
1423 CLOGE("Stream %d does not exist", streamId);
1424 return BAD_VALUE;
1425 }
1426
1427 stream = mOutputStreams.editValueAt(outputStreamIdx);
1428
1429 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001430 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001431 return BAD_VALUE;
1432 }
1433
1434 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001435 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001436 return BAD_VALUE;
1437 }
1438
Ruben Brunkc78ac262015-08-13 17:58:46 -07001439 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001440}
1441
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001442status_t Camera3Device::tearDown(int streamId) {
1443 ATRACE_CALL();
1444 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1445 Mutex::Autolock il(mInterfaceLock);
1446 Mutex::Autolock l(mLock);
1447
1448 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1449 // since we cannot call register_stream_buffers except right after configure_streams.
1450 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1451 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1452 __FUNCTION__, mHal3Device->common.version);
1453 return NO_INIT;
1454 }
1455
1456 sp<Camera3StreamInterface> stream;
1457 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1458 if (outputStreamIdx == NAME_NOT_FOUND) {
1459 CLOGE("Stream %d does not exist", streamId);
1460 return BAD_VALUE;
1461 }
1462
1463 stream = mOutputStreams.editValueAt(outputStreamIdx);
1464
1465 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1466 CLOGE("Stream %d is a target of a in-progress request", streamId);
1467 return BAD_VALUE;
1468 }
1469
1470 return stream->tearDown();
1471}
1472
Zhijun He204e3292014-07-14 17:09:23 -07001473uint32_t Camera3Device::getDeviceVersion() {
1474 ATRACE_CALL();
1475 Mutex::Autolock il(mInterfaceLock);
1476 return mDeviceVersion;
1477}
1478
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001479/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001480 * Methods called by subclasses
1481 */
1482
1483void Camera3Device::notifyStatus(bool idle) {
1484 {
1485 // Need mLock to safely update state and synchronize to current
1486 // state of methods in flight.
1487 Mutex::Autolock l(mLock);
1488 // We can get various system-idle notices from the status tracker
1489 // while starting up. Only care about them if we've actually sent
1490 // in some requests recently.
1491 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1492 return;
1493 }
1494 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1495 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001496 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001497
1498 // Skip notifying listener if we're doing some user-transparent
1499 // state changes
1500 if (mPauseStateNotify) return;
1501 }
1502 NotificationListener *listener;
1503 {
1504 Mutex::Autolock l(mOutputLock);
1505 listener = mListener;
1506 }
1507 if (idle && listener != NULL) {
1508 listener->notifyIdle();
1509 }
1510}
1511
1512/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001513 * Camera3Device private methods
1514 */
1515
1516sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1517 const CameraMetadata &request) {
1518 ATRACE_CALL();
1519 status_t res;
1520
1521 sp<CaptureRequest> newRequest = new CaptureRequest;
1522 newRequest->mSettings = request;
1523
1524 camera_metadata_entry_t inputStreams =
1525 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1526 if (inputStreams.count > 0) {
1527 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001528 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001529 CLOGE("Request references unknown input stream %d",
1530 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001531 return NULL;
1532 }
1533 // Lazy completion of stream configuration (allocation/registration)
1534 // on first use
1535 if (mInputStream->isConfiguring()) {
1536 res = mInputStream->finishConfiguration(mHal3Device);
1537 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001538 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001539 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001540 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001541 return NULL;
1542 }
1543 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001544 // Check if stream is being prepared
1545 if (mInputStream->isPreparing()) {
1546 CLOGE("Request references an input stream that's being prepared!");
1547 return NULL;
1548 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001549
1550 newRequest->mInputStream = mInputStream;
1551 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1552 }
1553
1554 camera_metadata_entry_t streams =
1555 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1556 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001557 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001558 return NULL;
1559 }
1560
1561 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001562 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001563 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001564 CLOGE("Request references unknown stream %d",
1565 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001566 return NULL;
1567 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001568 sp<Camera3OutputStreamInterface> stream =
1569 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001570
1571 // Lazy completion of stream configuration (allocation/registration)
1572 // on first use
1573 if (stream->isConfiguring()) {
1574 res = stream->finishConfiguration(mHal3Device);
1575 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001576 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1577 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001578 return NULL;
1579 }
1580 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001581 // Check if stream is being prepared
1582 if (stream->isPreparing()) {
1583 CLOGE("Request references an output stream that's being prepared!");
1584 return NULL;
1585 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001586
1587 newRequest->mOutputStreams.push(stream);
1588 }
1589 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1590
1591 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001592}
1593
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001594bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1595 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1596 Size size = mSupportedOpaqueInputSizes[i];
1597 if (size.width == width && size.height == height) {
1598 return true;
1599 }
1600 }
1601
1602 return false;
1603}
1604
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001605status_t Camera3Device::configureStreamsLocked() {
1606 ATRACE_CALL();
1607 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001608
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001609 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001610 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001611 return INVALID_OPERATION;
1612 }
1613
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001614 if (!mNeedConfig) {
1615 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1616 return OK;
1617 }
1618
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001619 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1620 // adding a dummy stream instead.
1621 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1622 if (mOutputStreams.size() == 0) {
1623 addDummyStreamLocked();
1624 } else {
1625 tryRemoveDummyStreamLocked();
1626 }
1627
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001628 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001629 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001630
1631 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001632 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1633 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1634 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001635 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1636
1637 Vector<camera3_stream_t*> streams;
1638 streams.setCapacity(config.num_streams);
1639
1640 if (mInputStream != NULL) {
1641 camera3_stream_t *inputStream;
1642 inputStream = mInputStream->startConfiguration();
1643 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001644 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001645 return INVALID_OPERATION;
1646 }
1647 streams.add(inputStream);
1648 }
1649
1650 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001651
1652 // Don't configure bidi streams twice, nor add them twice to the list
1653 if (mOutputStreams[i].get() ==
1654 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1655
1656 config.num_streams--;
1657 continue;
1658 }
1659
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001660 camera3_stream_t *outputStream;
1661 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1662 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001663 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001664 return INVALID_OPERATION;
1665 }
1666 streams.add(outputStream);
1667 }
1668
1669 config.streams = streams.editArray();
1670
1671 // Do the HAL configuration; will potentially touch stream
1672 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001673 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001674 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001675 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001676
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001677 if (res == BAD_VALUE) {
1678 // HAL rejected this set of streams as unsupported, clean up config
1679 // attempt and return to unconfigured state
1680 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1681 res = mInputStream->cancelConfiguration();
1682 if (res != OK) {
1683 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1684 mInputStream->getId(), strerror(-res), res);
1685 return res;
1686 }
1687 }
1688
1689 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1690 sp<Camera3OutputStreamInterface> outputStream =
1691 mOutputStreams.editValueAt(i);
1692 if (outputStream->isConfiguring()) {
1693 res = outputStream->cancelConfiguration();
1694 if (res != OK) {
1695 SET_ERR_L(
1696 "Can't cancel configuring output stream %d: %s (%d)",
1697 outputStream->getId(), strerror(-res), res);
1698 return res;
1699 }
1700 }
1701 }
1702
1703 // Return state to that at start of call, so that future configures
1704 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001705 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001706 mNeedConfig = true;
1707
1708 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1709 return BAD_VALUE;
1710 } else if (res != OK) {
1711 // Some other kind of error from configure_streams - this is not
1712 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001713 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1714 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001715 return res;
1716 }
1717
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001718 // Finish all stream configuration immediately.
1719 // TODO: Try to relax this later back to lazy completion, which should be
1720 // faster
1721
Igor Murashkin073f8572013-05-02 14:59:28 -07001722 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001723 res = mInputStream->finishConfiguration(mHal3Device);
1724 if (res != OK) {
1725 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1726 mInputStream->getId(), strerror(-res), res);
1727 return res;
1728 }
1729 }
1730
1731 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001732 sp<Camera3OutputStreamInterface> outputStream =
1733 mOutputStreams.editValueAt(i);
1734 if (outputStream->isConfiguring()) {
1735 res = outputStream->finishConfiguration(mHal3Device);
1736 if (res != OK) {
1737 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1738 outputStream->getId(), strerror(-res), res);
1739 return res;
1740 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001741 }
1742 }
1743
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001744 // Request thread needs to know to avoid using repeat-last-settings protocol
1745 // across configure_streams() calls
1746 mRequestThread->configurationComplete();
1747
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001748 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001749
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001750 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001751
Ruben Brunk183f0562015-08-12 12:55:02 -07001752 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1753 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001754
1755 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1756
Zhijun He0a210512014-07-24 13:45:15 -07001757 // tear down the deleted streams after configure streams.
1758 mDeletedStreams.clear();
1759
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001760 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001761}
1762
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001763status_t Camera3Device::addDummyStreamLocked() {
1764 ATRACE_CALL();
1765 status_t res;
1766
1767 if (mDummyStreamId != NO_STREAM) {
1768 // Should never be adding a second dummy stream when one is already
1769 // active
1770 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1771 __FUNCTION__, mId);
1772 return INVALID_OPERATION;
1773 }
1774
1775 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1776
1777 sp<Camera3OutputStreamInterface> dummyStream =
1778 new Camera3DummyStream(mNextStreamId);
1779
1780 res = mOutputStreams.add(mNextStreamId, dummyStream);
1781 if (res < 0) {
1782 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1783 return res;
1784 }
1785
1786 mDummyStreamId = mNextStreamId;
1787 mNextStreamId++;
1788
1789 return OK;
1790}
1791
1792status_t Camera3Device::tryRemoveDummyStreamLocked() {
1793 ATRACE_CALL();
1794 status_t res;
1795
1796 if (mDummyStreamId == NO_STREAM) return OK;
1797 if (mOutputStreams.size() == 1) return OK;
1798
1799 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1800
1801 // Ok, have a dummy stream and there's at least one other output stream,
1802 // so remove the dummy
1803
1804 sp<Camera3StreamInterface> deletedStream;
1805 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1806 if (outputStreamIdx == NAME_NOT_FOUND) {
1807 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1808 return INVALID_OPERATION;
1809 }
1810
1811 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1812 mOutputStreams.removeItemsAt(outputStreamIdx);
1813
1814 // Free up the stream endpoint so that it can be used by some other stream
1815 res = deletedStream->disconnect();
1816 if (res != OK) {
1817 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1818 // fall through since we want to still list the stream as deleted.
1819 }
1820 mDeletedStreams.add(deletedStream);
1821 mDummyStreamId = NO_STREAM;
1822
1823 return res;
1824}
1825
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001826void Camera3Device::setErrorState(const char *fmt, ...) {
1827 Mutex::Autolock l(mLock);
1828 va_list args;
1829 va_start(args, fmt);
1830
1831 setErrorStateLockedV(fmt, args);
1832
1833 va_end(args);
1834}
1835
1836void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1837 Mutex::Autolock l(mLock);
1838 setErrorStateLockedV(fmt, args);
1839}
1840
1841void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1842 va_list args;
1843 va_start(args, fmt);
1844
1845 setErrorStateLockedV(fmt, args);
1846
1847 va_end(args);
1848}
1849
1850void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001851 // Print out all error messages to log
1852 String8 errorCause = String8::formatV(fmt, args);
1853 ALOGE("Camera %d: %s", mId, errorCause.string());
1854
1855 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001856 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001857
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001858 mErrorCause = errorCause;
1859
1860 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07001861 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001862
1863 // Notify upstream about a device error
1864 if (mListener != NULL) {
1865 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1866 CaptureResultExtras());
1867 }
1868
1869 // Save stack trace. View by dumping it later.
1870 CameraTraces::saveTrace();
1871 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001872}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001873
1874/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001875 * In-flight request management
1876 */
1877
Jianing Weicb0652e2014-03-12 18:29:36 -07001878status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07001879 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1880 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001881 ATRACE_CALL();
1882 Mutex::Autolock l(mInFlightLock);
1883
1884 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07001885 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
1886 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001887 if (res < 0) return res;
1888
1889 return OK;
1890}
1891
1892/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001893 * Check if all 3A fields are ready, and send off a partial 3A-only result
1894 * to the output frame queue
1895 */
Zhijun He204e3292014-07-14 17:09:23 -07001896bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001897 uint32_t frameNumber,
1898 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001899
1900 // Check if all 3A states are present
1901 // The full list of fields is
1902 // android.control.afMode
1903 // android.control.awbMode
1904 // android.control.aeState
1905 // android.control.awbState
1906 // android.control.afState
1907 // android.control.afTriggerID
1908 // android.control.aePrecaptureID
1909 // TODO: Add android.control.aeMode
1910
1911 bool gotAllStates = true;
1912
1913 uint8_t afMode;
1914 uint8_t awbMode;
1915 uint8_t aeState;
1916 uint8_t afState;
1917 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001918
1919 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1920 &afMode, frameNumber);
1921
1922 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1923 &awbMode, frameNumber);
1924
1925 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1926 &aeState, frameNumber);
1927
1928 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1929 &afState, frameNumber);
1930
1931 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1932 &awbState, frameNumber);
1933
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001934 if (!gotAllStates) return false;
1935
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001936 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001937 "AF state %d, AE state %d, AWB state %d, "
1938 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001939 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001940 afMode, awbMode,
1941 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001942 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001943
1944 // Got all states, so construct a minimal result to send
1945 // In addition to the above fields, this means adding in
1946 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001947 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001948 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001949
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001950 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001951
1952 Mutex::Autolock l(mOutputLock);
1953
Jianing Weicb0652e2014-03-12 18:29:36 -07001954 CaptureResult captureResult;
1955 captureResult.mResultExtras = resultExtras;
1956 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1957 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1958 // but not limited to CameraDeviceBase::getNextResult
1959 CaptureResult& min3AResult =
1960 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001961
Jianing Weicb0652e2014-03-12 18:29:36 -07001962 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1963 // TODO: This is problematic casting. Need to fix CameraMetadata.
1964 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001965 return false;
1966 }
1967
Jianing Weicb0652e2014-03-12 18:29:36 -07001968 int32_t requestId = resultExtras.requestId;
1969 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001970 &requestId, frameNumber)) {
1971 return false;
1972 }
1973
Zhijun He204e3292014-07-14 17:09:23 -07001974 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1975 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1976 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1977 &partialResult, frameNumber)) {
1978 return false;
1979 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001980 }
1981
Jianing Weicb0652e2014-03-12 18:29:36 -07001982 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001983 &afMode, frameNumber)) {
1984 return false;
1985 }
1986
Jianing Weicb0652e2014-03-12 18:29:36 -07001987 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001988 &awbMode, frameNumber)) {
1989 return false;
1990 }
1991
Jianing Weicb0652e2014-03-12 18:29:36 -07001992 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001993 &aeState, frameNumber)) {
1994 return false;
1995 }
1996
Jianing Weicb0652e2014-03-12 18:29:36 -07001997 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001998 &afState, frameNumber)) {
1999 return false;
2000 }
2001
Jianing Weicb0652e2014-03-12 18:29:36 -07002002 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002003 &awbState, frameNumber)) {
2004 return false;
2005 }
2006
Jianing Weicb0652e2014-03-12 18:29:36 -07002007 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002008 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002009 return false;
2010 }
2011
Jianing Weicb0652e2014-03-12 18:29:36 -07002012 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002013 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002014 return false;
2015 }
2016
Zhijun He204e3292014-07-14 17:09:23 -07002017 // We only send the aggregated partial when all 3A related metadata are available
2018 // For both API1 and API2.
2019 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002020 mResultSignal.signal();
2021
2022 return true;
2023}
2024
2025template<typename T>
2026bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002027 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002028 (void) frameNumber;
2029
2030 camera_metadata_ro_entry_t entry;
2031
2032 entry = result.find(tag);
2033 if (entry.count == 0) {
2034 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2035 mId, frameNumber, get_camera_metadata_tag_name(tag));
2036 return false;
2037 }
2038
2039 if (sizeof(T) == sizeof(uint8_t)) {
2040 *value = entry.data.u8[0];
2041 } else if (sizeof(T) == sizeof(int32_t)) {
2042 *value = entry.data.i32[0];
2043 } else {
2044 ALOGE("%s: Unexpected type", __FUNCTION__);
2045 return false;
2046 }
2047 return true;
2048}
2049
2050template<typename T>
2051bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002052 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002053 if (result.update(tag, value, 1) != NO_ERROR) {
2054 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2055 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2056 frameNumber, get_camera_metadata_tag_name(tag));
2057 return false;
2058 }
2059 return true;
2060}
2061
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002062void Camera3Device::returnOutputBuffers(
2063 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2064 nsecs_t timestamp) {
2065 for (size_t i = 0; i < numBuffers; i++)
2066 {
2067 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2068 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2069 // Note: stream may be deallocated at this point, if this buffer was
2070 // the last reference to it.
2071 if (res != OK) {
2072 ALOGE("Can't return buffer to its stream: %s (%d)",
2073 strerror(-res), res);
2074 }
2075 }
2076}
2077
2078
2079void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2080
2081 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2082 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2083
2084 nsecs_t sensorTimestamp = request.sensorTimestamp;
2085 nsecs_t shutterTimestamp = request.shutterTimestamp;
2086
2087 // Check if it's okay to remove the request from InFlightMap:
2088 // In the case of a successful request:
2089 // all input and output buffers, all result metadata, shutter callback
2090 // arrived.
2091 // In the case of a unsuccessful request:
2092 // all input and output buffers arrived.
2093 if (request.numBuffersLeft == 0 &&
2094 (request.requestStatus != OK ||
2095 (request.haveResultMetadata && shutterTimestamp != 0))) {
2096 ATRACE_ASYNC_END("frame capture", frameNumber);
2097
2098 // Sanity check - if sensor timestamp matches shutter timestamp
2099 if (request.requestStatus == OK &&
2100 sensorTimestamp != shutterTimestamp) {
2101 SET_ERR("sensor timestamp (%" PRId64
2102 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2103 sensorTimestamp, frameNumber, shutterTimestamp);
2104 }
2105
2106 // for an unsuccessful request, it may have pending output buffers to
2107 // return.
2108 assert(request.requestStatus != OK ||
2109 request.pendingOutputBuffers.size() == 0);
2110 returnOutputBuffers(request.pendingOutputBuffers.array(),
2111 request.pendingOutputBuffers.size(), 0);
2112
2113 mInFlightMap.removeItemsAt(idx, 1);
2114
2115 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2116 }
2117
2118 // Sanity check - if we have too many in-flight frames, something has
2119 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002120 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002121 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002122 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2123 kInFlightWarnLimitHighSpeed) {
2124 CLOGE("In-flight list too large for high speed configuration: %zu",
2125 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002126 }
2127}
2128
2129
2130void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2131 CaptureResultExtras &resultExtras,
2132 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002133 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002134 bool reprocess,
2135 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002136 if (pendingMetadata.isEmpty())
2137 return;
2138
2139 Mutex::Autolock l(mOutputLock);
2140
2141 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002142 if (reprocess) {
2143 if (frameNumber < mNextReprocessResultFrameNumber) {
2144 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002145 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002146 frameNumber, mNextReprocessResultFrameNumber);
2147 return;
2148 }
2149 mNextReprocessResultFrameNumber = frameNumber + 1;
2150 } else {
2151 if (frameNumber < mNextResultFrameNumber) {
2152 SET_ERR("Out-of-order capture result metadata submitted! "
2153 "(got frame number %d, expecting %d)",
2154 frameNumber, mNextResultFrameNumber);
2155 return;
2156 }
2157 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002158 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002159
2160 CaptureResult captureResult;
2161 captureResult.mResultExtras = resultExtras;
2162 captureResult.mMetadata = pendingMetadata;
2163
2164 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2165 (int32_t*)&frameNumber, 1) != OK) {
2166 SET_ERR("Failed to set frame# in metadata (%d)",
2167 frameNumber);
2168 return;
2169 } else {
2170 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2171 __FUNCTION__, mId, frameNumber);
2172 }
2173
2174 // Append any previous partials to form a complete result
2175 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2176 captureResult.mMetadata.append(collectedPartialResult);
2177 }
2178
2179 captureResult.mMetadata.sort();
2180
2181 // Check that there's a timestamp in the result metadata
2182 camera_metadata_entry entry =
2183 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2184 if (entry.count == 0) {
2185 SET_ERR("No timestamp provided by HAL for frame %d!",
2186 frameNumber);
2187 return;
2188 }
2189
Chien-Yu Chend196d612015-06-22 19:49:01 -07002190 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2191
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002192 // Valid result, insert into queue
2193 List<CaptureResult>::iterator queuedResult =
2194 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2195 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2196 ", burstId = %" PRId32, __FUNCTION__,
2197 queuedResult->mResultExtras.requestId,
2198 queuedResult->mResultExtras.frameNumber,
2199 queuedResult->mResultExtras.burstId);
2200
2201 mResultSignal.signal();
2202}
2203
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002204/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002205 * Camera HAL device callback methods
2206 */
2207
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002208void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002209 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002210
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002211 status_t res;
2212
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002213 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002214 if (result->result == NULL && result->num_output_buffers == 0 &&
2215 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002216 SET_ERR("No result data provided by HAL for frame %d",
2217 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002218 return;
2219 }
Zhijun He204e3292014-07-14 17:09:23 -07002220
2221 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2222 // partial_result to 1 when metadata is included in this result.
2223 if (!mUsePartialResult &&
2224 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2225 result->result != NULL &&
2226 result->partial_result != 1) {
2227 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2228 " if partial result is not supported",
2229 frameNumber, result->partial_result);
2230 return;
2231 }
2232
2233 bool isPartialResult = false;
2234 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002235 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002236 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002237
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002238 // Get shutter timestamp and resultExtras from list of in-flight requests,
2239 // where it was added by the shutter notification for this frame. If the
2240 // shutter timestamp isn't received yet, append the output buffers to the
2241 // in-flight request and they will be returned when the shutter timestamp
2242 // arrives. Update the in-flight status and remove the in-flight entry if
2243 // all result data and shutter timestamp have been received.
2244 nsecs_t shutterTimestamp = 0;
2245
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002246 {
2247 Mutex::Autolock l(mInFlightLock);
2248 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2249 if (idx == NAME_NOT_FOUND) {
2250 SET_ERR("Unknown frame number for capture result: %d",
2251 frameNumber);
2252 return;
2253 }
2254 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002255 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2256 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2257 ", partialResultCount = %d",
2258 __FUNCTION__, request.resultExtras.requestId,
2259 request.resultExtras.frameNumber, request.resultExtras.burstId,
2260 result->partial_result);
2261 // Always update the partial count to the latest one if it's not 0
2262 // (buffers only). When framework aggregates adjacent partial results
2263 // into one, the latest partial count will be used.
2264 if (result->partial_result != 0)
2265 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002266
2267 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002268 if (mUsePartialResult && result->result != NULL) {
2269 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2270 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2271 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2272 " the range of [1, %d] when metadata is included in the result",
2273 frameNumber, result->partial_result, mNumPartialResults);
2274 return;
2275 }
2276 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002277 if (isPartialResult) {
2278 request.partialResult.collectedResult.append(result->result);
2279 }
Zhijun He204e3292014-07-14 17:09:23 -07002280 } else {
2281 camera_metadata_ro_entry_t partialResultEntry;
2282 res = find_camera_metadata_ro_entry(result->result,
2283 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2284 if (res != NAME_NOT_FOUND &&
2285 partialResultEntry.count > 0 &&
2286 partialResultEntry.data.u8[0] ==
2287 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2288 // A partial result. Flag this as such, and collect this
2289 // set of metadata into the in-flight entry.
2290 isPartialResult = true;
2291 request.partialResult.collectedResult.append(
2292 result->result);
2293 request.partialResult.collectedResult.erase(
2294 ANDROID_QUIRKS_PARTIAL_RESULT);
2295 }
2296 }
2297
2298 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002299 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002300 if (!request.partialResult.haveSent3A) {
2301 request.partialResult.haveSent3A =
2302 processPartial3AResult(frameNumber,
2303 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002304 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002305 }
2306 }
2307 }
2308
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002309 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002310 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002311
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002312 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002313 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002314 if (request.haveResultMetadata) {
2315 SET_ERR("Called multiple times with metadata for frame %d",
2316 frameNumber);
2317 return;
2318 }
Zhijun He204e3292014-07-14 17:09:23 -07002319 if (mUsePartialResult &&
2320 !request.partialResult.collectedResult.isEmpty()) {
2321 collectedPartialResult.acquire(
2322 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002323 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002324 request.haveResultMetadata = true;
2325 }
2326
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002327 uint32_t numBuffersReturned = result->num_output_buffers;
2328 if (result->input_buffer != NULL) {
2329 if (hasInputBufferInRequest) {
2330 numBuffersReturned += 1;
2331 } else {
2332 ALOGW("%s: Input buffer should be NULL if there is no input"
2333 " buffer sent in the request",
2334 __FUNCTION__);
2335 }
2336 }
2337 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002338 if (request.numBuffersLeft < 0) {
2339 SET_ERR("Too many buffers returned for frame %d",
2340 frameNumber);
2341 return;
2342 }
2343
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002344 camera_metadata_ro_entry_t entry;
2345 res = find_camera_metadata_ro_entry(result->result,
2346 ANDROID_SENSOR_TIMESTAMP, &entry);
2347 if (res == OK && entry.count == 1) {
2348 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002349 }
2350
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002351 // If shutter event isn't received yet, append the output buffers to
2352 // the in-flight request. Otherwise, return the output buffers to
2353 // streams.
2354 if (shutterTimestamp == 0) {
2355 request.pendingOutputBuffers.appendArray(result->output_buffers,
2356 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002357 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002358 returnOutputBuffers(result->output_buffers,
2359 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002360 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002361
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002362 if (result->result != NULL && !isPartialResult) {
2363 if (shutterTimestamp == 0) {
2364 request.pendingMetadata = result->result;
2365 request.partialResult.collectedResult = collectedPartialResult;
2366 } else {
2367 CameraMetadata metadata;
2368 metadata = result->result;
2369 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002370 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2371 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002372 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002373 }
2374
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002375 removeInFlightRequestIfReadyLocked(idx);
2376 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002377
Zhijun Hef0d962a2014-06-30 10:24:11 -07002378 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002379 if (hasInputBufferInRequest) {
2380 Camera3Stream *stream =
2381 Camera3Stream::cast(result->input_buffer->stream);
2382 res = stream->returnInputBuffer(*(result->input_buffer));
2383 // Note: stream may be deallocated at this point, if this buffer was the
2384 // last reference to it.
2385 if (res != OK) {
2386 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2387 " its stream:%s (%d)", __FUNCTION__,
2388 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002389 }
2390 } else {
2391 ALOGW("%s: Input buffer should be NULL if there is no input"
2392 " buffer sent in the request, skipping input buffer return.",
2393 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002394 }
2395 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002396}
2397
2398void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002399 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002400 NotificationListener *listener;
2401 {
2402 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002403 listener = mListener;
2404 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002405
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002406 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002407 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002408 return;
2409 }
2410
2411 switch (msg->type) {
2412 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002413 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002414 break;
2415 }
2416 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002417 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002418 break;
2419 }
2420 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002421 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002422 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002423 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002424}
2425
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002426void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2427 NotificationListener *listener) {
2428
2429 // Map camera HAL error codes to ICameraDeviceCallback error codes
2430 // Index into this with the HAL error code
2431 static const ICameraDeviceCallbacks::CameraErrorCode
2432 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2433 // 0 = Unused error code
2434 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2435 // 1 = CAMERA3_MSG_ERROR_DEVICE
2436 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2437 // 2 = CAMERA3_MSG_ERROR_REQUEST
2438 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2439 // 3 = CAMERA3_MSG_ERROR_RESULT
2440 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2441 // 4 = CAMERA3_MSG_ERROR_BUFFER
2442 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2443 };
2444
2445 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2446 ((msg.error_code >= 0) &&
2447 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2448 halErrorMap[msg.error_code] :
2449 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2450
2451 int streamId = 0;
2452 if (msg.error_stream != NULL) {
2453 Camera3Stream *stream =
2454 Camera3Stream::cast(msg.error_stream);
2455 streamId = stream->getId();
2456 }
2457 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2458 mId, __FUNCTION__, msg.frame_number,
2459 streamId, msg.error_code);
2460
2461 CaptureResultExtras resultExtras;
2462 switch (errorCode) {
2463 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2464 // SET_ERR calls notifyError
2465 SET_ERR("Camera HAL reported serious device error");
2466 break;
2467 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2468 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2469 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2470 {
2471 Mutex::Autolock l(mInFlightLock);
2472 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2473 if (idx >= 0) {
2474 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2475 r.requestStatus = msg.error_code;
2476 resultExtras = r.resultExtras;
2477 } else {
2478 resultExtras.frameNumber = msg.frame_number;
2479 ALOGE("Camera %d: %s: cannot find in-flight request on "
2480 "frame %" PRId64 " error", mId, __FUNCTION__,
2481 resultExtras.frameNumber);
2482 }
2483 }
2484 if (listener != NULL) {
2485 listener->notifyError(errorCode, resultExtras);
2486 } else {
2487 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2488 }
2489 break;
2490 default:
2491 // SET_ERR calls notifyError
2492 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2493 break;
2494 }
2495}
2496
2497void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2498 NotificationListener *listener) {
2499 ssize_t idx;
2500 // Verify ordering of shutter notifications
2501 {
2502 Mutex::Autolock l(mOutputLock);
2503 // TODO: need to track errors for tighter bounds on expected frame number.
2504 if (msg.frame_number < mNextShutterFrameNumber) {
2505 SET_ERR("Shutter notification out-of-order. Expected "
2506 "notification for frame %d, got frame %d",
2507 mNextShutterFrameNumber, msg.frame_number);
2508 return;
2509 }
2510 mNextShutterFrameNumber = msg.frame_number + 1;
2511 }
2512
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002513 // Set timestamp for the request in the in-flight tracking
2514 // and get the request ID to send upstream
2515 {
2516 Mutex::Autolock l(mInFlightLock);
2517 idx = mInFlightMap.indexOfKey(msg.frame_number);
2518 if (idx >= 0) {
2519 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002520
2521 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2522 mId, __FUNCTION__,
2523 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2524 // Call listener, if any
2525 if (listener != NULL) {
2526 listener->notifyShutter(r.resultExtras, msg.timestamp);
2527 }
2528
2529 r.shutterTimestamp = msg.timestamp;
2530
2531 // send pending result and buffers
2532 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002533 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002534 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002535 returnOutputBuffers(r.pendingOutputBuffers.array(),
2536 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2537 r.pendingOutputBuffers.clear();
2538
2539 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002540 }
2541 }
2542 if (idx < 0) {
2543 SET_ERR("Shutter notification for non-existent frame number %d",
2544 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002545 }
2546}
2547
2548
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002549CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002550 ALOGV("%s", __FUNCTION__);
2551
Igor Murashkin1e479c02013-09-06 16:55:14 -07002552 CameraMetadata retVal;
2553
2554 if (mRequestThread != NULL) {
2555 retVal = mRequestThread->getLatestRequest();
2556 }
2557
Igor Murashkin1e479c02013-09-06 16:55:14 -07002558 return retVal;
2559}
2560
Jianing Weicb0652e2014-03-12 18:29:36 -07002561
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002562/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002563 * RequestThread inner class methods
2564 */
2565
2566Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002567 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002568 camera3_device_t *hal3Device,
2569 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002570 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002571 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002572 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002573 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002574 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002575 mReconfigured(false),
2576 mDoPause(false),
2577 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002578 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002579 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002580 mCurrentAfTriggerId(0),
2581 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002582 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2583 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002584 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002585}
2586
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002587void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002588 NotificationListener *listener) {
2589 Mutex::Autolock l(mRequestLock);
2590 mListener = listener;
2591}
2592
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002593void Camera3Device::RequestThread::configurationComplete() {
2594 Mutex::Autolock l(mRequestLock);
2595 mReconfigured = true;
2596}
2597
Jianing Wei90e59c92014-03-12 18:29:36 -07002598status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002599 List<sp<CaptureRequest> > &requests,
2600 /*out*/
2601 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002602 Mutex::Autolock l(mRequestLock);
2603 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2604 ++it) {
2605 mRequestQueue.push_back(*it);
2606 }
2607
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002608 if (lastFrameNumber != NULL) {
2609 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2610 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2611 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2612 *lastFrameNumber);
2613 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002614
Jianing Wei90e59c92014-03-12 18:29:36 -07002615 unpauseForNewRequests();
2616
2617 return OK;
2618}
2619
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002620
2621status_t Camera3Device::RequestThread::queueTrigger(
2622 RequestTrigger trigger[],
2623 size_t count) {
2624
2625 Mutex::Autolock l(mTriggerMutex);
2626 status_t ret;
2627
2628 for (size_t i = 0; i < count; ++i) {
2629 ret = queueTriggerLocked(trigger[i]);
2630
2631 if (ret != OK) {
2632 return ret;
2633 }
2634 }
2635
2636 return OK;
2637}
2638
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002639int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2640 sp<Camera3Device> d = device.promote();
2641 if (d != NULL) return d->mId;
2642 return 0;
2643}
2644
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002645status_t Camera3Device::RequestThread::queueTriggerLocked(
2646 RequestTrigger trigger) {
2647
2648 uint32_t tag = trigger.metadataTag;
2649 ssize_t index = mTriggerMap.indexOfKey(tag);
2650
2651 switch (trigger.getTagType()) {
2652 case TYPE_BYTE:
2653 // fall-through
2654 case TYPE_INT32:
2655 break;
2656 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002657 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2658 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002659 return INVALID_OPERATION;
2660 }
2661
2662 /**
2663 * Collect only the latest trigger, since we only have 1 field
2664 * in the request settings per trigger tag, and can't send more than 1
2665 * trigger per request.
2666 */
2667 if (index != NAME_NOT_FOUND) {
2668 mTriggerMap.editValueAt(index) = trigger;
2669 } else {
2670 mTriggerMap.add(tag, trigger);
2671 }
2672
2673 return OK;
2674}
2675
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002676status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002677 const RequestList &requests,
2678 /*out*/
2679 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002680 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002681 if (lastFrameNumber != NULL) {
2682 *lastFrameNumber = mRepeatingLastFrameNumber;
2683 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002684 mRepeatingRequests.clear();
2685 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2686 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002687
2688 unpauseForNewRequests();
2689
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002690 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002691 return OK;
2692}
2693
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002694bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2695 if (mRepeatingRequests.empty()) {
2696 return false;
2697 }
2698 int32_t requestId = requestIn->mResultExtras.requestId;
2699 const RequestList &repeatRequests = mRepeatingRequests;
2700 // All repeating requests are guaranteed to have same id so only check first quest
2701 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2702 return (firstRequest->mResultExtras.requestId == requestId);
2703}
2704
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002705status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002706 Mutex::Autolock l(mRequestLock);
2707 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002708 if (lastFrameNumber != NULL) {
2709 *lastFrameNumber = mRepeatingLastFrameNumber;
2710 }
2711 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002712 return OK;
2713}
2714
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002715status_t Camera3Device::RequestThread::clear(
2716 NotificationListener *listener,
2717 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002718 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002719 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002720
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002721 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002722
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002723 // Send errors for all requests pending in the request queue, including
2724 // pending repeating requests
2725 if (listener != NULL) {
2726 for (RequestList::iterator it = mRequestQueue.begin();
2727 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002728 // Abort the input buffers for reprocess requests.
2729 if ((*it)->mInputStream != NULL) {
2730 camera3_stream_buffer_t inputBuffer;
2731 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2732 if (res != OK) {
2733 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2734 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2735 } else {
2736 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2737 if (res != OK) {
2738 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2739 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2740 }
2741 }
2742 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002743 // Set the frame number this request would have had, if it
2744 // had been submitted; this frame number will not be reused.
2745 // The requestId and burstId fields were set when the request was
2746 // submitted originally (in convertMetadataListToRequestListLocked)
2747 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2748 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2749 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002750 }
2751 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002752 mRequestQueue.clear();
2753 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002754 if (lastFrameNumber != NULL) {
2755 *lastFrameNumber = mRepeatingLastFrameNumber;
2756 }
2757 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002758 return OK;
2759}
2760
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002761void Camera3Device::RequestThread::setPaused(bool paused) {
2762 Mutex::Autolock l(mPauseLock);
2763 mDoPause = paused;
2764 mDoPauseSignal.signal();
2765}
2766
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002767status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2768 int32_t requestId, nsecs_t timeout) {
2769 Mutex::Autolock l(mLatestRequestMutex);
2770 status_t res;
2771 while (mLatestRequestId != requestId) {
2772 nsecs_t startTime = systemTime();
2773
2774 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2775 if (res != OK) return res;
2776
2777 timeout -= (systemTime() - startTime);
2778 }
2779
2780 return OK;
2781}
2782
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002783void Camera3Device::RequestThread::requestExit() {
2784 // Call parent to set up shutdown
2785 Thread::requestExit();
2786 // The exit from any possible waits
2787 mDoPauseSignal.signal();
2788 mRequestSignal.signal();
2789}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002790
Chien-Yu Chend196d612015-06-22 19:49:01 -07002791
2792/**
2793 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2794 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2795 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2796 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2797 * request.
2798 */
2799void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2800 request->mAeTriggerCancelOverride.applyAeLock = false;
2801 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2802
2803 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2804 return;
2805 }
2806
2807 camera_metadata_entry_t aePrecaptureTrigger =
2808 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2809 if (aePrecaptureTrigger.count > 0 &&
2810 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2811 // Always override CANCEL to IDLE
2812 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2813 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2814 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2815 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2816 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2817
2818 if (mAeLockAvailable == true) {
2819 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2820 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2821 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2822 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2823 request->mAeTriggerCancelOverride.applyAeLock = true;
2824 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2825 }
2826 }
2827 }
2828}
2829
2830/**
2831 * Override result metadata for cancelling AE precapture trigger applied in
2832 * handleAePrecaptureCancelRequest().
2833 */
2834void Camera3Device::overrideResultForPrecaptureCancel(
2835 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2836 if (aeTriggerCancelOverride.applyAeLock) {
2837 // Only devices <= v3.2 should have this override
2838 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2839 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2840 }
2841
2842 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2843 // Only devices <= v3.2 should have this override
2844 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2845 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2846 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2847 }
2848}
2849
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002850bool Camera3Device::RequestThread::threadLoop() {
2851
2852 status_t res;
2853
2854 // Handle paused state.
2855 if (waitIfPaused()) {
2856 return true;
2857 }
2858
2859 // Get work to do
2860
2861 sp<CaptureRequest> nextRequest = waitForNextRequest();
2862 if (nextRequest == NULL) {
2863 return true;
2864 }
2865
2866 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002867 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002868 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002869 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002870
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002871 // Get the request ID, if any
2872 int requestId;
2873 camera_metadata_entry_t requestIdEntry =
2874 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2875 if (requestIdEntry.count > 0) {
2876 requestId = requestIdEntry.data.i32[0];
2877 } else {
2878 ALOGW("%s: Did not have android.request.id set in the request",
2879 __FUNCTION__);
2880 requestId = NAME_NOT_FOUND;
2881 }
2882
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002883 // Insert any queued triggers (before metadata is locked)
2884 int32_t triggerCount;
2885 res = insertTriggers(nextRequest);
2886 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002887 SET_ERR("RequestThread: Unable to insert triggers "
2888 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002889 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002890 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2891 return false;
2892 }
2893 triggerCount = res;
2894
2895 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2896
2897 // If the request is the same as last, or we had triggers last time
2898 if (mPrevRequest != nextRequest || triggersMixedIn) {
2899 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002900 * HAL workaround:
2901 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2902 */
2903 res = addDummyTriggerIds(nextRequest);
2904 if (res != OK) {
2905 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2906 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002907 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002908 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2909 return false;
2910 }
2911
2912 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002913 * The request should be presorted so accesses in HAL
2914 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2915 */
2916 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002917 request.settings = nextRequest->mSettings.getAndLock();
2918 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002919 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2920
2921 IF_ALOGV() {
2922 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2923 find_camera_metadata_ro_entry(
2924 request.settings,
2925 ANDROID_CONTROL_AF_TRIGGER,
2926 &e
2927 );
2928 if (e.count > 0) {
2929 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2930 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002931 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002932 e.data.u8[0]);
2933 }
2934 }
2935 } else {
2936 // leave request.settings NULL to indicate 'reuse latest given'
2937 ALOGVV("%s: Request settings are REUSED",
2938 __FUNCTION__);
2939 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002940
Zhijun Hef0d962a2014-06-30 10:24:11 -07002941 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002942
2943 // Fill in buffers
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002944 if (nextRequest->mInputStream != NULL) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002945 request.input_buffer = &nextRequest->mInputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002946 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002947 } else {
2948 request.input_buffer = NULL;
2949 }
2950
2951 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2952 nextRequest->mOutputStreams.size());
2953 request.output_buffers = outputBuffers.array();
2954 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2955 res = nextRequest->mOutputStreams.editItemAt(i)->
2956 getBuffer(&outputBuffers.editItemAt(i));
2957 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002958 // Can't get output buffer from gralloc queue - this could be due to
2959 // abandoned queue or other consumer misbehavior, so not a fatal
2960 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002961 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2962 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala0ec23d32015-05-28 15:15:54 -07002963 {
2964 Mutex::Autolock l(mRequestLock);
2965 if (mListener != NULL) {
2966 mListener->notifyError(
2967 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2968 nextRequest->mResultExtras);
2969 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002970 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002971 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2972 return true;
2973 }
2974 request.num_output_buffers++;
2975 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002976 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002977
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002978 // Log request in the in-flight queue
2979 sp<Camera3Device> parent = mParent.promote();
2980 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002981 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002982 CLOGE("RequestThread: Parent is gone");
2983 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2984 return false;
2985 }
2986
Jianing Weicb0652e2014-03-12 18:29:36 -07002987 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002988 totalNumBuffers, nextRequest->mResultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002989 /*hasInput*/request.input_buffer != NULL,
2990 nextRequest->mAeTriggerCancelOverride);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002991 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2992 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002993 __FUNCTION__,
2994 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2995 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002996 if (res != OK) {
2997 SET_ERR("RequestThread: Unable to register new in-flight request:"
2998 " %s (%d)", strerror(-res), res);
2999 cleanUpFailedRequest(request, nextRequest, outputBuffers);
3000 return false;
3001 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003002
Zhijun Hecc27e112013-10-03 16:12:43 -07003003 // Inform waitUntilRequestProcessed thread of a new request ID
3004 {
3005 Mutex::Autolock al(mLatestRequestMutex);
3006
3007 mLatestRequestId = requestId;
3008 mLatestRequestSignal.signal();
3009 }
3010
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003011 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003012 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
3013 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003014 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003015 ATRACE_END();
3016
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003017 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003018 // Should only get a failure here for malformed requests or device-level
3019 // errors, so consider all errors fatal. Bad metadata failures should
3020 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003021 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003022 " device: %s (%d)", request.frame_number, strerror(-res), res);
3023 cleanUpFailedRequest(request, nextRequest, outputBuffers);
3024 return false;
3025 }
3026
Igor Murashkin1e479c02013-09-06 16:55:14 -07003027 // Update the latest request sent to HAL
3028 if (request.settings != NULL) { // Don't update them if they were unchanged
3029 Mutex::Autolock al(mLatestRequestMutex);
3030
3031 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
3032 mLatestRequest.acquire(cloned);
3033 }
3034
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003035 if (request.settings != NULL) {
3036 nextRequest->mSettings.unlock(request.settings);
3037 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003038
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003039 // Unset as current request
3040 {
3041 Mutex::Autolock l(mRequestLock);
3042 mNextRequest.clear();
3043 }
3044
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003045 // Remove any previously queued triggers (after unlock)
3046 res = removeTriggers(mPrevRequest);
3047 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003048 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003049 "(capture request %d, HAL device: %s (%d)",
3050 request.frame_number, strerror(-res), res);
3051 return false;
3052 }
3053 mPrevTriggers = triggerCount;
3054
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003055 return true;
3056}
3057
Igor Murashkin1e479c02013-09-06 16:55:14 -07003058CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3059 Mutex::Autolock al(mLatestRequestMutex);
3060
3061 ALOGV("RequestThread::%s", __FUNCTION__);
3062
3063 return mLatestRequest;
3064}
3065
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003066bool Camera3Device::RequestThread::isStreamPending(
3067 sp<Camera3StreamInterface>& stream) {
3068 Mutex::Autolock l(mRequestLock);
3069
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003070 if (mNextRequest != nullptr) {
3071 for (const auto& s : mNextRequest->mOutputStreams) {
3072 if (stream == s) return true;
3073 }
3074 if (stream == mNextRequest->mInputStream) return true;
3075 }
3076
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003077 for (const auto& request : mRequestQueue) {
3078 for (const auto& s : request->mOutputStreams) {
3079 if (stream == s) return true;
3080 }
3081 if (stream == request->mInputStream) return true;
3082 }
3083
3084 for (const auto& request : mRepeatingRequests) {
3085 for (const auto& s : request->mOutputStreams) {
3086 if (stream == s) return true;
3087 }
3088 if (stream == request->mInputStream) return true;
3089 }
3090
3091 return false;
3092}
Jianing Weicb0652e2014-03-12 18:29:36 -07003093
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003094void Camera3Device::RequestThread::cleanUpFailedRequest(
3095 camera3_capture_request_t &request,
3096 sp<CaptureRequest> &nextRequest,
3097 Vector<camera3_stream_buffer_t> &outputBuffers) {
3098
3099 if (request.settings != NULL) {
3100 nextRequest->mSettings.unlock(request.settings);
3101 }
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003102 if (nextRequest->mInputStream != NULL) {
3103 nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3104 nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003105 }
3106 for (size_t i = 0; i < request.num_output_buffers; i++) {
3107 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3108 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
3109 outputBuffers[i], 0);
3110 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003111
3112 Mutex::Autolock l(mRequestLock);
3113 mNextRequest.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003114}
3115
3116sp<Camera3Device::CaptureRequest>
3117 Camera3Device::RequestThread::waitForNextRequest() {
3118 status_t res;
3119 sp<CaptureRequest> nextRequest;
3120
3121 // Optimized a bit for the simple steady-state case (single repeating
3122 // request), to avoid putting that request in the queue temporarily.
3123 Mutex::Autolock l(mRequestLock);
3124
3125 while (mRequestQueue.empty()) {
3126 if (!mRepeatingRequests.empty()) {
3127 // Always atomically enqueue all requests in a repeating request
3128 // list. Guarantees a complete in-sequence set of captures to
3129 // application.
3130 const RequestList &requests = mRepeatingRequests;
3131 RequestList::const_iterator firstRequest =
3132 requests.begin();
3133 nextRequest = *firstRequest;
3134 mRequestQueue.insert(mRequestQueue.end(),
3135 ++firstRequest,
3136 requests.end());
3137 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003138
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003139 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003140
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003141 break;
3142 }
3143
3144 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3145
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003146 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3147 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003148 Mutex::Autolock pl(mPauseLock);
3149 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003150 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003151 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003152 // Let the tracker know
3153 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3154 if (statusTracker != 0) {
3155 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3156 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003157 }
3158 // Stop waiting for now and let thread management happen
3159 return NULL;
3160 }
3161 }
3162
3163 if (nextRequest == NULL) {
3164 // Don't have a repeating request already in hand, so queue
3165 // must have an entry now.
3166 RequestList::iterator firstRequest =
3167 mRequestQueue.begin();
3168 nextRequest = *firstRequest;
3169 mRequestQueue.erase(firstRequest);
3170 }
3171
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003172 // In case we've been unpaused by setPaused clearing mDoPause, need to
3173 // update internal pause state (capture/setRepeatingRequest unpause
3174 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003175 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003176 if (mPaused) {
3177 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3178 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3179 if (statusTracker != 0) {
3180 statusTracker->markComponentActive(mStatusId);
3181 }
3182 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003183 mPaused = false;
3184
3185 // Check if we've reconfigured since last time, and reset the preview
3186 // request if so. Can't use 'NULL request == repeat' across configure calls.
3187 if (mReconfigured) {
3188 mPrevRequest.clear();
3189 mReconfigured = false;
3190 }
3191
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003192 if (nextRequest != NULL) {
3193 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003194 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3195 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003196
3197 // Since RequestThread::clear() removes buffers from the input stream,
3198 // get the right buffer here before unlocking mRequestLock
3199 if (nextRequest->mInputStream != NULL) {
3200 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3201 if (res != OK) {
3202 // Can't get input buffer from gralloc queue - this could be due to
3203 // disconnected queue or other producer misbehavior, so not a fatal
3204 // error
3205 ALOGE("%s: Can't get input buffer, skipping request:"
3206 " %s (%d)", __FUNCTION__, strerror(-res), res);
3207 if (mListener != NULL) {
3208 mListener->notifyError(
3209 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3210 nextRequest->mResultExtras);
3211 }
3212 return NULL;
3213 }
3214 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003215 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003216
3217 handleAePrecaptureCancelRequest(nextRequest);
3218
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003219 mNextRequest = nextRequest;
3220
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003221 return nextRequest;
3222}
3223
3224bool Camera3Device::RequestThread::waitIfPaused() {
3225 status_t res;
3226 Mutex::Autolock l(mPauseLock);
3227 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003228 if (mPaused == false) {
3229 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003230 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3231 // Let the tracker know
3232 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3233 if (statusTracker != 0) {
3234 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3235 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003236 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003237
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003238 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003239 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003240 return true;
3241 }
3242 }
3243 // We don't set mPaused to false here, because waitForNextRequest needs
3244 // to further manage the paused state in case of starvation.
3245 return false;
3246}
3247
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003248void Camera3Device::RequestThread::unpauseForNewRequests() {
3249 // With work to do, mark thread as unpaused.
3250 // If paused by request (setPaused), don't resume, to avoid
3251 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003252 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003253 Mutex::Autolock p(mPauseLock);
3254 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003255 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3256 if (mPaused) {
3257 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3258 if (statusTracker != 0) {
3259 statusTracker->markComponentActive(mStatusId);
3260 }
3261 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003262 mPaused = false;
3263 }
3264}
3265
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003266void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3267 sp<Camera3Device> parent = mParent.promote();
3268 if (parent != NULL) {
3269 va_list args;
3270 va_start(args, fmt);
3271
3272 parent->setErrorStateV(fmt, args);
3273
3274 va_end(args);
3275 }
3276}
3277
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003278status_t Camera3Device::RequestThread::insertTriggers(
3279 const sp<CaptureRequest> &request) {
3280
3281 Mutex::Autolock al(mTriggerMutex);
3282
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003283 sp<Camera3Device> parent = mParent.promote();
3284 if (parent == NULL) {
3285 CLOGE("RequestThread: Parent is gone");
3286 return DEAD_OBJECT;
3287 }
3288
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003289 CameraMetadata &metadata = request->mSettings;
3290 size_t count = mTriggerMap.size();
3291
3292 for (size_t i = 0; i < count; ++i) {
3293 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003294 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003295
3296 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3297 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3298 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003299 if (isAeTrigger) {
3300 request->mResultExtras.precaptureTriggerId = triggerId;
3301 mCurrentPreCaptureTriggerId = triggerId;
3302 } else {
3303 request->mResultExtras.afTriggerId = triggerId;
3304 mCurrentAfTriggerId = triggerId;
3305 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003306 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3307 continue; // Trigger ID tag is deprecated since device HAL 3.2
3308 }
3309 }
3310
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003311 camera_metadata_entry entry = metadata.find(tag);
3312
3313 if (entry.count > 0) {
3314 /**
3315 * Already has an entry for this trigger in the request.
3316 * Rewrite it with our requested trigger value.
3317 */
3318 RequestTrigger oldTrigger = trigger;
3319
3320 oldTrigger.entryValue = entry.data.u8[0];
3321
3322 mTriggerReplacedMap.add(tag, oldTrigger);
3323 } else {
3324 /**
3325 * More typical, no trigger entry, so we just add it
3326 */
3327 mTriggerRemovedMap.add(tag, trigger);
3328 }
3329
3330 status_t res;
3331
3332 switch (trigger.getTagType()) {
3333 case TYPE_BYTE: {
3334 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3335 res = metadata.update(tag,
3336 &entryValue,
3337 /*count*/1);
3338 break;
3339 }
3340 case TYPE_INT32:
3341 res = metadata.update(tag,
3342 &trigger.entryValue,
3343 /*count*/1);
3344 break;
3345 default:
3346 ALOGE("%s: Type not supported: 0x%x",
3347 __FUNCTION__,
3348 trigger.getTagType());
3349 return INVALID_OPERATION;
3350 }
3351
3352 if (res != OK) {
3353 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3354 ", value %d", __FUNCTION__, trigger.getTagName(),
3355 trigger.entryValue);
3356 return res;
3357 }
3358
3359 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3360 trigger.getTagName(),
3361 trigger.entryValue);
3362 }
3363
3364 mTriggerMap.clear();
3365
3366 return count;
3367}
3368
3369status_t Camera3Device::RequestThread::removeTriggers(
3370 const sp<CaptureRequest> &request) {
3371 Mutex::Autolock al(mTriggerMutex);
3372
3373 CameraMetadata &metadata = request->mSettings;
3374
3375 /**
3376 * Replace all old entries with their old values.
3377 */
3378 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3379 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3380
3381 status_t res;
3382
3383 uint32_t tag = trigger.metadataTag;
3384 switch (trigger.getTagType()) {
3385 case TYPE_BYTE: {
3386 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3387 res = metadata.update(tag,
3388 &entryValue,
3389 /*count*/1);
3390 break;
3391 }
3392 case TYPE_INT32:
3393 res = metadata.update(tag,
3394 &trigger.entryValue,
3395 /*count*/1);
3396 break;
3397 default:
3398 ALOGE("%s: Type not supported: 0x%x",
3399 __FUNCTION__,
3400 trigger.getTagType());
3401 return INVALID_OPERATION;
3402 }
3403
3404 if (res != OK) {
3405 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3406 ", trigger value %d", __FUNCTION__,
3407 trigger.getTagName(), trigger.entryValue);
3408 return res;
3409 }
3410 }
3411 mTriggerReplacedMap.clear();
3412
3413 /**
3414 * Remove all new entries.
3415 */
3416 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3417 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3418 status_t res = metadata.erase(trigger.metadataTag);
3419
3420 if (res != OK) {
3421 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3422 ", trigger value %d", __FUNCTION__,
3423 trigger.getTagName(), trigger.entryValue);
3424 return res;
3425 }
3426 }
3427 mTriggerRemovedMap.clear();
3428
3429 return OK;
3430}
3431
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003432status_t Camera3Device::RequestThread::addDummyTriggerIds(
3433 const sp<CaptureRequest> &request) {
3434 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3435 static const int32_t dummyTriggerId = 1;
3436 status_t res;
3437
3438 CameraMetadata &metadata = request->mSettings;
3439
3440 // If AF trigger is active, insert a dummy AF trigger ID if none already
3441 // exists
3442 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3443 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3444 if (afTrigger.count > 0 &&
3445 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3446 afId.count == 0) {
3447 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3448 if (res != OK) return res;
3449 }
3450
3451 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3452 // if none already exists
3453 camera_metadata_entry pcTrigger =
3454 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3455 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3456 if (pcTrigger.count > 0 &&
3457 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3458 pcId.count == 0) {
3459 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3460 &dummyTriggerId, 1);
3461 if (res != OK) return res;
3462 }
3463
3464 return OK;
3465}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003466
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003467/**
3468 * PreparerThread inner class methods
3469 */
3470
3471Camera3Device::PreparerThread::PreparerThread() :
3472 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3473}
3474
3475Camera3Device::PreparerThread::~PreparerThread() {
3476 Thread::requestExitAndWait();
3477 if (mCurrentStream != nullptr) {
3478 mCurrentStream->cancelPrepare();
3479 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3480 mCurrentStream.clear();
3481 }
3482 clear();
3483}
3484
Ruben Brunkc78ac262015-08-13 17:58:46 -07003485status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003486 status_t res;
3487
3488 Mutex::Autolock l(mLock);
3489
Ruben Brunkc78ac262015-08-13 17:58:46 -07003490 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003491 if (res == OK) {
3492 // No preparation needed, fire listener right off
3493 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3494 if (mListener) {
3495 mListener->notifyPrepared(stream->getId());
3496 }
3497 return OK;
3498 } else if (res != NOT_ENOUGH_DATA) {
3499 return res;
3500 }
3501
3502 // Need to prepare, start up thread if necessary
3503 if (!mActive) {
3504 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3505 // isn't running
3506 Thread::requestExitAndWait();
3507 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3508 if (res != OK) {
3509 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3510 if (mListener) {
3511 mListener->notifyPrepared(stream->getId());
3512 }
3513 return res;
3514 }
3515 mCancelNow = false;
3516 mActive = true;
3517 ALOGV("%s: Preparer stream started", __FUNCTION__);
3518 }
3519
3520 // queue up the work
3521 mPendingStreams.push_back(stream);
3522 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3523
3524 return OK;
3525}
3526
3527status_t Camera3Device::PreparerThread::clear() {
3528 status_t res;
3529
3530 Mutex::Autolock l(mLock);
3531
3532 for (const auto& stream : mPendingStreams) {
3533 stream->cancelPrepare();
3534 }
3535 mPendingStreams.clear();
3536 mCancelNow = true;
3537
3538 return OK;
3539}
3540
3541void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3542 Mutex::Autolock l(mLock);
3543 mListener = listener;
3544}
3545
3546bool Camera3Device::PreparerThread::threadLoop() {
3547 status_t res;
3548 {
3549 Mutex::Autolock l(mLock);
3550 if (mCurrentStream == nullptr) {
3551 // End thread if done with work
3552 if (mPendingStreams.empty()) {
3553 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3554 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3555 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3556 mActive = false;
3557 return false;
3558 }
3559
3560 // Get next stream to prepare
3561 auto it = mPendingStreams.begin();
3562 mCurrentStream = *it;
3563 mPendingStreams.erase(it);
3564 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3565 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3566 } else if (mCancelNow) {
3567 mCurrentStream->cancelPrepare();
3568 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3569 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3570 mCurrentStream.clear();
3571 mCancelNow = false;
3572 return true;
3573 }
3574 }
3575
3576 res = mCurrentStream->prepareNextBuffer();
3577 if (res == NOT_ENOUGH_DATA) return true;
3578 if (res != OK) {
3579 // Something bad happened; try to recover by cancelling prepare and
3580 // signalling listener anyway
3581 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3582 mCurrentStream->getId(), res, strerror(-res));
3583 mCurrentStream->cancelPrepare();
3584 }
3585
3586 // This stream has finished, notify listener
3587 Mutex::Autolock l(mLock);
3588 if (mListener) {
3589 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3590 mCurrentStream->getId());
3591 mListener->notifyPrepared(mCurrentStream->getId());
3592 }
3593
3594 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3595 mCurrentStream.clear();
3596
3597 return true;
3598}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003599
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003600/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003601 * Static callback forwarding methods from HAL to instance
3602 */
3603
3604void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3605 const camera3_capture_result *result) {
3606 Camera3Device *d =
3607 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003608
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003609 d->processCaptureResult(result);
3610}
3611
3612void Camera3Device::sNotify(const camera3_callback_ops *cb,
3613 const camera3_notify_msg *msg) {
3614 Camera3Device *d =
3615 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3616 d->notify(msg);
3617}
3618
3619}; // namespace android