blob: cac82f3eb9f8b50668358c6cc33962f19f0d239e [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 Heedd41ae2016-02-03 14:45:53 -0800974 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -0800975 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -0800976 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -0800977 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 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001011 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1012 * requires buffers to be statically allocated for internal static buffer registration, while
1013 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1014 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001015 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001016 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001017 newStream->setBufferManager(mBufferManager);
1018 }
1019
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001020 res = mOutputStreams.add(mNextStreamId, newStream);
1021 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001022 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001023 return res;
1024 }
1025
1026 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001027 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001028
1029 // Continue captures if active at start
1030 if (wasActive) {
1031 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1032 res = configureStreamsLocked();
1033 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001034 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1035 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001036 return res;
1037 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001038 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001039 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001040 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001041 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001042}
1043
1044status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1045 ATRACE_CALL();
1046 (void)outputId; (void)id;
1047
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001048 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001049 return INVALID_OPERATION;
1050}
1051
1052
1053status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001054 uint32_t *width, uint32_t *height,
1055 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001056 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001057 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001058 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001059
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001060 switch (mStatus) {
1061 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001062 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001063 return INVALID_OPERATION;
1064 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001065 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001066 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001067 case STATUS_UNCONFIGURED:
1068 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 case STATUS_ACTIVE:
1070 // OK
1071 break;
1072 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001073 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001074 return INVALID_OPERATION;
1075 }
1076
1077 ssize_t idx = mOutputStreams.indexOfKey(id);
1078 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001079 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001080 return idx;
1081 }
1082
1083 if (width) *width = mOutputStreams[idx]->getWidth();
1084 if (height) *height = mOutputStreams[idx]->getHeight();
1085 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001086 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001087 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001088}
1089
1090status_t Camera3Device::setStreamTransform(int id,
1091 int transform) {
1092 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001093 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001094 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001095
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001096 switch (mStatus) {
1097 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001098 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001099 return INVALID_OPERATION;
1100 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001101 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001102 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001103 case STATUS_UNCONFIGURED:
1104 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001105 case STATUS_ACTIVE:
1106 // OK
1107 break;
1108 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001109 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001110 return INVALID_OPERATION;
1111 }
1112
1113 ssize_t idx = mOutputStreams.indexOfKey(id);
1114 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001115 CLOGE("Stream %d does not exist",
1116 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001117 return BAD_VALUE;
1118 }
1119
1120 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001121}
1122
1123status_t Camera3Device::deleteStream(int id) {
1124 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001125 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001126 Mutex::Autolock l(mLock);
1127 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001128
Igor Murashkine2172be2013-05-28 15:31:39 -07001129 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
1130
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001131 // CameraDevice semantics require device to already be idle before
1132 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001133 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -07001134 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
1135 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001136 }
1137
Igor Murashkin2fba5842013-04-22 14:03:54 -07001138 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001139 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001140 if (mInputStream != NULL && id == mInputStream->getId()) {
1141 deletedStream = mInputStream;
1142 mInputStream.clear();
1143 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001144 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001145 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001146 return BAD_VALUE;
1147 }
Zhijun He5f446352014-01-22 09:49:33 -08001148 }
1149
1150 // Delete output stream or the output part of a bi-directional stream.
1151 if (outputStreamIdx != NAME_NOT_FOUND) {
1152 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001153 mOutputStreams.removeItem(id);
1154 }
1155
1156 // Free up the stream endpoint so that it can be used by some other stream
1157 res = deletedStream->disconnect();
1158 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001159 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001160 // fall through since we want to still list the stream as deleted.
1161 }
1162 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001163 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001164
1165 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001166}
1167
1168status_t Camera3Device::deleteReprocessStream(int id) {
1169 ATRACE_CALL();
1170 (void)id;
1171
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001172 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001173 return INVALID_OPERATION;
1174}
1175
Zhijun He1fa89992015-06-01 15:44:31 -07001176status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001177 ATRACE_CALL();
1178 ALOGV("%s: E", __FUNCTION__);
1179
1180 Mutex::Autolock il(mInterfaceLock);
1181 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001182
1183 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1184 mNeedConfig = true;
1185 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1186 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001187
1188 return configureStreamsLocked();
1189}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001190
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001191status_t Camera3Device::getInputBufferProducer(
1192 sp<IGraphicBufferProducer> *producer) {
1193 Mutex::Autolock il(mInterfaceLock);
1194 Mutex::Autolock l(mLock);
1195
1196 if (producer == NULL) {
1197 return BAD_VALUE;
1198 } else if (mInputStream == NULL) {
1199 return INVALID_OPERATION;
1200 }
1201
1202 return mInputStream->getInputBufferProducer(producer);
1203}
1204
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001205status_t Camera3Device::createDefaultRequest(int templateId,
1206 CameraMetadata *request) {
1207 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001208 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001209 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001210 Mutex::Autolock l(mLock);
1211
1212 switch (mStatus) {
1213 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001214 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001215 return INVALID_OPERATION;
1216 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001217 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001218 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001219 case STATUS_UNCONFIGURED:
1220 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001221 case STATUS_ACTIVE:
1222 // OK
1223 break;
1224 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001225 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001226 return INVALID_OPERATION;
1227 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001228
Zhijun Hea1530f12014-09-14 12:44:20 -07001229 if (!mRequestTemplateCache[templateId].isEmpty()) {
1230 *request = mRequestTemplateCache[templateId];
1231 return OK;
1232 }
1233
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001234 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001235 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001236 rawRequest = mHal3Device->ops->construct_default_request_settings(
1237 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001238 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001239 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001240 ALOGI("%s: template %d is not supported on this camera device",
1241 __FUNCTION__, templateId);
1242 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001243 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001244 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001245 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001246
1247 return OK;
1248}
1249
1250status_t Camera3Device::waitUntilDrained() {
1251 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001252 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001253 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001254
Zhijun He69a37482014-03-23 18:44:49 -07001255 return waitUntilDrainedLocked();
1256}
1257
1258status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001259 switch (mStatus) {
1260 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001261 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001262 ALOGV("%s: Already idle", __FUNCTION__);
1263 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001264 case STATUS_CONFIGURED:
1265 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001266 case STATUS_ERROR:
1267 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001268 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001269 break;
1270 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001271 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001272 return INVALID_OPERATION;
1273 }
1274
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001275 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1276 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001277 if (res != OK) {
1278 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1279 res);
1280 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001281 return res;
1282}
1283
Ruben Brunk183f0562015-08-12 12:55:02 -07001284
1285void Camera3Device::internalUpdateStatusLocked(Status status) {
1286 mStatus = status;
1287 mRecentStatusUpdates.add(mStatus);
1288 mStatusChanged.broadcast();
1289}
1290
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001291// Pause to reconfigure
1292status_t Camera3Device::internalPauseAndWaitLocked() {
1293 mRequestThread->setPaused(true);
1294 mPauseStateNotify = true;
1295
1296 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1297 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1298 if (res != OK) {
1299 SET_ERR_L("Can't idle device in %f seconds!",
1300 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001301 }
1302
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001303 return res;
1304}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001305
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001306// Resume after internalPauseAndWaitLocked
1307status_t Camera3Device::internalResumeLocked() {
1308 status_t res;
1309
1310 mRequestThread->setPaused(false);
1311
1312 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1313 if (res != OK) {
1314 SET_ERR_L("Can't transition to active in %f seconds!",
1315 kActiveTimeout/1e9);
1316 }
1317 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001318 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001319}
1320
Ruben Brunk183f0562015-08-12 12:55:02 -07001321status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001322 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001323
1324 size_t startIndex = 0;
1325 if (mStatusWaiters == 0) {
1326 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1327 // this status list
1328 mRecentStatusUpdates.clear();
1329 } else {
1330 // If other threads are waiting on updates to this status list, set the position of the
1331 // first element that this list will check rather than clearing the list.
1332 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001333 }
1334
Ruben Brunk183f0562015-08-12 12:55:02 -07001335 mStatusWaiters++;
1336
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001337 bool stateSeen = false;
1338 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001339 if (active == (mStatus == STATUS_ACTIVE)) {
1340 // Desired state is current
1341 break;
1342 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001343
1344 res = mStatusChanged.waitRelative(mLock, timeout);
1345 if (res != OK) break;
1346
Ruben Brunk183f0562015-08-12 12:55:02 -07001347 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1348 // transitions.
1349 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1350 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1351 __FUNCTION__);
1352
1353 // Encountered desired state since we began waiting
1354 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001355 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1356 stateSeen = true;
1357 break;
1358 }
1359 }
1360 } while (!stateSeen);
1361
Ruben Brunk183f0562015-08-12 12:55:02 -07001362 mStatusWaiters--;
1363
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001364 return res;
1365}
1366
1367
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001368status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1369 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001370 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001371
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001372 if (listener != NULL && mListener != NULL) {
1373 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1374 }
1375 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001376 mRequestThread->setNotificationListener(listener);
1377 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001378
1379 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001380}
1381
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001382bool Camera3Device::willNotify3A() {
1383 return false;
1384}
1385
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001386status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001387 status_t res;
1388 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001389
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001390 while (mResultQueue.empty()) {
1391 res = mResultSignal.waitRelative(mOutputLock, timeout);
1392 if (res == TIMED_OUT) {
1393 return res;
1394 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001395 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001396 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001397 return res;
1398 }
1399 }
1400 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001401}
1402
Jianing Weicb0652e2014-03-12 18:29:36 -07001403status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001404 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001405 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001406
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001407 if (mResultQueue.empty()) {
1408 return NOT_ENOUGH_DATA;
1409 }
1410
Jianing Weicb0652e2014-03-12 18:29:36 -07001411 if (frame == NULL) {
1412 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1413 return BAD_VALUE;
1414 }
1415
1416 CaptureResult &result = *(mResultQueue.begin());
1417 frame->mResultExtras = result.mResultExtras;
1418 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001419 mResultQueue.erase(mResultQueue.begin());
1420
1421 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001422}
1423
1424status_t Camera3Device::triggerAutofocus(uint32_t id) {
1425 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001426 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001427
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001428 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1429 // Mix-in this trigger into the next request and only the next request.
1430 RequestTrigger trigger[] = {
1431 {
1432 ANDROID_CONTROL_AF_TRIGGER,
1433 ANDROID_CONTROL_AF_TRIGGER_START
1434 },
1435 {
1436 ANDROID_CONTROL_AF_TRIGGER_ID,
1437 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001438 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001439 };
1440
1441 return mRequestThread->queueTrigger(trigger,
1442 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001443}
1444
1445status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1446 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001447 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001448
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001449 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1450 // Mix-in this trigger into the next request and only the next request.
1451 RequestTrigger trigger[] = {
1452 {
1453 ANDROID_CONTROL_AF_TRIGGER,
1454 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1455 },
1456 {
1457 ANDROID_CONTROL_AF_TRIGGER_ID,
1458 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001459 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001460 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001461
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001462 return mRequestThread->queueTrigger(trigger,
1463 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001464}
1465
1466status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1467 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001468 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001469
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001470 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1471 // Mix-in this trigger into the next request and only the next request.
1472 RequestTrigger trigger[] = {
1473 {
1474 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1475 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1476 },
1477 {
1478 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1479 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001480 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001481 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001482
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001483 return mRequestThread->queueTrigger(trigger,
1484 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001485}
1486
1487status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1488 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1489 ATRACE_CALL();
1490 (void)reprocessStreamId; (void)buffer; (void)listener;
1491
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001492 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001493 return INVALID_OPERATION;
1494}
1495
Jianing Weicb0652e2014-03-12 18:29:36 -07001496status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001497 ATRACE_CALL();
1498 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001499 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001500
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001501 NotificationListener* listener;
1502 {
1503 Mutex::Autolock l(mOutputLock);
1504 listener = mListener;
1505 }
1506
Zhijun He7ef20392014-04-21 16:04:17 -07001507 {
1508 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001509 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001510 }
1511
Zhijun He491e3412013-12-27 10:57:44 -08001512 status_t res;
1513 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001514 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001515 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001516 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001517 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001518 }
1519
1520 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001521}
1522
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001523status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001524 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1525}
1526
1527status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001528 ATRACE_CALL();
1529 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001530 Mutex::Autolock il(mInterfaceLock);
1531 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001532
1533 sp<Camera3StreamInterface> stream;
1534 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1535 if (outputStreamIdx == NAME_NOT_FOUND) {
1536 CLOGE("Stream %d does not exist", streamId);
1537 return BAD_VALUE;
1538 }
1539
1540 stream = mOutputStreams.editValueAt(outputStreamIdx);
1541
1542 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001543 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001544 return BAD_VALUE;
1545 }
1546
1547 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001548 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001549 return BAD_VALUE;
1550 }
1551
Ruben Brunkc78ac262015-08-13 17:58:46 -07001552 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001553}
1554
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001555status_t Camera3Device::tearDown(int streamId) {
1556 ATRACE_CALL();
1557 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1558 Mutex::Autolock il(mInterfaceLock);
1559 Mutex::Autolock l(mLock);
1560
1561 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1562 // since we cannot call register_stream_buffers except right after configure_streams.
1563 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1564 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1565 __FUNCTION__, mHal3Device->common.version);
1566 return NO_INIT;
1567 }
1568
1569 sp<Camera3StreamInterface> stream;
1570 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1571 if (outputStreamIdx == NAME_NOT_FOUND) {
1572 CLOGE("Stream %d does not exist", streamId);
1573 return BAD_VALUE;
1574 }
1575
1576 stream = mOutputStreams.editValueAt(outputStreamIdx);
1577
1578 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1579 CLOGE("Stream %d is a target of a in-progress request", streamId);
1580 return BAD_VALUE;
1581 }
1582
1583 return stream->tearDown();
1584}
1585
Zhijun He204e3292014-07-14 17:09:23 -07001586uint32_t Camera3Device::getDeviceVersion() {
1587 ATRACE_CALL();
1588 Mutex::Autolock il(mInterfaceLock);
1589 return mDeviceVersion;
1590}
1591
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001592/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001593 * Methods called by subclasses
1594 */
1595
1596void Camera3Device::notifyStatus(bool idle) {
1597 {
1598 // Need mLock to safely update state and synchronize to current
1599 // state of methods in flight.
1600 Mutex::Autolock l(mLock);
1601 // We can get various system-idle notices from the status tracker
1602 // while starting up. Only care about them if we've actually sent
1603 // in some requests recently.
1604 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1605 return;
1606 }
1607 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1608 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001609 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001610
1611 // Skip notifying listener if we're doing some user-transparent
1612 // state changes
1613 if (mPauseStateNotify) return;
1614 }
1615 NotificationListener *listener;
1616 {
1617 Mutex::Autolock l(mOutputLock);
1618 listener = mListener;
1619 }
1620 if (idle && listener != NULL) {
1621 listener->notifyIdle();
1622 }
1623}
1624
1625/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001626 * Camera3Device private methods
1627 */
1628
1629sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1630 const CameraMetadata &request) {
1631 ATRACE_CALL();
1632 status_t res;
1633
1634 sp<CaptureRequest> newRequest = new CaptureRequest;
1635 newRequest->mSettings = request;
1636
1637 camera_metadata_entry_t inputStreams =
1638 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1639 if (inputStreams.count > 0) {
1640 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001641 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001642 CLOGE("Request references unknown input stream %d",
1643 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001644 return NULL;
1645 }
1646 // Lazy completion of stream configuration (allocation/registration)
1647 // on first use
1648 if (mInputStream->isConfiguring()) {
1649 res = mInputStream->finishConfiguration(mHal3Device);
1650 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001651 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001652 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001653 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001654 return NULL;
1655 }
1656 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001657 // Check if stream is being prepared
1658 if (mInputStream->isPreparing()) {
1659 CLOGE("Request references an input stream that's being prepared!");
1660 return NULL;
1661 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001662
1663 newRequest->mInputStream = mInputStream;
1664 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1665 }
1666
1667 camera_metadata_entry_t streams =
1668 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1669 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001670 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001671 return NULL;
1672 }
1673
1674 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001675 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001676 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001677 CLOGE("Request references unknown stream %d",
1678 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001679 return NULL;
1680 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001681 sp<Camera3OutputStreamInterface> stream =
1682 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001683
1684 // Lazy completion of stream configuration (allocation/registration)
1685 // on first use
1686 if (stream->isConfiguring()) {
1687 res = stream->finishConfiguration(mHal3Device);
1688 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001689 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1690 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001691 return NULL;
1692 }
1693 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001694 // Check if stream is being prepared
1695 if (stream->isPreparing()) {
1696 CLOGE("Request references an output stream that's being prepared!");
1697 return NULL;
1698 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001699
1700 newRequest->mOutputStreams.push(stream);
1701 }
1702 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001703 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001704
1705 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001706}
1707
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001708bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1709 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1710 Size size = mSupportedOpaqueInputSizes[i];
1711 if (size.width == width && size.height == height) {
1712 return true;
1713 }
1714 }
1715
1716 return false;
1717}
1718
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001719status_t Camera3Device::configureStreamsLocked() {
1720 ATRACE_CALL();
1721 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001722
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001723 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001724 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001725 return INVALID_OPERATION;
1726 }
1727
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001728 if (!mNeedConfig) {
1729 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1730 return OK;
1731 }
1732
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001733 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1734 // adding a dummy stream instead.
1735 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1736 if (mOutputStreams.size() == 0) {
1737 addDummyStreamLocked();
1738 } else {
1739 tryRemoveDummyStreamLocked();
1740 }
1741
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001742 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001743 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001744
1745 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001746 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1747 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1748 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001749 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1750
1751 Vector<camera3_stream_t*> streams;
1752 streams.setCapacity(config.num_streams);
1753
1754 if (mInputStream != NULL) {
1755 camera3_stream_t *inputStream;
1756 inputStream = mInputStream->startConfiguration();
1757 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001758 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001759 return INVALID_OPERATION;
1760 }
1761 streams.add(inputStream);
1762 }
1763
1764 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001765
1766 // Don't configure bidi streams twice, nor add them twice to the list
1767 if (mOutputStreams[i].get() ==
1768 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1769
1770 config.num_streams--;
1771 continue;
1772 }
1773
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001774 camera3_stream_t *outputStream;
1775 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1776 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001777 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001778 return INVALID_OPERATION;
1779 }
1780 streams.add(outputStream);
1781 }
1782
1783 config.streams = streams.editArray();
1784
1785 // Do the HAL configuration; will potentially touch stream
1786 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001787 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001788 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001789 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001790
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001791 if (res == BAD_VALUE) {
1792 // HAL rejected this set of streams as unsupported, clean up config
1793 // attempt and return to unconfigured state
1794 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1795 res = mInputStream->cancelConfiguration();
1796 if (res != OK) {
1797 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1798 mInputStream->getId(), strerror(-res), res);
1799 return res;
1800 }
1801 }
1802
1803 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1804 sp<Camera3OutputStreamInterface> outputStream =
1805 mOutputStreams.editValueAt(i);
1806 if (outputStream->isConfiguring()) {
1807 res = outputStream->cancelConfiguration();
1808 if (res != OK) {
1809 SET_ERR_L(
1810 "Can't cancel configuring output stream %d: %s (%d)",
1811 outputStream->getId(), strerror(-res), res);
1812 return res;
1813 }
1814 }
1815 }
1816
1817 // Return state to that at start of call, so that future configures
1818 // properly clean things up
Ruben Brunk183f0562015-08-12 12:55:02 -07001819 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001820 mNeedConfig = true;
1821
1822 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1823 return BAD_VALUE;
1824 } else if (res != OK) {
1825 // Some other kind of error from configure_streams - this is not
1826 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001827 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1828 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001829 return res;
1830 }
1831
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001832 // Finish all stream configuration immediately.
1833 // TODO: Try to relax this later back to lazy completion, which should be
1834 // faster
1835
Igor Murashkin073f8572013-05-02 14:59:28 -07001836 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001837 res = mInputStream->finishConfiguration(mHal3Device);
1838 if (res != OK) {
1839 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1840 mInputStream->getId(), strerror(-res), res);
1841 return res;
1842 }
1843 }
1844
1845 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001846 sp<Camera3OutputStreamInterface> outputStream =
1847 mOutputStreams.editValueAt(i);
1848 if (outputStream->isConfiguring()) {
1849 res = outputStream->finishConfiguration(mHal3Device);
1850 if (res != OK) {
1851 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1852 outputStream->getId(), strerror(-res), res);
1853 return res;
1854 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001855 }
1856 }
1857
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001858 // Request thread needs to know to avoid using repeat-last-settings protocol
1859 // across configure_streams() calls
1860 mRequestThread->configurationComplete();
1861
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07001862 // Boost priority of request thread for high speed recording to SCHED_FIFO
1863 if (mIsConstrainedHighSpeedConfiguration) {
1864 pid_t requestThreadTid = mRequestThread->getTid();
1865 res = requestPriority(getpid(), requestThreadTid,
1866 kConstrainedHighSpeedThreadPriority, true);
1867 if (res != OK) {
1868 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
1869 strerror(-res), res);
1870 } else {
1871 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
1872 }
1873 } else {
1874 // TODO: Set/restore normal priority for normal use cases
1875 }
1876
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001877 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001878
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001879 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001880
Ruben Brunk183f0562015-08-12 12:55:02 -07001881 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
1882 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001883
1884 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1885
Zhijun He0a210512014-07-24 13:45:15 -07001886 // tear down the deleted streams after configure streams.
1887 mDeletedStreams.clear();
1888
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001889 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001890}
1891
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001892status_t Camera3Device::addDummyStreamLocked() {
1893 ATRACE_CALL();
1894 status_t res;
1895
1896 if (mDummyStreamId != NO_STREAM) {
1897 // Should never be adding a second dummy stream when one is already
1898 // active
1899 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1900 __FUNCTION__, mId);
1901 return INVALID_OPERATION;
1902 }
1903
1904 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1905
1906 sp<Camera3OutputStreamInterface> dummyStream =
1907 new Camera3DummyStream(mNextStreamId);
1908
1909 res = mOutputStreams.add(mNextStreamId, dummyStream);
1910 if (res < 0) {
1911 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1912 return res;
1913 }
1914
1915 mDummyStreamId = mNextStreamId;
1916 mNextStreamId++;
1917
1918 return OK;
1919}
1920
1921status_t Camera3Device::tryRemoveDummyStreamLocked() {
1922 ATRACE_CALL();
1923 status_t res;
1924
1925 if (mDummyStreamId == NO_STREAM) return OK;
1926 if (mOutputStreams.size() == 1) return OK;
1927
1928 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1929
1930 // Ok, have a dummy stream and there's at least one other output stream,
1931 // so remove the dummy
1932
1933 sp<Camera3StreamInterface> deletedStream;
1934 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1935 if (outputStreamIdx == NAME_NOT_FOUND) {
1936 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1937 return INVALID_OPERATION;
1938 }
1939
1940 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1941 mOutputStreams.removeItemsAt(outputStreamIdx);
1942
1943 // Free up the stream endpoint so that it can be used by some other stream
1944 res = deletedStream->disconnect();
1945 if (res != OK) {
1946 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1947 // fall through since we want to still list the stream as deleted.
1948 }
1949 mDeletedStreams.add(deletedStream);
1950 mDummyStreamId = NO_STREAM;
1951
1952 return res;
1953}
1954
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001955void Camera3Device::setErrorState(const char *fmt, ...) {
1956 Mutex::Autolock l(mLock);
1957 va_list args;
1958 va_start(args, fmt);
1959
1960 setErrorStateLockedV(fmt, args);
1961
1962 va_end(args);
1963}
1964
1965void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1966 Mutex::Autolock l(mLock);
1967 setErrorStateLockedV(fmt, args);
1968}
1969
1970void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1971 va_list args;
1972 va_start(args, fmt);
1973
1974 setErrorStateLockedV(fmt, args);
1975
1976 va_end(args);
1977}
1978
1979void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001980 // Print out all error messages to log
1981 String8 errorCause = String8::formatV(fmt, args);
1982 ALOGE("Camera %d: %s", mId, errorCause.string());
1983
1984 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001985 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001986
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001987 mErrorCause = errorCause;
1988
1989 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07001990 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001991
1992 // Notify upstream about a device error
1993 if (mListener != NULL) {
1994 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1995 CaptureResultExtras());
1996 }
1997
1998 // Save stack trace. View by dumping it later.
1999 CameraTraces::saveTrace();
2000 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002001}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002002
2003/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002004 * In-flight request management
2005 */
2006
Jianing Weicb0652e2014-03-12 18:29:36 -07002007status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002008 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2009 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002010 ATRACE_CALL();
2011 Mutex::Autolock l(mInFlightLock);
2012
2013 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002014 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2015 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002016 if (res < 0) return res;
2017
2018 return OK;
2019}
2020
2021/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002022 * Check if all 3A fields are ready, and send off a partial 3A-only result
2023 * to the output frame queue
2024 */
Zhijun He204e3292014-07-14 17:09:23 -07002025bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07002026 uint32_t frameNumber,
2027 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002028
2029 // Check if all 3A states are present
2030 // The full list of fields is
2031 // android.control.afMode
2032 // android.control.awbMode
2033 // android.control.aeState
2034 // android.control.awbState
2035 // android.control.afState
2036 // android.control.afTriggerID
2037 // android.control.aePrecaptureID
2038 // TODO: Add android.control.aeMode
2039
2040 bool gotAllStates = true;
2041
2042 uint8_t afMode;
2043 uint8_t awbMode;
2044 uint8_t aeState;
2045 uint8_t afState;
2046 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002047
2048 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
2049 &afMode, frameNumber);
2050
2051 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
2052 &awbMode, frameNumber);
2053
2054 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
2055 &aeState, frameNumber);
2056
2057 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
2058 &afState, frameNumber);
2059
2060 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
2061 &awbState, frameNumber);
2062
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002063 if (!gotAllStates) return false;
2064
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002065 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002066 "AF state %d, AE state %d, AWB state %d, "
2067 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002068 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002069 afMode, awbMode,
2070 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002071 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002072
2073 // Got all states, so construct a minimal result to send
2074 // In addition to the above fields, this means adding in
2075 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002076 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07002077 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002078
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002079 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002080
2081 Mutex::Autolock l(mOutputLock);
2082
Jianing Weicb0652e2014-03-12 18:29:36 -07002083 CaptureResult captureResult;
2084 captureResult.mResultExtras = resultExtras;
2085 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
2086 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
2087 // but not limited to CameraDeviceBase::getNextResult
2088 CaptureResult& min3AResult =
2089 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002090
Jianing Weicb0652e2014-03-12 18:29:36 -07002091 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
2092 // TODO: This is problematic casting. Need to fix CameraMetadata.
2093 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002094 return false;
2095 }
2096
Jianing Weicb0652e2014-03-12 18:29:36 -07002097 int32_t requestId = resultExtras.requestId;
2098 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08002099 &requestId, frameNumber)) {
2100 return false;
2101 }
2102
Zhijun He204e3292014-07-14 17:09:23 -07002103 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
2104 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
2105 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
2106 &partialResult, frameNumber)) {
2107 return false;
2108 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002109 }
2110
Jianing Weicb0652e2014-03-12 18:29:36 -07002111 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002112 &afMode, frameNumber)) {
2113 return false;
2114 }
2115
Jianing Weicb0652e2014-03-12 18:29:36 -07002116 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002117 &awbMode, frameNumber)) {
2118 return false;
2119 }
2120
Jianing Weicb0652e2014-03-12 18:29:36 -07002121 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002122 &aeState, frameNumber)) {
2123 return false;
2124 }
2125
Jianing Weicb0652e2014-03-12 18:29:36 -07002126 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002127 &afState, frameNumber)) {
2128 return false;
2129 }
2130
Jianing Weicb0652e2014-03-12 18:29:36 -07002131 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002132 &awbState, frameNumber)) {
2133 return false;
2134 }
2135
Jianing Weicb0652e2014-03-12 18:29:36 -07002136 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002137 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002138 return false;
2139 }
2140
Jianing Weicb0652e2014-03-12 18:29:36 -07002141 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002142 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002143 return false;
2144 }
2145
Zhijun He204e3292014-07-14 17:09:23 -07002146 // We only send the aggregated partial when all 3A related metadata are available
2147 // For both API1 and API2.
2148 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002149 mResultSignal.signal();
2150
2151 return true;
2152}
2153
2154template<typename T>
2155bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002156 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002157 (void) frameNumber;
2158
2159 camera_metadata_ro_entry_t entry;
2160
2161 entry = result.find(tag);
2162 if (entry.count == 0) {
2163 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
2164 mId, frameNumber, get_camera_metadata_tag_name(tag));
2165 return false;
2166 }
2167
2168 if (sizeof(T) == sizeof(uint8_t)) {
2169 *value = entry.data.u8[0];
2170 } else if (sizeof(T) == sizeof(int32_t)) {
2171 *value = entry.data.i32[0];
2172 } else {
2173 ALOGE("%s: Unexpected type", __FUNCTION__);
2174 return false;
2175 }
2176 return true;
2177}
2178
2179template<typename T>
2180bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002181 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002182 if (result.update(tag, value, 1) != NO_ERROR) {
2183 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2184 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2185 frameNumber, get_camera_metadata_tag_name(tag));
2186 return false;
2187 }
2188 return true;
2189}
2190
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002191void Camera3Device::returnOutputBuffers(
2192 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2193 nsecs_t timestamp) {
2194 for (size_t i = 0; i < numBuffers; i++)
2195 {
2196 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2197 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2198 // Note: stream may be deallocated at this point, if this buffer was
2199 // the last reference to it.
2200 if (res != OK) {
2201 ALOGE("Can't return buffer to its stream: %s (%d)",
2202 strerror(-res), res);
2203 }
2204 }
2205}
2206
2207
2208void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2209
2210 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2211 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2212
2213 nsecs_t sensorTimestamp = request.sensorTimestamp;
2214 nsecs_t shutterTimestamp = request.shutterTimestamp;
2215
2216 // Check if it's okay to remove the request from InFlightMap:
2217 // In the case of a successful request:
2218 // all input and output buffers, all result metadata, shutter callback
2219 // arrived.
2220 // In the case of a unsuccessful request:
2221 // all input and output buffers arrived.
2222 if (request.numBuffersLeft == 0 &&
2223 (request.requestStatus != OK ||
2224 (request.haveResultMetadata && shutterTimestamp != 0))) {
2225 ATRACE_ASYNC_END("frame capture", frameNumber);
2226
2227 // Sanity check - if sensor timestamp matches shutter timestamp
2228 if (request.requestStatus == OK &&
2229 sensorTimestamp != shutterTimestamp) {
2230 SET_ERR("sensor timestamp (%" PRId64
2231 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2232 sensorTimestamp, frameNumber, shutterTimestamp);
2233 }
2234
2235 // for an unsuccessful request, it may have pending output buffers to
2236 // return.
2237 assert(request.requestStatus != OK ||
2238 request.pendingOutputBuffers.size() == 0);
2239 returnOutputBuffers(request.pendingOutputBuffers.array(),
2240 request.pendingOutputBuffers.size(), 0);
2241
2242 mInFlightMap.removeItemsAt(idx, 1);
2243
2244 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2245 }
2246
2247 // Sanity check - if we have too many in-flight frames, something has
2248 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002249 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002250 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002251 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2252 kInFlightWarnLimitHighSpeed) {
2253 CLOGE("In-flight list too large for high speed configuration: %zu",
2254 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002255 }
2256}
2257
2258
2259void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2260 CaptureResultExtras &resultExtras,
2261 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002262 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002263 bool reprocess,
2264 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002265 if (pendingMetadata.isEmpty())
2266 return;
2267
2268 Mutex::Autolock l(mOutputLock);
2269
2270 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002271 if (reprocess) {
2272 if (frameNumber < mNextReprocessResultFrameNumber) {
2273 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002274 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002275 frameNumber, mNextReprocessResultFrameNumber);
2276 return;
2277 }
2278 mNextReprocessResultFrameNumber = frameNumber + 1;
2279 } else {
2280 if (frameNumber < mNextResultFrameNumber) {
2281 SET_ERR("Out-of-order capture result metadata submitted! "
2282 "(got frame number %d, expecting %d)",
2283 frameNumber, mNextResultFrameNumber);
2284 return;
2285 }
2286 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002287 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002288
2289 CaptureResult captureResult;
2290 captureResult.mResultExtras = resultExtras;
2291 captureResult.mMetadata = pendingMetadata;
2292
2293 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2294 (int32_t*)&frameNumber, 1) != OK) {
2295 SET_ERR("Failed to set frame# in metadata (%d)",
2296 frameNumber);
2297 return;
2298 } else {
2299 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2300 __FUNCTION__, mId, frameNumber);
2301 }
2302
2303 // Append any previous partials to form a complete result
2304 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2305 captureResult.mMetadata.append(collectedPartialResult);
2306 }
2307
2308 captureResult.mMetadata.sort();
2309
2310 // Check that there's a timestamp in the result metadata
2311 camera_metadata_entry entry =
2312 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2313 if (entry.count == 0) {
2314 SET_ERR("No timestamp provided by HAL for frame %d!",
2315 frameNumber);
2316 return;
2317 }
2318
Chien-Yu Chend196d612015-06-22 19:49:01 -07002319 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2320
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002321 // Valid result, insert into queue
2322 List<CaptureResult>::iterator queuedResult =
2323 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2324 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2325 ", burstId = %" PRId32, __FUNCTION__,
2326 queuedResult->mResultExtras.requestId,
2327 queuedResult->mResultExtras.frameNumber,
2328 queuedResult->mResultExtras.burstId);
2329
2330 mResultSignal.signal();
2331}
2332
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002333/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002334 * Camera HAL device callback methods
2335 */
2336
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002337void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002338 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002339
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002340 status_t res;
2341
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002342 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002343 if (result->result == NULL && result->num_output_buffers == 0 &&
2344 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002345 SET_ERR("No result data provided by HAL for frame %d",
2346 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002347 return;
2348 }
Zhijun He204e3292014-07-14 17:09:23 -07002349
2350 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2351 // partial_result to 1 when metadata is included in this result.
2352 if (!mUsePartialResult &&
2353 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2354 result->result != NULL &&
2355 result->partial_result != 1) {
2356 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2357 " if partial result is not supported",
2358 frameNumber, result->partial_result);
2359 return;
2360 }
2361
2362 bool isPartialResult = false;
2363 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002364 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002365 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002366
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002367 // Get shutter timestamp and resultExtras from list of in-flight requests,
2368 // where it was added by the shutter notification for this frame. If the
2369 // shutter timestamp isn't received yet, append the output buffers to the
2370 // in-flight request and they will be returned when the shutter timestamp
2371 // arrives. Update the in-flight status and remove the in-flight entry if
2372 // all result data and shutter timestamp have been received.
2373 nsecs_t shutterTimestamp = 0;
2374
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002375 {
2376 Mutex::Autolock l(mInFlightLock);
2377 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2378 if (idx == NAME_NOT_FOUND) {
2379 SET_ERR("Unknown frame number for capture result: %d",
2380 frameNumber);
2381 return;
2382 }
2383 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002384 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2385 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2386 ", partialResultCount = %d",
2387 __FUNCTION__, request.resultExtras.requestId,
2388 request.resultExtras.frameNumber, request.resultExtras.burstId,
2389 result->partial_result);
2390 // Always update the partial count to the latest one if it's not 0
2391 // (buffers only). When framework aggregates adjacent partial results
2392 // into one, the latest partial count will be used.
2393 if (result->partial_result != 0)
2394 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002395
2396 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002397 if (mUsePartialResult && result->result != NULL) {
2398 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2399 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2400 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2401 " the range of [1, %d] when metadata is included in the result",
2402 frameNumber, result->partial_result, mNumPartialResults);
2403 return;
2404 }
2405 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002406 if (isPartialResult) {
2407 request.partialResult.collectedResult.append(result->result);
2408 }
Zhijun He204e3292014-07-14 17:09:23 -07002409 } else {
2410 camera_metadata_ro_entry_t partialResultEntry;
2411 res = find_camera_metadata_ro_entry(result->result,
2412 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2413 if (res != NAME_NOT_FOUND &&
2414 partialResultEntry.count > 0 &&
2415 partialResultEntry.data.u8[0] ==
2416 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2417 // A partial result. Flag this as such, and collect this
2418 // set of metadata into the in-flight entry.
2419 isPartialResult = true;
2420 request.partialResult.collectedResult.append(
2421 result->result);
2422 request.partialResult.collectedResult.erase(
2423 ANDROID_QUIRKS_PARTIAL_RESULT);
2424 }
2425 }
2426
2427 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002428 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002429 if (!request.partialResult.haveSent3A) {
2430 request.partialResult.haveSent3A =
2431 processPartial3AResult(frameNumber,
2432 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002433 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002434 }
2435 }
2436 }
2437
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002438 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002439 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002440
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002441 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002442 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002443 if (request.haveResultMetadata) {
2444 SET_ERR("Called multiple times with metadata for frame %d",
2445 frameNumber);
2446 return;
2447 }
Zhijun He204e3292014-07-14 17:09:23 -07002448 if (mUsePartialResult &&
2449 !request.partialResult.collectedResult.isEmpty()) {
2450 collectedPartialResult.acquire(
2451 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002452 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002453 request.haveResultMetadata = true;
2454 }
2455
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002456 uint32_t numBuffersReturned = result->num_output_buffers;
2457 if (result->input_buffer != NULL) {
2458 if (hasInputBufferInRequest) {
2459 numBuffersReturned += 1;
2460 } else {
2461 ALOGW("%s: Input buffer should be NULL if there is no input"
2462 " buffer sent in the request",
2463 __FUNCTION__);
2464 }
2465 }
2466 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002467 if (request.numBuffersLeft < 0) {
2468 SET_ERR("Too many buffers returned for frame %d",
2469 frameNumber);
2470 return;
2471 }
2472
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002473 camera_metadata_ro_entry_t entry;
2474 res = find_camera_metadata_ro_entry(result->result,
2475 ANDROID_SENSOR_TIMESTAMP, &entry);
2476 if (res == OK && entry.count == 1) {
2477 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002478 }
2479
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002480 // If shutter event isn't received yet, append the output buffers to
2481 // the in-flight request. Otherwise, return the output buffers to
2482 // streams.
2483 if (shutterTimestamp == 0) {
2484 request.pendingOutputBuffers.appendArray(result->output_buffers,
2485 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002486 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002487 returnOutputBuffers(result->output_buffers,
2488 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002489 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002490
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002491 if (result->result != NULL && !isPartialResult) {
2492 if (shutterTimestamp == 0) {
2493 request.pendingMetadata = result->result;
2494 request.partialResult.collectedResult = collectedPartialResult;
2495 } else {
2496 CameraMetadata metadata;
2497 metadata = result->result;
2498 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002499 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2500 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002501 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002502 }
2503
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002504 removeInFlightRequestIfReadyLocked(idx);
2505 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002506
Zhijun Hef0d962a2014-06-30 10:24:11 -07002507 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002508 if (hasInputBufferInRequest) {
2509 Camera3Stream *stream =
2510 Camera3Stream::cast(result->input_buffer->stream);
2511 res = stream->returnInputBuffer(*(result->input_buffer));
2512 // Note: stream may be deallocated at this point, if this buffer was the
2513 // last reference to it.
2514 if (res != OK) {
2515 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2516 " its stream:%s (%d)", __FUNCTION__,
2517 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002518 }
2519 } else {
2520 ALOGW("%s: Input buffer should be NULL if there is no input"
2521 " buffer sent in the request, skipping input buffer return.",
2522 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002523 }
2524 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002525}
2526
2527void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002528 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002529 NotificationListener *listener;
2530 {
2531 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002532 listener = mListener;
2533 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002534
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002535 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002536 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002537 return;
2538 }
2539
2540 switch (msg->type) {
2541 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002542 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002543 break;
2544 }
2545 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002546 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002547 break;
2548 }
2549 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002550 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002551 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002552 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002553}
2554
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002555void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2556 NotificationListener *listener) {
2557
2558 // Map camera HAL error codes to ICameraDeviceCallback error codes
2559 // Index into this with the HAL error code
2560 static const ICameraDeviceCallbacks::CameraErrorCode
2561 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2562 // 0 = Unused error code
2563 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2564 // 1 = CAMERA3_MSG_ERROR_DEVICE
2565 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2566 // 2 = CAMERA3_MSG_ERROR_REQUEST
2567 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2568 // 3 = CAMERA3_MSG_ERROR_RESULT
2569 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2570 // 4 = CAMERA3_MSG_ERROR_BUFFER
2571 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2572 };
2573
2574 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2575 ((msg.error_code >= 0) &&
2576 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2577 halErrorMap[msg.error_code] :
2578 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2579
2580 int streamId = 0;
2581 if (msg.error_stream != NULL) {
2582 Camera3Stream *stream =
2583 Camera3Stream::cast(msg.error_stream);
2584 streamId = stream->getId();
2585 }
2586 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2587 mId, __FUNCTION__, msg.frame_number,
2588 streamId, msg.error_code);
2589
2590 CaptureResultExtras resultExtras;
2591 switch (errorCode) {
2592 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2593 // SET_ERR calls notifyError
2594 SET_ERR("Camera HAL reported serious device error");
2595 break;
2596 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2597 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2598 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2599 {
2600 Mutex::Autolock l(mInFlightLock);
2601 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2602 if (idx >= 0) {
2603 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2604 r.requestStatus = msg.error_code;
2605 resultExtras = r.resultExtras;
2606 } else {
2607 resultExtras.frameNumber = msg.frame_number;
2608 ALOGE("Camera %d: %s: cannot find in-flight request on "
2609 "frame %" PRId64 " error", mId, __FUNCTION__,
2610 resultExtras.frameNumber);
2611 }
2612 }
2613 if (listener != NULL) {
2614 listener->notifyError(errorCode, resultExtras);
2615 } else {
2616 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2617 }
2618 break;
2619 default:
2620 // SET_ERR calls notifyError
2621 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2622 break;
2623 }
2624}
2625
2626void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2627 NotificationListener *listener) {
2628 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002629
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002630 // Set timestamp for the request in the in-flight tracking
2631 // and get the request ID to send upstream
2632 {
2633 Mutex::Autolock l(mInFlightLock);
2634 idx = mInFlightMap.indexOfKey(msg.frame_number);
2635 if (idx >= 0) {
2636 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002637
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002638 // Verify ordering of shutter notifications
2639 {
2640 Mutex::Autolock l(mOutputLock);
2641 // TODO: need to track errors for tighter bounds on expected frame number.
2642 if (r.hasInputBuffer) {
2643 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2644 SET_ERR("Shutter notification out-of-order. Expected "
2645 "notification for frame %d, got frame %d",
2646 mNextReprocessShutterFrameNumber, msg.frame_number);
2647 return;
2648 }
2649 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2650 } else {
2651 if (msg.frame_number < mNextShutterFrameNumber) {
2652 SET_ERR("Shutter notification out-of-order. Expected "
2653 "notification for frame %d, got frame %d",
2654 mNextShutterFrameNumber, msg.frame_number);
2655 return;
2656 }
2657 mNextShutterFrameNumber = msg.frame_number + 1;
2658 }
2659 }
2660
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002661 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2662 mId, __FUNCTION__,
2663 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2664 // Call listener, if any
2665 if (listener != NULL) {
2666 listener->notifyShutter(r.resultExtras, msg.timestamp);
2667 }
2668
2669 r.shutterTimestamp = msg.timestamp;
2670
2671 // send pending result and buffers
2672 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002673 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002674 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002675 returnOutputBuffers(r.pendingOutputBuffers.array(),
2676 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2677 r.pendingOutputBuffers.clear();
2678
2679 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002680 }
2681 }
2682 if (idx < 0) {
2683 SET_ERR("Shutter notification for non-existent frame number %d",
2684 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002685 }
2686}
2687
2688
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002689CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002690 ALOGV("%s", __FUNCTION__);
2691
Igor Murashkin1e479c02013-09-06 16:55:14 -07002692 CameraMetadata retVal;
2693
2694 if (mRequestThread != NULL) {
2695 retVal = mRequestThread->getLatestRequest();
2696 }
2697
Igor Murashkin1e479c02013-09-06 16:55:14 -07002698 return retVal;
2699}
2700
Jianing Weicb0652e2014-03-12 18:29:36 -07002701
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002702/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002703 * RequestThread inner class methods
2704 */
2705
2706Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002707 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002708 camera3_device_t *hal3Device,
2709 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002710 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002711 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002712 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002713 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002714 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002715 mReconfigured(false),
2716 mDoPause(false),
2717 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002718 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002719 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002720 mCurrentAfTriggerId(0),
2721 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002722 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2723 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002724 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002725}
2726
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002727void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002728 NotificationListener *listener) {
2729 Mutex::Autolock l(mRequestLock);
2730 mListener = listener;
2731}
2732
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002733void Camera3Device::RequestThread::configurationComplete() {
2734 Mutex::Autolock l(mRequestLock);
2735 mReconfigured = true;
2736}
2737
Jianing Wei90e59c92014-03-12 18:29:36 -07002738status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002739 List<sp<CaptureRequest> > &requests,
2740 /*out*/
2741 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002742 Mutex::Autolock l(mRequestLock);
2743 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2744 ++it) {
2745 mRequestQueue.push_back(*it);
2746 }
2747
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002748 if (lastFrameNumber != NULL) {
2749 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2750 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2751 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2752 *lastFrameNumber);
2753 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002754
Jianing Wei90e59c92014-03-12 18:29:36 -07002755 unpauseForNewRequests();
2756
2757 return OK;
2758}
2759
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002760
2761status_t Camera3Device::RequestThread::queueTrigger(
2762 RequestTrigger trigger[],
2763 size_t count) {
2764
2765 Mutex::Autolock l(mTriggerMutex);
2766 status_t ret;
2767
2768 for (size_t i = 0; i < count; ++i) {
2769 ret = queueTriggerLocked(trigger[i]);
2770
2771 if (ret != OK) {
2772 return ret;
2773 }
2774 }
2775
2776 return OK;
2777}
2778
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002779int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2780 sp<Camera3Device> d = device.promote();
2781 if (d != NULL) return d->mId;
2782 return 0;
2783}
2784
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002785status_t Camera3Device::RequestThread::queueTriggerLocked(
2786 RequestTrigger trigger) {
2787
2788 uint32_t tag = trigger.metadataTag;
2789 ssize_t index = mTriggerMap.indexOfKey(tag);
2790
2791 switch (trigger.getTagType()) {
2792 case TYPE_BYTE:
2793 // fall-through
2794 case TYPE_INT32:
2795 break;
2796 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002797 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2798 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002799 return INVALID_OPERATION;
2800 }
2801
2802 /**
2803 * Collect only the latest trigger, since we only have 1 field
2804 * in the request settings per trigger tag, and can't send more than 1
2805 * trigger per request.
2806 */
2807 if (index != NAME_NOT_FOUND) {
2808 mTriggerMap.editValueAt(index) = trigger;
2809 } else {
2810 mTriggerMap.add(tag, trigger);
2811 }
2812
2813 return OK;
2814}
2815
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002816status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002817 const RequestList &requests,
2818 /*out*/
2819 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002820 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002821 if (lastFrameNumber != NULL) {
2822 *lastFrameNumber = mRepeatingLastFrameNumber;
2823 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002824 mRepeatingRequests.clear();
2825 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2826 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002827
2828 unpauseForNewRequests();
2829
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002830 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002831 return OK;
2832}
2833
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002834bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2835 if (mRepeatingRequests.empty()) {
2836 return false;
2837 }
2838 int32_t requestId = requestIn->mResultExtras.requestId;
2839 const RequestList &repeatRequests = mRepeatingRequests;
2840 // All repeating requests are guaranteed to have same id so only check first quest
2841 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2842 return (firstRequest->mResultExtras.requestId == requestId);
2843}
2844
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002845status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002846 Mutex::Autolock l(mRequestLock);
2847 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002848 if (lastFrameNumber != NULL) {
2849 *lastFrameNumber = mRepeatingLastFrameNumber;
2850 }
2851 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002852 return OK;
2853}
2854
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002855status_t Camera3Device::RequestThread::clear(
2856 NotificationListener *listener,
2857 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002858 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002859 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002860
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002861 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002862
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002863 // Send errors for all requests pending in the request queue, including
2864 // pending repeating requests
2865 if (listener != NULL) {
2866 for (RequestList::iterator it = mRequestQueue.begin();
2867 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002868 // Abort the input buffers for reprocess requests.
2869 if ((*it)->mInputStream != NULL) {
2870 camera3_stream_buffer_t inputBuffer;
2871 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2872 if (res != OK) {
2873 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2874 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2875 } else {
2876 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2877 if (res != OK) {
2878 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2879 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2880 }
2881 }
2882 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002883 // Set the frame number this request would have had, if it
2884 // had been submitted; this frame number will not be reused.
2885 // The requestId and burstId fields were set when the request was
2886 // submitted originally (in convertMetadataListToRequestListLocked)
2887 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2888 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2889 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002890 }
2891 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002892 mRequestQueue.clear();
2893 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002894 if (lastFrameNumber != NULL) {
2895 *lastFrameNumber = mRepeatingLastFrameNumber;
2896 }
2897 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002898 return OK;
2899}
2900
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002901status_t Camera3Device::RequestThread::flush() {
2902 ATRACE_CALL();
2903 Mutex::Autolock l(mFlushLock);
2904
2905 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
2906 return mHal3Device->ops->flush(mHal3Device);
2907 }
2908
2909 return -ENOTSUP;
2910}
2911
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002912void Camera3Device::RequestThread::setPaused(bool paused) {
2913 Mutex::Autolock l(mPauseLock);
2914 mDoPause = paused;
2915 mDoPauseSignal.signal();
2916}
2917
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002918status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2919 int32_t requestId, nsecs_t timeout) {
2920 Mutex::Autolock l(mLatestRequestMutex);
2921 status_t res;
2922 while (mLatestRequestId != requestId) {
2923 nsecs_t startTime = systemTime();
2924
2925 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2926 if (res != OK) return res;
2927
2928 timeout -= (systemTime() - startTime);
2929 }
2930
2931 return OK;
2932}
2933
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002934void Camera3Device::RequestThread::requestExit() {
2935 // Call parent to set up shutdown
2936 Thread::requestExit();
2937 // The exit from any possible waits
2938 mDoPauseSignal.signal();
2939 mRequestSignal.signal();
2940}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002941
Chien-Yu Chend196d612015-06-22 19:49:01 -07002942
2943/**
2944 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2945 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2946 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2947 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2948 * request.
2949 */
2950void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2951 request->mAeTriggerCancelOverride.applyAeLock = false;
2952 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2953
2954 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2955 return;
2956 }
2957
2958 camera_metadata_entry_t aePrecaptureTrigger =
2959 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2960 if (aePrecaptureTrigger.count > 0 &&
2961 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2962 // Always override CANCEL to IDLE
2963 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2964 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2965 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2966 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2967 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2968
2969 if (mAeLockAvailable == true) {
2970 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2971 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2972 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2973 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2974 request->mAeTriggerCancelOverride.applyAeLock = true;
2975 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2976 }
2977 }
2978 }
2979}
2980
2981/**
2982 * Override result metadata for cancelling AE precapture trigger applied in
2983 * handleAePrecaptureCancelRequest().
2984 */
2985void Camera3Device::overrideResultForPrecaptureCancel(
2986 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2987 if (aeTriggerCancelOverride.applyAeLock) {
2988 // Only devices <= v3.2 should have this override
2989 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2990 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2991 }
2992
2993 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2994 // Only devices <= v3.2 should have this override
2995 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2996 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2997 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2998 }
2999}
3000
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003001bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003002 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003003 status_t res;
3004
3005 // Handle paused state.
3006 if (waitIfPaused()) {
3007 return true;
3008 }
3009
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003010 // Wait for the next batch of requests.
3011 waitForNextRequestBatch();
3012 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003013 return true;
3014 }
3015
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003016 // Get the latest request ID, if any
3017 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003018 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003019 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003020 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003021 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003022 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003023 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3024 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003025 }
3026
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003027 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003028 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003029 if (res == TIMED_OUT) {
3030 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003031 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003032 return true;
3033 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003034 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003035 return false;
3036 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003037
Zhijun Hecc27e112013-10-03 16:12:43 -07003038 // Inform waitUntilRequestProcessed thread of a new request ID
3039 {
3040 Mutex::Autolock al(mLatestRequestMutex);
3041
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003042 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003043 mLatestRequestSignal.signal();
3044 }
3045
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003046 // Submit a batch of requests to HAL.
3047 // Use flush lock only when submitting multilple requests in a batch.
3048 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3049 // which may take a long time to finish so synchronizing flush() and
3050 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3051 // For now, only synchronize for high speed recording and we should figure something out for
3052 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003053 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003054
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003055 if (useFlushLock) {
3056 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003057 }
3058
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003059 ALOGVV("%s: %d: submitting %d requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003060 mNextRequests.size());
3061 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003062 // Submit request and block until ready for next one
3063 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3064 ATRACE_BEGIN("camera3->process_capture_request");
3065 res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
3066 ATRACE_END();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003067
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003068 if (res != OK) {
3069 // Should only get a failure here for malformed requests or device-level
3070 // errors, so consider all errors fatal. Bad metadata failures should
3071 // come through notify.
3072 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3073 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3074 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003075 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003076 if (useFlushLock) {
3077 mFlushLock.unlock();
3078 }
3079 return false;
3080 }
3081
3082 // Mark that the request has be submitted successfully.
3083 nextRequest.submitted = true;
3084
3085 // Update the latest request sent to HAL
3086 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3087 Mutex::Autolock al(mLatestRequestMutex);
3088
3089 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3090 mLatestRequest.acquire(cloned);
3091 }
3092
3093 if (nextRequest.halRequest.settings != NULL) {
3094 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3095 }
3096
3097 // Remove any previously queued triggers (after unlock)
3098 res = removeTriggers(mPrevRequest);
3099 if (res != OK) {
3100 SET_ERR("RequestThread: Unable to remove triggers "
3101 "(capture request %d, HAL device: %s (%d)",
3102 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003103 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003104 if (useFlushLock) {
3105 mFlushLock.unlock();
3106 }
3107 return false;
3108 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003109 }
3110
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003111 if (useFlushLock) {
3112 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003113 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003114
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003115 // Unset as current request
3116 {
3117 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003118 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003119 }
3120
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003121 return true;
3122}
3123
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003124status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003125 ATRACE_CALL();
3126
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003127 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003128 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3129 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3130 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3131
3132 // Prepare a request to HAL
3133 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3134
3135 // Insert any queued triggers (before metadata is locked)
3136 status_t res = insertTriggers(captureRequest);
3137
3138 if (res < 0) {
3139 SET_ERR("RequestThread: Unable to insert triggers "
3140 "(capture request %d, HAL device: %s (%d)",
3141 halRequest->frame_number, strerror(-res), res);
3142 return INVALID_OPERATION;
3143 }
3144 int triggerCount = res;
3145 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3146 mPrevTriggers = triggerCount;
3147
3148 // If the request is the same as last, or we had triggers last time
3149 if (mPrevRequest != captureRequest || triggersMixedIn) {
3150 /**
3151 * HAL workaround:
3152 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3153 */
3154 res = addDummyTriggerIds(captureRequest);
3155 if (res != OK) {
3156 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3157 "(capture request %d, HAL device: %s (%d)",
3158 halRequest->frame_number, strerror(-res), res);
3159 return INVALID_OPERATION;
3160 }
3161
3162 /**
3163 * The request should be presorted so accesses in HAL
3164 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3165 */
3166 captureRequest->mSettings.sort();
3167 halRequest->settings = captureRequest->mSettings.getAndLock();
3168 mPrevRequest = captureRequest;
3169 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3170
3171 IF_ALOGV() {
3172 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3173 find_camera_metadata_ro_entry(
3174 halRequest->settings,
3175 ANDROID_CONTROL_AF_TRIGGER,
3176 &e
3177 );
3178 if (e.count > 0) {
3179 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3180 __FUNCTION__,
3181 halRequest->frame_number,
3182 e.data.u8[0]);
3183 }
3184 }
3185 } else {
3186 // leave request.settings NULL to indicate 'reuse latest given'
3187 ALOGVV("%s: Request settings are REUSED",
3188 __FUNCTION__);
3189 }
3190
3191 uint32_t totalNumBuffers = 0;
3192
3193 // Fill in buffers
3194 if (captureRequest->mInputStream != NULL) {
3195 halRequest->input_buffer = &captureRequest->mInputBuffer;
3196 totalNumBuffers += 1;
3197 } else {
3198 halRequest->input_buffer = NULL;
3199 }
3200
3201 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3202 captureRequest->mOutputStreams.size());
3203 halRequest->output_buffers = outputBuffers->array();
3204 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
3205 res = captureRequest->mOutputStreams.editItemAt(i)->
3206 getBuffer(&outputBuffers->editItemAt(i));
3207 if (res != OK) {
3208 // Can't get output buffer from gralloc queue - this could be due to
3209 // abandoned queue or other consumer misbehavior, so not a fatal
3210 // error
3211 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3212 " %s (%d)", strerror(-res), res);
3213
3214 return TIMED_OUT;
3215 }
3216 halRequest->num_output_buffers++;
3217 }
3218 totalNumBuffers += halRequest->num_output_buffers;
3219
3220 // Log request in the in-flight queue
3221 sp<Camera3Device> parent = mParent.promote();
3222 if (parent == NULL) {
3223 // Should not happen, and nowhere to send errors to, so just log it
3224 CLOGE("RequestThread: Parent is gone");
3225 return INVALID_OPERATION;
3226 }
3227 res = parent->registerInFlight(halRequest->frame_number,
3228 totalNumBuffers, captureRequest->mResultExtras,
3229 /*hasInput*/halRequest->input_buffer != NULL,
3230 captureRequest->mAeTriggerCancelOverride);
3231 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3232 ", burstId = %" PRId32 ".",
3233 __FUNCTION__,
3234 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3235 captureRequest->mResultExtras.burstId);
3236 if (res != OK) {
3237 SET_ERR("RequestThread: Unable to register new in-flight request:"
3238 " %s (%d)", strerror(-res), res);
3239 return INVALID_OPERATION;
3240 }
3241 }
3242
3243 return OK;
3244}
3245
Igor Murashkin1e479c02013-09-06 16:55:14 -07003246CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3247 Mutex::Autolock al(mLatestRequestMutex);
3248
3249 ALOGV("RequestThread::%s", __FUNCTION__);
3250
3251 return mLatestRequest;
3252}
3253
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003254bool Camera3Device::RequestThread::isStreamPending(
3255 sp<Camera3StreamInterface>& stream) {
3256 Mutex::Autolock l(mRequestLock);
3257
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003258 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003259 if (!nextRequest.submitted) {
3260 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3261 if (stream == s) return true;
3262 }
3263 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003264 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003265 }
3266
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003267 for (const auto& request : mRequestQueue) {
3268 for (const auto& s : request->mOutputStreams) {
3269 if (stream == s) return true;
3270 }
3271 if (stream == request->mInputStream) return true;
3272 }
3273
3274 for (const auto& request : mRepeatingRequests) {
3275 for (const auto& s : request->mOutputStreams) {
3276 if (stream == s) return true;
3277 }
3278 if (stream == request->mInputStream) return true;
3279 }
3280
3281 return false;
3282}
Jianing Weicb0652e2014-03-12 18:29:36 -07003283
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003284void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
3285 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003286 return;
3287 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003288
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003289 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003290 // Skip the ones that have been submitted successfully.
3291 if (nextRequest.submitted) {
3292 continue;
3293 }
3294
3295 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3296 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3297 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3298
3299 if (halRequest->settings != NULL) {
3300 captureRequest->mSettings.unlock(halRequest->settings);
3301 }
3302
3303 if (captureRequest->mInputStream != NULL) {
3304 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3305 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
3306 }
3307
3308 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
3309 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3310 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
3311 }
3312
3313 if (sendRequestError) {
3314 Mutex::Autolock l(mRequestLock);
3315 if (mListener != NULL) {
3316 mListener->notifyError(
3317 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3318 captureRequest->mResultExtras);
3319 }
3320 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003321 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003322
3323 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003324 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003325}
3326
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003327void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003328 // Optimized a bit for the simple steady-state case (single repeating
3329 // request), to avoid putting that request in the queue temporarily.
3330 Mutex::Autolock l(mRequestLock);
3331
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003332 assert(mNextRequests.empty());
3333
3334 NextRequest nextRequest;
3335 nextRequest.captureRequest = waitForNextRequestLocked();
3336 if (nextRequest.captureRequest == nullptr) {
3337 return;
3338 }
3339
3340 nextRequest.halRequest = camera3_capture_request_t();
3341 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003342 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003343
3344 // Wait for additional requests
3345 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
3346
3347 for (size_t i = 1; i < batchSize; i++) {
3348 NextRequest additionalRequest;
3349 additionalRequest.captureRequest = waitForNextRequestLocked();
3350 if (additionalRequest.captureRequest == nullptr) {
3351 break;
3352 }
3353
3354 additionalRequest.halRequest = camera3_capture_request_t();
3355 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003356 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003357 }
3358
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003359 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003360 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003361 mNextRequests.size(), batchSize);
3362 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003363 }
3364
3365 return;
3366}
3367
3368sp<Camera3Device::CaptureRequest>
3369 Camera3Device::RequestThread::waitForNextRequestLocked() {
3370 status_t res;
3371 sp<CaptureRequest> nextRequest;
3372
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003373 while (mRequestQueue.empty()) {
3374 if (!mRepeatingRequests.empty()) {
3375 // Always atomically enqueue all requests in a repeating request
3376 // list. Guarantees a complete in-sequence set of captures to
3377 // application.
3378 const RequestList &requests = mRepeatingRequests;
3379 RequestList::const_iterator firstRequest =
3380 requests.begin();
3381 nextRequest = *firstRequest;
3382 mRequestQueue.insert(mRequestQueue.end(),
3383 ++firstRequest,
3384 requests.end());
3385 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003386
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003387 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003388
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003389 break;
3390 }
3391
3392 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3393
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003394 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3395 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003396 Mutex::Autolock pl(mPauseLock);
3397 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003398 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003399 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003400 // Let the tracker know
3401 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3402 if (statusTracker != 0) {
3403 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3404 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003405 }
3406 // Stop waiting for now and let thread management happen
3407 return NULL;
3408 }
3409 }
3410
3411 if (nextRequest == NULL) {
3412 // Don't have a repeating request already in hand, so queue
3413 // must have an entry now.
3414 RequestList::iterator firstRequest =
3415 mRequestQueue.begin();
3416 nextRequest = *firstRequest;
3417 mRequestQueue.erase(firstRequest);
3418 }
3419
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003420 // In case we've been unpaused by setPaused clearing mDoPause, need to
3421 // update internal pause state (capture/setRepeatingRequest unpause
3422 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003423 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003424 if (mPaused) {
3425 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3426 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3427 if (statusTracker != 0) {
3428 statusTracker->markComponentActive(mStatusId);
3429 }
3430 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003431 mPaused = false;
3432
3433 // Check if we've reconfigured since last time, and reset the preview
3434 // request if so. Can't use 'NULL request == repeat' across configure calls.
3435 if (mReconfigured) {
3436 mPrevRequest.clear();
3437 mReconfigured = false;
3438 }
3439
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003440 if (nextRequest != NULL) {
3441 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003442 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3443 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003444
3445 // Since RequestThread::clear() removes buffers from the input stream,
3446 // get the right buffer here before unlocking mRequestLock
3447 if (nextRequest->mInputStream != NULL) {
3448 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3449 if (res != OK) {
3450 // Can't get input buffer from gralloc queue - this could be due to
3451 // disconnected queue or other producer misbehavior, so not a fatal
3452 // error
3453 ALOGE("%s: Can't get input buffer, skipping request:"
3454 " %s (%d)", __FUNCTION__, strerror(-res), res);
3455 if (mListener != NULL) {
3456 mListener->notifyError(
3457 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3458 nextRequest->mResultExtras);
3459 }
3460 return NULL;
3461 }
3462 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003463 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003464
3465 handleAePrecaptureCancelRequest(nextRequest);
3466
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003467 return nextRequest;
3468}
3469
3470bool Camera3Device::RequestThread::waitIfPaused() {
3471 status_t res;
3472 Mutex::Autolock l(mPauseLock);
3473 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003474 if (mPaused == false) {
3475 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003476 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3477 // Let the tracker know
3478 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3479 if (statusTracker != 0) {
3480 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3481 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003482 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003483
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003484 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003485 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003486 return true;
3487 }
3488 }
3489 // We don't set mPaused to false here, because waitForNextRequest needs
3490 // to further manage the paused state in case of starvation.
3491 return false;
3492}
3493
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003494void Camera3Device::RequestThread::unpauseForNewRequests() {
3495 // With work to do, mark thread as unpaused.
3496 // If paused by request (setPaused), don't resume, to avoid
3497 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003498 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003499 Mutex::Autolock p(mPauseLock);
3500 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003501 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3502 if (mPaused) {
3503 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3504 if (statusTracker != 0) {
3505 statusTracker->markComponentActive(mStatusId);
3506 }
3507 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003508 mPaused = false;
3509 }
3510}
3511
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003512void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3513 sp<Camera3Device> parent = mParent.promote();
3514 if (parent != NULL) {
3515 va_list args;
3516 va_start(args, fmt);
3517
3518 parent->setErrorStateV(fmt, args);
3519
3520 va_end(args);
3521 }
3522}
3523
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003524status_t Camera3Device::RequestThread::insertTriggers(
3525 const sp<CaptureRequest> &request) {
3526
3527 Mutex::Autolock al(mTriggerMutex);
3528
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003529 sp<Camera3Device> parent = mParent.promote();
3530 if (parent == NULL) {
3531 CLOGE("RequestThread: Parent is gone");
3532 return DEAD_OBJECT;
3533 }
3534
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003535 CameraMetadata &metadata = request->mSettings;
3536 size_t count = mTriggerMap.size();
3537
3538 for (size_t i = 0; i < count; ++i) {
3539 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003540 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003541
3542 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3543 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3544 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003545 if (isAeTrigger) {
3546 request->mResultExtras.precaptureTriggerId = triggerId;
3547 mCurrentPreCaptureTriggerId = triggerId;
3548 } else {
3549 request->mResultExtras.afTriggerId = triggerId;
3550 mCurrentAfTriggerId = triggerId;
3551 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003552 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3553 continue; // Trigger ID tag is deprecated since device HAL 3.2
3554 }
3555 }
3556
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003557 camera_metadata_entry entry = metadata.find(tag);
3558
3559 if (entry.count > 0) {
3560 /**
3561 * Already has an entry for this trigger in the request.
3562 * Rewrite it with our requested trigger value.
3563 */
3564 RequestTrigger oldTrigger = trigger;
3565
3566 oldTrigger.entryValue = entry.data.u8[0];
3567
3568 mTriggerReplacedMap.add(tag, oldTrigger);
3569 } else {
3570 /**
3571 * More typical, no trigger entry, so we just add it
3572 */
3573 mTriggerRemovedMap.add(tag, trigger);
3574 }
3575
3576 status_t res;
3577
3578 switch (trigger.getTagType()) {
3579 case TYPE_BYTE: {
3580 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3581 res = metadata.update(tag,
3582 &entryValue,
3583 /*count*/1);
3584 break;
3585 }
3586 case TYPE_INT32:
3587 res = metadata.update(tag,
3588 &trigger.entryValue,
3589 /*count*/1);
3590 break;
3591 default:
3592 ALOGE("%s: Type not supported: 0x%x",
3593 __FUNCTION__,
3594 trigger.getTagType());
3595 return INVALID_OPERATION;
3596 }
3597
3598 if (res != OK) {
3599 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3600 ", value %d", __FUNCTION__, trigger.getTagName(),
3601 trigger.entryValue);
3602 return res;
3603 }
3604
3605 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3606 trigger.getTagName(),
3607 trigger.entryValue);
3608 }
3609
3610 mTriggerMap.clear();
3611
3612 return count;
3613}
3614
3615status_t Camera3Device::RequestThread::removeTriggers(
3616 const sp<CaptureRequest> &request) {
3617 Mutex::Autolock al(mTriggerMutex);
3618
3619 CameraMetadata &metadata = request->mSettings;
3620
3621 /**
3622 * Replace all old entries with their old values.
3623 */
3624 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3625 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3626
3627 status_t res;
3628
3629 uint32_t tag = trigger.metadataTag;
3630 switch (trigger.getTagType()) {
3631 case TYPE_BYTE: {
3632 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3633 res = metadata.update(tag,
3634 &entryValue,
3635 /*count*/1);
3636 break;
3637 }
3638 case TYPE_INT32:
3639 res = metadata.update(tag,
3640 &trigger.entryValue,
3641 /*count*/1);
3642 break;
3643 default:
3644 ALOGE("%s: Type not supported: 0x%x",
3645 __FUNCTION__,
3646 trigger.getTagType());
3647 return INVALID_OPERATION;
3648 }
3649
3650 if (res != OK) {
3651 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3652 ", trigger value %d", __FUNCTION__,
3653 trigger.getTagName(), trigger.entryValue);
3654 return res;
3655 }
3656 }
3657 mTriggerReplacedMap.clear();
3658
3659 /**
3660 * Remove all new entries.
3661 */
3662 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3663 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3664 status_t res = metadata.erase(trigger.metadataTag);
3665
3666 if (res != OK) {
3667 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3668 ", trigger value %d", __FUNCTION__,
3669 trigger.getTagName(), trigger.entryValue);
3670 return res;
3671 }
3672 }
3673 mTriggerRemovedMap.clear();
3674
3675 return OK;
3676}
3677
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003678status_t Camera3Device::RequestThread::addDummyTriggerIds(
3679 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08003680 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003681 static const int32_t dummyTriggerId = 1;
3682 status_t res;
3683
3684 CameraMetadata &metadata = request->mSettings;
3685
3686 // If AF trigger is active, insert a dummy AF trigger ID if none already
3687 // exists
3688 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3689 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3690 if (afTrigger.count > 0 &&
3691 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3692 afId.count == 0) {
3693 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3694 if (res != OK) return res;
3695 }
3696
3697 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3698 // if none already exists
3699 camera_metadata_entry pcTrigger =
3700 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3701 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3702 if (pcTrigger.count > 0 &&
3703 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3704 pcId.count == 0) {
3705 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3706 &dummyTriggerId, 1);
3707 if (res != OK) return res;
3708 }
3709
3710 return OK;
3711}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003712
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003713/**
3714 * PreparerThread inner class methods
3715 */
3716
3717Camera3Device::PreparerThread::PreparerThread() :
3718 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3719}
3720
3721Camera3Device::PreparerThread::~PreparerThread() {
3722 Thread::requestExitAndWait();
3723 if (mCurrentStream != nullptr) {
3724 mCurrentStream->cancelPrepare();
3725 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3726 mCurrentStream.clear();
3727 }
3728 clear();
3729}
3730
Ruben Brunkc78ac262015-08-13 17:58:46 -07003731status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003732 status_t res;
3733
3734 Mutex::Autolock l(mLock);
3735
Ruben Brunkc78ac262015-08-13 17:58:46 -07003736 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003737 if (res == OK) {
3738 // No preparation needed, fire listener right off
3739 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3740 if (mListener) {
3741 mListener->notifyPrepared(stream->getId());
3742 }
3743 return OK;
3744 } else if (res != NOT_ENOUGH_DATA) {
3745 return res;
3746 }
3747
3748 // Need to prepare, start up thread if necessary
3749 if (!mActive) {
3750 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3751 // isn't running
3752 Thread::requestExitAndWait();
3753 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3754 if (res != OK) {
3755 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3756 if (mListener) {
3757 mListener->notifyPrepared(stream->getId());
3758 }
3759 return res;
3760 }
3761 mCancelNow = false;
3762 mActive = true;
3763 ALOGV("%s: Preparer stream started", __FUNCTION__);
3764 }
3765
3766 // queue up the work
3767 mPendingStreams.push_back(stream);
3768 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3769
3770 return OK;
3771}
3772
3773status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003774 Mutex::Autolock l(mLock);
3775
3776 for (const auto& stream : mPendingStreams) {
3777 stream->cancelPrepare();
3778 }
3779 mPendingStreams.clear();
3780 mCancelNow = true;
3781
3782 return OK;
3783}
3784
3785void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3786 Mutex::Autolock l(mLock);
3787 mListener = listener;
3788}
3789
3790bool Camera3Device::PreparerThread::threadLoop() {
3791 status_t res;
3792 {
3793 Mutex::Autolock l(mLock);
3794 if (mCurrentStream == nullptr) {
3795 // End thread if done with work
3796 if (mPendingStreams.empty()) {
3797 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3798 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3799 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3800 mActive = false;
3801 return false;
3802 }
3803
3804 // Get next stream to prepare
3805 auto it = mPendingStreams.begin();
3806 mCurrentStream = *it;
3807 mPendingStreams.erase(it);
3808 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3809 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3810 } else if (mCancelNow) {
3811 mCurrentStream->cancelPrepare();
3812 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3813 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3814 mCurrentStream.clear();
3815 mCancelNow = false;
3816 return true;
3817 }
3818 }
3819
3820 res = mCurrentStream->prepareNextBuffer();
3821 if (res == NOT_ENOUGH_DATA) return true;
3822 if (res != OK) {
3823 // Something bad happened; try to recover by cancelling prepare and
3824 // signalling listener anyway
3825 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3826 mCurrentStream->getId(), res, strerror(-res));
3827 mCurrentStream->cancelPrepare();
3828 }
3829
3830 // This stream has finished, notify listener
3831 Mutex::Autolock l(mLock);
3832 if (mListener) {
3833 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3834 mCurrentStream->getId());
3835 mListener->notifyPrepared(mCurrentStream->getId());
3836 }
3837
3838 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3839 mCurrentStream.clear();
3840
3841 return true;
3842}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003843
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003844/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003845 * Static callback forwarding methods from HAL to instance
3846 */
3847
3848void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3849 const camera3_capture_result *result) {
3850 Camera3Device *d =
3851 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003852
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003853 d->processCaptureResult(result);
3854}
3855
3856void Camera3Device::sNotify(const camera3_callback_ops *cb,
3857 const camera3_notify_msg *msg) {
3858 Camera3Device *d =
3859 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3860 d->notify(msg);
3861}
3862
3863}; // namespace android