blob: dc752a6c750db35d02b8eff760511702381a7e9e [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
178 /** Everything is good to go */
179
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700180 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800181 mDeviceInfo = info.static_camera_characteristics;
182 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700183 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800184 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700185 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700186 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700187 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800188
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700189 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700190 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
191 camera_metadata_entry partialResultsCount =
192 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
193 if (partialResultsCount.count > 0) {
194 mNumPartialResults = partialResultsCount.data.i32[0];
195 mUsePartialResult = (mNumPartialResults > 1);
196 }
197 } else {
198 camera_metadata_entry partialResultsQuirk =
199 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
200 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
201 mUsePartialResult = true;
202 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700203 }
204
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700205 camera_metadata_entry configs =
206 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
207 for (uint32_t i = 0; i < configs.count; i += 4) {
208 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
209 configs.data.i32[i + 3] ==
210 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
211 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
212 configs.data.i32[i + 2]));
213 }
214 }
215
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800216 return OK;
217}
218
219status_t Camera3Device::disconnect() {
220 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700221 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800222
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800223 ALOGV("%s: E", __FUNCTION__);
224
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700225 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800226
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700227 {
228 Mutex::Autolock l(mLock);
229 if (mStatus == STATUS_UNINITIALIZED) return res;
230
231 if (mStatus == STATUS_ACTIVE ||
232 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
233 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700234 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700235 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700236 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700237 } else {
238 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
239 if (res != OK) {
240 SET_ERR_L("Timeout waiting for HAL to drain");
241 // Continue to close device even in case of error
242 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700243 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800244 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800245
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700246 if (mStatus == STATUS_ERROR) {
247 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700248 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700249
250 if (mStatusTracker != NULL) {
251 mStatusTracker->requestExit();
252 }
253
254 if (mRequestThread != NULL) {
255 mRequestThread->requestExit();
256 }
257
258 mOutputStreams.clear();
259 mInputStream.clear();
260 }
261
262 // Joining done without holding mLock, otherwise deadlocks may ensue
263 // as the threads try to access parent state
264 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
265 // HAL may be in a bad state, so waiting for request thread
266 // (which may be stuck in the HAL processCaptureRequest call)
267 // could be dangerous.
268 mRequestThread->join();
269 }
270
271 if (mStatusTracker != NULL) {
272 mStatusTracker->join();
273 }
274
275 {
276 Mutex::Autolock l(mLock);
277
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800278 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700279 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800280
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700281 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800282 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700283 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800284 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700285 mHal3Device = NULL;
286 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800287
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700288 mStatus = STATUS_UNINITIALIZED;
289 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800290
291 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700292 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800293}
294
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700295// For dumping/debugging only -
296// try to acquire a lock a few times, eventually give up to proceed with
297// debug/dump operations
298bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
299 bool gotLock = false;
300 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
301 if (lock.tryLock() == NO_ERROR) {
302 gotLock = true;
303 break;
304 } else {
305 usleep(kDumpSleepDuration);
306 }
307 }
308 return gotLock;
309}
310
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700311Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
312 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
313 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
314 const int STREAM_CONFIGURATION_SIZE = 4;
315 const int STREAM_FORMAT_OFFSET = 0;
316 const int STREAM_WIDTH_OFFSET = 1;
317 const int STREAM_HEIGHT_OFFSET = 2;
318 const int STREAM_IS_INPUT_OFFSET = 3;
319 camera_metadata_ro_entry_t availableStreamConfigs =
320 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
321 if (availableStreamConfigs.count == 0 ||
322 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
323 return Size(0, 0);
324 }
325
326 // Get max jpeg size (area-wise).
327 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
328 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
329 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
330 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
331 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
332 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
333 && format == HAL_PIXEL_FORMAT_BLOB &&
334 (width * height > maxJpegWidth * maxJpegHeight)) {
335 maxJpegWidth = width;
336 maxJpegHeight = height;
337 }
338 }
339 } else {
340 camera_metadata_ro_entry availableJpegSizes =
341 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
342 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
343 return Size(0, 0);
344 }
345
346 // Get max jpeg size (area-wise).
347 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
348 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
349 > (maxJpegWidth * maxJpegHeight)) {
350 maxJpegWidth = availableJpegSizes.data.i32[i];
351 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
352 }
353 }
354 }
355 return Size(maxJpegWidth, maxJpegHeight);
356}
357
Zhijun Hef7da0962014-04-24 13:27:56 -0700358ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700359 // Get max jpeg size (area-wise).
360 Size maxJpegResolution = getMaxJpegResolution();
361 if (maxJpegResolution.width == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700362 ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
363 __FUNCTION__, mId);
364 return BAD_VALUE;
365 }
366
Zhijun Hef7da0962014-04-24 13:27:56 -0700367 // Get max jpeg buffer size
368 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700369 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
370 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700371 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
372 return BAD_VALUE;
373 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700374 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800375 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700376
377 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700378 float scaleFactor = ((float) (width * height)) /
379 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800380 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
381 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700382 if (jpegBufferSize > maxJpegBufferSize) {
383 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700384 }
385
386 return jpegBufferSize;
387}
388
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800389status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
390 ATRACE_CALL();
391 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700392
393 // Try to lock, but continue in case of failure (to avoid blocking in
394 // deadlocks)
395 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
396 bool gotLock = tryLockSpinRightRound(mLock);
397
398 ALOGW_IF(!gotInterfaceLock,
399 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
400 mId, __FUNCTION__);
401 ALOGW_IF(!gotLock,
402 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
403 mId, __FUNCTION__);
404
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800405 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800406
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800407 const char *status =
408 mStatus == STATUS_ERROR ? "ERROR" :
409 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700410 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
411 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800412 mStatus == STATUS_ACTIVE ? "ACTIVE" :
413 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700414
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800415 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700416 if (mStatus == STATUS_ERROR) {
417 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
418 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800419 lines.appendFormat(" Stream configuration:\n");
420
421 if (mInputStream != NULL) {
422 write(fd, lines.string(), lines.size());
423 mInputStream->dump(fd, args);
424 } else {
425 lines.appendFormat(" No input stream.\n");
426 write(fd, lines.string(), lines.size());
427 }
428 for (size_t i = 0; i < mOutputStreams.size(); i++) {
429 mOutputStreams[i]->dump(fd,args);
430 }
431
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700432 lines = String8(" In-flight requests:\n");
433 if (mInFlightMap.size() == 0) {
434 lines.append(" None\n");
435 } else {
436 for (size_t i = 0; i < mInFlightMap.size(); i++) {
437 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700438 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700439 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800440 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700441 r.numBuffersLeft);
442 }
443 }
444 write(fd, lines.string(), lines.size());
445
Igor Murashkin1e479c02013-09-06 16:55:14 -0700446 {
447 lines = String8(" Last request sent:\n");
448 write(fd, lines.string(), lines.size());
449
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700450 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700451 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
452 }
453
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800454 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700455 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800456 write(fd, lines.string(), lines.size());
457 mHal3Device->ops->dump(mHal3Device, fd);
458 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800459
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700460 if (gotLock) mLock.unlock();
461 if (gotInterfaceLock) mInterfaceLock.unlock();
462
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800463 return OK;
464}
465
466const CameraMetadata& Camera3Device::info() const {
467 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800468 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
469 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700470 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800471 mStatus == STATUS_ERROR ?
472 "when in error state" : "before init");
473 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800474 return mDeviceInfo;
475}
476
Jianing Wei90e59c92014-03-12 18:29:36 -0700477status_t Camera3Device::checkStatusOkToCaptureLocked() {
478 switch (mStatus) {
479 case STATUS_ERROR:
480 CLOGE("Device has encountered a serious error");
481 return INVALID_OPERATION;
482 case STATUS_UNINITIALIZED:
483 CLOGE("Device not initialized");
484 return INVALID_OPERATION;
485 case STATUS_UNCONFIGURED:
486 case STATUS_CONFIGURED:
487 case STATUS_ACTIVE:
488 // OK
489 break;
490 default:
491 SET_ERR_L("Unexpected status: %d", mStatus);
492 return INVALID_OPERATION;
493 }
494 return OK;
495}
496
497status_t Camera3Device::convertMetadataListToRequestListLocked(
498 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
499 if (requestList == NULL) {
500 CLOGE("requestList cannot be NULL.");
501 return BAD_VALUE;
502 }
503
Jianing Weicb0652e2014-03-12 18:29:36 -0700504 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700505 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
506 it != metadataList.end(); ++it) {
507 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
508 if (newRequest == 0) {
509 CLOGE("Can't create capture request");
510 return BAD_VALUE;
511 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700512
513 // Setup burst Id and request Id
514 newRequest->mResultExtras.burstId = burstId++;
515 if (it->exists(ANDROID_REQUEST_ID)) {
516 if (it->find(ANDROID_REQUEST_ID).count == 0) {
517 CLOGE("RequestID entry exists; but must not be empty in metadata");
518 return BAD_VALUE;
519 }
520 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
521 } else {
522 CLOGE("RequestID does not exist in metadata");
523 return BAD_VALUE;
524 }
525
Jianing Wei90e59c92014-03-12 18:29:36 -0700526 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700527
528 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700529 }
530 return OK;
531}
532
Jianing Weicb0652e2014-03-12 18:29:36 -0700533status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800534 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800535
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700536 List<const CameraMetadata> requests;
537 requests.push_back(request);
538 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800539}
540
Jianing Wei90e59c92014-03-12 18:29:36 -0700541status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700542 const List<const CameraMetadata> &requests, bool repeating,
543 /*out*/
544 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700545 ATRACE_CALL();
546 Mutex::Autolock il(mInterfaceLock);
547 Mutex::Autolock l(mLock);
548
549 status_t res = checkStatusOkToCaptureLocked();
550 if (res != OK) {
551 // error logged by previous call
552 return res;
553 }
554
555 RequestList requestList;
556
557 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
558 if (res != OK) {
559 // error logged by previous call
560 return res;
561 }
562
563 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700564 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700565 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700566 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700567 }
568
569 if (res == OK) {
570 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
571 if (res != OK) {
572 SET_ERR_L("Can't transition to active in %f seconds!",
573 kActiveTimeout/1e9);
574 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700575 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
576 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700577 } else {
578 CLOGE("Cannot queue request. Impossible.");
579 return BAD_VALUE;
580 }
581
582 return res;
583}
584
Jianing Weicb0652e2014-03-12 18:29:36 -0700585status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
586 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700587 ATRACE_CALL();
588
Jianing Weicb0652e2014-03-12 18:29:36 -0700589 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700590}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800591
Jianing Weicb0652e2014-03-12 18:29:36 -0700592status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
593 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800594 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800595
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700596 List<const CameraMetadata> requests;
597 requests.push_back(request);
598 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800599}
600
Jianing Weicb0652e2014-03-12 18:29:36 -0700601status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
602 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700603 ATRACE_CALL();
604
Jianing Weicb0652e2014-03-12 18:29:36 -0700605 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700606}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800607
608sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
609 const CameraMetadata &request) {
610 status_t res;
611
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700612 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800613 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700614 // Stream configuration failed due to unsupported configuration.
615 // Device back to unconfigured state. Client might try other configuraitons
616 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
617 CLOGE("No streams configured");
618 return NULL;
619 }
620 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800621 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700622 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800623 return NULL;
624 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700625 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700626 if (mStatus == STATUS_UNCONFIGURED) {
627 CLOGE("No streams configured");
628 return NULL;
629 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800630 }
631
632 sp<CaptureRequest> newRequest = createCaptureRequest(request);
633 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800634}
635
Jianing Weicb0652e2014-03-12 18:29:36 -0700636status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800637 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700638 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800639 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800640
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800641 switch (mStatus) {
642 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700643 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800644 return INVALID_OPERATION;
645 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700646 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800647 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700648 case STATUS_UNCONFIGURED:
649 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800650 case STATUS_ACTIVE:
651 // OK
652 break;
653 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700654 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800655 return INVALID_OPERATION;
656 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700657 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700658
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700659 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800660}
661
662status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
663 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700664 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800665
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700666 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800667}
668
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700669status_t Camera3Device::createInputStream(
670 uint32_t width, uint32_t height, int format, int *id) {
671 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700672 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700673 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700674 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
675 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700676
677 status_t res;
678 bool wasActive = false;
679
680 switch (mStatus) {
681 case STATUS_ERROR:
682 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
683 return INVALID_OPERATION;
684 case STATUS_UNINITIALIZED:
685 ALOGE("%s: Device not initialized", __FUNCTION__);
686 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700687 case STATUS_UNCONFIGURED:
688 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700689 // OK
690 break;
691 case STATUS_ACTIVE:
692 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700693 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700694 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700695 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700696 return res;
697 }
698 wasActive = true;
699 break;
700 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700701 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700702 return INVALID_OPERATION;
703 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700704 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700705
706 if (mInputStream != 0) {
707 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
708 return INVALID_OPERATION;
709 }
710
711 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
712 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700713 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700714
715 mInputStream = newStream;
716
717 *id = mNextStreamId++;
718
719 // Continue captures if active at start
720 if (wasActive) {
721 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
722 res = configureStreamsLocked();
723 if (res != OK) {
724 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
725 __FUNCTION__, mNextStreamId, strerror(-res), res);
726 return res;
727 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700728 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700729 }
730
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700731 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700732 return OK;
733}
734
Igor Murashkin2fba5842013-04-22 14:03:54 -0700735
736status_t Camera3Device::createZslStream(
737 uint32_t width, uint32_t height,
738 int depth,
739 /*out*/
740 int *id,
741 sp<Camera3ZslStream>* zslStream) {
742 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700743 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700744 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700745 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
746 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700747
748 status_t res;
749 bool wasActive = false;
750
751 switch (mStatus) {
752 case STATUS_ERROR:
753 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
754 return INVALID_OPERATION;
755 case STATUS_UNINITIALIZED:
756 ALOGE("%s: Device not initialized", __FUNCTION__);
757 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700758 case STATUS_UNCONFIGURED:
759 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700760 // OK
761 break;
762 case STATUS_ACTIVE:
763 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700764 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700765 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700766 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700767 return res;
768 }
769 wasActive = true;
770 break;
771 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700772 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700773 return INVALID_OPERATION;
774 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700775 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700776
777 if (mInputStream != 0) {
778 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
779 return INVALID_OPERATION;
780 }
781
782 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
783 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700784 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700785
786 res = mOutputStreams.add(mNextStreamId, newStream);
787 if (res < 0) {
788 ALOGE("%s: Can't add new stream to set: %s (%d)",
789 __FUNCTION__, strerror(-res), res);
790 return res;
791 }
792 mInputStream = newStream;
793
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530794 mNeedConfig = true;
795
Igor Murashkin2fba5842013-04-22 14:03:54 -0700796 *id = mNextStreamId++;
797 *zslStream = newStream;
798
799 // Continue captures if active at start
800 if (wasActive) {
801 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
802 res = configureStreamsLocked();
803 if (res != OK) {
804 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
805 __FUNCTION__, mNextStreamId, strerror(-res), res);
806 return res;
807 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700808 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700809 }
810
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700811 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700812 return OK;
813}
814
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800815status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800816 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700817 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800818 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700819 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800820 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700821 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
822 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800823
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800824 status_t res;
825 bool wasActive = false;
826
827 switch (mStatus) {
828 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700829 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800830 return INVALID_OPERATION;
831 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700832 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800833 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700834 case STATUS_UNCONFIGURED:
835 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800836 // OK
837 break;
838 case STATUS_ACTIVE:
839 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700840 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800841 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700842 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800843 return res;
844 }
845 wasActive = true;
846 break;
847 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700848 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800849 return INVALID_OPERATION;
850 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700851 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800852
853 sp<Camera3OutputStream> newStream;
854 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700855 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700856 if (jpegBufferSize <= 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700857 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
858 return BAD_VALUE;
859 }
860
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800861 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700862 width, height, jpegBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800863 } else {
864 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700865 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800866 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700867 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800868
869 res = mOutputStreams.add(mNextStreamId, newStream);
870 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700871 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800872 return res;
873 }
874
875 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700876 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800877
878 // Continue captures if active at start
879 if (wasActive) {
880 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
881 res = configureStreamsLocked();
882 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700883 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
884 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800885 return res;
886 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700887 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800888 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700889 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800890 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800891}
892
893status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
894 ATRACE_CALL();
895 (void)outputId; (void)id;
896
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700897 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800898 return INVALID_OPERATION;
899}
900
901
902status_t Camera3Device::getStreamInfo(int id,
903 uint32_t *width, uint32_t *height, uint32_t *format) {
904 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700905 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800906 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800907
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800908 switch (mStatus) {
909 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700910 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800911 return INVALID_OPERATION;
912 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700913 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800914 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700915 case STATUS_UNCONFIGURED:
916 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800917 case STATUS_ACTIVE:
918 // OK
919 break;
920 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700921 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800922 return INVALID_OPERATION;
923 }
924
925 ssize_t idx = mOutputStreams.indexOfKey(id);
926 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700927 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800928 return idx;
929 }
930
931 if (width) *width = mOutputStreams[idx]->getWidth();
932 if (height) *height = mOutputStreams[idx]->getHeight();
933 if (format) *format = mOutputStreams[idx]->getFormat();
934
935 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800936}
937
938status_t Camera3Device::setStreamTransform(int id,
939 int transform) {
940 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700941 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800942 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800943
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800944 switch (mStatus) {
945 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700946 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800947 return INVALID_OPERATION;
948 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700949 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800950 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700951 case STATUS_UNCONFIGURED:
952 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800953 case STATUS_ACTIVE:
954 // OK
955 break;
956 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700957 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800958 return INVALID_OPERATION;
959 }
960
961 ssize_t idx = mOutputStreams.indexOfKey(id);
962 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700963 CLOGE("Stream %d does not exist",
964 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800965 return BAD_VALUE;
966 }
967
968 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800969}
970
971status_t Camera3Device::deleteStream(int id) {
972 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700973 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800974 Mutex::Autolock l(mLock);
975 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800976
Igor Murashkine2172be2013-05-28 15:31:39 -0700977 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
978
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800979 // CameraDevice semantics require device to already be idle before
980 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700981 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700982 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
983 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800984 }
985
Igor Murashkin2fba5842013-04-22 14:03:54 -0700986 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800987 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800988 if (mInputStream != NULL && id == mInputStream->getId()) {
989 deletedStream = mInputStream;
990 mInputStream.clear();
991 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800992 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700993 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800994 return BAD_VALUE;
995 }
Zhijun He5f446352014-01-22 09:49:33 -0800996 }
997
998 // Delete output stream or the output part of a bi-directional stream.
999 if (outputStreamIdx != NAME_NOT_FOUND) {
1000 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001001 mOutputStreams.removeItem(id);
1002 }
1003
1004 // Free up the stream endpoint so that it can be used by some other stream
1005 res = deletedStream->disconnect();
1006 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001007 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001008 // fall through since we want to still list the stream as deleted.
1009 }
1010 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001011 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001012
1013 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001014}
1015
1016status_t Camera3Device::deleteReprocessStream(int id) {
1017 ATRACE_CALL();
1018 (void)id;
1019
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001020 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001021 return INVALID_OPERATION;
1022}
1023
Igor Murashkine2d167e2014-08-19 16:19:59 -07001024status_t Camera3Device::configureStreams() {
1025 ATRACE_CALL();
1026 ALOGV("%s: E", __FUNCTION__);
1027
1028 Mutex::Autolock il(mInterfaceLock);
1029 Mutex::Autolock l(mLock);
1030
1031 return configureStreamsLocked();
1032}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001033
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001034status_t Camera3Device::getInputBufferProducer(
1035 sp<IGraphicBufferProducer> *producer) {
1036 Mutex::Autolock il(mInterfaceLock);
1037 Mutex::Autolock l(mLock);
1038
1039 if (producer == NULL) {
1040 return BAD_VALUE;
1041 } else if (mInputStream == NULL) {
1042 return INVALID_OPERATION;
1043 }
1044
1045 return mInputStream->getInputBufferProducer(producer);
1046}
1047
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001048status_t Camera3Device::createDefaultRequest(int templateId,
1049 CameraMetadata *request) {
1050 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001051 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001052 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053 Mutex::Autolock l(mLock);
1054
1055 switch (mStatus) {
1056 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001057 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001058 return INVALID_OPERATION;
1059 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001060 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001061 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001062 case STATUS_UNCONFIGURED:
1063 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001064 case STATUS_ACTIVE:
1065 // OK
1066 break;
1067 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001068 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 return INVALID_OPERATION;
1070 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001071
Zhijun Hea1530f12014-09-14 12:44:20 -07001072 if (!mRequestTemplateCache[templateId].isEmpty()) {
1073 *request = mRequestTemplateCache[templateId];
1074 return OK;
1075 }
1076
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001077 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001078 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001079 rawRequest = mHal3Device->ops->construct_default_request_settings(
1080 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001081 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001082 if (rawRequest == NULL) {
1083 SET_ERR_L("HAL is unable to construct default settings for template %d",
1084 templateId);
1085 return DEAD_OBJECT;
1086 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001087 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001088 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001089
1090 return OK;
1091}
1092
1093status_t Camera3Device::waitUntilDrained() {
1094 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001095 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001096 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001097
Zhijun He69a37482014-03-23 18:44:49 -07001098 return waitUntilDrainedLocked();
1099}
1100
1101status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001102 switch (mStatus) {
1103 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001104 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001105 ALOGV("%s: Already idle", __FUNCTION__);
1106 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001107 case STATUS_CONFIGURED:
1108 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001109 case STATUS_ERROR:
1110 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001111 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001112 break;
1113 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001114 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001115 return INVALID_OPERATION;
1116 }
1117
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001118 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1119 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001120 if (res != OK) {
1121 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1122 res);
1123 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001124 return res;
1125}
1126
1127// Pause to reconfigure
1128status_t Camera3Device::internalPauseAndWaitLocked() {
1129 mRequestThread->setPaused(true);
1130 mPauseStateNotify = true;
1131
1132 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1133 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1134 if (res != OK) {
1135 SET_ERR_L("Can't idle device in %f seconds!",
1136 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001137 }
1138
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001139 return res;
1140}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001142// Resume after internalPauseAndWaitLocked
1143status_t Camera3Device::internalResumeLocked() {
1144 status_t res;
1145
1146 mRequestThread->setPaused(false);
1147
1148 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1149 if (res != OK) {
1150 SET_ERR_L("Can't transition to active in %f seconds!",
1151 kActiveTimeout/1e9);
1152 }
1153 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001154 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001155}
1156
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001157status_t Camera3Device::waitUntilStateThenRelock(bool active,
1158 nsecs_t timeout) {
1159 status_t res = OK;
1160 if (active == (mStatus == STATUS_ACTIVE)) {
1161 // Desired state already reached
1162 return res;
1163 }
1164
1165 bool stateSeen = false;
1166 do {
1167 mRecentStatusUpdates.clear();
1168
1169 res = mStatusChanged.waitRelative(mLock, timeout);
1170 if (res != OK) break;
1171
1172 // Check state change history during wait
1173 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1174 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1175 stateSeen = true;
1176 break;
1177 }
1178 }
1179 } while (!stateSeen);
1180
1181 return res;
1182}
1183
1184
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001185status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1186 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001187 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001188
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001189 if (listener != NULL && mListener != NULL) {
1190 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1191 }
1192 mListener = listener;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001193 mRequestThread->setNotifyCallback(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001194
1195 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001196}
1197
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001198bool Camera3Device::willNotify3A() {
1199 return false;
1200}
1201
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001202status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001203 status_t res;
1204 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001205
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001206 while (mResultQueue.empty()) {
1207 res = mResultSignal.waitRelative(mOutputLock, timeout);
1208 if (res == TIMED_OUT) {
1209 return res;
1210 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001211 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001212 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001213 return res;
1214 }
1215 }
1216 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001217}
1218
Jianing Weicb0652e2014-03-12 18:29:36 -07001219status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001220 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001221 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001222
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001223 if (mResultQueue.empty()) {
1224 return NOT_ENOUGH_DATA;
1225 }
1226
Jianing Weicb0652e2014-03-12 18:29:36 -07001227 if (frame == NULL) {
1228 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1229 return BAD_VALUE;
1230 }
1231
1232 CaptureResult &result = *(mResultQueue.begin());
1233 frame->mResultExtras = result.mResultExtras;
1234 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001235 mResultQueue.erase(mResultQueue.begin());
1236
1237 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001238}
1239
1240status_t Camera3Device::triggerAutofocus(uint32_t id) {
1241 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001242 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001243
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001244 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1245 // Mix-in this trigger into the next request and only the next request.
1246 RequestTrigger trigger[] = {
1247 {
1248 ANDROID_CONTROL_AF_TRIGGER,
1249 ANDROID_CONTROL_AF_TRIGGER_START
1250 },
1251 {
1252 ANDROID_CONTROL_AF_TRIGGER_ID,
1253 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001254 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001255 };
1256
1257 return mRequestThread->queueTrigger(trigger,
1258 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001259}
1260
1261status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1262 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001263 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001264
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001265 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1266 // Mix-in this trigger into the next request and only the next request.
1267 RequestTrigger trigger[] = {
1268 {
1269 ANDROID_CONTROL_AF_TRIGGER,
1270 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1271 },
1272 {
1273 ANDROID_CONTROL_AF_TRIGGER_ID,
1274 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001275 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001276 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001277
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001278 return mRequestThread->queueTrigger(trigger,
1279 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001280}
1281
1282status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1283 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001284 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001285
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001286 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1287 // Mix-in this trigger into the next request and only the next request.
1288 RequestTrigger trigger[] = {
1289 {
1290 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1291 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1292 },
1293 {
1294 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1295 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001296 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001297 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001298
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001299 return mRequestThread->queueTrigger(trigger,
1300 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001301}
1302
1303status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1304 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1305 ATRACE_CALL();
1306 (void)reprocessStreamId; (void)buffer; (void)listener;
1307
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001308 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001309 return INVALID_OPERATION;
1310}
1311
Jianing Weicb0652e2014-03-12 18:29:36 -07001312status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001313 ATRACE_CALL();
1314 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001315 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001316
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001317 NotificationListener* listener;
1318 {
1319 Mutex::Autolock l(mOutputLock);
1320 listener = mListener;
1321 }
1322
Zhijun He7ef20392014-04-21 16:04:17 -07001323 {
1324 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001325 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001326 }
1327
Zhijun He491e3412013-12-27 10:57:44 -08001328 status_t res;
1329 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1330 res = mHal3Device->ops->flush(mHal3Device);
1331 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001332 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001333 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001334 }
1335
1336 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001337}
1338
Zhijun He204e3292014-07-14 17:09:23 -07001339uint32_t Camera3Device::getDeviceVersion() {
1340 ATRACE_CALL();
1341 Mutex::Autolock il(mInterfaceLock);
1342 return mDeviceVersion;
1343}
1344
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001345/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001346 * Methods called by subclasses
1347 */
1348
1349void Camera3Device::notifyStatus(bool idle) {
1350 {
1351 // Need mLock to safely update state and synchronize to current
1352 // state of methods in flight.
1353 Mutex::Autolock l(mLock);
1354 // We can get various system-idle notices from the status tracker
1355 // while starting up. Only care about them if we've actually sent
1356 // in some requests recently.
1357 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1358 return;
1359 }
1360 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1361 idle ? "idle" : "active");
1362 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1363 mRecentStatusUpdates.add(mStatus);
1364 mStatusChanged.signal();
1365
1366 // Skip notifying listener if we're doing some user-transparent
1367 // state changes
1368 if (mPauseStateNotify) return;
1369 }
1370 NotificationListener *listener;
1371 {
1372 Mutex::Autolock l(mOutputLock);
1373 listener = mListener;
1374 }
1375 if (idle && listener != NULL) {
1376 listener->notifyIdle();
1377 }
1378}
1379
1380/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381 * Camera3Device private methods
1382 */
1383
1384sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1385 const CameraMetadata &request) {
1386 ATRACE_CALL();
1387 status_t res;
1388
1389 sp<CaptureRequest> newRequest = new CaptureRequest;
1390 newRequest->mSettings = request;
1391
1392 camera_metadata_entry_t inputStreams =
1393 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1394 if (inputStreams.count > 0) {
1395 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001396 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001397 CLOGE("Request references unknown input stream %d",
1398 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001399 return NULL;
1400 }
1401 // Lazy completion of stream configuration (allocation/registration)
1402 // on first use
1403 if (mInputStream->isConfiguring()) {
1404 res = mInputStream->finishConfiguration(mHal3Device);
1405 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001406 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001407 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001408 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001409 return NULL;
1410 }
1411 }
1412
1413 newRequest->mInputStream = mInputStream;
1414 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1415 }
1416
1417 camera_metadata_entry_t streams =
1418 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1419 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001420 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001421 return NULL;
1422 }
1423
1424 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001425 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001426 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001427 CLOGE("Request references unknown stream %d",
1428 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001429 return NULL;
1430 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001431 sp<Camera3OutputStreamInterface> stream =
1432 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001433
1434 // Lazy completion of stream configuration (allocation/registration)
1435 // on first use
1436 if (stream->isConfiguring()) {
1437 res = stream->finishConfiguration(mHal3Device);
1438 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001439 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1440 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001441 return NULL;
1442 }
1443 }
1444
1445 newRequest->mOutputStreams.push(stream);
1446 }
1447 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1448
1449 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001450}
1451
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001452bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1453 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1454 Size size = mSupportedOpaqueInputSizes[i];
1455 if (size.width == width && size.height == height) {
1456 return true;
1457 }
1458 }
1459
1460 return false;
1461}
1462
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001463status_t Camera3Device::configureStreamsLocked() {
1464 ATRACE_CALL();
1465 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001466
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001467 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001468 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001469 return INVALID_OPERATION;
1470 }
1471
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001472 if (!mNeedConfig) {
1473 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1474 return OK;
1475 }
1476
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001477 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1478 // adding a dummy stream instead.
1479 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1480 if (mOutputStreams.size() == 0) {
1481 addDummyStreamLocked();
1482 } else {
1483 tryRemoveDummyStreamLocked();
1484 }
1485
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001486 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001487 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001488
1489 camera3_stream_configuration config;
1490
1491 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1492
1493 Vector<camera3_stream_t*> streams;
1494 streams.setCapacity(config.num_streams);
1495
1496 if (mInputStream != NULL) {
1497 camera3_stream_t *inputStream;
1498 inputStream = mInputStream->startConfiguration();
1499 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001500 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001501 return INVALID_OPERATION;
1502 }
1503 streams.add(inputStream);
1504 }
1505
1506 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001507
1508 // Don't configure bidi streams twice, nor add them twice to the list
1509 if (mOutputStreams[i].get() ==
1510 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1511
1512 config.num_streams--;
1513 continue;
1514 }
1515
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001516 camera3_stream_t *outputStream;
1517 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1518 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001519 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001520 return INVALID_OPERATION;
1521 }
1522 streams.add(outputStream);
1523 }
1524
1525 config.streams = streams.editArray();
1526
1527 // Do the HAL configuration; will potentially touch stream
1528 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001529 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001530 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001531 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001532
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001533 if (res == BAD_VALUE) {
1534 // HAL rejected this set of streams as unsupported, clean up config
1535 // attempt and return to unconfigured state
1536 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1537 res = mInputStream->cancelConfiguration();
1538 if (res != OK) {
1539 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1540 mInputStream->getId(), strerror(-res), res);
1541 return res;
1542 }
1543 }
1544
1545 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1546 sp<Camera3OutputStreamInterface> outputStream =
1547 mOutputStreams.editValueAt(i);
1548 if (outputStream->isConfiguring()) {
1549 res = outputStream->cancelConfiguration();
1550 if (res != OK) {
1551 SET_ERR_L(
1552 "Can't cancel configuring output stream %d: %s (%d)",
1553 outputStream->getId(), strerror(-res), res);
1554 return res;
1555 }
1556 }
1557 }
1558
1559 // Return state to that at start of call, so that future configures
1560 // properly clean things up
1561 mStatus = STATUS_UNCONFIGURED;
1562 mNeedConfig = true;
1563
1564 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1565 return BAD_VALUE;
1566 } else if (res != OK) {
1567 // Some other kind of error from configure_streams - this is not
1568 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001569 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1570 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001571 return res;
1572 }
1573
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001574 // Finish all stream configuration immediately.
1575 // TODO: Try to relax this later back to lazy completion, which should be
1576 // faster
1577
Igor Murashkin073f8572013-05-02 14:59:28 -07001578 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001579 res = mInputStream->finishConfiguration(mHal3Device);
1580 if (res != OK) {
1581 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1582 mInputStream->getId(), strerror(-res), res);
1583 return res;
1584 }
1585 }
1586
1587 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001588 sp<Camera3OutputStreamInterface> outputStream =
1589 mOutputStreams.editValueAt(i);
1590 if (outputStream->isConfiguring()) {
1591 res = outputStream->finishConfiguration(mHal3Device);
1592 if (res != OK) {
1593 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1594 outputStream->getId(), strerror(-res), res);
1595 return res;
1596 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001597 }
1598 }
1599
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001600 // Request thread needs to know to avoid using repeat-last-settings protocol
1601 // across configure_streams() calls
1602 mRequestThread->configurationComplete();
1603
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001604 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001605
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001606 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001607
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001608 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001609 mStatus = STATUS_CONFIGURED;
1610 } else {
1611 mStatus = STATUS_UNCONFIGURED;
1612 }
1613
1614 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1615
Zhijun He0a210512014-07-24 13:45:15 -07001616 // tear down the deleted streams after configure streams.
1617 mDeletedStreams.clear();
1618
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001619 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001620}
1621
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001622status_t Camera3Device::addDummyStreamLocked() {
1623 ATRACE_CALL();
1624 status_t res;
1625
1626 if (mDummyStreamId != NO_STREAM) {
1627 // Should never be adding a second dummy stream when one is already
1628 // active
1629 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1630 __FUNCTION__, mId);
1631 return INVALID_OPERATION;
1632 }
1633
1634 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1635
1636 sp<Camera3OutputStreamInterface> dummyStream =
1637 new Camera3DummyStream(mNextStreamId);
1638
1639 res = mOutputStreams.add(mNextStreamId, dummyStream);
1640 if (res < 0) {
1641 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1642 return res;
1643 }
1644
1645 mDummyStreamId = mNextStreamId;
1646 mNextStreamId++;
1647
1648 return OK;
1649}
1650
1651status_t Camera3Device::tryRemoveDummyStreamLocked() {
1652 ATRACE_CALL();
1653 status_t res;
1654
1655 if (mDummyStreamId == NO_STREAM) return OK;
1656 if (mOutputStreams.size() == 1) return OK;
1657
1658 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1659
1660 // Ok, have a dummy stream and there's at least one other output stream,
1661 // so remove the dummy
1662
1663 sp<Camera3StreamInterface> deletedStream;
1664 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1665 if (outputStreamIdx == NAME_NOT_FOUND) {
1666 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1667 return INVALID_OPERATION;
1668 }
1669
1670 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1671 mOutputStreams.removeItemsAt(outputStreamIdx);
1672
1673 // Free up the stream endpoint so that it can be used by some other stream
1674 res = deletedStream->disconnect();
1675 if (res != OK) {
1676 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1677 // fall through since we want to still list the stream as deleted.
1678 }
1679 mDeletedStreams.add(deletedStream);
1680 mDummyStreamId = NO_STREAM;
1681
1682 return res;
1683}
1684
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001685void Camera3Device::setErrorState(const char *fmt, ...) {
1686 Mutex::Autolock l(mLock);
1687 va_list args;
1688 va_start(args, fmt);
1689
1690 setErrorStateLockedV(fmt, args);
1691
1692 va_end(args);
1693}
1694
1695void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1696 Mutex::Autolock l(mLock);
1697 setErrorStateLockedV(fmt, args);
1698}
1699
1700void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1701 va_list args;
1702 va_start(args, fmt);
1703
1704 setErrorStateLockedV(fmt, args);
1705
1706 va_end(args);
1707}
1708
1709void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001710 // Print out all error messages to log
1711 String8 errorCause = String8::formatV(fmt, args);
1712 ALOGE("Camera %d: %s", mId, errorCause.string());
1713
1714 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001715 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001716
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001717 mErrorCause = errorCause;
1718
1719 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001720 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001721
1722 // Notify upstream about a device error
1723 if (mListener != NULL) {
1724 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1725 CaptureResultExtras());
1726 }
1727
1728 // Save stack trace. View by dumping it later.
1729 CameraTraces::saveTrace();
1730 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001731}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001732
1733/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001734 * In-flight request management
1735 */
1736
Jianing Weicb0652e2014-03-12 18:29:36 -07001737status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001738 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001739 ATRACE_CALL();
1740 Mutex::Autolock l(mInFlightLock);
1741
1742 ssize_t res;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001743 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001744 if (res < 0) return res;
1745
1746 return OK;
1747}
1748
1749/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001750 * Check if all 3A fields are ready, and send off a partial 3A-only result
1751 * to the output frame queue
1752 */
Zhijun He204e3292014-07-14 17:09:23 -07001753bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001754 uint32_t frameNumber,
1755 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001756
1757 // Check if all 3A states are present
1758 // The full list of fields is
1759 // android.control.afMode
1760 // android.control.awbMode
1761 // android.control.aeState
1762 // android.control.awbState
1763 // android.control.afState
1764 // android.control.afTriggerID
1765 // android.control.aePrecaptureID
1766 // TODO: Add android.control.aeMode
1767
1768 bool gotAllStates = true;
1769
1770 uint8_t afMode;
1771 uint8_t awbMode;
1772 uint8_t aeState;
1773 uint8_t afState;
1774 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001775
1776 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1777 &afMode, frameNumber);
1778
1779 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1780 &awbMode, frameNumber);
1781
1782 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1783 &aeState, frameNumber);
1784
1785 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1786 &afState, frameNumber);
1787
1788 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1789 &awbState, frameNumber);
1790
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001791 if (!gotAllStates) return false;
1792
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001793 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001794 "AF state %d, AE state %d, AWB state %d, "
1795 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001796 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001797 afMode, awbMode,
1798 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001799 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001800
1801 // Got all states, so construct a minimal result to send
1802 // In addition to the above fields, this means adding in
1803 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001804 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001805 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001806
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001807 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001808
1809 Mutex::Autolock l(mOutputLock);
1810
Jianing Weicb0652e2014-03-12 18:29:36 -07001811 CaptureResult captureResult;
1812 captureResult.mResultExtras = resultExtras;
1813 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1814 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1815 // but not limited to CameraDeviceBase::getNextResult
1816 CaptureResult& min3AResult =
1817 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001818
Jianing Weicb0652e2014-03-12 18:29:36 -07001819 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1820 // TODO: This is problematic casting. Need to fix CameraMetadata.
1821 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001822 return false;
1823 }
1824
Jianing Weicb0652e2014-03-12 18:29:36 -07001825 int32_t requestId = resultExtras.requestId;
1826 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001827 &requestId, frameNumber)) {
1828 return false;
1829 }
1830
Zhijun He204e3292014-07-14 17:09:23 -07001831 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1832 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1833 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1834 &partialResult, frameNumber)) {
1835 return false;
1836 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001837 }
1838
Jianing Weicb0652e2014-03-12 18:29:36 -07001839 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001840 &afMode, frameNumber)) {
1841 return false;
1842 }
1843
Jianing Weicb0652e2014-03-12 18:29:36 -07001844 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001845 &awbMode, frameNumber)) {
1846 return false;
1847 }
1848
Jianing Weicb0652e2014-03-12 18:29:36 -07001849 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001850 &aeState, frameNumber)) {
1851 return false;
1852 }
1853
Jianing Weicb0652e2014-03-12 18:29:36 -07001854 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001855 &afState, frameNumber)) {
1856 return false;
1857 }
1858
Jianing Weicb0652e2014-03-12 18:29:36 -07001859 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001860 &awbState, frameNumber)) {
1861 return false;
1862 }
1863
Jianing Weicb0652e2014-03-12 18:29:36 -07001864 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001865 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001866 return false;
1867 }
1868
Jianing Weicb0652e2014-03-12 18:29:36 -07001869 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001870 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001871 return false;
1872 }
1873
Zhijun He204e3292014-07-14 17:09:23 -07001874 // We only send the aggregated partial when all 3A related metadata are available
1875 // For both API1 and API2.
1876 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001877 mResultSignal.signal();
1878
1879 return true;
1880}
1881
1882template<typename T>
1883bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001884 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001885 (void) frameNumber;
1886
1887 camera_metadata_ro_entry_t entry;
1888
1889 entry = result.find(tag);
1890 if (entry.count == 0) {
1891 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1892 mId, frameNumber, get_camera_metadata_tag_name(tag));
1893 return false;
1894 }
1895
1896 if (sizeof(T) == sizeof(uint8_t)) {
1897 *value = entry.data.u8[0];
1898 } else if (sizeof(T) == sizeof(int32_t)) {
1899 *value = entry.data.i32[0];
1900 } else {
1901 ALOGE("%s: Unexpected type", __FUNCTION__);
1902 return false;
1903 }
1904 return true;
1905}
1906
1907template<typename T>
1908bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001909 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001910 if (result.update(tag, value, 1) != NO_ERROR) {
1911 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1912 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1913 frameNumber, get_camera_metadata_tag_name(tag));
1914 return false;
1915 }
1916 return true;
1917}
1918
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08001919
1920void Camera3Device::returnOutputBuffers(
1921 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
1922 nsecs_t timestamp) {
1923 for (size_t i = 0; i < numBuffers; i++)
1924 {
1925 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
1926 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
1927 // Note: stream may be deallocated at this point, if this buffer was
1928 // the last reference to it.
1929 if (res != OK) {
1930 ALOGE("Can't return buffer to its stream: %s (%d)",
1931 strerror(-res), res);
1932 }
1933 }
1934}
1935
1936
1937void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
1938
1939 const InFlightRequest &request = mInFlightMap.valueAt(idx);
1940 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
1941
1942 nsecs_t sensorTimestamp = request.sensorTimestamp;
1943 nsecs_t shutterTimestamp = request.shutterTimestamp;
1944
1945 // Check if it's okay to remove the request from InFlightMap:
1946 // In the case of a successful request:
1947 // all input and output buffers, all result metadata, shutter callback
1948 // arrived.
1949 // In the case of a unsuccessful request:
1950 // all input and output buffers arrived.
1951 if (request.numBuffersLeft == 0 &&
1952 (request.requestStatus != OK ||
1953 (request.haveResultMetadata && shutterTimestamp != 0))) {
1954 ATRACE_ASYNC_END("frame capture", frameNumber);
1955
1956 // Sanity check - if sensor timestamp matches shutter timestamp
1957 if (request.requestStatus == OK &&
1958 sensorTimestamp != shutterTimestamp) {
1959 SET_ERR("sensor timestamp (%" PRId64
1960 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
1961 sensorTimestamp, frameNumber, shutterTimestamp);
1962 }
1963
1964 // for an unsuccessful request, it may have pending output buffers to
1965 // return.
1966 assert(request.requestStatus != OK ||
1967 request.pendingOutputBuffers.size() == 0);
1968 returnOutputBuffers(request.pendingOutputBuffers.array(),
1969 request.pendingOutputBuffers.size(), 0);
1970
1971 mInFlightMap.removeItemsAt(idx, 1);
1972
1973 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
1974 }
1975
1976 // Sanity check - if we have too many in-flight frames, something has
1977 // likely gone wrong
1978 if (mInFlightMap.size() > kInFlightWarnLimit) {
1979 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
1980 }
1981}
1982
1983
1984void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
1985 CaptureResultExtras &resultExtras,
1986 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001987 uint32_t frameNumber,
1988 bool reprocess) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08001989 if (pendingMetadata.isEmpty())
1990 return;
1991
1992 Mutex::Autolock l(mOutputLock);
1993
1994 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001995 if (reprocess) {
1996 if (frameNumber < mNextReprocessResultFrameNumber) {
1997 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08001998 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001999 frameNumber, mNextReprocessResultFrameNumber);
2000 return;
2001 }
2002 mNextReprocessResultFrameNumber = frameNumber + 1;
2003 } else {
2004 if (frameNumber < mNextResultFrameNumber) {
2005 SET_ERR("Out-of-order capture result metadata submitted! "
2006 "(got frame number %d, expecting %d)",
2007 frameNumber, mNextResultFrameNumber);
2008 return;
2009 }
2010 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002011 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002012
2013 CaptureResult captureResult;
2014 captureResult.mResultExtras = resultExtras;
2015 captureResult.mMetadata = pendingMetadata;
2016
2017 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2018 (int32_t*)&frameNumber, 1) != OK) {
2019 SET_ERR("Failed to set frame# in metadata (%d)",
2020 frameNumber);
2021 return;
2022 } else {
2023 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2024 __FUNCTION__, mId, frameNumber);
2025 }
2026
2027 // Append any previous partials to form a complete result
2028 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2029 captureResult.mMetadata.append(collectedPartialResult);
2030 }
2031
2032 captureResult.mMetadata.sort();
2033
2034 // Check that there's a timestamp in the result metadata
2035 camera_metadata_entry entry =
2036 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2037 if (entry.count == 0) {
2038 SET_ERR("No timestamp provided by HAL for frame %d!",
2039 frameNumber);
2040 return;
2041 }
2042
2043 // Valid result, insert into queue
2044 List<CaptureResult>::iterator queuedResult =
2045 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2046 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2047 ", burstId = %" PRId32, __FUNCTION__,
2048 queuedResult->mResultExtras.requestId,
2049 queuedResult->mResultExtras.frameNumber,
2050 queuedResult->mResultExtras.burstId);
2051
2052 mResultSignal.signal();
2053}
2054
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002055/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002056 * Camera HAL device callback methods
2057 */
2058
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002059void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002060 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002061
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002062 status_t res;
2063
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002064 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002065 if (result->result == NULL && result->num_output_buffers == 0 &&
2066 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002067 SET_ERR("No result data provided by HAL for frame %d",
2068 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002069 return;
2070 }
Zhijun He204e3292014-07-14 17:09:23 -07002071
2072 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2073 // partial_result to 1 when metadata is included in this result.
2074 if (!mUsePartialResult &&
2075 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2076 result->result != NULL &&
2077 result->partial_result != 1) {
2078 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2079 " if partial result is not supported",
2080 frameNumber, result->partial_result);
2081 return;
2082 }
2083
2084 bool isPartialResult = false;
2085 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002086 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002087 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002088
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002089 // Get shutter timestamp and resultExtras from list of in-flight requests,
2090 // where it was added by the shutter notification for this frame. If the
2091 // shutter timestamp isn't received yet, append the output buffers to the
2092 // in-flight request and they will be returned when the shutter timestamp
2093 // arrives. Update the in-flight status and remove the in-flight entry if
2094 // all result data and shutter timestamp have been received.
2095 nsecs_t shutterTimestamp = 0;
2096
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002097 {
2098 Mutex::Autolock l(mInFlightLock);
2099 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2100 if (idx == NAME_NOT_FOUND) {
2101 SET_ERR("Unknown frame number for capture result: %d",
2102 frameNumber);
2103 return;
2104 }
2105 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002106 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2107 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2108 ", partialResultCount = %d",
2109 __FUNCTION__, request.resultExtras.requestId,
2110 request.resultExtras.frameNumber, request.resultExtras.burstId,
2111 result->partial_result);
2112 // Always update the partial count to the latest one if it's not 0
2113 // (buffers only). When framework aggregates adjacent partial results
2114 // into one, the latest partial count will be used.
2115 if (result->partial_result != 0)
2116 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002117
2118 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002119 if (mUsePartialResult && result->result != NULL) {
2120 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2121 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2122 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2123 " the range of [1, %d] when metadata is included in the result",
2124 frameNumber, result->partial_result, mNumPartialResults);
2125 return;
2126 }
2127 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002128 if (isPartialResult) {
2129 request.partialResult.collectedResult.append(result->result);
2130 }
Zhijun He204e3292014-07-14 17:09:23 -07002131 } else {
2132 camera_metadata_ro_entry_t partialResultEntry;
2133 res = find_camera_metadata_ro_entry(result->result,
2134 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2135 if (res != NAME_NOT_FOUND &&
2136 partialResultEntry.count > 0 &&
2137 partialResultEntry.data.u8[0] ==
2138 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2139 // A partial result. Flag this as such, and collect this
2140 // set of metadata into the in-flight entry.
2141 isPartialResult = true;
2142 request.partialResult.collectedResult.append(
2143 result->result);
2144 request.partialResult.collectedResult.erase(
2145 ANDROID_QUIRKS_PARTIAL_RESULT);
2146 }
2147 }
2148
2149 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002150 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002151 if (!request.partialResult.haveSent3A) {
2152 request.partialResult.haveSent3A =
2153 processPartial3AResult(frameNumber,
2154 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002155 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002156 }
2157 }
2158 }
2159
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002160 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002161 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002162
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002163 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002164 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002165 if (request.haveResultMetadata) {
2166 SET_ERR("Called multiple times with metadata for frame %d",
2167 frameNumber);
2168 return;
2169 }
Zhijun He204e3292014-07-14 17:09:23 -07002170 if (mUsePartialResult &&
2171 !request.partialResult.collectedResult.isEmpty()) {
2172 collectedPartialResult.acquire(
2173 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002174 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002175 request.haveResultMetadata = true;
2176 }
2177
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002178 uint32_t numBuffersReturned = result->num_output_buffers;
2179 if (result->input_buffer != NULL) {
2180 if (hasInputBufferInRequest) {
2181 numBuffersReturned += 1;
2182 } else {
2183 ALOGW("%s: Input buffer should be NULL if there is no input"
2184 " buffer sent in the request",
2185 __FUNCTION__);
2186 }
2187 }
2188 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002189 if (request.numBuffersLeft < 0) {
2190 SET_ERR("Too many buffers returned for frame %d",
2191 frameNumber);
2192 return;
2193 }
2194
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002195 camera_metadata_ro_entry_t entry;
2196 res = find_camera_metadata_ro_entry(result->result,
2197 ANDROID_SENSOR_TIMESTAMP, &entry);
2198 if (res == OK && entry.count == 1) {
2199 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002200 }
2201
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002202 // If shutter event isn't received yet, append the output buffers to
2203 // the in-flight request. Otherwise, return the output buffers to
2204 // streams.
2205 if (shutterTimestamp == 0) {
2206 request.pendingOutputBuffers.appendArray(result->output_buffers,
2207 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002208 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002209 returnOutputBuffers(result->output_buffers,
2210 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002211 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002212
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002213 if (result->result != NULL && !isPartialResult) {
2214 if (shutterTimestamp == 0) {
2215 request.pendingMetadata = result->result;
2216 request.partialResult.collectedResult = collectedPartialResult;
2217 } else {
2218 CameraMetadata metadata;
2219 metadata = result->result;
2220 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002221 collectedPartialResult, frameNumber, hasInputBufferInRequest);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002222 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002223 }
2224
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002225 removeInFlightRequestIfReadyLocked(idx);
2226 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002227
Zhijun Hef0d962a2014-06-30 10:24:11 -07002228 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002229 if (hasInputBufferInRequest) {
2230 Camera3Stream *stream =
2231 Camera3Stream::cast(result->input_buffer->stream);
2232 res = stream->returnInputBuffer(*(result->input_buffer));
2233 // Note: stream may be deallocated at this point, if this buffer was the
2234 // last reference to it.
2235 if (res != OK) {
2236 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2237 " its stream:%s (%d)", __FUNCTION__,
2238 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002239 }
2240 } else {
2241 ALOGW("%s: Input buffer should be NULL if there is no input"
2242 " buffer sent in the request, skipping input buffer return.",
2243 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002244 }
2245 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002246}
2247
2248void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002249 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002250 NotificationListener *listener;
2251 {
2252 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002253 listener = mListener;
2254 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002255
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002256 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002257 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002258 return;
2259 }
2260
2261 switch (msg->type) {
2262 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002263 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002264 break;
2265 }
2266 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002267 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002268 break;
2269 }
2270 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002271 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002272 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002273 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002274}
2275
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002276void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2277 NotificationListener *listener) {
2278
2279 // Map camera HAL error codes to ICameraDeviceCallback error codes
2280 // Index into this with the HAL error code
2281 static const ICameraDeviceCallbacks::CameraErrorCode
2282 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2283 // 0 = Unused error code
2284 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2285 // 1 = CAMERA3_MSG_ERROR_DEVICE
2286 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2287 // 2 = CAMERA3_MSG_ERROR_REQUEST
2288 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2289 // 3 = CAMERA3_MSG_ERROR_RESULT
2290 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2291 // 4 = CAMERA3_MSG_ERROR_BUFFER
2292 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2293 };
2294
2295 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2296 ((msg.error_code >= 0) &&
2297 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2298 halErrorMap[msg.error_code] :
2299 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2300
2301 int streamId = 0;
2302 if (msg.error_stream != NULL) {
2303 Camera3Stream *stream =
2304 Camera3Stream::cast(msg.error_stream);
2305 streamId = stream->getId();
2306 }
2307 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2308 mId, __FUNCTION__, msg.frame_number,
2309 streamId, msg.error_code);
2310
2311 CaptureResultExtras resultExtras;
2312 switch (errorCode) {
2313 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2314 // SET_ERR calls notifyError
2315 SET_ERR("Camera HAL reported serious device error");
2316 break;
2317 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2318 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2319 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2320 {
2321 Mutex::Autolock l(mInFlightLock);
2322 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2323 if (idx >= 0) {
2324 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2325 r.requestStatus = msg.error_code;
2326 resultExtras = r.resultExtras;
2327 } else {
2328 resultExtras.frameNumber = msg.frame_number;
2329 ALOGE("Camera %d: %s: cannot find in-flight request on "
2330 "frame %" PRId64 " error", mId, __FUNCTION__,
2331 resultExtras.frameNumber);
2332 }
2333 }
2334 if (listener != NULL) {
2335 listener->notifyError(errorCode, resultExtras);
2336 } else {
2337 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2338 }
2339 break;
2340 default:
2341 // SET_ERR calls notifyError
2342 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2343 break;
2344 }
2345}
2346
2347void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2348 NotificationListener *listener) {
2349 ssize_t idx;
2350 // Verify ordering of shutter notifications
2351 {
2352 Mutex::Autolock l(mOutputLock);
2353 // TODO: need to track errors for tighter bounds on expected frame number.
2354 if (msg.frame_number < mNextShutterFrameNumber) {
2355 SET_ERR("Shutter notification out-of-order. Expected "
2356 "notification for frame %d, got frame %d",
2357 mNextShutterFrameNumber, msg.frame_number);
2358 return;
2359 }
2360 mNextShutterFrameNumber = msg.frame_number + 1;
2361 }
2362
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002363 // Set timestamp for the request in the in-flight tracking
2364 // and get the request ID to send upstream
2365 {
2366 Mutex::Autolock l(mInFlightLock);
2367 idx = mInFlightMap.indexOfKey(msg.frame_number);
2368 if (idx >= 0) {
2369 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002370
2371 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2372 mId, __FUNCTION__,
2373 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2374 // Call listener, if any
2375 if (listener != NULL) {
2376 listener->notifyShutter(r.resultExtras, msg.timestamp);
2377 }
2378
2379 r.shutterTimestamp = msg.timestamp;
2380
2381 // send pending result and buffers
2382 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002383 r.partialResult.collectedResult, msg.frame_number,
2384 r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002385 returnOutputBuffers(r.pendingOutputBuffers.array(),
2386 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2387 r.pendingOutputBuffers.clear();
2388
2389 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002390 }
2391 }
2392 if (idx < 0) {
2393 SET_ERR("Shutter notification for non-existent frame number %d",
2394 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002395 }
2396}
2397
2398
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002399CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002400 ALOGV("%s", __FUNCTION__);
2401
Igor Murashkin1e479c02013-09-06 16:55:14 -07002402 CameraMetadata retVal;
2403
2404 if (mRequestThread != NULL) {
2405 retVal = mRequestThread->getLatestRequest();
2406 }
2407
Igor Murashkin1e479c02013-09-06 16:55:14 -07002408 return retVal;
2409}
2410
Jianing Weicb0652e2014-03-12 18:29:36 -07002411
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002412/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002413 * RequestThread inner class methods
2414 */
2415
2416Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002417 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002418 camera3_device_t *hal3Device) :
2419 Thread(false),
2420 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002421 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002422 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002423 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002424 mReconfigured(false),
2425 mDoPause(false),
2426 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002427 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002428 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002429 mCurrentAfTriggerId(0),
2430 mCurrentPreCaptureTriggerId(0),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002431 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002432 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002433}
2434
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002435void Camera3Device::RequestThread::setNotifyCallback(
2436 NotificationListener *listener) {
2437 Mutex::Autolock l(mRequestLock);
2438 mListener = listener;
2439}
2440
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002441void Camera3Device::RequestThread::configurationComplete() {
2442 Mutex::Autolock l(mRequestLock);
2443 mReconfigured = true;
2444}
2445
Jianing Wei90e59c92014-03-12 18:29:36 -07002446status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002447 List<sp<CaptureRequest> > &requests,
2448 /*out*/
2449 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002450 Mutex::Autolock l(mRequestLock);
2451 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2452 ++it) {
2453 mRequestQueue.push_back(*it);
2454 }
2455
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002456 if (lastFrameNumber != NULL) {
2457 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2458 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2459 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2460 *lastFrameNumber);
2461 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002462
Jianing Wei90e59c92014-03-12 18:29:36 -07002463 unpauseForNewRequests();
2464
2465 return OK;
2466}
2467
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002468
2469status_t Camera3Device::RequestThread::queueTrigger(
2470 RequestTrigger trigger[],
2471 size_t count) {
2472
2473 Mutex::Autolock l(mTriggerMutex);
2474 status_t ret;
2475
2476 for (size_t i = 0; i < count; ++i) {
2477 ret = queueTriggerLocked(trigger[i]);
2478
2479 if (ret != OK) {
2480 return ret;
2481 }
2482 }
2483
2484 return OK;
2485}
2486
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002487int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2488 sp<Camera3Device> d = device.promote();
2489 if (d != NULL) return d->mId;
2490 return 0;
2491}
2492
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002493status_t Camera3Device::RequestThread::queueTriggerLocked(
2494 RequestTrigger trigger) {
2495
2496 uint32_t tag = trigger.metadataTag;
2497 ssize_t index = mTriggerMap.indexOfKey(tag);
2498
2499 switch (trigger.getTagType()) {
2500 case TYPE_BYTE:
2501 // fall-through
2502 case TYPE_INT32:
2503 break;
2504 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002505 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2506 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002507 return INVALID_OPERATION;
2508 }
2509
2510 /**
2511 * Collect only the latest trigger, since we only have 1 field
2512 * in the request settings per trigger tag, and can't send more than 1
2513 * trigger per request.
2514 */
2515 if (index != NAME_NOT_FOUND) {
2516 mTriggerMap.editValueAt(index) = trigger;
2517 } else {
2518 mTriggerMap.add(tag, trigger);
2519 }
2520
2521 return OK;
2522}
2523
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002524status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002525 const RequestList &requests,
2526 /*out*/
2527 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002528 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002529 if (lastFrameNumber != NULL) {
2530 *lastFrameNumber = mRepeatingLastFrameNumber;
2531 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002532 mRepeatingRequests.clear();
2533 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2534 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002535
2536 unpauseForNewRequests();
2537
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002538 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002539 return OK;
2540}
2541
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002542bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2543 if (mRepeatingRequests.empty()) {
2544 return false;
2545 }
2546 int32_t requestId = requestIn->mResultExtras.requestId;
2547 const RequestList &repeatRequests = mRepeatingRequests;
2548 // All repeating requests are guaranteed to have same id so only check first quest
2549 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2550 return (firstRequest->mResultExtras.requestId == requestId);
2551}
2552
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002553status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002554 Mutex::Autolock l(mRequestLock);
2555 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002556 if (lastFrameNumber != NULL) {
2557 *lastFrameNumber = mRepeatingLastFrameNumber;
2558 }
2559 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002560 return OK;
2561}
2562
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002563status_t Camera3Device::RequestThread::clear(
2564 NotificationListener *listener,
2565 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002566 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002567 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002568
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002569 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002570
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002571 // Send errors for all requests pending in the request queue, including
2572 // pending repeating requests
2573 if (listener != NULL) {
2574 for (RequestList::iterator it = mRequestQueue.begin();
2575 it != mRequestQueue.end(); ++it) {
2576 // Set the frame number this request would have had, if it
2577 // had been submitted; this frame number will not be reused.
2578 // The requestId and burstId fields were set when the request was
2579 // submitted originally (in convertMetadataListToRequestListLocked)
2580 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2581 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2582 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002583 }
2584 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002585 mRequestQueue.clear();
2586 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002587 if (lastFrameNumber != NULL) {
2588 *lastFrameNumber = mRepeatingLastFrameNumber;
2589 }
2590 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002591 return OK;
2592}
2593
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002594void Camera3Device::RequestThread::setPaused(bool paused) {
2595 Mutex::Autolock l(mPauseLock);
2596 mDoPause = paused;
2597 mDoPauseSignal.signal();
2598}
2599
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002600status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2601 int32_t requestId, nsecs_t timeout) {
2602 Mutex::Autolock l(mLatestRequestMutex);
2603 status_t res;
2604 while (mLatestRequestId != requestId) {
2605 nsecs_t startTime = systemTime();
2606
2607 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2608 if (res != OK) return res;
2609
2610 timeout -= (systemTime() - startTime);
2611 }
2612
2613 return OK;
2614}
2615
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002616void Camera3Device::RequestThread::requestExit() {
2617 // Call parent to set up shutdown
2618 Thread::requestExit();
2619 // The exit from any possible waits
2620 mDoPauseSignal.signal();
2621 mRequestSignal.signal();
2622}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002623
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002624bool Camera3Device::RequestThread::threadLoop() {
2625
2626 status_t res;
2627
2628 // Handle paused state.
2629 if (waitIfPaused()) {
2630 return true;
2631 }
2632
2633 // Get work to do
2634
2635 sp<CaptureRequest> nextRequest = waitForNextRequest();
2636 if (nextRequest == NULL) {
2637 return true;
2638 }
2639
2640 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002641 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002642 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002643 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002644
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002645 // Get the request ID, if any
2646 int requestId;
2647 camera_metadata_entry_t requestIdEntry =
2648 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2649 if (requestIdEntry.count > 0) {
2650 requestId = requestIdEntry.data.i32[0];
2651 } else {
2652 ALOGW("%s: Did not have android.request.id set in the request",
2653 __FUNCTION__);
2654 requestId = NAME_NOT_FOUND;
2655 }
2656
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002657 // Insert any queued triggers (before metadata is locked)
2658 int32_t triggerCount;
2659 res = insertTriggers(nextRequest);
2660 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002661 SET_ERR("RequestThread: Unable to insert triggers "
2662 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002663 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002664 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2665 return false;
2666 }
2667 triggerCount = res;
2668
2669 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2670
2671 // If the request is the same as last, or we had triggers last time
2672 if (mPrevRequest != nextRequest || triggersMixedIn) {
2673 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002674 * HAL workaround:
2675 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2676 */
2677 res = addDummyTriggerIds(nextRequest);
2678 if (res != OK) {
2679 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2680 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002681 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002682 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2683 return false;
2684 }
2685
2686 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002687 * The request should be presorted so accesses in HAL
2688 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2689 */
2690 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002691 request.settings = nextRequest->mSettings.getAndLock();
2692 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002693 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2694
2695 IF_ALOGV() {
2696 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2697 find_camera_metadata_ro_entry(
2698 request.settings,
2699 ANDROID_CONTROL_AF_TRIGGER,
2700 &e
2701 );
2702 if (e.count > 0) {
2703 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2704 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002705 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002706 e.data.u8[0]);
2707 }
2708 }
2709 } else {
2710 // leave request.settings NULL to indicate 'reuse latest given'
2711 ALOGVV("%s: Request settings are REUSED",
2712 __FUNCTION__);
2713 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002714
2715 camera3_stream_buffer_t inputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002716 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002717
2718 // Fill in buffers
2719
2720 if (nextRequest->mInputStream != NULL) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002721 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002722 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002723 // Can't get input buffer from gralloc queue - this could be due to
2724 // disconnected queue or other producer misbehavior, so not a fatal
2725 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002726 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002727 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002728 Mutex::Autolock l(mRequestLock);
2729 if (mListener != NULL) {
2730 mListener->notifyError(
2731 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2732 nextRequest->mResultExtras);
2733 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002734 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2735 return true;
2736 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002737 request.input_buffer = &inputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002738 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002739 } else {
2740 request.input_buffer = NULL;
2741 }
2742
2743 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2744 nextRequest->mOutputStreams.size());
2745 request.output_buffers = outputBuffers.array();
2746 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2747 res = nextRequest->mOutputStreams.editItemAt(i)->
2748 getBuffer(&outputBuffers.editItemAt(i));
2749 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002750 // Can't get output buffer from gralloc queue - this could be due to
2751 // abandoned queue or other consumer misbehavior, so not a fatal
2752 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002753 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2754 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002755 Mutex::Autolock l(mRequestLock);
2756 if (mListener != NULL) {
2757 mListener->notifyError(
2758 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2759 nextRequest->mResultExtras);
2760 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002761 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2762 return true;
2763 }
2764 request.num_output_buffers++;
2765 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002766 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002767
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002768 // Log request in the in-flight queue
2769 sp<Camera3Device> parent = mParent.promote();
2770 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002771 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002772 CLOGE("RequestThread: Parent is gone");
2773 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2774 return false;
2775 }
2776
Jianing Weicb0652e2014-03-12 18:29:36 -07002777 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002778 totalNumBuffers, nextRequest->mResultExtras,
2779 /*hasInput*/request.input_buffer != NULL);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002780 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2781 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002782 __FUNCTION__,
2783 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2784 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002785 if (res != OK) {
2786 SET_ERR("RequestThread: Unable to register new in-flight request:"
2787 " %s (%d)", strerror(-res), res);
2788 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2789 return false;
2790 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002791
Zhijun Hecc27e112013-10-03 16:12:43 -07002792 // Inform waitUntilRequestProcessed thread of a new request ID
2793 {
2794 Mutex::Autolock al(mLatestRequestMutex);
2795
2796 mLatestRequestId = requestId;
2797 mLatestRequestSignal.signal();
2798 }
2799
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002800 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002801 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2802 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002803 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002804 ATRACE_END();
2805
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002806 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002807 // Should only get a failure here for malformed requests or device-level
2808 // errors, so consider all errors fatal. Bad metadata failures should
2809 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002810 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002811 " device: %s (%d)", request.frame_number, strerror(-res), res);
2812 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2813 return false;
2814 }
2815
Igor Murashkin1e479c02013-09-06 16:55:14 -07002816 // Update the latest request sent to HAL
2817 if (request.settings != NULL) { // Don't update them if they were unchanged
2818 Mutex::Autolock al(mLatestRequestMutex);
2819
2820 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2821 mLatestRequest.acquire(cloned);
2822 }
2823
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002824 if (request.settings != NULL) {
2825 nextRequest->mSettings.unlock(request.settings);
2826 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002827
2828 // Remove any previously queued triggers (after unlock)
2829 res = removeTriggers(mPrevRequest);
2830 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002831 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002832 "(capture request %d, HAL device: %s (%d)",
2833 request.frame_number, strerror(-res), res);
2834 return false;
2835 }
2836 mPrevTriggers = triggerCount;
2837
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002838 return true;
2839}
2840
Igor Murashkin1e479c02013-09-06 16:55:14 -07002841CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2842 Mutex::Autolock al(mLatestRequestMutex);
2843
2844 ALOGV("RequestThread::%s", __FUNCTION__);
2845
2846 return mLatestRequest;
2847}
2848
Jianing Weicb0652e2014-03-12 18:29:36 -07002849
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002850void Camera3Device::RequestThread::cleanUpFailedRequest(
2851 camera3_capture_request_t &request,
2852 sp<CaptureRequest> &nextRequest,
2853 Vector<camera3_stream_buffer_t> &outputBuffers) {
2854
2855 if (request.settings != NULL) {
2856 nextRequest->mSettings.unlock(request.settings);
2857 }
2858 if (request.input_buffer != NULL) {
2859 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002860 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002861 }
2862 for (size_t i = 0; i < request.num_output_buffers; i++) {
2863 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2864 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2865 outputBuffers[i], 0);
2866 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002867}
2868
2869sp<Camera3Device::CaptureRequest>
2870 Camera3Device::RequestThread::waitForNextRequest() {
2871 status_t res;
2872 sp<CaptureRequest> nextRequest;
2873
2874 // Optimized a bit for the simple steady-state case (single repeating
2875 // request), to avoid putting that request in the queue temporarily.
2876 Mutex::Autolock l(mRequestLock);
2877
2878 while (mRequestQueue.empty()) {
2879 if (!mRepeatingRequests.empty()) {
2880 // Always atomically enqueue all requests in a repeating request
2881 // list. Guarantees a complete in-sequence set of captures to
2882 // application.
2883 const RequestList &requests = mRepeatingRequests;
2884 RequestList::const_iterator firstRequest =
2885 requests.begin();
2886 nextRequest = *firstRequest;
2887 mRequestQueue.insert(mRequestQueue.end(),
2888 ++firstRequest,
2889 requests.end());
2890 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002891
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002892 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002893
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002894 break;
2895 }
2896
2897 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2898
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002899 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2900 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002901 Mutex::Autolock pl(mPauseLock);
2902 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002903 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002904 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002905 // Let the tracker know
2906 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2907 if (statusTracker != 0) {
2908 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2909 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002910 }
2911 // Stop waiting for now and let thread management happen
2912 return NULL;
2913 }
2914 }
2915
2916 if (nextRequest == NULL) {
2917 // Don't have a repeating request already in hand, so queue
2918 // must have an entry now.
2919 RequestList::iterator firstRequest =
2920 mRequestQueue.begin();
2921 nextRequest = *firstRequest;
2922 mRequestQueue.erase(firstRequest);
2923 }
2924
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002925 // In case we've been unpaused by setPaused clearing mDoPause, need to
2926 // update internal pause state (capture/setRepeatingRequest unpause
2927 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002928 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002929 if (mPaused) {
2930 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2931 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2932 if (statusTracker != 0) {
2933 statusTracker->markComponentActive(mStatusId);
2934 }
2935 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002936 mPaused = false;
2937
2938 // Check if we've reconfigured since last time, and reset the preview
2939 // request if so. Can't use 'NULL request == repeat' across configure calls.
2940 if (mReconfigured) {
2941 mPrevRequest.clear();
2942 mReconfigured = false;
2943 }
2944
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002945 if (nextRequest != NULL) {
2946 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002947 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
2948 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002949 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002950 return nextRequest;
2951}
2952
2953bool Camera3Device::RequestThread::waitIfPaused() {
2954 status_t res;
2955 Mutex::Autolock l(mPauseLock);
2956 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002957 if (mPaused == false) {
2958 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002959 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2960 // Let the tracker know
2961 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2962 if (statusTracker != 0) {
2963 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2964 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002965 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002966
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002967 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002968 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002969 return true;
2970 }
2971 }
2972 // We don't set mPaused to false here, because waitForNextRequest needs
2973 // to further manage the paused state in case of starvation.
2974 return false;
2975}
2976
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002977void Camera3Device::RequestThread::unpauseForNewRequests() {
2978 // With work to do, mark thread as unpaused.
2979 // If paused by request (setPaused), don't resume, to avoid
2980 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002981 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002982 Mutex::Autolock p(mPauseLock);
2983 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002984 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2985 if (mPaused) {
2986 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2987 if (statusTracker != 0) {
2988 statusTracker->markComponentActive(mStatusId);
2989 }
2990 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002991 mPaused = false;
2992 }
2993}
2994
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002995void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2996 sp<Camera3Device> parent = mParent.promote();
2997 if (parent != NULL) {
2998 va_list args;
2999 va_start(args, fmt);
3000
3001 parent->setErrorStateV(fmt, args);
3002
3003 va_end(args);
3004 }
3005}
3006
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003007status_t Camera3Device::RequestThread::insertTriggers(
3008 const sp<CaptureRequest> &request) {
3009
3010 Mutex::Autolock al(mTriggerMutex);
3011
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003012 sp<Camera3Device> parent = mParent.promote();
3013 if (parent == NULL) {
3014 CLOGE("RequestThread: Parent is gone");
3015 return DEAD_OBJECT;
3016 }
3017
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003018 CameraMetadata &metadata = request->mSettings;
3019 size_t count = mTriggerMap.size();
3020
3021 for (size_t i = 0; i < count; ++i) {
3022 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003023 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003024
3025 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3026 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3027 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003028 if (isAeTrigger) {
3029 request->mResultExtras.precaptureTriggerId = triggerId;
3030 mCurrentPreCaptureTriggerId = triggerId;
3031 } else {
3032 request->mResultExtras.afTriggerId = triggerId;
3033 mCurrentAfTriggerId = triggerId;
3034 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003035 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3036 continue; // Trigger ID tag is deprecated since device HAL 3.2
3037 }
3038 }
3039
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003040 camera_metadata_entry entry = metadata.find(tag);
3041
3042 if (entry.count > 0) {
3043 /**
3044 * Already has an entry for this trigger in the request.
3045 * Rewrite it with our requested trigger value.
3046 */
3047 RequestTrigger oldTrigger = trigger;
3048
3049 oldTrigger.entryValue = entry.data.u8[0];
3050
3051 mTriggerReplacedMap.add(tag, oldTrigger);
3052 } else {
3053 /**
3054 * More typical, no trigger entry, so we just add it
3055 */
3056 mTriggerRemovedMap.add(tag, trigger);
3057 }
3058
3059 status_t res;
3060
3061 switch (trigger.getTagType()) {
3062 case TYPE_BYTE: {
3063 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3064 res = metadata.update(tag,
3065 &entryValue,
3066 /*count*/1);
3067 break;
3068 }
3069 case TYPE_INT32:
3070 res = metadata.update(tag,
3071 &trigger.entryValue,
3072 /*count*/1);
3073 break;
3074 default:
3075 ALOGE("%s: Type not supported: 0x%x",
3076 __FUNCTION__,
3077 trigger.getTagType());
3078 return INVALID_OPERATION;
3079 }
3080
3081 if (res != OK) {
3082 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3083 ", value %d", __FUNCTION__, trigger.getTagName(),
3084 trigger.entryValue);
3085 return res;
3086 }
3087
3088 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3089 trigger.getTagName(),
3090 trigger.entryValue);
3091 }
3092
3093 mTriggerMap.clear();
3094
3095 return count;
3096}
3097
3098status_t Camera3Device::RequestThread::removeTriggers(
3099 const sp<CaptureRequest> &request) {
3100 Mutex::Autolock al(mTriggerMutex);
3101
3102 CameraMetadata &metadata = request->mSettings;
3103
3104 /**
3105 * Replace all old entries with their old values.
3106 */
3107 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3108 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3109
3110 status_t res;
3111
3112 uint32_t tag = trigger.metadataTag;
3113 switch (trigger.getTagType()) {
3114 case TYPE_BYTE: {
3115 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3116 res = metadata.update(tag,
3117 &entryValue,
3118 /*count*/1);
3119 break;
3120 }
3121 case TYPE_INT32:
3122 res = metadata.update(tag,
3123 &trigger.entryValue,
3124 /*count*/1);
3125 break;
3126 default:
3127 ALOGE("%s: Type not supported: 0x%x",
3128 __FUNCTION__,
3129 trigger.getTagType());
3130 return INVALID_OPERATION;
3131 }
3132
3133 if (res != OK) {
3134 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3135 ", trigger value %d", __FUNCTION__,
3136 trigger.getTagName(), trigger.entryValue);
3137 return res;
3138 }
3139 }
3140 mTriggerReplacedMap.clear();
3141
3142 /**
3143 * Remove all new entries.
3144 */
3145 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3146 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3147 status_t res = metadata.erase(trigger.metadataTag);
3148
3149 if (res != OK) {
3150 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3151 ", trigger value %d", __FUNCTION__,
3152 trigger.getTagName(), trigger.entryValue);
3153 return res;
3154 }
3155 }
3156 mTriggerRemovedMap.clear();
3157
3158 return OK;
3159}
3160
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003161status_t Camera3Device::RequestThread::addDummyTriggerIds(
3162 const sp<CaptureRequest> &request) {
3163 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3164 static const int32_t dummyTriggerId = 1;
3165 status_t res;
3166
3167 CameraMetadata &metadata = request->mSettings;
3168
3169 // If AF trigger is active, insert a dummy AF trigger ID if none already
3170 // exists
3171 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3172 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3173 if (afTrigger.count > 0 &&
3174 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3175 afId.count == 0) {
3176 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3177 if (res != OK) return res;
3178 }
3179
3180 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3181 // if none already exists
3182 camera_metadata_entry pcTrigger =
3183 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3184 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3185 if (pcTrigger.count > 0 &&
3186 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3187 pcId.count == 0) {
3188 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3189 &dummyTriggerId, 1);
3190 if (res != OK) return res;
3191 }
3192
3193 return OK;
3194}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003195
3196
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003197/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003198 * Static callback forwarding methods from HAL to instance
3199 */
3200
3201void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3202 const camera3_capture_result *result) {
3203 Camera3Device *d =
3204 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3205 d->processCaptureResult(result);
3206}
3207
3208void Camera3Device::sNotify(const camera3_callback_ops *cb,
3209 const camera3_notify_msg *msg) {
3210 Camera3Device *d =
3211 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3212 d->notify(msg);
3213}
3214
3215}; // namespace android