blob: 204f68cbec3e2a79c4c56d7a3be2690288f7d4fe [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
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080029#define CLOGE(fmt, ...) ALOGE("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070030 ##__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>
Zhijun He90f7c372016-08-16 16:19:43 -070045#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070046
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080047#include <android/hardware/camera2/ICameraDeviceUser.h>
48
Igor Murashkinff3e31d2013-10-23 16:40:06 -070049#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070050#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070051#include "device3/Camera3Device.h"
52#include "device3/Camera3OutputStream.h"
53#include "device3/Camera3InputStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070054#include "device3/Camera3DummyStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070055#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070056#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080057
58using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080059using namespace android::hardware::camera;
60using namespace android::hardware::camera::device::V3_2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061
62namespace android {
63
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080064Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080065 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080066 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070067 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070068 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070069 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070070 mUsePartialResult(false),
71 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080072 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070073 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070074 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070075 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070076 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070077 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080078{
79 ATRACE_CALL();
80 camera3_callback_ops::notify = &sNotify;
81 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080082 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080083}
84
85Camera3Device::~Camera3Device()
86{
87 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080088 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080089 disconnect();
90}
91
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080092const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -080093 return mId;
94}
95
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080096/**
97 * CameraDeviceBase interface
98 */
99
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800100status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800101{
102 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700103 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800104 Mutex::Autolock l(mLock);
105
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800106 ALOGV("%s: Initializing device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800107 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700108 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800109 return INVALID_OPERATION;
110 }
111
112 /** Open HAL device */
113
114 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800115
116 camera3_device_t *device;
117
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800118 ATRACE_BEGIN("CameraHal::open");
119 res = module->open(mId.string(),
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800120 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800121 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800122
123 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700124 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800125 return res;
126 }
127
128 /** Cross-check device version */
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800129 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700130 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700131 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800132 CAMERA_DEVICE_API_VERSION_3_2,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800133 device->common.version);
134 device->common.close(&device->common);
135 return BAD_VALUE;
136 }
137
138 camera_info info;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800139 res = module->getCameraInfo(atoi(mId), &info);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800140 if (res != OK) return res;
141
142 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700143 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
144 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700145 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800146 device->common.close(&device->common);
147 return BAD_VALUE;
148 }
149
150 /** Initialize device with callback functions */
151
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800152 ATRACE_BEGIN("CameraHal::initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800153 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700154 ATRACE_END();
155
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800156 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700157 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
158 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800159 device->common.close(&device->common);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800160 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800161 }
162
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800163 /** Everything is good to go */
164
165 mDeviceVersion = device->common.version;
166 mDeviceInfo = info.static_camera_characteristics;
167 mInterface = std::make_unique<HalInterface>(device);
168
169 return initializeCommonLocked();
170}
171
172status_t Camera3Device::initialize(sp<CameraProviderManager> manager) {
173 ATRACE_CALL();
174 Mutex::Autolock il(mInterfaceLock);
175 Mutex::Autolock l(mLock);
176
177 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
178 if (mStatus != STATUS_UNINITIALIZED) {
179 CLOGE("Already initialized!");
180 return INVALID_OPERATION;
181 }
182 if (manager == nullptr) return INVALID_OPERATION;
183
184 sp<ICameraDeviceSession> session;
185 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800186 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800187 /*out*/ &session);
188 ATRACE_END();
189 if (res != OK) {
190 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
191 return res;
192 }
193
Steven Moreland5ff9c912017-03-09 23:13:00 -0800194 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800195 if (res != OK) {
196 SET_ERR_L("Could not retrive camera characteristics: %s (%d)", strerror(-res), res);
197 session->close();
198 return res;
199 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800200
201 // TODO: camera service will absorb 3_2/3_3/3_4 differences in the future
202 // for now use 3_4 to keep legacy devices working
203 mDeviceVersion = CAMERA_DEVICE_API_VERSION_3_4;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800204 mInterface = std::make_unique<HalInterface>(session);
205
206 return initializeCommonLocked();
207}
208
209status_t Camera3Device::initializeCommonLocked() {
210
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700211 /** Start up status tracker thread */
212 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800213 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700214 if (res != OK) {
215 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
216 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800217 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700218 mStatusTracker.clear();
219 return res;
220 }
221
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700222 /** Register in-flight map to the status tracker */
223 mInFlightStatusId = mStatusTracker->addComponent();
224
Zhijun He125684a2015-12-26 15:07:30 -0800225 /** Create buffer manager */
226 mBufferManager = new Camera3BufferManager();
227
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700228 bool aeLockAvailable = false;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800229 camera_metadata_entry aeLockAvailableEntry = mDeviceInfo.find(
230 ANDROID_CONTROL_AE_LOCK_AVAILABLE);
231 if (aeLockAvailableEntry.count > 0) {
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700232 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
233 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
234 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800235
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700236 /** Start up request queue thread */
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800237 mRequestThread = new RequestThread(this, mStatusTracker, mInterface.get(), mDeviceVersion,
238 aeLockAvailable);
239 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800240 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700241 SET_ERR_L("Unable to start request queue thread: %s (%d)",
242 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800243 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800244 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800245 return res;
246 }
247
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700248 mPreparerThread = new PreparerThread();
249
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700250 // Determine whether we need to derive sensitivity boost values for older devices.
251 // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
252 // be listed (as the default value 100)
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800253 if (mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700254 mDerivePostRawSensKey = true;
255 }
256
Ruben Brunk183f0562015-08-12 12:55:02 -0700257 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800258 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700259 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700260 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700261 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800262
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800263 // Measure the clock domain offset between camera and video/hw_composer
264 camera_metadata_entry timestampSource =
265 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
266 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
267 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
268 mTimestampOffset = getMonoToBoottimeOffset();
269 }
270
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700271 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700272 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
273 camera_metadata_entry partialResultsCount =
274 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
275 if (partialResultsCount.count > 0) {
276 mNumPartialResults = partialResultsCount.data.i32[0];
277 mUsePartialResult = (mNumPartialResults > 1);
278 }
279 } else {
280 camera_metadata_entry partialResultsQuirk =
281 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
282 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
283 mUsePartialResult = true;
284 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700285 }
286
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700287 camera_metadata_entry configs =
288 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
289 for (uint32_t i = 0; i < configs.count; i += 4) {
290 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
291 configs.data.i32[i + 3] ==
292 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
293 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
294 configs.data.i32[i + 2]));
295 }
296 }
297
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800298 return OK;
299}
300
301status_t Camera3Device::disconnect() {
302 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700303 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800304
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700305 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800306
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700307 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800308
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700309 {
310 Mutex::Autolock l(mLock);
311 if (mStatus == STATUS_UNINITIALIZED) return res;
312
313 if (mStatus == STATUS_ACTIVE ||
314 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
315 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700316 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700317 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700318 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700319 } else {
320 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
321 if (res != OK) {
322 SET_ERR_L("Timeout waiting for HAL to drain");
323 // Continue to close device even in case of error
324 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700325 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800326 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800327
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700328 if (mStatus == STATUS_ERROR) {
329 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700330 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700331
332 if (mStatusTracker != NULL) {
333 mStatusTracker->requestExit();
334 }
335
336 if (mRequestThread != NULL) {
337 mRequestThread->requestExit();
338 }
339
340 mOutputStreams.clear();
341 mInputStream.clear();
342 }
343
344 // Joining done without holding mLock, otherwise deadlocks may ensue
345 // as the threads try to access parent state
346 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
347 // HAL may be in a bad state, so waiting for request thread
348 // (which may be stuck in the HAL processCaptureRequest call)
349 // could be dangerous.
350 mRequestThread->join();
351 }
352
353 if (mStatusTracker != NULL) {
354 mStatusTracker->join();
355 }
356
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800357 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700358 {
359 Mutex::Autolock l(mLock);
360
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800361 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700362 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800363 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800364
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800365 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700366 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800367
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700368 // Call close without internal mutex held, as the HAL close may need to
369 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800370 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700371
372 {
373 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800374 mInterface->clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700375 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700376 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800377
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700378 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700379 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800380}
381
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700382// For dumping/debugging only -
383// try to acquire a lock a few times, eventually give up to proceed with
384// debug/dump operations
385bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
386 bool gotLock = false;
387 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
388 if (lock.tryLock() == NO_ERROR) {
389 gotLock = true;
390 break;
391 } else {
392 usleep(kDumpSleepDuration);
393 }
394 }
395 return gotLock;
396}
397
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700398Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
399 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
400 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
401 const int STREAM_CONFIGURATION_SIZE = 4;
402 const int STREAM_FORMAT_OFFSET = 0;
403 const int STREAM_WIDTH_OFFSET = 1;
404 const int STREAM_HEIGHT_OFFSET = 2;
405 const int STREAM_IS_INPUT_OFFSET = 3;
406 camera_metadata_ro_entry_t availableStreamConfigs =
407 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
408 if (availableStreamConfigs.count == 0 ||
409 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
410 return Size(0, 0);
411 }
412
413 // Get max jpeg size (area-wise).
414 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
415 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
416 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
417 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
418 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
419 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
420 && format == HAL_PIXEL_FORMAT_BLOB &&
421 (width * height > maxJpegWidth * maxJpegHeight)) {
422 maxJpegWidth = width;
423 maxJpegHeight = height;
424 }
425 }
426 } else {
427 camera_metadata_ro_entry availableJpegSizes =
428 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
429 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
430 return Size(0, 0);
431 }
432
433 // Get max jpeg size (area-wise).
434 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
435 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
436 > (maxJpegWidth * maxJpegHeight)) {
437 maxJpegWidth = availableJpegSizes.data.i32[i];
438 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
439 }
440 }
441 }
442 return Size(maxJpegWidth, maxJpegHeight);
443}
444
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800445nsecs_t Camera3Device::getMonoToBoottimeOffset() {
446 // try three times to get the clock offset, choose the one
447 // with the minimum gap in measurements.
448 const int tries = 3;
449 nsecs_t bestGap, measured;
450 for (int i = 0; i < tries; ++i) {
451 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
452 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
453 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
454 const nsecs_t gap = tmono2 - tmono;
455 if (i == 0 || gap < bestGap) {
456 bestGap = gap;
457 measured = tbase - ((tmono + tmono2) >> 1);
458 }
459 }
460 return measured;
461}
462
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -0700463/**
464 * Map Android N dataspace definitions back to Android M definitions, for
465 * use with HALv3.3 or older.
466 *
467 * Only map where correspondences exist, and otherwise preserve the value.
468 */
469android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) {
470 switch (dataSpace) {
471 case HAL_DATASPACE_V0_SRGB_LINEAR:
472 return HAL_DATASPACE_SRGB_LINEAR;
473 case HAL_DATASPACE_V0_SRGB:
474 return HAL_DATASPACE_SRGB;
475 case HAL_DATASPACE_V0_JFIF:
476 return HAL_DATASPACE_JFIF;
477 case HAL_DATASPACE_V0_BT601_625:
478 return HAL_DATASPACE_BT601_625;
479 case HAL_DATASPACE_V0_BT601_525:
480 return HAL_DATASPACE_BT601_525;
481 case HAL_DATASPACE_V0_BT709:
482 return HAL_DATASPACE_BT709;
483 default:
484 return dataSpace;
485 }
486}
487
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800488hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
489 int frameworkFormat) {
490 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
491}
492
493DataspaceFlags Camera3Device::mapToHidlDataspace(
494 android_dataspace dataSpace) {
495 return dataSpace;
496}
497
498ConsumerUsageFlags Camera3Device::mapToConsumerUsage(
499 uint32_t usage) {
500 return usage;
501}
502
503StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
504 switch (rotation) {
505 case CAMERA3_STREAM_ROTATION_0:
506 return StreamRotation::ROTATION_0;
507 case CAMERA3_STREAM_ROTATION_90:
508 return StreamRotation::ROTATION_90;
509 case CAMERA3_STREAM_ROTATION_180:
510 return StreamRotation::ROTATION_180;
511 case CAMERA3_STREAM_ROTATION_270:
512 return StreamRotation::ROTATION_270;
513 }
514 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
515 return StreamRotation::ROTATION_0;
516}
517
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800518status_t Camera3Device::mapToStreamConfigurationMode(
519 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
520 if (mode == nullptr) return BAD_VALUE;
521 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
522 switch(operationMode) {
523 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
524 *mode = StreamConfigurationMode::NORMAL_MODE;
525 break;
526 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
527 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
528 break;
529 default:
530 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
531 return BAD_VALUE;
532 }
533 } else {
534 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800535 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800536 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800537}
538
539camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
540 switch (status) {
541 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
542 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
543 }
544 return CAMERA3_BUFFER_STATUS_ERROR;
545}
546
547int Camera3Device::mapToFrameworkFormat(
548 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
549 return static_cast<uint32_t>(pixelFormat);
550}
551
552uint32_t Camera3Device::mapConsumerToFrameworkUsage(
553 ConsumerUsageFlags usage) {
554 return usage;
555}
556
557uint32_t Camera3Device::mapProducerToFrameworkUsage(
558 ProducerUsageFlags usage) {
559 return usage;
560}
561
Zhijun Hef7da0962014-04-24 13:27:56 -0700562ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700563 // Get max jpeg size (area-wise).
564 Size maxJpegResolution = getMaxJpegResolution();
565 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800566 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
567 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700568 return BAD_VALUE;
569 }
570
Zhijun Hef7da0962014-04-24 13:27:56 -0700571 // Get max jpeg buffer size
572 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700573 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
574 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800575 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
576 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700577 return BAD_VALUE;
578 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700579 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800580 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700581
582 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700583 float scaleFactor = ((float) (width * height)) /
584 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800585 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
586 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700587 if (jpegBufferSize > maxJpegBufferSize) {
588 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700589 }
590
591 return jpegBufferSize;
592}
593
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700594ssize_t Camera3Device::getPointCloudBufferSize() const {
595 const int FLOATS_PER_POINT=4;
596 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
597 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800598 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
599 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700600 return BAD_VALUE;
601 }
602 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
603 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
604 return maxBytesForPointCloud;
605}
606
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800607ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800608 const int PER_CONFIGURATION_SIZE = 3;
609 const int WIDTH_OFFSET = 0;
610 const int HEIGHT_OFFSET = 1;
611 const int SIZE_OFFSET = 2;
612 camera_metadata_ro_entry rawOpaqueSizes =
613 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800614 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800615 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800616 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
617 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800618 return BAD_VALUE;
619 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700620
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800621 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
622 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
623 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
624 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
625 }
626 }
627
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800628 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
629 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800630 return BAD_VALUE;
631}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700632
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800633status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
634 ATRACE_CALL();
635 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700636
637 // Try to lock, but continue in case of failure (to avoid blocking in
638 // deadlocks)
639 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
640 bool gotLock = tryLockSpinRightRound(mLock);
641
642 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800643 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
644 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700645 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800646 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
647 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700648
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800649 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700650
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800651 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700652 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800653 int n = args.size();
654 for (int i = 0; i < n; i++) {
655 if (args[i] == templatesOption) {
656 dumpTemplates = true;
657 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700658 if (args[i] == monitorOption) {
659 if (i + 1 < n) {
660 String8 monitorTags = String8(args[i + 1]);
661 if (monitorTags == "off") {
662 mTagMonitor.disableMonitoring();
663 } else {
664 mTagMonitor.parseTagsToMonitor(monitorTags);
665 }
666 } else {
667 mTagMonitor.disableMonitoring();
668 }
669 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800670 }
671
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800672 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800673
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800674 const char *status =
675 mStatus == STATUS_ERROR ? "ERROR" :
676 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700677 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
678 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800679 mStatus == STATUS_ACTIVE ? "ACTIVE" :
680 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700681
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800682 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700683 if (mStatus == STATUS_ERROR) {
684 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
685 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800686 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800687 const char *mode =
688 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
689 mOperatingMode == static_cast<int>(
690 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
691 "CUSTOM";
692 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800693
694 if (mInputStream != NULL) {
695 write(fd, lines.string(), lines.size());
696 mInputStream->dump(fd, args);
697 } else {
698 lines.appendFormat(" No input stream.\n");
699 write(fd, lines.string(), lines.size());
700 }
701 for (size_t i = 0; i < mOutputStreams.size(); i++) {
702 mOutputStreams[i]->dump(fd,args);
703 }
704
Zhijun He431503c2016-03-07 17:30:16 -0800705 if (mBufferManager != NULL) {
706 lines = String8(" Camera3 Buffer Manager:\n");
707 write(fd, lines.string(), lines.size());
708 mBufferManager->dump(fd, args);
709 }
Zhijun He125684a2015-12-26 15:07:30 -0800710
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700711 lines = String8(" In-flight requests:\n");
712 if (mInFlightMap.size() == 0) {
713 lines.append(" None\n");
714 } else {
715 for (size_t i = 0; i < mInFlightMap.size(); i++) {
716 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700717 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700718 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800719 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700720 r.numBuffersLeft);
721 }
722 }
723 write(fd, lines.string(), lines.size());
724
Igor Murashkin1e479c02013-09-06 16:55:14 -0700725 {
726 lines = String8(" Last request sent:\n");
727 write(fd, lines.string(), lines.size());
728
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700729 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700730 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
731 }
732
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800733 if (dumpTemplates) {
734 const char *templateNames[] = {
735 "TEMPLATE_PREVIEW",
736 "TEMPLATE_STILL_CAPTURE",
737 "TEMPLATE_VIDEO_RECORD",
738 "TEMPLATE_VIDEO_SNAPSHOT",
739 "TEMPLATE_ZERO_SHUTTER_LAG",
740 "TEMPLATE_MANUAL"
741 };
742
743 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800744 camera_metadata_t *templateRequest = nullptr;
745 mInterface->constructDefaultRequestSettings(
746 (camera3_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800747 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800748 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800749 lines.append(" Not supported\n");
750 write(fd, lines.string(), lines.size());
751 } else {
752 write(fd, lines.string(), lines.size());
753 dump_indented_camera_metadata(templateRequest,
754 fd, /*verbosity*/2, /*indentation*/8);
755 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800756 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800757 }
758 }
759
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700760 mTagMonitor.dumpMonitoredMetadata(fd);
761
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800762 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800763 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800764 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800765 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800766 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800767
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700768 if (gotLock) mLock.unlock();
769 if (gotInterfaceLock) mInterfaceLock.unlock();
770
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800771 return OK;
772}
773
774const CameraMetadata& Camera3Device::info() const {
775 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800776 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
777 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700778 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800779 mStatus == STATUS_ERROR ?
780 "when in error state" : "before init");
781 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800782 return mDeviceInfo;
783}
784
Jianing Wei90e59c92014-03-12 18:29:36 -0700785status_t Camera3Device::checkStatusOkToCaptureLocked() {
786 switch (mStatus) {
787 case STATUS_ERROR:
788 CLOGE("Device has encountered a serious error");
789 return INVALID_OPERATION;
790 case STATUS_UNINITIALIZED:
791 CLOGE("Device not initialized");
792 return INVALID_OPERATION;
793 case STATUS_UNCONFIGURED:
794 case STATUS_CONFIGURED:
795 case STATUS_ACTIVE:
796 // OK
797 break;
798 default:
799 SET_ERR_L("Unexpected status: %d", mStatus);
800 return INVALID_OPERATION;
801 }
802 return OK;
803}
804
805status_t Camera3Device::convertMetadataListToRequestListLocked(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700806 const List<const CameraMetadata> &metadataList,
807 const std::list<const SurfaceMap> &surfaceMaps,
808 bool repeating,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700809 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700810 if (requestList == NULL) {
811 CLOGE("requestList cannot be NULL.");
812 return BAD_VALUE;
813 }
814
Jianing Weicb0652e2014-03-12 18:29:36 -0700815 int32_t burstId = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700816 List<const CameraMetadata>::const_iterator metadataIt = metadataList.begin();
817 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
818 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
819 ++metadataIt, ++surfaceMapIt) {
820 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700821 if (newRequest == 0) {
822 CLOGE("Can't create capture request");
823 return BAD_VALUE;
824 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700825
Shuzhen Wang9d066012016-09-30 11:30:20 -0700826 newRequest->mRepeating = repeating;
827
Jianing Weicb0652e2014-03-12 18:29:36 -0700828 // Setup burst Id and request Id
829 newRequest->mResultExtras.burstId = burstId++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700830 if (metadataIt->exists(ANDROID_REQUEST_ID)) {
831 if (metadataIt->find(ANDROID_REQUEST_ID).count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700832 CLOGE("RequestID entry exists; but must not be empty in metadata");
833 return BAD_VALUE;
834 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700835 newRequest->mResultExtras.requestId = metadataIt->find(ANDROID_REQUEST_ID).data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700836 } else {
837 CLOGE("RequestID does not exist in metadata");
838 return BAD_VALUE;
839 }
840
Jianing Wei90e59c92014-03-12 18:29:36 -0700841 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700842
843 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700844 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700845 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
846 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
847 return BAD_VALUE;
848 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700849
850 // Setup batch size if this is a high speed video recording request.
851 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
852 auto firstRequest = requestList->begin();
853 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
854 if (outputStream->isVideoStream()) {
855 (*firstRequest)->mBatchSize = requestList->size();
856 break;
857 }
858 }
859 }
860
Jianing Wei90e59c92014-03-12 18:29:36 -0700861 return OK;
862}
863
Jianing Weicb0652e2014-03-12 18:29:36 -0700864status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800865 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800866
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700867 List<const CameraMetadata> requests;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700868 std::list<const SurfaceMap> surfaceMaps;
869 convertToRequestList(requests, surfaceMaps, request);
870
871 return captureList(requests, surfaceMaps, /*lastFrameNumber*/NULL);
872}
873
874void Camera3Device::convertToRequestList(List<const CameraMetadata>& requests,
875 std::list<const SurfaceMap>& surfaceMaps,
876 const CameraMetadata& request) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700877 requests.push_back(request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700878
879 SurfaceMap surfaceMap;
880 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
881 // With no surface list passed in, stream and surface will have 1-to-1
882 // mapping. So the surface index is 0 for each stream in the surfaceMap.
883 for (size_t i = 0; i < streams.count; i++) {
884 surfaceMap[streams.data.i32[i]].push_back(0);
885 }
886 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800887}
888
Jianing Wei90e59c92014-03-12 18:29:36 -0700889status_t Camera3Device::submitRequestsHelper(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700890 const List<const CameraMetadata> &requests,
891 const std::list<const SurfaceMap> &surfaceMaps,
892 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700893 /*out*/
894 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700895 ATRACE_CALL();
896 Mutex::Autolock il(mInterfaceLock);
897 Mutex::Autolock l(mLock);
898
899 status_t res = checkStatusOkToCaptureLocked();
900 if (res != OK) {
901 // error logged by previous call
902 return res;
903 }
904
905 RequestList requestList;
906
Shuzhen Wang0129d522016-10-30 22:43:41 -0700907 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
908 repeating, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700909 if (res != OK) {
910 // error logged by previous call
911 return res;
912 }
913
914 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700915 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700916 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700917 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700918 }
919
920 if (res == OK) {
921 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
922 if (res != OK) {
923 SET_ERR_L("Can't transition to active in %f seconds!",
924 kActiveTimeout/1e9);
925 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800926 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700927 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700928 } else {
929 CLOGE("Cannot queue request. Impossible.");
930 return BAD_VALUE;
931 }
932
933 return res;
934}
935
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800936hardware::Return<void> Camera3Device::processCaptureResult(
937 const device::V3_2::CaptureResult& result) {
938 camera3_capture_result r;
939 status_t res;
940 r.frame_number = result.frameNumber;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800941 if (result.result.size() != 0) {
942 r.result = reinterpret_cast<const camera_metadata_t*>(result.result.data());
943 size_t expected_metadata_size = result.result.size();
944 if ((res = validate_camera_metadata_structure(r.result, &expected_metadata_size)) != OK) {
945 ALOGE("%s: Frame %d: Invalid camera metadata received by camera service from HAL: %s (%d)",
946 __FUNCTION__, result.frameNumber, strerror(-res), res);
947 return hardware::Void();
948 }
949 } else {
950 r.result = nullptr;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800951 }
952
953 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
954 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
955 for (size_t i = 0; i < result.outputBuffers.size(); i++) {
956 auto& bDst = outputBuffers[i];
957 const StreamBuffer &bSrc = result.outputBuffers[i];
958
959 ssize_t idx = mOutputStreams.indexOfKey(bSrc.streamId);
960 if (idx == -1) {
961 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
962 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
963 return hardware::Void();
964 }
965 bDst.stream = mOutputStreams.valueAt(idx)->asHalStream();
966
967 buffer_handle_t *buffer;
Yin-Chia Yehf4650602017-01-10 13:13:39 -0800968 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800969 if (res != OK) {
970 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
971 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
972 return hardware::Void();
973 }
974 bDst.buffer = buffer;
975 bDst.status = mapHidlBufferStatus(bSrc.status);
976 bDst.acquire_fence = -1;
977 if (bSrc.releaseFence == nullptr) {
978 bDst.release_fence = -1;
979 } else if (bSrc.releaseFence->numFds == 1) {
980 bDst.release_fence = dup(bSrc.releaseFence->data[0]);
981 } else {
982 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
983 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
984 return hardware::Void();
985 }
986 }
987 r.num_output_buffers = outputBuffers.size();
988 r.output_buffers = outputBuffers.data();
989
990 camera3_stream_buffer_t inputBuffer;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800991 if (result.inputBuffer.streamId == -1) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800992 r.input_buffer = nullptr;
993 } else {
994 if (mInputStream->getId() != result.inputBuffer.streamId) {
995 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
996 result.frameNumber, result.inputBuffer.streamId);
997 return hardware::Void();
998 }
999 inputBuffer.stream = mInputStream->asHalStream();
1000 buffer_handle_t *buffer;
1001 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
1002 &buffer);
1003 if (res != OK) {
1004 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
1005 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
1006 return hardware::Void();
1007 }
1008 inputBuffer.buffer = buffer;
1009 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
1010 inputBuffer.acquire_fence = -1;
1011 if (result.inputBuffer.releaseFence == nullptr) {
1012 inputBuffer.release_fence = -1;
1013 } else if (result.inputBuffer.releaseFence->numFds == 1) {
1014 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
1015 } else {
1016 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
1017 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
1018 return hardware::Void();
1019 }
1020 r.input_buffer = &inputBuffer;
1021 }
1022
1023 r.partial_result = result.partialResult;
1024
1025 processCaptureResult(&r);
1026
1027 return hardware::Void();
1028}
1029
1030hardware::Return<void> Camera3Device::notify(
1031 const NotifyMsg& msg) {
1032 camera3_notify_msg m;
1033 switch (msg.type) {
1034 case MsgType::ERROR:
1035 m.type = CAMERA3_MSG_ERROR;
1036 m.message.error.frame_number = msg.msg.error.frameNumber;
1037 if (msg.msg.error.errorStreamId >= 0) {
1038 ssize_t idx = mOutputStreams.indexOfKey(msg.msg.error.errorStreamId);
1039 if (idx == -1) {
1040 ALOGE("%s: Frame %d: Invalid error stream id %d",
1041 __FUNCTION__, m.message.error.frame_number, msg.msg.error.errorStreamId);
1042 return hardware::Void();
1043 }
1044 m.message.error.error_stream = mOutputStreams.valueAt(idx)->asHalStream();
1045 } else {
1046 m.message.error.error_stream = nullptr;
1047 }
1048 switch (msg.msg.error.errorCode) {
1049 case ErrorCode::ERROR_DEVICE:
1050 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
1051 break;
1052 case ErrorCode::ERROR_REQUEST:
1053 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
1054 break;
1055 case ErrorCode::ERROR_RESULT:
1056 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
1057 break;
1058 case ErrorCode::ERROR_BUFFER:
1059 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
1060 break;
1061 }
1062 break;
1063 case MsgType::SHUTTER:
1064 m.type = CAMERA3_MSG_SHUTTER;
1065 m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
1066 m.message.shutter.timestamp = msg.msg.shutter.timestamp;
1067 break;
1068 }
1069 notify(&m);
1070
1071 return hardware::Void();
1072}
1073
Jianing Weicb0652e2014-03-12 18:29:36 -07001074status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001075 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001076 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001077 ATRACE_CALL();
1078
Shuzhen Wang0129d522016-10-30 22:43:41 -07001079 return submitRequestsHelper(requests, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001080}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001081
Jianing Weicb0652e2014-03-12 18:29:36 -07001082status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1083 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001084 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001085
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001086 List<const CameraMetadata> requests;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001087 std::list<const SurfaceMap> surfaceMaps;
1088 convertToRequestList(requests, surfaceMaps, request);
1089
1090 return setStreamingRequestList(requests, /*surfaceMap*/surfaceMaps,
1091 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001092}
1093
Jianing Weicb0652e2014-03-12 18:29:36 -07001094status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001095 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001096 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001097 ATRACE_CALL();
1098
Shuzhen Wang0129d522016-10-30 22:43:41 -07001099 return submitRequestsHelper(requests, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001100}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001101
1102sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Shuzhen Wang0129d522016-10-30 22:43:41 -07001103 const CameraMetadata &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001104 status_t res;
1105
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001106 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001107 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1108 // so unilaterally select normal operating mode.
1109 res = configureStreamsLocked(CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001110 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001111 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001112 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001113 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001114 } else if (mStatus == STATUS_UNCONFIGURED) {
1115 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001116 CLOGE("No streams configured");
1117 return NULL;
1118 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001119 }
1120
Shuzhen Wang0129d522016-10-30 22:43:41 -07001121 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001122 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001123}
1124
Jianing Weicb0652e2014-03-12 18:29:36 -07001125status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001126 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001127 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001128 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001129
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001130 switch (mStatus) {
1131 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001132 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001133 return INVALID_OPERATION;
1134 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001135 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001136 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001137 case STATUS_UNCONFIGURED:
1138 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001139 case STATUS_ACTIVE:
1140 // OK
1141 break;
1142 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001143 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001144 return INVALID_OPERATION;
1145 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001146 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001147
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001148 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001149}
1150
1151status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1152 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001153 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001154
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001155 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001156}
1157
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001158status_t Camera3Device::createInputStream(
1159 uint32_t width, uint32_t height, int format, int *id) {
1160 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001161 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001162 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001163 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1164 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001165
1166 status_t res;
1167 bool wasActive = false;
1168
1169 switch (mStatus) {
1170 case STATUS_ERROR:
1171 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1172 return INVALID_OPERATION;
1173 case STATUS_UNINITIALIZED:
1174 ALOGE("%s: Device not initialized", __FUNCTION__);
1175 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001176 case STATUS_UNCONFIGURED:
1177 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001178 // OK
1179 break;
1180 case STATUS_ACTIVE:
1181 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001182 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001183 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001184 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001185 return res;
1186 }
1187 wasActive = true;
1188 break;
1189 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001190 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001191 return INVALID_OPERATION;
1192 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001193 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001194
1195 if (mInputStream != 0) {
1196 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1197 return INVALID_OPERATION;
1198 }
1199
1200 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1201 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001202 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001203
1204 mInputStream = newStream;
1205
1206 *id = mNextStreamId++;
1207
1208 // Continue captures if active at start
1209 if (wasActive) {
1210 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001211 // Reuse current operating mode for new stream config
1212 res = configureStreamsLocked(mOperatingMode);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001213 if (res != OK) {
1214 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1215 __FUNCTION__, mNextStreamId, strerror(-res), res);
1216 return res;
1217 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001218 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001219 }
1220
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001221 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001222 return OK;
1223}
1224
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001225status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001226 uint32_t width, uint32_t height, int format,
1227 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001228 int streamSetId, bool isShared, uint32_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001229 ATRACE_CALL();
1230
1231 if (consumer == nullptr) {
1232 ALOGE("%s: consumer must not be null", __FUNCTION__);
1233 return BAD_VALUE;
1234 }
1235
1236 std::vector<sp<Surface>> consumers;
1237 consumers.push_back(consumer);
1238
1239 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001240 format, dataSpace, rotation, id, streamSetId, isShared, consumerUsage);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001241}
1242
1243status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1244 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
1245 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001246 int streamSetId, bool isShared, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001247 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001248 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001249 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001250 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wang758c2152017-01-10 18:26:18 -08001251 " consumer usage 0x%x, isShared %d", mId.string(), mNextStreamId, width, height, format,
1252 dataSpace, rotation, consumerUsage, isShared);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001253
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001254 status_t res;
1255 bool wasActive = false;
1256
1257 switch (mStatus) {
1258 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001259 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001260 return INVALID_OPERATION;
1261 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001262 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001263 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001264 case STATUS_UNCONFIGURED:
1265 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001266 // OK
1267 break;
1268 case STATUS_ACTIVE:
1269 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001270 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001271 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001272 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001273 return res;
1274 }
1275 wasActive = true;
1276 break;
1277 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001278 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001279 return INVALID_OPERATION;
1280 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001281 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001282
1283 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001284 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001285 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001286 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001287 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1288 }
Zhijun He5d677d12016-05-29 16:52:39 -07001289
Shuzhen Wang0129d522016-10-30 22:43:41 -07001290 if (consumers.size() == 0 && !hasDeferredConsumer) {
1291 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1292 return BAD_VALUE;
1293 }
Zhijun He5d677d12016-05-29 16:52:39 -07001294 // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
1295 // which requires a consumer surface to be available.
Shuzhen Wang0129d522016-10-30 22:43:41 -07001296 if (hasDeferredConsumer && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He5d677d12016-05-29 16:52:39 -07001297 ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
1298 return BAD_VALUE;
1299 }
1300
Shuzhen Wang0129d522016-10-30 22:43:41 -07001301 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001302 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1303 return BAD_VALUE;
1304 }
1305
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001306 // Use legacy dataspace values for older HALs
1307 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1308 dataSpace = mapToLegacyDataspace(dataSpace);
1309 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001310 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001311 ssize_t blobBufferSize;
1312 if (dataSpace != HAL_DATASPACE_DEPTH) {
1313 blobBufferSize = getJpegBufferSize(width, height);
1314 if (blobBufferSize <= 0) {
1315 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1316 return BAD_VALUE;
1317 }
1318 } else {
1319 blobBufferSize = getPointCloudBufferSize();
1320 if (blobBufferSize <= 0) {
1321 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1322 return BAD_VALUE;
1323 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001324 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001325 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001326 width, height, blobBufferSize, format, dataSpace, rotation,
1327 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001328 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1329 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1330 if (rawOpaqueBufferSize <= 0) {
1331 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1332 return BAD_VALUE;
1333 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001334 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001335 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1336 mTimestampOffset, streamSetId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001337 } else if (isShared) {
1338 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1339 width, height, format, consumerUsage, dataSpace, rotation,
1340 mTimestampOffset, streamSetId);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001341 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001342 newStream = new Camera3OutputStream(mNextStreamId,
1343 width, height, format, consumerUsage, dataSpace, rotation,
1344 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001345 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001346 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001347 width, height, format, dataSpace, rotation,
1348 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001349 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001350 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001351
Zhijun He125684a2015-12-26 15:07:30 -08001352 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001353 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1354 * requires buffers to be statically allocated for internal static buffer registration, while
1355 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1356 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001357 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001358 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001359 newStream->setBufferManager(mBufferManager);
1360 }
1361
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001362 res = mOutputStreams.add(mNextStreamId, newStream);
1363 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001364 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001365 return res;
1366 }
1367
1368 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001369 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001370
1371 // Continue captures if active at start
1372 if (wasActive) {
1373 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001374 // Reuse current operating mode for new stream config
1375 res = configureStreamsLocked(mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001376 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001377 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1378 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001379 return res;
1380 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001381 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001382 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001383 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001384 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001385}
1386
1387status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1388 ATRACE_CALL();
1389 (void)outputId; (void)id;
1390
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001391 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001392 return INVALID_OPERATION;
1393}
1394
1395
1396status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001397 uint32_t *width, uint32_t *height,
1398 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001399 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001400 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001401 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001402
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001403 switch (mStatus) {
1404 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001405 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001406 return INVALID_OPERATION;
1407 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001408 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001409 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001410 case STATUS_UNCONFIGURED:
1411 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001412 case STATUS_ACTIVE:
1413 // OK
1414 break;
1415 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001416 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001417 return INVALID_OPERATION;
1418 }
1419
1420 ssize_t idx = mOutputStreams.indexOfKey(id);
1421 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001422 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001423 return idx;
1424 }
1425
1426 if (width) *width = mOutputStreams[idx]->getWidth();
1427 if (height) *height = mOutputStreams[idx]->getHeight();
1428 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001429 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001430 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001431}
1432
1433status_t Camera3Device::setStreamTransform(int id,
1434 int transform) {
1435 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001436 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001437 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001438
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001439 switch (mStatus) {
1440 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001441 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001442 return INVALID_OPERATION;
1443 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001444 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001445 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001446 case STATUS_UNCONFIGURED:
1447 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001448 case STATUS_ACTIVE:
1449 // OK
1450 break;
1451 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001452 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001453 return INVALID_OPERATION;
1454 }
1455
1456 ssize_t idx = mOutputStreams.indexOfKey(id);
1457 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001458 CLOGE("Stream %d does not exist",
1459 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001460 return BAD_VALUE;
1461 }
1462
1463 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001464}
1465
1466status_t Camera3Device::deleteStream(int id) {
1467 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001468 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001469 Mutex::Autolock l(mLock);
1470 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001471
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001472 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001473
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001474 // CameraDevice semantics require device to already be idle before
1475 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001476 if (mStatus == STATUS_ACTIVE) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001477 ALOGV("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001478 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001479 }
1480
Igor Murashkin2fba5842013-04-22 14:03:54 -07001481 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001482 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001483 if (mInputStream != NULL && id == mInputStream->getId()) {
1484 deletedStream = mInputStream;
1485 mInputStream.clear();
1486 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001487 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001488 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001489 return BAD_VALUE;
1490 }
Zhijun He5f446352014-01-22 09:49:33 -08001491 }
1492
1493 // Delete output stream or the output part of a bi-directional stream.
1494 if (outputStreamIdx != NAME_NOT_FOUND) {
1495 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001496 mOutputStreams.removeItem(id);
1497 }
1498
1499 // Free up the stream endpoint so that it can be used by some other stream
1500 res = deletedStream->disconnect();
1501 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001502 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001503 // fall through since we want to still list the stream as deleted.
1504 }
1505 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001506 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001507
1508 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001509}
1510
1511status_t Camera3Device::deleteReprocessStream(int id) {
1512 ATRACE_CALL();
1513 (void)id;
1514
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001515 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001516 return INVALID_OPERATION;
1517}
1518
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08001519status_t Camera3Device::configureStreams(int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001520 ATRACE_CALL();
1521 ALOGV("%s: E", __FUNCTION__);
1522
1523 Mutex::Autolock il(mInterfaceLock);
1524 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001525
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001526 return configureStreamsLocked(operatingMode);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001527}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001528
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001529status_t Camera3Device::getInputBufferProducer(
1530 sp<IGraphicBufferProducer> *producer) {
1531 Mutex::Autolock il(mInterfaceLock);
1532 Mutex::Autolock l(mLock);
1533
1534 if (producer == NULL) {
1535 return BAD_VALUE;
1536 } else if (mInputStream == NULL) {
1537 return INVALID_OPERATION;
1538 }
1539
1540 return mInputStream->getInputBufferProducer(producer);
1541}
1542
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001543status_t Camera3Device::createDefaultRequest(int templateId,
1544 CameraMetadata *request) {
1545 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001546 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001547
1548 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1549 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1550 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1551 return BAD_VALUE;
1552 }
1553
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001554 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001555 Mutex::Autolock l(mLock);
1556
1557 switch (mStatus) {
1558 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001559 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001560 return INVALID_OPERATION;
1561 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001562 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001563 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001564 case STATUS_UNCONFIGURED:
1565 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001566 case STATUS_ACTIVE:
1567 // OK
1568 break;
1569 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001570 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001571 return INVALID_OPERATION;
1572 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001573
Zhijun Hea1530f12014-09-14 12:44:20 -07001574 if (!mRequestTemplateCache[templateId].isEmpty()) {
1575 *request = mRequestTemplateCache[templateId];
1576 return OK;
1577 }
1578
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001579 camera_metadata_t *rawRequest;
1580 status_t res = mInterface->constructDefaultRequestSettings(
1581 (camera3_request_template_t) templateId, &rawRequest);
1582 if (res == BAD_VALUE) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001583 ALOGI("%s: template %d is not supported on this camera device",
1584 __FUNCTION__, templateId);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001585 return res;
1586 } else if (res != OK) {
1587 CLOGE("Unable to construct request template %d: %s (%d)",
1588 templateId, strerror(-res), res);
1589 return res;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001590 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001591
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001592 mRequestTemplateCache[templateId].acquire(rawRequest);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001593
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001594 // Derive some new keys for backward compatibility
1595 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
1596 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
1597 int32_t defaultBoost[1] = {100};
1598 mRequestTemplateCache[templateId].update(
1599 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1600 defaultBoost, 1);
1601 }
1602
1603 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001604 return OK;
1605}
1606
1607status_t Camera3Device::waitUntilDrained() {
1608 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001609 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001610 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001611
Zhijun He69a37482014-03-23 18:44:49 -07001612 return waitUntilDrainedLocked();
1613}
1614
1615status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001616 switch (mStatus) {
1617 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001618 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001619 ALOGV("%s: Already idle", __FUNCTION__);
1620 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001621 case STATUS_CONFIGURED:
1622 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001623 case STATUS_ERROR:
1624 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001625 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001626 break;
1627 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001628 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001629 return INVALID_OPERATION;
1630 }
1631
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001632 ALOGV("%s: Camera %s: Waiting until idle", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001633 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001634 if (res != OK) {
1635 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1636 res);
1637 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001638 return res;
1639}
1640
Ruben Brunk183f0562015-08-12 12:55:02 -07001641
1642void Camera3Device::internalUpdateStatusLocked(Status status) {
1643 mStatus = status;
1644 mRecentStatusUpdates.add(mStatus);
1645 mStatusChanged.broadcast();
1646}
1647
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001648// Pause to reconfigure
1649status_t Camera3Device::internalPauseAndWaitLocked() {
1650 mRequestThread->setPaused(true);
1651 mPauseStateNotify = true;
1652
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001653 ALOGV("%s: Camera %s: Internal wait until idle", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001654 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1655 if (res != OK) {
1656 SET_ERR_L("Can't idle device in %f seconds!",
1657 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001658 }
1659
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001660 return res;
1661}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001662
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001663// Resume after internalPauseAndWaitLocked
1664status_t Camera3Device::internalResumeLocked() {
1665 status_t res;
1666
1667 mRequestThread->setPaused(false);
1668
1669 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1670 if (res != OK) {
1671 SET_ERR_L("Can't transition to active in %f seconds!",
1672 kActiveTimeout/1e9);
1673 }
1674 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001675 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001676}
1677
Ruben Brunk183f0562015-08-12 12:55:02 -07001678status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001679 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001680
1681 size_t startIndex = 0;
1682 if (mStatusWaiters == 0) {
1683 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1684 // this status list
1685 mRecentStatusUpdates.clear();
1686 } else {
1687 // If other threads are waiting on updates to this status list, set the position of the
1688 // first element that this list will check rather than clearing the list.
1689 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001690 }
1691
Ruben Brunk183f0562015-08-12 12:55:02 -07001692 mStatusWaiters++;
1693
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001694 bool stateSeen = false;
1695 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001696 if (active == (mStatus == STATUS_ACTIVE)) {
1697 // Desired state is current
1698 break;
1699 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001700
1701 res = mStatusChanged.waitRelative(mLock, timeout);
1702 if (res != OK) break;
1703
Ruben Brunk183f0562015-08-12 12:55:02 -07001704 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1705 // transitions.
1706 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1707 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1708 __FUNCTION__);
1709
1710 // Encountered desired state since we began waiting
1711 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001712 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1713 stateSeen = true;
1714 break;
1715 }
1716 }
1717 } while (!stateSeen);
1718
Ruben Brunk183f0562015-08-12 12:55:02 -07001719 mStatusWaiters--;
1720
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001721 return res;
1722}
1723
1724
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001725status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001726 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001727 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001728
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001729 if (listener != NULL && mListener != NULL) {
1730 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1731 }
1732 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001733 mRequestThread->setNotificationListener(listener);
1734 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001735
1736 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001737}
1738
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001739bool Camera3Device::willNotify3A() {
1740 return false;
1741}
1742
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001743status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001744 status_t res;
1745 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001746
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001747 while (mResultQueue.empty()) {
1748 res = mResultSignal.waitRelative(mOutputLock, timeout);
1749 if (res == TIMED_OUT) {
1750 return res;
1751 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001752 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
1753 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001754 return res;
1755 }
1756 }
1757 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001758}
1759
Jianing Weicb0652e2014-03-12 18:29:36 -07001760status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001761 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001762 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001763
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001764 if (mResultQueue.empty()) {
1765 return NOT_ENOUGH_DATA;
1766 }
1767
Jianing Weicb0652e2014-03-12 18:29:36 -07001768 if (frame == NULL) {
1769 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1770 return BAD_VALUE;
1771 }
1772
1773 CaptureResult &result = *(mResultQueue.begin());
1774 frame->mResultExtras = result.mResultExtras;
1775 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001776 mResultQueue.erase(mResultQueue.begin());
1777
1778 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001779}
1780
1781status_t Camera3Device::triggerAutofocus(uint32_t id) {
1782 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001783 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001784
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001785 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1786 // Mix-in this trigger into the next request and only the next request.
1787 RequestTrigger trigger[] = {
1788 {
1789 ANDROID_CONTROL_AF_TRIGGER,
1790 ANDROID_CONTROL_AF_TRIGGER_START
1791 },
1792 {
1793 ANDROID_CONTROL_AF_TRIGGER_ID,
1794 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001795 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001796 };
1797
1798 return mRequestThread->queueTrigger(trigger,
1799 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001800}
1801
1802status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1803 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001804 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001805
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001806 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1807 // Mix-in this trigger into the next request and only the next request.
1808 RequestTrigger trigger[] = {
1809 {
1810 ANDROID_CONTROL_AF_TRIGGER,
1811 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1812 },
1813 {
1814 ANDROID_CONTROL_AF_TRIGGER_ID,
1815 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001816 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001817 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001818
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001819 return mRequestThread->queueTrigger(trigger,
1820 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001821}
1822
1823status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1824 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001825 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001826
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001827 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1828 // Mix-in this trigger into the next request and only the next request.
1829 RequestTrigger trigger[] = {
1830 {
1831 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1832 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1833 },
1834 {
1835 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1836 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001837 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001838 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001839
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001840 return mRequestThread->queueTrigger(trigger,
1841 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001842}
1843
1844status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1845 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1846 ATRACE_CALL();
1847 (void)reprocessStreamId; (void)buffer; (void)listener;
1848
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001849 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001850 return INVALID_OPERATION;
1851}
1852
Jianing Weicb0652e2014-03-12 18:29:36 -07001853status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001854 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001855 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001856 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001857
Zhijun He7ef20392014-04-21 16:04:17 -07001858 {
1859 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001860 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001861 }
1862
Zhijun He491e3412013-12-27 10:57:44 -08001863 status_t res;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001864 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001865 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001866 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001867 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001868 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001869 }
1870
1871 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001872}
1873
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001874status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001875 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1876}
1877
1878status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001879 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001880 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001881 Mutex::Autolock il(mInterfaceLock);
1882 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001883
1884 sp<Camera3StreamInterface> stream;
1885 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1886 if (outputStreamIdx == NAME_NOT_FOUND) {
1887 CLOGE("Stream %d does not exist", streamId);
1888 return BAD_VALUE;
1889 }
1890
1891 stream = mOutputStreams.editValueAt(outputStreamIdx);
1892
1893 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001894 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001895 return BAD_VALUE;
1896 }
1897
1898 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001899 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001900 return BAD_VALUE;
1901 }
1902
Ruben Brunkc78ac262015-08-13 17:58:46 -07001903 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001904}
1905
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001906status_t Camera3Device::tearDown(int streamId) {
1907 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001908 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001909 Mutex::Autolock il(mInterfaceLock);
1910 Mutex::Autolock l(mLock);
1911
1912 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1913 // since we cannot call register_stream_buffers except right after configure_streams.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001914 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001915 ALOGE("%s: Unable to tear down streams on device HAL v%x",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001916 __FUNCTION__, mDeviceVersion);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001917 return NO_INIT;
1918 }
1919
1920 sp<Camera3StreamInterface> stream;
1921 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1922 if (outputStreamIdx == NAME_NOT_FOUND) {
1923 CLOGE("Stream %d does not exist", streamId);
1924 return BAD_VALUE;
1925 }
1926
1927 stream = mOutputStreams.editValueAt(outputStreamIdx);
1928
1929 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1930 CLOGE("Stream %d is a target of a in-progress request", streamId);
1931 return BAD_VALUE;
1932 }
1933
1934 return stream->tearDown();
1935}
1936
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001937status_t Camera3Device::addBufferListenerForStream(int streamId,
1938 wp<Camera3StreamBufferListener> listener) {
1939 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001940 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001941 Mutex::Autolock il(mInterfaceLock);
1942 Mutex::Autolock l(mLock);
1943
1944 sp<Camera3StreamInterface> stream;
1945 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1946 if (outputStreamIdx == NAME_NOT_FOUND) {
1947 CLOGE("Stream %d does not exist", streamId);
1948 return BAD_VALUE;
1949 }
1950
1951 stream = mOutputStreams.editValueAt(outputStreamIdx);
1952 stream->addBufferListener(listener);
1953
1954 return OK;
1955}
1956
Zhijun He204e3292014-07-14 17:09:23 -07001957uint32_t Camera3Device::getDeviceVersion() {
1958 ATRACE_CALL();
1959 Mutex::Autolock il(mInterfaceLock);
1960 return mDeviceVersion;
1961}
1962
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001963/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001964 * Methods called by subclasses
1965 */
1966
1967void Camera3Device::notifyStatus(bool idle) {
1968 {
1969 // Need mLock to safely update state and synchronize to current
1970 // state of methods in flight.
1971 Mutex::Autolock l(mLock);
1972 // We can get various system-idle notices from the status tracker
1973 // while starting up. Only care about them if we've actually sent
1974 // in some requests recently.
1975 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1976 return;
1977 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001978 ALOGV("%s: Camera %s: Now %s", __FUNCTION__, mId.string(),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001979 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001980 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001981
1982 // Skip notifying listener if we're doing some user-transparent
1983 // state changes
1984 if (mPauseStateNotify) return;
1985 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001986
1987 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001988 {
1989 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001990 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001991 }
1992 if (idle && listener != NULL) {
1993 listener->notifyIdle();
1994 }
1995}
1996
Shuzhen Wang758c2152017-01-10 18:26:18 -08001997status_t Camera3Device::setConsumerSurfaces(int streamId,
1998 const std::vector<sp<Surface>>& consumers) {
Zhijun He5d677d12016-05-29 16:52:39 -07001999 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002000 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2001 __FUNCTION__, mId.string(), streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07002002 Mutex::Autolock il(mInterfaceLock);
2003 Mutex::Autolock l(mLock);
2004
Shuzhen Wang758c2152017-01-10 18:26:18 -08002005 if (consumers.size() == 0) {
2006 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002007 return BAD_VALUE;
2008 }
2009
2010 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2011 if (idx == NAME_NOT_FOUND) {
2012 CLOGE("Stream %d is unknown", streamId);
2013 return idx;
2014 }
2015 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
Shuzhen Wang758c2152017-01-10 18:26:18 -08002016 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002017 if (res != OK) {
2018 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2019 return res;
2020 }
2021
Shuzhen Wang0129d522016-10-30 22:43:41 -07002022 if (stream->isConsumerConfigurationDeferred()) {
2023 if (!stream->isConfiguring()) {
2024 CLOGE("Stream %d was already fully configured.", streamId);
2025 return INVALID_OPERATION;
2026 }
Zhijun He5d677d12016-05-29 16:52:39 -07002027
Shuzhen Wang0129d522016-10-30 22:43:41 -07002028 res = stream->finishConfiguration();
2029 if (res != OK) {
2030 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2031 stream->getId(), strerror(-res), res);
2032 return res;
2033 }
Zhijun He5d677d12016-05-29 16:52:39 -07002034 }
2035
2036 return OK;
2037}
2038
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002039/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002040 * Camera3Device private methods
2041 */
2042
2043sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Shuzhen Wang0129d522016-10-30 22:43:41 -07002044 const CameraMetadata &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002045 ATRACE_CALL();
2046 status_t res;
2047
2048 sp<CaptureRequest> newRequest = new CaptureRequest;
2049 newRequest->mSettings = request;
2050
2051 camera_metadata_entry_t inputStreams =
2052 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
2053 if (inputStreams.count > 0) {
2054 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002055 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002056 CLOGE("Request references unknown input stream %d",
2057 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002058 return NULL;
2059 }
2060 // Lazy completion of stream configuration (allocation/registration)
2061 // on first use
2062 if (mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002063 res = mInputStream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002064 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002065 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002066 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002067 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002068 return NULL;
2069 }
2070 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002071 // Check if stream is being prepared
2072 if (mInputStream->isPreparing()) {
2073 CLOGE("Request references an input stream that's being prepared!");
2074 return NULL;
2075 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002076
2077 newRequest->mInputStream = mInputStream;
2078 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
2079 }
2080
2081 camera_metadata_entry_t streams =
2082 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
2083 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002084 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002085 return NULL;
2086 }
2087
2088 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07002089 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002090 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002091 CLOGE("Request references unknown stream %d",
2092 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002093 return NULL;
2094 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07002095 sp<Camera3OutputStreamInterface> stream =
2096 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002097
Zhijun He5d677d12016-05-29 16:52:39 -07002098 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002099 auto iter = surfaceMap.find(streams.data.i32[i]);
2100 if (iter != surfaceMap.end()) {
2101 const std::vector<size_t>& surfaces = iter->second;
2102 for (const auto& surface : surfaces) {
2103 if (stream->isConsumerConfigurationDeferred(surface)) {
2104 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2105 "due to deferred consumer", stream->getId(), surface);
2106 return NULL;
2107 }
2108 }
2109 newRequest->mOutputSurfaces[i] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002110 }
2111
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002112 // Lazy completion of stream configuration (allocation/registration)
2113 // on first use
2114 if (stream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002115 res = stream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002116 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002117 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
2118 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002119 return NULL;
2120 }
2121 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002122 // Check if stream is being prepared
2123 if (stream->isPreparing()) {
2124 CLOGE("Request references an output stream that's being prepared!");
2125 return NULL;
2126 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002127
2128 newRequest->mOutputStreams.push(stream);
2129 }
2130 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002131 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002132
2133 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002134}
2135
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002136bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2137 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2138 Size size = mSupportedOpaqueInputSizes[i];
2139 if (size.width == width && size.height == height) {
2140 return true;
2141 }
2142 }
2143
2144 return false;
2145}
2146
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002147void Camera3Device::cancelStreamsConfigurationLocked() {
2148 int res = OK;
2149 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2150 res = mInputStream->cancelConfiguration();
2151 if (res != OK) {
2152 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2153 mInputStream->getId(), strerror(-res), res);
2154 }
2155 }
2156
2157 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2158 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
2159 if (outputStream->isConfiguring()) {
2160 res = outputStream->cancelConfiguration();
2161 if (res != OK) {
2162 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2163 outputStream->getId(), strerror(-res), res);
2164 }
2165 }
2166 }
2167
2168 // Return state to that at start of call, so that future configures
2169 // properly clean things up
2170 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2171 mNeedConfig = true;
2172}
2173
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002174status_t Camera3Device::configureStreamsLocked(int operatingMode) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002175 ATRACE_CALL();
2176 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002177
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002178 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002179 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002180 return INVALID_OPERATION;
2181 }
2182
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002183 if (operatingMode < 0) {
2184 CLOGE("Invalid operating mode: %d", operatingMode);
2185 return BAD_VALUE;
2186 }
2187
2188 bool isConstrainedHighSpeed =
2189 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2190 operatingMode;
2191
2192 if (mOperatingMode != operatingMode) {
2193 mNeedConfig = true;
2194 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2195 mOperatingMode = operatingMode;
2196 }
2197
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002198 if (!mNeedConfig) {
2199 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2200 return OK;
2201 }
2202
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002203 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2204 // adding a dummy stream instead.
2205 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2206 if (mOutputStreams.size() == 0) {
2207 addDummyStreamLocked();
2208 } else {
2209 tryRemoveDummyStreamLocked();
2210 }
2211
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002212 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002213 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002214
2215 camera3_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002216 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002217 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2218
2219 Vector<camera3_stream_t*> streams;
2220 streams.setCapacity(config.num_streams);
2221
2222 if (mInputStream != NULL) {
2223 camera3_stream_t *inputStream;
2224 inputStream = mInputStream->startConfiguration();
2225 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002226 CLOGE("Can't start input stream configuration");
2227 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002228 return INVALID_OPERATION;
2229 }
2230 streams.add(inputStream);
2231 }
2232
2233 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002234
2235 // Don't configure bidi streams twice, nor add them twice to the list
2236 if (mOutputStreams[i].get() ==
2237 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2238
2239 config.num_streams--;
2240 continue;
2241 }
2242
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002243 camera3_stream_t *outputStream;
2244 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
2245 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002246 CLOGE("Can't start output stream configuration");
2247 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002248 return INVALID_OPERATION;
2249 }
2250 streams.add(outputStream);
2251 }
2252
2253 config.streams = streams.editArray();
2254
2255 // Do the HAL configuration; will potentially touch stream
2256 // max_buffers, usage, priv fields.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002257
2258 res = mInterface->configureStreams(&config);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002259
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002260 if (res == BAD_VALUE) {
2261 // HAL rejected this set of streams as unsupported, clean up config
2262 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002263 CLOGE("Set of requested inputs/outputs not supported by HAL");
2264 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002265 return BAD_VALUE;
2266 } else if (res != OK) {
2267 // Some other kind of error from configure_streams - this is not
2268 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002269 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2270 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002271 return res;
2272 }
2273
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002274 // Finish all stream configuration immediately.
2275 // TODO: Try to relax this later back to lazy completion, which should be
2276 // faster
2277
Igor Murashkin073f8572013-05-02 14:59:28 -07002278 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002279 res = mInputStream->finishConfiguration();
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002280 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002281 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002282 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002283 cancelStreamsConfigurationLocked();
2284 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002285 }
2286 }
2287
2288 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002289 sp<Camera3OutputStreamInterface> outputStream =
2290 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002291 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002292 res = outputStream->finishConfiguration();
Igor Murashkin073f8572013-05-02 14:59:28 -07002293 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002294 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002295 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002296 cancelStreamsConfigurationLocked();
2297 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002298 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002299 }
2300 }
2301
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002302 // Request thread needs to know to avoid using repeat-last-settings protocol
2303 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002304 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002305
Zhijun He90f7c372016-08-16 16:19:43 -07002306 char value[PROPERTY_VALUE_MAX];
2307 property_get("camera.fifo.disable", value, "0");
2308 int32_t disableFifo = atoi(value);
2309 if (disableFifo != 1) {
2310 // Boost priority of request thread to SCHED_FIFO.
2311 pid_t requestThreadTid = mRequestThread->getTid();
2312 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002313 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002314 if (res != OK) {
2315 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2316 strerror(-res), res);
2317 } else {
2318 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2319 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002320 }
2321
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002322 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002323
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002324 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002325
Ruben Brunk183f0562015-08-12 12:55:02 -07002326 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2327 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002328
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002329 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002330
Zhijun He0a210512014-07-24 13:45:15 -07002331 // tear down the deleted streams after configure streams.
2332 mDeletedStreams.clear();
2333
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002334 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002335}
2336
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002337status_t Camera3Device::addDummyStreamLocked() {
2338 ATRACE_CALL();
2339 status_t res;
2340
2341 if (mDummyStreamId != NO_STREAM) {
2342 // Should never be adding a second dummy stream when one is already
2343 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002344 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
2345 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002346 return INVALID_OPERATION;
2347 }
2348
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002349 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002350
2351 sp<Camera3OutputStreamInterface> dummyStream =
2352 new Camera3DummyStream(mNextStreamId);
2353
2354 res = mOutputStreams.add(mNextStreamId, dummyStream);
2355 if (res < 0) {
2356 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2357 return res;
2358 }
2359
2360 mDummyStreamId = mNextStreamId;
2361 mNextStreamId++;
2362
2363 return OK;
2364}
2365
2366status_t Camera3Device::tryRemoveDummyStreamLocked() {
2367 ATRACE_CALL();
2368 status_t res;
2369
2370 if (mDummyStreamId == NO_STREAM) return OK;
2371 if (mOutputStreams.size() == 1) return OK;
2372
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002373 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002374
2375 // Ok, have a dummy stream and there's at least one other output stream,
2376 // so remove the dummy
2377
2378 sp<Camera3StreamInterface> deletedStream;
2379 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2380 if (outputStreamIdx == NAME_NOT_FOUND) {
2381 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2382 return INVALID_OPERATION;
2383 }
2384
2385 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2386 mOutputStreams.removeItemsAt(outputStreamIdx);
2387
2388 // Free up the stream endpoint so that it can be used by some other stream
2389 res = deletedStream->disconnect();
2390 if (res != OK) {
2391 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2392 // fall through since we want to still list the stream as deleted.
2393 }
2394 mDeletedStreams.add(deletedStream);
2395 mDummyStreamId = NO_STREAM;
2396
2397 return res;
2398}
2399
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002400void Camera3Device::setErrorState(const char *fmt, ...) {
2401 Mutex::Autolock l(mLock);
2402 va_list args;
2403 va_start(args, fmt);
2404
2405 setErrorStateLockedV(fmt, args);
2406
2407 va_end(args);
2408}
2409
2410void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2411 Mutex::Autolock l(mLock);
2412 setErrorStateLockedV(fmt, args);
2413}
2414
2415void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2416 va_list args;
2417 va_start(args, fmt);
2418
2419 setErrorStateLockedV(fmt, args);
2420
2421 va_end(args);
2422}
2423
2424void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002425 // Print out all error messages to log
2426 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002427 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002428
2429 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002430 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002431
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002432 mErrorCause = errorCause;
2433
2434 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002435 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002436
2437 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002438 sp<NotificationListener> listener = mListener.promote();
2439 if (listener != NULL) {
2440 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002441 CaptureResultExtras());
2442 }
2443
2444 // Save stack trace. View by dumping it later.
2445 CameraTraces::saveTrace();
2446 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002447}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002448
2449/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002450 * In-flight request management
2451 */
2452
Jianing Weicb0652e2014-03-12 18:29:36 -07002453status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002454 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002455 const AeTriggerCancelOverride_t &aeTriggerCancelOverride,
2456 bool hasAppCallback) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002457 ATRACE_CALL();
2458 Mutex::Autolock l(mInFlightLock);
2459
2460 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002461 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002462 aeTriggerCancelOverride, hasAppCallback));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002463 if (res < 0) return res;
2464
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002465 if (mInFlightMap.size() == 1) {
2466 mStatusTracker->markComponentActive(mInFlightStatusId);
2467 }
2468
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002469 return OK;
2470}
2471
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002472void Camera3Device::returnOutputBuffers(
2473 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2474 nsecs_t timestamp) {
2475 for (size_t i = 0; i < numBuffers; i++)
2476 {
2477 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2478 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2479 // Note: stream may be deallocated at this point, if this buffer was
2480 // the last reference to it.
2481 if (res != OK) {
2482 ALOGE("Can't return buffer to its stream: %s (%d)",
2483 strerror(-res), res);
2484 }
2485 }
2486}
2487
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002488void Camera3Device::removeInFlightMapEntryLocked(int idx) {
2489 mInFlightMap.removeItemsAt(idx, 1);
2490
2491 // Indicate idle inFlightMap to the status tracker
2492 if (mInFlightMap.size() == 0) {
2493 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2494 }
2495}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002496
2497void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2498
2499 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2500 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2501
2502 nsecs_t sensorTimestamp = request.sensorTimestamp;
2503 nsecs_t shutterTimestamp = request.shutterTimestamp;
2504
2505 // Check if it's okay to remove the request from InFlightMap:
2506 // In the case of a successful request:
2507 // all input and output buffers, all result metadata, shutter callback
2508 // arrived.
2509 // In the case of a unsuccessful request:
2510 // all input and output buffers arrived.
2511 if (request.numBuffersLeft == 0 &&
2512 (request.requestStatus != OK ||
2513 (request.haveResultMetadata && shutterTimestamp != 0))) {
2514 ATRACE_ASYNC_END("frame capture", frameNumber);
2515
2516 // Sanity check - if sensor timestamp matches shutter timestamp
2517 if (request.requestStatus == OK &&
2518 sensorTimestamp != shutterTimestamp) {
2519 SET_ERR("sensor timestamp (%" PRId64
2520 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2521 sensorTimestamp, frameNumber, shutterTimestamp);
2522 }
2523
2524 // for an unsuccessful request, it may have pending output buffers to
2525 // return.
2526 assert(request.requestStatus != OK ||
2527 request.pendingOutputBuffers.size() == 0);
2528 returnOutputBuffers(request.pendingOutputBuffers.array(),
2529 request.pendingOutputBuffers.size(), 0);
2530
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002531 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002532 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2533 }
2534
2535 // Sanity check - if we have too many in-flight frames, something has
2536 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002537 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002538 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002539 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2540 kInFlightWarnLimitHighSpeed) {
2541 CLOGE("In-flight list too large for high speed configuration: %zu",
2542 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002543 }
2544}
2545
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002546void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2547 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2548 if (result == nullptr) return;
2549
2550 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2551 (int32_t*)&frameNumber, 1) != OK) {
2552 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2553 return;
2554 }
2555
2556 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2557 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2558 return;
2559 }
2560
2561 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2562
2563 // Valid result, insert into queue
2564 List<CaptureResult>::iterator queuedResult =
2565 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2566 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2567 ", burstId = %" PRId32, __FUNCTION__,
2568 queuedResult->mResultExtras.requestId,
2569 queuedResult->mResultExtras.frameNumber,
2570 queuedResult->mResultExtras.burstId);
2571
2572 mResultSignal.signal();
2573}
2574
2575
2576void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2577 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2578 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2579 Mutex::Autolock l(mOutputLock);
2580
2581 CaptureResult captureResult;
2582 captureResult.mResultExtras = resultExtras;
2583 captureResult.mMetadata = partialResult;
2584
2585 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2586}
2587
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002588
2589void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2590 CaptureResultExtras &resultExtras,
2591 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002592 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002593 bool reprocess,
2594 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002595 if (pendingMetadata.isEmpty())
2596 return;
2597
2598 Mutex::Autolock l(mOutputLock);
2599
2600 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002601 if (reprocess) {
2602 if (frameNumber < mNextReprocessResultFrameNumber) {
2603 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002604 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002605 frameNumber, mNextReprocessResultFrameNumber);
2606 return;
2607 }
2608 mNextReprocessResultFrameNumber = frameNumber + 1;
2609 } else {
2610 if (frameNumber < mNextResultFrameNumber) {
2611 SET_ERR("Out-of-order capture result metadata submitted! "
2612 "(got frame number %d, expecting %d)",
2613 frameNumber, mNextResultFrameNumber);
2614 return;
2615 }
2616 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002617 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002618
2619 CaptureResult captureResult;
2620 captureResult.mResultExtras = resultExtras;
2621 captureResult.mMetadata = pendingMetadata;
2622
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002623 // Append any previous partials to form a complete result
2624 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2625 captureResult.mMetadata.append(collectedPartialResult);
2626 }
2627
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07002628 // Derive some new keys for backward compaibility
2629 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
2630 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
2631 int32_t defaultBoost[1] = {100};
2632 captureResult.mMetadata.update(
2633 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
2634 defaultBoost, 1);
2635 }
2636
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002637 captureResult.mMetadata.sort();
2638
2639 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002640 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2641 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002642 SET_ERR("No timestamp provided by HAL for frame %d!",
2643 frameNumber);
2644 return;
2645 }
2646
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002647 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2648 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2649
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002650 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002651}
2652
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002653/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002654 * Camera HAL device callback methods
2655 */
2656
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002657void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002658 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002659
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002660 status_t res;
2661
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002662 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002663 if (result->result == NULL && result->num_output_buffers == 0 &&
2664 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002665 SET_ERR("No result data provided by HAL for frame %d",
2666 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002667 return;
2668 }
Zhijun He204e3292014-07-14 17:09:23 -07002669
2670 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2671 // partial_result to 1 when metadata is included in this result.
2672 if (!mUsePartialResult &&
2673 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2674 result->result != NULL &&
2675 result->partial_result != 1) {
2676 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2677 " if partial result is not supported",
2678 frameNumber, result->partial_result);
2679 return;
2680 }
2681
2682 bool isPartialResult = false;
2683 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002684 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002685 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002686
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002687 // Get shutter timestamp and resultExtras from list of in-flight requests,
2688 // where it was added by the shutter notification for this frame. If the
2689 // shutter timestamp isn't received yet, append the output buffers to the
2690 // in-flight request and they will be returned when the shutter timestamp
2691 // arrives. Update the in-flight status and remove the in-flight entry if
2692 // all result data and shutter timestamp have been received.
2693 nsecs_t shutterTimestamp = 0;
2694
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002695 {
2696 Mutex::Autolock l(mInFlightLock);
2697 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2698 if (idx == NAME_NOT_FOUND) {
2699 SET_ERR("Unknown frame number for capture result: %d",
2700 frameNumber);
2701 return;
2702 }
2703 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002704 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2705 ", frameNumber = %" PRId64 ", burstId = %" PRId32
Shuzhen Wang4a472662017-02-26 23:29:04 -08002706 ", partialResultCount = %d, hasCallback = %d",
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002707 __FUNCTION__, request.resultExtras.requestId,
2708 request.resultExtras.frameNumber, request.resultExtras.burstId,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002709 result->partial_result, request.hasCallback);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002710 // Always update the partial count to the latest one if it's not 0
2711 // (buffers only). When framework aggregates adjacent partial results
2712 // into one, the latest partial count will be used.
2713 if (result->partial_result != 0)
2714 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002715
2716 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002717 if (mUsePartialResult && result->result != NULL) {
2718 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2719 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2720 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2721 " the range of [1, %d] when metadata is included in the result",
2722 frameNumber, result->partial_result, mNumPartialResults);
2723 return;
2724 }
2725 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002726 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002727 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002728 }
Zhijun He204e3292014-07-14 17:09:23 -07002729 } else {
2730 camera_metadata_ro_entry_t partialResultEntry;
2731 res = find_camera_metadata_ro_entry(result->result,
2732 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2733 if (res != NAME_NOT_FOUND &&
2734 partialResultEntry.count > 0 &&
2735 partialResultEntry.data.u8[0] ==
2736 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2737 // A partial result. Flag this as such, and collect this
2738 // set of metadata into the in-flight entry.
2739 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002740 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002741 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002742 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002743 ANDROID_QUIRKS_PARTIAL_RESULT);
2744 }
2745 }
2746
Shuzhen Wang4a472662017-02-26 23:29:04 -08002747 if (isPartialResult && request.hasCallback) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002748 // Send partial capture result
2749 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2750 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002751 }
2752 }
2753
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002754 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002755 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002756
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002757 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002758 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002759 if (request.haveResultMetadata) {
2760 SET_ERR("Called multiple times with metadata for frame %d",
2761 frameNumber);
2762 return;
2763 }
Zhijun He204e3292014-07-14 17:09:23 -07002764 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002765 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002766 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002767 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002768 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002769 request.haveResultMetadata = true;
2770 }
2771
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002772 uint32_t numBuffersReturned = result->num_output_buffers;
2773 if (result->input_buffer != NULL) {
2774 if (hasInputBufferInRequest) {
2775 numBuffersReturned += 1;
2776 } else {
2777 ALOGW("%s: Input buffer should be NULL if there is no input"
2778 " buffer sent in the request",
2779 __FUNCTION__);
2780 }
2781 }
2782 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002783 if (request.numBuffersLeft < 0) {
2784 SET_ERR("Too many buffers returned for frame %d",
2785 frameNumber);
2786 return;
2787 }
2788
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002789 camera_metadata_ro_entry_t entry;
2790 res = find_camera_metadata_ro_entry(result->result,
2791 ANDROID_SENSOR_TIMESTAMP, &entry);
2792 if (res == OK && entry.count == 1) {
2793 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002794 }
2795
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002796 // If shutter event isn't received yet, append the output buffers to
2797 // the in-flight request. Otherwise, return the output buffers to
2798 // streams.
2799 if (shutterTimestamp == 0) {
2800 request.pendingOutputBuffers.appendArray(result->output_buffers,
2801 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002802 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002803 returnOutputBuffers(result->output_buffers,
2804 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002805 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002806
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002807 if (result->result != NULL && !isPartialResult) {
2808 if (shutterTimestamp == 0) {
2809 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002810 request.collectedPartialResult = collectedPartialResult;
Shuzhen Wang4a472662017-02-26 23:29:04 -08002811 } else if (request.hasCallback) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002812 CameraMetadata metadata;
2813 metadata = result->result;
2814 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002815 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2816 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002817 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002818 }
2819
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002820 removeInFlightRequestIfReadyLocked(idx);
2821 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002822
Zhijun Hef0d962a2014-06-30 10:24:11 -07002823 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002824 if (hasInputBufferInRequest) {
2825 Camera3Stream *stream =
2826 Camera3Stream::cast(result->input_buffer->stream);
2827 res = stream->returnInputBuffer(*(result->input_buffer));
2828 // Note: stream may be deallocated at this point, if this buffer was the
2829 // last reference to it.
2830 if (res != OK) {
2831 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2832 " its stream:%s (%d)", __FUNCTION__,
2833 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002834 }
2835 } else {
2836 ALOGW("%s: Input buffer should be NULL if there is no input"
2837 " buffer sent in the request, skipping input buffer return.",
2838 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002839 }
2840 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002841}
2842
2843void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002844 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002845 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002846 {
2847 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002848 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002849 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002850
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002851 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002852 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002853 return;
2854 }
2855
2856 switch (msg->type) {
2857 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002858 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002859 break;
2860 }
2861 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002862 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002863 break;
2864 }
2865 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002866 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002867 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002868 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002869}
2870
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002871void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002872 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002873
2874 // Map camera HAL error codes to ICameraDeviceCallback error codes
2875 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002876 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002877 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002878 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002879 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002880 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002881 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002882 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002883 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002884 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002885 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002886 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002887 };
2888
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002889 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002890 ((msg.error_code >= 0) &&
2891 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2892 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002893 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002894
2895 int streamId = 0;
2896 if (msg.error_stream != NULL) {
2897 Camera3Stream *stream =
2898 Camera3Stream::cast(msg.error_stream);
2899 streamId = stream->getId();
2900 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002901 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
2902 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002903 streamId, msg.error_code);
2904
2905 CaptureResultExtras resultExtras;
2906 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002907 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002908 // SET_ERR calls notifyError
2909 SET_ERR("Camera HAL reported serious device error");
2910 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002911 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2912 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2913 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002914 {
2915 Mutex::Autolock l(mInFlightLock);
2916 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2917 if (idx >= 0) {
2918 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2919 r.requestStatus = msg.error_code;
2920 resultExtras = r.resultExtras;
2921 } else {
2922 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002923 ALOGE("Camera %s: %s: cannot find in-flight request on "
2924 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002925 resultExtras.frameNumber);
2926 }
2927 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002928 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002929 if (listener != NULL) {
2930 listener->notifyError(errorCode, resultExtras);
2931 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002932 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002933 }
2934 break;
2935 default:
2936 // SET_ERR calls notifyError
2937 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2938 break;
2939 }
2940}
2941
2942void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002943 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002944 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002945
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002946 // Set timestamp for the request in the in-flight tracking
2947 // and get the request ID to send upstream
2948 {
2949 Mutex::Autolock l(mInFlightLock);
2950 idx = mInFlightMap.indexOfKey(msg.frame_number);
2951 if (idx >= 0) {
2952 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002953
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002954 // Verify ordering of shutter notifications
2955 {
2956 Mutex::Autolock l(mOutputLock);
2957 // TODO: need to track errors for tighter bounds on expected frame number.
2958 if (r.hasInputBuffer) {
2959 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2960 SET_ERR("Shutter notification out-of-order. Expected "
2961 "notification for frame %d, got frame %d",
2962 mNextReprocessShutterFrameNumber, msg.frame_number);
2963 return;
2964 }
2965 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2966 } else {
2967 if (msg.frame_number < mNextShutterFrameNumber) {
2968 SET_ERR("Shutter notification out-of-order. Expected "
2969 "notification for frame %d, got frame %d",
2970 mNextShutterFrameNumber, msg.frame_number);
2971 return;
2972 }
2973 mNextShutterFrameNumber = msg.frame_number + 1;
2974 }
2975 }
2976
Shuzhen Wang4a472662017-02-26 23:29:04 -08002977 r.shutterTimestamp = msg.timestamp;
2978 if (r.hasCallback) {
2979 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002980 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002981 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
Shuzhen Wang4a472662017-02-26 23:29:04 -08002982 // Call listener, if any
2983 if (listener != NULL) {
2984 listener->notifyShutter(r.resultExtras, msg.timestamp);
2985 }
2986 // send pending result and buffers
2987 sendCaptureResult(r.pendingMetadata, r.resultExtras,
2988 r.collectedPartialResult, msg.frame_number,
2989 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002990 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002991 returnOutputBuffers(r.pendingOutputBuffers.array(),
2992 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2993 r.pendingOutputBuffers.clear();
2994
2995 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002996 }
2997 }
2998 if (idx < 0) {
2999 SET_ERR("Shutter notification for non-existent frame number %d",
3000 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003001 }
3002}
3003
3004
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003005CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07003006 ALOGV("%s", __FUNCTION__);
3007
Igor Murashkin1e479c02013-09-06 16:55:14 -07003008 CameraMetadata retVal;
3009
3010 if (mRequestThread != NULL) {
3011 retVal = mRequestThread->getLatestRequest();
3012 }
3013
Igor Murashkin1e479c02013-09-06 16:55:14 -07003014 return retVal;
3015}
3016
Jianing Weicb0652e2014-03-12 18:29:36 -07003017
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003018void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
3019 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
3020 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
3021}
3022
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003023/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003024 * HalInterface inner class methods
3025 */
3026
3027Camera3Device::HalInterface::HalInterface(camera3_device_t *device) :
3028 mHal3Device(device) {}
3029
3030Camera3Device::HalInterface::HalInterface(sp<ICameraDeviceSession> &session) :
3031 mHal3Device(nullptr),
3032 mHidlSession(session) {}
3033
3034Camera3Device::HalInterface::HalInterface() :
3035 mHal3Device(nullptr) {}
3036
3037Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
3038 mHal3Device(other.mHal3Device), mHidlSession(other.mHidlSession) {}
3039
3040bool Camera3Device::HalInterface::valid() {
3041 return (mHal3Device != nullptr) || (mHidlSession != nullptr);
3042}
3043
3044void Camera3Device::HalInterface::clear() {
3045 mHal3Device = nullptr;
3046 mHidlSession.clear();
3047}
3048
3049status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
3050 camera3_request_template_t templateId,
3051 /*out*/ camera_metadata_t **requestTemplate) {
3052 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3053 if (!valid()) return INVALID_OPERATION;
3054 status_t res = OK;
3055
3056 if (mHal3Device != nullptr) {
3057 const camera_metadata *r;
3058 r = mHal3Device->ops->construct_default_request_settings(
3059 mHal3Device, templateId);
3060 if (r == nullptr) return BAD_VALUE;
3061 *requestTemplate = clone_camera_metadata(r);
3062 if (requestTemplate == nullptr) {
3063 ALOGE("%s: Unable to clone camera metadata received from HAL",
3064 __FUNCTION__);
3065 return INVALID_OPERATION;
3066 }
3067 } else {
3068 common::V1_0::Status status;
3069 RequestTemplate id;
3070 switch (templateId) {
3071 case CAMERA3_TEMPLATE_PREVIEW:
3072 id = RequestTemplate::PREVIEW;
3073 break;
3074 case CAMERA3_TEMPLATE_STILL_CAPTURE:
3075 id = RequestTemplate::STILL_CAPTURE;
3076 break;
3077 case CAMERA3_TEMPLATE_VIDEO_RECORD:
3078 id = RequestTemplate::VIDEO_RECORD;
3079 break;
3080 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
3081 id = RequestTemplate::VIDEO_SNAPSHOT;
3082 break;
3083 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
3084 id = RequestTemplate::ZERO_SHUTTER_LAG;
3085 break;
3086 case CAMERA3_TEMPLATE_MANUAL:
3087 id = RequestTemplate::MANUAL;
3088 break;
3089 default:
3090 // Unknown template ID
3091 return BAD_VALUE;
3092 }
3093 mHidlSession->constructDefaultRequestSettings(id,
3094 [&status, &requestTemplate]
3095 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
3096 status = s;
3097 if (status == common::V1_0::Status::OK) {
3098 const camera_metadata *r =
3099 reinterpret_cast<const camera_metadata_t*>(request.data());
3100 size_t expectedSize = request.size();
3101 int ret = validate_camera_metadata_structure(r, &expectedSize);
3102 if (ret == OK) {
3103 *requestTemplate = clone_camera_metadata(r);
3104 if (*requestTemplate == nullptr) {
3105 ALOGE("%s: Unable to clone camera metadata received from HAL",
3106 __FUNCTION__);
3107 status = common::V1_0::Status::INTERNAL_ERROR;
3108 }
3109 } else {
3110 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3111 status = common::V1_0::Status::INTERNAL_ERROR;
3112 }
3113 }
3114 });
3115 res = CameraProviderManager::mapToStatusT(status);
3116 }
3117 return res;
3118}
3119
3120status_t Camera3Device::HalInterface::configureStreams(camera3_stream_configuration *config) {
3121 ATRACE_NAME("CameraHal::configureStreams");
3122 if (!valid()) return INVALID_OPERATION;
3123 status_t res = OK;
3124
3125 if (mHal3Device != nullptr) {
3126 res = mHal3Device->ops->configure_streams(mHal3Device, config);
3127 } else {
3128 // Convert stream config to HIDL
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003129 std::set<int> activeStreams;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003130 StreamConfiguration requestedConfiguration;
3131 requestedConfiguration.streams.resize(config->num_streams);
3132 for (size_t i = 0; i < config->num_streams; i++) {
3133 Stream &dst = requestedConfiguration.streams[i];
3134 camera3_stream_t *src = config->streams[i];
3135
3136 int streamId = Camera3Stream::cast(src)->getId();
3137 StreamType streamType;
3138 switch (src->stream_type) {
3139 case CAMERA3_STREAM_OUTPUT:
3140 streamType = StreamType::OUTPUT;
3141 break;
3142 case CAMERA3_STREAM_INPUT:
3143 streamType = StreamType::INPUT;
3144 break;
3145 default:
3146 ALOGE("%s: Stream %d: Unsupported stream type %d",
3147 __FUNCTION__, streamId, config->streams[i]->stream_type);
3148 return BAD_VALUE;
3149 }
3150 dst.id = streamId;
3151 dst.streamType = streamType;
3152 dst.width = src->width;
3153 dst.height = src->height;
3154 dst.format = mapToPixelFormat(src->format);
3155 dst.usage = mapToConsumerUsage(src->usage);
3156 dst.dataSpace = mapToHidlDataspace(src->data_space);
3157 dst.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003158
3159 activeStreams.insert(streamId);
3160 // Create Buffer ID map if necessary
3161 if (mBufferIdMaps.count(streamId) == 0) {
3162 mBufferIdMaps.emplace(streamId, BufferIdMap{});
3163 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003164 }
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003165 // remove BufferIdMap for deleted streams
3166 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
3167 int streamId = it->first;
3168 bool active = activeStreams.count(streamId) > 0;
3169 if (!active) {
3170 it = mBufferIdMaps.erase(it);
3171 } else {
3172 ++it;
3173 }
3174 }
3175
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08003176 res = mapToStreamConfigurationMode(
3177 (camera3_stream_configuration_mode_t) config->operation_mode,
3178 /*out*/ &requestedConfiguration.operationMode);
3179 if (res != OK) {
3180 return res;
3181 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003182
3183 // Invoke configureStreams
3184
3185 HalStreamConfiguration finalConfiguration;
3186 common::V1_0::Status status;
3187 mHidlSession->configureStreams(requestedConfiguration,
3188 [&status, &finalConfiguration]
3189 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3190 finalConfiguration = halConfiguration;
3191 status = s;
3192 });
3193 if (status != common::V1_0::Status::OK ) {
3194 return CameraProviderManager::mapToStatusT(status);
3195 }
3196
3197 // And convert output stream configuration from HIDL
3198
3199 for (size_t i = 0; i < config->num_streams; i++) {
3200 camera3_stream_t *dst = config->streams[i];
3201 int streamId = Camera3Stream::cast(dst)->getId();
3202
3203 // Start scan at i, with the assumption that the stream order matches
3204 size_t realIdx = i;
3205 bool found = false;
3206 for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
3207 if (finalConfiguration.streams[realIdx].id == streamId) {
3208 found = true;
3209 break;
3210 }
3211 realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
3212 }
3213 if (!found) {
3214 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3215 __FUNCTION__, streamId);
3216 return INVALID_OPERATION;
3217 }
3218 HalStream &src = finalConfiguration.streams[realIdx];
3219
3220 int overrideFormat = mapToFrameworkFormat(src.overrideFormat);
3221 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
3222 if (dst->format != overrideFormat) {
3223 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3224 streamId, dst->format);
3225 }
3226 } else {
3227 // Override allowed with IMPLEMENTATION_DEFINED
3228 dst->format = overrideFormat;
3229 }
3230
3231 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
3232 if (src.producerUsage != 0) {
3233 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
3234 __FUNCTION__, streamId);
3235 return INVALID_OPERATION;
3236 }
3237 dst->usage = mapConsumerToFrameworkUsage(src.consumerUsage);
3238 } else {
3239 // OUTPUT
3240 if (src.consumerUsage != 0) {
3241 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3242 __FUNCTION__, streamId);
3243 return INVALID_OPERATION;
3244 }
3245 dst->usage = mapProducerToFrameworkUsage(src.producerUsage);
3246 }
3247 dst->max_buffers = src.maxBuffers;
3248 }
3249 }
3250 return res;
3251}
3252
3253status_t Camera3Device::HalInterface::processCaptureRequest(
3254 camera3_capture_request_t *request) {
3255 ATRACE_NAME("CameraHal::processCaptureRequest");
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003256 if (!valid()) return INVALID_OPERATION;
3257 status_t res = OK;
3258
3259 if (mHal3Device != nullptr) {
3260 res = mHal3Device->ops->process_capture_request(mHal3Device, request);
3261 } else {
3262 device::V3_2::CaptureRequest captureRequest;
3263 captureRequest.frameNumber = request->frame_number;
3264 std::vector<native_handle_t*> handlesCreated;
3265 // A null request settings maps to a size-0 CameraMetadata
3266 if (request->settings != nullptr) {
3267 captureRequest.settings.setToExternal(
3268 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3269 get_camera_metadata_size(request->settings));
3270 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08003271
3272 {
3273 std::lock_guard<std::mutex> lock(mInflightLock);
3274 if (request->input_buffer != nullptr) {
3275 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003276 buffer_handle_t buf = *(request->input_buffer->buffer);
3277 auto pair = getBufferId(buf, streamId);
3278 bool isNewBuffer = pair.first;
3279 uint64_t bufferId = pair.second;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08003280 captureRequest.inputBuffer.streamId = streamId;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003281 captureRequest.inputBuffer.bufferId = bufferId;
3282 captureRequest.inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08003283 captureRequest.inputBuffer.status = BufferStatus::OK;
3284 native_handle_t *acquireFence = nullptr;
3285 if (request->input_buffer->acquire_fence != -1) {
3286 acquireFence = native_handle_create(1,0);
3287 acquireFence->data[0] = request->input_buffer->acquire_fence;
3288 handlesCreated.push_back(acquireFence);
3289 }
3290 captureRequest.inputBuffer.acquireFence = acquireFence;
3291 captureRequest.inputBuffer.releaseFence = nullptr;
3292
3293 pushInflightBufferLocked(captureRequest.frameNumber, streamId,
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003294 request->input_buffer->buffer,
3295 request->input_buffer->acquire_fence);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003296 } else {
3297 captureRequest.inputBuffer.streamId = -1;
3298 captureRequest.inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003299 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003300
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08003301 captureRequest.outputBuffers.resize(request->num_output_buffers);
3302 for (size_t i = 0; i < request->num_output_buffers; i++) {
3303 const camera3_stream_buffer_t *src = request->output_buffers + i;
3304 StreamBuffer &dst = captureRequest.outputBuffers[i];
3305 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003306 buffer_handle_t buf = *(src->buffer);
3307 auto pair = getBufferId(buf, streamId);
3308 bool isNewBuffer = pair.first;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08003309 dst.streamId = streamId;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003310 dst.bufferId = pair.second;
3311 dst.buffer = isNewBuffer ? buf : nullptr;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08003312 dst.status = BufferStatus::OK;
3313 native_handle_t *acquireFence = nullptr;
3314 if (src->acquire_fence != -1) {
3315 acquireFence = native_handle_create(1,0);
3316 acquireFence->data[0] = src->acquire_fence;
3317 handlesCreated.push_back(acquireFence);
3318 }
3319 dst.acquireFence = acquireFence;
3320 dst.releaseFence = nullptr;
3321
3322 pushInflightBufferLocked(captureRequest.frameNumber, streamId,
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003323 src->buffer, src->acquire_fence);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003324 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003325 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003326 common::V1_0::Status status = mHidlSession->processCaptureRequest(captureRequest);
3327
3328 for (auto& handle : handlesCreated) {
3329 native_handle_delete(handle);
3330 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003331 res = CameraProviderManager::mapToStatusT(status);
3332 }
3333 return res;
3334}
3335
3336status_t Camera3Device::HalInterface::flush() {
3337 ATRACE_NAME("CameraHal::flush");
3338 if (!valid()) return INVALID_OPERATION;
3339 status_t res = OK;
3340
3341 if (mHal3Device != nullptr) {
3342 res = mHal3Device->ops->flush(mHal3Device);
3343 } else {
3344 res = CameraProviderManager::mapToStatusT(mHidlSession->flush());
3345 }
3346 return res;
3347}
3348
3349status_t Camera3Device::HalInterface::dump(int fd) {
3350 ATRACE_NAME("CameraHal::dump");
3351 if (!valid()) return INVALID_OPERATION;
3352 status_t res = OK;
3353
3354 if (mHal3Device != nullptr) {
3355 mHal3Device->ops->dump(mHal3Device, fd);
3356 } else {
3357 // Handled by CameraProviderManager::dump
3358 }
3359 return res;
3360}
3361
3362status_t Camera3Device::HalInterface::close() {
3363 ATRACE_NAME("CameraHal::close()");
3364 if (!valid()) return INVALID_OPERATION;
3365 status_t res = OK;
3366
3367 if (mHal3Device != nullptr) {
3368 mHal3Device->common.close(&mHal3Device->common);
3369 } else {
3370 mHidlSession->close();
3371 }
3372 return res;
3373}
3374
3375status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003376 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003377 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003378 auto pair = std::make_pair(buffer, acquireFence);
3379 mInflightBufferMap[key] = pair;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003380 return OK;
3381}
3382
3383status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003384 int32_t frameNumber, int32_t streamId,
3385 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003386 std::lock_guard<std::mutex> lock(mInflightLock);
3387
3388 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
3389 auto it = mInflightBufferMap.find(key);
3390 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003391 auto pair = it->second;
3392 *buffer = pair.first;
3393 int acquireFence = pair.second;
3394 if (acquireFence > 0) {
3395 ::close(acquireFence);
3396 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003397 mInflightBufferMap.erase(it);
3398 return OK;
3399}
3400
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003401std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
3402 const buffer_handle_t& buf, int streamId) {
3403 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3404
3405 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
3406 auto it = bIdMap.find(buf);
3407 if (it == bIdMap.end()) {
3408 bIdMap[buf] = mNextBufferId++;
3409 return std::make_pair(true, mNextBufferId - 1);
3410 } else {
3411 return std::make_pair(false, it->second);
3412 }
3413}
3414
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003415/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003416 * RequestThread inner class methods
3417 */
3418
3419Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003420 sp<StatusTracker> statusTracker,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003421 HalInterface* interface,
3422 uint32_t deviceVersion,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07003423 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003424 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003425 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003426 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003427 mInterface(interface),
3428 mDeviceVersion(deviceVersion),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003429 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003430 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003431 mReconfigured(false),
3432 mDoPause(false),
3433 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003434 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07003435 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003436 mCurrentAfTriggerId(0),
3437 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003438 mRepeatingLastFrameNumber(
3439 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003440 mAeLockAvailable(aeLockAvailable),
3441 mPrepareVideoStream(false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003442 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003443}
3444
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003445Camera3Device::RequestThread::~RequestThread() {}
3446
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003447void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003448 wp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003449 Mutex::Autolock l(mRequestLock);
3450 mListener = listener;
3451}
3452
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003453void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003454 Mutex::Autolock l(mRequestLock);
3455 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003456 // Prepare video stream for high speed recording.
3457 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003458}
3459
Jianing Wei90e59c92014-03-12 18:29:36 -07003460status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003461 List<sp<CaptureRequest> > &requests,
3462 /*out*/
3463 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07003464 Mutex::Autolock l(mRequestLock);
3465 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3466 ++it) {
3467 mRequestQueue.push_back(*it);
3468 }
3469
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003470 if (lastFrameNumber != NULL) {
3471 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3472 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3473 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3474 *lastFrameNumber);
3475 }
Jianing Weicb0652e2014-03-12 18:29:36 -07003476
Jianing Wei90e59c92014-03-12 18:29:36 -07003477 unpauseForNewRequests();
3478
3479 return OK;
3480}
3481
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003482
3483status_t Camera3Device::RequestThread::queueTrigger(
3484 RequestTrigger trigger[],
3485 size_t count) {
3486
3487 Mutex::Autolock l(mTriggerMutex);
3488 status_t ret;
3489
3490 for (size_t i = 0; i < count; ++i) {
3491 ret = queueTriggerLocked(trigger[i]);
3492
3493 if (ret != OK) {
3494 return ret;
3495 }
3496 }
3497
3498 return OK;
3499}
3500
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003501const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3502 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003503 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003504 if (d != nullptr) return d->mId;
3505 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003506}
3507
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003508status_t Camera3Device::RequestThread::queueTriggerLocked(
3509 RequestTrigger trigger) {
3510
3511 uint32_t tag = trigger.metadataTag;
3512 ssize_t index = mTriggerMap.indexOfKey(tag);
3513
3514 switch (trigger.getTagType()) {
3515 case TYPE_BYTE:
3516 // fall-through
3517 case TYPE_INT32:
3518 break;
3519 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003520 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3521 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003522 return INVALID_OPERATION;
3523 }
3524
3525 /**
3526 * Collect only the latest trigger, since we only have 1 field
3527 * in the request settings per trigger tag, and can't send more than 1
3528 * trigger per request.
3529 */
3530 if (index != NAME_NOT_FOUND) {
3531 mTriggerMap.editValueAt(index) = trigger;
3532 } else {
3533 mTriggerMap.add(tag, trigger);
3534 }
3535
3536 return OK;
3537}
3538
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003539status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003540 const RequestList &requests,
3541 /*out*/
3542 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003543 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003544 if (lastFrameNumber != NULL) {
3545 *lastFrameNumber = mRepeatingLastFrameNumber;
3546 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003547 mRepeatingRequests.clear();
3548 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3549 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003550
3551 unpauseForNewRequests();
3552
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003553 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003554 return OK;
3555}
3556
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003557bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003558 if (mRepeatingRequests.empty()) {
3559 return false;
3560 }
3561 int32_t requestId = requestIn->mResultExtras.requestId;
3562 const RequestList &repeatRequests = mRepeatingRequests;
3563 // All repeating requests are guaranteed to have same id so only check first quest
3564 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3565 return (firstRequest->mResultExtras.requestId == requestId);
3566}
3567
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003568status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003569 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003570 return clearRepeatingRequestsLocked(lastFrameNumber);
3571
3572}
3573
3574status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003575 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003576 if (lastFrameNumber != NULL) {
3577 *lastFrameNumber = mRepeatingLastFrameNumber;
3578 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003579 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003580 return OK;
3581}
3582
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003583status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003584 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003585 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003586 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003587
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003588 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003589
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003590 // Send errors for all requests pending in the request queue, including
3591 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003592 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003593 if (listener != NULL) {
3594 for (RequestList::iterator it = mRequestQueue.begin();
3595 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003596 // Abort the input buffers for reprocess requests.
3597 if ((*it)->mInputStream != NULL) {
3598 camera3_stream_buffer_t inputBuffer;
3599 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
3600 if (res != OK) {
3601 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3602 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3603 } else {
3604 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3605 if (res != OK) {
3606 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3607 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3608 }
3609 }
3610 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003611 // Set the frame number this request would have had, if it
3612 // had been submitted; this frame number will not be reused.
3613 // The requestId and burstId fields were set when the request was
3614 // submitted originally (in convertMetadataListToRequestListLocked)
3615 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003616 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003617 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003618 }
3619 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003620 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003621
3622 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003623 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003624 if (lastFrameNumber != NULL) {
3625 *lastFrameNumber = mRepeatingLastFrameNumber;
3626 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003627 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003628 return OK;
3629}
3630
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003631status_t Camera3Device::RequestThread::flush() {
3632 ATRACE_CALL();
3633 Mutex::Autolock l(mFlushLock);
3634
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003635 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_1) {
3636 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003637 }
3638
3639 return -ENOTSUP;
3640}
3641
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003642void Camera3Device::RequestThread::setPaused(bool paused) {
3643 Mutex::Autolock l(mPauseLock);
3644 mDoPause = paused;
3645 mDoPauseSignal.signal();
3646}
3647
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003648status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3649 int32_t requestId, nsecs_t timeout) {
3650 Mutex::Autolock l(mLatestRequestMutex);
3651 status_t res;
3652 while (mLatestRequestId != requestId) {
3653 nsecs_t startTime = systemTime();
3654
3655 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3656 if (res != OK) return res;
3657
3658 timeout -= (systemTime() - startTime);
3659 }
3660
3661 return OK;
3662}
3663
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003664void Camera3Device::RequestThread::requestExit() {
3665 // Call parent to set up shutdown
3666 Thread::requestExit();
3667 // The exit from any possible waits
3668 mDoPauseSignal.signal();
3669 mRequestSignal.signal();
3670}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003671
Chien-Yu Chend196d612015-06-22 19:49:01 -07003672
3673/**
3674 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
3675 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
3676 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
3677 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
3678 * request.
3679 */
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003680void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(const sp<CaptureRequest>& request) {
Chien-Yu Chend196d612015-06-22 19:49:01 -07003681 request->mAeTriggerCancelOverride.applyAeLock = false;
3682 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
3683
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003684 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Chien-Yu Chend196d612015-06-22 19:49:01 -07003685 return;
3686 }
3687
3688 camera_metadata_entry_t aePrecaptureTrigger =
3689 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3690 if (aePrecaptureTrigger.count > 0 &&
3691 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
3692 // Always override CANCEL to IDLE
3693 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
3694 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
3695 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
3696 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
3697 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3698
3699 if (mAeLockAvailable == true) {
3700 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3701 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3702 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3703 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3704 request->mAeTriggerCancelOverride.applyAeLock = true;
3705 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3706 }
3707 }
3708 }
3709}
3710
3711/**
3712 * Override result metadata for cancelling AE precapture trigger applied in
3713 * handleAePrecaptureCancelRequest().
3714 */
3715void Camera3Device::overrideResultForPrecaptureCancel(
3716 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3717 if (aeTriggerCancelOverride.applyAeLock) {
3718 // Only devices <= v3.2 should have this override
3719 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3720 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3721 }
3722
3723 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3724 // Only devices <= v3.2 should have this override
3725 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3726 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3727 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3728 }
3729}
3730
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003731void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003732 bool surfaceAbandoned = false;
3733 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003734 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003735 {
3736 Mutex::Autolock l(mRequestLock);
3737 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3738 // repeating requests.
3739 for (const auto& request : mRepeatingRequests) {
3740 for (const auto& s : request->mOutputStreams) {
3741 if (s->isAbandoned()) {
3742 surfaceAbandoned = true;
3743 clearRepeatingRequestsLocked(&lastFrameNumber);
3744 break;
3745 }
3746 }
3747 if (surfaceAbandoned) {
3748 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003749 }
3750 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003751 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003752 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003753
3754 if (listener != NULL && surfaceAbandoned) {
3755 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003756 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003757}
3758
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003759bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003760 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003761 status_t res;
3762
3763 // Handle paused state.
3764 if (waitIfPaused()) {
3765 return true;
3766 }
3767
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003768 // Wait for the next batch of requests.
3769 waitForNextRequestBatch();
3770 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003771 return true;
3772 }
3773
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003774 // Get the latest request ID, if any
3775 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003776 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003777 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003778 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003779 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003780 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003781 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3782 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003783 }
3784
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003785 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003786 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003787 if (res == TIMED_OUT) {
3788 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003789 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003790 // Check if any stream is abandoned.
3791 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003792 return true;
3793 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003794 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003795 return false;
3796 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003797
Zhijun Hecc27e112013-10-03 16:12:43 -07003798 // Inform waitUntilRequestProcessed thread of a new request ID
3799 {
3800 Mutex::Autolock al(mLatestRequestMutex);
3801
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003802 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003803 mLatestRequestSignal.signal();
3804 }
3805
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003806 // Submit a batch of requests to HAL.
3807 // Use flush lock only when submitting multilple requests in a batch.
3808 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3809 // which may take a long time to finish so synchronizing flush() and
3810 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3811 // For now, only synchronize for high speed recording and we should figure something out for
3812 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003813 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003814
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003815 if (useFlushLock) {
3816 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003817 }
3818
Zhijun Hef0645c12016-08-02 00:58:11 -07003819 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003820 mNextRequests.size());
3821 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003822 // Submit request and block until ready for next one
3823 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003824 res = mInterface->processCaptureRequest(&nextRequest.halRequest);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003825
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003826 if (res != OK) {
3827 // Should only get a failure here for malformed requests or device-level
3828 // errors, so consider all errors fatal. Bad metadata failures should
3829 // come through notify.
3830 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3831 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3832 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003833 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003834 if (useFlushLock) {
3835 mFlushLock.unlock();
3836 }
3837 return false;
3838 }
3839
3840 // Mark that the request has be submitted successfully.
3841 nextRequest.submitted = true;
3842
3843 // Update the latest request sent to HAL
3844 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3845 Mutex::Autolock al(mLatestRequestMutex);
3846
3847 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3848 mLatestRequest.acquire(cloned);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003849
3850 sp<Camera3Device> parent = mParent.promote();
3851 if (parent != NULL) {
3852 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
3853 0, mLatestRequest);
3854 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003855 }
3856
3857 if (nextRequest.halRequest.settings != NULL) {
3858 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3859 }
3860
3861 // Remove any previously queued triggers (after unlock)
3862 res = removeTriggers(mPrevRequest);
3863 if (res != OK) {
3864 SET_ERR("RequestThread: Unable to remove triggers "
3865 "(capture request %d, HAL device: %s (%d)",
3866 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003867 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003868 if (useFlushLock) {
3869 mFlushLock.unlock();
3870 }
3871 return false;
3872 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003873 }
3874
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003875 if (useFlushLock) {
3876 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003877 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003878
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003879 // Unset as current request
3880 {
3881 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003882 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003883 }
3884
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003885 return true;
3886}
3887
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003888status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003889 ATRACE_CALL();
3890
Shuzhen Wang4a472662017-02-26 23:29:04 -08003891 for (size_t i = 0; i < mNextRequests.size(); i++) {
3892 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003893 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3894 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3895 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3896
3897 // Prepare a request to HAL
3898 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3899
3900 // Insert any queued triggers (before metadata is locked)
3901 status_t res = insertTriggers(captureRequest);
3902
3903 if (res < 0) {
3904 SET_ERR("RequestThread: Unable to insert triggers "
3905 "(capture request %d, HAL device: %s (%d)",
3906 halRequest->frame_number, strerror(-res), res);
3907 return INVALID_OPERATION;
3908 }
3909 int triggerCount = res;
3910 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3911 mPrevTriggers = triggerCount;
3912
3913 // If the request is the same as last, or we had triggers last time
3914 if (mPrevRequest != captureRequest || triggersMixedIn) {
3915 /**
3916 * HAL workaround:
3917 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3918 */
3919 res = addDummyTriggerIds(captureRequest);
3920 if (res != OK) {
3921 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3922 "(capture request %d, HAL device: %s (%d)",
3923 halRequest->frame_number, strerror(-res), res);
3924 return INVALID_OPERATION;
3925 }
3926
3927 /**
3928 * The request should be presorted so accesses in HAL
3929 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3930 */
3931 captureRequest->mSettings.sort();
3932 halRequest->settings = captureRequest->mSettings.getAndLock();
3933 mPrevRequest = captureRequest;
3934 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3935
3936 IF_ALOGV() {
3937 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3938 find_camera_metadata_ro_entry(
3939 halRequest->settings,
3940 ANDROID_CONTROL_AF_TRIGGER,
3941 &e
3942 );
3943 if (e.count > 0) {
3944 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3945 __FUNCTION__,
3946 halRequest->frame_number,
3947 e.data.u8[0]);
3948 }
3949 }
3950 } else {
3951 // leave request.settings NULL to indicate 'reuse latest given'
3952 ALOGVV("%s: Request settings are REUSED",
3953 __FUNCTION__);
3954 }
3955
3956 uint32_t totalNumBuffers = 0;
3957
3958 // Fill in buffers
3959 if (captureRequest->mInputStream != NULL) {
3960 halRequest->input_buffer = &captureRequest->mInputBuffer;
3961 totalNumBuffers += 1;
3962 } else {
3963 halRequest->input_buffer = NULL;
3964 }
3965
3966 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3967 captureRequest->mOutputStreams.size());
3968 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang4a472662017-02-26 23:29:04 -08003969 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
3970 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003971
3972 // Prepare video buffers for high speed recording on the first video request.
3973 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3974 // Only try to prepare video stream on the first video request.
3975 mPrepareVideoStream = false;
3976
3977 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
3978 while (res == NOT_ENOUGH_DATA) {
3979 res = outputStream->prepareNextBuffer();
3980 }
3981 if (res != OK) {
3982 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3983 __FUNCTION__, strerror(-res), res);
3984 outputStream->cancelPrepare();
3985 }
3986 }
3987
Shuzhen Wang4a472662017-02-26 23:29:04 -08003988 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3989 captureRequest->mOutputSurfaces[j]);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003990 if (res != OK) {
3991 // Can't get output buffer from gralloc queue - this could be due to
3992 // abandoned queue or other consumer misbehavior, so not a fatal
3993 // error
3994 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3995 " %s (%d)", strerror(-res), res);
3996
3997 return TIMED_OUT;
3998 }
3999 halRequest->num_output_buffers++;
Shuzhen Wang0129d522016-10-30 22:43:41 -07004000
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004001 }
4002 totalNumBuffers += halRequest->num_output_buffers;
4003
4004 // Log request in the in-flight queue
4005 sp<Camera3Device> parent = mParent.promote();
4006 if (parent == NULL) {
4007 // Should not happen, and nowhere to send errors to, so just log it
4008 CLOGE("RequestThread: Parent is gone");
4009 return INVALID_OPERATION;
4010 }
Shuzhen Wang4a472662017-02-26 23:29:04 -08004011
4012 // If this request list is for constrained high speed recording (not
4013 // preview), and the current request is not the last one in the batch,
4014 // do not send callback to the app.
4015 bool hasCallback = true;
4016 if (mNextRequests[0].captureRequest->mBatchSize > 1 && i != mNextRequests.size()-1) {
4017 hasCallback = false;
4018 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004019 res = parent->registerInFlight(halRequest->frame_number,
4020 totalNumBuffers, captureRequest->mResultExtras,
4021 /*hasInput*/halRequest->input_buffer != NULL,
Shuzhen Wang4a472662017-02-26 23:29:04 -08004022 captureRequest->mAeTriggerCancelOverride,
4023 hasCallback);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004024 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4025 ", burstId = %" PRId32 ".",
4026 __FUNCTION__,
4027 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4028 captureRequest->mResultExtras.burstId);
4029 if (res != OK) {
4030 SET_ERR("RequestThread: Unable to register new in-flight request:"
4031 " %s (%d)", strerror(-res), res);
4032 return INVALID_OPERATION;
4033 }
4034 }
4035
4036 return OK;
4037}
4038
Igor Murashkin1e479c02013-09-06 16:55:14 -07004039CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
4040 Mutex::Autolock al(mLatestRequestMutex);
4041
4042 ALOGV("RequestThread::%s", __FUNCTION__);
4043
4044 return mLatestRequest;
4045}
4046
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004047bool Camera3Device::RequestThread::isStreamPending(
4048 sp<Camera3StreamInterface>& stream) {
4049 Mutex::Autolock l(mRequestLock);
4050
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004051 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004052 if (!nextRequest.submitted) {
4053 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4054 if (stream == s) return true;
4055 }
4056 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004057 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004058 }
4059
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004060 for (const auto& request : mRequestQueue) {
4061 for (const auto& s : request->mOutputStreams) {
4062 if (stream == s) return true;
4063 }
4064 if (stream == request->mInputStream) return true;
4065 }
4066
4067 for (const auto& request : mRepeatingRequests) {
4068 for (const auto& s : request->mOutputStreams) {
4069 if (stream == s) return true;
4070 }
4071 if (stream == request->mInputStream) return true;
4072 }
4073
4074 return false;
4075}
Jianing Weicb0652e2014-03-12 18:29:36 -07004076
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004077void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4078 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004079 return;
4080 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004081
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004082 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004083 // Skip the ones that have been submitted successfully.
4084 if (nextRequest.submitted) {
4085 continue;
4086 }
4087
4088 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4089 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4090 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4091
4092 if (halRequest->settings != NULL) {
4093 captureRequest->mSettings.unlock(halRequest->settings);
4094 }
4095
4096 if (captureRequest->mInputStream != NULL) {
4097 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
4098 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4099 }
4100
4101 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4102 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
4103 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
4104 }
4105
4106 if (sendRequestError) {
4107 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004108 sp<NotificationListener> listener = mListener.promote();
4109 if (listener != NULL) {
4110 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004111 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004112 captureRequest->mResultExtras);
4113 }
4114 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004115
4116 // Remove yet-to-be submitted inflight request from inflightMap
4117 {
4118 sp<Camera3Device> parent = mParent.promote();
4119 if (parent != NULL) {
4120 Mutex::Autolock l(parent->mInFlightLock);
4121 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4122 if (idx >= 0) {
4123 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4124 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4125 parent->removeInFlightMapEntryLocked(idx);
4126 }
4127 }
4128 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004129 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004130
4131 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004132 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004133}
4134
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004135void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004136 // Optimized a bit for the simple steady-state case (single repeating
4137 // request), to avoid putting that request in the queue temporarily.
4138 Mutex::Autolock l(mRequestLock);
4139
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004140 assert(mNextRequests.empty());
4141
4142 NextRequest nextRequest;
4143 nextRequest.captureRequest = waitForNextRequestLocked();
4144 if (nextRequest.captureRequest == nullptr) {
4145 return;
4146 }
4147
4148 nextRequest.halRequest = camera3_capture_request_t();
4149 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004150 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004151
4152 // Wait for additional requests
4153 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4154
4155 for (size_t i = 1; i < batchSize; i++) {
4156 NextRequest additionalRequest;
4157 additionalRequest.captureRequest = waitForNextRequestLocked();
4158 if (additionalRequest.captureRequest == nullptr) {
4159 break;
4160 }
4161
4162 additionalRequest.halRequest = camera3_capture_request_t();
4163 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004164 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004165 }
4166
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004167 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004168 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004169 mNextRequests.size(), batchSize);
4170 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004171 }
4172
4173 return;
4174}
4175
4176sp<Camera3Device::CaptureRequest>
4177 Camera3Device::RequestThread::waitForNextRequestLocked() {
4178 status_t res;
4179 sp<CaptureRequest> nextRequest;
4180
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004181 while (mRequestQueue.empty()) {
4182 if (!mRepeatingRequests.empty()) {
4183 // Always atomically enqueue all requests in a repeating request
4184 // list. Guarantees a complete in-sequence set of captures to
4185 // application.
4186 const RequestList &requests = mRepeatingRequests;
4187 RequestList::const_iterator firstRequest =
4188 requests.begin();
4189 nextRequest = *firstRequest;
4190 mRequestQueue.insert(mRequestQueue.end(),
4191 ++firstRequest,
4192 requests.end());
4193 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004194
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004195 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004196
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004197 break;
4198 }
4199
4200 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4201
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004202 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4203 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004204 Mutex::Autolock pl(mPauseLock);
4205 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004206 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004207 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004208 // Let the tracker know
4209 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4210 if (statusTracker != 0) {
4211 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4212 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004213 }
4214 // Stop waiting for now and let thread management happen
4215 return NULL;
4216 }
4217 }
4218
4219 if (nextRequest == NULL) {
4220 // Don't have a repeating request already in hand, so queue
4221 // must have an entry now.
4222 RequestList::iterator firstRequest =
4223 mRequestQueue.begin();
4224 nextRequest = *firstRequest;
4225 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004226 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4227 sp<NotificationListener> listener = mListener.promote();
4228 if (listener != NULL) {
4229 listener->notifyRequestQueueEmpty();
4230 }
4231 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004232 }
4233
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004234 // In case we've been unpaused by setPaused clearing mDoPause, need to
4235 // update internal pause state (capture/setRepeatingRequest unpause
4236 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004237 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004238 if (mPaused) {
4239 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4240 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4241 if (statusTracker != 0) {
4242 statusTracker->markComponentActive(mStatusId);
4243 }
4244 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004245 mPaused = false;
4246
4247 // Check if we've reconfigured since last time, and reset the preview
4248 // request if so. Can't use 'NULL request == repeat' across configure calls.
4249 if (mReconfigured) {
4250 mPrevRequest.clear();
4251 mReconfigured = false;
4252 }
4253
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004254 if (nextRequest != NULL) {
4255 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004256 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4257 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004258
4259 // Since RequestThread::clear() removes buffers from the input stream,
4260 // get the right buffer here before unlocking mRequestLock
4261 if (nextRequest->mInputStream != NULL) {
4262 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
4263 if (res != OK) {
4264 // Can't get input buffer from gralloc queue - this could be due to
4265 // disconnected queue or other producer misbehavior, so not a fatal
4266 // error
4267 ALOGE("%s: Can't get input buffer, skipping request:"
4268 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004269
4270 sp<NotificationListener> listener = mListener.promote();
4271 if (listener != NULL) {
4272 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004273 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004274 nextRequest->mResultExtras);
4275 }
4276 return NULL;
4277 }
4278 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004279 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004280
4281 handleAePrecaptureCancelRequest(nextRequest);
4282
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004283 return nextRequest;
4284}
4285
4286bool Camera3Device::RequestThread::waitIfPaused() {
4287 status_t res;
4288 Mutex::Autolock l(mPauseLock);
4289 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004290 if (mPaused == false) {
4291 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004292 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
4293 // Let the tracker know
4294 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4295 if (statusTracker != 0) {
4296 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4297 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004298 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004299
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004300 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004301 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004302 return true;
4303 }
4304 }
4305 // We don't set mPaused to false here, because waitForNextRequest needs
4306 // to further manage the paused state in case of starvation.
4307 return false;
4308}
4309
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004310void Camera3Device::RequestThread::unpauseForNewRequests() {
4311 // With work to do, mark thread as unpaused.
4312 // If paused by request (setPaused), don't resume, to avoid
4313 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004314 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004315 Mutex::Autolock p(mPauseLock);
4316 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004317 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4318 if (mPaused) {
4319 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4320 if (statusTracker != 0) {
4321 statusTracker->markComponentActive(mStatusId);
4322 }
4323 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004324 mPaused = false;
4325 }
4326}
4327
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004328void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4329 sp<Camera3Device> parent = mParent.promote();
4330 if (parent != NULL) {
4331 va_list args;
4332 va_start(args, fmt);
4333
4334 parent->setErrorStateV(fmt, args);
4335
4336 va_end(args);
4337 }
4338}
4339
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004340status_t Camera3Device::RequestThread::insertTriggers(
4341 const sp<CaptureRequest> &request) {
4342
4343 Mutex::Autolock al(mTriggerMutex);
4344
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004345 sp<Camera3Device> parent = mParent.promote();
4346 if (parent == NULL) {
4347 CLOGE("RequestThread: Parent is gone");
4348 return DEAD_OBJECT;
4349 }
4350
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004351 CameraMetadata &metadata = request->mSettings;
4352 size_t count = mTriggerMap.size();
4353
4354 for (size_t i = 0; i < count; ++i) {
4355 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004356 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004357
4358 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4359 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4360 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004361 if (isAeTrigger) {
4362 request->mResultExtras.precaptureTriggerId = triggerId;
4363 mCurrentPreCaptureTriggerId = triggerId;
4364 } else {
4365 request->mResultExtras.afTriggerId = triggerId;
4366 mCurrentAfTriggerId = triggerId;
4367 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004368 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
4369 continue; // Trigger ID tag is deprecated since device HAL 3.2
4370 }
4371 }
4372
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004373 camera_metadata_entry entry = metadata.find(tag);
4374
4375 if (entry.count > 0) {
4376 /**
4377 * Already has an entry for this trigger in the request.
4378 * Rewrite it with our requested trigger value.
4379 */
4380 RequestTrigger oldTrigger = trigger;
4381
4382 oldTrigger.entryValue = entry.data.u8[0];
4383
4384 mTriggerReplacedMap.add(tag, oldTrigger);
4385 } else {
4386 /**
4387 * More typical, no trigger entry, so we just add it
4388 */
4389 mTriggerRemovedMap.add(tag, trigger);
4390 }
4391
4392 status_t res;
4393
4394 switch (trigger.getTagType()) {
4395 case TYPE_BYTE: {
4396 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4397 res = metadata.update(tag,
4398 &entryValue,
4399 /*count*/1);
4400 break;
4401 }
4402 case TYPE_INT32:
4403 res = metadata.update(tag,
4404 &trigger.entryValue,
4405 /*count*/1);
4406 break;
4407 default:
4408 ALOGE("%s: Type not supported: 0x%x",
4409 __FUNCTION__,
4410 trigger.getTagType());
4411 return INVALID_OPERATION;
4412 }
4413
4414 if (res != OK) {
4415 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4416 ", value %d", __FUNCTION__, trigger.getTagName(),
4417 trigger.entryValue);
4418 return res;
4419 }
4420
4421 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4422 trigger.getTagName(),
4423 trigger.entryValue);
4424 }
4425
4426 mTriggerMap.clear();
4427
4428 return count;
4429}
4430
4431status_t Camera3Device::RequestThread::removeTriggers(
4432 const sp<CaptureRequest> &request) {
4433 Mutex::Autolock al(mTriggerMutex);
4434
4435 CameraMetadata &metadata = request->mSettings;
4436
4437 /**
4438 * Replace all old entries with their old values.
4439 */
4440 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4441 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4442
4443 status_t res;
4444
4445 uint32_t tag = trigger.metadataTag;
4446 switch (trigger.getTagType()) {
4447 case TYPE_BYTE: {
4448 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4449 res = metadata.update(tag,
4450 &entryValue,
4451 /*count*/1);
4452 break;
4453 }
4454 case TYPE_INT32:
4455 res = metadata.update(tag,
4456 &trigger.entryValue,
4457 /*count*/1);
4458 break;
4459 default:
4460 ALOGE("%s: Type not supported: 0x%x",
4461 __FUNCTION__,
4462 trigger.getTagType());
4463 return INVALID_OPERATION;
4464 }
4465
4466 if (res != OK) {
4467 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4468 ", trigger value %d", __FUNCTION__,
4469 trigger.getTagName(), trigger.entryValue);
4470 return res;
4471 }
4472 }
4473 mTriggerReplacedMap.clear();
4474
4475 /**
4476 * Remove all new entries.
4477 */
4478 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4479 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4480 status_t res = metadata.erase(trigger.metadataTag);
4481
4482 if (res != OK) {
4483 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4484 ", trigger value %d", __FUNCTION__,
4485 trigger.getTagName(), trigger.entryValue);
4486 return res;
4487 }
4488 }
4489 mTriggerRemovedMap.clear();
4490
4491 return OK;
4492}
4493
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004494status_t Camera3Device::RequestThread::addDummyTriggerIds(
4495 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004496 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004497 static const int32_t dummyTriggerId = 1;
4498 status_t res;
4499
4500 CameraMetadata &metadata = request->mSettings;
4501
4502 // If AF trigger is active, insert a dummy AF trigger ID if none already
4503 // exists
4504 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4505 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4506 if (afTrigger.count > 0 &&
4507 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4508 afId.count == 0) {
4509 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
4510 if (res != OK) return res;
4511 }
4512
4513 // If AE precapture trigger is active, insert a dummy precapture trigger ID
4514 // if none already exists
4515 camera_metadata_entry pcTrigger =
4516 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4517 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4518 if (pcTrigger.count > 0 &&
4519 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4520 pcId.count == 0) {
4521 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
4522 &dummyTriggerId, 1);
4523 if (res != OK) return res;
4524 }
4525
4526 return OK;
4527}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004528
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004529/**
4530 * PreparerThread inner class methods
4531 */
4532
4533Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004534 Thread(/*canCallJava*/false), mListener(nullptr),
4535 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004536}
4537
4538Camera3Device::PreparerThread::~PreparerThread() {
4539 Thread::requestExitAndWait();
4540 if (mCurrentStream != nullptr) {
4541 mCurrentStream->cancelPrepare();
4542 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4543 mCurrentStream.clear();
4544 }
4545 clear();
4546}
4547
Ruben Brunkc78ac262015-08-13 17:58:46 -07004548status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004549 status_t res;
4550
4551 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004552 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004553
Ruben Brunkc78ac262015-08-13 17:58:46 -07004554 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004555 if (res == OK) {
4556 // No preparation needed, fire listener right off
4557 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004558 if (listener != NULL) {
4559 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004560 }
4561 return OK;
4562 } else if (res != NOT_ENOUGH_DATA) {
4563 return res;
4564 }
4565
4566 // Need to prepare, start up thread if necessary
4567 if (!mActive) {
4568 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4569 // isn't running
4570 Thread::requestExitAndWait();
4571 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4572 if (res != OK) {
4573 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004574 if (listener != NULL) {
4575 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004576 }
4577 return res;
4578 }
4579 mCancelNow = false;
4580 mActive = true;
4581 ALOGV("%s: Preparer stream started", __FUNCTION__);
4582 }
4583
4584 // queue up the work
4585 mPendingStreams.push_back(stream);
4586 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4587
4588 return OK;
4589}
4590
4591status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004592 Mutex::Autolock l(mLock);
4593
4594 for (const auto& stream : mPendingStreams) {
4595 stream->cancelPrepare();
4596 }
4597 mPendingStreams.clear();
4598 mCancelNow = true;
4599
4600 return OK;
4601}
4602
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004603void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004604 Mutex::Autolock l(mLock);
4605 mListener = listener;
4606}
4607
4608bool Camera3Device::PreparerThread::threadLoop() {
4609 status_t res;
4610 {
4611 Mutex::Autolock l(mLock);
4612 if (mCurrentStream == nullptr) {
4613 // End thread if done with work
4614 if (mPendingStreams.empty()) {
4615 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4616 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4617 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4618 mActive = false;
4619 return false;
4620 }
4621
4622 // Get next stream to prepare
4623 auto it = mPendingStreams.begin();
4624 mCurrentStream = *it;
4625 mPendingStreams.erase(it);
4626 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4627 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4628 } else if (mCancelNow) {
4629 mCurrentStream->cancelPrepare();
4630 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4631 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4632 mCurrentStream.clear();
4633 mCancelNow = false;
4634 return true;
4635 }
4636 }
4637
4638 res = mCurrentStream->prepareNextBuffer();
4639 if (res == NOT_ENOUGH_DATA) return true;
4640 if (res != OK) {
4641 // Something bad happened; try to recover by cancelling prepare and
4642 // signalling listener anyway
4643 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4644 mCurrentStream->getId(), res, strerror(-res));
4645 mCurrentStream->cancelPrepare();
4646 }
4647
4648 // This stream has finished, notify listener
4649 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004650 sp<NotificationListener> listener = mListener.promote();
4651 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004652 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4653 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004654 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004655 }
4656
4657 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4658 mCurrentStream.clear();
4659
4660 return true;
4661}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004662
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004663/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004664 * Static callback forwarding methods from HAL to instance
4665 */
4666
4667void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
4668 const camera3_capture_result *result) {
4669 Camera3Device *d =
4670 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07004671
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004672 d->processCaptureResult(result);
4673}
4674
4675void Camera3Device::sNotify(const camera3_callback_ops *cb,
4676 const camera3_notify_msg *msg) {
4677 Camera3Device *d =
4678 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
4679 d->notify(msg);
4680}
4681
4682}; // namespace android