blob: 0bc8ed1241cf6259da1dc87285e8c22d0f2b2981 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Igor Murashkinff3e31d2013-10-23 16:40:06 -070046#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070047#include "device3/Camera3Device.h"
48#include "device3/Camera3OutputStream.h"
49#include "device3/Camera3InputStream.h"
50#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070051#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070052#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080053
54using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080055
56namespace android {
57
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058Camera3Device::Camera3Device(int id):
59 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080060 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070061 mStatus(STATUS_UNINITIALIZED),
Zhijun He204e3292014-07-14 17:09:23 -070062 mUsePartialResult(false),
63 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070064 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070065 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070066 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070067 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080068{
69 ATRACE_CALL();
70 camera3_callback_ops::notify = &sNotify;
71 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
72 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
73}
74
75Camera3Device::~Camera3Device()
76{
77 ATRACE_CALL();
78 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
79 disconnect();
80}
81
Igor Murashkin71381052013-03-04 14:53:08 -080082int Camera3Device::getId() const {
83 return mId;
84}
85
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080086/**
87 * CameraDeviceBase interface
88 */
89
Yin-Chia Yehe074a932015-01-30 10:29:02 -080090status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080091{
92 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070093 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080094 Mutex::Autolock l(mLock);
95
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080096 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080097 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070098 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080099 return INVALID_OPERATION;
100 }
101
102 /** Open HAL device */
103
104 status_t res;
105 String8 deviceName = String8::format("%d", mId);
106
107 camera3_device_t *device;
108
Zhijun He213ce792013-11-19 08:45:15 -0800109 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800110 res = module->open(deviceName.string(),
111 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800112 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800113
114 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700115 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800116 return res;
117 }
118
119 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700120 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700121 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700122 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700123 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800124 device->common.version);
125 device->common.close(&device->common);
126 return BAD_VALUE;
127 }
128
129 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800130 res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700131 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800132 if (res != OK) return res;
133
134 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700135 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
136 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700137 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 device->common.close(&device->common);
139 return BAD_VALUE;
140 }
141
142 /** Initialize device with callback functions */
143
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700144 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800145 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700146 ATRACE_END();
147
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800148 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700149 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
150 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151 device->common.close(&device->common);
152 return BAD_VALUE;
153 }
154
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700155 /** Start up status tracker thread */
156 mStatusTracker = new StatusTracker(this);
157 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
158 if (res != OK) {
159 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
160 strerror(-res), res);
161 device->common.close(&device->common);
162 mStatusTracker.clear();
163 return res;
164 }
165
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800166 /** Start up request queue thread */
167
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700168 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800169 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800170 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700171 SET_ERR_L("Unable to start request queue thread: %s (%d)",
172 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800173 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800174 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800175 return res;
176 }
177
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700178 mPreparerThread = new PreparerThread();
179
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800180 /** Everything is good to go */
181
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700182 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800183 mDeviceInfo = info.static_camera_characteristics;
184 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700185 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800186 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700187 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700188 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700189 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700191 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700192 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
193 camera_metadata_entry partialResultsCount =
194 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
195 if (partialResultsCount.count > 0) {
196 mNumPartialResults = partialResultsCount.data.i32[0];
197 mUsePartialResult = (mNumPartialResults > 1);
198 }
199 } else {
200 camera_metadata_entry partialResultsQuirk =
201 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
202 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
203 mUsePartialResult = true;
204 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700205 }
206
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700207 camera_metadata_entry configs =
208 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
209 for (uint32_t i = 0; i < configs.count; i += 4) {
210 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
211 configs.data.i32[i + 3] ==
212 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
213 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
214 configs.data.i32[i + 2]));
215 }
216 }
217
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800218 return OK;
219}
220
221status_t Camera3Device::disconnect() {
222 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700223 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800224
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800225 ALOGV("%s: E", __FUNCTION__);
226
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700227 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800228
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700229 {
230 Mutex::Autolock l(mLock);
231 if (mStatus == STATUS_UNINITIALIZED) return res;
232
233 if (mStatus == STATUS_ACTIVE ||
234 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
235 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700236 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700237 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700238 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700239 } else {
240 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
241 if (res != OK) {
242 SET_ERR_L("Timeout waiting for HAL to drain");
243 // Continue to close device even in case of error
244 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700245 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800246 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800247
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700248 if (mStatus == STATUS_ERROR) {
249 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700250 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700251
252 if (mStatusTracker != NULL) {
253 mStatusTracker->requestExit();
254 }
255
256 if (mRequestThread != NULL) {
257 mRequestThread->requestExit();
258 }
259
260 mOutputStreams.clear();
261 mInputStream.clear();
262 }
263
264 // Joining done without holding mLock, otherwise deadlocks may ensue
265 // as the threads try to access parent state
266 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
267 // HAL may be in a bad state, so waiting for request thread
268 // (which may be stuck in the HAL processCaptureRequest call)
269 // could be dangerous.
270 mRequestThread->join();
271 }
272
273 if (mStatusTracker != NULL) {
274 mStatusTracker->join();
275 }
276
277 {
278 Mutex::Autolock l(mLock);
279
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800280 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700281 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800282
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700283 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800284 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700285 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800286 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700287 mHal3Device = NULL;
288 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800289
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700290 mStatus = STATUS_UNINITIALIZED;
291 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800292
293 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700294 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800295}
296
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700297// For dumping/debugging only -
298// try to acquire a lock a few times, eventually give up to proceed with
299// debug/dump operations
300bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
301 bool gotLock = false;
302 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
303 if (lock.tryLock() == NO_ERROR) {
304 gotLock = true;
305 break;
306 } else {
307 usleep(kDumpSleepDuration);
308 }
309 }
310 return gotLock;
311}
312
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700313Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
314 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
315 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
316 const int STREAM_CONFIGURATION_SIZE = 4;
317 const int STREAM_FORMAT_OFFSET = 0;
318 const int STREAM_WIDTH_OFFSET = 1;
319 const int STREAM_HEIGHT_OFFSET = 2;
320 const int STREAM_IS_INPUT_OFFSET = 3;
321 camera_metadata_ro_entry_t availableStreamConfigs =
322 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
323 if (availableStreamConfigs.count == 0 ||
324 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
325 return Size(0, 0);
326 }
327
328 // Get max jpeg size (area-wise).
329 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
330 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
331 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
332 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
333 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
334 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
335 && format == HAL_PIXEL_FORMAT_BLOB &&
336 (width * height > maxJpegWidth * maxJpegHeight)) {
337 maxJpegWidth = width;
338 maxJpegHeight = height;
339 }
340 }
341 } else {
342 camera_metadata_ro_entry availableJpegSizes =
343 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
344 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
345 return Size(0, 0);
346 }
347
348 // Get max jpeg size (area-wise).
349 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
350 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
351 > (maxJpegWidth * maxJpegHeight)) {
352 maxJpegWidth = availableJpegSizes.data.i32[i];
353 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
354 }
355 }
356 }
357 return Size(maxJpegWidth, maxJpegHeight);
358}
359
Zhijun Hef7da0962014-04-24 13:27:56 -0700360ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700361 // Get max jpeg size (area-wise).
362 Size maxJpegResolution = getMaxJpegResolution();
363 if (maxJpegResolution.width == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700364 ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
365 __FUNCTION__, mId);
366 return BAD_VALUE;
367 }
368
Zhijun Hef7da0962014-04-24 13:27:56 -0700369 // Get max jpeg buffer size
370 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700371 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
372 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700373 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
374 return BAD_VALUE;
375 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700376 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800377 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700378
379 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700380 float scaleFactor = ((float) (width * height)) /
381 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800382 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
383 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700384 if (jpegBufferSize > maxJpegBufferSize) {
385 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700386 }
387
388 return jpegBufferSize;
389}
390
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800391status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
392 ATRACE_CALL();
393 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700394
395 // Try to lock, but continue in case of failure (to avoid blocking in
396 // deadlocks)
397 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
398 bool gotLock = tryLockSpinRightRound(mLock);
399
400 ALOGW_IF(!gotInterfaceLock,
401 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
402 mId, __FUNCTION__);
403 ALOGW_IF(!gotLock,
404 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
405 mId, __FUNCTION__);
406
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800407 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800408
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800409 const char *status =
410 mStatus == STATUS_ERROR ? "ERROR" :
411 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700412 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
413 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800414 mStatus == STATUS_ACTIVE ? "ACTIVE" :
415 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700416
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800417 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700418 if (mStatus == STATUS_ERROR) {
419 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
420 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800421 lines.appendFormat(" Stream configuration:\n");
422
423 if (mInputStream != NULL) {
424 write(fd, lines.string(), lines.size());
425 mInputStream->dump(fd, args);
426 } else {
427 lines.appendFormat(" No input stream.\n");
428 write(fd, lines.string(), lines.size());
429 }
430 for (size_t i = 0; i < mOutputStreams.size(); i++) {
431 mOutputStreams[i]->dump(fd,args);
432 }
433
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700434 lines = String8(" In-flight requests:\n");
435 if (mInFlightMap.size() == 0) {
436 lines.append(" None\n");
437 } else {
438 for (size_t i = 0; i < mInFlightMap.size(); i++) {
439 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700440 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700441 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800442 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700443 r.numBuffersLeft);
444 }
445 }
446 write(fd, lines.string(), lines.size());
447
Igor Murashkin1e479c02013-09-06 16:55:14 -0700448 {
449 lines = String8(" Last request sent:\n");
450 write(fd, lines.string(), lines.size());
451
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700452 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700453 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
454 }
455
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800456 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700457 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800458 write(fd, lines.string(), lines.size());
459 mHal3Device->ops->dump(mHal3Device, fd);
460 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800461
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700462 if (gotLock) mLock.unlock();
463 if (gotInterfaceLock) mInterfaceLock.unlock();
464
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800465 return OK;
466}
467
468const CameraMetadata& Camera3Device::info() const {
469 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800470 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
471 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700472 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800473 mStatus == STATUS_ERROR ?
474 "when in error state" : "before init");
475 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800476 return mDeviceInfo;
477}
478
Jianing Wei90e59c92014-03-12 18:29:36 -0700479status_t Camera3Device::checkStatusOkToCaptureLocked() {
480 switch (mStatus) {
481 case STATUS_ERROR:
482 CLOGE("Device has encountered a serious error");
483 return INVALID_OPERATION;
484 case STATUS_UNINITIALIZED:
485 CLOGE("Device not initialized");
486 return INVALID_OPERATION;
487 case STATUS_UNCONFIGURED:
488 case STATUS_CONFIGURED:
489 case STATUS_ACTIVE:
490 // OK
491 break;
492 default:
493 SET_ERR_L("Unexpected status: %d", mStatus);
494 return INVALID_OPERATION;
495 }
496 return OK;
497}
498
499status_t Camera3Device::convertMetadataListToRequestListLocked(
500 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
501 if (requestList == NULL) {
502 CLOGE("requestList cannot be NULL.");
503 return BAD_VALUE;
504 }
505
Jianing Weicb0652e2014-03-12 18:29:36 -0700506 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700507 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
508 it != metadataList.end(); ++it) {
509 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
510 if (newRequest == 0) {
511 CLOGE("Can't create capture request");
512 return BAD_VALUE;
513 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700514
515 // Setup burst Id and request Id
516 newRequest->mResultExtras.burstId = burstId++;
517 if (it->exists(ANDROID_REQUEST_ID)) {
518 if (it->find(ANDROID_REQUEST_ID).count == 0) {
519 CLOGE("RequestID entry exists; but must not be empty in metadata");
520 return BAD_VALUE;
521 }
522 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
523 } else {
524 CLOGE("RequestID does not exist in metadata");
525 return BAD_VALUE;
526 }
527
Jianing Wei90e59c92014-03-12 18:29:36 -0700528 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700529
530 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700531 }
532 return OK;
533}
534
Jianing Weicb0652e2014-03-12 18:29:36 -0700535status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800536 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800537
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700538 List<const CameraMetadata> requests;
539 requests.push_back(request);
540 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800541}
542
Jianing Wei90e59c92014-03-12 18:29:36 -0700543status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700544 const List<const CameraMetadata> &requests, bool repeating,
545 /*out*/
546 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700547 ATRACE_CALL();
548 Mutex::Autolock il(mInterfaceLock);
549 Mutex::Autolock l(mLock);
550
551 status_t res = checkStatusOkToCaptureLocked();
552 if (res != OK) {
553 // error logged by previous call
554 return res;
555 }
556
557 RequestList requestList;
558
559 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
560 if (res != OK) {
561 // error logged by previous call
562 return res;
563 }
564
565 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700566 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700567 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700568 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700569 }
570
571 if (res == OK) {
572 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
573 if (res != OK) {
574 SET_ERR_L("Can't transition to active in %f seconds!",
575 kActiveTimeout/1e9);
576 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700577 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
578 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700579 } else {
580 CLOGE("Cannot queue request. Impossible.");
581 return BAD_VALUE;
582 }
583
584 return res;
585}
586
Jianing Weicb0652e2014-03-12 18:29:36 -0700587status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
588 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700589 ATRACE_CALL();
590
Jianing Weicb0652e2014-03-12 18:29:36 -0700591 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700592}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800593
Jianing Weicb0652e2014-03-12 18:29:36 -0700594status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
595 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800596 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800597
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700598 List<const CameraMetadata> requests;
599 requests.push_back(request);
600 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800601}
602
Jianing Weicb0652e2014-03-12 18:29:36 -0700603status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
604 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700605 ATRACE_CALL();
606
Jianing Weicb0652e2014-03-12 18:29:36 -0700607 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700608}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800609
610sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
611 const CameraMetadata &request) {
612 status_t res;
613
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700614 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800615 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700616 // Stream configuration failed due to unsupported configuration.
617 // Device back to unconfigured state. Client might try other configuraitons
618 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
619 CLOGE("No streams configured");
620 return NULL;
621 }
622 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800623 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700624 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800625 return NULL;
626 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700627 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700628 if (mStatus == STATUS_UNCONFIGURED) {
629 CLOGE("No streams configured");
630 return NULL;
631 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800632 }
633
634 sp<CaptureRequest> newRequest = createCaptureRequest(request);
635 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800636}
637
Jianing Weicb0652e2014-03-12 18:29:36 -0700638status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800639 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700640 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800641 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800642
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800643 switch (mStatus) {
644 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700645 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800646 return INVALID_OPERATION;
647 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700648 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800649 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700650 case STATUS_UNCONFIGURED:
651 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800652 case STATUS_ACTIVE:
653 // OK
654 break;
655 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700656 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800657 return INVALID_OPERATION;
658 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700659 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700660
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700661 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800662}
663
664status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
665 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700666 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800667
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700668 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800669}
670
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700671status_t Camera3Device::createInputStream(
672 uint32_t width, uint32_t height, int format, int *id) {
673 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700674 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700675 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700676 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
677 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700678
679 status_t res;
680 bool wasActive = false;
681
682 switch (mStatus) {
683 case STATUS_ERROR:
684 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
685 return INVALID_OPERATION;
686 case STATUS_UNINITIALIZED:
687 ALOGE("%s: Device not initialized", __FUNCTION__);
688 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700689 case STATUS_UNCONFIGURED:
690 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700691 // OK
692 break;
693 case STATUS_ACTIVE:
694 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700695 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700696 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700697 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700698 return res;
699 }
700 wasActive = true;
701 break;
702 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700703 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700704 return INVALID_OPERATION;
705 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700706 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700707
708 if (mInputStream != 0) {
709 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
710 return INVALID_OPERATION;
711 }
712
713 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
714 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700715 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700716
717 mInputStream = newStream;
718
719 *id = mNextStreamId++;
720
721 // Continue captures if active at start
722 if (wasActive) {
723 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
724 res = configureStreamsLocked();
725 if (res != OK) {
726 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
727 __FUNCTION__, mNextStreamId, strerror(-res), res);
728 return res;
729 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700730 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700731 }
732
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700733 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700734 return OK;
735}
736
Igor Murashkin2fba5842013-04-22 14:03:54 -0700737
738status_t Camera3Device::createZslStream(
739 uint32_t width, uint32_t height,
740 int depth,
741 /*out*/
742 int *id,
743 sp<Camera3ZslStream>* zslStream) {
744 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700745 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700746 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700747 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
748 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700749
750 status_t res;
751 bool wasActive = false;
752
753 switch (mStatus) {
754 case STATUS_ERROR:
755 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
756 return INVALID_OPERATION;
757 case STATUS_UNINITIALIZED:
758 ALOGE("%s: Device not initialized", __FUNCTION__);
759 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700760 case STATUS_UNCONFIGURED:
761 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700762 // OK
763 break;
764 case STATUS_ACTIVE:
765 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700766 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700767 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700768 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700769 return res;
770 }
771 wasActive = true;
772 break;
773 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700774 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700775 return INVALID_OPERATION;
776 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700777 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700778
779 if (mInputStream != 0) {
780 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
781 return INVALID_OPERATION;
782 }
783
784 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
785 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700786 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700787
788 res = mOutputStreams.add(mNextStreamId, newStream);
789 if (res < 0) {
790 ALOGE("%s: Can't add new stream to set: %s (%d)",
791 __FUNCTION__, strerror(-res), res);
792 return res;
793 }
794 mInputStream = newStream;
795
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530796 mNeedConfig = true;
797
Igor Murashkin2fba5842013-04-22 14:03:54 -0700798 *id = mNextStreamId++;
799 *zslStream = newStream;
800
801 // Continue captures if active at start
802 if (wasActive) {
803 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
804 res = configureStreamsLocked();
805 if (res != OK) {
806 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
807 __FUNCTION__, mNextStreamId, strerror(-res), res);
808 return res;
809 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700810 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700811 }
812
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700813 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700814 return OK;
815}
816
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800817status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800818 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700819 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800820 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700821 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800822 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700823 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
824 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800825
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800826 status_t res;
827 bool wasActive = false;
828
829 switch (mStatus) {
830 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700831 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800832 return INVALID_OPERATION;
833 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700834 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800835 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700836 case STATUS_UNCONFIGURED:
837 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800838 // OK
839 break;
840 case STATUS_ACTIVE:
841 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700842 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800843 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700844 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800845 return res;
846 }
847 wasActive = true;
848 break;
849 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700850 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800851 return INVALID_OPERATION;
852 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700853 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800854
855 sp<Camera3OutputStream> newStream;
856 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700857 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700858 if (jpegBufferSize <= 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700859 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
860 return BAD_VALUE;
861 }
862
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800863 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700864 width, height, jpegBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800865 } else {
866 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700867 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800868 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700869 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800870
871 res = mOutputStreams.add(mNextStreamId, newStream);
872 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700873 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800874 return res;
875 }
876
877 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700878 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800879
880 // Continue captures if active at start
881 if (wasActive) {
882 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
883 res = configureStreamsLocked();
884 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700885 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
886 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800887 return res;
888 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700889 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800890 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700891 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800892 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800893}
894
895status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
896 ATRACE_CALL();
897 (void)outputId; (void)id;
898
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700899 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800900 return INVALID_OPERATION;
901}
902
903
904status_t Camera3Device::getStreamInfo(int id,
905 uint32_t *width, uint32_t *height, uint32_t *format) {
906 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700907 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800908 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800909
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800910 switch (mStatus) {
911 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700912 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800913 return INVALID_OPERATION;
914 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700915 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800916 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700917 case STATUS_UNCONFIGURED:
918 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800919 case STATUS_ACTIVE:
920 // OK
921 break;
922 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700923 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800924 return INVALID_OPERATION;
925 }
926
927 ssize_t idx = mOutputStreams.indexOfKey(id);
928 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700929 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800930 return idx;
931 }
932
933 if (width) *width = mOutputStreams[idx]->getWidth();
934 if (height) *height = mOutputStreams[idx]->getHeight();
935 if (format) *format = mOutputStreams[idx]->getFormat();
936
937 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800938}
939
940status_t Camera3Device::setStreamTransform(int id,
941 int transform) {
942 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700943 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800944 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800945
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800946 switch (mStatus) {
947 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700948 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800949 return INVALID_OPERATION;
950 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700951 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800952 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700953 case STATUS_UNCONFIGURED:
954 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800955 case STATUS_ACTIVE:
956 // OK
957 break;
958 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700959 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800960 return INVALID_OPERATION;
961 }
962
963 ssize_t idx = mOutputStreams.indexOfKey(id);
964 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700965 CLOGE("Stream %d does not exist",
966 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800967 return BAD_VALUE;
968 }
969
970 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800971}
972
973status_t Camera3Device::deleteStream(int id) {
974 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700975 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800976 Mutex::Autolock l(mLock);
977 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800978
Igor Murashkine2172be2013-05-28 15:31:39 -0700979 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
980
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800981 // CameraDevice semantics require device to already be idle before
982 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700983 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700984 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
985 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800986 }
987
Igor Murashkin2fba5842013-04-22 14:03:54 -0700988 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800989 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800990 if (mInputStream != NULL && id == mInputStream->getId()) {
991 deletedStream = mInputStream;
992 mInputStream.clear();
993 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800994 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700995 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800996 return BAD_VALUE;
997 }
Zhijun He5f446352014-01-22 09:49:33 -0800998 }
999
1000 // Delete output stream or the output part of a bi-directional stream.
1001 if (outputStreamIdx != NAME_NOT_FOUND) {
1002 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001003 mOutputStreams.removeItem(id);
1004 }
1005
1006 // Free up the stream endpoint so that it can be used by some other stream
1007 res = deletedStream->disconnect();
1008 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001009 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001010 // fall through since we want to still list the stream as deleted.
1011 }
1012 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001013 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001014
1015 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001016}
1017
1018status_t Camera3Device::deleteReprocessStream(int id) {
1019 ATRACE_CALL();
1020 (void)id;
1021
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001022 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001023 return INVALID_OPERATION;
1024}
1025
Igor Murashkine2d167e2014-08-19 16:19:59 -07001026status_t Camera3Device::configureStreams() {
1027 ATRACE_CALL();
1028 ALOGV("%s: E", __FUNCTION__);
1029
1030 Mutex::Autolock il(mInterfaceLock);
1031 Mutex::Autolock l(mLock);
1032
1033 return configureStreamsLocked();
1034}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001035
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001036status_t Camera3Device::getInputBufferProducer(
1037 sp<IGraphicBufferProducer> *producer) {
1038 Mutex::Autolock il(mInterfaceLock);
1039 Mutex::Autolock l(mLock);
1040
1041 if (producer == NULL) {
1042 return BAD_VALUE;
1043 } else if (mInputStream == NULL) {
1044 return INVALID_OPERATION;
1045 }
1046
1047 return mInputStream->getInputBufferProducer(producer);
1048}
1049
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001050status_t Camera3Device::createDefaultRequest(int templateId,
1051 CameraMetadata *request) {
1052 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001053 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001054 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001055 Mutex::Autolock l(mLock);
1056
1057 switch (mStatus) {
1058 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001059 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001060 return INVALID_OPERATION;
1061 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001062 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001063 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001064 case STATUS_UNCONFIGURED:
1065 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001066 case STATUS_ACTIVE:
1067 // OK
1068 break;
1069 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001070 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071 return INVALID_OPERATION;
1072 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001073
Zhijun Hea1530f12014-09-14 12:44:20 -07001074 if (!mRequestTemplateCache[templateId].isEmpty()) {
1075 *request = mRequestTemplateCache[templateId];
1076 return OK;
1077 }
1078
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001079 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001080 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001081 rawRequest = mHal3Device->ops->construct_default_request_settings(
1082 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001083 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001084 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001085 ALOGI("%s: template %d is not supported on this camera device",
1086 __FUNCTION__, templateId);
1087 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001088 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001089 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001090 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001091
1092 return OK;
1093}
1094
1095status_t Camera3Device::waitUntilDrained() {
1096 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001097 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001098 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001099
Zhijun He69a37482014-03-23 18:44:49 -07001100 return waitUntilDrainedLocked();
1101}
1102
1103status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001104 switch (mStatus) {
1105 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001106 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001107 ALOGV("%s: Already idle", __FUNCTION__);
1108 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001109 case STATUS_CONFIGURED:
1110 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001111 case STATUS_ERROR:
1112 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001113 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001114 break;
1115 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001116 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001117 return INVALID_OPERATION;
1118 }
1119
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001120 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1121 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001122 if (res != OK) {
1123 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1124 res);
1125 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001126 return res;
1127}
1128
1129// Pause to reconfigure
1130status_t Camera3Device::internalPauseAndWaitLocked() {
1131 mRequestThread->setPaused(true);
1132 mPauseStateNotify = true;
1133
1134 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1135 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1136 if (res != OK) {
1137 SET_ERR_L("Can't idle device in %f seconds!",
1138 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001139 }
1140
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001141 return res;
1142}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001143
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001144// Resume after internalPauseAndWaitLocked
1145status_t Camera3Device::internalResumeLocked() {
1146 status_t res;
1147
1148 mRequestThread->setPaused(false);
1149
1150 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1151 if (res != OK) {
1152 SET_ERR_L("Can't transition to active in %f seconds!",
1153 kActiveTimeout/1e9);
1154 }
1155 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001156 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001157}
1158
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001159status_t Camera3Device::waitUntilStateThenRelock(bool active,
1160 nsecs_t timeout) {
1161 status_t res = OK;
1162 if (active == (mStatus == STATUS_ACTIVE)) {
1163 // Desired state already reached
1164 return res;
1165 }
1166
1167 bool stateSeen = false;
1168 do {
1169 mRecentStatusUpdates.clear();
1170
1171 res = mStatusChanged.waitRelative(mLock, timeout);
1172 if (res != OK) break;
1173
1174 // Check state change history during wait
1175 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1176 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1177 stateSeen = true;
1178 break;
1179 }
1180 }
1181 } while (!stateSeen);
1182
1183 return res;
1184}
1185
1186
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001187status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1188 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001189 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001190
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001191 if (listener != NULL && mListener != NULL) {
1192 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1193 }
1194 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001195 mRequestThread->setNotificationListener(listener);
1196 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001197
1198 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001199}
1200
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001201bool Camera3Device::willNotify3A() {
1202 return false;
1203}
1204
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001205status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001206 status_t res;
1207 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001208
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001209 while (mResultQueue.empty()) {
1210 res = mResultSignal.waitRelative(mOutputLock, timeout);
1211 if (res == TIMED_OUT) {
1212 return res;
1213 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001214 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001215 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001216 return res;
1217 }
1218 }
1219 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001220}
1221
Jianing Weicb0652e2014-03-12 18:29:36 -07001222status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001223 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001224 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001225
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001226 if (mResultQueue.empty()) {
1227 return NOT_ENOUGH_DATA;
1228 }
1229
Jianing Weicb0652e2014-03-12 18:29:36 -07001230 if (frame == NULL) {
1231 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1232 return BAD_VALUE;
1233 }
1234
1235 CaptureResult &result = *(mResultQueue.begin());
1236 frame->mResultExtras = result.mResultExtras;
1237 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001238 mResultQueue.erase(mResultQueue.begin());
1239
1240 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001241}
1242
1243status_t Camera3Device::triggerAutofocus(uint32_t id) {
1244 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001245 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001246
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001247 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1248 // Mix-in this trigger into the next request and only the next request.
1249 RequestTrigger trigger[] = {
1250 {
1251 ANDROID_CONTROL_AF_TRIGGER,
1252 ANDROID_CONTROL_AF_TRIGGER_START
1253 },
1254 {
1255 ANDROID_CONTROL_AF_TRIGGER_ID,
1256 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001257 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001258 };
1259
1260 return mRequestThread->queueTrigger(trigger,
1261 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001262}
1263
1264status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1265 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001266 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001267
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001268 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1269 // Mix-in this trigger into the next request and only the next request.
1270 RequestTrigger trigger[] = {
1271 {
1272 ANDROID_CONTROL_AF_TRIGGER,
1273 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1274 },
1275 {
1276 ANDROID_CONTROL_AF_TRIGGER_ID,
1277 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001278 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001279 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001280
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001281 return mRequestThread->queueTrigger(trigger,
1282 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001283}
1284
1285status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1286 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001287 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001288
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001289 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1290 // Mix-in this trigger into the next request and only the next request.
1291 RequestTrigger trigger[] = {
1292 {
1293 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1294 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1295 },
1296 {
1297 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1298 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001299 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001300 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001301
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001302 return mRequestThread->queueTrigger(trigger,
1303 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001304}
1305
1306status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1307 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1308 ATRACE_CALL();
1309 (void)reprocessStreamId; (void)buffer; (void)listener;
1310
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001311 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001312 return INVALID_OPERATION;
1313}
1314
Jianing Weicb0652e2014-03-12 18:29:36 -07001315status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001316 ATRACE_CALL();
1317 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001318 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001319
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001320 NotificationListener* listener;
1321 {
1322 Mutex::Autolock l(mOutputLock);
1323 listener = mListener;
1324 }
1325
Zhijun He7ef20392014-04-21 16:04:17 -07001326 {
1327 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001328 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001329 }
1330
Zhijun He491e3412013-12-27 10:57:44 -08001331 status_t res;
1332 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1333 res = mHal3Device->ops->flush(mHal3Device);
1334 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001335 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001336 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001337 }
1338
1339 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001340}
1341
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001342status_t Camera3Device::prepare(int streamId) {
1343 ATRACE_CALL();
1344 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001345 Mutex::Autolock il(mInterfaceLock);
1346 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001347
1348 sp<Camera3StreamInterface> stream;
1349 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1350 if (outputStreamIdx == NAME_NOT_FOUND) {
1351 CLOGE("Stream %d does not exist", streamId);
1352 return BAD_VALUE;
1353 }
1354
1355 stream = mOutputStreams.editValueAt(outputStreamIdx);
1356
1357 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001358 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001359 return BAD_VALUE;
1360 }
1361
1362 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001363 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001364 return BAD_VALUE;
1365 }
1366
1367 return mPreparerThread->prepare(stream);
1368}
1369
Zhijun He204e3292014-07-14 17:09:23 -07001370uint32_t Camera3Device::getDeviceVersion() {
1371 ATRACE_CALL();
1372 Mutex::Autolock il(mInterfaceLock);
1373 return mDeviceVersion;
1374}
1375
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001376/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001377 * Methods called by subclasses
1378 */
1379
1380void Camera3Device::notifyStatus(bool idle) {
1381 {
1382 // Need mLock to safely update state and synchronize to current
1383 // state of methods in flight.
1384 Mutex::Autolock l(mLock);
1385 // We can get various system-idle notices from the status tracker
1386 // while starting up. Only care about them if we've actually sent
1387 // in some requests recently.
1388 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1389 return;
1390 }
1391 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1392 idle ? "idle" : "active");
1393 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1394 mRecentStatusUpdates.add(mStatus);
1395 mStatusChanged.signal();
1396
1397 // Skip notifying listener if we're doing some user-transparent
1398 // state changes
1399 if (mPauseStateNotify) return;
1400 }
1401 NotificationListener *listener;
1402 {
1403 Mutex::Autolock l(mOutputLock);
1404 listener = mListener;
1405 }
1406 if (idle && listener != NULL) {
1407 listener->notifyIdle();
1408 }
1409}
1410
1411/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001412 * Camera3Device private methods
1413 */
1414
1415sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1416 const CameraMetadata &request) {
1417 ATRACE_CALL();
1418 status_t res;
1419
1420 sp<CaptureRequest> newRequest = new CaptureRequest;
1421 newRequest->mSettings = request;
1422
1423 camera_metadata_entry_t inputStreams =
1424 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1425 if (inputStreams.count > 0) {
1426 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001427 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001428 CLOGE("Request references unknown input stream %d",
1429 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001430 return NULL;
1431 }
1432 // Lazy completion of stream configuration (allocation/registration)
1433 // on first use
1434 if (mInputStream->isConfiguring()) {
1435 res = mInputStream->finishConfiguration(mHal3Device);
1436 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001437 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001438 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001439 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001440 return NULL;
1441 }
1442 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001443 // Check if stream is being prepared
1444 if (mInputStream->isPreparing()) {
1445 CLOGE("Request references an input stream that's being prepared!");
1446 return NULL;
1447 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001448
1449 newRequest->mInputStream = mInputStream;
1450 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1451 }
1452
1453 camera_metadata_entry_t streams =
1454 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1455 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001456 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001457 return NULL;
1458 }
1459
1460 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001461 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001462 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001463 CLOGE("Request references unknown stream %d",
1464 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001465 return NULL;
1466 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001467 sp<Camera3OutputStreamInterface> stream =
1468 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001469
1470 // Lazy completion of stream configuration (allocation/registration)
1471 // on first use
1472 if (stream->isConfiguring()) {
1473 res = stream->finishConfiguration(mHal3Device);
1474 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001475 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1476 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001477 return NULL;
1478 }
1479 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001480 // Check if stream is being prepared
1481 if (stream->isPreparing()) {
1482 CLOGE("Request references an output stream that's being prepared!");
1483 return NULL;
1484 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001485
1486 newRequest->mOutputStreams.push(stream);
1487 }
1488 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1489
1490 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001491}
1492
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001493bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1494 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1495 Size size = mSupportedOpaqueInputSizes[i];
1496 if (size.width == width && size.height == height) {
1497 return true;
1498 }
1499 }
1500
1501 return false;
1502}
1503
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001504status_t Camera3Device::configureStreamsLocked() {
1505 ATRACE_CALL();
1506 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001507
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001508 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001509 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001510 return INVALID_OPERATION;
1511 }
1512
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001513 if (!mNeedConfig) {
1514 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1515 return OK;
1516 }
1517
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001518 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1519 // adding a dummy stream instead.
1520 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1521 if (mOutputStreams.size() == 0) {
1522 addDummyStreamLocked();
1523 } else {
1524 tryRemoveDummyStreamLocked();
1525 }
1526
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001527 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001528 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001529
1530 camera3_stream_configuration config;
1531
1532 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1533
1534 Vector<camera3_stream_t*> streams;
1535 streams.setCapacity(config.num_streams);
1536
1537 if (mInputStream != NULL) {
1538 camera3_stream_t *inputStream;
1539 inputStream = mInputStream->startConfiguration();
1540 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001541 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001542 return INVALID_OPERATION;
1543 }
1544 streams.add(inputStream);
1545 }
1546
1547 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001548
1549 // Don't configure bidi streams twice, nor add them twice to the list
1550 if (mOutputStreams[i].get() ==
1551 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1552
1553 config.num_streams--;
1554 continue;
1555 }
1556
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001557 camera3_stream_t *outputStream;
1558 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1559 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001560 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001561 return INVALID_OPERATION;
1562 }
1563 streams.add(outputStream);
1564 }
1565
1566 config.streams = streams.editArray();
1567
1568 // Do the HAL configuration; will potentially touch stream
1569 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001570 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001571 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001572 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001573
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001574 if (res == BAD_VALUE) {
1575 // HAL rejected this set of streams as unsupported, clean up config
1576 // attempt and return to unconfigured state
1577 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1578 res = mInputStream->cancelConfiguration();
1579 if (res != OK) {
1580 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1581 mInputStream->getId(), strerror(-res), res);
1582 return res;
1583 }
1584 }
1585
1586 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1587 sp<Camera3OutputStreamInterface> outputStream =
1588 mOutputStreams.editValueAt(i);
1589 if (outputStream->isConfiguring()) {
1590 res = outputStream->cancelConfiguration();
1591 if (res != OK) {
1592 SET_ERR_L(
1593 "Can't cancel configuring output stream %d: %s (%d)",
1594 outputStream->getId(), strerror(-res), res);
1595 return res;
1596 }
1597 }
1598 }
1599
1600 // Return state to that at start of call, so that future configures
1601 // properly clean things up
1602 mStatus = STATUS_UNCONFIGURED;
1603 mNeedConfig = true;
1604
1605 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1606 return BAD_VALUE;
1607 } else if (res != OK) {
1608 // Some other kind of error from configure_streams - this is not
1609 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001610 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1611 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001612 return res;
1613 }
1614
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001615 // Finish all stream configuration immediately.
1616 // TODO: Try to relax this later back to lazy completion, which should be
1617 // faster
1618
Igor Murashkin073f8572013-05-02 14:59:28 -07001619 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001620 res = mInputStream->finishConfiguration(mHal3Device);
1621 if (res != OK) {
1622 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1623 mInputStream->getId(), strerror(-res), res);
1624 return res;
1625 }
1626 }
1627
1628 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001629 sp<Camera3OutputStreamInterface> outputStream =
1630 mOutputStreams.editValueAt(i);
1631 if (outputStream->isConfiguring()) {
1632 res = outputStream->finishConfiguration(mHal3Device);
1633 if (res != OK) {
1634 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1635 outputStream->getId(), strerror(-res), res);
1636 return res;
1637 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001638 }
1639 }
1640
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001641 // Request thread needs to know to avoid using repeat-last-settings protocol
1642 // across configure_streams() calls
1643 mRequestThread->configurationComplete();
1644
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001645 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001646
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001647 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001648
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001649 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001650 mStatus = STATUS_CONFIGURED;
1651 } else {
1652 mStatus = STATUS_UNCONFIGURED;
1653 }
1654
1655 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1656
Zhijun He0a210512014-07-24 13:45:15 -07001657 // tear down the deleted streams after configure streams.
1658 mDeletedStreams.clear();
1659
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001660 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001661}
1662
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001663status_t Camera3Device::addDummyStreamLocked() {
1664 ATRACE_CALL();
1665 status_t res;
1666
1667 if (mDummyStreamId != NO_STREAM) {
1668 // Should never be adding a second dummy stream when one is already
1669 // active
1670 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1671 __FUNCTION__, mId);
1672 return INVALID_OPERATION;
1673 }
1674
1675 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1676
1677 sp<Camera3OutputStreamInterface> dummyStream =
1678 new Camera3DummyStream(mNextStreamId);
1679
1680 res = mOutputStreams.add(mNextStreamId, dummyStream);
1681 if (res < 0) {
1682 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1683 return res;
1684 }
1685
1686 mDummyStreamId = mNextStreamId;
1687 mNextStreamId++;
1688
1689 return OK;
1690}
1691
1692status_t Camera3Device::tryRemoveDummyStreamLocked() {
1693 ATRACE_CALL();
1694 status_t res;
1695
1696 if (mDummyStreamId == NO_STREAM) return OK;
1697 if (mOutputStreams.size() == 1) return OK;
1698
1699 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1700
1701 // Ok, have a dummy stream and there's at least one other output stream,
1702 // so remove the dummy
1703
1704 sp<Camera3StreamInterface> deletedStream;
1705 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1706 if (outputStreamIdx == NAME_NOT_FOUND) {
1707 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1708 return INVALID_OPERATION;
1709 }
1710
1711 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1712 mOutputStreams.removeItemsAt(outputStreamIdx);
1713
1714 // Free up the stream endpoint so that it can be used by some other stream
1715 res = deletedStream->disconnect();
1716 if (res != OK) {
1717 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1718 // fall through since we want to still list the stream as deleted.
1719 }
1720 mDeletedStreams.add(deletedStream);
1721 mDummyStreamId = NO_STREAM;
1722
1723 return res;
1724}
1725
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001726void Camera3Device::setErrorState(const char *fmt, ...) {
1727 Mutex::Autolock l(mLock);
1728 va_list args;
1729 va_start(args, fmt);
1730
1731 setErrorStateLockedV(fmt, args);
1732
1733 va_end(args);
1734}
1735
1736void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1737 Mutex::Autolock l(mLock);
1738 setErrorStateLockedV(fmt, args);
1739}
1740
1741void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1742 va_list args;
1743 va_start(args, fmt);
1744
1745 setErrorStateLockedV(fmt, args);
1746
1747 va_end(args);
1748}
1749
1750void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001751 // Print out all error messages to log
1752 String8 errorCause = String8::formatV(fmt, args);
1753 ALOGE("Camera %d: %s", mId, errorCause.string());
1754
1755 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001756 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001757
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001758 mErrorCause = errorCause;
1759
1760 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001761 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001762
1763 // Notify upstream about a device error
1764 if (mListener != NULL) {
1765 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1766 CaptureResultExtras());
1767 }
1768
1769 // Save stack trace. View by dumping it later.
1770 CameraTraces::saveTrace();
1771 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001772}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001773
1774/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001775 * In-flight request management
1776 */
1777
Jianing Weicb0652e2014-03-12 18:29:36 -07001778status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001779 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001780 ATRACE_CALL();
1781 Mutex::Autolock l(mInFlightLock);
1782
1783 ssize_t res;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001784 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001785 if (res < 0) return res;
1786
1787 return OK;
1788}
1789
1790/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001791 * Check if all 3A fields are ready, and send off a partial 3A-only result
1792 * to the output frame queue
1793 */
Zhijun He204e3292014-07-14 17:09:23 -07001794bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001795 uint32_t frameNumber,
1796 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001797
1798 // Check if all 3A states are present
1799 // The full list of fields is
1800 // android.control.afMode
1801 // android.control.awbMode
1802 // android.control.aeState
1803 // android.control.awbState
1804 // android.control.afState
1805 // android.control.afTriggerID
1806 // android.control.aePrecaptureID
1807 // TODO: Add android.control.aeMode
1808
1809 bool gotAllStates = true;
1810
1811 uint8_t afMode;
1812 uint8_t awbMode;
1813 uint8_t aeState;
1814 uint8_t afState;
1815 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001816
1817 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1818 &afMode, frameNumber);
1819
1820 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1821 &awbMode, frameNumber);
1822
1823 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1824 &aeState, frameNumber);
1825
1826 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1827 &afState, frameNumber);
1828
1829 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1830 &awbState, frameNumber);
1831
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001832 if (!gotAllStates) return false;
1833
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001834 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001835 "AF state %d, AE state %d, AWB state %d, "
1836 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001837 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001838 afMode, awbMode,
1839 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001840 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001841
1842 // Got all states, so construct a minimal result to send
1843 // In addition to the above fields, this means adding in
1844 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001845 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001846 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001847
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001848 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001849
1850 Mutex::Autolock l(mOutputLock);
1851
Jianing Weicb0652e2014-03-12 18:29:36 -07001852 CaptureResult captureResult;
1853 captureResult.mResultExtras = resultExtras;
1854 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1855 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1856 // but not limited to CameraDeviceBase::getNextResult
1857 CaptureResult& min3AResult =
1858 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001859
Jianing Weicb0652e2014-03-12 18:29:36 -07001860 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1861 // TODO: This is problematic casting. Need to fix CameraMetadata.
1862 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001863 return false;
1864 }
1865
Jianing Weicb0652e2014-03-12 18:29:36 -07001866 int32_t requestId = resultExtras.requestId;
1867 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001868 &requestId, frameNumber)) {
1869 return false;
1870 }
1871
Zhijun He204e3292014-07-14 17:09:23 -07001872 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1873 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1874 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1875 &partialResult, frameNumber)) {
1876 return false;
1877 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001878 }
1879
Jianing Weicb0652e2014-03-12 18:29:36 -07001880 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001881 &afMode, frameNumber)) {
1882 return false;
1883 }
1884
Jianing Weicb0652e2014-03-12 18:29:36 -07001885 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001886 &awbMode, frameNumber)) {
1887 return false;
1888 }
1889
Jianing Weicb0652e2014-03-12 18:29:36 -07001890 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001891 &aeState, frameNumber)) {
1892 return false;
1893 }
1894
Jianing Weicb0652e2014-03-12 18:29:36 -07001895 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001896 &afState, frameNumber)) {
1897 return false;
1898 }
1899
Jianing Weicb0652e2014-03-12 18:29:36 -07001900 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001901 &awbState, frameNumber)) {
1902 return false;
1903 }
1904
Jianing Weicb0652e2014-03-12 18:29:36 -07001905 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001906 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001907 return false;
1908 }
1909
Jianing Weicb0652e2014-03-12 18:29:36 -07001910 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001911 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001912 return false;
1913 }
1914
Zhijun He204e3292014-07-14 17:09:23 -07001915 // We only send the aggregated partial when all 3A related metadata are available
1916 // For both API1 and API2.
1917 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001918 mResultSignal.signal();
1919
1920 return true;
1921}
1922
1923template<typename T>
1924bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001925 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001926 (void) frameNumber;
1927
1928 camera_metadata_ro_entry_t entry;
1929
1930 entry = result.find(tag);
1931 if (entry.count == 0) {
1932 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1933 mId, frameNumber, get_camera_metadata_tag_name(tag));
1934 return false;
1935 }
1936
1937 if (sizeof(T) == sizeof(uint8_t)) {
1938 *value = entry.data.u8[0];
1939 } else if (sizeof(T) == sizeof(int32_t)) {
1940 *value = entry.data.i32[0];
1941 } else {
1942 ALOGE("%s: Unexpected type", __FUNCTION__);
1943 return false;
1944 }
1945 return true;
1946}
1947
1948template<typename T>
1949bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001950 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001951 if (result.update(tag, value, 1) != NO_ERROR) {
1952 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1953 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1954 frameNumber, get_camera_metadata_tag_name(tag));
1955 return false;
1956 }
1957 return true;
1958}
1959
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08001960void Camera3Device::returnOutputBuffers(
1961 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
1962 nsecs_t timestamp) {
1963 for (size_t i = 0; i < numBuffers; i++)
1964 {
1965 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
1966 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
1967 // Note: stream may be deallocated at this point, if this buffer was
1968 // the last reference to it.
1969 if (res != OK) {
1970 ALOGE("Can't return buffer to its stream: %s (%d)",
1971 strerror(-res), res);
1972 }
1973 }
1974}
1975
1976
1977void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
1978
1979 const InFlightRequest &request = mInFlightMap.valueAt(idx);
1980 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
1981
1982 nsecs_t sensorTimestamp = request.sensorTimestamp;
1983 nsecs_t shutterTimestamp = request.shutterTimestamp;
1984
1985 // Check if it's okay to remove the request from InFlightMap:
1986 // In the case of a successful request:
1987 // all input and output buffers, all result metadata, shutter callback
1988 // arrived.
1989 // In the case of a unsuccessful request:
1990 // all input and output buffers arrived.
1991 if (request.numBuffersLeft == 0 &&
1992 (request.requestStatus != OK ||
1993 (request.haveResultMetadata && shutterTimestamp != 0))) {
1994 ATRACE_ASYNC_END("frame capture", frameNumber);
1995
1996 // Sanity check - if sensor timestamp matches shutter timestamp
1997 if (request.requestStatus == OK &&
1998 sensorTimestamp != shutterTimestamp) {
1999 SET_ERR("sensor timestamp (%" PRId64
2000 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2001 sensorTimestamp, frameNumber, shutterTimestamp);
2002 }
2003
2004 // for an unsuccessful request, it may have pending output buffers to
2005 // return.
2006 assert(request.requestStatus != OK ||
2007 request.pendingOutputBuffers.size() == 0);
2008 returnOutputBuffers(request.pendingOutputBuffers.array(),
2009 request.pendingOutputBuffers.size(), 0);
2010
2011 mInFlightMap.removeItemsAt(idx, 1);
2012
2013 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2014 }
2015
2016 // Sanity check - if we have too many in-flight frames, something has
2017 // likely gone wrong
2018 if (mInFlightMap.size() > kInFlightWarnLimit) {
2019 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
2020 }
2021}
2022
2023
2024void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2025 CaptureResultExtras &resultExtras,
2026 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002027 uint32_t frameNumber,
2028 bool reprocess) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002029 if (pendingMetadata.isEmpty())
2030 return;
2031
2032 Mutex::Autolock l(mOutputLock);
2033
2034 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002035 if (reprocess) {
2036 if (frameNumber < mNextReprocessResultFrameNumber) {
2037 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002038 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002039 frameNumber, mNextReprocessResultFrameNumber);
2040 return;
2041 }
2042 mNextReprocessResultFrameNumber = frameNumber + 1;
2043 } else {
2044 if (frameNumber < mNextResultFrameNumber) {
2045 SET_ERR("Out-of-order capture result metadata submitted! "
2046 "(got frame number %d, expecting %d)",
2047 frameNumber, mNextResultFrameNumber);
2048 return;
2049 }
2050 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002051 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002052
2053 CaptureResult captureResult;
2054 captureResult.mResultExtras = resultExtras;
2055 captureResult.mMetadata = pendingMetadata;
2056
2057 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2058 (int32_t*)&frameNumber, 1) != OK) {
2059 SET_ERR("Failed to set frame# in metadata (%d)",
2060 frameNumber);
2061 return;
2062 } else {
2063 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2064 __FUNCTION__, mId, frameNumber);
2065 }
2066
2067 // Append any previous partials to form a complete result
2068 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2069 captureResult.mMetadata.append(collectedPartialResult);
2070 }
2071
2072 captureResult.mMetadata.sort();
2073
2074 // Check that there's a timestamp in the result metadata
2075 camera_metadata_entry entry =
2076 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2077 if (entry.count == 0) {
2078 SET_ERR("No timestamp provided by HAL for frame %d!",
2079 frameNumber);
2080 return;
2081 }
2082
2083 // Valid result, insert into queue
2084 List<CaptureResult>::iterator queuedResult =
2085 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2086 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2087 ", burstId = %" PRId32, __FUNCTION__,
2088 queuedResult->mResultExtras.requestId,
2089 queuedResult->mResultExtras.frameNumber,
2090 queuedResult->mResultExtras.burstId);
2091
2092 mResultSignal.signal();
2093}
2094
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002095/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002096 * Camera HAL device callback methods
2097 */
2098
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002099void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002100 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002101
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002102 status_t res;
2103
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002104 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002105 if (result->result == NULL && result->num_output_buffers == 0 &&
2106 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002107 SET_ERR("No result data provided by HAL for frame %d",
2108 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002109 return;
2110 }
Zhijun He204e3292014-07-14 17:09:23 -07002111
2112 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2113 // partial_result to 1 when metadata is included in this result.
2114 if (!mUsePartialResult &&
2115 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2116 result->result != NULL &&
2117 result->partial_result != 1) {
2118 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2119 " if partial result is not supported",
2120 frameNumber, result->partial_result);
2121 return;
2122 }
2123
2124 bool isPartialResult = false;
2125 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002126 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002127 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002128
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002129 // Get shutter timestamp and resultExtras from list of in-flight requests,
2130 // where it was added by the shutter notification for this frame. If the
2131 // shutter timestamp isn't received yet, append the output buffers to the
2132 // in-flight request and they will be returned when the shutter timestamp
2133 // arrives. Update the in-flight status and remove the in-flight entry if
2134 // all result data and shutter timestamp have been received.
2135 nsecs_t shutterTimestamp = 0;
2136
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002137 {
2138 Mutex::Autolock l(mInFlightLock);
2139 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2140 if (idx == NAME_NOT_FOUND) {
2141 SET_ERR("Unknown frame number for capture result: %d",
2142 frameNumber);
2143 return;
2144 }
2145 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002146 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2147 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2148 ", partialResultCount = %d",
2149 __FUNCTION__, request.resultExtras.requestId,
2150 request.resultExtras.frameNumber, request.resultExtras.burstId,
2151 result->partial_result);
2152 // Always update the partial count to the latest one if it's not 0
2153 // (buffers only). When framework aggregates adjacent partial results
2154 // into one, the latest partial count will be used.
2155 if (result->partial_result != 0)
2156 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002157
2158 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002159 if (mUsePartialResult && result->result != NULL) {
2160 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2161 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2162 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2163 " the range of [1, %d] when metadata is included in the result",
2164 frameNumber, result->partial_result, mNumPartialResults);
2165 return;
2166 }
2167 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002168 if (isPartialResult) {
2169 request.partialResult.collectedResult.append(result->result);
2170 }
Zhijun He204e3292014-07-14 17:09:23 -07002171 } else {
2172 camera_metadata_ro_entry_t partialResultEntry;
2173 res = find_camera_metadata_ro_entry(result->result,
2174 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2175 if (res != NAME_NOT_FOUND &&
2176 partialResultEntry.count > 0 &&
2177 partialResultEntry.data.u8[0] ==
2178 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2179 // A partial result. Flag this as such, and collect this
2180 // set of metadata into the in-flight entry.
2181 isPartialResult = true;
2182 request.partialResult.collectedResult.append(
2183 result->result);
2184 request.partialResult.collectedResult.erase(
2185 ANDROID_QUIRKS_PARTIAL_RESULT);
2186 }
2187 }
2188
2189 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002190 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002191 if (!request.partialResult.haveSent3A) {
2192 request.partialResult.haveSent3A =
2193 processPartial3AResult(frameNumber,
2194 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002195 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002196 }
2197 }
2198 }
2199
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002200 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002201 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002202
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002203 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002204 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002205 if (request.haveResultMetadata) {
2206 SET_ERR("Called multiple times with metadata for frame %d",
2207 frameNumber);
2208 return;
2209 }
Zhijun He204e3292014-07-14 17:09:23 -07002210 if (mUsePartialResult &&
2211 !request.partialResult.collectedResult.isEmpty()) {
2212 collectedPartialResult.acquire(
2213 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002214 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002215 request.haveResultMetadata = true;
2216 }
2217
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002218 uint32_t numBuffersReturned = result->num_output_buffers;
2219 if (result->input_buffer != NULL) {
2220 if (hasInputBufferInRequest) {
2221 numBuffersReturned += 1;
2222 } else {
2223 ALOGW("%s: Input buffer should be NULL if there is no input"
2224 " buffer sent in the request",
2225 __FUNCTION__);
2226 }
2227 }
2228 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002229 if (request.numBuffersLeft < 0) {
2230 SET_ERR("Too many buffers returned for frame %d",
2231 frameNumber);
2232 return;
2233 }
2234
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002235 camera_metadata_ro_entry_t entry;
2236 res = find_camera_metadata_ro_entry(result->result,
2237 ANDROID_SENSOR_TIMESTAMP, &entry);
2238 if (res == OK && entry.count == 1) {
2239 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002240 }
2241
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002242 // If shutter event isn't received yet, append the output buffers to
2243 // the in-flight request. Otherwise, return the output buffers to
2244 // streams.
2245 if (shutterTimestamp == 0) {
2246 request.pendingOutputBuffers.appendArray(result->output_buffers,
2247 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002248 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002249 returnOutputBuffers(result->output_buffers,
2250 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002251 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002252
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002253 if (result->result != NULL && !isPartialResult) {
2254 if (shutterTimestamp == 0) {
2255 request.pendingMetadata = result->result;
2256 request.partialResult.collectedResult = collectedPartialResult;
2257 } else {
2258 CameraMetadata metadata;
2259 metadata = result->result;
2260 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002261 collectedPartialResult, frameNumber, hasInputBufferInRequest);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002262 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002263 }
2264
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002265 removeInFlightRequestIfReadyLocked(idx);
2266 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002267
Zhijun Hef0d962a2014-06-30 10:24:11 -07002268 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002269 if (hasInputBufferInRequest) {
2270 Camera3Stream *stream =
2271 Camera3Stream::cast(result->input_buffer->stream);
2272 res = stream->returnInputBuffer(*(result->input_buffer));
2273 // Note: stream may be deallocated at this point, if this buffer was the
2274 // last reference to it.
2275 if (res != OK) {
2276 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2277 " its stream:%s (%d)", __FUNCTION__,
2278 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002279 }
2280 } else {
2281 ALOGW("%s: Input buffer should be NULL if there is no input"
2282 " buffer sent in the request, skipping input buffer return.",
2283 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002284 }
2285 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002286}
2287
2288void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002289 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002290 NotificationListener *listener;
2291 {
2292 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002293 listener = mListener;
2294 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002295
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002296 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002297 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002298 return;
2299 }
2300
2301 switch (msg->type) {
2302 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002303 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002304 break;
2305 }
2306 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002307 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002308 break;
2309 }
2310 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002311 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002312 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002313 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002314}
2315
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002316void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2317 NotificationListener *listener) {
2318
2319 // Map camera HAL error codes to ICameraDeviceCallback error codes
2320 // Index into this with the HAL error code
2321 static const ICameraDeviceCallbacks::CameraErrorCode
2322 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2323 // 0 = Unused error code
2324 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2325 // 1 = CAMERA3_MSG_ERROR_DEVICE
2326 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2327 // 2 = CAMERA3_MSG_ERROR_REQUEST
2328 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2329 // 3 = CAMERA3_MSG_ERROR_RESULT
2330 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2331 // 4 = CAMERA3_MSG_ERROR_BUFFER
2332 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2333 };
2334
2335 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2336 ((msg.error_code >= 0) &&
2337 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2338 halErrorMap[msg.error_code] :
2339 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2340
2341 int streamId = 0;
2342 if (msg.error_stream != NULL) {
2343 Camera3Stream *stream =
2344 Camera3Stream::cast(msg.error_stream);
2345 streamId = stream->getId();
2346 }
2347 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2348 mId, __FUNCTION__, msg.frame_number,
2349 streamId, msg.error_code);
2350
2351 CaptureResultExtras resultExtras;
2352 switch (errorCode) {
2353 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2354 // SET_ERR calls notifyError
2355 SET_ERR("Camera HAL reported serious device error");
2356 break;
2357 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2358 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2359 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2360 {
2361 Mutex::Autolock l(mInFlightLock);
2362 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2363 if (idx >= 0) {
2364 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2365 r.requestStatus = msg.error_code;
2366 resultExtras = r.resultExtras;
2367 } else {
2368 resultExtras.frameNumber = msg.frame_number;
2369 ALOGE("Camera %d: %s: cannot find in-flight request on "
2370 "frame %" PRId64 " error", mId, __FUNCTION__,
2371 resultExtras.frameNumber);
2372 }
2373 }
2374 if (listener != NULL) {
2375 listener->notifyError(errorCode, resultExtras);
2376 } else {
2377 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2378 }
2379 break;
2380 default:
2381 // SET_ERR calls notifyError
2382 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2383 break;
2384 }
2385}
2386
2387void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2388 NotificationListener *listener) {
2389 ssize_t idx;
2390 // Verify ordering of shutter notifications
2391 {
2392 Mutex::Autolock l(mOutputLock);
2393 // TODO: need to track errors for tighter bounds on expected frame number.
2394 if (msg.frame_number < mNextShutterFrameNumber) {
2395 SET_ERR("Shutter notification out-of-order. Expected "
2396 "notification for frame %d, got frame %d",
2397 mNextShutterFrameNumber, msg.frame_number);
2398 return;
2399 }
2400 mNextShutterFrameNumber = msg.frame_number + 1;
2401 }
2402
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002403 // Set timestamp for the request in the in-flight tracking
2404 // and get the request ID to send upstream
2405 {
2406 Mutex::Autolock l(mInFlightLock);
2407 idx = mInFlightMap.indexOfKey(msg.frame_number);
2408 if (idx >= 0) {
2409 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002410
2411 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2412 mId, __FUNCTION__,
2413 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2414 // Call listener, if any
2415 if (listener != NULL) {
2416 listener->notifyShutter(r.resultExtras, msg.timestamp);
2417 }
2418
2419 r.shutterTimestamp = msg.timestamp;
2420
2421 // send pending result and buffers
2422 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002423 r.partialResult.collectedResult, msg.frame_number,
2424 r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002425 returnOutputBuffers(r.pendingOutputBuffers.array(),
2426 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2427 r.pendingOutputBuffers.clear();
2428
2429 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002430 }
2431 }
2432 if (idx < 0) {
2433 SET_ERR("Shutter notification for non-existent frame number %d",
2434 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002435 }
2436}
2437
2438
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002439CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002440 ALOGV("%s", __FUNCTION__);
2441
Igor Murashkin1e479c02013-09-06 16:55:14 -07002442 CameraMetadata retVal;
2443
2444 if (mRequestThread != NULL) {
2445 retVal = mRequestThread->getLatestRequest();
2446 }
2447
Igor Murashkin1e479c02013-09-06 16:55:14 -07002448 return retVal;
2449}
2450
Jianing Weicb0652e2014-03-12 18:29:36 -07002451
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002452/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002453 * RequestThread inner class methods
2454 */
2455
2456Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002457 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002458 camera3_device_t *hal3Device) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002459 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002460 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002461 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002462 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002463 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002464 mReconfigured(false),
2465 mDoPause(false),
2466 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002467 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002468 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002469 mCurrentAfTriggerId(0),
2470 mCurrentPreCaptureTriggerId(0),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002471 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002472 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002473}
2474
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002475void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002476 NotificationListener *listener) {
2477 Mutex::Autolock l(mRequestLock);
2478 mListener = listener;
2479}
2480
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002481void Camera3Device::RequestThread::configurationComplete() {
2482 Mutex::Autolock l(mRequestLock);
2483 mReconfigured = true;
2484}
2485
Jianing Wei90e59c92014-03-12 18:29:36 -07002486status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002487 List<sp<CaptureRequest> > &requests,
2488 /*out*/
2489 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002490 Mutex::Autolock l(mRequestLock);
2491 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2492 ++it) {
2493 mRequestQueue.push_back(*it);
2494 }
2495
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002496 if (lastFrameNumber != NULL) {
2497 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2498 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2499 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2500 *lastFrameNumber);
2501 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002502
Jianing Wei90e59c92014-03-12 18:29:36 -07002503 unpauseForNewRequests();
2504
2505 return OK;
2506}
2507
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002508
2509status_t Camera3Device::RequestThread::queueTrigger(
2510 RequestTrigger trigger[],
2511 size_t count) {
2512
2513 Mutex::Autolock l(mTriggerMutex);
2514 status_t ret;
2515
2516 for (size_t i = 0; i < count; ++i) {
2517 ret = queueTriggerLocked(trigger[i]);
2518
2519 if (ret != OK) {
2520 return ret;
2521 }
2522 }
2523
2524 return OK;
2525}
2526
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002527int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2528 sp<Camera3Device> d = device.promote();
2529 if (d != NULL) return d->mId;
2530 return 0;
2531}
2532
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002533status_t Camera3Device::RequestThread::queueTriggerLocked(
2534 RequestTrigger trigger) {
2535
2536 uint32_t tag = trigger.metadataTag;
2537 ssize_t index = mTriggerMap.indexOfKey(tag);
2538
2539 switch (trigger.getTagType()) {
2540 case TYPE_BYTE:
2541 // fall-through
2542 case TYPE_INT32:
2543 break;
2544 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002545 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2546 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002547 return INVALID_OPERATION;
2548 }
2549
2550 /**
2551 * Collect only the latest trigger, since we only have 1 field
2552 * in the request settings per trigger tag, and can't send more than 1
2553 * trigger per request.
2554 */
2555 if (index != NAME_NOT_FOUND) {
2556 mTriggerMap.editValueAt(index) = trigger;
2557 } else {
2558 mTriggerMap.add(tag, trigger);
2559 }
2560
2561 return OK;
2562}
2563
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002564status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002565 const RequestList &requests,
2566 /*out*/
2567 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002568 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002569 if (lastFrameNumber != NULL) {
2570 *lastFrameNumber = mRepeatingLastFrameNumber;
2571 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002572 mRepeatingRequests.clear();
2573 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2574 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002575
2576 unpauseForNewRequests();
2577
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002578 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002579 return OK;
2580}
2581
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002582bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2583 if (mRepeatingRequests.empty()) {
2584 return false;
2585 }
2586 int32_t requestId = requestIn->mResultExtras.requestId;
2587 const RequestList &repeatRequests = mRepeatingRequests;
2588 // All repeating requests are guaranteed to have same id so only check first quest
2589 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2590 return (firstRequest->mResultExtras.requestId == requestId);
2591}
2592
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002593status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002594 Mutex::Autolock l(mRequestLock);
2595 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002596 if (lastFrameNumber != NULL) {
2597 *lastFrameNumber = mRepeatingLastFrameNumber;
2598 }
2599 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002600 return OK;
2601}
2602
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002603status_t Camera3Device::RequestThread::clear(
2604 NotificationListener *listener,
2605 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002606 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002607 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002608
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002609 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002610
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002611 // Send errors for all requests pending in the request queue, including
2612 // pending repeating requests
2613 if (listener != NULL) {
2614 for (RequestList::iterator it = mRequestQueue.begin();
2615 it != mRequestQueue.end(); ++it) {
2616 // Set the frame number this request would have had, if it
2617 // had been submitted; this frame number will not be reused.
2618 // The requestId and burstId fields were set when the request was
2619 // submitted originally (in convertMetadataListToRequestListLocked)
2620 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2621 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2622 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002623 }
2624 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002625 mRequestQueue.clear();
2626 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002627 if (lastFrameNumber != NULL) {
2628 *lastFrameNumber = mRepeatingLastFrameNumber;
2629 }
2630 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002631 return OK;
2632}
2633
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002634void Camera3Device::RequestThread::setPaused(bool paused) {
2635 Mutex::Autolock l(mPauseLock);
2636 mDoPause = paused;
2637 mDoPauseSignal.signal();
2638}
2639
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002640status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2641 int32_t requestId, nsecs_t timeout) {
2642 Mutex::Autolock l(mLatestRequestMutex);
2643 status_t res;
2644 while (mLatestRequestId != requestId) {
2645 nsecs_t startTime = systemTime();
2646
2647 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2648 if (res != OK) return res;
2649
2650 timeout -= (systemTime() - startTime);
2651 }
2652
2653 return OK;
2654}
2655
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002656void Camera3Device::RequestThread::requestExit() {
2657 // Call parent to set up shutdown
2658 Thread::requestExit();
2659 // The exit from any possible waits
2660 mDoPauseSignal.signal();
2661 mRequestSignal.signal();
2662}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002663
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002664bool Camera3Device::RequestThread::threadLoop() {
2665
2666 status_t res;
2667
2668 // Handle paused state.
2669 if (waitIfPaused()) {
2670 return true;
2671 }
2672
2673 // Get work to do
2674
2675 sp<CaptureRequest> nextRequest = waitForNextRequest();
2676 if (nextRequest == NULL) {
2677 return true;
2678 }
2679
2680 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002681 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002682 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002683 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002684
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002685 // Get the request ID, if any
2686 int requestId;
2687 camera_metadata_entry_t requestIdEntry =
2688 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2689 if (requestIdEntry.count > 0) {
2690 requestId = requestIdEntry.data.i32[0];
2691 } else {
2692 ALOGW("%s: Did not have android.request.id set in the request",
2693 __FUNCTION__);
2694 requestId = NAME_NOT_FOUND;
2695 }
2696
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002697 // Insert any queued triggers (before metadata is locked)
2698 int32_t triggerCount;
2699 res = insertTriggers(nextRequest);
2700 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002701 SET_ERR("RequestThread: Unable to insert triggers "
2702 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002703 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002704 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2705 return false;
2706 }
2707 triggerCount = res;
2708
2709 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2710
2711 // If the request is the same as last, or we had triggers last time
2712 if (mPrevRequest != nextRequest || triggersMixedIn) {
2713 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002714 * HAL workaround:
2715 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2716 */
2717 res = addDummyTriggerIds(nextRequest);
2718 if (res != OK) {
2719 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2720 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002721 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002722 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2723 return false;
2724 }
2725
2726 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002727 * The request should be presorted so accesses in HAL
2728 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2729 */
2730 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002731 request.settings = nextRequest->mSettings.getAndLock();
2732 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002733 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2734
2735 IF_ALOGV() {
2736 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2737 find_camera_metadata_ro_entry(
2738 request.settings,
2739 ANDROID_CONTROL_AF_TRIGGER,
2740 &e
2741 );
2742 if (e.count > 0) {
2743 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2744 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002745 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002746 e.data.u8[0]);
2747 }
2748 }
2749 } else {
2750 // leave request.settings NULL to indicate 'reuse latest given'
2751 ALOGVV("%s: Request settings are REUSED",
2752 __FUNCTION__);
2753 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002754
2755 camera3_stream_buffer_t inputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002756 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002757
2758 // Fill in buffers
2759
2760 if (nextRequest->mInputStream != NULL) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002761 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002762 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002763 // Can't get input buffer from gralloc queue - this could be due to
2764 // disconnected queue or other producer misbehavior, so not a fatal
2765 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002766 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002767 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala0ec23d32015-05-28 15:15:54 -07002768 {
2769 Mutex::Autolock l(mRequestLock);
2770 if (mListener != NULL) {
2771 mListener->notifyError(
2772 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2773 nextRequest->mResultExtras);
2774 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002775 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002776 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2777 return true;
2778 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002779 request.input_buffer = &inputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002780 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002781 } else {
2782 request.input_buffer = NULL;
2783 }
2784
2785 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2786 nextRequest->mOutputStreams.size());
2787 request.output_buffers = outputBuffers.array();
2788 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2789 res = nextRequest->mOutputStreams.editItemAt(i)->
2790 getBuffer(&outputBuffers.editItemAt(i));
2791 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002792 // Can't get output buffer from gralloc queue - this could be due to
2793 // abandoned queue or other consumer misbehavior, so not a fatal
2794 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002795 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2796 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala0ec23d32015-05-28 15:15:54 -07002797 {
2798 Mutex::Autolock l(mRequestLock);
2799 if (mListener != NULL) {
2800 mListener->notifyError(
2801 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2802 nextRequest->mResultExtras);
2803 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002804 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002805 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2806 return true;
2807 }
2808 request.num_output_buffers++;
2809 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002810 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002811
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002812 // Log request in the in-flight queue
2813 sp<Camera3Device> parent = mParent.promote();
2814 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002815 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002816 CLOGE("RequestThread: Parent is gone");
2817 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2818 return false;
2819 }
2820
Jianing Weicb0652e2014-03-12 18:29:36 -07002821 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002822 totalNumBuffers, nextRequest->mResultExtras,
2823 /*hasInput*/request.input_buffer != NULL);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002824 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2825 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002826 __FUNCTION__,
2827 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2828 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002829 if (res != OK) {
2830 SET_ERR("RequestThread: Unable to register new in-flight request:"
2831 " %s (%d)", strerror(-res), res);
2832 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2833 return false;
2834 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002835
Zhijun Hecc27e112013-10-03 16:12:43 -07002836 // Inform waitUntilRequestProcessed thread of a new request ID
2837 {
2838 Mutex::Autolock al(mLatestRequestMutex);
2839
2840 mLatestRequestId = requestId;
2841 mLatestRequestSignal.signal();
2842 }
2843
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002844 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002845 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2846 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002847 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002848 ATRACE_END();
2849
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002850 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002851 // Should only get a failure here for malformed requests or device-level
2852 // errors, so consider all errors fatal. Bad metadata failures should
2853 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002854 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002855 " device: %s (%d)", request.frame_number, strerror(-res), res);
2856 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2857 return false;
2858 }
2859
Igor Murashkin1e479c02013-09-06 16:55:14 -07002860 // Update the latest request sent to HAL
2861 if (request.settings != NULL) { // Don't update them if they were unchanged
2862 Mutex::Autolock al(mLatestRequestMutex);
2863
2864 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2865 mLatestRequest.acquire(cloned);
2866 }
2867
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002868 if (request.settings != NULL) {
2869 nextRequest->mSettings.unlock(request.settings);
2870 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002871
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002872 // Unset as current request
2873 {
2874 Mutex::Autolock l(mRequestLock);
2875 mNextRequest.clear();
2876 }
2877
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002878 // Remove any previously queued triggers (after unlock)
2879 res = removeTriggers(mPrevRequest);
2880 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002881 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002882 "(capture request %d, HAL device: %s (%d)",
2883 request.frame_number, strerror(-res), res);
2884 return false;
2885 }
2886 mPrevTriggers = triggerCount;
2887
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002888 return true;
2889}
2890
Igor Murashkin1e479c02013-09-06 16:55:14 -07002891CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2892 Mutex::Autolock al(mLatestRequestMutex);
2893
2894 ALOGV("RequestThread::%s", __FUNCTION__);
2895
2896 return mLatestRequest;
2897}
2898
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002899bool Camera3Device::RequestThread::isStreamPending(
2900 sp<Camera3StreamInterface>& stream) {
2901 Mutex::Autolock l(mRequestLock);
2902
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002903 if (mNextRequest != nullptr) {
2904 for (const auto& s : mNextRequest->mOutputStreams) {
2905 if (stream == s) return true;
2906 }
2907 if (stream == mNextRequest->mInputStream) return true;
2908 }
2909
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002910 for (const auto& request : mRequestQueue) {
2911 for (const auto& s : request->mOutputStreams) {
2912 if (stream == s) return true;
2913 }
2914 if (stream == request->mInputStream) return true;
2915 }
2916
2917 for (const auto& request : mRepeatingRequests) {
2918 for (const auto& s : request->mOutputStreams) {
2919 if (stream == s) return true;
2920 }
2921 if (stream == request->mInputStream) return true;
2922 }
2923
2924 return false;
2925}
Jianing Weicb0652e2014-03-12 18:29:36 -07002926
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002927void Camera3Device::RequestThread::cleanUpFailedRequest(
2928 camera3_capture_request_t &request,
2929 sp<CaptureRequest> &nextRequest,
2930 Vector<camera3_stream_buffer_t> &outputBuffers) {
2931
2932 if (request.settings != NULL) {
2933 nextRequest->mSettings.unlock(request.settings);
2934 }
2935 if (request.input_buffer != NULL) {
2936 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002937 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002938 }
2939 for (size_t i = 0; i < request.num_output_buffers; i++) {
2940 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2941 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2942 outputBuffers[i], 0);
2943 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002944
2945 Mutex::Autolock l(mRequestLock);
2946 mNextRequest.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002947}
2948
2949sp<Camera3Device::CaptureRequest>
2950 Camera3Device::RequestThread::waitForNextRequest() {
2951 status_t res;
2952 sp<CaptureRequest> nextRequest;
2953
2954 // Optimized a bit for the simple steady-state case (single repeating
2955 // request), to avoid putting that request in the queue temporarily.
2956 Mutex::Autolock l(mRequestLock);
2957
2958 while (mRequestQueue.empty()) {
2959 if (!mRepeatingRequests.empty()) {
2960 // Always atomically enqueue all requests in a repeating request
2961 // list. Guarantees a complete in-sequence set of captures to
2962 // application.
2963 const RequestList &requests = mRepeatingRequests;
2964 RequestList::const_iterator firstRequest =
2965 requests.begin();
2966 nextRequest = *firstRequest;
2967 mRequestQueue.insert(mRequestQueue.end(),
2968 ++firstRequest,
2969 requests.end());
2970 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002971
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002972 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002973
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002974 break;
2975 }
2976
2977 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2978
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002979 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2980 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002981 Mutex::Autolock pl(mPauseLock);
2982 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002983 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002984 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002985 // Let the tracker know
2986 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2987 if (statusTracker != 0) {
2988 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2989 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002990 }
2991 // Stop waiting for now and let thread management happen
2992 return NULL;
2993 }
2994 }
2995
2996 if (nextRequest == NULL) {
2997 // Don't have a repeating request already in hand, so queue
2998 // must have an entry now.
2999 RequestList::iterator firstRequest =
3000 mRequestQueue.begin();
3001 nextRequest = *firstRequest;
3002 mRequestQueue.erase(firstRequest);
3003 }
3004
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003005 // In case we've been unpaused by setPaused clearing mDoPause, need to
3006 // update internal pause state (capture/setRepeatingRequest unpause
3007 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003008 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003009 if (mPaused) {
3010 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3011 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3012 if (statusTracker != 0) {
3013 statusTracker->markComponentActive(mStatusId);
3014 }
3015 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003016 mPaused = false;
3017
3018 // Check if we've reconfigured since last time, and reset the preview
3019 // request if so. Can't use 'NULL request == repeat' across configure calls.
3020 if (mReconfigured) {
3021 mPrevRequest.clear();
3022 mReconfigured = false;
3023 }
3024
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003025 if (nextRequest != NULL) {
3026 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003027 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3028 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003029 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003030 mNextRequest = nextRequest;
3031
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003032 return nextRequest;
3033}
3034
3035bool Camera3Device::RequestThread::waitIfPaused() {
3036 status_t res;
3037 Mutex::Autolock l(mPauseLock);
3038 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003039 if (mPaused == false) {
3040 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003041 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3042 // Let the tracker know
3043 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3044 if (statusTracker != 0) {
3045 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3046 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003047 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003048
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003049 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003050 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003051 return true;
3052 }
3053 }
3054 // We don't set mPaused to false here, because waitForNextRequest needs
3055 // to further manage the paused state in case of starvation.
3056 return false;
3057}
3058
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003059void Camera3Device::RequestThread::unpauseForNewRequests() {
3060 // With work to do, mark thread as unpaused.
3061 // If paused by request (setPaused), don't resume, to avoid
3062 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003063 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003064 Mutex::Autolock p(mPauseLock);
3065 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003066 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3067 if (mPaused) {
3068 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3069 if (statusTracker != 0) {
3070 statusTracker->markComponentActive(mStatusId);
3071 }
3072 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003073 mPaused = false;
3074 }
3075}
3076
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003077void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3078 sp<Camera3Device> parent = mParent.promote();
3079 if (parent != NULL) {
3080 va_list args;
3081 va_start(args, fmt);
3082
3083 parent->setErrorStateV(fmt, args);
3084
3085 va_end(args);
3086 }
3087}
3088
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003089status_t Camera3Device::RequestThread::insertTriggers(
3090 const sp<CaptureRequest> &request) {
3091
3092 Mutex::Autolock al(mTriggerMutex);
3093
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003094 sp<Camera3Device> parent = mParent.promote();
3095 if (parent == NULL) {
3096 CLOGE("RequestThread: Parent is gone");
3097 return DEAD_OBJECT;
3098 }
3099
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003100 CameraMetadata &metadata = request->mSettings;
3101 size_t count = mTriggerMap.size();
3102
3103 for (size_t i = 0; i < count; ++i) {
3104 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003105 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003106
3107 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3108 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3109 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003110 if (isAeTrigger) {
3111 request->mResultExtras.precaptureTriggerId = triggerId;
3112 mCurrentPreCaptureTriggerId = triggerId;
3113 } else {
3114 request->mResultExtras.afTriggerId = triggerId;
3115 mCurrentAfTriggerId = triggerId;
3116 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003117 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3118 continue; // Trigger ID tag is deprecated since device HAL 3.2
3119 }
3120 }
3121
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003122 camera_metadata_entry entry = metadata.find(tag);
3123
3124 if (entry.count > 0) {
3125 /**
3126 * Already has an entry for this trigger in the request.
3127 * Rewrite it with our requested trigger value.
3128 */
3129 RequestTrigger oldTrigger = trigger;
3130
3131 oldTrigger.entryValue = entry.data.u8[0];
3132
3133 mTriggerReplacedMap.add(tag, oldTrigger);
3134 } else {
3135 /**
3136 * More typical, no trigger entry, so we just add it
3137 */
3138 mTriggerRemovedMap.add(tag, trigger);
3139 }
3140
3141 status_t res;
3142
3143 switch (trigger.getTagType()) {
3144 case TYPE_BYTE: {
3145 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3146 res = metadata.update(tag,
3147 &entryValue,
3148 /*count*/1);
3149 break;
3150 }
3151 case TYPE_INT32:
3152 res = metadata.update(tag,
3153 &trigger.entryValue,
3154 /*count*/1);
3155 break;
3156 default:
3157 ALOGE("%s: Type not supported: 0x%x",
3158 __FUNCTION__,
3159 trigger.getTagType());
3160 return INVALID_OPERATION;
3161 }
3162
3163 if (res != OK) {
3164 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3165 ", value %d", __FUNCTION__, trigger.getTagName(),
3166 trigger.entryValue);
3167 return res;
3168 }
3169
3170 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3171 trigger.getTagName(),
3172 trigger.entryValue);
3173 }
3174
3175 mTriggerMap.clear();
3176
3177 return count;
3178}
3179
3180status_t Camera3Device::RequestThread::removeTriggers(
3181 const sp<CaptureRequest> &request) {
3182 Mutex::Autolock al(mTriggerMutex);
3183
3184 CameraMetadata &metadata = request->mSettings;
3185
3186 /**
3187 * Replace all old entries with their old values.
3188 */
3189 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3190 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3191
3192 status_t res;
3193
3194 uint32_t tag = trigger.metadataTag;
3195 switch (trigger.getTagType()) {
3196 case TYPE_BYTE: {
3197 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3198 res = metadata.update(tag,
3199 &entryValue,
3200 /*count*/1);
3201 break;
3202 }
3203 case TYPE_INT32:
3204 res = metadata.update(tag,
3205 &trigger.entryValue,
3206 /*count*/1);
3207 break;
3208 default:
3209 ALOGE("%s: Type not supported: 0x%x",
3210 __FUNCTION__,
3211 trigger.getTagType());
3212 return INVALID_OPERATION;
3213 }
3214
3215 if (res != OK) {
3216 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3217 ", trigger value %d", __FUNCTION__,
3218 trigger.getTagName(), trigger.entryValue);
3219 return res;
3220 }
3221 }
3222 mTriggerReplacedMap.clear();
3223
3224 /**
3225 * Remove all new entries.
3226 */
3227 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3228 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3229 status_t res = metadata.erase(trigger.metadataTag);
3230
3231 if (res != OK) {
3232 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3233 ", trigger value %d", __FUNCTION__,
3234 trigger.getTagName(), trigger.entryValue);
3235 return res;
3236 }
3237 }
3238 mTriggerRemovedMap.clear();
3239
3240 return OK;
3241}
3242
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003243status_t Camera3Device::RequestThread::addDummyTriggerIds(
3244 const sp<CaptureRequest> &request) {
3245 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3246 static const int32_t dummyTriggerId = 1;
3247 status_t res;
3248
3249 CameraMetadata &metadata = request->mSettings;
3250
3251 // If AF trigger is active, insert a dummy AF trigger ID if none already
3252 // exists
3253 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3254 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3255 if (afTrigger.count > 0 &&
3256 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3257 afId.count == 0) {
3258 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3259 if (res != OK) return res;
3260 }
3261
3262 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3263 // if none already exists
3264 camera_metadata_entry pcTrigger =
3265 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3266 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3267 if (pcTrigger.count > 0 &&
3268 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3269 pcId.count == 0) {
3270 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3271 &dummyTriggerId, 1);
3272 if (res != OK) return res;
3273 }
3274
3275 return OK;
3276}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003277
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003278/**
3279 * PreparerThread inner class methods
3280 */
3281
3282Camera3Device::PreparerThread::PreparerThread() :
3283 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3284}
3285
3286Camera3Device::PreparerThread::~PreparerThread() {
3287 Thread::requestExitAndWait();
3288 if (mCurrentStream != nullptr) {
3289 mCurrentStream->cancelPrepare();
3290 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3291 mCurrentStream.clear();
3292 }
3293 clear();
3294}
3295
3296status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) {
3297 status_t res;
3298
3299 Mutex::Autolock l(mLock);
3300
3301 res = stream->startPrepare();
3302 if (res == OK) {
3303 // No preparation needed, fire listener right off
3304 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3305 if (mListener) {
3306 mListener->notifyPrepared(stream->getId());
3307 }
3308 return OK;
3309 } else if (res != NOT_ENOUGH_DATA) {
3310 return res;
3311 }
3312
3313 // Need to prepare, start up thread if necessary
3314 if (!mActive) {
3315 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3316 // isn't running
3317 Thread::requestExitAndWait();
3318 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3319 if (res != OK) {
3320 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3321 if (mListener) {
3322 mListener->notifyPrepared(stream->getId());
3323 }
3324 return res;
3325 }
3326 mCancelNow = false;
3327 mActive = true;
3328 ALOGV("%s: Preparer stream started", __FUNCTION__);
3329 }
3330
3331 // queue up the work
3332 mPendingStreams.push_back(stream);
3333 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3334
3335 return OK;
3336}
3337
3338status_t Camera3Device::PreparerThread::clear() {
3339 status_t res;
3340
3341 Mutex::Autolock l(mLock);
3342
3343 for (const auto& stream : mPendingStreams) {
3344 stream->cancelPrepare();
3345 }
3346 mPendingStreams.clear();
3347 mCancelNow = true;
3348
3349 return OK;
3350}
3351
3352void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3353 Mutex::Autolock l(mLock);
3354 mListener = listener;
3355}
3356
3357bool Camera3Device::PreparerThread::threadLoop() {
3358 status_t res;
3359 {
3360 Mutex::Autolock l(mLock);
3361 if (mCurrentStream == nullptr) {
3362 // End thread if done with work
3363 if (mPendingStreams.empty()) {
3364 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3365 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3366 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3367 mActive = false;
3368 return false;
3369 }
3370
3371 // Get next stream to prepare
3372 auto it = mPendingStreams.begin();
3373 mCurrentStream = *it;
3374 mPendingStreams.erase(it);
3375 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3376 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3377 } else if (mCancelNow) {
3378 mCurrentStream->cancelPrepare();
3379 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3380 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3381 mCurrentStream.clear();
3382 mCancelNow = false;
3383 return true;
3384 }
3385 }
3386
3387 res = mCurrentStream->prepareNextBuffer();
3388 if (res == NOT_ENOUGH_DATA) return true;
3389 if (res != OK) {
3390 // Something bad happened; try to recover by cancelling prepare and
3391 // signalling listener anyway
3392 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3393 mCurrentStream->getId(), res, strerror(-res));
3394 mCurrentStream->cancelPrepare();
3395 }
3396
3397 // This stream has finished, notify listener
3398 Mutex::Autolock l(mLock);
3399 if (mListener) {
3400 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3401 mCurrentStream->getId());
3402 mListener->notifyPrepared(mCurrentStream->getId());
3403 }
3404
3405 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3406 mCurrentStream.clear();
3407
3408 return true;
3409}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003410
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003411/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003412 * Static callback forwarding methods from HAL to instance
3413 */
3414
3415void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3416 const camera3_capture_result *result) {
3417 Camera3Device *d =
3418 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3419 d->processCaptureResult(result);
3420}
3421
3422void Camera3Device::sNotify(const camera3_callback_ops *cb,
3423 const camera3_notify_msg *msg) {
3424 Camera3Device *d =
3425 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3426 d->notify(msg);
3427}
3428
3429}; // namespace android