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