blob: 852751caf36acad758ac09c5e7d0736a32dee881 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Igor Murashkinff3e31d2013-10-23 16:40:06 -070046#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070047#include "device3/Camera3Device.h"
48#include "device3/Camera3OutputStream.h"
49#include "device3/Camera3InputStream.h"
50#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070051#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070052#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080053
54using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080055
56namespace android {
57
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058Camera3Device::Camera3Device(int id):
59 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080060 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070061 mStatus(STATUS_UNINITIALIZED),
Zhijun He204e3292014-07-14 17:09:23 -070062 mUsePartialResult(false),
63 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070064 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070065 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070066 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070067 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080068{
69 ATRACE_CALL();
70 camera3_callback_ops::notify = &sNotify;
71 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
72 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
73}
74
75Camera3Device::~Camera3Device()
76{
77 ATRACE_CALL();
78 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
79 disconnect();
80}
81
Igor Murashkin71381052013-03-04 14:53:08 -080082int Camera3Device::getId() const {
83 return mId;
84}
85
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080086/**
87 * CameraDeviceBase interface
88 */
89
Yin-Chia Yehe074a932015-01-30 10:29:02 -080090status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080091{
92 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070093 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080094 Mutex::Autolock l(mLock);
95
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080096 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080097 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070098 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080099 return INVALID_OPERATION;
100 }
101
102 /** Open HAL device */
103
104 status_t res;
105 String8 deviceName = String8::format("%d", mId);
106
107 camera3_device_t *device;
108
Zhijun He213ce792013-11-19 08:45:15 -0800109 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800110 res = module->open(deviceName.string(),
111 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800112 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800113
114 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700115 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800116 return res;
117 }
118
119 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700120 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700121 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700122 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700123 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800124 device->common.version);
125 device->common.close(&device->common);
126 return BAD_VALUE;
127 }
128
129 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800130 res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700131 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800132 if (res != OK) return res;
133
134 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700135 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
136 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700137 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 device->common.close(&device->common);
139 return BAD_VALUE;
140 }
141
142 /** Initialize device with callback functions */
143
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700144 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800145 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700146 ATRACE_END();
147
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800148 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700149 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
150 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151 device->common.close(&device->common);
152 return BAD_VALUE;
153 }
154
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700155 /** Start up status tracker thread */
156 mStatusTracker = new StatusTracker(this);
157 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
158 if (res != OK) {
159 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
160 strerror(-res), res);
161 device->common.close(&device->common);
162 mStatusTracker.clear();
163 return res;
164 }
165
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800166 /** Start up request queue thread */
167
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700168 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800169 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800170 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700171 SET_ERR_L("Unable to start request queue thread: %s (%d)",
172 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800173 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800174 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800175 return res;
176 }
177
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700178 mPreparerThread = new PreparerThread();
179
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800180 /** Everything is good to go */
181
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700182 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800183 mDeviceInfo = info.static_camera_characteristics;
184 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700185 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800186 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700187 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700188 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700189 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700191 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700192 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
193 camera_metadata_entry partialResultsCount =
194 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
195 if (partialResultsCount.count > 0) {
196 mNumPartialResults = partialResultsCount.data.i32[0];
197 mUsePartialResult = (mNumPartialResults > 1);
198 }
199 } else {
200 camera_metadata_entry partialResultsQuirk =
201 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
202 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
203 mUsePartialResult = true;
204 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700205 }
206
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700207 camera_metadata_entry configs =
208 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
209 for (uint32_t i = 0; i < configs.count; i += 4) {
210 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
211 configs.data.i32[i + 3] ==
212 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
213 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
214 configs.data.i32[i + 2]));
215 }
216 }
217
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800218 return OK;
219}
220
221status_t Camera3Device::disconnect() {
222 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700223 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800224
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800225 ALOGV("%s: E", __FUNCTION__);
226
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700227 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800228
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700229 {
230 Mutex::Autolock l(mLock);
231 if (mStatus == STATUS_UNINITIALIZED) return res;
232
233 if (mStatus == STATUS_ACTIVE ||
234 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
235 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700236 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700237 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700238 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700239 } else {
240 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
241 if (res != OK) {
242 SET_ERR_L("Timeout waiting for HAL to drain");
243 // Continue to close device even in case of error
244 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700245 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800246 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800247
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700248 if (mStatus == STATUS_ERROR) {
249 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700250 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700251
252 if (mStatusTracker != NULL) {
253 mStatusTracker->requestExit();
254 }
255
256 if (mRequestThread != NULL) {
257 mRequestThread->requestExit();
258 }
259
260 mOutputStreams.clear();
261 mInputStream.clear();
262 }
263
264 // Joining done without holding mLock, otherwise deadlocks may ensue
265 // as the threads try to access parent state
266 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
267 // HAL may be in a bad state, so waiting for request thread
268 // (which may be stuck in the HAL processCaptureRequest call)
269 // could be dangerous.
270 mRequestThread->join();
271 }
272
273 if (mStatusTracker != NULL) {
274 mStatusTracker->join();
275 }
276
277 {
278 Mutex::Autolock l(mLock);
279
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800280 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700281 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800282
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700283 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800284 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700285 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800286 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700287 mHal3Device = NULL;
288 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800289
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700290 mStatus = STATUS_UNINITIALIZED;
291 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800292
293 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700294 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800295}
296
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700297// For dumping/debugging only -
298// try to acquire a lock a few times, eventually give up to proceed with
299// debug/dump operations
300bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
301 bool gotLock = false;
302 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
303 if (lock.tryLock() == NO_ERROR) {
304 gotLock = true;
305 break;
306 } else {
307 usleep(kDumpSleepDuration);
308 }
309 }
310 return gotLock;
311}
312
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700313Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
314 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
315 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
316 const int STREAM_CONFIGURATION_SIZE = 4;
317 const int STREAM_FORMAT_OFFSET = 0;
318 const int STREAM_WIDTH_OFFSET = 1;
319 const int STREAM_HEIGHT_OFFSET = 2;
320 const int STREAM_IS_INPUT_OFFSET = 3;
321 camera_metadata_ro_entry_t availableStreamConfigs =
322 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
323 if (availableStreamConfigs.count == 0 ||
324 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
325 return Size(0, 0);
326 }
327
328 // Get max jpeg size (area-wise).
329 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
330 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
331 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
332 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
333 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
334 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
335 && format == HAL_PIXEL_FORMAT_BLOB &&
336 (width * height > maxJpegWidth * maxJpegHeight)) {
337 maxJpegWidth = width;
338 maxJpegHeight = height;
339 }
340 }
341 } else {
342 camera_metadata_ro_entry availableJpegSizes =
343 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
344 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
345 return Size(0, 0);
346 }
347
348 // Get max jpeg size (area-wise).
349 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
350 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
351 > (maxJpegWidth * maxJpegHeight)) {
352 maxJpegWidth = availableJpegSizes.data.i32[i];
353 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
354 }
355 }
356 }
357 return Size(maxJpegWidth, maxJpegHeight);
358}
359
Zhijun Hef7da0962014-04-24 13:27:56 -0700360ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700361 // Get max jpeg size (area-wise).
362 Size maxJpegResolution = getMaxJpegResolution();
363 if (maxJpegResolution.width == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700364 ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
365 __FUNCTION__, mId);
366 return BAD_VALUE;
367 }
368
Zhijun Hef7da0962014-04-24 13:27:56 -0700369 // Get max jpeg buffer size
370 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700371 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
372 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700373 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
374 return BAD_VALUE;
375 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700376 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800377 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700378
379 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700380 float scaleFactor = ((float) (width * height)) /
381 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800382 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
383 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700384 if (jpegBufferSize > maxJpegBufferSize) {
385 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700386 }
387
388 return jpegBufferSize;
389}
390
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800391status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
392 ATRACE_CALL();
393 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700394
395 // Try to lock, but continue in case of failure (to avoid blocking in
396 // deadlocks)
397 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
398 bool gotLock = tryLockSpinRightRound(mLock);
399
400 ALOGW_IF(!gotInterfaceLock,
401 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
402 mId, __FUNCTION__);
403 ALOGW_IF(!gotLock,
404 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
405 mId, __FUNCTION__);
406
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800407 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800408
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800409 const char *status =
410 mStatus == STATUS_ERROR ? "ERROR" :
411 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700412 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
413 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800414 mStatus == STATUS_ACTIVE ? "ACTIVE" :
415 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700416
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800417 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700418 if (mStatus == STATUS_ERROR) {
419 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
420 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800421 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700422 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
423 "CONSTAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800424
425 if (mInputStream != NULL) {
426 write(fd, lines.string(), lines.size());
427 mInputStream->dump(fd, args);
428 } else {
429 lines.appendFormat(" No input stream.\n");
430 write(fd, lines.string(), lines.size());
431 }
432 for (size_t i = 0; i < mOutputStreams.size(); i++) {
433 mOutputStreams[i]->dump(fd,args);
434 }
435
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700436 lines = String8(" In-flight requests:\n");
437 if (mInFlightMap.size() == 0) {
438 lines.append(" None\n");
439 } else {
440 for (size_t i = 0; i < mInFlightMap.size(); i++) {
441 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700442 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700443 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800444 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700445 r.numBuffersLeft);
446 }
447 }
448 write(fd, lines.string(), lines.size());
449
Igor Murashkin1e479c02013-09-06 16:55:14 -0700450 {
451 lines = String8(" Last request sent:\n");
452 write(fd, lines.string(), lines.size());
453
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700454 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700455 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
456 }
457
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800458 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700459 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800460 write(fd, lines.string(), lines.size());
461 mHal3Device->ops->dump(mHal3Device, fd);
462 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800463
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700464 if (gotLock) mLock.unlock();
465 if (gotInterfaceLock) mInterfaceLock.unlock();
466
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800467 return OK;
468}
469
470const CameraMetadata& Camera3Device::info() const {
471 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800472 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
473 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700474 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800475 mStatus == STATUS_ERROR ?
476 "when in error state" : "before init");
477 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800478 return mDeviceInfo;
479}
480
Jianing Wei90e59c92014-03-12 18:29:36 -0700481status_t Camera3Device::checkStatusOkToCaptureLocked() {
482 switch (mStatus) {
483 case STATUS_ERROR:
484 CLOGE("Device has encountered a serious error");
485 return INVALID_OPERATION;
486 case STATUS_UNINITIALIZED:
487 CLOGE("Device not initialized");
488 return INVALID_OPERATION;
489 case STATUS_UNCONFIGURED:
490 case STATUS_CONFIGURED:
491 case STATUS_ACTIVE:
492 // OK
493 break;
494 default:
495 SET_ERR_L("Unexpected status: %d", mStatus);
496 return INVALID_OPERATION;
497 }
498 return OK;
499}
500
501status_t Camera3Device::convertMetadataListToRequestListLocked(
502 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
503 if (requestList == NULL) {
504 CLOGE("requestList cannot be NULL.");
505 return BAD_VALUE;
506 }
507
Jianing Weicb0652e2014-03-12 18:29:36 -0700508 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700509 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
510 it != metadataList.end(); ++it) {
511 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
512 if (newRequest == 0) {
513 CLOGE("Can't create capture request");
514 return BAD_VALUE;
515 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700516
517 // Setup burst Id and request Id
518 newRequest->mResultExtras.burstId = burstId++;
519 if (it->exists(ANDROID_REQUEST_ID)) {
520 if (it->find(ANDROID_REQUEST_ID).count == 0) {
521 CLOGE("RequestID entry exists; but must not be empty in metadata");
522 return BAD_VALUE;
523 }
524 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
525 } else {
526 CLOGE("RequestID does not exist in metadata");
527 return BAD_VALUE;
528 }
529
Jianing Wei90e59c92014-03-12 18:29:36 -0700530 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700531
532 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700533 }
534 return OK;
535}
536
Jianing Weicb0652e2014-03-12 18:29:36 -0700537status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800538 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800539
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700540 List<const CameraMetadata> requests;
541 requests.push_back(request);
542 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800543}
544
Jianing Wei90e59c92014-03-12 18:29:36 -0700545status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700546 const List<const CameraMetadata> &requests, bool repeating,
547 /*out*/
548 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700549 ATRACE_CALL();
550 Mutex::Autolock il(mInterfaceLock);
551 Mutex::Autolock l(mLock);
552
553 status_t res = checkStatusOkToCaptureLocked();
554 if (res != OK) {
555 // error logged by previous call
556 return res;
557 }
558
559 RequestList requestList;
560
561 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
562 if (res != OK) {
563 // error logged by previous call
564 return res;
565 }
566
567 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700568 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700569 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700570 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700571 }
572
573 if (res == OK) {
574 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
575 if (res != OK) {
576 SET_ERR_L("Can't transition to active in %f seconds!",
577 kActiveTimeout/1e9);
578 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700579 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
580 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700581 } else {
582 CLOGE("Cannot queue request. Impossible.");
583 return BAD_VALUE;
584 }
585
586 return res;
587}
588
Jianing Weicb0652e2014-03-12 18:29:36 -0700589status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
590 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700591 ATRACE_CALL();
592
Jianing Weicb0652e2014-03-12 18:29:36 -0700593 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700594}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800595
Jianing Weicb0652e2014-03-12 18:29:36 -0700596status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
597 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800598 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800599
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700600 List<const CameraMetadata> requests;
601 requests.push_back(request);
602 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800603}
604
Jianing Weicb0652e2014-03-12 18:29:36 -0700605status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
606 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700607 ATRACE_CALL();
608
Jianing Weicb0652e2014-03-12 18:29:36 -0700609 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700610}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800611
612sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
613 const CameraMetadata &request) {
614 status_t res;
615
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700616 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800617 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700618 // Stream configuration failed due to unsupported configuration.
619 // Device back to unconfigured state. Client might try other configuraitons
620 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
621 CLOGE("No streams configured");
622 return NULL;
623 }
624 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800625 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700626 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800627 return NULL;
628 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700629 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700630 if (mStatus == STATUS_UNCONFIGURED) {
631 CLOGE("No streams configured");
632 return NULL;
633 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800634 }
635
636 sp<CaptureRequest> newRequest = createCaptureRequest(request);
637 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800638}
639
Jianing Weicb0652e2014-03-12 18:29:36 -0700640status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800641 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700642 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800643 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800644
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800645 switch (mStatus) {
646 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700647 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800648 return INVALID_OPERATION;
649 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700650 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800651 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700652 case STATUS_UNCONFIGURED:
653 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800654 case STATUS_ACTIVE:
655 // OK
656 break;
657 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700658 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800659 return INVALID_OPERATION;
660 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700661 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700662
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700663 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800664}
665
666status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
667 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700668 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800669
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700670 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800671}
672
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700673status_t Camera3Device::createInputStream(
674 uint32_t width, uint32_t height, int format, int *id) {
675 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700676 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700677 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700678 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
679 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700680
681 status_t res;
682 bool wasActive = false;
683
684 switch (mStatus) {
685 case STATUS_ERROR:
686 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
687 return INVALID_OPERATION;
688 case STATUS_UNINITIALIZED:
689 ALOGE("%s: Device not initialized", __FUNCTION__);
690 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700691 case STATUS_UNCONFIGURED:
692 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700693 // OK
694 break;
695 case STATUS_ACTIVE:
696 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700697 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700698 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700699 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700700 return res;
701 }
702 wasActive = true;
703 break;
704 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700705 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700706 return INVALID_OPERATION;
707 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700708 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700709
710 if (mInputStream != 0) {
711 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
712 return INVALID_OPERATION;
713 }
714
715 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
716 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700717 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700718
719 mInputStream = newStream;
720
721 *id = mNextStreamId++;
722
723 // Continue captures if active at start
724 if (wasActive) {
725 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
726 res = configureStreamsLocked();
727 if (res != OK) {
728 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
729 __FUNCTION__, mNextStreamId, strerror(-res), res);
730 return res;
731 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700732 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700733 }
734
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700735 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700736 return OK;
737}
738
Igor Murashkin2fba5842013-04-22 14:03:54 -0700739
740status_t Camera3Device::createZslStream(
741 uint32_t width, uint32_t height,
742 int depth,
743 /*out*/
744 int *id,
745 sp<Camera3ZslStream>* zslStream) {
746 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700747 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700748 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700749 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
750 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700751
752 status_t res;
753 bool wasActive = false;
754
755 switch (mStatus) {
756 case STATUS_ERROR:
757 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
758 return INVALID_OPERATION;
759 case STATUS_UNINITIALIZED:
760 ALOGE("%s: Device not initialized", __FUNCTION__);
761 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700762 case STATUS_UNCONFIGURED:
763 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700764 // OK
765 break;
766 case STATUS_ACTIVE:
767 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700768 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700769 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700770 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700771 return res;
772 }
773 wasActive = true;
774 break;
775 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700776 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700777 return INVALID_OPERATION;
778 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700779 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700780
781 if (mInputStream != 0) {
782 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
783 return INVALID_OPERATION;
784 }
785
786 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
787 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700788 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700789
790 res = mOutputStreams.add(mNextStreamId, newStream);
791 if (res < 0) {
792 ALOGE("%s: Can't add new stream to set: %s (%d)",
793 __FUNCTION__, strerror(-res), res);
794 return res;
795 }
796 mInputStream = newStream;
797
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530798 mNeedConfig = true;
799
Igor Murashkin2fba5842013-04-22 14:03:54 -0700800 *id = mNextStreamId++;
801 *zslStream = newStream;
802
803 // Continue captures if active at start
804 if (wasActive) {
805 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
806 res = configureStreamsLocked();
807 if (res != OK) {
808 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
809 __FUNCTION__, mNextStreamId, strerror(-res), res);
810 return res;
811 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700812 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700813 }
814
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700815 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700816 return OK;
817}
818
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800819status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800820 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700821 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800822 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700823 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800824 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700825 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
826 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800827
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800828 status_t res;
829 bool wasActive = false;
830
831 switch (mStatus) {
832 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700833 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800834 return INVALID_OPERATION;
835 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700836 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800837 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700838 case STATUS_UNCONFIGURED:
839 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800840 // OK
841 break;
842 case STATUS_ACTIVE:
843 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700844 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800845 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700846 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800847 return res;
848 }
849 wasActive = true;
850 break;
851 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700852 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800853 return INVALID_OPERATION;
854 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700855 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800856
857 sp<Camera3OutputStream> newStream;
858 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700859 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700860 if (jpegBufferSize <= 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700861 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
862 return BAD_VALUE;
863 }
864
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800865 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700866 width, height, jpegBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800867 } else {
868 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700869 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800870 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700871 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800872
873 res = mOutputStreams.add(mNextStreamId, newStream);
874 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700875 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800876 return res;
877 }
878
879 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700880 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800881
882 // Continue captures if active at start
883 if (wasActive) {
884 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
885 res = configureStreamsLocked();
886 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700887 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
888 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800889 return res;
890 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700891 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800892 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700893 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800894 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800895}
896
897status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
898 ATRACE_CALL();
899 (void)outputId; (void)id;
900
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700901 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800902 return INVALID_OPERATION;
903}
904
905
906status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700907 uint32_t *width, uint32_t *height,
908 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800909 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700910 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800911 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800912
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800913 switch (mStatus) {
914 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700915 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800916 return INVALID_OPERATION;
917 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700918 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800919 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700920 case STATUS_UNCONFIGURED:
921 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800922 case STATUS_ACTIVE:
923 // OK
924 break;
925 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700926 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800927 return INVALID_OPERATION;
928 }
929
930 ssize_t idx = mOutputStreams.indexOfKey(id);
931 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700932 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800933 return idx;
934 }
935
936 if (width) *width = mOutputStreams[idx]->getWidth();
937 if (height) *height = mOutputStreams[idx]->getHeight();
938 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700939 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800940 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800941}
942
943status_t Camera3Device::setStreamTransform(int id,
944 int transform) {
945 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700946 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800947 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800948
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800949 switch (mStatus) {
950 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700951 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800952 return INVALID_OPERATION;
953 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700954 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800955 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700956 case STATUS_UNCONFIGURED:
957 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800958 case STATUS_ACTIVE:
959 // OK
960 break;
961 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700962 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800963 return INVALID_OPERATION;
964 }
965
966 ssize_t idx = mOutputStreams.indexOfKey(id);
967 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700968 CLOGE("Stream %d does not exist",
969 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800970 return BAD_VALUE;
971 }
972
973 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800974}
975
976status_t Camera3Device::deleteStream(int id) {
977 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700978 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800979 Mutex::Autolock l(mLock);
980 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800981
Igor Murashkine2172be2013-05-28 15:31:39 -0700982 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
983
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800984 // CameraDevice semantics require device to already be idle before
985 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700986 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700987 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
988 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800989 }
990
Igor Murashkin2fba5842013-04-22 14:03:54 -0700991 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800992 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800993 if (mInputStream != NULL && id == mInputStream->getId()) {
994 deletedStream = mInputStream;
995 mInputStream.clear();
996 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800997 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700998 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800999 return BAD_VALUE;
1000 }
Zhijun He5f446352014-01-22 09:49:33 -08001001 }
1002
1003 // Delete output stream or the output part of a bi-directional stream.
1004 if (outputStreamIdx != NAME_NOT_FOUND) {
1005 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001006 mOutputStreams.removeItem(id);
1007 }
1008
1009 // Free up the stream endpoint so that it can be used by some other stream
1010 res = deletedStream->disconnect();
1011 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001012 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001013 // fall through since we want to still list the stream as deleted.
1014 }
1015 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001016 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001017
1018 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001019}
1020
1021status_t Camera3Device::deleteReprocessStream(int id) {
1022 ATRACE_CALL();
1023 (void)id;
1024
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001025 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001026 return INVALID_OPERATION;
1027}
1028
Zhijun He1fa89992015-06-01 15:44:31 -07001029status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001030 ATRACE_CALL();
1031 ALOGV("%s: E", __FUNCTION__);
1032
1033 Mutex::Autolock il(mInterfaceLock);
1034 Mutex::Autolock l(mLock);
Zhijun He1fa89992015-06-01 15:44:31 -07001035 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
Igor Murashkine2d167e2014-08-19 16:19:59 -07001036
1037 return configureStreamsLocked();
1038}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001039
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001040status_t Camera3Device::getInputBufferProducer(
1041 sp<IGraphicBufferProducer> *producer) {
1042 Mutex::Autolock il(mInterfaceLock);
1043 Mutex::Autolock l(mLock);
1044
1045 if (producer == NULL) {
1046 return BAD_VALUE;
1047 } else if (mInputStream == NULL) {
1048 return INVALID_OPERATION;
1049 }
1050
1051 return mInputStream->getInputBufferProducer(producer);
1052}
1053
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001054status_t Camera3Device::createDefaultRequest(int templateId,
1055 CameraMetadata *request) {
1056 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001057 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001058 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001059 Mutex::Autolock l(mLock);
1060
1061 switch (mStatus) {
1062 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001063 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001064 return INVALID_OPERATION;
1065 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001066 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001067 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001068 case STATUS_UNCONFIGURED:
1069 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001070 case STATUS_ACTIVE:
1071 // OK
1072 break;
1073 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001074 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001075 return INVALID_OPERATION;
1076 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001077
Zhijun Hea1530f12014-09-14 12:44:20 -07001078 if (!mRequestTemplateCache[templateId].isEmpty()) {
1079 *request = mRequestTemplateCache[templateId];
1080 return OK;
1081 }
1082
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001083 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001084 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001085 rawRequest = mHal3Device->ops->construct_default_request_settings(
1086 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001087 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001088 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001089 ALOGI("%s: template %d is not supported on this camera device",
1090 __FUNCTION__, templateId);
1091 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001092 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001093 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001094 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001095
1096 return OK;
1097}
1098
1099status_t Camera3Device::waitUntilDrained() {
1100 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001101 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001102 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001103
Zhijun He69a37482014-03-23 18:44:49 -07001104 return waitUntilDrainedLocked();
1105}
1106
1107status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001108 switch (mStatus) {
1109 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001110 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001111 ALOGV("%s: Already idle", __FUNCTION__);
1112 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001113 case STATUS_CONFIGURED:
1114 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001115 case STATUS_ERROR:
1116 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001117 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001118 break;
1119 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001120 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001121 return INVALID_OPERATION;
1122 }
1123
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001124 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1125 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001126 if (res != OK) {
1127 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1128 res);
1129 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001130 return res;
1131}
1132
1133// Pause to reconfigure
1134status_t Camera3Device::internalPauseAndWaitLocked() {
1135 mRequestThread->setPaused(true);
1136 mPauseStateNotify = true;
1137
1138 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1139 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1140 if (res != OK) {
1141 SET_ERR_L("Can't idle device in %f seconds!",
1142 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001143 }
1144
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001145 return res;
1146}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001147
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001148// Resume after internalPauseAndWaitLocked
1149status_t Camera3Device::internalResumeLocked() {
1150 status_t res;
1151
1152 mRequestThread->setPaused(false);
1153
1154 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1155 if (res != OK) {
1156 SET_ERR_L("Can't transition to active in %f seconds!",
1157 kActiveTimeout/1e9);
1158 }
1159 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001160 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001161}
1162
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001163status_t Camera3Device::waitUntilStateThenRelock(bool active,
1164 nsecs_t timeout) {
1165 status_t res = OK;
1166 if (active == (mStatus == STATUS_ACTIVE)) {
1167 // Desired state already reached
1168 return res;
1169 }
1170
1171 bool stateSeen = false;
1172 do {
1173 mRecentStatusUpdates.clear();
1174
1175 res = mStatusChanged.waitRelative(mLock, timeout);
1176 if (res != OK) break;
1177
1178 // Check state change history during wait
1179 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1180 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1181 stateSeen = true;
1182 break;
1183 }
1184 }
1185 } while (!stateSeen);
1186
1187 return res;
1188}
1189
1190
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001191status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1192 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001193 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001194
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001195 if (listener != NULL && mListener != NULL) {
1196 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1197 }
1198 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001199 mRequestThread->setNotificationListener(listener);
1200 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001201
1202 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001203}
1204
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001205bool Camera3Device::willNotify3A() {
1206 return false;
1207}
1208
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001209status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001210 status_t res;
1211 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001212
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001213 while (mResultQueue.empty()) {
1214 res = mResultSignal.waitRelative(mOutputLock, timeout);
1215 if (res == TIMED_OUT) {
1216 return res;
1217 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001218 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001219 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001220 return res;
1221 }
1222 }
1223 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001224}
1225
Jianing Weicb0652e2014-03-12 18:29:36 -07001226status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001227 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001228 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001229
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001230 if (mResultQueue.empty()) {
1231 return NOT_ENOUGH_DATA;
1232 }
1233
Jianing Weicb0652e2014-03-12 18:29:36 -07001234 if (frame == NULL) {
1235 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1236 return BAD_VALUE;
1237 }
1238
1239 CaptureResult &result = *(mResultQueue.begin());
1240 frame->mResultExtras = result.mResultExtras;
1241 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001242 mResultQueue.erase(mResultQueue.begin());
1243
1244 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001245}
1246
1247status_t Camera3Device::triggerAutofocus(uint32_t id) {
1248 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001249 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001250
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001251 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1252 // Mix-in this trigger into the next request and only the next request.
1253 RequestTrigger trigger[] = {
1254 {
1255 ANDROID_CONTROL_AF_TRIGGER,
1256 ANDROID_CONTROL_AF_TRIGGER_START
1257 },
1258 {
1259 ANDROID_CONTROL_AF_TRIGGER_ID,
1260 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001261 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001262 };
1263
1264 return mRequestThread->queueTrigger(trigger,
1265 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001266}
1267
1268status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1269 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001270 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001271
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001272 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1273 // Mix-in this trigger into the next request and only the next request.
1274 RequestTrigger trigger[] = {
1275 {
1276 ANDROID_CONTROL_AF_TRIGGER,
1277 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1278 },
1279 {
1280 ANDROID_CONTROL_AF_TRIGGER_ID,
1281 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001282 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001283 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001284
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001285 return mRequestThread->queueTrigger(trigger,
1286 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001287}
1288
1289status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1290 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001291 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001292
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001293 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1294 // Mix-in this trigger into the next request and only the next request.
1295 RequestTrigger trigger[] = {
1296 {
1297 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1298 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1299 },
1300 {
1301 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1302 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001303 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001304 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001305
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001306 return mRequestThread->queueTrigger(trigger,
1307 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001308}
1309
1310status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1311 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1312 ATRACE_CALL();
1313 (void)reprocessStreamId; (void)buffer; (void)listener;
1314
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001315 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001316 return INVALID_OPERATION;
1317}
1318
Jianing Weicb0652e2014-03-12 18:29:36 -07001319status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001320 ATRACE_CALL();
1321 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001322 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001323
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001324 NotificationListener* listener;
1325 {
1326 Mutex::Autolock l(mOutputLock);
1327 listener = mListener;
1328 }
1329
Zhijun He7ef20392014-04-21 16:04:17 -07001330 {
1331 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001332 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001333 }
1334
Zhijun He491e3412013-12-27 10:57:44 -08001335 status_t res;
1336 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1337 res = mHal3Device->ops->flush(mHal3Device);
1338 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001339 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001340 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001341 }
1342
1343 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001344}
1345
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001346status_t Camera3Device::prepare(int streamId) {
1347 ATRACE_CALL();
1348 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001349 Mutex::Autolock il(mInterfaceLock);
1350 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001351
1352 sp<Camera3StreamInterface> stream;
1353 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1354 if (outputStreamIdx == NAME_NOT_FOUND) {
1355 CLOGE("Stream %d does not exist", streamId);
1356 return BAD_VALUE;
1357 }
1358
1359 stream = mOutputStreams.editValueAt(outputStreamIdx);
1360
1361 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001362 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001363 return BAD_VALUE;
1364 }
1365
1366 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001367 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001368 return BAD_VALUE;
1369 }
1370
1371 return mPreparerThread->prepare(stream);
1372}
1373
Zhijun He204e3292014-07-14 17:09:23 -07001374uint32_t Camera3Device::getDeviceVersion() {
1375 ATRACE_CALL();
1376 Mutex::Autolock il(mInterfaceLock);
1377 return mDeviceVersion;
1378}
1379
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001380/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001381 * Methods called by subclasses
1382 */
1383
1384void Camera3Device::notifyStatus(bool idle) {
1385 {
1386 // Need mLock to safely update state and synchronize to current
1387 // state of methods in flight.
1388 Mutex::Autolock l(mLock);
1389 // We can get various system-idle notices from the status tracker
1390 // while starting up. Only care about them if we've actually sent
1391 // in some requests recently.
1392 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1393 return;
1394 }
1395 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1396 idle ? "idle" : "active");
1397 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1398 mRecentStatusUpdates.add(mStatus);
1399 mStatusChanged.signal();
1400
1401 // Skip notifying listener if we're doing some user-transparent
1402 // state changes
1403 if (mPauseStateNotify) return;
1404 }
1405 NotificationListener *listener;
1406 {
1407 Mutex::Autolock l(mOutputLock);
1408 listener = mListener;
1409 }
1410 if (idle && listener != NULL) {
1411 listener->notifyIdle();
1412 }
1413}
1414
1415/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001416 * Camera3Device private methods
1417 */
1418
1419sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1420 const CameraMetadata &request) {
1421 ATRACE_CALL();
1422 status_t res;
1423
1424 sp<CaptureRequest> newRequest = new CaptureRequest;
1425 newRequest->mSettings = request;
1426
1427 camera_metadata_entry_t inputStreams =
1428 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1429 if (inputStreams.count > 0) {
1430 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001431 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001432 CLOGE("Request references unknown input stream %d",
1433 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001434 return NULL;
1435 }
1436 // Lazy completion of stream configuration (allocation/registration)
1437 // on first use
1438 if (mInputStream->isConfiguring()) {
1439 res = mInputStream->finishConfiguration(mHal3Device);
1440 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001441 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001442 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001443 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001444 return NULL;
1445 }
1446 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001447 // Check if stream is being prepared
1448 if (mInputStream->isPreparing()) {
1449 CLOGE("Request references an input stream that's being prepared!");
1450 return NULL;
1451 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001452
1453 newRequest->mInputStream = mInputStream;
1454 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1455 }
1456
1457 camera_metadata_entry_t streams =
1458 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1459 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001460 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001461 return NULL;
1462 }
1463
1464 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001465 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001466 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001467 CLOGE("Request references unknown stream %d",
1468 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001469 return NULL;
1470 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001471 sp<Camera3OutputStreamInterface> stream =
1472 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001473
1474 // Lazy completion of stream configuration (allocation/registration)
1475 // on first use
1476 if (stream->isConfiguring()) {
1477 res = stream->finishConfiguration(mHal3Device);
1478 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001479 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1480 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001481 return NULL;
1482 }
1483 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001484 // Check if stream is being prepared
1485 if (stream->isPreparing()) {
1486 CLOGE("Request references an output stream that's being prepared!");
1487 return NULL;
1488 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001489
1490 newRequest->mOutputStreams.push(stream);
1491 }
1492 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1493
1494 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001495}
1496
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001497bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1498 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1499 Size size = mSupportedOpaqueInputSizes[i];
1500 if (size.width == width && size.height == height) {
1501 return true;
1502 }
1503 }
1504
1505 return false;
1506}
1507
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001508status_t Camera3Device::configureStreamsLocked() {
1509 ATRACE_CALL();
1510 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001511
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001512 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001513 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001514 return INVALID_OPERATION;
1515 }
1516
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001517 if (!mNeedConfig) {
1518 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1519 return OK;
1520 }
1521
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001522 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1523 // adding a dummy stream instead.
1524 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1525 if (mOutputStreams.size() == 0) {
1526 addDummyStreamLocked();
1527 } else {
1528 tryRemoveDummyStreamLocked();
1529 }
1530
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001531 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001532 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001533
1534 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001535 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1536 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1537 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001538 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1539
1540 Vector<camera3_stream_t*> streams;
1541 streams.setCapacity(config.num_streams);
1542
1543 if (mInputStream != NULL) {
1544 camera3_stream_t *inputStream;
1545 inputStream = mInputStream->startConfiguration();
1546 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001547 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001548 return INVALID_OPERATION;
1549 }
1550 streams.add(inputStream);
1551 }
1552
1553 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001554
1555 // Don't configure bidi streams twice, nor add them twice to the list
1556 if (mOutputStreams[i].get() ==
1557 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1558
1559 config.num_streams--;
1560 continue;
1561 }
1562
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001563 camera3_stream_t *outputStream;
1564 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1565 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001566 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001567 return INVALID_OPERATION;
1568 }
1569 streams.add(outputStream);
1570 }
1571
1572 config.streams = streams.editArray();
1573
1574 // Do the HAL configuration; will potentially touch stream
1575 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001576 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001577 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001578 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001579
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001580 if (res == BAD_VALUE) {
1581 // HAL rejected this set of streams as unsupported, clean up config
1582 // attempt and return to unconfigured state
1583 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1584 res = mInputStream->cancelConfiguration();
1585 if (res != OK) {
1586 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1587 mInputStream->getId(), strerror(-res), res);
1588 return res;
1589 }
1590 }
1591
1592 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1593 sp<Camera3OutputStreamInterface> outputStream =
1594 mOutputStreams.editValueAt(i);
1595 if (outputStream->isConfiguring()) {
1596 res = outputStream->cancelConfiguration();
1597 if (res != OK) {
1598 SET_ERR_L(
1599 "Can't cancel configuring output stream %d: %s (%d)",
1600 outputStream->getId(), strerror(-res), res);
1601 return res;
1602 }
1603 }
1604 }
1605
1606 // Return state to that at start of call, so that future configures
1607 // properly clean things up
1608 mStatus = STATUS_UNCONFIGURED;
1609 mNeedConfig = true;
1610
1611 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1612 return BAD_VALUE;
1613 } else if (res != OK) {
1614 // Some other kind of error from configure_streams - this is not
1615 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001616 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1617 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001618 return res;
1619 }
1620
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001621 // Finish all stream configuration immediately.
1622 // TODO: Try to relax this later back to lazy completion, which should be
1623 // faster
1624
Igor Murashkin073f8572013-05-02 14:59:28 -07001625 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001626 res = mInputStream->finishConfiguration(mHal3Device);
1627 if (res != OK) {
1628 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1629 mInputStream->getId(), strerror(-res), res);
1630 return res;
1631 }
1632 }
1633
1634 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001635 sp<Camera3OutputStreamInterface> outputStream =
1636 mOutputStreams.editValueAt(i);
1637 if (outputStream->isConfiguring()) {
1638 res = outputStream->finishConfiguration(mHal3Device);
1639 if (res != OK) {
1640 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1641 outputStream->getId(), strerror(-res), res);
1642 return res;
1643 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001644 }
1645 }
1646
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001647 // Request thread needs to know to avoid using repeat-last-settings protocol
1648 // across configure_streams() calls
1649 mRequestThread->configurationComplete();
1650
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001651 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001652
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001653 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001654
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001655 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001656 mStatus = STATUS_CONFIGURED;
1657 } else {
1658 mStatus = STATUS_UNCONFIGURED;
1659 }
1660
1661 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1662
Zhijun He0a210512014-07-24 13:45:15 -07001663 // tear down the deleted streams after configure streams.
1664 mDeletedStreams.clear();
1665
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001666 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001667}
1668
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001669status_t Camera3Device::addDummyStreamLocked() {
1670 ATRACE_CALL();
1671 status_t res;
1672
1673 if (mDummyStreamId != NO_STREAM) {
1674 // Should never be adding a second dummy stream when one is already
1675 // active
1676 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1677 __FUNCTION__, mId);
1678 return INVALID_OPERATION;
1679 }
1680
1681 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1682
1683 sp<Camera3OutputStreamInterface> dummyStream =
1684 new Camera3DummyStream(mNextStreamId);
1685
1686 res = mOutputStreams.add(mNextStreamId, dummyStream);
1687 if (res < 0) {
1688 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1689 return res;
1690 }
1691
1692 mDummyStreamId = mNextStreamId;
1693 mNextStreamId++;
1694
1695 return OK;
1696}
1697
1698status_t Camera3Device::tryRemoveDummyStreamLocked() {
1699 ATRACE_CALL();
1700 status_t res;
1701
1702 if (mDummyStreamId == NO_STREAM) return OK;
1703 if (mOutputStreams.size() == 1) return OK;
1704
1705 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1706
1707 // Ok, have a dummy stream and there's at least one other output stream,
1708 // so remove the dummy
1709
1710 sp<Camera3StreamInterface> deletedStream;
1711 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1712 if (outputStreamIdx == NAME_NOT_FOUND) {
1713 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1714 return INVALID_OPERATION;
1715 }
1716
1717 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1718 mOutputStreams.removeItemsAt(outputStreamIdx);
1719
1720 // Free up the stream endpoint so that it can be used by some other stream
1721 res = deletedStream->disconnect();
1722 if (res != OK) {
1723 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1724 // fall through since we want to still list the stream as deleted.
1725 }
1726 mDeletedStreams.add(deletedStream);
1727 mDummyStreamId = NO_STREAM;
1728
1729 return res;
1730}
1731
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001732void Camera3Device::setErrorState(const char *fmt, ...) {
1733 Mutex::Autolock l(mLock);
1734 va_list args;
1735 va_start(args, fmt);
1736
1737 setErrorStateLockedV(fmt, args);
1738
1739 va_end(args);
1740}
1741
1742void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1743 Mutex::Autolock l(mLock);
1744 setErrorStateLockedV(fmt, args);
1745}
1746
1747void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1748 va_list args;
1749 va_start(args, fmt);
1750
1751 setErrorStateLockedV(fmt, args);
1752
1753 va_end(args);
1754}
1755
1756void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001757 // Print out all error messages to log
1758 String8 errorCause = String8::formatV(fmt, args);
1759 ALOGE("Camera %d: %s", mId, errorCause.string());
1760
1761 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001762 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001763
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001764 mErrorCause = errorCause;
1765
1766 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001767 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001768
1769 // Notify upstream about a device error
1770 if (mListener != NULL) {
1771 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1772 CaptureResultExtras());
1773 }
1774
1775 // Save stack trace. View by dumping it later.
1776 CameraTraces::saveTrace();
1777 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001778}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001779
1780/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001781 * In-flight request management
1782 */
1783
Jianing Weicb0652e2014-03-12 18:29:36 -07001784status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001785 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001786 ATRACE_CALL();
1787 Mutex::Autolock l(mInFlightLock);
1788
1789 ssize_t res;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001790 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001791 if (res < 0) return res;
1792
1793 return OK;
1794}
1795
1796/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001797 * Check if all 3A fields are ready, and send off a partial 3A-only result
1798 * to the output frame queue
1799 */
Zhijun He204e3292014-07-14 17:09:23 -07001800bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001801 uint32_t frameNumber,
1802 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001803
1804 // Check if all 3A states are present
1805 // The full list of fields is
1806 // android.control.afMode
1807 // android.control.awbMode
1808 // android.control.aeState
1809 // android.control.awbState
1810 // android.control.afState
1811 // android.control.afTriggerID
1812 // android.control.aePrecaptureID
1813 // TODO: Add android.control.aeMode
1814
1815 bool gotAllStates = true;
1816
1817 uint8_t afMode;
1818 uint8_t awbMode;
1819 uint8_t aeState;
1820 uint8_t afState;
1821 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001822
1823 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1824 &afMode, frameNumber);
1825
1826 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1827 &awbMode, frameNumber);
1828
1829 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1830 &aeState, frameNumber);
1831
1832 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1833 &afState, frameNumber);
1834
1835 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1836 &awbState, frameNumber);
1837
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001838 if (!gotAllStates) return false;
1839
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001840 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001841 "AF state %d, AE state %d, AWB state %d, "
1842 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001843 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001844 afMode, awbMode,
1845 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001846 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001847
1848 // Got all states, so construct a minimal result to send
1849 // In addition to the above fields, this means adding in
1850 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001851 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001852 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001853
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001854 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001855
1856 Mutex::Autolock l(mOutputLock);
1857
Jianing Weicb0652e2014-03-12 18:29:36 -07001858 CaptureResult captureResult;
1859 captureResult.mResultExtras = resultExtras;
1860 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1861 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1862 // but not limited to CameraDeviceBase::getNextResult
1863 CaptureResult& min3AResult =
1864 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001865
Jianing Weicb0652e2014-03-12 18:29:36 -07001866 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1867 // TODO: This is problematic casting. Need to fix CameraMetadata.
1868 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001869 return false;
1870 }
1871
Jianing Weicb0652e2014-03-12 18:29:36 -07001872 int32_t requestId = resultExtras.requestId;
1873 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001874 &requestId, frameNumber)) {
1875 return false;
1876 }
1877
Zhijun He204e3292014-07-14 17:09:23 -07001878 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1879 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1880 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1881 &partialResult, frameNumber)) {
1882 return false;
1883 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001884 }
1885
Jianing Weicb0652e2014-03-12 18:29:36 -07001886 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001887 &afMode, frameNumber)) {
1888 return false;
1889 }
1890
Jianing Weicb0652e2014-03-12 18:29:36 -07001891 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001892 &awbMode, frameNumber)) {
1893 return false;
1894 }
1895
Jianing Weicb0652e2014-03-12 18:29:36 -07001896 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001897 &aeState, frameNumber)) {
1898 return false;
1899 }
1900
Jianing Weicb0652e2014-03-12 18:29:36 -07001901 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001902 &afState, frameNumber)) {
1903 return false;
1904 }
1905
Jianing Weicb0652e2014-03-12 18:29:36 -07001906 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001907 &awbState, frameNumber)) {
1908 return false;
1909 }
1910
Jianing Weicb0652e2014-03-12 18:29:36 -07001911 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001912 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001913 return false;
1914 }
1915
Jianing Weicb0652e2014-03-12 18:29:36 -07001916 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001917 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001918 return false;
1919 }
1920
Zhijun He204e3292014-07-14 17:09:23 -07001921 // We only send the aggregated partial when all 3A related metadata are available
1922 // For both API1 and API2.
1923 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001924 mResultSignal.signal();
1925
1926 return true;
1927}
1928
1929template<typename T>
1930bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001931 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001932 (void) frameNumber;
1933
1934 camera_metadata_ro_entry_t entry;
1935
1936 entry = result.find(tag);
1937 if (entry.count == 0) {
1938 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1939 mId, frameNumber, get_camera_metadata_tag_name(tag));
1940 return false;
1941 }
1942
1943 if (sizeof(T) == sizeof(uint8_t)) {
1944 *value = entry.data.u8[0];
1945 } else if (sizeof(T) == sizeof(int32_t)) {
1946 *value = entry.data.i32[0];
1947 } else {
1948 ALOGE("%s: Unexpected type", __FUNCTION__);
1949 return false;
1950 }
1951 return true;
1952}
1953
1954template<typename T>
1955bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001956 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001957 if (result.update(tag, value, 1) != NO_ERROR) {
1958 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1959 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1960 frameNumber, get_camera_metadata_tag_name(tag));
1961 return false;
1962 }
1963 return true;
1964}
1965
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08001966void Camera3Device::returnOutputBuffers(
1967 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
1968 nsecs_t timestamp) {
1969 for (size_t i = 0; i < numBuffers; i++)
1970 {
1971 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
1972 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
1973 // Note: stream may be deallocated at this point, if this buffer was
1974 // the last reference to it.
1975 if (res != OK) {
1976 ALOGE("Can't return buffer to its stream: %s (%d)",
1977 strerror(-res), res);
1978 }
1979 }
1980}
1981
1982
1983void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
1984
1985 const InFlightRequest &request = mInFlightMap.valueAt(idx);
1986 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
1987
1988 nsecs_t sensorTimestamp = request.sensorTimestamp;
1989 nsecs_t shutterTimestamp = request.shutterTimestamp;
1990
1991 // Check if it's okay to remove the request from InFlightMap:
1992 // In the case of a successful request:
1993 // all input and output buffers, all result metadata, shutter callback
1994 // arrived.
1995 // In the case of a unsuccessful request:
1996 // all input and output buffers arrived.
1997 if (request.numBuffersLeft == 0 &&
1998 (request.requestStatus != OK ||
1999 (request.haveResultMetadata && shutterTimestamp != 0))) {
2000 ATRACE_ASYNC_END("frame capture", frameNumber);
2001
2002 // Sanity check - if sensor timestamp matches shutter timestamp
2003 if (request.requestStatus == OK &&
2004 sensorTimestamp != shutterTimestamp) {
2005 SET_ERR("sensor timestamp (%" PRId64
2006 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2007 sensorTimestamp, frameNumber, shutterTimestamp);
2008 }
2009
2010 // for an unsuccessful request, it may have pending output buffers to
2011 // return.
2012 assert(request.requestStatus != OK ||
2013 request.pendingOutputBuffers.size() == 0);
2014 returnOutputBuffers(request.pendingOutputBuffers.array(),
2015 request.pendingOutputBuffers.size(), 0);
2016
2017 mInFlightMap.removeItemsAt(idx, 1);
2018
2019 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2020 }
2021
2022 // Sanity check - if we have too many in-flight frames, something has
2023 // likely gone wrong
2024 if (mInFlightMap.size() > kInFlightWarnLimit) {
2025 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
2026 }
2027}
2028
2029
2030void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2031 CaptureResultExtras &resultExtras,
2032 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002033 uint32_t frameNumber,
2034 bool reprocess) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002035 if (pendingMetadata.isEmpty())
2036 return;
2037
2038 Mutex::Autolock l(mOutputLock);
2039
2040 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002041 if (reprocess) {
2042 if (frameNumber < mNextReprocessResultFrameNumber) {
2043 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002044 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002045 frameNumber, mNextReprocessResultFrameNumber);
2046 return;
2047 }
2048 mNextReprocessResultFrameNumber = frameNumber + 1;
2049 } else {
2050 if (frameNumber < mNextResultFrameNumber) {
2051 SET_ERR("Out-of-order capture result metadata submitted! "
2052 "(got frame number %d, expecting %d)",
2053 frameNumber, mNextResultFrameNumber);
2054 return;
2055 }
2056 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002057 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002058
2059 CaptureResult captureResult;
2060 captureResult.mResultExtras = resultExtras;
2061 captureResult.mMetadata = pendingMetadata;
2062
2063 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2064 (int32_t*)&frameNumber, 1) != OK) {
2065 SET_ERR("Failed to set frame# in metadata (%d)",
2066 frameNumber);
2067 return;
2068 } else {
2069 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2070 __FUNCTION__, mId, frameNumber);
2071 }
2072
2073 // Append any previous partials to form a complete result
2074 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2075 captureResult.mMetadata.append(collectedPartialResult);
2076 }
2077
2078 captureResult.mMetadata.sort();
2079
2080 // Check that there's a timestamp in the result metadata
2081 camera_metadata_entry entry =
2082 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2083 if (entry.count == 0) {
2084 SET_ERR("No timestamp provided by HAL for frame %d!",
2085 frameNumber);
2086 return;
2087 }
2088
2089 // Valid result, insert into queue
2090 List<CaptureResult>::iterator queuedResult =
2091 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2092 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2093 ", burstId = %" PRId32, __FUNCTION__,
2094 queuedResult->mResultExtras.requestId,
2095 queuedResult->mResultExtras.frameNumber,
2096 queuedResult->mResultExtras.burstId);
2097
2098 mResultSignal.signal();
2099}
2100
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002101/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002102 * Camera HAL device callback methods
2103 */
2104
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002105void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002106 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002107
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002108 status_t res;
2109
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002110 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002111 if (result->result == NULL && result->num_output_buffers == 0 &&
2112 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002113 SET_ERR("No result data provided by HAL for frame %d",
2114 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002115 return;
2116 }
Zhijun He204e3292014-07-14 17:09:23 -07002117
2118 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2119 // partial_result to 1 when metadata is included in this result.
2120 if (!mUsePartialResult &&
2121 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2122 result->result != NULL &&
2123 result->partial_result != 1) {
2124 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2125 " if partial result is not supported",
2126 frameNumber, result->partial_result);
2127 return;
2128 }
2129
2130 bool isPartialResult = false;
2131 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002132 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002133 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002134
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002135 // Get shutter timestamp and resultExtras from list of in-flight requests,
2136 // where it was added by the shutter notification for this frame. If the
2137 // shutter timestamp isn't received yet, append the output buffers to the
2138 // in-flight request and they will be returned when the shutter timestamp
2139 // arrives. Update the in-flight status and remove the in-flight entry if
2140 // all result data and shutter timestamp have been received.
2141 nsecs_t shutterTimestamp = 0;
2142
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002143 {
2144 Mutex::Autolock l(mInFlightLock);
2145 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2146 if (idx == NAME_NOT_FOUND) {
2147 SET_ERR("Unknown frame number for capture result: %d",
2148 frameNumber);
2149 return;
2150 }
2151 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002152 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2153 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2154 ", partialResultCount = %d",
2155 __FUNCTION__, request.resultExtras.requestId,
2156 request.resultExtras.frameNumber, request.resultExtras.burstId,
2157 result->partial_result);
2158 // Always update the partial count to the latest one if it's not 0
2159 // (buffers only). When framework aggregates adjacent partial results
2160 // into one, the latest partial count will be used.
2161 if (result->partial_result != 0)
2162 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002163
2164 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002165 if (mUsePartialResult && result->result != NULL) {
2166 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2167 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2168 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2169 " the range of [1, %d] when metadata is included in the result",
2170 frameNumber, result->partial_result, mNumPartialResults);
2171 return;
2172 }
2173 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002174 if (isPartialResult) {
2175 request.partialResult.collectedResult.append(result->result);
2176 }
Zhijun He204e3292014-07-14 17:09:23 -07002177 } else {
2178 camera_metadata_ro_entry_t partialResultEntry;
2179 res = find_camera_metadata_ro_entry(result->result,
2180 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2181 if (res != NAME_NOT_FOUND &&
2182 partialResultEntry.count > 0 &&
2183 partialResultEntry.data.u8[0] ==
2184 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2185 // A partial result. Flag this as such, and collect this
2186 // set of metadata into the in-flight entry.
2187 isPartialResult = true;
2188 request.partialResult.collectedResult.append(
2189 result->result);
2190 request.partialResult.collectedResult.erase(
2191 ANDROID_QUIRKS_PARTIAL_RESULT);
2192 }
2193 }
2194
2195 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002196 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002197 if (!request.partialResult.haveSent3A) {
2198 request.partialResult.haveSent3A =
2199 processPartial3AResult(frameNumber,
2200 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002201 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002202 }
2203 }
2204 }
2205
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002206 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002207 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002208
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002209 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002210 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002211 if (request.haveResultMetadata) {
2212 SET_ERR("Called multiple times with metadata for frame %d",
2213 frameNumber);
2214 return;
2215 }
Zhijun He204e3292014-07-14 17:09:23 -07002216 if (mUsePartialResult &&
2217 !request.partialResult.collectedResult.isEmpty()) {
2218 collectedPartialResult.acquire(
2219 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002220 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002221 request.haveResultMetadata = true;
2222 }
2223
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002224 uint32_t numBuffersReturned = result->num_output_buffers;
2225 if (result->input_buffer != NULL) {
2226 if (hasInputBufferInRequest) {
2227 numBuffersReturned += 1;
2228 } else {
2229 ALOGW("%s: Input buffer should be NULL if there is no input"
2230 " buffer sent in the request",
2231 __FUNCTION__);
2232 }
2233 }
2234 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002235 if (request.numBuffersLeft < 0) {
2236 SET_ERR("Too many buffers returned for frame %d",
2237 frameNumber);
2238 return;
2239 }
2240
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002241 camera_metadata_ro_entry_t entry;
2242 res = find_camera_metadata_ro_entry(result->result,
2243 ANDROID_SENSOR_TIMESTAMP, &entry);
2244 if (res == OK && entry.count == 1) {
2245 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002246 }
2247
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002248 // If shutter event isn't received yet, append the output buffers to
2249 // the in-flight request. Otherwise, return the output buffers to
2250 // streams.
2251 if (shutterTimestamp == 0) {
2252 request.pendingOutputBuffers.appendArray(result->output_buffers,
2253 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002254 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002255 returnOutputBuffers(result->output_buffers,
2256 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002257 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002258
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002259 if (result->result != NULL && !isPartialResult) {
2260 if (shutterTimestamp == 0) {
2261 request.pendingMetadata = result->result;
2262 request.partialResult.collectedResult = collectedPartialResult;
2263 } else {
2264 CameraMetadata metadata;
2265 metadata = result->result;
2266 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002267 collectedPartialResult, frameNumber, hasInputBufferInRequest);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002268 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002269 }
2270
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002271 removeInFlightRequestIfReadyLocked(idx);
2272 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002273
Zhijun Hef0d962a2014-06-30 10:24:11 -07002274 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002275 if (hasInputBufferInRequest) {
2276 Camera3Stream *stream =
2277 Camera3Stream::cast(result->input_buffer->stream);
2278 res = stream->returnInputBuffer(*(result->input_buffer));
2279 // Note: stream may be deallocated at this point, if this buffer was the
2280 // last reference to it.
2281 if (res != OK) {
2282 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2283 " its stream:%s (%d)", __FUNCTION__,
2284 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002285 }
2286 } else {
2287 ALOGW("%s: Input buffer should be NULL if there is no input"
2288 " buffer sent in the request, skipping input buffer return.",
2289 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002290 }
2291 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002292}
2293
2294void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002295 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002296 NotificationListener *listener;
2297 {
2298 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002299 listener = mListener;
2300 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002301
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002302 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002303 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002304 return;
2305 }
2306
2307 switch (msg->type) {
2308 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002309 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002310 break;
2311 }
2312 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002313 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002314 break;
2315 }
2316 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002317 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002318 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002319 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002320}
2321
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002322void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2323 NotificationListener *listener) {
2324
2325 // Map camera HAL error codes to ICameraDeviceCallback error codes
2326 // Index into this with the HAL error code
2327 static const ICameraDeviceCallbacks::CameraErrorCode
2328 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2329 // 0 = Unused error code
2330 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2331 // 1 = CAMERA3_MSG_ERROR_DEVICE
2332 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2333 // 2 = CAMERA3_MSG_ERROR_REQUEST
2334 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2335 // 3 = CAMERA3_MSG_ERROR_RESULT
2336 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2337 // 4 = CAMERA3_MSG_ERROR_BUFFER
2338 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2339 };
2340
2341 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2342 ((msg.error_code >= 0) &&
2343 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2344 halErrorMap[msg.error_code] :
2345 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2346
2347 int streamId = 0;
2348 if (msg.error_stream != NULL) {
2349 Camera3Stream *stream =
2350 Camera3Stream::cast(msg.error_stream);
2351 streamId = stream->getId();
2352 }
2353 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2354 mId, __FUNCTION__, msg.frame_number,
2355 streamId, msg.error_code);
2356
2357 CaptureResultExtras resultExtras;
2358 switch (errorCode) {
2359 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2360 // SET_ERR calls notifyError
2361 SET_ERR("Camera HAL reported serious device error");
2362 break;
2363 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2364 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2365 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2366 {
2367 Mutex::Autolock l(mInFlightLock);
2368 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2369 if (idx >= 0) {
2370 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2371 r.requestStatus = msg.error_code;
2372 resultExtras = r.resultExtras;
2373 } else {
2374 resultExtras.frameNumber = msg.frame_number;
2375 ALOGE("Camera %d: %s: cannot find in-flight request on "
2376 "frame %" PRId64 " error", mId, __FUNCTION__,
2377 resultExtras.frameNumber);
2378 }
2379 }
2380 if (listener != NULL) {
2381 listener->notifyError(errorCode, resultExtras);
2382 } else {
2383 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2384 }
2385 break;
2386 default:
2387 // SET_ERR calls notifyError
2388 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2389 break;
2390 }
2391}
2392
2393void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2394 NotificationListener *listener) {
2395 ssize_t idx;
2396 // Verify ordering of shutter notifications
2397 {
2398 Mutex::Autolock l(mOutputLock);
2399 // TODO: need to track errors for tighter bounds on expected frame number.
2400 if (msg.frame_number < mNextShutterFrameNumber) {
2401 SET_ERR("Shutter notification out-of-order. Expected "
2402 "notification for frame %d, got frame %d",
2403 mNextShutterFrameNumber, msg.frame_number);
2404 return;
2405 }
2406 mNextShutterFrameNumber = msg.frame_number + 1;
2407 }
2408
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002409 // Set timestamp for the request in the in-flight tracking
2410 // and get the request ID to send upstream
2411 {
2412 Mutex::Autolock l(mInFlightLock);
2413 idx = mInFlightMap.indexOfKey(msg.frame_number);
2414 if (idx >= 0) {
2415 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002416
2417 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2418 mId, __FUNCTION__,
2419 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2420 // Call listener, if any
2421 if (listener != NULL) {
2422 listener->notifyShutter(r.resultExtras, msg.timestamp);
2423 }
2424
2425 r.shutterTimestamp = msg.timestamp;
2426
2427 // send pending result and buffers
2428 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002429 r.partialResult.collectedResult, msg.frame_number,
2430 r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002431 returnOutputBuffers(r.pendingOutputBuffers.array(),
2432 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2433 r.pendingOutputBuffers.clear();
2434
2435 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002436 }
2437 }
2438 if (idx < 0) {
2439 SET_ERR("Shutter notification for non-existent frame number %d",
2440 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002441 }
2442}
2443
2444
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002445CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002446 ALOGV("%s", __FUNCTION__);
2447
Igor Murashkin1e479c02013-09-06 16:55:14 -07002448 CameraMetadata retVal;
2449
2450 if (mRequestThread != NULL) {
2451 retVal = mRequestThread->getLatestRequest();
2452 }
2453
Igor Murashkin1e479c02013-09-06 16:55:14 -07002454 return retVal;
2455}
2456
Jianing Weicb0652e2014-03-12 18:29:36 -07002457
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002458/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002459 * RequestThread inner class methods
2460 */
2461
2462Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002463 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002464 camera3_device_t *hal3Device) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002465 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002466 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002467 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002468 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002469 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002470 mReconfigured(false),
2471 mDoPause(false),
2472 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002473 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002474 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002475 mCurrentAfTriggerId(0),
2476 mCurrentPreCaptureTriggerId(0),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002477 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002478 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002479}
2480
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002481void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002482 NotificationListener *listener) {
2483 Mutex::Autolock l(mRequestLock);
2484 mListener = listener;
2485}
2486
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002487void Camera3Device::RequestThread::configurationComplete() {
2488 Mutex::Autolock l(mRequestLock);
2489 mReconfigured = true;
2490}
2491
Jianing Wei90e59c92014-03-12 18:29:36 -07002492status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002493 List<sp<CaptureRequest> > &requests,
2494 /*out*/
2495 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002496 Mutex::Autolock l(mRequestLock);
2497 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2498 ++it) {
2499 mRequestQueue.push_back(*it);
2500 }
2501
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002502 if (lastFrameNumber != NULL) {
2503 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2504 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2505 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2506 *lastFrameNumber);
2507 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002508
Jianing Wei90e59c92014-03-12 18:29:36 -07002509 unpauseForNewRequests();
2510
2511 return OK;
2512}
2513
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002514
2515status_t Camera3Device::RequestThread::queueTrigger(
2516 RequestTrigger trigger[],
2517 size_t count) {
2518
2519 Mutex::Autolock l(mTriggerMutex);
2520 status_t ret;
2521
2522 for (size_t i = 0; i < count; ++i) {
2523 ret = queueTriggerLocked(trigger[i]);
2524
2525 if (ret != OK) {
2526 return ret;
2527 }
2528 }
2529
2530 return OK;
2531}
2532
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002533int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2534 sp<Camera3Device> d = device.promote();
2535 if (d != NULL) return d->mId;
2536 return 0;
2537}
2538
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002539status_t Camera3Device::RequestThread::queueTriggerLocked(
2540 RequestTrigger trigger) {
2541
2542 uint32_t tag = trigger.metadataTag;
2543 ssize_t index = mTriggerMap.indexOfKey(tag);
2544
2545 switch (trigger.getTagType()) {
2546 case TYPE_BYTE:
2547 // fall-through
2548 case TYPE_INT32:
2549 break;
2550 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002551 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2552 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002553 return INVALID_OPERATION;
2554 }
2555
2556 /**
2557 * Collect only the latest trigger, since we only have 1 field
2558 * in the request settings per trigger tag, and can't send more than 1
2559 * trigger per request.
2560 */
2561 if (index != NAME_NOT_FOUND) {
2562 mTriggerMap.editValueAt(index) = trigger;
2563 } else {
2564 mTriggerMap.add(tag, trigger);
2565 }
2566
2567 return OK;
2568}
2569
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002570status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002571 const RequestList &requests,
2572 /*out*/
2573 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002574 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002575 if (lastFrameNumber != NULL) {
2576 *lastFrameNumber = mRepeatingLastFrameNumber;
2577 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002578 mRepeatingRequests.clear();
2579 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2580 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002581
2582 unpauseForNewRequests();
2583
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002584 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002585 return OK;
2586}
2587
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002588bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2589 if (mRepeatingRequests.empty()) {
2590 return false;
2591 }
2592 int32_t requestId = requestIn->mResultExtras.requestId;
2593 const RequestList &repeatRequests = mRepeatingRequests;
2594 // All repeating requests are guaranteed to have same id so only check first quest
2595 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2596 return (firstRequest->mResultExtras.requestId == requestId);
2597}
2598
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002599status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002600 Mutex::Autolock l(mRequestLock);
2601 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002602 if (lastFrameNumber != NULL) {
2603 *lastFrameNumber = mRepeatingLastFrameNumber;
2604 }
2605 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002606 return OK;
2607}
2608
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002609status_t Camera3Device::RequestThread::clear(
2610 NotificationListener *listener,
2611 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002612 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002613 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002614
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002615 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002616
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002617 // Send errors for all requests pending in the request queue, including
2618 // pending repeating requests
2619 if (listener != NULL) {
2620 for (RequestList::iterator it = mRequestQueue.begin();
2621 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002622 // Abort the input buffers for reprocess requests.
2623 if ((*it)->mInputStream != NULL) {
2624 camera3_stream_buffer_t inputBuffer;
2625 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2626 if (res != OK) {
2627 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2628 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2629 } else {
2630 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2631 if (res != OK) {
2632 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2633 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2634 }
2635 }
2636 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002637 // Set the frame number this request would have had, if it
2638 // had been submitted; this frame number will not be reused.
2639 // The requestId and burstId fields were set when the request was
2640 // submitted originally (in convertMetadataListToRequestListLocked)
2641 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2642 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2643 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002644 }
2645 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002646 mRequestQueue.clear();
2647 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002648 if (lastFrameNumber != NULL) {
2649 *lastFrameNumber = mRepeatingLastFrameNumber;
2650 }
2651 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002652 return OK;
2653}
2654
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002655void Camera3Device::RequestThread::setPaused(bool paused) {
2656 Mutex::Autolock l(mPauseLock);
2657 mDoPause = paused;
2658 mDoPauseSignal.signal();
2659}
2660
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002661status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2662 int32_t requestId, nsecs_t timeout) {
2663 Mutex::Autolock l(mLatestRequestMutex);
2664 status_t res;
2665 while (mLatestRequestId != requestId) {
2666 nsecs_t startTime = systemTime();
2667
2668 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2669 if (res != OK) return res;
2670
2671 timeout -= (systemTime() - startTime);
2672 }
2673
2674 return OK;
2675}
2676
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002677void Camera3Device::RequestThread::requestExit() {
2678 // Call parent to set up shutdown
2679 Thread::requestExit();
2680 // The exit from any possible waits
2681 mDoPauseSignal.signal();
2682 mRequestSignal.signal();
2683}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002684
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002685bool Camera3Device::RequestThread::threadLoop() {
2686
2687 status_t res;
2688
2689 // Handle paused state.
2690 if (waitIfPaused()) {
2691 return true;
2692 }
2693
2694 // Get work to do
2695
2696 sp<CaptureRequest> nextRequest = waitForNextRequest();
2697 if (nextRequest == NULL) {
2698 return true;
2699 }
2700
2701 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002702 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002703 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002704 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002705
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002706 // Get the request ID, if any
2707 int requestId;
2708 camera_metadata_entry_t requestIdEntry =
2709 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2710 if (requestIdEntry.count > 0) {
2711 requestId = requestIdEntry.data.i32[0];
2712 } else {
2713 ALOGW("%s: Did not have android.request.id set in the request",
2714 __FUNCTION__);
2715 requestId = NAME_NOT_FOUND;
2716 }
2717
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002718 // Insert any queued triggers (before metadata is locked)
2719 int32_t triggerCount;
2720 res = insertTriggers(nextRequest);
2721 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002722 SET_ERR("RequestThread: Unable to insert triggers "
2723 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002724 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002725 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2726 return false;
2727 }
2728 triggerCount = res;
2729
2730 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2731
2732 // If the request is the same as last, or we had triggers last time
2733 if (mPrevRequest != nextRequest || triggersMixedIn) {
2734 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002735 * HAL workaround:
2736 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2737 */
2738 res = addDummyTriggerIds(nextRequest);
2739 if (res != OK) {
2740 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2741 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002742 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002743 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2744 return false;
2745 }
2746
2747 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002748 * The request should be presorted so accesses in HAL
2749 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2750 */
2751 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002752 request.settings = nextRequest->mSettings.getAndLock();
2753 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002754 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2755
2756 IF_ALOGV() {
2757 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2758 find_camera_metadata_ro_entry(
2759 request.settings,
2760 ANDROID_CONTROL_AF_TRIGGER,
2761 &e
2762 );
2763 if (e.count > 0) {
2764 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2765 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002766 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002767 e.data.u8[0]);
2768 }
2769 }
2770 } else {
2771 // leave request.settings NULL to indicate 'reuse latest given'
2772 ALOGVV("%s: Request settings are REUSED",
2773 __FUNCTION__);
2774 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002775
Zhijun Hef0d962a2014-06-30 10:24:11 -07002776 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002777
2778 // Fill in buffers
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002779 if (nextRequest->mInputStream != NULL) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002780 request.input_buffer = &nextRequest->mInputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002781 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002782 } else {
2783 request.input_buffer = NULL;
2784 }
2785
2786 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2787 nextRequest->mOutputStreams.size());
2788 request.output_buffers = outputBuffers.array();
2789 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2790 res = nextRequest->mOutputStreams.editItemAt(i)->
2791 getBuffer(&outputBuffers.editItemAt(i));
2792 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002793 // Can't get output buffer from gralloc queue - this could be due to
2794 // abandoned queue or other consumer misbehavior, so not a fatal
2795 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002796 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2797 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala0ec23d32015-05-28 15:15:54 -07002798 {
2799 Mutex::Autolock l(mRequestLock);
2800 if (mListener != NULL) {
2801 mListener->notifyError(
2802 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2803 nextRequest->mResultExtras);
2804 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002805 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002806 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2807 return true;
2808 }
2809 request.num_output_buffers++;
2810 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002811 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002812
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002813 // Log request in the in-flight queue
2814 sp<Camera3Device> parent = mParent.promote();
2815 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002816 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002817 CLOGE("RequestThread: Parent is gone");
2818 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2819 return false;
2820 }
2821
Jianing Weicb0652e2014-03-12 18:29:36 -07002822 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002823 totalNumBuffers, nextRequest->mResultExtras,
2824 /*hasInput*/request.input_buffer != NULL);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002825 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2826 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002827 __FUNCTION__,
2828 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2829 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002830 if (res != OK) {
2831 SET_ERR("RequestThread: Unable to register new in-flight request:"
2832 " %s (%d)", strerror(-res), res);
2833 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2834 return false;
2835 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002836
Zhijun Hecc27e112013-10-03 16:12:43 -07002837 // Inform waitUntilRequestProcessed thread of a new request ID
2838 {
2839 Mutex::Autolock al(mLatestRequestMutex);
2840
2841 mLatestRequestId = requestId;
2842 mLatestRequestSignal.signal();
2843 }
2844
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002845 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002846 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2847 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002848 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002849 ATRACE_END();
2850
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002851 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002852 // Should only get a failure here for malformed requests or device-level
2853 // errors, so consider all errors fatal. Bad metadata failures should
2854 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002855 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002856 " device: %s (%d)", request.frame_number, strerror(-res), res);
2857 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2858 return false;
2859 }
2860
Igor Murashkin1e479c02013-09-06 16:55:14 -07002861 // Update the latest request sent to HAL
2862 if (request.settings != NULL) { // Don't update them if they were unchanged
2863 Mutex::Autolock al(mLatestRequestMutex);
2864
2865 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2866 mLatestRequest.acquire(cloned);
2867 }
2868
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002869 if (request.settings != NULL) {
2870 nextRequest->mSettings.unlock(request.settings);
2871 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002872
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002873 // Unset as current request
2874 {
2875 Mutex::Autolock l(mRequestLock);
2876 mNextRequest.clear();
2877 }
2878
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002879 // Remove any previously queued triggers (after unlock)
2880 res = removeTriggers(mPrevRequest);
2881 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002882 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002883 "(capture request %d, HAL device: %s (%d)",
2884 request.frame_number, strerror(-res), res);
2885 return false;
2886 }
2887 mPrevTriggers = triggerCount;
2888
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002889 return true;
2890}
2891
Igor Murashkin1e479c02013-09-06 16:55:14 -07002892CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2893 Mutex::Autolock al(mLatestRequestMutex);
2894
2895 ALOGV("RequestThread::%s", __FUNCTION__);
2896
2897 return mLatestRequest;
2898}
2899
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002900bool Camera3Device::RequestThread::isStreamPending(
2901 sp<Camera3StreamInterface>& stream) {
2902 Mutex::Autolock l(mRequestLock);
2903
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002904 if (mNextRequest != nullptr) {
2905 for (const auto& s : mNextRequest->mOutputStreams) {
2906 if (stream == s) return true;
2907 }
2908 if (stream == mNextRequest->mInputStream) return true;
2909 }
2910
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002911 for (const auto& request : mRequestQueue) {
2912 for (const auto& s : request->mOutputStreams) {
2913 if (stream == s) return true;
2914 }
2915 if (stream == request->mInputStream) return true;
2916 }
2917
2918 for (const auto& request : mRepeatingRequests) {
2919 for (const auto& s : request->mOutputStreams) {
2920 if (stream == s) return true;
2921 }
2922 if (stream == request->mInputStream) return true;
2923 }
2924
2925 return false;
2926}
Jianing Weicb0652e2014-03-12 18:29:36 -07002927
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002928void Camera3Device::RequestThread::cleanUpFailedRequest(
2929 camera3_capture_request_t &request,
2930 sp<CaptureRequest> &nextRequest,
2931 Vector<camera3_stream_buffer_t> &outputBuffers) {
2932
2933 if (request.settings != NULL) {
2934 nextRequest->mSettings.unlock(request.settings);
2935 }
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002936 if (nextRequest->mInputStream != NULL) {
2937 nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
2938 nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002939 }
2940 for (size_t i = 0; i < request.num_output_buffers; i++) {
2941 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2942 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2943 outputBuffers[i], 0);
2944 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002945
2946 Mutex::Autolock l(mRequestLock);
2947 mNextRequest.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002948}
2949
2950sp<Camera3Device::CaptureRequest>
2951 Camera3Device::RequestThread::waitForNextRequest() {
2952 status_t res;
2953 sp<CaptureRequest> nextRequest;
2954
2955 // Optimized a bit for the simple steady-state case (single repeating
2956 // request), to avoid putting that request in the queue temporarily.
2957 Mutex::Autolock l(mRequestLock);
2958
2959 while (mRequestQueue.empty()) {
2960 if (!mRepeatingRequests.empty()) {
2961 // Always atomically enqueue all requests in a repeating request
2962 // list. Guarantees a complete in-sequence set of captures to
2963 // application.
2964 const RequestList &requests = mRepeatingRequests;
2965 RequestList::const_iterator firstRequest =
2966 requests.begin();
2967 nextRequest = *firstRequest;
2968 mRequestQueue.insert(mRequestQueue.end(),
2969 ++firstRequest,
2970 requests.end());
2971 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002972
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002973 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002974
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002975 break;
2976 }
2977
2978 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2979
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002980 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2981 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002982 Mutex::Autolock pl(mPauseLock);
2983 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002984 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002985 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002986 // Let the tracker know
2987 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2988 if (statusTracker != 0) {
2989 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2990 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002991 }
2992 // Stop waiting for now and let thread management happen
2993 return NULL;
2994 }
2995 }
2996
2997 if (nextRequest == NULL) {
2998 // Don't have a repeating request already in hand, so queue
2999 // must have an entry now.
3000 RequestList::iterator firstRequest =
3001 mRequestQueue.begin();
3002 nextRequest = *firstRequest;
3003 mRequestQueue.erase(firstRequest);
3004 }
3005
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003006 // In case we've been unpaused by setPaused clearing mDoPause, need to
3007 // update internal pause state (capture/setRepeatingRequest unpause
3008 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003009 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003010 if (mPaused) {
3011 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3012 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3013 if (statusTracker != 0) {
3014 statusTracker->markComponentActive(mStatusId);
3015 }
3016 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003017 mPaused = false;
3018
3019 // Check if we've reconfigured since last time, and reset the preview
3020 // request if so. Can't use 'NULL request == repeat' across configure calls.
3021 if (mReconfigured) {
3022 mPrevRequest.clear();
3023 mReconfigured = false;
3024 }
3025
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003026 if (nextRequest != NULL) {
3027 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003028 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3029 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003030
3031 // Since RequestThread::clear() removes buffers from the input stream,
3032 // get the right buffer here before unlocking mRequestLock
3033 if (nextRequest->mInputStream != NULL) {
3034 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3035 if (res != OK) {
3036 // Can't get input buffer from gralloc queue - this could be due to
3037 // disconnected queue or other producer misbehavior, so not a fatal
3038 // error
3039 ALOGE("%s: Can't get input buffer, skipping request:"
3040 " %s (%d)", __FUNCTION__, strerror(-res), res);
3041 if (mListener != NULL) {
3042 mListener->notifyError(
3043 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3044 nextRequest->mResultExtras);
3045 }
3046 return NULL;
3047 }
3048 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003049 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003050 mNextRequest = nextRequest;
3051
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003052 return nextRequest;
3053}
3054
3055bool Camera3Device::RequestThread::waitIfPaused() {
3056 status_t res;
3057 Mutex::Autolock l(mPauseLock);
3058 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003059 if (mPaused == false) {
3060 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003061 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3062 // Let the tracker know
3063 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3064 if (statusTracker != 0) {
3065 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3066 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003067 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003068
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003069 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003070 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003071 return true;
3072 }
3073 }
3074 // We don't set mPaused to false here, because waitForNextRequest needs
3075 // to further manage the paused state in case of starvation.
3076 return false;
3077}
3078
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003079void Camera3Device::RequestThread::unpauseForNewRequests() {
3080 // With work to do, mark thread as unpaused.
3081 // If paused by request (setPaused), don't resume, to avoid
3082 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003083 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003084 Mutex::Autolock p(mPauseLock);
3085 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003086 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3087 if (mPaused) {
3088 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3089 if (statusTracker != 0) {
3090 statusTracker->markComponentActive(mStatusId);
3091 }
3092 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003093 mPaused = false;
3094 }
3095}
3096
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003097void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3098 sp<Camera3Device> parent = mParent.promote();
3099 if (parent != NULL) {
3100 va_list args;
3101 va_start(args, fmt);
3102
3103 parent->setErrorStateV(fmt, args);
3104
3105 va_end(args);
3106 }
3107}
3108
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003109status_t Camera3Device::RequestThread::insertTriggers(
3110 const sp<CaptureRequest> &request) {
3111
3112 Mutex::Autolock al(mTriggerMutex);
3113
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003114 sp<Camera3Device> parent = mParent.promote();
3115 if (parent == NULL) {
3116 CLOGE("RequestThread: Parent is gone");
3117 return DEAD_OBJECT;
3118 }
3119
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003120 CameraMetadata &metadata = request->mSettings;
3121 size_t count = mTriggerMap.size();
3122
3123 for (size_t i = 0; i < count; ++i) {
3124 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003125 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003126
3127 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3128 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3129 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003130 if (isAeTrigger) {
3131 request->mResultExtras.precaptureTriggerId = triggerId;
3132 mCurrentPreCaptureTriggerId = triggerId;
3133 } else {
3134 request->mResultExtras.afTriggerId = triggerId;
3135 mCurrentAfTriggerId = triggerId;
3136 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003137 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3138 continue; // Trigger ID tag is deprecated since device HAL 3.2
3139 }
3140 }
3141
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003142 camera_metadata_entry entry = metadata.find(tag);
3143
3144 if (entry.count > 0) {
3145 /**
3146 * Already has an entry for this trigger in the request.
3147 * Rewrite it with our requested trigger value.
3148 */
3149 RequestTrigger oldTrigger = trigger;
3150
3151 oldTrigger.entryValue = entry.data.u8[0];
3152
3153 mTriggerReplacedMap.add(tag, oldTrigger);
3154 } else {
3155 /**
3156 * More typical, no trigger entry, so we just add it
3157 */
3158 mTriggerRemovedMap.add(tag, trigger);
3159 }
3160
3161 status_t res;
3162
3163 switch (trigger.getTagType()) {
3164 case TYPE_BYTE: {
3165 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3166 res = metadata.update(tag,
3167 &entryValue,
3168 /*count*/1);
3169 break;
3170 }
3171 case TYPE_INT32:
3172 res = metadata.update(tag,
3173 &trigger.entryValue,
3174 /*count*/1);
3175 break;
3176 default:
3177 ALOGE("%s: Type not supported: 0x%x",
3178 __FUNCTION__,
3179 trigger.getTagType());
3180 return INVALID_OPERATION;
3181 }
3182
3183 if (res != OK) {
3184 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3185 ", value %d", __FUNCTION__, trigger.getTagName(),
3186 trigger.entryValue);
3187 return res;
3188 }
3189
3190 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3191 trigger.getTagName(),
3192 trigger.entryValue);
3193 }
3194
3195 mTriggerMap.clear();
3196
3197 return count;
3198}
3199
3200status_t Camera3Device::RequestThread::removeTriggers(
3201 const sp<CaptureRequest> &request) {
3202 Mutex::Autolock al(mTriggerMutex);
3203
3204 CameraMetadata &metadata = request->mSettings;
3205
3206 /**
3207 * Replace all old entries with their old values.
3208 */
3209 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3210 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3211
3212 status_t res;
3213
3214 uint32_t tag = trigger.metadataTag;
3215 switch (trigger.getTagType()) {
3216 case TYPE_BYTE: {
3217 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3218 res = metadata.update(tag,
3219 &entryValue,
3220 /*count*/1);
3221 break;
3222 }
3223 case TYPE_INT32:
3224 res = metadata.update(tag,
3225 &trigger.entryValue,
3226 /*count*/1);
3227 break;
3228 default:
3229 ALOGE("%s: Type not supported: 0x%x",
3230 __FUNCTION__,
3231 trigger.getTagType());
3232 return INVALID_OPERATION;
3233 }
3234
3235 if (res != OK) {
3236 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3237 ", trigger value %d", __FUNCTION__,
3238 trigger.getTagName(), trigger.entryValue);
3239 return res;
3240 }
3241 }
3242 mTriggerReplacedMap.clear();
3243
3244 /**
3245 * Remove all new entries.
3246 */
3247 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3248 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3249 status_t res = metadata.erase(trigger.metadataTag);
3250
3251 if (res != OK) {
3252 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3253 ", trigger value %d", __FUNCTION__,
3254 trigger.getTagName(), trigger.entryValue);
3255 return res;
3256 }
3257 }
3258 mTriggerRemovedMap.clear();
3259
3260 return OK;
3261}
3262
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003263status_t Camera3Device::RequestThread::addDummyTriggerIds(
3264 const sp<CaptureRequest> &request) {
3265 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3266 static const int32_t dummyTriggerId = 1;
3267 status_t res;
3268
3269 CameraMetadata &metadata = request->mSettings;
3270
3271 // If AF trigger is active, insert a dummy AF trigger ID if none already
3272 // exists
3273 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3274 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3275 if (afTrigger.count > 0 &&
3276 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3277 afId.count == 0) {
3278 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3279 if (res != OK) return res;
3280 }
3281
3282 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3283 // if none already exists
3284 camera_metadata_entry pcTrigger =
3285 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3286 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3287 if (pcTrigger.count > 0 &&
3288 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3289 pcId.count == 0) {
3290 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3291 &dummyTriggerId, 1);
3292 if (res != OK) return res;
3293 }
3294
3295 return OK;
3296}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003297
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003298/**
3299 * PreparerThread inner class methods
3300 */
3301
3302Camera3Device::PreparerThread::PreparerThread() :
3303 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3304}
3305
3306Camera3Device::PreparerThread::~PreparerThread() {
3307 Thread::requestExitAndWait();
3308 if (mCurrentStream != nullptr) {
3309 mCurrentStream->cancelPrepare();
3310 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3311 mCurrentStream.clear();
3312 }
3313 clear();
3314}
3315
3316status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) {
3317 status_t res;
3318
3319 Mutex::Autolock l(mLock);
3320
3321 res = stream->startPrepare();
3322 if (res == OK) {
3323 // No preparation needed, fire listener right off
3324 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3325 if (mListener) {
3326 mListener->notifyPrepared(stream->getId());
3327 }
3328 return OK;
3329 } else if (res != NOT_ENOUGH_DATA) {
3330 return res;
3331 }
3332
3333 // Need to prepare, start up thread if necessary
3334 if (!mActive) {
3335 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3336 // isn't running
3337 Thread::requestExitAndWait();
3338 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3339 if (res != OK) {
3340 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3341 if (mListener) {
3342 mListener->notifyPrepared(stream->getId());
3343 }
3344 return res;
3345 }
3346 mCancelNow = false;
3347 mActive = true;
3348 ALOGV("%s: Preparer stream started", __FUNCTION__);
3349 }
3350
3351 // queue up the work
3352 mPendingStreams.push_back(stream);
3353 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3354
3355 return OK;
3356}
3357
3358status_t Camera3Device::PreparerThread::clear() {
3359 status_t res;
3360
3361 Mutex::Autolock l(mLock);
3362
3363 for (const auto& stream : mPendingStreams) {
3364 stream->cancelPrepare();
3365 }
3366 mPendingStreams.clear();
3367 mCancelNow = true;
3368
3369 return OK;
3370}
3371
3372void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3373 Mutex::Autolock l(mLock);
3374 mListener = listener;
3375}
3376
3377bool Camera3Device::PreparerThread::threadLoop() {
3378 status_t res;
3379 {
3380 Mutex::Autolock l(mLock);
3381 if (mCurrentStream == nullptr) {
3382 // End thread if done with work
3383 if (mPendingStreams.empty()) {
3384 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3385 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3386 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3387 mActive = false;
3388 return false;
3389 }
3390
3391 // Get next stream to prepare
3392 auto it = mPendingStreams.begin();
3393 mCurrentStream = *it;
3394 mPendingStreams.erase(it);
3395 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3396 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3397 } else if (mCancelNow) {
3398 mCurrentStream->cancelPrepare();
3399 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3400 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3401 mCurrentStream.clear();
3402 mCancelNow = false;
3403 return true;
3404 }
3405 }
3406
3407 res = mCurrentStream->prepareNextBuffer();
3408 if (res == NOT_ENOUGH_DATA) return true;
3409 if (res != OK) {
3410 // Something bad happened; try to recover by cancelling prepare and
3411 // signalling listener anyway
3412 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3413 mCurrentStream->getId(), res, strerror(-res));
3414 mCurrentStream->cancelPrepare();
3415 }
3416
3417 // This stream has finished, notify listener
3418 Mutex::Autolock l(mLock);
3419 if (mListener) {
3420 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3421 mCurrentStream->getId());
3422 mListener->notifyPrepared(mCurrentStream->getId());
3423 }
3424
3425 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3426 mCurrentStream.clear();
3427
3428 return true;
3429}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003430
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003431/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003432 * Static callback forwarding methods from HAL to instance
3433 */
3434
3435void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3436 const camera3_capture_result *result) {
3437 Camera3Device *d =
3438 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3439 d->processCaptureResult(result);
3440}
3441
3442void Camera3Device::sNotify(const camera3_callback_ops *cb,
3443 const camera3_notify_msg *msg) {
3444 Camera3Device *d =
3445 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3446 d->notify(msg);
3447}
3448
3449}; // namespace android