blob: ea1e5f6654747b15bd6e560aeefd47ec0bacd0d6 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Igor Murashkinff3e31d2013-10-23 16:40:06 -070046#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070047#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070048#include "device3/Camera3Device.h"
49#include "device3/Camera3OutputStream.h"
50#include "device3/Camera3InputStream.h"
51#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070052#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070053#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080054
55using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080056
57namespace android {
58
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080059Camera3Device::Camera3Device(int id):
60 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070061 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080062 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070063 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070064 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070065 mUsePartialResult(false),
66 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070067 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070068 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070069 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070070 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070071 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080072{
73 ATRACE_CALL();
74 camera3_callback_ops::notify = &sNotify;
75 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
76 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
77}
78
79Camera3Device::~Camera3Device()
80{
81 ATRACE_CALL();
82 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
83 disconnect();
84}
85
Igor Murashkin71381052013-03-04 14:53:08 -080086int Camera3Device::getId() const {
87 return mId;
88}
89
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080090/**
91 * CameraDeviceBase interface
92 */
93
Yin-Chia Yehe074a932015-01-30 10:29:02 -080094status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080095{
96 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070097 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080098 Mutex::Autolock l(mLock);
99
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800100 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800101 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700102 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 return INVALID_OPERATION;
104 }
105
106 /** Open HAL device */
107
108 status_t res;
109 String8 deviceName = String8::format("%d", mId);
110
111 camera3_device_t *device;
112
Zhijun He213ce792013-11-19 08:45:15 -0800113 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800114 res = module->open(deviceName.string(),
115 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800116 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117
118 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700119 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800120 return res;
121 }
122
123 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700124 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700125 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700126 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700127 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800128 device->common.version);
129 device->common.close(&device->common);
130 return BAD_VALUE;
131 }
132
133 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800134 res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700135 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800136 if (res != OK) return res;
137
138 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700139 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
140 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700141 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800142 device->common.close(&device->common);
143 return BAD_VALUE;
144 }
145
146 /** Initialize device with callback functions */
147
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700148 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800149 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700150 ATRACE_END();
151
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800152 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700153 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
154 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800155 device->common.close(&device->common);
156 return BAD_VALUE;
157 }
158
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700159 /** Start up status tracker thread */
160 mStatusTracker = new StatusTracker(this);
161 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
162 if (res != OK) {
163 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
164 strerror(-res), res);
165 device->common.close(&device->common);
166 mStatusTracker.clear();
167 return res;
168 }
169
Zhijun He125684a2015-12-26 15:07:30 -0800170 /** Create buffer manager */
171 mBufferManager = new Camera3BufferManager();
172
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700173 bool aeLockAvailable = false;
174 camera_metadata_ro_entry aeLockAvailableEntry;
175 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
176 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
177 if (res == OK && aeLockAvailableEntry.count > 0) {
178 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
179 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
180 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800181
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700182 /** Start up request queue thread */
183 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800184 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800185 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700186 SET_ERR_L("Unable to start request queue thread: %s (%d)",
187 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800188 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800189 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190 return res;
191 }
192
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700193 mPreparerThread = new PreparerThread();
194
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800195 /** Everything is good to go */
196
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700197 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800198 mDeviceInfo = info.static_camera_characteristics;
199 mHal3Device = device;
Ruben Brunk183f0562015-08-12 12:55:02 -0700200
201 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800202 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700203 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700204 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700205 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800206
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700207 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700208 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
209 camera_metadata_entry partialResultsCount =
210 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
211 if (partialResultsCount.count > 0) {
212 mNumPartialResults = partialResultsCount.data.i32[0];
213 mUsePartialResult = (mNumPartialResults > 1);
214 }
215 } else {
216 camera_metadata_entry partialResultsQuirk =
217 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
218 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
219 mUsePartialResult = true;
220 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700221 }
222
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700223 camera_metadata_entry configs =
224 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
225 for (uint32_t i = 0; i < configs.count; i += 4) {
226 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
227 configs.data.i32[i + 3] ==
228 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
229 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
230 configs.data.i32[i + 2]));
231 }
232 }
233
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800234 return OK;
235}
236
237status_t Camera3Device::disconnect() {
238 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700239 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800240
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800241 ALOGV("%s: E", __FUNCTION__);
242
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700243 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800244
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700245 {
246 Mutex::Autolock l(mLock);
247 if (mStatus == STATUS_UNINITIALIZED) return res;
248
249 if (mStatus == STATUS_ACTIVE ||
250 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
251 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700252 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700253 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700254 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700255 } else {
256 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
257 if (res != OK) {
258 SET_ERR_L("Timeout waiting for HAL to drain");
259 // Continue to close device even in case of error
260 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700261 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800262 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800263
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700264 if (mStatus == STATUS_ERROR) {
265 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700266 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700267
268 if (mStatusTracker != NULL) {
269 mStatusTracker->requestExit();
270 }
271
272 if (mRequestThread != NULL) {
273 mRequestThread->requestExit();
274 }
275
276 mOutputStreams.clear();
277 mInputStream.clear();
278 }
279
280 // Joining done without holding mLock, otherwise deadlocks may ensue
281 // as the threads try to access parent state
282 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
283 // HAL may be in a bad state, so waiting for request thread
284 // (which may be stuck in the HAL processCaptureRequest call)
285 // could be dangerous.
286 mRequestThread->join();
287 }
288
289 if (mStatusTracker != NULL) {
290 mStatusTracker->join();
291 }
292
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700293 camera3_device_t *hal3Device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700294 {
295 Mutex::Autolock l(mLock);
296
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800297 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700298 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800299 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800300
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700301 hal3Device = mHal3Device;
302 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800303
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700304 // Call close without internal mutex held, as the HAL close may need to
305 // wait on assorted callbacks,etc, to complete before it can return.
306 if (hal3Device != NULL) {
307 ATRACE_BEGIN("camera3->close");
308 hal3Device->common.close(&hal3Device->common);
309 ATRACE_END();
310 }
311
312 {
313 Mutex::Autolock l(mLock);
314 mHal3Device = NULL;
Ruben Brunk183f0562015-08-12 12:55:02 -0700315 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700316 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800317
318 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700319 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800320}
321
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700322// For dumping/debugging only -
323// try to acquire a lock a few times, eventually give up to proceed with
324// debug/dump operations
325bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
326 bool gotLock = false;
327 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
328 if (lock.tryLock() == NO_ERROR) {
329 gotLock = true;
330 break;
331 } else {
332 usleep(kDumpSleepDuration);
333 }
334 }
335 return gotLock;
336}
337
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700338Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
339 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
340 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
341 const int STREAM_CONFIGURATION_SIZE = 4;
342 const int STREAM_FORMAT_OFFSET = 0;
343 const int STREAM_WIDTH_OFFSET = 1;
344 const int STREAM_HEIGHT_OFFSET = 2;
345 const int STREAM_IS_INPUT_OFFSET = 3;
346 camera_metadata_ro_entry_t availableStreamConfigs =
347 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
348 if (availableStreamConfigs.count == 0 ||
349 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
350 return Size(0, 0);
351 }
352
353 // Get max jpeg size (area-wise).
354 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
355 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
356 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
357 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
358 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
359 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
360 && format == HAL_PIXEL_FORMAT_BLOB &&
361 (width * height > maxJpegWidth * maxJpegHeight)) {
362 maxJpegWidth = width;
363 maxJpegHeight = height;
364 }
365 }
366 } else {
367 camera_metadata_ro_entry availableJpegSizes =
368 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
369 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
370 return Size(0, 0);
371 }
372
373 // Get max jpeg size (area-wise).
374 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
375 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
376 > (maxJpegWidth * maxJpegHeight)) {
377 maxJpegWidth = availableJpegSizes.data.i32[i];
378 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
379 }
380 }
381 }
382 return Size(maxJpegWidth, maxJpegHeight);
383}
384
Zhijun Hef7da0962014-04-24 13:27:56 -0700385ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700386 // Get max jpeg size (area-wise).
387 Size maxJpegResolution = getMaxJpegResolution();
388 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700389 ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
Zhijun Hef7da0962014-04-24 13:27:56 -0700390 __FUNCTION__, mId);
391 return BAD_VALUE;
392 }
393
Zhijun Hef7da0962014-04-24 13:27:56 -0700394 // Get max jpeg buffer size
395 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700396 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
397 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700398 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
399 return BAD_VALUE;
400 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700401 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800402 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700403
404 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700405 float scaleFactor = ((float) (width * height)) /
406 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800407 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
408 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700409 if (jpegBufferSize > maxJpegBufferSize) {
410 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700411 }
412
413 return jpegBufferSize;
414}
415
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700416ssize_t Camera3Device::getPointCloudBufferSize() const {
417 const int FLOATS_PER_POINT=4;
418 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
419 if (maxPointCount.count == 0) {
420 ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
421 __FUNCTION__, mId);
422 return BAD_VALUE;
423 }
424 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
425 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
426 return maxBytesForPointCloud;
427}
428
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800429ssize_t Camera3Device::getRawOpaqueBufferSize(uint32_t width, uint32_t height) const {
430 const int PER_CONFIGURATION_SIZE = 3;
431 const int WIDTH_OFFSET = 0;
432 const int HEIGHT_OFFSET = 1;
433 const int SIZE_OFFSET = 2;
434 camera_metadata_ro_entry rawOpaqueSizes =
435 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
436 int count = rawOpaqueSizes.count;
437 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
438 ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%d)!",
439 __FUNCTION__, mId, count);
440 return BAD_VALUE;
441 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700442
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800443 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
444 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
445 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
446 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
447 }
448 }
449
450 ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
451 __FUNCTION__, mId, width, height);
452 return BAD_VALUE;
453}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700454
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800455status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
456 ATRACE_CALL();
457 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700458
459 // Try to lock, but continue in case of failure (to avoid blocking in
460 // deadlocks)
461 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
462 bool gotLock = tryLockSpinRightRound(mLock);
463
464 ALOGW_IF(!gotInterfaceLock,
465 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
466 mId, __FUNCTION__);
467 ALOGW_IF(!gotLock,
468 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
469 mId, __FUNCTION__);
470
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800471 bool dumpTemplates = false;
472 String16 templatesOption("-t");
473 int n = args.size();
474 for (int i = 0; i < n; i++) {
475 if (args[i] == templatesOption) {
476 dumpTemplates = true;
477 }
478 }
479
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800480 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800481
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800482 const char *status =
483 mStatus == STATUS_ERROR ? "ERROR" :
484 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700485 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
486 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800487 mStatus == STATUS_ACTIVE ? "ACTIVE" :
488 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700489
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800490 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700491 if (mStatus == STATUS_ERROR) {
492 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
493 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800494 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700495 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700496 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800497
498 if (mInputStream != NULL) {
499 write(fd, lines.string(), lines.size());
500 mInputStream->dump(fd, args);
501 } else {
502 lines.appendFormat(" No input stream.\n");
503 write(fd, lines.string(), lines.size());
504 }
505 for (size_t i = 0; i < mOutputStreams.size(); i++) {
506 mOutputStreams[i]->dump(fd,args);
507 }
508
Zhijun He125684a2015-12-26 15:07:30 -0800509 lines = String8(" Camera3 Buffer Manager:\n");
510 write(fd, lines.string(), lines.size());
511 mBufferManager->dump(fd, args);
512
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700513 lines = String8(" In-flight requests:\n");
514 if (mInFlightMap.size() == 0) {
515 lines.append(" None\n");
516 } else {
517 for (size_t i = 0; i < mInFlightMap.size(); i++) {
518 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700519 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700520 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800521 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700522 r.numBuffersLeft);
523 }
524 }
525 write(fd, lines.string(), lines.size());
526
Igor Murashkin1e479c02013-09-06 16:55:14 -0700527 {
528 lines = String8(" Last request sent:\n");
529 write(fd, lines.string(), lines.size());
530
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700531 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700532 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
533 }
534
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800535 if (dumpTemplates) {
536 const char *templateNames[] = {
537 "TEMPLATE_PREVIEW",
538 "TEMPLATE_STILL_CAPTURE",
539 "TEMPLATE_VIDEO_RECORD",
540 "TEMPLATE_VIDEO_SNAPSHOT",
541 "TEMPLATE_ZERO_SHUTTER_LAG",
542 "TEMPLATE_MANUAL"
543 };
544
545 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
546 const camera_metadata_t *templateRequest;
547 templateRequest =
548 mHal3Device->ops->construct_default_request_settings(
549 mHal3Device, i);
550 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
551 if (templateRequest == NULL) {
552 lines.append(" Not supported\n");
553 write(fd, lines.string(), lines.size());
554 } else {
555 write(fd, lines.string(), lines.size());
556 dump_indented_camera_metadata(templateRequest,
557 fd, /*verbosity*/2, /*indentation*/8);
558 }
559 }
560 }
561
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800562 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700563 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800564 write(fd, lines.string(), lines.size());
565 mHal3Device->ops->dump(mHal3Device, fd);
566 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800567
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700568 if (gotLock) mLock.unlock();
569 if (gotInterfaceLock) mInterfaceLock.unlock();
570
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800571 return OK;
572}
573
574const CameraMetadata& Camera3Device::info() const {
575 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800576 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
577 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700578 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800579 mStatus == STATUS_ERROR ?
580 "when in error state" : "before init");
581 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800582 return mDeviceInfo;
583}
584
Jianing Wei90e59c92014-03-12 18:29:36 -0700585status_t Camera3Device::checkStatusOkToCaptureLocked() {
586 switch (mStatus) {
587 case STATUS_ERROR:
588 CLOGE("Device has encountered a serious error");
589 return INVALID_OPERATION;
590 case STATUS_UNINITIALIZED:
591 CLOGE("Device not initialized");
592 return INVALID_OPERATION;
593 case STATUS_UNCONFIGURED:
594 case STATUS_CONFIGURED:
595 case STATUS_ACTIVE:
596 // OK
597 break;
598 default:
599 SET_ERR_L("Unexpected status: %d", mStatus);
600 return INVALID_OPERATION;
601 }
602 return OK;
603}
604
605status_t Camera3Device::convertMetadataListToRequestListLocked(
606 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
607 if (requestList == NULL) {
608 CLOGE("requestList cannot be NULL.");
609 return BAD_VALUE;
610 }
611
Jianing Weicb0652e2014-03-12 18:29:36 -0700612 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700613 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
614 it != metadataList.end(); ++it) {
615 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
616 if (newRequest == 0) {
617 CLOGE("Can't create capture request");
618 return BAD_VALUE;
619 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700620
621 // Setup burst Id and request Id
622 newRequest->mResultExtras.burstId = burstId++;
623 if (it->exists(ANDROID_REQUEST_ID)) {
624 if (it->find(ANDROID_REQUEST_ID).count == 0) {
625 CLOGE("RequestID entry exists; but must not be empty in metadata");
626 return BAD_VALUE;
627 }
628 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
629 } else {
630 CLOGE("RequestID does not exist in metadata");
631 return BAD_VALUE;
632 }
633
Jianing Wei90e59c92014-03-12 18:29:36 -0700634 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700635
636 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700637 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700638
639 // Setup batch size if this is a high speed video recording request.
640 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
641 auto firstRequest = requestList->begin();
642 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
643 if (outputStream->isVideoStream()) {
644 (*firstRequest)->mBatchSize = requestList->size();
645 break;
646 }
647 }
648 }
649
Jianing Wei90e59c92014-03-12 18:29:36 -0700650 return OK;
651}
652
Jianing Weicb0652e2014-03-12 18:29:36 -0700653status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800654 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800655
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700656 List<const CameraMetadata> requests;
657 requests.push_back(request);
658 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800659}
660
Jianing Wei90e59c92014-03-12 18:29:36 -0700661status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700662 const List<const CameraMetadata> &requests, bool repeating,
663 /*out*/
664 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700665 ATRACE_CALL();
666 Mutex::Autolock il(mInterfaceLock);
667 Mutex::Autolock l(mLock);
668
669 status_t res = checkStatusOkToCaptureLocked();
670 if (res != OK) {
671 // error logged by previous call
672 return res;
673 }
674
675 RequestList requestList;
676
677 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
678 if (res != OK) {
679 // error logged by previous call
680 return res;
681 }
682
683 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700684 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700685 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700686 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700687 }
688
689 if (res == OK) {
690 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
691 if (res != OK) {
692 SET_ERR_L("Can't transition to active in %f seconds!",
693 kActiveTimeout/1e9);
694 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700695 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
696 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700697 } else {
698 CLOGE("Cannot queue request. Impossible.");
699 return BAD_VALUE;
700 }
701
702 return res;
703}
704
Jianing Weicb0652e2014-03-12 18:29:36 -0700705status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
706 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700707 ATRACE_CALL();
708
Jianing Weicb0652e2014-03-12 18:29:36 -0700709 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700710}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800711
Jianing Weicb0652e2014-03-12 18:29:36 -0700712status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
713 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800714 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800715
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700716 List<const CameraMetadata> requests;
717 requests.push_back(request);
718 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800719}
720
Jianing Weicb0652e2014-03-12 18:29:36 -0700721status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
722 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700723 ATRACE_CALL();
724
Jianing Weicb0652e2014-03-12 18:29:36 -0700725 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700726}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800727
728sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
729 const CameraMetadata &request) {
730 status_t res;
731
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700732 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800733 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700734 // Stream configuration failed due to unsupported configuration.
735 // Device back to unconfigured state. Client might try other configuraitons
736 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
737 CLOGE("No streams configured");
738 return NULL;
739 }
740 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800741 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700742 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800743 return NULL;
744 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700745 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700746 if (mStatus == STATUS_UNCONFIGURED) {
747 CLOGE("No streams configured");
748 return NULL;
749 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800750 }
751
752 sp<CaptureRequest> newRequest = createCaptureRequest(request);
753 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800754}
755
Jianing Weicb0652e2014-03-12 18:29:36 -0700756status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800757 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700758 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800759 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800760
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800761 switch (mStatus) {
762 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700763 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800764 return INVALID_OPERATION;
765 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700766 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800767 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700768 case STATUS_UNCONFIGURED:
769 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800770 case STATUS_ACTIVE:
771 // OK
772 break;
773 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700774 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800775 return INVALID_OPERATION;
776 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700777 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700778
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700779 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800780}
781
782status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
783 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700784 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800785
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700786 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800787}
788
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700789status_t Camera3Device::createInputStream(
790 uint32_t width, uint32_t height, int format, int *id) {
791 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700792 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700793 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700794 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
795 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700796
797 status_t res;
798 bool wasActive = false;
799
800 switch (mStatus) {
801 case STATUS_ERROR:
802 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
803 return INVALID_OPERATION;
804 case STATUS_UNINITIALIZED:
805 ALOGE("%s: Device not initialized", __FUNCTION__);
806 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700807 case STATUS_UNCONFIGURED:
808 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700809 // OK
810 break;
811 case STATUS_ACTIVE:
812 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700813 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700814 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700815 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700816 return res;
817 }
818 wasActive = true;
819 break;
820 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700821 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700822 return INVALID_OPERATION;
823 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700824 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700825
826 if (mInputStream != 0) {
827 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
828 return INVALID_OPERATION;
829 }
830
831 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
832 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700833 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700834
835 mInputStream = newStream;
836
837 *id = mNextStreamId++;
838
839 // Continue captures if active at start
840 if (wasActive) {
841 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
842 res = configureStreamsLocked();
843 if (res != OK) {
844 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
845 __FUNCTION__, mNextStreamId, strerror(-res), res);
846 return res;
847 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700848 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700849 }
850
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700851 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700852 return OK;
853}
854
Igor Murashkin2fba5842013-04-22 14:03:54 -0700855
856status_t Camera3Device::createZslStream(
857 uint32_t width, uint32_t height,
858 int depth,
859 /*out*/
860 int *id,
861 sp<Camera3ZslStream>* zslStream) {
862 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700863 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700864 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700865 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
866 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700867
868 status_t res;
869 bool wasActive = false;
870
871 switch (mStatus) {
872 case STATUS_ERROR:
873 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
874 return INVALID_OPERATION;
875 case STATUS_UNINITIALIZED:
876 ALOGE("%s: Device not initialized", __FUNCTION__);
877 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700878 case STATUS_UNCONFIGURED:
879 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700880 // OK
881 break;
882 case STATUS_ACTIVE:
883 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700884 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700885 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700886 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700887 return res;
888 }
889 wasActive = true;
890 break;
891 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700892 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700893 return INVALID_OPERATION;
894 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700895 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700896
897 if (mInputStream != 0) {
898 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
899 return INVALID_OPERATION;
900 }
901
902 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
903 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700904 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700905
906 res = mOutputStreams.add(mNextStreamId, newStream);
907 if (res < 0) {
908 ALOGE("%s: Can't add new stream to set: %s (%d)",
909 __FUNCTION__, strerror(-res), res);
910 return res;
911 }
912 mInputStream = newStream;
913
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530914 mNeedConfig = true;
915
Igor Murashkin2fba5842013-04-22 14:03:54 -0700916 *id = mNextStreamId++;
917 *zslStream = newStream;
918
919 // Continue captures if active at start
920 if (wasActive) {
921 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
922 res = configureStreamsLocked();
923 if (res != OK) {
924 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
925 __FUNCTION__, mNextStreamId, strerror(-res), res);
926 return res;
927 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700928 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700929 }
930
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700931 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700932 return OK;
933}
934
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700935status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800936 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He125684a2015-12-26 15:07:30 -0800937 camera3_stream_rotation_t rotation, int *id, int streamSetId) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800938 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700939 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800940 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700941 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
942 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800943
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800944 status_t res;
945 bool wasActive = false;
946
947 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 // OK
957 break;
958 case STATUS_ACTIVE:
959 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700960 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800961 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700962 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800963 return res;
964 }
965 wasActive = true;
966 break;
967 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700968 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800969 return INVALID_OPERATION;
970 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700971 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800972
973 sp<Camera3OutputStream> newStream;
Zhijun He125684a2015-12-26 15:07:30 -0800974 // Overwrite stream set id to invalid for HAL3.1 or lower, as buffer manager does support
975 // such devices.
976 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
977 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
978 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800979 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700980 ssize_t blobBufferSize;
981 if (dataSpace != HAL_DATASPACE_DEPTH) {
982 blobBufferSize = getJpegBufferSize(width, height);
983 if (blobBufferSize <= 0) {
984 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
985 return BAD_VALUE;
986 }
987 } else {
988 blobBufferSize = getPointCloudBufferSize();
989 if (blobBufferSize <= 0) {
990 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
991 return BAD_VALUE;
992 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700993 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800994 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Zhijun He125684a2015-12-26 15:07:30 -0800995 width, height, blobBufferSize, format, dataSpace, rotation, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800996 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
997 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
998 if (rawOpaqueBufferSize <= 0) {
999 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1000 return BAD_VALUE;
1001 }
1002 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Zhijun He125684a2015-12-26 15:07:30 -08001003 width, height, rawOpaqueBufferSize, format, dataSpace, rotation, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001004 } else {
1005 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Zhijun He125684a2015-12-26 15:07:30 -08001006 width, height, format, dataSpace, rotation, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001007 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001008 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001009
Zhijun He125684a2015-12-26 15:07:30 -08001010 /**
1011 * Camera3 Buffer manager is only supported by HAL3.2 onwards, as the older HALs requires
1012 * buffers to be statically allocated for internal static buffer registration, while the
1013 * buffers provided by buffer manager are really dynamically allocated.
1014 */
1015 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
1016 newStream->setBufferManager(mBufferManager);
1017 }
1018
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001019 res = mOutputStreams.add(mNextStreamId, newStream);
1020 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001021 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001022 return res;
1023 }
1024
1025 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001026 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001027
1028 // Continue captures if active at start
1029 if (wasActive) {
1030 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1031 res = configureStreamsLocked();
1032 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001033 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1034 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001035 return res;
1036 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001037 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001038 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001039 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001040 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001041}
1042
1043status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1044 ATRACE_CALL();
1045 (void)outputId; (void)id;
1046
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001047 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001048 return INVALID_OPERATION;
1049}
1050
1051
1052status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001053 uint32_t *width, uint32_t *height,
1054 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001055 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001056 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001057 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001058
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001059 switch (mStatus) {
1060 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001061 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001062 return INVALID_OPERATION;
1063 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001064 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001065 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001066 case STATUS_UNCONFIGURED:
1067 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001068 case STATUS_ACTIVE:
1069 // OK
1070 break;
1071 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001072 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001073 return INVALID_OPERATION;
1074 }
1075
1076 ssize_t idx = mOutputStreams.indexOfKey(id);
1077 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001078 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001079 return idx;
1080 }
1081
1082 if (width) *width = mOutputStreams[idx]->getWidth();
1083 if (height) *height = mOutputStreams[idx]->getHeight();
1084 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001085 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001086 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001087}
1088
1089status_t Camera3Device::setStreamTransform(int id,
1090 int transform) {
1091 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001092 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001093 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001094
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001095 switch (mStatus) {
1096 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001097 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001098 return INVALID_OPERATION;
1099 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001100 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001101 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001102 case STATUS_UNCONFIGURED:
1103 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001104 case STATUS_ACTIVE:
1105 // OK
1106 break;
1107 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001108 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001109 return INVALID_OPERATION;
1110 }
1111
1112 ssize_t idx = mOutputStreams.indexOfKey(id);
1113 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001114 CLOGE("Stream %d does not exist",
1115 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001116 return BAD_VALUE;
1117 }
1118
1119 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001120}
1121
1122status_t Camera3Device::deleteStream(int id) {
1123 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001124 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001125 Mutex::Autolock l(mLock);
1126 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001127
Igor Murashkine2172be2013-05-28 15:31:39 -07001128 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1129
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001130 // CameraDevice semantics require device to already be idle before
1131 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001132 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001133 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1134 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001135 }
1136
Igor Murashkin2fba5842013-04-22 14:03:54 -07001137 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001138 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001139 if (mInputStream != NULL && id == mInputStream->getId()) {
1140 deletedStream = mInputStream;
1141 mInputStream.clear();
1142 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001143 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001144 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001145 return BAD_VALUE;
1146 }
Zhijun He5f446352014-01-22 09:49:33 -08001147 }
1148
1149 // Delete output stream or the output part of a bi-directional stream.
1150 if (outputStreamIdx != NAME_NOT_FOUND) {
1151 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001152 mOutputStreams.removeItem(id);
1153 }
1154
1155 // Free up the stream endpoint so that it can be used by some other stream
1156 res = deletedStream->disconnect();
1157 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001158 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001159 // fall through since we want to still list the stream as deleted.
1160 }
1161 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001162 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001163
1164 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001165}
1166
1167status_t Camera3Device::deleteReprocessStream(int id) {
1168 ATRACE_CALL();
1169 (void)id;
1170
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001171 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001172 return INVALID_OPERATION;
1173}
1174
Zhijun He1fa89992015-06-01 15:44:31 -07001175status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001176 ATRACE_CALL();
1177 ALOGV("%s: E", __FUNCTION__);
1178
1179 Mutex::Autolock il(mInterfaceLock);
1180 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001181
1182 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1183 mNeedConfig = true;
1184 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1185 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001186
1187 return configureStreamsLocked();
1188}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001189
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001190status_t Camera3Device::getInputBufferProducer(
1191 sp<IGraphicBufferProducer> *producer) {
1192 Mutex::Autolock il(mInterfaceLock);
1193 Mutex::Autolock l(mLock);
1194
1195 if (producer == NULL) {
1196 return BAD_VALUE;
1197 } else if (mInputStream == NULL) {
1198 return INVALID_OPERATION;
1199 }
1200
1201 return mInputStream->getInputBufferProducer(producer);
1202}
1203
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001204status_t Camera3Device::createDefaultRequest(int templateId,
1205 CameraMetadata *request) {
1206 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001207 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001208 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001209 Mutex::Autolock l(mLock);
1210
1211 switch (mStatus) {
1212 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001213 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001214 return INVALID_OPERATION;
1215 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001216 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001217 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001218 case STATUS_UNCONFIGURED:
1219 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001220 case STATUS_ACTIVE:
1221 // OK
1222 break;
1223 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001224 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001225 return INVALID_OPERATION;
1226 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001227
Zhijun Hea1530f12014-09-14 12:44:20 -07001228 if (!mRequestTemplateCache[templateId].isEmpty()) {
1229 *request = mRequestTemplateCache[templateId];
1230 return OK;
1231 }
1232
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001233 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001234 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001235 rawRequest = mHal3Device->ops->construct_default_request_settings(
1236 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001237 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001238 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001239 ALOGI("%s: template %d is not supported on this camera device",
1240 __FUNCTION__, templateId);
1241 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001242 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001243 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001244 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001245
1246 return OK;
1247}
1248
1249status_t Camera3Device::waitUntilDrained() {
1250 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001251 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001252 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001253
Zhijun He69a37482014-03-23 18:44:49 -07001254 return waitUntilDrainedLocked();
1255}
1256
1257status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001258 switch (mStatus) {
1259 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001260 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001261 ALOGV("%s: Already idle", __FUNCTION__);
1262 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001263 case STATUS_CONFIGURED:
1264 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001265 case STATUS_ERROR:
1266 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001267 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001268 break;
1269 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001270 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001271 return INVALID_OPERATION;
1272 }
1273
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001274 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1275 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001276 if (res != OK) {
1277 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1278 res);
1279 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001280 return res;
1281}
1282
Ruben Brunk183f0562015-08-12 12:55:02 -07001283
1284void Camera3Device::internalUpdateStatusLocked(Status status) {
1285 mStatus = status;
1286 mRecentStatusUpdates.add(mStatus);
1287 mStatusChanged.broadcast();
1288}
1289
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001290// Pause to reconfigure
1291status_t Camera3Device::internalPauseAndWaitLocked() {
1292 mRequestThread->setPaused(true);
1293 mPauseStateNotify = true;
1294
1295 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1296 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1297 if (res != OK) {
1298 SET_ERR_L("Can't idle device in %f seconds!",
1299 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001300 }
1301
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001302 return res;
1303}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001304
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001305// Resume after internalPauseAndWaitLocked
1306status_t Camera3Device::internalResumeLocked() {
1307 status_t res;
1308
1309 mRequestThread->setPaused(false);
1310
1311 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1312 if (res != OK) {
1313 SET_ERR_L("Can't transition to active in %f seconds!",
1314 kActiveTimeout/1e9);
1315 }
1316 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001317 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001318}
1319
Ruben Brunk183f0562015-08-12 12:55:02 -07001320status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001321 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001322
1323 size_t startIndex = 0;
1324 if (mStatusWaiters == 0) {
1325 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1326 // this status list
1327 mRecentStatusUpdates.clear();
1328 } else {
1329 // If other threads are waiting on updates to this status list, set the position of the
1330 // first element that this list will check rather than clearing the list.
1331 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001332 }
1333
Ruben Brunk183f0562015-08-12 12:55:02 -07001334 mStatusWaiters++;
1335
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001336 bool stateSeen = false;
1337 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001338 if (active == (mStatus == STATUS_ACTIVE)) {
1339 // Desired state is current
1340 break;
1341 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001342
1343 res = mStatusChanged.waitRelative(mLock, timeout);
1344 if (res != OK) break;
1345
Ruben Brunk183f0562015-08-12 12:55:02 -07001346 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1347 // transitions.
1348 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1349 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1350 __FUNCTION__);
1351
1352 // Encountered desired state since we began waiting
1353 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001354 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1355 stateSeen = true;
1356 break;
1357 }
1358 }
1359 } while (!stateSeen);
1360
Ruben Brunk183f0562015-08-12 12:55:02 -07001361 mStatusWaiters--;
1362
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001363 return res;
1364}
1365
1366
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001367status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1368 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001369 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001370
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001371 if (listener != NULL && mListener != NULL) {
1372 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1373 }
1374 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001375 mRequestThread->setNotificationListener(listener);
1376 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001377
1378 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001379}
1380
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001381bool Camera3Device::willNotify3A() {
1382 return false;
1383}
1384
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001385status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001386 status_t res;
1387 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001388
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001389 while (mResultQueue.empty()) {
1390 res = mResultSignal.waitRelative(mOutputLock, timeout);
1391 if (res == TIMED_OUT) {
1392 return res;
1393 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001394 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001395 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001396 return res;
1397 }
1398 }
1399 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001400}
1401
Jianing Weicb0652e2014-03-12 18:29:36 -07001402status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001403 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001404 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001405
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001406 if (mResultQueue.empty()) {
1407 return NOT_ENOUGH_DATA;
1408 }
1409
Jianing Weicb0652e2014-03-12 18:29:36 -07001410 if (frame == NULL) {
1411 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1412 return BAD_VALUE;
1413 }
1414
1415 CaptureResult &result = *(mResultQueue.begin());
1416 frame->mResultExtras = result.mResultExtras;
1417 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001418 mResultQueue.erase(mResultQueue.begin());
1419
1420 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001421}
1422
1423status_t Camera3Device::triggerAutofocus(uint32_t id) {
1424 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001425 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001426
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001427 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1428 // Mix-in this trigger into the next request and only the next request.
1429 RequestTrigger trigger[] = {
1430 {
1431 ANDROID_CONTROL_AF_TRIGGER,
1432 ANDROID_CONTROL_AF_TRIGGER_START
1433 },
1434 {
1435 ANDROID_CONTROL_AF_TRIGGER_ID,
1436 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001437 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001438 };
1439
1440 return mRequestThread->queueTrigger(trigger,
1441 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001442}
1443
1444status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1445 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001446 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001447
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001448 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1449 // Mix-in this trigger into the next request and only the next request.
1450 RequestTrigger trigger[] = {
1451 {
1452 ANDROID_CONTROL_AF_TRIGGER,
1453 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1454 },
1455 {
1456 ANDROID_CONTROL_AF_TRIGGER_ID,
1457 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001458 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001459 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001460
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001461 return mRequestThread->queueTrigger(trigger,
1462 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001463}
1464
1465status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1466 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001467 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001468
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001469 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1470 // Mix-in this trigger into the next request and only the next request.
1471 RequestTrigger trigger[] = {
1472 {
1473 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1474 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1475 },
1476 {
1477 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1478 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001479 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001480 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001481
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001482 return mRequestThread->queueTrigger(trigger,
1483 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001484}
1485
1486status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1487 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1488 ATRACE_CALL();
1489 (void)reprocessStreamId; (void)buffer; (void)listener;
1490
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001491 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001492 return INVALID_OPERATION;
1493}
1494
Jianing Weicb0652e2014-03-12 18:29:36 -07001495status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001496 ATRACE_CALL();
1497 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001498 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001499
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001500 NotificationListener* listener;
1501 {
1502 Mutex::Autolock l(mOutputLock);
1503 listener = mListener;
1504 }
1505
Zhijun He7ef20392014-04-21 16:04:17 -07001506 {
1507 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001508 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001509 }
1510
Zhijun He491e3412013-12-27 10:57:44 -08001511 status_t res;
1512 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001513 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001514 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001515 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001516 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001517 }
1518
1519 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001520}
1521
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001522status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001523 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1524}
1525
1526status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001527 ATRACE_CALL();
1528 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001529 Mutex::Autolock il(mInterfaceLock);
1530 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001531
1532 sp<Camera3StreamInterface> stream;
1533 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1534 if (outputStreamIdx == NAME_NOT_FOUND) {
1535 CLOGE("Stream %d does not exist", streamId);
1536 return BAD_VALUE;
1537 }
1538
1539 stream = mOutputStreams.editValueAt(outputStreamIdx);
1540
1541 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001542 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001543 return BAD_VALUE;
1544 }
1545
1546 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001547 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001548 return BAD_VALUE;
1549 }
1550
Ruben Brunkc78ac262015-08-13 17:58:46 -07001551 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001552}
1553
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001554status_t Camera3Device::tearDown(int streamId) {
1555 ATRACE_CALL();
1556 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1557 Mutex::Autolock il(mInterfaceLock);
1558 Mutex::Autolock l(mLock);
1559
1560 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1561 // since we cannot call register_stream_buffers except right after configure_streams.
1562 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1563 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1564 __FUNCTION__, mHal3Device->common.version);
1565 return NO_INIT;
1566 }
1567
1568 sp<Camera3StreamInterface> stream;
1569 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1570 if (outputStreamIdx == NAME_NOT_FOUND) {
1571 CLOGE("Stream %d does not exist", streamId);
1572 return BAD_VALUE;
1573 }
1574
1575 stream = mOutputStreams.editValueAt(outputStreamIdx);
1576
1577 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1578 CLOGE("Stream %d is a target of a in-progress request", streamId);
1579 return BAD_VALUE;
1580 }
1581
1582 return stream->tearDown();
1583}
1584
Zhijun He204e3292014-07-14 17:09:23 -07001585uint32_t Camera3Device::getDeviceVersion() {
1586 ATRACE_CALL();
1587 Mutex::Autolock il(mInterfaceLock);
1588 return mDeviceVersion;
1589}
1590
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001591/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001592 * Methods called by subclasses
1593 */
1594
1595void Camera3Device::notifyStatus(bool idle) {
1596 {
1597 // Need mLock to safely update state and synchronize to current
1598 // state of methods in flight.
1599 Mutex::Autolock l(mLock);
1600 // We can get various system-idle notices from the status tracker
1601 // while starting up. Only care about them if we've actually sent
1602 // in some requests recently.
1603 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1604 return;
1605 }
1606 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1607 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001608 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001609
1610 // Skip notifying listener if we're doing some user-transparent
1611 // state changes
1612 if (mPauseStateNotify) return;
1613 }
1614 NotificationListener *listener;
1615 {
1616 Mutex::Autolock l(mOutputLock);
1617 listener = mListener;
1618 }
1619 if (idle && listener != NULL) {
1620 listener->notifyIdle();
1621 }
1622}
1623
1624/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001625 * Camera3Device private methods
1626 */
1627
1628sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1629 const CameraMetadata &request) {
1630 ATRACE_CALL();
1631 status_t res;
1632
1633 sp<CaptureRequest> newRequest = new CaptureRequest;
1634 newRequest->mSettings = request;
1635
1636 camera_metadata_entry_t inputStreams =
1637 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1638 if (inputStreams.count > 0) {
1639 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001640 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001641 CLOGE("Request references unknown input stream %d",
1642 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001643 return NULL;
1644 }
1645 // Lazy completion of stream configuration (allocation/registration)
1646 // on first use
1647 if (mInputStream->isConfiguring()) {
1648 res = mInputStream->finishConfiguration(mHal3Device);
1649 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001650 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001651 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001652 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001653 return NULL;
1654 }
1655 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001656 // Check if stream is being prepared
1657 if (mInputStream->isPreparing()) {
1658 CLOGE("Request references an input stream that's being prepared!");
1659 return NULL;
1660 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001661
1662 newRequest->mInputStream = mInputStream;
1663 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1664 }
1665
1666 camera_metadata_entry_t streams =
1667 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1668 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001669 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001670 return NULL;
1671 }
1672
1673 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001674 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001675 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001676 CLOGE("Request references unknown stream %d",
1677 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001678 return NULL;
1679 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001680 sp<Camera3OutputStreamInterface> stream =
1681 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001682
1683 // Lazy completion of stream configuration (allocation/registration)
1684 // on first use
1685 if (stream->isConfiguring()) {
1686 res = stream->finishConfiguration(mHal3Device);
1687 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001688 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1689 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001690 return NULL;
1691 }
1692 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001693 // Check if stream is being prepared
1694 if (stream->isPreparing()) {
1695 CLOGE("Request references an output stream that's being prepared!");
1696 return NULL;
1697 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001698
1699 newRequest->mOutputStreams.push(stream);
1700 }
1701 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001702 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001703
1704 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001705}
1706
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001707bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1708 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1709 Size size = mSupportedOpaqueInputSizes[i];
1710 if (size.width == width && size.height == height) {
1711 return true;
1712 }
1713 }
1714
1715 return false;
1716}
1717
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001718status_t Camera3Device::configureStreamsLocked() {
1719 ATRACE_CALL();
1720 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001721
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001722 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001723 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001724 return INVALID_OPERATION;
1725 }
1726
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001727 if (!mNeedConfig) {
1728 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1729 return OK;
1730 }
1731
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001732 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1733 // adding a dummy stream instead.
1734 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1735 if (mOutputStreams.size() == 0) {
1736 addDummyStreamLocked();
1737 } else {
1738 tryRemoveDummyStreamLocked();
1739 }
1740
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001741 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001742 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001743
1744 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001745 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1746 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1747 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001748 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1749
1750 Vector<camera3_stream_t*> streams;
1751 streams.setCapacity(config.num_streams);
1752
1753 if (mInputStream != NULL) {
1754 camera3_stream_t *inputStream;
1755 inputStream = mInputStream->startConfiguration();
1756 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001757 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001758 return INVALID_OPERATION;
1759 }
1760 streams.add(inputStream);
1761 }
1762
1763 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001764
1765 // Don't configure bidi streams twice, nor add them twice to the list
1766 if (mOutputStreams[i].get() ==
1767 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1768
1769 config.num_streams--;
1770 continue;
1771 }
1772
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001773 camera3_stream_t *outputStream;
1774 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1775 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001776 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001777 return INVALID_OPERATION;
1778 }
1779 streams.add(outputStream);
1780 }
1781
1782 config.streams = streams.editArray();
1783
1784 // Do the HAL configuration; will potentially touch stream
1785 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001786 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001787 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001788 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001789
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001790 if (res == BAD_VALUE) {
1791 // HAL rejected this set of streams as unsupported, clean up config
1792 // attempt and return to unconfigured state
1793 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1794 res = mInputStream->cancelConfiguration();
1795 if (res != OK) {
1796 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1797 mInputStream->getId(), strerror(-res), res);
1798 return res;
1799 }
1800 }
1801
1802 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1803 sp<Camera3OutputStreamInterface> outputStream =
1804 mOutputStreams.editValueAt(i);
1805 if (outputStream->isConfiguring()) {
1806 res = outputStream->cancelConfiguration();
1807 if (res != OK) {
1808 SET_ERR_L(
1809 "Can't cancel configuring output stream %d: %s (%d)",
1810 outputStream->getId(), strerror(-res), res);
1811 return res;
1812 }
1813 }
1814 }
1815
1816 // Return state to that at start of call, so that future configures
1817 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001818 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001819 mNeedConfig = true;
1820
1821 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1822 return BAD_VALUE;
1823 } else if (res != OK) {
1824 // Some other kind of error from configure_streams - this is not
1825 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001826 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1827 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001828 return res;
1829 }
1830
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001831 // Finish all stream configuration immediately.
1832 // TODO: Try to relax this later back to lazy completion, which should be
1833 // faster
1834
Igor Murashkin073f8572013-05-02 14:59:28 -07001835 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001836 res = mInputStream->finishConfiguration(mHal3Device);
1837 if (res != OK) {
1838 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1839 mInputStream->getId(), strerror(-res), res);
1840 return res;
1841 }
1842 }
1843
1844 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001845 sp<Camera3OutputStreamInterface> outputStream =
1846 mOutputStreams.editValueAt(i);
1847 if (outputStream->isConfiguring()) {
1848 res = outputStream->finishConfiguration(mHal3Device);
1849 if (res != OK) {
1850 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1851 outputStream->getId(), strerror(-res), res);
1852 return res;
1853 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001854 }
1855 }
1856
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001857 // Request thread needs to know to avoid using repeat-last-settings protocol
1858 // across configure_streams() calls
1859 mRequestThread->configurationComplete();
1860
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07001861 // Boost priority of request thread for high speed recording to SCHED_FIFO
1862 if (mIsConstrainedHighSpeedConfiguration) {
1863 pid_t requestThreadTid = mRequestThread->getTid();
1864 res = requestPriority(getpid(), requestThreadTid,
1865 kConstrainedHighSpeedThreadPriority, true);
1866 if (res != OK) {
1867 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
1868 strerror(-res), res);
1869 } else {
1870 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
1871 }
1872 } else {
1873 // TODO: Set/restore normal priority for normal use cases
1874 }
1875
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001876 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001877
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001878 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001879
Ruben Brunk183f0562015-08-12 12:55:02 -07001880 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1881 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001882
1883 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1884
Zhijun He0a210512014-07-24 13:45:15 -07001885 // tear down the deleted streams after configure streams.
1886 mDeletedStreams.clear();
1887
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001888 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001889}
1890
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001891status_t Camera3Device::addDummyStreamLocked() {
1892 ATRACE_CALL();
1893 status_t res;
1894
1895 if (mDummyStreamId != NO_STREAM) {
1896 // Should never be adding a second dummy stream when one is already
1897 // active
1898 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1899 __FUNCTION__, mId);
1900 return INVALID_OPERATION;
1901 }
1902
1903 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1904
1905 sp<Camera3OutputStreamInterface> dummyStream =
1906 new Camera3DummyStream(mNextStreamId);
1907
1908 res = mOutputStreams.add(mNextStreamId, dummyStream);
1909 if (res < 0) {
1910 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1911 return res;
1912 }
1913
1914 mDummyStreamId = mNextStreamId;
1915 mNextStreamId++;
1916
1917 return OK;
1918}
1919
1920status_t Camera3Device::tryRemoveDummyStreamLocked() {
1921 ATRACE_CALL();
1922 status_t res;
1923
1924 if (mDummyStreamId == NO_STREAM) return OK;
1925 if (mOutputStreams.size() == 1) return OK;
1926
1927 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1928
1929 // Ok, have a dummy stream and there's at least one other output stream,
1930 // so remove the dummy
1931
1932 sp<Camera3StreamInterface> deletedStream;
1933 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1934 if (outputStreamIdx == NAME_NOT_FOUND) {
1935 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1936 return INVALID_OPERATION;
1937 }
1938
1939 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1940 mOutputStreams.removeItemsAt(outputStreamIdx);
1941
1942 // Free up the stream endpoint so that it can be used by some other stream
1943 res = deletedStream->disconnect();
1944 if (res != OK) {
1945 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1946 // fall through since we want to still list the stream as deleted.
1947 }
1948 mDeletedStreams.add(deletedStream);
1949 mDummyStreamId = NO_STREAM;
1950
1951 return res;
1952}
1953
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001954void Camera3Device::setErrorState(const char *fmt, ...) {
1955 Mutex::Autolock l(mLock);
1956 va_list args;
1957 va_start(args, fmt);
1958
1959 setErrorStateLockedV(fmt, args);
1960
1961 va_end(args);
1962}
1963
1964void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1965 Mutex::Autolock l(mLock);
1966 setErrorStateLockedV(fmt, args);
1967}
1968
1969void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1970 va_list args;
1971 va_start(args, fmt);
1972
1973 setErrorStateLockedV(fmt, args);
1974
1975 va_end(args);
1976}
1977
1978void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001979 // Print out all error messages to log
1980 String8 errorCause = String8::formatV(fmt, args);
1981 ALOGE("Camera %d: %s", mId, errorCause.string());
1982
1983 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001984 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001985
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001986 mErrorCause = errorCause;
1987
1988 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07001989 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001990
1991 // Notify upstream about a device error
1992 if (mListener != NULL) {
1993 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1994 CaptureResultExtras());
1995 }
1996
1997 // Save stack trace. View by dumping it later.
1998 CameraTraces::saveTrace();
1999 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002000}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002001
2002/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002003 * In-flight request management
2004 */
2005
Jianing Weicb0652e2014-03-12 18:29:36 -07002006status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002007 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2008 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002009 ATRACE_CALL();
2010 Mutex::Autolock l(mInFlightLock);
2011
2012 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002013 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2014 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002015 if (res < 0) return res;
2016
2017 return OK;
2018}
2019
2020/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002021 * Check if all 3A fields are ready, and send off a partial 3A-only result
2022 * to the output frame queue
2023 */
Zhijun He204e3292014-07-14 17:09:23 -07002024bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07002025 uint32_t frameNumber,
2026 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002027
2028 // Check if all 3A states are present
2029 // The full list of fields is
2030 // android.control.afMode
2031 // android.control.awbMode
2032 // android.control.aeState
2033 // android.control.awbState
2034 // android.control.afState
2035 // android.control.afTriggerID
2036 // android.control.aePrecaptureID
2037 // TODO: Add android.control.aeMode
2038
2039 bool gotAllStates = true;
2040
2041 uint8_t afMode;
2042 uint8_t awbMode;
2043 uint8_t aeState;
2044 uint8_t afState;
2045 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002046
2047 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
2048 &afMode, frameNumber);
2049
2050 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
2051 &awbMode, frameNumber);
2052
2053 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
2054 &aeState, frameNumber);
2055
2056 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
2057 &afState, frameNumber);
2058
2059 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
2060 &awbState, frameNumber);
2061
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002062 if (!gotAllStates) return false;
2063
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002064 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002065 "AF state %d, AE state %d, AWB state %d, "
2066 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002067 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002068 afMode, awbMode,
2069 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002070 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002071
2072 // Got all states, so construct a minimal result to send
2073 // In addition to the above fields, this means adding in
2074 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002075 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07002076 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002077
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002078 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002079
2080 Mutex::Autolock l(mOutputLock);
2081
Jianing Weicb0652e2014-03-12 18:29:36 -07002082 CaptureResult captureResult;
2083 captureResult.mResultExtras = resultExtras;
2084 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
2085 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
2086 // but not limited to CameraDeviceBase::getNextResult
2087 CaptureResult& min3AResult =
2088 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002089
Jianing Weicb0652e2014-03-12 18:29:36 -07002090 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
2091 // TODO: This is problematic casting. Need to fix CameraMetadata.
2092 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002093 return false;
2094 }
2095
Jianing Weicb0652e2014-03-12 18:29:36 -07002096 int32_t requestId = resultExtras.requestId;
2097 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002098 &requestId, frameNumber)) {
2099 return false;
2100 }
2101
Zhijun He204e3292014-07-14 17:09:23 -07002102 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
2103 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
2104 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
2105 &partialResult, frameNumber)) {
2106 return false;
2107 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002108 }
2109
Jianing Weicb0652e2014-03-12 18:29:36 -07002110 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002111 &afMode, frameNumber)) {
2112 return false;
2113 }
2114
Jianing Weicb0652e2014-03-12 18:29:36 -07002115 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002116 &awbMode, frameNumber)) {
2117 return false;
2118 }
2119
Jianing Weicb0652e2014-03-12 18:29:36 -07002120 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002121 &aeState, frameNumber)) {
2122 return false;
2123 }
2124
Jianing Weicb0652e2014-03-12 18:29:36 -07002125 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002126 &afState, frameNumber)) {
2127 return false;
2128 }
2129
Jianing Weicb0652e2014-03-12 18:29:36 -07002130 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002131 &awbState, frameNumber)) {
2132 return false;
2133 }
2134
Jianing Weicb0652e2014-03-12 18:29:36 -07002135 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002136 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002137 return false;
2138 }
2139
Jianing Weicb0652e2014-03-12 18:29:36 -07002140 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002141 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002142 return false;
2143 }
2144
Zhijun He204e3292014-07-14 17:09:23 -07002145 // We only send the aggregated partial when all 3A related metadata are available
2146 // For both API1 and API2.
2147 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002148 mResultSignal.signal();
2149
2150 return true;
2151}
2152
2153template<typename T>
2154bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002155 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002156 (void) frameNumber;
2157
2158 camera_metadata_ro_entry_t entry;
2159
2160 entry = result.find(tag);
2161 if (entry.count == 0) {
2162 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2163 mId, frameNumber, get_camera_metadata_tag_name(tag));
2164 return false;
2165 }
2166
2167 if (sizeof(T) == sizeof(uint8_t)) {
2168 *value = entry.data.u8[0];
2169 } else if (sizeof(T) == sizeof(int32_t)) {
2170 *value = entry.data.i32[0];
2171 } else {
2172 ALOGE("%s: Unexpected type", __FUNCTION__);
2173 return false;
2174 }
2175 return true;
2176}
2177
2178template<typename T>
2179bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002180 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002181 if (result.update(tag, value, 1) != NO_ERROR) {
2182 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2183 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2184 frameNumber, get_camera_metadata_tag_name(tag));
2185 return false;
2186 }
2187 return true;
2188}
2189
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002190void Camera3Device::returnOutputBuffers(
2191 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2192 nsecs_t timestamp) {
2193 for (size_t i = 0; i < numBuffers; i++)
2194 {
2195 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2196 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2197 // Note: stream may be deallocated at this point, if this buffer was
2198 // the last reference to it.
2199 if (res != OK) {
2200 ALOGE("Can't return buffer to its stream: %s (%d)",
2201 strerror(-res), res);
2202 }
2203 }
2204}
2205
2206
2207void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2208
2209 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2210 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2211
2212 nsecs_t sensorTimestamp = request.sensorTimestamp;
2213 nsecs_t shutterTimestamp = request.shutterTimestamp;
2214
2215 // Check if it's okay to remove the request from InFlightMap:
2216 // In the case of a successful request:
2217 // all input and output buffers, all result metadata, shutter callback
2218 // arrived.
2219 // In the case of a unsuccessful request:
2220 // all input and output buffers arrived.
2221 if (request.numBuffersLeft == 0 &&
2222 (request.requestStatus != OK ||
2223 (request.haveResultMetadata && shutterTimestamp != 0))) {
2224 ATRACE_ASYNC_END("frame capture", frameNumber);
2225
2226 // Sanity check - if sensor timestamp matches shutter timestamp
2227 if (request.requestStatus == OK &&
2228 sensorTimestamp != shutterTimestamp) {
2229 SET_ERR("sensor timestamp (%" PRId64
2230 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2231 sensorTimestamp, frameNumber, shutterTimestamp);
2232 }
2233
2234 // for an unsuccessful request, it may have pending output buffers to
2235 // return.
2236 assert(request.requestStatus != OK ||
2237 request.pendingOutputBuffers.size() == 0);
2238 returnOutputBuffers(request.pendingOutputBuffers.array(),
2239 request.pendingOutputBuffers.size(), 0);
2240
2241 mInFlightMap.removeItemsAt(idx, 1);
2242
2243 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2244 }
2245
2246 // Sanity check - if we have too many in-flight frames, something has
2247 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002248 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002249 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002250 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2251 kInFlightWarnLimitHighSpeed) {
2252 CLOGE("In-flight list too large for high speed configuration: %zu",
2253 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002254 }
2255}
2256
2257
2258void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2259 CaptureResultExtras &resultExtras,
2260 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002261 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002262 bool reprocess,
2263 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002264 if (pendingMetadata.isEmpty())
2265 return;
2266
2267 Mutex::Autolock l(mOutputLock);
2268
2269 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002270 if (reprocess) {
2271 if (frameNumber < mNextReprocessResultFrameNumber) {
2272 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002273 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002274 frameNumber, mNextReprocessResultFrameNumber);
2275 return;
2276 }
2277 mNextReprocessResultFrameNumber = frameNumber + 1;
2278 } else {
2279 if (frameNumber < mNextResultFrameNumber) {
2280 SET_ERR("Out-of-order capture result metadata submitted! "
2281 "(got frame number %d, expecting %d)",
2282 frameNumber, mNextResultFrameNumber);
2283 return;
2284 }
2285 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002286 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002287
2288 CaptureResult captureResult;
2289 captureResult.mResultExtras = resultExtras;
2290 captureResult.mMetadata = pendingMetadata;
2291
2292 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2293 (int32_t*)&frameNumber, 1) != OK) {
2294 SET_ERR("Failed to set frame# in metadata (%d)",
2295 frameNumber);
2296 return;
2297 } else {
2298 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2299 __FUNCTION__, mId, frameNumber);
2300 }
2301
2302 // Append any previous partials to form a complete result
2303 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2304 captureResult.mMetadata.append(collectedPartialResult);
2305 }
2306
2307 captureResult.mMetadata.sort();
2308
2309 // Check that there's a timestamp in the result metadata
2310 camera_metadata_entry entry =
2311 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2312 if (entry.count == 0) {
2313 SET_ERR("No timestamp provided by HAL for frame %d!",
2314 frameNumber);
2315 return;
2316 }
2317
Chien-Yu Chend196d612015-06-22 19:49:01 -07002318 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2319
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002320 // Valid result, insert into queue
2321 List<CaptureResult>::iterator queuedResult =
2322 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2323 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2324 ", burstId = %" PRId32, __FUNCTION__,
2325 queuedResult->mResultExtras.requestId,
2326 queuedResult->mResultExtras.frameNumber,
2327 queuedResult->mResultExtras.burstId);
2328
2329 mResultSignal.signal();
2330}
2331
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002332/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002333 * Camera HAL device callback methods
2334 */
2335
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002336void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002337 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002338
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002339 status_t res;
2340
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002341 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002342 if (result->result == NULL && result->num_output_buffers == 0 &&
2343 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002344 SET_ERR("No result data provided by HAL for frame %d",
2345 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002346 return;
2347 }
Zhijun He204e3292014-07-14 17:09:23 -07002348
2349 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2350 // partial_result to 1 when metadata is included in this result.
2351 if (!mUsePartialResult &&
2352 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2353 result->result != NULL &&
2354 result->partial_result != 1) {
2355 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2356 " if partial result is not supported",
2357 frameNumber, result->partial_result);
2358 return;
2359 }
2360
2361 bool isPartialResult = false;
2362 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002363 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002364 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002365
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002366 // Get shutter timestamp and resultExtras from list of in-flight requests,
2367 // where it was added by the shutter notification for this frame. If the
2368 // shutter timestamp isn't received yet, append the output buffers to the
2369 // in-flight request and they will be returned when the shutter timestamp
2370 // arrives. Update the in-flight status and remove the in-flight entry if
2371 // all result data and shutter timestamp have been received.
2372 nsecs_t shutterTimestamp = 0;
2373
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002374 {
2375 Mutex::Autolock l(mInFlightLock);
2376 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2377 if (idx == NAME_NOT_FOUND) {
2378 SET_ERR("Unknown frame number for capture result: %d",
2379 frameNumber);
2380 return;
2381 }
2382 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002383 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2384 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2385 ", partialResultCount = %d",
2386 __FUNCTION__, request.resultExtras.requestId,
2387 request.resultExtras.frameNumber, request.resultExtras.burstId,
2388 result->partial_result);
2389 // Always update the partial count to the latest one if it's not 0
2390 // (buffers only). When framework aggregates adjacent partial results
2391 // into one, the latest partial count will be used.
2392 if (result->partial_result != 0)
2393 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002394
2395 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002396 if (mUsePartialResult && result->result != NULL) {
2397 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2398 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2399 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2400 " the range of [1, %d] when metadata is included in the result",
2401 frameNumber, result->partial_result, mNumPartialResults);
2402 return;
2403 }
2404 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002405 if (isPartialResult) {
2406 request.partialResult.collectedResult.append(result->result);
2407 }
Zhijun He204e3292014-07-14 17:09:23 -07002408 } else {
2409 camera_metadata_ro_entry_t partialResultEntry;
2410 res = find_camera_metadata_ro_entry(result->result,
2411 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2412 if (res != NAME_NOT_FOUND &&
2413 partialResultEntry.count > 0 &&
2414 partialResultEntry.data.u8[0] ==
2415 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2416 // A partial result. Flag this as such, and collect this
2417 // set of metadata into the in-flight entry.
2418 isPartialResult = true;
2419 request.partialResult.collectedResult.append(
2420 result->result);
2421 request.partialResult.collectedResult.erase(
2422 ANDROID_QUIRKS_PARTIAL_RESULT);
2423 }
2424 }
2425
2426 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002427 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002428 if (!request.partialResult.haveSent3A) {
2429 request.partialResult.haveSent3A =
2430 processPartial3AResult(frameNumber,
2431 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002432 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002433 }
2434 }
2435 }
2436
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002437 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002438 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002439
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002440 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002441 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002442 if (request.haveResultMetadata) {
2443 SET_ERR("Called multiple times with metadata for frame %d",
2444 frameNumber);
2445 return;
2446 }
Zhijun He204e3292014-07-14 17:09:23 -07002447 if (mUsePartialResult &&
2448 !request.partialResult.collectedResult.isEmpty()) {
2449 collectedPartialResult.acquire(
2450 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002451 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002452 request.haveResultMetadata = true;
2453 }
2454
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002455 uint32_t numBuffersReturned = result->num_output_buffers;
2456 if (result->input_buffer != NULL) {
2457 if (hasInputBufferInRequest) {
2458 numBuffersReturned += 1;
2459 } else {
2460 ALOGW("%s: Input buffer should be NULL if there is no input"
2461 " buffer sent in the request",
2462 __FUNCTION__);
2463 }
2464 }
2465 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002466 if (request.numBuffersLeft < 0) {
2467 SET_ERR("Too many buffers returned for frame %d",
2468 frameNumber);
2469 return;
2470 }
2471
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002472 camera_metadata_ro_entry_t entry;
2473 res = find_camera_metadata_ro_entry(result->result,
2474 ANDROID_SENSOR_TIMESTAMP, &entry);
2475 if (res == OK && entry.count == 1) {
2476 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002477 }
2478
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002479 // If shutter event isn't received yet, append the output buffers to
2480 // the in-flight request. Otherwise, return the output buffers to
2481 // streams.
2482 if (shutterTimestamp == 0) {
2483 request.pendingOutputBuffers.appendArray(result->output_buffers,
2484 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002485 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002486 returnOutputBuffers(result->output_buffers,
2487 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002488 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002489
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002490 if (result->result != NULL && !isPartialResult) {
2491 if (shutterTimestamp == 0) {
2492 request.pendingMetadata = result->result;
2493 request.partialResult.collectedResult = collectedPartialResult;
2494 } else {
2495 CameraMetadata metadata;
2496 metadata = result->result;
2497 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002498 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2499 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002500 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002501 }
2502
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002503 removeInFlightRequestIfReadyLocked(idx);
2504 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002505
Zhijun Hef0d962a2014-06-30 10:24:11 -07002506 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002507 if (hasInputBufferInRequest) {
2508 Camera3Stream *stream =
2509 Camera3Stream::cast(result->input_buffer->stream);
2510 res = stream->returnInputBuffer(*(result->input_buffer));
2511 // Note: stream may be deallocated at this point, if this buffer was the
2512 // last reference to it.
2513 if (res != OK) {
2514 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2515 " its stream:%s (%d)", __FUNCTION__,
2516 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002517 }
2518 } else {
2519 ALOGW("%s: Input buffer should be NULL if there is no input"
2520 " buffer sent in the request, skipping input buffer return.",
2521 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002522 }
2523 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002524}
2525
2526void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002527 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002528 NotificationListener *listener;
2529 {
2530 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002531 listener = mListener;
2532 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002533
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002534 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002535 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002536 return;
2537 }
2538
2539 switch (msg->type) {
2540 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002541 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002542 break;
2543 }
2544 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002545 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002546 break;
2547 }
2548 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002549 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002550 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002551 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002552}
2553
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002554void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2555 NotificationListener *listener) {
2556
2557 // Map camera HAL error codes to ICameraDeviceCallback error codes
2558 // Index into this with the HAL error code
2559 static const ICameraDeviceCallbacks::CameraErrorCode
2560 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2561 // 0 = Unused error code
2562 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2563 // 1 = CAMERA3_MSG_ERROR_DEVICE
2564 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2565 // 2 = CAMERA3_MSG_ERROR_REQUEST
2566 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2567 // 3 = CAMERA3_MSG_ERROR_RESULT
2568 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2569 // 4 = CAMERA3_MSG_ERROR_BUFFER
2570 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2571 };
2572
2573 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2574 ((msg.error_code >= 0) &&
2575 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2576 halErrorMap[msg.error_code] :
2577 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2578
2579 int streamId = 0;
2580 if (msg.error_stream != NULL) {
2581 Camera3Stream *stream =
2582 Camera3Stream::cast(msg.error_stream);
2583 streamId = stream->getId();
2584 }
2585 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2586 mId, __FUNCTION__, msg.frame_number,
2587 streamId, msg.error_code);
2588
2589 CaptureResultExtras resultExtras;
2590 switch (errorCode) {
2591 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2592 // SET_ERR calls notifyError
2593 SET_ERR("Camera HAL reported serious device error");
2594 break;
2595 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2596 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2597 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2598 {
2599 Mutex::Autolock l(mInFlightLock);
2600 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2601 if (idx >= 0) {
2602 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2603 r.requestStatus = msg.error_code;
2604 resultExtras = r.resultExtras;
2605 } else {
2606 resultExtras.frameNumber = msg.frame_number;
2607 ALOGE("Camera %d: %s: cannot find in-flight request on "
2608 "frame %" PRId64 " error", mId, __FUNCTION__,
2609 resultExtras.frameNumber);
2610 }
2611 }
2612 if (listener != NULL) {
2613 listener->notifyError(errorCode, resultExtras);
2614 } else {
2615 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2616 }
2617 break;
2618 default:
2619 // SET_ERR calls notifyError
2620 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2621 break;
2622 }
2623}
2624
2625void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2626 NotificationListener *listener) {
2627 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002628
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002629 // Set timestamp for the request in the in-flight tracking
2630 // and get the request ID to send upstream
2631 {
2632 Mutex::Autolock l(mInFlightLock);
2633 idx = mInFlightMap.indexOfKey(msg.frame_number);
2634 if (idx >= 0) {
2635 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002636
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002637 // Verify ordering of shutter notifications
2638 {
2639 Mutex::Autolock l(mOutputLock);
2640 // TODO: need to track errors for tighter bounds on expected frame number.
2641 if (r.hasInputBuffer) {
2642 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2643 SET_ERR("Shutter notification out-of-order. Expected "
2644 "notification for frame %d, got frame %d",
2645 mNextReprocessShutterFrameNumber, msg.frame_number);
2646 return;
2647 }
2648 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2649 } else {
2650 if (msg.frame_number < mNextShutterFrameNumber) {
2651 SET_ERR("Shutter notification out-of-order. Expected "
2652 "notification for frame %d, got frame %d",
2653 mNextShutterFrameNumber, msg.frame_number);
2654 return;
2655 }
2656 mNextShutterFrameNumber = msg.frame_number + 1;
2657 }
2658 }
2659
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002660 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2661 mId, __FUNCTION__,
2662 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2663 // Call listener, if any
2664 if (listener != NULL) {
2665 listener->notifyShutter(r.resultExtras, msg.timestamp);
2666 }
2667
2668 r.shutterTimestamp = msg.timestamp;
2669
2670 // send pending result and buffers
2671 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002672 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002673 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002674 returnOutputBuffers(r.pendingOutputBuffers.array(),
2675 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2676 r.pendingOutputBuffers.clear();
2677
2678 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002679 }
2680 }
2681 if (idx < 0) {
2682 SET_ERR("Shutter notification for non-existent frame number %d",
2683 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002684 }
2685}
2686
2687
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002688CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002689 ALOGV("%s", __FUNCTION__);
2690
Igor Murashkin1e479c02013-09-06 16:55:14 -07002691 CameraMetadata retVal;
2692
2693 if (mRequestThread != NULL) {
2694 retVal = mRequestThread->getLatestRequest();
2695 }
2696
Igor Murashkin1e479c02013-09-06 16:55:14 -07002697 return retVal;
2698}
2699
Jianing Weicb0652e2014-03-12 18:29:36 -07002700
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002701/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002702 * RequestThread inner class methods
2703 */
2704
2705Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002706 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002707 camera3_device_t *hal3Device,
2708 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002709 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002710 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002711 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002712 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002713 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002714 mReconfigured(false),
2715 mDoPause(false),
2716 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002717 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002718 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002719 mCurrentAfTriggerId(0),
2720 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002721 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2722 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002723 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002724}
2725
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002726void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002727 NotificationListener *listener) {
2728 Mutex::Autolock l(mRequestLock);
2729 mListener = listener;
2730}
2731
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002732void Camera3Device::RequestThread::configurationComplete() {
2733 Mutex::Autolock l(mRequestLock);
2734 mReconfigured = true;
2735}
2736
Jianing Wei90e59c92014-03-12 18:29:36 -07002737status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002738 List<sp<CaptureRequest> > &requests,
2739 /*out*/
2740 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002741 Mutex::Autolock l(mRequestLock);
2742 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2743 ++it) {
2744 mRequestQueue.push_back(*it);
2745 }
2746
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002747 if (lastFrameNumber != NULL) {
2748 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2749 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2750 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2751 *lastFrameNumber);
2752 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002753
Jianing Wei90e59c92014-03-12 18:29:36 -07002754 unpauseForNewRequests();
2755
2756 return OK;
2757}
2758
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002759
2760status_t Camera3Device::RequestThread::queueTrigger(
2761 RequestTrigger trigger[],
2762 size_t count) {
2763
2764 Mutex::Autolock l(mTriggerMutex);
2765 status_t ret;
2766
2767 for (size_t i = 0; i < count; ++i) {
2768 ret = queueTriggerLocked(trigger[i]);
2769
2770 if (ret != OK) {
2771 return ret;
2772 }
2773 }
2774
2775 return OK;
2776}
2777
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002778int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2779 sp<Camera3Device> d = device.promote();
2780 if (d != NULL) return d->mId;
2781 return 0;
2782}
2783
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002784status_t Camera3Device::RequestThread::queueTriggerLocked(
2785 RequestTrigger trigger) {
2786
2787 uint32_t tag = trigger.metadataTag;
2788 ssize_t index = mTriggerMap.indexOfKey(tag);
2789
2790 switch (trigger.getTagType()) {
2791 case TYPE_BYTE:
2792 // fall-through
2793 case TYPE_INT32:
2794 break;
2795 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002796 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2797 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002798 return INVALID_OPERATION;
2799 }
2800
2801 /**
2802 * Collect only the latest trigger, since we only have 1 field
2803 * in the request settings per trigger tag, and can't send more than 1
2804 * trigger per request.
2805 */
2806 if (index != NAME_NOT_FOUND) {
2807 mTriggerMap.editValueAt(index) = trigger;
2808 } else {
2809 mTriggerMap.add(tag, trigger);
2810 }
2811
2812 return OK;
2813}
2814
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002815status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002816 const RequestList &requests,
2817 /*out*/
2818 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002819 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002820 if (lastFrameNumber != NULL) {
2821 *lastFrameNumber = mRepeatingLastFrameNumber;
2822 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002823 mRepeatingRequests.clear();
2824 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2825 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002826
2827 unpauseForNewRequests();
2828
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002829 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002830 return OK;
2831}
2832
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002833bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2834 if (mRepeatingRequests.empty()) {
2835 return false;
2836 }
2837 int32_t requestId = requestIn->mResultExtras.requestId;
2838 const RequestList &repeatRequests = mRepeatingRequests;
2839 // All repeating requests are guaranteed to have same id so only check first quest
2840 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2841 return (firstRequest->mResultExtras.requestId == requestId);
2842}
2843
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002844status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002845 Mutex::Autolock l(mRequestLock);
2846 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002847 if (lastFrameNumber != NULL) {
2848 *lastFrameNumber = mRepeatingLastFrameNumber;
2849 }
2850 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002851 return OK;
2852}
2853
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002854status_t Camera3Device::RequestThread::clear(
2855 NotificationListener *listener,
2856 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002857 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002858 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002859
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002860 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002861
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002862 // Send errors for all requests pending in the request queue, including
2863 // pending repeating requests
2864 if (listener != NULL) {
2865 for (RequestList::iterator it = mRequestQueue.begin();
2866 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002867 // Abort the input buffers for reprocess requests.
2868 if ((*it)->mInputStream != NULL) {
2869 camera3_stream_buffer_t inputBuffer;
2870 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2871 if (res != OK) {
2872 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2873 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2874 } else {
2875 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2876 if (res != OK) {
2877 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2878 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2879 }
2880 }
2881 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002882 // Set the frame number this request would have had, if it
2883 // had been submitted; this frame number will not be reused.
2884 // The requestId and burstId fields were set when the request was
2885 // submitted originally (in convertMetadataListToRequestListLocked)
2886 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2887 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2888 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002889 }
2890 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002891 mRequestQueue.clear();
2892 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002893 if (lastFrameNumber != NULL) {
2894 *lastFrameNumber = mRepeatingLastFrameNumber;
2895 }
2896 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002897 return OK;
2898}
2899
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002900status_t Camera3Device::RequestThread::flush() {
2901 ATRACE_CALL();
2902 Mutex::Autolock l(mFlushLock);
2903
2904 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2905 return mHal3Device->ops->flush(mHal3Device);
2906 }
2907
2908 return -ENOTSUP;
2909}
2910
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002911void Camera3Device::RequestThread::setPaused(bool paused) {
2912 Mutex::Autolock l(mPauseLock);
2913 mDoPause = paused;
2914 mDoPauseSignal.signal();
2915}
2916
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002917status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2918 int32_t requestId, nsecs_t timeout) {
2919 Mutex::Autolock l(mLatestRequestMutex);
2920 status_t res;
2921 while (mLatestRequestId != requestId) {
2922 nsecs_t startTime = systemTime();
2923
2924 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2925 if (res != OK) return res;
2926
2927 timeout -= (systemTime() - startTime);
2928 }
2929
2930 return OK;
2931}
2932
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002933void Camera3Device::RequestThread::requestExit() {
2934 // Call parent to set up shutdown
2935 Thread::requestExit();
2936 // The exit from any possible waits
2937 mDoPauseSignal.signal();
2938 mRequestSignal.signal();
2939}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002940
Chien-Yu Chend196d612015-06-22 19:49:01 -07002941
2942/**
2943 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2944 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2945 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2946 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2947 * request.
2948 */
2949void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2950 request->mAeTriggerCancelOverride.applyAeLock = false;
2951 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2952
2953 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2954 return;
2955 }
2956
2957 camera_metadata_entry_t aePrecaptureTrigger =
2958 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2959 if (aePrecaptureTrigger.count > 0 &&
2960 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2961 // Always override CANCEL to IDLE
2962 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2963 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2964 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2965 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2966 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2967
2968 if (mAeLockAvailable == true) {
2969 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2970 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2971 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2972 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2973 request->mAeTriggerCancelOverride.applyAeLock = true;
2974 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2975 }
2976 }
2977 }
2978}
2979
2980/**
2981 * Override result metadata for cancelling AE precapture trigger applied in
2982 * handleAePrecaptureCancelRequest().
2983 */
2984void Camera3Device::overrideResultForPrecaptureCancel(
2985 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2986 if (aeTriggerCancelOverride.applyAeLock) {
2987 // Only devices <= v3.2 should have this override
2988 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2989 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2990 }
2991
2992 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2993 // Only devices <= v3.2 should have this override
2994 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2995 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2996 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2997 }
2998}
2999
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003000bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003001 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003002 status_t res;
3003
3004 // Handle paused state.
3005 if (waitIfPaused()) {
3006 return true;
3007 }
3008
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003009 // Wait for the next batch of requests.
3010 waitForNextRequestBatch();
3011 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003012 return true;
3013 }
3014
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003015 // Get the latest request ID, if any
3016 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003017 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003018 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003019 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003020 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003021 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003022 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3023 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003024 }
3025
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003026 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003027 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003028 if (res == TIMED_OUT) {
3029 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003030 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003031 return true;
3032 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003033 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003034 return false;
3035 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003036
Zhijun Hecc27e112013-10-03 16:12:43 -07003037 // Inform waitUntilRequestProcessed thread of a new request ID
3038 {
3039 Mutex::Autolock al(mLatestRequestMutex);
3040
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003041 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003042 mLatestRequestSignal.signal();
3043 }
3044
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003045 // Submit a batch of requests to HAL.
3046 // Use flush lock only when submitting multilple requests in a batch.
3047 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3048 // which may take a long time to finish so synchronizing flush() and
3049 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3050 // For now, only synchronize for high speed recording and we should figure something out for
3051 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003052 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003053
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003054 if (useFlushLock) {
3055 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003056 }
3057
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003058 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003059 mNextRequests.size());
3060 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003061 // Submit request and block until ready for next one
3062 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3063 ATRACE_BEGIN("camera3->process_capture_request");
3064 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3065 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003066
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003067 if (res != OK) {
3068 // Should only get a failure here for malformed requests or device-level
3069 // errors, so consider all errors fatal. Bad metadata failures should
3070 // come through notify.
3071 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3072 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3073 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003074 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003075 if (useFlushLock) {
3076 mFlushLock.unlock();
3077 }
3078 return false;
3079 }
3080
3081 // Mark that the request has be submitted successfully.
3082 nextRequest.submitted = true;
3083
3084 // Update the latest request sent to HAL
3085 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3086 Mutex::Autolock al(mLatestRequestMutex);
3087
3088 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3089 mLatestRequest.acquire(cloned);
3090 }
3091
3092 if (nextRequest.halRequest.settings != NULL) {
3093 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3094 }
3095
3096 // Remove any previously queued triggers (after unlock)
3097 res = removeTriggers(mPrevRequest);
3098 if (res != OK) {
3099 SET_ERR("RequestThread: Unable to remove triggers "
3100 "(capture request %d, HAL device: %s (%d)",
3101 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003102 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003103 if (useFlushLock) {
3104 mFlushLock.unlock();
3105 }
3106 return false;
3107 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003108 }
3109
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003110 if (useFlushLock) {
3111 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003112 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003113
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003114 // Unset as current request
3115 {
3116 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003117 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003118 }
3119
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003120 return true;
3121}
3122
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003123status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003124 ATRACE_CALL();
3125
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003126 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003127 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3128 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3129 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3130
3131 // Prepare a request to HAL
3132 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3133
3134 // Insert any queued triggers (before metadata is locked)
3135 status_t res = insertTriggers(captureRequest);
3136
3137 if (res < 0) {
3138 SET_ERR("RequestThread: Unable to insert triggers "
3139 "(capture request %d, HAL device: %s (%d)",
3140 halRequest->frame_number, strerror(-res), res);
3141 return INVALID_OPERATION;
3142 }
3143 int triggerCount = res;
3144 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3145 mPrevTriggers = triggerCount;
3146
3147 // If the request is the same as last, or we had triggers last time
3148 if (mPrevRequest != captureRequest || triggersMixedIn) {
3149 /**
3150 * HAL workaround:
3151 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3152 */
3153 res = addDummyTriggerIds(captureRequest);
3154 if (res != OK) {
3155 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3156 "(capture request %d, HAL device: %s (%d)",
3157 halRequest->frame_number, strerror(-res), res);
3158 return INVALID_OPERATION;
3159 }
3160
3161 /**
3162 * The request should be presorted so accesses in HAL
3163 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3164 */
3165 captureRequest->mSettings.sort();
3166 halRequest->settings = captureRequest->mSettings.getAndLock();
3167 mPrevRequest = captureRequest;
3168 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3169
3170 IF_ALOGV() {
3171 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3172 find_camera_metadata_ro_entry(
3173 halRequest->settings,
3174 ANDROID_CONTROL_AF_TRIGGER,
3175 &e
3176 );
3177 if (e.count > 0) {
3178 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3179 __FUNCTION__,
3180 halRequest->frame_number,
3181 e.data.u8[0]);
3182 }
3183 }
3184 } else {
3185 // leave request.settings NULL to indicate 'reuse latest given'
3186 ALOGVV("%s: Request settings are REUSED",
3187 __FUNCTION__);
3188 }
3189
3190 uint32_t totalNumBuffers = 0;
3191
3192 // Fill in buffers
3193 if (captureRequest->mInputStream != NULL) {
3194 halRequest->input_buffer = &captureRequest->mInputBuffer;
3195 totalNumBuffers += 1;
3196 } else {
3197 halRequest->input_buffer = NULL;
3198 }
3199
3200 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3201 captureRequest->mOutputStreams.size());
3202 halRequest->output_buffers = outputBuffers->array();
3203 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3204 res = captureRequest->mOutputStreams.editItemAt(i)->
3205 getBuffer(&outputBuffers->editItemAt(i));
3206 if (res != OK) {
3207 // Can't get output buffer from gralloc queue - this could be due to
3208 // abandoned queue or other consumer misbehavior, so not a fatal
3209 // error
3210 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3211 " %s (%d)", strerror(-res), res);
3212
3213 return TIMED_OUT;
3214 }
3215 halRequest->num_output_buffers++;
3216 }
3217 totalNumBuffers += halRequest->num_output_buffers;
3218
3219 // Log request in the in-flight queue
3220 sp<Camera3Device> parent = mParent.promote();
3221 if (parent == NULL) {
3222 // Should not happen, and nowhere to send errors to, so just log it
3223 CLOGE("RequestThread: Parent is gone");
3224 return INVALID_OPERATION;
3225 }
3226 res = parent->registerInFlight(halRequest->frame_number,
3227 totalNumBuffers, captureRequest->mResultExtras,
3228 /*hasInput*/halRequest->input_buffer != NULL,
3229 captureRequest->mAeTriggerCancelOverride);
3230 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3231 ", burstId = %" PRId32 ".",
3232 __FUNCTION__,
3233 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3234 captureRequest->mResultExtras.burstId);
3235 if (res != OK) {
3236 SET_ERR("RequestThread: Unable to register new in-flight request:"
3237 " %s (%d)", strerror(-res), res);
3238 return INVALID_OPERATION;
3239 }
3240 }
3241
3242 return OK;
3243}
3244
Igor Murashkin1e479c02013-09-06 16:55:14 -07003245CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3246 Mutex::Autolock al(mLatestRequestMutex);
3247
3248 ALOGV("RequestThread::%s", __FUNCTION__);
3249
3250 return mLatestRequest;
3251}
3252
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003253bool Camera3Device::RequestThread::isStreamPending(
3254 sp<Camera3StreamInterface>& stream) {
3255 Mutex::Autolock l(mRequestLock);
3256
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003257 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003258 if (!nextRequest.submitted) {
3259 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3260 if (stream == s) return true;
3261 }
3262 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003263 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003264 }
3265
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003266 for (const auto& request : mRequestQueue) {
3267 for (const auto& s : request->mOutputStreams) {
3268 if (stream == s) return true;
3269 }
3270 if (stream == request->mInputStream) return true;
3271 }
3272
3273 for (const auto& request : mRepeatingRequests) {
3274 for (const auto& s : request->mOutputStreams) {
3275 if (stream == s) return true;
3276 }
3277 if (stream == request->mInputStream) return true;
3278 }
3279
3280 return false;
3281}
Jianing Weicb0652e2014-03-12 18:29:36 -07003282
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003283void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3284 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003285 return;
3286 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003287
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003288 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003289 // Skip the ones that have been submitted successfully.
3290 if (nextRequest.submitted) {
3291 continue;
3292 }
3293
3294 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3295 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3296 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3297
3298 if (halRequest->settings != NULL) {
3299 captureRequest->mSettings.unlock(halRequest->settings);
3300 }
3301
3302 if (captureRequest->mInputStream != NULL) {
3303 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3304 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3305 }
3306
3307 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3308 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3309 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3310 }
3311
3312 if (sendRequestError) {
3313 Mutex::Autolock l(mRequestLock);
3314 if (mListener != NULL) {
3315 mListener->notifyError(
3316 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3317 captureRequest->mResultExtras);
3318 }
3319 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003320 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003321
3322 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003323 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003324}
3325
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003326void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003327 // Optimized a bit for the simple steady-state case (single repeating
3328 // request), to avoid putting that request in the queue temporarily.
3329 Mutex::Autolock l(mRequestLock);
3330
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003331 assert(mNextRequests.empty());
3332
3333 NextRequest nextRequest;
3334 nextRequest.captureRequest = waitForNextRequestLocked();
3335 if (nextRequest.captureRequest == nullptr) {
3336 return;
3337 }
3338
3339 nextRequest.halRequest = camera3_capture_request_t();
3340 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003341 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003342
3343 // Wait for additional requests
3344 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3345
3346 for (size_t i = 1; i < batchSize; i++) {
3347 NextRequest additionalRequest;
3348 additionalRequest.captureRequest = waitForNextRequestLocked();
3349 if (additionalRequest.captureRequest == nullptr) {
3350 break;
3351 }
3352
3353 additionalRequest.halRequest = camera3_capture_request_t();
3354 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003355 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003356 }
3357
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003358 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003359 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003360 mNextRequests.size(), batchSize);
3361 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003362 }
3363
3364 return;
3365}
3366
3367sp<Camera3Device::CaptureRequest>
3368 Camera3Device::RequestThread::waitForNextRequestLocked() {
3369 status_t res;
3370 sp<CaptureRequest> nextRequest;
3371
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003372 while (mRequestQueue.empty()) {
3373 if (!mRepeatingRequests.empty()) {
3374 // Always atomically enqueue all requests in a repeating request
3375 // list. Guarantees a complete in-sequence set of captures to
3376 // application.
3377 const RequestList &requests = mRepeatingRequests;
3378 RequestList::const_iterator firstRequest =
3379 requests.begin();
3380 nextRequest = *firstRequest;
3381 mRequestQueue.insert(mRequestQueue.end(),
3382 ++firstRequest,
3383 requests.end());
3384 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003385
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003386 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003387
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003388 break;
3389 }
3390
3391 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3392
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003393 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3394 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003395 Mutex::Autolock pl(mPauseLock);
3396 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003397 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003398 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003399 // Let the tracker know
3400 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3401 if (statusTracker != 0) {
3402 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3403 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003404 }
3405 // Stop waiting for now and let thread management happen
3406 return NULL;
3407 }
3408 }
3409
3410 if (nextRequest == NULL) {
3411 // Don't have a repeating request already in hand, so queue
3412 // must have an entry now.
3413 RequestList::iterator firstRequest =
3414 mRequestQueue.begin();
3415 nextRequest = *firstRequest;
3416 mRequestQueue.erase(firstRequest);
3417 }
3418
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003419 // In case we've been unpaused by setPaused clearing mDoPause, need to
3420 // update internal pause state (capture/setRepeatingRequest unpause
3421 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003422 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003423 if (mPaused) {
3424 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3425 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3426 if (statusTracker != 0) {
3427 statusTracker->markComponentActive(mStatusId);
3428 }
3429 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003430 mPaused = false;
3431
3432 // Check if we've reconfigured since last time, and reset the preview
3433 // request if so. Can't use 'NULL request == repeat' across configure calls.
3434 if (mReconfigured) {
3435 mPrevRequest.clear();
3436 mReconfigured = false;
3437 }
3438
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003439 if (nextRequest != NULL) {
3440 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003441 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3442 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003443
3444 // Since RequestThread::clear() removes buffers from the input stream,
3445 // get the right buffer here before unlocking mRequestLock
3446 if (nextRequest->mInputStream != NULL) {
3447 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3448 if (res != OK) {
3449 // Can't get input buffer from gralloc queue - this could be due to
3450 // disconnected queue or other producer misbehavior, so not a fatal
3451 // error
3452 ALOGE("%s: Can't get input buffer, skipping request:"
3453 " %s (%d)", __FUNCTION__, strerror(-res), res);
3454 if (mListener != NULL) {
3455 mListener->notifyError(
3456 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3457 nextRequest->mResultExtras);
3458 }
3459 return NULL;
3460 }
3461 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003462 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003463
3464 handleAePrecaptureCancelRequest(nextRequest);
3465
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003466 return nextRequest;
3467}
3468
3469bool Camera3Device::RequestThread::waitIfPaused() {
3470 status_t res;
3471 Mutex::Autolock l(mPauseLock);
3472 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003473 if (mPaused == false) {
3474 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003475 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3476 // Let the tracker know
3477 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3478 if (statusTracker != 0) {
3479 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3480 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003481 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003482
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003483 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003484 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003485 return true;
3486 }
3487 }
3488 // We don't set mPaused to false here, because waitForNextRequest needs
3489 // to further manage the paused state in case of starvation.
3490 return false;
3491}
3492
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003493void Camera3Device::RequestThread::unpauseForNewRequests() {
3494 // With work to do, mark thread as unpaused.
3495 // If paused by request (setPaused), don't resume, to avoid
3496 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003497 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003498 Mutex::Autolock p(mPauseLock);
3499 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003500 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3501 if (mPaused) {
3502 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3503 if (statusTracker != 0) {
3504 statusTracker->markComponentActive(mStatusId);
3505 }
3506 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003507 mPaused = false;
3508 }
3509}
3510
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003511void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3512 sp<Camera3Device> parent = mParent.promote();
3513 if (parent != NULL) {
3514 va_list args;
3515 va_start(args, fmt);
3516
3517 parent->setErrorStateV(fmt, args);
3518
3519 va_end(args);
3520 }
3521}
3522
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003523status_t Camera3Device::RequestThread::insertTriggers(
3524 const sp<CaptureRequest> &request) {
3525
3526 Mutex::Autolock al(mTriggerMutex);
3527
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003528 sp<Camera3Device> parent = mParent.promote();
3529 if (parent == NULL) {
3530 CLOGE("RequestThread: Parent is gone");
3531 return DEAD_OBJECT;
3532 }
3533
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003534 CameraMetadata &metadata = request->mSettings;
3535 size_t count = mTriggerMap.size();
3536
3537 for (size_t i = 0; i < count; ++i) {
3538 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003539 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003540
3541 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3542 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3543 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003544 if (isAeTrigger) {
3545 request->mResultExtras.precaptureTriggerId = triggerId;
3546 mCurrentPreCaptureTriggerId = triggerId;
3547 } else {
3548 request->mResultExtras.afTriggerId = triggerId;
3549 mCurrentAfTriggerId = triggerId;
3550 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003551 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3552 continue; // Trigger ID tag is deprecated since device HAL 3.2
3553 }
3554 }
3555
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003556 camera_metadata_entry entry = metadata.find(tag);
3557
3558 if (entry.count > 0) {
3559 /**
3560 * Already has an entry for this trigger in the request.
3561 * Rewrite it with our requested trigger value.
3562 */
3563 RequestTrigger oldTrigger = trigger;
3564
3565 oldTrigger.entryValue = entry.data.u8[0];
3566
3567 mTriggerReplacedMap.add(tag, oldTrigger);
3568 } else {
3569 /**
3570 * More typical, no trigger entry, so we just add it
3571 */
3572 mTriggerRemovedMap.add(tag, trigger);
3573 }
3574
3575 status_t res;
3576
3577 switch (trigger.getTagType()) {
3578 case TYPE_BYTE: {
3579 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3580 res = metadata.update(tag,
3581 &entryValue,
3582 /*count*/1);
3583 break;
3584 }
3585 case TYPE_INT32:
3586 res = metadata.update(tag,
3587 &trigger.entryValue,
3588 /*count*/1);
3589 break;
3590 default:
3591 ALOGE("%s: Type not supported: 0x%x",
3592 __FUNCTION__,
3593 trigger.getTagType());
3594 return INVALID_OPERATION;
3595 }
3596
3597 if (res != OK) {
3598 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3599 ", value %d", __FUNCTION__, trigger.getTagName(),
3600 trigger.entryValue);
3601 return res;
3602 }
3603
3604 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3605 trigger.getTagName(),
3606 trigger.entryValue);
3607 }
3608
3609 mTriggerMap.clear();
3610
3611 return count;
3612}
3613
3614status_t Camera3Device::RequestThread::removeTriggers(
3615 const sp<CaptureRequest> &request) {
3616 Mutex::Autolock al(mTriggerMutex);
3617
3618 CameraMetadata &metadata = request->mSettings;
3619
3620 /**
3621 * Replace all old entries with their old values.
3622 */
3623 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3624 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3625
3626 status_t res;
3627
3628 uint32_t tag = trigger.metadataTag;
3629 switch (trigger.getTagType()) {
3630 case TYPE_BYTE: {
3631 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3632 res = metadata.update(tag,
3633 &entryValue,
3634 /*count*/1);
3635 break;
3636 }
3637 case TYPE_INT32:
3638 res = metadata.update(tag,
3639 &trigger.entryValue,
3640 /*count*/1);
3641 break;
3642 default:
3643 ALOGE("%s: Type not supported: 0x%x",
3644 __FUNCTION__,
3645 trigger.getTagType());
3646 return INVALID_OPERATION;
3647 }
3648
3649 if (res != OK) {
3650 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3651 ", trigger value %d", __FUNCTION__,
3652 trigger.getTagName(), trigger.entryValue);
3653 return res;
3654 }
3655 }
3656 mTriggerReplacedMap.clear();
3657
3658 /**
3659 * Remove all new entries.
3660 */
3661 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3662 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3663 status_t res = metadata.erase(trigger.metadataTag);
3664
3665 if (res != OK) {
3666 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3667 ", trigger value %d", __FUNCTION__,
3668 trigger.getTagName(), trigger.entryValue);
3669 return res;
3670 }
3671 }
3672 mTriggerRemovedMap.clear();
3673
3674 return OK;
3675}
3676
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003677status_t Camera3Device::RequestThread::addDummyTriggerIds(
3678 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003679 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003680 static const int32_t dummyTriggerId = 1;
3681 status_t res;
3682
3683 CameraMetadata &metadata = request->mSettings;
3684
3685 // If AF trigger is active, insert a dummy AF trigger ID if none already
3686 // exists
3687 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3688 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3689 if (afTrigger.count > 0 &&
3690 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3691 afId.count == 0) {
3692 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3693 if (res != OK) return res;
3694 }
3695
3696 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3697 // if none already exists
3698 camera_metadata_entry pcTrigger =
3699 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3700 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3701 if (pcTrigger.count > 0 &&
3702 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3703 pcId.count == 0) {
3704 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3705 &dummyTriggerId, 1);
3706 if (res != OK) return res;
3707 }
3708
3709 return OK;
3710}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003711
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003712/**
3713 * PreparerThread inner class methods
3714 */
3715
3716Camera3Device::PreparerThread::PreparerThread() :
3717 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3718}
3719
3720Camera3Device::PreparerThread::~PreparerThread() {
3721 Thread::requestExitAndWait();
3722 if (mCurrentStream != nullptr) {
3723 mCurrentStream->cancelPrepare();
3724 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3725 mCurrentStream.clear();
3726 }
3727 clear();
3728}
3729
Ruben Brunkc78ac262015-08-13 17:58:46 -07003730status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003731 status_t res;
3732
3733 Mutex::Autolock l(mLock);
3734
Ruben Brunkc78ac262015-08-13 17:58:46 -07003735 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003736 if (res == OK) {
3737 // No preparation needed, fire listener right off
3738 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3739 if (mListener) {
3740 mListener->notifyPrepared(stream->getId());
3741 }
3742 return OK;
3743 } else if (res != NOT_ENOUGH_DATA) {
3744 return res;
3745 }
3746
3747 // Need to prepare, start up thread if necessary
3748 if (!mActive) {
3749 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3750 // isn't running
3751 Thread::requestExitAndWait();
3752 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3753 if (res != OK) {
3754 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3755 if (mListener) {
3756 mListener->notifyPrepared(stream->getId());
3757 }
3758 return res;
3759 }
3760 mCancelNow = false;
3761 mActive = true;
3762 ALOGV("%s: Preparer stream started", __FUNCTION__);
3763 }
3764
3765 // queue up the work
3766 mPendingStreams.push_back(stream);
3767 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3768
3769 return OK;
3770}
3771
3772status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003773 Mutex::Autolock l(mLock);
3774
3775 for (const auto& stream : mPendingStreams) {
3776 stream->cancelPrepare();
3777 }
3778 mPendingStreams.clear();
3779 mCancelNow = true;
3780
3781 return OK;
3782}
3783
3784void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3785 Mutex::Autolock l(mLock);
3786 mListener = listener;
3787}
3788
3789bool Camera3Device::PreparerThread::threadLoop() {
3790 status_t res;
3791 {
3792 Mutex::Autolock l(mLock);
3793 if (mCurrentStream == nullptr) {
3794 // End thread if done with work
3795 if (mPendingStreams.empty()) {
3796 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3797 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3798 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3799 mActive = false;
3800 return false;
3801 }
3802
3803 // Get next stream to prepare
3804 auto it = mPendingStreams.begin();
3805 mCurrentStream = *it;
3806 mPendingStreams.erase(it);
3807 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3808 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3809 } else if (mCancelNow) {
3810 mCurrentStream->cancelPrepare();
3811 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3812 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3813 mCurrentStream.clear();
3814 mCancelNow = false;
3815 return true;
3816 }
3817 }
3818
3819 res = mCurrentStream->prepareNextBuffer();
3820 if (res == NOT_ENOUGH_DATA) return true;
3821 if (res != OK) {
3822 // Something bad happened; try to recover by cancelling prepare and
3823 // signalling listener anyway
3824 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3825 mCurrentStream->getId(), res, strerror(-res));
3826 mCurrentStream->cancelPrepare();
3827 }
3828
3829 // This stream has finished, notify listener
3830 Mutex::Autolock l(mLock);
3831 if (mListener) {
3832 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3833 mCurrentStream->getId());
3834 mListener->notifyPrepared(mCurrentStream->getId());
3835 }
3836
3837 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3838 mCurrentStream.clear();
3839
3840 return true;
3841}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003842
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003843/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003844 * Static callback forwarding methods from HAL to instance
3845 */
3846
3847void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3848 const camera3_capture_result *result) {
3849 Camera3Device *d =
3850 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003851
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003852 d->processCaptureResult(result);
3853}
3854
3855void Camera3Device::sNotify(const camera3_callback_ops *cb,
3856 const camera3_notify_msg *msg) {
3857 Camera3Device *d =
3858 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3859 d->notify(msg);
3860}
3861
3862}; // namespace android